blob: c31e5c40b45c5db4c9f84c58f2d9e363b6302371 [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", \
28 __FUNCTION__, __LINE__, ##args)
29
30struct backend_info
31{
32 struct xenbus_device *dev;
33 blkif_t *blkif;
34 struct xenbus_watch backend_watch;
35 unsigned major;
36 unsigned minor;
37 char *mode;
38};
39
40static void connect(struct backend_info *);
41static int connect_ring(struct backend_info *);
42static void backend_changed(struct xenbus_watch *, const char **,
43 unsigned int);
44
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070045struct xenbus_device *blkback_xenbus(struct backend_info *be)
46{
47 return be->dev;
48}
49
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040050static int blkback_name(blkif_t *blkif, char *buf)
51{
52 char *devpath, *devname;
53 struct xenbus_device *dev = blkif->be->dev;
54
55 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
56 if (IS_ERR(devpath))
57 return PTR_ERR(devpath);
58
59 if ((devname = strstr(devpath, "/dev/")) != NULL)
60 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
70static void update_blkif_status(blkif_t *blkif)
71{
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
94 blkif->xenblkd = kthread_run(blkif_schedule, blkif, name);
95 if (IS_ERR(blkif->xenblkd)) {
96 err = PTR_ERR(blkif->xenblkd);
97 blkif->xenblkd = NULL;
98 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
99 }
100}
101
102
103/****************************************************************
104 * sysfs interface for VBD I/O requests
105 */
106
107#define VBD_SHOW(name, format, args...) \
108 static ssize_t show_##name(struct device *_dev, \
109 struct device_attribute *attr, \
110 char *buf) \
111 { \
112 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800113 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400114 \
115 return sprintf(buf, format, ##args); \
116 } \
117 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
118
119VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
120VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
121VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
122VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
123VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
124VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
125
126static struct attribute *vbdstat_attrs[] = {
127 &dev_attr_oo_req.attr,
128 &dev_attr_rd_req.attr,
129 &dev_attr_wr_req.attr,
130 &dev_attr_br_req.attr,
131 &dev_attr_rd_sect.attr,
132 &dev_attr_wr_sect.attr,
133 NULL
134};
135
136static struct attribute_group vbdstat_group = {
137 .name = "statistics",
138 .attrs = vbdstat_attrs,
139};
140
141VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
142VBD_SHOW(mode, "%s\n", be->mode);
143
144int xenvbd_sysfs_addif(struct xenbus_device *dev)
145{
146 int error;
147
148 error = device_create_file(&dev->dev, &dev_attr_physical_device);
149 if (error)
150 goto fail1;
151
152 error = device_create_file(&dev->dev, &dev_attr_mode);
153 if (error)
154 goto fail2;
155
156 error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
157 if (error)
158 goto fail3;
159
160 return 0;
161
162fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
163fail2: device_remove_file(&dev->dev, &dev_attr_mode);
164fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
165 return error;
166}
167
168void xenvbd_sysfs_delif(struct xenbus_device *dev)
169{
170 sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
171 device_remove_file(&dev->dev, &dev_attr_mode);
172 device_remove_file(&dev->dev, &dev_attr_physical_device);
173}
174
175static int blkback_remove(struct xenbus_device *dev)
176{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800177 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400178
179 DPRINTK("");
180
181 if (be->major || be->minor)
182 xenvbd_sysfs_delif(dev);
183
184 if (be->backend_watch.node) {
185 unregister_xenbus_watch(&be->backend_watch);
186 kfree(be->backend_watch.node);
187 be->backend_watch.node = NULL;
188 }
189
190 if (be->blkif) {
191 blkif_disconnect(be->blkif);
192 vbd_free(&be->blkif->vbd);
193 blkif_free(be->blkif);
194 be->blkif = NULL;
195 }
196
197 kfree(be);
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800198 dev_set_drvdata(&dev->dev, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400199 return 0;
200}
201
202int blkback_barrier(struct xenbus_transaction xbt,
203 struct backend_info *be, int state)
204{
205 struct xenbus_device *dev = be->dev;
206 int err;
207
208 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
209 "%d", state);
210 if (err)
211 xenbus_dev_fatal(dev, err, "writing feature-barrier");
212
213 return err;
214}
215
216/**
217 * Entry point to this code when a new device is created. Allocate the basic
218 * structures, and watch the store waiting for the hotplug scripts to tell us
219 * the device's physical major and minor numbers. Switch to InitWait.
220 */
221static int blkback_probe(struct xenbus_device *dev,
222 const struct xenbus_device_id *id)
223{
224 int err;
225 struct backend_info *be = kzalloc(sizeof(struct backend_info),
226 GFP_KERNEL);
227 if (!be) {
228 xenbus_dev_fatal(dev, -ENOMEM,
229 "allocating backend structure");
230 return -ENOMEM;
231 }
232 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800233 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400234
235 be->blkif = blkif_alloc(dev->otherend_id);
236 if (IS_ERR(be->blkif)) {
237 err = PTR_ERR(be->blkif);
238 be->blkif = NULL;
239 xenbus_dev_fatal(dev, err, "creating block interface");
240 goto fail;
241 }
242
243 /* setup back pointer */
244 be->blkif->be = be;
245
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800246 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
247 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400248 if (err)
249 goto fail;
250
251 err = xenbus_switch_state(dev, XenbusStateInitWait);
252 if (err)
253 goto fail;
254
255 return 0;
256
257fail:
258 DPRINTK("failed");
259 blkback_remove(dev);
260 return err;
261}
262
263
264/**
265 * Callback received when the hotplug scripts have placed the physical-device
266 * node. Read it and the mode node, and create a vbd. If the frontend is
267 * ready, connect.
268 */
269static void backend_changed(struct xenbus_watch *watch,
270 const char **vec, unsigned int len)
271{
272 int err;
273 unsigned major;
274 unsigned minor;
275 struct backend_info *be
276 = container_of(watch, struct backend_info, backend_watch);
277 struct xenbus_device *dev = be->dev;
278 int cdrom = 0;
279 char *device_type;
280
281 DPRINTK("");
282
283 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
284 &major, &minor);
285 if (XENBUS_EXIST_ERR(err)) {
286 /* Since this watch will fire once immediately after it is
287 registered, we expect this. Ignore it, and wait for the
288 hotplug scripts. */
289 return;
290 }
291 if (err != 2) {
292 xenbus_dev_fatal(dev, err, "reading physical-device");
293 return;
294 }
295
296 if ((be->major || be->minor) &&
297 ((be->major != major) || (be->minor != minor))) {
298 printk(KERN_WARNING
299 "blkback: changing physical device (from %x:%x to "
300 "%x:%x) not supported.\n", be->major, be->minor,
301 major, minor);
302 return;
303 }
304
305 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
306 if (IS_ERR(be->mode)) {
307 err = PTR_ERR(be->mode);
308 be->mode = NULL;
309 xenbus_dev_fatal(dev, err, "reading mode");
310 return;
311 }
312
313 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
314 if (!IS_ERR(device_type)) {
315 cdrom = strcmp(device_type, "cdrom") == 0;
316 kfree(device_type);
317 }
318
319 if (be->major == 0 && be->minor == 0) {
320 /* Front end dir is a number, which is used as the handle. */
321
322 char *p = strrchr(dev->otherend, '/') + 1;
323 long handle = simple_strtoul(p, NULL, 0);
324
325 be->major = major;
326 be->minor = minor;
327
328 err = vbd_create(be->blkif, handle, major, minor,
329 (NULL == strchr(be->mode, 'w')), cdrom);
330 if (err) {
331 be->major = be->minor = 0;
332 xenbus_dev_fatal(dev, err, "creating vbd structure");
333 return;
334 }
335
336 err = xenvbd_sysfs_addif(dev);
337 if (err) {
338 vbd_free(&be->blkif->vbd);
339 be->major = be->minor = 0;
340 xenbus_dev_fatal(dev, err, "creating sysfs entries");
341 return;
342 }
343
344 /* We're potentially connected now */
345 update_blkif_status(be->blkif);
346 }
347}
348
349
350/**
351 * Callback received when the frontend's state changes.
352 */
353static void frontend_changed(struct xenbus_device *dev,
354 enum xenbus_state frontend_state)
355{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800356 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400357 int err;
358
359 DPRINTK("%s", xenbus_strstate(frontend_state));
360
361 switch (frontend_state) {
362 case XenbusStateInitialising:
363 if (dev->state == XenbusStateClosed) {
364 printk(KERN_INFO "%s: %s: prepare for reconnect\n",
365 __FUNCTION__, dev->nodename);
366 xenbus_switch_state(dev, XenbusStateInitWait);
367 }
368 break;
369
370 case XenbusStateInitialised:
371 case XenbusStateConnected:
372 /* Ensure we connect even when two watches fire in
373 close successsion and we miss the intermediate value
374 of frontend_state. */
375 if (dev->state == XenbusStateConnected)
376 break;
377
378 err = connect_ring(be);
379 if (err)
380 break;
381 update_blkif_status(be->blkif);
382 break;
383
384 case XenbusStateClosing:
385 blkif_disconnect(be->blkif);
386 xenbus_switch_state(dev, XenbusStateClosing);
387 break;
388
389 case XenbusStateClosed:
390 xenbus_switch_state(dev, XenbusStateClosed);
391 if (xenbus_dev_is_online(dev))
392 break;
393 /* fall through if not online */
394 case XenbusStateUnknown:
395 device_unregister(&dev->dev);
396 break;
397
398 default:
399 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
400 frontend_state);
401 break;
402 }
403}
404
405
406/* ** Connection ** */
407
408
409/**
410 * Write the physical details regarding the block device to the store, and
411 * switch to Connected state.
412 */
413static void connect(struct backend_info *be)
414{
415 struct xenbus_transaction xbt;
416 int err;
417 struct xenbus_device *dev = be->dev;
418
419 DPRINTK("%s", dev->otherend);
420
421 /* Supply the information about the device the frontend needs */
422again:
423 err = xenbus_transaction_start(&xbt);
424 if (err) {
425 xenbus_dev_fatal(dev, err, "starting transaction");
426 return;
427 }
428
429 err = blkback_barrier(xbt, be, 1);
430 if (err)
431 goto abort;
432
433 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
434 vbd_size(&be->blkif->vbd));
435 if (err) {
436 xenbus_dev_fatal(dev, err, "writing %s/sectors",
437 dev->nodename);
438 goto abort;
439 }
440
441 /* FIXME: use a typename instead */
442 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
443 vbd_info(&be->blkif->vbd));
444 if (err) {
445 xenbus_dev_fatal(dev, err, "writing %s/info",
446 dev->nodename);
447 goto abort;
448 }
449 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
450 vbd_secsize(&be->blkif->vbd));
451 if (err) {
452 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
453 dev->nodename);
454 goto abort;
455 }
456
457 err = xenbus_transaction_end(xbt, 0);
458 if (err == -EAGAIN)
459 goto again;
460 if (err)
461 xenbus_dev_fatal(dev, err, "ending transaction");
462
463 err = xenbus_switch_state(dev, XenbusStateConnected);
464 if (err)
465 xenbus_dev_fatal(dev, err, "switching to Connected state",
466 dev->nodename);
467
468 return;
469 abort:
470 xenbus_transaction_end(xbt, 1);
471}
472
473
474static int connect_ring(struct backend_info *be)
475{
476 struct xenbus_device *dev = be->dev;
477 unsigned long ring_ref;
478 unsigned int evtchn;
479 char protocol[64] = "";
480 int err;
481
482 DPRINTK("%s", dev->otherend);
483
484 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", &ring_ref,
485 "event-channel", "%u", &evtchn, NULL);
486 if (err) {
487 xenbus_dev_fatal(dev, err,
488 "reading %s/ring-ref and event-channel",
489 dev->otherend);
490 return err;
491 }
492
493 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
494 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
495 "%63s", protocol, NULL);
496 if (err)
497 strcpy(protocol, "unspecified, assuming native");
498 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
499 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
500 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
501 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
502 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
503 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
504 else {
505 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
506 return -1;
507 }
508 printk(KERN_INFO
509 "blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n",
510 ring_ref, evtchn, be->blkif->blk_protocol, protocol);
511
512 /* Map the shared frame, irq etc. */
513 err = blkif_map(be->blkif, ring_ref, evtchn);
514 if (err) {
515 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
516 ring_ref, evtchn);
517 return err;
518 }
519
520 return 0;
521}
522
523
524/* ** Driver Registration ** */
525
526
527static const struct xenbus_device_id blkback_ids[] = {
528 { "vbd" },
529 { "" }
530};
531
532
533static struct xenbus_driver blkback = {
534 .name = "vbd",
535 .owner = THIS_MODULE,
536 .ids = blkback_ids,
537 .probe = blkback_probe,
538 .remove = blkback_remove,
539 .otherend_changed = frontend_changed
540};
541
542
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400543int blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400544{
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400545 return xenbus_register_backend(&blkback);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400546}