int rel_prefix;
} XsWatch;
+typedef struct XsTransaction {
+ XsNode *root;
+ unsigned int base_tx;
+ unsigned int tx_id;
+ unsigned int dom_id;
+} XsTransaction;
+
struct XenstoreImplState {
XsNode *root;
GHashTable *watches;
+ GHashTable *transactions;
+ unsigned int root_tx;
+ unsigned int last_tx;
};
static void xs_node_init(Object *obj)
bool inplace;
bool mutating;
bool create_dirs;
+ bool in_transaction;
};
static void fire_watches(struct walk_op *op, bool parents)
GList *l = NULL;
XsWatch *w;
- if (!op->mutating) {
+ if (!op->mutating || op->in_transaction) {
return;
}
op->path[namelen] = '\0';
if (!namelen) {
assert(!op->watches);
+ /*
+ * If the main tree was changed, bump its tx ID so that outstanding
+ * transactions correctly fail. But don't bump it every time; only
+ * if it makes a difference.
+ */
+ if (op->mutating && !op->in_transaction) {
+ if (op->s->root_tx != op->s->last_tx) {
+ op->s->root_tx = ++op->s->last_tx;
+ }
+ }
}
return 0;
}
op->inplace = true;
op->mutating = false;
op->create_dirs = false;
+ op->in_transaction = false;
op->dom_id = dom_id;
op->s = s;
if (tx_id == XBT_NULL) {
*rootp = &s->root;
} else {
- return ENOENT;
+ XsTransaction *tx = g_hash_table_lookup(s->transactions,
+ GINT_TO_POINTER(tx_id));
+ if (!tx) {
+ return ENOENT;
+ }
+ *rootp = &tx->root;
+ op->in_transaction = true;
}
return 0;
int xs_impl_transaction_start(XenstoreImplState *s, unsigned int dom_id,
unsigned int tx_id, unsigned int *new_tx)
{
- return ENOSYS;
+ XsTransaction *tx = g_new0(XsTransaction, 1);
+
+ tx->tx_id = ++s->last_tx;
+ tx->base_tx = s->root_tx;
+ tx->root = xs_node_ref(s->root);
+ tx->dom_id = dom_id;
+
+ g_hash_table_insert(s->transactions, GINT_TO_POINTER(tx->tx_id), tx);
+ *new_tx = tx->tx_id;
+ return 0;
+}
+
+static int transaction_commit(XenstoreImplState *s, XsTransaction *tx)
+{
+ if (s->root_tx != tx->base_tx) {
+ return EAGAIN;
+ }
+ xs_node_unref(s->root);
+ s->root = tx->root;
+ tx->root = NULL;
+ s->root_tx = tx->tx_id;
+
+ /*
+ * XX: Walk the new root and fire watches on any node which has a
+ * refcount of one (which is therefore unique to this transaction).
+ */
+ return 0;
}
int xs_impl_transaction_end(XenstoreImplState *s, unsigned int dom_id,
unsigned int tx_id, bool commit)
{
- return ENOSYS;
+ int ret = 0;
+
+ if (commit) {
+ XsTransaction *tx = g_hash_table_lookup(s->transactions,
+ GINT_TO_POINTER(tx_id));
+ if (!tx || tx->dom_id != dom_id) {
+ return ENOENT;
+ }
+
+ ret = transaction_commit(s, tx);
+ /*
+ * It *is* in the hash table still, so g_hash_table_remove() will
+ * return true and we'll return ret;
+ */
+ }
+
+ if (g_hash_table_remove(s->transactions, GINT_TO_POINTER(tx_id))) {
+ return ret;
+ } else {
+ return ENOENT;
+ }
}
int xs_impl_rm(XenstoreImplState *s, unsigned int dom_id, unsigned int tx_id,
return 0;
}
+static void xs_tx_free(void *_tx)
+{
+ XsTransaction *tx = _tx;
+ if (tx->root) {
+ xs_node_unref(tx->root);
+ }
+ g_free(tx);
+}
XenstoreImplState *xs_impl_create(void)
{
XenstoreImplState *s = g_new0(XenstoreImplState, 1);
s->watches = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+ s->transactions = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+ NULL, xs_tx_free);
s->root = xs_node_new();
#ifdef XS_NODE_UNIT_TEST
s->root->name = g_strdup("/");
/* This doesn't happen in qemu but we want to make valgrind happy */
static void xs_impl_delete(XenstoreImplState *s)
{
+ g_hash_table_unref(s->transactions);
g_hash_table_unref(s->watches);
xs_node_unref(s->root);
g_free(s);
}
+static void do_test_xs_node_tx(bool fail, bool commit)
+{
+ XenstoreImplState *s = xs_impl_create();
+ GString *watches = g_string_new(NULL);
+ GByteArray *data = g_byte_array_new();
+ unsigned int tx_id;
+ int err;
+
+ g_assert(s);
+
+ /* Set a watch */
+ err = xs_impl_watch(s, DOMID_GUEST, "some", "watch",
+ watch_cb, watches);
+ g_assert(!err);
+
+ /* Write something */
+ err = write_str(s, DOMID_GUEST, XBT_NULL, "some/relative/path",
+ "something");
+ g_assert(!err);
+ g_assert(!strcmp(watches->str,
+ "some/relative/pathwatch"));
+ g_string_truncate(watches, 0);
+
+ /* Create a transaction */
+ err = xs_impl_transaction_start(s, DOMID_GUEST, XBT_NULL, &tx_id);
+ g_assert(!err);
+
+ if (fail) {
+ /* Write something else in the root */
+ err = write_str(s, DOMID_GUEST, XBT_NULL, "some/relative/path",
+ "another thing");
+ g_assert(!err);
+ g_assert(!strcmp(watches->str,
+ "some/relative/pathwatch"));
+ g_string_truncate(watches, 0);
+ }
+
+ g_assert(!watches->len);
+
+ /* Perform a write in the transaction */
+ err = write_str(s, DOMID_GUEST, tx_id, "some/relative/path",
+ "something else");
+ g_assert(!err);
+ g_assert(!watches->len);
+
+ /* The transaction should fail */
+ err = xs_impl_transaction_end(s, DOMID_GUEST, tx_id, commit);
+ if (commit && fail) {
+ g_assert(err == EAGAIN);
+ } else {
+ g_assert(!err);
+ }
+ g_assert(!watches->len);
+
+ err = xs_impl_unwatch(s, DOMID_GUEST, "some", "watch",
+ watch_cb, watches);
+ g_assert(!err);
+
+ err = xs_impl_read(s, DOMID_GUEST, XBT_NULL, "some/relative/path", data);
+ g_assert(!err);
+ if (fail) {
+ g_assert(data->len == strlen("another thing"));
+ g_assert(!memcmp(data->data, "another thing", data->len));
+ } else if (commit) {
+ g_assert(data->len == strlen("something else"));
+ g_assert(!memcmp(data->data, "something else", data->len));
+ } else {
+ g_assert(data->len == strlen("something"));
+ g_assert(!memcmp(data->data, "something", data->len));
+ }
+ g_byte_array_unref(data);
+ g_string_free(watches, true);
+ xs_impl_delete(s);
+}
+
+static void test_xs_node_tx_fail(void)
+{
+ do_test_xs_node_tx(true, true);
+}
+
+static void test_xs_node_tx_abort(void)
+{
+ do_test_xs_node_tx(false, false);
+ do_test_xs_node_tx(true, false);
+}
+static void test_xs_node_tx_succeed(void)
+{
+ do_test_xs_node_tx(false, true);
+}
+
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
module_call_init(MODULE_INIT_QOM);
g_test_add_func("/xs_node/simple", test_xs_node_simple);
+ g_test_add_func("/xs_node/tx_abort", test_xs_node_tx_abort);
+ g_test_add_func("/xs_node/tx_fail", test_xs_node_tx_fail);
+ g_test_add_func("/xs_node/tx_succeed", test_xs_node_tx_succeed);
return g_test_run();
}