From patchwork Fri Apr 7 23:17:59 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: 9670649 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 99DE360364 for ; Fri, 7 Apr 2017 23:19:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8ADD326E54 for ; Fri, 7 Apr 2017 23:19:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7F8FD28614; Fri, 7 Apr 2017 23:19:37 +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 1E64526E54 for ; Fri, 7 Apr 2017 23:19:37 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 05C432194233D; Fri, 7 Apr 2017 16:19:37 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (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 0355D2194235E for ; Fri, 7 Apr 2017 16:19:35 -0700 (PDT) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP; 07 Apr 2017 16:19:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.37,168,1488873600"; d="scan'208"; a="1117109707" Received: from omniknight.lm.intel.com ([10.232.112.53]) by orsmga001.jf.intel.com with ESMTP; 07 Apr 2017 16:19:35 -0700 From: Vishal Verma To: Subject: [ndctl PATCH v4 2/6] libndctl: add a ndctl_namespace_disable_safe() API Date: Fri, 7 Apr 2017 17:17:59 -0600 Message-Id: <20170407231803.14936-3-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170407231803.14936-1-vishal.l.verma@intel.com> References: <20170407231803.14936-1-vishal.l.verma@intel.com> X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.22 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 Signed-off-by: Dan Williams --- 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 ae029c5..a3481b1 100644 --- a/ndctl/lib/libndctl.c +++ b/ndctl/lib/libndctl.c @@ -3346,6 +3346,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 705ec4c..9d2a568 100644 --- a/ndctl/lib/libndctl.sym +++ b/ndctl/lib/libndctl.sym @@ -172,6 +172,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 586eb26..bcc569c 100644 --- a/ndctl/libndctl.h.in +++ b/ndctl/libndctl.h.in @@ -485,6 +485,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);