From patchwork Thu Jan 28 22:52:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 8154841 Return-Path: X-Original-To: patchwork-linux-nvdimm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id DAA019F38B for ; Thu, 28 Jan 2016 22:53:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D3E6420328 for ; Thu, 28 Jan 2016 22:53:06 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id AD6E320320 for ; Thu, 28 Jan 2016 22:53:05 +0000 (UTC) Received: from ml01.vlan14.01.org (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A27261A2473; Thu, 28 Jan 2016 14:53:05 -0800 (PST) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by ml01.01.org (Postfix) with ESMTP id 937D11A2453 for ; Thu, 28 Jan 2016 14:53:04 -0800 (PST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga101.jf.intel.com with ESMTP; 28 Jan 2016 14:53:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.22,360,1449561600"; d="scan'208";a="900549511" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.136]) by orsmga002.jf.intel.com with ESMTP; 28 Jan 2016 14:53:04 -0800 Subject: [ndctl PATCH 08/13] ndctl: refactor bus and region filtering to utility routines From: Dan Williams To: linux-nvdimm@lists.01.org Date: Thu, 28 Jan 2016 14:52:39 -0800 Message-ID: <20160128225239.17855.5237.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <20160128225157.17855.5190.stgit@dwillia2-desk3.amr.corp.intel.com> References: <20160128225157.17855.5190.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.17 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-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Before adding another use case for filtering by bus and region, move this functionality to new util_bus_filter() and util_region_filter() routines. Signed-off-by: Dan Williams --- Makefile.am | 3 ++ builtin-xable-region.c | 25 ++----------------- builtin-xaction-namespace.c | 36 +++------------------------- util/filter.c | 56 +++++++++++++++++++++++++++++++++++++++++++ util/filter.h | 6 +++++ 5 files changed, 71 insertions(+), 55 deletions(-) create mode 100644 util/filter.c create mode 100644 util/filter.h diff --git a/Makefile.am b/Makefile.am index baae4d0c1ee4..28437488ab93 100644 --- a/Makefile.am +++ b/Makefile.am @@ -74,7 +74,8 @@ ndctl_SOURCES = ndctl.c \ util/usage.c \ util/size.c \ util/strbuf.c \ - util/wrapper.c + util/wrapper.c \ + util/filter.c if ENABLE_TEST ndctl_SOURCES += test/libndctl.c \ diff --git a/builtin-xable-region.c b/builtin-xable-region.c index d21cb984c375..82bce63fda4b 100644 --- a/builtin-xable-region.c +++ b/builtin-xable-region.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include @@ -39,8 +39,6 @@ static const char *parse_region_options(int argc, const char **argv, static int do_xable_region(const char *region_arg, int (*xable_fn)(struct ndctl_region *)) { - unsigned long bus_id = ULONG_MAX, id; - const char *provider, *region_name; int rc = -ENXIO, success = 0; struct ndctl_region *region; struct ndctl_ctx *ctx; @@ -53,29 +51,12 @@ static int do_xable_region(const char *region_arg, if (rc < 0) goto out; - if (region_bus) { - char *end = NULL; - - bus_id = strtoul(region_bus, &end, 0); - if (end) - bus_id = ULONG_MAX; - } - ndctl_bus_foreach(ctx, bus) { - provider = ndctl_bus_get_provider(bus); - id = ndctl_bus_get_id(bus); - - if (bus_id < ULONG_MAX && bus_id != id) - continue; - else if (bus_id == ULONG_MAX && region_bus - && strcmp(region_bus, provider) != 0) + if (!util_bus_filter(bus, region_bus)) continue; ndctl_region_foreach(bus, region) { - region_name = ndctl_region_get_devname(region); - - if (strcmp(region_arg, "all") != 0 - && strcmp(region_arg, region_name) != 0) + if (!util_region_filter(region, region_arg)) continue; if (xable_fn(region) == 0) success++; diff --git a/builtin-xaction-namespace.c b/builtin-xaction-namespace.c index 1d7e460e0950..5eb28bbeedfc 100644 --- a/builtin-xaction-namespace.c +++ b/builtin-xaction-namespace.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -536,11 +537,10 @@ static int namespace_reconfig(struct ndctl_region *region, static int do_xaction_namespace(const char *namespace, enum namespace_action action) { - unsigned long bus_id = ULONG_MAX, region_id = ULONG_MAX, id; - const char *provider, *region_name, *ndns_name; int rc = -ENXIO, success = 0; struct ndctl_namespace *ndns; struct ndctl_region *region; + const char *ndns_name; struct ndctl_ctx *ctx; struct ndctl_bus *bus; @@ -554,40 +554,12 @@ static int do_xaction_namespace(const char *namespace, if (verbose) ndctl_set_log_priority(ctx, LOG_DEBUG); - if (param.bus) { - char *end = NULL; - - bus_id = strtoul(param.bus, &end, 0); - if (end) - bus_id = ULONG_MAX; - } - - if (param.region) { - char *end = NULL; - - region_id = strtoul(param.region, &end, 0); - if (end) - region_id = ULONG_MAX; - } - ndctl_bus_foreach(ctx, bus) { - provider = ndctl_bus_get_provider(bus); - id = ndctl_bus_get_id(bus); - - if (bus_id < ULONG_MAX && bus_id != id) - continue; - else if (bus_id == ULONG_MAX && param.bus - && strcmp(param.bus, provider) != 0) + if (!util_bus_filter(bus, param.bus)) continue; ndctl_region_foreach(bus, region) { - region_name = ndctl_region_get_devname(region); - id = ndctl_region_get_id(region); - - if (region_id < ULONG_MAX && region_id != id) - continue; - else if (region_id == ULONG_MAX && param.region - && strcmp(param.region, region_name) != 0) + if (!util_region_filter(region, param.region)) continue; if (param.type) { diff --git a/util/filter.c b/util/filter.c new file mode 100644 index 000000000000..8c8dbf1a8887 --- /dev/null +++ b/util/filter.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +struct ndctl_bus *util_bus_filter(struct ndctl_bus *bus, const char *ident) +{ + char *end = NULL; + const char *provider; + unsigned long bus_id, id; + + if (!ident || strcmp(ident, "all") == 0) + return bus; + + bus_id = strtoul(ident, &end, 0); + if (end == ident || end[0]) + bus_id = ULONG_MAX; + + provider = ndctl_bus_get_provider(bus); + id = ndctl_bus_get_id(bus); + + if (bus_id < ULONG_MAX && bus_id == id) + return bus; + + if (bus_id == ULONG_MAX && strcmp(ident, provider) == 0) + return bus; + + return NULL; +} + +struct ndctl_region *util_region_filter(struct ndctl_region *region, + const char *ident) +{ + char *end = NULL; + const char *name; + unsigned long region_id, id; + + if (!ident || strcmp(ident, "all") == 0) + return region; + + region_id = strtoul(ident, &end, 0); + if (end == ident || end[0]) + region_id = ULONG_MAX; + + name = ndctl_region_get_devname(region); + id = ndctl_region_get_id(region); + + if (region_id < ULONG_MAX && region_id == id) + return region; + + if (region_id == ULONG_MAX && strcmp(ident, name) == 0) + return region; + + return NULL; +} diff --git a/util/filter.h b/util/filter.h new file mode 100644 index 000000000000..bc5c2557a65c --- /dev/null +++ b/util/filter.h @@ -0,0 +1,6 @@ +#ifndef _NDCTL_FILTER_H_ +#define _NDCTL_FILTER_H_ +struct ndctl_bus *util_bus_filter(struct ndctl_bus *bus, const char *ident); +struct ndctl_region *util_region_filter(struct ndctl_region *region, + const char *ident); +#endif