xenbus_probe_backend.c 7.0 KB

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