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/delay.h>
43 #include <xen/balloon.h>
44 #include <asm/hypervisor.h>
48 * These are rather arbitrary. They are fairly large because adjacent requests
49 * pulled from a communication ring are quite likely to end up being part of
50 * the same scatter/gather request at the disc.
52 * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
54 * This will increase the chances of being able to write whole tracks.
55 * 64 should be enough to keep us competitive with Linux.
57 static int blkif_reqs = 64;
58 module_param_named(reqs, blkif_reqs, int, 0);
59 MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
61 /* Run-time switchable: /sys/module/blkback/parameters/ */
62 static unsigned int log_stats = 0;
63 static unsigned int debug_lvl = 0;
64 module_param(log_stats, int, 0644);
65 module_param(debug_lvl, int, 0644);
68 * Each outstanding request that we've passed to the lower device layers has a
69 * 'pending_req' allocated to it. Each buffer_head that completes decrements
70 * the pendcnt towards zero. When it hits zero, the specified domain has a
71 * response queued for it, with the saved 'id' passed back.
78 unsigned short operation;
80 struct list_head free_list;
83 static pending_req_t *pending_reqs;
84 static struct list_head pending_free;
85 static DEFINE_SPINLOCK(pending_free_lock);
86 static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq);
88 #define BLKBACK_INVALID_HANDLE (~0)
90 static struct page **pending_pages;
91 static grant_handle_t *pending_grant_handles;
93 static inline int vaddr_pagenr(pending_req_t *req, int seg)
95 return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
98 #define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
100 static inline unsigned long vaddr(pending_req_t *req, int seg)
102 unsigned long pfn = page_to_pfn(pending_page(req, seg));
103 return (unsigned long)pfn_to_kaddr(pfn);
106 #define pending_handle(_req, _seg) \
107 (pending_grant_handles[vaddr_pagenr(_req, _seg)])
110 static int do_block_io_op(blkif_t *blkif);
111 static int dispatch_rw_block_io(blkif_t *blkif,
112 blkif_request_t *req,
113 pending_req_t *pending_req);
114 static void make_response(blkif_t *blkif, u64 id,
115 unsigned short op, int st);
117 /******************************************************************
120 static pending_req_t* alloc_req(void)
122 pending_req_t *req = NULL;
125 spin_lock_irqsave(&pending_free_lock, flags);
126 if (!list_empty(&pending_free)) {
127 req = list_entry(pending_free.next, pending_req_t, free_list);
128 list_del(&req->free_list);
130 spin_unlock_irqrestore(&pending_free_lock, flags);
134 static void free_req(pending_req_t *req)
139 spin_lock_irqsave(&pending_free_lock, flags);
140 was_empty = list_empty(&pending_free);
141 list_add(&req->free_list, &pending_free);
142 spin_unlock_irqrestore(&pending_free_lock, flags);
144 wake_up(&pending_free_wq);
147 static void unplug_queue(blkif_t *blkif)
149 if (blkif->plug == NULL)
151 if (blkif->plug->unplug_fn)
152 blkif->plug->unplug_fn(blkif->plug);
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 | pk %4d\n",
199 current->comm, blkif->st_oo_req,
200 blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req,
202 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
203 blkif->st_rd_req = 0;
204 blkif->st_wr_req = 0;
205 blkif->st_oo_req = 0;
206 blkif->st_pk_req = 0;
209 int blkif_schedule(void *arg)
211 blkif_t *blkif = arg;
212 struct vbd *vbd = &blkif->vbd;
217 printk(KERN_DEBUG "%s: started\n", current->comm);
219 while (!kthread_should_stop()) {
222 if (unlikely(vbd->size != vbd_size(vbd)))
225 wait_event_interruptible(
227 blkif->waiting_reqs || kthread_should_stop());
228 wait_event_interruptible(
230 !list_empty(&pending_free) || kthread_should_stop());
232 blkif->waiting_reqs = 0;
233 smp_mb(); /* clear flag *before* checking for work */
235 if (do_block_io_op(blkif))
236 blkif->waiting_reqs = 1;
239 if (log_stats && time_after(jiffies, blkif->st_print))
246 printk(KERN_DEBUG "%s: exiting\n", current->comm);
248 blkif->xenblkd = NULL;
254 /******************************************************************
255 * COMPLETION CALLBACK -- Called as bh->b_end_io()
258 static void __end_block_io_op(pending_req_t *pending_req, int error)
260 /* An error fails the entire request. */
261 if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
262 (error == -EOPNOTSUPP)) {
263 DPRINTK("blkback: write barrier op failed, not supported\n");
264 blkback_barrier(XBT_NIL, pending_req->blkif->be, 0);
265 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
267 DPRINTK("Buffer not up-to-date at end of operation, "
268 "error=%d\n", error);
269 pending_req->status = BLKIF_RSP_ERROR;
272 if (atomic_dec_and_test(&pending_req->pendcnt)) {
273 fast_flush_area(pending_req);
274 make_response(pending_req->blkif, pending_req->id,
275 pending_req->operation, pending_req->status);
276 blkif_put(pending_req->blkif);
277 free_req(pending_req);
281 static void end_block_io_op(struct bio *bio, int error)
283 __end_block_io_op(bio->bi_private, error);
288 /******************************************************************************
289 * NOTIFICATION FROM GUEST OS.
292 static void blkif_notify_work(blkif_t *blkif)
294 blkif->waiting_reqs = 1;
298 irqreturn_t blkif_be_int(int irq, void *dev_id)
300 blkif_notify_work(dev_id);
306 /******************************************************************
307 * DOWNWARD CALLS -- These interface with the block-device layer proper.
310 static int do_block_io_op(blkif_t *blkif)
312 blkif_back_rings_t *blk_rings = &blkif->blk_rings;
314 pending_req_t *pending_req;
316 int more_to_do = 0, ret;
318 rc = blk_rings->common.req_cons;
319 rp = blk_rings->common.sring->req_prod;
320 rmb(); /* Ensure we see queued requests up to 'rp'. */
322 while ((rc != rp) || (blkif->is_suspended_req)) {
324 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
327 if (kthread_should_stop()) {
332 pending_req = alloc_req();
333 if (NULL == pending_req) {
339 /* Handle the suspended request first, if one exists */
340 if(blkif->is_suspended_req)
342 memcpy(&req, &blkif->suspended_req, sizeof(req));
343 blkif->is_suspended_req = 0;
347 switch (blkif->blk_protocol) {
348 case BLKIF_PROTOCOL_NATIVE:
349 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
351 case BLKIF_PROTOCOL_X86_32:
352 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
354 case BLKIF_PROTOCOL_X86_64:
355 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
360 blk_rings->common.req_cons = ++rc; /* before make_response() */
362 /* Apply all sanity checks to /private copy/ of request. */
367 switch (req.operation) {
370 ret = dispatch_rw_block_io(blkif, &req, pending_req);
372 case BLKIF_OP_WRITE_BARRIER:
377 ret = dispatch_rw_block_io(blkif, &req, pending_req);
379 case BLKIF_OP_PACKET:
380 DPRINTK("error: block operation BLKIF_OP_PACKET not implemented\n");
382 make_response(blkif, req.id, req.operation,
384 free_req(pending_req);
387 /* A good sign something is wrong: sleep for a while to
388 * avoid excessive CPU consumption by a bad guest. */
390 DPRINTK("error: unknown block io operation [%d]\n",
392 make_response(blkif, req.id, req.operation,
394 free_req(pending_req);
397 BUG_ON(ret != 0 && ret != -EAGAIN);
398 /* If we can't handle the request at the moment, save it, and break the
402 memcpy(&blkif->suspended_req, &req, sizeof(req));
403 blkif->is_suspended_req = 1;
404 /* Return "no more work pending", restart will be handled 'out of
409 /* Yield point for this unbounded loop. */
416 static int dispatch_rw_block_io(blkif_t *blkif,
417 blkif_request_t *req,
418 pending_req_t *pending_req)
420 extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]);
421 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
422 struct phys_req preq;
424 unsigned long buf; unsigned int nsec;
425 } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
427 struct bio *bio = NULL;
431 switch (req->operation) {
438 case BLKIF_OP_WRITE_BARRIER:
439 operation = WRITE_BARRIER;
442 operation = 0; /* make gcc happy */
446 /* Check that number of segments is sane. */
447 nseg = req->nr_segments;
448 if (unlikely(nseg == 0 && operation != WRITE_BARRIER) ||
449 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
450 DPRINTK("Bad number of segments in request (%d)\n", nseg);
454 preq.dev = req->handle;
455 preq.sector_number = req->sector_number;
458 pending_req->blkif = blkif;
459 pending_req->id = req->id;
460 pending_req->operation = req->operation;
461 pending_req->status = BLKIF_RSP_OKAY;
462 pending_req->nr_pages = nseg;
464 for (i = 0; i < nseg; i++) {
467 seg[i].nsec = req->seg[i].last_sect -
468 req->seg[i].first_sect + 1;
470 if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
471 (req->seg[i].last_sect < req->seg[i].first_sect))
473 preq.nr_sects += seg[i].nsec;
475 flags = GNTMAP_host_map;
476 if (operation != READ)
477 flags |= GNTMAP_readonly;
478 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
479 req->seg[i].gref, blkif->domid);
482 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
485 #define GENERAL_ERR (1<<0)
486 #define EAGAIN_ERR (1<<1)
487 for (i = 0; i < nseg; i++) {
488 if (unlikely(map[i].status != 0)) {
489 DPRINTK("invalid buffer -- could not remap it\n");
490 map[i].handle = BLKBACK_INVALID_HANDLE;
492 if(map[i].status == GNTST_eagain)
495 blkback_pagemap_set(vaddr_pagenr(pending_req, i),
496 pending_page(pending_req, i),
497 blkif->domid, req->handle,
501 pending_handle(pending_req, i) = map[i].handle;
507 page_to_pfn(pending_page(pending_req, i)),
508 FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
509 seg[i].buf = map[i].dev_bus_addr |
510 (req->seg[i].first_sect << 9);
513 /* If any of grant maps failed with GNTST_eagain, suspend and retry later */
516 fast_flush_area(pending_req);
517 free_req(pending_req);
524 if (vbd_translate(&preq, blkif, operation) != 0) {
525 DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
526 operation == READ ? "read" : "write",
528 preq.sector_number + preq.nr_sects, preq.dev);
532 plug_queue(blkif, preq.bdev);
533 atomic_set(&pending_req->pendcnt, 1);
536 for (i = 0; i < nseg; i++) {
537 if (((int)preq.sector_number|(int)seg[i].nsec) &
538 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
539 DPRINTK("Misaligned I/O request from domain %d",
544 while ((bio == NULL) ||
546 pending_page(pending_req, i),
548 seg[i].buf & ~PAGE_MASK) == 0)) {
550 atomic_inc(&pending_req->pendcnt);
551 submit_bio(operation, bio);
554 bio = bio_alloc(GFP_KERNEL, nseg-i);
555 if (unlikely(bio == NULL))
558 bio->bi_bdev = preq.bdev;
559 bio->bi_private = pending_req;
560 bio->bi_end_io = end_block_io_op;
561 bio->bi_sector = preq.sector_number;
564 preq.sector_number += seg[i].nsec;
568 BUG_ON(operation != WRITE_BARRIER);
569 bio = bio_alloc(GFP_KERNEL, 0);
570 if (unlikely(bio == NULL))
573 bio->bi_bdev = preq.bdev;
574 bio->bi_private = pending_req;
575 bio->bi_end_io = end_block_io_op;
579 submit_bio(operation, bio);
581 if (operation == READ)
582 blkif->st_rd_sect += preq.nr_sects;
583 else if (operation == WRITE || operation == WRITE_BARRIER)
584 blkif->st_wr_sect += preq.nr_sects;
589 fast_flush_area(pending_req);
591 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
592 free_req(pending_req);
593 msleep(1); /* back off a bit */
597 __end_block_io_op(pending_req, -EINVAL);
601 msleep(1); /* back off a bit */
607 /******************************************************************
608 * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
612 static void make_response(blkif_t *blkif, u64 id,
613 unsigned short op, int st)
615 blkif_response_t resp;
617 blkif_back_rings_t *blk_rings = &blkif->blk_rings;
625 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
626 /* Place on the response ring for the relevant domain. */
627 switch (blkif->blk_protocol) {
628 case BLKIF_PROTOCOL_NATIVE:
629 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
630 &resp, sizeof(resp));
632 case BLKIF_PROTOCOL_X86_32:
633 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
634 &resp, sizeof(resp));
636 case BLKIF_PROTOCOL_X86_64:
637 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
638 &resp, sizeof(resp));
643 blk_rings->common.rsp_prod_pvt++;
644 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
645 if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
647 * Tail check for pending requests. Allows frontend to avoid
648 * notifications if requests are already in flight (lower
649 * overheads and promotes batching).
651 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
653 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
657 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
660 blkif_notify_work(blkif);
662 notify_remote_via_irq(blkif->irq);
665 static int __init blkif_init(void)
669 if (!is_running_on_xen())
672 mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
674 pending_reqs = kzalloc(sizeof(pending_reqs[0]) *
675 blkif_reqs, GFP_KERNEL);
676 pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
677 mmap_pages, GFP_KERNEL);
678 pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
680 if (blkback_pagemap_init(mmap_pages))
683 if (!pending_reqs || !pending_grant_handles || !pending_pages)
686 for (i = 0; i < mmap_pages; i++)
687 pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
689 blkif_interface_init();
691 INIT_LIST_HEAD(&pending_free);
693 for (i = 0; i < blkif_reqs; i++)
694 list_add_tail(&pending_reqs[i].free_list, &pending_free);
702 kfree(pending_grant_handles);
703 free_empty_pages_and_pagevec(pending_pages, mmap_pages);
704 printk("%s: out of memory\n", __FUNCTION__);
708 module_init(blkif_init);
710 MODULE_LICENSE("Dual BSD/GPL");