This source file includes following definitions.
- xen_blkbk_xenbus
- xen_blkif_deferred_free
- blkback_name
- xen_update_blkif_status
- xen_blkif_alloc_rings
- xen_blkif_alloc
- xen_blkif_map
- xen_blkif_disconnect
- xen_blkif_free
- xen_blkif_interface_init
- xenvbd_sysfs_addif
- xenvbd_sysfs_delif
- xen_vbd_free
- xen_vbd_create
- xen_blkbk_remove
- xen_blkbk_flush_diskcache
- xen_blkbk_discard
- xen_blkbk_barrier
- xen_blkbk_probe
- backend_changed
- frontend_changed
- connect
- read_per_ring_refs
- connect_ring
- xen_blkif_xenbus_init
1
2
3
4
5
6
7
8
9 #define pr_fmt(fmt) "xen-blkback: " fmt
10
11 #include <stdarg.h>
12 #include <linux/module.h>
13 #include <linux/kthread.h>
14 #include <xen/events.h>
15 #include <xen/grant_table.h>
16 #include "common.h"
17
18
19 #define RINGREF_NAME_LEN (20)
20
21 struct backend_info {
22 struct xenbus_device *dev;
23 struct xen_blkif *blkif;
24 struct xenbus_watch backend_watch;
25 unsigned major;
26 unsigned minor;
27 char *mode;
28 };
29
30 static struct kmem_cache *xen_blkif_cachep;
31 static void connect(struct backend_info *);
32 static int connect_ring(struct backend_info *);
33 static void backend_changed(struct xenbus_watch *, const char *,
34 const char *);
35 static void xen_blkif_free(struct xen_blkif *blkif);
36 static void xen_vbd_free(struct xen_vbd *vbd);
37
38 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
39 {
40 return be->dev;
41 }
42
43
44
45
46
47 static void xen_blkif_deferred_free(struct work_struct *work)
48 {
49 struct xen_blkif *blkif;
50
51 blkif = container_of(work, struct xen_blkif, free_work);
52 xen_blkif_free(blkif);
53 }
54
55 static int blkback_name(struct xen_blkif *blkif, char *buf)
56 {
57 char *devpath, *devname;
58 struct xenbus_device *dev = blkif->be->dev;
59
60 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
61 if (IS_ERR(devpath))
62 return PTR_ERR(devpath);
63
64 devname = strstr(devpath, "/dev/");
65 if (devname != NULL)
66 devname += strlen("/dev/");
67 else
68 devname = devpath;
69
70 snprintf(buf, TASK_COMM_LEN, "%d.%s", blkif->domid, devname);
71 kfree(devpath);
72
73 return 0;
74 }
75
76 static void xen_update_blkif_status(struct xen_blkif *blkif)
77 {
78 int err;
79 char name[TASK_COMM_LEN];
80 struct xen_blkif_ring *ring;
81 int i;
82
83
84 if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev)
85 return;
86
87
88 if (blkif->be->dev->state == XenbusStateConnected)
89 return;
90
91
92 connect(blkif->be);
93 if (blkif->be->dev->state != XenbusStateConnected)
94 return;
95
96 err = blkback_name(blkif, name);
97 if (err) {
98 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
99 return;
100 }
101
102 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
103 if (err) {
104 xenbus_dev_error(blkif->be->dev, err, "block flush");
105 return;
106 }
107 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
108
109 for (i = 0; i < blkif->nr_rings; i++) {
110 ring = &blkif->rings[i];
111 ring->xenblkd = kthread_run(xen_blkif_schedule, ring, "%s-%d", name, i);
112 if (IS_ERR(ring->xenblkd)) {
113 err = PTR_ERR(ring->xenblkd);
114 ring->xenblkd = NULL;
115 xenbus_dev_fatal(blkif->be->dev, err,
116 "start %s-%d xenblkd", name, i);
117 goto out;
118 }
119 }
120 return;
121
122 out:
123 while (--i >= 0) {
124 ring = &blkif->rings[i];
125 kthread_stop(ring->xenblkd);
126 }
127 return;
128 }
129
130 static int xen_blkif_alloc_rings(struct xen_blkif *blkif)
131 {
132 unsigned int r;
133
134 blkif->rings = kcalloc(blkif->nr_rings, sizeof(struct xen_blkif_ring),
135 GFP_KERNEL);
136 if (!blkif->rings)
137 return -ENOMEM;
138
139 for (r = 0; r < blkif->nr_rings; r++) {
140 struct xen_blkif_ring *ring = &blkif->rings[r];
141
142 spin_lock_init(&ring->blk_ring_lock);
143 init_waitqueue_head(&ring->wq);
144 INIT_LIST_HEAD(&ring->pending_free);
145 INIT_LIST_HEAD(&ring->persistent_purge_list);
146 INIT_WORK(&ring->persistent_purge_work, xen_blkbk_unmap_purged_grants);
147 spin_lock_init(&ring->free_pages_lock);
148 INIT_LIST_HEAD(&ring->free_pages);
149
150 spin_lock_init(&ring->pending_free_lock);
151 init_waitqueue_head(&ring->pending_free_wq);
152 init_waitqueue_head(&ring->shutdown_wq);
153 ring->blkif = blkif;
154 ring->st_print = jiffies;
155 ring->active = true;
156 }
157
158 return 0;
159 }
160
161 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
162 {
163 struct xen_blkif *blkif;
164
165 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
166
167 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
168 if (!blkif)
169 return ERR_PTR(-ENOMEM);
170
171 blkif->domid = domid;
172 atomic_set(&blkif->refcnt, 1);
173 init_completion(&blkif->drain_complete);
174
175
176
177
178
179
180
181
182 __module_get(THIS_MODULE);
183 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
184
185 return blkif;
186 }
187
188 static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
189 unsigned int nr_grefs, unsigned int evtchn)
190 {
191 int err;
192 struct xen_blkif *blkif = ring->blkif;
193
194
195 if (ring->irq)
196 return 0;
197
198 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
199 &ring->blk_ring);
200 if (err < 0)
201 return err;
202
203 switch (blkif->blk_protocol) {
204 case BLKIF_PROTOCOL_NATIVE:
205 {
206 struct blkif_sring *sring;
207 sring = (struct blkif_sring *)ring->blk_ring;
208 BACK_RING_INIT(&ring->blk_rings.native, sring,
209 XEN_PAGE_SIZE * nr_grefs);
210 break;
211 }
212 case BLKIF_PROTOCOL_X86_32:
213 {
214 struct blkif_x86_32_sring *sring_x86_32;
215 sring_x86_32 = (struct blkif_x86_32_sring *)ring->blk_ring;
216 BACK_RING_INIT(&ring->blk_rings.x86_32, sring_x86_32,
217 XEN_PAGE_SIZE * nr_grefs);
218 break;
219 }
220 case BLKIF_PROTOCOL_X86_64:
221 {
222 struct blkif_x86_64_sring *sring_x86_64;
223 sring_x86_64 = (struct blkif_x86_64_sring *)ring->blk_ring;
224 BACK_RING_INIT(&ring->blk_rings.x86_64, sring_x86_64,
225 XEN_PAGE_SIZE * nr_grefs);
226 break;
227 }
228 default:
229 BUG();
230 }
231
232 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
233 xen_blkif_be_int, 0,
234 "blkif-backend", ring);
235 if (err < 0) {
236 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
237 ring->blk_rings.common.sring = NULL;
238 return err;
239 }
240 ring->irq = err;
241
242 return 0;
243 }
244
245 static int xen_blkif_disconnect(struct xen_blkif *blkif)
246 {
247 struct pending_req *req, *n;
248 unsigned int j, r;
249 bool busy = false;
250
251 for (r = 0; r < blkif->nr_rings; r++) {
252 struct xen_blkif_ring *ring = &blkif->rings[r];
253 unsigned int i = 0;
254
255 if (!ring->active)
256 continue;
257
258 if (ring->xenblkd) {
259 kthread_stop(ring->xenblkd);
260 wake_up(&ring->shutdown_wq);
261 }
262
263
264
265
266
267 if (atomic_read(&ring->inflight) > 0) {
268 busy = true;
269 continue;
270 }
271
272 if (ring->irq) {
273 unbind_from_irqhandler(ring->irq, ring);
274 ring->irq = 0;
275 }
276
277 if (ring->blk_rings.common.sring) {
278 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
279 ring->blk_rings.common.sring = NULL;
280 }
281
282
283 xen_blkbk_free_caches(ring);
284
285
286 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
287 list_del(&req->free_list);
288
289 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
290 kfree(req->segments[j]);
291
292 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
293 kfree(req->indirect_pages[j]);
294
295 kfree(req);
296 i++;
297 }
298
299 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
300 BUG_ON(!list_empty(&ring->persistent_purge_list));
301 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
302 BUG_ON(!list_empty(&ring->free_pages));
303 BUG_ON(ring->free_pages_num != 0);
304 BUG_ON(ring->persistent_gnt_c != 0);
305 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
306 ring->active = false;
307 }
308 if (busy)
309 return -EBUSY;
310
311 blkif->nr_ring_pages = 0;
312
313
314
315
316 kfree(blkif->rings);
317 blkif->rings = NULL;
318 blkif->nr_rings = 0;
319
320 return 0;
321 }
322
323 static void xen_blkif_free(struct xen_blkif *blkif)
324 {
325 WARN_ON(xen_blkif_disconnect(blkif));
326 xen_vbd_free(&blkif->vbd);
327 kfree(blkif->be->mode);
328 kfree(blkif->be);
329
330
331 kmem_cache_free(xen_blkif_cachep, blkif);
332 module_put(THIS_MODULE);
333 }
334
335 int __init xen_blkif_interface_init(void)
336 {
337 xen_blkif_cachep = kmem_cache_create("blkif_cache",
338 sizeof(struct xen_blkif),
339 0, 0, NULL);
340 if (!xen_blkif_cachep)
341 return -ENOMEM;
342
343 return 0;
344 }
345
346
347
348
349
350 #define VBD_SHOW_ALLRING(name, format) \
351 static ssize_t show_##name(struct device *_dev, \
352 struct device_attribute *attr, \
353 char *buf) \
354 { \
355 struct xenbus_device *dev = to_xenbus_device(_dev); \
356 struct backend_info *be = dev_get_drvdata(&dev->dev); \
357 struct xen_blkif *blkif = be->blkif; \
358 unsigned int i; \
359 unsigned long long result = 0; \
360 \
361 if (!blkif->rings) \
362 goto out; \
363 \
364 for (i = 0; i < blkif->nr_rings; i++) { \
365 struct xen_blkif_ring *ring = &blkif->rings[i]; \
366 \
367 result += ring->st_##name; \
368 } \
369 \
370 out: \
371 return sprintf(buf, format, result); \
372 } \
373 static DEVICE_ATTR(name, 0444, show_##name, NULL)
374
375 VBD_SHOW_ALLRING(oo_req, "%llu\n");
376 VBD_SHOW_ALLRING(rd_req, "%llu\n");
377 VBD_SHOW_ALLRING(wr_req, "%llu\n");
378 VBD_SHOW_ALLRING(f_req, "%llu\n");
379 VBD_SHOW_ALLRING(ds_req, "%llu\n");
380 VBD_SHOW_ALLRING(rd_sect, "%llu\n");
381 VBD_SHOW_ALLRING(wr_sect, "%llu\n");
382
383 static struct attribute *xen_vbdstat_attrs[] = {
384 &dev_attr_oo_req.attr,
385 &dev_attr_rd_req.attr,
386 &dev_attr_wr_req.attr,
387 &dev_attr_f_req.attr,
388 &dev_attr_ds_req.attr,
389 &dev_attr_rd_sect.attr,
390 &dev_attr_wr_sect.attr,
391 NULL
392 };
393
394 static const struct attribute_group xen_vbdstat_group = {
395 .name = "statistics",
396 .attrs = xen_vbdstat_attrs,
397 };
398
399 #define VBD_SHOW(name, format, args...) \
400 static ssize_t show_##name(struct device *_dev, \
401 struct device_attribute *attr, \
402 char *buf) \
403 { \
404 struct xenbus_device *dev = to_xenbus_device(_dev); \
405 struct backend_info *be = dev_get_drvdata(&dev->dev); \
406 \
407 return sprintf(buf, format, ##args); \
408 } \
409 static DEVICE_ATTR(name, 0444, show_##name, NULL)
410
411 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
412 VBD_SHOW(mode, "%s\n", be->mode);
413
414 static int xenvbd_sysfs_addif(struct xenbus_device *dev)
415 {
416 int error;
417
418 error = device_create_file(&dev->dev, &dev_attr_physical_device);
419 if (error)
420 goto fail1;
421
422 error = device_create_file(&dev->dev, &dev_attr_mode);
423 if (error)
424 goto fail2;
425
426 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
427 if (error)
428 goto fail3;
429
430 return 0;
431
432 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
433 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
434 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
435 return error;
436 }
437
438 static void xenvbd_sysfs_delif(struct xenbus_device *dev)
439 {
440 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
441 device_remove_file(&dev->dev, &dev_attr_mode);
442 device_remove_file(&dev->dev, &dev_attr_physical_device);
443 }
444
445
446 static void xen_vbd_free(struct xen_vbd *vbd)
447 {
448 if (vbd->bdev)
449 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
450 vbd->bdev = NULL;
451 }
452
453 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
454 unsigned major, unsigned minor, int readonly,
455 int cdrom)
456 {
457 struct xen_vbd *vbd;
458 struct block_device *bdev;
459 struct request_queue *q;
460
461 vbd = &blkif->vbd;
462 vbd->handle = handle;
463 vbd->readonly = readonly;
464 vbd->type = 0;
465
466 vbd->pdevice = MKDEV(major, minor);
467
468 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
469 FMODE_READ : FMODE_WRITE, NULL);
470
471 if (IS_ERR(bdev)) {
472 pr_warn("xen_vbd_create: device %08x could not be opened\n",
473 vbd->pdevice);
474 return -ENOENT;
475 }
476
477 vbd->bdev = bdev;
478 if (vbd->bdev->bd_disk == NULL) {
479 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
480 vbd->pdevice);
481 xen_vbd_free(vbd);
482 return -ENOENT;
483 }
484 vbd->size = vbd_sz(vbd);
485
486 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
487 vbd->type |= VDISK_CDROM;
488 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
489 vbd->type |= VDISK_REMOVABLE;
490
491 q = bdev_get_queue(bdev);
492 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags))
493 vbd->flush_support = true;
494
495 if (q && blk_queue_secure_erase(q))
496 vbd->discard_secure = true;
497
498 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
499 handle, blkif->domid);
500 return 0;
501 }
502 static int xen_blkbk_remove(struct xenbus_device *dev)
503 {
504 struct backend_info *be = dev_get_drvdata(&dev->dev);
505
506 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
507
508 if (be->major || be->minor)
509 xenvbd_sysfs_delif(dev);
510
511 if (be->backend_watch.node) {
512 unregister_xenbus_watch(&be->backend_watch);
513 kfree(be->backend_watch.node);
514 be->backend_watch.node = NULL;
515 }
516
517 dev_set_drvdata(&dev->dev, NULL);
518
519 if (be->blkif) {
520 xen_blkif_disconnect(be->blkif);
521
522
523 xen_blkif_put(be->blkif);
524 }
525
526 return 0;
527 }
528
529 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
530 struct backend_info *be, int state)
531 {
532 struct xenbus_device *dev = be->dev;
533 int err;
534
535 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
536 "%d", state);
537 if (err)
538 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
539
540 return err;
541 }
542
543 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
544 {
545 struct xenbus_device *dev = be->dev;
546 struct xen_blkif *blkif = be->blkif;
547 int err;
548 int state = 0;
549 struct block_device *bdev = be->blkif->vbd.bdev;
550 struct request_queue *q = bdev_get_queue(bdev);
551
552 if (!xenbus_read_unsigned(dev->nodename, "discard-enable", 1))
553 return;
554
555 if (blk_queue_discard(q)) {
556 err = xenbus_printf(xbt, dev->nodename,
557 "discard-granularity", "%u",
558 q->limits.discard_granularity);
559 if (err) {
560 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
561 return;
562 }
563 err = xenbus_printf(xbt, dev->nodename,
564 "discard-alignment", "%u",
565 q->limits.discard_alignment);
566 if (err) {
567 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
568 return;
569 }
570 state = 1;
571
572 err = xenbus_printf(xbt, dev->nodename,
573 "discard-secure", "%d",
574 blkif->vbd.discard_secure);
575 if (err) {
576 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
577 return;
578 }
579 }
580 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
581 "%d", state);
582 if (err)
583 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
584 }
585 int xen_blkbk_barrier(struct xenbus_transaction xbt,
586 struct backend_info *be, int state)
587 {
588 struct xenbus_device *dev = be->dev;
589 int err;
590
591 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
592 "%d", state);
593 if (err)
594 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
595
596 return err;
597 }
598
599
600
601
602
603
604 static int xen_blkbk_probe(struct xenbus_device *dev,
605 const struct xenbus_device_id *id)
606 {
607 int err;
608 struct backend_info *be = kzalloc(sizeof(struct backend_info),
609 GFP_KERNEL);
610
611
612 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
613
614 if (!be) {
615 xenbus_dev_fatal(dev, -ENOMEM,
616 "allocating backend structure");
617 return -ENOMEM;
618 }
619 be->dev = dev;
620 dev_set_drvdata(&dev->dev, be);
621
622 be->blkif = xen_blkif_alloc(dev->otherend_id);
623 if (IS_ERR(be->blkif)) {
624 err = PTR_ERR(be->blkif);
625 be->blkif = NULL;
626 xenbus_dev_fatal(dev, err, "creating block interface");
627 goto fail;
628 }
629
630 err = xenbus_printf(XBT_NIL, dev->nodename,
631 "feature-max-indirect-segments", "%u",
632 MAX_INDIRECT_SEGMENTS);
633 if (err)
634 dev_warn(&dev->dev,
635 "writing %s/feature-max-indirect-segments (%d)",
636 dev->nodename, err);
637
638
639 err = xenbus_printf(XBT_NIL, dev->nodename,
640 "multi-queue-max-queues", "%u", xenblk_max_queues);
641 if (err)
642 pr_warn("Error writing multi-queue-max-queues\n");
643
644
645 be->blkif->be = be;
646
647 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
648 "%s/%s", dev->nodename, "physical-device");
649 if (err)
650 goto fail;
651
652 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
653 xen_blkif_max_ring_order);
654 if (err)
655 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
656
657 err = xenbus_switch_state(dev, XenbusStateInitWait);
658 if (err)
659 goto fail;
660
661 return 0;
662
663 fail:
664 pr_warn("%s failed\n", __func__);
665 xen_blkbk_remove(dev);
666 return err;
667 }
668
669
670
671
672
673
674
675 static void backend_changed(struct xenbus_watch *watch,
676 const char *path, const char *token)
677 {
678 int err;
679 unsigned major;
680 unsigned minor;
681 struct backend_info *be
682 = container_of(watch, struct backend_info, backend_watch);
683 struct xenbus_device *dev = be->dev;
684 int cdrom = 0;
685 unsigned long handle;
686 char *device_type;
687
688 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
689
690 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
691 &major, &minor);
692 if (XENBUS_EXIST_ERR(err)) {
693
694
695
696
697
698 return;
699 }
700 if (err != 2) {
701 xenbus_dev_fatal(dev, err, "reading physical-device");
702 return;
703 }
704
705 if (be->major | be->minor) {
706 if (be->major != major || be->minor != minor)
707 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
708 be->major, be->minor, major, minor);
709 return;
710 }
711
712 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
713 if (IS_ERR(be->mode)) {
714 err = PTR_ERR(be->mode);
715 be->mode = NULL;
716 xenbus_dev_fatal(dev, err, "reading mode");
717 return;
718 }
719
720 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
721 if (!IS_ERR(device_type)) {
722 cdrom = strcmp(device_type, "cdrom") == 0;
723 kfree(device_type);
724 }
725
726
727 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
728 if (err) {
729 kfree(be->mode);
730 be->mode = NULL;
731 return;
732 }
733
734 be->major = major;
735 be->minor = minor;
736
737 err = xen_vbd_create(be->blkif, handle, major, minor,
738 !strchr(be->mode, 'w'), cdrom);
739
740 if (err)
741 xenbus_dev_fatal(dev, err, "creating vbd structure");
742 else {
743 err = xenvbd_sysfs_addif(dev);
744 if (err) {
745 xen_vbd_free(&be->blkif->vbd);
746 xenbus_dev_fatal(dev, err, "creating sysfs entries");
747 }
748 }
749
750 if (err) {
751 kfree(be->mode);
752 be->mode = NULL;
753 be->major = 0;
754 be->minor = 0;
755 } else {
756
757 xen_update_blkif_status(be->blkif);
758 }
759 }
760
761
762
763
764
765 static void frontend_changed(struct xenbus_device *dev,
766 enum xenbus_state frontend_state)
767 {
768 struct backend_info *be = dev_get_drvdata(&dev->dev);
769 int err;
770
771 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
772
773 switch (frontend_state) {
774 case XenbusStateInitialising:
775 if (dev->state == XenbusStateClosed) {
776 pr_info("%s: prepare for reconnect\n", dev->nodename);
777 xenbus_switch_state(dev, XenbusStateInitWait);
778 }
779 break;
780
781 case XenbusStateInitialised:
782 case XenbusStateConnected:
783
784
785
786
787
788 if (dev->state == XenbusStateConnected)
789 break;
790
791
792
793
794
795 err = xen_blkif_disconnect(be->blkif);
796 if (err) {
797 xenbus_dev_fatal(dev, err, "pending I/O");
798 break;
799 }
800
801 err = connect_ring(be);
802 if (err) {
803
804
805
806
807 xen_blkif_disconnect(be->blkif);
808 break;
809 }
810 xen_update_blkif_status(be->blkif);
811 break;
812
813 case XenbusStateClosing:
814 xenbus_switch_state(dev, XenbusStateClosing);
815 break;
816
817 case XenbusStateClosed:
818 xen_blkif_disconnect(be->blkif);
819 xenbus_switch_state(dev, XenbusStateClosed);
820 if (xenbus_dev_is_online(dev))
821 break;
822
823
824 case XenbusStateUnknown:
825
826 device_unregister(&dev->dev);
827 break;
828
829 default:
830 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
831 frontend_state);
832 break;
833 }
834 }
835
836
837
838
839
840
841
842
843
844 static void connect(struct backend_info *be)
845 {
846 struct xenbus_transaction xbt;
847 int err;
848 struct xenbus_device *dev = be->dev;
849
850 pr_debug("%s %s\n", __func__, dev->otherend);
851
852
853 again:
854 err = xenbus_transaction_start(&xbt);
855 if (err) {
856 xenbus_dev_fatal(dev, err, "starting transaction");
857 return;
858 }
859
860
861 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
862
863 xen_blkbk_discard(xbt, be);
864
865 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
866
867 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
868 if (err) {
869 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
870 dev->nodename);
871 goto abort;
872 }
873
874 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
875 (unsigned long long)vbd_sz(&be->blkif->vbd));
876 if (err) {
877 xenbus_dev_fatal(dev, err, "writing %s/sectors",
878 dev->nodename);
879 goto abort;
880 }
881
882
883 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
884 be->blkif->vbd.type |
885 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
886 if (err) {
887 xenbus_dev_fatal(dev, err, "writing %s/info",
888 dev->nodename);
889 goto abort;
890 }
891 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
892 (unsigned long)
893 bdev_logical_block_size(be->blkif->vbd.bdev));
894 if (err) {
895 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
896 dev->nodename);
897 goto abort;
898 }
899 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
900 bdev_physical_block_size(be->blkif->vbd.bdev));
901 if (err)
902 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
903 dev->nodename);
904
905 err = xenbus_transaction_end(xbt, 0);
906 if (err == -EAGAIN)
907 goto again;
908 if (err)
909 xenbus_dev_fatal(dev, err, "ending transaction");
910
911 err = xenbus_switch_state(dev, XenbusStateConnected);
912 if (err)
913 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
914 dev->nodename);
915
916 return;
917 abort:
918 xenbus_transaction_end(xbt, 1);
919 }
920
921
922
923
924 static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
925 {
926 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
927 struct pending_req *req, *n;
928 int err, i, j;
929 struct xen_blkif *blkif = ring->blkif;
930 struct xenbus_device *dev = blkif->be->dev;
931 unsigned int nr_grefs, evtchn;
932
933 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
934 &evtchn);
935 if (err != 1) {
936 err = -EINVAL;
937 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
938 return err;
939 }
940
941 nr_grefs = blkif->nr_ring_pages;
942
943 if (unlikely(!nr_grefs)) {
944 WARN_ON(true);
945 return -EINVAL;
946 }
947
948 for (i = 0; i < nr_grefs; i++) {
949 char ring_ref_name[RINGREF_NAME_LEN];
950
951 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
952 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
953 "%u", &ring_ref[i]);
954
955 if (err != 1) {
956 if (nr_grefs == 1)
957 break;
958
959 err = -EINVAL;
960 xenbus_dev_fatal(dev, err, "reading %s/%s",
961 dir, ring_ref_name);
962 return err;
963 }
964 }
965
966 if (err != 1) {
967 WARN_ON(nr_grefs != 1);
968
969 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u",
970 &ring_ref[0]);
971 if (err != 1) {
972 err = -EINVAL;
973 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
974 return err;
975 }
976 }
977
978 err = -ENOMEM;
979 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
980 req = kzalloc(sizeof(*req), GFP_KERNEL);
981 if (!req)
982 goto fail;
983 list_add_tail(&req->free_list, &ring->pending_free);
984 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
985 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
986 if (!req->segments[j])
987 goto fail;
988 }
989 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
990 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
991 GFP_KERNEL);
992 if (!req->indirect_pages[j])
993 goto fail;
994 }
995 }
996
997
998 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
999 if (err) {
1000 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
1001 goto fail;
1002 }
1003
1004 return 0;
1005
1006 fail:
1007 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
1008 list_del(&req->free_list);
1009 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
1010 if (!req->segments[j])
1011 break;
1012 kfree(req->segments[j]);
1013 }
1014 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1015 if (!req->indirect_pages[j])
1016 break;
1017 kfree(req->indirect_pages[j]);
1018 }
1019 kfree(req);
1020 }
1021 return err;
1022 }
1023
1024 static int connect_ring(struct backend_info *be)
1025 {
1026 struct xenbus_device *dev = be->dev;
1027 struct xen_blkif *blkif = be->blkif;
1028 unsigned int pers_grants;
1029 char protocol[64] = "";
1030 int err, i;
1031 char *xspath;
1032 size_t xspathsize;
1033 const size_t xenstore_path_ext_size = 11;
1034 unsigned int requested_num_queues = 0;
1035 unsigned int ring_page_order;
1036
1037 pr_debug("%s %s\n", __func__, dev->otherend);
1038
1039 blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
1040 err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol",
1041 "%63s", protocol);
1042 if (err <= 0)
1043 strcpy(protocol, "unspecified, assuming default");
1044 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
1045 blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
1046 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
1047 blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
1048 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
1049 blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
1050 else {
1051 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
1052 return -ENOSYS;
1053 }
1054 pers_grants = xenbus_read_unsigned(dev->otherend, "feature-persistent",
1055 0);
1056 blkif->vbd.feature_gnt_persistent = pers_grants;
1057 blkif->vbd.overflow_max_grants = 0;
1058
1059
1060
1061
1062 requested_num_queues = xenbus_read_unsigned(dev->otherend,
1063 "multi-queue-num-queues",
1064 1);
1065 if (requested_num_queues > xenblk_max_queues
1066 || requested_num_queues == 0) {
1067
1068 xenbus_dev_fatal(dev, err,
1069 "guest requested %u queues, exceeding the maximum of %u.",
1070 requested_num_queues, xenblk_max_queues);
1071 return -ENOSYS;
1072 }
1073 blkif->nr_rings = requested_num_queues;
1074 if (xen_blkif_alloc_rings(blkif))
1075 return -ENOMEM;
1076
1077 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
1078 blkif->nr_rings, blkif->blk_protocol, protocol,
1079 pers_grants ? "persistent grants" : "");
1080
1081 ring_page_order = xenbus_read_unsigned(dev->otherend,
1082 "ring-page-order", 0);
1083
1084 if (ring_page_order > xen_blkif_max_ring_order) {
1085 err = -EINVAL;
1086 xenbus_dev_fatal(dev, err,
1087 "requested ring page order %d exceed max:%d",
1088 ring_page_order,
1089 xen_blkif_max_ring_order);
1090 return err;
1091 }
1092
1093 blkif->nr_ring_pages = 1 << ring_page_order;
1094
1095 if (blkif->nr_rings == 1)
1096 return read_per_ring_refs(&blkif->rings[0], dev->otherend);
1097 else {
1098 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1099 xspath = kmalloc(xspathsize, GFP_KERNEL);
1100 if (!xspath) {
1101 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
1102 return -ENOMEM;
1103 }
1104
1105 for (i = 0; i < blkif->nr_rings; i++) {
1106 memset(xspath, 0, xspathsize);
1107 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
1108 err = read_per_ring_refs(&blkif->rings[i], xspath);
1109 if (err) {
1110 kfree(xspath);
1111 return err;
1112 }
1113 }
1114 kfree(xspath);
1115 }
1116 return 0;
1117 }
1118
1119 static const struct xenbus_device_id xen_blkbk_ids[] = {
1120 { "vbd" },
1121 { "" }
1122 };
1123
1124 static struct xenbus_driver xen_blkbk_driver = {
1125 .ids = xen_blkbk_ids,
1126 .probe = xen_blkbk_probe,
1127 .remove = xen_blkbk_remove,
1128 .otherend_changed = frontend_changed
1129 };
1130
1131 int xen_blkif_xenbus_init(void)
1132 {
1133 return xenbus_register_backend(&xen_blkbk_driver);
1134 }