rbd: add reference counting to rbd_spec
authorAlex Elder <elder@inktank.com>
Fri, 26 Oct 2012 22:25:24 +0000 (17:25 -0500)
committerAlex Elder <elder@inktank.com>
Thu, 1 Nov 2012 12:55:41 +0000 (07:55 -0500)
With layered images we'll share rbd_spec structures, so add a
reference count to it.  It neatens up some code also.

A silly get/put pair is added to the alloc routine just to avoid
"defined but not used" warnings.  It will go away soon.

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

index 2049810fcdc26c205f77a2dd2fb8c892b8e2c7e3..86206a75017dc63f194428780de86c9442e323f5 100644 (file)
@@ -2134,6 +2134,45 @@ static struct device_type rbd_snap_device_type = {
        .release        = rbd_snap_dev_release,
 };
 
+static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
+{
+       kref_get(&spec->kref);
+
+       return spec;
+}
+
+static void rbd_spec_free(struct kref *kref);
+static void rbd_spec_put(struct rbd_spec *spec)
+{
+       if (spec)
+               kref_put(&spec->kref, rbd_spec_free);
+}
+
+static struct rbd_spec *rbd_spec_alloc(void)
+{
+       struct rbd_spec *spec;
+
+       spec = kzalloc(sizeof (*spec), GFP_KERNEL);
+       if (!spec)
+               return NULL;
+       kref_init(&spec->kref);
+
+       rbd_spec_put(rbd_spec_get(spec));       /* TEMPORARY */
+
+       return spec;
+}
+
+static void rbd_spec_free(struct kref *kref)
+{
+       struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
+
+       kfree(spec->pool_name);
+       kfree(spec->image_id);
+       kfree(spec->image_name);
+       kfree(spec->snap_name);
+       kfree(spec);
+}
+
 static bool rbd_snap_registered(struct rbd_snap *snap)
 {
        bool ret = snap->dev.type == &rbd_snap_device_type;
@@ -3165,7 +3204,7 @@ static ssize_t rbd_add(struct bus_type *bus,
        rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
        if (!rbd_dev)
                return -ENOMEM;
-       rbd_dev->spec = kzalloc(sizeof (*rbd_dev->spec), GFP_KERNEL);
+       rbd_dev->spec = rbd_spec_alloc();
        if (!rbd_dev->spec)
                goto err_out_mem;
 
@@ -3278,16 +3317,12 @@ err_out_probe:
 err_out_client:
        kfree(rbd_dev->header_name);
        rbd_put_client(rbd_dev);
-       kfree(rbd_dev->spec->image_id);
 err_out_args:
        if (ceph_opts)
                ceph_destroy_options(ceph_opts);
-       kfree(rbd_dev->spec->snap_name);
-       kfree(rbd_dev->spec->image_name);
-       kfree(rbd_dev->spec->pool_name);
        kfree(rbd_opts);
 err_out_mem:
-       kfree(rbd_dev->spec);
+       rbd_spec_put(rbd_dev->spec);
        kfree(rbd_dev);
 
        dout("Error adding device %s\n", buf);
@@ -3336,12 +3371,9 @@ static void rbd_dev_release(struct device *dev)
        rbd_header_free(&rbd_dev->header);
 
        /* done with the id, and with the rbd_dev */
-       kfree(rbd_dev->spec->snap_name);
-       kfree(rbd_dev->spec->image_id);
        kfree(rbd_dev->header_name);
-       kfree(rbd_dev->spec->pool_name);
-       kfree(rbd_dev->spec->image_name);
        rbd_dev_id_put(rbd_dev);
+       rbd_spec_put(rbd_dev->spec);
        kfree(rbd_dev);
 
        /* release module ref */