All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alex Elder <elder@dreamhost.com>
To: ceph-devel@vger.kernel.org
Subject: [PATCH 1/5] rbd: encapsulate argument parsing for rbd_add()
Date: Tue, 28 Feb 2012 19:59:10 -0800	[thread overview]
Message-ID: <4F4DA28E.1090308@dreamhost.com> (raw)
In-Reply-To: <4F4DA1EA.3080705@dreamhost.com>

Move the code that parses the arguments provided to rbd_add() (which
are supplied via /sys/bus/rbd/add) into a separate function.

Also rename the "mon_dev_name" variable in rbd_add() to be
"mon_addrs".   The variable represents a list of one or more
comma-separated monitor IP addresses, each with an optional port
number.  I think "mon_addrs" captures that notion a little better.

Signed-off-by: Alex Elder <elder@dreamhost.com>
---
  drivers/block/rbd.c |   68 
+++++++++++++++++++++++++++++++-------------------
  1 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index 29bbac1..3731a15 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -2219,12 +2219,43 @@ static void rbd_id_put(struct rbd_device *rbd_dev)
  	atomic_cmpxchg(&rbd_id_max, rbd_id, max_id);
  }

+/*
+ * This fills in the pool_name, obj, obj_len, snap_name, obj_len,
+ * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based
+ * on the list of monitor addresses and other options provided via
+ * /sys/bus/rbd/add.
+ */
+static int rbd_add_parse_args(struct rbd_device *rbd_dev,
+			      const char *buf,
+			      char *mon_addrs,
+			      char *options)
+{
+	if (sscanf(buf, "%" __stringify(RBD_MAX_OPT_LEN) "s "
+		   "%" __stringify(RBD_MAX_OPT_LEN) "s "
+		   "%" __stringify(RBD_MAX_POOL_NAME_LEN) "s "
+		   "%" __stringify(RBD_MAX_OBJ_NAME_LEN) "s"
+		   "%" __stringify(RBD_MAX_SNAP_NAME_LEN) "s",
+		   mon_addrs, options, rbd_dev->pool_name,
+		   rbd_dev->obj, rbd_dev->snap_name) < 4)
+		return -EINVAL;
+
+	if (rbd_dev->snap_name[0] == 0)
+		memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
+			sizeof RBD_SNAP_HEAD_NAME);
+
+	rbd_dev->obj_len = strlen(rbd_dev->obj);
+	snprintf(rbd_dev->obj_md_name, sizeof(rbd_dev->obj_md_name), "%s%s",
+		 rbd_dev->obj, RBD_SUFFIX);
+
+	return 0;
+}
+
  static ssize_t rbd_add(struct bus_type *bus,
  		       const char *buf,
  		       size_t count)
  {
  	struct rbd_device *rbd_dev;
-	char *mon_dev_name = NULL;
+	char *mon_addrs = NULL;
  	char *options = NULL;
  	struct ceph_osd_client *osdc;
  	int rc = -ENOMEM;
@@ -2235,8 +2266,8 @@ static ssize_t rbd_add(struct bus_type *bus,
  	rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
  	if (!rbd_dev)
  		goto err_nomem;
-	mon_dev_name = kmalloc(count, GFP_KERNEL);
-	if (!mon_dev_name)
+	mon_addrs = kmalloc(count, GFP_KERNEL);
+	if (!mon_addrs)
  		goto err_nomem;
  	options = kmalloc(count, GFP_KERNEL);
  	if (!options)
@@ -2250,30 +2281,15 @@ static ssize_t rbd_add(struct bus_type *bus,
  	/* generate unique id: find highest unique id, add one */
  	rbd_id_get(rbd_dev);

+	/* Fill in the device name, now that we have its id. */
+	snprintf(rbd_dev->name, DEV_NAME_LEN, RBD_DRV_NAME "%d", rbd_dev->id);
+
  	/* parse add command */
-	if (sscanf(buf, "%" __stringify(RBD_MAX_OPT_LEN) "s "
-		   "%" __stringify(RBD_MAX_OPT_LEN) "s "
-		   "%" __stringify(RBD_MAX_POOL_NAME_LEN) "s "
-		   "%" __stringify(RBD_MAX_OBJ_NAME_LEN) "s"
-		   "%" __stringify(RBD_MAX_SNAP_NAME_LEN) "s",
-		   mon_dev_name, options, rbd_dev->pool_name,
-		   rbd_dev->obj, rbd_dev->snap_name) < 4) {
-		rc = -EINVAL;
+	rc = rbd_add_parse_args(rbd_dev, buf, mon_addrs, options);
+	if (rc)
  		goto err_put_id;
-	}
-
-	if (rbd_dev->snap_name[0] == 0)
-		memcpy(rbd_dev->snap_name, RBD_SNAP_HEAD_NAME,
-			sizeof RBD_SNAP_HEAD_NAME);
-
-	rbd_dev->obj_len = strlen(rbd_dev->obj);
-	snprintf(rbd_dev->obj_md_name, sizeof(rbd_dev->obj_md_name), "%s%s",
-		 rbd_dev->obj, RBD_SUFFIX);
-
-	/* initialize rest of new object */
-	snprintf(rbd_dev->name, DEV_NAME_LEN, RBD_DRV_NAME "%d", rbd_dev->id);

-	rbd_dev->rbd_client = rbd_get_client(mon_dev_name, options);
+	rbd_dev->rbd_client = rbd_get_client(mon_addrs, options);
  	if (IS_ERR(rbd_dev->rbd_client)) {
  		rc = PTR_ERR(rbd_dev->rbd_client);
  		goto err_put_id;
@@ -2314,7 +2330,7 @@ err_out_bus:

  	rbd_bus_del_dev(rbd_dev);
  	kfree(options);
-	kfree(mon_dev_name);
+	kfree(mon_addrs);
  	return rc;

  err_out_blkdev:
@@ -2325,7 +2341,7 @@ err_put_id:
  	rbd_id_put(rbd_dev);
  err_nomem:
  	kfree(options);
-	kfree(mon_dev_name);
+	kfree(mon_addrs);
  	kfree(rbd_dev);

  	dout("Error adding device %s\n", buf);
-- 
1.7.5.4


  reply	other threads:[~2012-02-29  3:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-29  3:56 [PATCH 0/5] rbd: cleanups related to argument parsing Alex Elder
2012-02-29  3:59 ` Alex Elder [this message]
2012-02-29  3:59 ` [PATCH 2/5] rbd: don't use sscanf() in rbd_add_parse_args() Alex Elder
2012-02-29  3:59 ` [PATCH 3/5] rbd: do a few checks at build time Alex Elder
2012-02-29  3:59 ` [PATCH 4/5] rbd: have rbd_parse_args() report found mon_addrs size Alex Elder
2012-02-29  3:59 ` [PATCH 5/5] rbd: don't allocate mon_addrs buffer in rbd_add() Alex Elder
2012-03-02 21:28 ` [PATCH 0/5] rbd: cleanups related to argument parsing Sage Weil

Reply instructions:

You may reply publicly 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=4F4DA28E.1090308@dreamhost.com \
    --to=elder@dreamhost.com \
    --cc=ceph-devel@vger.kernel.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.