1 /******************************************************************************
2 * arch/xen/drivers/blkif/backend/main.c
4 * Back-end of the driver for virtual block devices. This portion of the
5 * driver exports a 'unified' block-device interface that can be accessed
6 * by any operating system that implements a compatible front end. A
7 * reference front-end implementation can be found in:
8 * arch/xen/drivers/blkif/frontend
10 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
11 * Copyright (c) 2005, Christopher Clark
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation; or, when distributed
16 * separately from the Linux kernel or incorporated into other
17 * software packages, subject to the following license:
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this source file (the "Software"), to deal in the Software without
21 * restriction, including without limitation the rights to use, copy, modify,
22 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
23 * and to permit persons to whom the Software is furnished to do so, subject to
24 * the following conditions:
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
38 #include <linux/spinlock.h>
39 #include <linux/kthread.h>
40 #include <linux/list.h>
41 #include <linux/delay.h>
42 #include <linux/freezer.h>
44 #include <xen/balloon.h>
45 #include <xen/events.h>
47 #include <asm/xen/hypervisor.h>
48 #include <asm/xen/hypercall.h>
52 * These are rather arbitrary. They are fairly large because adjacent requests
53 * pulled from a communication ring are quite likely to end up being part of
54 * the same scatter/gather request at the disc.
56 * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
58 * This will increase the chances of being able to write whole tracks.
59 * 64 should be enough to keep us competitive with Linux.
61 static int blkif_reqs = 64;
62 module_param_named(reqs, blkif_reqs, int, 0);
63 MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
65 /* Run-time switchable: /sys/module/blkback/parameters/ */
66 static unsigned int log_stats = 0;
67 static unsigned int debug_lvl = 0;
68 module_param(log_stats, int, 0644);
69 module_param(debug_lvl, int, 0644);
72 * Each outstanding request that we've passed to the lower device layers has a
73 * 'pending_req' allocated to it. Each buffer_head that completes decrements
74 * the pendcnt towards zero. When it hits zero, the specified domain has a
75 * response queued for it, with the saved 'id' passed back.
82 unsigned short operation;
84 struct list_head free_list;
87 #define BLKBACK_INVALID_HANDLE (~0)
90 pending_req_t *pending_reqs;
91 struct list_head pending_free;
92 spinlock_t pending_free_lock;
93 wait_queue_head_t pending_free_wq;
94 struct page **pending_pages;
95 grant_handle_t *pending_grant_handles;
98 static struct xen_blkbk *blkbk;
100 static inline int vaddr_pagenr(pending_req_t *req, int seg)
102 return (req - blkbk->pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
105 #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
107 static inline unsigned long vaddr(pending_req_t *req, int seg)
109 unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
110 return (unsigned long)pfn_to_kaddr(pfn);
113 #define pending_handle(_req, _seg) \
114 (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
117 static int do_block_io_op(blkif_t *blkif);
118 static void dispatch_rw_block_io(blkif_t *blkif,
119 struct blkif_request *req,
120 pending_req_t *pending_req);
121 static void make_response(blkif_t *blkif, u64 id,
122 unsigned short op, int st);
124 /******************************************************************
127 static pending_req_t* alloc_req(void)
129 pending_req_t *req = NULL;
132 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
133 if (!list_empty(&blkbk->pending_free)) {
134 req = list_entry(blkbk->pending_free.next, pending_req_t, free_list);
135 list_del(&req->free_list);
137 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
141 static void free_req(pending_req_t *req)
146 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
147 was_empty = list_empty(&blkbk->pending_free);
148 list_add(&req->free_list, &blkbk->pending_free);
149 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
151 wake_up(&blkbk->pending_free_wq);
154 static void unplug_queue(blkif_t *blkif)
156 if (blkif->plug == NULL)
158 if (blkif->plug->unplug_fn)
159 blkif->plug->unplug_fn(blkif->plug);
160 blk_put_queue(blkif->plug);
164 static void plug_queue(blkif_t *blkif, struct block_device *bdev)
166 struct request_queue *q = bdev_get_queue(bdev);
168 if (q == blkif->plug)
175 static void fast_flush_area(pending_req_t *req)
177 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
178 unsigned int i, invcount = 0;
179 grant_handle_t handle;
182 for (i = 0; i < req->nr_pages; i++) {
183 handle = pending_handle(req, i);
184 if (handle == BLKBACK_INVALID_HANDLE)
186 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
187 GNTMAP_host_map, handle);
188 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
192 ret = HYPERVISOR_grant_table_op(
193 GNTTABOP_unmap_grant_ref, unmap, invcount);
197 /******************************************************************
198 * SCHEDULER FUNCTIONS
201 static void print_stats(blkif_t *blkif)
203 printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d | br %4d\n",
204 current->comm, blkif->st_oo_req,
205 blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req);
206 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
207 blkif->st_rd_req = 0;
208 blkif->st_wr_req = 0;
209 blkif->st_oo_req = 0;
212 int blkif_schedule(void *arg)
214 blkif_t *blkif = arg;
215 struct vbd *vbd = &blkif->vbd;
220 printk(KERN_DEBUG "%s: started\n", current->comm);
222 while (!kthread_should_stop()) {
225 if (unlikely(vbd->size != vbd_size(vbd)))
228 wait_event_interruptible(
230 blkif->waiting_reqs || kthread_should_stop());
231 wait_event_interruptible(
232 blkbk->pending_free_wq,
233 !list_empty(&blkbk->pending_free) || kthread_should_stop());
235 blkif->waiting_reqs = 0;
236 smp_mb(); /* clear flag *before* checking for work */
238 if (do_block_io_op(blkif))
239 blkif->waiting_reqs = 1;
242 if (log_stats && time_after(jiffies, blkif->st_print))
249 printk(KERN_DEBUG "%s: exiting\n", current->comm);
251 blkif->xenblkd = NULL;
257 /******************************************************************
258 * COMPLETION CALLBACK -- Called as bh->b_end_io()
261 static void __end_block_io_op(pending_req_t *pending_req, int error)
263 /* An error fails the entire request. */
264 if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
265 (error == -EOPNOTSUPP)) {
266 DPRINTK("blkback: write barrier op failed, not supported\n");
267 blkback_barrier(XBT_NIL, pending_req->blkif->be, 0);
268 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
270 DPRINTK("Buffer not up-to-date at end of operation, "
271 "error=%d\n", error);
272 pending_req->status = BLKIF_RSP_ERROR;
275 if (atomic_dec_and_test(&pending_req->pendcnt)) {
276 fast_flush_area(pending_req);
277 make_response(pending_req->blkif, pending_req->id,
278 pending_req->operation, pending_req->status);
279 blkif_put(pending_req->blkif);
280 free_req(pending_req);
284 static void end_block_io_op(struct bio *bio, int error)
286 __end_block_io_op(bio->bi_private, error);
291 /******************************************************************************
292 * NOTIFICATION FROM GUEST OS.
295 static void blkif_notify_work(blkif_t *blkif)
297 blkif->waiting_reqs = 1;
301 irqreturn_t blkif_be_int(int irq, void *dev_id)
303 blkif_notify_work(dev_id);
309 /******************************************************************
310 * DOWNWARD CALLS -- These interface with the block-device layer proper.
313 static int do_block_io_op(blkif_t *blkif)
315 union blkif_back_rings *blk_rings = &blkif->blk_rings;
316 struct blkif_request req;
317 pending_req_t *pending_req;
321 rc = blk_rings->common.req_cons;
322 rp = blk_rings->common.sring->req_prod;
323 rmb(); /* Ensure we see queued requests up to 'rp'. */
327 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
330 if (kthread_should_stop()) {
335 pending_req = alloc_req();
336 if (NULL == pending_req) {
342 switch (blkif->blk_protocol) {
343 case BLKIF_PROTOCOL_NATIVE:
344 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
346 case BLKIF_PROTOCOL_X86_32:
347 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
349 case BLKIF_PROTOCOL_X86_64:
350 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
355 blk_rings->common.req_cons = ++rc; /* before make_response() */
357 /* Apply all sanity checks to /private copy/ of request. */
360 switch (req.operation) {
363 dispatch_rw_block_io(blkif, &req, pending_req);
365 case BLKIF_OP_WRITE_BARRIER:
370 dispatch_rw_block_io(blkif, &req, pending_req);
373 /* A good sign something is wrong: sleep for a while to
374 * avoid excessive CPU consumption by a bad guest. */
376 DPRINTK("error: unknown block io operation [%d]\n",
378 make_response(blkif, req.id, req.operation,
380 free_req(pending_req);
384 /* Yield point for this unbounded loop. */
391 static void dispatch_rw_block_io(blkif_t *blkif,
392 struct blkif_request *req,
393 pending_req_t *pending_req)
395 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
396 struct phys_req preq;
398 unsigned long buf; unsigned int nsec;
399 } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
401 struct bio *bio = NULL;
405 switch (req->operation) {
412 case BLKIF_OP_WRITE_BARRIER:
413 operation = REQ_FLUSH | REQ_FUA;
416 operation = 0; /* make gcc happy */
420 /* Check that number of segments is sane. */
421 nseg = req->nr_segments;
422 if (unlikely(nseg == 0 && operation != (REQ_FLUSH | REQ_FUA)) ||
423 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
424 DPRINTK("Bad number of segments in request (%d)\n", nseg);
428 preq.dev = req->handle;
429 preq.sector_number = req->sector_number;
432 pending_req->blkif = blkif;
433 pending_req->id = req->id;
434 pending_req->operation = req->operation;
435 pending_req->status = BLKIF_RSP_OKAY;
436 pending_req->nr_pages = nseg;
438 for (i = 0; i < nseg; i++) {
441 seg[i].nsec = req->seg[i].last_sect -
442 req->seg[i].first_sect + 1;
444 if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
445 (req->seg[i].last_sect < req->seg[i].first_sect))
447 preq.nr_sects += seg[i].nsec;
449 flags = GNTMAP_host_map;
450 if (operation != READ)
451 flags |= GNTMAP_readonly;
452 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
453 req->seg[i].gref, blkif->domid);
456 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
459 for (i = 0; i < nseg; i++) {
460 if (unlikely(map[i].status != 0)) {
461 DPRINTK("invalid buffer -- could not remap it\n");
462 map[i].handle = BLKBACK_INVALID_HANDLE;
466 pending_handle(pending_req, i) = map[i].handle;
472 page_to_pfn(blkbk->pending_page(pending_req, i)),
473 FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
474 seg[i].buf = map[i].dev_bus_addr |
475 (req->seg[i].first_sect << 9);
481 if (vbd_translate(&preq, blkif, operation) != 0) {
482 DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
483 operation == READ ? "read" : "write",
485 preq.sector_number + preq.nr_sects, preq.dev);
489 plug_queue(blkif, preq.bdev);
490 atomic_set(&pending_req->pendcnt, 1);
493 for (i = 0; i < nseg; i++) {
494 if (((int)preq.sector_number|(int)seg[i].nsec) &
495 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
496 DPRINTK("Misaligned I/O request from domain %d",
501 while ((bio == NULL) ||
503 blkbk->pending_page(pending_req, i),
505 seg[i].buf & ~PAGE_MASK) == 0)) {
507 atomic_inc(&pending_req->pendcnt);
508 submit_bio(operation, bio);
511 bio = bio_alloc(GFP_KERNEL, nseg-i);
512 if (unlikely(bio == NULL))
515 bio->bi_bdev = preq.bdev;
516 bio->bi_private = pending_req;
517 bio->bi_end_io = end_block_io_op;
518 bio->bi_sector = preq.sector_number;
521 preq.sector_number += seg[i].nsec;
525 BUG_ON(operation != (REQ_FLUSH | REQ_FUA));
526 bio = bio_alloc(GFP_KERNEL, 0);
527 if (unlikely(bio == NULL))
530 bio->bi_bdev = preq.bdev;
531 bio->bi_private = pending_req;
532 bio->bi_end_io = end_block_io_op;
536 submit_bio(operation, bio);
538 if (operation == READ)
539 blkif->st_rd_sect += preq.nr_sects;
540 else if (operation == WRITE || operation == (REQ_FLUSH | REQ_FUA))
541 blkif->st_wr_sect += preq.nr_sects;
546 fast_flush_area(pending_req);
548 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
549 free_req(pending_req);
550 msleep(1); /* back off a bit */
554 __end_block_io_op(pending_req, -EINVAL);
558 msleep(1); /* back off a bit */
564 /******************************************************************
565 * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
569 static void make_response(blkif_t *blkif, u64 id,
570 unsigned short op, int st)
572 struct blkif_response resp;
574 union blkif_back_rings *blk_rings = &blkif->blk_rings;
582 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
583 /* Place on the response ring for the relevant domain. */
584 switch (blkif->blk_protocol) {
585 case BLKIF_PROTOCOL_NATIVE:
586 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
587 &resp, sizeof(resp));
589 case BLKIF_PROTOCOL_X86_32:
590 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
591 &resp, sizeof(resp));
593 case BLKIF_PROTOCOL_X86_64:
594 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
595 &resp, sizeof(resp));
600 blk_rings->common.rsp_prod_pvt++;
601 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
602 if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
604 * Tail check for pending requests. Allows frontend to avoid
605 * notifications if requests are already in flight (lower
606 * overheads and promotes batching).
608 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
610 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
614 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
617 blkif_notify_work(blkif);
619 notify_remote_via_irq(blkif->irq);
622 static int __init blkif_init(void)
627 if (!xen_pv_domain())
630 blkbk = (struct xen_blkbk *)vmalloc(sizeof(struct xen_blkbk));
632 printk(KERN_ALERT "%s: out of memory!\n", __func__);
636 mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
638 blkbk->pending_reqs = kmalloc(sizeof(blkbk->pending_reqs[0]) *
639 blkif_reqs, GFP_KERNEL);
640 blkbk->pending_grant_handles = kmalloc(sizeof(blkbk->pending_grant_handles[0]) *
641 mmap_pages, GFP_KERNEL);
642 blkbk->pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
644 if (!blkbk->pending_reqs || !blkbk->pending_grant_handles || !blkbk->pending_pages) {
649 for (i = 0; i < mmap_pages; i++)
650 blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
652 rc = blkif_interface_init();
656 memset(blkbk->pending_reqs, 0, sizeof(blkbk->pending_reqs));
658 INIT_LIST_HEAD(&blkbk->pending_free);
659 spin_lock_init(&blkbk->pending_free_lock);
660 init_waitqueue_head(&blkbk->pending_free_wq);
662 for (i = 0; i < blkif_reqs; i++)
663 list_add_tail(&blkbk->pending_reqs[i].free_list, &blkbk->pending_free);
665 rc = blkif_xenbus_init();
672 printk(KERN_ERR "%s: out of memory\n", __func__);
674 kfree(blkbk->pending_reqs);
675 kfree(blkbk->pending_grant_handles);
676 free_empty_pages_and_pagevec(blkbk->pending_pages, mmap_pages);
682 module_init(blkif_init);
684 MODULE_LICENSE("Dual BSD/GPL");