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>
43 #include <xen/balloon.h>
44 #include <xen/events.h>
46 #include <asm/xen/hypervisor.h>
47 #include <asm/xen/hypercall.h>
51 * These are rather arbitrary. They are fairly large because adjacent requests
52 * pulled from a communication ring are quite likely to end up being part of
53 * the same scatter/gather request at the disc.
55 * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
57 * This will increase the chances of being able to write whole tracks.
58 * 64 should be enough to keep us competitive with Linux.
60 static int blkif_reqs = 64;
61 module_param_named(reqs, blkif_reqs, int, 0);
62 MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
64 /* Run-time switchable: /sys/module/blkback/parameters/ */
65 static unsigned int log_stats = 0;
66 static unsigned int debug_lvl = 0;
67 module_param(log_stats, int, 0644);
68 module_param(debug_lvl, int, 0644);
71 * Each outstanding request that we've passed to the lower device layers has a
72 * 'pending_req' allocated to it. Each buffer_head that completes decrements
73 * the pendcnt towards zero. When it hits zero, the specified domain has a
74 * response queued for it, with the saved 'id' passed back.
81 unsigned short operation;
83 struct list_head free_list;
86 static pending_req_t *pending_reqs;
87 static struct list_head pending_free;
88 static DEFINE_SPINLOCK(pending_free_lock);
89 static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq);
91 #define BLKBACK_INVALID_HANDLE (~0)
93 static struct page **pending_pages;
94 static grant_handle_t *pending_grant_handles;
96 static inline int vaddr_pagenr(pending_req_t *req, int seg)
98 return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
101 static inline unsigned long vaddr(pending_req_t *req, int seg)
103 unsigned long pfn = page_to_pfn(pending_pages[vaddr_pagenr(req, seg)]);
104 return (unsigned long)pfn_to_kaddr(pfn);
107 #define pending_handle(_req, _seg) \
108 (pending_grant_handles[vaddr_pagenr(_req, _seg)])
111 static int do_block_io_op(blkif_t *blkif);
112 static void dispatch_rw_block_io(blkif_t *blkif,
113 struct blkif_request *req,
114 pending_req_t *pending_req);
115 static void make_response(blkif_t *blkif, u64 id,
116 unsigned short op, int st);
118 /******************************************************************
121 static pending_req_t* alloc_req(void)
123 pending_req_t *req = NULL;
126 spin_lock_irqsave(&pending_free_lock, flags);
127 if (!list_empty(&pending_free)) {
128 req = list_entry(pending_free.next, pending_req_t, free_list);
129 list_del(&req->free_list);
131 spin_unlock_irqrestore(&pending_free_lock, flags);
135 static void free_req(pending_req_t *req)
140 spin_lock_irqsave(&pending_free_lock, flags);
141 was_empty = list_empty(&pending_free);
142 list_add(&req->free_list, &pending_free);
143 spin_unlock_irqrestore(&pending_free_lock, flags);
145 wake_up(&pending_free_wq);
148 static void unplug_queue(blkif_t *blkif)
150 if (blkif->plug == NULL)
152 if (blkif->plug->unplug_fn)
153 blkif->plug->unplug_fn(blkif->plug);
154 blk_put_queue(blkif->plug);
158 static void plug_queue(blkif_t *blkif, struct block_device *bdev)
160 struct request_queue *q = bdev_get_queue(bdev);
162 if (q == blkif->plug)
169 static void fast_flush_area(pending_req_t *req)
171 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
172 unsigned int i, invcount = 0;
173 grant_handle_t handle;
176 for (i = 0; i < req->nr_pages; i++) {
177 handle = pending_handle(req, i);
178 if (handle == BLKBACK_INVALID_HANDLE)
180 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
181 GNTMAP_host_map, handle);
182 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
186 ret = HYPERVISOR_grant_table_op(
187 GNTTABOP_unmap_grant_ref, unmap, invcount);
191 /******************************************************************
192 * SCHEDULER FUNCTIONS
195 static void print_stats(blkif_t *blkif)
197 printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d | br %4d\n",
198 current->comm, blkif->st_oo_req,
199 blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req);
200 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
201 blkif->st_rd_req = 0;
202 blkif->st_wr_req = 0;
203 blkif->st_oo_req = 0;
206 int blkif_schedule(void *arg)
208 blkif_t *blkif = arg;
213 printk(KERN_DEBUG "%s: started\n", current->comm);
215 while (!kthread_should_stop()) {
219 wait_event_interruptible(
221 blkif->waiting_reqs || kthread_should_stop());
222 wait_event_interruptible(
224 !list_empty(&pending_free) || kthread_should_stop());
226 blkif->waiting_reqs = 0;
227 smp_mb(); /* clear flag *before* checking for work */
229 if (do_block_io_op(blkif))
230 blkif->waiting_reqs = 1;
233 if (log_stats && time_after(jiffies, blkif->st_print))
240 printk(KERN_DEBUG "%s: exiting\n", current->comm);
242 blkif->xenblkd = NULL;
248 /******************************************************************
249 * COMPLETION CALLBACK -- Called as bh->b_end_io()
252 static void __end_block_io_op(pending_req_t *pending_req, int error)
254 /* An error fails the entire request. */
255 if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
256 (error == -EOPNOTSUPP)) {
257 DPRINTK("blkback: write barrier op failed, not supported\n");
258 blkback_barrier(XBT_NIL, pending_req->blkif->be, 0);
259 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
261 DPRINTK("Buffer not up-to-date at end of operation, "
262 "error=%d\n", error);
263 pending_req->status = BLKIF_RSP_ERROR;
266 if (atomic_dec_and_test(&pending_req->pendcnt)) {
267 fast_flush_area(pending_req);
268 make_response(pending_req->blkif, pending_req->id,
269 pending_req->operation, pending_req->status);
270 blkif_put(pending_req->blkif);
271 free_req(pending_req);
275 static void end_block_io_op(struct bio *bio, int error)
277 __end_block_io_op(bio->bi_private, error);
282 /******************************************************************************
283 * NOTIFICATION FROM GUEST OS.
286 static void blkif_notify_work(blkif_t *blkif)
288 blkif->waiting_reqs = 1;
292 irqreturn_t blkif_be_int(int irq, void *dev_id)
294 blkif_notify_work(dev_id);
300 /******************************************************************
301 * DOWNWARD CALLS -- These interface with the block-device layer proper.
304 static int do_block_io_op(blkif_t *blkif)
306 union blkif_back_rings *blk_rings = &blkif->blk_rings;
307 struct blkif_request req;
308 pending_req_t *pending_req;
312 rc = blk_rings->common.req_cons;
313 rp = blk_rings->common.sring->req_prod;
314 rmb(); /* Ensure we see queued requests up to 'rp'. */
318 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
321 if (kthread_should_stop()) {
326 pending_req = alloc_req();
327 if (NULL == pending_req) {
333 switch (blkif->blk_protocol) {
334 case BLKIF_PROTOCOL_NATIVE:
335 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
337 case BLKIF_PROTOCOL_X86_32:
338 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
340 case BLKIF_PROTOCOL_X86_64:
341 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
346 blk_rings->common.req_cons = ++rc; /* before make_response() */
348 /* Apply all sanity checks to /private copy/ of request. */
351 switch (req.operation) {
354 dispatch_rw_block_io(blkif, &req, pending_req);
356 case BLKIF_OP_WRITE_BARRIER:
361 dispatch_rw_block_io(blkif, &req, pending_req);
364 /* A good sign something is wrong: sleep for a while to
365 * avoid excessive CPU consumption by a bad guest. */
367 DPRINTK("error: unknown block io operation [%d]\n",
369 make_response(blkif, req.id, req.operation,
371 free_req(pending_req);
375 /* Yield point for this unbounded loop. */
382 static void dispatch_rw_block_io(blkif_t *blkif,
383 struct blkif_request *req,
384 pending_req_t *pending_req)
386 extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]);
387 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
388 struct phys_req preq;
390 unsigned long buf; unsigned int nsec;
391 } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
393 struct bio *bio = NULL;
397 switch (req->operation) {
404 case BLKIF_OP_WRITE_BARRIER:
405 operation = WRITE_BARRIER;
408 operation = 0; /* make gcc happy */
412 /* Check that number of segments is sane. */
413 nseg = req->nr_segments;
414 if (unlikely(nseg == 0 && operation != WRITE_BARRIER) ||
415 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
416 DPRINTK("Bad number of segments in request (%d)\n", nseg);
420 preq.dev = req->handle;
421 preq.sector_number = req->sector_number;
424 pending_req->blkif = blkif;
425 pending_req->id = req->id;
426 pending_req->operation = req->operation;
427 pending_req->status = BLKIF_RSP_OKAY;
428 pending_req->nr_pages = nseg;
430 for (i = 0; i < nseg; i++) {
433 seg[i].nsec = req->seg[i].last_sect -
434 req->seg[i].first_sect + 1;
436 if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
437 (req->seg[i].last_sect < req->seg[i].first_sect))
439 preq.nr_sects += seg[i].nsec;
441 flags = GNTMAP_host_map;
442 if (operation != READ)
443 flags |= GNTMAP_readonly;
444 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
445 req->seg[i].gref, blkif->domid);
448 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
451 for (i = 0; i < nseg; i++) {
452 if (unlikely(map[i].status != 0)) {
453 DPRINTK("invalid buffer -- could not remap it\n");
454 map[i].handle = BLKBACK_INVALID_HANDLE;
458 pending_handle(pending_req, i) = map[i].handle;
463 set_phys_to_machine(__pa(vaddr(
464 pending_req, i)) >> PAGE_SHIFT,
465 FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
466 seg[i].buf = map[i].dev_bus_addr |
467 (req->seg[i].first_sect << 9);
473 if (vbd_translate(&preq, blkif, operation) != 0) {
474 DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
475 operation == READ ? "read" : "write",
477 preq.sector_number + preq.nr_sects, preq.dev);
481 plug_queue(blkif, preq.bdev);
482 atomic_set(&pending_req->pendcnt, 1);
485 for (i = 0; i < nseg; i++) {
486 if (((int)preq.sector_number|(int)seg[i].nsec) &
487 ((bdev_hardsect_size(preq.bdev) >> 9) - 1)) {
488 DPRINTK("Misaligned I/O request from domain %d",
493 while ((bio == NULL) ||
495 virt_to_page(vaddr(pending_req, i)),
497 seg[i].buf & ~PAGE_MASK) == 0)) {
499 atomic_inc(&pending_req->pendcnt);
500 submit_bio(operation, bio);
503 bio = bio_alloc(GFP_KERNEL, nseg-i);
504 if (unlikely(bio == NULL))
507 bio->bi_bdev = preq.bdev;
508 bio->bi_private = pending_req;
509 bio->bi_end_io = end_block_io_op;
510 bio->bi_sector = preq.sector_number;
513 preq.sector_number += seg[i].nsec;
517 BUG_ON(operation != WRITE_BARRIER);
518 bio = bio_alloc(GFP_KERNEL, 0);
519 if (unlikely(bio == NULL))
522 bio->bi_bdev = preq.bdev;
523 bio->bi_private = pending_req;
524 bio->bi_end_io = end_block_io_op;
528 submit_bio(operation, bio);
530 if (operation == READ)
531 blkif->st_rd_sect += preq.nr_sects;
532 else if (operation == WRITE || operation == WRITE_BARRIER)
533 blkif->st_wr_sect += preq.nr_sects;
538 fast_flush_area(pending_req);
540 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
541 free_req(pending_req);
542 msleep(1); /* back off a bit */
546 __end_block_io_op(pending_req, -EINVAL);
550 msleep(1); /* back off a bit */
556 /******************************************************************
557 * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
561 static void make_response(blkif_t *blkif, u64 id,
562 unsigned short op, int st)
564 struct blkif_response resp;
566 union blkif_back_rings *blk_rings = &blkif->blk_rings;
574 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
575 /* Place on the response ring for the relevant domain. */
576 switch (blkif->blk_protocol) {
577 case BLKIF_PROTOCOL_NATIVE:
578 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
579 &resp, sizeof(resp));
581 case BLKIF_PROTOCOL_X86_32:
582 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
583 &resp, sizeof(resp));
585 case BLKIF_PROTOCOL_X86_64:
586 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
587 &resp, sizeof(resp));
592 blk_rings->common.rsp_prod_pvt++;
593 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
594 if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
596 * Tail check for pending requests. Allows frontend to avoid
597 * notifications if requests are already in flight (lower
598 * overheads and promotes batching).
600 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
602 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
606 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
609 blkif_notify_work(blkif);
611 notify_remote_via_irq(blkif->irq);
614 static int __init blkif_init(void)
618 printk(KERN_CRIT "***blkif_init\n");
619 if (!xen_pv_domain())
622 mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
624 pending_reqs = kmalloc(sizeof(pending_reqs[0]) *
625 blkif_reqs, GFP_KERNEL);
626 pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
627 mmap_pages, GFP_KERNEL);
628 pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
630 if (!pending_reqs || !pending_grant_handles || !pending_pages)
633 for (i = 0; i < mmap_pages; i++)
634 pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
636 blkif_interface_init();
638 memset(pending_reqs, 0, sizeof(pending_reqs));
639 INIT_LIST_HEAD(&pending_free);
641 for (i = 0; i < blkif_reqs; i++)
642 list_add_tail(&pending_reqs[i].free_list, &pending_free);
650 kfree(pending_grant_handles);
651 free_empty_pages_and_pagevec(pending_pages, mmap_pages);
652 printk("%s: out of memory\n", __FUNCTION__);
656 module_init(blkif_init);
658 MODULE_LICENSE("Dual BSD/GPL");