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