Skip to content

Commit 4e752f0

Browse files
jdurginidryomov
authored andcommitted
rbd: access snapshot context and mapping size safely
These fields may both change while the image is mapped if a snapshot is created or deleted or the image is resized. They are guarded by rbd_dev->header_rwsem, so hold that while reading them, and store a local copy to refer to outside of the critical section. The local copy will stay consistent since the snapshot context is reference counted, and the mapping size is just a u64. This prevents torn loads from giving us inconsistent values. Move reading header.snapc into the caller of rbd_img_request_create() so that we only need to take the semaphore once. The read-only caller, rbd_parent_request_create() can just pass NULL for snapc, since the snapshot context is only relevant for writes. Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
1 parent 7dd440c commit 4e752f0

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

‎drivers/block/rbd.c‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,28 +2057,23 @@ static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
20572057
static struct rbd_img_request *rbd_img_request_create(
20582058
struct rbd_device *rbd_dev,
20592059
u64 offset, u64 length,
2060-
bool write_request)
2060+
bool write_request,
2061+
struct ceph_snap_context *snapc)
20612062
{
20622063
struct rbd_img_request *img_request;
20632064

20642065
img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO);
20652066
if (!img_request)
20662067
return NULL;
20672068

2068-
if (write_request) {
2069-
down_read(&rbd_dev->header_rwsem);
2070-
ceph_get_snap_context(rbd_dev->header.snapc);
2071-
up_read(&rbd_dev->header_rwsem);
2072-
}
2073-
20742069
img_request->rq = NULL;
20752070
img_request->rbd_dev = rbd_dev;
20762071
img_request->offset = offset;
20772072
img_request->length = length;
20782073
img_request->flags = 0;
20792074
if (write_request) {
20802075
img_request_write_set(img_request);
2081-
img_request->snapc = rbd_dev->header.snapc;
2076+
img_request->snapc = snapc;
20822077
} else {
20832078
img_request->snap_id = rbd_dev->spec->snap_id;
20842079
}
@@ -2134,8 +2129,8 @@ static struct rbd_img_request *rbd_parent_request_create(
21342129
rbd_assert(obj_request->img_request);
21352130
rbd_dev = obj_request->img_request->rbd_dev;
21362131

2137-
parent_request = rbd_img_request_create(rbd_dev->parent,
2138-
img_offset, length, false);
2132+
parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
2133+
length, false, NULL);
21392134
if (!parent_request)
21402135
return NULL;
21412136

@@ -3183,9 +3178,11 @@ static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
31833178
static void rbd_handle_request(struct rbd_device *rbd_dev, struct request *rq)
31843179
{
31853180
struct rbd_img_request *img_request;
3181+
struct ceph_snap_context *snapc = NULL;
31863182
u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
31873183
u64 length = blk_rq_bytes(rq);
31883184
bool wr = rq_data_dir(rq) == WRITE;
3185+
u64 mapping_size;
31893186
int result;
31903187

31913188
/* Ignore/skip any zero-length requests */
@@ -3226,14 +3223,23 @@ static void rbd_handle_request(struct rbd_device *rbd_dev, struct request *rq)
32263223
goto err_rq; /* Shouldn't happen */
32273224
}
32283225

3229-
if (offset + length > rbd_dev->mapping.size) {
3226+
down_read(&rbd_dev->header_rwsem);
3227+
mapping_size = rbd_dev->mapping.size;
3228+
if (wr) {
3229+
snapc = rbd_dev->header.snapc;
3230+
ceph_get_snap_context(snapc);
3231+
}
3232+
up_read(&rbd_dev->header_rwsem);
3233+
3234+
if (offset + length > mapping_size) {
32303235
rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
3231-
length, rbd_dev->mapping.size);
3236+
length, mapping_size);
32323237
result = -EIO;
32333238
goto err_rq;
32343239
}
32353240

3236-
img_request = rbd_img_request_create(rbd_dev, offset, length, wr);
3241+
img_request = rbd_img_request_create(rbd_dev, offset, length, wr,
3242+
snapc);
32373243
if (!img_request) {
32383244
result = -ENOMEM;
32393245
goto err_rq;
@@ -3256,6 +3262,8 @@ static void rbd_handle_request(struct rbd_device *rbd_dev, struct request *rq)
32563262
if (result)
32573263
rbd_warn(rbd_dev, "%s %llx at %llx result %d",
32583264
wr ? "write" : "read", length, offset, result);
3265+
if (snapc)
3266+
ceph_put_snap_context(snapc);
32593267
blk_end_request_all(rq, result);
32603268
}
32613269

0 commit comments

Comments
 (0)