From patchwork Wed Aug 22 22:22:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 10573293 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B3BCC17E0 for ; Wed, 22 Aug 2018 22:23:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A27052BC0F for ; Wed, 22 Aug 2018 22:23:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9675D2BC19; Wed, 22 Aug 2018 22:23:02 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, 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 1C5352BC11 for ; Wed, 22 Aug 2018 22:23:02 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id B17C521BADAB2; Wed, 22 Aug 2018 15:23:01 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.20; helo=mga02.intel.com; envelope-from=vishal.l.verma@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (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 C2A00210FC369 for ; Wed, 22 Aug 2018 15:23:00 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Aug 2018 15:23:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,276,1531810800"; d="scan'208";a="226889033" Received: from vverma7-desk1.lm.intel.com ([10.232.112.133]) by orsmga004.jf.intel.com with ESMTP; 22 Aug 2018 15:22:53 -0700 From: Vishal Verma To: Subject: [ndctl PATCH 1/2] ndctl, destroy-namespace: check for an already-zeroed info block Date: Wed, 22 Aug 2018 16:22:48 -0600 Message-Id: <20180822222249.30838-2-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180822222249.30838-1-vishal.l.verma@intel.com> References: <20180822222249.30838-1-vishal.l.verma@intel.com> X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Steve Scargal MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP in zero_info_block, check if the info block is already all-zeroes, and if so, don't write zeroes. This is in preparation for the fixes to destroy-namespace accounting where the number of namespaces 'destroyed' is often incorrect. Cc: Steve Scargall Link: https://github.com/pmem/ndctl/issues/41 Signed-off-by: Vishal Verma --- ndctl/namespace.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/ndctl/namespace.c b/ndctl/namespace.c index 510553c..65e8f7c 100644 --- a/ndctl/namespace.c +++ b/ndctl/namespace.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -786,23 +787,35 @@ static int namespace_create(struct ndctl_region *region) return setup_namespace(region, ndns, &p); } +/* + * Return convention: + * rc < 0 : Error while zeroing, propagate forward + * rc == 0 : Successfully cleared the info block, report as destroyed + * rc > 0 : skipped, do not count + */ static int zero_info_block(struct ndctl_namespace *ndns) { const char *devname = ndctl_namespace_get_devname(ndns); - int fd, rc = -ENXIO; - void *buf = NULL; + int fd, rc = -ENXIO, info_size = 8192; + void *buf = NULL, *read_buf = NULL; char path[50]; ndctl_namespace_set_raw_mode(ndns, 1); rc = ndctl_namespace_enable(ndns); if (rc < 0) { debug("%s failed to enable for zeroing, continuing\n", devname); - rc = 0; + rc = 1; goto out; } - if (posix_memalign(&buf, 4096, 4096) != 0) - return -ENXIO; + if (posix_memalign(&buf, 4096, info_size) != 0) { + rc = -ENOMEM; + goto out; + } + if (posix_memalign(&read_buf, 4096, info_size) != 0) { + rc = -ENOMEM; + goto out; + } sprintf(path, "/dev/%s", ndctl_namespace_get_block_device(ndns)); fd = open(path, O_RDWR|O_DIRECT|O_EXCL); @@ -812,18 +825,30 @@ static int zero_info_block(struct ndctl_namespace *ndns) goto out; } - memset(buf, 0, 4096); - rc = pwrite(fd, buf, 4096, 4096); - if (rc < 4096) { + memset(buf, 0, info_size); + rc = pread(fd, read_buf, info_size, 0); + if (rc < info_size) { + debug("%s: failed to read info block, continuing\n", + devname); + } + if (memcmp(buf, read_buf, info_size) == 0) { + rc = 1; + goto out_close; + } + + rc = pwrite(fd, buf, info_size, 0); + if (rc < info_size) { debug("%s: failed to zero info block %s\n", devname, path); rc = -ENXIO; } else rc = 0; + out_close: close(fd); out: ndctl_namespace_set_raw_mode(ndns, 0); ndctl_namespace_disable_invalidate(ndns); + free(read_buf); free(buf); return rc; } @@ -857,7 +882,7 @@ static int namespace_destroy(struct ndctl_region *region, if (pfn || btt || dax) { rc = zero_info_block(ndns); - if (rc) + if (rc < 0) return rc; } From patchwork Wed Aug 22 22:22:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 10573295 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 643255A4 for ; Wed, 22 Aug 2018 22:23:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 52D1B2BBA7 for ; Wed, 22 Aug 2018 22:23:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 46FC32BC11; Wed, 22 Aug 2018 22:23:03 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, 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 0CD0D2BBA7 for ; Wed, 22 Aug 2018 22:23:02 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 9D8A02194D387; Wed, 22 Aug 2018 15:23:01 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.20; helo=mga02.intel.com; envelope-from=vishal.l.verma@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (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 AB924210FC0D5 for ; Wed, 22 Aug 2018 15:23:00 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Aug 2018 15:23:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,276,1531810800"; d="scan'208";a="226889036" Received: from vverma7-desk1.lm.intel.com ([10.232.112.133]) by orsmga004.jf.intel.com with ESMTP; 22 Aug 2018 15:22:55 -0700 From: Vishal Verma To: Subject: [ndctl PATCH 2/2] ndctl, namespace: rework namespace action accounting Date: Wed, 22 Aug 2018 16:22:49 -0600 Message-Id: <20180822222249.30838-3-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20180822222249.30838-1-vishal.l.verma@intel.com> References: <20180822222249.30838-1-vishal.l.verma@intel.com> X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Steve Scargal MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP Make the count calculations consistent for all namespace actions by introducing a new parameter to do_xaction_namespace() which explicitly keeps a count of successful operations. For destroy-namespace, correctly propagate errors so that the above accounting works as expected. For legacy (labelless or emulated) namespaces, if we perform a destructive action such as writing zeroes to the info block area, acccount for them in the destroy count, but otherwise consider them as 'skipped' since they cannot be truly 'destroyed'. Cc: Steve Scargall Link: https://github.com/pmem/ndctl/issues/41 Signed-off-by: Vishal Verma --- ndctl/namespace.c | 154 +++++++++++++++++++++++++++++------------------------- 1 file changed, 82 insertions(+), 72 deletions(-) diff --git a/ndctl/namespace.c b/ndctl/namespace.c index 65e8f7c..b6f1230 100644 --- a/ndctl/namespace.c +++ b/ndctl/namespace.c @@ -860,6 +860,7 @@ static int namespace_destroy(struct ndctl_region *region, struct ndctl_pfn *pfn = ndctl_namespace_get_pfn(ndns); struct ndctl_dax *dax = ndctl_namespace_get_dax(ndns); struct ndctl_btt *btt = ndctl_namespace_get_btt(ndns); + bool did_zero = false; int rc; if (ndctl_region_get_ro(region)) { @@ -884,13 +885,33 @@ static int namespace_destroy(struct ndctl_region *region, rc = zero_info_block(ndns); if (rc < 0) return rc; + if (rc == 0) + did_zero = true; + } + + switch (ndctl_namespace_get_type(ndns)) { + case ND_DEVICE_NAMESPACE_PMEM: + case ND_DEVICE_NAMESPACE_BLK: + break; + default: + /* + * for legacy namespaces, we we did any info block + * zeroing, we need "processed" to be incremented + * but otherwise we are skipping in the count + */ + if (did_zero) + rc = 0; + else + rc = 1; + goto out; } rc = ndctl_namespace_delete(ndns); if (rc) debug("%s: failed to reclaim\n", devname); - return 0; +out: + return rc; } static int enable_labels(struct ndctl_region *region) @@ -987,7 +1008,7 @@ static int namespace_reconfig(struct ndctl_region *region, return rc; rc = namespace_destroy(region, ndns); - if (rc) + if (rc < 0) return rc; /* check if we can enable labels on this region */ @@ -1012,13 +1033,16 @@ int namespace_check(struct ndctl_namespace *ndns, bool verbose, bool force, bool repair, bool logfix); static int do_xaction_namespace(const char *namespace, - enum device_action action, struct ndctl_ctx *ctx) + enum device_action action, struct ndctl_ctx *ctx, + int *processed) { struct ndctl_namespace *ndns, *_n; - int rc = -ENXIO, success = 0; struct ndctl_region *region; const char *ndns_name; struct ndctl_bus *bus; + int rc = -ENXIO; + + *processed = 0; if (!namespace && action != ACTION_CREATE) return rc; @@ -1052,7 +1076,7 @@ static int do_xaction_namespace(const char *namespace, if (rc == -EAGAIN) continue; if (rc == 0) - rc = 1; + *processed = 1; return rc; } ndctl_namespace_foreach_safe(region, ndns, _n) { @@ -1064,36 +1088,43 @@ static int do_xaction_namespace(const char *namespace, switch (action) { case ACTION_DISABLE: rc = ndctl_namespace_disable_safe(ndns); + if (rc == 0) + (*processed)++; break; case ACTION_ENABLE: rc = ndctl_namespace_enable(ndns); + if (rc >= 0) { + (*processed)++; + rc = 0; + } break; case ACTION_DESTROY: rc = namespace_destroy(region, ndns); + if (rc == 0) + (*processed)++; + /* return success if skipped */ + if (rc > 0) + rc = 0; break; case ACTION_CHECK: rc = namespace_check(ndns, verbose, force, repair, logfix); - if (rc < 0) - return rc; + if (rc == 0) + (*processed)++; break; case ACTION_CREATE: rc = namespace_reconfig(region, ndns); - if (rc < 0) - return rc; - return 1; + if (rc == 0) + *processed = 1; + return rc; default: rc = -EINVAL; break; } - if (rc >= 0) - success++; } } } - if (success) - return success; return rc; } @@ -1102,20 +1133,16 @@ int cmd_disable_namespace(int argc, const char **argv, void *ctx) char *xable_usage = "ndctl disable-namespace []"; const char *namespace = parse_namespace_options(argc, argv, ACTION_DISABLE, base_options, xable_usage); - int disabled = do_xaction_namespace(namespace, ACTION_DISABLE, ctx); + int disabled, rc; - if (disabled < 0) { + rc = do_xaction_namespace(namespace, ACTION_DISABLE, ctx, &disabled); + if (rc < 0) fprintf(stderr, "error disabling namespaces: %s\n", - strerror(-disabled)); - return disabled; - } else if (disabled == 0) { - fprintf(stderr, "disabled 0 namespaces\n"); - return -ENXIO; - } else { - fprintf(stderr, "disabled %d namespace%s\n", disabled, - disabled > 1 ? "s" : ""); - return 0; - } + strerror(-rc)); + + fprintf(stderr, "disabled %d namespace%s\n", disabled, + disabled == 1 ? "" : "s"); + return rc; } int cmd_enable_namespace(int argc, const char **argv, void *ctx) @@ -1123,20 +1150,15 @@ int cmd_enable_namespace(int argc, const char **argv, void *ctx) char *xable_usage = "ndctl enable-namespace []"; const char *namespace = parse_namespace_options(argc, argv, ACTION_ENABLE, base_options, xable_usage); - int enabled = do_xaction_namespace(namespace, ACTION_ENABLE, ctx); + int enabled, rc; - if (enabled < 0) { + rc = do_xaction_namespace(namespace, ACTION_ENABLE, ctx, &enabled); + if (rc < 0) fprintf(stderr, "error enabling namespaces: %s\n", - strerror(-enabled)); - return enabled; - } else if (enabled == 0) { - fprintf(stderr, "enabled 0 namespaces\n"); - return 0; - } else { - fprintf(stderr, "enabled %d namespace%s\n", enabled, - enabled > 1 ? "s" : ""); - return 0; - } + strerror(-rc)); + fprintf(stderr, "enabled %d namespace%s\n", enabled, + enabled == 1 ? "" : "s"); + return rc; } int cmd_create_namespace(int argc, const char **argv, void *ctx) @@ -1144,9 +1166,10 @@ int cmd_create_namespace(int argc, const char **argv, void *ctx) char *xable_usage = "ndctl create-namespace []"; const char *namespace = parse_namespace_options(argc, argv, ACTION_CREATE, create_options, xable_usage); - int created = do_xaction_namespace(namespace, ACTION_CREATE, ctx); + int created, rc; - if (created < 1 && param.do_scan) { + rc = do_xaction_namespace(namespace, ACTION_CREATE, ctx, &created); + if (rc < 0 && created < 1 && param.do_scan) { /* * In the default scan case we try pmem first and then * fallback to blk before giving up. @@ -1154,19 +1177,17 @@ int cmd_create_namespace(int argc, const char **argv, void *ctx) memset(¶m, 0, sizeof(param)); param.type = "blk"; set_defaults(ACTION_CREATE); - created = do_xaction_namespace(NULL, ACTION_CREATE, ctx); + rc = do_xaction_namespace(NULL, ACTION_CREATE, ctx, &created); } - if (created < 0 || (!namespace && created < 1)) { + if (rc < 0 || (!namespace && created < 1)) { fprintf(stderr, "failed to %s namespace: %s\n", namespace - ? "reconfigure" : "create", strerror(-created)); + ? "reconfigure" : "create", strerror(-rc)); if (!namespace) - created = -ENODEV; + rc = -ENODEV; } - if (created < 0) - return created; - return 0; + return rc; } int cmd_destroy_namespace(int argc , const char **argv, void *ctx) @@ -1174,20 +1195,15 @@ int cmd_destroy_namespace(int argc , const char **argv, void *ctx) char *xable_usage = "ndctl destroy-namespace []"; const char *namespace = parse_namespace_options(argc, argv, ACTION_DESTROY, destroy_options, xable_usage); - int destroyed = do_xaction_namespace(namespace, ACTION_DESTROY, ctx); + int destroyed, rc; - if (destroyed < 0) { + rc = do_xaction_namespace(namespace, ACTION_DESTROY, ctx, &destroyed); + if (rc < 0) fprintf(stderr, "error destroying namespaces: %s\n", - strerror(-destroyed)); - return destroyed; - } else if (destroyed == 0) { - fprintf(stderr, "destroyed 0 namespaces\n"); - return 0; - } else { - fprintf(stderr, "destroyed %d namespace%s\n", destroyed, - destroyed > 1 ? "s" : ""); - return 0; - } + strerror(-rc)); + fprintf(stderr, "destroyed %d namespace%s\n", destroyed, + destroyed == 1 ? "" : "s"); + return rc; } int cmd_check_namespace(int argc , const char **argv, void *ctx) @@ -1195,19 +1211,13 @@ int cmd_check_namespace(int argc , const char **argv, void *ctx) char *xable_usage = "ndctl check-namespace []"; const char *namespace = parse_namespace_options(argc, argv, ACTION_CHECK, check_options, xable_usage); - int checked; + int checked, rc; - checked = do_xaction_namespace(namespace, ACTION_CHECK, ctx); - if (checked < 0) { + rc = do_xaction_namespace(namespace, ACTION_CHECK, ctx, &checked); + if (rc < 0) fprintf(stderr, "error checking namespaces: %s\n", - strerror(-checked)); - return checked; - } else if (checked == 0) { - fprintf(stderr, "checked 0 namespaces\n"); - return 0; - } else { - fprintf(stderr, "checked %d namespace%s\n", checked, - checked > 1 ? "s" : ""); - return 0; - } + strerror(-rc)); + fprintf(stderr, "checked %d namespace%s\n", checked, + checked == 1 ? "" : "s"); + return rc; }