blob: 1f1ade6d6e09f26c2636049d0c5876b60c95a52f [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");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200101 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400102 }
103}
104
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400105static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400106{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400107 struct xen_blkif *blkif;
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200108 int i;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400109
Wei Yongjun654dbef2012-08-27 12:28:57 +0800110 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400111 if (!blkif)
112 return ERR_PTR(-ENOMEM);
113
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400114 blkif->domid = domid;
115 spin_lock_init(&blkif->blk_ring_lock);
116 atomic_set(&blkif->refcnt, 1);
117 init_waitqueue_head(&blkif->wq);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400118 init_completion(&blkif->drain_complete);
119 atomic_set(&blkif->drain, 0);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400120 blkif->st_print = jiffies;
121 init_waitqueue_head(&blkif->waiting_to_free);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200122 blkif->persistent_gnts.rb_node = NULL;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200123 spin_lock_init(&blkif->free_pages_lock);
124 INIT_LIST_HEAD(&blkif->free_pages);
125 blkif->free_pages_num = 0;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200126 atomic_set(&blkif->persistent_gnt_in_use, 0);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400127
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200128 blkif->pending_reqs = kcalloc(XEN_BLKIF_REQS,
129 sizeof(blkif->pending_reqs[0]),
130 GFP_KERNEL);
131 if (!blkif->pending_reqs) {
132 kmem_cache_free(xen_blkif_cachep, blkif);
133 return ERR_PTR(-ENOMEM);
134 }
135 INIT_LIST_HEAD(&blkif->pending_free);
136 spin_lock_init(&blkif->pending_free_lock);
137 init_waitqueue_head(&blkif->pending_free_wq);
138
139 for (i = 0; i < XEN_BLKIF_REQS; i++)
140 list_add_tail(&blkif->pending_reqs[i].free_list,
141 &blkif->pending_free);
142
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400143 return blkif;
144}
145
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400146static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400147 unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400148{
149 int err;
150
151 /* Already connected through? */
152 if (blkif->irq)
153 return 0;
154
David Vrabel2d073842011-09-29 16:53:30 +0100155 err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring);
156 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400157 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400158
159 switch (blkif->blk_protocol) {
160 case BLKIF_PROTOCOL_NATIVE:
161 {
162 struct blkif_sring *sring;
David Vrabel2d073842011-09-29 16:53:30 +0100163 sring = (struct blkif_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400164 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
165 break;
166 }
167 case BLKIF_PROTOCOL_X86_32:
168 {
169 struct blkif_x86_32_sring *sring_x86_32;
David Vrabel2d073842011-09-29 16:53:30 +0100170 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400171 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
172 break;
173 }
174 case BLKIF_PROTOCOL_X86_64:
175 {
176 struct blkif_x86_64_sring *sring_x86_64;
David Vrabel2d073842011-09-29 16:53:30 +0100177 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400178 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
179 break;
180 }
181 default:
182 BUG();
183 }
184
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400185 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
186 xen_blkif_be_int, 0,
187 "blkif-backend", blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400188 if (err < 0) {
David Vrabel2d073842011-09-29 16:53:30 +0100189 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400190 blkif->blk_rings.common.sring = NULL;
191 return err;
192 }
193 blkif->irq = err;
194
195 return 0;
196}
197
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400198static void xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400199{
200 if (blkif->xenblkd) {
201 kthread_stop(blkif->xenblkd);
202 blkif->xenblkd = NULL;
203 }
204
205 atomic_dec(&blkif->refcnt);
206 wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
207 atomic_inc(&blkif->refcnt);
208
209 if (blkif->irq) {
210 unbind_from_irqhandler(blkif->irq, blkif);
211 blkif->irq = 0;
212 }
213
214 if (blkif->blk_rings.common.sring) {
David Vrabel2d073842011-09-29 16:53:30 +0100215 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400216 blkif->blk_rings.common.sring = NULL;
217 }
218}
219
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400220static void xen_blkif_free(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400221{
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200222 struct pending_req *req;
223 int i = 0;
224
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400225 if (!atomic_dec_and_test(&blkif->refcnt))
226 BUG();
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200227
228 /* Check that there is no request in use */
229 list_for_each_entry(req, &blkif->pending_free, free_list)
230 i++;
231 BUG_ON(i != XEN_BLKIF_REQS);
232
233 kfree(blkif->pending_reqs);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400234 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400235}
236
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400237int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400238{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400239 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400240 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400241 0, 0, NULL);
242 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400243 return -ENOMEM;
244
245 return 0;
246}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400247
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400248/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400249 * sysfs interface for VBD I/O requests
250 */
251
252#define VBD_SHOW(name, format, args...) \
253 static ssize_t show_##name(struct device *_dev, \
254 struct device_attribute *attr, \
255 char *buf) \
256 { \
257 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800258 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400259 \
260 return sprintf(buf, format, ##args); \
261 } \
262 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
263
Zoltan Kiss986cacb2013-03-11 16:15:50 +0000264VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req);
265VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req);
266VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req);
267VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req);
268VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req);
269VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect);
270VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400271
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400272static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400273 &dev_attr_oo_req.attr,
274 &dev_attr_rd_req.attr,
275 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400276 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800277 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400278 &dev_attr_rd_sect.attr,
279 &dev_attr_wr_sect.attr,
280 NULL
281};
282
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400283static struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400284 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400285 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400286};
287
288VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
289VBD_SHOW(mode, "%s\n", be->mode);
290
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400291static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400292{
293 int error;
294
295 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400296 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400297 goto fail1;
298
299 error = device_create_file(&dev->dev, &dev_attr_mode);
300 if (error)
301 goto fail2;
302
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400303 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400304 if (error)
305 goto fail3;
306
307 return 0;
308
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400309fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400310fail2: device_remove_file(&dev->dev, &dev_attr_mode);
311fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
312 return error;
313}
314
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400315static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400316{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400317 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400318 device_remove_file(&dev->dev, &dev_attr_mode);
319 device_remove_file(&dev->dev, &dev_attr_physical_device);
320}
321
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400322
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400323static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400324{
325 if (vbd->bdev)
326 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
327 vbd->bdev = NULL;
328}
329
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400330static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
331 unsigned major, unsigned minor, int readonly,
332 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400333{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400334 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400335 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400336 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400337
338 vbd = &blkif->vbd;
339 vbd->handle = handle;
340 vbd->readonly = readonly;
341 vbd->type = 0;
342
343 vbd->pdevice = MKDEV(major, minor);
344
345 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
346 FMODE_READ : FMODE_WRITE, NULL);
347
348 if (IS_ERR(bdev)) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400349 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400350 vbd->pdevice);
351 return -ENOENT;
352 }
353
354 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400355 if (vbd->bdev->bd_disk == NULL) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400356 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400357 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400358 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400359 return -ENOENT;
360 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200361 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400362
363 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
364 vbd->type |= VDISK_CDROM;
365 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
366 vbd->type |= VDISK_REMOVABLE;
367
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400368 q = bdev_get_queue(bdev);
369 if (q && q->flush_flags)
370 vbd->flush_support = true;
371
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400372 if (q && blk_queue_secdiscard(q))
373 vbd->discard_secure = true;
374
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400375 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
376 handle, blkif->domid);
377 return 0;
378}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400379static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400380{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800381 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400382
383 DPRINTK("");
384
385 if (be->major || be->minor)
386 xenvbd_sysfs_delif(dev);
387
388 if (be->backend_watch.node) {
389 unregister_xenbus_watch(&be->backend_watch);
390 kfree(be->backend_watch.node);
391 be->backend_watch.node = NULL;
392 }
393
394 if (be->blkif) {
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400395 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400396 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400397 xen_blkif_free(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400398 be->blkif = NULL;
399 }
400
Jan Beulich9d092602012-12-20 10:31:11 +0000401 kfree(be->mode);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400402 kfree(be);
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800403 dev_set_drvdata(&dev->dev, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400404 return 0;
405}
406
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400407int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
408 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400409{
410 struct xenbus_device *dev = be->dev;
411 int err;
412
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400413 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400414 "%d", state);
415 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400416 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400417
418 return err;
419}
420
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400421static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800422{
423 struct xenbus_device *dev = be->dev;
424 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800425 int err;
426 int state = 0;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400427 struct block_device *bdev = be->blkif->vbd.bdev;
428 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800429
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400430 if (blk_queue_discard(q)) {
431 err = xenbus_printf(xbt, dev->nodename,
432 "discard-granularity", "%u",
433 q->limits.discard_granularity);
434 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400435 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
436 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800437 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400438 err = xenbus_printf(xbt, dev->nodename,
439 "discard-alignment", "%u",
440 q->limits.discard_alignment);
441 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400442 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
443 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400444 }
445 state = 1;
446 /* Optional. */
447 err = xenbus_printf(xbt, dev->nodename,
448 "discard-secure", "%d",
449 blkif->vbd.discard_secure);
450 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400451 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400452 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800453 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800454 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800455 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
456 "%d", state);
457 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400458 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800459}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400460int xen_blkbk_barrier(struct xenbus_transaction xbt,
461 struct backend_info *be, int state)
462{
463 struct xenbus_device *dev = be->dev;
464 int err;
465
466 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
467 "%d", state);
468 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400469 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400470
471 return err;
472}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800473
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400474/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400475 * Entry point to this code when a new device is created. Allocate the basic
476 * structures, and watch the store waiting for the hotplug scripts to tell us
477 * the device's physical major and minor numbers. Switch to InitWait.
478 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400479static int xen_blkbk_probe(struct xenbus_device *dev,
480 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400481{
482 int err;
483 struct backend_info *be = kzalloc(sizeof(struct backend_info),
484 GFP_KERNEL);
485 if (!be) {
486 xenbus_dev_fatal(dev, -ENOMEM,
487 "allocating backend structure");
488 return -ENOMEM;
489 }
490 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800491 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400492
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400493 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400494 if (IS_ERR(be->blkif)) {
495 err = PTR_ERR(be->blkif);
496 be->blkif = NULL;
497 xenbus_dev_fatal(dev, err, "creating block interface");
498 goto fail;
499 }
500
501 /* setup back pointer */
502 be->blkif->be = be;
503
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800504 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
505 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400506 if (err)
507 goto fail;
508
509 err = xenbus_switch_state(dev, XenbusStateInitWait);
510 if (err)
511 goto fail;
512
513 return 0;
514
515fail:
516 DPRINTK("failed");
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400517 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400518 return err;
519}
520
521
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400522/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400523 * Callback received when the hotplug scripts have placed the physical-device
524 * node. Read it and the mode node, and create a vbd. If the frontend is
525 * ready, connect.
526 */
527static void backend_changed(struct xenbus_watch *watch,
528 const char **vec, unsigned int len)
529{
530 int err;
531 unsigned major;
532 unsigned minor;
533 struct backend_info *be
534 = container_of(watch, struct backend_info, backend_watch);
535 struct xenbus_device *dev = be->dev;
536 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000537 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400538 char *device_type;
539
540 DPRINTK("");
541
542 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
543 &major, &minor);
544 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400545 /*
546 * Since this watch will fire once immediately after it is
547 * registered, we expect this. Ignore it, and wait for the
548 * hotplug scripts.
549 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400550 return;
551 }
552 if (err != 2) {
553 xenbus_dev_fatal(dev, err, "reading physical-device");
554 return;
555 }
556
Jan Beulich9d092602012-12-20 10:31:11 +0000557 if (be->major | be->minor) {
558 if (be->major != major || be->minor != minor)
559 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
560 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400561 return;
562 }
563
564 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
565 if (IS_ERR(be->mode)) {
566 err = PTR_ERR(be->mode);
567 be->mode = NULL;
568 xenbus_dev_fatal(dev, err, "reading mode");
569 return;
570 }
571
572 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
573 if (!IS_ERR(device_type)) {
574 cdrom = strcmp(device_type, "cdrom") == 0;
575 kfree(device_type);
576 }
577
Jan Beulich9d092602012-12-20 10:31:11 +0000578 /* Front end dir is a number, which is used as the handle. */
579 err = strict_strtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
580 if (err)
581 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400582
Jan Beulich9d092602012-12-20 10:31:11 +0000583 be->major = major;
584 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400585
Jan Beulich9d092602012-12-20 10:31:11 +0000586 err = xen_vbd_create(be->blkif, handle, major, minor,
587 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400588
Jan Beulich9d092602012-12-20 10:31:11 +0000589 if (err)
590 xenbus_dev_fatal(dev, err, "creating vbd structure");
591 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400592 err = xenvbd_sysfs_addif(dev);
593 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400594 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400595 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400596 }
Jan Beulich9d092602012-12-20 10:31:11 +0000597 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400598
Jan Beulich9d092602012-12-20 10:31:11 +0000599 if (err) {
600 kfree(be->mode);
601 be->mode = NULL;
602 be->major = 0;
603 be->minor = 0;
604 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400605 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400606 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400607 }
608}
609
610
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400611/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400612 * Callback received when the frontend's state changes.
613 */
614static void frontend_changed(struct xenbus_device *dev,
615 enum xenbus_state frontend_state)
616{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800617 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400618 int err;
619
620 DPRINTK("%s", xenbus_strstate(frontend_state));
621
622 switch (frontend_state) {
623 case XenbusStateInitialising:
624 if (dev->state == XenbusStateClosed) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400625 pr_info(DRV_PFX "%s: prepare for reconnect\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400626 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400627 xenbus_switch_state(dev, XenbusStateInitWait);
628 }
629 break;
630
631 case XenbusStateInitialised:
632 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400633 /*
634 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800635 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400636 * of frontend_state.
637 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400638 if (dev->state == XenbusStateConnected)
639 break;
640
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400641 /*
642 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800643 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800644 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400645 xen_blkif_disconnect(be->blkif);
Keir Fraser313d7b02010-11-24 22:08:20 -0800646
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400647 err = connect_ring(be);
648 if (err)
649 break;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400650 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400651 break;
652
653 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400654 xenbus_switch_state(dev, XenbusStateClosing);
655 break;
656
657 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800658 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400659 xenbus_switch_state(dev, XenbusStateClosed);
660 if (xenbus_dev_is_online(dev))
661 break;
662 /* fall through if not online */
663 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800664 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400665 device_unregister(&dev->dev);
666 break;
667
668 default:
669 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
670 frontend_state);
671 break;
672 }
673}
674
675
676/* ** Connection ** */
677
678
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400679/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400680 * Write the physical details regarding the block device to the store, and
681 * switch to Connected state.
682 */
683static void connect(struct backend_info *be)
684{
685 struct xenbus_transaction xbt;
686 int err;
687 struct xenbus_device *dev = be->dev;
688
689 DPRINTK("%s", dev->otherend);
690
691 /* Supply the information about the device the frontend needs */
692again:
693 err = xenbus_transaction_start(&xbt);
694 if (err) {
695 xenbus_dev_fatal(dev, err, "starting transaction");
696 return;
697 }
698
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400699 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400700 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
701
702 xen_blkbk_discard(xbt, be);
703
704 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400705
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200706 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
707 if (err) {
708 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
709 dev->nodename);
710 goto abort;
711 }
712
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400713 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400714 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400715 if (err) {
716 xenbus_dev_fatal(dev, err, "writing %s/sectors",
717 dev->nodename);
718 goto abort;
719 }
720
721 /* FIXME: use a typename instead */
722 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400723 be->blkif->vbd.type |
724 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400725 if (err) {
726 xenbus_dev_fatal(dev, err, "writing %s/info",
727 dev->nodename);
728 goto abort;
729 }
730 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400731 (unsigned long)
732 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400733 if (err) {
734 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
735 dev->nodename);
736 goto abort;
737 }
738
739 err = xenbus_transaction_end(xbt, 0);
740 if (err == -EAGAIN)
741 goto again;
742 if (err)
743 xenbus_dev_fatal(dev, err, "ending transaction");
744
745 err = xenbus_switch_state(dev, XenbusStateConnected);
746 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700747 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400748 dev->nodename);
749
750 return;
751 abort:
752 xenbus_transaction_end(xbt, 1);
753}
754
755
756static int connect_ring(struct backend_info *be)
757{
758 struct xenbus_device *dev = be->dev;
759 unsigned long ring_ref;
760 unsigned int evtchn;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200761 unsigned int pers_grants;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400762 char protocol[64] = "";
763 int err;
764
765 DPRINTK("%s", dev->otherend);
766
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400767 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
768 &ring_ref, "event-channel", "%u", &evtchn, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400769 if (err) {
770 xenbus_dev_fatal(dev, err,
771 "reading %s/ring-ref and event-channel",
772 dev->otherend);
773 return err;
774 }
775
776 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
777 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
778 "%63s", protocol, NULL);
779 if (err)
780 strcpy(protocol, "unspecified, assuming native");
781 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
782 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
783 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
784 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
785 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
786 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
787 else {
788 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
789 return -1;
790 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200791 err = xenbus_gather(XBT_NIL, dev->otherend,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100792 "feature-persistent", "%u",
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200793 &pers_grants, NULL);
794 if (err)
795 pers_grants = 0;
796
797 be->blkif->vbd.feature_gnt_persistent = pers_grants;
798 be->blkif->vbd.overflow_max_grants = 0;
799
800 pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
801 ring_ref, evtchn, be->blkif->blk_protocol, protocol,
802 pers_grants ? "persistent grants" : "");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400803
804 /* Map the shared frame, irq etc. */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400805 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400806 if (err) {
807 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
808 ring_ref, evtchn);
809 return err;
810 }
811
812 return 0;
813}
814
815
816/* ** Driver Registration ** */
817
818
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400819static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400820 { "vbd" },
821 { "" }
822};
823
824
Jan Beulich73db1442011-12-22 09:08:13 +0000825static DEFINE_XENBUS_DRIVER(xen_blkbk, ,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400826 .probe = xen_blkbk_probe,
827 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400828 .otherend_changed = frontend_changed
Jan Beulich73db1442011-12-22 09:08:13 +0000829);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400830
831
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400832int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400833{
Jan Beulich73db1442011-12-22 09:08:13 +0000834 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400835}