xenbus_probe_backend.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <linux/semaphore.h>
  46. #include <asm/page.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 bool frontend_will_handle(struct xenbus_watch *watch,
  153. const char *path, const char *token)
  154. {
  155. return watch->nr_pending == 0;
  156. }
  157. static void frontend_changed(struct xenbus_watch *watch,
  158. const char *path, const char *token)
  159. {
  160. xenbus_otherend_changed(watch, path, token, 0);
  161. }
  162. static struct xen_bus_type xenbus_backend = {
  163. .root = "backend",
  164. .levels = 3, /* backend/type/<frontend>/<id> */
  165. .get_bus_id = backend_bus_id,
  166. .probe = xenbus_probe_backend,
  167. .otherend_will_handle = frontend_will_handle,
  168. .otherend_changed = frontend_changed,
  169. .bus = {
  170. .name = "xen-backend",
  171. .match = xenbus_match,
  172. .uevent = xenbus_uevent_backend,
  173. .probe = xenbus_dev_probe,
  174. .remove = xenbus_dev_remove,
  175. .dev_groups = xenbus_dev_groups,
  176. },
  177. };
  178. static void backend_changed(struct xenbus_watch *watch,
  179. const char *path, const char *token)
  180. {
  181. DPRINTK("");
  182. xenbus_dev_changed(path, &xenbus_backend);
  183. }
  184. static struct xenbus_watch be_watch = {
  185. .node = "backend",
  186. .callback = backend_changed,
  187. };
  188. static int read_frontend_details(struct xenbus_device *xendev)
  189. {
  190. return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
  191. }
  192. int xenbus_dev_is_online(struct xenbus_device *dev)
  193. {
  194. return !!xenbus_read_unsigned(dev->nodename, "online", 0);
  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 backend_reclaim_memory(struct device *dev, void *data)
  215. {
  216. const struct xenbus_driver *drv;
  217. struct xenbus_device *xdev;
  218. if (!dev->driver)
  219. return 0;
  220. drv = to_xenbus_driver(dev->driver);
  221. if (drv && drv->reclaim_memory) {
  222. xdev = to_xenbus_device(dev);
  223. if (down_trylock(&xdev->reclaim_sem))
  224. return 0;
  225. drv->reclaim_memory(xdev);
  226. up(&xdev->reclaim_sem);
  227. }
  228. return 0;
  229. }
  230. /*
  231. * Returns 0 always because we are using shrinker to only detect memory
  232. * pressure.
  233. */
  234. static unsigned long backend_shrink_memory_count(struct shrinker *shrinker,
  235. struct shrink_control *sc)
  236. {
  237. bus_for_each_dev(&xenbus_backend.bus, NULL, NULL,
  238. backend_reclaim_memory);
  239. return 0;
  240. }
  241. static struct shrinker backend_memory_shrinker = {
  242. .count_objects = backend_shrink_memory_count,
  243. .seeks = DEFAULT_SEEKS,
  244. };
  245. static int __init xenbus_probe_backend_init(void)
  246. {
  247. static struct notifier_block xenstore_notifier = {
  248. .notifier_call = backend_probe_and_watch
  249. };
  250. int err;
  251. DPRINTK("");
  252. /* Register ourselves with the kernel bus subsystem */
  253. err = bus_register(&xenbus_backend.bus);
  254. if (err)
  255. return err;
  256. register_xenstore_notifier(&xenstore_notifier);
  257. if (register_shrinker(&backend_memory_shrinker, "xen-backend"))
  258. pr_warn("shrinker registration failed\n");
  259. return 0;
  260. }
  261. subsys_initcall(xenbus_probe_backend_init);