From patchwork Sat Mar 4 06:13:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 9603779 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 6EC6D601D2 for ; Sat, 4 Mar 2017 06:14:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6056827F94 for ; Sat, 4 Mar 2017 06:14:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 558082861F; Sat, 4 Mar 2017 06:14:29 +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 E296027F94 for ; Sat, 4 Mar 2017 06:14:28 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id D8A70802E0; Fri, 3 Mar 2017 22:14:28 -0800 (PST) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) (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 79992802D3 for ; Fri, 3 Mar 2017 22:14:25 -0800 (PST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Mar 2017 22:14:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,239,1484035200"; d="scan'208";a="55786453" Received: from omniknight.lm.intel.com ([10.232.112.53]) by orsmga002.jf.intel.com with ESMTP; 03 Mar 2017 22:14:24 -0800 From: Vishal Verma To: Subject: [ndctl PATCH v3 2/7] libndctl: add a ndctl_namespace_disable_safe() API Date: Fri, 3 Mar 2017 23:13:02 -0700 Message-Id: <20170304061307.13530-3-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170304061307.13530-1-vishal.l.verma@intel.com> References: <20170304061307.13530-1-vishal.l.verma@intel.com> 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: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP Disabling a namespace which has a filesystem mounted on it is unsafe as filesystems are not prepared for a block device to be yanked from under them. The destroy_namespace routine checked for an active mount by performing an O_EXCL open of the backing block device, but many other callers of ndctl_namespace_disable* could benefit from this checking. Codify the mounted filesystem check in a new libndctl API - ndctl_namespace_disable_safe(), and use it for the destroy/disable namespace ndctl commands as well as the upcoming check-namespace command. Cc: Dan Williams Signed-off-by: Vishal Verma --- ndctl/builtin-xaction-namespace.c | 46 +++++++-------------------------------- ndctl/lib/libndctl.c | 44 +++++++++++++++++++++++++++++++++++++ ndctl/lib/libndctl.sym | 1 + ndctl/libndctl.h.in | 1 + 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/ndctl/builtin-xaction-namespace.c b/ndctl/builtin-xaction-namespace.c index 46d651e..d6b0c37 100644 --- a/ndctl/builtin-xaction-namespace.c +++ b/ndctl/builtin-xaction-namespace.c @@ -731,10 +731,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); - const char *bdev = NULL; - bool dax_active = false; - char path[50]; - int fd, rc; + int rc; if (ndctl_region_get_ro(region)) { error("%s: read-only, re-configuration disabled\n", @@ -742,42 +739,15 @@ static int namespace_destroy(struct ndctl_region *region, return -ENXIO; } - if (pfn && ndctl_pfn_is_enabled(pfn)) - bdev = ndctl_pfn_get_block_device(pfn); - else if (dax && ndctl_dax_is_enabled(dax)) - dax_active = true; - else if (btt && ndctl_btt_is_enabled(btt)) - bdev = ndctl_btt_get_block_device(btt); - else if (ndctl_namespace_is_enabled(ndns)) - bdev = ndctl_namespace_get_block_device(ndns); - - if ((bdev || dax_active) && !force) { + if (ndctl_namespace_is_active(ndns) && !force) { error("%s is active, specify --force for re-configuration\n", devname); return -EBUSY; - } else if (bdev) { - sprintf(path, "/dev/%s", bdev); - fd = open(path, O_RDWR|O_EXCL); - if (fd >= 0) { - /* - * Got it, now block new mounts while we have it - * pinned. - */ - ndctl_namespace_disable_invalidate(ndns); - close(fd); - } else { - /* - * Yes, TOCTOU hole, but if you're racing namespace - * creation you have other problems, and there's nothing - * stopping the !bdev case from racing to mount an fs or - * re-enabling the namepace. - */ - error("%s: %s failed exlusive open: %s\n", - devname, bdev, strerror(errno)); - return -errno; - } - } else if (dax_active) - ndctl_namespace_disable_invalidate(ndns); + } else { + rc = ndctl_namespace_disable_safe(ndns); + if (rc) + return rc; + } if (pfn || btt || dax) { rc = zero_info_block(ndns); @@ -869,7 +839,7 @@ static int do_xaction_namespace(const char *namespace, continue; switch (action) { case ACTION_DISABLE: - rc = ndctl_namespace_disable_invalidate(ndns); + rc = ndctl_namespace_disable_safe(ndns); break; case ACTION_ENABLE: rc = ndctl_namespace_enable(ndns); diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c index 8396e48..a53e953 100644 --- a/ndctl/lib/libndctl.c +++ b/ndctl/lib/libndctl.c @@ -3320,6 +3320,50 @@ NDCTL_EXPORT int ndctl_namespace_disable_invalidate(struct ndctl_namespace *ndns return ndctl_namespace_disable(ndns); } +NDCTL_EXPORT int ndctl_namespace_disable_safe(struct ndctl_namespace *ndns) +{ + const char *devname = ndctl_namespace_get_devname(ndns); + struct ndctl_ctx *ctx = ndctl_namespace_get_ctx(ndns); + struct ndctl_pfn *pfn = ndctl_namespace_get_pfn(ndns); + struct ndctl_btt *btt = ndctl_namespace_get_btt(ndns); + const char *bdev = NULL; + char path[50]; + int fd; + + if (pfn && ndctl_pfn_is_enabled(pfn)) + bdev = ndctl_pfn_get_block_device(pfn); + else if (btt && ndctl_btt_is_enabled(btt)) + bdev = ndctl_btt_get_block_device(btt); + else if (ndctl_namespace_is_enabled(ndns)) + bdev = ndctl_namespace_get_block_device(ndns); + + if (bdev) { + sprintf(path, "/dev/%s", bdev); + fd = open(path, O_RDWR|O_EXCL); + if (fd >= 0) { + /* + * Got it, now block new mounts while we have it + * pinned. + */ + ndctl_namespace_disable_invalidate(ndns); + close(fd); + } else { + /* + * Yes, TOCTOU hole, but if you're racing namespace + * creation you have other problems, and there's nothing + * stopping the !bdev case from racing to mount an fs or + * re-enabling the namepace. + */ + dbg(ctx, "%s: %s failed exclusive open: %s\n", + devname, bdev, strerror(errno)); + return -errno; + } + } else + ndctl_namespace_disable_invalidate(ndns); + + return 0; +} + static int pmem_namespace_is_configured(struct ndctl_namespace *ndns) { if (ndctl_namespace_get_size(ndns) < ND_MIN_NAMESPACE_SIZE) diff --git a/ndctl/lib/libndctl.sym b/ndctl/lib/libndctl.sym index 5f47a22..2163a09 100644 --- a/ndctl/lib/libndctl.sym +++ b/ndctl/lib/libndctl.sym @@ -171,6 +171,7 @@ global: ndctl_namespace_enable; ndctl_namespace_disable; ndctl_namespace_disable_invalidate; + ndctl_namespace_disable_safe; ndctl_namespace_is_active; ndctl_namespace_is_valid; ndctl_namespace_is_configured; diff --git a/ndctl/libndctl.h.in b/ndctl/libndctl.h.in index c70215c..06e6d49 100644 --- a/ndctl/libndctl.h.in +++ b/ndctl/libndctl.h.in @@ -484,6 +484,7 @@ int ndctl_namespace_is_enabled(struct ndctl_namespace *ndns); int ndctl_namespace_enable(struct ndctl_namespace *ndns); int ndctl_namespace_disable(struct ndctl_namespace *ndns); int ndctl_namespace_disable_invalidate(struct ndctl_namespace *ndns); +int ndctl_namespace_disable_safe(struct ndctl_namespace *ndns); bool ndctl_namespace_is_active(struct ndctl_namespace *ndns); int ndctl_namespace_is_valid(struct ndctl_namespace *ndns); int ndctl_namespace_is_configured(struct ndctl_namespace *ndns);