All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>, Wei Liu <wl@xen.org>,
	Julien Grall <julien@xen.org>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH v3 17/25] tools/xenstore: rework struct xs_tdb_record_hdr
Date: Mon, 24 Jul 2023 13:02:39 +0200	[thread overview]
Message-ID: <20230724110247.10520-18-jgross@suse.com> (raw)
In-Reply-To: <20230724110247.10520-1-jgross@suse.com>

Struct xs_tdb_record_hdr is used for nodes stored in the data base.
When working on a node, struct node is being used, which is including
the same information as struct xs_tdb_record_hdr, but in a different
format. Rework struct xs_tdb_record_hdr in order to prepare including
it in struct node.

Do the following modifications:

- move its definition to xenstored_core.h, as the reason to put it into
  utils.h are no longer existing

- rename it to struct node_hdr, as the "tdb" in its name has only
  historical reasons

- replace the empty permission array at the end with a comment about
  the layout of data in the data base (concatenation of header,
  permissions, node contents, and children list)

- use narrower types for num_perms and datalen, as those are naturally
  limited to XENSTORE_PAYLOAD_MAX (childlen is different here, as it is
  in theory basically unlimited)

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- new patch
---
 tools/xenstore/utils.h                 |  9 -------
 tools/xenstore/xenstored_core.c        | 35 +++++++++++++++-----------
 tools/xenstore/xenstored_core.h        | 20 ++++++++++++++-
 tools/xenstore/xenstored_transaction.c |  6 ++---
 4 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/tools/xenstore/utils.h b/tools/xenstore/utils.h
index 028ecb9d7a..405d662ea2 100644
--- a/tools/xenstore/utils.h
+++ b/tools/xenstore/utils.h
@@ -9,15 +9,6 @@
 
 #include "xenstore_lib.h"
 
-/* Header of the node record in tdb. */
-struct xs_tdb_record_hdr {
-	uint64_t generation;
-	uint32_t num_perms;
-	uint32_t datalen;
-	uint32_t childlen;
-	struct xs_permissions perms[0];
-};
-
 /* Is A == B ? */
 #define streq(a,b) (strcmp((a),(b)) == 0)
 
diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 1f5f118f1c..86b7c9bf36 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -555,9 +555,9 @@ static void initialize_fds(int *p_sock_pollfd_idx, int *ptimeout)
 	}
 }
 
-const struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size)
+const struct node_hdr *db_fetch(const char *db_name, size_t *size)
 {
-	struct xs_tdb_record_hdr *hdr;
+	struct node_hdr *hdr;
 
 	hdr = hashtable_search(nodes, db_name);
 	if (!hdr) {
@@ -565,7 +565,7 @@ const struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size)
 		return NULL;
 	}
 
-	*size = sizeof(*hdr) + hdr->num_perms * sizeof(hdr->perms[0]) +
+	*size = sizeof(*hdr) + hdr->num_perms * sizeof(struct xs_permissions) +
 		hdr->datalen + hdr->childlen;
 
 	trace_tdb("read %s size %zu\n", db_name, *size + strlen(db_name));
@@ -573,10 +573,15 @@ const struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size)
 	return hdr;
 }
 
+static struct xs_permissions *perms_from_node_hdr(const struct node_hdr *hdr)
+{
+	return (struct xs_permissions *)(hdr + 1);
+}
+
 static void get_acc_data(const char *name, struct node_account_data *acc)
 {
 	size_t size;
-	const struct xs_tdb_record_hdr *hdr;
+	const struct node_hdr *hdr;
 
 	if (acc->memory < 0) {
 		hdr = db_fetch(name, &size);
@@ -585,7 +590,7 @@ static void get_acc_data(const char *name, struct node_account_data *acc)
 			acc->memory = 0;
 		} else {
 			acc->memory = size;
-			acc->domid = hdr->perms[0].id;
+			acc->domid = perms_from_node_hdr(hdr)->id;
 		}
 	}
 }
@@ -606,7 +611,7 @@ int db_write(struct connection *conn, const char *db_name, const void *data,
 	     size_t size, struct node_account_data *acc,
 	     enum write_node_mode mode, bool no_quota_check)
 {
-	const struct xs_tdb_record_hdr *hdr = data;
+	const struct node_hdr *hdr = data;
 	struct node_account_data old_acc = {};
 	unsigned int old_domid, new_domid;
 	size_t name_len = strlen(db_name);
@@ -620,7 +625,7 @@ int db_write(struct connection *conn, const char *db_name, const void *data,
 
 	get_acc_data(db_name, &old_acc);
 	old_domid = get_acc_domid(conn, db_name, old_acc.domid);
-	new_domid = get_acc_domid(conn, db_name, hdr->perms[0].id);
+	new_domid = get_acc_domid(conn, db_name, perms_from_node_hdr(hdr)->id);
 
 	/*
 	 * Don't check for ENOENT, as we want to be able to switch orphaned
@@ -661,7 +666,7 @@ int db_write(struct connection *conn, const char *db_name, const void *data,
 
 	if (acc) {
 		/* Don't use new_domid, as it might be a transaction node. */
-		acc->domid = hdr->perms[0].id;
+		acc->domid = perms_from_node_hdr(hdr)->id;
 		acc->memory = size;
 	}
 
@@ -699,7 +704,7 @@ struct node *read_node(struct connection *conn, const void *ctx,
 		       const char *name)
 {
 	size_t size;
-	const struct xs_tdb_record_hdr *hdr;
+	const struct node_hdr *hdr;
 	struct node *node;
 	const char *db_name;
 	int err;
@@ -733,12 +738,12 @@ struct node *read_node(struct connection *conn, const void *ctx,
 	node->perms.num = hdr->num_perms;
 	node->datalen = hdr->datalen;
 	node->childlen = hdr->childlen;
-	node->acc.domid = hdr->perms[0].id;
+	node->acc.domid = perms_from_node_hdr(hdr)->id;
 	node->acc.memory = size;
 
 	/* Copy node data to new memory area, starting with permissions. */
 	size -= sizeof(*hdr);
-	node->perms.p = talloc_memdup(node, hdr->perms, size);
+	node->perms.p = talloc_memdup(node, perms_from_node_hdr(hdr), size);
 	if (node->perms.p == NULL) {
 		errno = ENOMEM;
 		goto error;
@@ -785,7 +790,7 @@ int write_node_raw(struct connection *conn, const char *db_name,
 	void *data;
 	size_t size;
 	void *p;
-	struct xs_tdb_record_hdr *hdr;
+	struct node_hdr *hdr;
 
 	if (domain_adjust_node_perms(node))
 		return errno;
@@ -812,9 +817,9 @@ int write_node_raw(struct connection *conn, const char *db_name,
 	hdr->datalen = node->datalen;
 	hdr->childlen = node->childlen;
 
-	memcpy(hdr->perms, node->perms.p,
-	       node->perms.num * sizeof(*node->perms.p));
-	p = hdr->perms + node->perms.num;
+	p = perms_from_node_hdr(hdr);
+	memcpy(p, node->perms.p, node->perms.num * sizeof(*node->perms.p));
+	p += node->perms.num * sizeof(*node->perms.p);
 	memcpy(p, node->data, node->datalen);
 	p += node->datalen;
 	memcpy(p, node->children, node->childlen);
diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_core.h
index 6d1578ce97..c965709090 100644
--- a/tools/xenstore/xenstored_core.h
+++ b/tools/xenstore/xenstored_core.h
@@ -168,6 +168,24 @@ struct connection
 };
 extern struct list_head connections;
 
+/*
+ * Header of the node record in the data base.
+ * In the data base the memory of the node is a single memory chunk with the
+ * following format:
+ * struct {
+ *     node_hdr hdr;
+ *     struct xs_permissions perms[hdr.num_perms];
+ *     char data[hdr.datalen];
+ *     char children[hdr.childlen];
+ * };
+ */
+struct node_hdr {
+	uint64_t generation;
+	uint16_t num_perms;
+	uint16_t datalen;
+	uint32_t childlen;
+};
+
 struct node_perms {
 	unsigned int num;
 	struct xs_permissions *p;
@@ -362,7 +380,7 @@ extern xengnttab_handle **xgt_handle;
 int remember_string(struct hashtable *hash, const char *str);
 
 /* Data base access functions. */
-const struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size);
+const struct node_hdr *db_fetch(const char *db_name, size_t *size);
 int db_write(struct connection *conn, const char *db_name, const void *data,
 	     size_t size, struct node_account_data *acc,
 	     enum write_node_mode mode, bool no_quota_check);
diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xenstored_transaction.c
index a90283dcc5..9ca73b9874 100644
--- a/tools/xenstore/xenstored_transaction.c
+++ b/tools/xenstore/xenstored_transaction.c
@@ -357,7 +357,7 @@ static int finalize_transaction(struct connection *conn,
 {
 	struct accessed_node *i, *n;
 	size_t size;
-	const struct xs_tdb_record_hdr *hdr;
+	const struct node_hdr *hdr;
 	uint64_t gen;
 
 	list_for_each_entry_safe(i, n, &trans->accessed, list) {
@@ -394,12 +394,12 @@ static int finalize_transaction(struct connection *conn,
 				 * generation count.
 				 */
 				enum write_node_mode mode;
-				struct xs_tdb_record_hdr *own;
+				struct node_hdr *own;
 
 				talloc_increase_ref_count(hdr);
 				db_delete(conn, i->trans_name, NULL);
 
-				own = (struct xs_tdb_record_hdr *)hdr;
+				own = (struct node_hdr *)hdr;
 				own->generation = ++generation;
 				mode = (i->generation == NO_GENERATION)
 				       ? NODE_CREATE : NODE_MODIFY;
-- 
2.35.3



  parent reply	other threads:[~2023-07-24 11:07 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24 11:02 [PATCH v3 00/25] tools/xenstore: drop TDB Juergen Gross
2023-07-24 11:02 ` [PATCH v3 01/25] tools/xenstore: explicitly specify create or modify for tdb_store() Juergen Gross
2023-07-25 16:01   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 02/25] tools/xenstore: replace key in struct node with data base name Juergen Gross
2023-07-24 11:02 ` [PATCH v3 03/25] tools/xenstore: let transaction_prepend() return the name for access Juergen Gross
2023-07-24 11:02 ` [PATCH v3 04/25] tools/xenstore: rename do_tdb_delete() and change parameter type Juergen Gross
2023-07-24 11:02 ` [PATCH v3 05/25] tools/xenstore: rename do_tdb_write() " Juergen Gross
2023-07-24 11:02 ` [PATCH v3 06/25] tools/xenstore: switch get_acc_data() to use name instead of key Juergen Gross
2023-07-24 11:02 ` [PATCH v3 07/25] tools/xenstore: add wrapper for tdb_fetch() Juergen Gross
2023-07-24 11:02 ` [PATCH v3 08/25] tools/xenstore: make hashtable key and value parameters const Juergen Gross
2023-07-25 16:08   ` Julien Grall
2023-07-26  6:19     ` Juergen Gross
2023-07-26  8:20       ` Julien Grall
2023-07-26  8:44         ` Juergen Gross
2023-07-26  9:29           ` Julien Grall
2023-07-26 11:07             ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 09/25] tools/xenstore: let hashtable_add() fail in case of existing entry Juergen Gross
2023-07-24 11:02 ` [PATCH v3 10/25] tools/xenstore: add hashtable_replace() function Juergen Gross
2023-07-27 21:00   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 11/25] tools/xenstore: drop use of tdb Juergen Gross
2023-07-27 21:07   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 12/25] tools/xenstore: remove tdb code Juergen Gross
2023-07-24 11:02 ` [PATCH v3 13/25] tools/xenstore: let db_delete() return void Juergen Gross
2023-07-24 11:02 ` [PATCH v3 14/25] tools/xenstore: change talloc_free() to take a const pointer Juergen Gross
2023-07-27 21:21   ` Julien Grall
2023-07-28  6:15     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 15/25] tools/xenstore: make data parameter of db_write() const Juergen Gross
2023-07-24 11:02 ` [PATCH v3 16/25] tools/xenstore: move copying of node data out of db_fetch() Juergen Gross
2023-07-27 21:33   ` Julien Grall
2023-07-28  6:18     ` Juergen Gross
2023-07-24 11:02 ` Juergen Gross [this message]
2023-07-27 21:53   ` [PATCH v3 17/25] tools/xenstore: rework struct xs_tdb_record_hdr Julien Grall
2023-07-28  6:23     ` Juergen Gross
2023-07-28  8:59       ` Julien Grall
2023-07-28  9:14         ` Juergen Gross
2023-07-28  9:38           ` Julien Grall
2023-07-28  9:45             ` Juergen Gross
2023-07-28 10:34               ` Julien Grall
2023-07-28 10:47                 ` Juergen Gross
2023-07-28 11:19                   ` Julien Grall
2023-07-28 12:06                     ` Juergen Gross
2023-07-28 12:48                       ` Julien Grall
2023-07-28 13:24                         ` Juergen Gross
2023-07-28 14:08                           ` Julien Grall
2023-07-28 14:32                             ` Juergen Gross
2023-07-28 14:59                               ` Julien Grall
2023-07-28 15:08                                 ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 18/25] tools/xenstore: don't use struct node_perms in struct node Juergen Gross
2023-08-01 21:29   ` Julien Grall
2023-08-02  4:47     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 19/25] tools/xenstore: use struct node_hdr " Juergen Gross
2023-08-01 21:34   ` Julien Grall
2023-08-02  4:50     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 20/25] tools/xenstore: alloc new memory in domain_adjust_node_perms() Juergen Gross
2023-08-01 21:46   ` Julien Grall
2023-08-02  4:51     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 21/25] tools/xenstore: introduce read_node_nocopy() Juergen Gross
2023-08-01 22:00   ` Julien Grall
2023-08-02  4:52     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 22/25] tools/xenstore: merge get_spec_node() into get_node_canonicalized() Juergen Gross
2023-08-03 21:36   ` Julien Grall
2023-08-04  9:17     ` Juergen Gross
2023-08-04  9:21       ` Julien Grall
2023-08-04  9:34         ` Juergen Gross
2023-08-04  9:44           ` Julien Grall
2023-08-04  9:56             ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 23/25] tools/xenstore: merge is_valid_nodename() into canonicalize() Juergen Gross
2023-08-03 21:46   ` Julien Grall
2023-08-04  9:35     ` Juergen Gross
2023-08-04 10:00       ` Julien Grall
2023-08-04 10:17         ` Juergen Gross
2023-08-04 10:33           ` Julien Grall
2023-08-04 12:05             ` Juergen Gross
2023-08-04 12:27               ` Julien Grall
2023-08-04 12:43                 ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 24/25] tools/xenstore: rework get_node() Juergen Gross
2023-08-12 11:56   ` Julien Grall
2023-08-14  5:42     ` Juergen Gross
2023-08-12 12:03   ` Julien Grall
2023-08-14  5:48     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 25/25] tools/xenstore: introduce get_node_const() Juergen Gross
2023-08-12 12:05   ` Julien Grall
2023-08-14  5:54     ` Juergen Gross
2023-07-27 21:02 ` [PATCH v3 00/25] tools/xenstore: drop TDB Julien Grall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230724110247.10520-18-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=anthony.perard@citrix.com \
    --cc=julien@xen.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.