Commit 8b8fb99c authored by Alex Elder's avatar Alex Elder

rbd: add reference counting to rbd_spec

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: default avatarAlex Elder <elder@inktank.com>
Reviewed-by: default avatarJosh Durgin <josh.durgin@inktank.com>
parent 0d7dbfce
......@@ -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 @@ static ssize_t rbd_add(struct bus_type *bus,
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 */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment