blob: 9b2fbe69bccc7cdadb264997e680952ef95fde08 [file] [log] [blame]
Ian Campbelldf660252009-02-09 12:05:51 -08001/******************************************************************************
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
Joe Perches283c0972013-06-28 03:21:41 -070034#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
36#define DPRINTK(fmt, ...) \
37 pr_debug("(%s:%d) " fmt "\n", \
38 __func__, __LINE__, ##__VA_ARGS__)
Ian Campbelldf660252009-02-09 12:05:51 -080039
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>
45#include <linux/mm.h>
46#include <linux/notifier.h>
Paul Gortmaker63c97442011-07-10 13:22:07 -040047#include <linux/export.h>
Juergen Gross2f69a112020-03-05 11:03:23 +010048#include <linux/semaphore.h>
Ian Campbelldf660252009-02-09 12:05:51 -080049
50#include <asm/page.h>
51#include <asm/pgtable.h>
52#include <asm/xen/hypervisor.h>
53#include <asm/hypervisor.h>
54#include <xen/xenbus.h>
Ian Campbelldf660252009-02-09 12:05:51 -080055#include <xen/features.h>
56
Juergen Gross332f7912017-02-09 14:39:56 +010057#include "xenbus.h"
Ian Campbelldf660252009-02-09 12:05:51 -080058
59/* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
60static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
61{
62 int domid, err;
63 const char *devid, *type, *frontend;
64 unsigned int typelen;
65
66 type = strchr(nodename, '/');
67 if (!type)
68 return -EINVAL;
69 type++;
70 typelen = strcspn(type, "/");
71 if (!typelen || type[typelen] != '/')
72 return -EINVAL;
73
74 devid = strrchr(nodename, '/') + 1;
75
76 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
77 "frontend", NULL, &frontend,
78 NULL);
79 if (err)
80 return err;
81 if (strlen(frontend) == 0)
82 err = -ERANGE;
83 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
84 err = -ENOENT;
85 kfree(frontend);
86
87 if (err)
88 return err;
89
Ian Campbell6bac7f92010-12-10 14:39:15 +000090 if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
91 typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
Ian Campbelldf660252009-02-09 12:05:51 -080092 return -ENOSPC;
93 return 0;
94}
95
96static int xenbus_uevent_backend(struct device *dev,
97 struct kobj_uevent_env *env)
98{
99 struct xenbus_device *xdev;
100 struct xenbus_driver *drv;
101 struct xen_bus_type *bus;
102
103 DPRINTK("");
104
105 if (dev == NULL)
106 return -ENODEV;
107
108 xdev = to_xenbus_device(dev);
109 bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
Ian Campbelldf660252009-02-09 12:05:51 -0800110
Bastian Blank149bb2f2011-06-29 14:40:08 +0200111 if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
112 return -ENOMEM;
113
Ian Campbelldf660252009-02-09 12:05:51 -0800114 /* stuff we want to pass to /sbin/hotplug */
115 if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
116 return -ENOMEM;
117
118 if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
119 return -ENOMEM;
120
121 if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
122 return -ENOMEM;
123
124 if (dev->driver) {
125 drv = to_xenbus_driver(dev->driver);
126 if (drv && drv->uevent)
127 return drv->uevent(xdev, env);
128 }
129
130 return 0;
131}
132
133/* backend/<typename>/<frontend-uuid>/<name> */
134static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
135 const char *dir,
136 const char *type,
137 const char *name)
138{
139 char *nodename;
140 int err;
141
142 nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
143 if (!nodename)
144 return -ENOMEM;
145
146 DPRINTK("%s\n", nodename);
147
148 err = xenbus_probe_node(bus, type, nodename);
149 kfree(nodename);
150 return err;
151}
152
153/* backend/<typename>/<frontend-domid> */
Ian Campbell6bac7f92010-12-10 14:39:15 +0000154static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
155 const char *domid)
Ian Campbelldf660252009-02-09 12:05:51 -0800156{
157 char *nodename;
158 int err = 0;
159 char **dir;
160 unsigned int i, dir_n = 0;
161
162 DPRINTK("");
163
164 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
165 if (!nodename)
166 return -ENOMEM;
167
168 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
169 if (IS_ERR(dir)) {
170 kfree(nodename);
171 return PTR_ERR(dir);
172 }
173
174 for (i = 0; i < dir_n; i++) {
175 err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
176 if (err)
177 break;
178 }
179 kfree(dir);
180 kfree(nodename);
181 return err;
182}
183
184static void frontend_changed(struct xenbus_watch *watch,
Juergen Gross5584ea22017-02-09 14:39:57 +0100185 const char *path, const char *token)
Ian Campbelldf660252009-02-09 12:05:51 -0800186{
Juergen Gross5584ea22017-02-09 14:39:57 +0100187 xenbus_otherend_changed(watch, path, token, 0);
Ian Campbelldf660252009-02-09 12:05:51 -0800188}
189
Ian Campbelldf660252009-02-09 12:05:51 -0800190static struct xen_bus_type xenbus_backend = {
191 .root = "backend",
Ian Campbell6bac7f92010-12-10 14:39:15 +0000192 .levels = 3, /* backend/type/<frontend>/<id> */
Ian Campbelldf660252009-02-09 12:05:51 -0800193 .get_bus_id = backend_bus_id,
194 .probe = xenbus_probe_backend,
195 .otherend_changed = frontend_changed,
196 .bus = {
Ian Campbell6bac7f92010-12-10 14:39:15 +0000197 .name = "xen-backend",
198 .match = xenbus_match,
199 .uevent = xenbus_uevent_backend,
200 .probe = xenbus_dev_probe,
201 .remove = xenbus_dev_remove,
Greg Kroah-Hartman85dd9262013-10-06 23:55:49 -0700202 .dev_groups = xenbus_dev_groups,
Ian Campbelldf660252009-02-09 12:05:51 -0800203 },
204};
205
206static void backend_changed(struct xenbus_watch *watch,
Juergen Gross5584ea22017-02-09 14:39:57 +0100207 const char *path, const char *token)
Ian Campbelldf660252009-02-09 12:05:51 -0800208{
209 DPRINTK("");
210
Juergen Gross5584ea22017-02-09 14:39:57 +0100211 xenbus_dev_changed(path, &xenbus_backend);
Ian Campbelldf660252009-02-09 12:05:51 -0800212}
213
214static struct xenbus_watch be_watch = {
215 .node = "backend",
216 .callback = backend_changed,
217};
218
219static int read_frontend_details(struct xenbus_device *xendev)
220{
221 return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
222}
223
Ian Campbelldf660252009-02-09 12:05:51 -0800224int xenbus_dev_is_online(struct xenbus_device *dev)
225{
Juergen Gross999c9af2016-10-31 14:58:42 +0100226 return !!xenbus_read_unsigned(dev->nodename, "online", 0);
Ian Campbelldf660252009-02-09 12:05:51 -0800227}
228EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
229
David Vrabel95afae42014-09-08 17:30:41 +0100230int __xenbus_register_backend(struct xenbus_driver *drv, struct module *owner,
231 const char *mod_name)
Ian Campbelldf660252009-02-09 12:05:51 -0800232{
233 drv->read_otherend_details = read_frontend_details;
234
David Vrabel95afae42014-09-08 17:30:41 +0100235 return xenbus_register_driver_common(drv, &xenbus_backend,
236 owner, mod_name);
Ian Campbelldf660252009-02-09 12:05:51 -0800237}
David Vrabel95afae42014-09-08 17:30:41 +0100238EXPORT_SYMBOL_GPL(__xenbus_register_backend);
Ian Campbelldf660252009-02-09 12:05:51 -0800239
240static int backend_probe_and_watch(struct notifier_block *notifier,
241 unsigned long event,
242 void *data)
243{
244 /* Enumerate devices in xenstore and watch for changes. */
245 xenbus_probe_devices(&xenbus_backend);
Ian Campbelldf660252009-02-09 12:05:51 -0800246 register_xenbus_watch(&be_watch);
Jeremy Fitzhardingeb5d33d02010-03-29 14:38:35 -0700247
Ian Campbelldf660252009-02-09 12:05:51 -0800248 return NOTIFY_DONE;
249}
250
SeongJae Park8a105672020-01-27 09:18:08 +0100251static int backend_reclaim_memory(struct device *dev, void *data)
252{
253 const struct xenbus_driver *drv;
SeongJae Park060eabe2020-01-27 09:18:09 +0100254 struct xenbus_device *xdev;
SeongJae Park8a105672020-01-27 09:18:08 +0100255
256 if (!dev->driver)
257 return 0;
258 drv = to_xenbus_driver(dev->driver);
SeongJae Park060eabe2020-01-27 09:18:09 +0100259 if (drv && drv->reclaim_memory) {
260 xdev = to_xenbus_device(dev);
Juergen Gross2f69a112020-03-05 11:03:23 +0100261 if (down_trylock(&xdev->reclaim_sem))
SeongJae Park060eabe2020-01-27 09:18:09 +0100262 return 0;
263 drv->reclaim_memory(xdev);
Juergen Gross2f69a112020-03-05 11:03:23 +0100264 up(&xdev->reclaim_sem);
SeongJae Park060eabe2020-01-27 09:18:09 +0100265 }
SeongJae Park8a105672020-01-27 09:18:08 +0100266 return 0;
267}
268
269/*
270 * Returns 0 always because we are using shrinker to only detect memory
271 * pressure.
272 */
273static unsigned long backend_shrink_memory_count(struct shrinker *shrinker,
274 struct shrink_control *sc)
275{
276 bus_for_each_dev(&xenbus_backend.bus, NULL, NULL,
277 backend_reclaim_memory);
278 return 0;
279}
280
281static struct shrinker backend_memory_shrinker = {
282 .count_objects = backend_shrink_memory_count,
283 .seeks = DEFAULT_SEEKS,
284};
285
Ian Campbelldf660252009-02-09 12:05:51 -0800286static int __init xenbus_probe_backend_init(void)
287{
288 static struct notifier_block xenstore_notifier = {
289 .notifier_call = backend_probe_and_watch
290 };
291 int err;
292
293 DPRINTK("");
294
295 /* Register ourselves with the kernel bus subsystem */
296 err = bus_register(&xenbus_backend.bus);
Jeremy Fitzhardingeb5d33d02010-03-29 14:38:35 -0700297 if (err)
Ian Campbelldf660252009-02-09 12:05:51 -0800298 return err;
Ian Campbelldf660252009-02-09 12:05:51 -0800299
300 register_xenstore_notifier(&xenstore_notifier);
301
SeongJae Park8a105672020-01-27 09:18:08 +0100302 if (register_shrinker(&backend_memory_shrinker))
303 pr_warn("shrinker registration failed\n");
304
Ian Campbelldf660252009-02-09 12:05:51 -0800305 return 0;
306}
Jeremy Fitzhardingec3676e82009-03-04 22:32:16 -0800307subsys_initcall(xenbus_probe_backend_init);