1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Xenbus code for blkif backend
3 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
4 Copyright (C) 2005 XenSource Ltd
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 /* On the XenBus the max length of 'ring-ref%u'. */
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
xen_blkbk_xenbus(struct backend_info * be)38 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
39 {
40 return be->dev;
41 }
42
43 /*
44 * The last request could free the device from softirq context and
45 * xen_blkif_free() can sleep.
46 */
xen_blkif_deferred_free(struct work_struct * work)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
blkback_name(struct xen_blkif * blkif,char * buf)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
xen_update_blkif_status(struct xen_blkif * blkif)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 /* Not ready to connect? */
84 if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev)
85 return;
86
87 /* Already connected? */
88 if (blkif->be->dev->state == XenbusStateConnected)
89 return;
90
91 /* Attempt to connect: exit if we fail to. */
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
xen_blkif_alloc_rings(struct xen_blkif * blkif)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 gnttab_page_cache_init(&ring->free_pages);
148
149 spin_lock_init(&ring->pending_free_lock);
150 init_waitqueue_head(&ring->pending_free_wq);
151 init_waitqueue_head(&ring->shutdown_wq);
152 ring->blkif = blkif;
153 ring->st_print = jiffies;
154 ring->active = true;
155 }
156
157 return 0;
158 }
159
xen_blkif_alloc(domid_t domid)160 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
161 {
162 struct xen_blkif *blkif;
163
164 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
165
166 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
167 if (!blkif)
168 return ERR_PTR(-ENOMEM);
169
170 blkif->domid = domid;
171 atomic_set(&blkif->refcnt, 1);
172 init_completion(&blkif->drain_complete);
173
174 /*
175 * Because freeing back to the cache may be deferred, it is not
176 * safe to unload the module (and hence destroy the cache) until
177 * this has completed. To prevent premature unloading, take an
178 * extra module reference here and release only when the object
179 * has been freed back to the cache.
180 */
181 __module_get(THIS_MODULE);
182 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
183
184 return blkif;
185 }
186
xen_blkif_map(struct xen_blkif_ring * ring,grant_ref_t * gref,unsigned int nr_grefs,unsigned int evtchn)187 static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
188 unsigned int nr_grefs, unsigned int evtchn)
189 {
190 int err;
191 struct xen_blkif *blkif = ring->blkif;
192 const struct blkif_common_sring *sring_common;
193 RING_IDX rsp_prod, req_prod;
194 unsigned int size;
195
196 /* Already connected through? */
197 if (ring->irq)
198 return 0;
199
200 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
201 &ring->blk_ring);
202 if (err < 0)
203 return err;
204
205 sring_common = (struct blkif_common_sring *)ring->blk_ring;
206 rsp_prod = READ_ONCE(sring_common->rsp_prod);
207 req_prod = READ_ONCE(sring_common->req_prod);
208
209 switch (blkif->blk_protocol) {
210 case BLKIF_PROTOCOL_NATIVE:
211 {
212 struct blkif_sring *sring_native =
213 (struct blkif_sring *)ring->blk_ring;
214
215 BACK_RING_ATTACH(&ring->blk_rings.native, sring_native,
216 rsp_prod, XEN_PAGE_SIZE * nr_grefs);
217 size = __RING_SIZE(sring_native, XEN_PAGE_SIZE * nr_grefs);
218 break;
219 }
220 case BLKIF_PROTOCOL_X86_32:
221 {
222 struct blkif_x86_32_sring *sring_x86_32 =
223 (struct blkif_x86_32_sring *)ring->blk_ring;
224
225 BACK_RING_ATTACH(&ring->blk_rings.x86_32, sring_x86_32,
226 rsp_prod, XEN_PAGE_SIZE * nr_grefs);
227 size = __RING_SIZE(sring_x86_32, XEN_PAGE_SIZE * nr_grefs);
228 break;
229 }
230 case BLKIF_PROTOCOL_X86_64:
231 {
232 struct blkif_x86_64_sring *sring_x86_64 =
233 (struct blkif_x86_64_sring *)ring->blk_ring;
234
235 BACK_RING_ATTACH(&ring->blk_rings.x86_64, sring_x86_64,
236 rsp_prod, XEN_PAGE_SIZE * nr_grefs);
237 size = __RING_SIZE(sring_x86_64, XEN_PAGE_SIZE * nr_grefs);
238 break;
239 }
240 default:
241 BUG();
242 }
243
244 err = -EIO;
245 if (req_prod - rsp_prod > size)
246 goto fail;
247
248 err = bind_interdomain_evtchn_to_irqhandler_lateeoi(blkif->domid,
249 evtchn, xen_blkif_be_int, 0, "blkif-backend", ring);
250 if (err < 0)
251 goto fail;
252 ring->irq = err;
253
254 return 0;
255
256 fail:
257 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
258 ring->blk_rings.common.sring = NULL;
259 return err;
260 }
261
xen_blkif_disconnect(struct xen_blkif * blkif)262 static int xen_blkif_disconnect(struct xen_blkif *blkif)
263 {
264 struct pending_req *req, *n;
265 unsigned int j, r;
266 bool busy = false;
267
268 for (r = 0; r < blkif->nr_rings; r++) {
269 struct xen_blkif_ring *ring = &blkif->rings[r];
270 unsigned int i = 0;
271
272 if (!ring->active)
273 continue;
274
275 if (ring->xenblkd) {
276 kthread_stop(ring->xenblkd);
277 wake_up(&ring->shutdown_wq);
278 }
279
280 /* The above kthread_stop() guarantees that at this point we
281 * don't have any discard_io or other_io requests. So, checking
282 * for inflight IO is enough.
283 */
284 if (atomic_read(&ring->inflight) > 0) {
285 busy = true;
286 continue;
287 }
288
289 if (ring->irq) {
290 unbind_from_irqhandler(ring->irq, ring);
291 ring->irq = 0;
292 }
293
294 if (ring->blk_rings.common.sring) {
295 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
296 ring->blk_rings.common.sring = NULL;
297 }
298
299 /* Remove all persistent grants and the cache of ballooned pages. */
300 xen_blkbk_free_caches(ring);
301
302 /* Check that there is no request in use */
303 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
304 list_del(&req->free_list);
305
306 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
307 kfree(req->segments[j]);
308
309 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
310 kfree(req->indirect_pages[j]);
311
312 kfree(req);
313 i++;
314 }
315
316 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
317 BUG_ON(!list_empty(&ring->persistent_purge_list));
318 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
319 BUG_ON(ring->free_pages.num_pages != 0);
320 BUG_ON(ring->persistent_gnt_c != 0);
321 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
322 ring->active = false;
323 }
324 if (busy)
325 return -EBUSY;
326
327 blkif->nr_ring_pages = 0;
328 /*
329 * blkif->rings was allocated in connect_ring, so we should free it in
330 * here.
331 */
332 kfree(blkif->rings);
333 blkif->rings = NULL;
334 blkif->nr_rings = 0;
335
336 return 0;
337 }
338
xen_blkif_free(struct xen_blkif * blkif)339 static void xen_blkif_free(struct xen_blkif *blkif)
340 {
341 WARN_ON(xen_blkif_disconnect(blkif));
342 xen_vbd_free(&blkif->vbd);
343 kfree(blkif->be->mode);
344 kfree(blkif->be);
345
346 /* Make sure everything is drained before shutting down */
347 kmem_cache_free(xen_blkif_cachep, blkif);
348 module_put(THIS_MODULE);
349 }
350
xen_blkif_interface_init(void)351 int __init xen_blkif_interface_init(void)
352 {
353 xen_blkif_cachep = kmem_cache_create("blkif_cache",
354 sizeof(struct xen_blkif),
355 0, 0, NULL);
356 if (!xen_blkif_cachep)
357 return -ENOMEM;
358
359 return 0;
360 }
361
xen_blkif_interface_fini(void)362 void xen_blkif_interface_fini(void)
363 {
364 kmem_cache_destroy(xen_blkif_cachep);
365 xen_blkif_cachep = NULL;
366 }
367
368 /*
369 * sysfs interface for VBD I/O requests
370 */
371
372 #define VBD_SHOW_ALLRING(name, format) \
373 static ssize_t show_##name(struct device *_dev, \
374 struct device_attribute *attr, \
375 char *buf) \
376 { \
377 struct xenbus_device *dev = to_xenbus_device(_dev); \
378 struct backend_info *be = dev_get_drvdata(&dev->dev); \
379 struct xen_blkif *blkif = be->blkif; \
380 unsigned int i; \
381 unsigned long long result = 0; \
382 \
383 if (!blkif->rings) \
384 goto out; \
385 \
386 for (i = 0; i < blkif->nr_rings; i++) { \
387 struct xen_blkif_ring *ring = &blkif->rings[i]; \
388 \
389 result += ring->st_##name; \
390 } \
391 \
392 out: \
393 return sprintf(buf, format, result); \
394 } \
395 static DEVICE_ATTR(name, 0444, show_##name, NULL)
396
397 VBD_SHOW_ALLRING(oo_req, "%llu\n");
398 VBD_SHOW_ALLRING(rd_req, "%llu\n");
399 VBD_SHOW_ALLRING(wr_req, "%llu\n");
400 VBD_SHOW_ALLRING(f_req, "%llu\n");
401 VBD_SHOW_ALLRING(ds_req, "%llu\n");
402 VBD_SHOW_ALLRING(rd_sect, "%llu\n");
403 VBD_SHOW_ALLRING(wr_sect, "%llu\n");
404
405 static struct attribute *xen_vbdstat_attrs[] = {
406 &dev_attr_oo_req.attr,
407 &dev_attr_rd_req.attr,
408 &dev_attr_wr_req.attr,
409 &dev_attr_f_req.attr,
410 &dev_attr_ds_req.attr,
411 &dev_attr_rd_sect.attr,
412 &dev_attr_wr_sect.attr,
413 NULL
414 };
415
416 static const struct attribute_group xen_vbdstat_group = {
417 .name = "statistics",
418 .attrs = xen_vbdstat_attrs,
419 };
420
421 #define VBD_SHOW(name, format, args...) \
422 static ssize_t show_##name(struct device *_dev, \
423 struct device_attribute *attr, \
424 char *buf) \
425 { \
426 struct xenbus_device *dev = to_xenbus_device(_dev); \
427 struct backend_info *be = dev_get_drvdata(&dev->dev); \
428 \
429 return sprintf(buf, format, ##args); \
430 } \
431 static DEVICE_ATTR(name, 0444, show_##name, NULL)
432
433 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
434 VBD_SHOW(mode, "%s\n", be->mode);
435
xenvbd_sysfs_addif(struct xenbus_device * dev)436 static int xenvbd_sysfs_addif(struct xenbus_device *dev)
437 {
438 int error;
439
440 error = device_create_file(&dev->dev, &dev_attr_physical_device);
441 if (error)
442 goto fail1;
443
444 error = device_create_file(&dev->dev, &dev_attr_mode);
445 if (error)
446 goto fail2;
447
448 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
449 if (error)
450 goto fail3;
451
452 return 0;
453
454 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
455 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
456 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
457 return error;
458 }
459
xenvbd_sysfs_delif(struct xenbus_device * dev)460 static void xenvbd_sysfs_delif(struct xenbus_device *dev)
461 {
462 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
463 device_remove_file(&dev->dev, &dev_attr_mode);
464 device_remove_file(&dev->dev, &dev_attr_physical_device);
465 }
466
xen_vbd_free(struct xen_vbd * vbd)467 static void xen_vbd_free(struct xen_vbd *vbd)
468 {
469 if (vbd->bdev)
470 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
471 vbd->bdev = NULL;
472 }
473
474 /* Enable the persistent grants feature. */
475 static bool feature_persistent = true;
476 module_param(feature_persistent, bool, 0644);
477 MODULE_PARM_DESC(feature_persistent,
478 "Enables the persistent grants feature");
479
xen_vbd_create(struct xen_blkif * blkif,blkif_vdev_t handle,unsigned major,unsigned minor,int readonly,int cdrom)480 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
481 unsigned major, unsigned minor, int readonly,
482 int cdrom)
483 {
484 struct xen_vbd *vbd;
485 struct block_device *bdev;
486 struct request_queue *q;
487
488 vbd = &blkif->vbd;
489 vbd->handle = handle;
490 vbd->readonly = readonly;
491 vbd->type = 0;
492
493 vbd->pdevice = MKDEV(major, minor);
494
495 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
496 FMODE_READ : FMODE_WRITE, NULL);
497
498 if (IS_ERR(bdev)) {
499 pr_warn("xen_vbd_create: device %08x could not be opened\n",
500 vbd->pdevice);
501 return -ENOENT;
502 }
503
504 vbd->bdev = bdev;
505 if (vbd->bdev->bd_disk == NULL) {
506 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
507 vbd->pdevice);
508 xen_vbd_free(vbd);
509 return -ENOENT;
510 }
511 vbd->size = vbd_sz(vbd);
512
513 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
514 vbd->type |= VDISK_CDROM;
515 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
516 vbd->type |= VDISK_REMOVABLE;
517
518 q = bdev_get_queue(bdev);
519 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags))
520 vbd->flush_support = true;
521
522 if (q && blk_queue_secure_erase(q))
523 vbd->discard_secure = true;
524
525 vbd->feature_gnt_persistent = feature_persistent;
526
527 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
528 handle, blkif->domid);
529 return 0;
530 }
531
xen_blkbk_remove(struct xenbus_device * dev)532 static int xen_blkbk_remove(struct xenbus_device *dev)
533 {
534 struct backend_info *be = dev_get_drvdata(&dev->dev);
535
536 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
537
538 if (be->major || be->minor)
539 xenvbd_sysfs_delif(dev);
540
541 if (be->backend_watch.node) {
542 unregister_xenbus_watch(&be->backend_watch);
543 kfree(be->backend_watch.node);
544 be->backend_watch.node = NULL;
545 }
546
547 dev_set_drvdata(&dev->dev, NULL);
548
549 if (be->blkif) {
550 xen_blkif_disconnect(be->blkif);
551
552 /* Put the reference we set in xen_blkif_alloc(). */
553 xen_blkif_put(be->blkif);
554 }
555
556 return 0;
557 }
558
xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,struct backend_info * be,int state)559 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
560 struct backend_info *be, int state)
561 {
562 struct xenbus_device *dev = be->dev;
563 int err;
564
565 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
566 "%d", state);
567 if (err)
568 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
569
570 return err;
571 }
572
xen_blkbk_discard(struct xenbus_transaction xbt,struct backend_info * be)573 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
574 {
575 struct xenbus_device *dev = be->dev;
576 struct xen_blkif *blkif = be->blkif;
577 int err;
578 int state = 0;
579 struct block_device *bdev = be->blkif->vbd.bdev;
580 struct request_queue *q = bdev_get_queue(bdev);
581
582 if (!xenbus_read_unsigned(dev->nodename, "discard-enable", 1))
583 return;
584
585 if (blk_queue_discard(q)) {
586 err = xenbus_printf(xbt, dev->nodename,
587 "discard-granularity", "%u",
588 q->limits.discard_granularity);
589 if (err) {
590 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
591 return;
592 }
593 err = xenbus_printf(xbt, dev->nodename,
594 "discard-alignment", "%u",
595 q->limits.discard_alignment);
596 if (err) {
597 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
598 return;
599 }
600 state = 1;
601 /* Optional. */
602 err = xenbus_printf(xbt, dev->nodename,
603 "discard-secure", "%d",
604 blkif->vbd.discard_secure);
605 if (err) {
606 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
607 return;
608 }
609 }
610 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
611 "%d", state);
612 if (err)
613 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
614 }
615
xen_blkbk_barrier(struct xenbus_transaction xbt,struct backend_info * be,int state)616 int xen_blkbk_barrier(struct xenbus_transaction xbt,
617 struct backend_info *be, int state)
618 {
619 struct xenbus_device *dev = be->dev;
620 int err;
621
622 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
623 "%d", state);
624 if (err)
625 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
626
627 return err;
628 }
629
630 /*
631 * Entry point to this code when a new device is created. Allocate the basic
632 * structures, and watch the store waiting for the hotplug scripts to tell us
633 * the device's physical major and minor numbers. Switch to InitWait.
634 */
xen_blkbk_probe(struct xenbus_device * dev,const struct xenbus_device_id * id)635 static int xen_blkbk_probe(struct xenbus_device *dev,
636 const struct xenbus_device_id *id)
637 {
638 int err;
639 struct backend_info *be = kzalloc(sizeof(struct backend_info),
640 GFP_KERNEL);
641
642 /* match the pr_debug in xen_blkbk_remove */
643 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
644
645 if (!be) {
646 xenbus_dev_fatal(dev, -ENOMEM,
647 "allocating backend structure");
648 return -ENOMEM;
649 }
650 be->dev = dev;
651 dev_set_drvdata(&dev->dev, be);
652
653 be->blkif = xen_blkif_alloc(dev->otherend_id);
654 if (IS_ERR(be->blkif)) {
655 err = PTR_ERR(be->blkif);
656 be->blkif = NULL;
657 xenbus_dev_fatal(dev, err, "creating block interface");
658 goto fail;
659 }
660
661 err = xenbus_printf(XBT_NIL, dev->nodename,
662 "feature-max-indirect-segments", "%u",
663 MAX_INDIRECT_SEGMENTS);
664 if (err)
665 dev_warn(&dev->dev,
666 "writing %s/feature-max-indirect-segments (%d)",
667 dev->nodename, err);
668
669 /* Multi-queue: advertise how many queues are supported by us.*/
670 err = xenbus_printf(XBT_NIL, dev->nodename,
671 "multi-queue-max-queues", "%u", xenblk_max_queues);
672 if (err)
673 pr_warn("Error writing multi-queue-max-queues\n");
674
675 /* setup back pointer */
676 be->blkif->be = be;
677
678 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
679 "%s/%s", dev->nodename, "physical-device");
680 if (err)
681 goto fail;
682
683 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
684 xen_blkif_max_ring_order);
685 if (err)
686 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
687
688 err = xenbus_switch_state(dev, XenbusStateInitWait);
689 if (err)
690 goto fail;
691
692 return 0;
693
694 fail:
695 pr_warn("%s failed\n", __func__);
696 xen_blkbk_remove(dev);
697 return err;
698 }
699
700 /*
701 * Callback received when the hotplug scripts have placed the physical-device
702 * node. Read it and the mode node, and create a vbd. If the frontend is
703 * ready, connect.
704 */
backend_changed(struct xenbus_watch * watch,const char * path,const char * token)705 static void backend_changed(struct xenbus_watch *watch,
706 const char *path, const char *token)
707 {
708 int err;
709 unsigned major;
710 unsigned minor;
711 struct backend_info *be
712 = container_of(watch, struct backend_info, backend_watch);
713 struct xenbus_device *dev = be->dev;
714 int cdrom = 0;
715 unsigned long handle;
716 char *device_type;
717
718 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
719
720 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
721 &major, &minor);
722 if (XENBUS_EXIST_ERR(err)) {
723 /*
724 * Since this watch will fire once immediately after it is
725 * registered, we expect this. Ignore it, and wait for the
726 * hotplug scripts.
727 */
728 return;
729 }
730 if (err != 2) {
731 xenbus_dev_fatal(dev, err, "reading physical-device");
732 return;
733 }
734
735 if (be->major | be->minor) {
736 if (be->major != major || be->minor != minor)
737 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
738 be->major, be->minor, major, minor);
739 return;
740 }
741
742 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
743 if (IS_ERR(be->mode)) {
744 err = PTR_ERR(be->mode);
745 be->mode = NULL;
746 xenbus_dev_fatal(dev, err, "reading mode");
747 return;
748 }
749
750 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
751 if (!IS_ERR(device_type)) {
752 cdrom = strcmp(device_type, "cdrom") == 0;
753 kfree(device_type);
754 }
755
756 /* Front end dir is a number, which is used as the handle. */
757 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
758 if (err) {
759 kfree(be->mode);
760 be->mode = NULL;
761 return;
762 }
763
764 be->major = major;
765 be->minor = minor;
766
767 err = xen_vbd_create(be->blkif, handle, major, minor,
768 !strchr(be->mode, 'w'), cdrom);
769
770 if (err)
771 xenbus_dev_fatal(dev, err, "creating vbd structure");
772 else {
773 err = xenvbd_sysfs_addif(dev);
774 if (err) {
775 xen_vbd_free(&be->blkif->vbd);
776 xenbus_dev_fatal(dev, err, "creating sysfs entries");
777 }
778 }
779
780 if (err) {
781 kfree(be->mode);
782 be->mode = NULL;
783 be->major = 0;
784 be->minor = 0;
785 } else {
786 /* We're potentially connected now */
787 xen_update_blkif_status(be->blkif);
788 }
789 }
790
791 /*
792 * Callback received when the frontend's state changes.
793 */
frontend_changed(struct xenbus_device * dev,enum xenbus_state frontend_state)794 static void frontend_changed(struct xenbus_device *dev,
795 enum xenbus_state frontend_state)
796 {
797 struct backend_info *be = dev_get_drvdata(&dev->dev);
798 int err;
799
800 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
801
802 switch (frontend_state) {
803 case XenbusStateInitialising:
804 if (dev->state == XenbusStateClosed) {
805 pr_info("%s: prepare for reconnect\n", dev->nodename);
806 xenbus_switch_state(dev, XenbusStateInitWait);
807 }
808 break;
809
810 case XenbusStateInitialised:
811 case XenbusStateConnected:
812 /*
813 * Ensure we connect even when two watches fire in
814 * close succession and we miss the intermediate value
815 * of frontend_state.
816 */
817 if (dev->state == XenbusStateConnected)
818 break;
819
820 /*
821 * Enforce precondition before potential leak point.
822 * xen_blkif_disconnect() is idempotent.
823 */
824 err = xen_blkif_disconnect(be->blkif);
825 if (err) {
826 xenbus_dev_fatal(dev, err, "pending I/O");
827 break;
828 }
829
830 err = connect_ring(be);
831 if (err) {
832 /*
833 * Clean up so that memory resources can be used by
834 * other devices. connect_ring reported already error.
835 */
836 xen_blkif_disconnect(be->blkif);
837 break;
838 }
839 xen_update_blkif_status(be->blkif);
840 break;
841
842 case XenbusStateClosing:
843 xenbus_switch_state(dev, XenbusStateClosing);
844 break;
845
846 case XenbusStateClosed:
847 xen_blkif_disconnect(be->blkif);
848 xenbus_switch_state(dev, XenbusStateClosed);
849 if (xenbus_dev_is_online(dev))
850 break;
851 fallthrough;
852 /* if not online */
853 case XenbusStateUnknown:
854 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
855 device_unregister(&dev->dev);
856 break;
857
858 default:
859 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
860 frontend_state);
861 break;
862 }
863 }
864
865 /* Once a memory pressure is detected, squeeze free page pools for a while. */
866 static unsigned int buffer_squeeze_duration_ms = 10;
867 module_param_named(buffer_squeeze_duration_ms,
868 buffer_squeeze_duration_ms, int, 0644);
869 MODULE_PARM_DESC(buffer_squeeze_duration_ms,
870 "Duration in ms to squeeze pages buffer when a memory pressure is detected");
871
872 /*
873 * Callback received when the memory pressure is detected.
874 */
reclaim_memory(struct xenbus_device * dev)875 static void reclaim_memory(struct xenbus_device *dev)
876 {
877 struct backend_info *be = dev_get_drvdata(&dev->dev);
878
879 if (!be)
880 return;
881 be->blkif->buffer_squeeze_end = jiffies +
882 msecs_to_jiffies(buffer_squeeze_duration_ms);
883 }
884
885 /* ** Connection ** */
886
887 /*
888 * Write the physical details regarding the block device to the store, and
889 * switch to Connected state.
890 */
connect(struct backend_info * be)891 static void connect(struct backend_info *be)
892 {
893 struct xenbus_transaction xbt;
894 int err;
895 struct xenbus_device *dev = be->dev;
896
897 pr_debug("%s %s\n", __func__, dev->otherend);
898
899 /* Supply the information about the device the frontend needs */
900 again:
901 err = xenbus_transaction_start(&xbt);
902 if (err) {
903 xenbus_dev_fatal(dev, err, "starting transaction");
904 return;
905 }
906
907 /* If we can't advertise it is OK. */
908 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
909
910 xen_blkbk_discard(xbt, be);
911
912 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
913
914 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u",
915 be->blkif->vbd.feature_gnt_persistent);
916 if (err) {
917 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
918 dev->nodename);
919 goto abort;
920 }
921
922 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
923 (unsigned long long)vbd_sz(&be->blkif->vbd));
924 if (err) {
925 xenbus_dev_fatal(dev, err, "writing %s/sectors",
926 dev->nodename);
927 goto abort;
928 }
929
930 /* FIXME: use a typename instead */
931 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
932 be->blkif->vbd.type |
933 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
934 if (err) {
935 xenbus_dev_fatal(dev, err, "writing %s/info",
936 dev->nodename);
937 goto abort;
938 }
939 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
940 (unsigned long)
941 bdev_logical_block_size(be->blkif->vbd.bdev));
942 if (err) {
943 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
944 dev->nodename);
945 goto abort;
946 }
947 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
948 bdev_physical_block_size(be->blkif->vbd.bdev));
949 if (err)
950 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
951 dev->nodename);
952
953 err = xenbus_transaction_end(xbt, 0);
954 if (err == -EAGAIN)
955 goto again;
956 if (err)
957 xenbus_dev_fatal(dev, err, "ending transaction");
958
959 err = xenbus_switch_state(dev, XenbusStateConnected);
960 if (err)
961 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
962 dev->nodename);
963
964 return;
965 abort:
966 xenbus_transaction_end(xbt, 1);
967 }
968
969 /*
970 * Each ring may have multi pages, depends on "ring-page-order".
971 */
read_per_ring_refs(struct xen_blkif_ring * ring,const char * dir)972 static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
973 {
974 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
975 struct pending_req *req, *n;
976 int err, i, j;
977 struct xen_blkif *blkif = ring->blkif;
978 struct xenbus_device *dev = blkif->be->dev;
979 unsigned int nr_grefs, evtchn;
980
981 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
982 &evtchn);
983 if (err != 1) {
984 err = -EINVAL;
985 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
986 return err;
987 }
988
989 nr_grefs = blkif->nr_ring_pages;
990
991 if (unlikely(!nr_grefs)) {
992 WARN_ON(true);
993 return -EINVAL;
994 }
995
996 for (i = 0; i < nr_grefs; i++) {
997 char ring_ref_name[RINGREF_NAME_LEN];
998
999 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1000 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
1001 "%u", &ring_ref[i]);
1002
1003 if (err != 1) {
1004 if (nr_grefs == 1)
1005 break;
1006
1007 err = -EINVAL;
1008 xenbus_dev_fatal(dev, err, "reading %s/%s",
1009 dir, ring_ref_name);
1010 return err;
1011 }
1012 }
1013
1014 if (err != 1) {
1015 WARN_ON(nr_grefs != 1);
1016
1017 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u",
1018 &ring_ref[0]);
1019 if (err != 1) {
1020 err = -EINVAL;
1021 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
1022 return err;
1023 }
1024 }
1025
1026 err = -ENOMEM;
1027 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
1028 req = kzalloc(sizeof(*req), GFP_KERNEL);
1029 if (!req)
1030 goto fail;
1031 list_add_tail(&req->free_list, &ring->pending_free);
1032 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
1033 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
1034 if (!req->segments[j])
1035 goto fail;
1036 }
1037 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1038 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
1039 GFP_KERNEL);
1040 if (!req->indirect_pages[j])
1041 goto fail;
1042 }
1043 }
1044
1045 /* Map the shared frame, irq etc. */
1046 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
1047 if (err) {
1048 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
1049 goto fail;
1050 }
1051
1052 return 0;
1053
1054 fail:
1055 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
1056 list_del(&req->free_list);
1057 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
1058 if (!req->segments[j])
1059 break;
1060 kfree(req->segments[j]);
1061 }
1062 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1063 if (!req->indirect_pages[j])
1064 break;
1065 kfree(req->indirect_pages[j]);
1066 }
1067 kfree(req);
1068 }
1069 return err;
1070 }
1071
connect_ring(struct backend_info * be)1072 static int connect_ring(struct backend_info *be)
1073 {
1074 struct xenbus_device *dev = be->dev;
1075 struct xen_blkif *blkif = be->blkif;
1076 char protocol[64] = "";
1077 int err, i;
1078 char *xspath;
1079 size_t xspathsize;
1080 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
1081 unsigned int requested_num_queues = 0;
1082 unsigned int ring_page_order;
1083
1084 pr_debug("%s %s\n", __func__, dev->otherend);
1085
1086 blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
1087 err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol",
1088 "%63s", protocol);
1089 if (err <= 0)
1090 strcpy(protocol, "unspecified, assuming default");
1091 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
1092 blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
1093 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
1094 blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
1095 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
1096 blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
1097 else {
1098 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
1099 return -ENOSYS;
1100 }
1101 if (blkif->vbd.feature_gnt_persistent)
1102 blkif->vbd.feature_gnt_persistent =
1103 xenbus_read_unsigned(dev->otherend,
1104 "feature-persistent", 0);
1105
1106 blkif->vbd.overflow_max_grants = 0;
1107
1108 /*
1109 * Read the number of hardware queues from frontend.
1110 */
1111 requested_num_queues = xenbus_read_unsigned(dev->otherend,
1112 "multi-queue-num-queues",
1113 1);
1114 if (requested_num_queues > xenblk_max_queues
1115 || requested_num_queues == 0) {
1116 /* Buggy or malicious guest. */
1117 xenbus_dev_fatal(dev, err,
1118 "guest requested %u queues, exceeding the maximum of %u.",
1119 requested_num_queues, xenblk_max_queues);
1120 return -ENOSYS;
1121 }
1122 blkif->nr_rings = requested_num_queues;
1123 if (xen_blkif_alloc_rings(blkif))
1124 return -ENOMEM;
1125
1126 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
1127 blkif->nr_rings, blkif->blk_protocol, protocol,
1128 blkif->vbd.feature_gnt_persistent ? "persistent grants" : "");
1129
1130 ring_page_order = xenbus_read_unsigned(dev->otherend,
1131 "ring-page-order", 0);
1132
1133 if (ring_page_order > xen_blkif_max_ring_order) {
1134 err = -EINVAL;
1135 xenbus_dev_fatal(dev, err,
1136 "requested ring page order %d exceed max:%d",
1137 ring_page_order,
1138 xen_blkif_max_ring_order);
1139 return err;
1140 }
1141
1142 blkif->nr_ring_pages = 1 << ring_page_order;
1143
1144 if (blkif->nr_rings == 1)
1145 return read_per_ring_refs(&blkif->rings[0], dev->otherend);
1146 else {
1147 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1148 xspath = kmalloc(xspathsize, GFP_KERNEL);
1149 if (!xspath) {
1150 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
1151 return -ENOMEM;
1152 }
1153
1154 for (i = 0; i < blkif->nr_rings; i++) {
1155 memset(xspath, 0, xspathsize);
1156 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
1157 err = read_per_ring_refs(&blkif->rings[i], xspath);
1158 if (err) {
1159 kfree(xspath);
1160 return err;
1161 }
1162 }
1163 kfree(xspath);
1164 }
1165 return 0;
1166 }
1167
1168 static const struct xenbus_device_id xen_blkbk_ids[] = {
1169 { "vbd" },
1170 { "" }
1171 };
1172
1173 static struct xenbus_driver xen_blkbk_driver = {
1174 .ids = xen_blkbk_ids,
1175 .probe = xen_blkbk_probe,
1176 .remove = xen_blkbk_remove,
1177 .otherend_changed = frontend_changed,
1178 .allow_rebind = true,
1179 .reclaim_memory = reclaim_memory,
1180 };
1181
xen_blkif_xenbus_init(void)1182 int xen_blkif_xenbus_init(void)
1183 {
1184 return xenbus_register_backend(&xen_blkbk_driver);
1185 }
1186
xen_blkif_xenbus_fini(void)1187 void xen_blkif_xenbus_fini(void)
1188 {
1189 xenbus_unregister_driver(&xen_blkbk_driver);
1190 }
1191