xen-blkback: move pending handles list from blkbk to pending_req
[deliverable/linux.git] / drivers / block / xen-blkback / blkback.c
CommitLineData
4d05a28d 1/******************************************************************************
4d05a28d
KRW
2 *
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:
a1397fa3 7 * drivers/block/xen-blkfront.c
4d05a28d
KRW
8 *
9 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
10 * Copyright (c) 2005, Christopher Clark
11 *
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:
17 *
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:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
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
34 * IN THE SOFTWARE.
35 */
36
37#include <linux/spinlock.h>
38#include <linux/kthread.h>
39#include <linux/list.h>
40#include <linux/delay.h>
88122933 41#include <linux/freezer.h>
0a8704a5 42#include <linux/bitmap.h>
afd91d07 43
88122933
JF
44#include <xen/events.h>
45#include <xen/page.h>
e79affc3 46#include <xen/xen.h>
88122933
JF
47#include <asm/xen/hypervisor.h>
48#include <asm/xen/hypercall.h>
087ffecd 49#include <xen/balloon.h>
4d05a28d
KRW
50#include "common.h"
51
52/*
53 * These are rather arbitrary. They are fairly large because adjacent requests
54 * pulled from a communication ring are quite likely to end up being part of
55 * the same scatter/gather request at the disc.
56 *
8b6bf747 57 * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
4d05a28d
KRW
58 *
59 * This will increase the chances of being able to write whole tracks.
60 * 64 should be enough to keep us competitive with Linux.
61 */
8b6bf747
KRW
62static int xen_blkif_reqs = 64;
63module_param_named(reqs, xen_blkif_reqs, int, 0);
4d05a28d
KRW
64MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
65
c6cc142d
RPM
66/*
67 * Maximum number of unused free pages to keep in the internal buffer.
68 * Setting this to a value too low will reduce memory used in each backend,
69 * but can have a performance penalty.
70 *
71 * A sane value is xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST, but can
72 * be set to a lower value that might degrade performance on some intensive
73 * IO workloads.
74 */
75
76static int xen_blkif_max_buffer_pages = 704;
77module_param_named(max_buffer_pages, xen_blkif_max_buffer_pages, int, 0644);
78MODULE_PARM_DESC(max_buffer_pages,
79"Maximum number of free pages to keep in each block backend buffer");
80
3f3aad5e
RPM
81/*
82 * Maximum number of grants to map persistently in blkback. For maximum
83 * performance this should be the total numbers of grants that can be used
84 * to fill the ring, but since this might become too high, specially with
85 * the use of indirect descriptors, we set it to a value that provides good
86 * performance without using too much memory.
87 *
88 * When the list of persistent grants is full we clean it up using a LRU
89 * algorithm.
90 */
91
92static int xen_blkif_max_pgrants = 352;
93module_param_named(max_persistent_grants, xen_blkif_max_pgrants, int, 0644);
94MODULE_PARM_DESC(max_persistent_grants,
95 "Maximum number of grants to map persistently");
96
97/*
98 * The LRU mechanism to clean the lists of persistent grants needs to
99 * be executed periodically. The time interval between consecutive executions
100 * of the purge mechanism is set in ms.
101 */
102#define LRU_INTERVAL 100
103
104/*
105 * When the persistent grants list is full we will remove unused grants
106 * from the list. The percent number of grants to be removed at each LRU
107 * execution.
108 */
109#define LRU_PERCENT_CLEAN 5
110
4d05a28d 111/* Run-time switchable: /sys/module/blkback/parameters/ */
2e9977c2 112static unsigned int log_stats;
4d05a28d 113module_param(log_stats, int, 0644);
4d05a28d
KRW
114
115/*
116 * Each outstanding request that we've passed to the lower device layers has a
117 * 'pending_req' allocated to it. Each buffer_head that completes decrements
118 * the pendcnt towards zero. When it hits zero, the specified domain has a
119 * response queued for it, with the saved 'id' passed back.
120 */
2e9977c2 121struct pending_req {
30fd1502 122 struct xen_blkif *blkif;
01f37f2d
KRW
123 u64 id;
124 int nr_pages;
125 atomic_t pendcnt;
126 unsigned short operation;
127 int status;
128 struct list_head free_list;
c6cc142d 129 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
3f3aad5e 130 struct persistent_gnt *persistent_gnts[BLKIF_MAX_SEGMENTS_PER_REQUEST];
bb6acb28 131 grant_handle_t grant_handles[BLKIF_MAX_SEGMENTS_PER_REQUEST];
2e9977c2 132};
4d05a28d 133
4d05a28d
KRW
134#define BLKBACK_INVALID_HANDLE (~0)
135
c6cc142d
RPM
136/* Number of free pages to remove on each call to free_xenballooned_pages */
137#define NUM_BATCH_FREE_PAGES 10
138
e8e28871 139struct xen_blkbk {
2e9977c2 140 struct pending_req *pending_reqs;
a1397fa3 141 /* List of all 'pending_req' available */
e8e28871 142 struct list_head pending_free;
a1397fa3 143 /* And its spinlock. */
e8e28871
KRW
144 spinlock_t pending_free_lock;
145 wait_queue_head_t pending_free_wq;
e8e28871
KRW
146};
147
148static struct xen_blkbk *blkbk;
4d05a28d 149
a1397fa3
KRW
150/*
151 * Little helpful macro to figure out the index and virtual address of the
152 * pending_pages[..]. For each 'pending_req' we have have up to
153 * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
01f37f2d
KRW
154 * 10 and would index in the pending_pages[..].
155 */
2e9977c2 156static inline int vaddr_pagenr(struct pending_req *req, int seg)
4d05a28d 157{
2e9977c2
KRW
158 return (req - blkbk->pending_reqs) *
159 BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
4d05a28d
KRW
160}
161
c6cc142d
RPM
162static inline int get_free_page(struct xen_blkif *blkif, struct page **page)
163{
164 unsigned long flags;
165
166 spin_lock_irqsave(&blkif->free_pages_lock, flags);
167 if (list_empty(&blkif->free_pages)) {
168 BUG_ON(blkif->free_pages_num != 0);
169 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
170 return alloc_xenballooned_pages(1, page, false);
171 }
172 BUG_ON(blkif->free_pages_num == 0);
173 page[0] = list_first_entry(&blkif->free_pages, struct page, lru);
174 list_del(&page[0]->lru);
175 blkif->free_pages_num--;
176 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
efe08a3e 177
c6cc142d
RPM
178 return 0;
179}
180
181static inline void put_free_pages(struct xen_blkif *blkif, struct page **page,
182 int num)
4d05a28d 183{
c6cc142d
RPM
184 unsigned long flags;
185 int i;
186
187 spin_lock_irqsave(&blkif->free_pages_lock, flags);
188 for (i = 0; i < num; i++)
189 list_add(&page[i]->lru, &blkif->free_pages);
190 blkif->free_pages_num += num;
191 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
192}
193
194static inline void shrink_free_pagepool(struct xen_blkif *blkif, int num)
195{
196 /* Remove requested pages in batches of NUM_BATCH_FREE_PAGES */
197 struct page *page[NUM_BATCH_FREE_PAGES];
198 unsigned int num_pages = 0;
199 unsigned long flags;
200
201 spin_lock_irqsave(&blkif->free_pages_lock, flags);
202 while (blkif->free_pages_num > num) {
203 BUG_ON(list_empty(&blkif->free_pages));
204 page[num_pages] = list_first_entry(&blkif->free_pages,
205 struct page, lru);
206 list_del(&page[num_pages]->lru);
207 blkif->free_pages_num--;
208 if (++num_pages == NUM_BATCH_FREE_PAGES) {
209 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
210 free_xenballooned_pages(num_pages, page);
211 spin_lock_irqsave(&blkif->free_pages_lock, flags);
212 num_pages = 0;
213 }
214 }
215 spin_unlock_irqrestore(&blkif->free_pages_lock, flags);
216 if (num_pages != 0)
217 free_xenballooned_pages(num_pages, page);
4d05a28d
KRW
218}
219
c6cc142d
RPM
220#define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
221
4d05a28d 222#define pending_handle(_req, _seg) \
bb6acb28 223 (_req->grant_handles[_seg])
4d05a28d
KRW
224
225
30fd1502
KRW
226static int do_block_io_op(struct xen_blkif *blkif);
227static int dispatch_rw_block_io(struct xen_blkif *blkif,
fc53bf75
KRW
228 struct blkif_request *req,
229 struct pending_req *pending_req);
30fd1502 230static void make_response(struct xen_blkif *blkif, u64 id,
4d05a28d
KRW
231 unsigned short op, int st);
232
7dc34117
RPM
233#define foreach_grant_safe(pos, n, rbtree, node) \
234 for ((pos) = container_of(rb_first((rbtree)), typeof(*(pos)), node), \
217fd5e7 235 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL; \
0a8704a5 236 &(pos)->node != NULL; \
7dc34117
RPM
237 (pos) = container_of(n, typeof(*(pos)), node), \
238 (n) = (&(pos)->node != NULL) ? rb_next(&(pos)->node) : NULL)
0a8704a5
RPM
239
240
3f3aad5e
RPM
241/*
242 * We don't need locking around the persistent grant helpers
243 * because blkback uses a single-thread for each backed, so we
244 * can be sure that this functions will never be called recursively.
245 *
246 * The only exception to that is put_persistent_grant, that can be called
247 * from interrupt context (by xen_blkbk_unmap), so we have to use atomic
248 * bit operations to modify the flags of a persistent grant and to count
249 * the number of used grants.
250 */
251static int add_persistent_gnt(struct xen_blkif *blkif,
0a8704a5
RPM
252 struct persistent_gnt *persistent_gnt)
253{
3f3aad5e 254 struct rb_node **new = NULL, *parent = NULL;
0a8704a5
RPM
255 struct persistent_gnt *this;
256
3f3aad5e
RPM
257 if (blkif->persistent_gnt_c >= xen_blkif_max_pgrants) {
258 if (!blkif->vbd.overflow_max_grants)
259 blkif->vbd.overflow_max_grants = 1;
260 return -EBUSY;
261 }
0a8704a5 262 /* Figure out where to put new node */
3f3aad5e 263 new = &blkif->persistent_gnts.rb_node;
0a8704a5
RPM
264 while (*new) {
265 this = container_of(*new, struct persistent_gnt, node);
266
267 parent = *new;
268 if (persistent_gnt->gnt < this->gnt)
269 new = &((*new)->rb_left);
270 else if (persistent_gnt->gnt > this->gnt)
271 new = &((*new)->rb_right);
272 else {
c6cc142d
RPM
273 pr_alert_ratelimited(DRV_PFX " trying to add a gref that's already in the tree\n");
274 return -EINVAL;
0a8704a5
RPM
275 }
276 }
277
3f3aad5e
RPM
278 bitmap_zero(persistent_gnt->flags, PERSISTENT_GNT_FLAGS_SIZE);
279 set_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
0a8704a5
RPM
280 /* Add new node and rebalance tree. */
281 rb_link_node(&(persistent_gnt->node), parent, new);
3f3aad5e
RPM
282 rb_insert_color(&(persistent_gnt->node), &blkif->persistent_gnts);
283 blkif->persistent_gnt_c++;
284 atomic_inc(&blkif->persistent_gnt_in_use);
c6cc142d 285 return 0;
0a8704a5
RPM
286}
287
3f3aad5e 288static struct persistent_gnt *get_persistent_gnt(struct xen_blkif *blkif,
0a8704a5
RPM
289 grant_ref_t gref)
290{
291 struct persistent_gnt *data;
3f3aad5e 292 struct rb_node *node = NULL;
0a8704a5 293
3f3aad5e 294 node = blkif->persistent_gnts.rb_node;
0a8704a5
RPM
295 while (node) {
296 data = container_of(node, struct persistent_gnt, node);
297
298 if (gref < data->gnt)
299 node = node->rb_left;
300 else if (gref > data->gnt)
301 node = node->rb_right;
3f3aad5e
RPM
302 else {
303 if(test_bit(PERSISTENT_GNT_ACTIVE, data->flags)) {
304 pr_alert_ratelimited(DRV_PFX " requesting a grant already in use\n");
305 return NULL;
306 }
307 set_bit(PERSISTENT_GNT_ACTIVE, data->flags);
308 atomic_inc(&blkif->persistent_gnt_in_use);
0a8704a5 309 return data;
3f3aad5e 310 }
0a8704a5
RPM
311 }
312 return NULL;
313}
314
3f3aad5e
RPM
315static void put_persistent_gnt(struct xen_blkif *blkif,
316 struct persistent_gnt *persistent_gnt)
317{
318 if(!test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
319 pr_alert_ratelimited(DRV_PFX " freeing a grant already unused");
320 set_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
321 clear_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags);
322 atomic_dec(&blkif->persistent_gnt_in_use);
323}
324
c6cc142d
RPM
325static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
326 unsigned int num)
4d4f270f
RPM
327{
328 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
329 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
330 struct persistent_gnt *persistent_gnt;
7dc34117 331 struct rb_node *n;
4d4f270f
RPM
332 int ret = 0;
333 int segs_to_unmap = 0;
334
7dc34117 335 foreach_grant_safe(persistent_gnt, n, root, node) {
4d4f270f
RPM
336 BUG_ON(persistent_gnt->handle ==
337 BLKBACK_INVALID_HANDLE);
338 gnttab_set_unmap_op(&unmap[segs_to_unmap],
339 (unsigned long) pfn_to_kaddr(page_to_pfn(
340 persistent_gnt->page)),
341 GNTMAP_host_map,
342 persistent_gnt->handle);
343
344 pages[segs_to_unmap] = persistent_gnt->page;
4d4f270f
RPM
345
346 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST ||
347 !rb_next(&persistent_gnt->node)) {
348 ret = gnttab_unmap_refs(unmap, NULL, pages,
349 segs_to_unmap);
350 BUG_ON(ret);
c6cc142d 351 put_free_pages(blkif, pages, segs_to_unmap);
4d4f270f
RPM
352 segs_to_unmap = 0;
353 }
7dc34117
RPM
354
355 rb_erase(&persistent_gnt->node, root);
356 kfree(persistent_gnt);
357 num--;
4d4f270f
RPM
358 }
359 BUG_ON(num != 0);
360}
361
3f3aad5e
RPM
362static void unmap_purged_grants(struct work_struct *work)
363{
364 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
365 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
366 struct persistent_gnt *persistent_gnt;
367 int ret, segs_to_unmap = 0;
368 struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
369
370 while(!list_empty(&blkif->persistent_purge_list)) {
371 persistent_gnt = list_first_entry(&blkif->persistent_purge_list,
372 struct persistent_gnt,
373 remove_node);
374 list_del(&persistent_gnt->remove_node);
375
376 gnttab_set_unmap_op(&unmap[segs_to_unmap],
377 vaddr(persistent_gnt->page),
378 GNTMAP_host_map,
379 persistent_gnt->handle);
380
381 pages[segs_to_unmap] = persistent_gnt->page;
382
383 if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
384 ret = gnttab_unmap_refs(unmap, NULL, pages,
385 segs_to_unmap);
386 BUG_ON(ret);
387 put_free_pages(blkif, pages, segs_to_unmap);
388 segs_to_unmap = 0;
389 }
390 kfree(persistent_gnt);
391 }
392 if (segs_to_unmap > 0) {
393 ret = gnttab_unmap_refs(unmap, NULL, pages, segs_to_unmap);
394 BUG_ON(ret);
395 put_free_pages(blkif, pages, segs_to_unmap);
396 }
397}
398
399static void purge_persistent_gnt(struct xen_blkif *blkif)
400{
401 struct persistent_gnt *persistent_gnt;
402 struct rb_node *n;
403 unsigned int num_clean, total;
404 bool scan_used = false;
405 struct rb_root *root;
406
407 if (blkif->persistent_gnt_c < xen_blkif_max_pgrants ||
408 (blkif->persistent_gnt_c == xen_blkif_max_pgrants &&
409 !blkif->vbd.overflow_max_grants)) {
410 return;
411 }
412
413 if (work_pending(&blkif->persistent_purge_work)) {
414 pr_alert_ratelimited(DRV_PFX "Scheduled work from previous purge is still pending, cannot purge list\n");
415 return;
416 }
417
418 num_clean = (xen_blkif_max_pgrants / 100) * LRU_PERCENT_CLEAN;
419 num_clean = blkif->persistent_gnt_c - xen_blkif_max_pgrants + num_clean;
420 num_clean = min(blkif->persistent_gnt_c, num_clean);
421 if (num_clean >
422 (blkif->persistent_gnt_c -
423 atomic_read(&blkif->persistent_gnt_in_use)))
424 return;
425
426 /*
427 * At this point, we can assure that there will be no calls
428 * to get_persistent_grant (because we are executing this code from
429 * xen_blkif_schedule), there can only be calls to put_persistent_gnt,
430 * which means that the number of currently used grants will go down,
431 * but never up, so we will always be able to remove the requested
432 * number of grants.
433 */
434
435 total = num_clean;
436
437 pr_debug(DRV_PFX "Going to purge %u persistent grants\n", num_clean);
438
439 INIT_LIST_HEAD(&blkif->persistent_purge_list);
440 root = &blkif->persistent_gnts;
441purge_list:
442 foreach_grant_safe(persistent_gnt, n, root, node) {
443 BUG_ON(persistent_gnt->handle ==
444 BLKBACK_INVALID_HANDLE);
445
446 if (test_bit(PERSISTENT_GNT_ACTIVE, persistent_gnt->flags))
447 continue;
448 if (!scan_used &&
449 (test_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags)))
450 continue;
451
452 rb_erase(&persistent_gnt->node, root);
453 list_add(&persistent_gnt->remove_node,
454 &blkif->persistent_purge_list);
455 if (--num_clean == 0)
456 goto finished;
457 }
458 /*
459 * If we get here it means we also need to start cleaning
460 * grants that were used since last purge in order to cope
461 * with the requested num
462 */
463 if (!scan_used) {
464 pr_debug(DRV_PFX "Still missing %u purged frames\n", num_clean);
465 scan_used = true;
466 goto purge_list;
467 }
468finished:
469 /* Remove the "used" flag from all the persistent grants */
470 foreach_grant_safe(persistent_gnt, n, root, node) {
471 BUG_ON(persistent_gnt->handle ==
472 BLKBACK_INVALID_HANDLE);
473 clear_bit(PERSISTENT_GNT_WAS_ACTIVE, persistent_gnt->flags);
474 }
475 blkif->persistent_gnt_c -= (total - num_clean);
476 blkif->vbd.overflow_max_grants = 0;
477
478 /* We can defer this work */
479 INIT_WORK(&blkif->persistent_purge_work, unmap_purged_grants);
480 schedule_work(&blkif->persistent_purge_work);
481 pr_debug(DRV_PFX "Purged %u/%u\n", (total - num_clean), total);
482 return;
483}
484
a1397fa3
KRW
485/*
486 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
4d05a28d 487 */
2e9977c2 488static struct pending_req *alloc_req(void)
4d05a28d 489{
2e9977c2 490 struct pending_req *req = NULL;
4d05a28d
KRW
491 unsigned long flags;
492
e8e28871
KRW
493 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
494 if (!list_empty(&blkbk->pending_free)) {
2e9977c2
KRW
495 req = list_entry(blkbk->pending_free.next, struct pending_req,
496 free_list);
4d05a28d
KRW
497 list_del(&req->free_list);
498 }
e8e28871 499 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
4d05a28d
KRW
500 return req;
501}
502
a1397fa3
KRW
503/*
504 * Return the 'pending_req' structure back to the freepool. We also
505 * wake up the thread if it was waiting for a free page.
506 */
2e9977c2 507static void free_req(struct pending_req *req)
4d05a28d
KRW
508{
509 unsigned long flags;
510 int was_empty;
511
e8e28871
KRW
512 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
513 was_empty = list_empty(&blkbk->pending_free);
514 list_add(&req->free_list, &blkbk->pending_free);
515 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
4d05a28d 516 if (was_empty)
e8e28871 517 wake_up(&blkbk->pending_free_wq);
4d05a28d
KRW
518}
519
ee9ff853
KRW
520/*
521 * Routines for managing virtual block devices (vbds).
522 */
3d814731
KRW
523static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
524 int operation)
ee9ff853 525{
3d814731 526 struct xen_vbd *vbd = &blkif->vbd;
ee9ff853
KRW
527 int rc = -EACCES;
528
529 if ((operation != READ) && vbd->readonly)
530 goto out;
531
8ab52150
JB
532 if (likely(req->nr_sects)) {
533 blkif_sector_t end = req->sector_number + req->nr_sects;
534
535 if (unlikely(end < req->sector_number))
536 goto out;
537 if (unlikely(end > vbd_sz(vbd)))
538 goto out;
539 }
ee9ff853
KRW
540
541 req->dev = vbd->pdevice;
542 req->bdev = vbd->bdev;
543 rc = 0;
544
545 out:
546 return rc;
547}
548
3d814731 549static void xen_vbd_resize(struct xen_blkif *blkif)
ee9ff853 550{
3d814731 551 struct xen_vbd *vbd = &blkif->vbd;
ee9ff853
KRW
552 struct xenbus_transaction xbt;
553 int err;
8b6bf747 554 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
42c7841d 555 unsigned long long new_size = vbd_sz(vbd);
ee9ff853 556
22b20f2d 557 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
ee9ff853 558 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
22b20f2d 559 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
ee9ff853
KRW
560 vbd->size = new_size;
561again:
562 err = xenbus_transaction_start(&xbt);
563 if (err) {
22b20f2d 564 pr_warn(DRV_PFX "Error starting transaction");
ee9ff853
KRW
565 return;
566 }
567 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
42c7841d 568 (unsigned long long)vbd_sz(vbd));
ee9ff853 569 if (err) {
22b20f2d 570 pr_warn(DRV_PFX "Error writing new size");
ee9ff853
KRW
571 goto abort;
572 }
573 /*
574 * Write the current state; we will use this to synchronize
575 * the front-end. If the current state is "connected" the
576 * front-end will get the new size information online.
577 */
578 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
579 if (err) {
22b20f2d 580 pr_warn(DRV_PFX "Error writing the state");
ee9ff853
KRW
581 goto abort;
582 }
583
584 err = xenbus_transaction_end(xbt, 0);
585 if (err == -EAGAIN)
586 goto again;
587 if (err)
22b20f2d 588 pr_warn(DRV_PFX "Error ending transaction");
496b318e 589 return;
ee9ff853
KRW
590abort:
591 xenbus_transaction_end(xbt, 1);
592}
593
a1397fa3 594/*
b0aef179
KRW
595 * Notification from the guest OS.
596 */
30fd1502 597static void blkif_notify_work(struct xen_blkif *blkif)
4d05a28d 598{
b0aef179
KRW
599 blkif->waiting_reqs = 1;
600 wake_up(&blkif->wq);
601}
4d05a28d 602
8b6bf747 603irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
b0aef179
KRW
604{
605 blkif_notify_work(dev_id);
606 return IRQ_HANDLED;
4d05a28d
KRW
607}
608
2e9977c2 609/*
4d05a28d
KRW
610 * SCHEDULER FUNCTIONS
611 */
612
30fd1502 613static void print_stats(struct xen_blkif *blkif)
4d05a28d 614{
986cacbd 615 pr_info("xen-blkback (%s): oo %3llu | rd %4llu | wr %4llu | f %4llu"
3f3aad5e 616 " | ds %4llu | pg: %4u/%4d\n",
ebe81906 617 current->comm, blkif->st_oo_req,
b3cb0d6a 618 blkif->st_rd_req, blkif->st_wr_req,
c1a15d08
RPM
619 blkif->st_f_req, blkif->st_ds_req,
620 blkif->persistent_gnt_c,
3f3aad5e 621 xen_blkif_max_pgrants);
4d05a28d
KRW
622 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
623 blkif->st_rd_req = 0;
624 blkif->st_wr_req = 0;
625 blkif->st_oo_req = 0;
b3cb0d6a 626 blkif->st_ds_req = 0;
4d05a28d
KRW
627}
628
8b6bf747 629int xen_blkif_schedule(void *arg)
4d05a28d 630{
30fd1502 631 struct xen_blkif *blkif = arg;
3d814731 632 struct xen_vbd *vbd = &blkif->vbd;
3f3aad5e 633 unsigned long timeout;
4d05a28d 634
8b6bf747 635 xen_blkif_get(blkif);
4d05a28d 636
4d05a28d
KRW
637 while (!kthread_should_stop()) {
638 if (try_to_freeze())
639 continue;
42c7841d 640 if (unlikely(vbd->size != vbd_sz(vbd)))
3d814731 641 xen_vbd_resize(blkif);
4d05a28d 642
3f3aad5e
RPM
643 timeout = msecs_to_jiffies(LRU_INTERVAL);
644
645 timeout = wait_event_interruptible_timeout(
4d05a28d 646 blkif->wq,
3f3aad5e
RPM
647 blkif->waiting_reqs || kthread_should_stop(),
648 timeout);
649 if (timeout == 0)
650 goto purge_gnt_list;
651 timeout = wait_event_interruptible_timeout(
e8e28871 652 blkbk->pending_free_wq,
2e9977c2 653 !list_empty(&blkbk->pending_free) ||
3f3aad5e
RPM
654 kthread_should_stop(),
655 timeout);
656 if (timeout == 0)
657 goto purge_gnt_list;
4d05a28d
KRW
658
659 blkif->waiting_reqs = 0;
660 smp_mb(); /* clear flag *before* checking for work */
661
662 if (do_block_io_op(blkif))
663 blkif->waiting_reqs = 1;
4d05a28d 664
3f3aad5e
RPM
665purge_gnt_list:
666 if (blkif->vbd.feature_gnt_persistent &&
667 time_after(jiffies, blkif->next_lru)) {
668 purge_persistent_gnt(blkif);
669 blkif->next_lru = jiffies + msecs_to_jiffies(LRU_INTERVAL);
670 }
671
c6cc142d
RPM
672 /* Shrink if we have more than xen_blkif_max_buffer_pages */
673 shrink_free_pagepool(blkif, xen_blkif_max_buffer_pages);
674
4d05a28d
KRW
675 if (log_stats && time_after(jiffies, blkif->st_print))
676 print_stats(blkif);
677 }
678
c6cc142d
RPM
679 /* Since we are shutting down remove all pages from the buffer */
680 shrink_free_pagepool(blkif, 0 /* All */);
681
0a8704a5 682 /* Free all persistent grant pages */
4d4f270f 683 if (!RB_EMPTY_ROOT(&blkif->persistent_gnts))
c6cc142d 684 free_persistent_gnts(blkif, &blkif->persistent_gnts,
4d4f270f 685 blkif->persistent_gnt_c);
0a8704a5 686
0a8704a5 687 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
4d4f270f 688 blkif->persistent_gnt_c = 0;
0a8704a5 689
4d05a28d
KRW
690 if (log_stats)
691 print_stats(blkif);
4d05a28d
KRW
692
693 blkif->xenblkd = NULL;
8b6bf747 694 xen_blkif_put(blkif);
4d05a28d
KRW
695
696 return 0;
697}
698
1a95fe6e 699struct seg_buf {
ffb1dabd 700 unsigned int offset;
1a95fe6e
KRW
701 unsigned int nsec;
702};
b0aef179
KRW
703/*
704 * Unmap the grant references, and also remove the M2P over-rides
705 * used in the 'pending_req'.
01f37f2d 706 */
9f3aedf5 707static void xen_blkbk_unmap(struct pending_req *req)
b0aef179
KRW
708{
709 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
4f14faaa 710 struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
b0aef179
KRW
711 unsigned int i, invcount = 0;
712 grant_handle_t handle;
c6cc142d 713 struct xen_blkif *blkif = req->blkif;
b0aef179
KRW
714 int ret;
715
716 for (i = 0; i < req->nr_pages; i++) {
3f3aad5e
RPM
717 if (req->persistent_gnts[i] != NULL) {
718 put_persistent_gnt(blkif, req->persistent_gnts[i]);
0a8704a5 719 continue;
3f3aad5e 720 }
b0aef179 721 handle = pending_handle(req, i);
c6cc142d 722 pages[invcount] = req->pages[i];
b0aef179
KRW
723 if (handle == BLKBACK_INVALID_HANDLE)
724 continue;
c6cc142d 725 gnttab_set_unmap_op(&unmap[invcount], vaddr(pages[invcount]),
b0aef179
KRW
726 GNTMAP_host_map, handle);
727 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
728 invcount++;
729 }
730
2fc136ee 731 ret = gnttab_unmap_refs(unmap, NULL, pages, invcount);
b0aef179 732 BUG_ON(ret);
c6cc142d 733 put_free_pages(blkif, pages, invcount);
b0aef179 734}
01f37f2d
KRW
735
736static int xen_blkbk_map(struct blkif_request *req,
737 struct pending_req *pending_req,
0a8704a5
RPM
738 struct seg_buf seg[],
739 struct page *pages[])
1a95fe6e
KRW
740{
741 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
0a8704a5 742 struct page *pages_to_gnt[BLKIF_MAX_SEGMENTS_PER_REQUEST];
3f3aad5e 743 struct persistent_gnt **persistent_gnts = pending_req->persistent_gnts;
0a8704a5
RPM
744 struct persistent_gnt *persistent_gnt = NULL;
745 struct xen_blkif *blkif = pending_req->blkif;
746 phys_addr_t addr = 0;
c6cc142d 747 int i, seg_idx, new_map_idx;
97e36834 748 int nseg = req->u.rw.nr_segments;
0a8704a5 749 int segs_to_map = 0;
1a95fe6e 750 int ret = 0;
0a8704a5
RPM
751 int use_persistent_gnts;
752
753 use_persistent_gnts = (blkif->vbd.feature_gnt_persistent);
754
01f37f2d
KRW
755 /*
756 * Fill out preq.nr_sects with proper amount of sectors, and setup
1a95fe6e
KRW
757 * assign map[..] with the PFN of the page in our domain with the
758 * corresponding grant reference for each page.
759 */
760 for (i = 0; i < nseg; i++) {
761 uint32_t flags;
762
0a8704a5
RPM
763 if (use_persistent_gnts)
764 persistent_gnt = get_persistent_gnt(
3f3aad5e 765 blkif,
0a8704a5
RPM
766 req->u.rw.seg[i].gref);
767
768 if (persistent_gnt) {
769 /*
770 * We are using persistent grants and
771 * the grant is already mapped
772 */
0a8704a5
RPM
773 pages[i] = persistent_gnt->page;
774 persistent_gnts[i] = persistent_gnt;
775 } else {
c6cc142d
RPM
776 if (get_free_page(blkif, &pages[i]))
777 goto out_of_memory;
778 addr = vaddr(pages[i]);
779 pages_to_gnt[segs_to_map] = pages[i];
0a8704a5 780 persistent_gnts[i] = NULL;
0a8704a5 781 flags = GNTMAP_host_map;
c6cc142d 782 if (!use_persistent_gnts &&
0a8704a5
RPM
783 (pending_req->operation != BLKIF_OP_READ))
784 flags |= GNTMAP_readonly;
785 gnttab_set_map_op(&map[segs_to_map++], addr,
786 flags, req->u.rw.seg[i].gref,
787 blkif->domid);
788 }
1a95fe6e
KRW
789 }
790
0a8704a5
RPM
791 if (segs_to_map) {
792 ret = gnttab_map_refs(map, NULL, pages_to_gnt, segs_to_map);
793 BUG_ON(ret);
794 }
1a95fe6e 795
01f37f2d
KRW
796 /*
797 * Now swizzle the MFN in our domain with the MFN from the other domain
1a95fe6e
KRW
798 * so that when we access vaddr(pending_req,i) it has the contents of
799 * the page from the other domain.
800 */
c6cc142d
RPM
801 for (seg_idx = 0, new_map_idx = 0; seg_idx < nseg; seg_idx++) {
802 if (!persistent_gnts[seg_idx]) {
0a8704a5 803 /* This is a newly mapped grant */
c6cc142d
RPM
804 BUG_ON(new_map_idx >= segs_to_map);
805 if (unlikely(map[new_map_idx].status != 0)) {
0a8704a5 806 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
c6cc142d 807 pending_handle(pending_req, seg_idx) = BLKBACK_INVALID_HANDLE;
0a8704a5 808 ret |= 1;
c6cc142d
RPM
809 new_map_idx++;
810 /*
811 * No need to set unmap_seg bit, since
812 * we can not unmap this grant because
813 * the handle is invalid.
814 */
815 continue;
0a8704a5 816 }
c6cc142d
RPM
817 pending_handle(pending_req, seg_idx) = map[new_map_idx].handle;
818 } else {
819 /* This grant is persistent and already mapped */
820 goto next;
0a8704a5 821 }
c6cc142d 822 if (use_persistent_gnts &&
3f3aad5e 823 blkif->persistent_gnt_c < xen_blkif_max_pgrants) {
c6cc142d
RPM
824 /*
825 * We are using persistent grants, the grant is
3f3aad5e 826 * not mapped but we might have room for it.
c6cc142d
RPM
827 */
828 persistent_gnt = kmalloc(sizeof(struct persistent_gnt),
829 GFP_KERNEL);
830 if (!persistent_gnt) {
0a8704a5 831 /*
c6cc142d
RPM
832 * If we don't have enough memory to
833 * allocate the persistent_gnt struct
834 * map this grant non-persistenly
0a8704a5 835 */
c6cc142d 836 goto next_unmap;
0a8704a5 837 }
c6cc142d
RPM
838 persistent_gnt->gnt = map[new_map_idx].ref;
839 persistent_gnt->handle = map[new_map_idx].handle;
840 persistent_gnt->page = pages[seg_idx];
3f3aad5e 841 if (add_persistent_gnt(blkif,
c6cc142d
RPM
842 persistent_gnt)) {
843 kfree(persistent_gnt);
844 persistent_gnt = NULL;
845 goto next_unmap;
846 }
3f3aad5e 847 persistent_gnts[seg_idx] = persistent_gnt;
c6cc142d
RPM
848 pr_debug(DRV_PFX " grant %u added to the tree of persistent grants, using %u/%u\n",
849 persistent_gnt->gnt, blkif->persistent_gnt_c,
3f3aad5e 850 xen_blkif_max_pgrants);
c6cc142d
RPM
851 new_map_idx++;
852 goto next;
853 }
854 if (use_persistent_gnts && !blkif->vbd.overflow_max_grants) {
855 blkif->vbd.overflow_max_grants = 1;
856 pr_debug(DRV_PFX " domain %u, device %#x is using maximum number of persistent grants\n",
857 blkif->domid, blkif->vbd.handle);
1a95fe6e 858 }
c6cc142d
RPM
859next_unmap:
860 /*
861 * We could not map this grant persistently, so use it as
862 * a non-persistent grant.
863 */
c6cc142d
RPM
864 new_map_idx++;
865next:
866 seg[seg_idx].offset = (req->u.rw.seg[seg_idx].first_sect << 9);
1a95fe6e
KRW
867 }
868 return ret;
c6cc142d
RPM
869
870out_of_memory:
871 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
872 put_free_pages(blkif, pages_to_gnt, segs_to_map);
873 return -ENOMEM;
1a95fe6e
KRW
874}
875
42146352
KRW
876static int dispatch_discard_io(struct xen_blkif *blkif,
877 struct blkif_request *req)
b3cb0d6a
LD
878{
879 int err = 0;
880 int status = BLKIF_RSP_OKAY;
881 struct block_device *bdev = blkif->vbd.bdev;
4dae7670 882 unsigned long secure;
b3cb0d6a 883
42146352
KRW
884 blkif->st_ds_req++;
885
886 xen_blkif_get(blkif);
4dae7670
KRW
887 secure = (blkif->vbd.discard_secure &&
888 (req->u.discard.flag & BLKIF_DISCARD_SECURE)) ?
889 BLKDEV_DISCARD_SECURE : 0;
890
891 err = blkdev_issue_discard(bdev, req->u.discard.sector_number,
892 req->u.discard.nr_sectors,
893 GFP_KERNEL, secure);
b3cb0d6a
LD
894
895 if (err == -EOPNOTSUPP) {
896 pr_debug(DRV_PFX "discard op failed, not supported\n");
897 status = BLKIF_RSP_EOPNOTSUPP;
898 } else if (err)
899 status = BLKIF_RSP_ERROR;
900
97e36834 901 make_response(blkif, req->u.discard.id, req->operation, status);
42146352
KRW
902 xen_blkif_put(blkif);
903 return err;
b3cb0d6a
LD
904}
905
0e367ae4
DV
906static int dispatch_other_io(struct xen_blkif *blkif,
907 struct blkif_request *req,
908 struct pending_req *pending_req)
909{
910 free_req(pending_req);
911 make_response(blkif, req->u.other.id, req->operation,
912 BLKIF_RSP_EOPNOTSUPP);
913 return -EIO;
914}
915
29bde093
KRW
916static void xen_blk_drain_io(struct xen_blkif *blkif)
917{
918 atomic_set(&blkif->drain, 1);
919 do {
6927d920
KRW
920 /* The initial value is one, and one refcnt taken at the
921 * start of the xen_blkif_schedule thread. */
922 if (atomic_read(&blkif->refcnt) <= 2)
923 break;
29bde093
KRW
924 wait_for_completion_interruptible_timeout(
925 &blkif->drain_complete, HZ);
926
927 if (!atomic_read(&blkif->drain))
928 break;
29bde093
KRW
929 } while (!kthread_should_stop());
930 atomic_set(&blkif->drain, 0);
931}
932
a1397fa3
KRW
933/*
934 * Completion callback on the bio's. Called as bh->b_end_io()
4d05a28d
KRW
935 */
936
2e9977c2 937static void __end_block_io_op(struct pending_req *pending_req, int error)
4d05a28d
KRW
938{
939 /* An error fails the entire request. */
24f567f9 940 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
4d05a28d 941 (error == -EOPNOTSUPP)) {
22b20f2d 942 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
24f567f9 943 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
4d05a28d 944 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
29bde093
KRW
945 } else if ((pending_req->operation == BLKIF_OP_WRITE_BARRIER) &&
946 (error == -EOPNOTSUPP)) {
947 pr_debug(DRV_PFX "write barrier op failed, not supported\n");
948 xen_blkbk_barrier(XBT_NIL, pending_req->blkif->be, 0);
949 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
4d05a28d 950 } else if (error) {
22b20f2d 951 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
ebe81906 952 " error=%d\n", error);
4d05a28d
KRW
953 pending_req->status = BLKIF_RSP_ERROR;
954 }
955
01f37f2d
KRW
956 /*
957 * If all of the bio's have completed it is time to unmap
a1397fa3 958 * the grant references associated with 'request' and provide
2e9977c2
KRW
959 * the proper response on the ring.
960 */
4d05a28d 961 if (atomic_dec_and_test(&pending_req->pendcnt)) {
9f3aedf5 962 xen_blkbk_unmap(pending_req);
4d05a28d
KRW
963 make_response(pending_req->blkif, pending_req->id,
964 pending_req->operation, pending_req->status);
8b6bf747 965 xen_blkif_put(pending_req->blkif);
29bde093
KRW
966 if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
967 if (atomic_read(&pending_req->blkif->drain))
968 complete(&pending_req->blkif->drain_complete);
969 }
4d05a28d
KRW
970 free_req(pending_req);
971 }
972}
973
a1397fa3
KRW
974/*
975 * bio callback.
976 */
88122933 977static void end_block_io_op(struct bio *bio, int error)
4d05a28d 978{
4d05a28d
KRW
979 __end_block_io_op(bio->bi_private, error);
980 bio_put(bio);
4d05a28d
KRW
981}
982
983
4d05a28d 984
a1397fa3
KRW
985/*
986 * Function to copy the from the ring buffer the 'struct blkif_request'
987 * (which has the sectors we want, number of them, grant references, etc),
988 * and transmute it to the block API to hand it over to the proper block disk.
4d05a28d 989 */
b4726a9d
DS
990static int
991__do_block_io_op(struct xen_blkif *blkif)
4d05a28d 992{
88122933
JF
993 union blkif_back_rings *blk_rings = &blkif->blk_rings;
994 struct blkif_request req;
2e9977c2 995 struct pending_req *pending_req;
4d05a28d
KRW
996 RING_IDX rc, rp;
997 int more_to_do = 0;
998
999 rc = blk_rings->common.req_cons;
1000 rp = blk_rings->common.sring->req_prod;
1001 rmb(); /* Ensure we see queued requests up to 'rp'. */
1002
1003 while (rc != rp) {
1004
1005 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
1006 break;
1007
8270b45b 1008 if (kthread_should_stop()) {
4d05a28d
KRW
1009 more_to_do = 1;
1010 break;
1011 }
1012
8270b45b
KF
1013 pending_req = alloc_req();
1014 if (NULL == pending_req) {
1015 blkif->st_oo_req++;
4d05a28d
KRW
1016 more_to_do = 1;
1017 break;
1018 }
1019
1020 switch (blkif->blk_protocol) {
1021 case BLKIF_PROTOCOL_NATIVE:
1022 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
1023 break;
1024 case BLKIF_PROTOCOL_X86_32:
1025 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
1026 break;
1027 case BLKIF_PROTOCOL_X86_64:
1028 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
1029 break;
1030 default:
1031 BUG();
1032 }
1033 blk_rings->common.req_cons = ++rc; /* before make_response() */
1034
1035 /* Apply all sanity checks to /private copy/ of request. */
1036 barrier();
0e367ae4
DV
1037
1038 switch (req.operation) {
1039 case BLKIF_OP_READ:
1040 case BLKIF_OP_WRITE:
1041 case BLKIF_OP_WRITE_BARRIER:
1042 case BLKIF_OP_FLUSH_DISKCACHE:
1043 if (dispatch_rw_block_io(blkif, &req, pending_req))
1044 goto done;
1045 break;
1046 case BLKIF_OP_DISCARD:
42146352
KRW
1047 free_req(pending_req);
1048 if (dispatch_discard_io(blkif, &req))
0e367ae4 1049 goto done;
4d05a28d 1050 break;
0e367ae4
DV
1051 default:
1052 if (dispatch_other_io(blkif, &req, pending_req))
1053 goto done;
1054 break;
1055 }
4d05a28d
KRW
1056
1057 /* Yield point for this unbounded loop. */
1058 cond_resched();
1059 }
0e367ae4 1060done:
4d05a28d
KRW
1061 return more_to_do;
1062}
1063
b4726a9d
DS
1064static int
1065do_block_io_op(struct xen_blkif *blkif)
1066{
1067 union blkif_back_rings *blk_rings = &blkif->blk_rings;
1068 int more_to_do;
1069
1070 do {
1071 more_to_do = __do_block_io_op(blkif);
1072 if (more_to_do)
1073 break;
1074
1075 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
1076 } while (more_to_do);
1077
1078 return more_to_do;
1079}
a1397fa3 1080/*
01f37f2d
KRW
1081 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
1082 * and call the 'submit_bio' to pass it to the underlying storage.
a1397fa3 1083 */
30fd1502
KRW
1084static int dispatch_rw_block_io(struct xen_blkif *blkif,
1085 struct blkif_request *req,
1086 struct pending_req *pending_req)
4d05a28d 1087{
4d05a28d 1088 struct phys_req preq;
1a95fe6e 1089 struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
4d05a28d
KRW
1090 unsigned int nseg;
1091 struct bio *bio = NULL;
77089926 1092 struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
1a95fe6e 1093 int i, nbio = 0;
4d05a28d 1094 int operation;
a19be5f0 1095 struct blk_plug plug;
29bde093 1096 bool drain = false;
c6cc142d 1097 struct page **pages = pending_req->pages;
4d05a28d
KRW
1098
1099 switch (req->operation) {
1100 case BLKIF_OP_READ:
fc53bf75 1101 blkif->st_rd_req++;
4d05a28d
KRW
1102 operation = READ;
1103 break;
1104 case BLKIF_OP_WRITE:
fc53bf75 1105 blkif->st_wr_req++;
013c3ca1 1106 operation = WRITE_ODIRECT;
4d05a28d 1107 break;
29bde093
KRW
1108 case BLKIF_OP_WRITE_BARRIER:
1109 drain = true;
24f567f9 1110 case BLKIF_OP_FLUSH_DISKCACHE:
fc53bf75 1111 blkif->st_f_req++;
24f567f9 1112 operation = WRITE_FLUSH;
4d05a28d
KRW
1113 break;
1114 default:
1115 operation = 0; /* make gcc happy */
fc53bf75
KRW
1116 goto fail_response;
1117 break;
4d05a28d
KRW
1118 }
1119
42146352
KRW
1120 /* Check that the number of segments is sane. */
1121 nseg = req->u.rw.nr_segments;
97e36834 1122
42146352 1123 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
4d05a28d 1124 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
22b20f2d 1125 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
ebe81906 1126 nseg);
1a95fe6e 1127 /* Haven't submitted any bio's yet. */
4d05a28d
KRW
1128 goto fail_response;
1129 }
1130
c35950bf 1131 preq.sector_number = req->u.rw.sector_number;
4d05a28d
KRW
1132 preq.nr_sects = 0;
1133
1134 pending_req->blkif = blkif;
97e36834 1135 pending_req->id = req->u.rw.id;
4d05a28d
KRW
1136 pending_req->operation = req->operation;
1137 pending_req->status = BLKIF_RSP_OKAY;
1138 pending_req->nr_pages = nseg;
e9350493 1139
4d05a28d 1140 for (i = 0; i < nseg; i++) {
c35950bf
KRW
1141 seg[i].nsec = req->u.rw.seg[i].last_sect -
1142 req->u.rw.seg[i].first_sect + 1;
c35950bf
KRW
1143 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
1144 (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
4d05a28d
KRW
1145 goto fail_response;
1146 preq.nr_sects += seg[i].nsec;
976222e0 1147
4d05a28d
KRW
1148 }
1149
3d814731 1150 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
22b20f2d 1151 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
ebe81906
KRW
1152 operation == READ ? "read" : "write",
1153 preq.sector_number,
a72d9002
CG
1154 preq.sector_number + preq.nr_sects,
1155 blkif->vbd.pdevice);
1a95fe6e 1156 goto fail_response;
4d05a28d 1157 }
01f37f2d
KRW
1158
1159 /*
3d814731 1160 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
01f37f2d
KRW
1161 * is set there.
1162 */
e9350493
KRW
1163 for (i = 0; i < nseg; i++) {
1164 if (((int)preq.sector_number|(int)seg[i].nsec) &
1165 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
22b20f2d 1166 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
ebe81906 1167 blkif->domid);
e9350493
KRW
1168 goto fail_response;
1169 }
1170 }
01f37f2d 1171
29bde093
KRW
1172 /* Wait on all outstanding I/O's and once that has been completed
1173 * issue the WRITE_FLUSH.
1174 */
1175 if (drain)
1176 xen_blk_drain_io(pending_req->blkif);
1177
01f37f2d
KRW
1178 /*
1179 * If we have failed at this point, we need to undo the M2P override,
2e9977c2
KRW
1180 * set gnttab_set_unmap_op on all of the grant references and perform
1181 * the hypercall to unmap the grants - that is all done in
9f3aedf5 1182 * xen_blkbk_unmap.
2e9977c2 1183 */
0a8704a5 1184 if (xen_blkbk_map(req, pending_req, seg, pages))
4d05a28d
KRW
1185 goto fail_flush;
1186
b3cb0d6a
LD
1187 /*
1188 * This corresponding xen_blkif_put is done in __end_block_io_op, or
1189 * below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
1190 */
8b6bf747 1191 xen_blkif_get(blkif);
4d05a28d
KRW
1192
1193 for (i = 0; i < nseg; i++) {
4d05a28d
KRW
1194 while ((bio == NULL) ||
1195 (bio_add_page(bio,
0a8704a5 1196 pages[i],
4d05a28d 1197 seg[i].nsec << 9,
ffb1dabd 1198 seg[i].offset) == 0)) {
2e9977c2 1199
03e0edf9 1200 bio = bio_alloc(GFP_KERNEL, nseg-i);
4d05a28d
KRW
1201 if (unlikely(bio == NULL))
1202 goto fail_put_bio;
1203
03e0edf9 1204 biolist[nbio++] = bio;
4d05a28d
KRW
1205 bio->bi_bdev = preq.bdev;
1206 bio->bi_private = pending_req;
1207 bio->bi_end_io = end_block_io_op;
1208 bio->bi_sector = preq.sector_number;
1209 }
1210
1211 preq.sector_number += seg[i].nsec;
1212 }
1213
b3cb0d6a 1214 /* This will be hit if the operation was a flush or discard. */
4d05a28d 1215 if (!bio) {
42146352 1216 BUG_ON(operation != WRITE_FLUSH);
b0f80127 1217
42146352
KRW
1218 bio = bio_alloc(GFP_KERNEL, 0);
1219 if (unlikely(bio == NULL))
1220 goto fail_put_bio;
4d05a28d 1221
42146352
KRW
1222 biolist[nbio++] = bio;
1223 bio->bi_bdev = preq.bdev;
1224 bio->bi_private = pending_req;
1225 bio->bi_end_io = end_block_io_op;
4d05a28d
KRW
1226 }
1227
77089926 1228 atomic_set(&pending_req->pendcnt, nbio);
a19be5f0
KRW
1229 blk_start_plug(&plug);
1230
77089926
KRW
1231 for (i = 0; i < nbio; i++)
1232 submit_bio(operation, biolist[i]);
1233
a19be5f0 1234 /* Let the I/Os go.. */
3d68b399 1235 blk_finish_plug(&plug);
a19be5f0 1236
4d05a28d
KRW
1237 if (operation == READ)
1238 blkif->st_rd_sect += preq.nr_sects;
5c62cb48 1239 else if (operation & WRITE)
4d05a28d
KRW
1240 blkif->st_wr_sect += preq.nr_sects;
1241
fc53bf75 1242 return 0;
4d05a28d
KRW
1243
1244 fail_flush:
9f3aedf5 1245 xen_blkbk_unmap(pending_req);
4d05a28d 1246 fail_response:
0faa8cca 1247 /* Haven't submitted any bio's yet. */
97e36834 1248 make_response(blkif, req->u.rw.id, req->operation, BLKIF_RSP_ERROR);
4d05a28d
KRW
1249 free_req(pending_req);
1250 msleep(1); /* back off a bit */
fc53bf75 1251 return -EIO;
4d05a28d
KRW
1252
1253 fail_put_bio:
03e0edf9 1254 for (i = 0; i < nbio; i++)
77089926 1255 bio_put(biolist[i]);
0e5e098a 1256 atomic_set(&pending_req->pendcnt, 1);
4d05a28d 1257 __end_block_io_op(pending_req, -EINVAL);
4d05a28d 1258 msleep(1); /* back off a bit */
fc53bf75 1259 return -EIO;
4d05a28d
KRW
1260}
1261
1262
1263
a1397fa3
KRW
1264/*
1265 * Put a response on the ring on how the operation fared.
4d05a28d 1266 */
30fd1502 1267static void make_response(struct xen_blkif *blkif, u64 id,
4d05a28d
KRW
1268 unsigned short op, int st)
1269{
88122933 1270 struct blkif_response resp;
4d05a28d 1271 unsigned long flags;
88122933 1272 union blkif_back_rings *blk_rings = &blkif->blk_rings;
4d05a28d
KRW
1273 int notify;
1274
1275 resp.id = id;
1276 resp.operation = op;
1277 resp.status = st;
1278
1279 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
1280 /* Place on the response ring for the relevant domain. */
1281 switch (blkif->blk_protocol) {
1282 case BLKIF_PROTOCOL_NATIVE:
1283 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
1284 &resp, sizeof(resp));
1285 break;
1286 case BLKIF_PROTOCOL_X86_32:
1287 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
1288 &resp, sizeof(resp));
1289 break;
1290 case BLKIF_PROTOCOL_X86_64:
1291 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
1292 &resp, sizeof(resp));
1293 break;
1294 default:
1295 BUG();
1296 }
1297 blk_rings->common.rsp_prod_pvt++;
1298 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
4d05a28d 1299 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
4d05a28d
KRW
1300 if (notify)
1301 notify_remote_via_irq(blkif->irq);
1302}
1303
8b6bf747 1304static int __init xen_blkif_init(void)
4d05a28d 1305{
bb6acb28 1306 int i;
8770b268 1307 int rc = 0;
4d05a28d 1308
b2167ba6 1309 if (!xen_domain())
4d05a28d
KRW
1310 return -ENODEV;
1311
2e9977c2 1312 blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
e8e28871 1313 if (!blkbk) {
22b20f2d 1314 pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
e8e28871
KRW
1315 return -ENOMEM;
1316 }
1317
4d05a28d 1318
8e6dc6fe 1319 blkbk->pending_reqs = kzalloc(sizeof(blkbk->pending_reqs[0]) *
8b6bf747 1320 xen_blkif_reqs, GFP_KERNEL);
4d05a28d 1321
bb6acb28 1322 if (!blkbk->pending_reqs) {
8770b268 1323 rc = -ENOMEM;
4d05a28d 1324 goto out_of_memory;
8770b268 1325 }
4d05a28d 1326
8b6bf747 1327 rc = xen_blkif_interface_init();
8770b268
KRW
1328 if (rc)
1329 goto failed_init;
4d05a28d 1330
e8e28871
KRW
1331 INIT_LIST_HEAD(&blkbk->pending_free);
1332 spin_lock_init(&blkbk->pending_free_lock);
1333 init_waitqueue_head(&blkbk->pending_free_wq);
4d05a28d 1334
8b6bf747 1335 for (i = 0; i < xen_blkif_reqs; i++)
2e9977c2
KRW
1336 list_add_tail(&blkbk->pending_reqs[i].free_list,
1337 &blkbk->pending_free);
4d05a28d 1338
8b6bf747 1339 rc = xen_blkif_xenbus_init();
8770b268
KRW
1340 if (rc)
1341 goto failed_init;
4d05a28d
KRW
1342
1343 return 0;
1344
1345 out_of_memory:
22b20f2d 1346 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
8770b268 1347 failed_init:
e8e28871 1348 kfree(blkbk->pending_reqs);
a742b02c 1349 kfree(blkbk);
e8e28871 1350 blkbk = NULL;
8770b268 1351 return rc;
4d05a28d
KRW
1352}
1353
8b6bf747 1354module_init(xen_blkif_init);
4d05a28d
KRW
1355
1356MODULE_LICENSE("Dual BSD/GPL");
a7e9357f 1357MODULE_ALIAS("xen-backend:vbd");
This page took 0.149258 seconds and 5 git commands to generate.