blob: b41ed65db2d316ed892b1d7ea7f2198d48233b6c [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
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20#include <stdarg.h>
21#include <linux/module.h>
22#include <linux/kthread.h>
23#include "common.h"
24
25#undef DPRINTK
26#define DPRINTK(fmt, args...) \
27 pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040028 __func__, __LINE__, ##args)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040029
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040030struct backend_info {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040031 struct xenbus_device *dev;
Konrad Rzeszutek Wilk54893772011-04-14 17:21:50 -040032 struct blkif_st *blkif;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040033 struct xenbus_watch backend_watch;
34 unsigned major;
35 unsigned minor;
36 char *mode;
37};
38
39static void connect(struct backend_info *);
40static int connect_ring(struct backend_info *);
41static void backend_changed(struct xenbus_watch *, const char **,
42 unsigned int);
43
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070044struct xenbus_device *blkback_xenbus(struct backend_info *be)
45{
46 return be->dev;
47}
48
Konrad Rzeszutek Wilk54893772011-04-14 17:21:50 -040049static int blkback_name(struct blkif_st *blkif, char *buf)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040050{
51 char *devpath, *devname;
52 struct xenbus_device *dev = blkif->be->dev;
53
54 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
55 if (IS_ERR(devpath))
56 return PTR_ERR(devpath);
57
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040058 devname = strstr(devpath, "/dev/");
59 if (devname != NULL)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040060 devname += strlen("/dev/");
61 else
62 devname = devpath;
63
64 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
65 kfree(devpath);
66
67 return 0;
68}
69
Konrad Rzeszutek Wilk54893772011-04-14 17:21:50 -040070static void update_blkif_status(struct blkif_st *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040071{
72 int err;
73 char name[TASK_COMM_LEN];
74
75 /* Not ready to connect? */
76 if (!blkif->irq || !blkif->vbd.bdev)
77 return;
78
79 /* Already connected? */
80 if (blkif->be->dev->state == XenbusStateConnected)
81 return;
82
83 /* Attempt to connect: exit if we fail to. */
84 connect(blkif->be);
85 if (blkif->be->dev->state != XenbusStateConnected)
86 return;
87
88 err = blkback_name(blkif, name);
89 if (err) {
90 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
91 return;
92 }
93
Chris Lalancettecbf46292010-07-21 12:41:45 -070094 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
95 if (err) {
96 xenbus_dev_error(blkif->be->dev, err, "block flush");
97 return;
98 }
99 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
100
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400101 blkif->xenblkd = kthread_run(blkif_schedule, blkif, name);
102 if (IS_ERR(blkif->xenblkd)) {
103 err = PTR_ERR(blkif->xenblkd);
104 blkif->xenblkd = NULL;
105 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
106 }
107}
108
109
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400110/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400111 * sysfs interface for VBD I/O requests
112 */
113
114#define VBD_SHOW(name, format, args...) \
115 static ssize_t show_##name(struct device *_dev, \
116 struct device_attribute *attr, \
117 char *buf) \
118 { \
119 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800120 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400121 \
122 return sprintf(buf, format, ##args); \
123 } \
124 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
125
126VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
127VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
128VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
129VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
130VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
131VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
132
133static struct attribute *vbdstat_attrs[] = {
134 &dev_attr_oo_req.attr,
135 &dev_attr_rd_req.attr,
136 &dev_attr_wr_req.attr,
137 &dev_attr_br_req.attr,
138 &dev_attr_rd_sect.attr,
139 &dev_attr_wr_sect.attr,
140 NULL
141};
142
143static struct attribute_group vbdstat_group = {
144 .name = "statistics",
145 .attrs = vbdstat_attrs,
146};
147
148VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
149VBD_SHOW(mode, "%s\n", be->mode);
150
151int xenvbd_sysfs_addif(struct xenbus_device *dev)
152{
153 int error;
154
155 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400156 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400157 goto fail1;
158
159 error = device_create_file(&dev->dev, &dev_attr_mode);
160 if (error)
161 goto fail2;
162
163 error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
164 if (error)
165 goto fail3;
166
167 return 0;
168
169fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
170fail2: device_remove_file(&dev->dev, &dev_attr_mode);
171fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
172 return error;
173}
174
175void xenvbd_sysfs_delif(struct xenbus_device *dev)
176{
177 sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
178 device_remove_file(&dev->dev, &dev_attr_mode);
179 device_remove_file(&dev->dev, &dev_attr_physical_device);
180}
181
182static int blkback_remove(struct xenbus_device *dev)
183{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800184 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400185
186 DPRINTK("");
187
188 if (be->major || be->minor)
189 xenvbd_sysfs_delif(dev);
190
191 if (be->backend_watch.node) {
192 unregister_xenbus_watch(&be->backend_watch);
193 kfree(be->backend_watch.node);
194 be->backend_watch.node = NULL;
195 }
196
197 if (be->blkif) {
198 blkif_disconnect(be->blkif);
199 vbd_free(&be->blkif->vbd);
200 blkif_free(be->blkif);
201 be->blkif = NULL;
202 }
203
204 kfree(be);
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800205 dev_set_drvdata(&dev->dev, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400206 return 0;
207}
208
209int blkback_barrier(struct xenbus_transaction xbt,
210 struct backend_info *be, int state)
211{
212 struct xenbus_device *dev = be->dev;
213 int err;
214
215 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
216 "%d", state);
217 if (err)
218 xenbus_dev_fatal(dev, err, "writing feature-barrier");
219
220 return err;
221}
222
223/**
224 * Entry point to this code when a new device is created. Allocate the basic
225 * structures, and watch the store waiting for the hotplug scripts to tell us
226 * the device's physical major and minor numbers. Switch to InitWait.
227 */
228static int blkback_probe(struct xenbus_device *dev,
229 const struct xenbus_device_id *id)
230{
231 int err;
232 struct backend_info *be = kzalloc(sizeof(struct backend_info),
233 GFP_KERNEL);
234 if (!be) {
235 xenbus_dev_fatal(dev, -ENOMEM,
236 "allocating backend structure");
237 return -ENOMEM;
238 }
239 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800240 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400241
242 be->blkif = blkif_alloc(dev->otherend_id);
243 if (IS_ERR(be->blkif)) {
244 err = PTR_ERR(be->blkif);
245 be->blkif = NULL;
246 xenbus_dev_fatal(dev, err, "creating block interface");
247 goto fail;
248 }
249
250 /* setup back pointer */
251 be->blkif->be = be;
252
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800253 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
254 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400255 if (err)
256 goto fail;
257
258 err = xenbus_switch_state(dev, XenbusStateInitWait);
259 if (err)
260 goto fail;
261
262 return 0;
263
264fail:
265 DPRINTK("failed");
266 blkback_remove(dev);
267 return err;
268}
269
270
271/**
272 * Callback received when the hotplug scripts have placed the physical-device
273 * node. Read it and the mode node, and create a vbd. If the frontend is
274 * ready, connect.
275 */
276static void backend_changed(struct xenbus_watch *watch,
277 const char **vec, unsigned int len)
278{
279 int err;
280 unsigned major;
281 unsigned minor;
282 struct backend_info *be
283 = container_of(watch, struct backend_info, backend_watch);
284 struct xenbus_device *dev = be->dev;
285 int cdrom = 0;
286 char *device_type;
287
288 DPRINTK("");
289
290 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
291 &major, &minor);
292 if (XENBUS_EXIST_ERR(err)) {
293 /* Since this watch will fire once immediately after it is
294 registered, we expect this. Ignore it, and wait for the
295 hotplug scripts. */
296 return;
297 }
298 if (err != 2) {
299 xenbus_dev_fatal(dev, err, "reading physical-device");
300 return;
301 }
302
303 if ((be->major || be->minor) &&
304 ((be->major != major) || (be->minor != minor))) {
305 printk(KERN_WARNING
306 "blkback: changing physical device (from %x:%x to "
307 "%x:%x) not supported.\n", be->major, be->minor,
308 major, minor);
309 return;
310 }
311
312 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
313 if (IS_ERR(be->mode)) {
314 err = PTR_ERR(be->mode);
315 be->mode = NULL;
316 xenbus_dev_fatal(dev, err, "reading mode");
317 return;
318 }
319
320 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
321 if (!IS_ERR(device_type)) {
322 cdrom = strcmp(device_type, "cdrom") == 0;
323 kfree(device_type);
324 }
325
326 if (be->major == 0 && be->minor == 0) {
327 /* Front end dir is a number, which is used as the handle. */
328
329 char *p = strrchr(dev->otherend, '/') + 1;
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400330 long handle;
331 err = strict_strtoul(p, 0, &handle);
332 if (err)
333 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400334
335 be->major = major;
336 be->minor = minor;
337
338 err = vbd_create(be->blkif, handle, major, minor,
339 (NULL == strchr(be->mode, 'w')), cdrom);
340 if (err) {
341 be->major = be->minor = 0;
342 xenbus_dev_fatal(dev, err, "creating vbd structure");
343 return;
344 }
345
346 err = xenvbd_sysfs_addif(dev);
347 if (err) {
348 vbd_free(&be->blkif->vbd);
349 be->major = be->minor = 0;
350 xenbus_dev_fatal(dev, err, "creating sysfs entries");
351 return;
352 }
353
354 /* We're potentially connected now */
355 update_blkif_status(be->blkif);
356 }
357}
358
359
360/**
361 * Callback received when the frontend's state changes.
362 */
363static void frontend_changed(struct xenbus_device *dev,
364 enum xenbus_state frontend_state)
365{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800366 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400367 int err;
368
369 DPRINTK("%s", xenbus_strstate(frontend_state));
370
371 switch (frontend_state) {
372 case XenbusStateInitialising:
373 if (dev->state == XenbusStateClosed) {
374 printk(KERN_INFO "%s: %s: prepare for reconnect\n",
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400375 __func__, dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400376 xenbus_switch_state(dev, XenbusStateInitWait);
377 }
378 break;
379
380 case XenbusStateInitialised:
381 case XenbusStateConnected:
382 /* Ensure we connect even when two watches fire in
383 close successsion and we miss the intermediate value
384 of frontend_state. */
385 if (dev->state == XenbusStateConnected)
386 break;
387
Keir Fraser313d7b02010-11-24 22:08:20 -0800388 /* Enforce precondition before potential leak point.
389 * blkif_disconnect() is idempotent.
390 */
391 blkif_disconnect(be->blkif);
392
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400393 err = connect_ring(be);
394 if (err)
395 break;
396 update_blkif_status(be->blkif);
397 break;
398
399 case XenbusStateClosing:
400 blkif_disconnect(be->blkif);
401 xenbus_switch_state(dev, XenbusStateClosing);
402 break;
403
404 case XenbusStateClosed:
405 xenbus_switch_state(dev, XenbusStateClosed);
406 if (xenbus_dev_is_online(dev))
407 break;
408 /* fall through if not online */
409 case XenbusStateUnknown:
Keir Fraser313d7b02010-11-24 22:08:20 -0800410 /* implies blkif_disconnect() via blkback_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400411 device_unregister(&dev->dev);
412 break;
413
414 default:
415 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
416 frontend_state);
417 break;
418 }
419}
420
421
422/* ** Connection ** */
423
424
425/**
426 * Write the physical details regarding the block device to the store, and
427 * switch to Connected state.
428 */
429static void connect(struct backend_info *be)
430{
431 struct xenbus_transaction xbt;
432 int err;
433 struct xenbus_device *dev = be->dev;
434
435 DPRINTK("%s", dev->otherend);
436
437 /* Supply the information about the device the frontend needs */
438again:
439 err = xenbus_transaction_start(&xbt);
440 if (err) {
441 xenbus_dev_fatal(dev, err, "starting transaction");
442 return;
443 }
444
445 err = blkback_barrier(xbt, be, 1);
446 if (err)
447 goto abort;
448
449 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
450 vbd_size(&be->blkif->vbd));
451 if (err) {
452 xenbus_dev_fatal(dev, err, "writing %s/sectors",
453 dev->nodename);
454 goto abort;
455 }
456
457 /* FIXME: use a typename instead */
458 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
459 vbd_info(&be->blkif->vbd));
460 if (err) {
461 xenbus_dev_fatal(dev, err, "writing %s/info",
462 dev->nodename);
463 goto abort;
464 }
465 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
466 vbd_secsize(&be->blkif->vbd));
467 if (err) {
468 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
469 dev->nodename);
470 goto abort;
471 }
472
473 err = xenbus_transaction_end(xbt, 0);
474 if (err == -EAGAIN)
475 goto again;
476 if (err)
477 xenbus_dev_fatal(dev, err, "ending transaction");
478
479 err = xenbus_switch_state(dev, XenbusStateConnected);
480 if (err)
481 xenbus_dev_fatal(dev, err, "switching to Connected state",
482 dev->nodename);
483
484 return;
485 abort:
486 xenbus_transaction_end(xbt, 1);
487}
488
489
490static int connect_ring(struct backend_info *be)
491{
492 struct xenbus_device *dev = be->dev;
493 unsigned long ring_ref;
494 unsigned int evtchn;
495 char protocol[64] = "";
496 int err;
497
498 DPRINTK("%s", dev->otherend);
499
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400500 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
501 &ring_ref, "event-channel", "%u", &evtchn, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400502 if (err) {
503 xenbus_dev_fatal(dev, err,
504 "reading %s/ring-ref and event-channel",
505 dev->otherend);
506 return err;
507 }
508
509 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
510 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
511 "%63s", protocol, NULL);
512 if (err)
513 strcpy(protocol, "unspecified, assuming native");
514 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
515 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
516 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
517 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
518 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
519 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
520 else {
521 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
522 return -1;
523 }
524 printk(KERN_INFO
525 "blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n",
526 ring_ref, evtchn, be->blkif->blk_protocol, protocol);
527
528 /* Map the shared frame, irq etc. */
529 err = blkif_map(be->blkif, ring_ref, evtchn);
530 if (err) {
531 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
532 ring_ref, evtchn);
533 return err;
534 }
535
536 return 0;
537}
538
539
540/* ** Driver Registration ** */
541
542
543static const struct xenbus_device_id blkback_ids[] = {
544 { "vbd" },
545 { "" }
546};
547
548
549static struct xenbus_driver blkback = {
550 .name = "vbd",
551 .owner = THIS_MODULE,
552 .ids = blkback_ids,
553 .probe = blkback_probe,
554 .remove = blkback_remove,
555 .otherend_changed = frontend_changed
556};
557
558
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400559int blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400560{
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400561 return xenbus_register_backend(&blkback);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400562}