From patchwork Thu Apr 26 18:17:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 10366621 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 977896032C for ; Thu, 26 Apr 2018 18:17:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87FFF291BA for ; Thu, 26 Apr 2018 18:17:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7CF8D291BF; Thu, 26 Apr 2018 18:17:21 +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 6A5F4291BA for ; Thu, 26 Apr 2018 18:17:20 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 25417203B9003; Thu, 26 Apr 2018 11:17:15 -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=192.55.52.136; helo=mga12.intel.com; envelope-from=dave.jiang@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) (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 28614203B8FF3 for ; Thu, 26 Apr 2018 11:17:14 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Apr 2018 11:17:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,331,1520924400"; d="scan'208";a="223635346" Received: from djiang5-desk3.ch.intel.com ([143.182.136.93]) by fmsmga005.fm.intel.com with ESMTP; 26 Apr 2018 11:17:13 -0700 Subject: [PATCH v3 2/4] ndctl: convert namespace actions to use util_filter_walk() From: Dave Jiang To: vishal.l.verma@intel.com, dan.j.williams@intel.com Date: Thu, 26 Apr 2018 11:17:13 -0700 Message-ID: <152476663350.70161.1906714489582336946.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <152476632250.70161.9698682147434730585.stgit@djiang5-desk3.ch.intel.com> References: <152476632250.70161.9698682147434730585.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-nvdimm@lists.01.org Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP util_filter_walk() does the looping through of busses and regions. Removing duplicate code in namespace ops and provide filter functions so we can utilize util_filter_walk() and share common code. Signed-off-by: Dave Jiang --- ndctl/namespace.c | 164 ++++++++++++++++++++++++++++++----------------------- test/btt-check.sh | 2 - util/filter.c | 5 +- util/filter.h | 8 +++ 4 files changed, 105 insertions(+), 74 deletions(-) diff --git a/ndctl/namespace.c b/ndctl/namespace.c index e61f500c..d2a4fbaf 100644 --- a/ndctl/namespace.c +++ b/ndctl/namespace.c @@ -983,14 +983,92 @@ static int namespace_reconfig(struct ndctl_region *region, int namespace_check(struct ndctl_namespace *ndns, bool verbose, bool force, bool repair, bool logfix); +static bool filter_bus(struct ndctl_bus *bus, struct util_filter_ctx *ctx) +{ + return true; +} + +static bool filter_region(struct ndctl_region *region, + struct util_filter_ctx *ctx) +{ + struct ndns_filter_arg *nfa = ctx->ndns; + int rc = 0; + bool out = true; + + if (nfa->action == ACTION_CREATE && !nfa->namespace) { + if (nfa->rc == 1) + return false; + + rc = namespace_create(region); + if (rc != -EAGAIN) { + if (rc == 0) + rc = 1; + /* don't proceed in the filter loop */ + out = false; + } + nfa->rc = rc; + } + + return out; +} + +static void filter_namespace(struct ndctl_namespace *ndns, + struct util_filter_ctx *ctx) +{ + struct ndctl_region *region = ndctl_namespace_get_region(ndns); + struct ndns_filter_arg *nfa = ctx->ndns; + const char *ndns_name; + int rc; + + /* we have an error, don't do anything else */ + if (nfa->rc < 0) + return; + + ndns_name = ndctl_namespace_get_devname(ndns); + if (strcmp(nfa->namespace, "all") != 0 + && strcmp(nfa->namespace, ndns_name) != 0) { + return; + } + + switch (nfa->action) { + case ACTION_DISABLE: + rc = ndctl_namespace_disable_safe(ndns); + break; + case ACTION_ENABLE: + rc = ndctl_namespace_enable(ndns); + break; + case ACTION_DESTROY: + rc = namespace_destroy(region, ndns); + break; + case ACTION_CHECK: + rc = namespace_check(ndns, verbose, force, repair, logfix); + if (rc < 0) { + nfa->rc = rc; + return; + } + break; + case ACTION_CREATE: + rc = namespace_reconfig(region, ndns); + if (rc < 0) { + nfa->rc = rc; + return; + } + break; + default: + rc = -EINVAL; + break; + } + + if (rc >= 0) + nfa->rc++; +} + static int do_xaction_namespace(const char *namespace, enum device_action action, struct ndctl_ctx *ctx) { - 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; + struct util_filter_ctx fctx = { 0 }; + struct ndns_filter_arg nfa = { 0 }; if (!namespace && action != ACTION_CREATE) return rc; @@ -998,74 +1076,18 @@ static int do_xaction_namespace(const char *namespace, if (verbose) ndctl_set_log_priority(ctx, LOG_DEBUG); - ndctl_bus_foreach(ctx, bus) { - if (!util_bus_filter(bus, uf_param.bus)) - continue; - - ndctl_region_foreach(bus, region) { - if (!util_region_filter(region, uf_param.region)) - continue; - - if (uf_param.type) { - if (strcmp(uf_param.type, "pmem") == 0 - && ndctl_region_get_type(region) - == ND_DEVICE_REGION_PMEM) - /* pass */; - else if (strcmp(uf_param.type, "blk") == 0 - && ndctl_region_get_type(region) - == ND_DEVICE_REGION_BLK) - /* pass */; - else - continue; - } + fctx.filter_bus = filter_bus; + fctx.filter_region = filter_region; + fctx.filter_namespace = filter_namespace; + fctx.ndns = &nfa; + fctx.ndns->action = action; + fctx.ndns->namespace = namespace; - if (action == ACTION_CREATE && !namespace) { - rc = namespace_create(region); - if (rc == -EAGAIN) - continue; - if (rc == 0) - rc = 1; - return rc; - } - ndctl_namespace_foreach_safe(region, ndns, _n) { - ndns_name = ndctl_namespace_get_devname(ndns); - - if (strcmp(namespace, "all") != 0 - && strcmp(namespace, ndns_name) != 0) - continue; - switch (action) { - case ACTION_DISABLE: - rc = ndctl_namespace_disable_safe(ndns); - break; - case ACTION_ENABLE: - rc = ndctl_namespace_enable(ndns); - break; - case ACTION_DESTROY: - rc = namespace_destroy(region, ndns); - break; - case ACTION_CHECK: - rc = namespace_check(ndns, verbose, - force, repair, logfix); - if (rc < 0) - return rc; - break; - case ACTION_CREATE: - rc = namespace_reconfig(region, ndns); - if (rc < 0) - return rc; - return 1; - default: - rc = -EINVAL; - break; - } - if (rc >= 0) - success++; - } - } - } + rc = util_filter_walk(ctx, &fctx, &uf_param); + if (rc < 0) + return rc; - if (success) - return success; + rc = nfa.rc; return rc; } diff --git a/test/btt-check.sh b/test/btt-check.sh index 353d4375..50c1b592 100755 --- a/test/btt-check.sh +++ b/test/btt-check.sh @@ -1,4 +1,4 @@ -#!/bin/bash -E +#!/bin/bash -Ex # Copyright(c) 2015-2017 Intel Corporation. All rights reserved. # diff --git a/util/filter.c b/util/filter.c index 0d3cc02f..b7a4fbd6 100644 --- a/util/filter.c +++ b/util/filter.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -418,7 +419,7 @@ int util_filter_walk(struct ndctl_ctx *ctx, struct util_filter_ctx *fctx, } ndctl_region_foreach(bus, region) { - struct ndctl_namespace *ndns; + struct ndctl_namespace *ndns, *_n; if (!util_region_filter(region, param->region) || !util_region_filter_by_dimm(region, @@ -437,7 +438,7 @@ int util_filter_walk(struct ndctl_ctx *ctx, struct util_filter_ctx *fctx, if (!fctx->filter_region(region, fctx)) continue; - ndctl_namespace_foreach(region, ndns) { + ndctl_namespace_foreach_safe(region, ndns, _n) { enum ndctl_namespace_mode mode; if (!fctx->filter_namespace) diff --git a/util/filter.h b/util/filter.h index effda24b..ce3336a5 100644 --- a/util/filter.h +++ b/util/filter.h @@ -13,6 +13,7 @@ #ifndef _UTIL_FILTER_H_ #define _UTIL_FILTER_H_ #include +#include struct ndctl_bus *util_bus_filter(struct ndctl_bus *bus, const char *ident); struct ndctl_region *util_region_filter(struct ndctl_region *region, @@ -50,6 +51,12 @@ struct list_filter_arg { unsigned long flags; }; +struct ndns_filter_arg { + enum device_action action; + const char *namespace; + int rc; +}; + /* * struct util_filter_ctx - control and callbacks for util_filter_walk() * ->filter_bus() and ->filter_region() return bool because the @@ -67,6 +74,7 @@ struct util_filter_ctx { union { void *arg; struct list_filter_arg *list; + struct ndns_filter_arg *ndns; }; };