From patchwork Tue Jan 10 01:08:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 9506131 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BD59260231 for ; Tue, 10 Jan 2017 01:12:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BEF5228493 for ; Tue, 10 Jan 2017 01:12:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B3E862849C; Tue, 10 Jan 2017 01:12:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 42A3728493 for ; Tue, 10 Jan 2017 01:12:20 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 06FD781B06; Mon, 9 Jan 2017 17:12:20 -0800 (PST) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id BB2C381B17 for ; Mon, 9 Jan 2017 17:12:18 -0800 (PST) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga105.jf.intel.com with ESMTP; 09 Jan 2017 17:12:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.33,341,1477983600"; d="scan'208"; a="1110292635" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.14]) by fmsmga002.fm.intel.com with ESMTP; 09 Jan 2017 17:12:18 -0800 Subject: [PATCH 9/9] ndctl, create-namespace: enforce --size must be even multiple of interleave-width From: Dan Williams To: linux-nvdimm@lists.01.org Date: Mon, 09 Jan 2017 17:08:12 -0800 Message-ID: <148401049211.16217.1553285320779246446.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <148401044568.16217.6942951063735168785.stgit@dwillia2-desk3.amr.corp.intel.com> References: <148401044568.16217.6942951063735168785.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP If we don't check the size is properly aligned then the kernel will cause the create-namespace request to abort. When the size is not properly aligned suggest an alternative (rounded to the same units that user specified) and bail out early without disturbing the current configuration. Signed-off-by: Dan Williams --- ndctl/builtin-xaction-namespace.c | 34 +++++++++++++++++++++++++++++++++- util/size.c | 3 +-- util/size.h | 1 + 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/ndctl/builtin-xaction-namespace.c b/ndctl/builtin-xaction-namespace.c index eb289b0e392f..5a19729b6376 100644 --- a/ndctl/builtin-xaction-namespace.c +++ b/ndctl/builtin-xaction-namespace.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #ifdef HAVE_NDCTL_H @@ -398,6 +399,8 @@ static int validate_namespace_options(struct ndctl_region *region, struct ndctl_namespace *ndns, struct parsed_parameters *p) { const char *region_name = ndctl_region_get_devname(region); + unsigned long long size_align, units = 1; + unsigned int ways; int rc = 0; memset(p, 0, sizeof(*p)); @@ -408,7 +411,7 @@ static int validate_namespace_options(struct ndctl_region *region, } if (param.size) - p->size = parse_size64(param.size); + p->size = __parse_size64(param.size, &units); else if (ndns) p->size = ndctl_namespace_get_size(ndns); @@ -487,6 +490,35 @@ static int validate_namespace_options(struct ndctl_region *region, } else p->align = SZ_2M; + /* (re-)validate that the size satisfies the alignment */ + if (param.align) + size_align = p->align; + else + size_align = SZ_4K; + ways = ndctl_region_get_interleave_ways(region); + size_align = max(units, size_align) * ways; + if (p->size % size_align) { + char *suffix = ""; + + if (units == SZ_1K) + suffix = "K"; + else if (units == SZ_1M) + suffix = "M"; + else if (units == SZ_1G) + suffix = "G"; + else if (units == SZ_1T) + suffix = "T"; + + p->size /= size_align; + p->size++; + p->size *= size_align; + p->size /= units; + error("'--size=' must align to interleave-width: %d and alignment: %ld\n" + " did you intend --size=%lld%s?\n", ways, param.align + ? p->align : SZ_4K, p->size, suffix); + return -EINVAL; + } + if (param.sector_size) { struct ndctl_btt *btt; int num, i; diff --git a/util/size.c b/util/size.c index 6ad622781a27..da76bd94aebd 100644 --- a/util/size.c +++ b/util/size.c @@ -2,8 +2,7 @@ #include #include -static unsigned long long __parse_size64(const char *str, - unsigned long long *units) +unsigned long long __parse_size64(const char *str, unsigned long long *units) { unsigned long long val, check; char *end; diff --git a/util/size.h b/util/size.h index 634c92621bdc..776363fd19e9 100644 --- a/util/size.h +++ b/util/size.h @@ -12,5 +12,6 @@ #define SZ_1T 0x10000000000ULL unsigned long long parse_size64(const char *str); +unsigned long long __parse_size64(const char *str, unsigned long long *units); #endif /* _NDCTL_SIZE_H_ */