]>
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 | ||
24 | #undef DPRINTK | |
25 | #define DPRINTK(fmt, args...) \ | |
26 | pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \ | |
d6091b21 | 27 | __func__, __LINE__, ##args) |
4d05a28d | 28 | |
d6091b21 | 29 | struct backend_info { |
4d05a28d | 30 | struct xenbus_device *dev; |
5489377c | 31 | struct blkif_st *blkif; |
4d05a28d KRW |
32 | struct xenbus_watch backend_watch; |
33 | unsigned major; | |
34 | unsigned minor; | |
35 | char *mode; | |
36 | }; | |
37 | ||
ee9ff853 | 38 | static struct kmem_cache *blkif_cachep; |
4d05a28d KRW |
39 | static void connect(struct backend_info *); |
40 | static int connect_ring(struct backend_info *); | |
41 | static void backend_changed(struct xenbus_watch *, const char **, | |
42 | unsigned int); | |
43 | ||
98e036a3 JF |
44 | struct xenbus_device *blkback_xenbus(struct backend_info *be) |
45 | { | |
46 | return be->dev; | |
47 | } | |
48 | ||
5489377c | 49 | static int blkback_name(struct blkif_st *blkif, char *buf) |
4d05a28d KRW |
50 | { |
51 | char *devpath, *devname; | |
52 | struct xenbus_device *dev = blkif->be->dev; | |
53 | ||
54 | devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL); | |
55 | if (IS_ERR(devpath)) | |
56 | return PTR_ERR(devpath); | |
57 | ||
d6091b21 KRW |
58 | devname = strstr(devpath, "/dev/"); |
59 | if (devname != NULL) | |
4d05a28d KRW |
60 | devname += strlen("/dev/"); |
61 | else | |
62 | devname = devpath; | |
63 | ||
64 | snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname); | |
65 | kfree(devpath); | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
5489377c | 70 | static void update_blkif_status(struct blkif_st *blkif) |
4d05a28d KRW |
71 | { |
72 | int err; | |
73 | char name[TASK_COMM_LEN]; | |
74 | ||
75 | /* Not ready to connect? */ | |
76 | if (!blkif->irq || !blkif->vbd.bdev) | |
77 | return; | |
78 | ||
79 | /* Already connected? */ | |
80 | if (blkif->be->dev->state == XenbusStateConnected) | |
81 | return; | |
82 | ||
83 | /* Attempt to connect: exit if we fail to. */ | |
84 | connect(blkif->be); | |
85 | if (blkif->be->dev->state != XenbusStateConnected) | |
86 | return; | |
87 | ||
88 | err = blkback_name(blkif, name); | |
89 | if (err) { | |
90 | xenbus_dev_error(blkif->be->dev, err, "get blkback dev name"); | |
91 | return; | |
92 | } | |
93 | ||
cbf46290 CL |
94 | err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping); |
95 | if (err) { | |
96 | xenbus_dev_error(blkif->be->dev, err, "block flush"); | |
97 | return; | |
98 | } | |
99 | invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping); | |
100 | ||
4d05a28d KRW |
101 | blkif->xenblkd = kthread_run(blkif_schedule, blkif, name); |
102 | if (IS_ERR(blkif->xenblkd)) { | |
103 | err = PTR_ERR(blkif->xenblkd); | |
104 | blkif->xenblkd = NULL; | |
105 | xenbus_dev_error(blkif->be->dev, err, "start xenblkd"); | |
106 | } | |
107 | } | |
108 | ||
ee9ff853 KRW |
109 | struct blkif_st *blkif_alloc(domid_t domid) |
110 | { | |
111 | struct blkif_st *blkif; | |
112 | ||
113 | blkif = kmem_cache_alloc(blkif_cachep, GFP_KERNEL); | |
114 | if (!blkif) | |
115 | return ERR_PTR(-ENOMEM); | |
116 | ||
117 | memset(blkif, 0, sizeof(*blkif)); | |
118 | blkif->domid = domid; | |
119 | spin_lock_init(&blkif->blk_ring_lock); | |
120 | atomic_set(&blkif->refcnt, 1); | |
121 | init_waitqueue_head(&blkif->wq); | |
122 | blkif->st_print = jiffies; | |
123 | init_waitqueue_head(&blkif->waiting_to_free); | |
124 | ||
125 | return blkif; | |
126 | } | |
127 | ||
128 | static int map_frontend_page(struct blkif_st *blkif, unsigned long shared_page) | |
129 | { | |
130 | struct gnttab_map_grant_ref op; | |
131 | ||
132 | gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr, | |
133 | GNTMAP_host_map, shared_page, blkif->domid); | |
134 | ||
135 | if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1)) | |
136 | BUG(); | |
137 | ||
138 | if (op.status) { | |
139 | DPRINTK(" Grant table operation failure !\n"); | |
140 | return op.status; | |
141 | } | |
142 | ||
143 | blkif->shmem_ref = shared_page; | |
144 | blkif->shmem_handle = op.handle; | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | static void unmap_frontend_page(struct blkif_st *blkif) | |
150 | { | |
151 | struct gnttab_unmap_grant_ref op; | |
152 | ||
153 | gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr, | |
154 | GNTMAP_host_map, blkif->shmem_handle); | |
155 | ||
156 | if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1)) | |
157 | BUG(); | |
158 | } | |
159 | ||
160 | int blkif_map(struct blkif_st *blkif, unsigned long shared_page, | |
161 | unsigned int evtchn) | |
162 | { | |
163 | int err; | |
164 | ||
165 | /* Already connected through? */ | |
166 | if (blkif->irq) | |
167 | return 0; | |
168 | ||
169 | blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE); | |
170 | if (!blkif->blk_ring_area) | |
171 | return -ENOMEM; | |
172 | ||
173 | err = map_frontend_page(blkif, shared_page); | |
174 | if (err) { | |
175 | free_vm_area(blkif->blk_ring_area); | |
176 | return err; | |
177 | } | |
178 | ||
179 | switch (blkif->blk_protocol) { | |
180 | case BLKIF_PROTOCOL_NATIVE: | |
181 | { | |
182 | struct blkif_sring *sring; | |
183 | sring = (struct blkif_sring *)blkif->blk_ring_area->addr; | |
184 | BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE); | |
185 | break; | |
186 | } | |
187 | case BLKIF_PROTOCOL_X86_32: | |
188 | { | |
189 | struct blkif_x86_32_sring *sring_x86_32; | |
190 | sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr; | |
191 | BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE); | |
192 | break; | |
193 | } | |
194 | case BLKIF_PROTOCOL_X86_64: | |
195 | { | |
196 | struct blkif_x86_64_sring *sring_x86_64; | |
197 | sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr; | |
198 | BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE); | |
199 | break; | |
200 | } | |
201 | default: | |
202 | BUG(); | |
203 | } | |
204 | ||
205 | err = bind_interdomain_evtchn_to_irqhandler( | |
206 | blkif->domid, evtchn, blkif_be_int, 0, "blkif-backend", blkif); | |
207 | if (err < 0) { | |
208 | unmap_frontend_page(blkif); | |
209 | free_vm_area(blkif->blk_ring_area); | |
210 | blkif->blk_rings.common.sring = NULL; | |
211 | return err; | |
212 | } | |
213 | blkif->irq = err; | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | void blkif_disconnect(struct blkif_st *blkif) | |
219 | { | |
220 | if (blkif->xenblkd) { | |
221 | kthread_stop(blkif->xenblkd); | |
222 | blkif->xenblkd = NULL; | |
223 | } | |
224 | ||
225 | atomic_dec(&blkif->refcnt); | |
226 | wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0); | |
227 | atomic_inc(&blkif->refcnt); | |
228 | ||
229 | if (blkif->irq) { | |
230 | unbind_from_irqhandler(blkif->irq, blkif); | |
231 | blkif->irq = 0; | |
232 | } | |
233 | ||
234 | if (blkif->blk_rings.common.sring) { | |
235 | unmap_frontend_page(blkif); | |
236 | free_vm_area(blkif->blk_ring_area); | |
237 | blkif->blk_rings.common.sring = NULL; | |
238 | } | |
239 | } | |
240 | ||
241 | void blkif_free(struct blkif_st *blkif) | |
242 | { | |
243 | if (!atomic_dec_and_test(&blkif->refcnt)) | |
244 | BUG(); | |
245 | kmem_cache_free(blkif_cachep, blkif); | |
246 | } | |
247 | ||
248 | int __init blkif_interface_init(void) | |
249 | { | |
250 | blkif_cachep = kmem_cache_create("blkif_cache", sizeof(struct blkif_st), | |
251 | 0, 0, NULL); | |
252 | if (!blkif_cachep) | |
253 | return -ENOMEM; | |
254 | ||
255 | return 0; | |
256 | } | |
4d05a28d | 257 | |
a1397fa3 | 258 | /* |
4d05a28d KRW |
259 | * sysfs interface for VBD I/O requests |
260 | */ | |
261 | ||
262 | #define VBD_SHOW(name, format, args...) \ | |
263 | static ssize_t show_##name(struct device *_dev, \ | |
264 | struct device_attribute *attr, \ | |
265 | char *buf) \ | |
266 | { \ | |
267 | struct xenbus_device *dev = to_xenbus_device(_dev); \ | |
5cf6e4f6 | 268 | struct backend_info *be = dev_get_drvdata(&dev->dev); \ |
4d05a28d KRW |
269 | \ |
270 | return sprintf(buf, format, ##args); \ | |
271 | } \ | |
272 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) | |
273 | ||
274 | VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req); | |
275 | VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req); | |
276 | VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req); | |
277 | VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req); | |
278 | VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect); | |
279 | VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect); | |
280 | ||
281 | static struct attribute *vbdstat_attrs[] = { | |
282 | &dev_attr_oo_req.attr, | |
283 | &dev_attr_rd_req.attr, | |
284 | &dev_attr_wr_req.attr, | |
285 | &dev_attr_br_req.attr, | |
286 | &dev_attr_rd_sect.attr, | |
287 | &dev_attr_wr_sect.attr, | |
288 | NULL | |
289 | }; | |
290 | ||
291 | static struct attribute_group vbdstat_group = { | |
292 | .name = "statistics", | |
293 | .attrs = vbdstat_attrs, | |
294 | }; | |
295 | ||
296 | VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); | |
297 | VBD_SHOW(mode, "%s\n", be->mode); | |
298 | ||
299 | int xenvbd_sysfs_addif(struct xenbus_device *dev) | |
300 | { | |
301 | int error; | |
302 | ||
303 | error = device_create_file(&dev->dev, &dev_attr_physical_device); | |
d6091b21 | 304 | if (error) |
4d05a28d KRW |
305 | goto fail1; |
306 | ||
307 | error = device_create_file(&dev->dev, &dev_attr_mode); | |
308 | if (error) | |
309 | goto fail2; | |
310 | ||
311 | error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group); | |
312 | if (error) | |
313 | goto fail3; | |
314 | ||
315 | return 0; | |
316 | ||
317 | fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group); | |
318 | fail2: device_remove_file(&dev->dev, &dev_attr_mode); | |
319 | fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); | |
320 | return error; | |
321 | } | |
322 | ||
323 | void xenvbd_sysfs_delif(struct xenbus_device *dev) | |
324 | { | |
325 | sysfs_remove_group(&dev->dev.kobj, &vbdstat_group); | |
326 | device_remove_file(&dev->dev, &dev_attr_mode); | |
327 | device_remove_file(&dev->dev, &dev_attr_physical_device); | |
328 | } | |
329 | ||
42c7841d KRW |
330 | |
331 | static void vbd_free(struct vbd *vbd) | |
332 | { | |
333 | if (vbd->bdev) | |
334 | blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); | |
335 | vbd->bdev = NULL; | |
336 | } | |
337 | ||
338 | static int vbd_create(struct blkif_st *blkif, blkif_vdev_t handle, | |
339 | unsigned major, unsigned minor, int readonly, | |
340 | int cdrom) | |
341 | { | |
342 | struct vbd *vbd; | |
343 | struct block_device *bdev; | |
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)) { | |
356 | DPRINTK("vbd_creat: device %08x could not be opened.\n", | |
357 | vbd->pdevice); | |
358 | return -ENOENT; | |
359 | } | |
360 | ||
361 | vbd->bdev = bdev; | |
362 | vbd->size = vbd_sz(vbd); | |
363 | ||
364 | if (vbd->bdev->bd_disk == NULL) { | |
365 | DPRINTK("vbd_creat: device %08x doesn't exist.\n", | |
366 | vbd->pdevice); | |
367 | vbd_free(vbd); | |
368 | return -ENOENT; | |
369 | } | |
370 | ||
371 | if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom) | |
372 | vbd->type |= VDISK_CDROM; | |
373 | if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) | |
374 | vbd->type |= VDISK_REMOVABLE; | |
375 | ||
376 | DPRINTK("Successful creation of handle=%04x (dom=%u)\n", | |
377 | handle, blkif->domid); | |
378 | return 0; | |
379 | } | |
4d05a28d KRW |
380 | static int blkback_remove(struct xenbus_device *dev) |
381 | { | |
5cf6e4f6 | 382 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
4d05a28d KRW |
383 | |
384 | DPRINTK(""); | |
385 | ||
386 | if (be->major || be->minor) | |
387 | xenvbd_sysfs_delif(dev); | |
388 | ||
389 | if (be->backend_watch.node) { | |
390 | unregister_xenbus_watch(&be->backend_watch); | |
391 | kfree(be->backend_watch.node); | |
392 | be->backend_watch.node = NULL; | |
393 | } | |
394 | ||
395 | if (be->blkif) { | |
396 | blkif_disconnect(be->blkif); | |
397 | vbd_free(&be->blkif->vbd); | |
398 | blkif_free(be->blkif); | |
399 | be->blkif = NULL; | |
400 | } | |
401 | ||
402 | kfree(be); | |
5cf6e4f6 | 403 | dev_set_drvdata(&dev->dev, NULL); |
4d05a28d KRW |
404 | return 0; |
405 | } | |
406 | ||
407 | int blkback_barrier(struct xenbus_transaction xbt, | |
408 | struct backend_info *be, int state) | |
409 | { | |
410 | struct xenbus_device *dev = be->dev; | |
411 | int err; | |
412 | ||
413 | err = xenbus_printf(xbt, dev->nodename, "feature-barrier", | |
414 | "%d", state); | |
415 | if (err) | |
416 | xenbus_dev_fatal(dev, err, "writing feature-barrier"); | |
417 | ||
418 | return err; | |
419 | } | |
420 | ||
421 | /** | |
422 | * Entry point to this code when a new device is created. Allocate the basic | |
423 | * structures, and watch the store waiting for the hotplug scripts to tell us | |
424 | * the device's physical major and minor numbers. Switch to InitWait. | |
425 | */ | |
426 | static int blkback_probe(struct xenbus_device *dev, | |
427 | const struct xenbus_device_id *id) | |
428 | { | |
429 | int err; | |
430 | struct backend_info *be = kzalloc(sizeof(struct backend_info), | |
431 | GFP_KERNEL); | |
432 | if (!be) { | |
433 | xenbus_dev_fatal(dev, -ENOMEM, | |
434 | "allocating backend structure"); | |
435 | return -ENOMEM; | |
436 | } | |
437 | be->dev = dev; | |
5cf6e4f6 | 438 | dev_set_drvdata(&dev->dev, be); |
4d05a28d KRW |
439 | |
440 | be->blkif = blkif_alloc(dev->otherend_id); | |
441 | if (IS_ERR(be->blkif)) { | |
442 | err = PTR_ERR(be->blkif); | |
443 | be->blkif = NULL; | |
444 | xenbus_dev_fatal(dev, err, "creating block interface"); | |
445 | goto fail; | |
446 | } | |
447 | ||
448 | /* setup back pointer */ | |
449 | be->blkif->be = be; | |
450 | ||
88122933 JF |
451 | err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed, |
452 | "%s/%s", dev->nodename, "physical-device"); | |
4d05a28d KRW |
453 | if (err) |
454 | goto fail; | |
455 | ||
456 | err = xenbus_switch_state(dev, XenbusStateInitWait); | |
457 | if (err) | |
458 | goto fail; | |
459 | ||
460 | return 0; | |
461 | ||
462 | fail: | |
463 | DPRINTK("failed"); | |
464 | blkback_remove(dev); | |
465 | return err; | |
466 | } | |
467 | ||
468 | ||
469 | /** | |
470 | * Callback received when the hotplug scripts have placed the physical-device | |
471 | * node. Read it and the mode node, and create a vbd. If the frontend is | |
472 | * ready, connect. | |
473 | */ | |
474 | static void backend_changed(struct xenbus_watch *watch, | |
475 | const char **vec, unsigned int len) | |
476 | { | |
477 | int err; | |
478 | unsigned major; | |
479 | unsigned minor; | |
480 | struct backend_info *be | |
481 | = container_of(watch, struct backend_info, backend_watch); | |
482 | struct xenbus_device *dev = be->dev; | |
483 | int cdrom = 0; | |
484 | char *device_type; | |
485 | ||
486 | DPRINTK(""); | |
487 | ||
488 | err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x", | |
489 | &major, &minor); | |
490 | if (XENBUS_EXIST_ERR(err)) { | |
491 | /* Since this watch will fire once immediately after it is | |
492 | registered, we expect this. Ignore it, and wait for the | |
493 | hotplug scripts. */ | |
494 | return; | |
495 | } | |
496 | if (err != 2) { | |
497 | xenbus_dev_fatal(dev, err, "reading physical-device"); | |
498 | return; | |
499 | } | |
500 | ||
501 | if ((be->major || be->minor) && | |
502 | ((be->major != major) || (be->minor != minor))) { | |
503 | printk(KERN_WARNING | |
504 | "blkback: changing physical device (from %x:%x to " | |
505 | "%x:%x) not supported.\n", be->major, be->minor, | |
506 | major, minor); | |
507 | return; | |
508 | } | |
509 | ||
510 | be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL); | |
511 | if (IS_ERR(be->mode)) { | |
512 | err = PTR_ERR(be->mode); | |
513 | be->mode = NULL; | |
514 | xenbus_dev_fatal(dev, err, "reading mode"); | |
515 | return; | |
516 | } | |
517 | ||
518 | device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL); | |
519 | if (!IS_ERR(device_type)) { | |
520 | cdrom = strcmp(device_type, "cdrom") == 0; | |
521 | kfree(device_type); | |
522 | } | |
523 | ||
524 | if (be->major == 0 && be->minor == 0) { | |
525 | /* Front end dir is a number, which is used as the handle. */ | |
526 | ||
527 | char *p = strrchr(dev->otherend, '/') + 1; | |
d6091b21 KRW |
528 | long handle; |
529 | err = strict_strtoul(p, 0, &handle); | |
530 | if (err) | |
531 | return; | |
4d05a28d KRW |
532 | |
533 | be->major = major; | |
534 | be->minor = minor; | |
535 | ||
536 | err = vbd_create(be->blkif, handle, major, minor, | |
537 | (NULL == strchr(be->mode, 'w')), cdrom); | |
538 | if (err) { | |
539 | be->major = be->minor = 0; | |
540 | xenbus_dev_fatal(dev, err, "creating vbd structure"); | |
541 | return; | |
542 | } | |
543 | ||
544 | err = xenvbd_sysfs_addif(dev); | |
545 | if (err) { | |
546 | vbd_free(&be->blkif->vbd); | |
547 | be->major = be->minor = 0; | |
548 | xenbus_dev_fatal(dev, err, "creating sysfs entries"); | |
549 | return; | |
550 | } | |
551 | ||
552 | /* We're potentially connected now */ | |
553 | update_blkif_status(be->blkif); | |
554 | } | |
555 | } | |
556 | ||
557 | ||
558 | /** | |
559 | * Callback received when the frontend's state changes. | |
560 | */ | |
561 | static void frontend_changed(struct xenbus_device *dev, | |
562 | enum xenbus_state frontend_state) | |
563 | { | |
5cf6e4f6 | 564 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
4d05a28d KRW |
565 | int err; |
566 | ||
567 | DPRINTK("%s", xenbus_strstate(frontend_state)); | |
568 | ||
569 | switch (frontend_state) { | |
570 | case XenbusStateInitialising: | |
571 | if (dev->state == XenbusStateClosed) { | |
572 | printk(KERN_INFO "%s: %s: prepare for reconnect\n", | |
d6091b21 | 573 | __func__, dev->nodename); |
4d05a28d KRW |
574 | xenbus_switch_state(dev, XenbusStateInitWait); |
575 | } | |
576 | break; | |
577 | ||
578 | case XenbusStateInitialised: | |
579 | case XenbusStateConnected: | |
580 | /* Ensure we connect even when two watches fire in | |
581 | close successsion and we miss the intermediate value | |
582 | of frontend_state. */ | |
583 | if (dev->state == XenbusStateConnected) | |
584 | break; | |
585 | ||
313d7b00 KF |
586 | /* Enforce precondition before potential leak point. |
587 | * blkif_disconnect() is idempotent. | |
588 | */ | |
589 | blkif_disconnect(be->blkif); | |
590 | ||
4d05a28d KRW |
591 | err = connect_ring(be); |
592 | if (err) | |
593 | break; | |
594 | update_blkif_status(be->blkif); | |
595 | break; | |
596 | ||
597 | case XenbusStateClosing: | |
598 | blkif_disconnect(be->blkif); | |
599 | xenbus_switch_state(dev, XenbusStateClosing); | |
600 | break; | |
601 | ||
602 | case XenbusStateClosed: | |
603 | xenbus_switch_state(dev, XenbusStateClosed); | |
604 | if (xenbus_dev_is_online(dev)) | |
605 | break; | |
606 | /* fall through if not online */ | |
607 | case XenbusStateUnknown: | |
313d7b00 | 608 | /* implies blkif_disconnect() via blkback_remove() */ |
4d05a28d KRW |
609 | device_unregister(&dev->dev); |
610 | break; | |
611 | ||
612 | default: | |
613 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", | |
614 | frontend_state); | |
615 | break; | |
616 | } | |
617 | } | |
618 | ||
619 | ||
620 | /* ** Connection ** */ | |
621 | ||
622 | ||
623 | /** | |
624 | * Write the physical details regarding the block device to the store, and | |
625 | * switch to Connected state. | |
626 | */ | |
627 | static void connect(struct backend_info *be) | |
628 | { | |
629 | struct xenbus_transaction xbt; | |
630 | int err; | |
631 | struct xenbus_device *dev = be->dev; | |
632 | ||
633 | DPRINTK("%s", dev->otherend); | |
634 | ||
635 | /* Supply the information about the device the frontend needs */ | |
636 | again: | |
637 | err = xenbus_transaction_start(&xbt); | |
638 | if (err) { | |
639 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
640 | return; | |
641 | } | |
642 | ||
643 | err = blkback_barrier(xbt, be, 1); | |
644 | if (err) | |
645 | goto abort; | |
646 | ||
647 | err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", | |
42c7841d | 648 | (unsigned long long)vbd_sz(&be->blkif->vbd)); |
4d05a28d KRW |
649 | if (err) { |
650 | xenbus_dev_fatal(dev, err, "writing %s/sectors", | |
651 | dev->nodename); | |
652 | goto abort; | |
653 | } | |
654 | ||
655 | /* FIXME: use a typename instead */ | |
656 | err = xenbus_printf(xbt, dev->nodename, "info", "%u", | |
42c7841d KRW |
657 | be->blkif->vbd.type | |
658 | (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); | |
4d05a28d KRW |
659 | if (err) { |
660 | xenbus_dev_fatal(dev, err, "writing %s/info", | |
661 | dev->nodename); | |
662 | goto abort; | |
663 | } | |
664 | err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", | |
42c7841d KRW |
665 | (unsigned long) |
666 | bdev_logical_block_size(be->blkif->vbd.bdev)); | |
4d05a28d KRW |
667 | if (err) { |
668 | xenbus_dev_fatal(dev, err, "writing %s/sector-size", | |
669 | dev->nodename); | |
670 | goto abort; | |
671 | } | |
672 | ||
673 | err = xenbus_transaction_end(xbt, 0); | |
674 | if (err == -EAGAIN) | |
675 | goto again; | |
676 | if (err) | |
677 | xenbus_dev_fatal(dev, err, "ending transaction"); | |
678 | ||
679 | err = xenbus_switch_state(dev, XenbusStateConnected); | |
680 | if (err) | |
681 | xenbus_dev_fatal(dev, err, "switching to Connected state", | |
682 | dev->nodename); | |
683 | ||
684 | return; | |
685 | abort: | |
686 | xenbus_transaction_end(xbt, 1); | |
687 | } | |
688 | ||
689 | ||
690 | static int connect_ring(struct backend_info *be) | |
691 | { | |
692 | struct xenbus_device *dev = be->dev; | |
693 | unsigned long ring_ref; | |
694 | unsigned int evtchn; | |
695 | char protocol[64] = ""; | |
696 | int err; | |
697 | ||
698 | DPRINTK("%s", dev->otherend); | |
699 | ||
d6091b21 KRW |
700 | err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", |
701 | &ring_ref, "event-channel", "%u", &evtchn, NULL); | |
4d05a28d KRW |
702 | if (err) { |
703 | xenbus_dev_fatal(dev, err, | |
704 | "reading %s/ring-ref and event-channel", | |
705 | dev->otherend); | |
706 | return err; | |
707 | } | |
708 | ||
709 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; | |
710 | err = xenbus_gather(XBT_NIL, dev->otherend, "protocol", | |
711 | "%63s", protocol, NULL); | |
712 | if (err) | |
713 | strcpy(protocol, "unspecified, assuming native"); | |
714 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) | |
715 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; | |
716 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) | |
717 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; | |
718 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) | |
719 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; | |
720 | else { | |
721 | xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); | |
722 | return -1; | |
723 | } | |
724 | printk(KERN_INFO | |
725 | "blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n", | |
726 | ring_ref, evtchn, be->blkif->blk_protocol, protocol); | |
727 | ||
728 | /* Map the shared frame, irq etc. */ | |
729 | err = blkif_map(be->blkif, ring_ref, evtchn); | |
730 | if (err) { | |
731 | xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u", | |
732 | ring_ref, evtchn); | |
733 | return err; | |
734 | } | |
735 | ||
736 | return 0; | |
737 | } | |
738 | ||
739 | ||
740 | /* ** Driver Registration ** */ | |
741 | ||
742 | ||
743 | static const struct xenbus_device_id blkback_ids[] = { | |
744 | { "vbd" }, | |
745 | { "" } | |
746 | }; | |
747 | ||
748 | ||
749 | static struct xenbus_driver blkback = { | |
750 | .name = "vbd", | |
751 | .owner = THIS_MODULE, | |
752 | .ids = blkback_ids, | |
753 | .probe = blkback_probe, | |
754 | .remove = blkback_remove, | |
755 | .otherend_changed = frontend_changed | |
756 | }; | |
757 | ||
758 | ||
8770b268 | 759 | int blkif_xenbus_init(void) |
4d05a28d | 760 | { |
8770b268 | 761 | return xenbus_register_backend(&blkback); |
4d05a28d | 762 | } |