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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #define DPRINTK(fmt, ...) \
  35. pr_debug("(%s:%d) " fmt "\n", \
  36. __func__, __LINE__, ##__VA_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/export.h>
  45. #include <asm/page.h>
  46. #include <asm/pgtable.h>
  47. #include <asm/xen/hypervisor.h>
  48. #include <asm/hypervisor.h>
  49. #include <xen/xenbus.h>
  50. #include <xen/features.h>
  51. #include "xenbus_comms.h"
  52. #include "xenbus_probe.h"
  53. /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
  54. static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
  55. {
  56. int domid, err;
  57. const char *devid, *type, *frontend;
  58. unsigned int typelen;
  59. type = strchr(nodename, '/');
  60. if (!type)
  61. return -EINVAL;
  62. type++;
  63. typelen = strcspn(type, "/");
  64. if (!typelen || type[typelen] != '/')
  65. return -EINVAL;
  66. devid = strrchr(nodename, '/') + 1;
  67. err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
  68. "frontend", NULL, &frontend,
  69. NULL);
  70. if (err)
  71. return err;
  72. if (strlen(frontend) == 0)
  73. err = -ERANGE;
  74. if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
  75. err = -ENOENT;
  76. kfree(frontend);
  77. if (err)
  78. return err;
  79. if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
  80. typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
  81. return -ENOSPC;
  82. return 0;
  83. }
  84. static int xenbus_uevent_backend(struct device *dev,
  85. struct kobj_uevent_env *env)
  86. {
  87. struct xenbus_device *xdev;
  88. struct xenbus_driver *drv;
  89. struct xen_bus_type *bus;
  90. DPRINTK("");
  91. if (dev == NULL)
  92. return -ENODEV;
  93. xdev = to_xenbus_device(dev);
  94. bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
  95. if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
  96. return -ENOMEM;
  97. /* stuff we want to pass to /sbin/hotplug */
  98. if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
  99. return -ENOMEM;
  100. if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
  101. return -ENOMEM;
  102. if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
  103. return -ENOMEM;
  104. if (dev->driver) {
  105. drv = to_xenbus_driver(dev->driver);
  106. if (drv && drv->uevent)
  107. return drv->uevent(xdev, env);
  108. }
  109. return 0;
  110. }
  111. /* backend/<typename>/<frontend-uuid>/<name> */
  112. static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
  113. const char *dir,
  114. const char *type,
  115. const char *name)
  116. {
  117. char *nodename;
  118. int err;
  119. nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
  120. if (!nodename)
  121. return -ENOMEM;
  122. DPRINTK("%s\n", nodename);
  123. err = xenbus_probe_node(bus, type, nodename);
  124. kfree(nodename);
  125. return err;
  126. }
  127. /* backend/<typename>/<frontend-domid> */
  128. static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
  129. const char *domid)
  130. {
  131. char *nodename;
  132. int err = 0;
  133. char **dir;
  134. unsigned int i, dir_n = 0;
  135. DPRINTK("");
  136. nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
  137. if (!nodename)
  138. return -ENOMEM;
  139. dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
  140. if (IS_ERR(dir)) {
  141. kfree(nodename);
  142. return PTR_ERR(dir);
  143. }
  144. for (i = 0; i < dir_n; i++) {
  145. err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
  146. if (err)
  147. break;
  148. }
  149. kfree(dir);
  150. kfree(nodename);
  151. return err;
  152. }
  153. static void frontend_changed(struct xenbus_watch *watch,
  154. const char **vec, unsigned int len)
  155. {
  156. xenbus_otherend_changed(watch, vec, len, 0);
  157. }
  158. static struct xen_bus_type xenbus_backend = {
  159. .root = "backend",
  160. .levels = 3, /* backend/type/<frontend>/<id> */
  161. .get_bus_id = backend_bus_id,
  162. .probe = xenbus_probe_backend,
  163. .otherend_changed = frontend_changed,
  164. .bus = {
  165. .name = "xen-backend",
  166. .match = xenbus_match,
  167. .uevent = xenbus_uevent_backend,
  168. .probe = xenbus_dev_probe,
  169. .remove = xenbus_dev_remove,
  170. .shutdown = xenbus_dev_shutdown,
  171. .dev_groups = xenbus_dev_groups,
  172. },
  173. };
  174. static void backend_changed(struct xenbus_watch *watch,
  175. const char **vec, unsigned int len)
  176. {
  177. DPRINTK("");
  178. xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
  179. }
  180. static struct xenbus_watch be_watch = {
  181. .node = "backend",
  182. .callback = backend_changed,
  183. };
  184. static int read_frontend_details(struct xenbus_device *xendev)
  185. {
  186. return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
  187. }
  188. int xenbus_dev_is_online(struct xenbus_device *dev)
  189. {
  190. int rc, val;
  191. rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val);
  192. if (rc != 1)
  193. val = 0; /* no online node present */
  194. return val;
  195. }
  196. EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
  197. int __xenbus_register_backend(struct xenbus_driver *drv, struct module *owner,
  198. const char *mod_name)
  199. {
  200. drv->read_otherend_details = read_frontend_details;
  201. return xenbus_register_driver_common(drv, &xenbus_backend,
  202. owner, mod_name);
  203. }
  204. EXPORT_SYMBOL_GPL(__xenbus_register_backend);
  205. static int backend_probe_and_watch(struct notifier_block *notifier,
  206. unsigned long event,
  207. void *data)
  208. {
  209. /* Enumerate devices in xenstore and watch for changes. */
  210. xenbus_probe_devices(&xenbus_backend);
  211. register_xenbus_watch(&be_watch);
  212. return NOTIFY_DONE;
  213. }
  214. static int __init xenbus_probe_backend_init(void)
  215. {
  216. static struct notifier_block xenstore_notifier = {
  217. .notifier_call = backend_probe_and_watch
  218. };
  219. int err;
  220. DPRINTK("");
  221. /* Register ourselves with the kernel bus subsystem */
  222. err = bus_register(&xenbus_backend.bus);
  223. if (err)
  224. return err;
  225. register_xenstore_notifier(&xenstore_notifier);
  226. return 0;
  227. }
  228. subsys_initcall(xenbus_probe_backend_init);