blob: 998915174bb8ca3f30b2a195bad57c5755730098 [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 *);
Juergen Gross5584ea22017-02-09 14:39:57 +010041static void backend_changed(struct xenbus_watch *, const char *,
42 const char *);
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 Gross46464412017-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);
Bob Liu59795702015-11-14 11:12:15 +0800181 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400182
183 return blkif;
184}
185
Bob Liu59795702015-11-14 11:12:15 +0800186static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
Bob Liu86839c52015-06-03 13:40:03 +0800187 unsigned int nr_grefs, unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400188{
189 int err;
Bob Liu59795702015-11-14 11:12:15 +0800190 struct xen_blkif *blkif = ring->blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400191
192 /* Already connected through? */
Bob Liu59795702015-11-14 11:12:15 +0800193 if (ring->irq)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400194 return 0;
195
Bob Liu86839c52015-06-03 13:40:03 +0800196 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
Bob Liu59795702015-11-14 11:12:15 +0800197 &ring->blk_ring);
David Vrabel2d073842011-09-29 16:53:30 +0100198 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400199 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400200
201 switch (blkif->blk_protocol) {
202 case BLKIF_PROTOCOL_NATIVE:
203 {
204 struct blkif_sring *sring;
Bob Liu59795702015-11-14 11:12:15 +0800205 sring = (struct blkif_sring *)ring->blk_ring;
206 BACK_RING_INIT(&ring->blk_rings.native, sring,
Julien Grall67de5df2015-05-05 16:25:56 +0100207 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400208 break;
209 }
210 case BLKIF_PROTOCOL_X86_32:
211 {
212 struct blkif_x86_32_sring *sring_x86_32;
Bob Liu59795702015-11-14 11:12:15 +0800213 sring_x86_32 = (struct blkif_x86_32_sring *)ring->blk_ring;
214 BACK_RING_INIT(&ring->blk_rings.x86_32, sring_x86_32,
Julien Grall67de5df2015-05-05 16:25:56 +0100215 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400216 break;
217 }
218 case BLKIF_PROTOCOL_X86_64:
219 {
220 struct blkif_x86_64_sring *sring_x86_64;
Bob Liu59795702015-11-14 11:12:15 +0800221 sring_x86_64 = (struct blkif_x86_64_sring *)ring->blk_ring;
222 BACK_RING_INIT(&ring->blk_rings.x86_64, sring_x86_64,
Julien Grall67de5df2015-05-05 16:25:56 +0100223 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400224 break;
225 }
226 default:
227 BUG();
228 }
229
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400230 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
231 xen_blkif_be_int, 0,
Bob Liu59795702015-11-14 11:12:15 +0800232 "blkif-backend", ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400233 if (err < 0) {
Bob Liu59795702015-11-14 11:12:15 +0800234 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
235 ring->blk_rings.common.sring = NULL;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400236 return err;
237 }
Bob Liu59795702015-11-14 11:12:15 +0800238 ring->irq = err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400239
240 return 0;
241}
242
Valentin Priescu814d04e2014-05-20 22:28:50 +0200243static int xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400244{
Roger Pau Monnef929d422015-09-04 12:08:07 +0200245 struct pending_req *req, *n;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500246 unsigned int j, r;
Roger Pau Monnef929d422015-09-04 12:08:07 +0200247
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500248 for (r = 0; r < blkif->nr_rings; r++) {
249 struct xen_blkif_ring *ring = &blkif->rings[r];
250 unsigned int i = 0;
251
Juergen Gross46464412017-05-18 17:28:47 +0200252 if (!ring->active)
253 continue;
254
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500255 if (ring->xenblkd) {
256 kthread_stop(ring->xenblkd);
257 wake_up(&ring->shutdown_wq);
258 ring->xenblkd = NULL;
259 }
260
261 /* The above kthread_stop() guarantees that at this point we
262 * don't have any discard_io or other_io requests. So, checking
263 * for inflight IO is enough.
264 */
265 if (atomic_read(&ring->inflight) > 0)
266 return -EBUSY;
267
268 if (ring->irq) {
269 unbind_from_irqhandler(ring->irq, ring);
270 ring->irq = 0;
271 }
272
273 if (ring->blk_rings.common.sring) {
274 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
275 ring->blk_rings.common.sring = NULL;
276 }
277
278 /* Remove all persistent grants and the cache of ballooned pages. */
279 xen_blkbk_free_caches(ring);
280
281 /* Check that there is no request in use */
282 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
283 list_del(&req->free_list);
284
285 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
286 kfree(req->segments[j]);
287
288 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
289 kfree(req->indirect_pages[j]);
290
291 kfree(req);
292 i++;
293 }
294
Bob Liud4bf0062015-11-14 11:12:19 +0800295 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
296 BUG_ON(!list_empty(&ring->persistent_purge_list));
297 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
298 BUG_ON(!list_empty(&ring->free_pages));
299 BUG_ON(ring->free_pages_num != 0);
300 BUG_ON(ring->persistent_gnt_c != 0);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500301 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
Juergen Gross46464412017-05-18 17:28:47 +0200302 ring->active = false;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400303 }
Roger Pau Monnef929d422015-09-04 12:08:07 +0200304 blkif->nr_ring_pages = 0;
Bob Liu93bb2772015-12-10 09:16:48 +0800305 /*
306 * blkif->rings was allocated in connect_ring, so we should free it in
307 * here.
308 */
309 kfree(blkif->rings);
310 blkif->rings = NULL;
311 blkif->nr_rings = 0;
Roger Pau Monnef929d422015-09-04 12:08:07 +0200312
313 return 0;
314}
315
316static void xen_blkif_free(struct xen_blkif *blkif)
317{
318
319 xen_blkif_disconnect(blkif);
320 xen_vbd_free(&blkif->vbd);
321
322 /* Make sure everything is drained before shutting down */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400323 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400324}
325
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400326int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400327{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400328 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400329 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400330 0, 0, NULL);
331 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400332 return -ENOMEM;
333
334 return 0;
335}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400336
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400337/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400338 * sysfs interface for VBD I/O requests
339 */
340
Bob Liudb6fbc12015-12-09 07:44:02 +0800341#define VBD_SHOW_ALLRING(name, format) \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400342 static ssize_t show_##name(struct device *_dev, \
343 struct device_attribute *attr, \
344 char *buf) \
345 { \
346 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800347 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Bob Liudb6fbc12015-12-09 07:44:02 +0800348 struct xen_blkif *blkif = be->blkif; \
349 unsigned int i; \
350 unsigned long long result = 0; \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400351 \
Bob Liudb6fbc12015-12-09 07:44:02 +0800352 if (!blkif->rings) \
353 goto out; \
354 \
355 for (i = 0; i < blkif->nr_rings; i++) { \
356 struct xen_blkif_ring *ring = &blkif->rings[i]; \
357 \
358 result += ring->st_##name; \
359 } \
360 \
361out: \
362 return sprintf(buf, format, result); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400363 } \
364 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
365
Bob Liudb6fbc12015-12-09 07:44:02 +0800366VBD_SHOW_ALLRING(oo_req, "%llu\n");
367VBD_SHOW_ALLRING(rd_req, "%llu\n");
368VBD_SHOW_ALLRING(wr_req, "%llu\n");
369VBD_SHOW_ALLRING(f_req, "%llu\n");
370VBD_SHOW_ALLRING(ds_req, "%llu\n");
371VBD_SHOW_ALLRING(rd_sect, "%llu\n");
372VBD_SHOW_ALLRING(wr_sect, "%llu\n");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400373
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400374static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400375 &dev_attr_oo_req.attr,
376 &dev_attr_rd_req.attr,
377 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400378 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800379 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400380 &dev_attr_rd_sect.attr,
381 &dev_attr_wr_sect.attr,
382 NULL
383};
384
Jan Beulich53043942016-07-07 01:38:58 -0600385static const struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400386 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400387 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400388};
389
Bob Liudb6fbc12015-12-09 07:44:02 +0800390#define VBD_SHOW(name, format, args...) \
391 static ssize_t show_##name(struct device *_dev, \
392 struct device_attribute *attr, \
393 char *buf) \
394 { \
395 struct xenbus_device *dev = to_xenbus_device(_dev); \
396 struct backend_info *be = dev_get_drvdata(&dev->dev); \
397 \
398 return sprintf(buf, format, ##args); \
399 } \
400 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
401
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400402VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
403VBD_SHOW(mode, "%s\n", be->mode);
404
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400405static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400406{
407 int error;
408
409 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400410 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400411 goto fail1;
412
413 error = device_create_file(&dev->dev, &dev_attr_mode);
414 if (error)
415 goto fail2;
416
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400417 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400418 if (error)
419 goto fail3;
420
421 return 0;
422
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400423fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400424fail2: device_remove_file(&dev->dev, &dev_attr_mode);
425fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
426 return error;
427}
428
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400429static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400430{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400431 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400432 device_remove_file(&dev->dev, &dev_attr_mode);
433 device_remove_file(&dev->dev, &dev_attr_physical_device);
434}
435
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400436
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400437static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400438{
439 if (vbd->bdev)
440 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
441 vbd->bdev = NULL;
442}
443
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400444static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
445 unsigned major, unsigned minor, int readonly,
446 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400447{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400448 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400449 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400450 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400451
452 vbd = &blkif->vbd;
453 vbd->handle = handle;
454 vbd->readonly = readonly;
455 vbd->type = 0;
456
457 vbd->pdevice = MKDEV(major, minor);
458
459 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
460 FMODE_READ : FMODE_WRITE, NULL);
461
462 if (IS_ERR(bdev)) {
Tao Chen77387b82015-04-01 15:04:22 +0000463 pr_warn("xen_vbd_create: device %08x could not be opened\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400464 vbd->pdevice);
465 return -ENOENT;
466 }
467
468 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400469 if (vbd->bdev->bd_disk == NULL) {
Tao Chen77387b82015-04-01 15:04:22 +0000470 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400471 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400472 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400473 return -ENOENT;
474 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200475 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400476
477 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
478 vbd->type |= VDISK_CDROM;
479 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
480 vbd->type |= VDISK_REMOVABLE;
481
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400482 q = bdev_get_queue(bdev);
Jens Axboec888a8f2016-04-13 13:33:19 -0600483 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags))
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400484 vbd->flush_support = true;
485
Christoph Hellwig288dab82016-06-09 16:00:36 +0200486 if (q && blk_queue_secure_erase(q))
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400487 vbd->discard_secure = true;
488
Tao Chen77387b82015-04-01 15:04:22 +0000489 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400490 handle, blkif->domid);
491 return 0;
492}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400493static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400494{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800495 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400496
Tao Chen77387b82015-04-01 15:04:22 +0000497 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400498
499 if (be->major || be->minor)
500 xenvbd_sysfs_delif(dev);
501
502 if (be->backend_watch.node) {
503 unregister_xenbus_watch(&be->backend_watch);
504 kfree(be->backend_watch.node);
505 be->backend_watch.node = NULL;
506 }
507
Valentin Priescu814d04e2014-05-20 22:28:50 +0200508 dev_set_drvdata(&dev->dev, NULL);
509
Gustavo A. R. Silva2d4456c2017-05-11 10:27:35 -0500510 if (be->blkif) {
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400511 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400512
Gustavo A. R. Silva2d4456c2017-05-11 10:27:35 -0500513 /* Put the reference we set in xen_blkif_alloc(). */
514 xen_blkif_put(be->blkif);
515 }
516
Jan Beulich9d092602012-12-20 10:31:11 +0000517 kfree(be->mode);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400518 kfree(be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400519 return 0;
520}
521
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400522int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
523 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400524{
525 struct xenbus_device *dev = be->dev;
526 int err;
527
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400528 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400529 "%d", state);
530 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400531 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400532
533 return err;
534}
535
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400536static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800537{
538 struct xenbus_device *dev = be->dev;
539 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800540 int err;
Juergen Gross82357772016-10-31 14:58:40 +0100541 int state = 0;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400542 struct block_device *bdev = be->blkif->vbd.bdev;
543 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800544
Juergen Gross82357772016-10-31 14:58:40 +0100545 if (!xenbus_read_unsigned(dev->nodename, "discard-enable", 1))
Olaf Heringc926b702014-05-21 16:32:42 +0200546 return;
547
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400548 if (blk_queue_discard(q)) {
549 err = xenbus_printf(xbt, dev->nodename,
550 "discard-granularity", "%u",
551 q->limits.discard_granularity);
552 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400553 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
554 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800555 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400556 err = xenbus_printf(xbt, dev->nodename,
557 "discard-alignment", "%u",
558 q->limits.discard_alignment);
559 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400560 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
561 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400562 }
563 state = 1;
564 /* Optional. */
565 err = xenbus_printf(xbt, dev->nodename,
566 "discard-secure", "%d",
567 blkif->vbd.discard_secure);
568 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400569 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400570 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800571 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800572 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800573 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
574 "%d", state);
575 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400576 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800577}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400578int xen_blkbk_barrier(struct xenbus_transaction xbt,
579 struct backend_info *be, int state)
580{
581 struct xenbus_device *dev = be->dev;
582 int err;
583
584 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
585 "%d", state);
586 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400587 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400588
589 return err;
590}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800591
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400592/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400593 * Entry point to this code when a new device is created. Allocate the basic
594 * structures, and watch the store waiting for the hotplug scripts to tell us
595 * the device's physical major and minor numbers. Switch to InitWait.
596 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400597static int xen_blkbk_probe(struct xenbus_device *dev,
598 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400599{
600 int err;
601 struct backend_info *be = kzalloc(sizeof(struct backend_info),
602 GFP_KERNEL);
Tao Chen77387b82015-04-01 15:04:22 +0000603
604 /* match the pr_debug in xen_blkbk_remove */
605 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
606
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400607 if (!be) {
608 xenbus_dev_fatal(dev, -ENOMEM,
609 "allocating backend structure");
610 return -ENOMEM;
611 }
612 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800613 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400614
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400615 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400616 if (IS_ERR(be->blkif)) {
617 err = PTR_ERR(be->blkif);
618 be->blkif = NULL;
619 xenbus_dev_fatal(dev, err, "creating block interface");
620 goto fail;
621 }
622
Jan Beulich5a705842016-02-10 04:18:10 -0700623 err = xenbus_printf(XBT_NIL, dev->nodename,
624 "feature-max-indirect-segments", "%u",
625 MAX_INDIRECT_SEGMENTS);
626 if (err)
627 dev_warn(&dev->dev,
628 "writing %s/feature-max-indirect-segments (%d)",
629 dev->nodename, err);
630
Bob Liud62d8602015-11-14 11:12:17 +0800631 /* Multi-queue: advertise how many queues are supported by us.*/
632 err = xenbus_printf(XBT_NIL, dev->nodename,
633 "multi-queue-max-queues", "%u", xenblk_max_queues);
634 if (err)
635 pr_warn("Error writing multi-queue-max-queues\n");
636
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400637 /* setup back pointer */
638 be->blkif->be = be;
639
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800640 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
641 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400642 if (err)
643 goto fail;
644
Bob Liu86839c52015-06-03 13:40:03 +0800645 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
646 xen_blkif_max_ring_order);
647 if (err)
648 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
649
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400650 err = xenbus_switch_state(dev, XenbusStateInitWait);
651 if (err)
652 goto fail;
653
654 return 0;
655
656fail:
Tao Chen77387b82015-04-01 15:04:22 +0000657 pr_warn("%s failed\n", __func__);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400658 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400659 return err;
660}
661
662
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400663/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400664 * Callback received when the hotplug scripts have placed the physical-device
665 * node. Read it and the mode node, and create a vbd. If the frontend is
666 * ready, connect.
667 */
668static void backend_changed(struct xenbus_watch *watch,
Juergen Gross5584ea22017-02-09 14:39:57 +0100669 const char *path, const char *token)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400670{
671 int err;
672 unsigned major;
673 unsigned minor;
674 struct backend_info *be
675 = container_of(watch, struct backend_info, backend_watch);
676 struct xenbus_device *dev = be->dev;
677 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000678 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400679 char *device_type;
680
Tao Chen77387b82015-04-01 15:04:22 +0000681 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400682
683 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
684 &major, &minor);
685 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400686 /*
687 * Since this watch will fire once immediately after it is
688 * registered, we expect this. Ignore it, and wait for the
689 * hotplug scripts.
690 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400691 return;
692 }
693 if (err != 2) {
694 xenbus_dev_fatal(dev, err, "reading physical-device");
695 return;
696 }
697
Jan Beulich9d092602012-12-20 10:31:11 +0000698 if (be->major | be->minor) {
699 if (be->major != major || be->minor != minor)
Tao Chen77387b82015-04-01 15:04:22 +0000700 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
Jan Beulich9d092602012-12-20 10:31:11 +0000701 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400702 return;
703 }
704
705 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
706 if (IS_ERR(be->mode)) {
707 err = PTR_ERR(be->mode);
708 be->mode = NULL;
709 xenbus_dev_fatal(dev, err, "reading mode");
710 return;
711 }
712
713 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
714 if (!IS_ERR(device_type)) {
715 cdrom = strcmp(device_type, "cdrom") == 0;
716 kfree(device_type);
717 }
718
Jan Beulich9d092602012-12-20 10:31:11 +0000719 /* Front end dir is a number, which is used as the handle. */
Jingoo Hanbb8e0e82013-09-11 14:20:07 -0700720 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
Jan Beulichaea305e12016-07-07 01:38:13 -0600721 if (err) {
722 kfree(be->mode);
723 be->mode = NULL;
Jan Beulich9d092602012-12-20 10:31:11 +0000724 return;
Jan Beulichaea305e12016-07-07 01:38:13 -0600725 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400726
Jan Beulich9d092602012-12-20 10:31:11 +0000727 be->major = major;
728 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400729
Jan Beulich9d092602012-12-20 10:31:11 +0000730 err = xen_vbd_create(be->blkif, handle, major, minor,
731 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400732
Jan Beulich9d092602012-12-20 10:31:11 +0000733 if (err)
734 xenbus_dev_fatal(dev, err, "creating vbd structure");
735 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400736 err = xenvbd_sysfs_addif(dev);
737 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400738 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400739 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400740 }
Jan Beulich9d092602012-12-20 10:31:11 +0000741 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400742
Jan Beulich9d092602012-12-20 10:31:11 +0000743 if (err) {
744 kfree(be->mode);
745 be->mode = NULL;
746 be->major = 0;
747 be->minor = 0;
748 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400749 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400750 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400751 }
752}
753
754
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400755/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400756 * Callback received when the frontend's state changes.
757 */
758static void frontend_changed(struct xenbus_device *dev,
759 enum xenbus_state frontend_state)
760{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800761 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400762 int err;
763
Tao Chen77387b82015-04-01 15:04:22 +0000764 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400765
766 switch (frontend_state) {
767 case XenbusStateInitialising:
768 if (dev->state == XenbusStateClosed) {
Tao Chen77387b82015-04-01 15:04:22 +0000769 pr_info("%s: prepare for reconnect\n", dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400770 xenbus_switch_state(dev, XenbusStateInitWait);
771 }
772 break;
773
774 case XenbusStateInitialised:
775 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400776 /*
777 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800778 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400779 * of frontend_state.
780 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400781 if (dev->state == XenbusStateConnected)
782 break;
783
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400784 /*
785 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800786 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800787 */
Valentin Priescu814d04e2014-05-20 22:28:50 +0200788 err = xen_blkif_disconnect(be->blkif);
789 if (err) {
790 xenbus_dev_fatal(dev, err, "pending I/O");
791 break;
792 }
Keir Fraser313d7b02010-11-24 22:08:20 -0800793
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400794 err = connect_ring(be);
Konrad Rzeszutek Wilk2d0382f2015-11-25 13:20:14 -0500795 if (err) {
796 /*
797 * Clean up so that memory resources can be used by
798 * other devices. connect_ring reported already error.
799 */
800 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400801 break;
Konrad Rzeszutek Wilk2d0382f2015-11-25 13:20:14 -0500802 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400803 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400804 break;
805
806 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400807 xenbus_switch_state(dev, XenbusStateClosing);
808 break;
809
810 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800811 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400812 xenbus_switch_state(dev, XenbusStateClosed);
813 if (xenbus_dev_is_online(dev))
814 break;
815 /* fall through if not online */
816 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800817 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400818 device_unregister(&dev->dev);
819 break;
820
821 default:
822 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
823 frontend_state);
824 break;
825 }
826}
827
828
829/* ** Connection ** */
830
831
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400832/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400833 * Write the physical details regarding the block device to the store, and
834 * switch to Connected state.
835 */
836static void connect(struct backend_info *be)
837{
838 struct xenbus_transaction xbt;
839 int err;
840 struct xenbus_device *dev = be->dev;
841
Tao Chen77387b82015-04-01 15:04:22 +0000842 pr_debug("%s %s\n", __func__, dev->otherend);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400843
844 /* Supply the information about the device the frontend needs */
845again:
846 err = xenbus_transaction_start(&xbt);
847 if (err) {
848 xenbus_dev_fatal(dev, err, "starting transaction");
849 return;
850 }
851
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400852 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400853 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
854
855 xen_blkbk_discard(xbt, be);
856
857 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400858
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200859 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
860 if (err) {
861 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
862 dev->nodename);
863 goto abort;
864 }
865
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400866 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400867 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400868 if (err) {
869 xenbus_dev_fatal(dev, err, "writing %s/sectors",
870 dev->nodename);
871 goto abort;
872 }
873
874 /* FIXME: use a typename instead */
875 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400876 be->blkif->vbd.type |
877 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400878 if (err) {
879 xenbus_dev_fatal(dev, err, "writing %s/info",
880 dev->nodename);
881 goto abort;
882 }
883 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400884 (unsigned long)
885 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400886 if (err) {
887 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
888 dev->nodename);
889 goto abort;
890 }
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200891 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
892 bdev_physical_block_size(be->blkif->vbd.bdev));
893 if (err)
894 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
895 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400896
897 err = xenbus_transaction_end(xbt, 0);
898 if (err == -EAGAIN)
899 goto again;
900 if (err)
901 xenbus_dev_fatal(dev, err, "ending transaction");
902
903 err = xenbus_switch_state(dev, XenbusStateConnected);
904 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700905 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400906 dev->nodename);
907
908 return;
909 abort:
910 xenbus_transaction_end(xbt, 1);
911}
912
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500913/*
914 * Each ring may have multi pages, depends on "ring-page-order".
915 */
916static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400917{
Julien Grall9cce2912015-10-13 17:50:11 +0100918 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
Bob Liu69b91ed2015-06-03 13:40:01 +0800919 struct pending_req *req, *n;
920 int err, i, j;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500921 struct xen_blkif *blkif = ring->blkif;
922 struct xenbus_device *dev = blkif->be->dev;
923 unsigned int ring_page_order, nr_grefs, evtchn;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400924
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500925 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
Bob Liu86839c52015-06-03 13:40:03 +0800926 &evtchn);
927 if (err != 1) {
928 err = -EINVAL;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500929 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400930 return err;
931 }
Bob Liu86839c52015-06-03 13:40:03 +0800932
933 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
934 &ring_page_order);
935 if (err != 1) {
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500936 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", &ring_ref[0]);
Bob Liu86839c52015-06-03 13:40:03 +0800937 if (err != 1) {
938 err = -EINVAL;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500939 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
Bob Liu86839c52015-06-03 13:40:03 +0800940 return err;
941 }
942 nr_grefs = 1;
Bob Liu86839c52015-06-03 13:40:03 +0800943 } else {
944 unsigned int i;
945
946 if (ring_page_order > xen_blkif_max_ring_order) {
947 err = -EINVAL;
948 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d",
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500949 dir, ring_page_order,
Bob Liu86839c52015-06-03 13:40:03 +0800950 xen_blkif_max_ring_order);
951 return err;
952 }
953
954 nr_grefs = 1 << ring_page_order;
955 for (i = 0; i < nr_grefs; i++) {
956 char ring_ref_name[RINGREF_NAME_LEN];
957
958 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500959 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
Bob Liu86839c52015-06-03 13:40:03 +0800960 "%u", &ring_ref[i]);
961 if (err != 1) {
962 err = -EINVAL;
963 xenbus_dev_fatal(dev, err, "reading %s/%s",
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500964 dir, ring_ref_name);
Bob Liu86839c52015-06-03 13:40:03 +0800965 return err;
966 }
Bob Liu86839c52015-06-03 13:40:03 +0800967 }
968 }
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500969 blkif->nr_ring_pages = nr_grefs;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400970
Bob Liu86839c52015-06-03 13:40:03 +0800971 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
Bob Liu69b91ed2015-06-03 13:40:01 +0800972 req = kzalloc(sizeof(*req), GFP_KERNEL);
973 if (!req)
974 goto fail;
Bob Liu59795702015-11-14 11:12:15 +0800975 list_add_tail(&req->free_list, &ring->pending_free);
Bob Liu69b91ed2015-06-03 13:40:01 +0800976 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
977 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
978 if (!req->segments[j])
979 goto fail;
980 }
981 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
982 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
983 GFP_KERNEL);
984 if (!req->indirect_pages[j])
985 goto fail;
986 }
987 }
988
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400989 /* Map the shared frame, irq etc. */
Bob Liu59795702015-11-14 11:12:15 +0800990 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400991 if (err) {
Bob Liu86839c52015-06-03 13:40:03 +0800992 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400993 return err;
994 }
995
996 return 0;
Bob Liu69b91ed2015-06-03 13:40:01 +0800997
998fail:
Bob Liu59795702015-11-14 11:12:15 +0800999 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
Bob Liu69b91ed2015-06-03 13:40:01 +08001000 list_del(&req->free_list);
1001 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
1002 if (!req->segments[j])
1003 break;
1004 kfree(req->segments[j]);
1005 }
1006 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1007 if (!req->indirect_pages[j])
1008 break;
1009 kfree(req->indirect_pages[j]);
1010 }
1011 kfree(req);
1012 }
1013 return -ENOMEM;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001014
1015}
1016
1017static int connect_ring(struct backend_info *be)
1018{
1019 struct xenbus_device *dev = be->dev;
1020 unsigned int pers_grants;
1021 char protocol[64] = "";
1022 int err, i;
1023 char *xspath;
1024 size_t xspathsize;
1025 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
Bob Liud62d8602015-11-14 11:12:17 +08001026 unsigned int requested_num_queues = 0;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001027
1028 pr_debug("%s %s\n", __func__, dev->otherend);
1029
1030 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
Jan Beulich66943892016-07-07 02:05:21 -06001031 err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol",
1032 "%63s", protocol);
1033 if (err <= 0)
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001034 strcpy(protocol, "unspecified, assuming default");
1035 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
1036 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
1037 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
1038 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
1039 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
1040 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
1041 else {
1042 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -05001043 return -ENOSYS;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001044 }
Juergen Gross82357772016-10-31 14:58:40 +01001045 pers_grants = xenbus_read_unsigned(dev->otherend, "feature-persistent",
1046 0);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001047 be->blkif->vbd.feature_gnt_persistent = pers_grants;
1048 be->blkif->vbd.overflow_max_grants = 0;
1049
Bob Liud62d8602015-11-14 11:12:17 +08001050 /*
1051 * Read the number of hardware queues from frontend.
1052 */
Juergen Gross82357772016-10-31 14:58:40 +01001053 requested_num_queues = xenbus_read_unsigned(dev->otherend,
1054 "multi-queue-num-queues",
1055 1);
1056 if (requested_num_queues > xenblk_max_queues
1057 || requested_num_queues == 0) {
1058 /* Buggy or malicious guest. */
1059 xenbus_dev_fatal(dev, err,
1060 "guest requested %u queues, exceeding the maximum of %u.",
1061 requested_num_queues, xenblk_max_queues);
1062 return -ENOSYS;
Bob Liud62d8602015-11-14 11:12:17 +08001063 }
1064 be->blkif->nr_rings = requested_num_queues;
1065 if (xen_blkif_alloc_rings(be->blkif))
1066 return -ENOMEM;
1067
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001068 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
1069 be->blkif->nr_rings, be->blkif->blk_protocol, protocol,
1070 pers_grants ? "persistent grants" : "");
1071
1072 if (be->blkif->nr_rings == 1)
1073 return read_per_ring_refs(&be->blkif->rings[0], dev->otherend);
1074 else {
1075 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1076 xspath = kmalloc(xspathsize, GFP_KERNEL);
1077 if (!xspath) {
1078 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
1079 return -ENOMEM;
1080 }
1081
1082 for (i = 0; i < be->blkif->nr_rings; i++) {
1083 memset(xspath, 0, xspathsize);
1084 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
1085 err = read_per_ring_refs(&be->blkif->rings[i], xspath);
1086 if (err) {
1087 kfree(xspath);
1088 return err;
1089 }
1090 }
1091 kfree(xspath);
1092 }
1093 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001094}
1095
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001096static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001097 { "vbd" },
1098 { "" }
1099};
1100
David Vrabel95afae42014-09-08 17:30:41 +01001101static struct xenbus_driver xen_blkbk_driver = {
1102 .ids = xen_blkbk_ids,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001103 .probe = xen_blkbk_probe,
1104 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001105 .otherend_changed = frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001106};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001107
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001108int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001109{
Jan Beulich73db1442011-12-22 09:08:13 +00001110 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001111}