@@ -98,6 +98,9 @@ struct blkfront_info
9898 unsigned long shadow_free ;
9999 unsigned int feature_flush ;
100100 unsigned int flush_op ;
101+ unsigned int feature_discard ;
102+ unsigned int discard_granularity ;
103+ unsigned int discard_alignment ;
101104 int is_ready ;
102105};
103106
@@ -302,29 +305,36 @@ static int blkif_queue_request(struct request *req)
302305 ring_req -> operation = info -> flush_op ;
303306 }
304307
305- ring_req -> nr_segments = blk_rq_map_sg (req -> q , req , info -> sg );
306- BUG_ON (ring_req -> nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST );
308+ if (unlikely (req -> cmd_flags & REQ_DISCARD )) {
309+ /* id, sector_number and handle are set above. */
310+ ring_req -> operation = BLKIF_OP_DISCARD ;
311+ ring_req -> nr_segments = 0 ;
312+ ring_req -> u .discard .nr_sectors = blk_rq_sectors (req );
313+ } else {
314+ ring_req -> nr_segments = blk_rq_map_sg (req -> q , req , info -> sg );
315+ BUG_ON (ring_req -> nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST );
307316
308- for_each_sg (info -> sg , sg , ring_req -> nr_segments , i ) {
309- buffer_mfn = pfn_to_mfn (page_to_pfn (sg_page (sg )));
310- fsect = sg -> offset >> 9 ;
311- lsect = fsect + (sg -> length >> 9 ) - 1 ;
312- /* install a grant reference. */
313- ref = gnttab_claim_grant_reference (& gref_head );
314- BUG_ON (ref == - ENOSPC );
317+ for_each_sg (info -> sg , sg , ring_req -> nr_segments , i ) {
318+ buffer_mfn = pfn_to_mfn (page_to_pfn (sg_page (sg )));
319+ fsect = sg -> offset >> 9 ;
320+ lsect = fsect + (sg -> length >> 9 ) - 1 ;
321+ /* install a grant reference. */
322+ ref = gnttab_claim_grant_reference (& gref_head );
323+ BUG_ON (ref == - ENOSPC );
315324
316- gnttab_grant_foreign_access_ref (
317- ref ,
318- info -> xbdev -> otherend_id ,
319- buffer_mfn ,
320- rq_data_dir (req ) );
321-
322- info -> shadow [id ].frame [i ] = mfn_to_pfn (buffer_mfn );
323- ring_req -> u .rw .seg [i ] =
324- (struct blkif_request_segment ) {
325- .gref = ref ,
326- .first_sect = fsect ,
327- .last_sect = lsect };
325+ gnttab_grant_foreign_access_ref (
326+ ref ,
327+ info -> xbdev -> otherend_id ,
328+ buffer_mfn ,
329+ rq_data_dir (req ));
330+
331+ info -> shadow [id ].frame [i ] = mfn_to_pfn (buffer_mfn );
332+ ring_req -> u .rw .seg [i ] =
333+ (struct blkif_request_segment ) {
334+ .gref = ref ,
335+ .first_sect = fsect ,
336+ .last_sect = lsect };
337+ }
328338 }
329339
330340 info -> ring .req_prod_pvt ++ ;
@@ -399,13 +409,21 @@ static void do_blkif_request(struct request_queue *rq)
399409static int xlvbd_init_blk_queue (struct gendisk * gd , u16 sector_size )
400410{
401411 struct request_queue * rq ;
412+ struct blkfront_info * info = gd -> private_data ;
402413
403414 rq = blk_init_queue (do_blkif_request , & blkif_io_lock );
404415 if (rq == NULL )
405416 return -1 ;
406417
407418 queue_flag_set_unlocked (QUEUE_FLAG_VIRT , rq );
408419
420+ if (info -> feature_discard ) {
421+ queue_flag_set_unlocked (QUEUE_FLAG_DISCARD , rq );
422+ blk_queue_max_discard_sectors (rq , get_capacity (gd ));
423+ rq -> limits .discard_granularity = info -> discard_granularity ;
424+ rq -> limits .discard_alignment = info -> discard_alignment ;
425+ }
426+
409427 /* Hard sector size and max sectors impersonate the equiv. hardware. */
410428 blk_queue_logical_block_size (rq , sector_size );
411429 blk_queue_max_hw_sectors (rq , 512 );
@@ -722,6 +740,19 @@ static irqreturn_t blkif_interrupt(int irq, void *dev_id)
722740
723741 error = (bret -> status == BLKIF_RSP_OKAY ) ? 0 : - EIO ;
724742 switch (bret -> operation ) {
743+ case BLKIF_OP_DISCARD :
744+ if (unlikely (bret -> status == BLKIF_RSP_EOPNOTSUPP )) {
745+ struct request_queue * rq = info -> rq ;
746+ printk (KERN_WARNING "blkfront: %s: discard op failed\n" ,
747+ info -> gd -> disk_name );
748+ error = - EOPNOTSUPP ;
749+ info -> feature_discard = 0 ;
750+ spin_lock (rq -> queue_lock );
751+ queue_flag_clear (QUEUE_FLAG_DISCARD , rq );
752+ spin_unlock (rq -> queue_lock );
753+ }
754+ __blk_end_request_all (req , error );
755+ break ;
725756 case BLKIF_OP_FLUSH_DISKCACHE :
726757 case BLKIF_OP_WRITE_BARRIER :
727758 if (unlikely (bret -> status == BLKIF_RSP_EOPNOTSUPP )) {
@@ -1098,6 +1129,33 @@ blkfront_closing(struct blkfront_info *info)
10981129 bdput (bdev );
10991130}
11001131
1132+ static void blkfront_setup_discard (struct blkfront_info * info )
1133+ {
1134+ int err ;
1135+ char * type ;
1136+ unsigned int discard_granularity ;
1137+ unsigned int discard_alignment ;
1138+
1139+ type = xenbus_read (XBT_NIL , info -> xbdev -> otherend , "type" , NULL );
1140+ if (IS_ERR (type ))
1141+ return ;
1142+
1143+ if (strncmp (type , "phy" , 3 ) == 0 ) {
1144+ err = xenbus_gather (XBT_NIL , info -> xbdev -> otherend ,
1145+ "discard-granularity" , "%u" , & discard_granularity ,
1146+ "discard-alignment" , "%u" , & discard_alignment ,
1147+ NULL );
1148+ if (!err ) {
1149+ info -> feature_discard = 1 ;
1150+ info -> discard_granularity = discard_granularity ;
1151+ info -> discard_alignment = discard_alignment ;
1152+ }
1153+ } else if (strncmp (type , "file" , 4 ) == 0 )
1154+ info -> feature_discard = 1 ;
1155+
1156+ kfree (type );
1157+ }
1158+
11011159/*
11021160 * Invoked when the backend is finally 'ready' (and has told produced
11031161 * the details about the physical device - #sectors, size, etc).
@@ -1108,7 +1166,7 @@ static void blkfront_connect(struct blkfront_info *info)
11081166 unsigned long sector_size ;
11091167 unsigned int binfo ;
11101168 int err ;
1111- int barrier , flush ;
1169+ int barrier , flush , discard ;
11121170
11131171 switch (info -> connected ) {
11141172 case BLKIF_STATE_CONNECTED :
@@ -1178,7 +1236,14 @@ static void blkfront_connect(struct blkfront_info *info)
11781236 info -> feature_flush = REQ_FLUSH ;
11791237 info -> flush_op = BLKIF_OP_FLUSH_DISKCACHE ;
11801238 }
1181-
1239+
1240+ err = xenbus_gather (XBT_NIL , info -> xbdev -> otherend ,
1241+ "feature-discard" , "%d" , & discard ,
1242+ NULL );
1243+
1244+ if (!err && discard )
1245+ blkfront_setup_discard (info );
1246+
11821247 err = xlvbd_alloc_gendisk (sectors , info , binfo , sector_size );
11831248 if (err ) {
11841249 xenbus_dev_fatal (info -> xbdev , err , "xlvbd_add at %s" ,
0 commit comments