xen/blkback: little cleanups
[deliverable/linux.git] / drivers / xen / blkback / blkback.c
CommitLineData
4d05a28d
KRW
1/******************************************************************************
2 * arch/xen/drivers/blkif/backend/main.c
3 *
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
9 *
10 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
11 * Copyright (c) 2005, Christopher Clark
12 *
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:
18 *
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:
25 *
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
28 *
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
35 * IN THE SOFTWARE.
36 */
37
38#include <linux/spinlock.h>
39#include <linux/kthread.h>
40#include <linux/list.h>
41#include <linux/delay.h>
88122933 42#include <linux/freezer.h>
afd91d07 43
4d05a28d 44#include <xen/balloon.h>
88122933
JF
45#include <xen/events.h>
46#include <xen/page.h>
47#include <asm/xen/hypervisor.h>
48#include <asm/xen/hypercall.h>
4d05a28d
KRW
49#include "common.h"
50
51/*
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.
55 *
56 * ** TRY INCREASING 'blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
57 *
58 * This will increase the chances of being able to write whole tracks.
59 * 64 should be enough to keep us competitive with Linux.
60 */
61static int blkif_reqs = 64;
62module_param_named(reqs, blkif_reqs, int, 0);
63MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
64
65/* Run-time switchable: /sys/module/blkback/parameters/ */
66static unsigned int log_stats = 0;
67static unsigned int debug_lvl = 0;
68module_param(log_stats, int, 0644);
69module_param(debug_lvl, int, 0644);
70
71/*
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.
76 */
77typedef struct {
78 blkif_t *blkif;
79 u64 id;
80 int nr_pages;
81 atomic_t pendcnt;
82 unsigned short operation;
83 int status;
84 struct list_head free_list;
85} pending_req_t;
86
87static pending_req_t *pending_reqs;
88static struct list_head pending_free;
89static DEFINE_SPINLOCK(pending_free_lock);
90static DECLARE_WAIT_QUEUE_HEAD(pending_free_wq);
91
92#define BLKBACK_INVALID_HANDLE (~0)
93
94static struct page **pending_pages;
95static grant_handle_t *pending_grant_handles;
96
97static inline int vaddr_pagenr(pending_req_t *req, int seg)
98{
99 return (req - pending_reqs) * BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
100}
101
102static inline unsigned long vaddr(pending_req_t *req, int seg)
103{
104 unsigned long pfn = page_to_pfn(pending_pages[vaddr_pagenr(req, seg)]);
105 return (unsigned long)pfn_to_kaddr(pfn);
106}
107
108#define pending_handle(_req, _seg) \
109 (pending_grant_handles[vaddr_pagenr(_req, _seg)])
110
111
112static int do_block_io_op(blkif_t *blkif);
113static void dispatch_rw_block_io(blkif_t *blkif,
88122933 114 struct blkif_request *req,
4d05a28d
KRW
115 pending_req_t *pending_req);
116static void make_response(blkif_t *blkif, u64 id,
117 unsigned short op, int st);
118
119/******************************************************************
120 * misc small helpers
121 */
122static pending_req_t* alloc_req(void)
123{
124 pending_req_t *req = NULL;
125 unsigned long flags;
126
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);
131 }
132 spin_unlock_irqrestore(&pending_free_lock, flags);
133 return req;
134}
135
136static void free_req(pending_req_t *req)
137{
138 unsigned long flags;
139 int was_empty;
140
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);
145 if (was_empty)
146 wake_up(&pending_free_wq);
147}
148
149static void unplug_queue(blkif_t *blkif)
150{
151 if (blkif->plug == NULL)
152 return;
153 if (blkif->plug->unplug_fn)
154 blkif->plug->unplug_fn(blkif->plug);
155 blk_put_queue(blkif->plug);
156 blkif->plug = NULL;
157}
158
159static void plug_queue(blkif_t *blkif, struct block_device *bdev)
160{
88122933 161 struct request_queue *q = bdev_get_queue(bdev);
4d05a28d
KRW
162
163 if (q == blkif->plug)
164 return;
165 unplug_queue(blkif);
166 blk_get_queue(q);
167 blkif->plug = q;
168}
169
170static void fast_flush_area(pending_req_t *req)
171{
172 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
173 unsigned int i, invcount = 0;
174 grant_handle_t handle;
175 int ret;
176
177 for (i = 0; i < req->nr_pages; i++) {
178 handle = pending_handle(req, i);
179 if (handle == BLKBACK_INVALID_HANDLE)
180 continue;
181 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
182 GNTMAP_host_map, handle);
183 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
184 invcount++;
185 }
186
187 ret = HYPERVISOR_grant_table_op(
188 GNTTABOP_unmap_grant_ref, unmap, invcount);
189 BUG_ON(ret);
190}
191
192/******************************************************************
193 * SCHEDULER FUNCTIONS
194 */
195
196static void print_stats(blkif_t *blkif)
197{
198 printk(KERN_DEBUG "%s: oo %3d | rd %4d | wr %4d | br %4d\n",
199 current->comm, blkif->st_oo_req,
200 blkif->st_rd_req, blkif->st_wr_req, blkif->st_br_req);
201 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
202 blkif->st_rd_req = 0;
203 blkif->st_wr_req = 0;
204 blkif->st_oo_req = 0;
205}
206
207int blkif_schedule(void *arg)
208{
209 blkif_t *blkif = arg;
210
211 blkif_get(blkif);
212
213 if (debug_lvl)
214 printk(KERN_DEBUG "%s: started\n", current->comm);
215
216 while (!kthread_should_stop()) {
217 if (try_to_freeze())
218 continue;
219
220 wait_event_interruptible(
221 blkif->wq,
222 blkif->waiting_reqs || kthread_should_stop());
223 wait_event_interruptible(
224 pending_free_wq,
225 !list_empty(&pending_free) || kthread_should_stop());
226
227 blkif->waiting_reqs = 0;
228 smp_mb(); /* clear flag *before* checking for work */
229
230 if (do_block_io_op(blkif))
231 blkif->waiting_reqs = 1;
232 unplug_queue(blkif);
233
234 if (log_stats && time_after(jiffies, blkif->st_print))
235 print_stats(blkif);
236 }
237
238 if (log_stats)
239 print_stats(blkif);
240 if (debug_lvl)
241 printk(KERN_DEBUG "%s: exiting\n", current->comm);
242
243 blkif->xenblkd = NULL;
244 blkif_put(blkif);
245
246 return 0;
247}
248
249/******************************************************************
250 * COMPLETION CALLBACK -- Called as bh->b_end_io()
251 */
252
253static void __end_block_io_op(pending_req_t *pending_req, int error)
254{
255 /* An error fails the entire request. */
256 if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
257 (error == -EOPNOTSUPP)) {
258 DPRINTK("blkback: write barrier op failed, not supported\n");
259 blkback_barrier(XBT_NIL, pending_req->blkif->be, 0);
260 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
261 } else if (error) {
262 DPRINTK("Buffer not up-to-date at end of operation, "
263 "error=%d\n", error);
264 pending_req->status = BLKIF_RSP_ERROR;
265 }
266
267 if (atomic_dec_and_test(&pending_req->pendcnt)) {
268 fast_flush_area(pending_req);
269 make_response(pending_req->blkif, pending_req->id,
270 pending_req->operation, pending_req->status);
271 blkif_put(pending_req->blkif);
272 free_req(pending_req);
273 }
274}
275
88122933 276static void end_block_io_op(struct bio *bio, int error)
4d05a28d 277{
4d05a28d
KRW
278 __end_block_io_op(bio->bi_private, error);
279 bio_put(bio);
4d05a28d
KRW
280}
281
282
283/******************************************************************************
284 * NOTIFICATION FROM GUEST OS.
285 */
286
287static void blkif_notify_work(blkif_t *blkif)
288{
289 blkif->waiting_reqs = 1;
290 wake_up(&blkif->wq);
291}
292
88122933 293irqreturn_t blkif_be_int(int irq, void *dev_id)
4d05a28d
KRW
294{
295 blkif_notify_work(dev_id);
296 return IRQ_HANDLED;
297}
298
299
300
301/******************************************************************
302 * DOWNWARD CALLS -- These interface with the block-device layer proper.
303 */
304
305static int do_block_io_op(blkif_t *blkif)
306{
88122933
JF
307 union blkif_back_rings *blk_rings = &blkif->blk_rings;
308 struct blkif_request req;
4d05a28d
KRW
309 pending_req_t *pending_req;
310 RING_IDX rc, rp;
311 int more_to_do = 0;
312
313 rc = blk_rings->common.req_cons;
314 rp = blk_rings->common.sring->req_prod;
315 rmb(); /* Ensure we see queued requests up to 'rp'. */
316
317 while (rc != rp) {
318
319 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
320 break;
321
8270b45b 322 if (kthread_should_stop()) {
4d05a28d
KRW
323 more_to_do = 1;
324 break;
325 }
326
8270b45b
KF
327 pending_req = alloc_req();
328 if (NULL == pending_req) {
329 blkif->st_oo_req++;
4d05a28d
KRW
330 more_to_do = 1;
331 break;
332 }
333
334 switch (blkif->blk_protocol) {
335 case BLKIF_PROTOCOL_NATIVE:
336 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
337 break;
338 case BLKIF_PROTOCOL_X86_32:
339 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
340 break;
341 case BLKIF_PROTOCOL_X86_64:
342 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
343 break;
344 default:
345 BUG();
346 }
347 blk_rings->common.req_cons = ++rc; /* before make_response() */
348
349 /* Apply all sanity checks to /private copy/ of request. */
350 barrier();
351
352 switch (req.operation) {
353 case BLKIF_OP_READ:
354 blkif->st_rd_req++;
355 dispatch_rw_block_io(blkif, &req, pending_req);
356 break;
357 case BLKIF_OP_WRITE_BARRIER:
358 blkif->st_br_req++;
359 /* fall through */
360 case BLKIF_OP_WRITE:
361 blkif->st_wr_req++;
362 dispatch_rw_block_io(blkif, &req, pending_req);
363 break;
364 default:
365 /* A good sign something is wrong: sleep for a while to
366 * avoid excessive CPU consumption by a bad guest. */
367 msleep(1);
368 DPRINTK("error: unknown block io operation [%d]\n",
369 req.operation);
370 make_response(blkif, req.id, req.operation,
371 BLKIF_RSP_ERROR);
372 free_req(pending_req);
373 break;
374 }
375
376 /* Yield point for this unbounded loop. */
377 cond_resched();
378 }
379
380 return more_to_do;
381}
382
383static void dispatch_rw_block_io(blkif_t *blkif,
88122933 384 struct blkif_request *req,
4d05a28d
KRW
385 pending_req_t *pending_req)
386{
4d05a28d
KRW
387 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
388 struct phys_req preq;
389 struct {
390 unsigned long buf; unsigned int nsec;
391 } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
392 unsigned int nseg;
393 struct bio *bio = NULL;
394 int ret, i;
395 int operation;
396
397 switch (req->operation) {
398 case BLKIF_OP_READ:
399 operation = READ;
400 break;
401 case BLKIF_OP_WRITE:
402 operation = WRITE;
403 break;
404 case BLKIF_OP_WRITE_BARRIER:
405 operation = WRITE_BARRIER;
406 break;
407 default:
408 operation = 0; /* make gcc happy */
409 BUG();
410 }
411
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);
417 goto fail_response;
418 }
419
420 preq.dev = req->handle;
421 preq.sector_number = req->sector_number;
422 preq.nr_sects = 0;
423
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;
429
430 for (i = 0; i < nseg; i++) {
431 uint32_t flags;
432
433 seg[i].nsec = req->seg[i].last_sect -
434 req->seg[i].first_sect + 1;
435
436 if ((req->seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
437 (req->seg[i].last_sect < req->seg[i].first_sect))
438 goto fail_response;
439 preq.nr_sects += seg[i].nsec;
440
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);
446 }
447
448 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
449 BUG_ON(ret);
450
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;
455 ret |= 1;
456 }
457
458 pending_handle(pending_req, i) = map[i].handle;
459
460 if (ret)
461 continue;
462
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);
468 }
469
470 if (ret)
471 goto fail_flush;
472
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",
476 preq.sector_number,
477 preq.sector_number + preq.nr_sects, preq.dev);
478 goto fail_flush;
479 }
480
481 plug_queue(blkif, preq.bdev);
482 atomic_set(&pending_req->pendcnt, 1);
483 blkif_get(blkif);
484
485 for (i = 0; i < nseg; i++) {
486 if (((int)preq.sector_number|(int)seg[i].nsec) &
05d43865 487 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
4d05a28d
KRW
488 DPRINTK("Misaligned I/O request from domain %d",
489 blkif->domid);
490 goto fail_put_bio;
491 }
492
493 while ((bio == NULL) ||
494 (bio_add_page(bio,
495 virt_to_page(vaddr(pending_req, i)),
496 seg[i].nsec << 9,
497 seg[i].buf & ~PAGE_MASK) == 0)) {
498 if (bio) {
499 atomic_inc(&pending_req->pendcnt);
500 submit_bio(operation, bio);
501 }
502
503 bio = bio_alloc(GFP_KERNEL, nseg-i);
504 if (unlikely(bio == NULL))
505 goto fail_put_bio;
506
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;
511 }
512
513 preq.sector_number += seg[i].nsec;
514 }
515
516 if (!bio) {
517 BUG_ON(operation != WRITE_BARRIER);
518 bio = bio_alloc(GFP_KERNEL, 0);
519 if (unlikely(bio == NULL))
520 goto fail_put_bio;
521
522 bio->bi_bdev = preq.bdev;
523 bio->bi_private = pending_req;
524 bio->bi_end_io = end_block_io_op;
525 bio->bi_sector = -1;
526 }
527
528 submit_bio(operation, bio);
529
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;
534
535 return;
536
537 fail_flush:
538 fast_flush_area(pending_req);
539 fail_response:
540 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
541 free_req(pending_req);
542 msleep(1); /* back off a bit */
543 return;
544
545 fail_put_bio:
546 __end_block_io_op(pending_req, -EINVAL);
547 if (bio)
548 bio_put(bio);
549 unplug_queue(blkif);
550 msleep(1); /* back off a bit */
551 return;
552}
553
554
555
556/******************************************************************
557 * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
558 */
559
560
561static void make_response(blkif_t *blkif, u64 id,
562 unsigned short op, int st)
563{
88122933 564 struct blkif_response resp;
4d05a28d 565 unsigned long flags;
88122933 566 union blkif_back_rings *blk_rings = &blkif->blk_rings;
4d05a28d
KRW
567 int more_to_do = 0;
568 int notify;
569
570 resp.id = id;
571 resp.operation = op;
572 resp.status = st;
573
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));
580 break;
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));
584 break;
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));
588 break;
589 default:
590 BUG();
591 }
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) {
595 /*
596 * Tail check for pending requests. Allows frontend to avoid
597 * notifications if requests are already in flight (lower
598 * overheads and promotes batching).
599 */
600 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
601
602 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
603 more_to_do = 1;
604 }
605
606 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
607
608 if (more_to_do)
609 blkif_notify_work(blkif);
610 if (notify)
611 notify_remote_via_irq(blkif->irq);
612}
613
614static int __init blkif_init(void)
615{
616 int i, mmap_pages;
617
88122933 618 if (!xen_pv_domain())
4d05a28d
KRW
619 return -ENODEV;
620
621 mmap_pages = blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
622
623 pending_reqs = kmalloc(sizeof(pending_reqs[0]) *
624 blkif_reqs, GFP_KERNEL);
625 pending_grant_handles = kmalloc(sizeof(pending_grant_handles[0]) *
626 mmap_pages, GFP_KERNEL);
627 pending_pages = alloc_empty_pages_and_pagevec(mmap_pages);
628
629 if (!pending_reqs || !pending_grant_handles || !pending_pages)
630 goto out_of_memory;
631
632 for (i = 0; i < mmap_pages; i++)
633 pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
634
635 blkif_interface_init();
636
637 memset(pending_reqs, 0, sizeof(pending_reqs));
638 INIT_LIST_HEAD(&pending_free);
639
640 for (i = 0; i < blkif_reqs; i++)
641 list_add_tail(&pending_reqs[i].free_list, &pending_free);
642
643 blkif_xenbus_init();
644
645 return 0;
646
647 out_of_memory:
648 kfree(pending_reqs);
649 kfree(pending_grant_handles);
650 free_empty_pages_and_pagevec(pending_pages, mmap_pages);
651 printk("%s: out of memory\n", __FUNCTION__);
652 return -ENOMEM;
653}
654
655module_init(blkif_init);
656
657MODULE_LICENSE("Dual BSD/GPL");
This page took 0.055113 seconds and 5 git commands to generate.