blob: 63980722db41af223941be7e83b33001c89bd9c7 [file] [log] [blame]
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001/* Xenbus code for blkif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040015*/
16
17#include <stdarg.h>
18#include <linux/module.h>
19#include <linux/kthread.h>
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -040020#include <xen/events.h>
21#include <xen/grant_table.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040022#include "common.h"
23
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040024struct backend_info {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040025 struct xenbus_device *dev;
Konrad Rzeszutek Wilk51854322011-05-12 18:02:28 -040026 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040027 struct xenbus_watch backend_watch;
28 unsigned major;
29 unsigned minor;
30 char *mode;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040031};
32
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040033static struct kmem_cache *xen_blkif_cachep;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040034static void connect(struct backend_info *);
35static int connect_ring(struct backend_info *);
36static void backend_changed(struct xenbus_watch *, const char **,
37 unsigned int);
38
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040039struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070040{
41 return be->dev;
42}
43
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040044static int blkback_name(struct xen_blkif *blkif, char *buf)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040045{
46 char *devpath, *devname;
47 struct xenbus_device *dev = blkif->be->dev;
48
49 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
50 if (IS_ERR(devpath))
51 return PTR_ERR(devpath);
52
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040053 devname = strstr(devpath, "/dev/");
54 if (devname != NULL)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040055 devname += strlen("/dev/");
56 else
57 devname = devpath;
58
59 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
60 kfree(devpath);
61
62 return 0;
63}
64
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040065static void xen_update_blkif_status(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040066{
67 int err;
68 char name[TASK_COMM_LEN];
69
70 /* Not ready to connect? */
71 if (!blkif->irq || !blkif->vbd.bdev)
72 return;
73
74 /* Already connected? */
75 if (blkif->be->dev->state == XenbusStateConnected)
76 return;
77
78 /* Attempt to connect: exit if we fail to. */
79 connect(blkif->be);
80 if (blkif->be->dev->state != XenbusStateConnected)
81 return;
82
83 err = blkback_name(blkif, name);
84 if (err) {
85 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
86 return;
87 }
88
Chris Lalancettecbf46292010-07-21 12:41:45 -070089 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
90 if (err) {
91 xenbus_dev_error(blkif->be->dev, err, "block flush");
92 return;
93 }
94 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
95
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040096 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, name);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040097 if (IS_ERR(blkif->xenblkd)) {
98 err = PTR_ERR(blkif->xenblkd);
99 blkif->xenblkd = NULL;
100 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
101 }
102}
103
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400104static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400105{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400106 struct xen_blkif *blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400107
Wei Yongjun654dbef2012-08-27 12:28:57 +0800108 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400109 if (!blkif)
110 return ERR_PTR(-ENOMEM);
111
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400112 blkif->domid = domid;
113 spin_lock_init(&blkif->blk_ring_lock);
114 atomic_set(&blkif->refcnt, 1);
115 init_waitqueue_head(&blkif->wq);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400116 init_completion(&blkif->drain_complete);
117 atomic_set(&blkif->drain, 0);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400118 blkif->st_print = jiffies;
119 init_waitqueue_head(&blkif->waiting_to_free);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200120 blkif->persistent_gnts.rb_node = NULL;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400121
122 return blkif;
123}
124
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400125static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400126 unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400127{
128 int err;
129
130 /* Already connected through? */
131 if (blkif->irq)
132 return 0;
133
David Vrabel2d073842011-09-29 16:53:30 +0100134 err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring);
135 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400136 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400137
138 switch (blkif->blk_protocol) {
139 case BLKIF_PROTOCOL_NATIVE:
140 {
141 struct blkif_sring *sring;
David Vrabel2d073842011-09-29 16:53:30 +0100142 sring = (struct blkif_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400143 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
144 break;
145 }
146 case BLKIF_PROTOCOL_X86_32:
147 {
148 struct blkif_x86_32_sring *sring_x86_32;
David Vrabel2d073842011-09-29 16:53:30 +0100149 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400150 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
151 break;
152 }
153 case BLKIF_PROTOCOL_X86_64:
154 {
155 struct blkif_x86_64_sring *sring_x86_64;
David Vrabel2d073842011-09-29 16:53:30 +0100156 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400157 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
158 break;
159 }
160 default:
161 BUG();
162 }
163
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400164 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
165 xen_blkif_be_int, 0,
166 "blkif-backend", blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400167 if (err < 0) {
David Vrabel2d073842011-09-29 16:53:30 +0100168 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400169 blkif->blk_rings.common.sring = NULL;
170 return err;
171 }
172 blkif->irq = err;
173
174 return 0;
175}
176
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400177static void xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400178{
179 if (blkif->xenblkd) {
180 kthread_stop(blkif->xenblkd);
181 blkif->xenblkd = NULL;
182 }
183
184 atomic_dec(&blkif->refcnt);
185 wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
186 atomic_inc(&blkif->refcnt);
187
188 if (blkif->irq) {
189 unbind_from_irqhandler(blkif->irq, blkif);
190 blkif->irq = 0;
191 }
192
193 if (blkif->blk_rings.common.sring) {
David Vrabel2d073842011-09-29 16:53:30 +0100194 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400195 blkif->blk_rings.common.sring = NULL;
196 }
197}
198
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400199static void xen_blkif_free(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400200{
201 if (!atomic_dec_and_test(&blkif->refcnt))
202 BUG();
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400203 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400204}
205
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400206int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400207{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400208 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400209 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400210 0, 0, NULL);
211 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400212 return -ENOMEM;
213
214 return 0;
215}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400216
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400217/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400218 * sysfs interface for VBD I/O requests
219 */
220
221#define VBD_SHOW(name, format, args...) \
222 static ssize_t show_##name(struct device *_dev, \
223 struct device_attribute *attr, \
224 char *buf) \
225 { \
226 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800227 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400228 \
229 return sprintf(buf, format, ##args); \
230 } \
231 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
232
233VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
234VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
235VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400236VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800237VBD_SHOW(ds_req, "%d\n", be->blkif->st_ds_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400238VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
239VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
240
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400241static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400242 &dev_attr_oo_req.attr,
243 &dev_attr_rd_req.attr,
244 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400245 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800246 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400247 &dev_attr_rd_sect.attr,
248 &dev_attr_wr_sect.attr,
249 NULL
250};
251
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400252static struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400253 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400254 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400255};
256
257VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
258VBD_SHOW(mode, "%s\n", be->mode);
259
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400260static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400261{
262 int error;
263
264 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400265 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400266 goto fail1;
267
268 error = device_create_file(&dev->dev, &dev_attr_mode);
269 if (error)
270 goto fail2;
271
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400272 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400273 if (error)
274 goto fail3;
275
276 return 0;
277
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400278fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400279fail2: device_remove_file(&dev->dev, &dev_attr_mode);
280fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
281 return error;
282}
283
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400284static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400285{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400286 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400287 device_remove_file(&dev->dev, &dev_attr_mode);
288 device_remove_file(&dev->dev, &dev_attr_physical_device);
289}
290
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400291
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400292static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400293{
294 if (vbd->bdev)
295 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
296 vbd->bdev = NULL;
297}
298
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400299static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
300 unsigned major, unsigned minor, int readonly,
301 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400302{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400303 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400304 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400305 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400306
307 vbd = &blkif->vbd;
308 vbd->handle = handle;
309 vbd->readonly = readonly;
310 vbd->type = 0;
311
312 vbd->pdevice = MKDEV(major, minor);
313
314 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
315 FMODE_READ : FMODE_WRITE, NULL);
316
317 if (IS_ERR(bdev)) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400318 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400319 vbd->pdevice);
320 return -ENOENT;
321 }
322
323 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400324 if (vbd->bdev->bd_disk == NULL) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400325 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400326 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400327 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400328 return -ENOENT;
329 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200330 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400331
332 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
333 vbd->type |= VDISK_CDROM;
334 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
335 vbd->type |= VDISK_REMOVABLE;
336
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400337 q = bdev_get_queue(bdev);
338 if (q && q->flush_flags)
339 vbd->flush_support = true;
340
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400341 if (q && blk_queue_secdiscard(q))
342 vbd->discard_secure = true;
343
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400344 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
345 handle, blkif->domid);
346 return 0;
347}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400348static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400349{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800350 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400351
352 DPRINTK("");
353
354 if (be->major || be->minor)
355 xenvbd_sysfs_delif(dev);
356
357 if (be->backend_watch.node) {
358 unregister_xenbus_watch(&be->backend_watch);
359 kfree(be->backend_watch.node);
360 be->backend_watch.node = NULL;
361 }
362
363 if (be->blkif) {
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400364 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400365 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400366 xen_blkif_free(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400367 be->blkif = NULL;
368 }
369
370 kfree(be);
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800371 dev_set_drvdata(&dev->dev, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400372 return 0;
373}
374
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400375int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
376 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400377{
378 struct xenbus_device *dev = be->dev;
379 int err;
380
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400381 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400382 "%d", state);
383 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400384 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400385
386 return err;
387}
388
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400389static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800390{
391 struct xenbus_device *dev = be->dev;
392 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800393 int err;
394 int state = 0;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400395 struct block_device *bdev = be->blkif->vbd.bdev;
396 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800397
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400398 if (blk_queue_discard(q)) {
399 err = xenbus_printf(xbt, dev->nodename,
400 "discard-granularity", "%u",
401 q->limits.discard_granularity);
402 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400403 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
404 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800405 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400406 err = xenbus_printf(xbt, dev->nodename,
407 "discard-alignment", "%u",
408 q->limits.discard_alignment);
409 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400410 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
411 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400412 }
413 state = 1;
414 /* Optional. */
415 err = xenbus_printf(xbt, dev->nodename,
416 "discard-secure", "%d",
417 blkif->vbd.discard_secure);
418 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400419 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400420 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800421 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800422 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800423 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
424 "%d", state);
425 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400426 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800427}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400428int xen_blkbk_barrier(struct xenbus_transaction xbt,
429 struct backend_info *be, int state)
430{
431 struct xenbus_device *dev = be->dev;
432 int err;
433
434 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
435 "%d", state);
436 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400437 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400438
439 return err;
440}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800441
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400442/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400443 * Entry point to this code when a new device is created. Allocate the basic
444 * structures, and watch the store waiting for the hotplug scripts to tell us
445 * the device's physical major and minor numbers. Switch to InitWait.
446 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400447static int xen_blkbk_probe(struct xenbus_device *dev,
448 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400449{
450 int err;
451 struct backend_info *be = kzalloc(sizeof(struct backend_info),
452 GFP_KERNEL);
453 if (!be) {
454 xenbus_dev_fatal(dev, -ENOMEM,
455 "allocating backend structure");
456 return -ENOMEM;
457 }
458 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800459 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400460
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400461 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400462 if (IS_ERR(be->blkif)) {
463 err = PTR_ERR(be->blkif);
464 be->blkif = NULL;
465 xenbus_dev_fatal(dev, err, "creating block interface");
466 goto fail;
467 }
468
469 /* setup back pointer */
470 be->blkif->be = be;
471
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800472 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
473 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400474 if (err)
475 goto fail;
476
477 err = xenbus_switch_state(dev, XenbusStateInitWait);
478 if (err)
479 goto fail;
480
481 return 0;
482
483fail:
484 DPRINTK("failed");
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400485 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400486 return err;
487}
488
489
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400490/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400491 * Callback received when the hotplug scripts have placed the physical-device
492 * node. Read it and the mode node, and create a vbd. If the frontend is
493 * ready, connect.
494 */
495static void backend_changed(struct xenbus_watch *watch,
496 const char **vec, unsigned int len)
497{
498 int err;
499 unsigned major;
500 unsigned minor;
501 struct backend_info *be
502 = container_of(watch, struct backend_info, backend_watch);
503 struct xenbus_device *dev = be->dev;
504 int cdrom = 0;
505 char *device_type;
506
507 DPRINTK("");
508
509 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
510 &major, &minor);
511 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400512 /*
513 * Since this watch will fire once immediately after it is
514 * registered, we expect this. Ignore it, and wait for the
515 * hotplug scripts.
516 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400517 return;
518 }
519 if (err != 2) {
520 xenbus_dev_fatal(dev, err, "reading physical-device");
521 return;
522 }
523
524 if ((be->major || be->minor) &&
525 ((be->major != major) || (be->minor != minor))) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400526 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400527 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400528 return;
529 }
530
531 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
532 if (IS_ERR(be->mode)) {
533 err = PTR_ERR(be->mode);
534 be->mode = NULL;
535 xenbus_dev_fatal(dev, err, "reading mode");
536 return;
537 }
538
539 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
540 if (!IS_ERR(device_type)) {
541 cdrom = strcmp(device_type, "cdrom") == 0;
542 kfree(device_type);
543 }
544
545 if (be->major == 0 && be->minor == 0) {
546 /* Front end dir is a number, which is used as the handle. */
547
548 char *p = strrchr(dev->otherend, '/') + 1;
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400549 long handle;
550 err = strict_strtoul(p, 0, &handle);
551 if (err)
552 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400553
554 be->major = major;
555 be->minor = minor;
556
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400557 err = xen_vbd_create(be->blkif, handle, major, minor,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400558 (NULL == strchr(be->mode, 'w')), cdrom);
559 if (err) {
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400560 be->major = 0;
561 be->minor = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400562 xenbus_dev_fatal(dev, err, "creating vbd structure");
563 return;
564 }
565
566 err = xenvbd_sysfs_addif(dev);
567 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400568 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400569 be->major = 0;
570 be->minor = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400571 xenbus_dev_fatal(dev, err, "creating sysfs entries");
572 return;
573 }
574
575 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400576 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400577 }
578}
579
580
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400581/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400582 * Callback received when the frontend's state changes.
583 */
584static void frontend_changed(struct xenbus_device *dev,
585 enum xenbus_state frontend_state)
586{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800587 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400588 int err;
589
590 DPRINTK("%s", xenbus_strstate(frontend_state));
591
592 switch (frontend_state) {
593 case XenbusStateInitialising:
594 if (dev->state == XenbusStateClosed) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400595 pr_info(DRV_PFX "%s: prepare for reconnect\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400596 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400597 xenbus_switch_state(dev, XenbusStateInitWait);
598 }
599 break;
600
601 case XenbusStateInitialised:
602 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400603 /*
604 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800605 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400606 * of frontend_state.
607 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400608 if (dev->state == XenbusStateConnected)
609 break;
610
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400611 /*
612 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800613 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800614 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400615 xen_blkif_disconnect(be->blkif);
Keir Fraser313d7b02010-11-24 22:08:20 -0800616
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400617 err = connect_ring(be);
618 if (err)
619 break;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400620 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400621 break;
622
623 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400624 xenbus_switch_state(dev, XenbusStateClosing);
625 break;
626
627 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800628 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400629 xenbus_switch_state(dev, XenbusStateClosed);
630 if (xenbus_dev_is_online(dev))
631 break;
632 /* fall through if not online */
633 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800634 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400635 device_unregister(&dev->dev);
636 break;
637
638 default:
639 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
640 frontend_state);
641 break;
642 }
643}
644
645
646/* ** Connection ** */
647
648
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400649/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400650 * Write the physical details regarding the block device to the store, and
651 * switch to Connected state.
652 */
653static void connect(struct backend_info *be)
654{
655 struct xenbus_transaction xbt;
656 int err;
657 struct xenbus_device *dev = be->dev;
658
659 DPRINTK("%s", dev->otherend);
660
661 /* Supply the information about the device the frontend needs */
662again:
663 err = xenbus_transaction_start(&xbt);
664 if (err) {
665 xenbus_dev_fatal(dev, err, "starting transaction");
666 return;
667 }
668
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400669 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400670 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
671
672 xen_blkbk_discard(xbt, be);
673
674 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400675
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200676 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
677 if (err) {
678 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
679 dev->nodename);
680 goto abort;
681 }
682
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400683 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400684 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400685 if (err) {
686 xenbus_dev_fatal(dev, err, "writing %s/sectors",
687 dev->nodename);
688 goto abort;
689 }
690
691 /* FIXME: use a typename instead */
692 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400693 be->blkif->vbd.type |
694 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400695 if (err) {
696 xenbus_dev_fatal(dev, err, "writing %s/info",
697 dev->nodename);
698 goto abort;
699 }
700 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400701 (unsigned long)
702 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400703 if (err) {
704 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
705 dev->nodename);
706 goto abort;
707 }
708
709 err = xenbus_transaction_end(xbt, 0);
710 if (err == -EAGAIN)
711 goto again;
712 if (err)
713 xenbus_dev_fatal(dev, err, "ending transaction");
714
715 err = xenbus_switch_state(dev, XenbusStateConnected);
716 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700717 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400718 dev->nodename);
719
720 return;
721 abort:
722 xenbus_transaction_end(xbt, 1);
723}
724
725
726static int connect_ring(struct backend_info *be)
727{
728 struct xenbus_device *dev = be->dev;
729 unsigned long ring_ref;
730 unsigned int evtchn;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200731 unsigned int pers_grants;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400732 char protocol[64] = "";
733 int err;
734
735 DPRINTK("%s", dev->otherend);
736
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400737 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
738 &ring_ref, "event-channel", "%u", &evtchn, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400739 if (err) {
740 xenbus_dev_fatal(dev, err,
741 "reading %s/ring-ref and event-channel",
742 dev->otherend);
743 return err;
744 }
745
746 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
747 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
748 "%63s", protocol, NULL);
749 if (err)
750 strcpy(protocol, "unspecified, assuming native");
751 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
752 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
753 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
754 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
755 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
756 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
757 else {
758 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
759 return -1;
760 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200761 err = xenbus_gather(XBT_NIL, dev->otherend,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100762 "feature-persistent", "%u",
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200763 &pers_grants, NULL);
764 if (err)
765 pers_grants = 0;
766
767 be->blkif->vbd.feature_gnt_persistent = pers_grants;
768 be->blkif->vbd.overflow_max_grants = 0;
769
770 pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
771 ring_ref, evtchn, be->blkif->blk_protocol, protocol,
772 pers_grants ? "persistent grants" : "");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400773
774 /* Map the shared frame, irq etc. */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400775 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400776 if (err) {
777 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
778 ring_ref, evtchn);
779 return err;
780 }
781
782 return 0;
783}
784
785
786/* ** Driver Registration ** */
787
788
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400789static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400790 { "vbd" },
791 { "" }
792};
793
794
Jan Beulich73db1442011-12-22 09:08:13 +0000795static DEFINE_XENBUS_DRIVER(xen_blkbk, ,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400796 .probe = xen_blkbk_probe,
797 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400798 .otherend_changed = frontend_changed
Jan Beulich73db1442011-12-22 09:08:13 +0000799);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400800
801
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400802int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400803{
Jan Beulich73db1442011-12-22 09:08:13 +0000804 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400805}