Skip to content

Commit 1fec709

Browse files
Yehuda Sadehliewegas
authored andcommitted
rbd: fix split bio handling
The rbd driver currently splits bios when they span an object boundary. However, the blk_end_request expects the completions to roll up the results in block device order, and the split rbd/ceph ops can complete in any order. This patch adds a struct rbd_req_coll to track completion of split requests and ensures that the results are passed back up to the block layer in order. This fixes errors where the file system gets completion of a read operation that spans an object boundary before the data has actually arrived. The bug is easily reproduced with iozone with a working set larger than available RAM. Reported-by: Fyodor Ustinov <ufm@ufm.su> Signed-off-by: Yehuda Sadeh <yehuda@hq.newdream.net> Signed-off-by: Sage Weil <sage@newdream.net>
1 parent 11f7700 commit 1fec709

1 file changed

Lines changed: 151 additions & 20 deletions

File tree

‎drivers/block/rbd.c‎

Lines changed: 151 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ struct rbd_client {
9292
struct list_head node;
9393
};
9494

95+
struct rbd_req_coll;
96+
9597
/*
9698
* a single io request
9799
*/
@@ -100,6 +102,24 @@ struct rbd_request {
100102
struct bio *bio; /* cloned bio */
101103
struct page **pages; /* list of used pages */
102104
u64 len;
105+
int coll_index;
106+
struct rbd_req_coll *coll;
107+
};
108+
109+
struct rbd_req_status {
110+
int done;
111+
int rc;
112+
u64 bytes;
113+
};
114+
115+
/*
116+
* a collection of requests
117+
*/
118+
struct rbd_req_coll {
119+
int total;
120+
int num_done;
121+
struct kref kref;
122+
struct rbd_req_status status[0];
103123
};
104124

105125
struct rbd_snap {
@@ -416,6 +436,17 @@ static void rbd_put_client(struct rbd_device *rbd_dev)
416436
rbd_dev->client = NULL;
417437
}
418438

439+
/*
440+
* Destroy requests collection
441+
*/
442+
static void rbd_coll_release(struct kref *kref)
443+
{
444+
struct rbd_req_coll *coll =
445+
container_of(kref, struct rbd_req_coll, kref);
446+
447+
dout("rbd_coll_release %p\n", coll);
448+
kfree(coll);
449+
}
419450

420451
/*
421452
* Create a new header structure, translate header format from the on-disk
@@ -590,6 +621,14 @@ static u64 rbd_get_segment(struct rbd_image_header *header,
590621
return len;
591622
}
592623

624+
static int rbd_get_num_segments(struct rbd_image_header *header,
625+
u64 ofs, u64 len)
626+
{
627+
u64 start_seg = ofs >> header->obj_order;
628+
u64 end_seg = (ofs + len - 1) >> header->obj_order;
629+
return end_seg - start_seg + 1;
630+
}
631+
593632
/*
594633
* bio helpers
595634
*/
@@ -735,6 +774,50 @@ static void rbd_destroy_ops(struct ceph_osd_req_op *ops)
735774
kfree(ops);
736775
}
737776

777+
static void rbd_coll_end_req_index(struct request *rq,
778+
struct rbd_req_coll *coll,
779+
int index,
780+
int ret, u64 len)
781+
{
782+
struct request_queue *q;
783+
int min, max, i;
784+
785+
dout("rbd_coll_end_req_index %p index %d ret %d len %lld\n",
786+
coll, index, ret, len);
787+
788+
if (!rq)
789+
return;
790+
791+
if (!coll) {
792+
blk_end_request(rq, ret, len);
793+
return;
794+
}
795+
796+
q = rq->q;
797+
798+
spin_lock_irq(q->queue_lock);
799+
coll->status[index].done = 1;
800+
coll->status[index].rc = ret;
801+
coll->status[index].bytes = len;
802+
max = min = coll->num_done;
803+
while (max < coll->total && coll->status[max].done)
804+
max++;
805+
806+
for (i = min; i<max; i++) {
807+
__blk_end_request(rq, coll->status[i].rc,
808+
coll->status[i].bytes);
809+
coll->num_done++;
810+
kref_put(&coll->kref, rbd_coll_release);
811+
}
812+
spin_unlock_irq(q->queue_lock);
813+
}
814+
815+
static void rbd_coll_end_req(struct rbd_request *req,
816+
int ret, u64 len)
817+
{
818+
rbd_coll_end_req_index(req->rq, req->coll, req->coll_index, ret, len);
819+
}
820+
738821
/*
739822
* Send ceph osd request
740823
*/
@@ -749,6 +832,8 @@ static int rbd_do_request(struct request *rq,
749832
int flags,
750833
struct ceph_osd_req_op *ops,
751834
int num_reply,
835+
struct rbd_req_coll *coll,
836+
int coll_index,
752837
void (*rbd_cb)(struct ceph_osd_request *req,
753838
struct ceph_msg *msg),
754839
struct ceph_osd_request **linger_req,
@@ -763,12 +848,20 @@ static int rbd_do_request(struct request *rq,
763848
struct ceph_osd_request_head *reqhead;
764849
struct rbd_image_header *header = &dev->header;
765850

766-
ret = -ENOMEM;
767851
req_data = kzalloc(sizeof(*req_data), GFP_NOIO);
768-
if (!req_data)
769-
goto done;
852+
if (!req_data) {
853+
if (coll)
854+
rbd_coll_end_req_index(rq, coll, coll_index,
855+
-ENOMEM, len);
856+
return -ENOMEM;
857+
}
858+
859+
if (coll) {
860+
req_data->coll = coll;
861+
req_data->coll_index = coll_index;
862+
}
770863

771-
dout("rbd_do_request len=%lld ofs=%lld\n", len, ofs);
864+
dout("rbd_do_request obj=%s ofs=%lld len=%lld\n", obj, len, ofs);
772865

773866
down_read(&header->snap_rwsem);
774867

@@ -828,7 +921,8 @@ static int rbd_do_request(struct request *rq,
828921
ret = ceph_osdc_wait_request(&dev->client->osdc, req);
829922
if (ver)
830923
*ver = le64_to_cpu(req->r_reassert_version.version);
831-
dout("reassert_ver=%lld\n", le64_to_cpu(req->r_reassert_version.version));
924+
dout("reassert_ver=%lld\n",
925+
le64_to_cpu(req->r_reassert_version.version));
832926
ceph_osdc_put_request(req);
833927
}
834928
return ret;
@@ -837,10 +931,8 @@ static int rbd_do_request(struct request *rq,
837931
bio_chain_put(req_data->bio);
838932
ceph_osdc_put_request(req);
839933
done_pages:
934+
rbd_coll_end_req(req_data, ret, len);
840935
kfree(req_data);
841-
done:
842-
if (rq)
843-
blk_end_request(rq, ret, len);
844936
return ret;
845937
}
846938

@@ -874,7 +966,7 @@ static void rbd_req_cb(struct ceph_osd_request *req, struct ceph_msg *msg)
874966
bytes = req_data->len;
875967
}
876968

877-
blk_end_request(req_data->rq, rc, bytes);
969+
rbd_coll_end_req(req_data, rc, bytes);
878970

879971
if (req_data->bio)
880972
bio_chain_put(req_data->bio);
@@ -934,6 +1026,7 @@ static int rbd_req_sync_op(struct rbd_device *dev,
9341026
flags,
9351027
ops,
9361028
2,
1029+
NULL, 0,
9371030
NULL,
9381031
linger_req, ver);
9391032
if (ret < 0)
@@ -959,7 +1052,9 @@ static int rbd_do_op(struct request *rq,
9591052
u64 snapid,
9601053
int opcode, int flags, int num_reply,
9611054
u64 ofs, u64 len,
962-
struct bio *bio)
1055+
struct bio *bio,
1056+
struct rbd_req_coll *coll,
1057+
int coll_index)
9631058
{
9641059
char *seg_name;
9651060
u64 seg_ofs;
@@ -995,6 +1090,7 @@ static int rbd_do_op(struct request *rq,
9951090
flags,
9961091
ops,
9971092
num_reply,
1093+
coll, coll_index,
9981094
rbd_req_cb, 0, NULL);
9991095

10001096
rbd_destroy_ops(ops);
@@ -1010,13 +1106,15 @@ static int rbd_req_write(struct request *rq,
10101106
struct rbd_device *rbd_dev,
10111107
struct ceph_snap_context *snapc,
10121108
u64 ofs, u64 len,
1013-
struct bio *bio)
1109+
struct bio *bio,
1110+
struct rbd_req_coll *coll,
1111+
int coll_index)
10141112
{
10151113
return rbd_do_op(rq, rbd_dev, snapc, CEPH_NOSNAP,
10161114
CEPH_OSD_OP_WRITE,
10171115
CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
10181116
2,
1019-
ofs, len, bio);
1117+
ofs, len, bio, coll, coll_index);
10201118
}
10211119

10221120
/*
@@ -1026,14 +1124,16 @@ static int rbd_req_read(struct request *rq,
10261124
struct rbd_device *rbd_dev,
10271125
u64 snapid,
10281126
u64 ofs, u64 len,
1029-
struct bio *bio)
1127+
struct bio *bio,
1128+
struct rbd_req_coll *coll,
1129+
int coll_index)
10301130
{
10311131
return rbd_do_op(rq, rbd_dev, NULL,
10321132
(snapid ? snapid : CEPH_NOSNAP),
10331133
CEPH_OSD_OP_READ,
10341134
CEPH_OSD_FLAG_READ,
10351135
2,
1036-
ofs, len, bio);
1136+
ofs, len, bio, coll, coll_index);
10371137
}
10381138

10391139
/*
@@ -1081,6 +1181,7 @@ static int rbd_req_sync_notify_ack(struct rbd_device *dev,
10811181
CEPH_OSD_FLAG_READ,
10821182
ops,
10831183
1,
1184+
NULL, 0,
10841185
rbd_simple_req_cb, 0, NULL);
10851186

10861187
rbd_destroy_ops(ops);
@@ -1278,6 +1379,20 @@ static int rbd_req_sync_exec(struct rbd_device *dev,
12781379
return ret;
12791380
}
12801381

1382+
static struct rbd_req_coll *rbd_alloc_coll(int num_reqs)
1383+
{
1384+
struct rbd_req_coll *coll =
1385+
kzalloc(sizeof(struct rbd_req_coll) +
1386+
sizeof(struct rbd_req_status) * num_reqs,
1387+
GFP_ATOMIC);
1388+
1389+
if (!coll)
1390+
return NULL;
1391+
coll->total = num_reqs;
1392+
kref_init(&coll->kref);
1393+
return coll;
1394+
}
1395+
12811396
/*
12821397
* block device queue callback
12831398
*/
@@ -1295,6 +1410,8 @@ static void rbd_rq_fn(struct request_queue *q)
12951410
bool do_write;
12961411
int size, op_size = 0;
12971412
u64 ofs;
1413+
int num_segs, cur_seg = 0;
1414+
struct rbd_req_coll *coll;
12981415

12991416
/* peek at request from block layer */
13001417
if (!rq)
@@ -1325,42 +1442,56 @@ static void rbd_rq_fn(struct request_queue *q)
13251442
do_write ? "write" : "read",
13261443
size, blk_rq_pos(rq) * 512ULL);
13271444

1445+
num_segs = rbd_get_num_segments(&rbd_dev->header, ofs, size);
1446+
coll = rbd_alloc_coll(num_segs);
1447+
if (!coll) {
1448+
spin_lock_irq(q->queue_lock);
1449+
__blk_end_request_all(rq, -ENOMEM);
1450+
goto next;
1451+
}
1452+
13281453
do {
13291454
/* a bio clone to be passed down to OSD req */
13301455
dout("rq->bio->bi_vcnt=%d\n", rq->bio->bi_vcnt);
13311456
op_size = rbd_get_segment(&rbd_dev->header,
13321457
rbd_dev->header.block_name,
13331458
ofs, size,
13341459
NULL, NULL);
1460+
kref_get(&coll->kref);
13351461
bio = bio_chain_clone(&rq_bio, &next_bio, &bp,
13361462
op_size, GFP_ATOMIC);
13371463
if (!bio) {
1338-
spin_lock_irq(q->queue_lock);
1339-
__blk_end_request_all(rq, -ENOMEM);
1340-
goto next;
1464+
rbd_coll_end_req_index(rq, coll, cur_seg,
1465+
-ENOMEM, op_size);
1466+
goto next_seg;
13411467
}
13421468

1469+
13431470
/* init OSD command: write or read */
13441471
if (do_write)
13451472
rbd_req_write(rq, rbd_dev,
13461473
rbd_dev->header.snapc,
13471474
ofs,
1348-
op_size, bio);
1475+
op_size, bio,
1476+
coll, cur_seg);
13491477
else
13501478
rbd_req_read(rq, rbd_dev,
13511479
cur_snap_id(rbd_dev),
13521480
ofs,
1353-
op_size, bio);
1481+
op_size, bio,
1482+
coll, cur_seg);
13541483

1484+
next_seg:
13551485
size -= op_size;
13561486
ofs += op_size;
13571487

1488+
cur_seg++;
13581489
rq_bio = next_bio;
13591490
} while (size > 0);
1491+
kref_put(&coll->kref, rbd_coll_release);
13601492

13611493
if (bp)
13621494
bio_pair_release(bp);
1363-
13641495
spin_lock_irq(q->queue_lock);
13651496
next:
13661497
rq = blk_fetch_request(q);

0 commit comments

Comments
 (0)