direct-io.hg

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 10903:ee6014279103

[LINUX] Clarify a confusing error message in xenbus_probe.c.

Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>
author kfraser@localhost.localdomain
date Wed Aug 02 14:59:22 2006 +0100 (2006-08-02)
parents a377304fc4e6
children 857854460d36
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, 2006 XenSource Ltd
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation; or, when distributed
11 * separately from the Linux kernel or incorporated into other
12 * software packages, subject to the following license:
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this source file (the "Software"), to deal in the Software without
16 * restriction, including without limitation the rights to use, copy, modify,
17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 * IN THE SOFTWARE.
31 */
33 #define DPRINTK(fmt, args...) \
34 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
35 __FUNCTION__, __LINE__, ##args)
37 #include <linux/kernel.h>
38 #include <linux/err.h>
39 #include <linux/string.h>
40 #include <linux/ctype.h>
41 #include <linux/fcntl.h>
42 #include <linux/mm.h>
43 #include <linux/notifier.h>
44 #include <linux/kthread.h>
46 #include <asm/io.h>
47 #include <asm/page.h>
48 #include <asm/pgtable.h>
49 #include <asm/hypervisor.h>
50 #include <xen/xenbus.h>
51 #include <xen/xen_proc.h>
52 #include <xen/evtchn.h>
53 #include <xen/features.h>
55 #include "xenbus_comms.h"
57 extern struct mutex xenwatch_mutex;
59 static struct notifier_block *xenstore_chain;
61 /* If something in array of ids matches this device, return it. */
62 static const struct xenbus_device_id *
63 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
64 {
65 for (; *arr->devicetype != '\0'; arr++) {
66 if (!strcmp(arr->devicetype, dev->devicetype))
67 return arr;
68 }
69 return NULL;
70 }
72 static int xenbus_match(struct device *_dev, struct device_driver *_drv)
73 {
74 struct xenbus_driver *drv = to_xenbus_driver(_drv);
76 if (!drv->ids)
77 return 0;
79 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
80 }
82 struct xen_bus_type
83 {
84 char *root;
85 unsigned int levels;
86 int (*get_bus_id)(char bus_id[BUS_ID_SIZE], const char *nodename);
87 int (*probe)(const char *type, const char *dir);
88 struct bus_type bus;
89 struct device dev;
90 };
93 /* device/<type>/<id> => <type>-<id> */
94 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
95 {
96 nodename = strchr(nodename, '/');
97 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
98 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
99 return -EINVAL;
100 }
102 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
103 if (!strchr(bus_id, '/')) {
104 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
105 return -EINVAL;
106 }
107 *strchr(bus_id, '/') = '-';
108 return 0;
109 }
112 static void free_otherend_details(struct xenbus_device *dev)
113 {
114 kfree(dev->otherend);
115 dev->otherend = NULL;
116 }
119 static void free_otherend_watch(struct xenbus_device *dev)
120 {
121 if (dev->otherend_watch.node) {
122 unregister_xenbus_watch(&dev->otherend_watch);
123 kfree(dev->otherend_watch.node);
124 dev->otherend_watch.node = NULL;
125 }
126 }
129 static int read_otherend_details(struct xenbus_device *xendev,
130 char *id_node, char *path_node)
131 {
132 int err = xenbus_gather(XBT_NIL, xendev->nodename,
133 id_node, "%i", &xendev->otherend_id,
134 path_node, NULL, &xendev->otherend,
135 NULL);
136 if (err) {
137 xenbus_dev_fatal(xendev, err,
138 "reading other end details from %s",
139 xendev->nodename);
140 return err;
141 }
142 if (strlen(xendev->otherend) == 0 ||
143 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
144 xenbus_dev_fatal(xendev, -ENOENT,
145 "unable to read other end from %s. "
146 "missing or inaccessible.",
147 xendev->nodename);
148 free_otherend_details(xendev);
149 return -ENOENT;
150 }
152 return 0;
153 }
156 static int read_backend_details(struct xenbus_device *xendev)
157 {
158 return read_otherend_details(xendev, "backend-id", "backend");
159 }
162 static int read_frontend_details(struct xenbus_device *xendev)
163 {
164 return read_otherend_details(xendev, "frontend-id", "frontend");
165 }
168 /* Bus type for frontend drivers. */
169 static int xenbus_probe_frontend(const char *type, const char *name);
170 static struct xen_bus_type xenbus_frontend = {
171 .root = "device",
172 .levels = 2, /* device/type/<id> */
173 .get_bus_id = frontend_bus_id,
174 .probe = xenbus_probe_frontend,
175 .bus = {
176 .name = "xen",
177 .match = xenbus_match,
178 },
179 .dev = {
180 .bus_id = "xen",
181 },
182 };
184 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
185 static int backend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
186 {
187 int domid, err;
188 const char *devid, *type, *frontend;
189 unsigned int typelen;
191 type = strchr(nodename, '/');
192 if (!type)
193 return -EINVAL;
194 type++;
195 typelen = strcspn(type, "/");
196 if (!typelen || type[typelen] != '/')
197 return -EINVAL;
199 devid = strrchr(nodename, '/') + 1;
201 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
202 "frontend", NULL, &frontend,
203 NULL);
204 if (err)
205 return err;
206 if (strlen(frontend) == 0)
207 err = -ERANGE;
208 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
209 err = -ENOENT;
211 kfree(frontend);
213 if (err)
214 return err;
216 if (snprintf(bus_id, BUS_ID_SIZE,
217 "%.*s-%i-%s", typelen, type, domid, devid) >= BUS_ID_SIZE)
218 return -ENOSPC;
219 return 0;
220 }
222 static int xenbus_uevent_backend(struct device *dev, char **envp,
223 int num_envp, char *buffer, int buffer_size);
224 static int xenbus_probe_backend(const char *type, const char *domid);
225 static struct xen_bus_type xenbus_backend = {
226 .root = "backend",
227 .levels = 3, /* backend/type/<frontend>/<id> */
228 .get_bus_id = backend_bus_id,
229 .probe = xenbus_probe_backend,
230 .bus = {
231 .name = "xen-backend",
232 .match = xenbus_match,
233 .uevent = xenbus_uevent_backend,
234 },
235 .dev = {
236 .bus_id = "xen-backend",
237 },
238 };
240 static int xenbus_uevent_backend(struct device *dev, char **envp,
241 int num_envp, char *buffer, int buffer_size)
242 {
243 struct xenbus_device *xdev;
244 struct xenbus_driver *drv;
245 int i = 0;
246 int length = 0;
248 DPRINTK("");
250 if (dev == NULL)
251 return -ENODEV;
253 xdev = to_xenbus_device(dev);
254 if (xdev == NULL)
255 return -ENODEV;
257 /* stuff we want to pass to /sbin/hotplug */
258 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
259 "XENBUS_TYPE=%s", xdev->devicetype);
261 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
262 "XENBUS_PATH=%s", xdev->nodename);
264 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
265 "XENBUS_BASE_PATH=%s", xenbus_backend.root);
267 /* terminate, set to next free slot, shrink available space */
268 envp[i] = NULL;
269 envp = &envp[i];
270 num_envp -= i;
271 buffer = &buffer[length];
272 buffer_size -= length;
274 if (dev->driver) {
275 drv = to_xenbus_driver(dev->driver);
276 if (drv && drv->uevent)
277 return drv->uevent(xdev, envp, num_envp, buffer,
278 buffer_size);
279 }
281 return 0;
282 }
284 static void otherend_changed(struct xenbus_watch *watch,
285 const char **vec, unsigned int len)
286 {
287 struct xenbus_device *dev =
288 container_of(watch, struct xenbus_device, otherend_watch);
289 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
290 enum xenbus_state state;
292 /* Protect us against watches firing on old details when the otherend
293 details change, say immediately after a resume. */
294 if (!dev->otherend ||
295 strncmp(dev->otherend, vec[XS_WATCH_PATH],
296 strlen(dev->otherend))) {
297 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
298 return;
299 }
301 state = xenbus_read_driver_state(dev->otherend);
303 DPRINTK("state is %d, %s, %s",
304 state, dev->otherend_watch.node, vec[XS_WATCH_PATH]);
305 if (drv->otherend_changed)
306 drv->otherend_changed(dev, state);
307 }
310 static int talk_to_otherend(struct xenbus_device *dev)
311 {
312 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
314 free_otherend_watch(dev);
315 free_otherend_details(dev);
317 return drv->read_otherend_details(dev);
318 }
321 static int watch_otherend(struct xenbus_device *dev)
322 {
323 return xenbus_watch_path2(dev, dev->otherend, "state",
324 &dev->otherend_watch, otherend_changed);
325 }
328 static int xenbus_dev_probe(struct device *_dev)
329 {
330 struct xenbus_device *dev = to_xenbus_device(_dev);
331 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
332 const struct xenbus_device_id *id;
333 int err;
335 DPRINTK("");
337 if (!drv->probe) {
338 err = -ENODEV;
339 goto fail;
340 }
342 id = match_device(drv->ids, dev);
343 if (!id) {
344 err = -ENODEV;
345 goto fail;
346 }
348 err = talk_to_otherend(dev);
349 if (err) {
350 printk(KERN_WARNING
351 "xenbus_probe: talk_to_otherend on %s failed.\n",
352 dev->nodename);
353 return err;
354 }
356 err = drv->probe(dev, id);
357 if (err)
358 goto fail;
360 err = watch_otherend(dev);
361 if (err) {
362 printk(KERN_WARNING
363 "xenbus_probe: watch_otherend on %s failed.\n",
364 dev->nodename);
365 return err;
366 }
368 return 0;
369 fail:
370 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
371 xenbus_switch_state(dev, XenbusStateClosed);
372 return -ENODEV;
373 }
375 static int xenbus_dev_remove(struct device *_dev)
376 {
377 struct xenbus_device *dev = to_xenbus_device(_dev);
378 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
380 DPRINTK("");
382 free_otherend_watch(dev);
383 free_otherend_details(dev);
385 if (drv->remove)
386 drv->remove(dev);
388 xenbus_switch_state(dev, XenbusStateClosed);
389 return 0;
390 }
392 static int xenbus_register_driver_common(struct xenbus_driver *drv,
393 struct xen_bus_type *bus)
394 {
395 int ret;
397 drv->driver.name = drv->name;
398 drv->driver.bus = &bus->bus;
399 drv->driver.owner = drv->owner;
400 drv->driver.probe = xenbus_dev_probe;
401 drv->driver.remove = xenbus_dev_remove;
403 mutex_lock(&xenwatch_mutex);
404 ret = driver_register(&drv->driver);
405 mutex_unlock(&xenwatch_mutex);
406 return ret;
407 }
409 int xenbus_register_frontend(struct xenbus_driver *drv)
410 {
411 drv->read_otherend_details = read_backend_details;
413 return xenbus_register_driver_common(drv, &xenbus_frontend);
414 }
415 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
417 int xenbus_register_backend(struct xenbus_driver *drv)
418 {
419 drv->read_otherend_details = read_frontend_details;
421 return xenbus_register_driver_common(drv, &xenbus_backend);
422 }
423 EXPORT_SYMBOL_GPL(xenbus_register_backend);
425 void xenbus_unregister_driver(struct xenbus_driver *drv)
426 {
427 driver_unregister(&drv->driver);
428 }
429 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
431 struct xb_find_info
432 {
433 struct xenbus_device *dev;
434 const char *nodename;
435 };
437 static int cmp_dev(struct device *dev, void *data)
438 {
439 struct xenbus_device *xendev = to_xenbus_device(dev);
440 struct xb_find_info *info = data;
442 if (!strcmp(xendev->nodename, info->nodename)) {
443 info->dev = xendev;
444 get_device(dev);
445 return 1;
446 }
447 return 0;
448 }
450 struct xenbus_device *xenbus_device_find(const char *nodename,
451 struct bus_type *bus)
452 {
453 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
455 bus_for_each_dev(bus, NULL, &info, cmp_dev);
456 return info.dev;
457 }
459 static int cleanup_dev(struct device *dev, void *data)
460 {
461 struct xenbus_device *xendev = to_xenbus_device(dev);
462 struct xb_find_info *info = data;
463 int len = strlen(info->nodename);
465 DPRINTK("%s", info->nodename);
467 /* Match the info->nodename path, or any subdirectory of that path. */
468 if (strncmp(xendev->nodename, info->nodename, len))
469 return 0;
471 /* If the node name is longer, ensure it really is a subdirectory. */
472 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
473 return 0;
475 info->dev = xendev;
476 get_device(dev);
477 return 1;
478 }
480 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
481 {
482 struct xb_find_info info = { .nodename = path };
484 do {
485 info.dev = NULL;
486 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
487 if (info.dev) {
488 device_unregister(&info.dev->dev);
489 put_device(&info.dev->dev);
490 }
491 } while (info.dev);
492 }
494 static void xenbus_dev_release(struct device *dev)
495 {
496 if (dev)
497 kfree(to_xenbus_device(dev));
498 }
500 /* Simplified asprintf. */
501 char *kasprintf(const char *fmt, ...)
502 {
503 va_list ap;
504 unsigned int len;
505 char *p, dummy[1];
507 va_start(ap, fmt);
508 /* FIXME: vsnprintf has a bug, NULL should work */
509 len = vsnprintf(dummy, 0, fmt, ap);
510 va_end(ap);
512 p = kmalloc(len + 1, GFP_KERNEL);
513 if (!p)
514 return NULL;
515 va_start(ap, fmt);
516 vsprintf(p, fmt, ap);
517 va_end(ap);
518 return p;
519 }
521 static ssize_t xendev_show_nodename(struct device *dev,
522 struct device_attribute *attr, char *buf)
523 {
524 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
525 }
526 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
528 static ssize_t xendev_show_devtype(struct device *dev,
529 struct device_attribute *attr, char *buf)
530 {
531 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
532 }
533 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
536 static int xenbus_probe_node(struct xen_bus_type *bus,
537 const char *type,
538 const char *nodename)
539 {
540 int err;
541 struct xenbus_device *xendev;
542 size_t stringlen;
543 char *tmpstring;
545 enum xenbus_state state = xenbus_read_driver_state(nodename);
547 if (state != XenbusStateInitialising) {
548 /* Device is not new, so ignore it. This can happen if a
549 device is going away after switching to Closed. */
550 return 0;
551 }
553 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
554 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
555 if (!xendev)
556 return -ENOMEM;
558 /* Copy the strings into the extra space. */
560 tmpstring = (char *)(xendev + 1);
561 strcpy(tmpstring, nodename);
562 xendev->nodename = tmpstring;
564 tmpstring += strlen(tmpstring) + 1;
565 strcpy(tmpstring, type);
566 xendev->devicetype = tmpstring;
568 xendev->dev.parent = &bus->dev;
569 xendev->dev.bus = &bus->bus;
570 xendev->dev.release = xenbus_dev_release;
572 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
573 if (err)
574 goto fail;
576 /* Register with generic device framework. */
577 err = device_register(&xendev->dev);
578 if (err)
579 goto fail;
581 device_create_file(&xendev->dev, &dev_attr_nodename);
582 device_create_file(&xendev->dev, &dev_attr_devtype);
584 return 0;
585 fail:
586 kfree(xendev);
587 return err;
588 }
590 /* device/<typename>/<name> */
591 static int xenbus_probe_frontend(const char *type, const char *name)
592 {
593 char *nodename;
594 int err;
596 nodename = kasprintf("%s/%s/%s", xenbus_frontend.root, type, name);
597 if (!nodename)
598 return -ENOMEM;
600 DPRINTK("%s", nodename);
602 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
603 kfree(nodename);
604 return err;
605 }
607 /* backend/<typename>/<frontend-uuid>/<name> */
608 static int xenbus_probe_backend_unit(const char *dir,
609 const char *type,
610 const char *name)
611 {
612 char *nodename;
613 int err;
615 nodename = kasprintf("%s/%s", dir, name);
616 if (!nodename)
617 return -ENOMEM;
619 DPRINTK("%s\n", nodename);
621 err = xenbus_probe_node(&xenbus_backend, type, nodename);
622 kfree(nodename);
623 return err;
624 }
626 /* backend/<typename>/<frontend-domid> */
627 static int xenbus_probe_backend(const char *type, const char *domid)
628 {
629 char *nodename;
630 int err = 0;
631 char **dir;
632 unsigned int i, dir_n = 0;
634 DPRINTK("");
636 nodename = kasprintf("%s/%s/%s", xenbus_backend.root, type, domid);
637 if (!nodename)
638 return -ENOMEM;
640 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
641 if (IS_ERR(dir)) {
642 kfree(nodename);
643 return PTR_ERR(dir);
644 }
646 for (i = 0; i < dir_n; i++) {
647 err = xenbus_probe_backend_unit(nodename, type, dir[i]);
648 if (err)
649 break;
650 }
651 kfree(dir);
652 kfree(nodename);
653 return err;
654 }
656 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
657 {
658 int err = 0;
659 char **dir;
660 unsigned int dir_n = 0;
661 int i;
663 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
664 if (IS_ERR(dir))
665 return PTR_ERR(dir);
667 for (i = 0; i < dir_n; i++) {
668 err = bus->probe(type, dir[i]);
669 if (err)
670 break;
671 }
672 kfree(dir);
673 return err;
674 }
676 static int xenbus_probe_devices(struct xen_bus_type *bus)
677 {
678 int err = 0;
679 char **dir;
680 unsigned int i, dir_n;
682 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
683 if (IS_ERR(dir))
684 return PTR_ERR(dir);
686 for (i = 0; i < dir_n; i++) {
687 err = xenbus_probe_device_type(bus, dir[i]);
688 if (err)
689 break;
690 }
691 kfree(dir);
692 return err;
693 }
695 static unsigned int char_count(const char *str, char c)
696 {
697 unsigned int i, ret = 0;
699 for (i = 0; str[i]; i++)
700 if (str[i] == c)
701 ret++;
702 return ret;
703 }
705 static int strsep_len(const char *str, char c, unsigned int len)
706 {
707 unsigned int i;
709 for (i = 0; str[i]; i++)
710 if (str[i] == c) {
711 if (len == 0)
712 return i;
713 len--;
714 }
715 return (len == 0) ? i : -ERANGE;
716 }
718 static void dev_changed(const char *node, struct xen_bus_type *bus)
719 {
720 int exists, rootlen;
721 struct xenbus_device *dev;
722 char type[BUS_ID_SIZE];
723 const char *p, *root;
725 if (char_count(node, '/') < 2)
726 return;
728 exists = xenbus_exists(XBT_NIL, node, "");
729 if (!exists) {
730 xenbus_cleanup_devices(node, &bus->bus);
731 return;
732 }
734 /* backend/<type>/... or device/<type>/... */
735 p = strchr(node, '/') + 1;
736 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
737 type[BUS_ID_SIZE-1] = '\0';
739 rootlen = strsep_len(node, '/', bus->levels);
740 if (rootlen < 0)
741 return;
742 root = kasprintf("%.*s", rootlen, node);
743 if (!root)
744 return;
746 dev = xenbus_device_find(root, &bus->bus);
747 if (!dev)
748 xenbus_probe_node(bus, type, root);
749 else
750 put_device(&dev->dev);
752 kfree(root);
753 }
755 static void frontend_changed(struct xenbus_watch *watch,
756 const char **vec, unsigned int len)
757 {
758 DPRINTK("");
760 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
761 }
763 static void backend_changed(struct xenbus_watch *watch,
764 const char **vec, unsigned int len)
765 {
766 DPRINTK("");
768 dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
769 }
771 /* We watch for devices appearing and vanishing. */
772 static struct xenbus_watch fe_watch = {
773 .node = "device",
774 .callback = frontend_changed,
775 };
777 static struct xenbus_watch be_watch = {
778 .node = "backend",
779 .callback = backend_changed,
780 };
782 static int suspend_dev(struct device *dev, void *data)
783 {
784 int err = 0;
785 struct xenbus_driver *drv;
786 struct xenbus_device *xdev;
788 DPRINTK("");
790 if (dev->driver == NULL)
791 return 0;
792 drv = to_xenbus_driver(dev->driver);
793 xdev = container_of(dev, struct xenbus_device, dev);
794 if (drv->suspend)
795 err = drv->suspend(xdev);
796 if (err)
797 printk(KERN_WARNING
798 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
799 return 0;
800 }
802 static int resume_dev(struct device *dev, void *data)
803 {
804 int err;
805 struct xenbus_driver *drv;
806 struct xenbus_device *xdev;
808 DPRINTK("");
810 if (dev->driver == NULL)
811 return 0;
813 drv = to_xenbus_driver(dev->driver);
814 xdev = container_of(dev, struct xenbus_device, dev);
816 err = talk_to_otherend(xdev);
817 if (err) {
818 printk(KERN_WARNING
819 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
820 dev->bus_id, err);
821 return err;
822 }
824 xdev->state = XenbusStateInitialising;
826 if (drv->resume) {
827 err = drv->resume(xdev);
828 if (err) {
829 printk(KERN_WARNING
830 "xenbus: resume %s failed: %i\n",
831 dev->bus_id, err);
832 return err;
833 }
834 }
836 err = watch_otherend(xdev);
837 if (err) {
838 printk(KERN_WARNING
839 "xenbus_probe: resume (watch_otherend) %s failed: "
840 "%d.\n", dev->bus_id, err);
841 return err;
842 }
844 return 0;
845 }
847 void xenbus_suspend(void)
848 {
849 DPRINTK("");
851 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
852 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, suspend_dev);
853 xs_suspend();
854 }
855 EXPORT_SYMBOL_GPL(xenbus_suspend);
857 void xenbus_resume(void)
858 {
859 xb_init_comms();
860 xs_resume();
861 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
862 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL, resume_dev);
863 }
864 EXPORT_SYMBOL_GPL(xenbus_resume);
867 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
868 int xenstored_ready = 0;
871 int register_xenstore_notifier(struct notifier_block *nb)
872 {
873 int ret = 0;
875 if (xenstored_ready > 0)
876 ret = nb->notifier_call(nb, 0, NULL);
877 else
878 notifier_chain_register(&xenstore_chain, nb);
880 return ret;
881 }
882 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
884 void unregister_xenstore_notifier(struct notifier_block *nb)
885 {
886 notifier_chain_unregister(&xenstore_chain, nb);
887 }
888 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
891 void xenbus_probe(void *unused)
892 {
893 BUG_ON((xenstored_ready <= 0));
895 /* Enumerate devices in xenstore. */
896 xenbus_probe_devices(&xenbus_frontend);
897 xenbus_probe_devices(&xenbus_backend);
899 /* Watch for changes. */
900 register_xenbus_watch(&fe_watch);
901 register_xenbus_watch(&be_watch);
903 /* Notify others that xenstore is up */
904 notifier_call_chain(&xenstore_chain, 0, NULL);
905 }
908 #ifdef CONFIG_PROC_FS
909 static struct file_operations xsd_kva_fops;
910 static struct proc_dir_entry *xsd_kva_intf;
911 static struct proc_dir_entry *xsd_port_intf;
913 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
914 {
915 size_t size = vma->vm_end - vma->vm_start;
917 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
918 return -EINVAL;
920 if (remap_pfn_range(vma, vma->vm_start,
921 mfn_to_pfn(xen_start_info->store_mfn),
922 size, vma->vm_page_prot))
923 return -EAGAIN;
925 return 0;
926 }
928 static int xsd_kva_read(char *page, char **start, off_t off,
929 int count, int *eof, void *data)
930 {
931 int len;
933 len = sprintf(page, "0x%p", mfn_to_virt(xen_start_info->store_mfn));
934 *eof = 1;
935 return len;
936 }
938 static int xsd_port_read(char *page, char **start, off_t off,
939 int count, int *eof, void *data)
940 {
941 int len;
943 len = sprintf(page, "%d", xen_start_info->store_evtchn);
944 *eof = 1;
945 return len;
946 }
947 #endif
950 static int __init xenbus_probe_init(void)
951 {
952 int err = 0, dom0;
953 unsigned long page = 0;
955 DPRINTK("");
957 if (!is_running_on_xen())
958 return -ENODEV;
960 /* Register ourselves with the kernel bus subsystem */
961 bus_register(&xenbus_frontend.bus);
962 bus_register(&xenbus_backend.bus);
964 /*
965 * Domain0 doesn't have a store_evtchn or store_mfn yet.
966 */
967 dom0 = (xen_start_info->store_evtchn == 0);
969 if (dom0) {
970 struct evtchn_alloc_unbound alloc_unbound;
972 /* Allocate page. */
973 page = get_zeroed_page(GFP_KERNEL);
974 if (!page)
975 return -ENOMEM;
977 xen_start_info->store_mfn =
978 pfn_to_mfn(virt_to_phys((void *)page) >>
979 PAGE_SHIFT);
981 /* Next allocate a local port which xenstored can bind to */
982 alloc_unbound.dom = DOMID_SELF;
983 alloc_unbound.remote_dom = 0;
985 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
986 &alloc_unbound);
987 if (err == -ENOSYS)
988 goto err;
989 BUG_ON(err);
990 xen_start_info->store_evtchn = alloc_unbound.port;
992 #ifdef CONFIG_PROC_FS
993 /* And finally publish the above info in /proc/xen */
994 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
995 if (xsd_kva_intf) {
996 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
997 sizeof(xsd_kva_fops));
998 xsd_kva_fops.mmap = xsd_kva_mmap;
999 xsd_kva_intf->proc_fops = &xsd_kva_fops;
1000 xsd_kva_intf->read_proc = xsd_kva_read;
1002 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
1003 if (xsd_port_intf)
1004 xsd_port_intf->read_proc = xsd_port_read;
1005 #endif
1006 } else
1007 xenstored_ready = 1;
1009 /* Initialize the interface to xenstore. */
1010 err = xs_init();
1011 if (err) {
1012 printk(KERN_WARNING
1013 "XENBUS: Error initializing xenstore comms: %i\n", err);
1014 goto err;
1017 /* Register ourselves with the kernel device subsystem */
1018 device_register(&xenbus_frontend.dev);
1019 device_register(&xenbus_backend.dev);
1021 if (!dom0)
1022 xenbus_probe(NULL);
1024 return 0;
1026 err:
1027 if (page)
1028 free_page(page);
1030 /*
1031 * Do not unregister the xenbus front/backend buses here. The buses
1032 * must exist because front/backend drivers will use them when they are
1033 * registered.
1034 */
1036 return err;
1039 postcore_initcall(xenbus_probe_init);
1042 static int is_disconnected_device(struct device *dev, void *data)
1044 struct xenbus_device *xendev = to_xenbus_device(dev);
1046 /*
1047 * A device with no driver will never connect. We care only about
1048 * devices which should currently be in the process of connecting.
1049 */
1050 if (!dev->driver)
1051 return 0;
1053 return (xendev->state != XenbusStateConnected);
1056 static int exists_disconnected_device(void)
1058 return bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL,
1059 is_disconnected_device);
1062 static int print_device_status(struct device *dev, void *data)
1064 struct xenbus_device *xendev = to_xenbus_device(dev);
1066 if (!dev->driver) {
1067 /* Information only: is this too noisy? */
1068 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
1069 xendev->nodename);
1070 } else if (xendev->state != XenbusStateConnected) {
1071 printk(KERN_WARNING "XENBUS: Timeout connecting "
1072 "to device: %s (state %d)\n",
1073 xendev->nodename, xendev->state);
1076 return 0;
1079 /*
1080 * On a 10 second timeout, wait for all devices currently configured. We need
1081 * to do this to guarantee that the filesystems and / or network devices
1082 * needed for boot are available, before we can allow the boot to proceed.
1084 * This needs to be on a late_initcall, to happen after the frontend device
1085 * drivers have been initialised, but before the root fs is mounted.
1087 * A possible improvement here would be to have the tools add a per-device
1088 * flag to the store entry, indicating whether it is needed at boot time.
1089 * This would allow people who knew what they were doing to accelerate their
1090 * boot slightly, but of course needs tools or manual intervention to set up
1091 * those flags correctly.
1092 */
1093 static int __init wait_for_devices(void)
1095 unsigned long timeout = jiffies + 10*HZ;
1097 if (!is_running_on_xen())
1098 return -ENODEV;
1100 while (time_before(jiffies, timeout) && exists_disconnected_device())
1101 schedule_timeout_interruptible(HZ/10);
1103 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL,
1104 print_device_status);
1106 return 0;
1109 late_initcall(wait_for_devices);