@@ -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
105125struct 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+ }
770858
771- dout ("rbd_do_request len=%lld ofs=%lld\n" , len , ofs );
859+ if (coll ) {
860+ req_data -> coll = coll ;
861+ req_data -> coll_index = coll_index ;
862+ }
863+
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 );
839933done_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,7 +1090,10 @@ 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 );
1095+
1096+ rbd_destroy_ops (ops );
9991097done :
10001098 kfree (seg_name );
10011099 return ret ;
@@ -1008,13 +1106,15 @@ static int rbd_req_write(struct request *rq,
10081106 struct rbd_device * rbd_dev ,
10091107 struct ceph_snap_context * snapc ,
10101108 u64 ofs , u64 len ,
1011- struct bio * bio )
1109+ struct bio * bio ,
1110+ struct rbd_req_coll * coll ,
1111+ int coll_index )
10121112{
10131113 return rbd_do_op (rq , rbd_dev , snapc , CEPH_NOSNAP ,
10141114 CEPH_OSD_OP_WRITE ,
10151115 CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK ,
10161116 2 ,
1017- ofs , len , bio );
1117+ ofs , len , bio , coll , coll_index );
10181118}
10191119
10201120/*
@@ -1024,14 +1124,16 @@ static int rbd_req_read(struct request *rq,
10241124 struct rbd_device * rbd_dev ,
10251125 u64 snapid ,
10261126 u64 ofs , u64 len ,
1027- struct bio * bio )
1127+ struct bio * bio ,
1128+ struct rbd_req_coll * coll ,
1129+ int coll_index )
10281130{
10291131 return rbd_do_op (rq , rbd_dev , NULL ,
10301132 (snapid ? snapid : CEPH_NOSNAP ),
10311133 CEPH_OSD_OP_READ ,
10321134 CEPH_OSD_FLAG_READ ,
10331135 2 ,
1034- ofs , len , bio );
1136+ ofs , len , bio , coll , coll_index );
10351137}
10361138
10371139/*
@@ -1063,7 +1165,9 @@ static int rbd_req_sync_notify_ack(struct rbd_device *dev,
10631165{
10641166 struct ceph_osd_req_op * ops ;
10651167 struct page * * pages = NULL ;
1066- int ret = rbd_create_rw_ops (& ops , 1 , CEPH_OSD_OP_NOTIFY_ACK , 0 );
1168+ int ret ;
1169+
1170+ ret = rbd_create_rw_ops (& ops , 1 , CEPH_OSD_OP_NOTIFY_ACK , 0 );
10671171 if (ret < 0 )
10681172 return ret ;
10691173
@@ -1077,6 +1181,7 @@ static int rbd_req_sync_notify_ack(struct rbd_device *dev,
10771181 CEPH_OSD_FLAG_READ ,
10781182 ops ,
10791183 1 ,
1184+ NULL , 0 ,
10801185 rbd_simple_req_cb , 0 , NULL );
10811186
10821187 rbd_destroy_ops (ops );
@@ -1274,6 +1379,20 @@ static int rbd_req_sync_exec(struct rbd_device *dev,
12741379 return ret ;
12751380}
12761381
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+
12771396/*
12781397 * block device queue callback
12791398 */
@@ -1291,6 +1410,8 @@ static void rbd_rq_fn(struct request_queue *q)
12911410 bool do_write ;
12921411 int size , op_size = 0 ;
12931412 u64 ofs ;
1413+ int num_segs , cur_seg = 0 ;
1414+ struct rbd_req_coll * coll ;
12941415
12951416 /* peek at request from block layer */
12961417 if (!rq )
@@ -1321,42 +1442,56 @@ static void rbd_rq_fn(struct request_queue *q)
13211442 do_write ? "write" : "read" ,
13221443 size , blk_rq_pos (rq ) * 512ULL );
13231444
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+
13241453 do {
13251454 /* a bio clone to be passed down to OSD req */
13261455 dout ("rq->bio->bi_vcnt=%d\n" , rq -> bio -> bi_vcnt );
13271456 op_size = rbd_get_segment (& rbd_dev -> header ,
13281457 rbd_dev -> header .block_name ,
13291458 ofs , size ,
13301459 NULL , NULL );
1460+ kref_get (& coll -> kref );
13311461 bio = bio_chain_clone (& rq_bio , & next_bio , & bp ,
13321462 op_size , GFP_ATOMIC );
13331463 if (!bio ) {
1334- spin_lock_irq ( q -> queue_lock );
1335- __blk_end_request_all ( rq , - ENOMEM );
1336- goto next ;
1464+ rbd_coll_end_req_index ( rq , coll , cur_seg ,
1465+ - ENOMEM , op_size );
1466+ goto next_seg ;
13371467 }
13381468
1469+
13391470 /* init OSD command: write or read */
13401471 if (do_write )
13411472 rbd_req_write (rq , rbd_dev ,
13421473 rbd_dev -> header .snapc ,
13431474 ofs ,
1344- op_size , bio );
1475+ op_size , bio ,
1476+ coll , cur_seg );
13451477 else
13461478 rbd_req_read (rq , rbd_dev ,
13471479 cur_snap_id (rbd_dev ),
13481480 ofs ,
1349- op_size , bio );
1481+ op_size , bio ,
1482+ coll , cur_seg );
13501483
1484+ next_seg :
13511485 size -= op_size ;
13521486 ofs += op_size ;
13531487
1488+ cur_seg ++ ;
13541489 rq_bio = next_bio ;
13551490 } while (size > 0 );
1491+ kref_put (& coll -> kref , rbd_coll_release );
13561492
13571493 if (bp )
13581494 bio_pair_release (bp );
1359-
13601495 spin_lock_irq (q -> queue_lock );
13611496next :
13621497 rq = blk_fetch_request (q );
0 commit comments