xenbus_probe_backend.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /******************************************************************************
  2. * Talks to Xen Store to figure out what devices we have (backend half).
  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. * Copyright (C) 2007 Solarflare Communications, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation; or, when distributed
  12. * separately from the Linux kernel or incorporated into other
  13. * software packages, subject to the following license:
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a copy
  16. * of this source file (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy, modify,
  18. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  19. * and to permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be included in
  23. * all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  31. * IN THE SOFTWARE.
  32. */
  33. #define DPRINTK(fmt, args...) \
  34. pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
  35. __func__, __LINE__, ##args)
  36. #include <linux/kernel.h>
  37. #include <linux/err.h>
  38. #include <linux/string.h>
  39. #include <linux/ctype.h>
  40. #include <linux/fcntl.h>
  41. #include <linux/mm.h>
  42. #include <linux/notifier.h>
  43. #include <asm/page.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/xen/hypervisor.h>
  46. #include <asm/hypervisor.h>
  47. #include <xen/xenbus.h>
  48. #include <xen/features.h>
  49. #include "xenbus_comms.h"
  50. #include "xenbus_probe.h"
  51. /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
  52. static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
  53. {
  54. int domid, err;
  55. const char *devid, *type, *frontend;
  56. unsigned int typelen;
  57. type = strchr(nodename, '/');
  58. if (!type)
  59. return -EINVAL;
  60. type++;
  61. typelen = strcspn(type, "/");
  62. if (!typelen || type[typelen] != '/')
  63. return -EINVAL;
  64. devid = strrchr(nodename, '/') + 1;
  65. err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
  66. "frontend", NULL, &frontend,
  67. NULL);
  68. if (err)
  69. return err;
  70. if (strlen(frontend) == 0)
  71. err = -ERANGE;
  72. if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
  73. err = -ENOENT;
  74. kfree(frontend);
  75. if (err)
  76. return err;
  77. if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
  78. typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
  79. return -ENOSPC;
  80. return 0;
  81. }
  82. static int xenbus_uevent_backend(struct device *dev,
  83. struct kobj_uevent_env *env)
  84. {
  85. struct xenbus_device *xdev;
  86. struct xenbus_driver *drv;
  87. struct xen_bus_type *bus;
  88. DPRINTK("");
  89. if (dev == NULL)
  90. return -ENODEV;
  91. xdev = to_xenbus_device(dev);
  92. bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
  93. if (xdev == NULL)
  94. return -ENODEV;
  95. /* stuff we want to pass to /sbin/hotplug */
  96. if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
  97. return -ENOMEM;
  98. if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
  99. return -ENOMEM;
  100. if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
  101. return -ENOMEM;
  102. if (dev->driver) {
  103. drv = to_xenbus_driver(dev->driver);
  104. if (drv && drv->uevent)
  105. return drv->uevent(xdev, env);
  106. }
  107. return 0;
  108. }
  109. /* backend/<typename>/<frontend-uuid>/<name> */
  110. static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
  111. const char *dir,
  112. const char *type,
  113. const char *name)
  114. {
  115. char *nodename;
  116. int err;
  117. nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
  118. if (!nodename)
  119. return -ENOMEM;
  120. DPRINTK("%s\n", nodename);
  121. err = xenbus_probe_node(bus, type, nodename);
  122. kfree(nodename);
  123. return err;
  124. }
  125. /* backend/<typename>/<frontend-domid> */
  126. static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
  127. const char *domid)
  128. {
  129. char *nodename;
  130. int err = 0;
  131. char **dir;
  132. unsigned int i, dir_n = 0;
  133. DPRINTK("");
  134. nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
  135. if (!nodename)
  136. return -ENOMEM;
  137. dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
  138. if (IS_ERR(dir)) {
  139. kfree(nodename);
  140. return PTR_ERR(dir);
  141. }
  142. for (i = 0; i < dir_n; i++) {
  143. err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
  144. if (err)
  145. break;
  146. }
  147. kfree(dir);
  148. kfree(nodename);
  149. return err;
  150. }
  151. static void frontend_changed(struct xenbus_watch *watch,
  152. const char **vec, unsigned int len)
  153. {
  154. xenbus_otherend_changed(watch, vec, len, 0);
  155. }
  156. static struct device_attribute xenbus_backend_dev_attrs[] = {
  157. __ATTR_NULL
  158. };
  159. static struct xen_bus_type xenbus_backend = {
  160. .root = "backend",
  161. .levels = 3, /* backend/type/<frontend>/<id> */
  162. .get_bus_id = backend_bus_id,
  163. .probe = xenbus_probe_backend,
  164. .otherend_changed = frontend_changed,
  165. .bus = {
  166. .name = "xen-backend",
  167. .match = xenbus_match,
  168. .uevent = xenbus_uevent_backend,
  169. .probe = xenbus_dev_probe,
  170. .remove = xenbus_dev_remove,
  171. .shutdown = xenbus_dev_shutdown,
  172. .dev_attrs = xenbus_backend_dev_attrs,
  173. },
  174. };
  175. static void backend_changed(struct xenbus_watch *watch,
  176. const char **vec, unsigned int len)
  177. {
  178. DPRINTK("");
  179. xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
  180. }
  181. static struct xenbus_watch be_watch = {
  182. .node = "backend",
  183. .callback = backend_changed,
  184. };
  185. static int read_frontend_details(struct xenbus_device *xendev)
  186. {
  187. return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
  188. }
  189. int xenbus_dev_is_online(struct xenbus_device *dev)
  190. {
  191. int rc, val;
  192. rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val);
  193. if (rc != 1)
  194. val = 0; /* no online node present */
  195. return val;
  196. }
  197. EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
  198. int __xenbus_register_backend(struct xenbus_driver *drv,
  199. struct module *owner, const char *mod_name)
  200. {
  201. drv->read_otherend_details = read_frontend_details;
  202. return xenbus_register_driver_common(drv, &xenbus_backend,
  203. owner, mod_name);
  204. }
  205. EXPORT_SYMBOL_GPL(__xenbus_register_backend);
  206. static int backend_probe_and_watch(struct notifier_block *notifier,
  207. unsigned long event,
  208. void *data)
  209. {
  210. /* Enumerate devices in xenstore and watch for changes. */
  211. xenbus_probe_devices(&xenbus_backend);
  212. register_xenbus_watch(&be_watch);
  213. return NOTIFY_DONE;
  214. }
  215. static int __init xenbus_probe_backend_init(void)
  216. {
  217. static struct notifier_block xenstore_notifier = {
  218. .notifier_call = backend_probe_and_watch
  219. };
  220. int err;
  221. DPRINTK("");
  222. /* Register ourselves with the kernel bus subsystem */
  223. err = bus_register(&xenbus_backend.bus);
  224. if (err)
  225. return err;
  226. register_xenstore_notifier(&xenstore_notifier);
  227. return 0;
  228. }
  229. subsys_initcall(xenbus_probe_backend_init);