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