rbd: use snaps list in rbd_snap_by_name()
authorAlex Elder <elder@inktank.com>
Wed, 11 Jul 2012 01:30:11 +0000 (20:30 -0500)
committerAlex Elder <elder@inktank.com>
Mon, 1 Oct 2012 19:30:52 +0000 (14:30 -0500)
An rbd_dev structure maintains a list of current snapshots that have
already been fully initialized.  The entries on the list have type
struct rbd_snap, and each entry contains a copy of information
that's found in the rbd_dev's snapshot context and header.

The only caller of snap_by_name() is rbd_header_set_snap().  In that
call site any positive return value (the index in the snapshot
array) is ignored, so there's no need to return the index in
the snapshot context's id array when it's found.

rbd_header_set_snap() also has only one caller--rbd_add()--and that
call is made after a call to rbd_dev_snap_devs_update().  Because
the rbd_snap structures are initialized in that function, the
current snapshot list can be used instead of the snapshot context to
look up a snapshot's information by name.

Change snap_by_name() so it uses the snapshot list rather than the
rbd_dev's snapshot context in looking up snapshot information.
Return 0 if it's found rather than the snapshot id.

Signed-off-by: Alex Elder <elder@inktank.com>
Reviewed-by: Josh Durgin <josh.durgin@inktank.com>
drivers/block/rbd.c

index 144694e..1ecdeb1 100644 (file)
@@ -623,23 +623,18 @@ out_err:
 
 static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
 {
-       int i;
-       struct rbd_image_header *header = &rbd_dev->header;
-       char *p = header->snap_names;
-
-       rbd_assert(header->snapc != NULL);
-       for (i = 0; i < header->snapc->num_snaps; i++) {
-               if (!strcmp(snap_name, p)) {
 
-                       /* Found it.  Pass back its id and/or size */
+       struct rbd_snap *snap;
 
-                       rbd_dev->mapping.snap_id = header->snapc->snaps[i];
-                       rbd_dev->mapping.size = header->snap_sizes[i];
+       list_for_each_entry(snap, &rbd_dev->snaps, node) {
+               if (!strcmp(snap_name, snap->name)) {
+                       rbd_dev->mapping.snap_id = snap->id;
+                       rbd_dev->mapping.size = snap->size;
 
-                       return i;
+                       return 0;
                }
-               p += strlen(p) + 1;     /* Skip ahead to the next name */
        }
+
        return -ENOENT;
 }
 
@@ -653,6 +648,7 @@ static int rbd_header_set_snap(struct rbd_device *rbd_dev, char *snap_name)
                rbd_dev->mapping.size = rbd_dev->header.image_size;
                rbd_dev->mapping.snap_exists = false;
                rbd_dev->mapping.read_only = rbd_dev->rbd_opts.read_only;
+               ret = 0;
        } else {
                ret = snap_by_name(rbd_dev, snap_name);
                if (ret < 0)
@@ -661,8 +657,6 @@ static int rbd_header_set_snap(struct rbd_device *rbd_dev, char *snap_name)
                rbd_dev->mapping.read_only = true;
        }
        rbd_dev->mapping.snap_name = snap_name;
-
-       ret = 0;
 done:
        return ret;
 }