]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/block/xen-blkfront.c
Merge branch 'stable/for-jens-4.2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-zesty-kernel.git] / drivers / block / xen-blkfront.c
CommitLineData
9f27ee59
JF
1/*
2 * blkfront.c
3 *
4 * XenLinux virtual block device driver.
5 *
6 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
7 * Modifications by Mark A. Williamson are (c) Intel Research Cambridge
8 * Copyright (c) 2004, Christian Limpach
9 * Copyright (c) 2004, Andrew Warfield
10 * Copyright (c) 2005, Christopher Clark
11 * Copyright (c) 2005, XenSource Ltd
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/interrupt.h>
39#include <linux/blkdev.h>
597592d9 40#include <linux/hdreg.h>
440a01a7 41#include <linux/cdrom.h>
9f27ee59 42#include <linux/module.h>
5a0e3ad6 43#include <linux/slab.h>
2a48fc0a 44#include <linux/mutex.h>
9e973e64 45#include <linux/scatterlist.h>
34ae2e47 46#include <linux/bitmap.h>
155b7edb 47#include <linux/list.h>
9f27ee59 48
1ccbf534 49#include <xen/xen.h>
9f27ee59
JF
50#include <xen/xenbus.h>
51#include <xen/grant_table.h>
52#include <xen/events.h>
53#include <xen/page.h>
c1c5413a 54#include <xen/platform_pci.h>
9f27ee59
JF
55
56#include <xen/interface/grant_table.h>
57#include <xen/interface/io/blkif.h>
3e334239 58#include <xen/interface/io/protocols.h>
9f27ee59
JF
59
60#include <asm/xen/hypervisor.h>
61
62enum blkif_state {
63 BLKIF_STATE_DISCONNECTED,
64 BLKIF_STATE_CONNECTED,
65 BLKIF_STATE_SUSPENDED,
66};
67
0a8704a5
RPM
68struct grant {
69 grant_ref_t gref;
70 unsigned long pfn;
155b7edb 71 struct list_head node;
0a8704a5
RPM
72};
73
9f27ee59
JF
74struct blk_shadow {
75 struct blkif_request req;
a945b980 76 struct request *request;
402b27f9
RPM
77 struct grant **grants_used;
78 struct grant **indirect_grants;
b7649158 79 struct scatterlist *sg;
402b27f9
RPM
80};
81
82struct split_bio {
83 struct bio *bio;
84 atomic_t pending;
85 int err;
9f27ee59
JF
86};
87
2a48fc0a 88static DEFINE_MUTEX(blkfront_mutex);
83d5cde4 89static const struct block_device_operations xlvbd_block_fops;
9f27ee59 90
402b27f9
RPM
91/*
92 * Maximum number of segments in indirect requests, the actual value used by
93 * the frontend driver is the minimum of this value and the value provided
94 * by the backend driver.
95 */
96
97static unsigned int xen_blkif_max_segments = 32;
2d5dc3ba
KRW
98module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
99MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
402b27f9 100
86839c56
BL
101/*
102 * Maximum order of pages to be used for the shared ring between front and
103 * backend, 4KB page granularity is used.
104 */
105static unsigned int xen_blkif_max_ring_order;
106module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, S_IRUGO);
107MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
108
109#define BLK_RING_SIZE(info) __CONST_RING_SIZE(blkif, PAGE_SIZE * (info)->nr_ring_pages)
110#define BLK_MAX_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE * XENBUS_MAX_RING_PAGES)
111/*
112 * ring-ref%i i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
113 * characters are enough. Define to 20 to keep consist with backend.
114 */
115#define RINGREF_NAME_LEN (20)
9f27ee59
JF
116
117/*
118 * We have one of these per vbd, whether ide, scsi or 'other'. They
119 * hang in private_data off the gendisk structure. We may end up
120 * putting all kinds of interesting stuff here :-)
121 */
122struct blkfront_info
123{
3467811e 124 spinlock_t io_lock;
b70f5fa0 125 struct mutex mutex;
9f27ee59 126 struct xenbus_device *xbdev;
9f27ee59
JF
127 struct gendisk *gd;
128 int vdevice;
129 blkif_vdev_t handle;
130 enum blkif_state connected;
86839c56
BL
131 int ring_ref[XENBUS_MAX_RING_PAGES];
132 unsigned int nr_ring_pages;
9f27ee59
JF
133 struct blkif_front_ring ring;
134 unsigned int evtchn, irq;
135 struct request_queue *rq;
136 struct work_struct work;
137 struct gnttab_free_callback callback;
86839c56 138 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
bfe11d6d
RPM
139 struct list_head grants;
140 struct list_head indirect_pages;
0a8704a5 141 unsigned int persistent_gnts_c;
9f27ee59 142 unsigned long shadow_free;
4913efe4 143 unsigned int feature_flush;
5ea42986
KRW
144 unsigned int feature_discard:1;
145 unsigned int feature_secdiscard:1;
ed30bf31
LD
146 unsigned int discard_granularity;
147 unsigned int discard_alignment;
0a8704a5 148 unsigned int feature_persistent:1;
402b27f9 149 unsigned int max_indirect_segments;
1d78d705 150 int is_ready;
9f27ee59
JF
151};
152
0e345826
JB
153static unsigned int nr_minors;
154static unsigned long *minors;
155static DEFINE_SPINLOCK(minor_lock);
156
9f27ee59
JF
157#define GRANT_INVALID_REF 0
158
159#define PARTS_PER_DISK 16
9246b5f0 160#define PARTS_PER_EXT_DISK 256
9f27ee59
JF
161
162#define BLKIF_MAJOR(dev) ((dev)>>8)
163#define BLKIF_MINOR(dev) ((dev) & 0xff)
164
9246b5f0
CL
165#define EXT_SHIFT 28
166#define EXTENDED (1<<EXT_SHIFT)
167#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
168#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
c80a4209
SS
169#define EMULATED_HD_DISK_MINOR_OFFSET (0)
170#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
196cfe2a
SB
171#define EMULATED_SD_DISK_MINOR_OFFSET (0)
172#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
9f27ee59 173
9246b5f0 174#define DEV_NAME "xvd" /* name in /dev */
9f27ee59 175
402b27f9 176#define SEGS_PER_INDIRECT_FRAME \
80bfa2f6 177 (PAGE_SIZE/sizeof(struct blkif_request_segment))
402b27f9
RPM
178#define INDIRECT_GREFS(_segs) \
179 ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
180
181static int blkfront_setup_indirect(struct blkfront_info *info);
d50babbe 182static int blkfront_gather_backend_features(struct blkfront_info *info);
402b27f9 183
9f27ee59
JF
184static int get_id_from_freelist(struct blkfront_info *info)
185{
186 unsigned long free = info->shadow_free;
86839c56 187 BUG_ON(free >= BLK_RING_SIZE(info));
97e36834
KRW
188 info->shadow_free = info->shadow[free].req.u.rw.id;
189 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
9f27ee59
JF
190 return free;
191}
192
6878c32e 193static int add_id_to_freelist(struct blkfront_info *info,
9f27ee59
JF
194 unsigned long id)
195{
6878c32e
KRW
196 if (info->shadow[id].req.u.rw.id != id)
197 return -EINVAL;
198 if (info->shadow[id].request == NULL)
199 return -EINVAL;
97e36834 200 info->shadow[id].req.u.rw.id = info->shadow_free;
a945b980 201 info->shadow[id].request = NULL;
9f27ee59 202 info->shadow_free = id;
6878c32e 203 return 0;
9f27ee59
JF
204}
205
9c1e050c
RPM
206static int fill_grant_buffer(struct blkfront_info *info, int num)
207{
208 struct page *granted_page;
209 struct grant *gnt_list_entry, *n;
210 int i = 0;
211
212 while(i < num) {
213 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
214 if (!gnt_list_entry)
215 goto out_of_memory;
216
bfe11d6d
RPM
217 if (info->feature_persistent) {
218 granted_page = alloc_page(GFP_NOIO);
219 if (!granted_page) {
220 kfree(gnt_list_entry);
221 goto out_of_memory;
222 }
223 gnt_list_entry->pfn = page_to_pfn(granted_page);
9c1e050c
RPM
224 }
225
9c1e050c 226 gnt_list_entry->gref = GRANT_INVALID_REF;
bfe11d6d 227 list_add(&gnt_list_entry->node, &info->grants);
9c1e050c
RPM
228 i++;
229 }
230
231 return 0;
232
233out_of_memory:
234 list_for_each_entry_safe(gnt_list_entry, n,
bfe11d6d 235 &info->grants, node) {
9c1e050c 236 list_del(&gnt_list_entry->node);
bfe11d6d
RPM
237 if (info->feature_persistent)
238 __free_page(pfn_to_page(gnt_list_entry->pfn));
9c1e050c
RPM
239 kfree(gnt_list_entry);
240 i--;
241 }
242 BUG_ON(i != 0);
243 return -ENOMEM;
244}
245
246static struct grant *get_grant(grant_ref_t *gref_head,
bfe11d6d 247 unsigned long pfn,
9c1e050c
RPM
248 struct blkfront_info *info)
249{
250 struct grant *gnt_list_entry;
251 unsigned long buffer_mfn;
252
bfe11d6d
RPM
253 BUG_ON(list_empty(&info->grants));
254 gnt_list_entry = list_first_entry(&info->grants, struct grant,
9c1e050c
RPM
255 node);
256 list_del(&gnt_list_entry->node);
257
258 if (gnt_list_entry->gref != GRANT_INVALID_REF) {
259 info->persistent_gnts_c--;
260 return gnt_list_entry;
261 }
262
263 /* Assign a gref to this page */
264 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
265 BUG_ON(gnt_list_entry->gref == -ENOSPC);
bfe11d6d
RPM
266 if (!info->feature_persistent) {
267 BUG_ON(!pfn);
268 gnt_list_entry->pfn = pfn;
269 }
9c1e050c
RPM
270 buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
271 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
272 info->xbdev->otherend_id,
273 buffer_mfn, 0);
274 return gnt_list_entry;
275}
276
6878c32e
KRW
277static const char *op_name(int op)
278{
279 static const char *const names[] = {
280 [BLKIF_OP_READ] = "read",
281 [BLKIF_OP_WRITE] = "write",
282 [BLKIF_OP_WRITE_BARRIER] = "barrier",
283 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
284 [BLKIF_OP_DISCARD] = "discard" };
285
286 if (op < 0 || op >= ARRAY_SIZE(names))
287 return "unknown";
288
289 if (!names[op])
290 return "reserved";
291
292 return names[op];
293}
0e345826
JB
294static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
295{
296 unsigned int end = minor + nr;
297 int rc;
298
299 if (end > nr_minors) {
300 unsigned long *bitmap, *old;
301
f094148a 302 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
0e345826
JB
303 GFP_KERNEL);
304 if (bitmap == NULL)
305 return -ENOMEM;
306
307 spin_lock(&minor_lock);
308 if (end > nr_minors) {
309 old = minors;
310 memcpy(bitmap, minors,
311 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
312 minors = bitmap;
313 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
314 } else
315 old = bitmap;
316 spin_unlock(&minor_lock);
317 kfree(old);
318 }
319
320 spin_lock(&minor_lock);
321 if (find_next_bit(minors, end, minor) >= end) {
34ae2e47 322 bitmap_set(minors, minor, nr);
0e345826
JB
323 rc = 0;
324 } else
325 rc = -EBUSY;
326 spin_unlock(&minor_lock);
327
328 return rc;
329}
330
331static void xlbd_release_minors(unsigned int minor, unsigned int nr)
332{
333 unsigned int end = minor + nr;
334
335 BUG_ON(end > nr_minors);
336 spin_lock(&minor_lock);
34ae2e47 337 bitmap_clear(minors, minor, nr);
0e345826
JB
338 spin_unlock(&minor_lock);
339}
340
9f27ee59
JF
341static void blkif_restart_queue_callback(void *arg)
342{
343 struct blkfront_info *info = (struct blkfront_info *)arg;
344 schedule_work(&info->work);
345}
346
afe42d7d 347static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
597592d9
IC
348{
349 /* We don't have real geometry info, but let's at least return
350 values consistent with the size of the device */
351 sector_t nsect = get_capacity(bd->bd_disk);
352 sector_t cylinders = nsect;
353
354 hg->heads = 0xff;
355 hg->sectors = 0x3f;
356 sector_div(cylinders, hg->heads * hg->sectors);
357 hg->cylinders = cylinders;
358 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
359 hg->cylinders = 0xffff;
360 return 0;
361}
362
a63c848b 363static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
62aa0054 364 unsigned command, unsigned long argument)
440a01a7 365{
a63c848b 366 struct blkfront_info *info = bdev->bd_disk->private_data;
440a01a7
CL
367 int i;
368
369 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
370 command, (long)argument);
371
372 switch (command) {
373 case CDROMMULTISESSION:
374 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
375 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
376 if (put_user(0, (char __user *)(argument + i)))
377 return -EFAULT;
378 return 0;
379
380 case CDROM_GET_CAPABILITY: {
381 struct gendisk *gd = info->gd;
382 if (gd->flags & GENHD_FL_CD)
383 return 0;
384 return -EINVAL;
385 }
386
387 default:
388 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
389 command);*/
390 return -EINVAL; /* same return as native Linux */
391 }
392
393 return 0;
394}
395
9f27ee59 396/*
c64e38ea 397 * Generate a Xen blkfront IO request from a blk layer request. Reads
edf6ef59 398 * and writes are handled as expected.
9f27ee59 399 *
c64e38ea 400 * @req: a request struct
9f27ee59
JF
401 */
402static int blkif_queue_request(struct request *req)
403{
404 struct blkfront_info *info = req->rq_disk->private_data;
9f27ee59 405 struct blkif_request *ring_req;
9f27ee59
JF
406 unsigned long id;
407 unsigned int fsect, lsect;
402b27f9 408 int i, ref, n;
80bfa2f6 409 struct blkif_request_segment *segments = NULL;
0a8704a5
RPM
410
411 /*
412 * Used to store if we are able to queue the request by just using
413 * existing persistent grants, or if we have to get new grants,
414 * as there are not sufficiently many free.
415 */
416 bool new_persistent_gnts;
9f27ee59 417 grant_ref_t gref_head;
0a8704a5 418 struct grant *gnt_list_entry = NULL;
9e973e64 419 struct scatterlist *sg;
402b27f9 420 int nseg, max_grefs;
9f27ee59
JF
421
422 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
423 return 1;
424
c47206e2
RPM
425 max_grefs = req->nr_phys_segments;
426 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
427 /*
428 * If we are using indirect segments we need to account
429 * for the indirect grefs used in the request.
430 */
431 max_grefs += INDIRECT_GREFS(req->nr_phys_segments);
402b27f9
RPM
432
433 /* Check if we have enough grants to allocate a requests */
434 if (info->persistent_gnts_c < max_grefs) {
0a8704a5
RPM
435 new_persistent_gnts = 1;
436 if (gnttab_alloc_grant_references(
402b27f9 437 max_grefs - info->persistent_gnts_c,
0a8704a5
RPM
438 &gref_head) < 0) {
439 gnttab_request_free_callback(
440 &info->callback,
441 blkif_restart_queue_callback,
442 info,
402b27f9 443 max_grefs);
0a8704a5
RPM
444 return 1;
445 }
446 } else
447 new_persistent_gnts = 0;
9f27ee59
JF
448
449 /* Fill out a communications ring structure. */
450 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
451 id = get_id_from_freelist(info);
a945b980 452 info->shadow[id].request = req;
9f27ee59 453
5ea42986 454 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
ed30bf31 455 ring_req->operation = BLKIF_OP_DISCARD;
ed30bf31 456 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
402b27f9
RPM
457 ring_req->u.discard.id = id;
458 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
5ea42986
KRW
459 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
460 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
461 else
462 ring_req->u.discard.flag = 0;
ed30bf31 463 } else {
402b27f9
RPM
464 BUG_ON(info->max_indirect_segments == 0 &&
465 req->nr_phys_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
466 BUG_ON(info->max_indirect_segments &&
467 req->nr_phys_segments > info->max_indirect_segments);
b7649158 468 nseg = blk_rq_map_sg(req->q, req, info->shadow[id].sg);
402b27f9
RPM
469 ring_req->u.rw.id = id;
470 if (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
471 /*
472 * The indirect operation can only be a BLKIF_OP_READ or
473 * BLKIF_OP_WRITE
474 */
475 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
476 ring_req->operation = BLKIF_OP_INDIRECT;
477 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
478 BLKIF_OP_WRITE : BLKIF_OP_READ;
479 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
480 ring_req->u.indirect.handle = info->handle;
481 ring_req->u.indirect.nr_segments = nseg;
482 } else {
483 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
484 ring_req->u.rw.handle = info->handle;
485 ring_req->operation = rq_data_dir(req) ?
486 BLKIF_OP_WRITE : BLKIF_OP_READ;
487 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
488 /*
489 * Ideally we can do an unordered flush-to-disk. In case the
490 * backend onlysupports barriers, use that. A barrier request
491 * a superset of FUA, so we can implement it the same
492 * way. (It's also a FLUSH+FUA, since it is
493 * guaranteed ordered WRT previous writes.)
494 */
fdf9b965
VK
495 switch (info->feature_flush &
496 ((REQ_FLUSH|REQ_FUA))) {
497 case REQ_FLUSH|REQ_FUA:
498 ring_req->operation =
499 BLKIF_OP_WRITE_BARRIER;
500 break;
501 case REQ_FLUSH:
502 ring_req->operation =
503 BLKIF_OP_FLUSH_DISKCACHE;
504 break;
505 default:
506 ring_req->operation = 0;
507 }
402b27f9
RPM
508 }
509 ring_req->u.rw.nr_segments = nseg;
510 }
b7649158 511 for_each_sg(info->shadow[id].sg, sg, nseg, i) {
ed30bf31
LD
512 fsect = sg->offset >> 9;
513 lsect = fsect + (sg->length >> 9) - 1;
6c92e699 514
402b27f9
RPM
515 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
516 (i % SEGS_PER_INDIRECT_FRAME == 0)) {
427bfe07 517 unsigned long uninitialized_var(pfn);
bfe11d6d 518
402b27f9
RPM
519 if (segments)
520 kunmap_atomic(segments);
521
522 n = i / SEGS_PER_INDIRECT_FRAME;
bfe11d6d
RPM
523 if (!info->feature_persistent) {
524 struct page *indirect_page;
525
526 /* Fetch a pre-allocated page to use for indirect grefs */
527 BUG_ON(list_empty(&info->indirect_pages));
528 indirect_page = list_first_entry(&info->indirect_pages,
529 struct page, lru);
530 list_del(&indirect_page->lru);
531 pfn = page_to_pfn(indirect_page);
532 }
533 gnt_list_entry = get_grant(&gref_head, pfn, info);
402b27f9
RPM
534 info->shadow[id].indirect_grants[n] = gnt_list_entry;
535 segments = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
536 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
537 }
538
bfe11d6d 539 gnt_list_entry = get_grant(&gref_head, page_to_pfn(sg_page(sg)), info);
9c1e050c 540 ref = gnt_list_entry->gref;
0a8704a5
RPM
541
542 info->shadow[id].grants_used[i] = gnt_list_entry;
543
bfe11d6d 544 if (rq_data_dir(req) && info->feature_persistent) {
0a8704a5
RPM
545 char *bvec_data;
546 void *shared_data;
547
548 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
549
402b27f9 550 shared_data = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
0a8704a5
RPM
551 bvec_data = kmap_atomic(sg_page(sg));
552
553 /*
554 * this does not wipe data stored outside the
555 * range sg->offset..sg->offset+sg->length.
556 * Therefore, blkback *could* see data from
557 * previous requests. This is OK as long as
558 * persistent grants are shared with just one
559 * domain. It may need refactoring if this
560 * changes
561 */
562 memcpy(shared_data + sg->offset,
563 bvec_data + sg->offset,
564 sg->length);
565
566 kunmap_atomic(bvec_data);
567 kunmap_atomic(shared_data);
568 }
402b27f9
RPM
569 if (ring_req->operation != BLKIF_OP_INDIRECT) {
570 ring_req->u.rw.seg[i] =
571 (struct blkif_request_segment) {
572 .gref = ref,
573 .first_sect = fsect,
574 .last_sect = lsect };
575 } else {
576 n = i % SEGS_PER_INDIRECT_FRAME;
577 segments[n] =
80bfa2f6 578 (struct blkif_request_segment) {
402b27f9
RPM
579 .gref = ref,
580 .first_sect = fsect,
581 .last_sect = lsect };
582 }
ed30bf31 583 }
402b27f9
RPM
584 if (segments)
585 kunmap_atomic(segments);
9f27ee59
JF
586 }
587
588 info->ring.req_prod_pvt++;
589
590 /* Keep a private copy so we can reissue requests when recovering. */
591 info->shadow[id].req = *ring_req;
592
0a8704a5
RPM
593 if (new_persistent_gnts)
594 gnttab_free_grant_references(gref_head);
9f27ee59
JF
595
596 return 0;
597}
598
599
600static inline void flush_requests(struct blkfront_info *info)
601{
602 int notify;
603
604 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
605
606 if (notify)
607 notify_remote_via_irq(info->irq);
608}
609
ad42d391
VK
610static inline bool blkif_request_flush_invalid(struct request *req,
611 struct blkfront_info *info)
0f1ca65e
AA
612{
613 return ((req->cmd_type != REQ_TYPE_FS) ||
ad42d391
VK
614 ((req->cmd_flags & REQ_FLUSH) &&
615 !(info->feature_flush & REQ_FLUSH)) ||
616 ((req->cmd_flags & REQ_FUA) &&
617 !(info->feature_flush & REQ_FUA)));
0f1ca65e
AA
618}
619
9f27ee59
JF
620/*
621 * do_blkif_request
622 * read a block; request is in a request queue
623 */
165125e1 624static void do_blkif_request(struct request_queue *rq)
9f27ee59
JF
625{
626 struct blkfront_info *info = NULL;
627 struct request *req;
628 int queued;
629
630 pr_debug("Entered do_blkif_request\n");
631
632 queued = 0;
633
9934c8c0 634 while ((req = blk_peek_request(rq)) != NULL) {
9f27ee59 635 info = req->rq_disk->private_data;
9f27ee59
JF
636
637 if (RING_FULL(&info->ring))
638 goto wait;
639
9934c8c0 640 blk_start_request(req);
296b2f6a 641
ad42d391
VK
642 if (blkif_request_flush_invalid(req, info)) {
643 __blk_end_request_all(req, -EOPNOTSUPP);
296b2f6a
TH
644 continue;
645 }
646
9f27ee59 647 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
b4f42e28 648 "(%u/%u) [%s]\n",
83096ebf
TH
649 req, req->cmd, (unsigned long)blk_rq_pos(req),
650 blk_rq_cur_sectors(req), blk_rq_sectors(req),
b4f42e28 651 rq_data_dir(req) ? "write" : "read");
9f27ee59 652
9f27ee59
JF
653 if (blkif_queue_request(req)) {
654 blk_requeue_request(rq, req);
655wait:
656 /* Avoid pointless unplugs. */
657 blk_stop_queue(rq);
658 break;
659 }
660
661 queued++;
662 }
663
664 if (queued != 0)
665 flush_requests(info);
666}
667
402b27f9 668static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
7c4d7d71 669 unsigned int physical_sector_size,
402b27f9 670 unsigned int segments)
9f27ee59 671{
165125e1 672 struct request_queue *rq;
ed30bf31 673 struct blkfront_info *info = gd->private_data;
9f27ee59 674
3467811e 675 rq = blk_init_queue(do_blkif_request, &info->io_lock);
9f27ee59
JF
676 if (rq == NULL)
677 return -1;
678
66d352e1 679 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
9f27ee59 680
ed30bf31
LD
681 if (info->feature_discard) {
682 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
683 blk_queue_max_discard_sectors(rq, get_capacity(gd));
684 rq->limits.discard_granularity = info->discard_granularity;
685 rq->limits.discard_alignment = info->discard_alignment;
5ea42986
KRW
686 if (info->feature_secdiscard)
687 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
ed30bf31
LD
688 }
689
9f27ee59 690 /* Hard sector size and max sectors impersonate the equiv. hardware. */
e1defc4f 691 blk_queue_logical_block_size(rq, sector_size);
7c4d7d71 692 blk_queue_physical_block_size(rq, physical_sector_size);
294caaf2 693 blk_queue_max_hw_sectors(rq, (segments * PAGE_SIZE) / 512);
9f27ee59
JF
694
695 /* Each segment in a request is up to an aligned page in size. */
696 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
697 blk_queue_max_segment_size(rq, PAGE_SIZE);
698
699 /* Ensure a merged request will fit in a single I/O ring slot. */
402b27f9 700 blk_queue_max_segments(rq, segments);
9f27ee59
JF
701
702 /* Make sure buffer addresses are sector-aligned. */
703 blk_queue_dma_alignment(rq, 511);
704
1c91fe1a
IC
705 /* Make sure we don't use bounce buffers. */
706 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
707
9f27ee59
JF
708 gd->queue = rq;
709
710 return 0;
711}
712
fdf9b965
VK
713static const char *flush_info(unsigned int feature_flush)
714{
715 switch (feature_flush & ((REQ_FLUSH | REQ_FUA))) {
716 case REQ_FLUSH|REQ_FUA:
717 return "barrier: enabled;";
718 case REQ_FLUSH:
719 return "flush diskcache: enabled;";
720 default:
721 return "barrier or flush: disabled;";
722 }
723}
9f27ee59 724
4913efe4 725static void xlvbd_flush(struct blkfront_info *info)
9f27ee59 726{
4913efe4 727 blk_queue_flush(info->rq, info->feature_flush);
fdf9b965
VK
728 pr_info("blkfront: %s: %s %s %s %s %s\n",
729 info->gd->disk_name, flush_info(info->feature_flush),
730 "persistent grants:", info->feature_persistent ?
731 "enabled;" : "disabled;", "indirect descriptors:",
732 info->max_indirect_segments ? "enabled;" : "disabled;");
9f27ee59
JF
733}
734
c80a4209
SS
735static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
736{
737 int major;
738 major = BLKIF_MAJOR(vdevice);
739 *minor = BLKIF_MINOR(vdevice);
740 switch (major) {
741 case XEN_IDE0_MAJOR:
742 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
743 *minor = ((*minor / 64) * PARTS_PER_DISK) +
744 EMULATED_HD_DISK_MINOR_OFFSET;
745 break;
746 case XEN_IDE1_MAJOR:
747 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
748 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
749 EMULATED_HD_DISK_MINOR_OFFSET;
750 break;
751 case XEN_SCSI_DISK0_MAJOR:
752 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
753 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
754 break;
755 case XEN_SCSI_DISK1_MAJOR:
756 case XEN_SCSI_DISK2_MAJOR:
757 case XEN_SCSI_DISK3_MAJOR:
758 case XEN_SCSI_DISK4_MAJOR:
759 case XEN_SCSI_DISK5_MAJOR:
760 case XEN_SCSI_DISK6_MAJOR:
761 case XEN_SCSI_DISK7_MAJOR:
762 *offset = (*minor / PARTS_PER_DISK) +
763 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
764 EMULATED_SD_DISK_NAME_OFFSET;
765 *minor = *minor +
766 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
767 EMULATED_SD_DISK_MINOR_OFFSET;
768 break;
769 case XEN_SCSI_DISK8_MAJOR:
770 case XEN_SCSI_DISK9_MAJOR:
771 case XEN_SCSI_DISK10_MAJOR:
772 case XEN_SCSI_DISK11_MAJOR:
773 case XEN_SCSI_DISK12_MAJOR:
774 case XEN_SCSI_DISK13_MAJOR:
775 case XEN_SCSI_DISK14_MAJOR:
776 case XEN_SCSI_DISK15_MAJOR:
777 *offset = (*minor / PARTS_PER_DISK) +
778 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
779 EMULATED_SD_DISK_NAME_OFFSET;
780 *minor = *minor +
781 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
782 EMULATED_SD_DISK_MINOR_OFFSET;
783 break;
784 case XENVBD_MAJOR:
785 *offset = *minor / PARTS_PER_DISK;
786 break;
787 default:
788 printk(KERN_WARNING "blkfront: your disk configuration is "
789 "incorrect, please use an xvd device instead\n");
790 return -ENODEV;
791 }
792 return 0;
793}
9f27ee59 794
e77c78c0
JB
795static char *encode_disk_name(char *ptr, unsigned int n)
796{
797 if (n >= 26)
798 ptr = encode_disk_name(ptr, n / 26 - 1);
799 *ptr = 'a' + n % 26;
800 return ptr + 1;
801}
802
9246b5f0
CL
803static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
804 struct blkfront_info *info,
7c4d7d71
SB
805 u16 vdisk_info, u16 sector_size,
806 unsigned int physical_sector_size)
9f27ee59
JF
807{
808 struct gendisk *gd;
809 int nr_minors = 1;
c80a4209 810 int err;
9246b5f0
CL
811 unsigned int offset;
812 int minor;
813 int nr_parts;
e77c78c0 814 char *ptr;
9f27ee59
JF
815
816 BUG_ON(info->gd != NULL);
817 BUG_ON(info->rq != NULL);
818
9246b5f0
CL
819 if ((info->vdevice>>EXT_SHIFT) > 1) {
820 /* this is above the extended range; something is wrong */
821 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
822 return -ENODEV;
823 }
824
825 if (!VDEV_IS_EXTENDED(info->vdevice)) {
c80a4209
SS
826 err = xen_translate_vdev(info->vdevice, &minor, &offset);
827 if (err)
828 return err;
829 nr_parts = PARTS_PER_DISK;
9246b5f0
CL
830 } else {
831 minor = BLKIF_MINOR_EXT(info->vdevice);
832 nr_parts = PARTS_PER_EXT_DISK;
c80a4209 833 offset = minor / nr_parts;
89153b5c 834 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
c80a4209
SS
835 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
836 "emulated IDE disks,\n\t choose an xvd device name"
837 "from xvde on\n", info->vdevice);
9246b5f0 838 }
e77c78c0
JB
839 if (minor >> MINORBITS) {
840 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
841 info->vdevice, minor);
842 return -ENODEV;
843 }
9246b5f0
CL
844
845 if ((minor % nr_parts) == 0)
846 nr_minors = nr_parts;
9f27ee59 847
0e345826
JB
848 err = xlbd_reserve_minors(minor, nr_minors);
849 if (err)
850 goto out;
851 err = -ENODEV;
852
9f27ee59
JF
853 gd = alloc_disk(nr_minors);
854 if (gd == NULL)
0e345826 855 goto release;
9f27ee59 856
e77c78c0
JB
857 strcpy(gd->disk_name, DEV_NAME);
858 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
859 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
860 if (nr_minors > 1)
861 *ptr = 0;
862 else
863 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
864 "%d", minor & (nr_parts - 1));
9f27ee59
JF
865
866 gd->major = XENVBD_MAJOR;
867 gd->first_minor = minor;
868 gd->fops = &xlvbd_block_fops;
869 gd->private_data = info;
870 gd->driverfs_dev = &(info->xbdev->dev);
871 set_capacity(gd, capacity);
872
7c4d7d71 873 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
402b27f9
RPM
874 info->max_indirect_segments ? :
875 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
9f27ee59 876 del_gendisk(gd);
0e345826 877 goto release;
9f27ee59
JF
878 }
879
880 info->rq = gd->queue;
881 info->gd = gd;
882
4913efe4 883 xlvbd_flush(info);
9f27ee59
JF
884
885 if (vdisk_info & VDISK_READONLY)
886 set_disk_ro(gd, 1);
887
888 if (vdisk_info & VDISK_REMOVABLE)
889 gd->flags |= GENHD_FL_REMOVABLE;
890
891 if (vdisk_info & VDISK_CDROM)
892 gd->flags |= GENHD_FL_CD;
893
894 return 0;
895
0e345826
JB
896 release:
897 xlbd_release_minors(minor, nr_minors);
9f27ee59
JF
898 out:
899 return err;
900}
901
a66b5aeb
DS
902static void xlvbd_release_gendisk(struct blkfront_info *info)
903{
904 unsigned int minor, nr_minors;
905 unsigned long flags;
906
907 if (info->rq == NULL)
908 return;
909
3467811e 910 spin_lock_irqsave(&info->io_lock, flags);
a66b5aeb
DS
911
912 /* No more blkif_request(). */
913 blk_stop_queue(info->rq);
914
915 /* No more gnttab callback work. */
916 gnttab_cancel_free_callback(&info->callback);
3467811e 917 spin_unlock_irqrestore(&info->io_lock, flags);
a66b5aeb
DS
918
919 /* Flush gnttab callback work. Must be done with no locks held. */
43829731 920 flush_work(&info->work);
a66b5aeb
DS
921
922 del_gendisk(info->gd);
923
924 minor = info->gd->first_minor;
925 nr_minors = info->gd->minors;
926 xlbd_release_minors(minor, nr_minors);
927
928 blk_cleanup_queue(info->rq);
929 info->rq = NULL;
930
931 put_disk(info->gd);
932 info->gd = NULL;
933}
934
9f27ee59
JF
935static void kick_pending_request_queues(struct blkfront_info *info)
936{
937 if (!RING_FULL(&info->ring)) {
938 /* Re-enable calldowns. */
939 blk_start_queue(info->rq);
940 /* Kick things off immediately. */
941 do_blkif_request(info->rq);
942 }
943}
944
945static void blkif_restart_queue(struct work_struct *work)
946{
947 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
948
3467811e 949 spin_lock_irq(&info->io_lock);
9f27ee59
JF
950 if (info->connected == BLKIF_STATE_CONNECTED)
951 kick_pending_request_queues(info);
3467811e 952 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
953}
954
955static void blkif_free(struct blkfront_info *info, int suspend)
956{
155b7edb
RPM
957 struct grant *persistent_gnt;
958 struct grant *n;
402b27f9 959 int i, j, segs;
0a8704a5 960
9f27ee59 961 /* Prevent new requests being issued until we fix things up. */
3467811e 962 spin_lock_irq(&info->io_lock);
9f27ee59
JF
963 info->connected = suspend ?
964 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
965 /* No more blkif_request(). */
966 if (info->rq)
967 blk_stop_queue(info->rq);
0a8704a5
RPM
968
969 /* Remove all persistent grants */
bfe11d6d 970 if (!list_empty(&info->grants)) {
155b7edb 971 list_for_each_entry_safe(persistent_gnt, n,
bfe11d6d 972 &info->grants, node) {
155b7edb 973 list_del(&persistent_gnt->node);
9c1e050c
RPM
974 if (persistent_gnt->gref != GRANT_INVALID_REF) {
975 gnttab_end_foreign_access(persistent_gnt->gref,
976 0, 0UL);
977 info->persistent_gnts_c--;
978 }
bfe11d6d
RPM
979 if (info->feature_persistent)
980 __free_page(pfn_to_page(persistent_gnt->pfn));
155b7edb 981 kfree(persistent_gnt);
0a8704a5 982 }
0a8704a5 983 }
9c1e050c 984 BUG_ON(info->persistent_gnts_c != 0);
0a8704a5 985
bfe11d6d
RPM
986 /*
987 * Remove indirect pages, this only happens when using indirect
988 * descriptors but not persistent grants
989 */
990 if (!list_empty(&info->indirect_pages)) {
991 struct page *indirect_page, *n;
992
993 BUG_ON(info->feature_persistent);
994 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
995 list_del(&indirect_page->lru);
996 __free_page(indirect_page);
997 }
998 }
999
86839c56 1000 for (i = 0; i < BLK_RING_SIZE(info); i++) {
402b27f9
RPM
1001 /*
1002 * Clear persistent grants present in requests already
1003 * on the shared ring
1004 */
1005 if (!info->shadow[i].request)
1006 goto free_shadow;
1007
1008 segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1009 info->shadow[i].req.u.indirect.nr_segments :
1010 info->shadow[i].req.u.rw.nr_segments;
1011 for (j = 0; j < segs; j++) {
1012 persistent_gnt = info->shadow[i].grants_used[j];
1013 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
bfe11d6d
RPM
1014 if (info->feature_persistent)
1015 __free_page(pfn_to_page(persistent_gnt->pfn));
402b27f9
RPM
1016 kfree(persistent_gnt);
1017 }
1018
1019 if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1020 /*
1021 * If this is not an indirect operation don't try to
1022 * free indirect segments
1023 */
1024 goto free_shadow;
1025
1026 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1027 persistent_gnt = info->shadow[i].indirect_grants[j];
1028 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1029 __free_page(pfn_to_page(persistent_gnt->pfn));
1030 kfree(persistent_gnt);
1031 }
1032
1033free_shadow:
1034 kfree(info->shadow[i].grants_used);
1035 info->shadow[i].grants_used = NULL;
1036 kfree(info->shadow[i].indirect_grants);
1037 info->shadow[i].indirect_grants = NULL;
b7649158
RPM
1038 kfree(info->shadow[i].sg);
1039 info->shadow[i].sg = NULL;
402b27f9
RPM
1040 }
1041
9f27ee59
JF
1042 /* No more gnttab callback work. */
1043 gnttab_cancel_free_callback(&info->callback);
3467811e 1044 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
1045
1046 /* Flush gnttab callback work. Must be done with no locks held. */
43829731 1047 flush_work(&info->work);
9f27ee59
JF
1048
1049 /* Free resources associated with old device channel. */
86839c56
BL
1050 for (i = 0; i < info->nr_ring_pages; i++) {
1051 if (info->ring_ref[i] != GRANT_INVALID_REF) {
1052 gnttab_end_foreign_access(info->ring_ref[i], 0, 0);
1053 info->ring_ref[i] = GRANT_INVALID_REF;
1054 }
9f27ee59 1055 }
86839c56
BL
1056 free_pages((unsigned long)info->ring.sring, get_order(info->nr_ring_pages * PAGE_SIZE));
1057 info->ring.sring = NULL;
1058
9f27ee59
JF
1059 if (info->irq)
1060 unbind_from_irqhandler(info->irq, info);
1061 info->evtchn = info->irq = 0;
1062
1063}
1064
0a8704a5
RPM
1065static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
1066 struct blkif_response *bret)
9f27ee59 1067{
d62f6918 1068 int i = 0;
b7649158 1069 struct scatterlist *sg;
0a8704a5
RPM
1070 char *bvec_data;
1071 void *shared_data;
402b27f9
RPM
1072 int nseg;
1073
1074 nseg = s->req.operation == BLKIF_OP_INDIRECT ?
1075 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
0a8704a5 1076
bfe11d6d 1077 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
b7649158
RPM
1078 for_each_sg(s->sg, sg, nseg, i) {
1079 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
0a8704a5
RPM
1080 shared_data = kmap_atomic(
1081 pfn_to_page(s->grants_used[i]->pfn));
b7649158
RPM
1082 bvec_data = kmap_atomic(sg_page(sg));
1083 memcpy(bvec_data + sg->offset,
1084 shared_data + sg->offset,
1085 sg->length);
1086 kunmap_atomic(bvec_data);
0a8704a5 1087 kunmap_atomic(shared_data);
0a8704a5
RPM
1088 }
1089 }
1090 /* Add the persistent grant into the list of free grants */
402b27f9 1091 for (i = 0; i < nseg; i++) {
fbe363c4
RPM
1092 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1093 /*
1094 * If the grant is still mapped by the backend (the
1095 * backend has chosen to make this grant persistent)
1096 * we add it at the head of the list, so it will be
1097 * reused first.
1098 */
bfe11d6d
RPM
1099 if (!info->feature_persistent)
1100 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1101 s->grants_used[i]->gref);
1102 list_add(&s->grants_used[i]->node, &info->grants);
fbe363c4
RPM
1103 info->persistent_gnts_c++;
1104 } else {
1105 /*
1106 * If the grant is not mapped by the backend we end the
1107 * foreign access and add it to the tail of the list,
1108 * so it will not be picked again unless we run out of
1109 * persistent grants.
1110 */
1111 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1112 s->grants_used[i]->gref = GRANT_INVALID_REF;
bfe11d6d 1113 list_add_tail(&s->grants_used[i]->node, &info->grants);
fbe363c4 1114 }
0a8704a5 1115 }
402b27f9
RPM
1116 if (s->req.operation == BLKIF_OP_INDIRECT) {
1117 for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
fbe363c4 1118 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
bfe11d6d
RPM
1119 if (!info->feature_persistent)
1120 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1121 s->indirect_grants[i]->gref);
1122 list_add(&s->indirect_grants[i]->node, &info->grants);
fbe363c4
RPM
1123 info->persistent_gnts_c++;
1124 } else {
bfe11d6d
RPM
1125 struct page *indirect_page;
1126
fbe363c4 1127 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
bfe11d6d
RPM
1128 /*
1129 * Add the used indirect page back to the list of
1130 * available pages for indirect grefs.
1131 */
7b076750
BL
1132 if (!info->feature_persistent) {
1133 indirect_page = pfn_to_page(s->indirect_grants[i]->pfn);
1134 list_add(&indirect_page->lru, &info->indirect_pages);
1135 }
fbe363c4 1136 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
bfe11d6d 1137 list_add_tail(&s->indirect_grants[i]->node, &info->grants);
fbe363c4 1138 }
402b27f9
RPM
1139 }
1140 }
9f27ee59
JF
1141}
1142
1143static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1144{
1145 struct request *req;
1146 struct blkif_response *bret;
1147 RING_IDX i, rp;
1148 unsigned long flags;
1149 struct blkfront_info *info = (struct blkfront_info *)dev_id;
f530f036 1150 int error;
9f27ee59 1151
3467811e 1152 spin_lock_irqsave(&info->io_lock, flags);
9f27ee59
JF
1153
1154 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
3467811e 1155 spin_unlock_irqrestore(&info->io_lock, flags);
9f27ee59
JF
1156 return IRQ_HANDLED;
1157 }
1158
1159 again:
1160 rp = info->ring.sring->rsp_prod;
1161 rmb(); /* Ensure we see queued responses up to 'rp'. */
1162
1163 for (i = info->ring.rsp_cons; i != rp; i++) {
1164 unsigned long id;
9f27ee59
JF
1165
1166 bret = RING_GET_RESPONSE(&info->ring, i);
1167 id = bret->id;
6878c32e
KRW
1168 /*
1169 * The backend has messed up and given us an id that we would
1170 * never have given to it (we stamp it up to BLK_RING_SIZE -
1171 * look in get_id_from_freelist.
1172 */
86839c56 1173 if (id >= BLK_RING_SIZE(info)) {
6878c32e
KRW
1174 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1175 info->gd->disk_name, op_name(bret->operation), id);
1176 /* We can't safely get the 'struct request' as
1177 * the id is busted. */
1178 continue;
1179 }
a945b980 1180 req = info->shadow[id].request;
9f27ee59 1181
5ea42986 1182 if (bret->operation != BLKIF_OP_DISCARD)
0a8704a5 1183 blkif_completion(&info->shadow[id], info, bret);
9f27ee59 1184
6878c32e
KRW
1185 if (add_id_to_freelist(info, id)) {
1186 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1187 info->gd->disk_name, op_name(bret->operation), id);
1188 continue;
1189 }
9f27ee59 1190
f530f036 1191 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
9f27ee59 1192 switch (bret->operation) {
ed30bf31
LD
1193 case BLKIF_OP_DISCARD:
1194 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1195 struct request_queue *rq = info->rq;
6878c32e
KRW
1196 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1197 info->gd->disk_name, op_name(bret->operation));
ed30bf31
LD
1198 error = -EOPNOTSUPP;
1199 info->feature_discard = 0;
5ea42986 1200 info->feature_secdiscard = 0;
ed30bf31 1201 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
5ea42986 1202 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
ed30bf31
LD
1203 }
1204 __blk_end_request_all(req, error);
1205 break;
edf6ef59 1206 case BLKIF_OP_FLUSH_DISKCACHE:
9f27ee59
JF
1207 case BLKIF_OP_WRITE_BARRIER:
1208 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
6878c32e
KRW
1209 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1210 info->gd->disk_name, op_name(bret->operation));
f530f036 1211 error = -EOPNOTSUPP;
dcb8baec
JF
1212 }
1213 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
97e36834 1214 info->shadow[id].req.u.rw.nr_segments == 0)) {
6878c32e
KRW
1215 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1216 info->gd->disk_name, op_name(bret->operation));
dcb8baec
JF
1217 error = -EOPNOTSUPP;
1218 }
1219 if (unlikely(error)) {
1220 if (error == -EOPNOTSUPP)
1221 error = 0;
4913efe4
TH
1222 info->feature_flush = 0;
1223 xlvbd_flush(info);
9f27ee59
JF
1224 }
1225 /* fall through */
1226 case BLKIF_OP_READ:
1227 case BLKIF_OP_WRITE:
1228 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1229 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1230 "request: %x\n", bret->status);
1231
40cbbb78 1232 __blk_end_request_all(req, error);
9f27ee59
JF
1233 break;
1234 default:
1235 BUG();
1236 }
1237 }
1238
1239 info->ring.rsp_cons = i;
1240
1241 if (i != info->ring.req_prod_pvt) {
1242 int more_to_do;
1243 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1244 if (more_to_do)
1245 goto again;
1246 } else
1247 info->ring.sring->rsp_event = i + 1;
1248
1249 kick_pending_request_queues(info);
1250
3467811e 1251 spin_unlock_irqrestore(&info->io_lock, flags);
9f27ee59
JF
1252
1253 return IRQ_HANDLED;
1254}
1255
1256
1257static int setup_blkring(struct xenbus_device *dev,
1258 struct blkfront_info *info)
1259{
1260 struct blkif_sring *sring;
86839c56
BL
1261 int err, i;
1262 unsigned long ring_size = info->nr_ring_pages * PAGE_SIZE;
1263 grant_ref_t gref[XENBUS_MAX_RING_PAGES];
9f27ee59 1264
86839c56
BL
1265 for (i = 0; i < info->nr_ring_pages; i++)
1266 info->ring_ref[i] = GRANT_INVALID_REF;
9f27ee59 1267
86839c56
BL
1268 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1269 get_order(ring_size));
9f27ee59
JF
1270 if (!sring) {
1271 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1272 return -ENOMEM;
1273 }
1274 SHARED_RING_INIT(sring);
86839c56 1275 FRONT_RING_INIT(&info->ring, sring, ring_size);
9e973e64 1276
86839c56 1277 err = xenbus_grant_ring(dev, info->ring.sring, info->nr_ring_pages, gref);
9f27ee59 1278 if (err < 0) {
86839c56 1279 free_pages((unsigned long)sring, get_order(ring_size));
9f27ee59
JF
1280 info->ring.sring = NULL;
1281 goto fail;
1282 }
86839c56
BL
1283 for (i = 0; i < info->nr_ring_pages; i++)
1284 info->ring_ref[i] = gref[i];
9f27ee59
JF
1285
1286 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1287 if (err)
1288 goto fail;
1289
89c30f16
TT
1290 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1291 "blkif", info);
9f27ee59
JF
1292 if (err <= 0) {
1293 xenbus_dev_fatal(dev, err,
1294 "bind_evtchn_to_irqhandler failed");
1295 goto fail;
1296 }
1297 info->irq = err;
1298
1299 return 0;
1300fail:
1301 blkif_free(info, 0);
1302 return err;
1303}
1304
1305
1306/* Common code used when first setting up, and when resuming. */
203fd61f 1307static int talk_to_blkback(struct xenbus_device *dev,
9f27ee59
JF
1308 struct blkfront_info *info)
1309{
1310 const char *message = NULL;
1311 struct xenbus_transaction xbt;
86839c56
BL
1312 int err, i;
1313 unsigned int max_page_order = 0;
1314 unsigned int ring_page_order = 0;
1315
1316 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1317 "max-ring-page-order", "%u", &max_page_order);
1318 if (err != 1)
1319 info->nr_ring_pages = 1;
1320 else {
1321 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1322 info->nr_ring_pages = 1 << ring_page_order;
1323 }
9f27ee59
JF
1324
1325 /* Create shared ring, alloc event channel. */
1326 err = setup_blkring(dev, info);
1327 if (err)
1328 goto out;
1329
1330again:
1331 err = xenbus_transaction_start(&xbt);
1332 if (err) {
1333 xenbus_dev_fatal(dev, err, "starting transaction");
1334 goto destroy_blkring;
1335 }
1336
86839c56
BL
1337 if (info->nr_ring_pages == 1) {
1338 err = xenbus_printf(xbt, dev->nodename,
1339 "ring-ref", "%u", info->ring_ref[0]);
1340 if (err) {
1341 message = "writing ring-ref";
1342 goto abort_transaction;
1343 }
1344 } else {
1345 err = xenbus_printf(xbt, dev->nodename,
1346 "ring-page-order", "%u", ring_page_order);
1347 if (err) {
1348 message = "writing ring-page-order";
1349 goto abort_transaction;
1350 }
1351
1352 for (i = 0; i < info->nr_ring_pages; i++) {
1353 char ring_ref_name[RINGREF_NAME_LEN];
1354
1355 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1356 err = xenbus_printf(xbt, dev->nodename, ring_ref_name,
1357 "%u", info->ring_ref[i]);
1358 if (err) {
1359 message = "writing ring-ref";
1360 goto abort_transaction;
1361 }
1362 }
9f27ee59
JF
1363 }
1364 err = xenbus_printf(xbt, dev->nodename,
1365 "event-channel", "%u", info->evtchn);
1366 if (err) {
1367 message = "writing event-channel";
1368 goto abort_transaction;
1369 }
3e334239
MA
1370 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1371 XEN_IO_PROTO_ABI_NATIVE);
1372 if (err) {
1373 message = "writing protocol";
1374 goto abort_transaction;
1375 }
0a8704a5 1376 err = xenbus_printf(xbt, dev->nodename,
cb5bd4d1 1377 "feature-persistent", "%u", 1);
0a8704a5
RPM
1378 if (err)
1379 dev_warn(&dev->dev,
1380 "writing persistent grants feature to xenbus");
9f27ee59
JF
1381
1382 err = xenbus_transaction_end(xbt, 0);
1383 if (err) {
1384 if (err == -EAGAIN)
1385 goto again;
1386 xenbus_dev_fatal(dev, err, "completing transaction");
1387 goto destroy_blkring;
1388 }
1389
86839c56
BL
1390 for (i = 0; i < BLK_RING_SIZE(info); i++)
1391 info->shadow[i].req.u.rw.id = i+1;
1392 info->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
9f27ee59
JF
1393 xenbus_switch_state(dev, XenbusStateInitialised);
1394
1395 return 0;
1396
1397 abort_transaction:
1398 xenbus_transaction_end(xbt, 1);
1399 if (message)
1400 xenbus_dev_fatal(dev, err, "%s", message);
1401 destroy_blkring:
1402 blkif_free(info, 0);
1403 out:
1404 return err;
1405}
1406
9f27ee59
JF
1407/**
1408 * Entry point to this code when a new device is created. Allocate the basic
1409 * structures and the ring buffer for communication with the backend, and
1410 * inform the backend of the appropriate details for those. Switch to
1411 * Initialised state.
1412 */
1413static int blkfront_probe(struct xenbus_device *dev,
1414 const struct xenbus_device_id *id)
1415{
86839c56 1416 int err, vdevice;
9f27ee59
JF
1417 struct blkfront_info *info;
1418
1419 /* FIXME: Use dynamic device id if this is not set. */
1420 err = xenbus_scanf(XBT_NIL, dev->nodename,
1421 "virtual-device", "%i", &vdevice);
1422 if (err != 1) {
9246b5f0
CL
1423 /* go looking in the extended area instead */
1424 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1425 "%i", &vdevice);
1426 if (err != 1) {
1427 xenbus_dev_fatal(dev, err, "reading virtual-device");
1428 return err;
1429 }
9f27ee59
JF
1430 }
1431
b98a409b
SS
1432 if (xen_hvm_domain()) {
1433 char *type;
1434 int len;
1435 /* no unplug has been done: do not hook devices != xen vbds */
51c71a3b 1436 if (xen_has_pv_and_legacy_disk_devices()) {
b98a409b
SS
1437 int major;
1438
1439 if (!VDEV_IS_EXTENDED(vdevice))
1440 major = BLKIF_MAJOR(vdevice);
1441 else
1442 major = XENVBD_MAJOR;
1443
1444 if (major != XENVBD_MAJOR) {
1445 printk(KERN_INFO
1446 "%s: HVM does not support vbd %d as xen block device\n",
02f1f217 1447 __func__, vdevice);
b98a409b
SS
1448 return -ENODEV;
1449 }
1450 }
1451 /* do not create a PV cdrom device if we are an HVM guest */
1452 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1453 if (IS_ERR(type))
1454 return -ENODEV;
1455 if (strncmp(type, "cdrom", 5) == 0) {
1456 kfree(type);
c1c5413a
SS
1457 return -ENODEV;
1458 }
b98a409b 1459 kfree(type);
c1c5413a 1460 }
9f27ee59
JF
1461 info = kzalloc(sizeof(*info), GFP_KERNEL);
1462 if (!info) {
1463 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1464 return -ENOMEM;
1465 }
1466
b70f5fa0 1467 mutex_init(&info->mutex);
3467811e 1468 spin_lock_init(&info->io_lock);
9f27ee59
JF
1469 info->xbdev = dev;
1470 info->vdevice = vdevice;
bfe11d6d
RPM
1471 INIT_LIST_HEAD(&info->grants);
1472 INIT_LIST_HEAD(&info->indirect_pages);
0a8704a5 1473 info->persistent_gnts_c = 0;
9f27ee59
JF
1474 info->connected = BLKIF_STATE_DISCONNECTED;
1475 INIT_WORK(&info->work, blkif_restart_queue);
1476
9f27ee59
JF
1477 /* Front end dir is a number, which is used as the id. */
1478 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
a1b4b12b 1479 dev_set_drvdata(&dev->dev, info);
9f27ee59 1480
9f27ee59
JF
1481 return 0;
1482}
1483
402b27f9
RPM
1484static void split_bio_end(struct bio *bio, int error)
1485{
1486 struct split_bio *split_bio = bio->bi_private;
1487
1488 if (error)
1489 split_bio->err = error;
1490
1491 if (atomic_dec_and_test(&split_bio->pending)) {
1492 split_bio->bio->bi_phys_segments = 0;
1493 bio_endio(split_bio->bio, split_bio->err);
1494 kfree(split_bio);
1495 }
1496 bio_put(bio);
1497}
9f27ee59
JF
1498
1499static int blkif_recover(struct blkfront_info *info)
1500{
1501 int i;
402b27f9 1502 struct request *req, *n;
9f27ee59 1503 struct blk_shadow *copy;
402b27f9
RPM
1504 int rc;
1505 struct bio *bio, *cloned_bio;
1506 struct bio_list bio_list, merge_bio;
1507 unsigned int segs, offset;
1508 int pending, size;
1509 struct split_bio *split_bio;
1510 struct list_head requests;
9f27ee59
JF
1511
1512 /* Stage 1: Make a safe copy of the shadow state. */
29d0b218 1513 copy = kmemdup(info->shadow, sizeof(info->shadow),
a144ff09 1514 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
9f27ee59
JF
1515 if (!copy)
1516 return -ENOMEM;
9f27ee59
JF
1517
1518 /* Stage 2: Set up free list. */
1519 memset(&info->shadow, 0, sizeof(info->shadow));
86839c56 1520 for (i = 0; i < BLK_RING_SIZE(info); i++)
97e36834 1521 info->shadow[i].req.u.rw.id = i+1;
9f27ee59 1522 info->shadow_free = info->ring.req_prod_pvt;
86839c56 1523 info->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
9f27ee59 1524
d50babbe 1525 rc = blkfront_gather_backend_features(info);
402b27f9
RPM
1526 if (rc) {
1527 kfree(copy);
1528 return rc;
1529 }
1530
1531 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1532 blk_queue_max_segments(info->rq, segs);
1533 bio_list_init(&bio_list);
1534 INIT_LIST_HEAD(&requests);
86839c56 1535 for (i = 0; i < BLK_RING_SIZE(info); i++) {
9f27ee59 1536 /* Not in use? */
a945b980 1537 if (!copy[i].request)
9f27ee59
JF
1538 continue;
1539
402b27f9
RPM
1540 /*
1541 * Get the bios in the request so we can re-queue them.
1542 */
1543 if (copy[i].request->cmd_flags &
1544 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1545 /*
1546 * Flush operations don't contain bios, so
1547 * we need to requeue the whole request
1548 */
1549 list_add(&copy[i].request->queuelist, &requests);
1550 continue;
5ea42986 1551 }
402b27f9
RPM
1552 merge_bio.head = copy[i].request->bio;
1553 merge_bio.tail = copy[i].request->biotail;
1554 bio_list_merge(&bio_list, &merge_bio);
1555 copy[i].request->bio = NULL;
3bb8c98e 1556 blk_end_request_all(copy[i].request, 0);
9f27ee59
JF
1557 }
1558
1559 kfree(copy);
1560
402b27f9
RPM
1561 /*
1562 * Empty the queue, this is important because we might have
1563 * requests in the queue with more segments than what we
1564 * can handle now.
1565 */
1566 spin_lock_irq(&info->io_lock);
1567 while ((req = blk_fetch_request(info->rq)) != NULL) {
1568 if (req->cmd_flags &
1569 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1570 list_add(&req->queuelist, &requests);
1571 continue;
1572 }
1573 merge_bio.head = req->bio;
1574 merge_bio.tail = req->biotail;
1575 bio_list_merge(&bio_list, &merge_bio);
1576 req->bio = NULL;
1577 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
1578 pr_alert("diskcache flush request found!\n");
3bb8c98e 1579 __blk_end_request_all(req, 0);
402b27f9
RPM
1580 }
1581 spin_unlock_irq(&info->io_lock);
1582
9f27ee59
JF
1583 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1584
3467811e 1585 spin_lock_irq(&info->io_lock);
9f27ee59
JF
1586
1587 /* Now safe for us to use the shared ring */
1588 info->connected = BLKIF_STATE_CONNECTED;
1589
9f27ee59
JF
1590 /* Kick any other new requests queued since we resumed */
1591 kick_pending_request_queues(info);
1592
402b27f9
RPM
1593 list_for_each_entry_safe(req, n, &requests, queuelist) {
1594 /* Requeue pending requests (flush or discard) */
1595 list_del_init(&req->queuelist);
1596 BUG_ON(req->nr_phys_segments > segs);
1597 blk_requeue_request(info->rq, req);
1598 }
3467811e 1599 spin_unlock_irq(&info->io_lock);
9f27ee59 1600
402b27f9
RPM
1601 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1602 /* Traverse the list of pending bios and re-queue them */
1603 if (bio_segments(bio) > segs) {
1604 /*
1605 * This bio has more segments than what we can
1606 * handle, we have to split it.
1607 */
1608 pending = (bio_segments(bio) + segs - 1) / segs;
1609 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1610 BUG_ON(split_bio == NULL);
1611 atomic_set(&split_bio->pending, pending);
1612 split_bio->bio = bio;
1613 for (i = 0; i < pending; i++) {
1614 offset = (i * segs * PAGE_SIZE) >> 9;
1615 size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
4f024f37 1616 (unsigned int)bio_sectors(bio) - offset);
402b27f9
RPM
1617 cloned_bio = bio_clone(bio, GFP_NOIO);
1618 BUG_ON(cloned_bio == NULL);
6678d83f 1619 bio_trim(cloned_bio, offset, size);
402b27f9
RPM
1620 cloned_bio->bi_private = split_bio;
1621 cloned_bio->bi_end_io = split_bio_end;
1622 submit_bio(cloned_bio->bi_rw, cloned_bio);
1623 }
1624 /*
1625 * Now we have to wait for all those smaller bios to
1626 * end, so we can also end the "parent" bio.
1627 */
1628 continue;
1629 }
1630 /* We don't need to split this bio */
1631 submit_bio(bio->bi_rw, bio);
1632 }
1633
9f27ee59
JF
1634 return 0;
1635}
1636
1637/**
1638 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1639 * driver restart. We tear down our blkif structure and recreate it, but
1640 * leave the device-layer structures intact so that this is transparent to the
1641 * rest of the kernel.
1642 */
1643static int blkfront_resume(struct xenbus_device *dev)
1644{
a1b4b12b 1645 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
9f27ee59
JF
1646 int err;
1647
1648 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1649
1650 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1651
203fd61f 1652 err = talk_to_blkback(dev, info);
402b27f9
RPM
1653
1654 /*
1655 * We have to wait for the backend to switch to
1656 * connected state, since we want to read which
1657 * features it supports.
1658 */
9f27ee59
JF
1659
1660 return err;
1661}
1662
b70f5fa0
DS
1663static void
1664blkfront_closing(struct blkfront_info *info)
1665{
1666 struct xenbus_device *xbdev = info->xbdev;
1667 struct block_device *bdev = NULL;
1668
1669 mutex_lock(&info->mutex);
1670
1671 if (xbdev->state == XenbusStateClosing) {
1672 mutex_unlock(&info->mutex);
1673 return;
1674 }
1675
1676 if (info->gd)
1677 bdev = bdget_disk(info->gd, 0);
1678
1679 mutex_unlock(&info->mutex);
1680
1681 if (!bdev) {
1682 xenbus_frontend_closed(xbdev);
1683 return;
1684 }
1685
1686 mutex_lock(&bdev->bd_mutex);
1687
7b32d104 1688 if (bdev->bd_openers) {
b70f5fa0
DS
1689 xenbus_dev_error(xbdev, -EBUSY,
1690 "Device in use; refusing to close");
1691 xenbus_switch_state(xbdev, XenbusStateClosing);
1692 } else {
1693 xlvbd_release_gendisk(info);
1694 xenbus_frontend_closed(xbdev);
1695 }
1696
1697 mutex_unlock(&bdev->bd_mutex);
1698 bdput(bdev);
1699}
9f27ee59 1700
ed30bf31
LD
1701static void blkfront_setup_discard(struct blkfront_info *info)
1702{
1703 int err;
ed30bf31
LD
1704 unsigned int discard_granularity;
1705 unsigned int discard_alignment;
5ea42986 1706 unsigned int discard_secure;
ed30bf31 1707
1c8cad6c
OH
1708 info->feature_discard = 1;
1709 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1710 "discard-granularity", "%u", &discard_granularity,
1711 "discard-alignment", "%u", &discard_alignment,
1712 NULL);
1713 if (!err) {
1714 info->discard_granularity = discard_granularity;
1715 info->discard_alignment = discard_alignment;
1716 }
1717 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1718 "discard-secure", "%d", &discard_secure,
1719 NULL);
1720 if (!err)
1721 info->feature_secdiscard = !!discard_secure;
ed30bf31
LD
1722}
1723
402b27f9
RPM
1724static int blkfront_setup_indirect(struct blkfront_info *info)
1725{
d50babbe 1726 unsigned int segs;
402b27f9
RPM
1727 int err, i;
1728
d50babbe 1729 if (info->max_indirect_segments == 0)
402b27f9 1730 segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
d50babbe 1731 else
402b27f9 1732 segs = info->max_indirect_segments;
402b27f9 1733
86839c56 1734 err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE(info));
402b27f9
RPM
1735 if (err)
1736 goto out_of_memory;
1737
bfe11d6d
RPM
1738 if (!info->feature_persistent && info->max_indirect_segments) {
1739 /*
1740 * We are using indirect descriptors but not persistent
1741 * grants, we need to allocate a set of pages that can be
1742 * used for mapping indirect grefs
1743 */
86839c56 1744 int num = INDIRECT_GREFS(segs) * BLK_RING_SIZE(info);
bfe11d6d
RPM
1745
1746 BUG_ON(!list_empty(&info->indirect_pages));
1747 for (i = 0; i < num; i++) {
1748 struct page *indirect_page = alloc_page(GFP_NOIO);
1749 if (!indirect_page)
1750 goto out_of_memory;
1751 list_add(&indirect_page->lru, &info->indirect_pages);
1752 }
1753 }
1754
86839c56 1755 for (i = 0; i < BLK_RING_SIZE(info); i++) {
402b27f9
RPM
1756 info->shadow[i].grants_used = kzalloc(
1757 sizeof(info->shadow[i].grants_used[0]) * segs,
1758 GFP_NOIO);
b7649158 1759 info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * segs, GFP_NOIO);
402b27f9
RPM
1760 if (info->max_indirect_segments)
1761 info->shadow[i].indirect_grants = kzalloc(
1762 sizeof(info->shadow[i].indirect_grants[0]) *
1763 INDIRECT_GREFS(segs),
1764 GFP_NOIO);
1765 if ((info->shadow[i].grants_used == NULL) ||
b7649158 1766 (info->shadow[i].sg == NULL) ||
402b27f9
RPM
1767 (info->max_indirect_segments &&
1768 (info->shadow[i].indirect_grants == NULL)))
1769 goto out_of_memory;
b7649158 1770 sg_init_table(info->shadow[i].sg, segs);
402b27f9
RPM
1771 }
1772
1773
1774 return 0;
1775
1776out_of_memory:
86839c56 1777 for (i = 0; i < BLK_RING_SIZE(info); i++) {
402b27f9
RPM
1778 kfree(info->shadow[i].grants_used);
1779 info->shadow[i].grants_used = NULL;
b7649158
RPM
1780 kfree(info->shadow[i].sg);
1781 info->shadow[i].sg = NULL;
402b27f9
RPM
1782 kfree(info->shadow[i].indirect_grants);
1783 info->shadow[i].indirect_grants = NULL;
1784 }
bfe11d6d
RPM
1785 if (!list_empty(&info->indirect_pages)) {
1786 struct page *indirect_page, *n;
1787 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
1788 list_del(&indirect_page->lru);
1789 __free_page(indirect_page);
1790 }
1791 }
402b27f9
RPM
1792 return -ENOMEM;
1793}
1794
d50babbe
BL
1795/*
1796 * Gather all backend feature-*
1797 */
1798static int blkfront_gather_backend_features(struct blkfront_info *info)
1799{
1800 int err;
1801 int barrier, flush, discard, persistent;
1802 unsigned int indirect_segments;
1803
1804 info->feature_flush = 0;
1805
1806 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1807 "feature-barrier", "%d", &barrier,
1808 NULL);
1809
1810 /*
1811 * If there's no "feature-barrier" defined, then it means
1812 * we're dealing with a very old backend which writes
1813 * synchronously; nothing to do.
1814 *
1815 * If there are barriers, then we use flush.
1816 */
1817 if (!err && barrier)
1818 info->feature_flush = REQ_FLUSH | REQ_FUA;
1819 /*
1820 * And if there is "feature-flush-cache" use that above
1821 * barriers.
1822 */
1823 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1824 "feature-flush-cache", "%d", &flush,
1825 NULL);
1826
1827 if (!err && flush)
1828 info->feature_flush = REQ_FLUSH;
1829
1830 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1831 "feature-discard", "%d", &discard,
1832 NULL);
1833
1834 if (!err && discard)
1835 blkfront_setup_discard(info);
1836
1837 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1838 "feature-persistent", "%u", &persistent,
1839 NULL);
1840 if (err)
1841 info->feature_persistent = 0;
1842 else
1843 info->feature_persistent = persistent;
1844
1845 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1846 "feature-max-indirect-segments", "%u", &indirect_segments,
1847 NULL);
1848 if (err)
1849 info->max_indirect_segments = 0;
1850 else
1851 info->max_indirect_segments = min(indirect_segments,
1852 xen_blkif_max_segments);
1853
1854 return blkfront_setup_indirect(info);
1855}
1856
9f27ee59
JF
1857/*
1858 * Invoked when the backend is finally 'ready' (and has told produced
1859 * the details about the physical device - #sectors, size, etc).
1860 */
1861static void blkfront_connect(struct blkfront_info *info)
1862{
1863 unsigned long long sectors;
1864 unsigned long sector_size;
7c4d7d71 1865 unsigned int physical_sector_size;
9f27ee59
JF
1866 unsigned int binfo;
1867 int err;
1868
1fa73be6
S
1869 switch (info->connected) {
1870 case BLKIF_STATE_CONNECTED:
1871 /*
1872 * Potentially, the back-end may be signalling
1873 * a capacity change; update the capacity.
1874 */
1875 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1876 "sectors", "%Lu", &sectors);
1877 if (XENBUS_EXIST_ERR(err))
1878 return;
1879 printk(KERN_INFO "Setting capacity to %Lu\n",
1880 sectors);
1881 set_capacity(info->gd, sectors);
2def141e 1882 revalidate_disk(info->gd);
1fa73be6 1883
402b27f9 1884 return;
1fa73be6 1885 case BLKIF_STATE_SUSPENDED:
402b27f9
RPM
1886 /*
1887 * If we are recovering from suspension, we need to wait
1888 * for the backend to announce it's features before
1889 * reconnecting, at least we need to know if the backend
1890 * supports indirect descriptors, and how many.
1891 */
1892 blkif_recover(info);
9f27ee59
JF
1893 return;
1894
b4dddb49
JF
1895 default:
1896 break;
1fa73be6 1897 }
9f27ee59
JF
1898
1899 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1900 __func__, info->xbdev->otherend);
1901
1902 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1903 "sectors", "%llu", &sectors,
1904 "info", "%u", &binfo,
1905 "sector-size", "%lu", &sector_size,
1906 NULL);
1907 if (err) {
1908 xenbus_dev_fatal(info->xbdev, err,
1909 "reading backend fields at %s",
1910 info->xbdev->otherend);
1911 return;
1912 }
1913
7c4d7d71
SB
1914 /*
1915 * physcial-sector-size is a newer field, so old backends may not
1916 * provide this. Assume physical sector size to be the same as
1917 * sector_size in that case.
1918 */
1919 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1920 "physical-sector-size", "%u", &physical_sector_size);
1921 if (err != 1)
1922 physical_sector_size = sector_size;
1923
d50babbe 1924 err = blkfront_gather_backend_features(info);
402b27f9
RPM
1925 if (err) {
1926 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
1927 info->xbdev->otherend);
1928 return;
1929 }
1930
7c4d7d71
SB
1931 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
1932 physical_sector_size);
9f27ee59
JF
1933 if (err) {
1934 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1935 info->xbdev->otherend);
1936 return;
1937 }
1938
1939 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1940
1941 /* Kick pending requests. */
3467811e 1942 spin_lock_irq(&info->io_lock);
9f27ee59
JF
1943 info->connected = BLKIF_STATE_CONNECTED;
1944 kick_pending_request_queues(info);
3467811e 1945 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
1946
1947 add_disk(info->gd);
1d78d705
CL
1948
1949 info->is_ready = 1;
9f27ee59
JF
1950}
1951
9f27ee59
JF
1952/**
1953 * Callback received when the backend's state changes.
1954 */
203fd61f 1955static void blkback_changed(struct xenbus_device *dev,
9f27ee59
JF
1956 enum xenbus_state backend_state)
1957{
a1b4b12b 1958 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
9f27ee59 1959
203fd61f 1960 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
9f27ee59
JF
1961
1962 switch (backend_state) {
9f27ee59 1963 case XenbusStateInitWait:
a9b54bb9
BL
1964 if (dev->state != XenbusStateInitialising)
1965 break;
8ab0144a
BL
1966 if (talk_to_blkback(dev, info)) {
1967 kfree(info);
1968 dev_set_drvdata(&dev->dev, NULL);
1969 break;
1970 }
1971 case XenbusStateInitialising:
9f27ee59 1972 case XenbusStateInitialised:
b78c9512
NI
1973 case XenbusStateReconfiguring:
1974 case XenbusStateReconfigured:
9f27ee59 1975 case XenbusStateUnknown:
9f27ee59
JF
1976 break;
1977
1978 case XenbusStateConnected:
1979 blkfront_connect(info);
1980 break;
1981
36613717
DV
1982 case XenbusStateClosed:
1983 if (dev->state == XenbusStateClosed)
1984 break;
1985 /* Missed the backend's Closing state -- fallthrough */
9f27ee59 1986 case XenbusStateClosing:
b70f5fa0 1987 blkfront_closing(info);
9f27ee59
JF
1988 break;
1989 }
1990}
1991
fa1bd359 1992static int blkfront_remove(struct xenbus_device *xbdev)
9f27ee59 1993{
fa1bd359
DS
1994 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1995 struct block_device *bdev = NULL;
1996 struct gendisk *disk;
9f27ee59 1997
fa1bd359 1998 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
9f27ee59
JF
1999
2000 blkif_free(info, 0);
2001
fa1bd359
DS
2002 mutex_lock(&info->mutex);
2003
2004 disk = info->gd;
2005 if (disk)
2006 bdev = bdget_disk(disk, 0);
2007
2008 info->xbdev = NULL;
2009 mutex_unlock(&info->mutex);
2010
2011 if (!bdev) {
2012 kfree(info);
2013 return 0;
2014 }
2015
2016 /*
2017 * The xbdev was removed before we reached the Closed
2018 * state. See if it's safe to remove the disk. If the bdev
2019 * isn't closed yet, we let release take care of it.
2020 */
2021
2022 mutex_lock(&bdev->bd_mutex);
2023 info = disk->private_data;
2024
d54142c7
DS
2025 dev_warn(disk_to_dev(disk),
2026 "%s was hot-unplugged, %d stale handles\n",
2027 xbdev->nodename, bdev->bd_openers);
2028
7b32d104 2029 if (info && !bdev->bd_openers) {
fa1bd359
DS
2030 xlvbd_release_gendisk(info);
2031 disk->private_data = NULL;
0e345826 2032 kfree(info);
fa1bd359
DS
2033 }
2034
2035 mutex_unlock(&bdev->bd_mutex);
2036 bdput(bdev);
9f27ee59
JF
2037
2038 return 0;
2039}
2040
1d78d705
CL
2041static int blkfront_is_ready(struct xenbus_device *dev)
2042{
a1b4b12b 2043 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1d78d705 2044
5d7ed20e 2045 return info->is_ready && info->xbdev;
1d78d705
CL
2046}
2047
a63c848b 2048static int blkif_open(struct block_device *bdev, fmode_t mode)
9f27ee59 2049{
13961743
DS
2050 struct gendisk *disk = bdev->bd_disk;
2051 struct blkfront_info *info;
2052 int err = 0;
6e9624b8 2053
2a48fc0a 2054 mutex_lock(&blkfront_mutex);
6e9624b8 2055
13961743
DS
2056 info = disk->private_data;
2057 if (!info) {
2058 /* xbdev gone */
2059 err = -ERESTARTSYS;
2060 goto out;
2061 }
2062
2063 mutex_lock(&info->mutex);
2064
2065 if (!info->gd)
2066 /* xbdev is closed */
2067 err = -ERESTARTSYS;
2068
2069 mutex_unlock(&info->mutex);
2070
13961743 2071out:
2a48fc0a 2072 mutex_unlock(&blkfront_mutex);
13961743 2073 return err;
9f27ee59
JF
2074}
2075
db2a144b 2076static void blkif_release(struct gendisk *disk, fmode_t mode)
9f27ee59 2077{
a63c848b 2078 struct blkfront_info *info = disk->private_data;
7fd152f4
DS
2079 struct block_device *bdev;
2080 struct xenbus_device *xbdev;
2081
2a48fc0a 2082 mutex_lock(&blkfront_mutex);
7fd152f4
DS
2083
2084 bdev = bdget_disk(disk, 0);
7fd152f4 2085
2f089cb8
FP
2086 if (!bdev) {
2087 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2088 goto out_mutex;
2089 }
acfca3c6
DS
2090 if (bdev->bd_openers)
2091 goto out;
2092
7fd152f4
DS
2093 /*
2094 * Check if we have been instructed to close. We will have
2095 * deferred this request, because the bdev was still open.
2096 */
2097
2098 mutex_lock(&info->mutex);
2099 xbdev = info->xbdev;
2100
2101 if (xbdev && xbdev->state == XenbusStateClosing) {
2102 /* pending switch to state closed */
d54142c7 2103 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
7fd152f4
DS
2104 xlvbd_release_gendisk(info);
2105 xenbus_frontend_closed(info->xbdev);
2106 }
2107
2108 mutex_unlock(&info->mutex);
2109
2110 if (!xbdev) {
2111 /* sudden device removal */
d54142c7 2112 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
7fd152f4
DS
2113 xlvbd_release_gendisk(info);
2114 disk->private_data = NULL;
2115 kfree(info);
9f27ee59 2116 }
7fd152f4 2117
a4cc14ec 2118out:
dad5cf65 2119 bdput(bdev);
2f089cb8 2120out_mutex:
2a48fc0a 2121 mutex_unlock(&blkfront_mutex);
9f27ee59
JF
2122}
2123
83d5cde4 2124static const struct block_device_operations xlvbd_block_fops =
9f27ee59
JF
2125{
2126 .owner = THIS_MODULE,
a63c848b
AV
2127 .open = blkif_open,
2128 .release = blkif_release,
597592d9 2129 .getgeo = blkif_getgeo,
8a6cfeb6 2130 .ioctl = blkif_ioctl,
9f27ee59
JF
2131};
2132
2133
ec9c42ec 2134static const struct xenbus_device_id blkfront_ids[] = {
9f27ee59
JF
2135 { "vbd" },
2136 { "" }
2137};
2138
95afae48
DV
2139static struct xenbus_driver blkfront_driver = {
2140 .ids = blkfront_ids,
9f27ee59
JF
2141 .probe = blkfront_probe,
2142 .remove = blkfront_remove,
2143 .resume = blkfront_resume,
203fd61f 2144 .otherend_changed = blkback_changed,
1d78d705 2145 .is_ready = blkfront_is_ready,
95afae48 2146};
9f27ee59
JF
2147
2148static int __init xlblk_init(void)
2149{
469738e6
LE
2150 int ret;
2151
6e833587 2152 if (!xen_domain())
9f27ee59
JF
2153 return -ENODEV;
2154
86839c56
BL
2155 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_PAGE_ORDER) {
2156 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2157 xen_blkif_max_ring_order, XENBUS_MAX_RING_PAGE_ORDER);
2158 xen_blkif_max_ring_order = 0;
2159 }
2160
51c71a3b 2161 if (!xen_has_pv_disk_devices())
b9136d20
IM
2162 return -ENODEV;
2163
9f27ee59
JF
2164 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2165 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2166 XENVBD_MAJOR, DEV_NAME);
2167 return -ENODEV;
2168 }
2169
73db144b 2170 ret = xenbus_register_frontend(&blkfront_driver);
469738e6
LE
2171 if (ret) {
2172 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2173 return ret;
2174 }
2175
2176 return 0;
9f27ee59
JF
2177}
2178module_init(xlblk_init);
2179
2180
5a60d0cd 2181static void __exit xlblk_exit(void)
9f27ee59 2182{
8605067f
JB
2183 xenbus_unregister_driver(&blkfront_driver);
2184 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2185 kfree(minors);
9f27ee59
JF
2186}
2187module_exit(xlblk_exit);
2188
2189MODULE_DESCRIPTION("Xen virtual block device frontend");
2190MODULE_LICENSE("GPL");
2191MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
d2f0c52b 2192MODULE_ALIAS("xen:vbd");
4f93f09b 2193MODULE_ALIAS("xenblk");