Skip to content

Commit 0407759

Browse files
committed
rbd: split rbd_dev_spec_update() into two functions
rbd_dev_spec_update() has two modes of operation, with nothing in common between them. Split it into two functions, one for each mode and make our expectations more clear. Signed-off-by: Ilya Dryomov <ilya.dryomov@inktank.com> Reviewed-by: Alex Elder <elder@linaro.org>
1 parent 7626eb7 commit 0407759

1 file changed

Lines changed: 48 additions & 31 deletions

File tree

‎drivers/block/rbd.c‎

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3798,6 +3798,9 @@ static struct rbd_spec *rbd_spec_alloc(void)
37983798
spec = kzalloc(sizeof (*spec), GFP_KERNEL);
37993799
if (!spec)
38003800
return NULL;
3801+
3802+
spec->pool_id = CEPH_NOPOOL;
3803+
spec->snap_id = CEPH_NOSNAP;
38013804
kref_init(&spec->kref);
38023805

38033806
return spec;
@@ -4257,18 +4260,38 @@ static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
42574260
}
42584261

42594262
/*
4260-
* When an rbd image has a parent image, it is identified by the
4261-
* pool, image, and snapshot ids (not names). This function fills
4262-
* in the names for those ids. (It's OK if we can't figure out the
4263-
* name for an image id, but the pool and snapshot ids should always
4264-
* exist and have names.) All names in an rbd spec are dynamically
4265-
* allocated.
4263+
* An image being mapped will have everything but the snap id.
4264+
*/
4265+
static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
4266+
{
4267+
struct rbd_spec *spec = rbd_dev->spec;
4268+
4269+
rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
4270+
rbd_assert(spec->image_id && spec->image_name);
4271+
rbd_assert(spec->snap_name);
4272+
4273+
if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
4274+
u64 snap_id;
4275+
4276+
snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
4277+
if (snap_id == CEPH_NOSNAP)
4278+
return -ENOENT;
4279+
4280+
spec->snap_id = snap_id;
4281+
} else {
4282+
spec->snap_id = CEPH_NOSNAP;
4283+
}
4284+
4285+
return 0;
4286+
}
4287+
4288+
/*
4289+
* A parent image will have all ids but none of the names.
42664290
*
4267-
* When an image being mapped (not a parent) is probed, we have the
4268-
* pool name and pool id, image name and image id, and the snapshot
4269-
* name. The only thing we're missing is the snapshot id.
4291+
* All names in an rbd spec are dynamically allocated. It's OK if we
4292+
* can't figure out the name for an image id.
42704293
*/
4271-
static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
4294+
static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
42724295
{
42734296
struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
42744297
struct rbd_spec *spec = rbd_dev->spec;
@@ -4277,24 +4300,9 @@ static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
42774300
const char *snap_name;
42784301
int ret;
42794302

4280-
/*
4281-
* An image being mapped will have the pool name (etc.), but
4282-
* we need to look up the snapshot id.
4283-
*/
4284-
if (spec->pool_name) {
4285-
if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
4286-
u64 snap_id;
4287-
4288-
snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
4289-
if (snap_id == CEPH_NOSNAP)
4290-
return -ENOENT;
4291-
spec->snap_id = snap_id;
4292-
} else {
4293-
spec->snap_id = CEPH_NOSNAP;
4294-
}
4295-
4296-
return 0;
4297-
}
4303+
rbd_assert(spec->pool_id != CEPH_NOPOOL);
4304+
rbd_assert(spec->image_id);
4305+
rbd_assert(spec->snap_id != CEPH_NOSNAP);
42984306

42994307
/* Get the pool name; we have to make our own copy of this */
43004308

@@ -4313,7 +4321,7 @@ static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
43134321
if (!image_name)
43144322
rbd_warn(rbd_dev, "unable to get image name");
43154323

4316-
/* Look up the snapshot name, and make a copy */
4324+
/* Fetch the snapshot name */
43174325

43184326
snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
43194327
if (IS_ERR(snap_name)) {
@@ -4326,10 +4334,10 @@ static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
43264334
spec->snap_name = snap_name;
43274335

43284336
return 0;
4337+
43294338
out_err:
43304339
kfree(image_name);
43314340
kfree(pool_name);
4332-
43334341
return ret;
43344342
}
43354343

@@ -5158,7 +5166,16 @@ static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
51585166
if (ret)
51595167
goto err_out_watch;
51605168

5161-
ret = rbd_dev_spec_update(rbd_dev);
5169+
/*
5170+
* If this image is the one being mapped, we have pool name and
5171+
* id, image name and id, and snap name - need to fill snap id.
5172+
* Otherwise this is a parent image, identified by pool, image
5173+
* and snap ids - need to fill in names for those ids.
5174+
*/
5175+
if (mapping)
5176+
ret = rbd_spec_fill_snap_id(rbd_dev);
5177+
else
5178+
ret = rbd_spec_fill_names(rbd_dev);
51625179
if (ret)
51635180
goto err_out_probe;
51645181

0 commit comments

Comments
 (0)