ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/xenbus/xenbus_probe.c @ 13026:79315be2c9b9

[NET] front: Fix crashes when xenstore watches fire multiple times.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Dec 14 12:21:28 2006 +0000 (2006-12-14)
parents c242b6d6a64a
children 4fad820a2233
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>
45 #include <linux/mutex.h>
47 #include <asm/io.h>
48 #include <asm/page.h>
49 #include <asm/maddr.h>
50 #include <asm/pgtable.h>
51 #include <asm/hypervisor.h>
52 #include <xen/xenbus.h>
53 #include <xen/xen_proc.h>
54 #include <xen/evtchn.h>
55 #include <xen/features.h>
56 #include <xen/hvm.h>
58 #include "xenbus_comms.h"
59 #include "xenbus_probe.h"
61 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
62 #include <xen/platform-compat.h>
63 #endif
65 int xen_store_evtchn;
66 struct xenstore_domain_interface *xen_store_interface;
67 static unsigned long xen_store_mfn;
69 extern struct mutex xenwatch_mutex;
71 static struct notifier_block *xenstore_chain;
73 static void wait_for_devices(struct xenbus_driver *xendrv);
75 static int xenbus_probe_frontend(const char *type, const char *name);
77 static void xenbus_dev_shutdown(struct device *_dev);
79 /* If something in array of ids matches this device, return it. */
80 static const struct xenbus_device_id *
81 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
82 {
83 for (; *arr->devicetype != '\0'; arr++) {
84 if (!strcmp(arr->devicetype, dev->devicetype))
85 return arr;
86 }
87 return NULL;
88 }
90 int xenbus_match(struct device *_dev, struct device_driver *_drv)
91 {
92 struct xenbus_driver *drv = to_xenbus_driver(_drv);
94 if (!drv->ids)
95 return 0;
97 return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
98 }
100 /* device/<type>/<id> => <type>-<id> */
101 static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
102 {
103 nodename = strchr(nodename, '/');
104 if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
105 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
106 return -EINVAL;
107 }
109 strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
110 if (!strchr(bus_id, '/')) {
111 printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
112 return -EINVAL;
113 }
114 *strchr(bus_id, '/') = '-';
115 return 0;
116 }
119 static void free_otherend_details(struct xenbus_device *dev)
120 {
121 kfree(dev->otherend);
122 dev->otherend = NULL;
123 }
126 static void free_otherend_watch(struct xenbus_device *dev)
127 {
128 if (dev->otherend_watch.node) {
129 unregister_xenbus_watch(&dev->otherend_watch);
130 kfree(dev->otherend_watch.node);
131 dev->otherend_watch.node = NULL;
132 }
133 }
136 int read_otherend_details(struct xenbus_device *xendev,
137 char *id_node, char *path_node)
138 {
139 int err = xenbus_gather(XBT_NIL, xendev->nodename,
140 id_node, "%i", &xendev->otherend_id,
141 path_node, NULL, &xendev->otherend,
142 NULL);
143 if (err) {
144 xenbus_dev_fatal(xendev, err,
145 "reading other end details from %s",
146 xendev->nodename);
147 return err;
148 }
149 if (strlen(xendev->otherend) == 0 ||
150 !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
151 xenbus_dev_fatal(xendev, -ENOENT,
152 "unable to read other end from %s. "
153 "missing or inaccessible.",
154 xendev->nodename);
155 free_otherend_details(xendev);
156 return -ENOENT;
157 }
159 return 0;
160 }
163 static int read_backend_details(struct xenbus_device *xendev)
164 {
165 return read_otherend_details(xendev, "backend-id", "backend");
166 }
169 /* Bus type for frontend drivers. */
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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
179 .probe = xenbus_dev_probe,
180 .remove = xenbus_dev_remove,
181 .shutdown = xenbus_dev_shutdown,
182 #endif
183 },
184 .dev = {
185 .bus_id = "xen",
186 },
187 };
189 static void otherend_changed(struct xenbus_watch *watch,
190 const char **vec, unsigned int len)
191 {
192 struct xenbus_device *dev =
193 container_of(watch, struct xenbus_device, otherend_watch);
194 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
195 enum xenbus_state state;
197 /* Protect us against watches firing on old details when the otherend
198 details change, say immediately after a resume. */
199 if (!dev->otherend ||
200 strncmp(dev->otherend, vec[XS_WATCH_PATH],
201 strlen(dev->otherend))) {
202 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
203 return;
204 }
206 state = xenbus_read_driver_state(dev->otherend);
208 DPRINTK("state is %d (%s), %s, %s", state, xenbus_strstate(state),
209 dev->otherend_watch.node, vec[XS_WATCH_PATH]);
211 /*
212 * Ignore xenbus transitions during shutdown. This prevents us doing
213 * work that can fail e.g., when the rootfs is gone.
214 */
215 if (system_state > SYSTEM_RUNNING) {
216 struct xen_bus_type *bus = bus;
217 bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
218 /* If we're frontend, drive the state machine to Closed. */
219 /* This should cause the backend to release our resources. */
220 if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
221 xenbus_frontend_closed(dev);
222 return;
223 }
225 if (drv->otherend_changed)
226 drv->otherend_changed(dev, state);
227 }
230 static int talk_to_otherend(struct xenbus_device *dev)
231 {
232 struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
234 free_otherend_watch(dev);
235 free_otherend_details(dev);
237 return drv->read_otherend_details(dev);
238 }
241 static int watch_otherend(struct xenbus_device *dev)
242 {
243 return xenbus_watch_path2(dev, dev->otherend, "state",
244 &dev->otherend_watch, otherend_changed);
245 }
248 int xenbus_dev_probe(struct device *_dev)
249 {
250 struct xenbus_device *dev = to_xenbus_device(_dev);
251 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
252 const struct xenbus_device_id *id;
253 int err;
255 DPRINTK("%s", dev->nodename);
257 if (!drv->probe) {
258 err = -ENODEV;
259 goto fail;
260 }
262 id = match_device(drv->ids, dev);
263 if (!id) {
264 err = -ENODEV;
265 goto fail;
266 }
268 err = talk_to_otherend(dev);
269 if (err) {
270 printk(KERN_WARNING
271 "xenbus_probe: talk_to_otherend on %s failed.\n",
272 dev->nodename);
273 return err;
274 }
276 err = drv->probe(dev, id);
277 if (err)
278 goto fail;
280 err = watch_otherend(dev);
281 if (err) {
282 printk(KERN_WARNING
283 "xenbus_probe: watch_otherend on %s failed.\n",
284 dev->nodename);
285 return err;
286 }
288 return 0;
289 fail:
290 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
291 xenbus_switch_state(dev, XenbusStateClosed);
292 return -ENODEV;
293 }
295 int xenbus_dev_remove(struct device *_dev)
296 {
297 struct xenbus_device *dev = to_xenbus_device(_dev);
298 struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
300 DPRINTK("%s", dev->nodename);
302 free_otherend_watch(dev);
303 free_otherend_details(dev);
305 if (drv->remove)
306 drv->remove(dev);
308 xenbus_switch_state(dev, XenbusStateClosed);
309 return 0;
310 }
312 static void xenbus_dev_shutdown(struct device *_dev)
313 {
314 struct xenbus_device *dev = to_xenbus_device(_dev);
315 unsigned long timeout = 5*HZ;
317 DPRINTK("%s", dev->nodename);
319 get_device(&dev->dev);
320 if (dev->state != XenbusStateConnected) {
321 printk("%s: %s: %s != Connected, skipping\n", __FUNCTION__,
322 dev->nodename, xenbus_strstate(dev->state));
323 goto out;
324 }
325 xenbus_switch_state(dev, XenbusStateClosing);
326 timeout = wait_for_completion_timeout(&dev->down, timeout);
327 if (!timeout)
328 printk("%s: %s timeout closing device\n", __FUNCTION__, dev->nodename);
329 out:
330 put_device(&dev->dev);
331 }
333 int xenbus_register_driver_common(struct xenbus_driver *drv,
334 struct xen_bus_type *bus)
335 {
336 int ret;
338 drv->driver.name = drv->name;
339 drv->driver.bus = &bus->bus;
340 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
341 drv->driver.owner = drv->owner;
342 #endif
343 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
344 drv->driver.probe = xenbus_dev_probe;
345 drv->driver.remove = xenbus_dev_remove;
346 drv->driver.shutdown = xenbus_dev_shutdown;
347 #endif
349 mutex_lock(&xenwatch_mutex);
350 ret = driver_register(&drv->driver);
351 mutex_unlock(&xenwatch_mutex);
352 return ret;
353 }
355 int xenbus_register_frontend(struct xenbus_driver *drv)
356 {
357 int ret;
359 drv->read_otherend_details = read_backend_details;
361 ret = xenbus_register_driver_common(drv, &xenbus_frontend);
362 if (ret)
363 return ret;
365 /* If this driver is loaded as a module wait for devices to attach. */
366 wait_for_devices(drv);
368 return 0;
369 }
370 EXPORT_SYMBOL_GPL(xenbus_register_frontend);
372 void xenbus_unregister_driver(struct xenbus_driver *drv)
373 {
374 driver_unregister(&drv->driver);
375 }
376 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
378 struct xb_find_info
379 {
380 struct xenbus_device *dev;
381 const char *nodename;
382 };
384 static int cmp_dev(struct device *dev, void *data)
385 {
386 struct xenbus_device *xendev = to_xenbus_device(dev);
387 struct xb_find_info *info = data;
389 if (!strcmp(xendev->nodename, info->nodename)) {
390 info->dev = xendev;
391 get_device(dev);
392 return 1;
393 }
394 return 0;
395 }
397 struct xenbus_device *xenbus_device_find(const char *nodename,
398 struct bus_type *bus)
399 {
400 struct xb_find_info info = { .dev = NULL, .nodename = nodename };
402 bus_for_each_dev(bus, NULL, &info, cmp_dev);
403 return info.dev;
404 }
406 static int cleanup_dev(struct device *dev, void *data)
407 {
408 struct xenbus_device *xendev = to_xenbus_device(dev);
409 struct xb_find_info *info = data;
410 int len = strlen(info->nodename);
412 DPRINTK("%s", info->nodename);
414 /* Match the info->nodename path, or any subdirectory of that path. */
415 if (strncmp(xendev->nodename, info->nodename, len))
416 return 0;
418 /* If the node name is longer, ensure it really is a subdirectory. */
419 if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
420 return 0;
422 info->dev = xendev;
423 get_device(dev);
424 return 1;
425 }
427 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
428 {
429 struct xb_find_info info = { .nodename = path };
431 do {
432 info.dev = NULL;
433 bus_for_each_dev(bus, NULL, &info, cleanup_dev);
434 if (info.dev) {
435 device_unregister(&info.dev->dev);
436 put_device(&info.dev->dev);
437 }
438 } while (info.dev);
439 }
441 static void xenbus_dev_release(struct device *dev)
442 {
443 if (dev)
444 kfree(to_xenbus_device(dev));
445 }
447 static ssize_t xendev_show_nodename(struct device *dev,
448 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
449 struct device_attribute *attr,
450 #endif
451 char *buf)
452 {
453 return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
454 }
455 DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
457 static ssize_t xendev_show_devtype(struct device *dev,
458 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
459 struct device_attribute *attr,
460 #endif
461 char *buf)
462 {
463 return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
464 }
465 DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
468 int xenbus_probe_node(struct xen_bus_type *bus,
469 const char *type,
470 const char *nodename)
471 {
472 int err;
473 struct xenbus_device *xendev;
474 size_t stringlen;
475 char *tmpstring;
477 enum xenbus_state state = xenbus_read_driver_state(nodename);
479 if (state != XenbusStateInitialising) {
480 /* Device is not new, so ignore it. This can happen if a
481 device is going away after switching to Closed. */
482 return 0;
483 }
485 stringlen = strlen(nodename) + 1 + strlen(type) + 1;
486 xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
487 if (!xendev)
488 return -ENOMEM;
490 xendev->state = XenbusStateInitialising;
492 /* Copy the strings into the extra space. */
494 tmpstring = (char *)(xendev + 1);
495 strcpy(tmpstring, nodename);
496 xendev->nodename = tmpstring;
498 tmpstring += strlen(tmpstring) + 1;
499 strcpy(tmpstring, type);
500 xendev->devicetype = tmpstring;
501 init_completion(&xendev->down);
503 xendev->dev.parent = &bus->dev;
504 xendev->dev.bus = &bus->bus;
505 xendev->dev.release = xenbus_dev_release;
507 err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
508 if (err)
509 goto fail;
511 /* Register with generic device framework. */
512 err = device_register(&xendev->dev);
513 if (err)
514 goto fail;
516 device_create_file(&xendev->dev, &dev_attr_nodename);
517 device_create_file(&xendev->dev, &dev_attr_devtype);
519 return 0;
520 fail:
521 kfree(xendev);
522 return err;
523 }
525 /* device/<typename>/<name> */
526 static int xenbus_probe_frontend(const char *type, const char *name)
527 {
528 char *nodename;
529 int err;
531 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", xenbus_frontend.root, type, name);
532 if (!nodename)
533 return -ENOMEM;
535 DPRINTK("%s", nodename);
537 err = xenbus_probe_node(&xenbus_frontend, type, nodename);
538 kfree(nodename);
539 return err;
540 }
542 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
543 {
544 int err = 0;
545 char **dir;
546 unsigned int dir_n = 0;
547 int i;
549 dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
550 if (IS_ERR(dir))
551 return PTR_ERR(dir);
553 for (i = 0; i < dir_n; i++) {
554 err = bus->probe(type, dir[i]);
555 if (err)
556 break;
557 }
558 kfree(dir);
559 return err;
560 }
562 int xenbus_probe_devices(struct xen_bus_type *bus)
563 {
564 int err = 0;
565 char **dir;
566 unsigned int i, dir_n;
568 dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
569 if (IS_ERR(dir))
570 return PTR_ERR(dir);
572 for (i = 0; i < dir_n; i++) {
573 err = xenbus_probe_device_type(bus, dir[i]);
574 if (err)
575 break;
576 }
577 kfree(dir);
578 return err;
579 }
581 static unsigned int char_count(const char *str, char c)
582 {
583 unsigned int i, ret = 0;
585 for (i = 0; str[i]; i++)
586 if (str[i] == c)
587 ret++;
588 return ret;
589 }
591 static int strsep_len(const char *str, char c, unsigned int len)
592 {
593 unsigned int i;
595 for (i = 0; str[i]; i++)
596 if (str[i] == c) {
597 if (len == 0)
598 return i;
599 len--;
600 }
601 return (len == 0) ? i : -ERANGE;
602 }
604 void dev_changed(const char *node, struct xen_bus_type *bus)
605 {
606 int exists, rootlen;
607 struct xenbus_device *dev;
608 char type[BUS_ID_SIZE];
609 const char *p, *root;
611 if (char_count(node, '/') < 2)
612 return;
614 exists = xenbus_exists(XBT_NIL, node, "");
615 if (!exists) {
616 xenbus_cleanup_devices(node, &bus->bus);
617 return;
618 }
620 /* backend/<type>/... or device/<type>/... */
621 p = strchr(node, '/') + 1;
622 snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
623 type[BUS_ID_SIZE-1] = '\0';
625 rootlen = strsep_len(node, '/', bus->levels);
626 if (rootlen < 0)
627 return;
628 root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
629 if (!root)
630 return;
632 dev = xenbus_device_find(root, &bus->bus);
633 if (!dev)
634 xenbus_probe_node(bus, type, root);
635 else
636 put_device(&dev->dev);
638 kfree(root);
639 }
641 static void frontend_changed(struct xenbus_watch *watch,
642 const char **vec, unsigned int len)
643 {
644 DPRINTK("");
646 dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
647 }
649 /* We watch for devices appearing and vanishing. */
650 static struct xenbus_watch fe_watch = {
651 .node = "device",
652 .callback = frontend_changed,
653 };
655 static int suspend_dev(struct device *dev, void *data)
656 {
657 int err = 0;
658 struct xenbus_driver *drv;
659 struct xenbus_device *xdev;
661 DPRINTK("");
663 if (dev->driver == NULL)
664 return 0;
665 drv = to_xenbus_driver(dev->driver);
666 xdev = container_of(dev, struct xenbus_device, dev);
667 if (drv->suspend)
668 err = drv->suspend(xdev);
669 if (err)
670 printk(KERN_WARNING
671 "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
672 return 0;
673 }
675 static int resume_dev(struct device *dev, void *data)
676 {
677 int err;
678 struct xenbus_driver *drv;
679 struct xenbus_device *xdev;
681 DPRINTK("");
683 if (dev->driver == NULL)
684 return 0;
686 drv = to_xenbus_driver(dev->driver);
687 xdev = container_of(dev, struct xenbus_device, dev);
689 err = talk_to_otherend(xdev);
690 if (err) {
691 printk(KERN_WARNING
692 "xenbus: resume (talk_to_otherend) %s failed: %i\n",
693 dev->bus_id, err);
694 return err;
695 }
697 xdev->state = XenbusStateInitialising;
699 if (drv->resume) {
700 err = drv->resume(xdev);
701 if (err) {
702 printk(KERN_WARNING
703 "xenbus: resume %s failed: %i\n",
704 dev->bus_id, err);
705 return err;
706 }
707 }
709 err = watch_otherend(xdev);
710 if (err) {
711 printk(KERN_WARNING
712 "xenbus_probe: resume (watch_otherend) %s failed: "
713 "%d.\n", dev->bus_id, err);
714 return err;
715 }
717 return 0;
718 }
720 void xenbus_suspend(void)
721 {
722 DPRINTK("");
724 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
725 xenbus_backend_suspend(suspend_dev);
726 xs_suspend();
727 }
728 EXPORT_SYMBOL_GPL(xenbus_suspend);
730 void xenbus_resume(void)
731 {
732 xb_init_comms();
733 xs_resume();
734 bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
735 xenbus_backend_resume(resume_dev);
736 }
737 EXPORT_SYMBOL_GPL(xenbus_resume);
740 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
741 int xenstored_ready = 0;
744 int register_xenstore_notifier(struct notifier_block *nb)
745 {
746 int ret = 0;
748 if (xenstored_ready > 0)
749 ret = nb->notifier_call(nb, 0, NULL);
750 else
751 notifier_chain_register(&xenstore_chain, nb);
753 return ret;
754 }
755 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
757 void unregister_xenstore_notifier(struct notifier_block *nb)
758 {
759 notifier_chain_unregister(&xenstore_chain, nb);
760 }
761 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
764 void xenbus_probe(void *unused)
765 {
766 BUG_ON((xenstored_ready <= 0));
768 /* Enumerate devices in xenstore and watch for changes. */
769 xenbus_probe_devices(&xenbus_frontend);
770 register_xenbus_watch(&fe_watch);
771 xenbus_backend_probe_and_watch();
773 /* Notify others that xenstore is up */
774 notifier_call_chain(&xenstore_chain, 0, NULL);
775 }
778 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
779 static struct file_operations xsd_kva_fops;
780 static struct proc_dir_entry *xsd_kva_intf;
781 static struct proc_dir_entry *xsd_port_intf;
783 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
784 {
785 size_t size = vma->vm_end - vma->vm_start;
787 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
788 return -EINVAL;
790 if (remap_pfn_range(vma, vma->vm_start, mfn_to_pfn(xen_store_mfn),
791 size, vma->vm_page_prot))
792 return -EAGAIN;
794 return 0;
795 }
797 static int xsd_kva_read(char *page, char **start, off_t off,
798 int count, int *eof, void *data)
799 {
800 int len;
802 len = sprintf(page, "0x%p", xen_store_interface);
803 *eof = 1;
804 return len;
805 }
807 static int xsd_port_read(char *page, char **start, off_t off,
808 int count, int *eof, void *data)
809 {
810 int len;
812 len = sprintf(page, "%d", xen_store_evtchn);
813 *eof = 1;
814 return len;
815 }
816 #endif
818 static int __init xenbus_probe_init(void)
819 {
820 int err = 0;
821 unsigned long page = 0;
823 DPRINTK("");
825 if (!is_running_on_xen())
826 return -ENODEV;
828 /* Register ourselves with the kernel bus subsystem */
829 bus_register(&xenbus_frontend.bus);
830 xenbus_backend_bus_register();
832 /*
833 * Domain0 doesn't have a store_evtchn or store_mfn yet.
834 */
835 if (is_initial_xendomain()) {
836 struct evtchn_alloc_unbound alloc_unbound;
838 /* Allocate page. */
839 page = get_zeroed_page(GFP_KERNEL);
840 if (!page)
841 return -ENOMEM;
843 xen_store_mfn = xen_start_info->store_mfn =
844 pfn_to_mfn(virt_to_phys((void *)page) >>
845 PAGE_SHIFT);
847 /* Next allocate a local port which xenstored can bind to */
848 alloc_unbound.dom = DOMID_SELF;
849 alloc_unbound.remote_dom = 0;
851 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
852 &alloc_unbound);
853 if (err == -ENOSYS)
854 goto err;
855 BUG_ON(err);
856 xen_store_evtchn = xen_start_info->store_evtchn =
857 alloc_unbound.port;
859 #if defined(CONFIG_PROC_FS) && defined(CONFIG_XEN_PRIVILEGED_GUEST)
860 /* And finally publish the above info in /proc/xen */
861 xsd_kva_intf = create_xen_proc_entry("xsd_kva", 0600);
862 if (xsd_kva_intf) {
863 memcpy(&xsd_kva_fops, xsd_kva_intf->proc_fops,
864 sizeof(xsd_kva_fops));
865 xsd_kva_fops.mmap = xsd_kva_mmap;
866 xsd_kva_intf->proc_fops = &xsd_kva_fops;
867 xsd_kva_intf->read_proc = xsd_kva_read;
868 }
869 xsd_port_intf = create_xen_proc_entry("xsd_port", 0400);
870 if (xsd_port_intf)
871 xsd_port_intf->read_proc = xsd_port_read;
872 #endif
873 xen_store_interface = mfn_to_virt(xen_store_mfn);
874 } else {
875 xenstored_ready = 1;
876 #ifdef CONFIG_XEN
877 xen_store_evtchn = xen_start_info->store_evtchn;
878 xen_store_mfn = xen_start_info->store_mfn;
879 xen_store_interface = mfn_to_virt(xen_store_mfn);
880 #else
881 xen_store_evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
882 xen_store_mfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
883 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT,
884 PAGE_SIZE);
885 #endif
886 }
889 xenbus_dev_init();
891 /* Initialize the interface to xenstore. */
892 err = xs_init();
893 if (err) {
894 printk(KERN_WARNING
895 "XENBUS: Error initializing xenstore comms: %i\n", err);
896 goto err;
897 }
899 /* Register ourselves with the kernel device subsystem */
900 device_register(&xenbus_frontend.dev);
901 xenbus_backend_device_register();
903 if (!is_initial_xendomain())
904 xenbus_probe(NULL);
906 return 0;
908 err:
909 if (page)
910 free_page(page);
912 /*
913 * Do not unregister the xenbus front/backend buses here. The buses
914 * must exist because front/backend drivers will use them when they are
915 * registered.
916 */
918 return err;
919 }
921 postcore_initcall(xenbus_probe_init);
923 MODULE_LICENSE("Dual BSD/GPL");
926 static int is_disconnected_device(struct device *dev, void *data)
927 {
928 struct xenbus_device *xendev = to_xenbus_device(dev);
929 struct device_driver *drv = data;
931 /*
932 * A device with no driver will never connect. We care only about
933 * devices which should currently be in the process of connecting.
934 */
935 if (!dev->driver)
936 return 0;
938 /* Is this search limited to a particular driver? */
939 if (drv && (dev->driver != drv))
940 return 0;
942 return (xendev->state != XenbusStateConnected);
943 }
945 static int exists_disconnected_device(struct device_driver *drv)
946 {
947 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
948 is_disconnected_device);
949 }
951 static int print_device_status(struct device *dev, void *data)
952 {
953 struct xenbus_device *xendev = to_xenbus_device(dev);
954 struct device_driver *drv = data;
956 /* Is this operation limited to a particular driver? */
957 if (drv && (dev->driver != drv))
958 return 0;
960 if (!dev->driver) {
961 /* Information only: is this too noisy? */
962 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
963 xendev->nodename);
964 } else if (xendev->state != XenbusStateConnected) {
965 printk(KERN_WARNING "XENBUS: Timeout connecting "
966 "to device: %s (state %d)\n",
967 xendev->nodename, xendev->state);
968 }
970 return 0;
971 }
973 /* We only wait for device setup after most initcalls have run. */
974 static int ready_to_wait_for_devices;
976 /*
977 * On a 10 second timeout, wait for all devices currently configured. We need
978 * to do this to guarantee that the filesystems and / or network devices
979 * needed for boot are available, before we can allow the boot to proceed.
980 *
981 * This needs to be on a late_initcall, to happen after the frontend device
982 * drivers have been initialised, but before the root fs is mounted.
983 *
984 * A possible improvement here would be to have the tools add a per-device
985 * flag to the store entry, indicating whether it is needed at boot time.
986 * This would allow people who knew what they were doing to accelerate their
987 * boot slightly, but of course needs tools or manual intervention to set up
988 * those flags correctly.
989 */
990 static void wait_for_devices(struct xenbus_driver *xendrv)
991 {
992 unsigned long timeout = jiffies + 10*HZ;
993 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
995 if (!ready_to_wait_for_devices || !is_running_on_xen())
996 return;
998 while (exists_disconnected_device(drv)) {
999 if (time_after(jiffies, timeout))
1000 break;
1001 schedule_timeout_interruptible(HZ/10);
1004 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
1005 print_device_status);
1008 #ifndef MODULE
1009 static int __init boot_wait_for_devices(void)
1011 ready_to_wait_for_devices = 1;
1012 wait_for_devices(NULL);
1013 return 0;
1016 late_initcall(boot_wait_for_devices);
1017 #endif