1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#define DPRINTK(fmt, args...) \
35 pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
36 __func__, __LINE__, ##args)
37
38#include <linux/kernel.h>
39#include <linux/err.h>
40#include <linux/string.h>
41#include <linux/ctype.h>
42#include <linux/fcntl.h>
43#include <linux/mm.h>
44#include <linux/notifier.h>
45#include <linux/export.h>
46
47#include <asm/page.h>
48#include <asm/pgtable.h>
49#include <asm/xen/hypervisor.h>
50#include <asm/hypervisor.h>
51#include <xen/xenbus.h>
52#include <xen/features.h>
53
54#include "xenbus_comms.h"
55#include "xenbus_probe.h"
56
57
58static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
59{
60 int domid, err;
61 const char *devid, *type, *frontend;
62 unsigned int typelen;
63
64 type = strchr(nodename, '/');
65 if (!type)
66 return -EINVAL;
67 type++;
68 typelen = strcspn(type, "/");
69 if (!typelen || type[typelen] != '/')
70 return -EINVAL;
71
72 devid = strrchr(nodename, '/') + 1;
73
74 err = xenbus_gather(XBT_NIL, nodename, "frontend-id", "%i", &domid,
75 "frontend", NULL, &frontend,
76 NULL);
77 if (err)
78 return err;
79 if (strlen(frontend) == 0)
80 err = -ERANGE;
81 if (!err && !xenbus_exists(XBT_NIL, frontend, ""))
82 err = -ENOENT;
83 kfree(frontend);
84
85 if (err)
86 return err;
87
88 if (snprintf(bus_id, XEN_BUS_ID_SIZE, "%.*s-%i-%s",
89 typelen, type, domid, devid) >= XEN_BUS_ID_SIZE)
90 return -ENOSPC;
91 return 0;
92}
93
94static int xenbus_uevent_backend(struct device *dev,
95 struct kobj_uevent_env *env)
96{
97 struct xenbus_device *xdev;
98 struct xenbus_driver *drv;
99 struct xen_bus_type *bus;
100
101 DPRINTK("");
102
103 if (dev == NULL)
104 return -ENODEV;
105
106 xdev = to_xenbus_device(dev);
107 bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
108
109 if (add_uevent_var(env, "MODALIAS=xen-backend:%s", xdev->devicetype))
110 return -ENOMEM;
111
112
113 if (add_uevent_var(env, "XENBUS_TYPE=%s", xdev->devicetype))
114 return -ENOMEM;
115
116 if (add_uevent_var(env, "XENBUS_PATH=%s", xdev->nodename))
117 return -ENOMEM;
118
119 if (add_uevent_var(env, "XENBUS_BASE_PATH=%s", bus->root))
120 return -ENOMEM;
121
122 if (dev->driver) {
123 drv = to_xenbus_driver(dev->driver);
124 if (drv && drv->uevent)
125 return drv->uevent(xdev, env);
126 }
127
128 return 0;
129}
130
131
132static int xenbus_probe_backend_unit(struct xen_bus_type *bus,
133 const char *dir,
134 const char *type,
135 const char *name)
136{
137 char *nodename;
138 int err;
139
140 nodename = kasprintf(GFP_KERNEL, "%s/%s", dir, name);
141 if (!nodename)
142 return -ENOMEM;
143
144 DPRINTK("%s\n", nodename);
145
146 err = xenbus_probe_node(bus, type, nodename);
147 kfree(nodename);
148 return err;
149}
150
151
152static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
153 const char *domid)
154{
155 char *nodename;
156 int err = 0;
157 char **dir;
158 unsigned int i, dir_n = 0;
159
160 DPRINTK("");
161
162 nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, domid);
163 if (!nodename)
164 return -ENOMEM;
165
166 dir = xenbus_directory(XBT_NIL, nodename, "", &dir_n);
167 if (IS_ERR(dir)) {
168 kfree(nodename);
169 return PTR_ERR(dir);
170 }
171
172 for (i = 0; i < dir_n; i++) {
173 err = xenbus_probe_backend_unit(bus, nodename, type, dir[i]);
174 if (err)
175 break;
176 }
177 kfree(dir);
178 kfree(nodename);
179 return err;
180}
181
182static void frontend_changed(struct xenbus_watch *watch,
183 const char **vec, unsigned int len)
184{
185 xenbus_otherend_changed(watch, vec, len, 0);
186}
187
188static struct xen_bus_type xenbus_backend = {
189 .root = "backend",
190 .levels = 3,
191 .get_bus_id = backend_bus_id,
192 .probe = xenbus_probe_backend,
193 .otherend_changed = frontend_changed,
194 .bus = {
195 .name = "xen-backend",
196 .match = xenbus_match,
197 .uevent = xenbus_uevent_backend,
198 .probe = xenbus_dev_probe,
199 .remove = xenbus_dev_remove,
200 .shutdown = xenbus_dev_shutdown,
201 .dev_attrs = xenbus_dev_attrs,
202 },
203};
204
205static void backend_changed(struct xenbus_watch *watch,
206 const char **vec, unsigned int len)
207{
208 DPRINTK("");
209
210 xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
211}
212
213static struct xenbus_watch be_watch = {
214 .node = "backend",
215 .callback = backend_changed,
216};
217
218static int read_frontend_details(struct xenbus_device *xendev)
219{
220 return xenbus_read_otherend_details(xendev, "frontend-id", "frontend");
221}
222
223int xenbus_dev_is_online(struct xenbus_device *dev)
224{
225 int rc, val;
226
227 rc = xenbus_scanf(XBT_NIL, dev->nodename, "online", "%d", &val);
228 if (rc != 1)
229 val = 0;
230
231 return val;
232}
233EXPORT_SYMBOL_GPL(xenbus_dev_is_online);
234
235int xenbus_register_backend(struct xenbus_driver *drv)
236{
237 drv->read_otherend_details = read_frontend_details;
238
239 return xenbus_register_driver_common(drv, &xenbus_backend);
240}
241EXPORT_SYMBOL_GPL(xenbus_register_backend);
242
243static int backend_probe_and_watch(struct notifier_block *notifier,
244 unsigned long event,
245 void *data)
246{
247
248 xenbus_probe_devices(&xenbus_backend);
249 register_xenbus_watch(&be_watch);
250
251 return NOTIFY_DONE;
252}
253
254static int __init xenbus_probe_backend_init(void)
255{
256 static struct notifier_block xenstore_notifier = {
257 .notifier_call = backend_probe_and_watch
258 };
259 int err;
260
261 DPRINTK("");
262
263
264 err = bus_register(&xenbus_backend.bus);
265 if (err)
266 return err;
267
268 register_xenstore_notifier(&xenstore_notifier);
269
270 return 0;
271}
272subsys_initcall(xenbus_probe_backend_init);
273