blob: 715de7d8ce010af21c7bfd91eb42f70e5bbe3b19 [file] [log] [blame]
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001/*
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>
Ian Campbell597592d2008-02-21 13:03:45 -080040#include <linux/hdreg.h>
Christian Limpach440a01a2008-06-17 10:47:08 +020041#include <linux/cdrom.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070042#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020044#include <linux/smp_lock.h>
Jens Axboe9e973e62009-02-24 08:10:09 +010045#include <linux/scatterlist.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070046
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070047#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070048#include <xen/xenbus.h>
49#include <xen/grant_table.h>
50#include <xen/events.h>
51#include <xen/page.h>
52
53#include <xen/interface/grant_table.h>
54#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070055#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070056
57#include <asm/xen/hypervisor.h>
58
59enum blkif_state {
60 BLKIF_STATE_DISCONNECTED,
61 BLKIF_STATE_CONNECTED,
62 BLKIF_STATE_SUSPENDED,
63};
64
65struct blk_shadow {
66 struct blkif_request req;
67 unsigned long request;
68 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
69};
70
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070071static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070072
73#define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
74
75/*
76 * We have one of these per vbd, whether ide, scsi or 'other'. They
77 * hang in private_data off the gendisk structure. We may end up
78 * putting all kinds of interesting stuff here :-)
79 */
80struct blkfront_info
81{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +000082 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070083 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070084 struct gendisk *gd;
85 int vdevice;
86 blkif_vdev_t handle;
87 enum blkif_state connected;
88 int ring_ref;
89 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +010090 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070091 unsigned int evtchn, irq;
92 struct request_queue *rq;
93 struct work_struct work;
94 struct gnttab_free_callback callback;
95 struct blk_shadow shadow[BLK_RING_SIZE];
96 unsigned long shadow_free;
97 int feature_barrier;
Christian Limpach1d78d702008-04-02 10:54:04 -070098 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070099
100 /**
101 * The number of people holding this device open. We won't allow a
102 * hot-unplug unless this is 0.
103 */
104 int users;
105};
106
107static DEFINE_SPINLOCK(blkif_io_lock);
108
Jan Beulich0e345822010-08-07 18:28:55 +0200109static unsigned int nr_minors;
110static unsigned long *minors;
111static DEFINE_SPINLOCK(minor_lock);
112
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700113#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
114 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
115#define GRANT_INVALID_REF 0
116
117#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700118#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700119
120#define BLKIF_MAJOR(dev) ((dev)>>8)
121#define BLKIF_MINOR(dev) ((dev) & 0xff)
122
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700123#define EXT_SHIFT 28
124#define EXTENDED (1<<EXT_SHIFT)
125#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
126#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700127
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700128#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700129
130static int get_id_from_freelist(struct blkfront_info *info)
131{
132 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200133 BUG_ON(free >= BLK_RING_SIZE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700134 info->shadow_free = info->shadow[free].req.id;
135 info->shadow[free].req.id = 0x0fffffee; /* debug */
136 return free;
137}
138
139static void add_id_to_freelist(struct blkfront_info *info,
140 unsigned long id)
141{
142 info->shadow[id].req.id = info->shadow_free;
143 info->shadow[id].request = 0;
144 info->shadow_free = id;
145}
146
Jan Beulich0e345822010-08-07 18:28:55 +0200147static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
148{
149 unsigned int end = minor + nr;
150 int rc;
151
152 if (end > nr_minors) {
153 unsigned long *bitmap, *old;
154
155 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
156 GFP_KERNEL);
157 if (bitmap == NULL)
158 return -ENOMEM;
159
160 spin_lock(&minor_lock);
161 if (end > nr_minors) {
162 old = minors;
163 memcpy(bitmap, minors,
164 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
165 minors = bitmap;
166 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
167 } else
168 old = bitmap;
169 spin_unlock(&minor_lock);
170 kfree(old);
171 }
172
173 spin_lock(&minor_lock);
174 if (find_next_bit(minors, end, minor) >= end) {
175 for (; minor < end; ++minor)
176 __set_bit(minor, minors);
177 rc = 0;
178 } else
179 rc = -EBUSY;
180 spin_unlock(&minor_lock);
181
182 return rc;
183}
184
185static void xlbd_release_minors(unsigned int minor, unsigned int nr)
186{
187 unsigned int end = minor + nr;
188
189 BUG_ON(end > nr_minors);
190 spin_lock(&minor_lock);
191 for (; minor < end; ++minor)
192 __clear_bit(minor, minors);
193 spin_unlock(&minor_lock);
194}
195
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700196static void blkif_restart_queue_callback(void *arg)
197{
198 struct blkfront_info *info = (struct blkfront_info *)arg;
199 schedule_work(&info->work);
200}
201
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700202static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800203{
204 /* We don't have real geometry info, but let's at least return
205 values consistent with the size of the device */
206 sector_t nsect = get_capacity(bd->bd_disk);
207 sector_t cylinders = nsect;
208
209 hg->heads = 0xff;
210 hg->sectors = 0x3f;
211 sector_div(cylinders, hg->heads * hg->sectors);
212 hg->cylinders = cylinders;
213 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
214 hg->cylinders = 0xffff;
215 return 0;
216}
217
Al Viroa63c8482008-03-02 10:23:47 -0500218static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200219 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200220{
Al Viroa63c8482008-03-02 10:23:47 -0500221 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200222 int i;
223
224 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
225 command, (long)argument);
226
227 switch (command) {
228 case CDROMMULTISESSION:
229 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
230 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
231 if (put_user(0, (char __user *)(argument + i)))
232 return -EFAULT;
233 return 0;
234
235 case CDROM_GET_CAPABILITY: {
236 struct gendisk *gd = info->gd;
237 if (gd->flags & GENHD_FL_CD)
238 return 0;
239 return -EINVAL;
240 }
241
242 default:
243 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
244 command);*/
245 return -EINVAL; /* same return as native Linux */
246 }
247
248 return 0;
249}
250
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700251/*
252 * blkif_queue_request
253 *
254 * request block io
255 *
256 * id: for guest use only.
257 * operation: BLKIF_OP_{READ,WRITE,PROBE}
258 * buffer: buffer to read/write into. this should be a
259 * virtual address in the guest os.
260 */
261static int blkif_queue_request(struct request *req)
262{
263 struct blkfront_info *info = req->rq_disk->private_data;
264 unsigned long buffer_mfn;
265 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700266 unsigned long id;
267 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100268 int i, ref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700269 grant_ref_t gref_head;
Jens Axboe9e973e62009-02-24 08:10:09 +0100270 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700271
272 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
273 return 1;
274
275 if (gnttab_alloc_grant_references(
276 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
277 gnttab_request_free_callback(
278 &info->callback,
279 blkif_restart_queue_callback,
280 info,
281 BLKIF_MAX_SEGMENTS_PER_REQUEST);
282 return 1;
283 }
284
285 /* Fill out a communications ring structure. */
286 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
287 id = get_id_from_freelist(info);
288 info->shadow[id].request = (unsigned long)req;
289
290 ring_req->id = id;
Tejun Heo83096eb2009-05-07 22:24:39 +0900291 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700292 ring_req->handle = info->handle;
293
294 ring_req->operation = rq_data_dir(req) ?
295 BLKIF_OP_WRITE : BLKIF_OP_READ;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200296 if (req->cmd_flags & REQ_HARDBARRIER)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700297 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
298
Jens Axboe9e973e62009-02-24 08:10:09 +0100299 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
300 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
301
302 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
303 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
304 fsect = sg->offset >> 9;
305 lsect = fsect + (sg->length >> 9) - 1;
Jens Axboe6c92e692007-08-16 13:43:12 +0200306 /* install a grant reference. */
307 ref = gnttab_claim_grant_reference(&gref_head);
308 BUG_ON(ref == -ENOSPC);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700309
Jens Axboe6c92e692007-08-16 13:43:12 +0200310 gnttab_grant_foreign_access_ref(
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700311 ref,
312 info->xbdev->otherend_id,
313 buffer_mfn,
314 rq_data_dir(req) );
315
Jens Axboe9e973e62009-02-24 08:10:09 +0100316 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
317 ring_req->seg[i] =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700318 (struct blkif_request_segment) {
319 .gref = ref,
320 .first_sect = fsect,
321 .last_sect = lsect };
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700322 }
323
324 info->ring.req_prod_pvt++;
325
326 /* Keep a private copy so we can reissue requests when recovering. */
327 info->shadow[id].req = *ring_req;
328
329 gnttab_free_grant_references(gref_head);
330
331 return 0;
332}
333
334
335static inline void flush_requests(struct blkfront_info *info)
336{
337 int notify;
338
339 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
340
341 if (notify)
342 notify_remote_via_irq(info->irq);
343}
344
345/*
346 * do_blkif_request
347 * read a block; request is in a request queue
348 */
Jens Axboe165125e2007-07-24 09:28:11 +0200349static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700350{
351 struct blkfront_info *info = NULL;
352 struct request *req;
353 int queued;
354
355 pr_debug("Entered do_blkif_request\n");
356
357 queued = 0;
358
Tejun Heo9934c8c2009-05-08 11:54:16 +0900359 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700360 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700361
362 if (RING_FULL(&info->ring))
363 goto wait;
364
Tejun Heo9934c8c2009-05-08 11:54:16 +0900365 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900366
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200367 if (req->cmd_type != REQ_TYPE_FS) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900368 __blk_end_request_all(req, -EIO);
369 continue;
370 }
371
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700372 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900373 "(%u/%u) buffer:%p [%s]\n",
374 req, req->cmd, (unsigned long)blk_rq_pos(req),
375 blk_rq_cur_sectors(req), blk_rq_sectors(req),
376 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700377
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700378 if (blkif_queue_request(req)) {
379 blk_requeue_request(rq, req);
380wait:
381 /* Avoid pointless unplugs. */
382 blk_stop_queue(rq);
383 break;
384 }
385
386 queued++;
387 }
388
389 if (queued != 0)
390 flush_requests(info);
391}
392
393static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
394{
Jens Axboe165125e2007-07-24 09:28:11 +0200395 struct request_queue *rq;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700396
397 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
398 if (rq == NULL)
399 return -1;
400
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900401 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700402
403 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400404 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500405 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700406
407 /* Each segment in a request is up to an aligned page in size. */
408 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
409 blk_queue_max_segment_size(rq, PAGE_SIZE);
410
411 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500412 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700413
414 /* Make sure buffer addresses are sector-aligned. */
415 blk_queue_dma_alignment(rq, 511);
416
Ian Campbell1c91fe12008-06-17 10:47:08 +0200417 /* Make sure we don't use bounce buffers. */
418 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
419
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700420 gd->queue = rq;
421
422 return 0;
423}
424
425
426static int xlvbd_barrier(struct blkfront_info *info)
427{
428 int err;
429
430 err = blk_queue_ordered(info->rq,
FUJITA Tomonori00fff262010-07-03 17:45:40 +0900431 info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700432
433 if (err)
434 return err;
435
436 printk(KERN_INFO "blkfront: %s: barriers %s\n",
437 info->gd->disk_name,
438 info->feature_barrier ? "enabled" : "disabled");
439 return 0;
440}
441
442
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700443static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
444 struct blkfront_info *info,
445 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700446{
447 struct gendisk *gd;
448 int nr_minors = 1;
449 int err = -ENODEV;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700450 unsigned int offset;
451 int minor;
452 int nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700453
454 BUG_ON(info->gd != NULL);
455 BUG_ON(info->rq != NULL);
456
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700457 if ((info->vdevice>>EXT_SHIFT) > 1) {
458 /* this is above the extended range; something is wrong */
459 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
460 return -ENODEV;
461 }
462
463 if (!VDEV_IS_EXTENDED(info->vdevice)) {
464 minor = BLKIF_MINOR(info->vdevice);
465 nr_parts = PARTS_PER_DISK;
466 } else {
467 minor = BLKIF_MINOR_EXT(info->vdevice);
468 nr_parts = PARTS_PER_EXT_DISK;
469 }
470
471 if ((minor % nr_parts) == 0)
472 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700473
Jan Beulich0e345822010-08-07 18:28:55 +0200474 err = xlbd_reserve_minors(minor, nr_minors);
475 if (err)
476 goto out;
477 err = -ENODEV;
478
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700479 gd = alloc_disk(nr_minors);
480 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200481 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700482
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700483 offset = minor / nr_parts;
484
485 if (nr_minors > 1) {
486 if (offset < 26)
487 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
488 else
489 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
490 'a' + ((offset / 26)-1), 'a' + (offset % 26));
491 } else {
492 if (offset < 26)
493 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
494 'a' + offset,
495 minor & (nr_parts - 1));
496 else
497 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
498 'a' + ((offset / 26) - 1),
499 'a' + (offset % 26),
500 minor & (nr_parts - 1));
501 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700502
503 gd->major = XENVBD_MAJOR;
504 gd->first_minor = minor;
505 gd->fops = &xlvbd_block_fops;
506 gd->private_data = info;
507 gd->driverfs_dev = &(info->xbdev->dev);
508 set_capacity(gd, capacity);
509
510 if (xlvbd_init_blk_queue(gd, sector_size)) {
511 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200512 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700513 }
514
515 info->rq = gd->queue;
516 info->gd = gd;
517
518 if (info->feature_barrier)
519 xlvbd_barrier(info);
520
521 if (vdisk_info & VDISK_READONLY)
522 set_disk_ro(gd, 1);
523
524 if (vdisk_info & VDISK_REMOVABLE)
525 gd->flags |= GENHD_FL_REMOVABLE;
526
527 if (vdisk_info & VDISK_CDROM)
528 gd->flags |= GENHD_FL_CD;
529
530 return 0;
531
Jan Beulich0e345822010-08-07 18:28:55 +0200532 release:
533 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700534 out:
535 return err;
536}
537
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200538static void xlvbd_release_gendisk(struct blkfront_info *info)
539{
540 unsigned int minor, nr_minors;
541 unsigned long flags;
542
543 if (info->rq == NULL)
544 return;
545
546 spin_lock_irqsave(&blkif_io_lock, flags);
547
548 /* No more blkif_request(). */
549 blk_stop_queue(info->rq);
550
551 /* No more gnttab callback work. */
552 gnttab_cancel_free_callback(&info->callback);
553 spin_unlock_irqrestore(&blkif_io_lock, flags);
554
555 /* Flush gnttab callback work. Must be done with no locks held. */
556 flush_scheduled_work();
557
558 del_gendisk(info->gd);
559
560 minor = info->gd->first_minor;
561 nr_minors = info->gd->minors;
562 xlbd_release_minors(minor, nr_minors);
563
564 blk_cleanup_queue(info->rq);
565 info->rq = NULL;
566
567 put_disk(info->gd);
568 info->gd = NULL;
569}
570
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700571static void kick_pending_request_queues(struct blkfront_info *info)
572{
573 if (!RING_FULL(&info->ring)) {
574 /* Re-enable calldowns. */
575 blk_start_queue(info->rq);
576 /* Kick things off immediately. */
577 do_blkif_request(info->rq);
578 }
579}
580
581static void blkif_restart_queue(struct work_struct *work)
582{
583 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
584
585 spin_lock_irq(&blkif_io_lock);
586 if (info->connected == BLKIF_STATE_CONNECTED)
587 kick_pending_request_queues(info);
588 spin_unlock_irq(&blkif_io_lock);
589}
590
591static void blkif_free(struct blkfront_info *info, int suspend)
592{
593 /* Prevent new requests being issued until we fix things up. */
594 spin_lock_irq(&blkif_io_lock);
595 info->connected = suspend ?
596 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
597 /* No more blkif_request(). */
598 if (info->rq)
599 blk_stop_queue(info->rq);
600 /* No more gnttab callback work. */
601 gnttab_cancel_free_callback(&info->callback);
602 spin_unlock_irq(&blkif_io_lock);
603
604 /* Flush gnttab callback work. Must be done with no locks held. */
605 flush_scheduled_work();
606
607 /* Free resources associated with old device channel. */
608 if (info->ring_ref != GRANT_INVALID_REF) {
609 gnttab_end_foreign_access(info->ring_ref, 0,
610 (unsigned long)info->ring.sring);
611 info->ring_ref = GRANT_INVALID_REF;
612 info->ring.sring = NULL;
613 }
614 if (info->irq)
615 unbind_from_irqhandler(info->irq, info);
616 info->evtchn = info->irq = 0;
617
618}
619
620static void blkif_completion(struct blk_shadow *s)
621{
622 int i;
623 for (i = 0; i < s->req.nr_segments; i++)
624 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
625}
626
627static irqreturn_t blkif_interrupt(int irq, void *dev_id)
628{
629 struct request *req;
630 struct blkif_response *bret;
631 RING_IDX i, rp;
632 unsigned long flags;
633 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500634 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700635
636 spin_lock_irqsave(&blkif_io_lock, flags);
637
638 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
639 spin_unlock_irqrestore(&blkif_io_lock, flags);
640 return IRQ_HANDLED;
641 }
642
643 again:
644 rp = info->ring.sring->rsp_prod;
645 rmb(); /* Ensure we see queued responses up to 'rp'. */
646
647 for (i = info->ring.rsp_cons; i != rp; i++) {
648 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700649
650 bret = RING_GET_RESPONSE(&info->ring, i);
651 id = bret->id;
652 req = (struct request *)info->shadow[id].request;
653
654 blkif_completion(&info->shadow[id]);
655
656 add_id_to_freelist(info, id);
657
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500658 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700659 switch (bret->operation) {
660 case BLKIF_OP_WRITE_BARRIER:
661 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
662 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
663 info->gd->disk_name);
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500664 error = -EOPNOTSUPP;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700665 info->feature_barrier = 0;
666 xlvbd_barrier(info);
667 }
668 /* fall through */
669 case BLKIF_OP_READ:
670 case BLKIF_OP_WRITE:
671 if (unlikely(bret->status != BLKIF_RSP_OKAY))
672 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
673 "request: %x\n", bret->status);
674
Tejun Heo40cbbb72009-04-23 11:05:19 +0900675 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700676 break;
677 default:
678 BUG();
679 }
680 }
681
682 info->ring.rsp_cons = i;
683
684 if (i != info->ring.req_prod_pvt) {
685 int more_to_do;
686 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
687 if (more_to_do)
688 goto again;
689 } else
690 info->ring.sring->rsp_event = i + 1;
691
692 kick_pending_request_queues(info);
693
694 spin_unlock_irqrestore(&blkif_io_lock, flags);
695
696 return IRQ_HANDLED;
697}
698
699
700static int setup_blkring(struct xenbus_device *dev,
701 struct blkfront_info *info)
702{
703 struct blkif_sring *sring;
704 int err;
705
706 info->ring_ref = GRANT_INVALID_REF;
707
Ian Campbella144ff02008-06-17 10:47:08 +0200708 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700709 if (!sring) {
710 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
711 return -ENOMEM;
712 }
713 SHARED_RING_INIT(sring);
714 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
715
Jens Axboe9e973e62009-02-24 08:10:09 +0100716 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
717
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700718 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
719 if (err < 0) {
720 free_page((unsigned long)sring);
721 info->ring.sring = NULL;
722 goto fail;
723 }
724 info->ring_ref = err;
725
726 err = xenbus_alloc_evtchn(dev, &info->evtchn);
727 if (err)
728 goto fail;
729
730 err = bind_evtchn_to_irqhandler(info->evtchn,
731 blkif_interrupt,
732 IRQF_SAMPLE_RANDOM, "blkif", info);
733 if (err <= 0) {
734 xenbus_dev_fatal(dev, err,
735 "bind_evtchn_to_irqhandler failed");
736 goto fail;
737 }
738 info->irq = err;
739
740 return 0;
741fail:
742 blkif_free(info, 0);
743 return err;
744}
745
746
747/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +0000748static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700749 struct blkfront_info *info)
750{
751 const char *message = NULL;
752 struct xenbus_transaction xbt;
753 int err;
754
755 /* Create shared ring, alloc event channel. */
756 err = setup_blkring(dev, info);
757 if (err)
758 goto out;
759
760again:
761 err = xenbus_transaction_start(&xbt);
762 if (err) {
763 xenbus_dev_fatal(dev, err, "starting transaction");
764 goto destroy_blkring;
765 }
766
767 err = xenbus_printf(xbt, dev->nodename,
768 "ring-ref", "%u", info->ring_ref);
769 if (err) {
770 message = "writing ring-ref";
771 goto abort_transaction;
772 }
773 err = xenbus_printf(xbt, dev->nodename,
774 "event-channel", "%u", info->evtchn);
775 if (err) {
776 message = "writing event-channel";
777 goto abort_transaction;
778 }
Markus Armbruster3e334232008-04-02 10:54:02 -0700779 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
780 XEN_IO_PROTO_ABI_NATIVE);
781 if (err) {
782 message = "writing protocol";
783 goto abort_transaction;
784 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700785
786 err = xenbus_transaction_end(xbt, 0);
787 if (err) {
788 if (err == -EAGAIN)
789 goto again;
790 xenbus_dev_fatal(dev, err, "completing transaction");
791 goto destroy_blkring;
792 }
793
794 xenbus_switch_state(dev, XenbusStateInitialised);
795
796 return 0;
797
798 abort_transaction:
799 xenbus_transaction_end(xbt, 1);
800 if (message)
801 xenbus_dev_fatal(dev, err, "%s", message);
802 destroy_blkring:
803 blkif_free(info, 0);
804 out:
805 return err;
806}
807
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700808/**
809 * Entry point to this code when a new device is created. Allocate the basic
810 * structures and the ring buffer for communication with the backend, and
811 * inform the backend of the appropriate details for those. Switch to
812 * Initialised state.
813 */
814static int blkfront_probe(struct xenbus_device *dev,
815 const struct xenbus_device_id *id)
816{
817 int err, vdevice, i;
818 struct blkfront_info *info;
819
820 /* FIXME: Use dynamic device id if this is not set. */
821 err = xenbus_scanf(XBT_NIL, dev->nodename,
822 "virtual-device", "%i", &vdevice);
823 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700824 /* go looking in the extended area instead */
825 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
826 "%i", &vdevice);
827 if (err != 1) {
828 xenbus_dev_fatal(dev, err, "reading virtual-device");
829 return err;
830 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700831 }
832
833 info = kzalloc(sizeof(*info), GFP_KERNEL);
834 if (!info) {
835 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
836 return -ENOMEM;
837 }
838
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000839 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700840 info->xbdev = dev;
841 info->vdevice = vdevice;
842 info->connected = BLKIF_STATE_DISCONNECTED;
843 INIT_WORK(&info->work, blkif_restart_queue);
844
845 for (i = 0; i < BLK_RING_SIZE; i++)
846 info->shadow[i].req.id = i+1;
847 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
848
849 /* Front end dir is a number, which is used as the id. */
850 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700851 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700852
Ian Campbell203fd612009-12-04 15:33:54 +0000853 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700854 if (err) {
855 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700856 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700857 return err;
858 }
859
860 return 0;
861}
862
863
864static int blkif_recover(struct blkfront_info *info)
865{
866 int i;
867 struct blkif_request *req;
868 struct blk_shadow *copy;
869 int j;
870
871 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +0200872 copy = kmalloc(sizeof(info->shadow),
873 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700874 if (!copy)
875 return -ENOMEM;
876 memcpy(copy, info->shadow, sizeof(info->shadow));
877
878 /* Stage 2: Set up free list. */
879 memset(&info->shadow, 0, sizeof(info->shadow));
880 for (i = 0; i < BLK_RING_SIZE; i++)
881 info->shadow[i].req.id = i+1;
882 info->shadow_free = info->ring.req_prod_pvt;
883 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
884
885 /* Stage 3: Find pending requests and requeue them. */
886 for (i = 0; i < BLK_RING_SIZE; i++) {
887 /* Not in use? */
888 if (copy[i].request == 0)
889 continue;
890
891 /* Grab a request slot and copy shadow state into it. */
892 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
893 *req = copy[i].req;
894
895 /* We get a new request id, and must reset the shadow state. */
896 req->id = get_id_from_freelist(info);
897 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
898
899 /* Rewrite any grant references invalidated by susp/resume. */
900 for (j = 0; j < req->nr_segments; j++)
901 gnttab_grant_foreign_access_ref(
902 req->seg[j].gref,
903 info->xbdev->otherend_id,
904 pfn_to_mfn(info->shadow[req->id].frame[j]),
905 rq_data_dir(
906 (struct request *)
907 info->shadow[req->id].request));
908 info->shadow[req->id].req = *req;
909
910 info->ring.req_prod_pvt++;
911 }
912
913 kfree(copy);
914
915 xenbus_switch_state(info->xbdev, XenbusStateConnected);
916
917 spin_lock_irq(&blkif_io_lock);
918
919 /* Now safe for us to use the shared ring */
920 info->connected = BLKIF_STATE_CONNECTED;
921
922 /* Send off requeued requests */
923 flush_requests(info);
924
925 /* Kick any other new requests queued since we resumed */
926 kick_pending_request_queues(info);
927
928 spin_unlock_irq(&blkif_io_lock);
929
930 return 0;
931}
932
933/**
934 * We are reconnecting to the backend, due to a suspend/resume, or a backend
935 * driver restart. We tear down our blkif structure and recreate it, but
936 * leave the device-layer structures intact so that this is transparent to the
937 * rest of the kernel.
938 */
939static int blkfront_resume(struct xenbus_device *dev)
940{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700941 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700942 int err;
943
944 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
945
946 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
947
Ian Campbell203fd612009-12-04 15:33:54 +0000948 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700949 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
950 err = blkif_recover(info);
951
952 return err;
953}
954
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000955static void
956blkfront_closing(struct blkfront_info *info)
957{
958 struct xenbus_device *xbdev = info->xbdev;
959 struct block_device *bdev = NULL;
960
961 mutex_lock(&info->mutex);
962
963 if (xbdev->state == XenbusStateClosing) {
964 mutex_unlock(&info->mutex);
965 return;
966 }
967
968 if (info->gd)
969 bdev = bdget_disk(info->gd, 0);
970
971 mutex_unlock(&info->mutex);
972
973 if (!bdev) {
974 xenbus_frontend_closed(xbdev);
975 return;
976 }
977
978 mutex_lock(&bdev->bd_mutex);
979
980 if (info->users) {
981 xenbus_dev_error(xbdev, -EBUSY,
982 "Device in use; refusing to close");
983 xenbus_switch_state(xbdev, XenbusStateClosing);
984 } else {
985 xlvbd_release_gendisk(info);
986 xenbus_frontend_closed(xbdev);
987 }
988
989 mutex_unlock(&bdev->bd_mutex);
990 bdput(bdev);
991}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700992
993/*
994 * Invoked when the backend is finally 'ready' (and has told produced
995 * the details about the physical device - #sectors, size, etc).
996 */
997static void blkfront_connect(struct blkfront_info *info)
998{
999 unsigned long long sectors;
1000 unsigned long sector_size;
1001 unsigned int binfo;
1002 int err;
1003
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001004 switch (info->connected) {
1005 case BLKIF_STATE_CONNECTED:
1006 /*
1007 * Potentially, the back-end may be signalling
1008 * a capacity change; update the capacity.
1009 */
1010 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1011 "sectors", "%Lu", &sectors);
1012 if (XENBUS_EXIST_ERR(err))
1013 return;
1014 printk(KERN_INFO "Setting capacity to %Lu\n",
1015 sectors);
1016 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001017 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001018
1019 /* fall through */
1020 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001021 return;
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001022
1023 default:
1024 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001025 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001026
1027 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1028 __func__, info->xbdev->otherend);
1029
1030 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1031 "sectors", "%llu", &sectors,
1032 "info", "%u", &binfo,
1033 "sector-size", "%lu", &sector_size,
1034 NULL);
1035 if (err) {
1036 xenbus_dev_fatal(info->xbdev, err,
1037 "reading backend fields at %s",
1038 info->xbdev->otherend);
1039 return;
1040 }
1041
1042 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1043 "feature-barrier", "%lu", &info->feature_barrier,
1044 NULL);
1045 if (err)
1046 info->feature_barrier = 0;
1047
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001048 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001049 if (err) {
1050 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1051 info->xbdev->otherend);
1052 return;
1053 }
1054
1055 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1056
1057 /* Kick pending requests. */
1058 spin_lock_irq(&blkif_io_lock);
1059 info->connected = BLKIF_STATE_CONNECTED;
1060 kick_pending_request_queues(info);
1061 spin_unlock_irq(&blkif_io_lock);
1062
1063 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001064
1065 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001066}
1067
1068/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001069 * Callback received when the backend's state changes.
1070 */
Ian Campbell203fd612009-12-04 15:33:54 +00001071static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001072 enum xenbus_state backend_state)
1073{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001074 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001075
Ian Campbell203fd612009-12-04 15:33:54 +00001076 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001077
1078 switch (backend_state) {
1079 case XenbusStateInitialising:
1080 case XenbusStateInitWait:
1081 case XenbusStateInitialised:
1082 case XenbusStateUnknown:
1083 case XenbusStateClosed:
1084 break;
1085
1086 case XenbusStateConnected:
1087 blkfront_connect(info);
1088 break;
1089
1090 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001091 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001092 break;
1093 }
1094}
1095
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001096static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001097{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001098 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1099 struct block_device *bdev = NULL;
1100 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001101
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001102 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001103
1104 blkif_free(info, 0);
1105
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001106 mutex_lock(&info->mutex);
1107
1108 disk = info->gd;
1109 if (disk)
1110 bdev = bdget_disk(disk, 0);
1111
1112 info->xbdev = NULL;
1113 mutex_unlock(&info->mutex);
1114
1115 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001116 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001117 return 0;
1118 }
1119
1120 /*
1121 * The xbdev was removed before we reached the Closed
1122 * state. See if it's safe to remove the disk. If the bdev
1123 * isn't closed yet, we let release take care of it.
1124 */
1125
1126 mutex_lock(&bdev->bd_mutex);
1127 info = disk->private_data;
1128
1129 if (info && !info->users) {
1130 xlvbd_release_gendisk(info);
1131 disk->private_data = NULL;
1132 kfree(info);
1133 }
1134
1135 mutex_unlock(&bdev->bd_mutex);
1136 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001137
1138 return 0;
1139}
1140
Christian Limpach1d78d702008-04-02 10:54:04 -07001141static int blkfront_is_ready(struct xenbus_device *dev)
1142{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001143 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001144
Jan Beulich5d7ed202010-08-07 18:31:12 +02001145 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001146}
1147
Al Viroa63c8482008-03-02 10:23:47 -05001148static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001149{
Daniel Stodden13961742010-08-07 18:36:53 +02001150 struct gendisk *disk = bdev->bd_disk;
1151 struct blkfront_info *info;
1152 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001153
1154 lock_kernel();
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001155
Daniel Stodden13961742010-08-07 18:36:53 +02001156 info = disk->private_data;
1157 if (!info) {
1158 /* xbdev gone */
1159 err = -ERESTARTSYS;
1160 goto out;
1161 }
1162
1163 mutex_lock(&info->mutex);
1164
1165 if (!info->gd)
1166 /* xbdev is closed */
1167 err = -ERESTARTSYS;
1168
1169 mutex_unlock(&info->mutex);
1170
1171 if (!err)
1172 ++info->users;
1173
Daniel Stodden13961742010-08-07 18:36:53 +02001174out:
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001175 unlock_kernel();
Daniel Stodden13961742010-08-07 18:36:53 +02001176 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001177}
1178
Al Viroa63c8482008-03-02 10:23:47 -05001179static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001180{
Al Viroa63c8482008-03-02 10:23:47 -05001181 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001182 struct block_device *bdev;
1183 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001184
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001185 lock_kernel();
1186 if (--info->users)
1187 goto out;
1188
1189 bdev = bdget_disk(disk, 0);
1190 bdput(bdev);
1191
1192 /*
1193 * Check if we have been instructed to close. We will have
1194 * deferred this request, because the bdev was still open.
1195 */
1196
1197 mutex_lock(&info->mutex);
1198 xbdev = info->xbdev;
1199
1200 if (xbdev && xbdev->state == XenbusStateClosing) {
1201 /* pending switch to state closed */
1202 xlvbd_release_gendisk(info);
1203 xenbus_frontend_closed(info->xbdev);
1204 }
1205
1206 mutex_unlock(&info->mutex);
1207
1208 if (!xbdev) {
1209 /* sudden device removal */
1210 xlvbd_release_gendisk(info);
1211 disk->private_data = NULL;
1212 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001213 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001214
1215out:
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001216 unlock_kernel();
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001217 return 0;
1218}
1219
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001220static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001221{
1222 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001223 .open = blkif_open,
1224 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001225 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001226 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001227};
1228
1229
Márton Némethec9c42e2010-01-10 13:39:52 +01001230static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001231 { "vbd" },
1232 { "" }
1233};
1234
1235static struct xenbus_driver blkfront = {
1236 .name = "vbd",
1237 .owner = THIS_MODULE,
1238 .ids = blkfront_ids,
1239 .probe = blkfront_probe,
1240 .remove = blkfront_remove,
1241 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001242 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001243 .is_ready = blkfront_is_ready,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001244};
1245
1246static int __init xlblk_init(void)
1247{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001248 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001249 return -ENODEV;
1250
1251 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1252 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1253 XENVBD_MAJOR, DEV_NAME);
1254 return -ENODEV;
1255 }
1256
1257 return xenbus_register_frontend(&blkfront);
1258}
1259module_init(xlblk_init);
1260
1261
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001262static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001263{
1264 return xenbus_unregister_driver(&blkfront);
1265}
1266module_exit(xlblk_exit);
1267
1268MODULE_DESCRIPTION("Xen virtual block device frontend");
1269MODULE_LICENSE("GPL");
1270MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001271MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001272MODULE_ALIAS("xenblk");