1 /******************************************************************************
2 * xenbus_xs.c
3 *
4 * This is the kernel equivalent of the "xs" library. We don't need everything
5 * and we use xenbus_comms for communication.
6 *
7 * Copyright (C) 2005 Rusty Russell, IBM Corporation
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
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
36 #include <linux/unistd.h>
37 #include <linux/errno.h>
38 #include <linux/types.h>
39 #include <linux/uio.h>
40 #include <linux/kernel.h>
41 #include <linux/string.h>
42 #include <linux/err.h>
43 #include <linux/slab.h>
44 #include <linux/fcntl.h>
45 #include <linux/kthread.h>
46 #include <linux/reboot.h>
47 #include <linux/rwsem.h>
48 #include <linux/mutex.h>
49 #include <asm/xen/hypervisor.h>
50 #ifdef CONFIG_X86
51 #include <asm/cpuid/api.h>
52 #endif
53 #include <xen/xenbus.h>
54 #include <xen/xen.h>
55 #include "xenbus.h"
56
57 /*
58 * Framework to protect suspend/resume handling against normal Xenstore
59 * message handling:
60 * During suspend/resume there must be no open transaction and no pending
61 * Xenstore request.
62 * New watch events happening in this time can be ignored by firing all watches
63 * after resume.
64 */
65
66 /* Lock protecting enter/exit critical region. */
67 static DEFINE_SPINLOCK(xs_state_lock);
68 /* Number of users in critical region (protected by xs_state_lock). */
69 static unsigned int xs_state_users;
70 /* Suspend handler waiting or already active (protected by xs_state_lock)? */
71 static int xs_suspend_active;
72 /* Unique Xenstore request id (protected by xs_state_lock). */
73 static uint32_t xs_request_id;
74
75 /* Wait queue for all callers waiting for critical region to become usable. */
76 static DECLARE_WAIT_QUEUE_HEAD(xs_state_enter_wq);
77 /* Wait queue for suspend handling waiting for critical region being empty. */
78 static DECLARE_WAIT_QUEUE_HEAD(xs_state_exit_wq);
79
80 /* List of registered watches, and a lock to protect it. */
81 static LIST_HEAD(watches);
82 static DEFINE_SPINLOCK(watches_lock);
83
84 /* List of pending watch callback events, and a lock to protect it. */
85 static LIST_HEAD(watch_events);
86 static DEFINE_SPINLOCK(watch_events_lock);
87
88 /* Protect watch (de)register against save/restore. */
89 static DECLARE_RWSEM(xs_watch_rwsem);
90
91 /*
92 * Details of the xenwatch callback kernel thread. The thread waits on the
93 * watch_events_waitq for work to do (queued on watch_events list). When it
94 * wakes up it acquires the xenwatch_mutex before reading the list and
95 * carrying out work.
96 */
97 static pid_t xenwatch_pid;
98 static DEFINE_MUTEX(xenwatch_mutex);
99 static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
100
xs_suspend_enter(void)101 static void xs_suspend_enter(void)
102 {
103 spin_lock(&xs_state_lock);
104 xs_suspend_active++;
105 spin_unlock(&xs_state_lock);
106 wait_event(xs_state_exit_wq, xs_state_users == 0);
107 }
108
xs_suspend_exit(void)109 static void xs_suspend_exit(void)
110 {
111 xb_dev_generation_id++;
112 spin_lock(&xs_state_lock);
113 xs_suspend_active--;
114 spin_unlock(&xs_state_lock);
115 wake_up_all(&xs_state_enter_wq);
116 }
117
xs_free_req(struct kref * kref)118 void xs_free_req(struct kref *kref)
119 {
120 struct xb_req_data *req = container_of(kref, struct xb_req_data, kref);
121 kfree(req);
122 }
123
xs_request_enter(struct xb_req_data * req)124 static uint32_t xs_request_enter(struct xb_req_data *req)
125 {
126 uint32_t rq_id;
127
128 req->type = req->msg.type;
129
130 spin_lock(&xs_state_lock);
131
132 while (!xs_state_users && xs_suspend_active) {
133 spin_unlock(&xs_state_lock);
134 wait_event(xs_state_enter_wq, xs_suspend_active == 0);
135 spin_lock(&xs_state_lock);
136 }
137
138 if (req->type == XS_TRANSACTION_START && !req->user_req)
139 xs_state_users++;
140 xs_state_users++;
141 rq_id = xs_request_id++;
142
143 spin_unlock(&xs_state_lock);
144
145 return rq_id;
146 }
147
xs_request_exit(struct xb_req_data * req)148 void xs_request_exit(struct xb_req_data *req)
149 {
150 spin_lock(&xs_state_lock);
151 xs_state_users--;
152 if ((req->type == XS_TRANSACTION_START && req->msg.type == XS_ERROR) ||
153 (req->type == XS_TRANSACTION_END && !req->user_req &&
154 !WARN_ON_ONCE(req->msg.type == XS_ERROR &&
155 !strcmp(req->body, "ENOENT"))))
156 xs_state_users--;
157 spin_unlock(&xs_state_lock);
158
159 if (xs_suspend_active && !xs_state_users)
160 wake_up(&xs_state_exit_wq);
161 }
162
get_error(const char * errorstring)163 static int get_error(const char *errorstring)
164 {
165 unsigned int i;
166
167 for (i = 0; strcmp(errorstring, xsd_errors[i].errstring) != 0; i++) {
168 if (i == ARRAY_SIZE(xsd_errors) - 1) {
169 pr_warn("xen store gave: unknown error %s\n",
170 errorstring);
171 return EINVAL;
172 }
173 }
174 return xsd_errors[i].errnum;
175 }
176
xenbus_ok(void)177 static bool xenbus_ok(void)
178 {
179 switch (xen_store_domain_type) {
180 case XS_LOCAL:
181 switch (system_state) {
182 case SYSTEM_POWER_OFF:
183 case SYSTEM_RESTART:
184 case SYSTEM_HALT:
185 return false;
186 default:
187 break;
188 }
189 return true;
190 case XS_PV:
191 case XS_HVM:
192 /* FIXME: Could check that the remote domain is alive,
193 * but it is normally initial domain. */
194 return true;
195 default:
196 break;
197 }
198 return false;
199 }
200
test_reply(struct xb_req_data * req)201 static bool test_reply(struct xb_req_data *req)
202 {
203 if (req->state == xb_req_state_got_reply || !xenbus_ok()) {
204 /* read req->state before all other fields */
205 virt_rmb();
206 return true;
207 }
208
209 /* Make sure to reread req->state each time. */
210 barrier();
211
212 return false;
213 }
214
read_reply(struct xb_req_data * req)215 static void *read_reply(struct xb_req_data *req)
216 {
217 do {
218 wait_event(req->wq, test_reply(req));
219
220 if (!xenbus_ok())
221 /*
222 * If we are in the process of being shut-down there is
223 * no point of trying to contact XenBus - it is either
224 * killed (xenstored application) or the other domain
225 * has been killed or is unreachable.
226 */
227 return ERR_PTR(-EIO);
228 if (req->err)
229 return ERR_PTR(req->err);
230
231 } while (req->state != xb_req_state_got_reply);
232
233 return req->body;
234 }
235
xs_send(struct xb_req_data * req,struct xsd_sockmsg * msg)236 static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
237 {
238 bool notify;
239
240 req->msg = *msg;
241 req->err = 0;
242 req->state = xb_req_state_queued;
243 init_waitqueue_head(&req->wq);
244
245 /* Save the caller req_id and restore it later in the reply */
246 req->caller_req_id = req->msg.req_id;
247 req->msg.req_id = xs_request_enter(req);
248
249 /*
250 * Take 2nd ref. One for this thread, and the second for the
251 * xenbus_thread.
252 */
253 kref_get(&req->kref);
254
255 mutex_lock(&xb_write_mutex);
256 list_add_tail(&req->list, &xb_write_list);
257 notify = list_is_singular(&xb_write_list);
258 mutex_unlock(&xb_write_mutex);
259
260 if (notify)
261 wake_up(&xb_waitq);
262 }
263
xs_wait_for_reply(struct xb_req_data * req,struct xsd_sockmsg * msg)264 static void *xs_wait_for_reply(struct xb_req_data *req, struct xsd_sockmsg *msg)
265 {
266 void *ret;
267
268 ret = read_reply(req);
269
270 xs_request_exit(req);
271
272 msg->type = req->msg.type;
273 msg->len = req->msg.len;
274
275 mutex_lock(&xb_write_mutex);
276 if (req->state == xb_req_state_queued ||
277 req->state == xb_req_state_wait_reply)
278 req->state = xb_req_state_aborted;
279
280 kref_put(&req->kref, xs_free_req);
281 mutex_unlock(&xb_write_mutex);
282
283 return ret;
284 }
285
xs_wake_up(struct xb_req_data * req)286 static void xs_wake_up(struct xb_req_data *req)
287 {
288 wake_up(&req->wq);
289 }
290
xenbus_dev_request_and_reply(struct xsd_sockmsg * msg,void * par)291 int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par)
292 {
293 struct xb_req_data *req;
294 struct kvec *vec;
295
296 req = kmalloc(sizeof(*req) + sizeof(*vec), GFP_KERNEL);
297 if (!req)
298 return -ENOMEM;
299
300 vec = (struct kvec *)(req + 1);
301 vec->iov_len = msg->len;
302 vec->iov_base = msg + 1;
303
304 req->vec = vec;
305 req->num_vecs = 1;
306 req->cb = xenbus_dev_queue_reply;
307 req->par = par;
308 req->user_req = true;
309 kref_init(&req->kref);
310
311 xs_send(req, msg);
312
313 return 0;
314 }
315 EXPORT_SYMBOL(xenbus_dev_request_and_reply);
316
317 /* Send message to xs, get kmalloc'ed reply. ERR_PTR() on error. */
xs_talkv(struct xenbus_transaction t,enum xsd_sockmsg_type type,const struct kvec * iovec,unsigned int num_vecs,unsigned int * len)318 static void *xs_talkv(struct xenbus_transaction t,
319 enum xsd_sockmsg_type type,
320 const struct kvec *iovec,
321 unsigned int num_vecs,
322 unsigned int *len)
323 {
324 struct xb_req_data *req;
325 struct xsd_sockmsg msg;
326 void *ret = NULL;
327 unsigned int i;
328 int err;
329
330 req = kmalloc_obj(*req, GFP_NOIO | __GFP_HIGH);
331 if (!req)
332 return ERR_PTR(-ENOMEM);
333
334 req->vec = iovec;
335 req->num_vecs = num_vecs;
336 req->cb = xs_wake_up;
337 req->user_req = false;
338 kref_init(&req->kref);
339
340 msg.req_id = 0;
341 msg.tx_id = t.id;
342 msg.type = type;
343 msg.len = 0;
344 for (i = 0; i < num_vecs; i++)
345 msg.len += iovec[i].iov_len;
346
347 xs_send(req, &msg);
348
349 ret = xs_wait_for_reply(req, &msg);
350 if (len)
351 *len = msg.len;
352
353 if (IS_ERR(ret))
354 return ret;
355
356 if (msg.type == XS_ERROR) {
357 err = get_error(ret);
358 kfree(ret);
359 return ERR_PTR(-err);
360 }
361
362 if (msg.type != type) {
363 pr_warn_ratelimited("unexpected type [%d], expected [%d]\n",
364 msg.type, type);
365 kfree(ret);
366 return ERR_PTR(-EINVAL);
367 }
368 return ret;
369 }
370
371 /* Simplified version of xs_talkv: single message. */
xs_single(struct xenbus_transaction t,enum xsd_sockmsg_type type,const char * string,unsigned int * len)372 static void *xs_single(struct xenbus_transaction t,
373 enum xsd_sockmsg_type type,
374 const char *string,
375 unsigned int *len)
376 {
377 struct kvec iovec;
378
379 iovec.iov_base = (void *)string;
380 iovec.iov_len = strlen(string) + 1;
381 return xs_talkv(t, type, &iovec, 1, len);
382 }
383
384 /* Many commands only need an ack, don't care what it says. */
xs_error(char * reply)385 static int xs_error(char *reply)
386 {
387 if (IS_ERR(reply))
388 return PTR_ERR(reply);
389 kfree(reply);
390 return 0;
391 }
392
count_strings(const char * strings,unsigned int len)393 static unsigned int count_strings(const char *strings, unsigned int len)
394 {
395 unsigned int num;
396 const char *p;
397
398 for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
399 num++;
400
401 return num;
402 }
403
404 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
join(const char * dir,const char * name)405 static char *join(const char *dir, const char *name)
406 {
407 char *buffer;
408
409 if (strlen(name) == 0)
410 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir);
411 else
412 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name);
413 return buffer ?: ERR_PTR(-ENOMEM);
414 }
415
split_strings(char * strings,unsigned int len,unsigned int * num)416 static char **split_strings(char *strings, unsigned int len, unsigned int *num)
417 {
418 char *p, **ret;
419
420 if (len && strings[len - 1]) {
421 pr_err_once("malformed XS_DIRECTORY reply\n");
422 kfree(strings);
423 return ERR_PTR(-EIO);
424 }
425
426 /* Count the strings. */
427 *num = count_strings(strings, len);
428
429 /* Transfer to one big alloc for easy freeing. */
430 ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH);
431 if (!ret) {
432 kfree(strings);
433 return ERR_PTR(-ENOMEM);
434 }
435 memcpy(&ret[*num], strings, len);
436 kfree(strings);
437
438 strings = (char *)&ret[*num];
439 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
440 ret[(*num)++] = p;
441
442 return ret;
443 }
444
xenbus_directory(struct xenbus_transaction t,const char * dir,const char * node,unsigned int * num)445 char **xenbus_directory(struct xenbus_transaction t,
446 const char *dir, const char *node, unsigned int *num)
447 {
448 char *strings, *path;
449 unsigned int len;
450
451 path = join(dir, node);
452 if (IS_ERR(path))
453 return ERR_CAST(path);
454
455 strings = xs_single(t, XS_DIRECTORY, path, &len);
456 kfree(path);
457 if (IS_ERR(strings))
458 return ERR_CAST(strings);
459
460 return split_strings(strings, len, num);
461 }
462 EXPORT_SYMBOL_GPL(xenbus_directory);
463
464 /* Check if a path exists. Return 1 if it does. */
xenbus_exists(struct xenbus_transaction t,const char * dir,const char * node)465 int xenbus_exists(struct xenbus_transaction t,
466 const char *dir, const char *node)
467 {
468 char **d;
469 int dir_n;
470
471 d = xenbus_directory(t, dir, node, &dir_n);
472 if (IS_ERR(d))
473 return 0;
474 kfree(d);
475 return 1;
476 }
477 EXPORT_SYMBOL_GPL(xenbus_exists);
478
479 /* Get the value of a single file.
480 * Returns a kmalloced value: call free() on it after use.
481 * len indicates length in bytes.
482 */
xenbus_read(struct xenbus_transaction t,const char * dir,const char * node,unsigned int * len)483 void *xenbus_read(struct xenbus_transaction t,
484 const char *dir, const char *node, unsigned int *len)
485 {
486 char *path;
487 void *ret;
488
489 path = join(dir, node);
490 if (IS_ERR(path))
491 return ERR_CAST(path);
492
493 ret = xs_single(t, XS_READ, path, len);
494 kfree(path);
495 return ret;
496 }
497 EXPORT_SYMBOL_GPL(xenbus_read);
498
499 /* Write the value of a single file.
500 * Returns -err on failure.
501 */
xenbus_write(struct xenbus_transaction t,const char * dir,const char * node,const char * string)502 int xenbus_write(struct xenbus_transaction t,
503 const char *dir, const char *node, const char *string)
504 {
505 const char *path;
506 struct kvec iovec[2];
507 int ret;
508
509 path = join(dir, node);
510 if (IS_ERR(path))
511 return PTR_ERR(path);
512
513 iovec[0].iov_base = (void *)path;
514 iovec[0].iov_len = strlen(path) + 1;
515 iovec[1].iov_base = (void *)string;
516 iovec[1].iov_len = strlen(string);
517
518 ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
519 kfree(path);
520 return ret;
521 }
522 EXPORT_SYMBOL_GPL(xenbus_write);
523
524 /* Destroy a file or directory (directories must be empty). */
xenbus_rm(struct xenbus_transaction t,const char * dir,const char * node)525 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
526 {
527 char *path;
528 int ret;
529
530 path = join(dir, node);
531 if (IS_ERR(path))
532 return PTR_ERR(path);
533
534 ret = xs_error(xs_single(t, XS_RM, path, NULL));
535 kfree(path);
536 return ret;
537 }
538 EXPORT_SYMBOL_GPL(xenbus_rm);
539
540 /* Start a transaction: changes by others will not be seen during this
541 * transaction, and changes will not be visible to others until end.
542 */
xenbus_transaction_start(struct xenbus_transaction * t)543 int xenbus_transaction_start(struct xenbus_transaction *t)
544 {
545 char *id_str;
546
547 id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
548 if (IS_ERR(id_str))
549 return PTR_ERR(id_str);
550
551 t->id = simple_strtoul(id_str, NULL, 0);
552 kfree(id_str);
553 return 0;
554 }
555 EXPORT_SYMBOL_GPL(xenbus_transaction_start);
556
557 /* End a transaction.
558 * If abort is true, transaction is discarded instead of committed.
559 */
xenbus_transaction_end(struct xenbus_transaction t,bool abort)560 int xenbus_transaction_end(struct xenbus_transaction t, bool abort)
561 {
562 return xs_error(xs_single(t, XS_TRANSACTION_END, abort ? "F" : "T",
563 NULL));
564 }
565 EXPORT_SYMBOL_GPL(xenbus_transaction_end);
566
567 /* Single read and scanf: returns -errno or num scanned. */
xenbus_scanf(struct xenbus_transaction t,const char * dir,const char * node,const char * fmt,...)568 int xenbus_scanf(struct xenbus_transaction t,
569 const char *dir, const char *node, const char *fmt, ...)
570 {
571 va_list ap;
572 int ret;
573 char *val;
574
575 val = xenbus_read(t, dir, node, NULL);
576 if (IS_ERR(val))
577 return PTR_ERR(val);
578
579 va_start(ap, fmt);
580 ret = vsscanf(val, fmt, ap);
581 va_end(ap);
582 kfree(val);
583 /* Distinctive errno. */
584 if (ret == 0)
585 return -ERANGE;
586 return ret;
587 }
588 EXPORT_SYMBOL_GPL(xenbus_scanf);
589
590 /* Read an (optional) unsigned value. */
xenbus_read_unsigned(const char * dir,const char * node,unsigned int default_val)591 unsigned int xenbus_read_unsigned(const char *dir, const char *node,
592 unsigned int default_val)
593 {
594 unsigned int val;
595 int ret;
596
597 ret = xenbus_scanf(XBT_NIL, dir, node, "%u", &val);
598 if (ret <= 0)
599 val = default_val;
600
601 return val;
602 }
603 EXPORT_SYMBOL_GPL(xenbus_read_unsigned);
604
605 /* Single printf and write: returns -errno or 0. */
xenbus_printf(struct xenbus_transaction t,const char * dir,const char * node,const char * fmt,...)606 int xenbus_printf(struct xenbus_transaction t,
607 const char *dir, const char *node, const char *fmt, ...)
608 {
609 va_list ap;
610 int ret;
611 char *buf;
612
613 va_start(ap, fmt);
614 buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap);
615 va_end(ap);
616
617 if (!buf)
618 return -ENOMEM;
619
620 ret = xenbus_write(t, dir, node, buf);
621
622 kfree(buf);
623
624 return ret;
625 }
626 EXPORT_SYMBOL_GPL(xenbus_printf);
627
628 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
xenbus_gather(struct xenbus_transaction t,const char * dir,...)629 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
630 {
631 va_list ap;
632 const char *name;
633 int ret = 0;
634
635 va_start(ap, dir);
636 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
637 const char *fmt = va_arg(ap, char *);
638 void *result = va_arg(ap, void *);
639 char *p;
640
641 p = xenbus_read(t, dir, name, NULL);
642 if (IS_ERR(p)) {
643 ret = PTR_ERR(p);
644 break;
645 }
646 if (fmt) {
647 if (sscanf(p, fmt, result) == 0)
648 ret = -EINVAL;
649 kfree(p);
650 } else
651 *(char **)result = p;
652 }
653 va_end(ap);
654 return ret;
655 }
656 EXPORT_SYMBOL_GPL(xenbus_gather);
657
xs_watch(const char * path,const char * token)658 static int xs_watch(const char *path, const char *token)
659 {
660 struct kvec iov[2];
661
662 iov[0].iov_base = (void *)path;
663 iov[0].iov_len = strlen(path) + 1;
664 iov[1].iov_base = (void *)token;
665 iov[1].iov_len = strlen(token) + 1;
666
667 return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
668 ARRAY_SIZE(iov), NULL));
669 }
670
xs_unwatch(const char * path,const char * token)671 static int xs_unwatch(const char *path, const char *token)
672 {
673 struct kvec iov[2];
674
675 iov[0].iov_base = (char *)path;
676 iov[0].iov_len = strlen(path) + 1;
677 iov[1].iov_base = (char *)token;
678 iov[1].iov_len = strlen(token) + 1;
679
680 return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
681 ARRAY_SIZE(iov), NULL));
682 }
683
find_watch(const char * token)684 static struct xenbus_watch *find_watch(const char *token)
685 {
686 struct xenbus_watch *i, *cmp;
687
688 cmp = (void *)simple_strtoul(token, NULL, 16);
689
690 list_for_each_entry(i, &watches, list)
691 if (i == cmp)
692 return i;
693
694 return NULL;
695 }
696
xs_watch_msg(struct xs_watch_event * event)697 int xs_watch_msg(struct xs_watch_event *event)
698 {
699 if (count_strings(event->body, event->len) != 2) {
700 kfree(event);
701 return -EINVAL;
702 }
703 event->path = (const char *)event->body;
704 event->token = (const char *)strchr(event->body, '\0') + 1;
705
706 spin_lock(&watches_lock);
707 event->handle = find_watch(event->token);
708 if (event->handle != NULL &&
709 (!event->handle->will_handle ||
710 event->handle->will_handle(event->handle,
711 event->path, event->token))) {
712 spin_lock(&watch_events_lock);
713 list_add_tail(&event->list, &watch_events);
714 event->handle->nr_pending++;
715 wake_up(&watch_events_waitq);
716 spin_unlock(&watch_events_lock);
717 } else
718 kfree(event);
719 spin_unlock(&watches_lock);
720
721 return 0;
722 }
723
xs_reset_watches(void)724 static void xs_reset_watches(void)
725 {
726 int err;
727
728 if (!xen_hvm_domain() || xen_initial_domain())
729 return;
730
731 if (!xenbus_read_unsigned("control",
732 "platform-feature-xs_reset_watches", 0))
733 return;
734
735 err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL));
736 if (err && err != -EEXIST)
737 pr_warn("xs_reset_watches failed: %d\n", err);
738 }
739
740 /* Register callback to watch this node. */
register_xenbus_watch(struct xenbus_watch * watch)741 int register_xenbus_watch(struct xenbus_watch *watch)
742 {
743 /* Pointer in ascii is the token. */
744 char token[sizeof(watch) * 2 + 1];
745 int err;
746
747 sprintf(token, "%lX", (long)watch);
748
749 watch->nr_pending = 0;
750
751 down_read(&xs_watch_rwsem);
752
753 spin_lock(&watches_lock);
754 BUG_ON(find_watch(token));
755 list_add(&watch->list, &watches);
756 spin_unlock(&watches_lock);
757
758 err = xs_watch(watch->node, token);
759
760 if (err) {
761 spin_lock(&watches_lock);
762 list_del(&watch->list);
763 spin_unlock(&watches_lock);
764 }
765
766 up_read(&xs_watch_rwsem);
767
768 return err;
769 }
770 EXPORT_SYMBOL_GPL(register_xenbus_watch);
771
unregister_xenbus_watch(struct xenbus_watch * watch)772 void unregister_xenbus_watch(struct xenbus_watch *watch)
773 {
774 struct xs_watch_event *event, *tmp;
775 char token[sizeof(watch) * 2 + 1];
776 int err;
777
778 sprintf(token, "%lX", (long)watch);
779
780 down_read(&xs_watch_rwsem);
781
782 spin_lock(&watches_lock);
783 BUG_ON(!find_watch(token));
784 list_del(&watch->list);
785 spin_unlock(&watches_lock);
786
787 err = xs_unwatch(watch->node, token);
788 if (err)
789 pr_warn("Failed to release watch %s: %i\n", watch->node, err);
790
791 up_read(&xs_watch_rwsem);
792
793 /* Make sure there are no callbacks running currently (unless
794 its us) */
795 if (current->pid != xenwatch_pid)
796 mutex_lock(&xenwatch_mutex);
797
798 /* Cancel pending watch events. */
799 spin_lock(&watch_events_lock);
800 if (watch->nr_pending) {
801 list_for_each_entry_safe(event, tmp, &watch_events, list) {
802 if (event->handle != watch)
803 continue;
804 list_del(&event->list);
805 kfree(event);
806 }
807 watch->nr_pending = 0;
808 }
809 spin_unlock(&watch_events_lock);
810
811 if (current->pid != xenwatch_pid)
812 mutex_unlock(&xenwatch_mutex);
813 }
814 EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
815
xs_suspend(void)816 void xs_suspend(void)
817 {
818 xs_suspend_enter();
819
820 mutex_lock(&xs_response_mutex);
821 down_write(&xs_watch_rwsem);
822 }
823
xs_resume(void)824 void xs_resume(void)
825 {
826 struct xenbus_watch *watch;
827 char token[sizeof(watch) * 2 + 1];
828
829 xb_init_comms();
830
831 mutex_unlock(&xs_response_mutex);
832
833 xs_suspend_exit();
834
835 /* No need for watches_lock: the xs_watch_rwsem is sufficient. */
836 list_for_each_entry(watch, &watches, list) {
837 sprintf(token, "%lX", (long)watch);
838 xs_watch(watch->node, token);
839 }
840
841 up_write(&xs_watch_rwsem);
842 }
843
xs_suspend_cancel(void)844 void xs_suspend_cancel(void)
845 {
846 up_write(&xs_watch_rwsem);
847 mutex_unlock(&xs_response_mutex);
848
849 xs_suspend_exit();
850 }
851
xenwatch_thread(void * unused)852 static int xenwatch_thread(void *unused)
853 {
854 struct xs_watch_event *event;
855
856 xenwatch_pid = current->pid;
857
858 for (;;) {
859 wait_event_interruptible(watch_events_waitq,
860 !list_empty(&watch_events));
861
862 if (kthread_should_stop())
863 break;
864
865 mutex_lock(&xenwatch_mutex);
866
867 spin_lock(&watch_events_lock);
868 event = list_first_entry_or_null(&watch_events,
869 struct xs_watch_event, list);
870 if (event) {
871 list_del(&event->list);
872 event->handle->nr_pending--;
873 }
874 spin_unlock(&watch_events_lock);
875
876 if (event) {
877 event->handle->callback(event->handle, event->path,
878 event->token);
879 kfree(event);
880 }
881
882 mutex_unlock(&xenwatch_mutex);
883 }
884
885 return 0;
886 }
887
888 /*
889 * Wake up all threads waiting for a xenstore reply. In case of shutdown all
890 * pending replies will be marked as "aborted" in order to let the waiters
891 * return in spite of xenstore possibly no longer being able to reply. This
892 * will avoid blocking shutdown by a thread waiting for xenstore but being
893 * necessary for shutdown processing to proceed.
894 */
xs_reboot_notify(struct notifier_block * nb,unsigned long code,void * unused)895 static int xs_reboot_notify(struct notifier_block *nb,
896 unsigned long code, void *unused)
897 {
898 struct xb_req_data *req;
899
900 mutex_lock(&xb_write_mutex);
901 list_for_each_entry(req, &xs_reply_list, list)
902 wake_up(&req->wq);
903 list_for_each_entry(req, &xb_write_list, list)
904 wake_up(&req->wq);
905 mutex_unlock(&xb_write_mutex);
906 return NOTIFY_DONE;
907 }
908
909 static struct notifier_block xs_reboot_nb = {
910 .notifier_call = xs_reboot_notify,
911 };
912
xs_init(void)913 int xs_init(void)
914 {
915 int err;
916 struct task_struct *task;
917
918 err = register_reboot_notifier(&xs_reboot_nb);
919 if (err)
920 return err;
921
922 /* Initialize the shared memory rings to talk to xenstored */
923 err = xb_init_comms();
924 if (err)
925 goto err_unregister_reboot_notifier;
926
927 task = kthread_run(xenwatch_thread, NULL, "xenwatch");
928 if (IS_ERR(task)) {
929 err = PTR_ERR(task);
930 goto err_unregister_reboot_notifier;
931 }
932
933 /* shutdown watches for kexec boot */
934 xs_reset_watches();
935
936 return 0;
937
938 err_unregister_reboot_notifier:
939 unregister_reboot_notifier(&xs_reboot_nb);
940 return err;
941 }
942