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 <xen/balloon.h>
42 #include <asm/hypervisor.h>
43 #include <asm/hypercall.h>
47 * These are rather arbitrary. They are fairly large because adjacent requests
48 * pulled from a communication ring are quite likely to end up being part of
49 * the same scatter/gather request at the disc.
51 * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
53 * This will increase the chances of being able to write whole tracks.
54 * 64 should be enough to keep us competitive with Linux.
56 static int blkif_reqs = 64;
57 module_param_named(reqs, blkif_reqs, int, 0);
58 MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
60 /* Run-time switchable: /sys/module/blkback/parameters/ */
61 static unsigned int log_stats = 0;
62 static unsigned int debug_lvl = 0;
63 module_param(log_stats, int, 0644);
64 module_param(debug_lvl, int, 0644);
67 * Each outstanding request that we've passed to the lower device layers has a
68 * 'pending_req' allocated to it. Each buffer_head that completes decrements
69 * the pendcnt towards zero. When it hits zero, the specified domain has a
70 * response queued for it, with the saved 'id' passed back.
77 unsigned short operation;
79 struct list_head free_list;
82 static pending_req_t *pending_reqs;
83 static struct list_head pending_free;
84 static DEFINE_SPINLOCK(pending_free_lock);
85 static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq);
87 #define BLKBACK_INVALID_HANDLE (~0)
89 static struct page **pending_pages;
90 static grant_handle_t *pending_grant_handles;
92 static inline int vaddr_pagenr(pending_req_t *req, int seg)
94 return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
97 static inline unsigned long vaddr(pending_req_t *req, int seg)
99 unsigned long pfn = page_to_pfn(pending_pages[vaddr_pagenr(req, seg)]);
100 return (unsigned long)pfn_to_kaddr(pfn);
103 #define pending_handle(_req, _seg) \
104 (pending_grant_handles[vaddr_pagenr(_req, _seg)])
107 static int do_block_io_op(blkif_t *blkif);
108 static void dispatch_rw_block_io(blkif_t *blkif,
109 blkif_request_t *req,
110 pending_req_t *pending_req);
111 static void make_response(blkif_t *blkif, unsigned long id,
112 unsigned short op, int st);
114 /******************************************************************
117 static pending_req_t* alloc_req(void)
119 pending_req_t *req = NULL;
122 spin_lock_irqsave(&pending_free_lock, flags);
123 if (!list_empty(&pending_free)) {
124 req = list_entry(pending_free.next, pending_req_t, free_list);
125 list_del(&req->free_list);
127 spin_unlock_irqrestore(&pending_free_lock, flags);
131 static void free_req(pending_req_t *req)
136 spin_lock_irqsave(&pending_free_lock, flags);
137 was_empty = list_empty(&pending_free);
138 list_add(&req->free_list, &pending_free);
139 spin_unlock_irqrestore(&pending_free_lock, flags);
141 wake_up(&pending_free_wq);
144 static void unplug_queue(blkif_t *blkif)
146 if (blkif->plug == NULL)
148 if (blkif->plug->unplug_fn)
149 blkif->plug->unplug_fn(blkif->plug);
150 blk_put_queue(blkif->plug);
154 static void plug_queue(blkif_t *blkif, struct bio *bio)
156 request_queue_t *q = bdev_get_queue(bio->bi_bdev);
158 if (q == blkif->plug)
165 static void fast_flush_area(pending_req_t *req)
167 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
168 unsigned int i, invcount = 0;
169 grant_handle_t handle;
172 for (i = 0; i < req->nr_pages; i++) {
173 handle = pending_handle(req, i);
174 if (handle == BLKBACK_INVALID_HANDLE)
176 gnttab_set_unmap_op(&unmap[i], vaddr(req, i), GNTMAP_host_map,
178 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
182 ret = HYPERVISOR_grant_table_op(
183 GNTTABOP_unmap_grant_ref, unmap, invcount);
187 /******************************************************************
188 * SCHEDULER FUNCTIONS
191 static void print_stats(blkif_t *blkif)
193 printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d\n",
194 current->comm, blkif->st_oo_req,
195 blkif->st_rd_req, blkif->st_wr_req);
196 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
197 blkif->st_rd_req = 0;
198 blkif->st_wr_req = 0;
199 blkif->st_oo_req = 0;
202 int blkif_schedule(void *arg)
204 blkif_t *blkif = arg;
209 printk(KERN_DEBUG "%s: started\n", current->comm);
211 while (!kthread_should_stop()) {
212 wait_event_interruptible(
214 blkif->waiting_reqs || kthread_should_stop());
215 wait_event_interruptible(
217 !list_empty(&pending_free) || kthread_should_stop());
219 blkif->waiting_reqs = 0;
220 smp_mb(); /* clear flag *before* checking for work */
222 if (do_block_io_op(blkif))
223 blkif->waiting_reqs = 1;
226 if (log_stats && time_after(jiffies, blkif->st_print))
233 printk(KERN_DEBUG "%s: exiting\n", current->comm);
235 blkif->xenblkd = NULL;
241 /******************************************************************
242 * COMPLETION CALLBACK -- Called as bh->b_end_io()
245 static void __end_block_io_op(pending_req_t *pending_req, int uptodate)
247 /* An error fails the entire request. */
249 DPRINTK("Buffer not up-to-date at end of operation\n");
250 pending_req->status = BLKIF_RSP_ERROR;
253 if (atomic_dec_and_test(&pending_req->pendcnt)) {
254 fast_flush_area(pending_req);
255 make_response(pending_req->blkif, pending_req->id,
256 pending_req->operation, pending_req->status);
257 blkif_put(pending_req->blkif);
258 free_req(pending_req);
262 static int end_block_io_op(struct bio *bio, unsigned int done, int error)
264 if (bio->bi_size != 0)
266 __end_block_io_op(bio->bi_private, !error);
272 /******************************************************************************
273 * NOTIFICATION FROM GUEST OS.
276 static void blkif_notify_work(blkif_t *blkif)
278 blkif->waiting_reqs = 1;
282 irqreturn_t blkif_be_int(int irq, void *dev_id, struct pt_regs *regs)
284 blkif_notify_work(dev_id);
290 /******************************************************************
291 * DOWNWARD CALLS -- These interface with the block-device layer proper.
294 static int do_block_io_op(blkif_t *blkif)
296 blkif_back_ring_t *blk_ring = &blkif->blk_ring;
297 blkif_request_t *req;
298 pending_req_t *pending_req;
302 rc = blk_ring->req_cons;
303 rp = blk_ring->sring->req_prod;
304 rmb(); /* Ensure we see queued requests up to 'rp'. */
306 while ((rc != rp) && !RING_REQUEST_CONS_OVERFLOW(blk_ring, rc)) {
308 pending_req = alloc_req();
309 if (NULL == pending_req) {
315 req = RING_GET_REQUEST(blk_ring, rc);
316 blk_ring->req_cons = ++rc; /* before make_response() */
318 switch (req->operation) {
321 dispatch_rw_block_io(blkif, req, pending_req);
325 dispatch_rw_block_io(blkif, req, pending_req);
328 DPRINTK("error: unknown block io operation [%d]\n",
330 make_response(blkif, req->id, req->operation,
332 free_req(pending_req);
339 static void dispatch_rw_block_io(blkif_t *blkif,
340 blkif_request_t *req,
341 pending_req_t *pending_req)
343 extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]);
344 int operation = (req->operation == BLKIF_OP_WRITE) ? WRITE : READ;
345 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
346 struct phys_req preq;
348 unsigned long buf; unsigned int nsec;
349 } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
351 struct bio *bio = NULL, *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
352 int ret, i, nbio = 0;
354 /* Check that number of segments is sane. */
355 nseg = req->nr_segments;
356 if (unlikely(nseg == 0) ||
357 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
358 DPRINTK("Bad number of segments in request (%d)\n", nseg);
362 preq.dev = req->handle;
363 preq.sector_number = req->sector_number;
366 pending_req->blkif = blkif;
367 pending_req->id = req->id;
368 pending_req->operation = operation;
369 pending_req->status = BLKIF_RSP_OKAY;
370 pending_req->nr_pages = nseg;
372 for (i = 0; i < nseg; i++) {
375 seg[i].nsec = req->seg[i].last_sect -
376 req->seg[i].first_sect + 1;
378 if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
381 preq.nr_sects += seg[i].nsec;
383 flags = GNTMAP_host_map;
384 if ( operation == WRITE )
385 flags |= GNTMAP_readonly;
386 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
387 req->seg[i].gref, blkif->domid);
390 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
393 for (i = 0; i < nseg; i++) {
394 if (unlikely(map[i].status != 0)) {
395 DPRINTK("invalid buffer -- could not remap it\n");
399 pending_handle(pending_req, i) = map[i].handle;
400 set_phys_to_machine(__pa(vaddr(
401 pending_req, i)) >> PAGE_SHIFT,
402 FOREIGN_FRAME(map[i].dev_bus_addr >> PAGE_SHIFT));
403 seg[i].buf = map[i].dev_bus_addr |
404 (req->seg[i].first_sect << 9);
407 if (vbd_translate(&preq, blkif, operation) != 0) {
408 DPRINTK("access denied: %s of [%llu,%llu] on dev=%04x\n",
409 operation == READ ? "read" : "write",
411 preq.sector_number + preq.nr_sects, preq.dev);
415 for (i = 0; i < nseg; i++) {
416 if (((int)preq.sector_number|(int)seg[i].nsec) &
417 ((bdev_hardsect_size(preq.bdev) >> 9) - 1)) {
418 DPRINTK("Misaligned I/O request from domain %d",
423 while ((bio == NULL) ||
425 virt_to_page(vaddr(pending_req, i)),
427 seg[i].buf & ~PAGE_MASK) == 0)) {
428 bio = biolist[nbio++] = bio_alloc(GFP_KERNEL, nseg-i);
429 if (unlikely(bio == NULL))
432 bio->bi_bdev = preq.bdev;
433 bio->bi_private = pending_req;
434 bio->bi_end_io = end_block_io_op;
435 bio->bi_sector = preq.sector_number;
438 preq.sector_number += seg[i].nsec;
441 plug_queue(blkif, bio);
442 atomic_set(&pending_req->pendcnt, nbio);
445 for (i = 0; i < nbio; i++)
446 submit_bio(operation, biolist[i]);
451 for (i = 0; i < (nbio-1); i++)
454 fast_flush_area(pending_req);
456 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
457 free_req(pending_req);
462 /******************************************************************
463 * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
467 static void make_response(blkif_t *blkif, unsigned long id,
468 unsigned short op, int st)
470 blkif_response_t *resp;
472 blkif_back_ring_t *blk_ring = &blkif->blk_ring;
476 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
478 /* Place on the response ring for the relevant domain. */
479 resp = RING_GET_RESPONSE(blk_ring, blk_ring->rsp_prod_pvt);
481 resp->operation = op;
483 blk_ring->rsp_prod_pvt++;
484 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(blk_ring, notify);
486 if (blk_ring->rsp_prod_pvt == blk_ring->req_cons) {
488 * Tail check for pending requests. Allows frontend to avoid
489 * notifications if requests are already in flight (lower
490 * overheads and promotes batching).
492 RING_FINAL_CHECK_FOR_REQUESTS(blk_ring, more_to_do);
494 } else if (RING_HAS_UNCONSUMED_REQUESTS(blk_ring)) {
498 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
501 blkif_notify_work(blkif);
503 notify_remote_via_irq(blkif->irq);
506 static int __init blkif_init(void)
510 if (!is_running_on_xen())
513 mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
515 pending_reqs = kmalloc(sizeof(pending_reqs[0]) *
516 blkif_reqs, GFP_KERNEL);
517 pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
518 mmap_pages, GFP_KERNEL);
519 pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
521 if (!pending_reqs || !pending_grant_handles || !pending_pages)
524 for (i = 0; i < mmap_pages; i++)
525 pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
527 blkif_interface_init();
529 memset(pending_reqs, 0, sizeof(pending_reqs));
530 INIT_LIST_HEAD(&pending_free);
532 for (i = 0; i < blkif_reqs; i++)
533 list_add_tail(&pending_reqs[i].free_list, &pending_free);
541 kfree(pending_grant_handles);
542 free_empty_pages_and_pagevec(pending_pages, mmap_pages);
543 printk("%s: out of memory\n", __FUNCTION__);
547 module_init(blkif_init);
549 MODULE_LICENSE("Dual BSD/GPL");