direct-io.hg

changeset 12333:4eaadb2ae198

[BLK] Add support for barrier writes to blkfront/blkback.

Protocol changes:
* There is a new operation (BLKIF_OP_WRITE_BARRIER)
to pass on barrier requests.
* There is a new state (BLKIF_RSP_EOPNOTSUPP) to indicate
unsupported operations (barrier writes may fail depending
on the underlying block device).
* A new xenstore node named "feature-barrier" indicates the
backend is able to handle barrier writes. The value can
be 1 (all is fine) or 0 (underlying block device doesn't
support barriers).

blkback changes: Add "feature-barrier" node to indicate barrier
support, pass incoming barrier requests to the block layer using
submit_bio(WRITE_BARRIER, bio). Some error handling fixes to
properly pass through barrier write failures, so the frontend
can turn off barriers then.

blkfront changes: Check if the backend sets "feature-barrier", if
present switch to QUEUE_ORDERED_DRAIN mode. Send off barrier
requests to the backend using the new BLKIF_OP_WRITE_BARRIER
operation. Also some error handling for the EOPNOTSUPP case.

Background: Barriers are needed to make journaling filesystems work
reliable. For some requests they need order constrains to make the
transactions work correctly. Barrier requests are used to pass that
ordering information to the block layer and/or to the device, so the
constrains are obeyed when reordering requests for better write
performance.

Signed-off-by: Gerd Hoffmann <kraxel@suse.de>
author kfraser@localhost.localdomain
date Thu Nov 09 16:49:59 2006 +0000 (2006-11-09)
parents d293b17cd1c4
children 7968f02ede38
files linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c linux-2.6-xen-sparse/drivers/xen/blkback/common.h linux-2.6-xen-sparse/drivers/xen/blkback/vbd.c linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c linux-2.6-xen-sparse/drivers/xen/blkfront/block.h linux-2.6-xen-sparse/drivers/xen/blkfront/vbd.c
line diff
     1.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c	Thu Nov 09 16:20:58 2006 +0000
     1.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/blkback.c	Thu Nov 09 16:49:59 2006 +0000
     1.3 @@ -189,9 +189,9 @@ static void fast_flush_area(pending_req_
     1.4  
     1.5  static void print_stats(blkif_t *blkif)
     1.6  {
     1.7 -	printk(KERN_DEBUG "%s: oo %3d  |  rd %4d  |  wr %4d\n",
     1.8 +	printk(KERN_DEBUG "%s: oo %3d  |  rd %4d  |  wr %4d  |  br %4d\n",
     1.9  	       current->comm, blkif->st_oo_req,
    1.10 -	       blkif->st_rd_req, blkif->st_wr_req);
    1.11 +	       blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req);
    1.12  	blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
    1.13  	blkif->st_rd_req = 0;
    1.14  	blkif->st_wr_req = 0;
    1.15 @@ -241,11 +241,17 @@ int blkif_schedule(void *arg)
    1.16   * COMPLETION CALLBACK -- Called as bh->b_end_io()
    1.17   */
    1.18  
    1.19 -static void __end_block_io_op(pending_req_t *pending_req, int uptodate)
    1.20 +static void __end_block_io_op(pending_req_t *pending_req, int error)
    1.21  {
    1.22  	/* An error fails the entire request. */
    1.23 -	if (!uptodate) {
    1.24 -		DPRINTK("Buffer not up-to-date at end of operation\n");
    1.25 +	if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
    1.26 +	    (error == -EOPNOTSUPP)) {
    1.27 +		DPRINTK("blkback: write barrier op failed, not supported\n");
    1.28 +		blkback_barrier(XBT_NIL, pending_req->blkif->be, 0);
    1.29 +		pending_req->status = BLKIF_RSP_EOPNOTSUPP;
    1.30 +	} else if (error) {
    1.31 +		DPRINTK("Buffer not up-to-date at end of operation, "
    1.32 +			"error=%d\n", error);
    1.33  		pending_req->status = BLKIF_RSP_ERROR;
    1.34  	}
    1.35  
    1.36 @@ -262,7 +268,7 @@ static int end_block_io_op(struct bio *b
    1.37  {
    1.38  	if (bio->bi_size != 0)
    1.39  		return 1;
    1.40 -	__end_block_io_op(bio->bi_private, !error);
    1.41 +	__end_block_io_op(bio->bi_private, error);
    1.42  	bio_put(bio);
    1.43  	return error;
    1.44  }
    1.45 @@ -319,6 +325,9 @@ static int do_block_io_op(blkif_t *blkif
    1.46  			blkif->st_rd_req++;
    1.47  			dispatch_rw_block_io(blkif, &req, pending_req);
    1.48  			break;
    1.49 +		case BLKIF_OP_WRITE_BARRIER:
    1.50 +			blkif->st_br_req++;
    1.51 +			/* fall through */
    1.52  		case BLKIF_OP_WRITE:
    1.53  			blkif->st_wr_req++;
    1.54  			dispatch_rw_block_io(blkif, &req, pending_req);
    1.55 @@ -340,7 +349,6 @@ static void dispatch_rw_block_io(blkif_t
    1.56  				 pending_req_t *pending_req)
    1.57  {
    1.58  	extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]);
    1.59 -	int operation = (req->operation == BLKIF_OP_WRITE) ? WRITE : READ;
    1.60  	struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
    1.61  	struct phys_req preq;
    1.62  	struct { 
    1.63 @@ -349,6 +357,22 @@ static void dispatch_rw_block_io(blkif_t
    1.64  	unsigned int nseg;
    1.65  	struct bio *bio = NULL, *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
    1.66  	int ret, i, nbio = 0;
    1.67 +	int operation;
    1.68 +
    1.69 +	switch (req->operation) {
    1.70 +	case BLKIF_OP_READ:
    1.71 +		operation = READ;
    1.72 +		break;
    1.73 +	case BLKIF_OP_WRITE:
    1.74 +		operation = WRITE;
    1.75 +		break;
    1.76 +	case BLKIF_OP_WRITE_BARRIER:
    1.77 +		operation = WRITE_BARRIER;
    1.78 +		break;
    1.79 +	default:
    1.80 +		operation = 0; /* make gcc happy */
    1.81 +		BUG();
    1.82 +	}
    1.83  
    1.84  	/* Check that number of segments is sane. */
    1.85  	nseg = req->nr_segments;
    1.86 @@ -364,7 +388,7 @@ static void dispatch_rw_block_io(blkif_t
    1.87  
    1.88  	pending_req->blkif     = blkif;
    1.89  	pending_req->id        = req->id;
    1.90 -	pending_req->operation = operation;
    1.91 +	pending_req->operation = req->operation;
    1.92  	pending_req->status    = BLKIF_RSP_OKAY;
    1.93  	pending_req->nr_pages  = nseg;
    1.94  
    1.95 @@ -380,7 +404,7 @@ static void dispatch_rw_block_io(blkif_t
    1.96  		preq.nr_sects += seg[i].nsec;
    1.97  
    1.98  		flags = GNTMAP_host_map;
    1.99 -		if ( operation == WRITE )
   1.100 +		if (operation != READ)
   1.101  			flags |= GNTMAP_readonly;
   1.102  		gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
   1.103  				  req->seg[i].gref, blkif->domid);
     2.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/common.h	Thu Nov 09 16:20:58 2006 +0000
     2.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/common.h	Thu Nov 09 16:49:59 2006 +0000
     2.3 @@ -44,6 +44,7 @@
     2.4  #include <xen/interface/io/ring.h>
     2.5  #include <xen/gnttab.h>
     2.6  #include <xen/driver_util.h>
     2.7 +#include <xen/xenbus.h>
     2.8  
     2.9  #define DPRINTK(_f, _a...)			\
    2.10  	pr_debug("(file=%s, line=%d) " _f,	\
    2.11 @@ -87,6 +88,7 @@ typedef struct blkif_st {
    2.12  	int                 st_rd_req;
    2.13  	int                 st_wr_req;
    2.14  	int                 st_oo_req;
    2.15 +	int                 st_br_req;
    2.16  
    2.17  	wait_queue_head_t waiting_to_free;
    2.18  
    2.19 @@ -131,4 +133,7 @@ void blkif_xenbus_init(void);
    2.20  irqreturn_t blkif_be_int(int irq, void *dev_id, struct pt_regs *regs);
    2.21  int blkif_schedule(void *arg);
    2.22  
    2.23 +int blkback_barrier(struct xenbus_transaction xbt,
    2.24 +		    struct backend_info *be, int state);
    2.25 +
    2.26  #endif /* __BLKIF__BACKEND__COMMON_H__ */
     3.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/vbd.c	Thu Nov 09 16:20:58 2006 +0000
     3.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/vbd.c	Thu Nov 09 16:49:59 2006 +0000
     3.3 @@ -31,7 +31,6 @@
     3.4   */
     3.5  
     3.6  #include "common.h"
     3.7 -#include <xen/xenbus.h>
     3.8  
     3.9  #define vbd_sz(_v)   ((_v)->bdev->bd_part ?				\
    3.10  	(_v)->bdev->bd_part->nr_sects : (_v)->bdev->bd_disk->capacity)
    3.11 @@ -104,7 +103,7 @@ int vbd_translate(struct phys_req *req, 
    3.12  	struct vbd *vbd = &blkif->vbd;
    3.13  	int rc = -EACCES;
    3.14  
    3.15 -	if ((operation == WRITE) && vbd->readonly)
    3.16 +	if ((operation != READ) && vbd->readonly)
    3.17  		goto out;
    3.18  
    3.19  	if (unlikely((req->sector_number + req->nr_sects) > vbd_sz(vbd)))
     4.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c	Thu Nov 09 16:20:58 2006 +0000
     4.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c	Thu Nov 09 16:49:59 2006 +0000
     4.3 @@ -20,7 +20,6 @@
     4.4  #include <stdarg.h>
     4.5  #include <linux/module.h>
     4.6  #include <linux/kthread.h>
     4.7 -#include <xen/xenbus.h>
     4.8  #include "common.h"
     4.9  
    4.10  #undef DPRINTK
    4.11 @@ -91,11 +90,13 @@ static void update_blkif_status(blkif_t 
    4.12  VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
    4.13  VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
    4.14  VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
    4.15 +VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
    4.16  
    4.17  static struct attribute *vbdstat_attrs[] = {
    4.18  	&dev_attr_oo_req.attr,
    4.19  	&dev_attr_rd_req.attr,
    4.20  	&dev_attr_wr_req.attr,
    4.21 +	&dev_attr_br_req.attr,
    4.22  	NULL
    4.23  };
    4.24  
    4.25 @@ -165,6 +166,19 @@ static int blkback_remove(struct xenbus_
    4.26  	return 0;
    4.27  }
    4.28  
    4.29 +int blkback_barrier(struct xenbus_transaction xbt,
    4.30 +		    struct backend_info *be, int state)
    4.31 +{
    4.32 +	struct xenbus_device *dev = be->dev;
    4.33 +	int err;
    4.34 +
    4.35 +	err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
    4.36 +			    "%d", state);
    4.37 +	if (err)
    4.38 +		xenbus_dev_fatal(dev, err, "writing feature-barrier");
    4.39 +
    4.40 +	return err;
    4.41 +}
    4.42  
    4.43  /**
    4.44   * Entry point to this code when a new device is created.  Allocate the basic
    4.45 @@ -366,12 +380,15 @@ static void connect(struct backend_info 
    4.46  	/* Supply the information about the device the frontend needs */
    4.47  again:
    4.48  	err = xenbus_transaction_start(&xbt);
    4.49 -
    4.50  	if (err) {
    4.51  		xenbus_dev_fatal(dev, err, "starting transaction");
    4.52  		return;
    4.53  	}
    4.54  
    4.55 +	err = blkback_barrier(xbt, be, 1);
    4.56 +	if (err)
    4.57 +		goto abort;
    4.58 +
    4.59  	err = xenbus_printf(xbt, dev->nodename, "sectors", "%lu",
    4.60  			    vbd_size(&be->blkif->vbd));
    4.61  	if (err) {
     5.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c	Thu Nov 09 16:20:58 2006 +0000
     5.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkfront/blkfront.c	Thu Nov 09 16:49:59 2006 +0000
     5.3 @@ -320,6 +320,12 @@ static void connect(struct blkfront_info
     5.4  		return;
     5.5  	}
     5.6  
     5.7 +	err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
     5.8 +			    "feature-barrier", "%lu", &info->feature_barrier,
     5.9 +			    NULL);
    5.10 +	if (err)
    5.11 +		info->feature_barrier = 0;
    5.12 +
    5.13  	err = xlvbd_add(sectors, info->vdevice, binfo, sector_size, info);
    5.14  	if (err) {
    5.15  		xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
    5.16 @@ -569,10 +575,13 @@ static int blkif_queue_request(struct re
    5.17  	info->shadow[id].request = (unsigned long)req;
    5.18  
    5.19  	ring_req->id = id;
    5.20 +	ring_req->sector_number = (blkif_sector_t)req->sector;
    5.21 +	ring_req->handle = info->handle;
    5.22 +
    5.23  	ring_req->operation = rq_data_dir(req) ?
    5.24  		BLKIF_OP_WRITE : BLKIF_OP_READ;
    5.25 -	ring_req->sector_number = (blkif_sector_t)req->sector;
    5.26 -	ring_req->handle = info->handle;
    5.27 +	if (blk_barrier_rq(req))
    5.28 +		ring_req->operation = BLKIF_OP_WRITE_BARRIER;
    5.29  
    5.30  	ring_req->nr_segments = 0;
    5.31  	rq_for_each_bio (bio, req) {
    5.32 @@ -670,6 +679,7 @@ static irqreturn_t blkif_int(int irq, vo
    5.33  	RING_IDX i, rp;
    5.34  	unsigned long flags;
    5.35  	struct blkfront_info *info = (struct blkfront_info *)dev_id;
    5.36 +	int uptodate;
    5.37  
    5.38  	spin_lock_irqsave(&blkif_io_lock, flags);
    5.39  
    5.40 @@ -694,19 +704,27 @@ static irqreturn_t blkif_int(int irq, vo
    5.41  
    5.42  		ADD_ID_TO_FREELIST(info, id);
    5.43  
    5.44 +		uptodate = (bret->status == BLKIF_RSP_OKAY);
    5.45  		switch (bret->operation) {
    5.46 +		case BLKIF_OP_WRITE_BARRIER:
    5.47 +			if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
    5.48 +				printk("blkfront: %s: write barrier op failed\n",
    5.49 +				       info->gd->disk_name);
    5.50 +				uptodate = -EOPNOTSUPP;
    5.51 +				info->feature_barrier = 0;
    5.52 +			        xlvbd_barrier(info);
    5.53 +			}
    5.54 +			/* fall through */
    5.55  		case BLKIF_OP_READ:
    5.56  		case BLKIF_OP_WRITE:
    5.57  			if (unlikely(bret->status != BLKIF_RSP_OKAY))
    5.58  				DPRINTK("Bad return from blkdev data "
    5.59  					"request: %x\n", bret->status);
    5.60  
    5.61 -			ret = end_that_request_first(
    5.62 -				req, (bret->status == BLKIF_RSP_OKAY),
    5.63 +			ret = end_that_request_first(req, uptodate,
    5.64  				req->hard_nr_sectors);
    5.65  			BUG_ON(ret);
    5.66 -			end_that_request_last(
    5.67 -				req, (bret->status == BLKIF_RSP_OKAY));
    5.68 +			end_that_request_last(req, uptodate);
    5.69  			break;
    5.70  		default:
    5.71  			BUG();
     6.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkfront/block.h	Thu Nov 09 16:20:58 2006 +0000
     6.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkfront/block.h	Thu Nov 09 16:49:59 2006 +0000
     6.3 @@ -126,6 +126,7 @@ struct blkfront_info
     6.4  	struct gnttab_free_callback callback;
     6.5  	struct blk_shadow shadow[BLK_RING_SIZE];
     6.6  	unsigned long shadow_free;
     6.7 +	int feature_barrier;
     6.8  
     6.9  	/**
    6.10  	 * The number of people holding this device open.  We won't allow a
    6.11 @@ -152,5 +153,6 @@ extern void do_blkif_request (request_qu
    6.12  int xlvbd_add(blkif_sector_t capacity, int device,
    6.13  	      u16 vdisk_info, u16 sector_size, struct blkfront_info *info);
    6.14  void xlvbd_del(struct blkfront_info *info);
    6.15 +int xlvbd_barrier(struct blkfront_info *info);
    6.16  
    6.17  #endif /* __XEN_DRIVERS_BLOCK_H__ */
     7.1 --- a/linux-2.6-xen-sparse/drivers/xen/blkfront/vbd.c	Thu Nov 09 16:20:58 2006 +0000
     7.2 +++ b/linux-2.6-xen-sparse/drivers/xen/blkfront/vbd.c	Thu Nov 09 16:49:59 2006 +0000
     7.3 @@ -289,6 +289,10 @@ xlvbd_alloc_gendisk(int minor, blkif_sec
     7.4  	}
     7.5  
     7.6  	info->rq = gd->queue;
     7.7 +	info->gd = gd;
     7.8 +
     7.9 +	if (info->feature_barrier)
    7.10 +		xlvbd_barrier(info);
    7.11  
    7.12  	if (vdisk_info & VDISK_READONLY)
    7.13  		set_disk_ro(gd, 1);
    7.14 @@ -299,8 +303,6 @@ xlvbd_alloc_gendisk(int minor, blkif_sec
    7.15  	if (vdisk_info & VDISK_CDROM)
    7.16  		gd->flags |= GENHD_FL_CD;
    7.17  
    7.18 -	info->gd = gd;
    7.19 -
    7.20  	return 0;
    7.21  
    7.22   out:
    7.23 @@ -348,3 +350,17 @@ xlvbd_del(struct blkfront_info *info)
    7.24  	blk_cleanup_queue(info->rq);
    7.25  	info->rq = NULL;
    7.26  }
    7.27 +
    7.28 +int
    7.29 +xlvbd_barrier(struct blkfront_info *info)
    7.30 +{
    7.31 +	int err;
    7.32 +
    7.33 +	err = blk_queue_ordered(info->rq,
    7.34 +		info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE, NULL);
    7.35 +	if (err)
    7.36 +		return err;
    7.37 +	printk("blkfront: %s: barriers %s\n",
    7.38 +	       info->gd->disk_name, info->feature_barrier ? "enabled" : "disabled");
    7.39 +	return 0;
    7.40 +}