Skip to content

Commit b3cb0d6

Browse files
Li Dongyangkonradwilk
authored andcommitted
xen-blkback: Implement discard requests ('feature-discard')
..aka ATA TRIM/SCSI UNMAP command to be passed through the frontend and used as appropiately by the backend. We also advertise certain granulity parameters to the frontend so it can plug them in. If the backend is a realy device - we just end up using 'blkdev_issue_discard' while for loopback devices - we just punch a hole in the image file. Signed-off-by: Li Dongyang <lidongyang@novell.com> [v1: Fixed up pr_debug and commit description] Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent 32a8d26 commit b3cb0d6

3 files changed

Lines changed: 206 additions & 31 deletions

File tree

‎drivers/block/xen-blkback/blkback.c‎

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#include <linux/list.h>
4040
#include <linux/delay.h>
4141
#include <linux/freezer.h>
42+
#include <linux/loop.h>
43+
#include <linux/falloc.h>
44+
#include <linux/fs.h>
4245

4346
#include <xen/events.h>
4447
#include <xen/page.h>
@@ -258,13 +261,16 @@ irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
258261

259262
static void print_stats(struct xen_blkif *blkif)
260263
{
261-
pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d\n",
264+
pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d"
265+
" | ds %4d\n",
262266
current->comm, blkif->st_oo_req,
263-
blkif->st_rd_req, blkif->st_wr_req, blkif->st_f_req);
267+
blkif->st_rd_req, blkif->st_wr_req,
268+
blkif->st_f_req, blkif->st_ds_req);
264269
blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
265270
blkif->st_rd_req = 0;
266271
blkif->st_wr_req = 0;
267272
blkif->st_oo_req = 0;
273+
blkif->st_ds_req = 0;
268274
}
269275

270276
int xen_blkif_schedule(void *arg)
@@ -410,6 +416,42 @@ static int xen_blkbk_map(struct blkif_request *req,
410416
return ret;
411417
}
412418

419+
static void xen_blk_discard(struct xen_blkif *blkif, struct blkif_request *req)
420+
{
421+
int err = 0;
422+
int status = BLKIF_RSP_OKAY;
423+
struct block_device *bdev = blkif->vbd.bdev;
424+
425+
if (blkif->blk_backend_type == BLKIF_BACKEND_PHY)
426+
/* just forward the discard request */
427+
err = blkdev_issue_discard(bdev,
428+
req->u.discard.sector_number,
429+
req->u.discard.nr_sectors,
430+
GFP_KERNEL, 0);
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
444+
err = -EOPNOTSUPP;
445+
446+
if (err == -EOPNOTSUPP) {
447+
pr_debug(DRV_PFX "discard op failed, not supported\n");
448+
status = BLKIF_RSP_EOPNOTSUPP;
449+
} else if (err)
450+
status = BLKIF_RSP_ERROR;
451+
452+
make_response(blkif, req->id, req->operation, status);
453+
}
454+
413455
/*
414456
* Completion callback on the bio's. Called as bh->b_end_io()
415457
*/
@@ -563,6 +605,10 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
563605
blkif->st_f_req++;
564606
operation = WRITE_FLUSH;
565607
break;
608+
case BLKIF_OP_DISCARD:
609+
blkif->st_ds_req++;
610+
operation = REQ_DISCARD;
611+
break;
566612
case BLKIF_OP_WRITE_BARRIER:
567613
default:
568614
operation = 0; /* make gcc happy */
@@ -572,7 +618,8 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
572618

573619
/* Check that the number of segments is sane. */
574620
nseg = req->nr_segments;
575-
if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
621+
if (unlikely(nseg == 0 && operation != WRITE_FLUSH &&
622+
operation != REQ_DISCARD) ||
576623
unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
577624
pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
578625
nseg);
@@ -627,10 +674,14 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
627674
* the hypercall to unmap the grants - that is all done in
628675
* xen_blkbk_unmap.
629676
*/
630-
if (xen_blkbk_map(req, pending_req, seg))
677+
if (operation != BLKIF_OP_DISCARD &&
678+
xen_blkbk_map(req, pending_req, seg))
631679
goto fail_flush;
632680

633-
/* This corresponding xen_blkif_put is done in __end_block_io_op */
681+
/*
682+
* This corresponding xen_blkif_put is done in __end_block_io_op, or
683+
* below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
684+
*/
634685
xen_blkif_get(blkif);
635686

636687
for (i = 0; i < nseg; i++) {
@@ -654,18 +705,25 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
654705
preq.sector_number += seg[i].nsec;
655706
}
656707

657-
/* This will be hit if the operation was a flush. */
708+
/* This will be hit if the operation was a flush or discard. */
658709
if (!bio) {
659-
BUG_ON(operation != WRITE_FLUSH);
710+
BUG_ON(operation != WRITE_FLUSH && operation != REQ_DISCARD);
660711

661-
bio = bio_alloc(GFP_KERNEL, 0);
662-
if (unlikely(bio == NULL))
663-
goto fail_put_bio;
712+
if (operation == WRITE_FLUSH) {
713+
bio = bio_alloc(GFP_KERNEL, 0);
714+
if (unlikely(bio == NULL))
715+
goto fail_put_bio;
664716

665-
biolist[nbio++] = bio;
666-
bio->bi_bdev = preq.bdev;
667-
bio->bi_private = pending_req;
668-
bio->bi_end_io = end_block_io_op;
717+
biolist[nbio++] = bio;
718+
bio->bi_bdev = preq.bdev;
719+
bio->bi_private = pending_req;
720+
bio->bi_end_io = end_block_io_op;
721+
} else if (operation == REQ_DISCARD) {
722+
xen_blk_discard(blkif, req);
723+
xen_blkif_put(blkif);
724+
free_req(pending_req);
725+
return 0;
726+
}
669727
}
670728

671729
/*

‎drivers/block/xen-blkback/common.h‎

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,26 @@ struct blkif_common_response {
6363

6464
/* i386 protocol version */
6565
#pragma pack(push, 4)
66+
67+
struct blkif_x86_32_request_rw {
68+
blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
69+
struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
70+
};
71+
72+
struct blkif_x86_32_request_discard {
73+
blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
74+
uint64_t nr_sectors;
75+
};
76+
6677
struct blkif_x86_32_request {
6778
uint8_t operation; /* BLKIF_OP_??? */
6879
uint8_t nr_segments; /* number of segments */
6980
blkif_vdev_t handle; /* only for read/write requests */
7081
uint64_t id; /* private guest value, echoed in resp */
71-
blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
72-
struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
82+
union {
83+
struct blkif_x86_32_request_rw rw;
84+
struct blkif_x86_32_request_discard discard;
85+
} u;
7386
};
7487
struct blkif_x86_32_response {
7588
uint64_t id; /* copied from request */
@@ -79,13 +92,26 @@ struct blkif_x86_32_response {
7992
#pragma pack(pop)
8093

8194
/* x86_64 protocol version */
95+
96+
struct blkif_x86_64_request_rw {
97+
blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
98+
struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
99+
};
100+
101+
struct blkif_x86_64_request_discard {
102+
blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
103+
uint64_t nr_sectors;
104+
};
105+
82106
struct blkif_x86_64_request {
83107
uint8_t operation; /* BLKIF_OP_??? */
84108
uint8_t nr_segments; /* number of segments */
85109
blkif_vdev_t handle; /* only for read/write requests */
86110
uint64_t __attribute__((__aligned__(8))) id;
87-
blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
88-
struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
111+
union {
112+
struct blkif_x86_64_request_rw rw;
113+
struct blkif_x86_64_request_discard discard;
114+
} u;
89115
};
90116
struct blkif_x86_64_response {
91117
uint64_t __attribute__((__aligned__(8))) id;
@@ -113,6 +139,11 @@ enum blkif_protocol {
113139
BLKIF_PROTOCOL_X86_64 = 3,
114140
};
115141

142+
enum blkif_backend_type {
143+
BLKIF_BACKEND_PHY = 1,
144+
BLKIF_BACKEND_FILE = 2,
145+
};
146+
116147
struct xen_vbd {
117148
/* What the domain refers to this vbd as. */
118149
blkif_vdev_t handle;
@@ -138,6 +169,7 @@ struct xen_blkif {
138169
unsigned int irq;
139170
/* Comms information. */
140171
enum blkif_protocol blk_protocol;
172+
enum blkif_backend_type blk_backend_type;
141173
union blkif_back_rings blk_rings;
142174
struct vm_struct *blk_ring_area;
143175
/* The VBD attached to this interface. */
@@ -159,6 +191,7 @@ struct xen_blkif {
159191
int st_wr_req;
160192
int st_oo_req;
161193
int st_f_req;
194+
int st_ds_req;
162195
int st_rd_sect;
163196
int st_wr_sect;
164197

@@ -182,7 +215,7 @@ struct xen_blkif {
182215

183216
struct phys_req {
184217
unsigned short dev;
185-
unsigned short nr_sects;
218+
blkif_sector_t nr_sects;
186219
struct block_device *bdev;
187220
blkif_sector_t sector_number;
188221
};
@@ -206,12 +239,25 @@ static inline void blkif_get_x86_32_req(struct blkif_request *dst,
206239
dst->nr_segments = src->nr_segments;
207240
dst->handle = src->handle;
208241
dst->id = src->id;
209-
dst->u.rw.sector_number = src->sector_number;
210-
barrier();
211-
if (n > dst->nr_segments)
212-
n = dst->nr_segments;
213-
for (i = 0; i < n; i++)
214-
dst->u.rw.seg[i] = src->seg[i];
242+
switch (src->operation) {
243+
case BLKIF_OP_READ:
244+
case BLKIF_OP_WRITE:
245+
case BLKIF_OP_WRITE_BARRIER:
246+
case BLKIF_OP_FLUSH_DISKCACHE:
247+
dst->u.rw.sector_number = src->u.rw.sector_number;
248+
barrier();
249+
if (n > dst->nr_segments)
250+
n = dst->nr_segments;
251+
for (i = 0; i < n; i++)
252+
dst->u.rw.seg[i] = src->u.rw.seg[i];
253+
break;
254+
case BLKIF_OP_DISCARD:
255+
dst->u.discard.sector_number = src->u.discard.sector_number;
256+
dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
257+
break;
258+
default:
259+
break;
260+
}
215261
}
216262

217263
static inline void blkif_get_x86_64_req(struct blkif_request *dst,
@@ -222,12 +268,25 @@ static inline void blkif_get_x86_64_req(struct blkif_request *dst,
222268
dst->nr_segments = src->nr_segments;
223269
dst->handle = src->handle;
224270
dst->id = src->id;
225-
dst->u.rw.sector_number = src->sector_number;
226-
barrier();
227-
if (n > dst->nr_segments)
228-
n = dst->nr_segments;
229-
for (i = 0; i < n; i++)
230-
dst->u.rw.seg[i] = src->seg[i];
271+
switch (src->operation) {
272+
case BLKIF_OP_READ:
273+
case BLKIF_OP_WRITE:
274+
case BLKIF_OP_WRITE_BARRIER:
275+
case BLKIF_OP_FLUSH_DISKCACHE:
276+
dst->u.rw.sector_number = src->u.rw.sector_number;
277+
barrier();
278+
if (n > dst->nr_segments)
279+
n = dst->nr_segments;
280+
for (i = 0; i < n; i++)
281+
dst->u.rw.seg[i] = src->u.rw.seg[i];
282+
break;
283+
case BLKIF_OP_DISCARD:
284+
dst->u.discard.sector_number = src->u.discard.sector_number;
285+
dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
286+
break;
287+
default:
288+
break;
289+
}
231290
}
232291

233292
#endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */

‎drivers/block/xen-blkback/xenbus.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
272272
VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
273273
VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
274274
VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req);
275+
VBD_SHOW(ds_req, "%d\n", be->blkif->st_ds_req);
275276
VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
276277
VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
277278

@@ -280,6 +281,7 @@ static struct attribute *xen_vbdstat_attrs[] = {
280281
&dev_attr_rd_req.attr,
281282
&dev_attr_wr_req.attr,
282283
&dev_attr_f_req.attr,
284+
&dev_attr_ds_req.attr,
283285
&dev_attr_rd_sect.attr,
284286
&dev_attr_wr_sect.attr,
285287
NULL
@@ -419,6 +421,60 @@ int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
419421
return err;
420422
}
421423

424+
int xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
425+
{
426+
struct xenbus_device *dev = be->dev;
427+
struct xen_blkif *blkif = be->blkif;
428+
char *type;
429+
int err;
430+
int state = 0;
431+
432+
type = xenbus_read(XBT_NIL, dev->nodename, "type", NULL);
433+
if (!IS_ERR(type)) {
434+
if (strncmp(type, "file", 4) == 0) {
435+
state = 1;
436+
blkif->blk_backend_type = BLKIF_BACKEND_FILE;
437+
}
438+
if (strncmp(type, "phy", 3) == 0) {
439+
struct block_device *bdev = be->blkif->vbd.bdev;
440+
struct request_queue *q = bdev_get_queue(bdev);
441+
if (blk_queue_discard(q)) {
442+
err = xenbus_printf(xbt, dev->nodename,
443+
"discard-granularity", "%u",
444+
q->limits.discard_granularity);
445+
if (err) {
446+
xenbus_dev_fatal(dev, err,
447+
"writing discard-granularity");
448+
goto kfree;
449+
}
450+
err = xenbus_printf(xbt, dev->nodename,
451+
"discard-alignment", "%u",
452+
q->limits.discard_alignment);
453+
if (err) {
454+
xenbus_dev_fatal(dev, err,
455+
"writing discard-alignment");
456+
goto kfree;
457+
}
458+
state = 1;
459+
blkif->blk_backend_type = BLKIF_BACKEND_PHY;
460+
}
461+
}
462+
} else {
463+
err = PTR_ERR(type);
464+
xenbus_dev_fatal(dev, err, "reading type");
465+
goto out;
466+
}
467+
468+
err = xenbus_printf(xbt, dev->nodename, "feature-discard",
469+
"%d", state);
470+
if (err)
471+
xenbus_dev_fatal(dev, err, "writing feature-discard");
472+
kfree:
473+
kfree(type);
474+
out:
475+
return err;
476+
}
477+
422478
/*
423479
* Entry point to this code when a new device is created. Allocate the basic
424480
* structures, and watch the store waiting for the hotplug scripts to tell us
@@ -650,6 +706,8 @@ static void connect(struct backend_info *be)
650706
if (err)
651707
goto abort;
652708

709+
err = xen_blkbk_discard(xbt, be);
710+
653711
err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
654712
(unsigned long long)vbd_sz(&be->blkif->vbd));
655713
if (err) {

0 commit comments

Comments
 (0)