Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xenbus_probe_frontend.c
Go to the documentation of this file.
1 #define DPRINTK(fmt, args...) \
2  pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
3  __func__, __LINE__, ##args)
4 
5 #include <linux/kernel.h>
6 #include <linux/err.h>
7 #include <linux/string.h>
8 #include <linux/ctype.h>
9 #include <linux/fcntl.h>
10 #include <linux/mm.h>
11 #include <linux/proc_fs.h>
12 #include <linux/notifier.h>
13 #include <linux/kthread.h>
14 #include <linux/mutex.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 
18 #include <asm/page.h>
19 #include <asm/pgtable.h>
20 #include <asm/xen/hypervisor.h>
21 #include <xen/xenbus.h>
22 #include <xen/events.h>
23 #include <xen/page.h>
24 #include <xen/xen.h>
25 
26 #include <xen/platform_pci.h>
27 
28 #include "xenbus_comms.h"
29 #include "xenbus_probe.h"
30 
31 
32 /* device/<type>/<id> => <type>-<id> */
33 static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
34 {
35  nodename = strchr(nodename, '/');
36  if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
37  printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
38  return -EINVAL;
39  }
40 
41  strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
42  if (!strchr(bus_id, '/')) {
43  printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
44  return -EINVAL;
45  }
46  *strchr(bus_id, '/') = '-';
47  return 0;
48 }
49 
50 /* device/<typename>/<name> */
51 static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
52  const char *name)
53 {
54  char *nodename;
55  int err;
56 
57  /* ignore console/0 */
58  if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
59  DPRINTK("Ignoring buggy device entry console/0");
60  return 0;
61  }
62 
63  nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
64  if (!nodename)
65  return -ENOMEM;
66 
67  DPRINTK("%s", nodename);
68 
69  err = xenbus_probe_node(bus, type, nodename);
70  kfree(nodename);
71  return err;
72 }
73 
74 static int xenbus_uevent_frontend(struct device *_dev,
75  struct kobj_uevent_env *env)
76 {
77  struct xenbus_device *dev = to_xenbus_device(_dev);
78 
79  if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
80  return -ENOMEM;
81 
82  return 0;
83 }
84 
85 
86 static void backend_changed(struct xenbus_watch *watch,
87  const char **vec, unsigned int len)
88 {
89  xenbus_otherend_changed(watch, vec, len, 1);
90 }
91 
92 static const struct dev_pm_ops xenbus_pm_ops = {
93  .suspend = xenbus_dev_suspend,
94  .resume = xenbus_dev_resume,
95  .freeze = xenbus_dev_suspend,
96  .thaw = xenbus_dev_cancel,
97  .restore = xenbus_dev_resume,
98 };
99 
100 static struct xen_bus_type xenbus_frontend = {
101  .root = "device",
102  .levels = 2, /* device/type/<id> */
103  .get_bus_id = frontend_bus_id,
104  .probe = xenbus_probe_frontend,
105  .otherend_changed = backend_changed,
106  .bus = {
107  .name = "xen",
108  .match = xenbus_match,
109  .uevent = xenbus_uevent_frontend,
110  .probe = xenbus_dev_probe,
111  .remove = xenbus_dev_remove,
112  .shutdown = xenbus_dev_shutdown,
113  .dev_attrs = xenbus_dev_attrs,
114 
115  .pm = &xenbus_pm_ops,
116  },
117 };
118 
119 static void frontend_changed(struct xenbus_watch *watch,
120  const char **vec, unsigned int len)
121 {
122  DPRINTK("");
123 
124  xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
125 }
126 
127 
128 /* We watch for devices appearing and vanishing. */
129 static struct xenbus_watch fe_watch = {
130  .node = "device",
131  .callback = frontend_changed,
132 };
133 
134 static int read_backend_details(struct xenbus_device *xendev)
135 {
136  return xenbus_read_otherend_details(xendev, "backend-id", "backend");
137 }
138 
139 static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
140 {
141  struct xenbus_device *xendev = to_xenbus_device(dev);
142  struct device_driver *drv = data;
143  struct xenbus_driver *xendrv;
144 
145  /*
146  * A device with no driver will never connect. We care only about
147  * devices which should currently be in the process of connecting.
148  */
149  if (!dev->driver)
150  return 0;
151 
152  /* Is this search limited to a particular driver? */
153  if (drv && (dev->driver != drv))
154  return 0;
155 
156  if (ignore_nonessential) {
157  /* With older QEMU, for PVonHVM guests the guest config files
158  * could contain: vfb = [ 'vnc=1, vnclisten=0.0.0.0']
159  * which is nonsensical as there is no PV FB (there can be
160  * a PVKB) running as HVM guest. */
161 
162  if ((strncmp(xendev->nodename, "device/vkbd", 11) == 0))
163  return 0;
164 
165  if ((strncmp(xendev->nodename, "device/vfb", 10) == 0))
166  return 0;
167  }
168  xendrv = to_xenbus_driver(dev->driver);
169  return (xendev->state < XenbusStateConnected ||
170  (xendev->state == XenbusStateConnected &&
171  xendrv->is_ready && !xendrv->is_ready(xendev)));
172 }
173 static int essential_device_connecting(struct device *dev, void *data)
174 {
175  return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
176 }
177 static int non_essential_device_connecting(struct device *dev, void *data)
178 {
179  return is_device_connecting(dev, data, false);
180 }
181 
182 static int exists_essential_connecting_device(struct device_driver *drv)
183 {
184  return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
185  essential_device_connecting);
186 }
187 static int exists_non_essential_connecting_device(struct device_driver *drv)
188 {
189  return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
190  non_essential_device_connecting);
191 }
192 
193 static int print_device_status(struct device *dev, void *data)
194 {
195  struct xenbus_device *xendev = to_xenbus_device(dev);
196  struct device_driver *drv = data;
197 
198  /* Is this operation limited to a particular driver? */
199  if (drv && (dev->driver != drv))
200  return 0;
201 
202  if (!dev->driver) {
203  /* Information only: is this too noisy? */
204  printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
205  xendev->nodename);
206  } else if (xendev->state < XenbusStateConnected) {
207  enum xenbus_state rstate = XenbusStateUnknown;
208  if (xendev->otherend)
209  rstate = xenbus_read_driver_state(xendev->otherend);
210  printk(KERN_WARNING "XENBUS: Timeout connecting "
211  "to device: %s (local state %d, remote state %d)\n",
212  xendev->nodename, xendev->state, rstate);
213  }
214 
215  return 0;
216 }
217 
218 /* We only wait for device setup after most initcalls have run. */
219 static int ready_to_wait_for_devices;
220 
221 static bool wait_loop(unsigned long start, unsigned int max_delay,
222  unsigned int *seconds_waited)
223 {
224  if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
225  if (!*seconds_waited)
226  printk(KERN_WARNING "XENBUS: Waiting for "
227  "devices to initialise: ");
228  *seconds_waited += 5;
229  printk("%us...", max_delay - *seconds_waited);
230  if (*seconds_waited == max_delay)
231  return true;
232  }
233 
235 
236  return false;
237 }
238 /*
239  * On a 5-minute timeout, wait for all devices currently configured. We need
240  * to do this to guarantee that the filesystems and / or network devices
241  * needed for boot are available, before we can allow the boot to proceed.
242  *
243  * This needs to be on a late_initcall, to happen after the frontend device
244  * drivers have been initialised, but before the root fs is mounted.
245  *
246  * A possible improvement here would be to have the tools add a per-device
247  * flag to the store entry, indicating whether it is needed at boot time.
248  * This would allow people who knew what they were doing to accelerate their
249  * boot slightly, but of course needs tools or manual intervention to set up
250  * those flags correctly.
251  */
252 static void wait_for_devices(struct xenbus_driver *xendrv)
253 {
254  unsigned long start = jiffies;
255  struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
256  unsigned int seconds_waited = 0;
257 
258  if (!ready_to_wait_for_devices || !xen_domain())
259  return;
260 
261  while (exists_non_essential_connecting_device(drv))
262  if (wait_loop(start, 30, &seconds_waited))
263  break;
264 
265  /* Skips PVKB and PVFB check.*/
266  while (exists_essential_connecting_device(drv))
267  if (wait_loop(start, 270, &seconds_waited))
268  break;
269 
270  if (seconds_waited)
271  printk("\n");
272 
273  bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
274  print_device_status);
275 }
276 
278 {
279  int ret;
280 
281  drv->read_otherend_details = read_backend_details;
282 
283  ret = xenbus_register_driver_common(drv, &xenbus_frontend);
284  if (ret)
285  return ret;
286 
287  /* If this driver is loaded as a module wait for devices to attach. */
288  wait_for_devices(drv);
289 
290  return 0;
291 }
293 
294 static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
295 static int backend_state;
296 
297 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
298  const char **v, unsigned int l)
299 {
300  xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i", &backend_state);
301  printk(KERN_DEBUG "XENBUS: backend %s %s\n",
302  v[XS_WATCH_PATH], xenbus_strstate(backend_state));
303  wake_up(&backend_state_wq);
304 }
305 
306 static void xenbus_reset_wait_for_backend(char *be, int expected)
307 {
308  long timeout;
309  timeout = wait_event_interruptible_timeout(backend_state_wq,
310  backend_state == expected, 5 * HZ);
311  if (timeout <= 0)
312  printk(KERN_INFO "XENBUS: backend %s timed out.\n", be);
313 }
314 
315 /*
316  * Reset frontend if it is in Connected or Closed state.
317  * Wait for backend to catch up.
318  * State Connected happens during kdump, Closed after kexec.
319  */
320 static void xenbus_reset_frontend(char *fe, char *be, int be_state)
321 {
322  struct xenbus_watch be_watch;
323 
324  printk(KERN_DEBUG "XENBUS: backend %s %s\n",
325  be, xenbus_strstate(be_state));
326 
327  memset(&be_watch, 0, sizeof(be_watch));
328  be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
329  if (!be_watch.node)
330  return;
331 
332  be_watch.callback = xenbus_reset_backend_state_changed;
333  backend_state = XenbusStateUnknown;
334 
335  printk(KERN_INFO "XENBUS: triggering reconnect on %s\n", be);
336  register_xenbus_watch(&be_watch);
337 
338  /* fall through to forward backend to state XenbusStateInitialising */
339  switch (be_state) {
341  xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
342  xenbus_reset_wait_for_backend(be, XenbusStateClosing);
343 
344  case XenbusStateClosing:
345  xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
346  xenbus_reset_wait_for_backend(be, XenbusStateClosed);
347 
348  case XenbusStateClosed:
349  xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
350  xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
351  }
352 
353  unregister_xenbus_watch(&be_watch);
354  printk(KERN_INFO "XENBUS: reconnect done on %s\n", be);
355  kfree(be_watch.node);
356 }
357 
358 static void xenbus_check_frontend(char *class, char *dev)
359 {
360  int be_state, fe_state, err;
361  char *backend, *frontend;
362 
363  frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
364  if (!frontend)
365  return;
366 
367  err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
368  if (err != 1)
369  goto out;
370 
371  switch (fe_state) {
373  case XenbusStateClosed:
374  printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
375  frontend, xenbus_strstate(fe_state));
376  backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
377  if (!backend || IS_ERR(backend))
378  goto out;
379  err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
380  if (err == 1)
381  xenbus_reset_frontend(frontend, backend, be_state);
382  kfree(backend);
383  break;
384  default:
385  break;
386  }
387 out:
388  kfree(frontend);
389 }
390 
391 static void xenbus_reset_state(void)
392 {
393  char **devclass, **dev;
394  int devclass_n, dev_n;
395  int i, j;
396 
397  devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
398  if (IS_ERR(devclass))
399  return;
400 
401  for (i = 0; i < devclass_n; i++) {
402  dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
403  if (IS_ERR(dev))
404  continue;
405  for (j = 0; j < dev_n; j++)
406  xenbus_check_frontend(devclass[i], dev[j]);
407  kfree(dev);
408  }
409  kfree(devclass);
410 }
411 
412 static int frontend_probe_and_watch(struct notifier_block *notifier,
413  unsigned long event,
414  void *data)
415 {
416  /* reset devices in Connected or Closed state */
417  if (xen_hvm_domain())
418  xenbus_reset_state();
419  /* Enumerate devices in xenstore and watch for changes. */
420  xenbus_probe_devices(&xenbus_frontend);
421  register_xenbus_watch(&fe_watch);
422 
423  return NOTIFY_DONE;
424 }
425 
426 
427 static int __init xenbus_probe_frontend_init(void)
428 {
429  static struct notifier_block xenstore_notifier = {
430  .notifier_call = frontend_probe_and_watch
431  };
432  int err;
433 
434  DPRINTK("");
435 
436  /* Register ourselves with the kernel bus subsystem */
437  err = bus_register(&xenbus_frontend.bus);
438  if (err)
439  return err;
440 
441  register_xenstore_notifier(&xenstore_notifier);
442 
443  return 0;
444 }
445 subsys_initcall(xenbus_probe_frontend_init);
446 
447 #ifndef MODULE
448 static int __init boot_wait_for_devices(void)
449 {
451  return -ENODEV;
452 
453  ready_to_wait_for_devices = 1;
454  wait_for_devices(NULL);
455  return 0;
456 }
457 
458 late_initcall(boot_wait_for_devices);
459 #endif
460 
461 MODULE_LICENSE("GPL");