Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / hw / i386 / kvm / xenstore_impl.c
blob1d134a686648577eb68bf8a567af2cf4b196e26f
1 /*
2 * QEMU Xen emulation: The actual implementation of XenStore
4 * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * Authors: David Woodhouse <dwmw2@infradead.org>, Paul Durrant <paul@xen.org>
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "qom/object.h"
15 #include "hw/xen/xen.h"
17 #include "xen_xenstore.h"
18 #include "xenstore_impl.h"
20 #include "hw/xen/interface/io/xs_wire.h"
22 #define XS_MAX_WATCHES 128
23 #define XS_MAX_DOMAIN_NODES 1000
24 #define XS_MAX_NODE_SIZE 2048
25 #define XS_MAX_TRANSACTIONS 10
26 #define XS_MAX_PERMS_PER_NODE 5
28 #define XS_VALID_CHARS "abcdefghijklmnopqrstuvwxyz" \
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
30 "0123456789-/_"
32 typedef struct XsNode {
33 uint32_t ref;
34 GByteArray *content;
35 GList *perms;
36 GHashTable *children;
37 uint64_t gencnt;
38 bool deleted_in_tx;
39 bool modified_in_tx;
40 unsigned int serialized_tx;
41 #ifdef XS_NODE_UNIT_TEST
42 gchar *name; /* debug only */
43 #endif
44 } XsNode;
46 typedef struct XsWatch {
47 struct XsWatch *next;
48 xs_impl_watch_fn *cb;
49 void *cb_opaque;
50 char *token;
51 unsigned int dom_id;
52 int rel_prefix;
53 } XsWatch;
55 typedef struct XsTransaction {
56 XsNode *root;
57 unsigned int nr_nodes;
58 unsigned int base_tx;
59 unsigned int tx_id;
60 unsigned int dom_id;
61 } XsTransaction;
63 struct XenstoreImplState {
64 XsNode *root;
65 unsigned int nr_nodes;
66 GHashTable *watches;
67 unsigned int nr_domu_watches;
68 GHashTable *transactions;
69 unsigned int nr_domu_transactions;
70 unsigned int root_tx;
71 unsigned int last_tx;
72 bool serialized;
76 static void nobble_tx(gpointer key, gpointer value, gpointer user_data)
78 unsigned int *new_tx_id = user_data;
79 XsTransaction *tx = value;
81 if (tx->base_tx == *new_tx_id) {
82 /* Transactions based on XBT_NULL will always fail */
83 tx->base_tx = XBT_NULL;
87 static inline unsigned int next_tx(struct XenstoreImplState *s)
89 unsigned int tx_id;
91 /* Find the next TX id which isn't either XBT_NULL or in use. */
92 do {
93 tx_id = ++s->last_tx;
94 } while (tx_id == XBT_NULL || tx_id == s->root_tx ||
95 g_hash_table_lookup(s->transactions, GINT_TO_POINTER(tx_id)));
98 * It is vanishingly unlikely, but ensure that no outstanding transaction
99 * is based on the (previous incarnation of the) newly-allocated TX id.
101 g_hash_table_foreach(s->transactions, nobble_tx, &tx_id);
103 return tx_id;
106 static inline XsNode *xs_node_new(void)
108 XsNode *n = g_new0(XsNode, 1);
109 n->ref = 1;
111 #ifdef XS_NODE_UNIT_TEST
112 nr_xs_nodes++;
113 xs_node_list = g_list_prepend(xs_node_list, n);
114 #endif
115 return n;
118 static inline XsNode *xs_node_ref(XsNode *n)
120 /* With just 10 transactions, it can never get anywhere near this. */
121 g_assert(n->ref < INT_MAX);
123 g_assert(n->ref);
124 n->ref++;
125 return n;
128 static inline void xs_node_unref(XsNode *n)
130 if (!n) {
131 return;
133 g_assert(n->ref);
134 if (--n->ref) {
135 return;
138 if (n->content) {
139 g_byte_array_unref(n->content);
141 if (n->perms) {
142 g_list_free_full(n->perms, g_free);
144 if (n->children) {
145 g_hash_table_unref(n->children);
147 #ifdef XS_NODE_UNIT_TEST
148 g_free(n->name);
149 nr_xs_nodes--;
150 xs_node_list = g_list_remove(xs_node_list, n);
151 #endif
152 g_free(n);
155 char *xs_perm_as_string(unsigned int perm, unsigned int domid)
157 char letter;
159 switch (perm) {
160 case XS_PERM_READ | XS_PERM_WRITE:
161 letter = 'b';
162 break;
163 case XS_PERM_READ:
164 letter = 'r';
165 break;
166 case XS_PERM_WRITE:
167 letter = 'w';
168 break;
169 case XS_PERM_NONE:
170 default:
171 letter = 'n';
172 break;
175 return g_strdup_printf("%c%u", letter, domid);
178 static gpointer do_perm_copy(gconstpointer src, gpointer user_data)
180 return g_strdup(src);
183 static XsNode *xs_node_create(const char *name, GList *perms)
185 XsNode *n = xs_node_new();
187 #ifdef XS_NODE_UNIT_TEST
188 if (name) {
189 n->name = g_strdup(name);
191 #endif
193 n->perms = g_list_copy_deep(perms, do_perm_copy, NULL);
195 return n;
198 /* For copying from one hash table to another using g_hash_table_foreach() */
199 static void do_child_insert(gpointer key, gpointer value, gpointer user_data)
201 g_hash_table_insert(user_data, g_strdup(key), xs_node_ref(value));
204 static XsNode *xs_node_copy(XsNode *old)
206 XsNode *n = xs_node_new();
208 n->gencnt = old->gencnt;
210 #ifdef XS_NODE_UNIT_TEST
211 if (n->name) {
212 n->name = g_strdup(old->name);
214 #endif
216 assert(old);
217 if (old->children) {
218 n->children = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
219 (GDestroyNotify)xs_node_unref);
220 g_hash_table_foreach(old->children, do_child_insert, n->children);
222 if (old->perms) {
223 n->perms = g_list_copy_deep(old->perms, do_perm_copy, NULL);
225 if (old->content) {
226 n->content = g_byte_array_ref(old->content);
228 return n;
231 /* Returns true if it made a change to the hash table */
232 static bool xs_node_add_child(XsNode *n, const char *path_elem, XsNode *child)
234 assert(!strchr(path_elem, '/'));
236 if (!child) {
237 assert(n->children);
238 return g_hash_table_remove(n->children, path_elem);
241 #ifdef XS_NODE_UNIT_TEST
242 g_free(child->name);
243 child->name = g_strdup(path_elem);
244 #endif
245 if (!n->children) {
246 n->children = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
247 (GDestroyNotify)xs_node_unref);
251 * The documentation for g_hash_table_insert() says that it "returns a
252 * boolean value to indicate whether the newly added value was already
253 * in the hash table or not."
255 * It could perhaps be clearer that returning TRUE means it wasn't,
257 return g_hash_table_insert(n->children, g_strdup(path_elem), child);
260 struct walk_op {
261 struct XenstoreImplState *s;
262 char path[XENSTORE_ABS_PATH_MAX + 2]; /* Two NUL terminators */
263 int (*op_fn)(XsNode **n, struct walk_op *op);
264 void *op_opaque;
265 void *op_opaque2;
267 GList *watches;
268 unsigned int dom_id;
269 unsigned int tx_id;
271 /* The number of nodes which will exist in the tree if this op succeeds. */
272 unsigned int new_nr_nodes;
275 * This is maintained on the way *down* the walk to indicate
276 * whether nodes can be modified in place or whether COW is
277 * required. It starts off being true, as we're always going to
278 * replace the root node. If we walk into a shared subtree it
279 * becomes false. If we start *creating* new nodes for a write,
280 * it becomes true again.
282 * Do not use it on the way back up.
284 bool inplace;
285 bool mutating;
286 bool create_dirs;
287 bool in_transaction;
289 /* Tracking during recursion so we know which is first. */
290 bool deleted_in_tx;
293 static void fire_watches(struct walk_op *op, bool parents)
295 GList *l = NULL;
296 XsWatch *w;
298 if (!op->mutating || op->in_transaction) {
299 return;
302 if (parents) {
303 l = op->watches;
306 w = g_hash_table_lookup(op->s->watches, op->path);
307 while (w || l) {
308 if (!w) {
309 /* Fire the parent nodes from 'op' if asked to */
310 w = l->data;
311 l = l->next;
312 continue;
315 assert(strlen(op->path) > w->rel_prefix);
316 w->cb(w->cb_opaque, op->path + w->rel_prefix, w->token);
318 w = w->next;
322 static int xs_node_add_content(XsNode **n, struct walk_op *op)
324 GByteArray *data = op->op_opaque;
326 if (op->dom_id) {
328 * The real XenStored includes permissions and names of child nodes
329 * in the calculated datasize but life's too short. For a single
330 * tenant internal XenStore, we don't have to be quite as pedantic.
332 if (data->len > XS_MAX_NODE_SIZE) {
333 return E2BIG;
336 /* We *are* the node to be written. Either this or a copy. */
337 if (!op->inplace) {
338 XsNode *old = *n;
339 *n = xs_node_copy(old);
340 xs_node_unref(old);
343 if ((*n)->content) {
344 g_byte_array_unref((*n)->content);
346 (*n)->content = g_byte_array_ref(data);
347 if (op->tx_id != XBT_NULL) {
348 (*n)->modified_in_tx = true;
350 return 0;
353 static int xs_node_get_content(XsNode **n, struct walk_op *op)
355 GByteArray *data = op->op_opaque;
356 GByteArray *node_data;
358 assert(op->inplace);
359 assert(*n);
361 node_data = (*n)->content;
362 if (node_data) {
363 g_byte_array_append(data, node_data->data, node_data->len);
366 return 0;
369 static int node_rm_recurse(gpointer key, gpointer value, gpointer user_data)
371 struct walk_op *op = user_data;
372 int path_len = strlen(op->path);
373 int key_len = strlen(key);
374 XsNode *n = value;
375 bool this_inplace = op->inplace;
377 if (n->ref != 1) {
378 op->inplace = 0;
381 assert(key_len + path_len + 2 <= sizeof(op->path));
382 op->path[path_len] = '/';
383 memcpy(op->path + path_len + 1, key, key_len + 1);
385 if (n->children) {
386 g_hash_table_foreach_remove(n->children, node_rm_recurse, op);
388 op->new_nr_nodes--;
391 * Fire watches on *this* node but not the parents because they are
392 * going to be deleted too, so the watch will fire for them anyway.
394 fire_watches(op, false);
395 op->path[path_len] = '\0';
398 * Actually deleting the child here is just an optimisation; if we
399 * don't then the final unref on the topmost victim will just have
400 * to cascade down again repeating all the g_hash_table_foreach()
401 * calls.
403 return this_inplace;
406 static XsNode *xs_node_copy_deleted(XsNode *old, struct walk_op *op);
407 static void copy_deleted_recurse(gpointer key, gpointer value,
408 gpointer user_data)
410 struct walk_op *op = user_data;
411 GHashTable *siblings = op->op_opaque2;
412 XsNode *n = xs_node_copy_deleted(value, op);
415 * Reinsert the deleted_in_tx copy of the node into the parent's
416 * 'children' hash table. Having stashed it from op->op_opaque2
417 * before the recursive call to xs_node_copy_deleted() scribbled
418 * over it.
420 g_hash_table_insert(siblings, g_strdup(key), n);
423 static XsNode *xs_node_copy_deleted(XsNode *old, struct walk_op *op)
425 XsNode *n = xs_node_new();
427 n->gencnt = old->gencnt;
429 #ifdef XS_NODE_UNIT_TEST
430 if (old->name) {
431 n->name = g_strdup(old->name);
433 #endif
435 if (old->children) {
436 n->children = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
437 (GDestroyNotify)xs_node_unref);
438 op->op_opaque2 = n->children;
439 g_hash_table_foreach(old->children, copy_deleted_recurse, op);
441 if (old->perms) {
442 n->perms = g_list_copy_deep(old->perms, do_perm_copy, NULL);
444 n->deleted_in_tx = true;
445 /* If it gets resurrected we only fire a watch if it lost its content */
446 if (old->content) {
447 n->modified_in_tx = true;
449 op->new_nr_nodes--;
450 return n;
453 static int xs_node_rm(XsNode **n, struct walk_op *op)
455 bool this_inplace = op->inplace;
457 if (op->tx_id != XBT_NULL) {
458 /* It's not trivial to do inplace handling for this one */
459 XsNode *old = *n;
460 *n = xs_node_copy_deleted(old, op);
461 xs_node_unref(old);
462 return 0;
465 /* Fire watches for, and count, nodes in the subtree which get deleted */
466 if ((*n)->children) {
467 g_hash_table_foreach_remove((*n)->children, node_rm_recurse, op);
469 op->new_nr_nodes--;
471 if (this_inplace) {
472 xs_node_unref(*n);
474 *n = NULL;
475 return 0;
478 static int xs_node_get_perms(XsNode **n, struct walk_op *op)
480 GList **perms = op->op_opaque;
482 assert(op->inplace);
483 assert(*n);
485 *perms = g_list_copy_deep((*n)->perms, do_perm_copy, NULL);
486 return 0;
489 static void parse_perm(const char *perm, char *letter, unsigned int *dom_id)
491 unsigned int n = sscanf(perm, "%c%u", letter, dom_id);
493 assert(n == 2);
496 static bool can_access(unsigned int dom_id, GList *perms, const char *letters)
498 unsigned int i, n;
499 char perm_letter;
500 unsigned int perm_dom_id;
501 bool access;
503 if (dom_id == 0) {
504 return true;
507 n = g_list_length(perms);
508 assert(n >= 1);
511 * The dom_id of the first perm is the owner, and the owner always has
512 * read-write access.
514 parse_perm(g_list_nth_data(perms, 0), &perm_letter, &perm_dom_id);
515 if (dom_id == perm_dom_id) {
516 return true;
520 * The letter of the first perm specified the default access for all other
521 * domains.
523 access = !!strchr(letters, perm_letter);
524 for (i = 1; i < n; i++) {
525 parse_perm(g_list_nth_data(perms, i), &perm_letter, &perm_dom_id);
526 if (dom_id != perm_dom_id) {
527 continue;
529 access = !!strchr(letters, perm_letter);
532 return access;
535 static int xs_node_set_perms(XsNode **n, struct walk_op *op)
537 GList *perms = op->op_opaque;
539 if (op->dom_id) {
540 unsigned int perm_dom_id;
541 char perm_letter;
543 /* A guest may not change permissions on nodes it does not own */
544 if (!can_access(op->dom_id, (*n)->perms, "")) {
545 return EPERM;
548 /* A guest may not change the owner of a node it owns. */
549 parse_perm(perms->data, &perm_letter, &perm_dom_id);
550 if (perm_dom_id != op->dom_id) {
551 return EPERM;
554 if (g_list_length(perms) > XS_MAX_PERMS_PER_NODE) {
555 return ENOSPC;
559 /* We *are* the node to be written. Either this or a copy. */
560 if (!op->inplace) {
561 XsNode *old = *n;
562 *n = xs_node_copy(old);
563 xs_node_unref(old);
566 if ((*n)->perms) {
567 g_list_free_full((*n)->perms, g_free);
569 (*n)->perms = g_list_copy_deep(perms, do_perm_copy, NULL);
570 if (op->tx_id != XBT_NULL) {
571 (*n)->modified_in_tx = true;
573 return 0;
577 * Passed a full reference in *n which it may free if it needs to COW.
579 * When changing the tree, the op->inplace flag indicates whether this
580 * node may be modified in place (i.e. it and all its parents had a
581 * refcount of one). If walking down the tree we find a node whose
582 * refcount is higher, we must clear op->inplace and COW from there
583 * down. Unless we are creating new nodes as scaffolding for a write
584 * (which works like 'mkdir -p' does). In which case those newly
585 * created nodes can (and must) be modified in place again.
587 static int xs_node_walk(XsNode **n, struct walk_op *op)
589 char *child_name = NULL;
590 size_t namelen;
591 XsNode *old = *n, *child = NULL;
592 bool stole_child = false;
593 bool this_inplace;
594 XsWatch *watch;
595 int err;
597 namelen = strlen(op->path);
598 watch = g_hash_table_lookup(op->s->watches, op->path);
600 /* Is there a child, or do we hit the double-NUL termination? */
601 if (op->path[namelen + 1]) {
602 char *slash;
603 child_name = op->path + namelen + 1;
604 slash = strchr(child_name, '/');
605 if (slash) {
606 *slash = '\0';
608 op->path[namelen] = '/';
611 /* If we walk into a subtree which is shared, we must COW */
612 if (op->mutating && old->ref != 1) {
613 op->inplace = false;
616 if (!child_name) {
617 const char *letters = op->mutating ? "wb" : "rb";
619 if (!can_access(op->dom_id, old->perms, letters)) {
620 err = EACCES;
621 goto out;
624 /* This is the actual node on which the operation shall be performed */
625 err = op->op_fn(n, op);
626 if (!err) {
627 fire_watches(op, true);
629 goto out;
632 /* op->inplace will be further modified during the recursion */
633 this_inplace = op->inplace;
635 if (old && old->children) {
636 child = g_hash_table_lookup(old->children, child_name);
637 /* This is a *weak* reference to 'child', owned by the hash table */
640 if (child) {
641 if (child->deleted_in_tx) {
642 assert(child->ref == 1);
643 /* Cannot actually set child->deleted_in_tx = false until later */
645 xs_node_ref(child);
647 * Now we own it too. But if we can modify inplace, that's going to
648 * foil the check and force it to COW. We want to be the *only* owner
649 * so that it can be modified in place, so remove it from the hash
650 * table in that case. We'll add it (or its replacement) back later.
652 if (op->mutating && this_inplace) {
653 g_hash_table_remove(old->children, child_name);
654 stole_child = true;
656 } else if (op->create_dirs) {
657 assert(op->mutating);
659 if (!can_access(op->dom_id, old->perms, "wb")) {
660 err = EACCES;
661 goto out;
664 if (op->dom_id && op->new_nr_nodes >= XS_MAX_DOMAIN_NODES) {
665 err = ENOSPC;
666 goto out;
669 child = xs_node_create(child_name, old->perms);
670 op->new_nr_nodes++;
673 * If we're creating a new child, we can clearly modify it (and its
674 * children) in place from here on down.
676 op->inplace = true;
677 } else {
678 err = ENOENT;
679 goto out;
683 * If there's a watch on this node, add it to the list to be fired
684 * (with the correct full pathname for the modified node) at the end.
686 if (watch) {
687 op->watches = g_list_append(op->watches, watch);
691 * Except for the temporary child-stealing as noted, our node has not
692 * changed yet. We don't yet know the overall operation will complete.
694 err = xs_node_walk(&child, op);
696 if (watch) {
697 op->watches = g_list_remove(op->watches, watch);
700 if (err || !op->mutating) {
701 if (stole_child) {
702 /* Put it back as it was. */
703 g_hash_table_replace(old->children, g_strdup(child_name), child);
704 } else {
705 xs_node_unref(child);
707 goto out;
711 * Now we know the operation has completed successfully and we're on
712 * the way back up. Make the change, substituting 'child' in the
713 * node at our level.
715 if (!this_inplace) {
716 *n = xs_node_copy(old);
717 xs_node_unref(old);
721 * If we resurrected a deleted_in_tx node, we can mark it as no longer
722 * deleted now that we know the overall operation has succeeded.
724 if (op->create_dirs && child && child->deleted_in_tx) {
725 op->new_nr_nodes++;
726 child->deleted_in_tx = false;
730 * The child may be NULL here, for a remove operation. Either way,
731 * xs_node_add_child() will do the right thing and return a value
732 * indicating whether it changed the parent's hash table or not.
734 * We bump the parent gencnt if it adds a child that we *didn't*
735 * steal from it in the first place, or if child==NULL and was
736 * thus removed (whether we stole it earlier and didn't put it
737 * back, or xs_node_add_child() actually removed it now).
739 if ((xs_node_add_child(*n, child_name, child) && !stole_child) || !child) {
740 (*n)->gencnt++;
743 out:
744 op->path[namelen] = '\0';
745 if (!namelen) {
746 assert(!op->watches);
748 * On completing the recursion back up the path walk and reaching the
749 * top, assign the new node count if the operation was successful. If
750 * the main tree was changed, bump its tx ID so that outstanding
751 * transactions correctly fail. But don't bump it every time; only
752 * if it makes a difference.
754 if (!err && op->mutating) {
755 if (!op->in_transaction) {
756 if (op->s->root_tx != op->s->last_tx) {
757 op->s->root_tx = next_tx(op->s);
759 op->s->nr_nodes = op->new_nr_nodes;
760 } else {
761 XsTransaction *tx = g_hash_table_lookup(op->s->transactions,
762 GINT_TO_POINTER(op->tx_id));
763 assert(tx);
764 tx->nr_nodes = op->new_nr_nodes;
768 return err;
771 static void append_directory_item(gpointer key, gpointer value,
772 gpointer user_data)
774 GList **items = user_data;
776 *items = g_list_insert_sorted(*items, g_strdup(key), (GCompareFunc)strcmp);
779 /* Populates items with char * names which caller must free. */
780 static int xs_node_directory(XsNode **n, struct walk_op *op)
782 GList **items = op->op_opaque;
784 assert(op->inplace);
785 assert(*n);
787 if ((*n)->children) {
788 g_hash_table_foreach((*n)->children, append_directory_item, items);
791 if (op->op_opaque2) {
792 *(uint64_t *)op->op_opaque2 = (*n)->gencnt;
795 return 0;
798 static int validate_path(char *outpath, const char *userpath,
799 unsigned int dom_id)
801 size_t i, pathlen = strlen(userpath);
803 if (!pathlen || userpath[pathlen] == '/' || strstr(userpath, "//")) {
804 return EINVAL;
806 for (i = 0; i < pathlen; i++) {
807 if (!strchr(XS_VALID_CHARS, userpath[i])) {
808 return EINVAL;
811 if (userpath[0] == '/') {
812 if (pathlen > XENSTORE_ABS_PATH_MAX) {
813 return E2BIG;
815 memcpy(outpath, userpath, pathlen + 1);
816 } else {
817 if (pathlen > XENSTORE_REL_PATH_MAX) {
818 return E2BIG;
820 snprintf(outpath, XENSTORE_ABS_PATH_MAX, "/local/domain/%u/%s", dom_id,
821 userpath);
823 return 0;
827 static int init_walk_op(XenstoreImplState *s, struct walk_op *op,
828 xs_transaction_t tx_id, unsigned int dom_id,
829 const char *path, XsNode ***rootp)
831 int ret = validate_path(op->path, path, dom_id);
832 if (ret) {
833 return ret;
837 * We use *two* NUL terminators at the end of the path, as during the walk
838 * we will temporarily turn each '/' into a NUL to allow us to use that
839 * path element for the lookup.
841 op->path[strlen(op->path) + 1] = '\0';
842 op->watches = NULL;
843 op->path[0] = '\0';
844 op->inplace = true;
845 op->mutating = false;
846 op->create_dirs = false;
847 op->in_transaction = false;
848 op->dom_id = dom_id;
849 op->tx_id = tx_id;
850 op->s = s;
852 if (tx_id == XBT_NULL) {
853 *rootp = &s->root;
854 op->new_nr_nodes = s->nr_nodes;
855 } else {
856 XsTransaction *tx = g_hash_table_lookup(s->transactions,
857 GINT_TO_POINTER(tx_id));
858 if (!tx) {
859 return ENOENT;
861 *rootp = &tx->root;
862 op->new_nr_nodes = tx->nr_nodes;
863 op->in_transaction = true;
866 return 0;
869 int xs_impl_read(XenstoreImplState *s, unsigned int dom_id,
870 xs_transaction_t tx_id, const char *path, GByteArray *data)
873 * The data GByteArray shall exist, and will be freed by caller.
874 * Just g_byte_array_append() to it.
876 struct walk_op op;
877 XsNode **n;
878 int ret;
880 ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
881 if (ret) {
882 return ret;
884 op.op_fn = xs_node_get_content;
885 op.op_opaque = data;
886 return xs_node_walk(n, &op);
889 int xs_impl_write(XenstoreImplState *s, unsigned int dom_id,
890 xs_transaction_t tx_id, const char *path, GByteArray *data)
893 * The data GByteArray shall exist, will be freed by caller. You are
894 * free to use g_byte_array_steal() and keep the data. Or just ref it.
896 struct walk_op op;
897 XsNode **n;
898 int ret;
900 ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
901 if (ret) {
902 return ret;
904 op.op_fn = xs_node_add_content;
905 op.op_opaque = data;
906 op.mutating = true;
907 op.create_dirs = true;
908 return xs_node_walk(n, &op);
911 int xs_impl_directory(XenstoreImplState *s, unsigned int dom_id,
912 xs_transaction_t tx_id, const char *path,
913 uint64_t *gencnt, GList **items)
916 * The items are (char *) to be freed by caller. Although it's consumed
917 * immediately so if you want to change it to (const char *) and keep
918 * them, go ahead and change the caller.
920 struct walk_op op;
921 XsNode **n;
922 int ret;
924 ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
925 if (ret) {
926 return ret;
928 op.op_fn = xs_node_directory;
929 op.op_opaque = items;
930 op.op_opaque2 = gencnt;
931 return xs_node_walk(n, &op);
934 int xs_impl_transaction_start(XenstoreImplState *s, unsigned int dom_id,
935 xs_transaction_t *tx_id)
937 XsTransaction *tx;
939 if (*tx_id != XBT_NULL) {
940 return EINVAL;
943 if (dom_id && s->nr_domu_transactions >= XS_MAX_TRANSACTIONS) {
944 return ENOSPC;
947 tx = g_new0(XsTransaction, 1);
949 tx->nr_nodes = s->nr_nodes;
950 tx->tx_id = next_tx(s);
951 tx->base_tx = s->root_tx;
952 tx->root = xs_node_ref(s->root);
953 tx->dom_id = dom_id;
955 g_hash_table_insert(s->transactions, GINT_TO_POINTER(tx->tx_id), tx);
956 if (dom_id) {
957 s->nr_domu_transactions++;
959 *tx_id = tx->tx_id;
960 return 0;
963 static gboolean tx_commit_walk(gpointer key, gpointer value,
964 gpointer user_data)
966 struct walk_op *op = user_data;
967 int path_len = strlen(op->path);
968 int key_len = strlen(key);
969 bool fire_parents = true;
970 XsWatch *watch;
971 XsNode *n = value;
973 if (n->ref != 1) {
974 return false;
977 if (n->deleted_in_tx) {
979 * We fire watches on our parents if we are the *first* node
980 * to be deleted (the topmost one). This matches the behaviour
981 * when deleting in the live tree.
983 fire_parents = !op->deleted_in_tx;
985 /* Only used on the way down so no need to clear it later */
986 op->deleted_in_tx = true;
989 assert(key_len + path_len + 2 <= sizeof(op->path));
990 op->path[path_len] = '/';
991 memcpy(op->path + path_len + 1, key, key_len + 1);
993 watch = g_hash_table_lookup(op->s->watches, op->path);
994 if (watch) {
995 op->watches = g_list_append(op->watches, watch);
998 if (n->children) {
999 g_hash_table_foreach_remove(n->children, tx_commit_walk, op);
1002 if (watch) {
1003 op->watches = g_list_remove(op->watches, watch);
1007 * Don't fire watches if this node was only copied because a
1008 * descendent was changed. The modified_in_tx flag indicates the
1009 * ones which were really changed.
1011 if (n->modified_in_tx || n->deleted_in_tx) {
1012 fire_watches(op, fire_parents);
1013 n->modified_in_tx = false;
1015 op->path[path_len] = '\0';
1017 /* Deleted nodes really do get expunged when we commit */
1018 return n->deleted_in_tx;
1021 static int transaction_commit(XenstoreImplState *s, XsTransaction *tx)
1023 struct walk_op op;
1024 XsNode **n;
1025 int ret;
1027 if (s->root_tx != tx->base_tx) {
1028 return EAGAIN;
1030 xs_node_unref(s->root);
1031 s->root = tx->root;
1032 tx->root = NULL;
1033 s->root_tx = tx->tx_id;
1034 s->nr_nodes = tx->nr_nodes;
1036 ret = init_walk_op(s, &op, XBT_NULL, tx->dom_id, "/", &n);
1038 * There are two reasons why init_walk_op() may fail: an invalid tx_id,
1039 * or an invalid path. We pass XBT_NULL and "/", and it cannot fail.
1040 * If it does, the world is broken. And returning 'ret' would be weird
1041 * because the transaction *was* committed, and all this tree walk is
1042 * trying to do is fire the resulting watches on newly-committed nodes.
1044 g_assert(!ret);
1046 op.deleted_in_tx = false;
1047 op.mutating = true;
1050 * Walk the new root and fire watches on any node which has a
1051 * refcount of one (which is therefore unique to this transaction).
1053 if (s->root->children) {
1054 g_hash_table_foreach_remove(s->root->children, tx_commit_walk, &op);
1057 return 0;
1060 int xs_impl_transaction_end(XenstoreImplState *s, unsigned int dom_id,
1061 xs_transaction_t tx_id, bool commit)
1063 int ret = 0;
1064 XsTransaction *tx = g_hash_table_lookup(s->transactions,
1065 GINT_TO_POINTER(tx_id));
1067 if (!tx || tx->dom_id != dom_id) {
1068 return ENOENT;
1071 if (commit) {
1072 ret = transaction_commit(s, tx);
1075 g_hash_table_remove(s->transactions, GINT_TO_POINTER(tx_id));
1076 if (dom_id) {
1077 assert(s->nr_domu_transactions);
1078 s->nr_domu_transactions--;
1080 return ret;
1083 int xs_impl_rm(XenstoreImplState *s, unsigned int dom_id,
1084 xs_transaction_t tx_id, const char *path)
1086 struct walk_op op;
1087 XsNode **n;
1088 int ret;
1090 ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
1091 if (ret) {
1092 return ret;
1094 op.op_fn = xs_node_rm;
1095 op.mutating = true;
1096 return xs_node_walk(n, &op);
1099 int xs_impl_get_perms(XenstoreImplState *s, unsigned int dom_id,
1100 xs_transaction_t tx_id, const char *path, GList **perms)
1102 struct walk_op op;
1103 XsNode **n;
1104 int ret;
1106 ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
1107 if (ret) {
1108 return ret;
1110 op.op_fn = xs_node_get_perms;
1111 op.op_opaque = perms;
1112 return xs_node_walk(n, &op);
1115 static void is_valid_perm(gpointer data, gpointer user_data)
1117 char *perm = data;
1118 bool *valid = user_data;
1119 char letter;
1120 unsigned int dom_id;
1122 if (!*valid) {
1123 return;
1126 if (sscanf(perm, "%c%u", &letter, &dom_id) != 2) {
1127 *valid = false;
1128 return;
1131 switch (letter) {
1132 case 'n':
1133 case 'r':
1134 case 'w':
1135 case 'b':
1136 break;
1138 default:
1139 *valid = false;
1140 break;
1144 int xs_impl_set_perms(XenstoreImplState *s, unsigned int dom_id,
1145 xs_transaction_t tx_id, const char *path, GList *perms)
1147 struct walk_op op;
1148 XsNode **n;
1149 bool valid = true;
1150 int ret;
1152 if (!g_list_length(perms)) {
1153 return EINVAL;
1156 g_list_foreach(perms, is_valid_perm, &valid);
1157 if (!valid) {
1158 return EINVAL;
1161 ret = init_walk_op(s, &op, tx_id, dom_id, path, &n);
1162 if (ret) {
1163 return ret;
1165 op.op_fn = xs_node_set_perms;
1166 op.op_opaque = perms;
1167 op.mutating = true;
1168 return xs_node_walk(n, &op);
1171 static int do_xs_impl_watch(XenstoreImplState *s, unsigned int dom_id,
1172 const char *path, const char *token,
1173 xs_impl_watch_fn fn, void *opaque)
1176 char abspath[XENSTORE_ABS_PATH_MAX + 1];
1177 XsWatch *w, *l;
1178 int ret;
1180 ret = validate_path(abspath, path, dom_id);
1181 if (ret) {
1182 return ret;
1185 /* Check for duplicates */
1186 l = w = g_hash_table_lookup(s->watches, abspath);
1187 while (w) {
1188 if (!g_strcmp0(token, w->token) && opaque == w->cb_opaque &&
1189 fn == w->cb && dom_id == w->dom_id) {
1190 return EEXIST;
1192 w = w->next;
1195 if (dom_id && s->nr_domu_watches >= XS_MAX_WATCHES) {
1196 return E2BIG;
1199 w = g_new0(XsWatch, 1);
1200 w->token = g_strdup(token);
1201 w->cb = fn;
1202 w->cb_opaque = opaque;
1203 w->dom_id = dom_id;
1204 w->rel_prefix = strlen(abspath) - strlen(path);
1206 /* l was looked up above when checking for duplicates */
1207 if (l) {
1208 w->next = l->next;
1209 l->next = w;
1210 } else {
1211 g_hash_table_insert(s->watches, g_strdup(abspath), w);
1213 if (dom_id) {
1214 s->nr_domu_watches++;
1217 return 0;
1220 int xs_impl_watch(XenstoreImplState *s, unsigned int dom_id, const char *path,
1221 const char *token, xs_impl_watch_fn fn, void *opaque)
1223 int ret = do_xs_impl_watch(s, dom_id, path, token, fn, opaque);
1225 if (!ret) {
1226 /* A new watch should fire immediately */
1227 fn(opaque, path, token);
1230 return ret;
1233 static XsWatch *free_watch(XenstoreImplState *s, XsWatch *w)
1235 XsWatch *next = w->next;
1237 if (w->dom_id) {
1238 assert(s->nr_domu_watches);
1239 s->nr_domu_watches--;
1242 g_free(w->token);
1243 g_free(w);
1245 return next;
1248 int xs_impl_unwatch(XenstoreImplState *s, unsigned int dom_id,
1249 const char *path, const char *token,
1250 xs_impl_watch_fn fn, void *opaque)
1252 char abspath[XENSTORE_ABS_PATH_MAX + 1];
1253 XsWatch *w, **l;
1254 int ret;
1256 ret = validate_path(abspath, path, dom_id);
1257 if (ret) {
1258 return ret;
1261 w = g_hash_table_lookup(s->watches, abspath);
1262 if (!w) {
1263 return ENOENT;
1267 * The hash table contains the first element of a list of
1268 * watches. Removing the first element in the list is a
1269 * special case because we have to update the hash table to
1270 * point to the next (or remove it if there's nothing left).
1272 if (!g_strcmp0(token, w->token) && fn == w->cb && opaque == w->cb_opaque &&
1273 dom_id == w->dom_id) {
1274 if (w->next) {
1275 /* Insert the previous 'next' into the hash table */
1276 g_hash_table_insert(s->watches, g_strdup(abspath), w->next);
1277 } else {
1278 /* Nothing left; remove from hash table */
1279 g_hash_table_remove(s->watches, abspath);
1281 free_watch(s, w);
1282 return 0;
1286 * We're all done messing with the hash table because the element
1287 * it points to has survived the cull. Now it's just a simple
1288 * linked list removal operation.
1290 for (l = &w->next; *l; l = &w->next) {
1291 w = *l;
1293 if (!g_strcmp0(token, w->token) && fn == w->cb &&
1294 opaque != w->cb_opaque && dom_id == w->dom_id) {
1295 *l = free_watch(s, w);
1296 return 0;
1300 return ENOENT;
1303 int xs_impl_reset_watches(XenstoreImplState *s, unsigned int dom_id)
1305 char **watch_paths;
1306 guint nr_watch_paths;
1307 guint i;
1309 watch_paths = (char **)g_hash_table_get_keys_as_array(s->watches,
1310 &nr_watch_paths);
1312 for (i = 0; i < nr_watch_paths; i++) {
1313 XsWatch *w1 = g_hash_table_lookup(s->watches, watch_paths[i]);
1314 XsWatch *w2, *w, **l;
1317 * w1 is the original list. The hash table has this pointer.
1318 * w2 is the head of our newly-filtered list.
1319 * w and l are temporary for processing. w is somewhat redundant
1320 * with *l but makes my eyes bleed less.
1323 w = w2 = w1;
1324 l = &w;
1325 while (w) {
1326 if (w->dom_id == dom_id) {
1327 /* If we're freeing the head of the list, bump w2 */
1328 if (w2 == w) {
1329 w2 = w->next;
1331 *l = free_watch(s, w);
1332 } else {
1333 l = &w->next;
1335 w = *l;
1338 * If the head of the list survived the cull, we don't need to
1339 * touch the hash table and we're done with this path. Else...
1341 if (w1 != w2) {
1342 g_hash_table_steal(s->watches, watch_paths[i]);
1345 * It was already freed. (Don't worry, this whole thing is
1346 * single-threaded and nobody saw it in the meantime). And
1347 * having *stolen* it, we now own the watch_paths[i] string
1348 * so if we don't give it back to the hash table, we need
1349 * to free it.
1351 if (w2) {
1352 g_hash_table_insert(s->watches, watch_paths[i], w2);
1353 } else {
1354 g_free(watch_paths[i]);
1358 g_free(watch_paths);
1359 return 0;
1362 static void xs_tx_free(void *_tx)
1364 XsTransaction *tx = _tx;
1365 if (tx->root) {
1366 xs_node_unref(tx->root);
1368 g_free(tx);
1371 XenstoreImplState *xs_impl_create(unsigned int dom_id)
1373 XenstoreImplState *s = g_new0(XenstoreImplState, 1);
1374 GList *perms;
1376 s->watches = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1377 s->transactions = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1378 NULL, xs_tx_free);
1380 perms = g_list_append(NULL, xs_perm_as_string(XS_PERM_NONE, 0));
1381 s->root = xs_node_create("/", perms);
1382 g_list_free_full(perms, g_free);
1383 s->nr_nodes = 1;
1385 s->root_tx = s->last_tx = 1;
1386 return s;
1390 static void clear_serialized_tx(gpointer key, gpointer value, gpointer opaque)
1392 XsNode *n = value;
1394 n->serialized_tx = XBT_NULL;
1395 if (n->children) {
1396 g_hash_table_foreach(n->children, clear_serialized_tx, NULL);
1400 static void clear_tx_serialized_tx(gpointer key, gpointer value,
1401 gpointer opaque)
1403 XsTransaction *t = value;
1405 clear_serialized_tx(NULL, t->root, NULL);
1408 static void write_be32(GByteArray *save, uint32_t val)
1410 uint32_t be = htonl(val);
1411 g_byte_array_append(save, (void *)&be, sizeof(be));
1415 struct save_state {
1416 GByteArray *bytes;
1417 unsigned int tx_id;
1420 #define MODIFIED_IN_TX (1U << 0)
1421 #define DELETED_IN_TX (1U << 1)
1422 #define NODE_REF (1U << 2)
1424 static void save_node(gpointer key, gpointer value, gpointer opaque)
1426 struct save_state *ss = opaque;
1427 XsNode *n = value;
1428 char *name = key;
1429 uint8_t flag = 0;
1431 /* Child nodes (i.e. anything but the root) have a name */
1432 if (name) {
1433 g_byte_array_append(ss->bytes, key, strlen(key) + 1);
1437 * If we already wrote this node, refer to the previous copy.
1438 * There's no rename/move in XenStore, so all we need to find
1439 * it is the tx_id of the transaction in which it exists. Which
1440 * may be the root tx.
1442 if (n->serialized_tx != XBT_NULL) {
1443 flag = NODE_REF;
1444 g_byte_array_append(ss->bytes, &flag, 1);
1445 write_be32(ss->bytes, n->serialized_tx);
1446 } else {
1447 GList *l;
1448 n->serialized_tx = ss->tx_id;
1450 if (n->modified_in_tx) {
1451 flag |= MODIFIED_IN_TX;
1453 if (n->deleted_in_tx) {
1454 flag |= DELETED_IN_TX;
1456 g_byte_array_append(ss->bytes, &flag, 1);
1458 if (n->content) {
1459 write_be32(ss->bytes, n->content->len);
1460 g_byte_array_append(ss->bytes, n->content->data, n->content->len);
1461 } else {
1462 write_be32(ss->bytes, 0);
1465 for (l = n->perms; l; l = l->next) {
1466 g_byte_array_append(ss->bytes, l->data, strlen(l->data) + 1);
1468 /* NUL termination after perms */
1469 g_byte_array_append(ss->bytes, (void *)"", 1);
1471 if (n->children) {
1472 g_hash_table_foreach(n->children, save_node, ss);
1474 /* NUL termination after children (child name is NUL) */
1475 g_byte_array_append(ss->bytes, (void *)"", 1);
1479 static void save_tree(struct save_state *ss, uint32_t tx_id, XsNode *root)
1481 write_be32(ss->bytes, tx_id);
1482 ss->tx_id = tx_id;
1483 save_node(NULL, root, ss);
1486 static void save_tx(gpointer key, gpointer value, gpointer opaque)
1488 uint32_t tx_id = GPOINTER_TO_INT(key);
1489 struct save_state *ss = opaque;
1490 XsTransaction *n = value;
1492 write_be32(ss->bytes, n->base_tx);
1493 write_be32(ss->bytes, n->dom_id);
1495 save_tree(ss, tx_id, n->root);
1498 static void save_watch(gpointer key, gpointer value, gpointer opaque)
1500 struct save_state *ss = opaque;
1501 XsWatch *w = value;
1503 /* We only save the *guest* watches. */
1504 if (w->dom_id) {
1505 gpointer relpath = key + w->rel_prefix;
1506 g_byte_array_append(ss->bytes, relpath, strlen(relpath) + 1);
1507 g_byte_array_append(ss->bytes, (void *)w->token, strlen(w->token) + 1);
1511 GByteArray *xs_impl_serialize(XenstoreImplState *s)
1513 struct save_state ss;
1515 ss.bytes = g_byte_array_new();
1518 * node = flags [ real_node / node_ref ]
1519 * flags = uint8_t (MODIFIED_IN_TX | DELETED_IN_TX | NODE_REF)
1520 * node_ref = tx_id (in which the original version of this node exists)
1521 * real_node = content perms child* NUL
1522 * content = len data
1523 * len = uint32_t
1524 * data = uint8_t{len}
1525 * perms = perm* NUL
1526 * perm = asciiz
1527 * child = name node
1528 * name = asciiz
1530 * tree = tx_id node
1531 * tx_id = uint32_t
1533 * transaction = base_tx_id dom_id tree
1534 * base_tx_id = uint32_t
1535 * dom_id = uint32_t
1537 * tx_list = tree transaction* XBT_NULL
1539 * watch = path token
1540 * path = asciiz
1541 * token = asciiz
1543 * watch_list = watch* NUL
1545 * xs_serialize_stream = last_tx tx_list watch_list
1546 * last_tx = uint32_t
1549 /* Clear serialized_tx in every node. */
1550 if (s->serialized) {
1551 clear_serialized_tx(NULL, s->root, NULL);
1552 g_hash_table_foreach(s->transactions, clear_tx_serialized_tx, NULL);
1555 s->serialized = true;
1557 write_be32(ss.bytes, s->last_tx);
1558 save_tree(&ss, s->root_tx, s->root);
1559 g_hash_table_foreach(s->transactions, save_tx, &ss);
1561 write_be32(ss.bytes, XBT_NULL);
1563 g_hash_table_foreach(s->watches, save_watch, &ss);
1564 g_byte_array_append(ss.bytes, (void *)"", 1);
1566 return ss.bytes;
1569 struct unsave_state {
1570 char path[XENSTORE_ABS_PATH_MAX + 1];
1571 XenstoreImplState *s;
1572 GByteArray *bytes;
1573 uint8_t *d;
1574 size_t l;
1575 bool root_walk;
1578 static int consume_be32(struct unsave_state *us, unsigned int *val)
1580 uint32_t d;
1582 if (us->l < sizeof(d)) {
1583 return -EINVAL;
1585 memcpy(&d, us->d, sizeof(d));
1586 *val = ntohl(d);
1587 us->d += sizeof(d);
1588 us->l -= sizeof(d);
1589 return 0;
1592 static int consume_string(struct unsave_state *us, char **str, size_t *len)
1594 size_t l;
1596 if (!us->l) {
1597 return -EINVAL;
1600 l = strnlen((void *)us->d, us->l);
1601 if (l == us->l) {
1602 return -EINVAL;
1605 if (str) {
1606 *str = (void *)us->d;
1608 if (len) {
1609 *len = l;
1612 us->d += l + 1;
1613 us->l -= l + 1;
1614 return 0;
1617 static XsNode *lookup_node(XsNode *n, char *path)
1619 char *slash = strchr(path, '/');
1620 XsNode *child;
1622 if (path[0] == '\0') {
1623 return n;
1626 if (slash) {
1627 *slash = '\0';
1630 if (!n->children) {
1631 return NULL;
1633 child = g_hash_table_lookup(n->children, path);
1634 if (!slash) {
1635 return child;
1638 *slash = '/';
1639 if (!child) {
1640 return NULL;
1642 return lookup_node(child, slash + 1);
1645 static XsNode *lookup_tx_node(struct unsave_state *us, unsigned int tx_id)
1647 XsTransaction *t;
1648 if (tx_id == us->s->root_tx) {
1649 return lookup_node(us->s->root, us->path + 1);
1652 t = g_hash_table_lookup(us->s->transactions, GINT_TO_POINTER(tx_id));
1653 if (!t) {
1654 return NULL;
1656 g_assert(t->root);
1657 return lookup_node(t->root, us->path + 1);
1660 static void count_child_nodes(gpointer key, gpointer value, gpointer user_data)
1662 unsigned int *nr_nodes = user_data;
1663 XsNode *n = value;
1665 (*nr_nodes)++;
1667 if (n->children) {
1668 g_hash_table_foreach(n->children, count_child_nodes, nr_nodes);
1672 static int consume_node(struct unsave_state *us, XsNode **nodep,
1673 unsigned int *nr_nodes)
1675 XsNode *n = NULL;
1676 uint8_t flags;
1677 int ret;
1679 if (us->l < 1) {
1680 return -EINVAL;
1682 flags = us->d[0];
1683 us->d++;
1684 us->l--;
1686 if (flags == NODE_REF) {
1687 unsigned int tx;
1689 ret = consume_be32(us, &tx);
1690 if (ret) {
1691 return ret;
1694 n = lookup_tx_node(us, tx);
1695 if (!n) {
1696 return -EINVAL;
1698 n->ref++;
1699 if (n->children) {
1700 g_hash_table_foreach(n->children, count_child_nodes, nr_nodes);
1702 } else {
1703 uint32_t datalen;
1705 if (flags & ~(DELETED_IN_TX | MODIFIED_IN_TX)) {
1706 return -EINVAL;
1708 n = xs_node_new();
1710 if (flags & DELETED_IN_TX) {
1711 n->deleted_in_tx = true;
1713 if (flags & MODIFIED_IN_TX) {
1714 n->modified_in_tx = true;
1716 ret = consume_be32(us, &datalen);
1717 if (ret) {
1718 xs_node_unref(n);
1719 return -EINVAL;
1721 if (datalen) {
1722 if (datalen > us->l) {
1723 xs_node_unref(n);
1724 return -EINVAL;
1727 GByteArray *node_data = g_byte_array_new();
1728 g_byte_array_append(node_data, us->d, datalen);
1729 us->d += datalen;
1730 us->l -= datalen;
1731 n->content = node_data;
1733 if (us->root_walk) {
1734 n->modified_in_tx = true;
1737 while (1) {
1738 char *perm = NULL;
1739 size_t permlen = 0;
1741 ret = consume_string(us, &perm, &permlen);
1742 if (ret) {
1743 xs_node_unref(n);
1744 return ret;
1747 if (!permlen) {
1748 break;
1751 n->perms = g_list_append(n->perms, g_strdup(perm));
1754 /* Now children */
1755 while (1) {
1756 size_t childlen;
1757 char *childname;
1758 char *pathend;
1759 XsNode *child = NULL;
1761 ret = consume_string(us, &childname, &childlen);
1762 if (ret) {
1763 xs_node_unref(n);
1764 return ret;
1767 if (!childlen) {
1768 break;
1771 pathend = us->path + strlen(us->path);
1772 strncat(us->path, "/", sizeof(us->path) - 1);
1773 strncat(us->path, childname, sizeof(us->path) - 1);
1775 ret = consume_node(us, &child, nr_nodes);
1776 *pathend = '\0';
1777 if (ret) {
1778 xs_node_unref(n);
1779 return ret;
1781 g_assert(child);
1782 xs_node_add_child(n, childname, child);
1786 * If the node has no data and no children we still want to fire
1787 * a watch on it.
1789 if (us->root_walk && !n->children) {
1790 n->modified_in_tx = true;
1794 if (!n->deleted_in_tx) {
1795 (*nr_nodes)++;
1798 *nodep = n;
1799 return 0;
1802 static int consume_tree(struct unsave_state *us, XsTransaction *t)
1804 int ret;
1806 ret = consume_be32(us, &t->tx_id);
1807 if (ret) {
1808 return ret;
1811 if (t->tx_id > us->s->last_tx) {
1812 return -EINVAL;
1815 us->path[0] = '\0';
1817 return consume_node(us, &t->root, &t->nr_nodes);
1820 int xs_impl_deserialize(XenstoreImplState *s, GByteArray *bytes,
1821 unsigned int dom_id, xs_impl_watch_fn watch_fn,
1822 void *watch_opaque)
1824 struct unsave_state us;
1825 XsTransaction base_t = { 0 };
1826 int ret;
1828 us.s = s;
1829 us.bytes = bytes;
1830 us.d = bytes->data;
1831 us.l = bytes->len;
1833 xs_impl_reset_watches(s, dom_id);
1834 g_hash_table_remove_all(s->transactions);
1836 xs_node_unref(s->root);
1837 s->root = NULL;
1838 s->root_tx = s->last_tx = XBT_NULL;
1840 ret = consume_be32(&us, &s->last_tx);
1841 if (ret) {
1842 return ret;
1846 * Consume the base tree into a transaction so that watches can be
1847 * fired as we commit it. By setting us.root_walk we cause the nodes
1848 * to be marked as 'modified_in_tx' as they are created, so that the
1849 * watches are triggered on them.
1851 base_t.dom_id = dom_id;
1852 base_t.base_tx = XBT_NULL;
1853 us.root_walk = true;
1854 ret = consume_tree(&us, &base_t);
1855 if (ret) {
1856 return ret;
1858 us.root_walk = false;
1861 * Commit the transaction now while the refcount on all nodes is 1.
1862 * Note that we haven't yet reinstated the *guest* watches but that's
1863 * OK because we don't want the guest to see any changes. Even any
1864 * backend nodes which get recreated should be *precisely* as they
1865 * were before the migration. Back ends may have been instantiated
1866 * already, and will see the frontend magically blink into existence
1867 * now (well, from the aio_bh which fires the watches). It's their
1868 * responsibility to rebuild everything precisely as it was before.
1870 ret = transaction_commit(s, &base_t);
1871 if (ret) {
1872 return ret;
1875 while (1) {
1876 unsigned int base_tx;
1877 XsTransaction *t;
1879 ret = consume_be32(&us, &base_tx);
1880 if (ret) {
1881 return ret;
1883 if (base_tx == XBT_NULL) {
1884 break;
1887 t = g_new0(XsTransaction, 1);
1888 t->base_tx = base_tx;
1890 ret = consume_be32(&us, &t->dom_id);
1891 if (!ret) {
1892 ret = consume_tree(&us, t);
1894 if (ret) {
1895 g_free(t);
1896 return ret;
1898 g_assert(t->root);
1899 if (t->dom_id) {
1900 s->nr_domu_transactions++;
1902 g_hash_table_insert(s->transactions, GINT_TO_POINTER(t->tx_id), t);
1905 while (1) {
1906 char *path, *token;
1907 size_t pathlen, toklen;
1909 ret = consume_string(&us, &path, &pathlen);
1910 if (ret) {
1911 return ret;
1913 if (!pathlen) {
1914 break;
1917 ret = consume_string(&us, &token, &toklen);
1918 if (ret) {
1919 return ret;
1922 if (!watch_fn) {
1923 continue;
1926 ret = do_xs_impl_watch(s, dom_id, path, token, watch_fn, watch_opaque);
1927 if (ret) {
1928 return ret;
1932 if (us.l) {
1933 return -EINVAL;
1936 return 0;