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