Re: [PATCH v4 11/19] tools/xenstore: use struct node_hdr in struct node

2023-08-18 Thread Julien Grall

Hi Juergen,

On 14/08/2023 08:46, Juergen Gross wrote:

Replace some individual fields in struct node with struct node_hdr.

This allows to add a helper for calculating the accounted memory size
of a node.

Signed-off-by: Juergen Gross 


Reviewed-by: Julien Grall 

Cheers,

--
Julien Grall



[PATCH v4 11/19] tools/xenstore: use struct node_hdr in struct node

2023-08-14 Thread Juergen Gross
Replace some individual fields in struct node with struct node_hdr.

This allows to add a helper for calculating the accounted memory size
of a node.

Signed-off-by: Juergen Gross 
---
V2:
- new patch
V4:
- add const to parameter of calc_node_acc_size()
- modify comment (Julien Grall)
---
 tools/xenstore/xenstored_core.c| 110 -
 tools/xenstore/xenstored_core.h|  16 ++--
 tools/xenstore/xenstored_domain.c  |   5 +-
 tools/xenstore/xenstored_transaction.c |  13 +--
 4 files changed, 72 insertions(+), 72 deletions(-)

diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index d7a4f0f1cb..ab4714263d 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -555,6 +555,12 @@ static void initialize_fds(int *p_sock_pollfd_idx, int 
*ptimeout)
}
 }
 
+static size_t calc_node_acc_size(const struct node_hdr *hdr)
+{
+   return sizeof(*hdr) + hdr->num_perms * sizeof(struct xs_permissions) +
+  hdr->datalen + hdr->childlen;
+}
+
 const struct node_hdr *db_fetch(const char *db_name, size_t *size)
 {
const struct node_hdr *hdr;
@@ -565,8 +571,7 @@ const struct node_hdr *db_fetch(const char *db_name, size_t 
*size)
return NULL;
}
 
-   *size = sizeof(*hdr) + hdr->num_perms * sizeof(struct xs_permissions) +
-   hdr->datalen + hdr->childlen;
+   *size = calc_node_acc_size(hdr);
 
trace_tdb("read %s size %zu\n", db_name, *size + strlen(db_name));
 
@@ -726,7 +731,7 @@ struct node *read_node(struct connection *conn, const void 
*ctx,
hdr = db_fetch(db_name, );
 
if (hdr == NULL) {
-   node->generation = NO_GENERATION;
+   node->hdr.generation = NO_GENERATION;
err = access_node(conn, node, NODE_ACCESS_READ, NULL);
errno = err ? : ENOENT;
goto error;
@@ -735,10 +740,7 @@ struct node *read_node(struct connection *conn, const void 
*ctx,
node->parent = NULL;
 
/* Datalen, childlen, number of permissions */
-   node->generation = hdr->generation;
-   node->num_perms = hdr->num_perms;
-   node->datalen = hdr->datalen;
-   node->childlen = hdr->childlen;
+   node->hdr = *hdr;
node->acc.domid = perms_from_node_hdr(hdr)->id;
node->acc.memory = size;
 
@@ -760,7 +762,7 @@ struct node *read_node(struct connection *conn, const void 
*ctx,
/* Data is binary blob (usually ascii, no nul). */
node->data = node->perms + hdr->num_perms;
/* Children is strings, nul separated. */
-   node->children = node->data + node->datalen;
+   node->children = node->data + node->hdr.datalen;
 
if (access_node(conn, node, NODE_ACCESS_READ, NULL))
goto error;
@@ -796,9 +798,7 @@ int write_node_raw(struct connection *conn, const char 
*db_name,
if (domain_adjust_node_perms(node))
return errno;
 
-   size = sizeof(*hdr)
-   + node->num_perms * sizeof(node->perms[0])
-   + node->datalen + node->childlen;
+   size = calc_node_acc_size(>hdr);
 
/* Call domain_max_chk() in any case in order to record max values. */
if (domain_max_chk(conn, ACC_NODESZ, size) && !no_quota_check) {
@@ -815,18 +815,15 @@ int write_node_raw(struct connection *conn, const char 
*db_name,
BUILD_BUG_ON(XENSTORE_PAYLOAD_MAX >= (typeof(hdr->datalen))(-1));
 
hdr = data;
-   hdr->generation = node->generation;
-   hdr->num_perms = node->num_perms;
-   hdr->datalen = node->datalen;
-   hdr->childlen = node->childlen;
+   *hdr = node->hdr;
 
/* Open code perms_from_node_hdr() for the non-const case. */
p = hdr + 1;
-   memcpy(p, node->perms, node->num_perms * sizeof(*node->perms));
-   p += node->num_perms * sizeof(*node->perms);
-   memcpy(p, node->data, node->datalen);
-   p += node->datalen;
-   memcpy(p, node->children, node->childlen);
+   memcpy(p, node->perms, node->hdr.num_perms * sizeof(*node->perms));
+   p += node->hdr.num_perms * sizeof(*node->perms);
+   memcpy(p, node->data, node->hdr.datalen);
+   p += node->hdr.datalen;
+   memcpy(p, node->children, node->hdr.childlen);
 
if (db_write(conn, db_name, data, size, >acc, mode,
 no_quota_check))
@@ -1216,7 +1213,7 @@ static char *node_perms_to_strings(const struct node 
*node, unsigned int *len)
char *strings = NULL;
char buffer[MAX_STRLEN(unsigned int) + 1];
 
-   for (*len = 0, i = 0; i < node->num_perms; i++) {
+   for (*len = 0, i = 0; i < node->hdr.num_perms; i++) {
if (!xenstore_perm_to_string(>perms[i], buffer,
 sizeof(buffer)))
return NULL;
@@ -1287,7 +1284,7 @@ static int send_directory(const void *ctx, struct 
connection *conn,
if (!node)