1 /******************************************************************************
3 * Back-end of the driver for virtual block devices. This portion of the
4 * driver exports a 'unified' block-device interface that can be accessed
5 * by any operating system that implements a compatible front end. A
6 * reference front-end implementation can be found in:
7 * drivers/block/xen-blkfront.c
9 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
10 * Copyright (c) 2005, Christopher Clark
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation; or, when distributed
15 * separately from the Linux kernel or incorporated into other
16 * software packages, subject to the following license:
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this source file (the "Software"), to deal in the Software without
20 * restriction, including without limitation the rights to use, copy, modify,
21 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
22 * and to permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
37 #include <linux/spinlock.h>
38 #include <linux/kthread.h>
39 #include <linux/list.h>
40 #include <linux/delay.h>
41 #include <linux/freezer.h>
42 #include <linux/bitmap.h>
44 #include <xen/events.h>
47 #include <asm/xen/hypervisor.h>
48 #include <asm/xen/hypercall.h>
49 #include <xen/balloon.h>
53 * Maximum number of unused free pages to keep in the internal buffer.
54 * Setting this to a value too low will reduce memory used in each backend,
55 * but can have a performance penalty.
57 * A sane value is xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST, but can
58 * be set to a lower value that might degrade performance on some intensive
62 static int xen_blkif_max_buffer_pages = 1024;
63 module_param_named(max_buffer_pages, xen_blkif_max_buffer_pages, int, 0644);
64 MODULE_PARM_DESC(max_buffer_pages,
65 "Maximum number of free pages to keep in each block backend buffer");
68 * Maximum number of grants to map persistently in blkback. For maximum
69 * performance this should be the total numbers of grants that can be used
70 * to fill the ring, but since this might become too high, specially with
71 * the use of indirect descriptors, we set it to a value that provides good
72 * performance without using too much memory.
74 * When the list of persistent grants is full we clean it up using a LRU
78 static int xen_blkif_max_pgrants = 1056;
79 module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644);
80 MODULE_PARM_DESC(max_persistent_grants,
81 "Maximum number of grants to map persistently");
84 * The LRU mechanism to clean the lists of persistent grants needs to
85 * be executed periodically. The time interval between consecutive executions
86 * of the purge mechanism is set in ms.
88 #define LRU_INTERVAL 100
91 * When the persistent grants list is full we will remove unused grants
92 * from the list. The percent number of grants to be removed at each LRU
95 #define LRU_PERCENT_CLEAN 5
97 /* Run-time switchable: /sys/module/blkback/parameters/ */
98 static unsigned int log_stats;
99 module_param(log_stats, int, 0644);
101 #define BLKBACK_INVALID_HANDLE (~0)
103 /* Number of free pages to remove on each call to free_xenballooned_pages */
104 #define NUM_BATCH_FREE_PAGES 10
106 static inline int get_free_page(struct xen_blkif *blkif, struct page **page)
110 spin_lock_irqsave(&blkif->free_pages_lock, flags);
111 if (list_empty(&blkif->free_pages)) {
112 BUG_ON(blkif->free_pages_num != 0);
113 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
114 return alloc_xenballooned_pages(1, page, false);
116 BUG_ON(blkif->free_pages_num == 0);
117 page[0] = list_first_entry(&blkif->free_pages, struct page, lru);
118 list_del(&page[0]->lru);
119 blkif->free_pages_num--;
120 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
125 static inline void put_free_pages(struct xen_blkif *blkif, struct page **page,
131 spin_lock_irqsave(&blkif->free_pages_lock, flags);
132 for (i = 0; i < num; i++)
133 list_add(&page[i]->lru, &blkif->free_pages);
134 blkif->free_pages_num += num;
135 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
138 static inline void shrink_free_pagepool(struct xen_blkif *blkif, int num)
140 /* Remove requested pages in batches of NUM_BATCH_FREE_PAGES */
141 struct page *page[NUM_BATCH_FREE_PAGES];
142 unsigned int num_pages = 0;
145 spin_lock_irqsave(&blkif->free_pages_lock, flags);
146 while (blkif->free_pages_num > num) {
147 BUG_ON(list_empty(&blkif->free_pages));
148 page[num_pages] = list_first_entry(&blkif->free_pages,
150 list_del(&page[num_pages]->lru);
151 blkif->free_pages_num--;
152 if (++num_pages == NUM_BATCH_FREE_PAGES) {
153 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
154 free_xenballooned_pages(num_pages, page);
155 spin_lock_irqsave(&blkif->free_pages_lock, flags);
159 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
161 free_xenballooned_pages(num_pages, page);
164 #define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
166 static int do_block_io_op(struct xen_blkif *blkif);
167 static int dispatch_rw_block_io(struct xen_blkif *blkif,
168 struct blkif_request *req,
169 struct pending_req *pending_req);
170 static void make_response(struct xen_blkif *blkif, u64 id,
171 unsigned short op, int st);
173 #define foreach_grant_safe(pos, n, rbtree, node) \
174 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
175 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL; \
176 &(pos)->node != NULL; \
177 (pos) = container_of(n, typeof(*(pos)), node), \
178 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
182 * We don't need locking around the persistent grant helpers
183 * because blkback uses a single-thread for each backed, so we
184 * can be sure that this functions will never be called recursively.
186 * The only exception to that is put_persistent_grant, that can be called
187 * from interrupt context (by xen_blkbk_unmap), so we have to use atomic
188 * bit operations to modify the flags of a persistent grant and to count
189 * the number of used grants.
191 static int add_persistent_gnt(struct xen_blkif *blkif,
192 struct persistent_gnt *persistent_gnt)
194 struct rb_node **new = NULL, *parent = NULL;
195 struct persistent_gnt *this;
197 if (blkif->persistent_gnt_c >= xen_blkif_max_pgrants) {
198 if (!blkif->vbd.overflow_max_grants)
199 blkif->vbd.overflow_max_grants = 1;
202 /* Figure out where to put new node */
203 new = &blkif->persistent_gnts.rb_node;
205 this = container_of(*new, struct persistent_gnt, node);
208 if (persistent_gnt->gnt < this->gnt)
209 new = &((*new)->rb_left);
210 else if (persistent_gnt->gnt > this->gnt)
211 new = &((*new)->rb_right);
213 pr_alert_ratelimited(DRV_PFX " trying to add a gref that's already in the tree\n");
218 bitmap_zero(persistent_gnt->flags, PERSISTENT_GNT_FLAGS_SIZE);
219 set_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
220 /* Add new node and rebalance tree. */
221 rb_link_node(&(persistent_gnt->node), parent, new);
222 rb_insert_color(&(persistent_gnt->node), &blkif->persistent_gnts);
223 blkif->persistent_gnt_c++;
224 atomic_inc(&blkif->persistent_gnt_in_use);
228 static struct persistent_gnt *get_persistent_gnt(struct xen_blkif *blkif,
231 struct persistent_gnt *data;
232 struct rb_node *node = NULL;
234 node = blkif->persistent_gnts.rb_node;
236 data = container_of(node, struct persistent_gnt, node);
238 if (gref < data->gnt)
239 node = node->rb_left;
240 else if (gref > data->gnt)
241 node = node->rb_right;
243 if(test_bit(PERSISTENT_GNT_ACTIVE, data->flags)) {
244 pr_alert_ratelimited(DRV_PFX " requesting a grant already in use\n");
247 set_bit(PERSISTENT_GNT_ACTIVE, data->flags);
248 atomic_inc(&blkif->persistent_gnt_in_use);
255 static void put_persistent_gnt(struct xen_blkif *blkif,
256 struct persistent_gnt *persistent_gnt)
258 if(!test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
259 pr_alert_ratelimited(DRV_PFX " freeing a grant already unused");
260 set_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
261 clear_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
262 atomic_dec(&blkif->persistent_gnt_in_use);
265 static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
268 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
269 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
270 struct persistent_gnt *persistent_gnt;
273 int segs_to_unmap = 0;
275 foreach_grant_safe(persistent_gnt, n, root, node) {
276 BUG_ON(persistent_gnt->handle ==
277 BLKBACK_INVALID_HANDLE);
278 gnttab_set_unmap_op(&unmap[segs_to_unmap],
279 (unsigned long) pfn_to_kaddr(page_to_pfn(
280 persistent_gnt->page)),
282 persistent_gnt->handle);
284 pages[segs_to_unmap] = persistent_gnt->page;
286 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
287 !rb_next(&persistent_gnt->node)) {
288 ret = gnttab_unmap_refs(unmap, NULL, pages,
291 put_free_pages(blkif, pages, segs_to_unmap);
295 rb_erase(&persistent_gnt->node, root);
296 kfree(persistent_gnt);
302 static void unmap_purged_grants(struct work_struct *work)
304 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
305 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
306 struct persistent_gnt *persistent_gnt;
307 int ret, segs_to_unmap = 0;
308 struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
310 while(!list_empty(&blkif->persistent_purge_list)) {
311 persistent_gnt = list_first_entry(&blkif->persistent_purge_list,
312 struct persistent_gnt,
314 list_del(&persistent_gnt->remove_node);
316 gnttab_set_unmap_op(&unmap[segs_to_unmap],
317 vaddr(persistent_gnt->page),
319 persistent_gnt->handle);
321 pages[segs_to_unmap] = persistent_gnt->page;
323 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
324 ret = gnttab_unmap_refs(unmap, NULL, pages,
327 put_free_pages(blkif, pages, segs_to_unmap);
330 kfree(persistent_gnt);
332 if (segs_to_unmap > 0) {
333 ret = gnttab_unmap_refs(unmap, NULL, pages, segs_to_unmap);
335 put_free_pages(blkif, pages, segs_to_unmap);
339 static void purge_persistent_gnt(struct xen_blkif *blkif)
341 struct persistent_gnt *persistent_gnt;
343 unsigned int num_clean, total;
344 bool scan_used = false, clean_used = false;
345 struct rb_root *root;
347 if (blkif->persistent_gnt_c < xen_blkif_max_pgrants ||
348 (blkif->persistent_gnt_c == xen_blkif_max_pgrants &&
349 !blkif->vbd.overflow_max_grants)) {
353 if (work_pending(&blkif->persistent_purge_work)) {
354 pr_alert_ratelimited(DRV_PFX "Scheduled work from previous purge is still pending, cannot purge list\n");
358 num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
359 num_clean = blkif->persistent_gnt_c - xen_blkif_max_pgrants + num_clean;
360 num_clean = min(blkif->persistent_gnt_c, num_clean);
361 if ((num_clean == 0) ||
362 (num_clean > (blkif->persistent_gnt_c - atomic_read(&blkif->persistent_gnt_in_use))))
366 * At this point, we can assure that there will be no calls
367 * to get_persistent_grant (because we are executing this code from
368 * xen_blkif_schedule), there can only be calls to put_persistent_gnt,
369 * which means that the number of currently used grants will go down,
370 * but never up, so we will always be able to remove the requested
376 pr_debug(DRV_PFX "Going to purge %u persistent grants\n", num_clean);
378 INIT_LIST_HEAD(&blkif->persistent_purge_list);
379 root = &blkif->persistent_gnts;
381 foreach_grant_safe(persistent_gnt, n, root, node) {
382 BUG_ON(persistent_gnt->handle ==
383 BLKBACK_INVALID_HANDLE);
386 clear_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
390 if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
393 (test_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags)))
396 rb_erase(&persistent_gnt->node, root);
397 list_add(&persistent_gnt->remove_node,
398 &blkif->persistent_purge_list);
399 if (--num_clean == 0)
403 * If we get here it means we also need to start cleaning
404 * grants that were used since last purge in order to cope
405 * with the requested num
407 if (!scan_used && !clean_used) {
408 pr_debug(DRV_PFX "Still missing %u purged frames\n", num_clean);
414 pr_debug(DRV_PFX "Finished scanning for grants to clean, removing used flag\n");
419 blkif->persistent_gnt_c -= (total - num_clean);
420 blkif->vbd.overflow_max_grants = 0;
422 /* We can defer this work */
423 INIT_WORK(&blkif->persistent_purge_work, unmap_purged_grants);
424 schedule_work(&blkif->persistent_purge_work);
425 pr_debug(DRV_PFX "Purged %u/%u\n", (total - num_clean), total);
430 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
432 static struct pending_req *alloc_req(struct xen_blkif *blkif)
434 struct pending_req *req = NULL;
437 spin_lock_irqsave(&blkif->pending_free_lock, flags);
438 if (!list_empty(&blkif->pending_free)) {
439 req = list_entry(blkif->pending_free.next, struct pending_req,
441 list_del(&req->free_list);
443 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
448 * Return the 'pending_req' structure back to the freepool. We also
449 * wake up the thread if it was waiting for a free page.
451 static void free_req(struct xen_blkif *blkif, struct pending_req *req)
456 spin_lock_irqsave(&blkif->pending_free_lock, flags);
457 was_empty = list_empty(&blkif->pending_free);
458 list_add(&req->free_list, &blkif->pending_free);
459 spin_unlock_irqrestore(&blkif->pending_free_lock, flags);
461 wake_up(&blkif->pending_free_wq);
465 * Routines for managing virtual block devices (vbds).
467 static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
470 struct xen_vbd *vbd = &blkif->vbd;
473 if ((operation != READ) && vbd->readonly)
476 if (likely(req->nr_sects)) {
477 blkif_sector_t end = req->sector_number + req->nr_sects;
479 if (unlikely(end < req->sector_number))
481 if (unlikely(end > vbd_sz(vbd)))
485 req->dev = vbd->pdevice;
486 req->bdev = vbd->bdev;
493 static void xen_vbd_resize(struct xen_blkif *blkif)
495 struct xen_vbd *vbd = &blkif->vbd;
496 struct xenbus_transaction xbt;
498 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
499 unsigned long long new_size = vbd_sz(vbd);
501 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
502 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
503 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
504 vbd->size = new_size;
506 err = xenbus_transaction_start(&xbt);
508 pr_warn(DRV_PFX "Error starting transaction");
511 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
512 (unsigned long long)vbd_sz(vbd));
514 pr_warn(DRV_PFX "Error writing new size");
518 * Write the current state; we will use this to synchronize
519 * the front-end. If the current state is "connected" the
520 * front-end will get the new size information online.
522 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
524 pr_warn(DRV_PFX "Error writing the state");
528 err = xenbus_transaction_end(xbt, 0);
532 pr_warn(DRV_PFX "Error ending transaction");
535 xenbus_transaction_end(xbt, 1);
539 * Notification from the guest OS.
541 static void blkif_notify_work(struct xen_blkif *blkif)
543 blkif->waiting_reqs = 1;
547 irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
549 blkif_notify_work(dev_id);
554 * SCHEDULER FUNCTIONS
557 static void print_stats(struct xen_blkif *blkif)
559 pr_info("xen-blkback (%s): oo %3llu | rd %4llu | wr %4llu | f %4llu"
560 " | ds %4llu | pg: %4u/%4d\n",
561 current->comm, blkif->st_oo_req,
562 blkif->st_rd_req, blkif->st_wr_req,
563 blkif->st_f_req, blkif->st_ds_req,
564 blkif->persistent_gnt_c,
565 xen_blkif_max_pgrants);
566 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
567 blkif->st_rd_req = 0;
568 blkif->st_wr_req = 0;
569 blkif->st_oo_req = 0;
570 blkif->st_ds_req = 0;
573 int xen_blkif_schedule(void *arg)
575 struct xen_blkif *blkif = arg;
576 struct xen_vbd *vbd = &blkif->vbd;
577 unsigned long timeout;
580 xen_blkif_get(blkif);
582 while (!kthread_should_stop()) {
585 if (unlikely(vbd->size != vbd_sz(vbd)))
586 xen_vbd_resize(blkif);
588 timeout = msecs_to_jiffies(LRU_INTERVAL);
590 timeout = wait_event_interruptible_timeout(
592 blkif->waiting_reqs || kthread_should_stop(),
596 timeout = wait_event_interruptible_timeout(
597 blkif->pending_free_wq,
598 !list_empty(&blkif->pending_free) ||
599 kthread_should_stop(),
604 blkif->waiting_reqs = 0;
605 smp_mb(); /* clear flag *before* checking for work */
607 ret = do_block_io_op(blkif);
609 blkif->waiting_reqs = 1;
611 wait_event_interruptible(blkif->shutdown_wq,
612 kthread_should_stop());
615 if (blkif->vbd.feature_gnt_persistent &&
616 time_after(jiffies, blkif->next_lru)) {
617 purge_persistent_gnt(blkif);
618 blkif->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
621 /* Shrink if we have more than xen_blkif_max_buffer_pages */
622 shrink_free_pagepool(blkif, xen_blkif_max_buffer_pages);
624 if (log_stats && time_after(jiffies, blkif->st_print))
628 /* Since we are shutting down remove all pages from the buffer */
629 shrink_free_pagepool(blkif, 0 /* All */);
631 /* Free all persistent grant pages */
632 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
633 free_persistent_gnts(blkif, &blkif->persistent_gnts,
634 blkif->persistent_gnt_c);
636 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
637 blkif->persistent_gnt_c = 0;
642 blkif->xenblkd = NULL;
643 xen_blkif_put(blkif);
649 * Unmap the grant references, and also remove the M2P over-rides
650 * used in the 'pending_req'.
652 static void xen_blkbk_unmap(struct xen_blkif *blkif,
653 struct grant_page *pages[],
656 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
657 struct page *unmap_pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
658 unsigned int i, invcount = 0;
661 for (i = 0; i < num; i++) {
662 if (pages[i]->persistent_gnt != NULL) {
663 put_persistent_gnt(blkif, pages[i]->persistent_gnt);
666 if (pages[i]->handle == BLKBACK_INVALID_HANDLE)
668 unmap_pages[invcount] = pages[i]->page;
669 gnttab_set_unmap_op(&unmap[invcount], vaddr(pages[i]->page),
670 GNTMAP_host_map, pages[i]->handle);
671 pages[i]->handle = BLKBACK_INVALID_HANDLE;
672 if (++invcount == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
673 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages,
676 put_free_pages(blkif, unmap_pages, invcount);
681 ret = gnttab_unmap_refs(unmap, NULL, unmap_pages, invcount);
683 put_free_pages(blkif, unmap_pages, invcount);
687 static int xen_blkbk_map(struct xen_blkif *blkif,
688 struct grant_page *pages[],
691 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
692 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
693 struct persistent_gnt *persistent_gnt = NULL;
694 phys_addr_t addr = 0;
695 int i, seg_idx, new_map_idx;
698 int last_map = 0, map_until = 0;
699 int use_persistent_gnts;
701 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
704 * Fill out preq.nr_sects with proper amount of sectors, and setup
705 * assign map[..] with the PFN of the page in our domain with the
706 * corresponding grant reference for each page.
709 for (i = map_until; i < num; i++) {
712 if (use_persistent_gnts)
713 persistent_gnt = get_persistent_gnt(
717 if (persistent_gnt) {
719 * We are using persistent grants and
720 * the grant is already mapped
722 pages[i]->page = persistent_gnt->page;
723 pages[i]->persistent_gnt = persistent_gnt;
725 if (get_free_page(blkif, &pages[i]->page))
727 addr = vaddr(pages[i]->page);
728 pages_to_gnt[segs_to_map] = pages[i]->page;
729 pages[i]->persistent_gnt = NULL;
730 flags = GNTMAP_host_map;
731 if (!use_persistent_gnts && ro)
732 flags |= GNTMAP_readonly;
733 gnttab_set_map_op(&map[segs_to_map++], addr,
734 flags, pages[i]->gref,
738 if (segs_to_map == BLKIF_MAX_SEGMENTS_PER_REQUEST)
743 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
748 * Now swizzle the MFN in our domain with the MFN from the other domain
749 * so that when we access vaddr(pending_req,i) it has the contents of
750 * the page from the other domain.
752 for (seg_idx = last_map, new_map_idx = 0; seg_idx < map_until; seg_idx++) {
753 if (!pages[seg_idx]->persistent_gnt) {
754 /* This is a newly mapped grant */
755 BUG_ON(new_map_idx >= segs_to_map);
756 if (unlikely(map[new_map_idx].status != 0)) {
757 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
758 pages[seg_idx]->handle = BLKBACK_INVALID_HANDLE;
762 pages[seg_idx]->handle = map[new_map_idx].handle;
766 if (use_persistent_gnts &&
767 blkif->persistent_gnt_c < xen_blkif_max_pgrants) {
769 * We are using persistent grants, the grant is
770 * not mapped but we might have room for it.
772 persistent_gnt = kmalloc(sizeof(struct persistent_gnt),
774 if (!persistent_gnt) {
776 * If we don't have enough memory to
777 * allocate the persistent_gnt struct
778 * map this grant non-persistenly
782 persistent_gnt->gnt = map[new_map_idx].ref;
783 persistent_gnt->handle = map[new_map_idx].handle;
784 persistent_gnt->page = pages[seg_idx]->page;
785 if (add_persistent_gnt(blkif,
787 kfree(persistent_gnt);
788 persistent_gnt = NULL;
791 pages[seg_idx]->persistent_gnt = persistent_gnt;
792 pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
793 persistent_gnt->gnt, blkif->persistent_gnt_c,
794 xen_blkif_max_pgrants);
797 if (use_persistent_gnts && !blkif->vbd.overflow_max_grants) {
798 blkif->vbd.overflow_max_grants = 1;
799 pr_debug(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
800 blkif->domid, blkif->vbd.handle);
803 * We could not map this grant persistently, so use it as
804 * a non-persistent grant.
810 last_map = map_until;
811 if (map_until != num)
817 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
818 put_free_pages(blkif, pages_to_gnt, segs_to_map);
822 static int xen_blkbk_map_seg(struct pending_req *pending_req)
826 rc = xen_blkbk_map(pending_req->blkif, pending_req->segments,
827 pending_req->nr_pages,
828 (pending_req->operation != BLKIF_OP_READ));
833 static int xen_blkbk_parse_indirect(struct blkif_request *req,
834 struct pending_req *pending_req,
835 struct seg_buf seg[],
836 struct phys_req *preq)
838 struct grant_page **pages = pending_req->indirect_pages;
839 struct xen_blkif *blkif = pending_req->blkif;
840 int indirect_grefs, rc, n, nseg, i;
841 struct blkif_request_segment_aligned *segments = NULL;
843 nseg = pending_req->nr_pages;
844 indirect_grefs = INDIRECT_PAGES(nseg);
845 BUG_ON(indirect_grefs > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
847 for (i = 0; i < indirect_grefs; i++)
848 pages[i]->gref = req->u.indirect.indirect_grefs[i];
850 rc = xen_blkbk_map(blkif, pages, indirect_grefs, true);
854 for (n = 0, i = 0; n < nseg; n++) {
855 if ((n % SEGS_PER_INDIRECT_FRAME) == 0) {
856 /* Map indirect segments */
858 kunmap_atomic(segments);
859 segments = kmap_atomic(pages[n/SEGS_PER_INDIRECT_FRAME]->page);
861 i = n % SEGS_PER_INDIRECT_FRAME;
862 pending_req->segments[n]->gref = segments[i].gref;
863 seg[n].nsec = segments[i].last_sect -
864 segments[i].first_sect + 1;
865 seg[n].offset = (segments[i].first_sect << 9);
866 if ((segments[i].last_sect >= (PAGE_SIZE >> 9)) ||
867 (segments[i].last_sect < segments[i].first_sect)) {
871 preq->nr_sects += seg[n].nsec;
876 kunmap_atomic(segments);
877 xen_blkbk_unmap(blkif, pages, indirect_grefs);
881 static int dispatch_discard_io(struct xen_blkif *blkif,
882 struct blkif_request *req)
885 int status = BLKIF_RSP_OKAY;
886 struct block_device *bdev = blkif->vbd.bdev;
887 unsigned long secure;
888 struct phys_req preq;
890 xen_blkif_get(blkif);
892 preq.sector_number = req->u.discard.sector_number;
893 preq.nr_sects = req->u.discard.nr_sectors;
895 err = xen_vbd_translate(&preq, blkif, WRITE);
897 pr_warn(DRV_PFX "access denied: DISCARD [%llu->%llu] on dev=%04x\n",
899 preq.sector_number + preq.nr_sects, blkif->vbd.pdevice);
904 secure = (blkif->vbd.discard_secure &&
905 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
906 BLKDEV_DISCARD_SECURE : 0;
908 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
909 req->u.discard.nr_sectors,
912 if (err == -EOPNOTSUPP) {
913 pr_debug(DRV_PFX "discard op failed, not supported\n");
914 status = BLKIF_RSP_EOPNOTSUPP;
916 status = BLKIF_RSP_ERROR;
918 make_response(blkif, req->u.discard.id, req->operation, status);
919 xen_blkif_put(blkif);
923 static int dispatch_other_io(struct xen_blkif *blkif,
924 struct blkif_request *req,
925 struct pending_req *pending_req)
927 free_req(blkif, pending_req);
928 make_response(blkif, req->u.other.id, req->operation,
929 BLKIF_RSP_EOPNOTSUPP);
933 static void xen_blk_drain_io(struct xen_blkif *blkif)
935 atomic_set(&blkif->drain, 1);
937 /* The initial value is one, and one refcnt taken at the
938 * start of the xen_blkif_schedule thread. */
939 if (atomic_read(&blkif->refcnt) <= 2)
941 wait_for_completion_interruptible_timeout(
942 &blkif->drain_complete, HZ);
944 if (!atomic_read(&blkif->drain))
946 } while (!kthread_should_stop());
947 atomic_set(&blkif->drain, 0);
951 * Completion callback on the bio's. Called as bh->b_end_io()
954 static void __end_block_io_op(struct pending_req *pending_req, int error)
956 /* An error fails the entire request. */
957 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
958 (error == -EOPNOTSUPP)) {
959 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
960 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
961 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
962 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
963 (error == -EOPNOTSUPP)) {
964 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
965 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
966 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
968 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
969 " error=%d\n", error);
970 pending_req->status = BLKIF_RSP_ERROR;
974 * If all of the bio's have completed it is time to unmap
975 * the grant references associated with 'request' and provide
976 * the proper response on the ring.
978 if (atomic_dec_and_test(&pending_req->pendcnt)) {
979 xen_blkbk_unmap(pending_req->blkif,
980 pending_req->segments,
981 pending_req->nr_pages);
982 make_response(pending_req->blkif, pending_req->id,
983 pending_req->operation, pending_req->status);
984 xen_blkif_put(pending_req->blkif);
985 if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
986 if (atomic_read(&pending_req->blkif->drain))
987 complete(&pending_req->blkif->drain_complete);
989 free_req(pending_req->blkif, pending_req);
996 static void end_block_io_op(struct bio *bio, int error)
998 __end_block_io_op(bio->bi_private, error);
1005 * Function to copy the from the ring buffer the 'struct blkif_request'
1006 * (which has the sectors we want, number of them, grant references, etc),
1007 * and transmute it to the block API to hand it over to the proper block disk.
1010 __do_block_io_op(struct xen_blkif *blkif)
1012 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1013 struct blkif_request req;
1014 struct pending_req *pending_req;
1018 rc = blk_rings->common.req_cons;
1019 rp = blk_rings->common.sring->req_prod;
1020 rmb(); /* Ensure we see queued requests up to 'rp'. */
1022 if (RING_REQUEST_PROD_OVERFLOW(&blk_rings->common, rp)) {
1023 rc = blk_rings->common.rsp_prod_pvt;
1024 pr_warn(DRV_PFX "Frontend provided bogus ring requests (%d - %d = %d). Halting ring processing on dev=%04x\n",
1025 rp, rc, rp - rc, blkif->vbd.pdevice);
1030 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
1033 if (kthread_should_stop()) {
1038 pending_req = alloc_req(blkif);
1039 if (NULL == pending_req) {
1045 switch (blkif->blk_protocol) {
1046 case BLKIF_PROTOCOL_NATIVE:
1047 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
1049 case BLKIF_PROTOCOL_X86_32:
1050 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
1052 case BLKIF_PROTOCOL_X86_64:
1053 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
1058 blk_rings->common.req_cons = ++rc; /* before make_response() */
1060 /* Apply all sanity checks to /private copy/ of request. */
1063 switch (req.operation) {
1065 case BLKIF_OP_WRITE:
1066 case BLKIF_OP_WRITE_BARRIER:
1067 case BLKIF_OP_FLUSH_DISKCACHE:
1068 case BLKIF_OP_INDIRECT:
1069 if (dispatch_rw_block_io(blkif, &req, pending_req))
1072 case BLKIF_OP_DISCARD:
1073 free_req(blkif, pending_req);
1074 if (dispatch_discard_io(blkif, &req))
1078 if (dispatch_other_io(blkif, &req, pending_req))
1083 /* Yield point for this unbounded loop. */
1091 do_block_io_op(struct xen_blkif *blkif)
1093 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1097 more_to_do = __do_block_io_op(blkif);
1101 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
1102 } while (more_to_do);
1107 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
1108 * and call the 'submit_bio' to pass it to the underlying storage.
1110 static int dispatch_rw_block_io(struct xen_blkif *blkif,
1111 struct blkif_request *req,
1112 struct pending_req *pending_req)
1114 struct phys_req preq;
1115 struct seg_buf *seg = pending_req->seg;
1117 struct bio *bio = NULL;
1118 struct bio **biolist = pending_req->biolist;
1121 struct blk_plug plug;
1123 struct grant_page **pages = pending_req->segments;
1124 unsigned short req_operation;
1126 req_operation = req->operation == BLKIF_OP_INDIRECT ?
1127 req->u.indirect.indirect_op : req->operation;
1128 if ((req->operation == BLKIF_OP_INDIRECT) &&
1129 (req_operation != BLKIF_OP_READ) &&
1130 (req_operation != BLKIF_OP_WRITE)) {
1131 pr_debug(DRV_PFX "Invalid indirect operation (%u)\n",
1136 switch (req_operation) {
1141 case BLKIF_OP_WRITE:
1143 operation = WRITE_ODIRECT;
1145 case BLKIF_OP_WRITE_BARRIER:
1147 case BLKIF_OP_FLUSH_DISKCACHE:
1149 operation = WRITE_FLUSH;
1152 operation = 0; /* make gcc happy */
1157 /* Check that the number of segments is sane. */
1158 nseg = req->operation == BLKIF_OP_INDIRECT ?
1159 req->u.indirect.nr_segments : req->u.rw.nr_segments;
1161 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
1162 unlikely((req->operation != BLKIF_OP_INDIRECT) &&
1163 (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) ||
1164 unlikely((req->operation == BLKIF_OP_INDIRECT) &&
1165 (nseg > MAX_INDIRECT_SEGMENTS))) {
1166 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
1168 /* Haven't submitted any bio's yet. */
1174 pending_req->blkif = blkif;
1175 pending_req->id = req->u.rw.id;
1176 pending_req->operation = req_operation;
1177 pending_req->status = BLKIF_RSP_OKAY;
1178 pending_req->nr_pages = nseg;
1180 if (req->operation != BLKIF_OP_INDIRECT) {
1181 preq.dev = req->u.rw.handle;
1182 preq.sector_number = req->u.rw.sector_number;
1183 for (i = 0; i < nseg; i++) {
1184 pages[i]->gref = req->u.rw.seg[i].gref;
1185 seg[i].nsec = req->u.rw.seg[i].last_sect -
1186 req->u.rw.seg[i].first_sect + 1;
1187 seg[i].offset = (req->u.rw.seg[i].first_sect << 9);
1188 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
1189 (req->u.rw.seg[i].last_sect <
1190 req->u.rw.seg[i].first_sect))
1192 preq.nr_sects += seg[i].nsec;
1195 preq.dev = req->u.indirect.handle;
1196 preq.sector_number = req->u.indirect.sector_number;
1197 if (xen_blkbk_parse_indirect(req, pending_req, seg, &preq))
1201 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
1202 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
1203 operation == READ ? "read" : "write",
1205 preq.sector_number + preq.nr_sects,
1206 blkif->vbd.pdevice);
1211 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
1214 for (i = 0; i < nseg; i++) {
1215 if (((int)preq.sector_number|(int)seg[i].nsec) &
1216 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
1217 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
1223 /* Wait on all outstanding I/O's and once that has been completed
1224 * issue the WRITE_FLUSH.
1227 xen_blk_drain_io(pending_req->blkif);
1230 * If we have failed at this point, we need to undo the M2P override,
1231 * set gnttab_set_unmap_op on all of the grant references and perform
1232 * the hypercall to unmap the grants - that is all done in
1235 if (xen_blkbk_map_seg(pending_req))
1239 * This corresponding xen_blkif_put is done in __end_block_io_op, or
1240 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
1242 xen_blkif_get(blkif);
1244 for (i = 0; i < nseg; i++) {
1245 while ((bio == NULL) ||
1249 seg[i].offset) == 0)) {
1251 int nr_iovecs = min_t(int, (nseg-i), BIO_MAX_PAGES);
1252 bio = bio_alloc(GFP_KERNEL, nr_iovecs);
1253 if (unlikely(bio == NULL))
1256 biolist[nbio++] = bio;
1257 bio->bi_bdev = preq.bdev;
1258 bio->bi_private = pending_req;
1259 bio->bi_end_io = end_block_io_op;
1260 bio->bi_iter.bi_sector = preq.sector_number;
1263 preq.sector_number += seg[i].nsec;
1266 /* This will be hit if the operation was a flush or discard. */
1268 BUG_ON(operation != WRITE_FLUSH);
1270 bio = bio_alloc(GFP_KERNEL, 0);
1271 if (unlikely(bio == NULL))
1274 biolist[nbio++] = bio;
1275 bio->bi_bdev = preq.bdev;
1276 bio->bi_private = pending_req;
1277 bio->bi_end_io = end_block_io_op;
1280 atomic_set(&pending_req->pendcnt, nbio);
1281 blk_start_plug(&plug);
1283 for (i = 0; i < nbio; i++)
1284 submit_bio(operation, biolist[i]);
1286 /* Let the I/Os go.. */
1287 blk_finish_plug(&plug);
1289 if (operation == READ)
1290 blkif->st_rd_sect += preq.nr_sects;
1291 else if (operation & WRITE)
1292 blkif->st_wr_sect += preq.nr_sects;
1297 xen_blkbk_unmap(blkif, pending_req->segments,
1298 pending_req->nr_pages);
1300 /* Haven't submitted any bio's yet. */
1301 make_response(blkif, req->u.rw.id, req_operation, BLKIF_RSP_ERROR);
1302 free_req(blkif, pending_req);
1303 msleep(1); /* back off a bit */
1307 for (i = 0; i < nbio; i++)
1308 bio_put(biolist[i]);
1309 atomic_set(&pending_req->pendcnt, 1);
1310 __end_block_io_op(pending_req, -EINVAL);
1311 msleep(1); /* back off a bit */
1318 * Put a response on the ring on how the operation fared.
1320 static void make_response(struct xen_blkif *blkif, u64 id,
1321 unsigned short op, int st)
1323 struct blkif_response resp;
1324 unsigned long flags;
1325 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1329 resp.operation = op;
1332 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1333 /* Place on the response ring for the relevant domain. */
1334 switch (blkif->blk_protocol) {
1335 case BLKIF_PROTOCOL_NATIVE:
1336 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1337 &resp, sizeof(resp));
1339 case BLKIF_PROTOCOL_X86_32:
1340 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1341 &resp, sizeof(resp));
1343 case BLKIF_PROTOCOL_X86_64:
1344 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1345 &resp, sizeof(resp));
1350 blk_rings->common.rsp_prod_pvt++;
1351 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
1352 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
1354 notify_remote_via_irq(blkif->irq);
1357 static int __init xen_blkif_init(void)
1364 rc = xen_blkif_interface_init();
1368 rc = xen_blkif_xenbus_init();
1376 module_init(xen_blkif_init);
1378 MODULE_LICENSE("Dual BSD/GPL");
1379 MODULE_ALIAS("xen-backend:vbd");