blob: 1d1f8665796793fe92ebc262fbd41f111322f4e4 [file] [log] [blame]
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001/* Xenbus code for blkif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040015*/
16
Tao Chen77387b82015-04-01 15:04:22 +000017#define pr_fmt(fmt) "xen-blkback: " fmt
18
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040019#include <stdarg.h>
20#include <linux/module.h>
21#include <linux/kthread.h>
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -040022#include <xen/events.h>
23#include <xen/grant_table.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040024#include "common.h"
25
Konrad Rzeszutek Wilkfa3184b2016-02-03 16:40:05 -050026/* On the XenBus the max length of 'ring-ref%u'. */
Bob Liu86839c52015-06-03 13:40:03 +080027#define RINGREF_NAME_LEN (20)
Tao Chen13755902015-03-27 13:15:54 +000028
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040029struct backend_info {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040030 struct xenbus_device *dev;
Konrad Rzeszutek Wilk51854322011-05-12 18:02:28 -040031 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040032 struct xenbus_watch backend_watch;
33 unsigned major;
34 unsigned minor;
35 char *mode;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040036};
37
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040038static struct kmem_cache *xen_blkif_cachep;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040039static void connect(struct backend_info *);
40static int connect_ring(struct backend_info *);
41static void backend_changed(struct xenbus_watch *, const char **,
42 unsigned int);
Valentin Priescu814d04e2014-05-20 22:28:50 +020043static void xen_blkif_free(struct xen_blkif *blkif);
44static void xen_vbd_free(struct xen_vbd *vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040045
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040046struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070047{
48 return be->dev;
49}
50
Valentin Priescu814d04e2014-05-20 22:28:50 +020051/*
52 * The last request could free the device from softirq context and
53 * xen_blkif_free() can sleep.
54 */
55static void xen_blkif_deferred_free(struct work_struct *work)
56{
57 struct xen_blkif *blkif;
58
59 blkif = container_of(work, struct xen_blkif, free_work);
60 xen_blkif_free(blkif);
61}
62
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040063static int blkback_name(struct xen_blkif *blkif, char *buf)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040064{
65 char *devpath, *devname;
66 struct xenbus_device *dev = blkif->be->dev;
67
68 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
69 if (IS_ERR(devpath))
70 return PTR_ERR(devpath);
71
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040072 devname = strstr(devpath, "/dev/");
73 if (devname != NULL)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040074 devname += strlen("/dev/");
75 else
76 devname = devpath;
77
Konrad Rzeszutek Wilkfa3184b2016-02-03 16:40:05 -050078 snprintf(buf, TASK_COMM_LEN, "%d.%s", blkif->domid, devname);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040079 kfree(devpath);
80
81 return 0;
82}
83
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040084static void xen_update_blkif_status(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040085{
86 int err;
Konrad Rzeszutek Wilkfa3184b2016-02-03 16:40:05 -050087 char name[TASK_COMM_LEN];
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -050088 struct xen_blkif_ring *ring;
89 int i;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040090
91 /* Not ready to connect? */
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -050092 if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040093 return;
94
95 /* Already connected? */
96 if (blkif->be->dev->state == XenbusStateConnected)
97 return;
98
99 /* Attempt to connect: exit if we fail to. */
100 connect(blkif->be);
101 if (blkif->be->dev->state != XenbusStateConnected)
102 return;
103
104 err = blkback_name(blkif, name);
105 if (err) {
106 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
107 return;
108 }
109
Chris Lalancettecbf46292010-07-21 12:41:45 -0700110 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
111 if (err) {
112 xenbus_dev_error(blkif->be->dev, err, "block flush");
113 return;
114 }
115 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
116
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500117 for (i = 0; i < blkif->nr_rings; i++) {
118 ring = &blkif->rings[i];
119 ring->xenblkd = kthread_run(xen_blkif_schedule, ring, "%s-%d", name, i);
120 if (IS_ERR(ring->xenblkd)) {
121 err = PTR_ERR(ring->xenblkd);
122 ring->xenblkd = NULL;
123 xenbus_dev_fatal(blkif->be->dev, err,
124 "start %s-%d xenblkd", name, i);
125 goto out;
126 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400127 }
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500128 return;
129
130out:
131 while (--i >= 0) {
132 ring = &blkif->rings[i];
133 kthread_stop(ring->xenblkd);
134 }
135 return;
136}
137
138static int xen_blkif_alloc_rings(struct xen_blkif *blkif)
139{
140 unsigned int r;
141
142 blkif->rings = kzalloc(blkif->nr_rings * sizeof(struct xen_blkif_ring), GFP_KERNEL);
143 if (!blkif->rings)
144 return -ENOMEM;
145
146 for (r = 0; r < blkif->nr_rings; r++) {
147 struct xen_blkif_ring *ring = &blkif->rings[r];
148
149 spin_lock_init(&ring->blk_ring_lock);
150 init_waitqueue_head(&ring->wq);
151 INIT_LIST_HEAD(&ring->pending_free);
Bob Liud4bf0062015-11-14 11:12:19 +0800152 INIT_LIST_HEAD(&ring->persistent_purge_list);
153 INIT_WORK(&ring->persistent_purge_work, xen_blkbk_unmap_purged_grants);
154 spin_lock_init(&ring->free_pages_lock);
155 INIT_LIST_HEAD(&ring->free_pages);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500156
157 spin_lock_init(&ring->pending_free_lock);
158 init_waitqueue_head(&ring->pending_free_wq);
159 init_waitqueue_head(&ring->shutdown_wq);
160 ring->blkif = blkif;
Bob Liudb6fbc12015-12-09 07:44:02 +0800161 ring->st_print = jiffies;
Juergen Grosse5c49c12017-05-18 17:28:47 +0200162 ring->active = true;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500163 }
164
165 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400166}
167
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400168static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400169{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400170 struct xen_blkif *blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400171
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200172 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400173
Wei Yongjun654dbef2012-08-27 12:28:57 +0800174 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400175 if (!blkif)
176 return ERR_PTR(-ENOMEM);
177
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400178 blkif->domid = domid;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400179 atomic_set(&blkif->refcnt, 1);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400180 init_completion(&blkif->drain_complete);
Paul Durrante0e0a952019-12-10 14:53:05 +0000181
182 /*
183 * Because freeing back to the cache may be deferred, it is not
184 * safe to unload the module (and hence destroy the cache) until
185 * this has completed. To prevent premature unloading, take an
186 * extra module reference here and release only when the object
187 * has been freed back to the cache.
188 */
189 __module_get(THIS_MODULE);
Bob Liu59795702015-11-14 11:12:15 +0800190 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400191
192 return blkif;
193}
194
Bob Liu59795702015-11-14 11:12:15 +0800195static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
Bob Liu86839c52015-06-03 13:40:03 +0800196 unsigned int nr_grefs, unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400197{
198 int err;
Bob Liu59795702015-11-14 11:12:15 +0800199 struct xen_blkif *blkif = ring->blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400200
201 /* Already connected through? */
Bob Liu59795702015-11-14 11:12:15 +0800202 if (ring->irq)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400203 return 0;
204
Bob Liu86839c52015-06-03 13:40:03 +0800205 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
Bob Liu59795702015-11-14 11:12:15 +0800206 &ring->blk_ring);
David Vrabel2d073842011-09-29 16:53:30 +0100207 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400208 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400209
210 switch (blkif->blk_protocol) {
211 case BLKIF_PROTOCOL_NATIVE:
212 {
213 struct blkif_sring *sring;
Bob Liu59795702015-11-14 11:12:15 +0800214 sring = (struct blkif_sring *)ring->blk_ring;
215 BACK_RING_INIT(&ring->blk_rings.native, sring,
Julien Grall67de5df2015-05-05 16:25:56 +0100216 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400217 break;
218 }
219 case BLKIF_PROTOCOL_X86_32:
220 {
221 struct blkif_x86_32_sring *sring_x86_32;
Bob Liu59795702015-11-14 11:12:15 +0800222 sring_x86_32 = (struct blkif_x86_32_sring *)ring->blk_ring;
223 BACK_RING_INIT(&ring->blk_rings.x86_32, sring_x86_32,
Julien Grall67de5df2015-05-05 16:25:56 +0100224 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400225 break;
226 }
227 case BLKIF_PROTOCOL_X86_64:
228 {
229 struct blkif_x86_64_sring *sring_x86_64;
Bob Liu59795702015-11-14 11:12:15 +0800230 sring_x86_64 = (struct blkif_x86_64_sring *)ring->blk_ring;
231 BACK_RING_INIT(&ring->blk_rings.x86_64, sring_x86_64,
Julien Grall67de5df2015-05-05 16:25:56 +0100232 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400233 break;
234 }
235 default:
236 BUG();
237 }
238
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400239 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
240 xen_blkif_be_int, 0,
Bob Liu59795702015-11-14 11:12:15 +0800241 "blkif-backend", ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400242 if (err < 0) {
Bob Liu59795702015-11-14 11:12:15 +0800243 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
244 ring->blk_rings.common.sring = NULL;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400245 return err;
246 }
Bob Liu59795702015-11-14 11:12:15 +0800247 ring->irq = err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400248
249 return 0;
250}
251
Valentin Priescu814d04e2014-05-20 22:28:50 +0200252static int xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400253{
Roger Pau Monnef929d422015-09-04 12:08:07 +0200254 struct pending_req *req, *n;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500255 unsigned int j, r;
Roger Pau Monnef929d422015-09-04 12:08:07 +0200256
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500257 for (r = 0; r < blkif->nr_rings; r++) {
258 struct xen_blkif_ring *ring = &blkif->rings[r];
259 unsigned int i = 0;
260
Juergen Grosse5c49c12017-05-18 17:28:47 +0200261 if (!ring->active)
262 continue;
263
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500264 if (ring->xenblkd) {
265 kthread_stop(ring->xenblkd);
266 wake_up(&ring->shutdown_wq);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500267 }
268
269 /* The above kthread_stop() guarantees that at this point we
270 * don't have any discard_io or other_io requests. So, checking
271 * for inflight IO is enough.
272 */
273 if (atomic_read(&ring->inflight) > 0)
274 return -EBUSY;
275
276 if (ring->irq) {
277 unbind_from_irqhandler(ring->irq, ring);
278 ring->irq = 0;
279 }
280
281 if (ring->blk_rings.common.sring) {
282 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
283 ring->blk_rings.common.sring = NULL;
284 }
285
286 /* Remove all persistent grants and the cache of ballooned pages. */
287 xen_blkbk_free_caches(ring);
288
289 /* Check that there is no request in use */
290 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
291 list_del(&req->free_list);
292
293 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
294 kfree(req->segments[j]);
295
296 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
297 kfree(req->indirect_pages[j]);
298
299 kfree(req);
300 i++;
301 }
302
Bob Liud4bf0062015-11-14 11:12:19 +0800303 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
304 BUG_ON(!list_empty(&ring->persistent_purge_list));
305 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
306 BUG_ON(!list_empty(&ring->free_pages));
307 BUG_ON(ring->free_pages_num != 0);
308 BUG_ON(ring->persistent_gnt_c != 0);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500309 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
Juergen Grosse5c49c12017-05-18 17:28:47 +0200310 ring->active = false;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400311 }
Roger Pau Monnef929d422015-09-04 12:08:07 +0200312 blkif->nr_ring_pages = 0;
Bob Liu93bb2772015-12-10 09:16:48 +0800313 /*
314 * blkif->rings was allocated in connect_ring, so we should free it in
315 * here.
316 */
317 kfree(blkif->rings);
318 blkif->rings = NULL;
319 blkif->nr_rings = 0;
Roger Pau Monnef929d422015-09-04 12:08:07 +0200320
321 return 0;
322}
323
324static void xen_blkif_free(struct xen_blkif *blkif)
325{
326
Juergen Grossafaee3e2017-05-18 17:28:48 +0200327 WARN_ON(xen_blkif_disconnect(blkif));
Roger Pau Monnef929d422015-09-04 12:08:07 +0200328 xen_vbd_free(&blkif->vbd);
Juergen Grossafaee3e2017-05-18 17:28:48 +0200329 kfree(blkif->be->mode);
330 kfree(blkif->be);
Roger Pau Monnef929d422015-09-04 12:08:07 +0200331
332 /* Make sure everything is drained before shutting down */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400333 kmem_cache_free(xen_blkif_cachep, blkif);
Paul Durrante0e0a952019-12-10 14:53:05 +0000334 module_put(THIS_MODULE);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400335}
336
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400337int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400338{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400339 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400340 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400341 0, 0, NULL);
342 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400343 return -ENOMEM;
344
345 return 0;
346}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400347
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400348/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400349 * sysfs interface for VBD I/O requests
350 */
351
Bob Liudb6fbc12015-12-09 07:44:02 +0800352#define VBD_SHOW_ALLRING(name, format) \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400353 static ssize_t show_##name(struct device *_dev, \
354 struct device_attribute *attr, \
355 char *buf) \
356 { \
357 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800358 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Bob Liudb6fbc12015-12-09 07:44:02 +0800359 struct xen_blkif *blkif = be->blkif; \
360 unsigned int i; \
361 unsigned long long result = 0; \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400362 \
Bob Liudb6fbc12015-12-09 07:44:02 +0800363 if (!blkif->rings) \
364 goto out; \
365 \
366 for (i = 0; i < blkif->nr_rings; i++) { \
367 struct xen_blkif_ring *ring = &blkif->rings[i]; \
368 \
369 result += ring->st_##name; \
370 } \
371 \
372out: \
373 return sprintf(buf, format, result); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400374 } \
375 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
376
Bob Liudb6fbc12015-12-09 07:44:02 +0800377VBD_SHOW_ALLRING(oo_req, "%llu\n");
378VBD_SHOW_ALLRING(rd_req, "%llu\n");
379VBD_SHOW_ALLRING(wr_req, "%llu\n");
380VBD_SHOW_ALLRING(f_req, "%llu\n");
381VBD_SHOW_ALLRING(ds_req, "%llu\n");
382VBD_SHOW_ALLRING(rd_sect, "%llu\n");
383VBD_SHOW_ALLRING(wr_sect, "%llu\n");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400384
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400385static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400386 &dev_attr_oo_req.attr,
387 &dev_attr_rd_req.attr,
388 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400389 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800390 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400391 &dev_attr_rd_sect.attr,
392 &dev_attr_wr_sect.attr,
393 NULL
394};
395
Jan Beulich53043942016-07-07 01:38:58 -0600396static const struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400397 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400398 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400399};
400
Bob Liudb6fbc12015-12-09 07:44:02 +0800401#define VBD_SHOW(name, format, args...) \
402 static ssize_t show_##name(struct device *_dev, \
403 struct device_attribute *attr, \
404 char *buf) \
405 { \
406 struct xenbus_device *dev = to_xenbus_device(_dev); \
407 struct backend_info *be = dev_get_drvdata(&dev->dev); \
408 \
409 return sprintf(buf, format, ##args); \
410 } \
411 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
412
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400413VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
414VBD_SHOW(mode, "%s\n", be->mode);
415
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400416static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400417{
418 int error;
419
420 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400421 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400422 goto fail1;
423
424 error = device_create_file(&dev->dev, &dev_attr_mode);
425 if (error)
426 goto fail2;
427
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400428 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400429 if (error)
430 goto fail3;
431
432 return 0;
433
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400434fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400435fail2: device_remove_file(&dev->dev, &dev_attr_mode);
436fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
437 return error;
438}
439
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400440static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400441{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400442 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400443 device_remove_file(&dev->dev, &dev_attr_mode);
444 device_remove_file(&dev->dev, &dev_attr_physical_device);
445}
446
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400447
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400448static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400449{
450 if (vbd->bdev)
451 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
452 vbd->bdev = NULL;
453}
454
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400455static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
456 unsigned major, unsigned minor, int readonly,
457 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400458{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400459 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400460 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400461 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400462
463 vbd = &blkif->vbd;
464 vbd->handle = handle;
465 vbd->readonly = readonly;
466 vbd->type = 0;
467
468 vbd->pdevice = MKDEV(major, minor);
469
470 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
471 FMODE_READ : FMODE_WRITE, NULL);
472
473 if (IS_ERR(bdev)) {
Tao Chen77387b82015-04-01 15:04:22 +0000474 pr_warn("xen_vbd_create: device %08x could not be opened\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400475 vbd->pdevice);
476 return -ENOENT;
477 }
478
479 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400480 if (vbd->bdev->bd_disk == NULL) {
Tao Chen77387b82015-04-01 15:04:22 +0000481 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400482 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400483 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400484 return -ENOENT;
485 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200486 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400487
488 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
489 vbd->type |= VDISK_CDROM;
490 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
491 vbd->type |= VDISK_REMOVABLE;
492
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400493 q = bdev_get_queue(bdev);
Jens Axboec888a8f2016-04-13 13:33:19 -0600494 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags))
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400495 vbd->flush_support = true;
496
Christoph Hellwig288dab82016-06-09 16:00:36 +0200497 if (q && blk_queue_secure_erase(q))
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400498 vbd->discard_secure = true;
499
Tao Chen77387b82015-04-01 15:04:22 +0000500 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400501 handle, blkif->domid);
502 return 0;
503}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400504static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400505{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800506 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400507
Tao Chen77387b82015-04-01 15:04:22 +0000508 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400509
510 if (be->major || be->minor)
511 xenvbd_sysfs_delif(dev);
512
513 if (be->backend_watch.node) {
514 unregister_xenbus_watch(&be->backend_watch);
515 kfree(be->backend_watch.node);
516 be->backend_watch.node = NULL;
517 }
518
Valentin Priescu814d04e2014-05-20 22:28:50 +0200519 dev_set_drvdata(&dev->dev, NULL);
520
Bob Liu93bb2772015-12-10 09:16:48 +0800521 if (be->blkif)
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400522 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400523
Bob Liu93bb2772015-12-10 09:16:48 +0800524 /* Put the reference we set in xen_blkif_alloc(). */
525 xen_blkif_put(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400526 return 0;
527}
528
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400529int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
530 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400531{
532 struct xenbus_device *dev = be->dev;
533 int err;
534
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400535 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400536 "%d", state);
537 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400538 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400539
540 return err;
541}
542
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400543static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800544{
545 struct xenbus_device *dev = be->dev;
546 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800547 int err;
Olaf Heringc926b702014-05-21 16:32:42 +0200548 int state = 0, discard_enable;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400549 struct block_device *bdev = be->blkif->vbd.bdev;
550 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800551
Olaf Heringc926b702014-05-21 16:32:42 +0200552 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d",
553 &discard_enable);
554 if (err == 1 && !discard_enable)
555 return;
556
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400557 if (blk_queue_discard(q)) {
558 err = xenbus_printf(xbt, dev->nodename,
559 "discard-granularity", "%u",
560 q->limits.discard_granularity);
561 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400562 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
563 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800564 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400565 err = xenbus_printf(xbt, dev->nodename,
566 "discard-alignment", "%u",
567 q->limits.discard_alignment);
568 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400569 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
570 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400571 }
572 state = 1;
573 /* Optional. */
574 err = xenbus_printf(xbt, dev->nodename,
575 "discard-secure", "%d",
576 blkif->vbd.discard_secure);
577 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400578 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400579 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800580 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800581 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800582 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
583 "%d", state);
584 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400585 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800586}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400587int xen_blkbk_barrier(struct xenbus_transaction xbt,
588 struct backend_info *be, int state)
589{
590 struct xenbus_device *dev = be->dev;
591 int err;
592
593 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
594 "%d", state);
595 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400596 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400597
598 return err;
599}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800600
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400601/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400602 * Entry point to this code when a new device is created. Allocate the basic
603 * structures, and watch the store waiting for the hotplug scripts to tell us
604 * the device's physical major and minor numbers. Switch to InitWait.
605 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400606static int xen_blkbk_probe(struct xenbus_device *dev,
607 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400608{
609 int err;
610 struct backend_info *be = kzalloc(sizeof(struct backend_info),
611 GFP_KERNEL);
Tao Chen77387b82015-04-01 15:04:22 +0000612
613 /* match the pr_debug in xen_blkbk_remove */
614 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
615
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400616 if (!be) {
617 xenbus_dev_fatal(dev, -ENOMEM,
618 "allocating backend structure");
619 return -ENOMEM;
620 }
621 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800622 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400623
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400624 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400625 if (IS_ERR(be->blkif)) {
626 err = PTR_ERR(be->blkif);
627 be->blkif = NULL;
628 xenbus_dev_fatal(dev, err, "creating block interface");
629 goto fail;
630 }
631
Jan Beulich5a705842016-02-10 04:18:10 -0700632 err = xenbus_printf(XBT_NIL, dev->nodename,
633 "feature-max-indirect-segments", "%u",
634 MAX_INDIRECT_SEGMENTS);
635 if (err)
636 dev_warn(&dev->dev,
637 "writing %s/feature-max-indirect-segments (%d)",
638 dev->nodename, err);
639
Bob Liud62d8602015-11-14 11:12:17 +0800640 /* Multi-queue: advertise how many queues are supported by us.*/
641 err = xenbus_printf(XBT_NIL, dev->nodename,
642 "multi-queue-max-queues", "%u", xenblk_max_queues);
643 if (err)
644 pr_warn("Error writing multi-queue-max-queues\n");
645
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400646 /* setup back pointer */
647 be->blkif->be = be;
648
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800649 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
650 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400651 if (err)
652 goto fail;
653
Bob Liu86839c52015-06-03 13:40:03 +0800654 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
655 xen_blkif_max_ring_order);
656 if (err)
657 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
658
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400659 err = xenbus_switch_state(dev, XenbusStateInitWait);
660 if (err)
661 goto fail;
662
663 return 0;
664
665fail:
Tao Chen77387b82015-04-01 15:04:22 +0000666 pr_warn("%s failed\n", __func__);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400667 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400668 return err;
669}
670
671
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400672/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400673 * Callback received when the hotplug scripts have placed the physical-device
674 * node. Read it and the mode node, and create a vbd. If the frontend is
675 * ready, connect.
676 */
677static void backend_changed(struct xenbus_watch *watch,
678 const char **vec, unsigned int len)
679{
680 int err;
681 unsigned major;
682 unsigned minor;
683 struct backend_info *be
684 = container_of(watch, struct backend_info, backend_watch);
685 struct xenbus_device *dev = be->dev;
686 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000687 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400688 char *device_type;
689
Tao Chen77387b82015-04-01 15:04:22 +0000690 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400691
692 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
693 &major, &minor);
694 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400695 /*
696 * Since this watch will fire once immediately after it is
697 * registered, we expect this. Ignore it, and wait for the
698 * hotplug scripts.
699 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400700 return;
701 }
702 if (err != 2) {
703 xenbus_dev_fatal(dev, err, "reading physical-device");
704 return;
705 }
706
Jan Beulich9d092602012-12-20 10:31:11 +0000707 if (be->major | be->minor) {
708 if (be->major != major || be->minor != minor)
Tao Chen77387b82015-04-01 15:04:22 +0000709 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
Jan Beulich9d092602012-12-20 10:31:11 +0000710 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400711 return;
712 }
713
714 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
715 if (IS_ERR(be->mode)) {
716 err = PTR_ERR(be->mode);
717 be->mode = NULL;
718 xenbus_dev_fatal(dev, err, "reading mode");
719 return;
720 }
721
722 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
723 if (!IS_ERR(device_type)) {
724 cdrom = strcmp(device_type, "cdrom") == 0;
725 kfree(device_type);
726 }
727
Jan Beulich9d092602012-12-20 10:31:11 +0000728 /* Front end dir is a number, which is used as the handle. */
Jingoo Hanbb8e0e82013-09-11 14:20:07 -0700729 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
Jan Beulichaea305e12016-07-07 01:38:13 -0600730 if (err) {
731 kfree(be->mode);
732 be->mode = NULL;
Jan Beulich9d092602012-12-20 10:31:11 +0000733 return;
Jan Beulichaea305e12016-07-07 01:38:13 -0600734 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400735
Jan Beulich9d092602012-12-20 10:31:11 +0000736 be->major = major;
737 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400738
Jan Beulich9d092602012-12-20 10:31:11 +0000739 err = xen_vbd_create(be->blkif, handle, major, minor,
740 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400741
Jan Beulich9d092602012-12-20 10:31:11 +0000742 if (err)
743 xenbus_dev_fatal(dev, err, "creating vbd structure");
744 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400745 err = xenvbd_sysfs_addif(dev);
746 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400747 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400748 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400749 }
Jan Beulich9d092602012-12-20 10:31:11 +0000750 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400751
Jan Beulich9d092602012-12-20 10:31:11 +0000752 if (err) {
753 kfree(be->mode);
754 be->mode = NULL;
755 be->major = 0;
756 be->minor = 0;
757 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400758 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400759 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400760 }
761}
762
763
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400764/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400765 * Callback received when the frontend's state changes.
766 */
767static void frontend_changed(struct xenbus_device *dev,
768 enum xenbus_state frontend_state)
769{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800770 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400771 int err;
772
Tao Chen77387b82015-04-01 15:04:22 +0000773 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400774
775 switch (frontend_state) {
776 case XenbusStateInitialising:
777 if (dev->state == XenbusStateClosed) {
Tao Chen77387b82015-04-01 15:04:22 +0000778 pr_info("%s: prepare for reconnect\n", dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400779 xenbus_switch_state(dev, XenbusStateInitWait);
780 }
781 break;
782
783 case XenbusStateInitialised:
784 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400785 /*
786 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800787 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400788 * of frontend_state.
789 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400790 if (dev->state == XenbusStateConnected)
791 break;
792
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400793 /*
794 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800795 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800796 */
Valentin Priescu814d04e2014-05-20 22:28:50 +0200797 err = xen_blkif_disconnect(be->blkif);
798 if (err) {
799 xenbus_dev_fatal(dev, err, "pending I/O");
800 break;
801 }
Keir Fraser313d7b02010-11-24 22:08:20 -0800802
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400803 err = connect_ring(be);
Konrad Rzeszutek Wilk2d0382f2015-11-25 13:20:14 -0500804 if (err) {
805 /*
806 * Clean up so that memory resources can be used by
807 * other devices. connect_ring reported already error.
808 */
809 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400810 break;
Konrad Rzeszutek Wilk2d0382f2015-11-25 13:20:14 -0500811 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400812 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400813 break;
814
815 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400816 xenbus_switch_state(dev, XenbusStateClosing);
817 break;
818
819 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800820 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400821 xenbus_switch_state(dev, XenbusStateClosed);
822 if (xenbus_dev_is_online(dev))
823 break;
824 /* fall through if not online */
825 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800826 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400827 device_unregister(&dev->dev);
828 break;
829
830 default:
831 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
832 frontend_state);
833 break;
834 }
835}
836
837
838/* ** Connection ** */
839
840
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400841/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400842 * Write the physical details regarding the block device to the store, and
843 * switch to Connected state.
844 */
845static void connect(struct backend_info *be)
846{
847 struct xenbus_transaction xbt;
848 int err;
849 struct xenbus_device *dev = be->dev;
850
Tao Chen77387b82015-04-01 15:04:22 +0000851 pr_debug("%s %s\n", __func__, dev->otherend);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400852
853 /* Supply the information about the device the frontend needs */
854again:
855 err = xenbus_transaction_start(&xbt);
856 if (err) {
857 xenbus_dev_fatal(dev, err, "starting transaction");
858 return;
859 }
860
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400861 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400862 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
863
864 xen_blkbk_discard(xbt, be);
865
866 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400867
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200868 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
869 if (err) {
870 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
871 dev->nodename);
872 goto abort;
873 }
874
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400875 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400876 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400877 if (err) {
878 xenbus_dev_fatal(dev, err, "writing %s/sectors",
879 dev->nodename);
880 goto abort;
881 }
882
883 /* FIXME: use a typename instead */
884 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400885 be->blkif->vbd.type |
886 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400887 if (err) {
888 xenbus_dev_fatal(dev, err, "writing %s/info",
889 dev->nodename);
890 goto abort;
891 }
892 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400893 (unsigned long)
894 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400895 if (err) {
896 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
897 dev->nodename);
898 goto abort;
899 }
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200900 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
901 bdev_physical_block_size(be->blkif->vbd.bdev));
902 if (err)
903 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
904 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400905
906 err = xenbus_transaction_end(xbt, 0);
907 if (err == -EAGAIN)
908 goto again;
909 if (err)
910 xenbus_dev_fatal(dev, err, "ending transaction");
911
912 err = xenbus_switch_state(dev, XenbusStateConnected);
913 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700914 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400915 dev->nodename);
916
917 return;
918 abort:
919 xenbus_transaction_end(xbt, 1);
920}
921
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500922/*
923 * Each ring may have multi pages, depends on "ring-page-order".
924 */
925static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400926{
Julien Grall9cce2912015-10-13 17:50:11 +0100927 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
Bob Liu69b91ed2015-06-03 13:40:01 +0800928 struct pending_req *req, *n;
929 int err, i, j;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500930 struct xen_blkif *blkif = ring->blkif;
931 struct xenbus_device *dev = blkif->be->dev;
932 unsigned int ring_page_order, nr_grefs, evtchn;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400933
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500934 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
Bob Liu86839c52015-06-03 13:40:03 +0800935 &evtchn);
936 if (err != 1) {
937 err = -EINVAL;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500938 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400939 return err;
940 }
Bob Liu86839c52015-06-03 13:40:03 +0800941
942 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
943 &ring_page_order);
944 if (err != 1) {
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500945 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", &ring_ref[0]);
Bob Liu86839c52015-06-03 13:40:03 +0800946 if (err != 1) {
947 err = -EINVAL;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500948 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
Bob Liu86839c52015-06-03 13:40:03 +0800949 return err;
950 }
951 nr_grefs = 1;
Bob Liu86839c52015-06-03 13:40:03 +0800952 } else {
953 unsigned int i;
954
955 if (ring_page_order > xen_blkif_max_ring_order) {
956 err = -EINVAL;
957 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d",
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500958 dir, ring_page_order,
Bob Liu86839c52015-06-03 13:40:03 +0800959 xen_blkif_max_ring_order);
960 return err;
961 }
962
963 nr_grefs = 1 << ring_page_order;
964 for (i = 0; i < nr_grefs; i++) {
965 char ring_ref_name[RINGREF_NAME_LEN];
966
967 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500968 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
Bob Liu86839c52015-06-03 13:40:03 +0800969 "%u", &ring_ref[i]);
970 if (err != 1) {
971 err = -EINVAL;
972 xenbus_dev_fatal(dev, err, "reading %s/%s",
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500973 dir, ring_ref_name);
Bob Liu86839c52015-06-03 13:40:03 +0800974 return err;
975 }
Bob Liu86839c52015-06-03 13:40:03 +0800976 }
977 }
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500978 blkif->nr_ring_pages = nr_grefs;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400979
Wenwen Wangf5679e52019-08-11 12:23:22 -0500980 err = -ENOMEM;
Bob Liu86839c52015-06-03 13:40:03 +0800981 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
Bob Liu69b91ed2015-06-03 13:40:01 +0800982 req = kzalloc(sizeof(*req), GFP_KERNEL);
983 if (!req)
984 goto fail;
Bob Liu59795702015-11-14 11:12:15 +0800985 list_add_tail(&req->free_list, &ring->pending_free);
Bob Liu69b91ed2015-06-03 13:40:01 +0800986 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
987 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
988 if (!req->segments[j])
989 goto fail;
990 }
991 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
992 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
993 GFP_KERNEL);
994 if (!req->indirect_pages[j])
995 goto fail;
996 }
997 }
998
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400999 /* Map the shared frame, irq etc. */
Bob Liu59795702015-11-14 11:12:15 +08001000 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001001 if (err) {
Bob Liu86839c52015-06-03 13:40:03 +08001002 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
Wenwen Wangf5679e52019-08-11 12:23:22 -05001003 goto fail;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001004 }
1005
1006 return 0;
Bob Liu69b91ed2015-06-03 13:40:01 +08001007
1008fail:
Bob Liu59795702015-11-14 11:12:15 +08001009 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
Bob Liu69b91ed2015-06-03 13:40:01 +08001010 list_del(&req->free_list);
1011 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
1012 if (!req->segments[j])
1013 break;
1014 kfree(req->segments[j]);
1015 }
1016 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1017 if (!req->indirect_pages[j])
1018 break;
1019 kfree(req->indirect_pages[j]);
1020 }
1021 kfree(req);
1022 }
Wenwen Wangf5679e52019-08-11 12:23:22 -05001023 return err;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001024}
1025
1026static int connect_ring(struct backend_info *be)
1027{
1028 struct xenbus_device *dev = be->dev;
1029 unsigned int pers_grants;
1030 char protocol[64] = "";
1031 int err, i;
1032 char *xspath;
1033 size_t xspathsize;
1034 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
Bob Liud62d8602015-11-14 11:12:17 +08001035 unsigned int requested_num_queues = 0;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001036
1037 pr_debug("%s %s\n", __func__, dev->otherend);
1038
1039 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
Jan Beulich66943892016-07-07 02:05:21 -06001040 err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol",
1041 "%63s", protocol);
1042 if (err <= 0)
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001043 strcpy(protocol, "unspecified, assuming default");
1044 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
1045 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
1046 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
1047 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
1048 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
1049 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
1050 else {
1051 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -05001052 return -ENOSYS;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001053 }
Jan Beulich66943892016-07-07 02:05:21 -06001054 err = xenbus_scanf(XBT_NIL, dev->otherend,
1055 "feature-persistent", "%u", &pers_grants);
1056 if (err <= 0)
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001057 pers_grants = 0;
1058
1059 be->blkif->vbd.feature_gnt_persistent = pers_grants;
1060 be->blkif->vbd.overflow_max_grants = 0;
1061
Bob Liud62d8602015-11-14 11:12:17 +08001062 /*
1063 * Read the number of hardware queues from frontend.
1064 */
1065 err = xenbus_scanf(XBT_NIL, dev->otherend, "multi-queue-num-queues",
1066 "%u", &requested_num_queues);
1067 if (err < 0) {
1068 requested_num_queues = 1;
1069 } else {
1070 if (requested_num_queues > xenblk_max_queues
1071 || requested_num_queues == 0) {
1072 /* Buggy or malicious guest. */
1073 xenbus_dev_fatal(dev, err,
1074 "guest requested %u queues, exceeding the maximum of %u.",
1075 requested_num_queues, xenblk_max_queues);
1076 return -ENOSYS;
1077 }
1078 }
1079 be->blkif->nr_rings = requested_num_queues;
1080 if (xen_blkif_alloc_rings(be->blkif))
1081 return -ENOMEM;
1082
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001083 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
1084 be->blkif->nr_rings, be->blkif->blk_protocol, protocol,
1085 pers_grants ? "persistent grants" : "");
1086
1087 if (be->blkif->nr_rings == 1)
1088 return read_per_ring_refs(&be->blkif->rings[0], dev->otherend);
1089 else {
1090 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1091 xspath = kmalloc(xspathsize, GFP_KERNEL);
1092 if (!xspath) {
1093 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
1094 return -ENOMEM;
1095 }
1096
1097 for (i = 0; i < be->blkif->nr_rings; i++) {
1098 memset(xspath, 0, xspathsize);
1099 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
1100 err = read_per_ring_refs(&be->blkif->rings[i], xspath);
1101 if (err) {
1102 kfree(xspath);
1103 return err;
1104 }
1105 }
1106 kfree(xspath);
1107 }
1108 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001109}
1110
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001111static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001112 { "vbd" },
1113 { "" }
1114};
1115
David Vrabel95afae42014-09-08 17:30:41 +01001116static struct xenbus_driver xen_blkbk_driver = {
1117 .ids = xen_blkbk_ids,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001118 .probe = xen_blkbk_probe,
1119 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001120 .otherend_changed = frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001121};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001122
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001123int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001124{
Jan Beulich73db1442011-12-22 09:08:13 +00001125 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001126}