Linux-NVDIMM Archive on lore.kernel.org
 help / color / Atom feed
From: "Li, Redhairer" <redhairer.li@intel.com>
To: "linux-nvdimm@lists.01.org" <linux-nvdimm@lists.01.org>
Cc: "Williams, Dan J" <dan.j.williams@intel.com>
Subject: [PATCH] daxctl: Change region input type from INTEGER to STRING.
Date: Wed, 4 Dec 2019 02:33:18 +0000
Message-ID: <2369E669066F8E42A79A3DF0E43B9E643AC9EB31@pgsmsx114.gar.corp.intel.com> (raw)

Allow daxctl to accept both <region-id>, and region name as region parameter.
For example:

    daxctl list -r region5
    daxctl list -r 5

Link: https://github.com/pmem/ndctl/issues/109
Signed-off-by: Redhairer Li <redhairer.li@intel.com>
---
 daxctl/device.c | 11 ++++-------
 daxctl/list.c   | 14 ++++++--------
 util/filter.c   | 16 ++++++++++++++++
 util/filter.h   |  2 ++
 4 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/daxctl/device.c b/daxctl/device.c
index 72e506e..d9db2f9 100644
--- a/daxctl/device.c
+++ b/daxctl/device.c
@@ -19,15 +19,13 @@
 static struct {
 	const char *dev;
 	const char *mode;
-	int region_id;
+	const char *region;
 	bool no_online;
 	bool no_movable;
 	bool force;
 	bool human;
 	bool verbose;
-} param = {
-	.region_id = -1,
-};
+} param;
 
 enum dev_mode {
 	DAXCTL_DEV_MODE_UNKNOWN,
@@ -51,7 +49,7 @@ enum device_action {
 };
 
 #define BASE_OPTIONS() \
-OPT_INTEGER('r', "region", &param.region_id, "restrict to the given region"), \
+OPT_STRING('r', "region", &param.region, "region-id", "filter by region"), \
 OPT_BOOLEAN('u', "human", &param.human, "use human friendly number formats"), \
 OPT_BOOLEAN('v', "verbose", &param.verbose, "emit more debug messages")
 
@@ -484,8 +482,7 @@ static int do_xaction_device(const char *device, enum device_action action,
 	*processed = 0;
 
 	daxctl_region_foreach(ctx, region) {
-		if (param.region_id >= 0 && param.region_id
-				!= daxctl_region_get_id(region))
+		if (!util_daxctl_region_filter(region, device))
 			continue;
 
 		daxctl_dev_foreach(region, dev) {
diff --git a/daxctl/list.c b/daxctl/list.c
index e56300d..6c6251b 100644
--- a/daxctl/list.c
+++ b/daxctl/list.c
@@ -44,10 +44,8 @@ static unsigned long listopts_to_flags(void)
 
 static struct {
 	const char *dev;
-	int region_id;
-} param = {
-	.region_id = -1,
-};
+	const char *region;
+} param;
 
 static int did_fail;
 
@@ -66,7 +64,8 @@ static int num_list_flags(void)
 int cmd_list(int argc, const char **argv, struct daxctl_ctx *ctx)
 {
 	const struct option options[] = {
-		OPT_INTEGER('r', "region", &param.region_id, "filter by region"),
+		OPT_STRING('r', "region", &param.region, "region-id",
+				"filter by region"),
 		OPT_STRING('d', "dev", &param.dev, "dev-id",
 				"filter by dax device instance name"),
 		OPT_BOOLEAN('D', "devices", &list.devs, "include dax device info"),
@@ -94,7 +93,7 @@ int cmd_list(int argc, const char **argv, struct daxctl_ctx *ctx)
 		usage_with_options(u, options);
 
 	if (num_list_flags() == 0) {
-		list.regions = param.region_id >= 0;
+		list.regions = !!param.region;
 		list.devs = !!param.dev;
 	}
 
@@ -106,8 +105,7 @@ int cmd_list(int argc, const char **argv, struct daxctl_ctx *ctx)
 	daxctl_region_foreach(ctx, region) {
 		struct json_object *jregion = NULL;
 
-		if (param.region_id >= 0 && param.region_id
-				!= daxctl_region_get_id(region))
+		if (!util_daxctl_region_filter(region, param.region))
 			continue;
 
 		if (list.regions) {
diff --git a/util/filter.c b/util/filter.c
index 1734bce..877d6c7 100644
--- a/util/filter.c
+++ b/util/filter.c
@@ -335,6 +335,22 @@ struct daxctl_dev *util_daxctl_dev_filter(struct daxctl_dev *dev,
 	return NULL;
 }
 
+struct daxctl_region *util_daxctl_region_filter(struct daxctl_region *region,
+		const char *ident)
+{
+	int region_id;
+
+	if (!ident || strcmp(ident, "all") == 0)
+		return region;
+
+	if ((sscanf(ident, "%d", &region_id) == 1
+       || sscanf(ident, "region%d", &region_id) == 1)
+			&& daxctl_region_get_id(region) == region_id)
+		return region;
+
+	return NULL;
+}
+
 static enum ndctl_namespace_mode mode_to_type(const char *mode)
 {
 	if (!mode)
diff --git a/util/filter.h b/util/filter.h
index c2cdddf..0c12b94 100644
--- a/util/filter.h
+++ b/util/filter.h
@@ -37,6 +37,8 @@ struct ndctl_region *util_region_filter_by_namespace(struct ndctl_region *region
 		const char *ident);
 struct daxctl_dev *util_daxctl_dev_filter(struct daxctl_dev *dev,
 		const char *ident);
+struct daxctl_region *util_daxctl_region_filter(struct daxctl_region *region,
+		const char *ident);
 
 struct json_object;
 
-- 
2.20.1.windows.1

_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

             reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-04  2:33 Li, Redhairer [this message]
2019-12-04  2:42 ` Dan Williams

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2369E669066F8E42A79A3DF0E43B9E643AC9EB31@pgsmsx114.gar.corp.intel.com \
    --to=redhairer.li@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=linux-nvdimm@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-NVDIMM Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-nvdimm/0 linux-nvdimm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-nvdimm linux-nvdimm/ https://lore.kernel.org/linux-nvdimm \
		linux-nvdimm@lists.01.org
	public-inbox-index linux-nvdimm

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.01.lists.linux-nvdimm


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git