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