From patchwork Fri May 11 20:24:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 10395333 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 CB6E460236 for ; Fri, 11 May 2018 20:24:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B8B2E28DA1 for ; Fri, 11 May 2018 20:24:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AD82A28FA6; Fri, 11 May 2018 20:24:55 +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 5CB6B28DA1 for ; Fri, 11 May 2018 20:24:55 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 576FC2098C22E; Fri, 11 May 2018 13:24:55 -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=134.134.136.100; helo=mga07.intel.com; envelope-from=dave.jiang@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) (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 3CB0B2098C222 for ; Fri, 11 May 2018 13:24:54 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 May 2018 13:24:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,390,1520924400"; d="scan'208";a="40273466" Received: from djiang5-desk3.ch.intel.com ([143.182.136.93]) by orsmga007.jf.intel.com with ESMTP; 11 May 2018 13:24:53 -0700 Subject: [PATCH v6 3/4] ndctl: convert region actions to use util_filter_walk() From: Dave Jiang To: vishal.l.verma@intel.com, dan.j.williams@intel.com Date: Fri, 11 May 2018 13:24:53 -0700 Message-ID: <152607029316.4523.4236595984903653707.stgit@djiang5-desk3.ch.intel.com> In-Reply-To: <152607009656.4523.5355287262776399070.stgit@djiang5-desk3.ch.intel.com> References: <152607009656.4523.5355287262776399070.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 region ops and provide filter functions so we can utilize util_filter_walk() and share common code. Signed-off-by: Dave Jiang --- ndctl/region.c | 54 +++++++++++++++++++++++++++++++----------------------- util/filter.h | 6 ++++++ 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/ndctl/region.c b/ndctl/region.c index 9fc90808..fa8f15ad 100644 --- a/ndctl/region.c +++ b/ndctl/region.c @@ -19,10 +19,7 @@ #include #include -static struct { - const char *bus; - const char *type; -} param; +struct util_filter_params param; static const struct option region_options[] = { OPT_STRING('b', "bus", ¶m.bus, "bus-id", @@ -92,33 +89,44 @@ static int region_action(struct ndctl_region *region, enum device_action mode) return 0; } +static bool filter_region(struct ndctl_region *region, + struct util_filter_ctx *ctx) +{ + struct rgaction_filter_arg *rfa = ctx->rgaction; + int rc; + + if (rfa->rc < 0) + return false; + + rc = region_action(region, rfa->action); + + if (rc == 0) + rfa->rc++; + else + rfa->rc = 0; + + /* we don't need to fall through, can continue the loop */ + return false; +} + static int do_xable_region(const char *region_arg, enum device_action mode, struct ndctl_ctx *ctx) { - int rc = -ENXIO, success = 0; - struct ndctl_region *region; - struct ndctl_bus *bus; + int rc = -ENXIO; + struct util_filter_ctx fctx = { 0 }; + struct rgaction_filter_arg rfa = { 0 }; if (!region_arg) goto out; - ndctl_bus_foreach(ctx, bus) { - if (!util_bus_filter(bus, param.bus)) - continue; - - ndctl_region_foreach(bus, region) { - const char *type = ndctl_region_get_type_name(region); - - if (param.type && strcmp(param.type, type) != 0) - continue; - if (!util_region_filter(region, region_arg)) - continue; - if (region_action(region, mode) == 0) - success++; - } - } + fctx.filter_bus = filter_bus_passthrough; + fctx.filter_region = filter_region; + fctx.rgaction = &rfa; + fctx.rgaction->action = mode; + rc = util_filter_walk(ctx, &fctx, ¶m); + if (rc == 0) + rc = fctx.rgaction->rc; - rc = success; out: param.bus = NULL; return rc; diff --git a/util/filter.h b/util/filter.h index e59bb8da..9aede013 100644 --- a/util/filter.h +++ b/util/filter.h @@ -57,6 +57,11 @@ struct nsaction_filter_arg { int rc; }; +struct rgaction_filter_arg { + enum device_action action; + int rc; +}; + /* * struct util_filter_ctx - control and callbacks for util_filter_walk() * ->filter_bus() and ->filter_region() return bool because the @@ -75,6 +80,7 @@ struct util_filter_ctx { void *arg; struct list_filter_arg *list; struct nsaction_filter_arg *nsaction; + struct rgaction_filter_arg *rgaction; }; };