ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 9131:9e19dcb05f56

Wait for the devices configured at start-up, before allowing the boot to
proceed. This avoids the race (bug #549) that would result in the infamous
VFS: Cannot open root device "sda1" or unknown-block(0,0) message even when
the hotplug layer had succeeded to configure the device.

Note that The Infamous Message has many possible root causes -- this change
only fixes one of them. This particular root cause is characterised by
successful boots interleaved with unsuccessful ones, and diagnose.py showing
both front and backend drivers in the Connected state.

Closes bug #549.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Fri Mar 03 15:56:03 2006 +0100 (2006-03-03)
parents f6445afee702
children aefc75f5db5e 8853631252d8
line source
1 /******************************************************************************
2 * Talks to Xen Store to figure out what devices we have.
3 *
4 * Copyright (C) 2005 Rusty Russell, IBM Corporation
5 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
6 * Copyright (C) 2005 XenSource Ltd
7 *
8 * This file may be distributed separately from the Linux kernel, or
9 * incorporated into other software packages, subject to the following license:
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this source file (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use, copy, modify,
14 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
15 * and to permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * IN THE SOFTWARE.
28 */
30 #define DPRINTK(fmt, args...) \
31 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
33 #include <linux/kernel.h>
34 #include <linux/err.h>
35 #include <linux/string.h>
36 #include <linux/ctype.h>
37 #include <linux/fcntl.h>
38 #include <linux/mm.h>
39 #include <linux/notifier.h>
40 #include <linux/kthread.h>
42 #include <asm/io.h>
43 #include <asm/page.h>
44 #include <asm/pgtable.h>
45 #include <asm/hypervisor.h>
46 #include <xen/xenbus.h>
47 #include <xen/xen_proc.h>
48 #include <xen/evtchn.h>
50 #include "xenbus_comms.h"
52 extern struct semaphore xenwatch_mutex;
54 #define streq(a, b) (strcmp((a), (b)) == 0)
56 static struct notifier_block *xenstore_chain;
58 /* If something in array of ids matches this device, return it. */
59 static const struct xenbus_device_id *
60 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
61 {
62 for (; !streq(arr->devicetype, ""); arr++) {
63 if (streq(arr->devicetype, dev->devicetype))
64 return arr;
65 }
66 return NULL;
67 }
69 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
70 {
71 struct xenbus_driver *drv = to_xenbus_driver(_drv);
73 if (!drv->ids)
74 return 0;
76 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
77 }
79 struct xen_bus_type
80 {
81 char *root;
82 unsigned int levels;
83 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
84 int (*probe)(const char *type, const char *dir);
85 struct bus_type bus;
86 struct device dev;
87 };
90 /* device/<type>/<id> => <type>-<id> */
91 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
92 {
93 nodename = strchr(nodename, '/');
94 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
95 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
96 return -EINVAL;
97 }
99 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
100 if (!strchr(bus_id, '/')) {
101 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
102 return -EINVAL;
103 }
104 *strchr(bus_id, '/') = '-';
105 return 0;
106 }
109 static int read_otherend_details(struct xenbus_device *xendev,
110 char *id_node, char *path_node)
111 {
112 int err = xenbus_gather(XBT_NULL, xendev->nodename,
113 id_node, "%i", &xendev->otherend_id,
114 path_node, NULL, &xendev->otherend,
115 NULL);
116 if (err) {
117 xenbus_dev_fatal(xendev, err,
118 "reading other end details from %s",
119 xendev->nodename);
120 return err;
121 }
122 if (strlen(xendev->otherend) == 0 ||
123 !xenbus_exists(XBT_NULL, xendev->otherend, "")) {
124 xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
125 xendev->nodename);
126 kfree(xendev->otherend);
127 xendev->otherend = NULL;
128 return -ENOENT;
129 }
131 return 0;
132 }
135 static int read_backend_details(struct xenbus_device *xendev)
136 {
137 return read_otherend_details(xendev, "backend-id", "backend");
138 }
141 static int read_frontend_details(struct xenbus_device *xendev)
142 {
143 return read_otherend_details(xendev, "frontend-id", "frontend");
144 }
147 static void free_otherend_details(struct xenbus_device *dev)
148 {
149 kfree(dev->otherend);
150 dev->otherend = NULL;
151 }
154 static void free_otherend_watch(struct xenbus_device *dev)
155 {
156 if (dev->otherend_watch.node) {
157 unregister_xenbus_watch(&dev->otherend_watch);
158 kfree(dev->otherend_watch.node);
159 dev->otherend_watch.node = NULL;
160 }
161 }
164 /* Bus type for frontend drivers. */
165 static int xenbus_probe_frontend(const char *type, const char *name);
166 static struct xen_bus_type xenbus_frontend = {
167 .root = "device",
168 .levels = 2, /* device/type/<id> */
169 .get_bus_id = frontend_bus_id,
170 .probe = xenbus_probe_frontend,
171 .bus = {
172 .name = "xen",
173 .match = xenbus_match,
174 },
175 .dev = {
176 .bus_id = "xen",
177 },
178 };
180 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
181 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
182 {
183 int domid, err;
184 const char *devid, *type, *frontend;
185 unsigned int typelen;
187 type = strchr(nodename, '/');
188 if (!type)
189 return -EINVAL;
190 type++;
191 typelen = strcspn(type, "/");
192 if (!typelen || type[typelen] != '/')
193 return -EINVAL;
195 devid = strrchr(nodename, '/') + 1;
197 err = xenbus_gather(XBT_NULL, nodename, "frontend-id", "%i", &domid,
198 "frontend", NULL, &frontend,
199 NULL);
200 if (err)
201 return err;
202 if (strlen(frontend) == 0)
203 err = -ERANGE;
204 if (!err && !xenbus_exists(XBT_NULL, frontend, ""))
205 err = -ENOENT;
207 kfree(frontend);
209 if (err)
210 return err;
212 if (snprintf(bus_id, BUS_ID_SIZE,
213 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
214 return -ENOSPC;
215 return 0;
216 }
218 static int xenbus_uevent_backend(struct device *dev, char **envp,
219 int num_envp, char *buffer, int buffer_size);
220 static int xenbus_probe_backend(const char *type, const char *domid);
221 static struct xen_bus_type xenbus_backend = {
222 .root = "backend",
223 .levels = 3, /* backend/type/<frontend>/<id> */
224 .get_bus_id = backend_bus_id,
225 .probe = xenbus_probe_backend,
226 .bus = {
227 .name = "xen-backend",
228 .match = xenbus_match,
229 .uevent = xenbus_uevent_backend,
230 },
231 .dev = {
232 .bus_id = "xen-backend",
233 },
234 };
236 static int xenbus_uevent_backend(struct device *dev, char **envp,
237 int num_envp, char *buffer, int buffer_size)
238 {
239 struct xenbus_device *xdev;
240 struct xenbus_driver *drv;
241 int i = 0;
242 int length = 0;
244 DPRINTK("");
246 if (dev == NULL)
247 return -ENODEV;
249 xdev = to_xenbus_device(dev);
250 if (xdev == NULL)
251 return -ENODEV;
253 /* stuff we want to pass to /sbin/hotplug */
254 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
255 "XENBUS_TYPE=%s", xdev->devicetype);
257 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
258 "XENBUS_PATH=%s", xdev->nodename);
260 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
261 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
263 /* terminate, set to next free slot, shrink available space */
264 envp[i] = NULL;
265 envp = &envp[i];
266 num_envp -= i;
267 buffer = &buffer[length];
268 buffer_size -= length;
270 if (dev->driver) {
271 drv = to_xenbus_driver(dev->driver);
272 if (drv && drv->uevent)
273 return drv->uevent(xdev, envp, num_envp, buffer,
274 buffer_size);
275 }
277 return 0;
278 }
280 static void otherend_changed(struct xenbus_watch *watch,
281 const char **vec, unsigned int len)
282 {
283 struct xenbus_device *dev =
284 container_of(watch, struct xenbus_device, otherend_watch);
285 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
286 XenbusState state;
288 /* Protect us against watches firing on old details when the otherend
289 details change, say immediately after a resume. */
290 if (!dev->otherend ||
291 strncmp(dev->otherend, vec[XS_WATCH_PATH],
292 strlen(dev->otherend))) {
293 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
294 return;
295 }
297 state = xenbus_read_driver_state(dev->otherend);
299 DPRINTK("state is %d, %s, %s",
300 state, dev->otherend_watch.node, vec[XS_WATCH_PATH]);
301 if (drv->otherend_changed)
302 drv->otherend_changed(dev, state);
303 }
306 static int talk_to_otherend(struct xenbus_device *dev)
307 {
308 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
310 free_otherend_watch(dev);
311 free_otherend_details(dev);
313 return drv->read_otherend_details(dev);
314 }
317 static int watch_otherend(struct xenbus_device *dev)
318 {
319 return xenbus_watch_path2(dev, dev->otherend, "state",
320 &dev->otherend_watch, otherend_changed);
321 }
324 static int xenbus_dev_probe(struct device *_dev)
325 {
326 struct xenbus_device *dev = to_xenbus_device(_dev);
327 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
328 const struct xenbus_device_id *id;
329 int err;
331 DPRINTK("");
333 if (!drv->probe) {
334 err = -ENODEV;
335 goto fail;
336 }
338 id = match_device(drv->ids, dev);
339 if (!id) {
340 err = -ENODEV;
341 goto fail;
342 }
344 err = talk_to_otherend(dev);
345 if (err) {
346 printk(KERN_WARNING
347 "xenbus_probe: talk_to_otherend on %s failed.\n",
348 dev->nodename);
349 return err;
350 }
352 err = drv->probe(dev, id);
353 if (err)
354 goto fail;
356 err = watch_otherend(dev);
357 if (err) {
358 printk(KERN_WARNING
359 "xenbus_probe: watch_otherend on %s failed.\n",
360 dev->nodename);
361 return err;
362 }
364 return 0;
365 fail:
366 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
367 xenbus_switch_state(dev, XBT_NULL, XenbusStateClosed);
368 return -ENODEV;
370 }
372 static int xenbus_dev_remove(struct device *_dev)
373 {
374 struct xenbus_device *dev = to_xenbus_device(_dev);
375 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
377 DPRINTK("");
379 free_otherend_watch(dev);
380 free_otherend_details(dev);
382 if (drv->remove)
383 drv->remove(dev);
385 xenbus_switch_state(dev, XBT_NULL, XenbusStateClosed);
386 return 0;
387 }
389 static int xenbus_register_driver_common(struct xenbus_driver *drv,
390 struct xen_bus_type *bus)
391 {
392 int ret;
394 drv->driver.name = drv->name;
395 drv->driver.bus = &bus->bus;
396 drv->driver.owner = drv->owner;
397 drv->driver.probe = xenbus_dev_probe;
398 drv->driver.remove = xenbus_dev_remove;
400 down(&xenwatch_mutex);
401 ret = driver_register(&drv->driver);
402 up(&xenwatch_mutex);
403 return ret;
404 }
406 int xenbus_register_frontend(struct xenbus_driver *drv)
407 {
408 drv->read_otherend_details = read_backend_details;
410 return xenbus_register_driver_common(drv, &xenbus_frontend);
411 }
412 EXPORT_SYMBOL(xenbus_register_frontend);
414 int xenbus_register_backend(struct xenbus_driver *drv)
415 {
416 drv->read_otherend_details = read_frontend_details;
418 return xenbus_register_driver_common(drv, &xenbus_backend);
419 }
420 EXPORT_SYMBOL(xenbus_register_backend);
422 void xenbus_unregister_driver(struct xenbus_driver *drv)
423 {
424 driver_unregister(&drv->driver);
425 }
426 EXPORT_SYMBOL(xenbus_unregister_driver);
428 struct xb_find_info
429 {
430 struct xenbus_device *dev;
431 const char *nodename;
432 };
434 static int cmp_dev(struct device *dev, void *data)
435 {
436 struct xenbus_device *xendev = to_xenbus_device(dev);
437 struct xb_find_info *info = data;
439 if (streq(xendev->nodename, info->nodename)) {
440 info->dev = xendev;
441 get_device(dev);
442 return 1;
443 }
444 return 0;
445 }
447 struct xenbus_device *xenbus_device_find(const char *nodename,
448 struct bus_type *bus)
449 {
450 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
452 bus_for_each_dev(bus, NULL, &info, cmp_dev);
453 return info.dev;
454 }
456 static int cleanup_dev(struct device *dev, void *data)
457 {
458 struct xenbus_device *xendev = to_xenbus_device(dev);
459 struct xb_find_info *info = data;
460 int len = strlen(info->nodename);
462 DPRINTK("%s", info->nodename);
464 /* Match the info->nodename path, or any subdirectory of that path. */
465 if (strncmp(xendev->nodename, info->nodename, len))
466 return 0;
468 /* If the node name is longer, ensure it really is a subdirectory. */
469 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
470 return 0;
472 info->dev = xendev;
473 get_device(dev);
474 return 1;
475 }
477 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
478 {
479 struct xb_find_info info = { .nodename = path };
481 do {
482 info.dev = NULL;
483 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
484 if (info.dev) {
485 device_unregister(&info.dev->dev);
486 put_device(&info.dev->dev);
487 }
488 } while (info.dev);
489 }
491 static void xenbus_dev_free(struct xenbus_device *xendev)
492 {
493 kfree(xendev);
494 }
496 static void xenbus_dev_release(struct device *dev)
497 {
498 if (dev) {
499 xenbus_dev_free(to_xenbus_device(dev));
500 }
501 }
503 /* Simplified asprintf. */
504 char *kasprintf(const char *fmt, ...)
505 {
506 va_list ap;
507 unsigned int len;
508 char *p, dummy[1];
510 va_start(ap, fmt);
511 /* FIXME: vsnprintf has a bug, NULL should work */
512 len = vsnprintf(dummy, 0, fmt, ap);
513 va_end(ap);
515 p = kmalloc(len + 1, GFP_KERNEL);
516 if (!p)
517 return NULL;
518 va_start(ap, fmt);
519 vsprintf(p, fmt, ap);
520 va_end(ap);
521 return p;
522 }
524 static ssize_t xendev_show_nodename(struct device *dev,
525 struct device_attribute *attr, char *buf)
526 {
527 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
528 }
529 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
531 static ssize_t xendev_show_devtype(struct device *dev,
532 struct device_attribute *attr, char *buf)
533 {
534 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
535 }
536 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
539 static int xenbus_probe_node(struct xen_bus_type *bus,
540 const char *type,
541 const char *nodename)
542 {
543 int err;
544 struct xenbus_device *xendev;
545 size_t stringlen;
546 char *tmpstring;
548 XenbusState state = xenbus_read_driver_state(nodename);
550 if (state != XenbusStateInitialising) {
551 /* Device is not new, so ignore it. This can happen if a
552 device is going away after switching to Closed. */
553 return 0;
554 }
556 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
557 xendev = kmalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
558 if (!xendev)
559 return -ENOMEM;
560 memset(xendev, 0, sizeof(*xendev));
562 /* Copy the strings into the extra space. */
564 tmpstring = (char *)(xendev + 1);
565 strcpy(tmpstring, nodename);
566 xendev->nodename = tmpstring;
568 tmpstring += strlen(tmpstring) + 1;
569 strcpy(tmpstring, type);
570 xendev->devicetype = tmpstring;
572 xendev->dev.parent = &bus->dev;
573 xendev->dev.bus = &bus->bus;
574 xendev->dev.release = xenbus_dev_release;
576 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
577 if (err)
578 goto fail;
580 /* Register with generic device framework. */
581 err = device_register(&xendev->dev);
582 if (err)
583 goto fail;
585 device_create_file(&xendev->dev, &dev_attr_nodename);
586 device_create_file(&xendev->dev, &dev_attr_devtype);
588 return 0;
589 fail:
590 xenbus_dev_free(xendev);
591 return err;
592 }
594 /* device/<typename>/<name> */
595 static int xenbus_probe_frontend(const char *type, const char *name)
596 {
597 char *nodename;
598 int err;
600 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
601 if (!nodename)
602 return -ENOMEM;
604 DPRINTK("%s", nodename);
606 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
607 kfree(nodename);
608 return err;
609 }
611 /* backend/<typename>/<frontend-uuid>/<name> */
612 static int xenbus_probe_backend_unit(const char *dir,
613 const char *type,
614 const char *name)
615 {
616 char *nodename;
617 int err;
619 nodename = kasprintf("%s/%s", dir, name);
620 if (!nodename)
621 return -ENOMEM;
623 DPRINTK("%s\n", nodename);
625 err = xenbus_probe_node(&xenbus_backend, type, nodename);
626 kfree(nodename);
627 return err;
628 }
630 /* backend/<typename>/<frontend-domid> */
631 static int xenbus_probe_backend(const char *type, const char *domid)
632 {
633 char *nodename;
634 int err = 0;
635 char **dir;
636 unsigned int i, dir_n = 0;
638 DPRINTK("");
640 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
641 if (!nodename)
642 return -ENOMEM;
644 dir = xenbus_directory(XBT_NULL, nodename, "", &dir_n);
645 if (IS_ERR(dir)) {
646 kfree(nodename);
647 return PTR_ERR(dir);
648 }
650 for (i = 0; i < dir_n; i++) {
651 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
652 if (err)
653 break;
654 }
655 kfree(dir);
656 kfree(nodename);
657 return err;
658 }
660 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
661 {
662 int err = 0;
663 char **dir;
664 unsigned int dir_n = 0;
665 int i;
667 dir = xenbus_directory(XBT_NULL, bus->root, type, &dir_n);
668 if (IS_ERR(dir))
669 return PTR_ERR(dir);
671 for (i = 0; i < dir_n; i++) {
672 err = bus->probe(type, dir[i]);
673 if (err)
674 break;
675 }
676 kfree(dir);
677 return err;
678 }
680 static int xenbus_probe_devices(struct xen_bus_type *bus)
681 {
682 int err = 0;
683 char **dir;
684 unsigned int i, dir_n;
686 dir = xenbus_directory(XBT_NULL, bus->root, "", &dir_n);
687 if (IS_ERR(dir))
688 return PTR_ERR(dir);
690 for (i = 0; i < dir_n; i++) {
691 err = xenbus_probe_device_type(bus, dir[i]);
692 if (err)
693 break;
694 }
695 kfree(dir);
696 return err;
697 }
699 static unsigned int char_count(const char *str, char c)
700 {
701 unsigned int i, ret = 0;
703 for (i = 0; str[i]; i++)
704 if (str[i] == c)
705 ret++;
706 return ret;
707 }
709 static int strsep_len(const char *str, char c, unsigned int len)
710 {
711 unsigned int i;
713 for (i = 0; str[i]; i++)
714 if (str[i] == c) {
715 if (len == 0)
716 return i;
717 len--;
718 }
719 return (len == 0) ? i : -ERANGE;
720 }
722 static void dev_changed(const char *node, struct xen_bus_type *bus)
723 {
724 int exists, rootlen;
725 struct xenbus_device *dev;
726 char type[BUS_ID_SIZE];
727 const char *p, *root;
729 if (char_count(node, '/') < 2)
730 return;
732 exists = xenbus_exists(XBT_NULL, node, "");
733 if (!exists) {
734 xenbus_cleanup_devices(node, &bus->bus);
735 return;
736 }
738 /* backend/<type>/... or device/<type>/... */
739 p = strchr(node, '/') + 1;
740 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
741 type[BUS_ID_SIZE-1] = '\0';
743 rootlen = strsep_len(node, '/', bus->levels);
744 if (rootlen < 0)
745 return;
746 root = kasprintf("%.*s", rootlen, node);
747 if (!root)
748 return;
750 dev = xenbus_device_find(root, &bus->bus);
751 if (!dev)
752 xenbus_probe_node(bus, type, root);
753 else
754 put_device(&dev->dev);
756 kfree(root);
757 }
759 static void frontend_changed(struct xenbus_watch *watch,
760 const char **vec, unsigned int len)
761 {
762 DPRINTK("");
764 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
765 }
767 static void backend_changed(struct xenbus_watch *watch,
768 const char **vec, unsigned int len)
769 {
770 DPRINTK("");
772 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
773 }
775 /* We watch for devices appearing and vanishing. */
776 static struct xenbus_watch fe_watch = {
777 .node = "device",
778 .callback = frontend_changed,
779 };
781 static struct xenbus_watch be_watch = {
782 .node = "backend",
783 .callback = backend_changed,
784 };
786 static int suspend_dev(struct device *dev, void *data)
787 {
788 int err = 0;
789 struct xenbus_driver *drv;
790 struct xenbus_device *xdev;
792 DPRINTK("");
794 if (dev->driver == NULL)
795 return 0;
796 drv = to_xenbus_driver(dev->driver);
797 xdev = container_of(dev, struct xenbus_device, dev);
798 if (drv->suspend)
799 err = drv->suspend(xdev);
800 if (err)
801 printk(KERN_WARNING
802 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
803 return 0;
804 }
806 static int resume_dev(struct device *dev, void *data)
807 {
808 int err;
809 struct xenbus_driver *drv;
810 struct xenbus_device *xdev;
812 DPRINTK("");
814 if (dev->driver == NULL)
815 return 0;
816 drv = to_xenbus_driver(dev->driver);
817 xdev = container_of(dev, struct xenbus_device, dev);
819 err = talk_to_otherend(xdev);
820 if (err) {
821 printk(KERN_WARNING
822 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
823 dev->bus_id, err);
824 return err;
825 }
827 err = watch_otherend(xdev);
828 if (err) {
829 printk(KERN_WARNING
830 "xenbus_probe: resume (watch_otherend) %s failed: "
831 "%d.\n", dev->bus_id, err);
832 return err;
833 }
835 if (drv->resume)
836 err = drv->resume(xdev);
837 if (err)
838 printk(KERN_WARNING
839 "xenbus: resume %s failed: %i\n", dev->bus_id, err);
840 return err;
841 }
843 void xenbus_suspend(void)
844 {
845 DPRINTK("");
847 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
848 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
849 xs_suspend();
850 }
851 EXPORT_SYMBOL(xenbus_suspend);
853 void xenbus_resume(void)
854 {
855 xb_init_comms();
856 xs_resume();
857 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
858 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
859 }
860 EXPORT_SYMBOL(xenbus_resume);
863 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
864 int xenstored_ready = 0;
867 int register_xenstore_notifier(struct notifier_block *nb)
868 {
869 int ret = 0;
871 if (xenstored_ready > 0)
872 ret = nb->notifier_call(nb, 0, NULL);
873 else
874 notifier_chain_register(&xenstore_chain, nb);
876 return ret;
877 }
878 EXPORT_SYMBOL(register_xenstore_notifier);
880 void unregister_xenstore_notifier(struct notifier_block *nb)
881 {
882 notifier_chain_unregister(&xenstore_chain, nb);
883 }
884 EXPORT_SYMBOL(unregister_xenstore_notifier);
887 static int all_devices_ready_(struct device *dev, void *data)
888 {
889 struct xenbus_device *xendev = to_xenbus_device(dev);
890 int *result = data;
891 int state;
893 int err = xenbus_scanf(XBT_NULL, xendev->nodename, "state", "%d",
894 &state);
895 if (err != 1 || state != XenbusStateConnected) {
896 result = 0;
897 return 1;
898 }
900 return 1;
901 }
904 static int all_devices_ready(void)
905 {
906 int ready = 1;
907 bus_for_each_dev(&xenbus_frontend.bus, NULL, &ready,
908 all_devices_ready_);
909 return ready;
910 }
913 void xenbus_probe(void *unused)
914 {
915 int i;
917 BUG_ON((xenstored_ready <= 0));
919 /* Enumerate devices in xenstore. */
920 xenbus_probe_devices(&xenbus_frontend);
921 xenbus_probe_devices(&xenbus_backend);
923 /* Watch for changes. */
924 register_xenbus_watch(&fe_watch);
925 register_xenbus_watch(&be_watch);
927 /* Notify others that xenstore is up */
928 notifier_call_chain(&xenstore_chain, 0, NULL);
930 /* On a 10 second timeout, waiting for all devices currently
931 configured. We need to do this to guarantee that the filesystems
932 and / or network devices needed for boot are available, before we
933 can allow the boot to proceed.
935 A possible improvement here would be to have the tools add a
936 per-device flag to the store entry, indicating whether it is needed
937 at boot time. This would allow people who knew what they were
938 doing to accelerate their boot slightly, but of course needs tools
939 or manual intervention to set up those flags correctly.
940 */
941 for (i = 0; i < 10 * HZ; i++) {
942 if (all_devices_ready())
943 return;
945 set_current_state(TASK_INTERRUPTIBLE);
946 schedule_timeout(1);
947 }
949 printk(KERN_WARNING
950 "XENBUS: Timeout connecting to devices!\n");
951 }
954 static struct proc_dir_entry *xsd_kva_intf;
955 static struct proc_dir_entry *xsd_port_intf;
958 static int xsd_kva_read(char *page, char **start, off_t off,
959 int count, int *eof, void *data)
960 {
961 int len;
962 len = sprintf(page, "0x%p", mfn_to_virt(xen_start_info->store_mfn));
963 *eof = 1;
964 return len;
965 }
967 static int xsd_port_read(char *page, char **start, off_t off,
968 int count, int *eof, void *data)
969 {
970 int len;
972 len = sprintf(page, "%d", xen_start_info->store_evtchn);
973 *eof = 1;
974 return len;
975 }
978 static int __init xenbus_probe_init(void)
979 {
980 int err = 0, dom0;
982 DPRINTK("");
984 if (xen_init() < 0) {
985 DPRINTK("failed");
986 return -ENODEV;
987 }
989 /* Register ourselves with the kernel bus & device subsystems */
990 bus_register(&xenbus_frontend.bus);
991 bus_register(&xenbus_backend.bus);
992 device_register(&xenbus_frontend.dev);
993 device_register(&xenbus_backend.dev);
995 /*
996 ** Domain0 doesn't have a store_evtchn or store_mfn yet.
997 */
998 dom0 = (xen_start_info->store_evtchn == 0);
1000 if (dom0) {
1002 unsigned long page;
1003 evtchn_op_t op = { 0 };
1004 int ret;
1007 /* Allocate page. */
1008 page = get_zeroed_page(GFP_KERNEL);
1009 if (!page)
1010 return -ENOMEM;
1012 /* We don't refcnt properly, so set reserved on page.
1013 * (this allocation is permanent) */
1014 SetPageReserved(virt_to_page(page));
1016 xen_start_info->store_mfn =
1017 pfn_to_mfn(virt_to_phys((void *)page) >>
1018 PAGE_SHIFT);
1020 /* Next allocate a local port which xenstored can bind to */
1021 op.cmd = EVTCHNOP_alloc_unbound;
1022 op.u.alloc_unbound.dom = DOMID_SELF;
1023 op.u.alloc_unbound.remote_dom = 0;
1025 ret = HYPERVISOR_event_channel_op(&op);
1026 BUG_ON(ret);
1027 xen_start_info->store_evtchn = op.u.alloc_unbound.port;
1029 /* And finally publish the above info in /proc/xen */
1030 if((xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0400)))
1031 xsd_kva_intf->read_proc = xsd_kva_read;
1032 if((xsd_port_intf = create_xen_proc_entry("xsd_port", 0400)))
1033 xsd_port_intf->read_proc = xsd_port_read;
1036 /* Initialize the interface to xenstore. */
1037 err = xs_init();
1038 if (err) {
1039 printk(KERN_WARNING
1040 "XENBUS: Error initializing xenstore comms: %i\n", err);
1041 return err;
1044 if (!dom0) {
1045 xenstored_ready = 1;
1046 xenbus_probe(NULL);
1049 return 0;
1052 postcore_initcall(xenbus_probe_init);
1054 /*
1055 * Local variables:
1056 * c-file-style: "linux"
1057 * indent-tabs-mode: t
1058 * c-indent-level: 8
1059 * c-basic-offset: 8
1060 * tab-width: 8
1061 * End:
1062 */