1 /******************************************************************************
2 * Talks to Xen Store to figure out what devices we have (backend half).
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.
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:
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:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
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
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #define DPRINTK(fmt, ...) \
37 pr_debug("(%s:%d) " fmt "\n", \
38 __func__, __LINE__, ##__VA_ARGS__)
40 #include <linux/kernel.h>
41 #include <linux/err.h>
42 #include <linux/string.h>
43 #include <linux/ctype.h>
44 #include <linux/fcntl.h>
46 #include <linux/notifier.h>
47 #include <linux/export.h>
48 #include <linux/semaphore.h>
51 #include <asm/xen/hypervisor.h>
52 #include <asm/hypervisor.h>
53 #include <xen/xenbus.h>
54 #include <xen/features.h>
58 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
59 static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
62 const char *devid, *type, *frontend;
65 type = strchr(nodename, '/');
69 typelen = strcspn(type, "/");
70 if (!typelen || type[typelen] != '/')
73 devid = strrchr(nodename, '/') + 1;
75 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
76 "frontend", NULL, &frontend,
80 if (strlen(frontend) == 0)
82 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
89 if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
90 typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
95 static int xenbus_uevent_backend(struct device *dev,
96 struct kobj_uevent_env *env)
98 struct xenbus_device *xdev;
99 struct xenbus_driver *drv;
100 struct xen_bus_type *bus;
107 xdev = to_xenbus_device(dev);
108 bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
110 if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
113 /* stuff we want to pass to /sbin/hotplug */
114 if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
117 if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
120 if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
124 drv = to_xenbus_driver(dev->driver);
125 if (drv && drv->uevent)
126 return drv->uevent(xdev, env);
132 /* backend/<typename>/<frontend-uuid>/<name> */
133 static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
141 nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
145 DPRINTK("%s\n", nodename);
147 err = xenbus_probe_node(bus, type, nodename);
152 /* backend/<typename>/<frontend-domid> */
153 static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
159 unsigned int i, dir_n = 0;
163 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
167 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
173 for (i = 0; i < dir_n; i++) {
174 err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
183 static bool frontend_will_handle(struct xenbus_watch *watch,
184 const char *path, const char *token)
186 return watch->nr_pending == 0;
189 static void frontend_changed(struct xenbus_watch *watch,
190 const char *path, const char *token)
192 xenbus_otherend_changed(watch, path, token, 0);
195 static struct xen_bus_type xenbus_backend = {
197 .levels = 3, /* backend/type/<frontend>/<id> */
198 .get_bus_id = backend_bus_id,
199 .probe = xenbus_probe_backend,
200 .otherend_will_handle = frontend_will_handle,
201 .otherend_changed = frontend_changed,
203 .name = "xen-backend",
204 .match = xenbus_match,
205 .uevent = xenbus_uevent_backend,
206 .probe = xenbus_dev_probe,
207 .remove = xenbus_dev_remove,
208 .dev_groups = xenbus_dev_groups,
212 static void backend_changed(struct xenbus_watch *watch,
213 const char *path, const char *token)
217 xenbus_dev_changed(path, &xenbus_backend);
220 static struct xenbus_watch be_watch = {
222 .callback = backend_changed,
225 static int read_frontend_details(struct xenbus_device *xendev)
227 return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
230 int xenbus_dev_is_online(struct xenbus_device *dev)
232 return !!xenbus_read_unsigned(dev->nodename, "online", 0);
234 EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
236 int __xenbus_register_backend(struct xenbus_driver *drv, struct module *owner,
237 const char *mod_name)
239 drv->read_otherend_details = read_frontend_details;
241 return xenbus_register_driver_common(drv, &xenbus_backend,
244 EXPORT_SYMBOL_GPL(__xenbus_register_backend);
246 static int backend_probe_and_watch(struct notifier_block *notifier,
250 /* Enumerate devices in xenstore and watch for changes. */
251 xenbus_probe_devices(&xenbus_backend);
252 register_xenbus_watch(&be_watch);
257 static int backend_reclaim_memory(struct device *dev, void *data)
259 const struct xenbus_driver *drv;
260 struct xenbus_device *xdev;
264 drv = to_xenbus_driver(dev->driver);
265 if (drv && drv->reclaim_memory) {
266 xdev = to_xenbus_device(dev);
267 if (down_trylock(&xdev->reclaim_sem))
269 drv->reclaim_memory(xdev);
270 up(&xdev->reclaim_sem);
276 * Returns 0 always because we are using shrinker to only detect memory
279 static unsigned long backend_shrink_memory_count(struct shrinker *shrinker,
280 struct shrink_control *sc)
282 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL,
283 backend_reclaim_memory);
287 static struct shrinker backend_memory_shrinker = {
288 .count_objects = backend_shrink_memory_count,
289 .seeks = DEFAULT_SEEKS,
292 static int __init xenbus_probe_backend_init(void)
294 static struct notifier_block xenstore_notifier = {
295 .notifier_call = backend_probe_and_watch
301 /* Register ourselves with the kernel bus subsystem */
302 err = bus_register(&xenbus_backend.bus);
306 register_xenstore_notifier(&xenstore_notifier);
308 if (register_shrinker(&backend_memory_shrinker, "xen-backend"))
309 pr_warn("shrinker registration failed\n");
313 subsys_initcall(xenbus_probe_backend_init);