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