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/rwsem.h>
47 #include <linux/module.h>
48 #include <linux/mutex.h>
49 #include <asm/xen/hypervisor.h>
50 #include <xen/xenbus.h>
51 #include <xen/xen.h>
52 #include "xenbus_comms.h"
53 #include "xenbus_probe.h"
54
55 struct xs_stored_msg {
56 struct list_head list;
57
58 struct xsd_sockmsg hdr;
59
60 union {
61 /* Queued replies. */
62 struct {
63 char *body;
64 } reply;
65
66 /* Queued watch events. */
67 struct {
68 struct xenbus_watch *handle;
69 char **vec;
70 unsigned int vec_size;
71 } watch;
72 } u;
73 };
74
75 struct xs_handle {
76 /* A list of replies. Currently only one will ever be outstanding. */
77 struct list_head reply_list;
78 spinlock_t reply_lock;
79 wait_queue_head_t reply_waitq;
80
81 /*
82 * Mutex ordering: transaction_mutex -> watch_mutex -> request_mutex.
83 * response_mutex is never taken simultaneously with the other three.
84 *
85 * transaction_mutex must be held before incrementing
86 * transaction_count. The mutex is held when a suspend is in
87 * progress to prevent new transactions starting.
88 *
89 * When decrementing transaction_count to zero the wait queue
90 * should be woken up, the suspend code waits for count to
91 * reach zero.
92 */
93
94 /* One request at a time. */
95 struct mutex request_mutex;
96
97 /* Protect xenbus reader thread against save/restore. */
98 struct mutex response_mutex;
99
100 /* Protect transactions against save/restore. */
101 struct mutex transaction_mutex;
102 atomic_t transaction_count;
103 wait_queue_head_t transaction_wq;
104
105 /* Protect watch (de)register against save/restore. */
106 struct rw_semaphore watch_mutex;
107 };
108
109 static struct xs_handle xs_state;
110
111 /* List of registered watches, and a lock to protect it. */
112 static LIST_HEAD(watches);
113 static DEFINE_SPINLOCK(watches_lock);
114
115 /* List of pending watch callback events, and a lock to protect it. */
116 static LIST_HEAD(watch_events);
117 static DEFINE_SPINLOCK(watch_events_lock);
118
119 /*
120 * Details of the xenwatch callback kernel thread. The thread waits on the
121 * watch_events_waitq for work to do (queued on watch_events list). When it
122 * wakes up it acquires the xenwatch_mutex before reading the list and
123 * carrying out work.
124 */
125 static pid_t xenwatch_pid;
126 static DEFINE_MUTEX(xenwatch_mutex);
127 static DECLARE_WAIT_QUEUE_HEAD(watch_events_waitq);
128
get_error(const char * errorstring)129 static int get_error(const char *errorstring)
130 {
131 unsigned int i;
132
133 for (i = 0; strcmp(errorstring, xsd_errors[i].errstring) != 0; i++) {
134 if (i == ARRAY_SIZE(xsd_errors) - 1) {
135 pr_warn("xen store gave: unknown error %s\n",
136 errorstring);
137 return EINVAL;
138 }
139 }
140 return xsd_errors[i].errnum;
141 }
142
xenbus_ok(void)143 static bool xenbus_ok(void)
144 {
145 switch (xen_store_domain_type) {
146 case XS_LOCAL:
147 switch (system_state) {
148 case SYSTEM_POWER_OFF:
149 case SYSTEM_RESTART:
150 case SYSTEM_HALT:
151 return false;
152 default:
153 break;
154 }
155 return true;
156 case XS_PV:
157 case XS_HVM:
158 /* FIXME: Could check that the remote domain is alive,
159 * but it is normally initial domain. */
160 return true;
161 default:
162 break;
163 }
164 return false;
165 }
read_reply(enum xsd_sockmsg_type * type,unsigned int * len)166 static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len)
167 {
168 struct xs_stored_msg *msg;
169 char *body;
170
171 spin_lock(&xs_state.reply_lock);
172
173 while (list_empty(&xs_state.reply_list)) {
174 spin_unlock(&xs_state.reply_lock);
175 if (xenbus_ok())
176 /* XXX FIXME: Avoid synchronous wait for response here. */
177 wait_event_timeout(xs_state.reply_waitq,
178 !list_empty(&xs_state.reply_list),
179 msecs_to_jiffies(500));
180 else {
181 /*
182 * If we are in the process of being shut-down there is
183 * no point of trying to contact XenBus - it is either
184 * killed (xenstored application) or the other domain
185 * has been killed or is unreachable.
186 */
187 return ERR_PTR(-EIO);
188 }
189 spin_lock(&xs_state.reply_lock);
190 }
191
192 msg = list_entry(xs_state.reply_list.next,
193 struct xs_stored_msg, list);
194 list_del(&msg->list);
195
196 spin_unlock(&xs_state.reply_lock);
197
198 *type = msg->hdr.type;
199 if (len)
200 *len = msg->hdr.len;
201 body = msg->u.reply.body;
202
203 kfree(msg);
204
205 return body;
206 }
207
transaction_start(void)208 static void transaction_start(void)
209 {
210 mutex_lock(&xs_state.transaction_mutex);
211 atomic_inc(&xs_state.transaction_count);
212 mutex_unlock(&xs_state.transaction_mutex);
213 }
214
transaction_end(void)215 static void transaction_end(void)
216 {
217 if (atomic_dec_and_test(&xs_state.transaction_count))
218 wake_up(&xs_state.transaction_wq);
219 }
220
transaction_suspend(void)221 static void transaction_suspend(void)
222 {
223 mutex_lock(&xs_state.transaction_mutex);
224 wait_event(xs_state.transaction_wq,
225 atomic_read(&xs_state.transaction_count) == 0);
226 }
227
transaction_resume(void)228 static void transaction_resume(void)
229 {
230 mutex_unlock(&xs_state.transaction_mutex);
231 }
232
xenbus_dev_request_and_reply(struct xsd_sockmsg * msg)233 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg)
234 {
235 void *ret;
236 struct xsd_sockmsg req_msg = *msg;
237 int err;
238
239 if (req_msg.type == XS_TRANSACTION_START)
240 transaction_start();
241
242 mutex_lock(&xs_state.request_mutex);
243
244 err = xb_write(msg, sizeof(*msg) + msg->len);
245 if (err) {
246 msg->type = XS_ERROR;
247 ret = ERR_PTR(err);
248 } else
249 ret = read_reply(&msg->type, &msg->len);
250
251 mutex_unlock(&xs_state.request_mutex);
252
253 if ((msg->type == XS_TRANSACTION_END) ||
254 ((req_msg.type == XS_TRANSACTION_START) &&
255 (msg->type == XS_ERROR)))
256 transaction_end();
257
258 return ret;
259 }
260 EXPORT_SYMBOL(xenbus_dev_request_and_reply);
261
262 /* 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)263 static void *xs_talkv(struct xenbus_transaction t,
264 enum xsd_sockmsg_type type,
265 const struct kvec *iovec,
266 unsigned int num_vecs,
267 unsigned int *len)
268 {
269 struct xsd_sockmsg msg;
270 void *ret = NULL;
271 unsigned int i;
272 int err;
273
274 msg.tx_id = t.id;
275 msg.req_id = 0;
276 msg.type = type;
277 msg.len = 0;
278 for (i = 0; i < num_vecs; i++)
279 msg.len += iovec[i].iov_len;
280
281 mutex_lock(&xs_state.request_mutex);
282
283 err = xb_write(&msg, sizeof(msg));
284 if (err) {
285 mutex_unlock(&xs_state.request_mutex);
286 return ERR_PTR(err);
287 }
288
289 for (i = 0; i < num_vecs; i++) {
290 err = xb_write(iovec[i].iov_base, iovec[i].iov_len);
291 if (err) {
292 mutex_unlock(&xs_state.request_mutex);
293 return ERR_PTR(err);
294 }
295 }
296
297 ret = read_reply(&msg.type, len);
298
299 mutex_unlock(&xs_state.request_mutex);
300
301 if (IS_ERR(ret))
302 return ret;
303
304 if (msg.type == XS_ERROR) {
305 err = get_error(ret);
306 kfree(ret);
307 return ERR_PTR(-err);
308 }
309
310 if (msg.type != type) {
311 pr_warn_ratelimited("unexpected type [%d], expected [%d]\n",
312 msg.type, type);
313 kfree(ret);
314 return ERR_PTR(-EINVAL);
315 }
316 return ret;
317 }
318
319 /* Simplified version of xs_talkv: single message. */
xs_single(struct xenbus_transaction t,enum xsd_sockmsg_type type,const char * string,unsigned int * len)320 static void *xs_single(struct xenbus_transaction t,
321 enum xsd_sockmsg_type type,
322 const char *string,
323 unsigned int *len)
324 {
325 struct kvec iovec;
326
327 iovec.iov_base = (void *)string;
328 iovec.iov_len = strlen(string) + 1;
329 return xs_talkv(t, type, &iovec, 1, len);
330 }
331
332 /* Many commands only need an ack, don't care what it says. */
xs_error(char * reply)333 static int xs_error(char *reply)
334 {
335 if (IS_ERR(reply))
336 return PTR_ERR(reply);
337 kfree(reply);
338 return 0;
339 }
340
count_strings(const char * strings,unsigned int len)341 static unsigned int count_strings(const char *strings, unsigned int len)
342 {
343 unsigned int num;
344 const char *p;
345
346 for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
347 num++;
348
349 return num;
350 }
351
352 /* Return the path to dir with /name appended. Buffer must be kfree()'ed. */
join(const char * dir,const char * name)353 static char *join(const char *dir, const char *name)
354 {
355 char *buffer;
356
357 if (strlen(name) == 0)
358 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s", dir);
359 else
360 buffer = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/%s", dir, name);
361 return (!buffer) ? ERR_PTR(-ENOMEM) : buffer;
362 }
363
split(char * strings,unsigned int len,unsigned int * num)364 static char **split(char *strings, unsigned int len, unsigned int *num)
365 {
366 char *p, **ret;
367
368 /* Count the strings. */
369 *num = count_strings(strings, len);
370
371 /* Transfer to one big alloc for easy freeing. */
372 ret = kmalloc(*num * sizeof(char *) + len, GFP_NOIO | __GFP_HIGH);
373 if (!ret) {
374 kfree(strings);
375 return ERR_PTR(-ENOMEM);
376 }
377 memcpy(&ret[*num], strings, len);
378 kfree(strings);
379
380 strings = (char *)&ret[*num];
381 for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
382 ret[(*num)++] = p;
383
384 return ret;
385 }
386
xenbus_directory(struct xenbus_transaction t,const char * dir,const char * node,unsigned int * num)387 char **xenbus_directory(struct xenbus_transaction t,
388 const char *dir, const char *node, unsigned int *num)
389 {
390 char *strings, *path;
391 unsigned int len;
392
393 path = join(dir, node);
394 if (IS_ERR(path))
395 return (char **)path;
396
397 strings = xs_single(t, XS_DIRECTORY, path, &len);
398 kfree(path);
399 if (IS_ERR(strings))
400 return (char **)strings;
401
402 return split(strings, len, num);
403 }
404 EXPORT_SYMBOL_GPL(xenbus_directory);
405
406 /* Check if a path exists. Return 1 if it does. */
xenbus_exists(struct xenbus_transaction t,const char * dir,const char * node)407 int xenbus_exists(struct xenbus_transaction t,
408 const char *dir, const char *node)
409 {
410 char **d;
411 int dir_n;
412
413 d = xenbus_directory(t, dir, node, &dir_n);
414 if (IS_ERR(d))
415 return 0;
416 kfree(d);
417 return 1;
418 }
419 EXPORT_SYMBOL_GPL(xenbus_exists);
420
421 /* Get the value of a single file.
422 * Returns a kmalloced value: call free() on it after use.
423 * len indicates length in bytes.
424 */
xenbus_read(struct xenbus_transaction t,const char * dir,const char * node,unsigned int * len)425 void *xenbus_read(struct xenbus_transaction t,
426 const char *dir, const char *node, unsigned int *len)
427 {
428 char *path;
429 void *ret;
430
431 path = join(dir, node);
432 if (IS_ERR(path))
433 return (void *)path;
434
435 ret = xs_single(t, XS_READ, path, len);
436 kfree(path);
437 return ret;
438 }
439 EXPORT_SYMBOL_GPL(xenbus_read);
440
441 /* Write the value of a single file.
442 * Returns -err on failure.
443 */
xenbus_write(struct xenbus_transaction t,const char * dir,const char * node,const char * string)444 int xenbus_write(struct xenbus_transaction t,
445 const char *dir, const char *node, const char *string)
446 {
447 const char *path;
448 struct kvec iovec[2];
449 int ret;
450
451 path = join(dir, node);
452 if (IS_ERR(path))
453 return PTR_ERR(path);
454
455 iovec[0].iov_base = (void *)path;
456 iovec[0].iov_len = strlen(path) + 1;
457 iovec[1].iov_base = (void *)string;
458 iovec[1].iov_len = strlen(string);
459
460 ret = xs_error(xs_talkv(t, XS_WRITE, iovec, ARRAY_SIZE(iovec), NULL));
461 kfree(path);
462 return ret;
463 }
464 EXPORT_SYMBOL_GPL(xenbus_write);
465
466 /* Create a new directory. */
xenbus_mkdir(struct xenbus_transaction t,const char * dir,const char * node)467 int xenbus_mkdir(struct xenbus_transaction t,
468 const char *dir, const char *node)
469 {
470 char *path;
471 int ret;
472
473 path = join(dir, node);
474 if (IS_ERR(path))
475 return PTR_ERR(path);
476
477 ret = xs_error(xs_single(t, XS_MKDIR, path, NULL));
478 kfree(path);
479 return ret;
480 }
481 EXPORT_SYMBOL_GPL(xenbus_mkdir);
482
483 /* Destroy a file or directory (directories must be empty). */
xenbus_rm(struct xenbus_transaction t,const char * dir,const char * node)484 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
485 {
486 char *path;
487 int ret;
488
489 path = join(dir, node);
490 if (IS_ERR(path))
491 return PTR_ERR(path);
492
493 ret = xs_error(xs_single(t, XS_RM, path, NULL));
494 kfree(path);
495 return ret;
496 }
497 EXPORT_SYMBOL_GPL(xenbus_rm);
498
499 /* Start a transaction: changes by others will not be seen during this
500 * transaction, and changes will not be visible to others until end.
501 */
xenbus_transaction_start(struct xenbus_transaction * t)502 int xenbus_transaction_start(struct xenbus_transaction *t)
503 {
504 char *id_str;
505
506 transaction_start();
507
508 id_str = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL);
509 if (IS_ERR(id_str)) {
510 transaction_end();
511 return PTR_ERR(id_str);
512 }
513
514 t->id = simple_strtoul(id_str, NULL, 0);
515 kfree(id_str);
516 return 0;
517 }
518 EXPORT_SYMBOL_GPL(xenbus_transaction_start);
519
520 /* End a transaction.
521 * If abandon is true, transaction is discarded instead of committed.
522 */
xenbus_transaction_end(struct xenbus_transaction t,int abort)523 int xenbus_transaction_end(struct xenbus_transaction t, int abort)
524 {
525 char abortstr[2];
526 int err;
527
528 if (abort)
529 strcpy(abortstr, "F");
530 else
531 strcpy(abortstr, "T");
532
533 err = xs_error(xs_single(t, XS_TRANSACTION_END, abortstr, NULL));
534
535 transaction_end();
536
537 return err;
538 }
539 EXPORT_SYMBOL_GPL(xenbus_transaction_end);
540
541 /* Single read and scanf: returns -errno or num scanned. */
xenbus_scanf(struct xenbus_transaction t,const char * dir,const char * node,const char * fmt,...)542 int xenbus_scanf(struct xenbus_transaction t,
543 const char *dir, const char *node, const char *fmt, ...)
544 {
545 va_list ap;
546 int ret;
547 char *val;
548
549 val = xenbus_read(t, dir, node, NULL);
550 if (IS_ERR(val))
551 return PTR_ERR(val);
552
553 va_start(ap, fmt);
554 ret = vsscanf(val, fmt, ap);
555 va_end(ap);
556 kfree(val);
557 /* Distinctive errno. */
558 if (ret == 0)
559 return -ERANGE;
560 return ret;
561 }
562 EXPORT_SYMBOL_GPL(xenbus_scanf);
563
564 /* Single printf and write: returns -errno or 0. */
xenbus_printf(struct xenbus_transaction t,const char * dir,const char * node,const char * fmt,...)565 int xenbus_printf(struct xenbus_transaction t,
566 const char *dir, const char *node, const char *fmt, ...)
567 {
568 va_list ap;
569 int ret;
570 char *buf;
571
572 va_start(ap, fmt);
573 buf = kvasprintf(GFP_NOIO | __GFP_HIGH, fmt, ap);
574 va_end(ap);
575
576 if (!buf)
577 return -ENOMEM;
578
579 ret = xenbus_write(t, dir, node, buf);
580
581 kfree(buf);
582
583 return ret;
584 }
585 EXPORT_SYMBOL_GPL(xenbus_printf);
586
587 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
xenbus_gather(struct xenbus_transaction t,const char * dir,...)588 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
589 {
590 va_list ap;
591 const char *name;
592 int ret = 0;
593
594 va_start(ap, dir);
595 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
596 const char *fmt = va_arg(ap, char *);
597 void *result = va_arg(ap, void *);
598 char *p;
599
600 p = xenbus_read(t, dir, name, NULL);
601 if (IS_ERR(p)) {
602 ret = PTR_ERR(p);
603 break;
604 }
605 if (fmt) {
606 if (sscanf(p, fmt, result) == 0)
607 ret = -EINVAL;
608 kfree(p);
609 } else
610 *(char **)result = p;
611 }
612 va_end(ap);
613 return ret;
614 }
615 EXPORT_SYMBOL_GPL(xenbus_gather);
616
xs_watch(const char * path,const char * token)617 static int xs_watch(const char *path, const char *token)
618 {
619 struct kvec iov[2];
620
621 iov[0].iov_base = (void *)path;
622 iov[0].iov_len = strlen(path) + 1;
623 iov[1].iov_base = (void *)token;
624 iov[1].iov_len = strlen(token) + 1;
625
626 return xs_error(xs_talkv(XBT_NIL, XS_WATCH, iov,
627 ARRAY_SIZE(iov), NULL));
628 }
629
xs_unwatch(const char * path,const char * token)630 static int xs_unwatch(const char *path, const char *token)
631 {
632 struct kvec iov[2];
633
634 iov[0].iov_base = (char *)path;
635 iov[0].iov_len = strlen(path) + 1;
636 iov[1].iov_base = (char *)token;
637 iov[1].iov_len = strlen(token) + 1;
638
639 return xs_error(xs_talkv(XBT_NIL, XS_UNWATCH, iov,
640 ARRAY_SIZE(iov), NULL));
641 }
642
find_watch(const char * token)643 static struct xenbus_watch *find_watch(const char *token)
644 {
645 struct xenbus_watch *i, *cmp;
646
647 cmp = (void *)simple_strtoul(token, NULL, 16);
648
649 list_for_each_entry(i, &watches, list)
650 if (i == cmp)
651 return i;
652
653 return NULL;
654 }
655 /*
656 * Certain older XenBus toolstack cannot handle reading values that are
657 * not populated. Some Xen 3.4 installation are incapable of doing this
658 * so if we are running on anything older than 4 do not attempt to read
659 * control/platform-feature-xs_reset_watches.
660 */
xen_strict_xenbus_quirk(void)661 static bool xen_strict_xenbus_quirk(void)
662 {
663 #ifdef CONFIG_X86
664 uint32_t eax, ebx, ecx, edx, base;
665
666 base = xen_cpuid_base();
667 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
668
669 if ((eax >> 16) < 4)
670 return true;
671 #endif
672 return false;
673
674 }
xs_reset_watches(void)675 static void xs_reset_watches(void)
676 {
677 int err, supported = 0;
678
679 if (!xen_hvm_domain() || xen_initial_domain())
680 return;
681
682 if (xen_strict_xenbus_quirk())
683 return;
684
685 err = xenbus_scanf(XBT_NIL, "control",
686 "platform-feature-xs_reset_watches", "%d", &supported);
687 if (err != 1 || !supported)
688 return;
689
690 err = xs_error(xs_single(XBT_NIL, XS_RESET_WATCHES, "", NULL));
691 if (err && err != -EEXIST)
692 pr_warn("xs_reset_watches failed: %d\n", err);
693 }
694
695 /* Register callback to watch this node. */
register_xenbus_watch(struct xenbus_watch * watch)696 int register_xenbus_watch(struct xenbus_watch *watch)
697 {
698 /* Pointer in ascii is the token. */
699 char token[sizeof(watch) * 2 + 1];
700 int err;
701
702 sprintf(token, "%lX", (long)watch);
703
704 watch->nr_pending = 0;
705
706 down_read(&xs_state.watch_mutex);
707
708 spin_lock(&watches_lock);
709 BUG_ON(find_watch(token));
710 list_add(&watch->list, &watches);
711 spin_unlock(&watches_lock);
712
713 err = xs_watch(watch->node, token);
714
715 if (err) {
716 spin_lock(&watches_lock);
717 list_del(&watch->list);
718 spin_unlock(&watches_lock);
719 }
720
721 up_read(&xs_state.watch_mutex);
722
723 return err;
724 }
725 EXPORT_SYMBOL_GPL(register_xenbus_watch);
726
unregister_xenbus_watch(struct xenbus_watch * watch)727 void unregister_xenbus_watch(struct xenbus_watch *watch)
728 {
729 struct xs_stored_msg *msg, *tmp;
730 char token[sizeof(watch) * 2 + 1];
731 int err;
732
733 sprintf(token, "%lX", (long)watch);
734
735 down_read(&xs_state.watch_mutex);
736
737 spin_lock(&watches_lock);
738 BUG_ON(!find_watch(token));
739 list_del(&watch->list);
740 spin_unlock(&watches_lock);
741
742 err = xs_unwatch(watch->node, token);
743 if (err)
744 pr_warn("Failed to release watch %s: %i\n", watch->node, err);
745
746 up_read(&xs_state.watch_mutex);
747
748 /* Make sure there are no callbacks running currently (unless
749 its us) */
750 if (current->pid != xenwatch_pid)
751 mutex_lock(&xenwatch_mutex);
752
753 /* Cancel pending watch events. */
754 spin_lock(&watch_events_lock);
755 if (watch->nr_pending) {
756 list_for_each_entry_safe(msg, tmp, &watch_events, list) {
757 if (msg->u.watch.handle != watch)
758 continue;
759 list_del(&msg->list);
760 kfree(msg->u.watch.vec);
761 kfree(msg);
762 }
763 watch->nr_pending = 0;
764 }
765 spin_unlock(&watch_events_lock);
766
767 if (current->pid != xenwatch_pid)
768 mutex_unlock(&xenwatch_mutex);
769 }
770 EXPORT_SYMBOL_GPL(unregister_xenbus_watch);
771
xs_suspend(void)772 void xs_suspend(void)
773 {
774 transaction_suspend();
775 down_write(&xs_state.watch_mutex);
776 mutex_lock(&xs_state.request_mutex);
777 mutex_lock(&xs_state.response_mutex);
778 }
779
xs_resume(void)780 void xs_resume(void)
781 {
782 struct xenbus_watch *watch;
783 char token[sizeof(watch) * 2 + 1];
784
785 xb_init_comms();
786
787 mutex_unlock(&xs_state.response_mutex);
788 mutex_unlock(&xs_state.request_mutex);
789 transaction_resume();
790
791 /* No need for watches_lock: the watch_mutex is sufficient. */
792 list_for_each_entry(watch, &watches, list) {
793 sprintf(token, "%lX", (long)watch);
794 xs_watch(watch->node, token);
795 }
796
797 up_write(&xs_state.watch_mutex);
798 }
799
xs_suspend_cancel(void)800 void xs_suspend_cancel(void)
801 {
802 mutex_unlock(&xs_state.response_mutex);
803 mutex_unlock(&xs_state.request_mutex);
804 up_write(&xs_state.watch_mutex);
805 mutex_unlock(&xs_state.transaction_mutex);
806 }
807
xenwatch_thread(void * unused)808 static int xenwatch_thread(void *unused)
809 {
810 struct xs_stored_msg *msg;
811
812 for (;;) {
813 wait_event_interruptible(watch_events_waitq,
814 !list_empty(&watch_events));
815
816 if (kthread_should_stop())
817 break;
818
819 mutex_lock(&xenwatch_mutex);
820
821 spin_lock(&watch_events_lock);
822 msg = list_first_entry_or_null(&watch_events,
823 struct xs_stored_msg, list);
824 if (msg) {
825 list_del(&msg->list);
826 msg->u.watch.handle->nr_pending--;
827 }
828 spin_unlock(&watch_events_lock);
829
830 if (msg) {
831 msg->u.watch.handle->callback(
832 msg->u.watch.handle,
833 (const char **)msg->u.watch.vec,
834 msg->u.watch.vec_size);
835 kfree(msg->u.watch.vec);
836 kfree(msg);
837 }
838
839 mutex_unlock(&xenwatch_mutex);
840 }
841
842 return 0;
843 }
844
process_msg(void)845 static int process_msg(void)
846 {
847 struct xs_stored_msg *msg;
848 char *body;
849 int err;
850
851 /*
852 * We must disallow save/restore while reading a xenstore message.
853 * A partial read across s/r leaves us out of sync with xenstored.
854 */
855 for (;;) {
856 err = xb_wait_for_data_to_read();
857 if (err)
858 return err;
859 mutex_lock(&xs_state.response_mutex);
860 if (xb_data_to_read())
861 break;
862 /* We raced with save/restore: pending data 'disappeared'. */
863 mutex_unlock(&xs_state.response_mutex);
864 }
865
866
867 msg = kmalloc(sizeof(*msg), GFP_NOIO | __GFP_HIGH);
868 if (msg == NULL) {
869 err = -ENOMEM;
870 goto out;
871 }
872
873 err = xb_read(&msg->hdr, sizeof(msg->hdr));
874 if (err) {
875 kfree(msg);
876 goto out;
877 }
878
879 if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
880 kfree(msg);
881 err = -EINVAL;
882 goto out;
883 }
884
885 body = kmalloc(msg->hdr.len + 1, GFP_NOIO | __GFP_HIGH);
886 if (body == NULL) {
887 kfree(msg);
888 err = -ENOMEM;
889 goto out;
890 }
891
892 err = xb_read(body, msg->hdr.len);
893 if (err) {
894 kfree(body);
895 kfree(msg);
896 goto out;
897 }
898 body[msg->hdr.len] = '\0';
899
900 if (msg->hdr.type == XS_WATCH_EVENT) {
901 msg->u.watch.vec = split(body, msg->hdr.len,
902 &msg->u.watch.vec_size);
903 if (IS_ERR(msg->u.watch.vec)) {
904 err = PTR_ERR(msg->u.watch.vec);
905 kfree(msg);
906 goto out;
907 }
908
909 spin_lock(&watches_lock);
910 msg->u.watch.handle = find_watch(
911 msg->u.watch.vec[XS_WATCH_TOKEN]);
912 if (msg->u.watch.handle != NULL &&
913 (!msg->u.watch.handle->will_handle ||
914 msg->u.watch.handle->will_handle(
915 msg->u.watch.handle,
916 (const char **)msg->u.watch.vec,
917 msg->u.watch.vec_size))) {
918 spin_lock(&watch_events_lock);
919 list_add_tail(&msg->list, &watch_events);
920 msg->u.watch.handle->nr_pending++;
921 wake_up(&watch_events_waitq);
922 spin_unlock(&watch_events_lock);
923 } else {
924 kfree(msg->u.watch.vec);
925 kfree(msg);
926 }
927 spin_unlock(&watches_lock);
928 } else {
929 msg->u.reply.body = body;
930 spin_lock(&xs_state.reply_lock);
931 list_add_tail(&msg->list, &xs_state.reply_list);
932 spin_unlock(&xs_state.reply_lock);
933 wake_up(&xs_state.reply_waitq);
934 }
935
936 out:
937 mutex_unlock(&xs_state.response_mutex);
938 return err;
939 }
940
xenbus_thread(void * unused)941 static int xenbus_thread(void *unused)
942 {
943 int err;
944
945 for (;;) {
946 err = process_msg();
947 if (err)
948 pr_warn("error %d while reading message\n", err);
949 if (kthread_should_stop())
950 break;
951 }
952
953 return 0;
954 }
955
xs_init(void)956 int xs_init(void)
957 {
958 int err;
959 struct task_struct *task;
960
961 INIT_LIST_HEAD(&xs_state.reply_list);
962 spin_lock_init(&xs_state.reply_lock);
963 init_waitqueue_head(&xs_state.reply_waitq);
964
965 mutex_init(&xs_state.request_mutex);
966 mutex_init(&xs_state.response_mutex);
967 mutex_init(&xs_state.transaction_mutex);
968 init_rwsem(&xs_state.watch_mutex);
969 atomic_set(&xs_state.transaction_count, 0);
970 init_waitqueue_head(&xs_state.transaction_wq);
971
972 /* Initialize the shared memory rings to talk to xenstored */
973 err = xb_init_comms();
974 if (err)
975 return err;
976
977 task = kthread_run(xenwatch_thread, NULL, "xenwatch");
978 if (IS_ERR(task))
979 return PTR_ERR(task);
980 xenwatch_pid = task->pid;
981
982 task = kthread_run(xenbus_thread, NULL, "xenbus");
983 if (IS_ERR(task))
984 return PTR_ERR(task);
985
986 /* shutdown watches for kexec boot */
987 xs_reset_watches();
988
989 return 0;
990 }
991