blob: 76b874a791758dd4782450d7053ada3f8148263e [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 Bergmann2a48fc02010-06-02 14:28:52 +020044#include <linux/mutex.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>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010052#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070053
54#include <xen/interface/grant_table.h>
55#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070056#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070057
58#include <asm/xen/hypervisor.h>
59
60enum blkif_state {
61 BLKIF_STATE_DISCONNECTED,
62 BLKIF_STATE_CONNECTED,
63 BLKIF_STATE_SUSPENDED,
64};
65
66struct blk_shadow {
67 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040068 struct request *request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070069 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
70};
71
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070073static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070074
75#define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
76
77/*
78 * We have one of these per vbd, whether ide, scsi or 'other'. They
79 * hang in private_data off the gendisk structure. We may end up
80 * putting all kinds of interesting stuff here :-)
81 */
82struct blkfront_info
83{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +000084 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070085 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070086 struct gendisk *gd;
87 int vdevice;
88 blkif_vdev_t handle;
89 enum blkif_state connected;
90 int ring_ref;
91 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +010092 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070093 unsigned int evtchn, irq;
94 struct request_queue *rq;
95 struct work_struct work;
96 struct gnttab_free_callback callback;
97 struct blk_shadow shadow[BLK_RING_SIZE];
98 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +020099 unsigned int feature_flush;
Christian Limpach1d78d702008-04-02 10:54:04 -0700100 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700101};
102
103static DEFINE_SPINLOCK(blkif_io_lock);
104
Jan Beulich0e345822010-08-07 18:28:55 +0200105static unsigned int nr_minors;
106static unsigned long *minors;
107static DEFINE_SPINLOCK(minor_lock);
108
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700109#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
110 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
111#define GRANT_INVALID_REF 0
112
113#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700114#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700115
116#define BLKIF_MAJOR(dev) ((dev)>>8)
117#define BLKIF_MINOR(dev) ((dev) & 0xff)
118
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700119#define EXT_SHIFT 28
120#define EXTENDED (1<<EXT_SHIFT)
121#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
122#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700123
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700124#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700125
126static int get_id_from_freelist(struct blkfront_info *info)
127{
128 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200129 BUG_ON(free >= BLK_RING_SIZE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700130 info->shadow_free = info->shadow[free].req.id;
131 info->shadow[free].req.id = 0x0fffffee; /* debug */
132 return free;
133}
134
135static void add_id_to_freelist(struct blkfront_info *info,
136 unsigned long id)
137{
138 info->shadow[id].req.id = info->shadow_free;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400139 info->shadow[id].request = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700140 info->shadow_free = id;
141}
142
Jan Beulich0e345822010-08-07 18:28:55 +0200143static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
144{
145 unsigned int end = minor + nr;
146 int rc;
147
148 if (end > nr_minors) {
149 unsigned long *bitmap, *old;
150
151 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
152 GFP_KERNEL);
153 if (bitmap == NULL)
154 return -ENOMEM;
155
156 spin_lock(&minor_lock);
157 if (end > nr_minors) {
158 old = minors;
159 memcpy(bitmap, minors,
160 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
161 minors = bitmap;
162 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
163 } else
164 old = bitmap;
165 spin_unlock(&minor_lock);
166 kfree(old);
167 }
168
169 spin_lock(&minor_lock);
170 if (find_next_bit(minors, end, minor) >= end) {
171 for (; minor < end; ++minor)
172 __set_bit(minor, minors);
173 rc = 0;
174 } else
175 rc = -EBUSY;
176 spin_unlock(&minor_lock);
177
178 return rc;
179}
180
181static void xlbd_release_minors(unsigned int minor, unsigned int nr)
182{
183 unsigned int end = minor + nr;
184
185 BUG_ON(end > nr_minors);
186 spin_lock(&minor_lock);
187 for (; minor < end; ++minor)
188 __clear_bit(minor, minors);
189 spin_unlock(&minor_lock);
190}
191
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700192static void blkif_restart_queue_callback(void *arg)
193{
194 struct blkfront_info *info = (struct blkfront_info *)arg;
195 schedule_work(&info->work);
196}
197
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700198static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800199{
200 /* We don't have real geometry info, but let's at least return
201 values consistent with the size of the device */
202 sector_t nsect = get_capacity(bd->bd_disk);
203 sector_t cylinders = nsect;
204
205 hg->heads = 0xff;
206 hg->sectors = 0x3f;
207 sector_div(cylinders, hg->heads * hg->sectors);
208 hg->cylinders = cylinders;
209 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
210 hg->cylinders = 0xffff;
211 return 0;
212}
213
Al Viroa63c8482008-03-02 10:23:47 -0500214static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200215 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200216{
Al Viroa63c8482008-03-02 10:23:47 -0500217 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200218 int i;
219
220 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
221 command, (long)argument);
222
223 switch (command) {
224 case CDROMMULTISESSION:
225 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
226 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
227 if (put_user(0, (char __user *)(argument + i)))
228 return -EFAULT;
229 return 0;
230
231 case CDROM_GET_CAPABILITY: {
232 struct gendisk *gd = info->gd;
233 if (gd->flags & GENHD_FL_CD)
234 return 0;
235 return -EINVAL;
236 }
237
238 default:
239 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
240 command);*/
241 return -EINVAL; /* same return as native Linux */
242 }
243
244 return 0;
245}
246
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700247/*
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400248 * Generate a Xen blkfront IO request from a blk layer request. Reads
249 * and writes are handled as expected. Since we lack a loose flush
250 * request, we map flushes into a full ordered barrier.
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700251 *
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400252 * @req: a request struct
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700253 */
254static int blkif_queue_request(struct request *req)
255{
256 struct blkfront_info *info = req->rq_disk->private_data;
257 unsigned long buffer_mfn;
258 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700259 unsigned long id;
260 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100261 int i, ref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700262 grant_ref_t gref_head;
Jens Axboe9e973e62009-02-24 08:10:09 +0100263 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700264
265 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
266 return 1;
267
268 if (gnttab_alloc_grant_references(
269 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
270 gnttab_request_free_callback(
271 &info->callback,
272 blkif_restart_queue_callback,
273 info,
274 BLKIF_MAX_SEGMENTS_PER_REQUEST);
275 return 1;
276 }
277
278 /* Fill out a communications ring structure. */
279 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
280 id = get_id_from_freelist(info);
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400281 info->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700282
283 ring_req->id = id;
Tejun Heo83096eb2009-05-07 22:24:39 +0900284 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700285 ring_req->handle = info->handle;
286
287 ring_req->operation = rq_data_dir(req) ?
288 BLKIF_OP_WRITE : BLKIF_OP_READ;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400289
290 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
291 /*
292 * Ideally we could just do an unordered
293 * flush-to-disk, but all we have is a full write
294 * barrier at the moment. However, a barrier write is
295 * a superset of FUA, so we can implement it the same
296 * way. (It's also a FLUSH+FUA, since it is
297 * guaranteed ordered WRT previous writes.)
298 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700299 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400300 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700301
Jens Axboe9e973e62009-02-24 08:10:09 +0100302 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
303 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
304
305 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
306 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
307 fsect = sg->offset >> 9;
308 lsect = fsect + (sg->length >> 9) - 1;
Jens Axboe6c92e692007-08-16 13:43:12 +0200309 /* install a grant reference. */
310 ref = gnttab_claim_grant_reference(&gref_head);
311 BUG_ON(ref == -ENOSPC);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700312
Jens Axboe6c92e692007-08-16 13:43:12 +0200313 gnttab_grant_foreign_access_ref(
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700314 ref,
315 info->xbdev->otherend_id,
316 buffer_mfn,
317 rq_data_dir(req) );
318
Jens Axboe9e973e62009-02-24 08:10:09 +0100319 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
320 ring_req->seg[i] =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700321 (struct blkif_request_segment) {
322 .gref = ref,
323 .first_sect = fsect,
324 .last_sect = lsect };
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700325 }
326
327 info->ring.req_prod_pvt++;
328
329 /* Keep a private copy so we can reissue requests when recovering. */
330 info->shadow[id].req = *ring_req;
331
332 gnttab_free_grant_references(gref_head);
333
334 return 0;
335}
336
337
338static inline void flush_requests(struct blkfront_info *info)
339{
340 int notify;
341
342 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
343
344 if (notify)
345 notify_remote_via_irq(info->irq);
346}
347
348/*
349 * do_blkif_request
350 * read a block; request is in a request queue
351 */
Jens Axboe165125e2007-07-24 09:28:11 +0200352static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700353{
354 struct blkfront_info *info = NULL;
355 struct request *req;
356 int queued;
357
358 pr_debug("Entered do_blkif_request\n");
359
360 queued = 0;
361
Tejun Heo9934c8c2009-05-08 11:54:16 +0900362 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700363 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700364
365 if (RING_FULL(&info->ring))
366 goto wait;
367
Tejun Heo9934c8c2009-05-08 11:54:16 +0900368 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900369
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200370 if (req->cmd_type != REQ_TYPE_FS) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900371 __blk_end_request_all(req, -EIO);
372 continue;
373 }
374
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700375 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900376 "(%u/%u) buffer:%p [%s]\n",
377 req, req->cmd, (unsigned long)blk_rq_pos(req),
378 blk_rq_cur_sectors(req), blk_rq_sectors(req),
379 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700380
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700381 if (blkif_queue_request(req)) {
382 blk_requeue_request(rq, req);
383wait:
384 /* Avoid pointless unplugs. */
385 blk_stop_queue(rq);
386 break;
387 }
388
389 queued++;
390 }
391
392 if (queued != 0)
393 flush_requests(info);
394}
395
396static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
397{
Jens Axboe165125e2007-07-24 09:28:11 +0200398 struct request_queue *rq;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700399
400 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
401 if (rq == NULL)
402 return -1;
403
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900404 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700405
406 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400407 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500408 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700409
410 /* Each segment in a request is up to an aligned page in size. */
411 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
412 blk_queue_max_segment_size(rq, PAGE_SIZE);
413
414 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500415 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700416
417 /* Make sure buffer addresses are sector-aligned. */
418 blk_queue_dma_alignment(rq, 511);
419
Ian Campbell1c91fe12008-06-17 10:47:08 +0200420 /* Make sure we don't use bounce buffers. */
421 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
422
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700423 gd->queue = rq;
424
425 return 0;
426}
427
428
Tejun Heo4913efe2010-09-03 11:56:16 +0200429static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700430{
Tejun Heo4913efe2010-09-03 11:56:16 +0200431 blk_queue_flush(info->rq, info->feature_flush);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700432 printk(KERN_INFO "blkfront: %s: barriers %s\n",
Tejun Heo4913efe2010-09-03 11:56:16 +0200433 info->gd->disk_name,
434 info->feature_flush ? "enabled" : "disabled");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700435}
436
437
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700438static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
439 struct blkfront_info *info,
440 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700441{
442 struct gendisk *gd;
443 int nr_minors = 1;
444 int err = -ENODEV;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700445 unsigned int offset;
446 int minor;
447 int nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700448
449 BUG_ON(info->gd != NULL);
450 BUG_ON(info->rq != NULL);
451
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700452 if ((info->vdevice>>EXT_SHIFT) > 1) {
453 /* this is above the extended range; something is wrong */
454 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
455 return -ENODEV;
456 }
457
458 if (!VDEV_IS_EXTENDED(info->vdevice)) {
459 minor = BLKIF_MINOR(info->vdevice);
460 nr_parts = PARTS_PER_DISK;
461 } else {
462 minor = BLKIF_MINOR_EXT(info->vdevice);
463 nr_parts = PARTS_PER_EXT_DISK;
464 }
465
466 if ((minor % nr_parts) == 0)
467 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700468
Jan Beulich0e345822010-08-07 18:28:55 +0200469 err = xlbd_reserve_minors(minor, nr_minors);
470 if (err)
471 goto out;
472 err = -ENODEV;
473
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700474 gd = alloc_disk(nr_minors);
475 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200476 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700477
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700478 offset = minor / nr_parts;
479
480 if (nr_minors > 1) {
481 if (offset < 26)
482 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
483 else
484 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
485 'a' + ((offset / 26)-1), 'a' + (offset % 26));
486 } else {
487 if (offset < 26)
488 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
489 'a' + offset,
490 minor & (nr_parts - 1));
491 else
492 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
493 'a' + ((offset / 26) - 1),
494 'a' + (offset % 26),
495 minor & (nr_parts - 1));
496 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700497
498 gd->major = XENVBD_MAJOR;
499 gd->first_minor = minor;
500 gd->fops = &xlvbd_block_fops;
501 gd->private_data = info;
502 gd->driverfs_dev = &(info->xbdev->dev);
503 set_capacity(gd, capacity);
504
505 if (xlvbd_init_blk_queue(gd, sector_size)) {
506 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200507 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700508 }
509
510 info->rq = gd->queue;
511 info->gd = gd;
512
Tejun Heo4913efe2010-09-03 11:56:16 +0200513 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700514
515 if (vdisk_info & VDISK_READONLY)
516 set_disk_ro(gd, 1);
517
518 if (vdisk_info & VDISK_REMOVABLE)
519 gd->flags |= GENHD_FL_REMOVABLE;
520
521 if (vdisk_info & VDISK_CDROM)
522 gd->flags |= GENHD_FL_CD;
523
524 return 0;
525
Jan Beulich0e345822010-08-07 18:28:55 +0200526 release:
527 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700528 out:
529 return err;
530}
531
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200532static void xlvbd_release_gendisk(struct blkfront_info *info)
533{
534 unsigned int minor, nr_minors;
535 unsigned long flags;
536
537 if (info->rq == NULL)
538 return;
539
540 spin_lock_irqsave(&blkif_io_lock, flags);
541
542 /* No more blkif_request(). */
543 blk_stop_queue(info->rq);
544
545 /* No more gnttab callback work. */
546 gnttab_cancel_free_callback(&info->callback);
547 spin_unlock_irqrestore(&blkif_io_lock, flags);
548
549 /* Flush gnttab callback work. Must be done with no locks held. */
550 flush_scheduled_work();
551
552 del_gendisk(info->gd);
553
554 minor = info->gd->first_minor;
555 nr_minors = info->gd->minors;
556 xlbd_release_minors(minor, nr_minors);
557
558 blk_cleanup_queue(info->rq);
559 info->rq = NULL;
560
561 put_disk(info->gd);
562 info->gd = NULL;
563}
564
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700565static void kick_pending_request_queues(struct blkfront_info *info)
566{
567 if (!RING_FULL(&info->ring)) {
568 /* Re-enable calldowns. */
569 blk_start_queue(info->rq);
570 /* Kick things off immediately. */
571 do_blkif_request(info->rq);
572 }
573}
574
575static void blkif_restart_queue(struct work_struct *work)
576{
577 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
578
579 spin_lock_irq(&blkif_io_lock);
580 if (info->connected == BLKIF_STATE_CONNECTED)
581 kick_pending_request_queues(info);
582 spin_unlock_irq(&blkif_io_lock);
583}
584
585static void blkif_free(struct blkfront_info *info, int suspend)
586{
587 /* Prevent new requests being issued until we fix things up. */
588 spin_lock_irq(&blkif_io_lock);
589 info->connected = suspend ?
590 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
591 /* No more blkif_request(). */
592 if (info->rq)
593 blk_stop_queue(info->rq);
594 /* No more gnttab callback work. */
595 gnttab_cancel_free_callback(&info->callback);
596 spin_unlock_irq(&blkif_io_lock);
597
598 /* Flush gnttab callback work. Must be done with no locks held. */
599 flush_scheduled_work();
600
601 /* Free resources associated with old device channel. */
602 if (info->ring_ref != GRANT_INVALID_REF) {
603 gnttab_end_foreign_access(info->ring_ref, 0,
604 (unsigned long)info->ring.sring);
605 info->ring_ref = GRANT_INVALID_REF;
606 info->ring.sring = NULL;
607 }
608 if (info->irq)
609 unbind_from_irqhandler(info->irq, info);
610 info->evtchn = info->irq = 0;
611
612}
613
614static void blkif_completion(struct blk_shadow *s)
615{
616 int i;
617 for (i = 0; i < s->req.nr_segments; i++)
618 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
619}
620
621static irqreturn_t blkif_interrupt(int irq, void *dev_id)
622{
623 struct request *req;
624 struct blkif_response *bret;
625 RING_IDX i, rp;
626 unsigned long flags;
627 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500628 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700629
630 spin_lock_irqsave(&blkif_io_lock, flags);
631
632 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
633 spin_unlock_irqrestore(&blkif_io_lock, flags);
634 return IRQ_HANDLED;
635 }
636
637 again:
638 rp = info->ring.sring->rsp_prod;
639 rmb(); /* Ensure we see queued responses up to 'rp'. */
640
641 for (i = info->ring.rsp_cons; i != rp; i++) {
642 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700643
644 bret = RING_GET_RESPONSE(&info->ring, i);
645 id = bret->id;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400646 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700647
648 blkif_completion(&info->shadow[id]);
649
650 add_id_to_freelist(info, id);
651
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500652 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700653 switch (bret->operation) {
654 case BLKIF_OP_WRITE_BARRIER:
655 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
656 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
657 info->gd->disk_name);
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500658 error = -EOPNOTSUPP;
Tejun Heo4913efe2010-09-03 11:56:16 +0200659 info->feature_flush = 0;
660 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700661 }
662 /* fall through */
663 case BLKIF_OP_READ:
664 case BLKIF_OP_WRITE:
665 if (unlikely(bret->status != BLKIF_RSP_OKAY))
666 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
667 "request: %x\n", bret->status);
668
Tejun Heo40cbbb72009-04-23 11:05:19 +0900669 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700670 break;
671 default:
672 BUG();
673 }
674 }
675
676 info->ring.rsp_cons = i;
677
678 if (i != info->ring.req_prod_pvt) {
679 int more_to_do;
680 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
681 if (more_to_do)
682 goto again;
683 } else
684 info->ring.sring->rsp_event = i + 1;
685
686 kick_pending_request_queues(info);
687
688 spin_unlock_irqrestore(&blkif_io_lock, flags);
689
690 return IRQ_HANDLED;
691}
692
693
694static int setup_blkring(struct xenbus_device *dev,
695 struct blkfront_info *info)
696{
697 struct blkif_sring *sring;
698 int err;
699
700 info->ring_ref = GRANT_INVALID_REF;
701
Ian Campbella144ff02008-06-17 10:47:08 +0200702 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700703 if (!sring) {
704 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
705 return -ENOMEM;
706 }
707 SHARED_RING_INIT(sring);
708 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
709
Jens Axboe9e973e62009-02-24 08:10:09 +0100710 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
711
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700712 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
713 if (err < 0) {
714 free_page((unsigned long)sring);
715 info->ring.sring = NULL;
716 goto fail;
717 }
718 info->ring_ref = err;
719
720 err = xenbus_alloc_evtchn(dev, &info->evtchn);
721 if (err)
722 goto fail;
723
724 err = bind_evtchn_to_irqhandler(info->evtchn,
725 blkif_interrupt,
726 IRQF_SAMPLE_RANDOM, "blkif", info);
727 if (err <= 0) {
728 xenbus_dev_fatal(dev, err,
729 "bind_evtchn_to_irqhandler failed");
730 goto fail;
731 }
732 info->irq = err;
733
734 return 0;
735fail:
736 blkif_free(info, 0);
737 return err;
738}
739
740
741/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +0000742static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700743 struct blkfront_info *info)
744{
745 const char *message = NULL;
746 struct xenbus_transaction xbt;
747 int err;
748
749 /* Create shared ring, alloc event channel. */
750 err = setup_blkring(dev, info);
751 if (err)
752 goto out;
753
754again:
755 err = xenbus_transaction_start(&xbt);
756 if (err) {
757 xenbus_dev_fatal(dev, err, "starting transaction");
758 goto destroy_blkring;
759 }
760
761 err = xenbus_printf(xbt, dev->nodename,
762 "ring-ref", "%u", info->ring_ref);
763 if (err) {
764 message = "writing ring-ref";
765 goto abort_transaction;
766 }
767 err = xenbus_printf(xbt, dev->nodename,
768 "event-channel", "%u", info->evtchn);
769 if (err) {
770 message = "writing event-channel";
771 goto abort_transaction;
772 }
Markus Armbruster3e334232008-04-02 10:54:02 -0700773 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
774 XEN_IO_PROTO_ABI_NATIVE);
775 if (err) {
776 message = "writing protocol";
777 goto abort_transaction;
778 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700779
780 err = xenbus_transaction_end(xbt, 0);
781 if (err) {
782 if (err == -EAGAIN)
783 goto again;
784 xenbus_dev_fatal(dev, err, "completing transaction");
785 goto destroy_blkring;
786 }
787
788 xenbus_switch_state(dev, XenbusStateInitialised);
789
790 return 0;
791
792 abort_transaction:
793 xenbus_transaction_end(xbt, 1);
794 if (message)
795 xenbus_dev_fatal(dev, err, "%s", message);
796 destroy_blkring:
797 blkif_free(info, 0);
798 out:
799 return err;
800}
801
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700802/**
803 * Entry point to this code when a new device is created. Allocate the basic
804 * structures and the ring buffer for communication with the backend, and
805 * inform the backend of the appropriate details for those. Switch to
806 * Initialised state.
807 */
808static int blkfront_probe(struct xenbus_device *dev,
809 const struct xenbus_device_id *id)
810{
811 int err, vdevice, i;
812 struct blkfront_info *info;
813
814 /* FIXME: Use dynamic device id if this is not set. */
815 err = xenbus_scanf(XBT_NIL, dev->nodename,
816 "virtual-device", "%i", &vdevice);
817 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700818 /* go looking in the extended area instead */
819 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
820 "%i", &vdevice);
821 if (err != 1) {
822 xenbus_dev_fatal(dev, err, "reading virtual-device");
823 return err;
824 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700825 }
826
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100827 if (xen_hvm_domain()) {
828 char *type;
829 int len;
830 /* no unplug has been done: do not hook devices != xen vbds */
Ian Campbell1dc7ce92010-08-23 11:59:29 +0100831 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100832 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100833
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100834 if (!VDEV_IS_EXTENDED(vdevice))
835 major = BLKIF_MAJOR(vdevice);
836 else
837 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100838
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100839 if (major != XENVBD_MAJOR) {
840 printk(KERN_INFO
841 "%s: HVM does not support vbd %d as xen block device\n",
842 __FUNCTION__, vdevice);
843 return -ENODEV;
844 }
845 }
846 /* do not create a PV cdrom device if we are an HVM guest */
847 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
848 if (IS_ERR(type))
849 return -ENODEV;
850 if (strncmp(type, "cdrom", 5) == 0) {
851 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100852 return -ENODEV;
853 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100854 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100855 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700856 info = kzalloc(sizeof(*info), GFP_KERNEL);
857 if (!info) {
858 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
859 return -ENOMEM;
860 }
861
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000862 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700863 info->xbdev = dev;
864 info->vdevice = vdevice;
865 info->connected = BLKIF_STATE_DISCONNECTED;
866 INIT_WORK(&info->work, blkif_restart_queue);
867
868 for (i = 0; i < BLK_RING_SIZE; i++)
869 info->shadow[i].req.id = i+1;
870 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
871
872 /* Front end dir is a number, which is used as the id. */
873 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700874 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700875
Ian Campbell203fd612009-12-04 15:33:54 +0000876 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700877 if (err) {
878 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700879 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700880 return err;
881 }
882
883 return 0;
884}
885
886
887static int blkif_recover(struct blkfront_info *info)
888{
889 int i;
890 struct blkif_request *req;
891 struct blk_shadow *copy;
892 int j;
893
894 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +0200895 copy = kmalloc(sizeof(info->shadow),
896 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700897 if (!copy)
898 return -ENOMEM;
899 memcpy(copy, info->shadow, sizeof(info->shadow));
900
901 /* Stage 2: Set up free list. */
902 memset(&info->shadow, 0, sizeof(info->shadow));
903 for (i = 0; i < BLK_RING_SIZE; i++)
904 info->shadow[i].req.id = i+1;
905 info->shadow_free = info->ring.req_prod_pvt;
906 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
907
908 /* Stage 3: Find pending requests and requeue them. */
909 for (i = 0; i < BLK_RING_SIZE; i++) {
910 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400911 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700912 continue;
913
914 /* Grab a request slot and copy shadow state into it. */
915 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
916 *req = copy[i].req;
917
918 /* We get a new request id, and must reset the shadow state. */
919 req->id = get_id_from_freelist(info);
920 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
921
922 /* Rewrite any grant references invalidated by susp/resume. */
923 for (j = 0; j < req->nr_segments; j++)
924 gnttab_grant_foreign_access_ref(
925 req->seg[j].gref,
926 info->xbdev->otherend_id,
927 pfn_to_mfn(info->shadow[req->id].frame[j]),
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400928 rq_data_dir(info->shadow[req->id].request));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700929 info->shadow[req->id].req = *req;
930
931 info->ring.req_prod_pvt++;
932 }
933
934 kfree(copy);
935
936 xenbus_switch_state(info->xbdev, XenbusStateConnected);
937
938 spin_lock_irq(&blkif_io_lock);
939
940 /* Now safe for us to use the shared ring */
941 info->connected = BLKIF_STATE_CONNECTED;
942
943 /* Send off requeued requests */
944 flush_requests(info);
945
946 /* Kick any other new requests queued since we resumed */
947 kick_pending_request_queues(info);
948
949 spin_unlock_irq(&blkif_io_lock);
950
951 return 0;
952}
953
954/**
955 * We are reconnecting to the backend, due to a suspend/resume, or a backend
956 * driver restart. We tear down our blkif structure and recreate it, but
957 * leave the device-layer structures intact so that this is transparent to the
958 * rest of the kernel.
959 */
960static int blkfront_resume(struct xenbus_device *dev)
961{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700962 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700963 int err;
964
965 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
966
967 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
968
Ian Campbell203fd612009-12-04 15:33:54 +0000969 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700970 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
971 err = blkif_recover(info);
972
973 return err;
974}
975
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000976static void
977blkfront_closing(struct blkfront_info *info)
978{
979 struct xenbus_device *xbdev = info->xbdev;
980 struct block_device *bdev = NULL;
981
982 mutex_lock(&info->mutex);
983
984 if (xbdev->state == XenbusStateClosing) {
985 mutex_unlock(&info->mutex);
986 return;
987 }
988
989 if (info->gd)
990 bdev = bdget_disk(info->gd, 0);
991
992 mutex_unlock(&info->mutex);
993
994 if (!bdev) {
995 xenbus_frontend_closed(xbdev);
996 return;
997 }
998
999 mutex_lock(&bdev->bd_mutex);
1000
Daniel Stodden7b32d102010-04-30 22:01:23 +00001001 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001002 xenbus_dev_error(xbdev, -EBUSY,
1003 "Device in use; refusing to close");
1004 xenbus_switch_state(xbdev, XenbusStateClosing);
1005 } else {
1006 xlvbd_release_gendisk(info);
1007 xenbus_frontend_closed(xbdev);
1008 }
1009
1010 mutex_unlock(&bdev->bd_mutex);
1011 bdput(bdev);
1012}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001013
1014/*
1015 * Invoked when the backend is finally 'ready' (and has told produced
1016 * the details about the physical device - #sectors, size, etc).
1017 */
1018static void blkfront_connect(struct blkfront_info *info)
1019{
1020 unsigned long long sectors;
1021 unsigned long sector_size;
1022 unsigned int binfo;
1023 int err;
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001024 int barrier;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001025
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001026 switch (info->connected) {
1027 case BLKIF_STATE_CONNECTED:
1028 /*
1029 * Potentially, the back-end may be signalling
1030 * a capacity change; update the capacity.
1031 */
1032 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1033 "sectors", "%Lu", &sectors);
1034 if (XENBUS_EXIST_ERR(err))
1035 return;
1036 printk(KERN_INFO "Setting capacity to %Lu\n",
1037 sectors);
1038 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001039 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001040
1041 /* fall through */
1042 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001043 return;
1044
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001045 default:
1046 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001047 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001048
1049 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1050 __func__, info->xbdev->otherend);
1051
1052 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1053 "sectors", "%llu", &sectors,
1054 "info", "%u", &binfo,
1055 "sector-size", "%lu", &sector_size,
1056 NULL);
1057 if (err) {
1058 xenbus_dev_fatal(info->xbdev, err,
1059 "reading backend fields at %s",
1060 info->xbdev->otherend);
1061 return;
1062 }
1063
1064 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001065 "feature-barrier", "%lu", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001066 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001067
1068 /*
1069 * If there's no "feature-barrier" defined, then it means
1070 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001071 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001072 *
Tejun Heo6958f142010-09-03 11:56:16 +02001073 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001074 */
Tejun Heo4913efe2010-09-03 11:56:16 +02001075 info->feature_flush = 0;
Jens Axboe005a1d12010-10-22 10:58:33 +02001076
Tejun Heo4913efe2010-09-03 11:56:16 +02001077 if (!err && barrier)
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001078 info->feature_flush = REQ_FLUSH | REQ_FUA;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001079
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001080 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001081 if (err) {
1082 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1083 info->xbdev->otherend);
1084 return;
1085 }
1086
1087 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1088
1089 /* Kick pending requests. */
1090 spin_lock_irq(&blkif_io_lock);
1091 info->connected = BLKIF_STATE_CONNECTED;
1092 kick_pending_request_queues(info);
1093 spin_unlock_irq(&blkif_io_lock);
1094
1095 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001096
1097 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001098}
1099
1100/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001101 * Callback received when the backend's state changes.
1102 */
Ian Campbell203fd612009-12-04 15:33:54 +00001103static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001104 enum xenbus_state backend_state)
1105{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001106 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001107
Ian Campbell203fd612009-12-04 15:33:54 +00001108 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001109
1110 switch (backend_state) {
1111 case XenbusStateInitialising:
1112 case XenbusStateInitWait:
1113 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001114 case XenbusStateReconfiguring:
1115 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001116 case XenbusStateUnknown:
1117 case XenbusStateClosed:
1118 break;
1119
1120 case XenbusStateConnected:
1121 blkfront_connect(info);
1122 break;
1123
1124 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001125 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001126 break;
1127 }
1128}
1129
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001130static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001131{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001132 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1133 struct block_device *bdev = NULL;
1134 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001135
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001136 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001137
1138 blkif_free(info, 0);
1139
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001140 mutex_lock(&info->mutex);
1141
1142 disk = info->gd;
1143 if (disk)
1144 bdev = bdget_disk(disk, 0);
1145
1146 info->xbdev = NULL;
1147 mutex_unlock(&info->mutex);
1148
1149 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001150 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001151 return 0;
1152 }
1153
1154 /*
1155 * The xbdev was removed before we reached the Closed
1156 * state. See if it's safe to remove the disk. If the bdev
1157 * isn't closed yet, we let release take care of it.
1158 */
1159
1160 mutex_lock(&bdev->bd_mutex);
1161 info = disk->private_data;
1162
Daniel Stoddend54142c2010-08-07 18:51:21 +02001163 dev_warn(disk_to_dev(disk),
1164 "%s was hot-unplugged, %d stale handles\n",
1165 xbdev->nodename, bdev->bd_openers);
1166
Daniel Stodden7b32d102010-04-30 22:01:23 +00001167 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001168 xlvbd_release_gendisk(info);
1169 disk->private_data = NULL;
1170 kfree(info);
1171 }
1172
1173 mutex_unlock(&bdev->bd_mutex);
1174 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001175
1176 return 0;
1177}
1178
Christian Limpach1d78d702008-04-02 10:54:04 -07001179static int blkfront_is_ready(struct xenbus_device *dev)
1180{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001181 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001182
Jan Beulich5d7ed202010-08-07 18:31:12 +02001183 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001184}
1185
Al Viroa63c8482008-03-02 10:23:47 -05001186static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001187{
Daniel Stodden13961742010-08-07 18:36:53 +02001188 struct gendisk *disk = bdev->bd_disk;
1189 struct blkfront_info *info;
1190 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001191
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001192 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001193
Daniel Stodden13961742010-08-07 18:36:53 +02001194 info = disk->private_data;
1195 if (!info) {
1196 /* xbdev gone */
1197 err = -ERESTARTSYS;
1198 goto out;
1199 }
1200
1201 mutex_lock(&info->mutex);
1202
1203 if (!info->gd)
1204 /* xbdev is closed */
1205 err = -ERESTARTSYS;
1206
1207 mutex_unlock(&info->mutex);
1208
Daniel Stodden13961742010-08-07 18:36:53 +02001209out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001210 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02001211 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001212}
1213
Al Viroa63c8482008-03-02 10:23:47 -05001214static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001215{
Al Viroa63c8482008-03-02 10:23:47 -05001216 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001217 struct block_device *bdev;
1218 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001219
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001220 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001221
1222 bdev = bdget_disk(disk, 0);
1223 bdput(bdev);
1224
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001225 if (bdev->bd_openers)
1226 goto out;
1227
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001228 /*
1229 * Check if we have been instructed to close. We will have
1230 * deferred this request, because the bdev was still open.
1231 */
1232
1233 mutex_lock(&info->mutex);
1234 xbdev = info->xbdev;
1235
1236 if (xbdev && xbdev->state == XenbusStateClosing) {
1237 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001238 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001239 xlvbd_release_gendisk(info);
1240 xenbus_frontend_closed(info->xbdev);
1241 }
1242
1243 mutex_unlock(&info->mutex);
1244
1245 if (!xbdev) {
1246 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001247 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001248 xlvbd_release_gendisk(info);
1249 disk->private_data = NULL;
1250 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001251 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001252
Jens Axboea4cc14e2010-08-08 21:50:05 -04001253out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001254 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001255 return 0;
1256}
1257
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001258static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001259{
1260 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001261 .open = blkif_open,
1262 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001263 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001264 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001265};
1266
1267
Márton Némethec9c42e2010-01-10 13:39:52 +01001268static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001269 { "vbd" },
1270 { "" }
1271};
1272
1273static struct xenbus_driver blkfront = {
1274 .name = "vbd",
1275 .owner = THIS_MODULE,
1276 .ids = blkfront_ids,
1277 .probe = blkfront_probe,
1278 .remove = blkfront_remove,
1279 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001280 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001281 .is_ready = blkfront_is_ready,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001282};
1283
1284static int __init xlblk_init(void)
1285{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001286 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001287 return -ENODEV;
1288
1289 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1290 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1291 XENVBD_MAJOR, DEV_NAME);
1292 return -ENODEV;
1293 }
1294
1295 return xenbus_register_frontend(&blkfront);
1296}
1297module_init(xlblk_init);
1298
1299
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001300static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001301{
1302 return xenbus_unregister_driver(&blkfront);
1303}
1304module_exit(xlblk_exit);
1305
1306MODULE_DESCRIPTION("Xen virtual block device frontend");
1307MODULE_LICENSE("GPL");
1308MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001309MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f092008-04-02 10:54:06 -07001310MODULE_ALIAS("xenblk");