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 21/25] tools/xenstore: introduce read_node_nocopy()
Date: Mon, 24 Jul 2023 13:02:43 +0200	[thread overview]
Message-ID: <20230724110247.10520-22-jgross@suse.com> (raw)
In-Reply-To: <20230724110247.10520-1-jgross@suse.com>

Introduce a read_node() variant returning a pointer to const struct
node, which doesn't do a copy of the node data after retrieval from
the data base.

Call this variant where appropriate.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V3:
- new approach (Julien Grall)
---
 tools/xenstore/xenstored_core.c   | 104 ++++++++++++++++++++++--------
 tools/xenstore/xenstored_core.h   |   2 +
 tools/xenstore/xenstored_domain.c |   4 +-
 tools/xenstore/xenstored_watch.c  |  10 +--
 tools/xenstore/xenstored_watch.h  |   3 +-
 5 files changed, 89 insertions(+), 34 deletions(-)

diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index ea3d20a372..102be92a43 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -705,11 +705,11 @@ void db_delete(struct connection *conn, const char *name,
  * If it fails, returns NULL and sets errno.
  * Temporary memory allocations will be done with ctx.
  */
-struct node *read_node(struct connection *conn, const void *ctx,
-		       const char *name)
+static struct node *read_node_alloc(struct connection *conn, const void *ctx,
+				    const char *name,
+				    const struct node_hdr **hdr)
 {
 	size_t size;
-	const struct node_hdr *hdr;
 	struct node *node;
 	const char *db_name;
 	int err;
@@ -719,17 +719,16 @@ struct node *read_node(struct connection *conn, const void *ctx,
 		errno = ENOMEM;
 		return NULL;
 	}
+
 	node->name = talloc_strdup(node, name);
 	if (!node->name) {
-		talloc_free(node);
 		errno = ENOMEM;
-		return NULL;
+		goto error;
 	}
 
 	db_name = transaction_prepend(conn, name);
-	hdr = db_fetch(db_name, &size);
-
-	if (hdr == NULL) {
+	*hdr = db_fetch(db_name, &size);
+	if (*hdr == NULL) {
 		node->hdr.generation = NO_GENERATION;
 		err = access_node(conn, node, NODE_ACCESS_READ, NULL);
 		errno = err ? : ENOENT;
@@ -739,31 +738,79 @@ struct node *read_node(struct connection *conn, const void *ctx,
 	node->parent = NULL;
 
 	/* Datalen, childlen, number of permissions */
-	node->hdr = *hdr;
-	node->acc.domid = perms_from_node_hdr(hdr)->id;
+	node->hdr = **hdr;
+	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 = talloc_memdup(node, perms_from_node_hdr(hdr), size);
-	if (node->perms == NULL) {
-		errno = ENOMEM;
-		goto error;
-	}
+	return node;
 
+ error:
+	talloc_free(node);
+	return NULL;
+}
+
+static int read_node_helper(struct connection *conn, struct node *node)
+{
 	/* Data is binary blob (usually ascii, no nul). */
-	node->data = node->perms + hdr->num_perms;
+	node->data = node->perms + node->hdr.num_perms;
 	/* Children is strings, nul separated. */
 	node->children = node->data + node->hdr.datalen;
 
 	if (domain_adjust_node_perms(node))
-		goto error;
+		return -1;
 
 	/* If owner is gone reset currently accounted memory size. */
 	if (node->acc.domid != get_node_owner(node))
 		node->acc.memory = 0;
 
 	if (access_node(conn, node, NODE_ACCESS_READ, NULL))
+		return -1;
+
+	return 0;
+}
+
+struct node *read_node(struct connection *conn, const void *ctx,
+		       const char *name)
+{
+	size_t size;
+	const struct node_hdr *hdr;
+	struct node *node;
+
+	node = read_node_alloc(conn, ctx, name, &hdr);
+	if (!node)
+		return NULL;
+
+	/* Copy node data to new memory area, starting with permissions. */
+	size = node->acc.memory - sizeof(*hdr);
+	node->perms = talloc_memdup(node, perms_from_node_hdr(hdr), size);
+	if (node->perms == NULL) {
+		errno = ENOMEM;
+		goto error;
+	}
+
+	if (read_node_helper(conn, node))
+		goto error;
+
+	return node;
+
+ error:
+	talloc_free(node);
+	return NULL;
+}
+
+const struct node *read_node_const(struct connection *conn, const void *ctx,
+				   const char *name)
+{
+	const struct node_hdr *hdr;
+	struct node *node;
+
+	node = read_node_alloc(conn, ctx, name, &hdr);
+	if (!node)
+		return NULL;
+
+	node->perms = perms_from_node_hdr(hdr);
+
+	if (read_node_helper(conn, node))
 		goto error;
 
 	return node;
@@ -896,14 +943,14 @@ char *get_parent(const void *ctx, const char *node)
 static int ask_parents(struct connection *conn, const void *ctx,
 		       const char *name, unsigned int *perm)
 {
-	struct node *node;
+	const struct node *node;
 	struct node_perms perms;
 
 	do {
 		name = get_parent(ctx, name);
 		if (!name)
 			return errno;
-		node = read_node(conn, ctx, name);
+		node = read_node_const(conn, ctx, name);
 		if (node)
 			break;
 		if (read_node_can_propagate_errno())
@@ -3194,9 +3241,8 @@ static int dump_state_node_err(struct dump_node_data *data, const char *err)
 }
 
 static int dump_state_node(const void *ctx, struct connection *conn,
-			   struct node *node, void *arg)
+			   const struct node *node, struct dump_node_data *data)
 {
-	struct dump_node_data *data = arg;
 	FILE *fp = data->fp;
 	unsigned int pathlen;
 	struct xs_state_record_header head;
@@ -3241,14 +3287,20 @@ static int dump_state_node(const void *ctx, struct connection *conn,
 	return WALK_TREE_OK;
 }
 
+static int dump_state_node_enter(const void *ctx, struct connection *conn,
+				 struct node *node, void *arg)
+{
+	return dump_state_node(ctx, conn, node, arg);
+}
+
 static int dump_state_special_node(FILE *fp, const void *ctx,
 				   struct dump_node_data *data,
 				   const char *name)
 {
-	struct node *node;
+	const struct node *node;
 	int ret;
 
-	node = read_node(NULL, ctx, name);
+	node = read_node_const(NULL, ctx, name);
 	if (!node)
 		return dump_state_node_err(data, "Dump node read node error");
 
@@ -3264,7 +3316,7 @@ const char *dump_state_nodes(FILE *fp, const void *ctx)
 		.fp = fp,
 		.err = "Dump node walk error"
 	};
-	struct walk_funcs walkfuncs = { .enter = dump_state_node };
+	struct walk_funcs walkfuncs = { .enter = dump_state_node_enter };
 
 	if (walk_node_tree(ctx, NULL, "/", &walkfuncs, &data))
 		return data.err;
diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_core.h
index adf8a785fc..65782c559d 100644
--- a/tools/xenstore/xenstored_core.h
+++ b/tools/xenstore/xenstored_core.h
@@ -282,6 +282,8 @@ int write_node_raw(struct connection *conn, const char *db_name,
 /* Get a node from the data base. */
 struct node *read_node(struct connection *conn, const void *ctx,
 		       const char *name);
+const struct node *read_node_const(struct connection *conn, const void *ctx,
+				   const char *name);
 
 /* Remove a node and its children. */
 int rm_node(struct connection *conn, const void *ctx, const char *name);
diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_domain.c
index cdef6efef4..7290bbc848 100644
--- a/tools/xenstore/xenstored_domain.c
+++ b/tools/xenstore/xenstored_domain.c
@@ -563,12 +563,12 @@ static void domain_tree_remove(struct domain *domain)
 static void fire_special_watches(const char *name)
 {
 	void *ctx = talloc_new(NULL);
-	struct node *node;
+	const struct node *node;
 
 	if (!ctx)
 		return;
 
-	node = read_node(NULL, ctx, name);
+	node = read_node_const(NULL, ctx, name);
 
 	if (node)
 		fire_watches(NULL, ctx, name, node, true, NULL);
diff --git a/tools/xenstore/xenstored_watch.c b/tools/xenstore/xenstored_watch.c
index c161385f89..86cf8322b4 100644
--- a/tools/xenstore/xenstored_watch.c
+++ b/tools/xenstore/xenstored_watch.c
@@ -73,11 +73,11 @@ static const char *get_watch_path(const struct watch *watch, const char *name)
  * changed permissions we need to take the old permissions into account, too.
  */
 static bool watch_permitted(struct connection *conn, const void *ctx,
-			    const char *name, struct node *node,
+			    const char *name, const struct node *node,
 			    struct node_perms *perms)
 {
 	unsigned int perm;
-	struct node *parent;
+	const struct node *parent;
 	char *parent_name;
 	struct node_perms node_perms;
 
@@ -88,7 +88,7 @@ static bool watch_permitted(struct connection *conn, const void *ctx,
 	}
 
 	if (!node) {
-		node = read_node(conn, ctx, name);
+		node = read_node_const(conn, ctx, name);
 		if (!node)
 			return false;
 	}
@@ -103,7 +103,7 @@ static bool watch_permitted(struct connection *conn, const void *ctx,
 		parent_name = get_parent(ctx, node->name);
 		if (!parent_name)
 			return false;
-		parent = read_node(conn, ctx, parent_name);
+		parent = read_node_const(conn, ctx, parent_name);
 		if (!parent)
 			return false;
 	}
@@ -122,7 +122,7 @@ static bool watch_permitted(struct connection *conn, const void *ctx,
  * watch event, too.
  */
 void fire_watches(struct connection *conn, const void *ctx, const char *name,
-		  struct node *node, bool exact, struct node_perms *perms)
+		  const struct node *node, bool exact, struct node_perms *perms)
 {
 	struct connection *i;
 	struct buffered_data *req;
diff --git a/tools/xenstore/xenstored_watch.h b/tools/xenstore/xenstored_watch.h
index 091890edca..ea247997ad 100644
--- a/tools/xenstore/xenstored_watch.h
+++ b/tools/xenstore/xenstored_watch.h
@@ -28,7 +28,8 @@ int do_unwatch(const void *ctx, struct connection *conn,
 
 /* Fire all watches: !exact means all the children are affected (ie. rm). */
 void fire_watches(struct connection *conn, const void *tmp, const char *name,
-		  struct node *node, bool exact, struct node_perms *perms);
+		  const struct node *node, bool exact,
+		  struct node_perms *perms);
 
 void conn_delete_all_watches(struct connection *conn);
 
-- 
2.35.3



  parent reply	other threads:[~2023-07-24 11:08 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 ` [PATCH v3 17/25] tools/xenstore: rework struct xs_tdb_record_hdr Juergen Gross
2023-07-27 21:53   ` 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 ` Juergen Gross [this message]
2023-08-01 22:00   ` [PATCH v3 21/25] tools/xenstore: introduce read_node_nocopy() 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-22-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.