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/freezer.h>
41 #include <linux/list.h>
42 #include <linux/module.h>
43 #include <linux/delay.h>
44 #include <xen/balloon.h>
45 #include <xen/evtchn.h>
46 #include <xen/gnttab.h>
47 #include <asm/hypervisor.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.
80 unsigned short nr_pages;
81 unsigned short operation;
82 struct list_head free_list;
85 static pending_req_t *pending_reqs;
86 static struct list_head pending_free;
87 static DEFINE_SPINLOCK(pending_free_lock);
88 static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq);
90 #define BLKBACK_INVALID_HANDLE (~0)
92 static struct page **pending_pages;
93 static grant_handle_t *pending_grant_handles;
95 static inline int vaddr_pagenr(pending_req_t *req, int seg)
97 return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
100 #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
102 static inline unsigned long vaddr(pending_req_t *req, int seg)
104 unsigned long pfn = page_to_pfn(pending_page(req, seg));
105 return (unsigned long)pfn_to_kaddr(pfn);
108 #define pending_handle(_req, _seg) \
109 (pending_grant_handles[vaddr_pagenr(_req, _seg)])
112 static int do_block_io_op(blkif_t *blkif);
113 static void dispatch_rw_block_io(blkif_t *blkif,
114 blkif_request_t *req,
115 pending_req_t *pending_req);
116 static void make_response(blkif_t *blkif, u64 id,
117 unsigned short op, int st);
119 /******************************************************************
122 static pending_req_t* alloc_req(void)
124 pending_req_t *req = NULL;
127 spin_lock_irqsave(&pending_free_lock, flags);
128 if (!list_empty(&pending_free)) {
129 req = list_entry(pending_free.next, pending_req_t, free_list);
130 list_del(&req->free_list);
132 spin_unlock_irqrestore(&pending_free_lock, flags);
136 static void free_req(pending_req_t *req)
141 spin_lock_irqsave(&pending_free_lock, flags);
142 was_empty = list_empty(&pending_free);
143 list_add(&req->free_list, &pending_free);
144 spin_unlock_irqrestore(&pending_free_lock, flags);
146 wake_up(&pending_free_wq);
149 static void unplug_queue(blkif_t *blkif)
151 if (blkif->plug == NULL)
153 kobject_put(&blkif->plug->kobj);
157 static void plug_queue(blkif_t *blkif, struct block_device *bdev)
159 struct request_queue *q = bdev_get_queue(bdev);
161 if (q == blkif->plug)
164 WARN_ON(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags));
165 kobject_get(&q->kobj);
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 blkback_pagemap_clear(pending_page(req, i));
181 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
182 GNTMAP_host_map, handle);
183 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
187 ret = HYPERVISOR_grant_table_op(
188 GNTTABOP_unmap_grant_ref, unmap, invcount);
192 /******************************************************************
193 * SCHEDULER FUNCTIONS
196 static void print_stats(blkif_t *blkif)
198 printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d | br %4d"
199 " | fl %4d | ds %4d | pk %4d\n",
200 current->comm, blkif->st_oo_req,
201 blkif->st_rd_req, blkif->st_wr_req,
202 blkif->st_br_req, blkif->st_fl_req,
203 blkif->st_ds_req, blkif->st_pk_req);
204 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
205 blkif->st_rd_req = 0;
206 blkif->st_wr_req = 0;
207 blkif->st_oo_req = 0;
208 blkif->st_br_req = 0;
209 blkif->st_fl_req = 0;
210 blkif->st_ds_req = 0;
211 blkif->st_pk_req = 0;
214 int blkif_schedule(void *arg)
216 blkif_t *blkif = arg;
217 struct vbd *vbd = &blkif->vbd;
222 printk(KERN_DEBUG "%s: started\n", current->comm);
224 while (!kthread_should_stop()) {
227 if (unlikely(vbd->size != vbd_size(vbd)))
230 wait_event_interruptible(
232 blkif->waiting_reqs || kthread_should_stop());
233 wait_event_interruptible(
235 !list_empty(&pending_free) || kthread_should_stop());
237 blkif->waiting_reqs = 0;
238 smp_mb(); /* clear flag *before* checking for work */
240 if (do_block_io_op(blkif))
241 blkif->waiting_reqs = 1;
244 if (log_stats && time_after(jiffies, blkif->st_print))
251 printk(KERN_DEBUG "%s: exiting\n", current->comm);
253 blkif->xenblkd = NULL;
259 static void do_discard(blkif_t *blkif, struct blkif_request_discard *req)
261 int err = -EOPNOTSUPP;
262 int status = BLKIF_RSP_OKAY;
263 struct block_device *bdev = blkif->vbd.bdev;
265 if (blkif->blk_backend_type == BLKIF_BACKEND_PHY ||
266 blkif->blk_backend_type == BLKIF_BACKEND_FILE) {
267 unsigned long secure = (blkif->vbd.discard_secure &&
268 (req->flag & BLKIF_DISCARD_SECURE)) ?
269 BLKDEV_DISCARD_SECURE : 0;
271 err = blkdev_issue_discard(bdev, req->sector_number,
272 req->nr_sectors, GFP_KERNEL,
276 if (err == -EOPNOTSUPP) {
277 DPRINTK("discard op failed, not supported\n");
278 status = BLKIF_RSP_EOPNOTSUPP;
280 status = BLKIF_RSP_ERROR;
282 make_response(blkif, req->id, req->operation, status);
285 static void drain_io(blkif_t *blkif)
287 atomic_set(&blkif->drain, 1);
289 /* The initial value is one, and one refcnt taken at the
290 * start of the blkif_schedule thread. */
291 if (atomic_read(&blkif->refcnt) <= 2)
294 wait_for_completion_interruptible_timeout(
295 &blkif->drain_complete, HZ);
297 if (!atomic_read(&blkif->drain))
299 } while (!kthread_should_stop());
300 atomic_set(&blkif->drain, 0);
303 /******************************************************************
304 * COMPLETION CALLBACK -- Called as bh->b_end_io()
307 static void __end_block_io_op(pending_req_t *pending_req, int error)
309 blkif_t *blkif = pending_req->blkif;
310 int status = BLKIF_RSP_OKAY;
312 /* An error fails the entire request. */
313 if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
314 (error == -EOPNOTSUPP)) {
315 DPRINTK("blkback: write barrier op failed, not supported\n");
316 blkback_barrier(XBT_NIL, blkif->be, 0);
317 status = BLKIF_RSP_EOPNOTSUPP;
318 } else if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
319 (error == -EOPNOTSUPP)) {
320 DPRINTK("blkback: flush diskcache op failed, not supported\n");
321 blkback_flush_diskcache(XBT_NIL, blkif->be, 0);
322 status = BLKIF_RSP_EOPNOTSUPP;
324 DPRINTK("Buffer not up-to-date at end of operation, "
325 "error=%d\n", error);
326 status = BLKIF_RSP_ERROR;
329 if (atomic_dec_and_test(&pending_req->pendcnt)) {
330 fast_flush_area(pending_req);
331 make_response(blkif, pending_req->id,
332 pending_req->operation, status);
334 free_req(pending_req);
335 if (atomic_read(&blkif->drain)
336 && atomic_read(&blkif->refcnt) <= 2)
337 complete(&blkif->drain_complete);
341 static void end_block_io_op(struct bio *bio, int error)
343 __end_block_io_op(bio->bi_private, error);
348 /******************************************************************************
349 * NOTIFICATION FROM GUEST OS.
352 static void blkif_notify_work(blkif_t *blkif)
354 blkif->waiting_reqs = 1;
358 irqreturn_t blkif_be_int(int irq, void *dev_id)
360 blkif_notify_work(dev_id);
366 /******************************************************************
367 * DOWNWARD CALLS -- These interface with the block-device layer proper.
370 static int _do_block_io_op(blkif_t *blkif)
372 blkif_back_rings_t *blk_rings = &blkif->blk_rings;
374 pending_req_t *pending_req;
378 rc = blk_rings->common.req_cons;
379 rp = blk_rings->common.sring->req_prod;
380 rmb(); /* Ensure we see queued requests up to 'rp'. */
384 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
387 if (kthread_should_stop()) {
392 pending_req = alloc_req();
393 if (NULL == pending_req) {
399 switch (blkif->blk_protocol) {
400 case BLKIF_PROTOCOL_NATIVE:
401 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
403 case BLKIF_PROTOCOL_X86_32:
404 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
406 case BLKIF_PROTOCOL_X86_64:
407 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
412 blk_rings->common.req_cons = ++rc; /* before make_response() */
414 /* Apply all sanity checks to /private copy/ of request. */
417 switch (req.operation) {
420 case BLKIF_OP_WRITE_BARRIER:
421 case BLKIF_OP_FLUSH_DISKCACHE:
422 case BLKIF_OP_DISCARD:
423 dispatch_rw_block_io(blkif, &req, pending_req);
425 case BLKIF_OP_PACKET:
426 DPRINTK("error: block operation BLKIF_OP_PACKET not implemented\n");
428 make_response(blkif, req.id, req.operation,
430 free_req(pending_req);
433 /* A good sign something is wrong: sleep for a while to
434 * avoid excessive CPU consumption by a bad guest. */
436 DPRINTK("error: unknown block io operation [%d]\n",
438 make_response(blkif, req.id, req.operation,
440 free_req(pending_req);
444 /* Yield point for this unbounded loop. */
452 do_block_io_op(blkif_t *blkif)
454 blkif_back_rings_t *blk_rings = &blkif->blk_rings;
458 more_to_do = _do_block_io_op(blkif);
462 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
463 } while (more_to_do);
468 static void dispatch_rw_block_io(blkif_t *blkif,
469 blkif_request_t *req,
470 pending_req_t *pending_req)
472 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
473 struct phys_req preq;
475 unsigned long buf; unsigned int nsec;
476 } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
477 unsigned int nseg = req->nr_segments;
478 struct bio *bio = NULL;
483 switch (req->operation) {
492 case BLKIF_OP_WRITE_BARRIER:
494 operation = WRITE_FLUSH_FUA;
496 case BLKIF_OP_FLUSH_DISKCACHE:
498 operation = WRITE_FLUSH;
500 case BLKIF_OP_DISCARD:
502 operation = REQ_DISCARD;
506 operation = 0; /* make gcc happy */
510 /* Check that number of segments is sane. */
511 if (unlikely(nseg == 0 && !(operation & (REQ_FLUSH|REQ_DISCARD))) ||
512 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
513 DPRINTK("Bad number of segments in request (%d)\n", nseg);
517 preq.dev = req->handle;
518 preq.sector_number = req->sector_number;
521 pending_req->blkif = blkif;
522 pending_req->id = req->id;
523 pending_req->operation = req->operation;
524 pending_req->nr_pages = nseg;
526 flags = GNTMAP_host_map;
527 if (operation != READ)
528 flags |= GNTMAP_readonly;
530 for (i = 0; i < nseg; i++) {
531 seg[i].nsec = req->seg[i].last_sect -
532 req->seg[i].first_sect + 1;
534 if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
535 (req->seg[i].last_sect < req->seg[i].first_sect))
537 preq.nr_sects += seg[i].nsec;
539 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
540 req->seg[i].gref, blkif->domid);
543 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
546 for (i = 0; i < nseg; i++) {
547 if (unlikely(map[i].status == GNTST_eagain))
548 gnttab_check_GNTST_eagain_do_while(GNTTABOP_map_grant_ref, &map[i])
549 if (unlikely(map[i].status != GNTST_okay)) {
550 DPRINTK("invalid buffer -- could not remap it\n");
551 map[i].handle = BLKBACK_INVALID_HANDLE;
554 blkback_pagemap_set(vaddr_pagenr(pending_req, i),
555 pending_page(pending_req, i),
556 blkif->domid, req->handle,
560 pending_handle(pending_req, i) = map[i].handle;
566 page_to_pfn(pending_page(pending_req, i)),
567 FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
568 seg[i].buf = map[i].dev_bus_addr |
569 (req->seg[i].first_sect << 9);
575 if (vbd_translate(&preq, blkif, operation) != 0) {
576 DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
577 operation == READ ? "read" : "write",
579 preq.sector_number + preq.nr_sects, preq.dev);
583 /* Wait on all outstanding I/O's and once that has been completed
584 * issue the WRITE_FLUSH.
586 if (req->operation == BLKIF_OP_WRITE_BARRIER)
589 plug_queue(blkif, preq.bdev);
590 atomic_set(&pending_req->pendcnt, 1);
593 for (i = 0; i < nseg; i++) {
594 if (((int)preq.sector_number|(int)seg[i].nsec) &
595 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
596 DPRINTK("Misaligned I/O request from domain %d",
601 while ((bio == NULL) ||
603 pending_page(pending_req, i),
605 seg[i].buf & ~PAGE_MASK) == 0)) {
607 atomic_inc(&pending_req->pendcnt);
608 submit_bio(operation, bio);
611 bio = bio_alloc(GFP_KERNEL, nseg-i);
612 if (unlikely(bio == NULL))
615 bio->bi_bdev = preq.bdev;
616 bio->bi_private = pending_req;
617 bio->bi_end_io = end_block_io_op;
618 bio->bi_sector = preq.sector_number;
621 preq.sector_number += seg[i].nsec;
625 if (operation == REQ_DISCARD) {
626 do_discard(blkif, (void *)req);
628 free_req(pending_req);
632 BUG_ON(!(operation & (REQ_FLUSH|REQ_FUA)));
633 bio = bio_alloc(GFP_KERNEL, 0);
634 if (unlikely(bio == NULL))
637 bio->bi_bdev = preq.bdev;
638 bio->bi_private = pending_req;
639 bio->bi_end_io = end_block_io_op;
643 submit_bio(operation, bio);
645 if (operation == READ)
646 blkif->st_rd_sect += preq.nr_sects;
648 blkif->st_wr_sect += preq.nr_sects;
653 fast_flush_area(pending_req);
655 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
656 free_req(pending_req);
657 msleep(1); /* back off a bit */
661 __end_block_io_op(pending_req, -EINVAL);
665 msleep(1); /* back off a bit */
671 /******************************************************************
672 * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
676 static void make_response(blkif_t *blkif, u64 id,
677 unsigned short op, int st)
679 blkif_response_t resp;
681 blkif_back_rings_t *blk_rings = &blkif->blk_rings;
688 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
689 /* Place on the response ring for the relevant domain. */
690 switch (blkif->blk_protocol) {
691 case BLKIF_PROTOCOL_NATIVE:
692 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
693 &resp, sizeof(resp));
695 case BLKIF_PROTOCOL_X86_32:
696 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
697 &resp, sizeof(resp));
699 case BLKIF_PROTOCOL_X86_64:
700 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
701 &resp, sizeof(resp));
706 blk_rings->common.rsp_prod_pvt++;
707 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
708 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
711 notify_remote_via_irq(blkif->irq);
714 static int __init blkif_init(void)
718 if (!is_running_on_xen())
721 mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
723 pending_reqs = kzalloc(sizeof(pending_reqs[0]) *
724 blkif_reqs, GFP_KERNEL);
725 pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
726 mmap_pages, GFP_KERNEL);
727 pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
729 if (blkback_pagemap_init(mmap_pages))
732 if (!pending_reqs || !pending_grant_handles || !pending_pages)
735 for (i = 0; i < mmap_pages; i++)
736 pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
738 blkif_interface_init();
740 INIT_LIST_HEAD(&pending_free);
742 for (i = 0; i < blkif_reqs; i++)
743 list_add_tail(&pending_reqs[i].free_list, &pending_free);
751 kfree(pending_grant_handles);
752 free_empty_pages_and_pagevec(pending_pages, mmap_pages);
753 pr_warning("%s: out of memory\n", __FUNCTION__);
757 module_init(blkif_init);
759 MODULE_LICENSE("Dual BSD/GPL");
760 MODULE_ALIAS("xen-backend:vbd");