blob: d4cb7fd82b4c6fbdb346051d8d01b8677a5d84ea [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{
82 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070083 struct gendisk *gd;
84 int vdevice;
85 blkif_vdev_t handle;
86 enum blkif_state connected;
87 int ring_ref;
88 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +010089 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070090 unsigned int evtchn, irq;
91 struct request_queue *rq;
92 struct work_struct work;
93 struct gnttab_free_callback callback;
94 struct blk_shadow shadow[BLK_RING_SIZE];
95 unsigned long shadow_free;
96 int feature_barrier;
Christian Limpach1d78d702008-04-02 10:54:04 -070097 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070098
99 /**
100 * The number of people holding this device open. We won't allow a
101 * hot-unplug unless this is 0.
102 */
103 int users;
104};
105
106static DEFINE_SPINLOCK(blkif_io_lock);
107
Jan Beulich0e345822010-08-07 18:28:55 +0200108static unsigned int nr_minors;
109static unsigned long *minors;
110static DEFINE_SPINLOCK(minor_lock);
111
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700112#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
113 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
114#define GRANT_INVALID_REF 0
115
116#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700117#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700118
119#define BLKIF_MAJOR(dev) ((dev)>>8)
120#define BLKIF_MINOR(dev) ((dev) & 0xff)
121
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700122#define EXT_SHIFT 28
123#define EXTENDED (1<<EXT_SHIFT)
124#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
125#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700126
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700127#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700128
129static int get_id_from_freelist(struct blkfront_info *info)
130{
131 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200132 BUG_ON(free >= BLK_RING_SIZE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700133 info->shadow_free = info->shadow[free].req.id;
134 info->shadow[free].req.id = 0x0fffffee; /* debug */
135 return free;
136}
137
138static void add_id_to_freelist(struct blkfront_info *info,
139 unsigned long id)
140{
141 info->shadow[id].req.id = info->shadow_free;
142 info->shadow[id].request = 0;
143 info->shadow_free = id;
144}
145
Jan Beulich0e345822010-08-07 18:28:55 +0200146static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
147{
148 unsigned int end = minor + nr;
149 int rc;
150
151 if (end > nr_minors) {
152 unsigned long *bitmap, *old;
153
154 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
155 GFP_KERNEL);
156 if (bitmap == NULL)
157 return -ENOMEM;
158
159 spin_lock(&minor_lock);
160 if (end > nr_minors) {
161 old = minors;
162 memcpy(bitmap, minors,
163 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
164 minors = bitmap;
165 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
166 } else
167 old = bitmap;
168 spin_unlock(&minor_lock);
169 kfree(old);
170 }
171
172 spin_lock(&minor_lock);
173 if (find_next_bit(minors, end, minor) >= end) {
174 for (; minor < end; ++minor)
175 __set_bit(minor, minors);
176 rc = 0;
177 } else
178 rc = -EBUSY;
179 spin_unlock(&minor_lock);
180
181 return rc;
182}
183
184static void xlbd_release_minors(unsigned int minor, unsigned int nr)
185{
186 unsigned int end = minor + nr;
187
188 BUG_ON(end > nr_minors);
189 spin_lock(&minor_lock);
190 for (; minor < end; ++minor)
191 __clear_bit(minor, minors);
192 spin_unlock(&minor_lock);
193}
194
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700195static void blkif_restart_queue_callback(void *arg)
196{
197 struct blkfront_info *info = (struct blkfront_info *)arg;
198 schedule_work(&info->work);
199}
200
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700201static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800202{
203 /* We don't have real geometry info, but let's at least return
204 values consistent with the size of the device */
205 sector_t nsect = get_capacity(bd->bd_disk);
206 sector_t cylinders = nsect;
207
208 hg->heads = 0xff;
209 hg->sectors = 0x3f;
210 sector_div(cylinders, hg->heads * hg->sectors);
211 hg->cylinders = cylinders;
212 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
213 hg->cylinders = 0xffff;
214 return 0;
215}
216
Al Viroa63c8482008-03-02 10:23:47 -0500217static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa00542008-08-04 11:59:05 +0200218 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200219{
Al Viroa63c8482008-03-02 10:23:47 -0500220 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200221 int i;
222
223 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
224 command, (long)argument);
225
226 switch (command) {
227 case CDROMMULTISESSION:
228 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
229 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
230 if (put_user(0, (char __user *)(argument + i)))
231 return -EFAULT;
232 return 0;
233
234 case CDROM_GET_CAPABILITY: {
235 struct gendisk *gd = info->gd;
236 if (gd->flags & GENHD_FL_CD)
237 return 0;
238 return -EINVAL;
239 }
240
241 default:
242 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
243 command);*/
244 return -EINVAL; /* same return as native Linux */
245 }
246
247 return 0;
248}
249
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700250/*
251 * blkif_queue_request
252 *
253 * request block io
254 *
255 * id: for guest use only.
256 * operation: BLKIF_OP_{READ,WRITE,PROBE}
257 * buffer: buffer to read/write into. this should be a
258 * virtual address in the guest os.
259 */
260static int blkif_queue_request(struct request *req)
261{
262 struct blkfront_info *info = req->rq_disk->private_data;
263 unsigned long buffer_mfn;
264 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700265 unsigned long id;
266 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100267 int i, ref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700268 grant_ref_t gref_head;
Jens Axboe9e973e62009-02-24 08:10:09 +0100269 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700270
271 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
272 return 1;
273
274 if (gnttab_alloc_grant_references(
275 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
276 gnttab_request_free_callback(
277 &info->callback,
278 blkif_restart_queue_callback,
279 info,
280 BLKIF_MAX_SEGMENTS_PER_REQUEST);
281 return 1;
282 }
283
284 /* Fill out a communications ring structure. */
285 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
286 id = get_id_from_freelist(info);
287 info->shadow[id].request = (unsigned long)req;
288
289 ring_req->id = id;
Tejun Heo83096eb2009-05-07 22:24:39 +0900290 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700291 ring_req->handle = info->handle;
292
293 ring_req->operation = rq_data_dir(req) ?
294 BLKIF_OP_WRITE : BLKIF_OP_READ;
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200295 if (req->cmd_flags & REQ_HARDBARRIER)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700296 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
297
Jens Axboe9e973e62009-02-24 08:10:09 +0100298 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
299 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
300
301 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
302 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
303 fsect = sg->offset >> 9;
304 lsect = fsect + (sg->length >> 9) - 1;
Jens Axboe6c92e692007-08-16 13:43:12 +0200305 /* install a grant reference. */
306 ref = gnttab_claim_grant_reference(&gref_head);
307 BUG_ON(ref == -ENOSPC);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700308
Jens Axboe6c92e692007-08-16 13:43:12 +0200309 gnttab_grant_foreign_access_ref(
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700310 ref,
311 info->xbdev->otherend_id,
312 buffer_mfn,
313 rq_data_dir(req) );
314
Jens Axboe9e973e62009-02-24 08:10:09 +0100315 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
316 ring_req->seg[i] =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700317 (struct blkif_request_segment) {
318 .gref = ref,
319 .first_sect = fsect,
320 .last_sect = lsect };
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700321 }
322
323 info->ring.req_prod_pvt++;
324
325 /* Keep a private copy so we can reissue requests when recovering. */
326 info->shadow[id].req = *ring_req;
327
328 gnttab_free_grant_references(gref_head);
329
330 return 0;
331}
332
333
334static inline void flush_requests(struct blkfront_info *info)
335{
336 int notify;
337
338 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
339
340 if (notify)
341 notify_remote_via_irq(info->irq);
342}
343
344/*
345 * do_blkif_request
346 * read a block; request is in a request queue
347 */
Jens Axboe165125e2007-07-24 09:28:11 +0200348static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700349{
350 struct blkfront_info *info = NULL;
351 struct request *req;
352 int queued;
353
354 pr_debug("Entered do_blkif_request\n");
355
356 queued = 0;
357
Tejun Heo9934c8c2009-05-08 11:54:16 +0900358 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700359 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700360
361 if (RING_FULL(&info->ring))
362 goto wait;
363
Tejun Heo9934c8c2009-05-08 11:54:16 +0900364 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900365
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200366 if (req->cmd_type != REQ_TYPE_FS) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900367 __blk_end_request_all(req, -EIO);
368 continue;
369 }
370
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700371 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900372 "(%u/%u) buffer:%p [%s]\n",
373 req, req->cmd, (unsigned long)blk_rq_pos(req),
374 blk_rq_cur_sectors(req), blk_rq_sectors(req),
375 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700376
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700377 if (blkif_queue_request(req)) {
378 blk_requeue_request(rq, req);
379wait:
380 /* Avoid pointless unplugs. */
381 blk_stop_queue(rq);
382 break;
383 }
384
385 queued++;
386 }
387
388 if (queued != 0)
389 flush_requests(info);
390}
391
392static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
393{
Jens Axboe165125e2007-07-24 09:28:11 +0200394 struct request_queue *rq;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700395
396 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
397 if (rq == NULL)
398 return -1;
399
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900400 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700401
402 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400403 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500404 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700405
406 /* Each segment in a request is up to an aligned page in size. */
407 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
408 blk_queue_max_segment_size(rq, PAGE_SIZE);
409
410 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500411 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700412
413 /* Make sure buffer addresses are sector-aligned. */
414 blk_queue_dma_alignment(rq, 511);
415
Ian Campbell1c91fe12008-06-17 10:47:08 +0200416 /* Make sure we don't use bounce buffers. */
417 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
418
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700419 gd->queue = rq;
420
421 return 0;
422}
423
424
425static int xlvbd_barrier(struct blkfront_info *info)
426{
427 int err;
428
429 err = blk_queue_ordered(info->rq,
FUJITA Tomonori00fff262010-07-03 17:45:40 +0900430 info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700431
432 if (err)
433 return err;
434
435 printk(KERN_INFO "blkfront: %s: barriers %s\n",
436 info->gd->disk_name,
437 info->feature_barrier ? "enabled" : "disabled");
438 return 0;
439}
440
441
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700442static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
443 struct blkfront_info *info,
444 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700445{
446 struct gendisk *gd;
447 int nr_minors = 1;
448 int err = -ENODEV;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700449 unsigned int offset;
450 int minor;
451 int nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700452
453 BUG_ON(info->gd != NULL);
454 BUG_ON(info->rq != NULL);
455
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700456 if ((info->vdevice>>EXT_SHIFT) > 1) {
457 /* this is above the extended range; something is wrong */
458 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
459 return -ENODEV;
460 }
461
462 if (!VDEV_IS_EXTENDED(info->vdevice)) {
463 minor = BLKIF_MINOR(info->vdevice);
464 nr_parts = PARTS_PER_DISK;
465 } else {
466 minor = BLKIF_MINOR_EXT(info->vdevice);
467 nr_parts = PARTS_PER_EXT_DISK;
468 }
469
470 if ((minor % nr_parts) == 0)
471 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700472
Jan Beulich0e345822010-08-07 18:28:55 +0200473 err = xlbd_reserve_minors(minor, nr_minors);
474 if (err)
475 goto out;
476 err = -ENODEV;
477
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700478 gd = alloc_disk(nr_minors);
479 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200480 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700481
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700482 offset = minor / nr_parts;
483
484 if (nr_minors > 1) {
485 if (offset < 26)
486 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
487 else
488 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
489 'a' + ((offset / 26)-1), 'a' + (offset % 26));
490 } else {
491 if (offset < 26)
492 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
493 'a' + offset,
494 minor & (nr_parts - 1));
495 else
496 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
497 'a' + ((offset / 26) - 1),
498 'a' + (offset % 26),
499 minor & (nr_parts - 1));
500 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700501
502 gd->major = XENVBD_MAJOR;
503 gd->first_minor = minor;
504 gd->fops = &xlvbd_block_fops;
505 gd->private_data = info;
506 gd->driverfs_dev = &(info->xbdev->dev);
507 set_capacity(gd, capacity);
508
509 if (xlvbd_init_blk_queue(gd, sector_size)) {
510 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200511 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700512 }
513
514 info->rq = gd->queue;
515 info->gd = gd;
516
517 if (info->feature_barrier)
518 xlvbd_barrier(info);
519
520 if (vdisk_info & VDISK_READONLY)
521 set_disk_ro(gd, 1);
522
523 if (vdisk_info & VDISK_REMOVABLE)
524 gd->flags |= GENHD_FL_REMOVABLE;
525
526 if (vdisk_info & VDISK_CDROM)
527 gd->flags |= GENHD_FL_CD;
528
529 return 0;
530
Jan Beulich0e345822010-08-07 18:28:55 +0200531 release:
532 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700533 out:
534 return err;
535}
536
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200537static void xlvbd_release_gendisk(struct blkfront_info *info)
538{
539 unsigned int minor, nr_minors;
540 unsigned long flags;
541
542 if (info->rq == NULL)
543 return;
544
545 spin_lock_irqsave(&blkif_io_lock, flags);
546
547 /* No more blkif_request(). */
548 blk_stop_queue(info->rq);
549
550 /* No more gnttab callback work. */
551 gnttab_cancel_free_callback(&info->callback);
552 spin_unlock_irqrestore(&blkif_io_lock, flags);
553
554 /* Flush gnttab callback work. Must be done with no locks held. */
555 flush_scheduled_work();
556
557 del_gendisk(info->gd);
558
559 minor = info->gd->first_minor;
560 nr_minors = info->gd->minors;
561 xlbd_release_minors(minor, nr_minors);
562
563 blk_cleanup_queue(info->rq);
564 info->rq = NULL;
565
566 put_disk(info->gd);
567 info->gd = NULL;
568}
569
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700570static void kick_pending_request_queues(struct blkfront_info *info)
571{
572 if (!RING_FULL(&info->ring)) {
573 /* Re-enable calldowns. */
574 blk_start_queue(info->rq);
575 /* Kick things off immediately. */
576 do_blkif_request(info->rq);
577 }
578}
579
580static void blkif_restart_queue(struct work_struct *work)
581{
582 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
583
584 spin_lock_irq(&blkif_io_lock);
585 if (info->connected == BLKIF_STATE_CONNECTED)
586 kick_pending_request_queues(info);
587 spin_unlock_irq(&blkif_io_lock);
588}
589
590static void blkif_free(struct blkfront_info *info, int suspend)
591{
592 /* Prevent new requests being issued until we fix things up. */
593 spin_lock_irq(&blkif_io_lock);
594 info->connected = suspend ?
595 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
596 /* No more blkif_request(). */
597 if (info->rq)
598 blk_stop_queue(info->rq);
599 /* No more gnttab callback work. */
600 gnttab_cancel_free_callback(&info->callback);
601 spin_unlock_irq(&blkif_io_lock);
602
603 /* Flush gnttab callback work. Must be done with no locks held. */
604 flush_scheduled_work();
605
606 /* Free resources associated with old device channel. */
607 if (info->ring_ref != GRANT_INVALID_REF) {
608 gnttab_end_foreign_access(info->ring_ref, 0,
609 (unsigned long)info->ring.sring);
610 info->ring_ref = GRANT_INVALID_REF;
611 info->ring.sring = NULL;
612 }
613 if (info->irq)
614 unbind_from_irqhandler(info->irq, info);
615 info->evtchn = info->irq = 0;
616
617}
618
619static void blkif_completion(struct blk_shadow *s)
620{
621 int i;
622 for (i = 0; i < s->req.nr_segments; i++)
623 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
624}
625
626static irqreturn_t blkif_interrupt(int irq, void *dev_id)
627{
628 struct request *req;
629 struct blkif_response *bret;
630 RING_IDX i, rp;
631 unsigned long flags;
632 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500633 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700634
635 spin_lock_irqsave(&blkif_io_lock, flags);
636
637 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
638 spin_unlock_irqrestore(&blkif_io_lock, flags);
639 return IRQ_HANDLED;
640 }
641
642 again:
643 rp = info->ring.sring->rsp_prod;
644 rmb(); /* Ensure we see queued responses up to 'rp'. */
645
646 for (i = info->ring.rsp_cons; i != rp; i++) {
647 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700648
649 bret = RING_GET_RESPONSE(&info->ring, i);
650 id = bret->id;
651 req = (struct request *)info->shadow[id].request;
652
653 blkif_completion(&info->shadow[id]);
654
655 add_id_to_freelist(info, id);
656
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500657 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700658 switch (bret->operation) {
659 case BLKIF_OP_WRITE_BARRIER:
660 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
661 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
662 info->gd->disk_name);
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500663 error = -EOPNOTSUPP;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700664 info->feature_barrier = 0;
665 xlvbd_barrier(info);
666 }
667 /* fall through */
668 case BLKIF_OP_READ:
669 case BLKIF_OP_WRITE:
670 if (unlikely(bret->status != BLKIF_RSP_OKAY))
671 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
672 "request: %x\n", bret->status);
673
Tejun Heo40cbbb72009-04-23 11:05:19 +0900674 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700675 break;
676 default:
677 BUG();
678 }
679 }
680
681 info->ring.rsp_cons = i;
682
683 if (i != info->ring.req_prod_pvt) {
684 int more_to_do;
685 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
686 if (more_to_do)
687 goto again;
688 } else
689 info->ring.sring->rsp_event = i + 1;
690
691 kick_pending_request_queues(info);
692
693 spin_unlock_irqrestore(&blkif_io_lock, flags);
694
695 return IRQ_HANDLED;
696}
697
698
699static int setup_blkring(struct xenbus_device *dev,
700 struct blkfront_info *info)
701{
702 struct blkif_sring *sring;
703 int err;
704
705 info->ring_ref = GRANT_INVALID_REF;
706
Ian Campbella144ff02008-06-17 10:47:08 +0200707 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700708 if (!sring) {
709 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
710 return -ENOMEM;
711 }
712 SHARED_RING_INIT(sring);
713 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
714
Jens Axboe9e973e62009-02-24 08:10:09 +0100715 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
716
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700717 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
718 if (err < 0) {
719 free_page((unsigned long)sring);
720 info->ring.sring = NULL;
721 goto fail;
722 }
723 info->ring_ref = err;
724
725 err = xenbus_alloc_evtchn(dev, &info->evtchn);
726 if (err)
727 goto fail;
728
729 err = bind_evtchn_to_irqhandler(info->evtchn,
730 blkif_interrupt,
731 IRQF_SAMPLE_RANDOM, "blkif", info);
732 if (err <= 0) {
733 xenbus_dev_fatal(dev, err,
734 "bind_evtchn_to_irqhandler failed");
735 goto fail;
736 }
737 info->irq = err;
738
739 return 0;
740fail:
741 blkif_free(info, 0);
742 return err;
743}
744
745
746/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +0000747static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700748 struct blkfront_info *info)
749{
750 const char *message = NULL;
751 struct xenbus_transaction xbt;
752 int err;
753
754 /* Create shared ring, alloc event channel. */
755 err = setup_blkring(dev, info);
756 if (err)
757 goto out;
758
759again:
760 err = xenbus_transaction_start(&xbt);
761 if (err) {
762 xenbus_dev_fatal(dev, err, "starting transaction");
763 goto destroy_blkring;
764 }
765
766 err = xenbus_printf(xbt, dev->nodename,
767 "ring-ref", "%u", info->ring_ref);
768 if (err) {
769 message = "writing ring-ref";
770 goto abort_transaction;
771 }
772 err = xenbus_printf(xbt, dev->nodename,
773 "event-channel", "%u", info->evtchn);
774 if (err) {
775 message = "writing event-channel";
776 goto abort_transaction;
777 }
Markus Armbruster3e334232008-04-02 10:54:02 -0700778 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
779 XEN_IO_PROTO_ABI_NATIVE);
780 if (err) {
781 message = "writing protocol";
782 goto abort_transaction;
783 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700784
785 err = xenbus_transaction_end(xbt, 0);
786 if (err) {
787 if (err == -EAGAIN)
788 goto again;
789 xenbus_dev_fatal(dev, err, "completing transaction");
790 goto destroy_blkring;
791 }
792
793 xenbus_switch_state(dev, XenbusStateInitialised);
794
795 return 0;
796
797 abort_transaction:
798 xenbus_transaction_end(xbt, 1);
799 if (message)
800 xenbus_dev_fatal(dev, err, "%s", message);
801 destroy_blkring:
802 blkif_free(info, 0);
803 out:
804 return err;
805}
806
807
808/**
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
839 info->xbdev = dev;
840 info->vdevice = vdevice;
841 info->connected = BLKIF_STATE_DISCONNECTED;
842 INIT_WORK(&info->work, blkif_restart_queue);
843
844 for (i = 0; i < BLK_RING_SIZE; i++)
845 info->shadow[i].req.id = i+1;
846 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
847
848 /* Front end dir is a number, which is used as the id. */
849 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700850 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700851
Ian Campbell203fd612009-12-04 15:33:54 +0000852 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700853 if (err) {
854 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700855 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700856 return err;
857 }
858
859 return 0;
860}
861
862
863static int blkif_recover(struct blkfront_info *info)
864{
865 int i;
866 struct blkif_request *req;
867 struct blk_shadow *copy;
868 int j;
869
870 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +0200871 copy = kmalloc(sizeof(info->shadow),
872 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700873 if (!copy)
874 return -ENOMEM;
875 memcpy(copy, info->shadow, sizeof(info->shadow));
876
877 /* Stage 2: Set up free list. */
878 memset(&info->shadow, 0, sizeof(info->shadow));
879 for (i = 0; i < BLK_RING_SIZE; i++)
880 info->shadow[i].req.id = i+1;
881 info->shadow_free = info->ring.req_prod_pvt;
882 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
883
884 /* Stage 3: Find pending requests and requeue them. */
885 for (i = 0; i < BLK_RING_SIZE; i++) {
886 /* Not in use? */
887 if (copy[i].request == 0)
888 continue;
889
890 /* Grab a request slot and copy shadow state into it. */
891 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
892 *req = copy[i].req;
893
894 /* We get a new request id, and must reset the shadow state. */
895 req->id = get_id_from_freelist(info);
896 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
897
898 /* Rewrite any grant references invalidated by susp/resume. */
899 for (j = 0; j < req->nr_segments; j++)
900 gnttab_grant_foreign_access_ref(
901 req->seg[j].gref,
902 info->xbdev->otherend_id,
903 pfn_to_mfn(info->shadow[req->id].frame[j]),
904 rq_data_dir(
905 (struct request *)
906 info->shadow[req->id].request));
907 info->shadow[req->id].req = *req;
908
909 info->ring.req_prod_pvt++;
910 }
911
912 kfree(copy);
913
914 xenbus_switch_state(info->xbdev, XenbusStateConnected);
915
916 spin_lock_irq(&blkif_io_lock);
917
918 /* Now safe for us to use the shared ring */
919 info->connected = BLKIF_STATE_CONNECTED;
920
921 /* Send off requeued requests */
922 flush_requests(info);
923
924 /* Kick any other new requests queued since we resumed */
925 kick_pending_request_queues(info);
926
927 spin_unlock_irq(&blkif_io_lock);
928
929 return 0;
930}
931
932/**
933 * We are reconnecting to the backend, due to a suspend/resume, or a backend
934 * driver restart. We tear down our blkif structure and recreate it, but
935 * leave the device-layer structures intact so that this is transparent to the
936 * rest of the kernel.
937 */
938static int blkfront_resume(struct xenbus_device *dev)
939{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700940 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700941 int err;
942
943 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
944
945 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
946
Ian Campbell203fd612009-12-04 15:33:54 +0000947 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700948 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
949 err = blkif_recover(info);
950
951 return err;
952}
953
954
955/*
956 * Invoked when the backend is finally 'ready' (and has told produced
957 * the details about the physical device - #sectors, size, etc).
958 */
959static void blkfront_connect(struct blkfront_info *info)
960{
961 unsigned long long sectors;
962 unsigned long sector_size;
963 unsigned int binfo;
964 int err;
965
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -0800966 switch (info->connected) {
967 case BLKIF_STATE_CONNECTED:
968 /*
969 * Potentially, the back-end may be signalling
970 * a capacity change; update the capacity.
971 */
972 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
973 "sectors", "%Lu", &sectors);
974 if (XENBUS_EXIST_ERR(err))
975 return;
976 printk(KERN_INFO "Setting capacity to %Lu\n",
977 sectors);
978 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -0700979 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -0800980
981 /* fall through */
982 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700983 return;
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -0800984
985 default:
986 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -0800987 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700988
989 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
990 __func__, info->xbdev->otherend);
991
992 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
993 "sectors", "%llu", &sectors,
994 "info", "%u", &binfo,
995 "sector-size", "%lu", &sector_size,
996 NULL);
997 if (err) {
998 xenbus_dev_fatal(info->xbdev, err,
999 "reading backend fields at %s",
1000 info->xbdev->otherend);
1001 return;
1002 }
1003
1004 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1005 "feature-barrier", "%lu", &info->feature_barrier,
1006 NULL);
1007 if (err)
1008 info->feature_barrier = 0;
1009
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001010 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001011 if (err) {
1012 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1013 info->xbdev->otherend);
1014 return;
1015 }
1016
1017 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1018
1019 /* Kick pending requests. */
1020 spin_lock_irq(&blkif_io_lock);
1021 info->connected = BLKIF_STATE_CONNECTED;
1022 kick_pending_request_queues(info);
1023 spin_unlock_irq(&blkif_io_lock);
1024
1025 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001026
1027 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001028}
1029
1030/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001031 * Callback received when the backend's state changes.
1032 */
Ian Campbell203fd612009-12-04 15:33:54 +00001033static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001034 enum xenbus_state backend_state)
1035{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001036 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001037 struct block_device *bd;
1038
Ian Campbell203fd612009-12-04 15:33:54 +00001039 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001040
1041 switch (backend_state) {
1042 case XenbusStateInitialising:
1043 case XenbusStateInitWait:
1044 case XenbusStateInitialised:
1045 case XenbusStateUnknown:
1046 case XenbusStateClosed:
1047 break;
1048
1049 case XenbusStateConnected:
1050 blkfront_connect(info);
1051 break;
1052
1053 case XenbusStateClosing:
Ian Campbell28afea52009-05-19 08:25:48 +02001054 if (info->gd == NULL) {
1055 xenbus_frontend_closed(dev);
1056 break;
1057 }
Jeremy Fitzhardinge53f0e8a2008-04-02 10:54:03 -07001058 bd = bdget_disk(info->gd, 0);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001059 if (bd == NULL)
1060 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
1061
1062 mutex_lock(&bd->bd_mutex);
1063 if (info->users > 0)
1064 xenbus_dev_error(dev, -EBUSY,
1065 "Device in use; refusing to close");
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001066 else {
1067 xlvbd_release_gendisk(info);
1068 xenbus_frontend_closed(info->xbdev);
1069 }
1070
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001071 mutex_unlock(&bd->bd_mutex);
1072 bdput(bd);
1073 break;
1074 }
1075}
1076
1077static int blkfront_remove(struct xenbus_device *dev)
1078{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001079 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001080
1081 dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
1082
1083 blkif_free(info, 0);
1084
Jan Beulich0e345822010-08-07 18:28:55 +02001085 if(info->users == 0)
1086 kfree(info);
1087 else
Jan Beulich5d7ed202010-08-07 18:31:12 +02001088 info->xbdev = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001089
1090 return 0;
1091}
1092
Christian Limpach1d78d702008-04-02 10:54:04 -07001093static int blkfront_is_ready(struct xenbus_device *dev)
1094{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001095 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001096
Jan Beulich5d7ed202010-08-07 18:31:12 +02001097 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001098}
1099
Al Viroa63c8482008-03-02 10:23:47 -05001100static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001101{
Al Viroa63c8482008-03-02 10:23:47 -05001102 struct blkfront_info *info = bdev->bd_disk->private_data;
Jan Beulich5d7ed202010-08-07 18:31:12 +02001103
1104 if (!info->xbdev)
1105 return -ENODEV;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001106
1107 lock_kernel();
Jan Beulich5d7ed202010-08-07 18:31:12 +02001108 info->users++;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001109 unlock_kernel();
1110
Jan Beulich5d7ed202010-08-07 18:31:12 +02001111 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001112}
1113
Al Viroa63c8482008-03-02 10:23:47 -05001114static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001115{
Al Viroa63c8482008-03-02 10:23:47 -05001116 struct blkfront_info *info = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001117 lock_kernel();
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001118 info->users--;
1119 if (info->users == 0) {
1120 /* Check whether we have been instructed to close. We will
1121 have ignored this request initially, as the device was
1122 still mounted. */
1123 struct xenbus_device *dev = info->xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001124
Jan Beulich5d7ed202010-08-07 18:31:12 +02001125 if (!dev) {
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001126 xlvbd_release_gendisk(info);
Jan Beulich0e345822010-08-07 18:28:55 +02001127 kfree(info);
Jan Beulich5d7ed202010-08-07 18:31:12 +02001128 } else if (xenbus_read_driver_state(dev->otherend)
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001129 == XenbusStateClosing && info->is_ready) {
1130 xlvbd_release_gendisk(info);
1131 xenbus_frontend_closed(dev);
1132 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001133 }
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001134 unlock_kernel();
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001135 return 0;
1136}
1137
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001138static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001139{
1140 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001141 .open = blkif_open,
1142 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001143 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001144 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001145};
1146
1147
Márton Némethec9c42e2010-01-10 13:39:52 +01001148static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001149 { "vbd" },
1150 { "" }
1151};
1152
1153static struct xenbus_driver blkfront = {
1154 .name = "vbd",
1155 .owner = THIS_MODULE,
1156 .ids = blkfront_ids,
1157 .probe = blkfront_probe,
1158 .remove = blkfront_remove,
1159 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001160 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001161 .is_ready = blkfront_is_ready,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001162};
1163
1164static int __init xlblk_init(void)
1165{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001166 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001167 return -ENODEV;
1168
1169 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1170 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1171 XENVBD_MAJOR, DEV_NAME);
1172 return -ENODEV;
1173 }
1174
1175 return xenbus_register_frontend(&blkfront);
1176}
1177module_init(xlblk_init);
1178
1179
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001180static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001181{
1182 return xenbus_unregister_driver(&blkfront);
1183}
1184module_exit(xlblk_exit);
1185
1186MODULE_DESCRIPTION("Xen virtual block device frontend");
1187MODULE_LICENSE("GPL");
1188MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001189MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001190MODULE_ALIAS("xenblk");