]>
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); | |
814d04e7 VP |
38 | static void xen_blkif_free(struct xen_blkif *blkif); |
39 | static void xen_vbd_free(struct xen_vbd *vbd); | |
4d05a28d | 40 | |
8b6bf747 | 41 | struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be) |
98e036a3 JF |
42 | { |
43 | return be->dev; | |
44 | } | |
45 | ||
814d04e7 VP |
46 | /* |
47 | * The last request could free the device from softirq context and | |
48 | * xen_blkif_free() can sleep. | |
49 | */ | |
50 | static void xen_blkif_deferred_free(struct work_struct *work) | |
51 | { | |
52 | struct xen_blkif *blkif; | |
53 | ||
54 | blkif = container_of(work, struct xen_blkif, free_work); | |
55 | xen_blkif_free(blkif); | |
56 | } | |
57 | ||
30fd1502 | 58 | static int blkback_name(struct xen_blkif *blkif, char *buf) |
4d05a28d KRW |
59 | { |
60 | char *devpath, *devname; | |
61 | struct xenbus_device *dev = blkif->be->dev; | |
62 | ||
63 | devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL); | |
64 | if (IS_ERR(devpath)) | |
65 | return PTR_ERR(devpath); | |
66 | ||
d6091b21 KRW |
67 | devname = strstr(devpath, "/dev/"); |
68 | if (devname != NULL) | |
4d05a28d KRW |
69 | devname += strlen("/dev/"); |
70 | else | |
71 | devname = devpath; | |
72 | ||
73 | snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname); | |
74 | kfree(devpath); | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
30fd1502 | 79 | static void xen_update_blkif_status(struct xen_blkif *blkif) |
4d05a28d KRW |
80 | { |
81 | int err; | |
82 | char name[TASK_COMM_LEN]; | |
83 | ||
84 | /* Not ready to connect? */ | |
85 | if (!blkif->irq || !blkif->vbd.bdev) | |
86 | return; | |
87 | ||
88 | /* Already connected? */ | |
89 | if (blkif->be->dev->state == XenbusStateConnected) | |
90 | return; | |
91 | ||
92 | /* Attempt to connect: exit if we fail to. */ | |
93 | connect(blkif->be); | |
94 | if (blkif->be->dev->state != XenbusStateConnected) | |
95 | return; | |
96 | ||
97 | err = blkback_name(blkif, name); | |
98 | if (err) { | |
99 | xenbus_dev_error(blkif->be->dev, err, "get blkback dev name"); | |
100 | return; | |
101 | } | |
102 | ||
cbf46290 CL |
103 | err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping); |
104 | if (err) { | |
105 | xenbus_dev_error(blkif->be->dev, err, "block flush"); | |
106 | return; | |
107 | } | |
108 | invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping); | |
109 | ||
f170168b | 110 | blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, "%s", name); |
4d05a28d KRW |
111 | if (IS_ERR(blkif->xenblkd)) { |
112 | err = PTR_ERR(blkif->xenblkd); | |
113 | blkif->xenblkd = NULL; | |
114 | xenbus_dev_error(blkif->be->dev, err, "start xenblkd"); | |
3f3aad5e | 115 | return; |
4d05a28d KRW |
116 | } |
117 | } | |
118 | ||
30fd1502 | 119 | static struct xen_blkif *xen_blkif_alloc(domid_t domid) |
ee9ff853 | 120 | { |
30fd1502 | 121 | struct xen_blkif *blkif; |
bb642e83 RPM |
122 | struct pending_req *req, *n; |
123 | int i, j; | |
ee9ff853 | 124 | |
402b27f9 | 125 | BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST); |
ee9ff853 | 126 | |
654dbef2 | 127 | blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL); |
ee9ff853 KRW |
128 | if (!blkif) |
129 | return ERR_PTR(-ENOMEM); | |
130 | ||
ee9ff853 KRW |
131 | blkif->domid = domid; |
132 | spin_lock_init(&blkif->blk_ring_lock); | |
133 | atomic_set(&blkif->refcnt, 1); | |
134 | init_waitqueue_head(&blkif->wq); | |
29bde093 KRW |
135 | init_completion(&blkif->drain_complete); |
136 | atomic_set(&blkif->drain, 0); | |
ee9ff853 | 137 | blkif->st_print = jiffies; |
0a8704a5 | 138 | blkif->persistent_gnts.rb_node = NULL; |
c6cc142d RPM |
139 | spin_lock_init(&blkif->free_pages_lock); |
140 | INIT_LIST_HEAD(&blkif->free_pages); | |
ef753411 | 141 | INIT_LIST_HEAD(&blkif->persistent_purge_list); |
c6cc142d | 142 | blkif->free_pages_num = 0; |
3f3aad5e | 143 | atomic_set(&blkif->persistent_gnt_in_use, 0); |
c05f3e3c | 144 | atomic_set(&blkif->inflight, 0); |
abb97b8c | 145 | INIT_WORK(&blkif->persistent_purge_work, xen_blkbk_unmap_purged_grants); |
ee9ff853 | 146 | |
bf0720c4 | 147 | INIT_LIST_HEAD(&blkif->pending_free); |
814d04e7 | 148 | INIT_WORK(&blkif->free_work, xen_blkif_deferred_free); |
bb642e83 RPM |
149 | |
150 | for (i = 0; i < XEN_BLKIF_REQS; i++) { | |
151 | req = kzalloc(sizeof(*req), GFP_KERNEL); | |
152 | if (!req) | |
153 | goto fail; | |
154 | list_add_tail(&req->free_list, | |
155 | &blkif->pending_free); | |
156 | for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { | |
157 | req->segments[j] = kzalloc(sizeof(*req->segments[0]), | |
158 | GFP_KERNEL); | |
159 | if (!req->segments[j]) | |
160 | goto fail; | |
161 | } | |
162 | for (j = 0; j < MAX_INDIRECT_PAGES; j++) { | |
163 | req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]), | |
164 | GFP_KERNEL); | |
165 | if (!req->indirect_pages[j]) | |
166 | goto fail; | |
167 | } | |
168 | } | |
bf0720c4 RPM |
169 | spin_lock_init(&blkif->pending_free_lock); |
170 | init_waitqueue_head(&blkif->pending_free_wq); | |
8e3f8755 | 171 | init_waitqueue_head(&blkif->shutdown_wq); |
ee9ff853 KRW |
172 | |
173 | return blkif; | |
bb642e83 RPM |
174 | |
175 | fail: | |
176 | list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) { | |
177 | list_del(&req->free_list); | |
178 | for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { | |
179 | if (!req->segments[j]) | |
180 | break; | |
181 | kfree(req->segments[j]); | |
182 | } | |
183 | for (j = 0; j < MAX_INDIRECT_PAGES; j++) { | |
184 | if (!req->indirect_pages[j]) | |
185 | break; | |
186 | kfree(req->indirect_pages[j]); | |
187 | } | |
188 | kfree(req); | |
189 | } | |
190 | ||
191 | kmem_cache_free(xen_blkif_cachep, blkif); | |
192 | ||
193 | return ERR_PTR(-ENOMEM); | |
ee9ff853 KRW |
194 | } |
195 | ||
30fd1502 | 196 | static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page, |
8b6bf747 | 197 | unsigned int evtchn) |
ee9ff853 KRW |
198 | { |
199 | int err; | |
200 | ||
201 | /* Already connected through? */ | |
202 | if (blkif->irq) | |
203 | return 0; | |
204 | ||
2d073846 DV |
205 | err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring); |
206 | if (err < 0) | |
ee9ff853 | 207 | return err; |
ee9ff853 KRW |
208 | |
209 | switch (blkif->blk_protocol) { | |
210 | case BLKIF_PROTOCOL_NATIVE: | |
211 | { | |
212 | struct blkif_sring *sring; | |
2d073846 | 213 | sring = (struct blkif_sring *)blkif->blk_ring; |
ee9ff853 KRW |
214 | BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE); |
215 | break; | |
216 | } | |
217 | case BLKIF_PROTOCOL_X86_32: | |
218 | { | |
219 | struct blkif_x86_32_sring *sring_x86_32; | |
2d073846 | 220 | sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring; |
ee9ff853 KRW |
221 | BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE); |
222 | break; | |
223 | } | |
224 | case BLKIF_PROTOCOL_X86_64: | |
225 | { | |
226 | struct blkif_x86_64_sring *sring_x86_64; | |
2d073846 | 227 | sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring; |
ee9ff853 KRW |
228 | BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE); |
229 | break; | |
230 | } | |
231 | default: | |
232 | BUG(); | |
233 | } | |
234 | ||
8b6bf747 KRW |
235 | err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn, |
236 | xen_blkif_be_int, 0, | |
237 | "blkif-backend", blkif); | |
ee9ff853 | 238 | if (err < 0) { |
2d073846 | 239 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
ee9ff853 KRW |
240 | blkif->blk_rings.common.sring = NULL; |
241 | return err; | |
242 | } | |
243 | blkif->irq = err; | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
814d04e7 | 248 | static int xen_blkif_disconnect(struct xen_blkif *blkif) |
ee9ff853 KRW |
249 | { |
250 | if (blkif->xenblkd) { | |
251 | kthread_stop(blkif->xenblkd); | |
8e3f8755 | 252 | wake_up(&blkif->shutdown_wq); |
ee9ff853 KRW |
253 | blkif->xenblkd = NULL; |
254 | } | |
255 | ||
814d04e7 VP |
256 | /* The above kthread_stop() guarantees that at this point we |
257 | * don't have any discard_io or other_io requests. So, checking | |
258 | * for inflight IO is enough. | |
259 | */ | |
260 | if (atomic_read(&blkif->inflight) > 0) | |
261 | return -EBUSY; | |
ee9ff853 KRW |
262 | |
263 | if (blkif->irq) { | |
264 | unbind_from_irqhandler(blkif->irq, blkif); | |
265 | blkif->irq = 0; | |
266 | } | |
267 | ||
268 | if (blkif->blk_rings.common.sring) { | |
2d073846 | 269 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
ee9ff853 KRW |
270 | blkif->blk_rings.common.sring = NULL; |
271 | } | |
814d04e7 | 272 | |
12ea7296 VK |
273 | /* Remove all persistent grants and the cache of ballooned pages. */ |
274 | xen_blkbk_free_caches(blkif); | |
275 | ||
814d04e7 | 276 | return 0; |
ee9ff853 KRW |
277 | } |
278 | ||
2911758f | 279 | static void xen_blkif_free(struct xen_blkif *blkif) |
ee9ff853 | 280 | { |
bb642e83 RPM |
281 | struct pending_req *req, *n; |
282 | int i = 0, j; | |
bf0720c4 | 283 | |
814d04e7 VP |
284 | xen_blkif_disconnect(blkif); |
285 | xen_vbd_free(&blkif->vbd); | |
bf0720c4 | 286 | |
ef753411 RPM |
287 | /* Make sure everything is drained before shutting down */ |
288 | BUG_ON(blkif->persistent_gnt_c != 0); | |
289 | BUG_ON(atomic_read(&blkif->persistent_gnt_in_use) != 0); | |
290 | BUG_ON(blkif->free_pages_num != 0); | |
291 | BUG_ON(!list_empty(&blkif->persistent_purge_list)); | |
292 | BUG_ON(!list_empty(&blkif->free_pages)); | |
293 | BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts)); | |
294 | ||
bf0720c4 | 295 | /* Check that there is no request in use */ |
bb642e83 RPM |
296 | list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) { |
297 | list_del(&req->free_list); | |
298 | ||
299 | for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) | |
300 | kfree(req->segments[j]); | |
301 | ||
302 | for (j = 0; j < MAX_INDIRECT_PAGES; j++) | |
303 | kfree(req->indirect_pages[j]); | |
304 | ||
305 | kfree(req); | |
bf0720c4 | 306 | i++; |
bb642e83 RPM |
307 | } |
308 | ||
309 | WARN_ON(i != XEN_BLKIF_REQS); | |
bf0720c4 | 310 | |
8b6bf747 | 311 | kmem_cache_free(xen_blkif_cachep, blkif); |
ee9ff853 KRW |
312 | } |
313 | ||
8b6bf747 | 314 | int __init xen_blkif_interface_init(void) |
ee9ff853 | 315 | { |
8b6bf747 | 316 | xen_blkif_cachep = kmem_cache_create("blkif_cache", |
30fd1502 | 317 | sizeof(struct xen_blkif), |
8b6bf747 KRW |
318 | 0, 0, NULL); |
319 | if (!xen_blkif_cachep) | |
ee9ff853 KRW |
320 | return -ENOMEM; |
321 | ||
322 | return 0; | |
323 | } | |
4d05a28d | 324 | |
a1397fa3 | 325 | /* |
4d05a28d KRW |
326 | * sysfs interface for VBD I/O requests |
327 | */ | |
328 | ||
329 | #define VBD_SHOW(name, format, args...) \ | |
330 | static ssize_t show_##name(struct device *_dev, \ | |
331 | struct device_attribute *attr, \ | |
332 | char *buf) \ | |
333 | { \ | |
334 | struct xenbus_device *dev = to_xenbus_device(_dev); \ | |
5cf6e4f6 | 335 | struct backend_info *be = dev_get_drvdata(&dev->dev); \ |
4d05a28d KRW |
336 | \ |
337 | return sprintf(buf, format, ##args); \ | |
338 | } \ | |
339 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) | |
340 | ||
986cacbd ZK |
341 | VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req); |
342 | VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req); | |
343 | VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req); | |
344 | VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req); | |
345 | VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req); | |
346 | VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect); | |
347 | VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect); | |
4d05a28d | 348 | |
3d814731 | 349 | static struct attribute *xen_vbdstat_attrs[] = { |
4d05a28d KRW |
350 | &dev_attr_oo_req.attr, |
351 | &dev_attr_rd_req.attr, | |
352 | &dev_attr_wr_req.attr, | |
24f567f9 | 353 | &dev_attr_f_req.attr, |
b3cb0d6a | 354 | &dev_attr_ds_req.attr, |
4d05a28d KRW |
355 | &dev_attr_rd_sect.attr, |
356 | &dev_attr_wr_sect.attr, | |
357 | NULL | |
358 | }; | |
359 | ||
3d814731 | 360 | static struct attribute_group xen_vbdstat_group = { |
4d05a28d | 361 | .name = "statistics", |
3d814731 | 362 | .attrs = xen_vbdstat_attrs, |
4d05a28d KRW |
363 | }; |
364 | ||
365 | VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); | |
366 | VBD_SHOW(mode, "%s\n", be->mode); | |
367 | ||
2911758f | 368 | static int xenvbd_sysfs_addif(struct xenbus_device *dev) |
4d05a28d KRW |
369 | { |
370 | int error; | |
371 | ||
372 | error = device_create_file(&dev->dev, &dev_attr_physical_device); | |
d6091b21 | 373 | if (error) |
4d05a28d KRW |
374 | goto fail1; |
375 | ||
376 | error = device_create_file(&dev->dev, &dev_attr_mode); | |
377 | if (error) | |
378 | goto fail2; | |
379 | ||
3d814731 | 380 | error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group); |
4d05a28d KRW |
381 | if (error) |
382 | goto fail3; | |
383 | ||
384 | return 0; | |
385 | ||
3d814731 | 386 | fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
4d05a28d KRW |
387 | fail2: device_remove_file(&dev->dev, &dev_attr_mode); |
388 | fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); | |
389 | return error; | |
390 | } | |
391 | ||
2911758f | 392 | static void xenvbd_sysfs_delif(struct xenbus_device *dev) |
4d05a28d | 393 | { |
3d814731 | 394 | sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
4d05a28d KRW |
395 | device_remove_file(&dev->dev, &dev_attr_mode); |
396 | device_remove_file(&dev->dev, &dev_attr_physical_device); | |
397 | } | |
398 | ||
42c7841d | 399 | |
3d814731 | 400 | static void xen_vbd_free(struct xen_vbd *vbd) |
42c7841d KRW |
401 | { |
402 | if (vbd->bdev) | |
403 | blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); | |
404 | vbd->bdev = NULL; | |
405 | } | |
406 | ||
3d814731 KRW |
407 | static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle, |
408 | unsigned major, unsigned minor, int readonly, | |
409 | int cdrom) | |
42c7841d | 410 | { |
3d814731 | 411 | struct xen_vbd *vbd; |
42c7841d | 412 | struct block_device *bdev; |
24f567f9 | 413 | struct request_queue *q; |
42c7841d KRW |
414 | |
415 | vbd = &blkif->vbd; | |
416 | vbd->handle = handle; | |
417 | vbd->readonly = readonly; | |
418 | vbd->type = 0; | |
419 | ||
420 | vbd->pdevice = MKDEV(major, minor); | |
421 | ||
422 | bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ? | |
423 | FMODE_READ : FMODE_WRITE, NULL); | |
424 | ||
425 | if (IS_ERR(bdev)) { | |
3d814731 | 426 | DPRINTK("xen_vbd_create: device %08x could not be opened.\n", |
42c7841d KRW |
427 | vbd->pdevice); |
428 | return -ENOENT; | |
429 | } | |
430 | ||
431 | vbd->bdev = bdev; | |
42c7841d | 432 | if (vbd->bdev->bd_disk == NULL) { |
3d814731 | 433 | DPRINTK("xen_vbd_create: device %08x doesn't exist.\n", |
42c7841d | 434 | vbd->pdevice); |
3d814731 | 435 | xen_vbd_free(vbd); |
42c7841d KRW |
436 | return -ENOENT; |
437 | } | |
6464920a | 438 | vbd->size = vbd_sz(vbd); |
42c7841d KRW |
439 | |
440 | if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom) | |
441 | vbd->type |= VDISK_CDROM; | |
442 | if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) | |
443 | vbd->type |= VDISK_REMOVABLE; | |
444 | ||
24f567f9 KRW |
445 | q = bdev_get_queue(bdev); |
446 | if (q && q->flush_flags) | |
447 | vbd->flush_support = true; | |
448 | ||
5ea42986 KRW |
449 | if (q && blk_queue_secdiscard(q)) |
450 | vbd->discard_secure = true; | |
451 | ||
42c7841d KRW |
452 | DPRINTK("Successful creation of handle=%04x (dom=%u)\n", |
453 | handle, blkif->domid); | |
454 | return 0; | |
455 | } | |
8b6bf747 | 456 | static int xen_blkbk_remove(struct xenbus_device *dev) |
4d05a28d | 457 | { |
5cf6e4f6 | 458 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
4d05a28d KRW |
459 | |
460 | DPRINTK(""); | |
461 | ||
462 | if (be->major || be->minor) | |
463 | xenvbd_sysfs_delif(dev); | |
464 | ||
465 | if (be->backend_watch.node) { | |
466 | unregister_xenbus_watch(&be->backend_watch); | |
467 | kfree(be->backend_watch.node); | |
468 | be->backend_watch.node = NULL; | |
469 | } | |
470 | ||
814d04e7 VP |
471 | dev_set_drvdata(&dev->dev, NULL); |
472 | ||
4d05a28d | 473 | if (be->blkif) { |
8b6bf747 | 474 | xen_blkif_disconnect(be->blkif); |
814d04e7 | 475 | xen_blkif_put(be->blkif); |
4d05a28d KRW |
476 | } |
477 | ||
9d092603 | 478 | kfree(be->mode); |
4d05a28d | 479 | kfree(be); |
4d05a28d KRW |
480 | return 0; |
481 | } | |
482 | ||
24f567f9 KRW |
483 | int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, |
484 | struct backend_info *be, int state) | |
4d05a28d KRW |
485 | { |
486 | struct xenbus_device *dev = be->dev; | |
487 | int err; | |
488 | ||
24f567f9 | 489 | err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache", |
4d05a28d KRW |
490 | "%d", state); |
491 | if (err) | |
3389bb8b | 492 | dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err); |
4d05a28d KRW |
493 | |
494 | return err; | |
495 | } | |
496 | ||
3389bb8b | 497 | static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be) |
b3cb0d6a LD |
498 | { |
499 | struct xenbus_device *dev = be->dev; | |
500 | struct xen_blkif *blkif = be->blkif; | |
b3cb0d6a | 501 | int err; |
c926b701 | 502 | int state = 0, discard_enable; |
4dae7670 KRW |
503 | struct block_device *bdev = be->blkif->vbd.bdev; |
504 | struct request_queue *q = bdev_get_queue(bdev); | |
505 | ||
c926b701 OH |
506 | err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d", |
507 | &discard_enable); | |
508 | if (err == 1 && !discard_enable) | |
509 | return; | |
510 | ||
4dae7670 KRW |
511 | if (blk_queue_discard(q)) { |
512 | err = xenbus_printf(xbt, dev->nodename, | |
513 | "discard-granularity", "%u", | |
514 | q->limits.discard_granularity); | |
515 | if (err) { | |
3389bb8b KRW |
516 | dev_warn(&dev->dev, "writing discard-granularity (%d)", err); |
517 | return; | |
4dae7670 KRW |
518 | } |
519 | err = xenbus_printf(xbt, dev->nodename, | |
520 | "discard-alignment", "%u", | |
521 | q->limits.discard_alignment); | |
522 | if (err) { | |
3389bb8b KRW |
523 | dev_warn(&dev->dev, "writing discard-alignment (%d)", err); |
524 | return; | |
b3cb0d6a | 525 | } |
4dae7670 KRW |
526 | state = 1; |
527 | /* Optional. */ | |
528 | err = xenbus_printf(xbt, dev->nodename, | |
529 | "discard-secure", "%d", | |
530 | blkif->vbd.discard_secure); | |
531 | if (err) { | |
a71e23d9 | 532 | dev_warn(&dev->dev, "writing discard-secure (%d)", err); |
3389bb8b | 533 | return; |
b3cb0d6a | 534 | } |
b3cb0d6a | 535 | } |
b3cb0d6a LD |
536 | err = xenbus_printf(xbt, dev->nodename, "feature-discard", |
537 | "%d", state); | |
538 | if (err) | |
3389bb8b | 539 | dev_warn(&dev->dev, "writing feature-discard (%d)", err); |
b3cb0d6a | 540 | } |
29bde093 KRW |
541 | int xen_blkbk_barrier(struct xenbus_transaction xbt, |
542 | struct backend_info *be, int state) | |
543 | { | |
544 | struct xenbus_device *dev = be->dev; | |
545 | int err; | |
546 | ||
547 | err = xenbus_printf(xbt, dev->nodename, "feature-barrier", | |
548 | "%d", state); | |
549 | if (err) | |
3389bb8b | 550 | dev_warn(&dev->dev, "writing feature-barrier (%d)", err); |
29bde093 KRW |
551 | |
552 | return err; | |
553 | } | |
b3cb0d6a | 554 | |
01f37f2d | 555 | /* |
4d05a28d KRW |
556 | * Entry point to this code when a new device is created. Allocate the basic |
557 | * structures, and watch the store waiting for the hotplug scripts to tell us | |
558 | * the device's physical major and minor numbers. Switch to InitWait. | |
559 | */ | |
8b6bf747 KRW |
560 | static int xen_blkbk_probe(struct xenbus_device *dev, |
561 | const struct xenbus_device_id *id) | |
4d05a28d KRW |
562 | { |
563 | int err; | |
564 | struct backend_info *be = kzalloc(sizeof(struct backend_info), | |
565 | GFP_KERNEL); | |
566 | if (!be) { | |
567 | xenbus_dev_fatal(dev, -ENOMEM, | |
568 | "allocating backend structure"); | |
569 | return -ENOMEM; | |
570 | } | |
571 | be->dev = dev; | |
5cf6e4f6 | 572 | dev_set_drvdata(&dev->dev, be); |
4d05a28d | 573 | |
8b6bf747 | 574 | be->blkif = xen_blkif_alloc(dev->otherend_id); |
4d05a28d KRW |
575 | if (IS_ERR(be->blkif)) { |
576 | err = PTR_ERR(be->blkif); | |
577 | be->blkif = NULL; | |
578 | xenbus_dev_fatal(dev, err, "creating block interface"); | |
579 | goto fail; | |
580 | } | |
581 | ||
582 | /* setup back pointer */ | |
583 | be->blkif->be = be; | |
584 | ||
88122933 JF |
585 | err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed, |
586 | "%s/%s", dev->nodename, "physical-device"); | |
4d05a28d KRW |
587 | if (err) |
588 | goto fail; | |
589 | ||
590 | err = xenbus_switch_state(dev, XenbusStateInitWait); | |
591 | if (err) | |
592 | goto fail; | |
593 | ||
594 | return 0; | |
595 | ||
596 | fail: | |
597 | DPRINTK("failed"); | |
8b6bf747 | 598 | xen_blkbk_remove(dev); |
4d05a28d KRW |
599 | return err; |
600 | } | |
601 | ||
602 | ||
01f37f2d | 603 | /* |
4d05a28d KRW |
604 | * Callback received when the hotplug scripts have placed the physical-device |
605 | * node. Read it and the mode node, and create a vbd. If the frontend is | |
606 | * ready, connect. | |
607 | */ | |
608 | static void backend_changed(struct xenbus_watch *watch, | |
609 | const char **vec, unsigned int len) | |
610 | { | |
611 | int err; | |
612 | unsigned major; | |
613 | unsigned minor; | |
614 | struct backend_info *be | |
615 | = container_of(watch, struct backend_info, backend_watch); | |
616 | struct xenbus_device *dev = be->dev; | |
617 | int cdrom = 0; | |
9d092603 | 618 | unsigned long handle; |
4d05a28d KRW |
619 | char *device_type; |
620 | ||
621 | DPRINTK(""); | |
622 | ||
623 | err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x", | |
624 | &major, &minor); | |
625 | if (XENBUS_EXIST_ERR(err)) { | |
01f37f2d KRW |
626 | /* |
627 | * Since this watch will fire once immediately after it is | |
628 | * registered, we expect this. Ignore it, and wait for the | |
629 | * hotplug scripts. | |
630 | */ | |
4d05a28d KRW |
631 | return; |
632 | } | |
633 | if (err != 2) { | |
634 | xenbus_dev_fatal(dev, err, "reading physical-device"); | |
635 | return; | |
636 | } | |
637 | ||
9d092603 JB |
638 | if (be->major | be->minor) { |
639 | if (be->major != major || be->minor != minor) | |
640 | pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n", | |
641 | be->major, be->minor, major, minor); | |
4d05a28d KRW |
642 | return; |
643 | } | |
644 | ||
645 | be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL); | |
646 | if (IS_ERR(be->mode)) { | |
647 | err = PTR_ERR(be->mode); | |
648 | be->mode = NULL; | |
649 | xenbus_dev_fatal(dev, err, "reading mode"); | |
650 | return; | |
651 | } | |
652 | ||
653 | device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL); | |
654 | if (!IS_ERR(device_type)) { | |
655 | cdrom = strcmp(device_type, "cdrom") == 0; | |
656 | kfree(device_type); | |
657 | } | |
658 | ||
9d092603 | 659 | /* Front end dir is a number, which is used as the handle. */ |
bb8e0e84 | 660 | err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle); |
9d092603 JB |
661 | if (err) |
662 | return; | |
4d05a28d | 663 | |
9d092603 JB |
664 | be->major = major; |
665 | be->minor = minor; | |
4d05a28d | 666 | |
9d092603 JB |
667 | err = xen_vbd_create(be->blkif, handle, major, minor, |
668 | !strchr(be->mode, 'w'), cdrom); | |
4d05a28d | 669 | |
9d092603 JB |
670 | if (err) |
671 | xenbus_dev_fatal(dev, err, "creating vbd structure"); | |
672 | else { | |
4d05a28d KRW |
673 | err = xenvbd_sysfs_addif(dev); |
674 | if (err) { | |
3d814731 | 675 | xen_vbd_free(&be->blkif->vbd); |
4d05a28d | 676 | xenbus_dev_fatal(dev, err, "creating sysfs entries"); |
4d05a28d | 677 | } |
9d092603 | 678 | } |
4d05a28d | 679 | |
9d092603 JB |
680 | if (err) { |
681 | kfree(be->mode); | |
682 | be->mode = NULL; | |
683 | be->major = 0; | |
684 | be->minor = 0; | |
685 | } else { | |
4d05a28d | 686 | /* We're potentially connected now */ |
8b6bf747 | 687 | xen_update_blkif_status(be->blkif); |
4d05a28d KRW |
688 | } |
689 | } | |
690 | ||
691 | ||
01f37f2d | 692 | /* |
4d05a28d KRW |
693 | * Callback received when the frontend's state changes. |
694 | */ | |
695 | static void frontend_changed(struct xenbus_device *dev, | |
696 | enum xenbus_state frontend_state) | |
697 | { | |
5cf6e4f6 | 698 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
4d05a28d KRW |
699 | int err; |
700 | ||
701 | DPRINTK("%s", xenbus_strstate(frontend_state)); | |
702 | ||
703 | switch (frontend_state) { | |
704 | case XenbusStateInitialising: | |
705 | if (dev->state == XenbusStateClosed) { | |
22b20f2d | 706 | pr_info(DRV_PFX "%s: prepare for reconnect\n", |
ebe81906 | 707 | dev->nodename); |
4d05a28d KRW |
708 | xenbus_switch_state(dev, XenbusStateInitWait); |
709 | } | |
710 | break; | |
711 | ||
712 | case XenbusStateInitialised: | |
713 | case XenbusStateConnected: | |
01f37f2d KRW |
714 | /* |
715 | * Ensure we connect even when two watches fire in | |
42b2aa86 | 716 | * close succession and we miss the intermediate value |
01f37f2d KRW |
717 | * of frontend_state. |
718 | */ | |
4d05a28d KRW |
719 | if (dev->state == XenbusStateConnected) |
720 | break; | |
721 | ||
01f37f2d KRW |
722 | /* |
723 | * Enforce precondition before potential leak point. | |
1bc05b0a | 724 | * xen_blkif_disconnect() is idempotent. |
313d7b00 | 725 | */ |
814d04e7 VP |
726 | err = xen_blkif_disconnect(be->blkif); |
727 | if (err) { | |
728 | xenbus_dev_fatal(dev, err, "pending I/O"); | |
729 | break; | |
730 | } | |
313d7b00 | 731 | |
4d05a28d KRW |
732 | err = connect_ring(be); |
733 | if (err) | |
734 | break; | |
8b6bf747 | 735 | xen_update_blkif_status(be->blkif); |
4d05a28d KRW |
736 | break; |
737 | ||
738 | case XenbusStateClosing: | |
4d05a28d KRW |
739 | xenbus_switch_state(dev, XenbusStateClosing); |
740 | break; | |
741 | ||
742 | case XenbusStateClosed: | |
6f5986bc | 743 | xen_blkif_disconnect(be->blkif); |
4d05a28d KRW |
744 | xenbus_switch_state(dev, XenbusStateClosed); |
745 | if (xenbus_dev_is_online(dev)) | |
746 | break; | |
747 | /* fall through if not online */ | |
748 | case XenbusStateUnknown: | |
1bc05b0a | 749 | /* implies xen_blkif_disconnect() via xen_blkbk_remove() */ |
4d05a28d KRW |
750 | device_unregister(&dev->dev); |
751 | break; | |
752 | ||
753 | default: | |
754 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", | |
755 | frontend_state); | |
756 | break; | |
757 | } | |
758 | } | |
759 | ||
760 | ||
761 | /* ** Connection ** */ | |
762 | ||
763 | ||
01f37f2d | 764 | /* |
4d05a28d KRW |
765 | * Write the physical details regarding the block device to the store, and |
766 | * switch to Connected state. | |
767 | */ | |
768 | static void connect(struct backend_info *be) | |
769 | { | |
770 | struct xenbus_transaction xbt; | |
771 | int err; | |
772 | struct xenbus_device *dev = be->dev; | |
773 | ||
774 | DPRINTK("%s", dev->otherend); | |
775 | ||
776 | /* Supply the information about the device the frontend needs */ | |
777 | again: | |
778 | err = xenbus_transaction_start(&xbt); | |
779 | if (err) { | |
780 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
781 | return; | |
782 | } | |
783 | ||
3389bb8b KRW |
784 | /* If we can't advertise it is OK. */ |
785 | xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support); | |
4d05a28d | 786 | |
3389bb8b | 787 | xen_blkbk_discard(xbt, be); |
b3cb0d6a | 788 | |
3389bb8b | 789 | xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support); |
29bde093 | 790 | |
0a8704a5 RPM |
791 | err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1); |
792 | if (err) { | |
793 | xenbus_dev_fatal(dev, err, "writing %s/feature-persistent", | |
794 | dev->nodename); | |
795 | goto abort; | |
796 | } | |
402b27f9 RPM |
797 | err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u", |
798 | MAX_INDIRECT_SEGMENTS); | |
799 | if (err) | |
800 | dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)", | |
801 | dev->nodename, err); | |
0a8704a5 | 802 | |
4d05a28d | 803 | err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", |
42c7841d | 804 | (unsigned long long)vbd_sz(&be->blkif->vbd)); |
4d05a28d KRW |
805 | if (err) { |
806 | xenbus_dev_fatal(dev, err, "writing %s/sectors", | |
807 | dev->nodename); | |
808 | goto abort; | |
809 | } | |
810 | ||
811 | /* FIXME: use a typename instead */ | |
812 | err = xenbus_printf(xbt, dev->nodename, "info", "%u", | |
42c7841d KRW |
813 | be->blkif->vbd.type | |
814 | (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); | |
4d05a28d KRW |
815 | if (err) { |
816 | xenbus_dev_fatal(dev, err, "writing %s/info", | |
817 | dev->nodename); | |
818 | goto abort; | |
819 | } | |
820 | err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", | |
42c7841d KRW |
821 | (unsigned long) |
822 | bdev_logical_block_size(be->blkif->vbd.bdev)); | |
4d05a28d KRW |
823 | if (err) { |
824 | xenbus_dev_fatal(dev, err, "writing %s/sector-size", | |
825 | dev->nodename); | |
826 | goto abort; | |
827 | } | |
7c4d7d71 SB |
828 | err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u", |
829 | bdev_physical_block_size(be->blkif->vbd.bdev)); | |
830 | if (err) | |
831 | xenbus_dev_error(dev, err, "writing %s/physical-sector-size", | |
832 | dev->nodename); | |
4d05a28d KRW |
833 | |
834 | err = xenbus_transaction_end(xbt, 0); | |
835 | if (err == -EAGAIN) | |
836 | goto again; | |
837 | if (err) | |
838 | xenbus_dev_fatal(dev, err, "ending transaction"); | |
839 | ||
840 | err = xenbus_switch_state(dev, XenbusStateConnected); | |
841 | if (err) | |
08b8bfc1 | 842 | xenbus_dev_fatal(dev, err, "%s: switching to Connected state", |
4d05a28d KRW |
843 | dev->nodename); |
844 | ||
845 | return; | |
846 | abort: | |
847 | xenbus_transaction_end(xbt, 1); | |
848 | } | |
849 | ||
850 | ||
851 | static int connect_ring(struct backend_info *be) | |
852 | { | |
853 | struct xenbus_device *dev = be->dev; | |
854 | unsigned long ring_ref; | |
855 | unsigned int evtchn; | |
0a8704a5 | 856 | unsigned int pers_grants; |
4d05a28d KRW |
857 | char protocol[64] = ""; |
858 | int err; | |
859 | ||
860 | DPRINTK("%s", dev->otherend); | |
861 | ||
d6091b21 KRW |
862 | err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", |
863 | &ring_ref, "event-channel", "%u", &evtchn, NULL); | |
4d05a28d KRW |
864 | if (err) { |
865 | xenbus_dev_fatal(dev, err, | |
866 | "reading %s/ring-ref and event-channel", | |
867 | dev->otherend); | |
868 | return err; | |
869 | } | |
870 | ||
b042a3ca | 871 | be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT; |
4d05a28d KRW |
872 | err = xenbus_gather(XBT_NIL, dev->otherend, "protocol", |
873 | "%63s", protocol, NULL); | |
874 | if (err) | |
b042a3ca | 875 | strcpy(protocol, "unspecified, assuming default"); |
4d05a28d KRW |
876 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) |
877 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; | |
878 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) | |
879 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; | |
880 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) | |
881 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; | |
882 | else { | |
883 | xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); | |
884 | return -1; | |
885 | } | |
0a8704a5 | 886 | err = xenbus_gather(XBT_NIL, dev->otherend, |
cb5bd4d1 | 887 | "feature-persistent", "%u", |
0a8704a5 RPM |
888 | &pers_grants, NULL); |
889 | if (err) | |
890 | pers_grants = 0; | |
891 | ||
892 | be->blkif->vbd.feature_gnt_persistent = pers_grants; | |
893 | be->blkif->vbd.overflow_max_grants = 0; | |
894 | ||
895 | pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n", | |
896 | ring_ref, evtchn, be->blkif->blk_protocol, protocol, | |
897 | pers_grants ? "persistent grants" : ""); | |
4d05a28d KRW |
898 | |
899 | /* Map the shared frame, irq etc. */ | |
8b6bf747 | 900 | err = xen_blkif_map(be->blkif, ring_ref, evtchn); |
4d05a28d KRW |
901 | if (err) { |
902 | xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u", | |
903 | ring_ref, evtchn); | |
904 | return err; | |
905 | } | |
906 | ||
907 | return 0; | |
908 | } | |
909 | ||
8b6bf747 | 910 | static const struct xenbus_device_id xen_blkbk_ids[] = { |
4d05a28d KRW |
911 | { "vbd" }, |
912 | { "" } | |
913 | }; | |
914 | ||
95afae48 DV |
915 | static struct xenbus_driver xen_blkbk_driver = { |
916 | .ids = xen_blkbk_ids, | |
8b6bf747 KRW |
917 | .probe = xen_blkbk_probe, |
918 | .remove = xen_blkbk_remove, | |
4d05a28d | 919 | .otherend_changed = frontend_changed |
95afae48 | 920 | }; |
4d05a28d | 921 | |
8b6bf747 | 922 | int xen_blkif_xenbus_init(void) |
4d05a28d | 923 | { |
73db144b | 924 | return xenbus_register_backend(&xen_blkbk_driver); |
4d05a28d | 925 | } |