aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2011-12-25 10:46:46 -0500
committerJens Axboe <axboe@kernel.dk>2011-12-25 10:46:46 -0500
commitf748040bb875a0d94c3ceef180ab704bdf43079f (patch)
treeb4f91bfaafc1e714e5ff95782b6fe4081a6c7e49
parentfd63836811d6e5b5f5f608abf865bc9e91762c8c (diff)
parentf094148a1751d6ece9374851eb2926bc3cfd16ef (diff)
Merge branch 'stable/for-jens-3.3' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen into for-3.3/drivers
-rw-r--r--drivers/block/xen-blkback/blkback.c84
-rw-r--r--drivers/block/xen-blkback/common.h67
-rw-r--r--drivers/block/xen-blkback/xenbus.c12
-rw-r--r--drivers/block/xen-blkfront.c79
-rw-r--r--include/xen/interface/io/blkif.h40
5 files changed, 170 insertions, 112 deletions
diff --git a/drivers/block/xen-blkback/blkback.c b/drivers/block/xen-blkback/blkback.c
index 15ec4db194d1..0088bf60f368 100644
--- a/drivers/block/xen-blkback/blkback.c
+++ b/drivers/block/xen-blkback/blkback.c
@@ -39,9 +39,6 @@
39#include <linux/list.h> 39#include <linux/list.h>
40#include <linux/delay.h> 40#include <linux/delay.h>
41#include <linux/freezer.h> 41#include <linux/freezer.h>
42#include <linux/loop.h>
43#include <linux/falloc.h>
44#include <linux/fs.h>
45 42
46#include <xen/events.h> 43#include <xen/events.h>
47#include <xen/page.h> 44#include <xen/page.h>
@@ -362,7 +359,7 @@ static int xen_blkbk_map(struct blkif_request *req,
362{ 359{
363 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 360 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
364 int i; 361 int i;
365 int nseg = req->nr_segments; 362 int nseg = req->u.rw.nr_segments;
366 int ret = 0; 363 int ret = 0;
367 364
368 /* 365 /*
@@ -416,30 +413,25 @@ static int xen_blkbk_map(struct blkif_request *req,
416 return ret; 413 return ret;
417} 414}
418 415
419static void xen_blk_discard(struct xen_blkif *blkif, struct blkif_request *req) 416static int dispatch_discard_io(struct xen_blkif *blkif,
417 struct blkif_request *req)
420{ 418{
421 int err = 0; 419 int err = 0;
422 int status = BLKIF_RSP_OKAY; 420 int status = BLKIF_RSP_OKAY;
423 struct block_device *bdev = blkif->vbd.bdev; 421 struct block_device *bdev = blkif->vbd.bdev;
424 422
425 if (blkif->blk_backend_type == BLKIF_BACKEND_PHY) 423 blkif->st_ds_req++;
426 /* just forward the discard request */ 424
425 xen_blkif_get(blkif);
426 if (blkif->blk_backend_type == BLKIF_BACKEND_PHY ||
427 blkif->blk_backend_type == BLKIF_BACKEND_FILE) {
428 unsigned long secure = (blkif->vbd.discard_secure &&
429 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
430 BLKDEV_DISCARD_SECURE : 0;
427 err = blkdev_issue_discard(bdev, 431 err = blkdev_issue_discard(bdev,
428 req->u.discard.sector_number, 432 req->u.discard.sector_number,
429 req->u.discard.nr_sectors, 433 req->u.discard.nr_sectors,
430 GFP_KERNEL, 0); 434 GFP_KERNEL, secure);
431 else if (blkif->blk_backend_type == BLKIF_BACKEND_FILE) {
432 /* punch a hole in the backing file */
433 struct loop_device *lo = bdev->bd_disk->private_data;
434 struct file *file = lo->lo_backing_file;
435
436 if (file->f_op->fallocate)
437 err = file->f_op->fallocate(file,
438 FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
439 req->u.discard.sector_number << 9,
440 req->u.discard.nr_sectors << 9);
441 else
442 err = -EOPNOTSUPP;
443 } else 435 } else
444 err = -EOPNOTSUPP; 436 err = -EOPNOTSUPP;
445 437
@@ -449,7 +441,9 @@ static void xen_blk_discard(struct xen_blkif *blkif, struct blkif_request *req)
449 } else if (err) 441 } else if (err)
450 status = BLKIF_RSP_ERROR; 442 status = BLKIF_RSP_ERROR;
451 443
452 make_response(blkif, req->id, req->operation, status); 444 make_response(blkif, req->u.discard.id, req->operation, status);
445 xen_blkif_put(blkif);
446 return err;
453} 447}
454 448
455static void xen_blk_drain_io(struct xen_blkif *blkif) 449static void xen_blk_drain_io(struct xen_blkif *blkif)
@@ -573,8 +567,11 @@ __do_block_io_op(struct xen_blkif *blkif)
573 567
574 /* Apply all sanity checks to /private copy/ of request. */ 568 /* Apply all sanity checks to /private copy/ of request. */
575 barrier(); 569 barrier();
576 570 if (unlikely(req.operation == BLKIF_OP_DISCARD)) {
577 if (dispatch_rw_block_io(blkif, &req, pending_req)) 571 free_req(pending_req);
572 if (dispatch_discard_io(blkif, &req))
573 break;
574 } else if (dispatch_rw_block_io(blkif, &req, pending_req))
578 break; 575 break;
579 576
580 /* Yield point for this unbounded loop. */ 577 /* Yield point for this unbounded loop. */
@@ -633,10 +630,6 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
633 blkif->st_f_req++; 630 blkif->st_f_req++;
634 operation = WRITE_FLUSH; 631 operation = WRITE_FLUSH;
635 break; 632 break;
636 case BLKIF_OP_DISCARD:
637 blkif->st_ds_req++;
638 operation = REQ_DISCARD;
639 break;
640 default: 633 default:
641 operation = 0; /* make gcc happy */ 634 operation = 0; /* make gcc happy */
642 goto fail_response; 635 goto fail_response;
@@ -644,9 +637,9 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
644 } 637 }
645 638
646 /* Check that the number of segments is sane. */ 639 /* Check that the number of segments is sane. */
647 nseg = req->nr_segments; 640 nseg = req->u.rw.nr_segments;
648 if (unlikely(nseg == 0 && operation != WRITE_FLUSH && 641
649 operation != REQ_DISCARD) || 642 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
650 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) { 643 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
651 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n", 644 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
652 nseg); 645 nseg);
@@ -654,12 +647,12 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
654 goto fail_response; 647 goto fail_response;
655 } 648 }
656 649
657 preq.dev = req->handle; 650 preq.dev = req->u.rw.handle;
658 preq.sector_number = req->u.rw.sector_number; 651 preq.sector_number = req->u.rw.sector_number;
659 preq.nr_sects = 0; 652 preq.nr_sects = 0;
660 653
661 pending_req->blkif = blkif; 654 pending_req->blkif = blkif;
662 pending_req->id = req->id; 655 pending_req->id = req->u.rw.id;
663 pending_req->operation = req->operation; 656 pending_req->operation = req->operation;
664 pending_req->status = BLKIF_RSP_OKAY; 657 pending_req->status = BLKIF_RSP_OKAY;
665 pending_req->nr_pages = nseg; 658 pending_req->nr_pages = nseg;
@@ -707,7 +700,7 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
707 * the hypercall to unmap the grants - that is all done in 700 * the hypercall to unmap the grants - that is all done in
708 * xen_blkbk_unmap. 701 * xen_blkbk_unmap.
709 */ 702 */
710 if (operation != REQ_DISCARD && xen_blkbk_map(req, pending_req, seg)) 703 if (xen_blkbk_map(req, pending_req, seg))
711 goto fail_flush; 704 goto fail_flush;
712 705
713 /* 706 /*
@@ -739,23 +732,16 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
739 732
740 /* This will be hit if the operation was a flush or discard. */ 733 /* This will be hit if the operation was a flush or discard. */
741 if (!bio) { 734 if (!bio) {
742 BUG_ON(operation != WRITE_FLUSH && operation != REQ_DISCARD); 735 BUG_ON(operation != WRITE_FLUSH);
743 736
744 if (operation == WRITE_FLUSH) { 737 bio = bio_alloc(GFP_KERNEL, 0);
745 bio = bio_alloc(GFP_KERNEL, 0); 738 if (unlikely(bio == NULL))
746 if (unlikely(bio == NULL)) 739 goto fail_put_bio;
747 goto fail_put_bio;
748 740
749 biolist[nbio++] = bio; 741 biolist[nbio++] = bio;
750 bio->bi_bdev = preq.bdev; 742 bio->bi_bdev = preq.bdev;
751 bio->bi_private = pending_req; 743 bio->bi_private = pending_req;
752 bio->bi_end_io = end_block_io_op; 744 bio->bi_end_io = end_block_io_op;
753 } else if (operation == REQ_DISCARD) {
754 xen_blk_discard(blkif, req);
755 xen_blkif_put(blkif);
756 free_req(pending_req);
757 return 0;
758 }
759 } 745 }
760 746
761 /* 747 /*
@@ -784,7 +770,7 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
784 xen_blkbk_unmap(pending_req); 770 xen_blkbk_unmap(pending_req);
785 fail_response: 771 fail_response:
786 /* Haven't submitted any bio's yet. */ 772 /* Haven't submitted any bio's yet. */
787 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR); 773 make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
788 free_req(pending_req); 774 free_req(pending_req);
789 msleep(1); /* back off a bit */ 775 msleep(1); /* back off a bit */
790 return -EIO; 776 return -EIO;
diff --git a/drivers/block/xen-blkback/common.h b/drivers/block/xen-blkback/common.h
index dfb1b3a43a5d..d0ee7edc9be8 100644
--- a/drivers/block/xen-blkback/common.h
+++ b/drivers/block/xen-blkback/common.h
@@ -60,58 +60,66 @@ struct blkif_common_response {
60 char dummy; 60 char dummy;
61}; 61};
62 62
63/* i386 protocol version */
64#pragma pack(push, 4)
65
66struct blkif_x86_32_request_rw { 63struct blkif_x86_32_request_rw {
64 uint8_t nr_segments; /* number of segments */
65 blkif_vdev_t handle; /* only for read/write requests */
66 uint64_t id; /* private guest value, echoed in resp */
67 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ 67 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
68 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 68 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
69}; 69} __attribute__((__packed__));
70 70
71struct blkif_x86_32_request_discard { 71struct blkif_x86_32_request_discard {
72 uint8_t flag; /* BLKIF_DISCARD_SECURE or zero */
73 blkif_vdev_t _pad1; /* was "handle" for read/write requests */
74 uint64_t id; /* private guest value, echoed in resp */
72 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ 75 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
73 uint64_t nr_sectors; 76 uint64_t nr_sectors;
74}; 77} __attribute__((__packed__));
75 78
76struct blkif_x86_32_request { 79struct blkif_x86_32_request {
77 uint8_t operation; /* BLKIF_OP_??? */ 80 uint8_t operation; /* BLKIF_OP_??? */
78 uint8_t nr_segments; /* number of segments */
79 blkif_vdev_t handle; /* only for read/write requests */
80 uint64_t id; /* private guest value, echoed in resp */
81 union { 81 union {
82 struct blkif_x86_32_request_rw rw; 82 struct blkif_x86_32_request_rw rw;
83 struct blkif_x86_32_request_discard discard; 83 struct blkif_x86_32_request_discard discard;
84 } u; 84 } u;
85}; 85} __attribute__((__packed__));
86
87/* i386 protocol version */
88#pragma pack(push, 4)
86struct blkif_x86_32_response { 89struct blkif_x86_32_response {
87 uint64_t id; /* copied from request */ 90 uint64_t id; /* copied from request */
88 uint8_t operation; /* copied from request */ 91 uint8_t operation; /* copied from request */
89 int16_t status; /* BLKIF_RSP_??? */ 92 int16_t status; /* BLKIF_RSP_??? */
90}; 93};
91#pragma pack(pop) 94#pragma pack(pop)
92
93/* x86_64 protocol version */ 95/* x86_64 protocol version */
94 96
95struct blkif_x86_64_request_rw { 97struct blkif_x86_64_request_rw {
98 uint8_t nr_segments; /* number of segments */
99 blkif_vdev_t handle; /* only for read/write requests */
100 uint32_t _pad1; /* offsetof(blkif_reqest..,u.rw.id)==8 */
101 uint64_t id;
96 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ 102 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
97 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 103 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
98}; 104} __attribute__((__packed__));
99 105
100struct blkif_x86_64_request_discard { 106struct blkif_x86_64_request_discard {
107 uint8_t flag; /* BLKIF_DISCARD_SECURE or zero */
108 blkif_vdev_t _pad1; /* was "handle" for read/write requests */
109 uint32_t _pad2; /* offsetof(blkif_..,u.discard.id)==8 */
110 uint64_t id;
101 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ 111 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
102 uint64_t nr_sectors; 112 uint64_t nr_sectors;
103}; 113} __attribute__((__packed__));
104 114
105struct blkif_x86_64_request { 115struct blkif_x86_64_request {
106 uint8_t operation; /* BLKIF_OP_??? */ 116 uint8_t operation; /* BLKIF_OP_??? */
107 uint8_t nr_segments; /* number of segments */
108 blkif_vdev_t handle; /* only for read/write requests */
109 uint64_t __attribute__((__aligned__(8))) id;
110 union { 117 union {
111 struct blkif_x86_64_request_rw rw; 118 struct blkif_x86_64_request_rw rw;
112 struct blkif_x86_64_request_discard discard; 119 struct blkif_x86_64_request_discard discard;
113 } u; 120 } u;
114}; 121} __attribute__((__packed__));
122
115struct blkif_x86_64_response { 123struct blkif_x86_64_response {
116 uint64_t __attribute__((__aligned__(8))) id; 124 uint64_t __attribute__((__aligned__(8))) id;
117 uint8_t operation; /* copied from request */ 125 uint8_t operation; /* copied from request */
@@ -156,6 +164,7 @@ struct xen_vbd {
156 /* Cached size parameter. */ 164 /* Cached size parameter. */
157 sector_t size; 165 sector_t size;
158 bool flush_support; 166 bool flush_support;
167 bool discard_secure;
159}; 168};
160 169
161struct backend_info; 170struct backend_info;
@@ -237,22 +246,23 @@ static inline void blkif_get_x86_32_req(struct blkif_request *dst,
237{ 246{
238 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; 247 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
239 dst->operation = src->operation; 248 dst->operation = src->operation;
240 dst->nr_segments = src->nr_segments;
241 dst->handle = src->handle;
242 dst->id = src->id;
243 switch (src->operation) { 249 switch (src->operation) {
244 case BLKIF_OP_READ: 250 case BLKIF_OP_READ:
245 case BLKIF_OP_WRITE: 251 case BLKIF_OP_WRITE:
246 case BLKIF_OP_WRITE_BARRIER: 252 case BLKIF_OP_WRITE_BARRIER:
247 case BLKIF_OP_FLUSH_DISKCACHE: 253 case BLKIF_OP_FLUSH_DISKCACHE:
254 dst->u.rw.nr_segments = src->u.rw.nr_segments;
255 dst->u.rw.handle = src->u.rw.handle;
256 dst->u.rw.id = src->u.rw.id;
248 dst->u.rw.sector_number = src->u.rw.sector_number; 257 dst->u.rw.sector_number = src->u.rw.sector_number;
249 barrier(); 258 barrier();
250 if (n > dst->nr_segments) 259 if (n > dst->u.rw.nr_segments)
251 n = dst->nr_segments; 260 n = dst->u.rw.nr_segments;
252 for (i = 0; i < n; i++) 261 for (i = 0; i < n; i++)
253 dst->u.rw.seg[i] = src->u.rw.seg[i]; 262 dst->u.rw.seg[i] = src->u.rw.seg[i];
254 break; 263 break;
255 case BLKIF_OP_DISCARD: 264 case BLKIF_OP_DISCARD:
265 dst->u.discard.flag = src->u.discard.flag;
256 dst->u.discard.sector_number = src->u.discard.sector_number; 266 dst->u.discard.sector_number = src->u.discard.sector_number;
257 dst->u.discard.nr_sectors = src->u.discard.nr_sectors; 267 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
258 break; 268 break;
@@ -266,22 +276,23 @@ static inline void blkif_get_x86_64_req(struct blkif_request *dst,
266{ 276{
267 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; 277 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
268 dst->operation = src->operation; 278 dst->operation = src->operation;
269 dst->nr_segments = src->nr_segments;
270 dst->handle = src->handle;
271 dst->id = src->id;
272 switch (src->operation) { 279 switch (src->operation) {
273 case BLKIF_OP_READ: 280 case BLKIF_OP_READ:
274 case BLKIF_OP_WRITE: 281 case BLKIF_OP_WRITE:
275 case BLKIF_OP_WRITE_BARRIER: 282 case BLKIF_OP_WRITE_BARRIER:
276 case BLKIF_OP_FLUSH_DISKCACHE: 283 case BLKIF_OP_FLUSH_DISKCACHE:
284 dst->u.rw.nr_segments = src->u.rw.nr_segments;
285 dst->u.rw.handle = src->u.rw.handle;
286 dst->u.rw.id = src->u.rw.id;
277 dst->u.rw.sector_number = src->u.rw.sector_number; 287 dst->u.rw.sector_number = src->u.rw.sector_number;
278 barrier(); 288 barrier();
279 if (n > dst->nr_segments) 289 if (n > dst->u.rw.nr_segments)
280 n = dst->nr_segments; 290 n = dst->u.rw.nr_segments;
281 for (i = 0; i < n; i++) 291 for (i = 0; i < n; i++)
282 dst->u.rw.seg[i] = src->u.rw.seg[i]; 292 dst->u.rw.seg[i] = src->u.rw.seg[i];
283 break; 293 break;
284 case BLKIF_OP_DISCARD: 294 case BLKIF_OP_DISCARD:
295 dst->u.discard.flag = src->u.discard.flag;
285 dst->u.discard.sector_number = src->u.discard.sector_number; 296 dst->u.discard.sector_number = src->u.discard.sector_number;
286 dst->u.discard.nr_sectors = src->u.discard.nr_sectors; 297 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
287 break; 298 break;
diff --git a/drivers/block/xen-blkback/xenbus.c b/drivers/block/xen-blkback/xenbus.c
index f759ad4584c3..187fd2c1a15d 100644
--- a/drivers/block/xen-blkback/xenbus.c
+++ b/drivers/block/xen-blkback/xenbus.c
@@ -338,6 +338,9 @@ static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
338 if (q && q->flush_flags) 338 if (q && q->flush_flags)
339 vbd->flush_support = true; 339 vbd->flush_support = true;
340 340
341 if (q && blk_queue_secdiscard(q))
342 vbd->discard_secure = true;
343
341 DPRINTK("Successful creation of handle=%04x (dom=%u)\n", 344 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
342 handle, blkif->domid); 345 handle, blkif->domid);
343 return 0; 346 return 0;
@@ -420,6 +423,15 @@ int xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
420 state = 1; 423 state = 1;
421 blkif->blk_backend_type = BLKIF_BACKEND_PHY; 424 blkif->blk_backend_type = BLKIF_BACKEND_PHY;
422 } 425 }
426 /* Optional. */
427 err = xenbus_printf(xbt, dev->nodename,
428 "discard-secure", "%d",
429 blkif->vbd.discard_secure);
430 if (err) {
431 xenbus_dev_fatal(dev, err,
432 "writting discard-secure");
433 goto kfree;
434 }
423 } 435 }
424 } else { 436 } else {
425 err = PTR_ERR(type); 437 err = PTR_ERR(type);
diff --git a/drivers/block/xen-blkfront.c b/drivers/block/xen-blkfront.c
index 7b2ec5908413..8cb0c27f2654 100644
--- a/drivers/block/xen-blkfront.c
+++ b/drivers/block/xen-blkfront.c
@@ -98,7 +98,8 @@ struct blkfront_info
98 unsigned long shadow_free; 98 unsigned long shadow_free;
99 unsigned int feature_flush; 99 unsigned int feature_flush;
100 unsigned int flush_op; 100 unsigned int flush_op;
101 unsigned int feature_discard; 101 unsigned int feature_discard:1;
102 unsigned int feature_secdiscard:1;
102 unsigned int discard_granularity; 103 unsigned int discard_granularity;
103 unsigned int discard_alignment; 104 unsigned int discard_alignment;
104 int is_ready; 105 int is_ready;
@@ -135,15 +136,15 @@ static int get_id_from_freelist(struct blkfront_info *info)
135{ 136{
136 unsigned long free = info->shadow_free; 137 unsigned long free = info->shadow_free;
137 BUG_ON(free >= BLK_RING_SIZE); 138 BUG_ON(free >= BLK_RING_SIZE);
138 info->shadow_free = info->shadow[free].req.id; 139 info->shadow_free = info->shadow[free].req.u.rw.id;
139 info->shadow[free].req.id = 0x0fffffee; /* debug */ 140 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
140 return free; 141 return free;
141} 142}
142 143
143static void add_id_to_freelist(struct blkfront_info *info, 144static void add_id_to_freelist(struct blkfront_info *info,
144 unsigned long id) 145 unsigned long id)
145{ 146{
146 info->shadow[id].req.id = info->shadow_free; 147 info->shadow[id].req.u.rw.id = info->shadow_free;
147 info->shadow[id].request = NULL; 148 info->shadow[id].request = NULL;
148 info->shadow_free = id; 149 info->shadow_free = id;
149} 150}
@@ -156,7 +157,7 @@ static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
156 if (end > nr_minors) { 157 if (end > nr_minors) {
157 unsigned long *bitmap, *old; 158 unsigned long *bitmap, *old;
158 159
159 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap), 160 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
160 GFP_KERNEL); 161 GFP_KERNEL);
161 if (bitmap == NULL) 162 if (bitmap == NULL)
162 return -ENOMEM; 163 return -ENOMEM;
@@ -287,9 +288,9 @@ static int blkif_queue_request(struct request *req)
287 id = get_id_from_freelist(info); 288 id = get_id_from_freelist(info);
288 info->shadow[id].request = req; 289 info->shadow[id].request = req;
289 290
290 ring_req->id = id; 291 ring_req->u.rw.id = id;
291 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req); 292 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
292 ring_req->handle = info->handle; 293 ring_req->u.rw.handle = info->handle;
293 294
294 ring_req->operation = rq_data_dir(req) ? 295 ring_req->operation = rq_data_dir(req) ?
295 BLKIF_OP_WRITE : BLKIF_OP_READ; 296 BLKIF_OP_WRITE : BLKIF_OP_READ;
@@ -305,16 +306,21 @@ static int blkif_queue_request(struct request *req)
305 ring_req->operation = info->flush_op; 306 ring_req->operation = info->flush_op;
306 } 307 }
307 308
308 if (unlikely(req->cmd_flags & REQ_DISCARD)) { 309 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
309 /* id, sector_number and handle are set above. */ 310 /* id, sector_number and handle are set above. */
310 ring_req->operation = BLKIF_OP_DISCARD; 311 ring_req->operation = BLKIF_OP_DISCARD;
311 ring_req->nr_segments = 0;
312 ring_req->u.discard.nr_sectors = blk_rq_sectors(req); 312 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
313 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
314 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
315 else
316 ring_req->u.discard.flag = 0;
313 } else { 317 } else {
314 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg); 318 ring_req->u.rw.nr_segments = blk_rq_map_sg(req->q, req,
315 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST); 319 info->sg);
320 BUG_ON(ring_req->u.rw.nr_segments >
321 BLKIF_MAX_SEGMENTS_PER_REQUEST);
316 322
317 for_each_sg(info->sg, sg, ring_req->nr_segments, i) { 323 for_each_sg(info->sg, sg, ring_req->u.rw.nr_segments, i) {
318 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg))); 324 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
319 fsect = sg->offset >> 9; 325 fsect = sg->offset >> 9;
320 lsect = fsect + (sg->length >> 9) - 1; 326 lsect = fsect + (sg->length >> 9) - 1;
@@ -424,6 +430,8 @@ static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
424 blk_queue_max_discard_sectors(rq, get_capacity(gd)); 430 blk_queue_max_discard_sectors(rq, get_capacity(gd));
425 rq->limits.discard_granularity = info->discard_granularity; 431 rq->limits.discard_granularity = info->discard_granularity;
426 rq->limits.discard_alignment = info->discard_alignment; 432 rq->limits.discard_alignment = info->discard_alignment;
433 if (info->feature_secdiscard)
434 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
427 } 435 }
428 436
429 /* Hard sector size and max sectors impersonate the equiv. hardware. */ 437 /* Hard sector size and max sectors impersonate the equiv. hardware. */
@@ -705,7 +713,9 @@ static void blkif_free(struct blkfront_info *info, int suspend)
705static void blkif_completion(struct blk_shadow *s) 713static void blkif_completion(struct blk_shadow *s)
706{ 714{
707 int i; 715 int i;
708 for (i = 0; i < s->req.nr_segments; i++) 716 /* Do not let BLKIF_OP_DISCARD as nr_segment is in the same place
717 * flag. */
718 for (i = 0; i < s->req.u.rw.nr_segments; i++)
709 gnttab_end_foreign_access(s->req.u.rw.seg[i].gref, 0, 0UL); 719 gnttab_end_foreign_access(s->req.u.rw.seg[i].gref, 0, 0UL);
710} 720}
711 721
@@ -736,7 +746,8 @@ static irqreturn_t blkif_interrupt(int irq, void *dev_id)
736 id = bret->id; 746 id = bret->id;
737 req = info->shadow[id].request; 747 req = info->shadow[id].request;
738 748
739 blkif_completion(&info->shadow[id]); 749 if (bret->operation != BLKIF_OP_DISCARD)
750 blkif_completion(&info->shadow[id]);
740 751
741 add_id_to_freelist(info, id); 752 add_id_to_freelist(info, id);
742 753
@@ -749,7 +760,9 @@ static irqreturn_t blkif_interrupt(int irq, void *dev_id)
749 info->gd->disk_name); 760 info->gd->disk_name);
750 error = -EOPNOTSUPP; 761 error = -EOPNOTSUPP;
751 info->feature_discard = 0; 762 info->feature_discard = 0;
763 info->feature_secdiscard = 0;
752 queue_flag_clear(QUEUE_FLAG_DISCARD, rq); 764 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
765 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
753 } 766 }
754 __blk_end_request_all(req, error); 767 __blk_end_request_all(req, error);
755 break; 768 break;
@@ -763,7 +776,7 @@ static irqreturn_t blkif_interrupt(int irq, void *dev_id)
763 error = -EOPNOTSUPP; 776 error = -EOPNOTSUPP;
764 } 777 }
765 if (unlikely(bret->status == BLKIF_RSP_ERROR && 778 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
766 info->shadow[id].req.nr_segments == 0)) { 779 info->shadow[id].req.u.rw.nr_segments == 0)) {
767 printk(KERN_WARNING "blkfront: %s: empty write %s op failed\n", 780 printk(KERN_WARNING "blkfront: %s: empty write %s op failed\n",
768 info->flush_op == BLKIF_OP_WRITE_BARRIER ? 781 info->flush_op == BLKIF_OP_WRITE_BARRIER ?
769 "barrier" : "flush disk cache", 782 "barrier" : "flush disk cache",
@@ -984,8 +997,8 @@ static int blkfront_probe(struct xenbus_device *dev,
984 INIT_WORK(&info->work, blkif_restart_queue); 997 INIT_WORK(&info->work, blkif_restart_queue);
985 998
986 for (i = 0; i < BLK_RING_SIZE; i++) 999 for (i = 0; i < BLK_RING_SIZE; i++)
987 info->shadow[i].req.id = i+1; 1000 info->shadow[i].req.u.rw.id = i+1;
988 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; 1001 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
989 1002
990 /* Front end dir is a number, which is used as the id. */ 1003 /* Front end dir is a number, which is used as the id. */
991 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); 1004 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
@@ -1019,9 +1032,9 @@ static int blkif_recover(struct blkfront_info *info)
1019 /* Stage 2: Set up free list. */ 1032 /* Stage 2: Set up free list. */
1020 memset(&info->shadow, 0, sizeof(info->shadow)); 1033 memset(&info->shadow, 0, sizeof(info->shadow));
1021 for (i = 0; i < BLK_RING_SIZE; i++) 1034 for (i = 0; i < BLK_RING_SIZE; i++)
1022 info->shadow[i].req.id = i+1; 1035 info->shadow[i].req.u.rw.id = i+1;
1023 info->shadow_free = info->ring.req_prod_pvt; 1036 info->shadow_free = info->ring.req_prod_pvt;
1024 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff; 1037 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
1025 1038
1026 /* Stage 3: Find pending requests and requeue them. */ 1039 /* Stage 3: Find pending requests and requeue them. */
1027 for (i = 0; i < BLK_RING_SIZE; i++) { 1040 for (i = 0; i < BLK_RING_SIZE; i++) {
@@ -1034,17 +1047,19 @@ static int blkif_recover(struct blkfront_info *info)
1034 *req = copy[i].req; 1047 *req = copy[i].req;
1035 1048
1036 /* We get a new request id, and must reset the shadow state. */ 1049 /* We get a new request id, and must reset the shadow state. */
1037 req->id = get_id_from_freelist(info); 1050 req->u.rw.id = get_id_from_freelist(info);
1038 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i])); 1051 memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
1039 1052
1053 if (req->operation != BLKIF_OP_DISCARD) {
1040 /* Rewrite any grant references invalidated by susp/resume. */ 1054 /* Rewrite any grant references invalidated by susp/resume. */
1041 for (j = 0; j < req->nr_segments; j++) 1055 for (j = 0; j < req->u.rw.nr_segments; j++)
1042 gnttab_grant_foreign_access_ref( 1056 gnttab_grant_foreign_access_ref(
1043 req->u.rw.seg[j].gref, 1057 req->u.rw.seg[j].gref,
1044 info->xbdev->otherend_id, 1058 info->xbdev->otherend_id,
1045 pfn_to_mfn(info->shadow[req->id].frame[j]), 1059 pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]),
1046 rq_data_dir(info->shadow[req->id].request)); 1060 rq_data_dir(info->shadow[req->u.rw.id].request));
1047 info->shadow[req->id].req = *req; 1061 }
1062 info->shadow[req->u.rw.id].req = *req;
1048 1063
1049 info->ring.req_prod_pvt++; 1064 info->ring.req_prod_pvt++;
1050 } 1065 }
@@ -1135,11 +1150,13 @@ static void blkfront_setup_discard(struct blkfront_info *info)
1135 char *type; 1150 char *type;
1136 unsigned int discard_granularity; 1151 unsigned int discard_granularity;
1137 unsigned int discard_alignment; 1152 unsigned int discard_alignment;
1153 unsigned int discard_secure;
1138 1154
1139 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL); 1155 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
1140 if (IS_ERR(type)) 1156 if (IS_ERR(type))
1141 return; 1157 return;
1142 1158
1159 info->feature_secdiscard = 0;
1143 if (strncmp(type, "phy", 3) == 0) { 1160 if (strncmp(type, "phy", 3) == 0) {
1144 err = xenbus_gather(XBT_NIL, info->xbdev->otherend, 1161 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1145 "discard-granularity", "%u", &discard_granularity, 1162 "discard-granularity", "%u", &discard_granularity,
@@ -1150,6 +1167,12 @@ static void blkfront_setup_discard(struct blkfront_info *info)
1150 info->discard_granularity = discard_granularity; 1167 info->discard_granularity = discard_granularity;
1151 info->discard_alignment = discard_alignment; 1168 info->discard_alignment = discard_alignment;
1152 } 1169 }
1170 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1171 "discard-secure", "%d", &discard_secure,
1172 NULL);
1173 if (!err)
1174 info->feature_secdiscard = discard_secure;
1175
1153 } else if (strncmp(type, "file", 4) == 0) 1176 } else if (strncmp(type, "file", 4) == 0)
1154 info->feature_discard = 1; 1177 info->feature_discard = 1;
1155 1178
diff --git a/include/xen/interface/io/blkif.h b/include/xen/interface/io/blkif.h
index 9324488f23f0..ee338bfde18b 100644
--- a/include/xen/interface/io/blkif.h
+++ b/include/xen/interface/io/blkif.h
@@ -84,6 +84,21 @@ typedef uint64_t blkif_sector_t;
84 * e07154r6-Data_Set_Management_Proposal_for_ATA-ACS2.doc 84 * e07154r6-Data_Set_Management_Proposal_for_ATA-ACS2.doc
85 * http://www.seagate.com/staticfiles/support/disc/manuals/ 85 * http://www.seagate.com/staticfiles/support/disc/manuals/
86 * Interface%20manuals/100293068c.pdf 86 * Interface%20manuals/100293068c.pdf
87 * The backend can optionally provide three extra XenBus attributes to
88 * further optimize the discard functionality:
89 * 'discard-aligment' - Devices that support discard functionality may
90 * internally allocate space in units that are bigger than the exported
91 * logical block size. The discard-alignment parameter indicates how many bytes
92 * the beginning of the partition is offset from the internal allocation unit's
93 * natural alignment.
94 * 'discard-granularity' - Devices that support discard functionality may
95 * internally allocate space using units that are bigger than the logical block
96 * size. The discard-granularity parameter indicates the size of the internal
97 * allocation unit in bytes if reported by the device. Otherwise the
98 * discard-granularity will be set to match the device's physical block size.
99 * 'discard-secure' - All copies of the discarded sectors (potentially created
100 * by garbage collection) must also be erased. To use this feature, the flag
101 * BLKIF_DISCARD_SECURE must be set in the blkif_request_trim.
87 */ 102 */
88#define BLKIF_OP_DISCARD 5 103#define BLKIF_OP_DISCARD 5
89 104
@@ -95,6 +110,12 @@ typedef uint64_t blkif_sector_t;
95#define BLKIF_MAX_SEGMENTS_PER_REQUEST 11 110#define BLKIF_MAX_SEGMENTS_PER_REQUEST 11
96 111
97struct blkif_request_rw { 112struct blkif_request_rw {
113 uint8_t nr_segments; /* number of segments */
114 blkif_vdev_t handle; /* only for read/write requests */
115#ifdef CONFIG_X86_64
116 uint32_t _pad1; /* offsetof(blkif_request,u.rw.id) == 8 */
117#endif
118 uint64_t id; /* private guest value, echoed in resp */
98 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ 119 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
99 struct blkif_request_segment { 120 struct blkif_request_segment {
100 grant_ref_t gref; /* reference to I/O buffer frame */ 121 grant_ref_t gref; /* reference to I/O buffer frame */
@@ -102,23 +123,28 @@ struct blkif_request_rw {
102 /* @last_sect: last sector in frame to transfer (inclusive). */ 123 /* @last_sect: last sector in frame to transfer (inclusive). */
103 uint8_t first_sect, last_sect; 124 uint8_t first_sect, last_sect;
104 } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 125 } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
105}; 126} __attribute__((__packed__));
106 127
107struct blkif_request_discard { 128struct blkif_request_discard {
129 uint8_t flag; /* BLKIF_DISCARD_SECURE or zero. */
130#define BLKIF_DISCARD_SECURE (1<<0) /* ignored if discard-secure=0 */
131 blkif_vdev_t _pad1; /* only for read/write requests */
132#ifdef CONFIG_X86_64
133 uint32_t _pad2; /* offsetof(blkif_req..,u.discard.id)==8*/
134#endif
135 uint64_t id; /* private guest value, echoed in resp */
108 blkif_sector_t sector_number; 136 blkif_sector_t sector_number;
109 uint64_t nr_sectors; 137 uint64_t nr_sectors;
110}; 138 uint8_t _pad3;
139} __attribute__((__packed__));
111 140
112struct blkif_request { 141struct blkif_request {
113 uint8_t operation; /* BLKIF_OP_??? */ 142 uint8_t operation; /* BLKIF_OP_??? */
114 uint8_t nr_segments; /* number of segments */
115 blkif_vdev_t handle; /* only for read/write requests */
116 uint64_t id; /* private guest value, echoed in resp */
117 union { 143 union {
118 struct blkif_request_rw rw; 144 struct blkif_request_rw rw;
119 struct blkif_request_discard discard; 145 struct blkif_request_discard discard;
120 } u; 146 } u;
121}; 147} __attribute__((__packed__));
122 148
123struct blkif_response { 149struct blkif_response {
124 uint64_t id; /* copied from request */ 150 uint64_t id; /* copied from request */