Commit | Line | Data |
---|---|---|
4d05a28d KRW |
1 | /* 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 | ||
4d05a28d KRW |
15 | */ |
16 | ||
17 | #include <stdarg.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/kthread.h> | |
ee9ff853 KRW |
20 | #include <xen/events.h> |
21 | #include <xen/grant_table.h> | |
4d05a28d KRW |
22 | #include "common.h" |
23 | ||
d6091b21 | 24 | struct backend_info { |
01f37f2d | 25 | struct xenbus_device *dev; |
51854322 | 26 | struct xen_blkif *blkif; |
01f37f2d KRW |
27 | struct xenbus_watch backend_watch; |
28 | unsigned major; | |
29 | unsigned minor; | |
30 | char *mode; | |
4d05a28d KRW |
31 | }; |
32 | ||
8b6bf747 | 33 | static struct kmem_cache *xen_blkif_cachep; |
4d05a28d KRW |
34 | static void connect(struct backend_info *); |
35 | static int connect_ring(struct backend_info *); | |
36 | static void backend_changed(struct xenbus_watch *, const char **, | |
37 | unsigned int); | |
38 | ||
8b6bf747 | 39 | struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be) |
98e036a3 JF |
40 | { |
41 | return be->dev; | |
42 | } | |
43 | ||
30fd1502 | 44 | static int blkback_name(struct xen_blkif *blkif, char *buf) |
4d05a28d KRW |
45 | { |
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 | ||
d6091b21 KRW |
53 | devname = strstr(devpath, "/dev/"); |
54 | if (devname != NULL) | |
4d05a28d KRW |
55 | 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 | ||
30fd1502 | 65 | static void xen_update_blkif_status(struct xen_blkif *blkif) |
4d05a28d KRW |
66 | { |
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 | ||
cbf46290 CL |
89 | 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 | ||
8b6bf747 | 96 | blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, name); |
4d05a28d KRW |
97 | 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 | ||
30fd1502 | 104 | static struct xen_blkif *xen_blkif_alloc(domid_t domid) |
ee9ff853 | 105 | { |
30fd1502 | 106 | struct xen_blkif *blkif; |
ee9ff853 | 107 | |
8b6bf747 | 108 | blkif = kmem_cache_alloc(xen_blkif_cachep, GFP_KERNEL); |
ee9ff853 KRW |
109 | if (!blkif) |
110 | return ERR_PTR(-ENOMEM); | |
111 | ||
112 | memset(blkif, 0, sizeof(*blkif)); | |
113 | blkif->domid = domid; | |
114 | spin_lock_init(&blkif->blk_ring_lock); | |
115 | atomic_set(&blkif->refcnt, 1); | |
116 | init_waitqueue_head(&blkif->wq); | |
29bde093 KRW |
117 | init_completion(&blkif->drain_complete); |
118 | atomic_set(&blkif->drain, 0); | |
ee9ff853 KRW |
119 | blkif->st_print = jiffies; |
120 | init_waitqueue_head(&blkif->waiting_to_free); | |
121 | ||
122 | return blkif; | |
123 | } | |
124 | ||
30fd1502 | 125 | static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page, |
8b6bf747 | 126 | unsigned int evtchn) |
ee9ff853 KRW |
127 | { |
128 | int err; | |
129 | ||
130 | /* Already connected through? */ | |
131 | if (blkif->irq) | |
132 | return 0; | |
133 | ||
2d073846 DV |
134 | err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring); |
135 | if (err < 0) | |
ee9ff853 | 136 | return err; |
ee9ff853 KRW |
137 | |
138 | switch (blkif->blk_protocol) { | |
139 | case BLKIF_PROTOCOL_NATIVE: | |
140 | { | |
141 | struct blkif_sring *sring; | |
2d073846 | 142 | sring = (struct blkif_sring *)blkif->blk_ring; |
ee9ff853 KRW |
143 | 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; | |
2d073846 | 149 | sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring; |
ee9ff853 KRW |
150 | 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; | |
2d073846 | 156 | sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring; |
ee9ff853 KRW |
157 | BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE); |
158 | break; | |
159 | } | |
160 | default: | |
161 | BUG(); | |
162 | } | |
163 | ||
8b6bf747 KRW |
164 | err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn, |
165 | xen_blkif_be_int, 0, | |
166 | "blkif-backend", blkif); | |
ee9ff853 | 167 | if (err < 0) { |
2d073846 | 168 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
ee9ff853 KRW |
169 | blkif->blk_rings.common.sring = NULL; |
170 | return err; | |
171 | } | |
172 | blkif->irq = err; | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
30fd1502 | 177 | static void xen_blkif_disconnect(struct xen_blkif *blkif) |
ee9ff853 KRW |
178 | { |
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) { | |
2d073846 | 194 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
ee9ff853 KRW |
195 | blkif->blk_rings.common.sring = NULL; |
196 | } | |
197 | } | |
198 | ||
30fd1502 | 199 | void xen_blkif_free(struct xen_blkif *blkif) |
ee9ff853 KRW |
200 | { |
201 | if (!atomic_dec_and_test(&blkif->refcnt)) | |
202 | BUG(); | |
8b6bf747 | 203 | kmem_cache_free(xen_blkif_cachep, blkif); |
ee9ff853 KRW |
204 | } |
205 | ||
8b6bf747 | 206 | int __init xen_blkif_interface_init(void) |
ee9ff853 | 207 | { |
8b6bf747 | 208 | xen_blkif_cachep = kmem_cache_create("blkif_cache", |
30fd1502 | 209 | sizeof(struct xen_blkif), |
8b6bf747 KRW |
210 | 0, 0, NULL); |
211 | if (!xen_blkif_cachep) | |
ee9ff853 KRW |
212 | return -ENOMEM; |
213 | ||
214 | return 0; | |
215 | } | |
4d05a28d | 216 | |
a1397fa3 | 217 | /* |
4d05a28d KRW |
218 | * 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); \ | |
5cf6e4f6 | 227 | struct backend_info *be = dev_get_drvdata(&dev->dev); \ |
4d05a28d KRW |
228 | \ |
229 | return sprintf(buf, format, ##args); \ | |
230 | } \ | |
231 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) | |
232 | ||
233 | VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req); | |
234 | VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req); | |
235 | VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req); | |
24f567f9 | 236 | VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req); |
b3cb0d6a | 237 | VBD_SHOW(ds_req, "%d\n", be->blkif->st_ds_req); |
4d05a28d KRW |
238 | VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect); |
239 | VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect); | |
240 | ||
3d814731 | 241 | static struct attribute *xen_vbdstat_attrs[] = { |
4d05a28d KRW |
242 | &dev_attr_oo_req.attr, |
243 | &dev_attr_rd_req.attr, | |
244 | &dev_attr_wr_req.attr, | |
24f567f9 | 245 | &dev_attr_f_req.attr, |
b3cb0d6a | 246 | &dev_attr_ds_req.attr, |
4d05a28d KRW |
247 | &dev_attr_rd_sect.attr, |
248 | &dev_attr_wr_sect.attr, | |
249 | NULL | |
250 | }; | |
251 | ||
3d814731 | 252 | static struct attribute_group xen_vbdstat_group = { |
4d05a28d | 253 | .name = "statistics", |
3d814731 | 254 | .attrs = xen_vbdstat_attrs, |
4d05a28d KRW |
255 | }; |
256 | ||
257 | VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); | |
258 | VBD_SHOW(mode, "%s\n", be->mode); | |
259 | ||
260 | int xenvbd_sysfs_addif(struct xenbus_device *dev) | |
261 | { | |
262 | int error; | |
263 | ||
264 | error = device_create_file(&dev->dev, &dev_attr_physical_device); | |
d6091b21 | 265 | if (error) |
4d05a28d KRW |
266 | goto fail1; |
267 | ||
268 | error = device_create_file(&dev->dev, &dev_attr_mode); | |
269 | if (error) | |
270 | goto fail2; | |
271 | ||
3d814731 | 272 | error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group); |
4d05a28d KRW |
273 | if (error) |
274 | goto fail3; | |
275 | ||
276 | return 0; | |
277 | ||
3d814731 | 278 | fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
4d05a28d KRW |
279 | fail2: device_remove_file(&dev->dev, &dev_attr_mode); |
280 | fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); | |
281 | return error; | |
282 | } | |
283 | ||
284 | void xenvbd_sysfs_delif(struct xenbus_device *dev) | |
285 | { | |
3d814731 | 286 | sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
4d05a28d KRW |
287 | device_remove_file(&dev->dev, &dev_attr_mode); |
288 | device_remove_file(&dev->dev, &dev_attr_physical_device); | |
289 | } | |
290 | ||
42c7841d | 291 | |
3d814731 | 292 | static void xen_vbd_free(struct xen_vbd *vbd) |
42c7841d KRW |
293 | { |
294 | if (vbd->bdev) | |
295 | blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); | |
296 | vbd->bdev = NULL; | |
297 | } | |
298 | ||
3d814731 KRW |
299 | static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle, |
300 | unsigned major, unsigned minor, int readonly, | |
301 | int cdrom) | |
42c7841d | 302 | { |
3d814731 | 303 | struct xen_vbd *vbd; |
42c7841d | 304 | struct block_device *bdev; |
24f567f9 | 305 | struct request_queue *q; |
42c7841d KRW |
306 | |
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)) { | |
3d814731 | 318 | DPRINTK("xen_vbd_create: device %08x could not be opened.\n", |
42c7841d KRW |
319 | vbd->pdevice); |
320 | return -ENOENT; | |
321 | } | |
322 | ||
323 | vbd->bdev = bdev; | |
42c7841d | 324 | if (vbd->bdev->bd_disk == NULL) { |
3d814731 | 325 | DPRINTK("xen_vbd_create: device %08x doesn't exist.\n", |
42c7841d | 326 | vbd->pdevice); |
3d814731 | 327 | xen_vbd_free(vbd); |
42c7841d KRW |
328 | return -ENOENT; |
329 | } | |
6464920a | 330 | vbd->size = vbd_sz(vbd); |
42c7841d KRW |
331 | |
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 | ||
24f567f9 KRW |
337 | q = bdev_get_queue(bdev); |
338 | if (q && q->flush_flags) | |
339 | vbd->flush_support = true; | |
340 | ||
5ea42986 KRW |
341 | if (q && blk_queue_secdiscard(q)) |
342 | vbd->discard_secure = true; | |
343 | ||
42c7841d KRW |
344 | DPRINTK("Successful creation of handle=%04x (dom=%u)\n", |
345 | handle, blkif->domid); | |
346 | return 0; | |
347 | } | |
8b6bf747 | 348 | static int xen_blkbk_remove(struct xenbus_device *dev) |
4d05a28d | 349 | { |
5cf6e4f6 | 350 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
4d05a28d KRW |
351 | |
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) { | |
8b6bf747 | 364 | xen_blkif_disconnect(be->blkif); |
3d814731 | 365 | xen_vbd_free(&be->blkif->vbd); |
8b6bf747 | 366 | xen_blkif_free(be->blkif); |
4d05a28d KRW |
367 | be->blkif = NULL; |
368 | } | |
369 | ||
370 | kfree(be); | |
5cf6e4f6 | 371 | dev_set_drvdata(&dev->dev, NULL); |
4d05a28d KRW |
372 | return 0; |
373 | } | |
374 | ||
24f567f9 KRW |
375 | int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, |
376 | struct backend_info *be, int state) | |
4d05a28d KRW |
377 | { |
378 | struct xenbus_device *dev = be->dev; | |
379 | int err; | |
380 | ||
24f567f9 | 381 | err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache", |
4d05a28d KRW |
382 | "%d", state); |
383 | if (err) | |
24f567f9 | 384 | xenbus_dev_fatal(dev, err, "writing feature-flush-cache"); |
4d05a28d KRW |
385 | |
386 | return err; | |
387 | } | |
388 | ||
b3cb0d6a LD |
389 | int xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be) |
390 | { | |
391 | struct xenbus_device *dev = be->dev; | |
392 | struct xen_blkif *blkif = be->blkif; | |
393 | char *type; | |
394 | int err; | |
395 | int state = 0; | |
396 | ||
397 | type = xenbus_read(XBT_NIL, dev->nodename, "type", NULL); | |
398 | if (!IS_ERR(type)) { | |
399 | if (strncmp(type, "file", 4) == 0) { | |
400 | state = 1; | |
401 | blkif->blk_backend_type = BLKIF_BACKEND_FILE; | |
402 | } | |
403 | if (strncmp(type, "phy", 3) == 0) { | |
404 | struct block_device *bdev = be->blkif->vbd.bdev; | |
405 | struct request_queue *q = bdev_get_queue(bdev); | |
406 | if (blk_queue_discard(q)) { | |
407 | err = xenbus_printf(xbt, dev->nodename, | |
408 | "discard-granularity", "%u", | |
409 | q->limits.discard_granularity); | |
410 | if (err) { | |
411 | xenbus_dev_fatal(dev, err, | |
412 | "writing discard-granularity"); | |
413 | goto kfree; | |
414 | } | |
415 | err = xenbus_printf(xbt, dev->nodename, | |
416 | "discard-alignment", "%u", | |
417 | q->limits.discard_alignment); | |
418 | if (err) { | |
419 | xenbus_dev_fatal(dev, err, | |
420 | "writing discard-alignment"); | |
421 | goto kfree; | |
422 | } | |
423 | state = 1; | |
424 | blkif->blk_backend_type = BLKIF_BACKEND_PHY; | |
425 | } | |
5ea42986 KRW |
426 | /* Optional. */ |
427 | err = xenbus_printf(xbt, dev->nodename, | |
428 | "discard-secure", "%d", | |
429 | blkif->vbd.discard_secure); | |
430 | if (err) { | |
431 | xenbus_dev_fatal(dev, err, | |
432 | "writting discard-secure"); | |
433 | goto kfree; | |
434 | } | |
b3cb0d6a LD |
435 | } |
436 | } else { | |
437 | err = PTR_ERR(type); | |
438 | xenbus_dev_fatal(dev, err, "reading type"); | |
439 | goto out; | |
440 | } | |
441 | ||
442 | err = xenbus_printf(xbt, dev->nodename, "feature-discard", | |
443 | "%d", state); | |
444 | if (err) | |
445 | xenbus_dev_fatal(dev, err, "writing feature-discard"); | |
446 | kfree: | |
447 | kfree(type); | |
448 | out: | |
449 | return err; | |
450 | } | |
29bde093 KRW |
451 | int xen_blkbk_barrier(struct xenbus_transaction xbt, |
452 | struct backend_info *be, int state) | |
453 | { | |
454 | struct xenbus_device *dev = be->dev; | |
455 | int err; | |
456 | ||
457 | err = xenbus_printf(xbt, dev->nodename, "feature-barrier", | |
458 | "%d", state); | |
459 | if (err) | |
460 | xenbus_dev_fatal(dev, err, "writing feature-barrier"); | |
461 | ||
462 | return err; | |
463 | } | |
b3cb0d6a | 464 | |
01f37f2d | 465 | /* |
4d05a28d KRW |
466 | * Entry point to this code when a new device is created. Allocate the basic |
467 | * structures, and watch the store waiting for the hotplug scripts to tell us | |
468 | * the device's physical major and minor numbers. Switch to InitWait. | |
469 | */ | |
8b6bf747 KRW |
470 | static int xen_blkbk_probe(struct xenbus_device *dev, |
471 | const struct xenbus_device_id *id) | |
4d05a28d KRW |
472 | { |
473 | int err; | |
474 | struct backend_info *be = kzalloc(sizeof(struct backend_info), | |
475 | GFP_KERNEL); | |
476 | if (!be) { | |
477 | xenbus_dev_fatal(dev, -ENOMEM, | |
478 | "allocating backend structure"); | |
479 | return -ENOMEM; | |
480 | } | |
481 | be->dev = dev; | |
5cf6e4f6 | 482 | dev_set_drvdata(&dev->dev, be); |
4d05a28d | 483 | |
8b6bf747 | 484 | be->blkif = xen_blkif_alloc(dev->otherend_id); |
4d05a28d KRW |
485 | if (IS_ERR(be->blkif)) { |
486 | err = PTR_ERR(be->blkif); | |
487 | be->blkif = NULL; | |
488 | xenbus_dev_fatal(dev, err, "creating block interface"); | |
489 | goto fail; | |
490 | } | |
491 | ||
492 | /* setup back pointer */ | |
493 | be->blkif->be = be; | |
494 | ||
88122933 JF |
495 | err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed, |
496 | "%s/%s", dev->nodename, "physical-device"); | |
4d05a28d KRW |
497 | if (err) |
498 | goto fail; | |
499 | ||
500 | err = xenbus_switch_state(dev, XenbusStateInitWait); | |
501 | if (err) | |
502 | goto fail; | |
503 | ||
504 | return 0; | |
505 | ||
506 | fail: | |
507 | DPRINTK("failed"); | |
8b6bf747 | 508 | xen_blkbk_remove(dev); |
4d05a28d KRW |
509 | return err; |
510 | } | |
511 | ||
512 | ||
01f37f2d | 513 | /* |
4d05a28d KRW |
514 | * Callback received when the hotplug scripts have placed the physical-device |
515 | * node. Read it and the mode node, and create a vbd. If the frontend is | |
516 | * ready, connect. | |
517 | */ | |
518 | static void backend_changed(struct xenbus_watch *watch, | |
519 | const char **vec, unsigned int len) | |
520 | { | |
521 | int err; | |
522 | unsigned major; | |
523 | unsigned minor; | |
524 | struct backend_info *be | |
525 | = container_of(watch, struct backend_info, backend_watch); | |
526 | struct xenbus_device *dev = be->dev; | |
527 | int cdrom = 0; | |
528 | char *device_type; | |
529 | ||
530 | DPRINTK(""); | |
531 | ||
532 | err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x", | |
533 | &major, &minor); | |
534 | if (XENBUS_EXIST_ERR(err)) { | |
01f37f2d KRW |
535 | /* |
536 | * Since this watch will fire once immediately after it is | |
537 | * registered, we expect this. Ignore it, and wait for the | |
538 | * hotplug scripts. | |
539 | */ | |
4d05a28d KRW |
540 | return; |
541 | } | |
542 | if (err != 2) { | |
543 | xenbus_dev_fatal(dev, err, "reading physical-device"); | |
544 | return; | |
545 | } | |
546 | ||
547 | if ((be->major || be->minor) && | |
548 | ((be->major != major) || (be->minor != minor))) { | |
22b20f2d | 549 | pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n", |
ebe81906 | 550 | be->major, be->minor, major, minor); |
4d05a28d KRW |
551 | return; |
552 | } | |
553 | ||
554 | be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL); | |
555 | if (IS_ERR(be->mode)) { | |
556 | err = PTR_ERR(be->mode); | |
557 | be->mode = NULL; | |
558 | xenbus_dev_fatal(dev, err, "reading mode"); | |
559 | return; | |
560 | } | |
561 | ||
562 | device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL); | |
563 | if (!IS_ERR(device_type)) { | |
564 | cdrom = strcmp(device_type, "cdrom") == 0; | |
565 | kfree(device_type); | |
566 | } | |
567 | ||
568 | if (be->major == 0 && be->minor == 0) { | |
569 | /* Front end dir is a number, which is used as the handle. */ | |
570 | ||
571 | char *p = strrchr(dev->otherend, '/') + 1; | |
d6091b21 KRW |
572 | long handle; |
573 | err = strict_strtoul(p, 0, &handle); | |
574 | if (err) | |
575 | return; | |
4d05a28d KRW |
576 | |
577 | be->major = major; | |
578 | be->minor = minor; | |
579 | ||
3d814731 | 580 | err = xen_vbd_create(be->blkif, handle, major, minor, |
4d05a28d KRW |
581 | (NULL == strchr(be->mode, 'w')), cdrom); |
582 | if (err) { | |
03e0edf9 KRW |
583 | be->major = 0; |
584 | be->minor = 0; | |
4d05a28d KRW |
585 | xenbus_dev_fatal(dev, err, "creating vbd structure"); |
586 | return; | |
587 | } | |
588 | ||
589 | err = xenvbd_sysfs_addif(dev); | |
590 | if (err) { | |
3d814731 | 591 | xen_vbd_free(&be->blkif->vbd); |
03e0edf9 KRW |
592 | be->major = 0; |
593 | be->minor = 0; | |
4d05a28d KRW |
594 | xenbus_dev_fatal(dev, err, "creating sysfs entries"); |
595 | return; | |
596 | } | |
597 | ||
598 | /* We're potentially connected now */ | |
8b6bf747 | 599 | xen_update_blkif_status(be->blkif); |
4d05a28d KRW |
600 | } |
601 | } | |
602 | ||
603 | ||
01f37f2d | 604 | /* |
4d05a28d KRW |
605 | * Callback received when the frontend's state changes. |
606 | */ | |
607 | static void frontend_changed(struct xenbus_device *dev, | |
608 | enum xenbus_state frontend_state) | |
609 | { | |
5cf6e4f6 | 610 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
4d05a28d KRW |
611 | int err; |
612 | ||
613 | DPRINTK("%s", xenbus_strstate(frontend_state)); | |
614 | ||
615 | switch (frontend_state) { | |
616 | case XenbusStateInitialising: | |
617 | if (dev->state == XenbusStateClosed) { | |
22b20f2d | 618 | pr_info(DRV_PFX "%s: prepare for reconnect\n", |
ebe81906 | 619 | dev->nodename); |
4d05a28d KRW |
620 | xenbus_switch_state(dev, XenbusStateInitWait); |
621 | } | |
622 | break; | |
623 | ||
624 | case XenbusStateInitialised: | |
625 | case XenbusStateConnected: | |
01f37f2d KRW |
626 | /* |
627 | * Ensure we connect even when two watches fire in | |
42b2aa86 | 628 | * close succession and we miss the intermediate value |
01f37f2d KRW |
629 | * of frontend_state. |
630 | */ | |
4d05a28d KRW |
631 | if (dev->state == XenbusStateConnected) |
632 | break; | |
633 | ||
01f37f2d KRW |
634 | /* |
635 | * Enforce precondition before potential leak point. | |
1bc05b0a | 636 | * xen_blkif_disconnect() is idempotent. |
313d7b00 | 637 | */ |
8b6bf747 | 638 | xen_blkif_disconnect(be->blkif); |
313d7b00 | 639 | |
4d05a28d KRW |
640 | err = connect_ring(be); |
641 | if (err) | |
642 | break; | |
8b6bf747 | 643 | xen_update_blkif_status(be->blkif); |
4d05a28d KRW |
644 | break; |
645 | ||
646 | case XenbusStateClosing: | |
4d05a28d KRW |
647 | xenbus_switch_state(dev, XenbusStateClosing); |
648 | break; | |
649 | ||
650 | case XenbusStateClosed: | |
6f5986bc | 651 | xen_blkif_disconnect(be->blkif); |
4d05a28d KRW |
652 | xenbus_switch_state(dev, XenbusStateClosed); |
653 | if (xenbus_dev_is_online(dev)) | |
654 | break; | |
655 | /* fall through if not online */ | |
656 | case XenbusStateUnknown: | |
1bc05b0a | 657 | /* implies xen_blkif_disconnect() via xen_blkbk_remove() */ |
4d05a28d KRW |
658 | device_unregister(&dev->dev); |
659 | break; | |
660 | ||
661 | default: | |
662 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", | |
663 | frontend_state); | |
664 | break; | |
665 | } | |
666 | } | |
667 | ||
668 | ||
669 | /* ** Connection ** */ | |
670 | ||
671 | ||
01f37f2d | 672 | /* |
4d05a28d KRW |
673 | * Write the physical details regarding the block device to the store, and |
674 | * switch to Connected state. | |
675 | */ | |
676 | static void connect(struct backend_info *be) | |
677 | { | |
678 | struct xenbus_transaction xbt; | |
679 | int err; | |
680 | struct xenbus_device *dev = be->dev; | |
681 | ||
682 | DPRINTK("%s", dev->otherend); | |
683 | ||
684 | /* Supply the information about the device the frontend needs */ | |
685 | again: | |
686 | err = xenbus_transaction_start(&xbt); | |
687 | if (err) { | |
688 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
689 | return; | |
690 | } | |
691 | ||
24f567f9 | 692 | err = xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support); |
4d05a28d KRW |
693 | if (err) |
694 | goto abort; | |
695 | ||
b3cb0d6a LD |
696 | err = xen_blkbk_discard(xbt, be); |
697 | ||
29bde093 KRW |
698 | /* If we can't advertise it is OK. */ |
699 | err = xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support); | |
700 | ||
4d05a28d | 701 | err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", |
42c7841d | 702 | (unsigned long long)vbd_sz(&be->blkif->vbd)); |
4d05a28d KRW |
703 | if (err) { |
704 | xenbus_dev_fatal(dev, err, "writing %s/sectors", | |
705 | dev->nodename); | |
706 | goto abort; | |
707 | } | |
708 | ||
709 | /* FIXME: use a typename instead */ | |
710 | err = xenbus_printf(xbt, dev->nodename, "info", "%u", | |
42c7841d KRW |
711 | be->blkif->vbd.type | |
712 | (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); | |
4d05a28d KRW |
713 | if (err) { |
714 | xenbus_dev_fatal(dev, err, "writing %s/info", | |
715 | dev->nodename); | |
716 | goto abort; | |
717 | } | |
718 | err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", | |
42c7841d KRW |
719 | (unsigned long) |
720 | bdev_logical_block_size(be->blkif->vbd.bdev)); | |
4d05a28d KRW |
721 | if (err) { |
722 | xenbus_dev_fatal(dev, err, "writing %s/sector-size", | |
723 | dev->nodename); | |
724 | goto abort; | |
725 | } | |
726 | ||
727 | err = xenbus_transaction_end(xbt, 0); | |
728 | if (err == -EAGAIN) | |
729 | goto again; | |
730 | if (err) | |
731 | xenbus_dev_fatal(dev, err, "ending transaction"); | |
732 | ||
733 | err = xenbus_switch_state(dev, XenbusStateConnected); | |
734 | if (err) | |
08b8bfc1 | 735 | xenbus_dev_fatal(dev, err, "%s: switching to Connected state", |
4d05a28d KRW |
736 | dev->nodename); |
737 | ||
738 | return; | |
739 | abort: | |
740 | xenbus_transaction_end(xbt, 1); | |
741 | } | |
742 | ||
743 | ||
744 | static int connect_ring(struct backend_info *be) | |
745 | { | |
746 | struct xenbus_device *dev = be->dev; | |
747 | unsigned long ring_ref; | |
748 | unsigned int evtchn; | |
749 | char protocol[64] = ""; | |
750 | int err; | |
751 | ||
752 | DPRINTK("%s", dev->otherend); | |
753 | ||
d6091b21 KRW |
754 | err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", |
755 | &ring_ref, "event-channel", "%u", &evtchn, NULL); | |
4d05a28d KRW |
756 | if (err) { |
757 | xenbus_dev_fatal(dev, err, | |
758 | "reading %s/ring-ref and event-channel", | |
759 | dev->otherend); | |
760 | return err; | |
761 | } | |
762 | ||
763 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; | |
764 | err = xenbus_gather(XBT_NIL, dev->otherend, "protocol", | |
765 | "%63s", protocol, NULL); | |
766 | if (err) | |
767 | strcpy(protocol, "unspecified, assuming native"); | |
768 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) | |
769 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; | |
770 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) | |
771 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; | |
772 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) | |
773 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; | |
774 | else { | |
775 | xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); | |
776 | return -1; | |
777 | } | |
22b20f2d | 778 | pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s)\n", |
ebe81906 | 779 | ring_ref, evtchn, be->blkif->blk_protocol, protocol); |
4d05a28d KRW |
780 | |
781 | /* Map the shared frame, irq etc. */ | |
8b6bf747 | 782 | err = xen_blkif_map(be->blkif, ring_ref, evtchn); |
4d05a28d KRW |
783 | if (err) { |
784 | xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u", | |
785 | ring_ref, evtchn); | |
786 | return err; | |
787 | } | |
788 | ||
789 | return 0; | |
790 | } | |
791 | ||
792 | ||
793 | /* ** Driver Registration ** */ | |
794 | ||
795 | ||
8b6bf747 | 796 | static const struct xenbus_device_id xen_blkbk_ids[] = { |
4d05a28d KRW |
797 | { "vbd" }, |
798 | { "" } | |
799 | }; | |
800 | ||
801 | ||
73db144b | 802 | static DEFINE_XENBUS_DRIVER(xen_blkbk, , |
8b6bf747 KRW |
803 | .probe = xen_blkbk_probe, |
804 | .remove = xen_blkbk_remove, | |
4d05a28d | 805 | .otherend_changed = frontend_changed |
73db144b | 806 | ); |
4d05a28d KRW |
807 | |
808 | ||
8b6bf747 | 809 | int xen_blkif_xenbus_init(void) |
4d05a28d | 810 | { |
73db144b | 811 | return xenbus_register_backend(&xen_blkbk_driver); |
4d05a28d | 812 | } |