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