From patchwork Fri Apr 27 18:18:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 10369599 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 74C01601BE for ; Fri, 27 Apr 2018 18:18:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 64424294E9 for ; Fri, 27 Apr 2018 18:18:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5892D29502; Fri, 27 Apr 2018 18:18:54 +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 E0C25294E9 for ; Fri, 27 Apr 2018 18:18:53 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id D62762034C8A5; Fri, 27 Apr 2018 11:18:53 -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.115; helo=mga14.intel.com; envelope-from=dave.jiang@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (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 5E80C2034C8A1 for ; Fri, 27 Apr 2018 11:18:52 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 Apr 2018 11:18:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,336,1520924400"; d="scan'208";a="41176084" Received: from djiang5-desk3.ch.intel.com ([143.182.136.93]) by fmsmga002.fm.intel.com with ESMTP; 27 Apr 2018 11:18:51 -0700 Subject: [PATCH v4 4/4] ndctl: convert dimm actions to use util_filter_walk() From: Dave Jiang To: vishal.l.verma@intel.com, dan.j.williams@intel.com Date: Fri, 27 Apr 2018 11:18:51 -0700 Message-ID: <152485313162.47995.17962466882440254997.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <152485300486.47995.7487206758776476576.stgit@djiang5-desk3.ch.intel.com> References: <152485300486.47995.7487206758776476576.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 dimms. Removing duplicate code for dimm actions and provide filter functions so we can utilize util_filter_walk() and share common code. Signed-off-by: Dave Jiang --- ndctl/dimm.c | 83 ++++++++++++++++++++++++++++++++++----------------------- util/filter.h | 9 ++++++ 2 files changed, 59 insertions(+), 33 deletions(-) diff --git a/ndctl/dimm.c b/ndctl/dimm.c index 1c168997..d652be69 100644 --- a/ndctl/dimm.c +++ b/ndctl/dimm.c @@ -822,7 +822,6 @@ static int action_update(struct ndctl_dimm *dimm, struct action_context *actx) } static struct parameters { - const char *bus; const char *outfile; const char *infile; const char *labelversion; @@ -833,6 +832,8 @@ static struct parameters { .labelversion = "1.1", }; +struct util_filter_params uf_param; + static int __action_init(struct ndctl_dimm *dimm, enum ndctl_namespace_version version, int chk_only) { @@ -899,7 +900,7 @@ static int action_check(struct ndctl_dimm *dimm, struct action_context *actx) #define BASE_OPTIONS() \ -OPT_STRING('b', "bus", ¶m.bus, "bus-id", \ +OPT_STRING('b', "bus", &uf_param.bus, "bus-id", \ " must be on a bus with an id/provider of "), \ OPT_BOOLEAN('v',"verbose", ¶m.verbose, "turn on debug") @@ -951,18 +952,48 @@ static const struct option init_options[] = { OPT_END(), }; +static bool filter_bus(struct ndctl_bus *bus, struct util_filter_ctx *ctx) +{ + return true; +} + +static void filter_dimm(struct ndctl_dimm *dimm, struct util_filter_ctx *ctx) +{ + struct dmaction_filter_arg *dfa = ctx->dmaction; + int rc; + + if (dfa->action == action_write) { + dfa->dimm = dimm; + rc = 0; + } else + rc = dfa->action(dimm, dfa->actx); + + if (rc == 0) + dfa->count++; + else if (rc && !dfa->rc) + dfa->rc = rc; +} + +/* dummy filter function to skip the loop */ +static bool filter_region(struct ndctl_region *region, + struct util_filter_ctx *ctx) +{ + return false; +} + static int dimm_action(int argc, const char **argv, void *ctx, int (*action)(struct ndctl_dimm *dimm, struct action_context *actx), const struct option *options, const char *usage) { struct action_context actx = { 0 }; - int i, rc = 0, count = 0, err = 0; - struct ndctl_dimm *single = NULL; + int i, rc = 0, err = 0; const char * const u[] = { usage, NULL }; unsigned long id; + struct util_filter_ctx fctx = { 0 }; + struct dmaction_filter_arg dfa = { 0 }; argc = parse_options(argc, argv, options, u, 0); @@ -1039,45 +1070,31 @@ static int dimm_action(int argc, const char **argv, void *ctx, goto out; } + fctx.filter_bus = filter_bus; + fctx.filter_dimm = filter_dimm; + fctx.filter_region = filter_region; + fctx.dmaction = &dfa; + dfa.action = action; + dfa.actx = &actx; + rc = 0; - err = 0; - count = 0; for (i = 0; i < argc; i++) { - struct ndctl_dimm *dimm; - struct ndctl_bus *bus; - if (sscanf(argv[i], "nmem%lu", &id) != 1 && strcmp(argv[i], "all") != 0) continue; - ndctl_bus_foreach(ctx, bus) { - if (!util_bus_filter(bus, param.bus)) - continue; - ndctl_dimm_foreach(bus, dimm) { - if (!util_dimm_filter(dimm, argv[i])) - continue; - if (action == action_write) { - single = dimm; - rc = 0; - } else - rc = action(dimm, &actx); - - if (rc == 0) - count++; - else if (rc && !err) - err = rc; - } - } + uf_param.dimm = argv[i]; + rc = util_filter_walk(ctx, &fctx, &uf_param); } - rc = err; + rc = dfa.rc; if (action == action_write) { - if (count > 1) { + if (dfa.count > 1) { error("write-labels only supports writing a single dimm\n"); usage_with_options(u, options); return -EINVAL; - } else if (single) - rc = action(single, &actx); + } else if (dfa.dimm) + rc = action(dfa.dimm, &actx); } if (actx.jdimms) @@ -1095,8 +1112,8 @@ static int dimm_action(int argc, const char **argv, void *ctx, * count if some actions succeeded, 0 if none were attempted, * negative error code otherwise. */ - if (count > 0) - return count; + if (dfa.count > 0) + return dfa.count; return rc; } diff --git a/util/filter.h b/util/filter.h index 0e0e3bec..2bef9310 100644 --- a/util/filter.h +++ b/util/filter.h @@ -62,6 +62,14 @@ struct rgaction_filter_arg { int rc; }; +struct dmaction_filter_arg { + struct action_context *actx; + struct ndctl_dimm *dimm; + int (*action)(struct ndctl_dimm *dimm, struct action_context *actx); + int count; + int rc; +}; + /* * struct util_filter_ctx - control and callbacks for util_filter_walk() * ->filter_bus() and ->filter_region() return bool because the @@ -81,6 +89,7 @@ struct util_filter_ctx { struct list_filter_arg *list; struct nsaction_filter_arg *nsaction; struct rgaction_filter_arg *rgaction; + struct dmaction_filter_arg *dmaction; }; };