All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alex Elder <elder@dreamhost.com>
To: ceph-devel@vger.kernel.org
Subject: [PATCH 4/5] rbd: tie rbd_dev_list changes to rbd_id operations
Date: Tue, 28 Feb 2012 19:40:12 -0800	[thread overview]
Message-ID: <4F4D9E1C.2070906@dreamhost.com> (raw)
In-Reply-To: <4F4D9DA5.8050101@dreamhost.com>

The only time entries are added to or removed from the global
rbd_dev_list is exactly when a "put" or "get" operation is being
performed on a rbd_dev's id.  So just move the list management code
into get/put routines.

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

diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index e839289..042377b 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -2153,26 +2153,36 @@ static int rbd_init_watch_dev(struct rbd_device 
*rbd_dev)
  static atomic_t rbd_id_max = ATOMIC_INIT(0);

  /*
- * Get a unique rbd identifier.  The minimum rbd id is 1.
+ * Get a unique rbd identifier for the given new rbd_dev, and add
+ * the rbd_dev to the global list.  The minimum rbd id is 1.
   */
-static int rbd_id_get(void)
+static void rbd_id_get(struct rbd_device *rbd_dev)
  {
-	return atomic_inc_return(&rbd_id_max);
+	rbd_dev->id = atomic_inc_return(&rbd_id_max);
+
+	spin_lock(&rbd_dev_list_lock);
+	list_add_tail(&rbd_dev->node, &rbd_dev_list);
+	spin_unlock(&rbd_dev_list_lock);
  }

  /*
- * Record that an rbd identifier is no longer in use.
+ * Remove an rbd_dev from the global list, and record that its
+ * identifier is no longer in use.
   */
-static void rbd_id_put(int rbd_id)
+static void rbd_id_put(struct rbd_device *rbd_dev)
  {
-	BUG_ON(rbd_id < 1);
+	BUG_ON(rbd_dev->id < 1);
+
+	spin_lock(&rbd_dev_list_lock);
+	list_del_init(&rbd_dev->node);
+	spin_unlock(&rbd_dev_list_lock);

  	/*
  	 * New id's are always one more than the current maximum.
  	 * If the id being "put" *is* that maximum, decrement the
  	 * maximum so the next one requested just reuses this one.
  	 */
-	atomic_cmpxchg(&rbd_id_max, rbd_id, rbd_id - 1);
+	atomic_cmpxchg(&rbd_id_max, rbd_dev->id, rbd_dev->id - 1);
  }

  static ssize_t rbd_add(struct bus_type *bus,
@@ -2208,12 +2218,7 @@ static ssize_t rbd_add(struct bus_type *bus,
  	INIT_LIST_HEAD(&rbd_dev->snaps);

  	/* generate unique id: one more than highest used so far */
-	rbd_dev->id = rbd_id_get();
-
-	/* add to global list */
-	spin_lock(&rbd_dev_list_lock);
-	list_add_tail(&rbd_dev->node, &rbd_dev_list);
-	spin_unlock(&rbd_dev_list_lock);
+	rbd_id_get(rbd_dev);

  	/* parse add command */
  	if (sscanf(buf, "%" __stringify(RBD_MAX_OPT_LEN) "s "
@@ -2276,10 +2281,7 @@ static ssize_t rbd_add(struct bus_type *bus,
  	return count;

  err_out_bus:
-	spin_lock(&rbd_dev_list_lock);
-	list_del_init(&rbd_dev->node);
-	spin_unlock(&rbd_dev_list_lock);
-	rbd_id_put(target_id);
+	rbd_id_put(rbd_dev);

  	/* this will also clean up rest of rbd_dev stuff */

@@ -2293,10 +2295,7 @@ err_out_blkdev:
  err_out_client:
  	rbd_put_client(rbd_dev);
  err_out_slot:
-	spin_lock(&rbd_dev_list_lock);
-	list_del_init(&rbd_dev->node);
-	spin_unlock(&rbd_dev_list_lock);
-	rbd_id_put(target_id);
+	rbd_id_put(rbd_dev);

  	kfree(rbd_dev);
  err_out_opt:
@@ -2377,11 +2376,7 @@ static ssize_t rbd_remove(struct bus_type *bus,
  		goto done;
  	}

-	spin_lock(&rbd_dev_list_lock);
-	list_del_init(&rbd_dev->node);
-	spin_unlock(&rbd_dev_list_lock);
-
-	rbd_id_put(target_id);
+	rbd_id_put(rbd_dev);

  	__rbd_remove_all_snaps(rbd_dev);
  	rbd_bus_del_dev(rbd_dev);
-- 
1.7.5.4


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

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-29  3:38 [PATCH 0/5] rbd: improve how rbd ids are selected Alex Elder
2012-02-29  3:40 ` [PATCH 2/5] rbd: encapsulate new rbd id selection Alex Elder
2012-02-29  3:40 ` [PATCH 2/5] rbd: rework calculation of new rbd id's Alex Elder
2012-02-29 20:37   ` Yehuda Sadeh Weinraub
2012-02-29  3:40 ` [PATCH 3/5] rbd: protect the rbd_dev_list with a spinlock Alex Elder
2012-02-29  3:40 ` Alex Elder [this message]
2012-02-29  3:40 ` [PATCH 5/5] rbd: restore previous rbd id sequence behavior Alex Elder
2012-02-29 20:53   ` Yehuda Sadeh Weinraub
2012-02-29 21:27     ` Alex Elder
2012-03-02 19:54 ` [PATCH 0/5] rbd: improve how rbd ids are selected 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=4F4D9E1C.2070906@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.