From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196611; cv=none; d=zohomail.com; s=zohoarc; b=Wj5tqPe5/2gpYFBfJwuAT90Q1vkFnSOyz6wSPofjRvM3ZQU4EF5mRgMv2AoHwJe2szTpFbenLEANP8ksYRJjRL6/GTkRhA8nqqG7/fiJSQchwoZEqinfKr1Xa0UW1PBy82VSNhSRytpq/RDmqCQTRhzf07R1Ubgduy7G8GI6mOw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196611; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cT5wbOHPovpy2wmMRthEc2mPjIWIHsQuAYvw6oSHDTA=; b=C0B9XTnbEEES+wuuvE7QC42TKm83P/xfZqnz1MkJ1h6siX4E2SZGZP0VoyzQSTJGa52Dju+L97Kl/0nnNAv0sa8thiDPDFvdES6/FP4+DqWmt6OweyBbMq/h4QK7Y6ag2nIhDcQh5UgEp+cR2bQk7phbe+p4GSYXgDqRSxIm7CQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196611310280.8224959930608; Mon, 24 Jul 2023 04:03:31 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568730.888563 (Exim 4.92) (envelope-from ) id 1qNtL9-0000LB-1k; Mon, 24 Jul 2023 11:02:59 +0000 Received: by outflank-mailman (output) from mailman id 568730.888563; Mon, 24 Jul 2023 11:02:59 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtL8-0000L4-VB; Mon, 24 Jul 2023 11:02:58 +0000 Received: by outflank-mailman (input) for mailman id 568730; Mon, 24 Jul 2023 11:02:58 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtL8-0000KR-Ex for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:02:58 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2001:67c:2178:6::1d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a49787cc-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:02:56 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id D2E6B20695; Mon, 24 Jul 2023 11:02:55 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A1E9D13476; Mon, 24 Jul 2023 11:02:55 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id KKYWJl9avmQ1YQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:02:55 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a49787cc-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196575; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cT5wbOHPovpy2wmMRthEc2mPjIWIHsQuAYvw6oSHDTA=; b=tZn4pcGGTi0KztJ64BTwtc6PiFzGkIzQoGq7EfZkZQHXj8PoDgd8VVlulmaaYzMYrjGnvq 6O2n8ht8qDcX+wjuiIgjbLGMnyAlhmHE7sQi6LtgweLFV9Dael0ZTbkcAC3B2MXcpkoEH5 pLIIn7U62eSEqyhG4opnl/bYf4cdiw0= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 01/25] tools/xenstore: explicitly specify create or modify for tdb_store() Date: Mon, 24 Jul 2023 13:02:23 +0200 Message-Id: <20230724110247.10520-2-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196611645100001 Content-Type: text/plain; charset="utf-8" Instead of using TDB_REPLACE for either creating or modifying a TDB entry, use either TDB_INSERT or TDB_MODIFY when calling tdb_store(). At higher function levels use the abstract mode values NODE_CREATE and NODE_MODIFY. This is for preparing to get rid of TDB, even if it is beneficial while using TDB, too. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - use an enum for the new "flag" parameter of do_tdb_write() (Julien Grall) - add comment to read_state_node() (Julien Grall) V3: - use enum in finalize_transaction() and reduce the scope of it (Julien Grall) --- tools/xenstore/xenstored_core.c | 32 +++++++++++++++----------- tools/xenstore/xenstored_core.h | 10 ++++++-- tools/xenstore/xenstored_domain.c | 2 +- tools/xenstore/xenstored_transaction.c | 9 ++++++-- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 9f1328a31f..b7fdf16718 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -601,7 +601,8 @@ static unsigned int get_acc_domid(struct connection *co= nn, TDB_DATA *key, } =20 int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data, - struct node_account_data *acc, bool no_quota_check) + struct node_account_data *acc, enum write_node_mode mode, + bool no_quota_check) { struct xs_tdb_record_hdr *hdr =3D (void *)data->dptr; struct node_account_data old_acc =3D {}; @@ -635,7 +636,8 @@ int do_tdb_write(struct connection *conn, TDB_DATA *key= , TDB_DATA *data, } =20 /* TDB should set errno, but doesn't even set ecode AFAICT. */ - if (tdb_store(tdb_ctx, *key, *data, TDB_REPLACE) !=3D 0) { + if (tdb_store(tdb_ctx, *key, *data, + (mode =3D=3D NODE_CREATE) ? TDB_INSERT : TDB_MODIFY) !=3D 0) { domain_memory_add_nochk(conn, new_domid, -data->dsize - key->dsize); /* Error path, so no quota check. */ @@ -774,7 +776,7 @@ static bool read_node_can_propagate_errno(void) } =20 int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *no= de, - bool no_quota_check) + enum write_node_mode mode, bool no_quota_check) { TDB_DATA data; void *p; @@ -812,7 +814,7 @@ int write_node_raw(struct connection *conn, TDB_DATA *k= ey, struct node *node, p +=3D node->datalen; memcpy(p, node->children, node->childlen); =20 - if (do_tdb_write(conn, key, &data, &node->acc, no_quota_check)) + if (do_tdb_write(conn, key, &data, &node->acc, mode, no_quota_check)) return EIO; =20 return 0; @@ -823,14 +825,14 @@ int write_node_raw(struct connection *conn, TDB_DATA = *key, struct node *node, * node->key. This can later be used if the change needs to be reverted. */ static int write_node(struct connection *conn, struct node *node, - bool no_quota_check) + enum write_node_mode mode, bool no_quota_check) { int ret; =20 if (access_node(conn, node, NODE_ACCESS_WRITE, &node->key)) return errno; =20 - ret =3D write_node_raw(conn, &node->key, node, no_quota_check); + ret =3D write_node_raw(conn, &node->key, node, mode, no_quota_check); if (ret && conn && conn->transaction) { /* * Reverting access_node() is hard, so just fail the @@ -1500,7 +1502,8 @@ static struct node *create_node(struct connection *co= nn, const void *ctx, goto err; } =20 - ret =3D write_node(conn, i, false); + ret =3D write_node(conn, i, i->parent ? NODE_CREATE : NODE_MODIFY, + false); if (ret) goto err; =20 @@ -1564,7 +1567,7 @@ static int do_write(const void *ctx, struct connectio= n *conn, } else { node->data =3D in->buffer + offset; node->datalen =3D datalen; - if (write_node(conn, node, false)) + if (write_node(conn, node, NODE_MODIFY, false)) return errno; } =20 @@ -1614,7 +1617,7 @@ static int remove_child_entry(struct connection *conn= , struct node *node, memdel(node->children, offset, childlen + 1, node->childlen); node->childlen -=3D childlen + 1; =20 - return write_node(conn, node, true); + return write_node(conn, node, NODE_MODIFY, true); } =20 static int delete_child(struct connection *conn, @@ -1812,7 +1815,7 @@ static int do_set_perms(const void *ctx, struct conne= ction *conn, if (domain_nbentry_inc(conn, get_node_owner(node))) return ENOMEM; =20 - if (write_node(conn, node, false)) + if (write_node(conn, node, NODE_MODIFY, false)) return errno; =20 fire_watches(conn, ctx, name, node, false, &old_perms); @@ -2326,7 +2329,7 @@ static void manual_node(const char *name, const char = *child) if (child) node->childlen =3D strlen(child) + 1; =20 - if (write_node(NULL, node, false)) + if (write_node(NULL, node, NODE_CREATE, false)) barf_perror("Could not create initial node %s", name); talloc_free(node); } @@ -3474,12 +3477,15 @@ void read_state_node(const void *ctx, const void *s= tate) barf("allocation error restoring node"); =20 set_tdb_key(parentname, &key); - if (write_node_raw(NULL, &key, parent, true)) + if (write_node_raw(NULL, &key, parent, NODE_MODIFY, true)) barf("write parent error restoring node"); } =20 set_tdb_key(name, &key); - if (write_node_raw(NULL, &key, node, true)) + + /* The "/" node is already existing, so it can only be modified here. */ + if (write_node_raw(NULL, &key, node, + strcmp(name, "/") ? NODE_CREATE : NODE_MODIFY, true)) barf("write node error restoring node"); =20 if (domain_nbentry_inc(&conn, get_node_owner(node))) diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 005556de90..19a81adc49 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -238,8 +238,13 @@ static inline unsigned int get_node_owner(const struct= node *node) } =20 /* Write a node to the tdb data base. */ +enum write_node_mode { + NODE_CREATE, + NODE_MODIFY +}; + int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *no= de, - bool no_quota_check); + enum write_node_mode mode, bool no_quota_check); =20 /* Get a node from the tdb data base. */ struct node *read_node(struct connection *conn, const void *ctx, @@ -359,7 +364,8 @@ int remember_string(struct hashtable *hash, const char = *str); =20 void set_tdb_key(const char *name, TDB_DATA *key); int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data, - struct node_account_data *acc, bool no_quota_check); + struct node_account_data *acc, enum write_node_mode mode, + bool no_quota_check); int do_tdb_delete(struct connection *conn, TDB_DATA *key, struct node_account_data *acc); =20 diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index cc67af5b98..569c3bfbd0 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -523,7 +523,7 @@ static int domain_tree_remove_sub(const void *ctx, stru= ct connection *conn, node->perms.p[0].id =3D priv_domid; node->acc.memory =3D 0; domain_nbentry_inc(NULL, priv_domid); - if (write_node_raw(NULL, &key, node, true)) { + if (write_node_raw(NULL, &key, node, NODE_MODIFY, true)) { /* That's unfortunate. We only can try to continue. */ syslog(LOG_ERR, "error when moving orphaned node %s to dom0\n", diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 334f1609f1..baad9e472d 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -290,7 +290,8 @@ int access_node(struct connection *conn, struct node *n= ode, i->check_gen =3D true; if (node->generation !=3D NO_GENERATION) { set_tdb_key(i->trans_name, &local_key); - ret =3D write_node_raw(conn, &local_key, node, true); + ret =3D write_node_raw(conn, &local_key, node, + NODE_CREATE, true); if (ret) goto err; i->ta_node =3D true; @@ -401,12 +402,16 @@ static int finalize_transaction(struct connection *co= nn, set_tdb_key(i->trans_name, &ta_key); data =3D tdb_fetch(tdb_ctx, ta_key); if (data.dptr) { + enum write_node_mode mode; + trace_tdb("read %s size %zu\n", ta_key.dptr, ta_key.dsize + data.dsize); hdr =3D (void *)data.dptr; hdr->generation =3D ++generation; + mode =3D (i->generation =3D=3D NO_GENERATION) + ? NODE_CREATE : NODE_MODIFY; *is_corrupt |=3D do_tdb_write(conn, &key, &data, - NULL, true); + NULL, mode, true); talloc_free(data.dptr); if (do_tdb_delete(conn, &ta_key, NULL)) *is_corrupt =3D true; --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196605; cv=none; d=zohomail.com; s=zohoarc; b=m4BZ4qwcEb1I8Tjhzw5Vop4lYR6Rexa6jgVtun3T6PAMM8xmYnnbv4L5QLSeco0IZCh9JbFI8xqbuayh/x7Tgus68oX8rqNYcz59lhwj1L41YwyZ2GtZOTK4a0Y/j0z949TFwRVrnGudoXVK+pEkPaB2x3LuekK01KsaP5H5acg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196605; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+f/2hssl9+9pqd2iopuWIXDOp4+T2afqdOEfLUF2eXs=; b=Xo5z8QoXFBVUAzzSZftPB3qUWdRx/CHQz+3xVn0GDltqtQpr6pILHZ7tC5rkwseMrcdGOVTll4fYYuyVV/U9DFny0bMA+3T/fRiDoqM77xGyLQbyrNgoRo7+C2rBNBJfTPg0JV1dZQN0U1yPdLIfjf0F++9Zu2iCDkqxpOSOnl4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196605583591.3302101311399; Mon, 24 Jul 2023 04:03:25 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568731.888573 (Exim 4.92) (envelope-from ) id 1qNtLD-0000cz-Fr; Mon, 24 Jul 2023 11:03:03 +0000 Received: by outflank-mailman (output) from mailman id 568731.888573; Mon, 24 Jul 2023 11:03:03 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLD-0000cm-Bf; Mon, 24 Jul 2023 11:03:03 +0000 Received: by outflank-mailman (input) for mailman id 568731; Mon, 24 Jul 2023 11:03:02 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLC-0000KR-Dn for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:02 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id a7f7b8d4-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:03:01 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 7E64F20695; Mon, 24 Jul 2023 11:03:01 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 47ED013476; Mon, 24 Jul 2023 11:03:01 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id shAlEGVavmRCYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:01 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a7f7b8d4-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196581; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+f/2hssl9+9pqd2iopuWIXDOp4+T2afqdOEfLUF2eXs=; b=aZejtC3pt47Q/KokoffOLlIiVzv9E9jbRDtmAQpCNjbn1+pHDrGUjVKSZVSOpTv8CUXkf1 TsFDcBIhZ/bkaIyBM44hOASLTSdqWSLiw9BN+PKw6+2sa7MN7gaC3+ojczYCqBKjc4t7/L t/nFy8Q7mYpX2abMiB6Es6YImsmN+YQ= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 02/25] tools/xenstore: replace key in struct node with data base name Date: Mon, 24 Jul 2023 13:02:24 +0200 Message-Id: <20230724110247.10520-3-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196607917100003 Content-Type: text/plain; charset="utf-8" Instead of storing the TDB key in struct node, only store the name of the node used to access it in the data base. Associated with that change replace the key parameter of access_node() with the equivalent db_name. This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- tools/xenstore/xenstored_core.c | 19 +++++++++++++------ tools/xenstore/xenstored_core.h | 4 ++-- tools/xenstore/xenstored_transaction.c | 10 +++++----- tools/xenstore/xenstored_transaction.h | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index b7fdf16718..ba4798dd6e 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -821,18 +821,20 @@ int write_node_raw(struct connection *conn, TDB_DATA = *key, struct node *node, } =20 /* - * Write the node. If the node is written, caller can find the key used in - * node->key. This can later be used if the change needs to be reverted. + * Write the node. If the node is written, caller can find the DB name use= d in + * node->db_name. This can later be used if the change needs to be reverte= d. */ static int write_node(struct connection *conn, struct node *node, enum write_node_mode mode, bool no_quota_check) { int ret; + TDB_DATA key; =20 - if (access_node(conn, node, NODE_ACCESS_WRITE, &node->key)) + if (access_node(conn, node, NODE_ACCESS_WRITE, &node->db_name)) return errno; =20 - ret =3D write_node_raw(conn, &node->key, node, mode, no_quota_check); + set_tdb_key(node->db_name, &key); + ret =3D write_node_raw(conn, &key, node, mode, no_quota_check); if (ret && conn && conn->transaction) { /* * Reverting access_node() is hard, so just fail the @@ -1450,10 +1452,13 @@ nomem: =20 static void destroy_node_rm(struct connection *conn, struct node *node) { + TDB_DATA key; + if (streq(node->name, "/")) corrupt(NULL, "Destroying root node!"); =20 - do_tdb_delete(conn, &node->key, &node->acc); + set_tdb_key(node->db_name, &key); + do_tdb_delete(conn, &key, &node->acc); } =20 static int destroy_node(struct connection *conn, struct node *node) @@ -1643,10 +1648,11 @@ static int delnode_sub(const void *ctx, struct conn= ection *conn, const char *root =3D arg; bool watch_exact; int ret; + const char *db_name; TDB_DATA key; =20 /* Any error here will probably be repeated for all following calls. */ - ret =3D access_node(conn, node, NODE_ACCESS_DELETE, &key); + ret =3D access_node(conn, node, NODE_ACCESS_DELETE, &db_name); if (ret > 0) return WALK_TREE_SUCCESS_STOP; =20 @@ -1654,6 +1660,7 @@ static int delnode_sub(const void *ctx, struct connec= tion *conn, return WALK_TREE_ERROR_STOP; =20 /* In case of error stop the walk. */ + set_tdb_key(db_name, &key); if (!ret && do_tdb_delete(conn, &key, &node->acc)) return WALK_TREE_ERROR_STOP; =20 diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 19a81adc49..ec7a56a869 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -181,8 +181,8 @@ struct node_account_data { =20 struct node { const char *name; - /* Key used to update TDB */ - TDB_DATA key; + /* Name used to access data base. */ + const char *db_name; =20 /* Parent (optional) */ struct node *parent; diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index baad9e472d..020b70d954 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -227,7 +227,7 @@ void transaction_prepend(struct connection *conn, const= char *name, * to be accessed in the data base. */ int access_node(struct connection *conn, struct node *node, - enum node_access_type type, TDB_DATA *key) + enum node_access_type type, const char **db_name) { struct accessed_node *i =3D NULL; struct transaction *trans; @@ -243,8 +243,8 @@ int access_node(struct connection *conn, struct node *n= ode, =20 if (!conn || !conn->transaction) { /* They're changing the global database. */ - if (key) - set_tdb_key(node->name, key); + if (db_name) + *db_name =3D node->name; return 0; } =20 @@ -308,8 +308,8 @@ int access_node(struct connection *conn, struct node *n= ode, /* Nothing to delete. */ return -1; =20 - if (key) { - set_tdb_key(i->trans_name, key); + if (db_name) { + *db_name =3D i->trans_name; if (type =3D=3D NODE_ACCESS_WRITE) i->ta_node =3D true; if (type =3D=3D NODE_ACCESS_DELETE) diff --git a/tools/xenstore/xenstored_transaction.h b/tools/xenstore/xensto= red_transaction.h index 883145163f..f6a2e2f7f5 100644 --- a/tools/xenstore/xenstored_transaction.h +++ b/tools/xenstore/xenstored_transaction.h @@ -41,7 +41,7 @@ void ta_node_created(struct transaction *trans); =20 /* This node was accessed. */ int __must_check access_node(struct connection *conn, struct node *node, - enum node_access_type type, TDB_DATA *key); + enum node_access_type type, const char **db_n= ame); =20 /* Queue watches for a modified node. */ void queue_watches(struct connection *conn, const char *name, bool watch_e= xact); --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196612; cv=none; d=zohomail.com; s=zohoarc; b=fmpLh2ndzScrgIdMu4sDZHLMTk0W5xI/KoQX90omeibBCoykj3K7zLaVO2dedkPDUhdA3zC+sNiE5S6wQznIJO/Izd4QsRWBiO6bIu8mZIl0P16maYGGtsGYZHVczSmh3h8mXsglakeLVFBauM6NfnY9WsSgz+hGRHM0Am2JabI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196612; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=SQf628ebvedy/oSH7lsRps7bb3KYzWqEHfLoukckbyU=; b=Fvn10dwGjvfNMG4KJy4Ers09oeKuvgHHRxAk0DDUZKU2BXg3pPL1Meh4z/Izly0gt6+R7btSNh9NM0WLY3b18Oqm+OgDV/Gd/RZTwRY1q3uOUB7N8iGgI3GVKP+bgDsBf+bclLl3REVHScNN123CRaxTLknWszvfKFs5KSBs/AY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196612918399.46564457166164; Mon, 24 Jul 2023 04:03:32 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568732.888583 (Exim 4.92) (envelope-from ) id 1qNtLJ-0000xb-MF; Mon, 24 Jul 2023 11:03:09 +0000 Received: by outflank-mailman (output) from mailman id 568732.888583; Mon, 24 Jul 2023 11:03:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLJ-0000xR-Im; Mon, 24 Jul 2023 11:03:09 +0000 Received: by outflank-mailman (input) for mailman id 568732; Mon, 24 Jul 2023 11:03:07 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLH-0000KR-Q0 for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:07 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id ab4d4a66-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:03:07 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 1D14020695; Mon, 24 Jul 2023 11:03:07 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id DE56013476; Mon, 24 Jul 2023 11:03:06 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id QgPXNGpavmRVYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:06 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: ab4d4a66-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196587; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SQf628ebvedy/oSH7lsRps7bb3KYzWqEHfLoukckbyU=; b=N3OsHHCa1qqmtwZ6iexrpcTMEBbBIgUl8kaOV7Of4r+BskBO9y80MPhlh4r4PorEiGcZwG Tv/4+5GEc0Ow4VSfqQalbSNAk4l3zxDWBjALtzUe3FfJNxLQjVkDGHzuylzNusZf+aUm1A 9Tbzy/gQSFeMRVA2Ajb6OZHrZZ+6sDs= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 03/25] tools/xenstore: let transaction_prepend() return the name for access Date: Mon, 24 Jul 2023 13:02:25 +0200 Message-Id: <20230724110247.10520-4-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196615020100001 Content-Type: text/plain; charset="utf-8" Instead of setting the TDB key for accessing the node in the data base, let transaction_prepend() return the associated name instead. This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- tools/xenstore/xenstored_core.c | 4 +++- tools/xenstore/xenstored_transaction.c | 11 ++++------- tools/xenstore/xenstored_transaction.h | 3 +-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index ba4798dd6e..474bc57e73 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -695,6 +695,7 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, TDB_DATA key, data; struct xs_tdb_record_hdr *hdr; struct node *node; + const char *db_name; int err; =20 node =3D talloc(ctx, struct node); @@ -709,7 +710,8 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, return NULL; } =20 - transaction_prepend(conn, name, &key); + db_name =3D transaction_prepend(conn, name); + set_tdb_key(db_name, &key); =20 data =3D tdb_fetch(tdb_ctx, key); =20 diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 020b70d954..36567db753 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -196,20 +196,17 @@ static char *transaction_get_node_name(void *ctx, str= uct transaction *trans, * Prepend the transaction to name if node has been modified in the current * transaction. */ -void transaction_prepend(struct connection *conn, const char *name, - TDB_DATA *key) +const char *transaction_prepend(struct connection *conn, const char *name) { struct accessed_node *i; =20 if (conn && conn->transaction) { i =3D find_accessed_node(conn->transaction, name); - if (i) { - set_tdb_key(i->trans_name, key); - return; - } + if (i) + return i->trans_name; } =20 - set_tdb_key(name, key); + return name; } =20 /* diff --git a/tools/xenstore/xenstored_transaction.h b/tools/xenstore/xensto= red_transaction.h index f6a2e2f7f5..b196b1ab07 100644 --- a/tools/xenstore/xenstored_transaction.h +++ b/tools/xenstore/xenstored_transaction.h @@ -47,8 +47,7 @@ int __must_check access_node(struct connection *conn, str= uct node *node, void queue_watches(struct connection *conn, const char *name, bool watch_e= xact); =20 /* Prepend the transaction to name if appropriate. */ -void transaction_prepend(struct connection *conn, const char *name, - TDB_DATA *key); +const char *transaction_prepend(struct connection *conn, const char *name); =20 /* Mark the transaction as failed. This will prevent it to be committed. */ void fail_transaction(struct transaction *trans); --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196620; cv=none; d=zohomail.com; s=zohoarc; b=NDEjLoHNZW0JNmw4EdsiDhBFVcP4EzALyxl5GIRuPoYAWUBOcBltyJkRKaZErgm9nGG2jT2c4d7EVi2uwQ24EVWToBpspQ59evdvfyniKbMxHW0j20qcMotSsiA2D/TSUjeXQcGkocbY6YVT1Z0kP/RlAOdgzmnGyuic40SGpjE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196620; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=f6YolIcQ4D4Yg1XVjgJIVFTRGQJMKOPi0ubUNqhQlnI=; b=BWVfEpH5pfoI05VJq/FXMx5DHsMZSOuPhE8wuyc6HvMwTkH9Z6jQmuaAMKm6K5FY/STH687SviNOso5meN+ixIwPvS5SuIR5v5t002/6RnZyn2vt03rMYVMkLm6Iei/CRMckqGUA6Wh1/u5yLR/ZuFmtT9UCqa9THdpMUd4K64s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196620815220.29540230931173; Mon, 24 Jul 2023 04:03:40 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568734.888603 (Exim 4.92) (envelope-from ) id 1qNtLP-0001cp-8R; Mon, 24 Jul 2023 11:03:15 +0000 Received: by outflank-mailman (output) from mailman id 568734.888603; Mon, 24 Jul 2023 11:03:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLP-0001cg-4v; Mon, 24 Jul 2023 11:03:15 +0000 Received: by outflank-mailman (input) for mailman id 568734; Mon, 24 Jul 2023 11:03:14 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLO-0008WC-LG for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:14 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id aeade6d1-2a11-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 13:03:13 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id BE6F820697; Mon, 24 Jul 2023 11:03:12 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 7FC8113476; Mon, 24 Jul 2023 11:03:12 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id lHDbHXBavmRlYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:12 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: aeade6d1-2a11-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196592; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f6YolIcQ4D4Yg1XVjgJIVFTRGQJMKOPi0ubUNqhQlnI=; b=Bp0orEkYQdAkL6dfv/KVe4cMZURJxEm1tF80Nh3IXAWi7hjcsWYHHBULblf+UfvYCxgA9r 4XrfOiDXygaDfeMLaC+GU1J/ZBZieNihCd5CLuPJLkwhSLvk9dOukXQQrnXmXW0EBQ7pFK sYRBfULqvT8AjlLSKnX3r6Lhi4ErASo= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 04/25] tools/xenstore: rename do_tdb_delete() and change parameter type Date: Mon, 24 Jul 2023 13:02:26 +0200 Message-Id: <20230724110247.10520-5-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196622905100001 Content-Type: text/plain; charset="utf-8" Rename do_tdb_delete() to db_delete() and replace the key parameter with db_name specifying the name of the node in the data base. This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - remove unrelated change (Julien Grall) --- tools/xenstore/xenstored_core.c | 31 ++++++++++++-------------- tools/xenstore/xenstored_core.h | 5 +++-- tools/xenstore/xenstored_transaction.c | 16 +++++-------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 474bc57e73..aa3885682d 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -658,28 +658,31 @@ int do_tdb_write(struct connection *conn, TDB_DATA *k= ey, TDB_DATA *data, return 0; } =20 -int do_tdb_delete(struct connection *conn, TDB_DATA *key, - struct node_account_data *acc) +int db_delete(struct connection *conn, const char *name, + struct node_account_data *acc) { struct node_account_data tmp_acc; unsigned int domid; + TDB_DATA key; + + set_tdb_key(name, &key); =20 if (!acc) { acc =3D &tmp_acc; acc->memory =3D -1; } =20 - get_acc_data(key, acc); + get_acc_data(&key, acc); =20 - if (tdb_delete(tdb_ctx, *key)) { + if (tdb_delete(tdb_ctx, key)) { errno =3D EIO; return errno; } - trace_tdb("delete %s\n", key->dptr); + trace_tdb("delete %s\n", name); =20 if (acc->memory) { - domid =3D get_acc_domid(conn, key, acc->domid); - domain_memory_add_nochk(conn, domid, -acc->memory - key->dsize); + domid =3D get_acc_domid(conn, &key, acc->domid); + domain_memory_add_nochk(conn, domid, -acc->memory - key.dsize); } =20 return 0; @@ -1454,13 +1457,10 @@ nomem: =20 static void destroy_node_rm(struct connection *conn, struct node *node) { - TDB_DATA key; - if (streq(node->name, "/")) corrupt(NULL, "Destroying root node!"); =20 - set_tdb_key(node->db_name, &key); - do_tdb_delete(conn, &key, &node->acc); + db_delete(conn, node->db_name, &node->acc); } =20 static int destroy_node(struct connection *conn, struct node *node) @@ -1651,7 +1651,6 @@ static int delnode_sub(const void *ctx, struct connec= tion *conn, bool watch_exact; int ret; const char *db_name; - TDB_DATA key; =20 /* Any error here will probably be repeated for all following calls. */ ret =3D access_node(conn, node, NODE_ACCESS_DELETE, &db_name); @@ -1662,8 +1661,7 @@ static int delnode_sub(const void *ctx, struct connec= tion *conn, return WALK_TREE_ERROR_STOP; =20 /* In case of error stop the walk. */ - set_tdb_key(db_name, &key); - if (!ret && do_tdb_delete(conn, &key, &node->acc)) + if (!ret && db_delete(conn, db_name, &node->acc)) return WALK_TREE_ERROR_STOP; =20 /* @@ -2489,9 +2487,8 @@ static int clean_store_(TDB_CONTEXT *tdb, TDB_DATA ke= y, TDB_DATA val, } if (!hashtable_search(reachable, name)) { log("clean_store: '%s' is orphaned!", name); - if (recovery) { - do_tdb_delete(NULL, &key, NULL); - } + if (recovery) + db_delete(NULL, name, NULL); } =20 talloc_free(name); diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index ec7a56a869..1aae324c77 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -362,12 +362,13 @@ extern xengnttab_handle **xgt_handle; =20 int remember_string(struct hashtable *hash, const char *str); =20 +/* Data base access functions. */ void set_tdb_key(const char *name, TDB_DATA *key); int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data, struct node_account_data *acc, enum write_node_mode mode, bool no_quota_check); -int do_tdb_delete(struct connection *conn, TDB_DATA *key, - struct node_account_data *acc); +int db_delete(struct connection *conn, const char *name, + struct node_account_data *acc); =20 void conn_free_buffered_data(struct connection *conn); =20 diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 36567db753..4a243369b0 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -384,8 +384,7 @@ static int finalize_transaction(struct connection *conn, /* Entries for unmodified nodes can be removed early. */ if (!i->modified) { if (i->ta_node) { - set_tdb_key(i->trans_name, &ta_key); - if (do_tdb_delete(conn, &ta_key, NULL)) + if (db_delete(conn, i->trans_name, NULL)) return EIO; } list_del(&i->list); @@ -394,7 +393,6 @@ static int finalize_transaction(struct connection *conn, } =20 while ((i =3D list_top(&trans->accessed, struct accessed_node, list))) { - set_tdb_key(i->node, &key); if (i->ta_node) { set_tdb_key(i->trans_name, &ta_key); data =3D tdb_fetch(tdb_ctx, ta_key); @@ -407,10 +405,11 @@ static int finalize_transaction(struct connection *co= nn, hdr->generation =3D ++generation; mode =3D (i->generation =3D=3D NO_GENERATION) ? NODE_CREATE : NODE_MODIFY; + set_tdb_key(i->node, &key); *is_corrupt |=3D do_tdb_write(conn, &key, &data, NULL, mode, true); talloc_free(data.dptr); - if (do_tdb_delete(conn, &ta_key, NULL)) + if (db_delete(conn, i->trans_name, NULL)) *is_corrupt =3D true; } else { *is_corrupt =3D true; @@ -423,7 +422,7 @@ static int finalize_transaction(struct connection *conn, */ *is_corrupt |=3D (i->generation =3D=3D NO_GENERATION) ? false - : do_tdb_delete(conn, &key, NULL); + : db_delete(conn, i->node, NULL); } if (i->fire_watch) fire_watches(conn, trans, i->node, NULL, i->watch_exact, @@ -440,15 +439,12 @@ static int destroy_transaction(void *_transaction) { struct transaction *trans =3D _transaction; struct accessed_node *i; - TDB_DATA key; =20 wrl_ntransactions--; trace_destroy(trans, "transaction"); while ((i =3D list_top(&trans->accessed, struct accessed_node, list))) { - if (i->ta_node) { - set_tdb_key(i->trans_name, &key); - do_tdb_delete(trans->conn, &key, NULL); - } + if (i->ta_node) + db_delete(trans->conn, i->trans_name, NULL); list_del(&i->list); talloc_free(i); } --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196622; cv=none; d=zohomail.com; s=zohoarc; b=IDIDNLIMFaI7UZZ1zlKe9VycWVgJiVBfBhiAbarzvzZqvMdiAosl8pqvCeVawc7SjDvPOFueW87/30Gy7HOBj81kRiESQa7/LCYBANJ1dbOvIf8AJE4S7Y8MEzfQMzPC5N/DlUt3/9maEbPrOmGoDewFULIFCWTLidExvk04mxo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196622; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=20QNajxZk743EX0aY+cQsL9LD9drTSx4TUjevmwUJ6E=; b=ZjVWjOF6/f243sx7VmPcwLFcHDrKJNWeAhMeT84HmcmbwxyGUo6WmB3cv8e0axgtv4QoX0K9XpCreTF2fgTXMpy8sMEvTpUdgI2sZXQ2gGUKAQKxve3K96d2ffnTbw/rdM/c5MwIbgo4k2aB167U9ekIxz72zHDF6sFja6rhJc0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196622473200.497433720304; Mon, 24 Jul 2023 04:03:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568736.888613 (Exim 4.92) (envelope-from ) id 1qNtLU-00026G-On; Mon, 24 Jul 2023 11:03:20 +0000 Received: by outflank-mailman (output) from mailman id 568736.888613; Mon, 24 Jul 2023 11:03:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLU-000269-Lr; Mon, 24 Jul 2023 11:03:20 +0000 Received: by outflank-mailman (input) for mailman id 568736; Mon, 24 Jul 2023 11:03:19 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLT-0000KR-D9 for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:19 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id b1fc07cb-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:03:18 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 51CCB20694; Mon, 24 Jul 2023 11:03:18 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1EE3013476; Mon, 24 Jul 2023 11:03:18 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id RfohBnZavmRzYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:18 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b1fc07cb-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196598; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=20QNajxZk743EX0aY+cQsL9LD9drTSx4TUjevmwUJ6E=; b=rmXs4+NUDGQWf0svXnxtB3EadaPS11KNyaZosp6kxxLj/esZfXbqvx2jpHUx4XcrqTKSPV ji8hfHWzDijUmpItQ7xcB7XwqlhQPTkxAMZ1XtTIc9cVlV0zZqXPDWrlUNt+dMIe67CSHC HA7RiHrZin4P7ta/pMewAMLrNaB/HtY= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 05/25] tools/xenstore: rename do_tdb_write() and change parameter type Date: Mon, 24 Jul 2023 13:02:27 +0200 Message-Id: <20230724110247.10520-6-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196623697100003 Content-Type: text/plain; charset="utf-8" Rename do_tdb_write() to db_write() and replace the key parameter with db_name specifying the name of the node in the data base, and the data parameter with a data pointer and a length. Do the same key parameter type change for write_node_raw(), too. This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - keep ta_key in finalize_transaction() (Julien Grall) --- tools/xenstore/xenstored_core.c | 70 +++++++++++++------------- tools/xenstore/xenstored_core.h | 11 ++-- tools/xenstore/xenstored_domain.c | 4 +- tools/xenstore/xenstored_transaction.c | 10 ++-- 4 files changed, 46 insertions(+), 49 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index aa3885682d..2aa3d37c18 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -600,23 +600,27 @@ static unsigned int get_acc_domid(struct connection *= conn, TDB_DATA *key, ? domid : conn->id; } =20 -int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data, - struct node_account_data *acc, enum write_node_mode mode, - bool no_quota_check) +int db_write(struct connection *conn, const char *db_name, void *data, + size_t size, struct node_account_data *acc, + enum write_node_mode mode, bool no_quota_check) { - struct xs_tdb_record_hdr *hdr =3D (void *)data->dptr; + struct xs_tdb_record_hdr *hdr =3D data; struct node_account_data old_acc =3D {}; unsigned int old_domid, new_domid; int ret; + TDB_DATA key, dat; =20 + set_tdb_key(db_name, &key); + dat.dptr =3D data; + dat.dsize =3D size; if (!acc) old_acc.memory =3D -1; else old_acc =3D *acc; =20 - get_acc_data(key, &old_acc); - old_domid =3D get_acc_domid(conn, key, old_acc.domid); - new_domid =3D get_acc_domid(conn, key, hdr->perms[0].id); + get_acc_data(&key, &old_acc); + old_domid =3D get_acc_domid(conn, &key, old_acc.domid); + new_domid =3D get_acc_domid(conn, &key, hdr->perms[0].id); =20 /* * Don't check for ENOENT, as we want to be able to switch orphaned @@ -624,35 +628,34 @@ int do_tdb_write(struct connection *conn, TDB_DATA *k= ey, TDB_DATA *data, */ if (old_acc.memory) domain_memory_add_nochk(conn, old_domid, - -old_acc.memory - key->dsize); - ret =3D domain_memory_add(conn, new_domid, - data->dsize + key->dsize, no_quota_check); + -old_acc.memory - key.dsize); + ret =3D domain_memory_add(conn, new_domid, size + key.dsize, + no_quota_check); if (ret) { /* Error path, so no quota check. */ if (old_acc.memory) domain_memory_add_nochk(conn, old_domid, - old_acc.memory + key->dsize); + old_acc.memory + key.dsize); return ret; } =20 /* TDB should set errno, but doesn't even set ecode AFAICT. */ - if (tdb_store(tdb_ctx, *key, *data, + if (tdb_store(tdb_ctx, key, dat, (mode =3D=3D NODE_CREATE) ? TDB_INSERT : TDB_MODIFY) !=3D 0) { - domain_memory_add_nochk(conn, new_domid, - -data->dsize - key->dsize); + domain_memory_add_nochk(conn, new_domid, -size - key.dsize); /* Error path, so no quota check. */ if (old_acc.memory) domain_memory_add_nochk(conn, old_domid, - old_acc.memory + key->dsize); + old_acc.memory + key.dsize); errno =3D EIO; return errno; } - trace_tdb("store %s size %zu\n", key->dptr, data->dsize + key->dsize); + trace_tdb("store %s size %zu\n", db_name, size + key.dsize); =20 if (acc) { /* Don't use new_domid, as it might be a transaction node. */ acc->domid =3D hdr->perms[0].id; - acc->memory =3D data->dsize; + acc->memory =3D size; } =20 return 0; @@ -780,33 +783,35 @@ static bool read_node_can_propagate_errno(void) return errno =3D=3D ENOMEM || errno =3D=3D ENOSPC; } =20 -int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *no= de, - enum write_node_mode mode, bool no_quota_check) +int write_node_raw(struct connection *conn, const char *db_name, + struct node *node, enum write_node_mode mode, + bool no_quota_check) { - TDB_DATA data; + void *data; + size_t size; void *p; struct xs_tdb_record_hdr *hdr; =20 if (domain_adjust_node_perms(node)) return errno; =20 - data.dsize =3D sizeof(*hdr) + size =3D sizeof(*hdr) + node->perms.num * sizeof(node->perms.p[0]) + node->datalen + node->childlen; =20 /* Call domain_max_chk() in any case in order to record max values. */ - if (domain_max_chk(conn, ACC_NODESZ, data.dsize) && !no_quota_check) { + if (domain_max_chk(conn, ACC_NODESZ, size) && !no_quota_check) { errno =3D ENOSPC; return errno; } =20 - data.dptr =3D talloc_size(node, data.dsize); - if (!data.dptr) { + data =3D talloc_size(node, size); + if (!data) { errno =3D ENOMEM; return errno; } =20 - hdr =3D (void *)data.dptr; + hdr =3D data; hdr->generation =3D node->generation; hdr->num_perms =3D node->perms.num; hdr->datalen =3D node->datalen; @@ -819,7 +824,8 @@ int write_node_raw(struct connection *conn, TDB_DATA *k= ey, struct node *node, p +=3D node->datalen; memcpy(p, node->children, node->childlen); =20 - if (do_tdb_write(conn, key, &data, &node->acc, mode, no_quota_check)) + if (db_write(conn, db_name, data, size, &node->acc, mode, + no_quota_check)) return EIO; =20 return 0; @@ -833,13 +839,11 @@ static int write_node(struct connection *conn, struct= node *node, enum write_node_mode mode, bool no_quota_check) { int ret; - TDB_DATA key; =20 if (access_node(conn, node, NODE_ACCESS_WRITE, &node->db_name)) return errno; =20 - set_tdb_key(node->db_name, &key); - ret =3D write_node_raw(conn, &key, node, mode, no_quota_check); + ret =3D write_node_raw(conn, node->db_name, node, mode, no_quota_check); if (ret && conn && conn->transaction) { /* * Reverting access_node() is hard, so just fail the @@ -3429,7 +3433,6 @@ void read_state_node(const void *ctx, const void *sta= te) { const struct xs_state_node *sn =3D state; struct node *node, *parent; - TDB_DATA key; char *name, *parentname; unsigned int i; struct connection conn =3D { .id =3D priv_domid }; @@ -3482,15 +3485,12 @@ void read_state_node(const void *ctx, const void *s= tate) if (add_child(node, parent, name)) barf("allocation error restoring node"); =20 - set_tdb_key(parentname, &key); - if (write_node_raw(NULL, &key, parent, NODE_MODIFY, true)) + if (write_node_raw(NULL, parentname, parent, NODE_MODIFY, true)) barf("write parent error restoring node"); } =20 - set_tdb_key(name, &key); - /* The "/" node is already existing, so it can only be modified here. */ - if (write_node_raw(NULL, &key, node, + if (write_node_raw(NULL, name, node, strcmp(name, "/") ? NODE_CREATE : NODE_MODIFY, true)) barf("write node error restoring node"); =20 diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 1aae324c77..875ee5192b 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -243,8 +243,9 @@ enum write_node_mode { NODE_MODIFY }; =20 -int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *no= de, - enum write_node_mode mode, bool no_quota_check); +int write_node_raw(struct connection *conn, const char *db_name, + struct node *node, enum write_node_mode mode, + bool no_quota_check); =20 /* Get a node from the tdb data base. */ struct node *read_node(struct connection *conn, const void *ctx, @@ -364,9 +365,9 @@ int remember_string(struct hashtable *hash, const char = *str); =20 /* Data base access functions. */ void set_tdb_key(const char *name, TDB_DATA *key); -int do_tdb_write(struct connection *conn, TDB_DATA *key, TDB_DATA *data, - struct node_account_data *acc, enum write_node_mode mode, - bool no_quota_check); +int db_write(struct connection *conn, const char *db_name, void *data, + size_t size, struct node_account_data *acc, + enum write_node_mode mode, bool no_quota_check); int db_delete(struct connection *conn, const char *name, struct node_account_data *acc); =20 diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index 569c3bfbd0..10d2280f84 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -511,19 +511,17 @@ static int domain_tree_remove_sub(const void *ctx, st= ruct connection *conn, struct node *node, void *arg) { struct domain *domain =3D arg; - TDB_DATA key; int ret =3D WALK_TREE_OK; =20 if (node->perms.p[0].id !=3D domain->domid) return WALK_TREE_OK; =20 if (keep_orphans) { - set_tdb_key(node->name, &key); domain_nbentry_dec(NULL, domain->domid); node->perms.p[0].id =3D priv_domid; node->acc.memory =3D 0; domain_nbentry_inc(NULL, priv_domid); - if (write_node_raw(NULL, &key, node, NODE_MODIFY, true)) { + if (write_node_raw(NULL, node->name, node, NODE_MODIFY, true)) { /* That's unfortunate. We only can try to continue. */ syslog(LOG_ERR, "error when moving orphaned node %s to dom0\n", diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 4a243369b0..1961aef0d7 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -228,7 +228,6 @@ int access_node(struct connection *conn, struct node *n= ode, { struct accessed_node *i =3D NULL; struct transaction *trans; - TDB_DATA local_key; int ret; bool introduce =3D false; =20 @@ -286,8 +285,7 @@ int access_node(struct connection *conn, struct node *n= ode, i->generation =3D node->generation; i->check_gen =3D true; if (node->generation !=3D NO_GENERATION) { - set_tdb_key(i->trans_name, &local_key); - ret =3D write_node_raw(conn, &local_key, node, + ret =3D write_node_raw(conn, i->trans_name, node, NODE_CREATE, true); if (ret) goto err; @@ -405,9 +403,9 @@ static int finalize_transaction(struct connection *conn, hdr->generation =3D ++generation; mode =3D (i->generation =3D=3D NO_GENERATION) ? NODE_CREATE : NODE_MODIFY; - set_tdb_key(i->node, &key); - *is_corrupt |=3D do_tdb_write(conn, &key, &data, - NULL, mode, true); + *is_corrupt |=3D db_write(conn, i->node, + data.dptr, data.dsize, + NULL, mode, true); talloc_free(data.dptr); if (db_delete(conn, i->trans_name, NULL)) *is_corrupt =3D true; --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196629; cv=none; d=zohomail.com; s=zohoarc; b=m+P6V/C6+9XG+u8X9Mu3A3JbvMtTqyMgnIzcnah9Kt7PKYlgdGppuEX56hBk6tVE8gw1C8yuhemPTC7WSA7ZulVBx9bMUhon/soF4hk1n8e6gPBFFQGTmP9ykVGsQ6LjTzPkNedsWTpDyD3DWtOuA8gYd9VNxOKQr3jh0/xOQKc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196629; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6lXZ5IdZRWlP0NjLbCuiGLRNG5pWjkshr1ucn8awGQY=; b=ArqGWW9mu/5yEKeU4soBQ0O3TcryZMTAkHKPbwHm2zPVLYvnxocmdYDUIdryrZGi/pp1XrED2MrJzVcEVEkMd2NuACxGR4pPIvvssM2nSDt1OI6vyMUPwFV6hskeB99xFOtJEECeGRotnTZhNTIn5gSX4bpHjKZZZ1zUAPIOQ8o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 169019662969427.31977375990948; Mon, 24 Jul 2023 04:03:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568740.888623 (Exim 4.92) (envelope-from ) id 1qNtLa-0002hv-27; Mon, 24 Jul 2023 11:03:26 +0000 Received: by outflank-mailman (output) from mailman id 568740.888623; Mon, 24 Jul 2023 11:03:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLZ-0002hA-VX; Mon, 24 Jul 2023 11:03:25 +0000 Received: by outflank-mailman (input) for mailman id 568740; Mon, 24 Jul 2023 11:03:24 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLY-0000KR-NF for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:24 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id b5561376-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:03:24 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id EADFC20694; Mon, 24 Jul 2023 11:03:23 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B4B0113476; Mon, 24 Jul 2023 11:03:23 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id btGyKntavmSAYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:23 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b5561376-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196603; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6lXZ5IdZRWlP0NjLbCuiGLRNG5pWjkshr1ucn8awGQY=; b=h8EyHs4MVggwfz2Lvg1G2aglMdM6p8SqTFBdAgrJZbUykaC49FaSfBDf/eeUJXmZ01dxgu 3L1WQ/MdRy9GI5rJ6cU9vP8ZOEd4MeWz6H7COg6Ohp9Jfo4OqSYJfQ/ky8gLbeHG2wBvsg Drf11jy7FEX1DRdsMkVyLcfgj5fpzAA= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 06/25] tools/xenstore: switch get_acc_data() to use name instead of key Date: Mon, 24 Jul 2023 13:02:28 +0200 Message-Id: <20230724110247.10520-7-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196630282100001 Content-Type: text/plain; charset="utf-8" Eliminate further TDB_DATA usage by switching get_acc_data() and get_acc_domid() from a TDB key to the name of the node in the data base as a parameter. This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- tools/xenstore/xenstored_core.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 2aa3d37c18..dab09b5236 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -566,19 +566,20 @@ void set_tdb_key(const char *name, TDB_DATA *key) key->dsize =3D strlen(name); } =20 -static void get_acc_data(TDB_DATA *key, struct node_account_data *acc) +static void get_acc_data(const char *name, struct node_account_data *acc) { - TDB_DATA old_data; + TDB_DATA key, old_data; struct xs_tdb_record_hdr *hdr; =20 if (acc->memory < 0) { - old_data =3D tdb_fetch(tdb_ctx, *key); + set_tdb_key(name, &key); + old_data =3D tdb_fetch(tdb_ctx, key); /* No check for error, as the node might not exist. */ if (old_data.dptr =3D=3D NULL) { acc->memory =3D 0; } else { - trace_tdb("read %s size %zu\n", key->dptr, - old_data.dsize + key->dsize); + trace_tdb("read %s size %zu\n", name, + old_data.dsize + key.dsize); hdr =3D (void *)old_data.dptr; acc->memory =3D old_data.dsize; acc->domid =3D hdr->perms[0].id; @@ -593,11 +594,10 @@ static void get_acc_data(TDB_DATA *key, struct node_a= ccount_data *acc) * count prepended (e.g. 123/local/domain/...). So testing for the node's * key not to start with "/" or "@" is sufficient. */ -static unsigned int get_acc_domid(struct connection *conn, TDB_DATA *key, +static unsigned int get_acc_domid(struct connection *conn, const char *nam= e, unsigned int domid) { - return (!conn || key->dptr[0] =3D=3D '/' || key->dptr[0] =3D=3D '@') - ? domid : conn->id; + return (!conn || name[0] =3D=3D '/' || name[0] =3D=3D '@') ? domid : conn= ->id; } =20 int db_write(struct connection *conn, const char *db_name, void *data, @@ -618,9 +618,9 @@ int db_write(struct connection *conn, const char *db_na= me, void *data, else old_acc =3D *acc; =20 - get_acc_data(&key, &old_acc); - old_domid =3D get_acc_domid(conn, &key, old_acc.domid); - new_domid =3D get_acc_domid(conn, &key, hdr->perms[0].id); + get_acc_data(db_name, &old_acc); + old_domid =3D get_acc_domid(conn, db_name, old_acc.domid); + new_domid =3D get_acc_domid(conn, db_name, hdr->perms[0].id); =20 /* * Don't check for ENOENT, as we want to be able to switch orphaned @@ -675,7 +675,7 @@ int db_delete(struct connection *conn, const char *name, acc->memory =3D -1; } =20 - get_acc_data(&key, acc); + get_acc_data(name, acc); =20 if (tdb_delete(tdb_ctx, key)) { errno =3D EIO; @@ -684,7 +684,7 @@ int db_delete(struct connection *conn, const char *name, trace_tdb("delete %s\n", name); =20 if (acc->memory) { - domid =3D get_acc_domid(conn, &key, acc->domid); + domid =3D get_acc_domid(conn, name, acc->domid); domain_memory_add_nochk(conn, domid, -acc->memory - key.dsize); } =20 --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196631; cv=none; d=zohomail.com; s=zohoarc; b=a19tulLH+NnWcH19V3oeRrH7zt0fMdXfk6WagO8AdCMw7OBZXeKb9r3f+3JoOnVB8SqKEJQpTDC5xtjVh/HruXQkBmhKwZAmSPOE0M5KRDCGK1OMpsMs3t313kneF0yzSrgozdmzcMtPXbU5N1eNVp9g+U92v/kR+uoIsOTwOqQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196631; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=r7Ym4LUS2VI66FFn3rEX1b9dLO2vvX5eqYIpBnAurpE=; b=gedfpNQ7Vkr9k/pwSPDbfFtJ2t8F5IjbnZotC0eYfjcLxg3gn+Cd2nKA9z3UxQJKQBSaylR7yqozniIYpYR8+mD5vibCTSj8Vt2DL4KUY81XOpBZl0MmAVoF9ibcIBpyguJxvugvg8ZlEEp9Ml5pgu36rgJauJIQy5i5KIu06+4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196631955986.4243788700136; Mon, 24 Jul 2023 04:03:51 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568744.888633 (Exim 4.92) (envelope-from ) id 1qNtLj-0003Wq-Bc; Mon, 24 Jul 2023 11:03:35 +0000 Received: by outflank-mailman (output) from mailman id 568744.888633; Mon, 24 Jul 2023 11:03:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLj-0003Wh-8C; Mon, 24 Jul 2023 11:03:35 +0000 Received: by outflank-mailman (input) for mailman id 568744; Mon, 24 Jul 2023 11:03:33 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLh-0008WC-HP for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:33 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id b8bb4c32-2a11-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 13:03:29 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 9CCD620695; Mon, 24 Jul 2023 11:03:29 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 5F91F13476; Mon, 24 Jul 2023 11:03:29 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id cxT0FYFavmSPYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:29 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b8bb4c32-2a11-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196609; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=r7Ym4LUS2VI66FFn3rEX1b9dLO2vvX5eqYIpBnAurpE=; b=YoqMEZGwt6iKt5rvlm+FBPXB3LTOxVbCCNH3l1dvNBjNHt96loBzBkPpqlhVf6FuWAHkoz 9P1BdvIcF+QC+2f5kB4sjVgtE1OK9QS6idc0UYBLL4+FdUN52ziSsxLav2OjMXgRzpHYuP jhYuATWr79WO0vjMwexd043/TYUG8T0= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 07/25] tools/xenstore: add wrapper for tdb_fetch() Date: Mon, 24 Jul 2023 13:02:29 +0200 Message-Id: <20230724110247.10520-8-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196632990100001 Content-Type: text/plain; charset="utf-8" Add a wrapper function for tdb_fetch taking the name of the node in the data base as a parameter. Let it return a data pointer and the length of the data via a length pointer provided as additional parameter. Move logging of the TDB access from the callers into the wrapper. This enables to make set_tdb_key() and tdb_ctx static. This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - always set *size in db_fetch() (Julien Grall) - change db_fetch() return type (Julien Grall) - move logging --- tools/xenstore/xenstored_core.c | 55 +++++++++++++++----------- tools/xenstore/xenstored_core.h | 3 +- tools/xenstore/xenstored_transaction.c | 31 ++++++--------- 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index dab09b5236..a12ede147c 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -85,7 +85,7 @@ bool keep_orphans =3D false; static int reopen_log_pipe[2]; static int reopen_log_pipe0_pollfd_idx =3D -1; char *tracefile =3D NULL; -TDB_CONTEXT *tdb_ctx =3D NULL; +static TDB_CONTEXT *tdb_ctx =3D NULL; unsigned int trace_flags =3D TRACE_OBJ | TRACE_IO; =20 static const char *sockmsg_string(enum xsd_sockmsg_type type); @@ -556,7 +556,7 @@ static void initialize_fds(int *p_sock_pollfd_idx, int = *ptimeout) } } =20 -void set_tdb_key(const char *name, TDB_DATA *key) +static void set_tdb_key(const char *name, TDB_DATA *key) { /* * Dropping const is fine here, as the key will never be modified @@ -566,25 +566,39 @@ void set_tdb_key(const char *name, TDB_DATA *key) key->dsize =3D strlen(name); } =20 +struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size) +{ + TDB_DATA key, data; + + set_tdb_key(db_name, &key); + data =3D tdb_fetch(tdb_ctx, key); + if (!data.dptr) { + errno =3D (tdb_error(tdb_ctx) =3D=3D TDB_ERR_NOEXIST) ? ENOENT : EIO; + *size =3D 0; + } else { + *size =3D data.dsize; + trace_tdb("read %s size %zu\n", db_name, + *size + strlen(db_name)); + } + + return (struct xs_tdb_record_hdr *)data.dptr; +} + static void get_acc_data(const char *name, struct node_account_data *acc) { - TDB_DATA key, old_data; + size_t size; struct xs_tdb_record_hdr *hdr; =20 if (acc->memory < 0) { - set_tdb_key(name, &key); - old_data =3D tdb_fetch(tdb_ctx, key); + hdr =3D db_fetch(name, &size); /* No check for error, as the node might not exist. */ - if (old_data.dptr =3D=3D NULL) { + if (hdr =3D=3D NULL) { acc->memory =3D 0; } else { - trace_tdb("read %s size %zu\n", name, - old_data.dsize + key.dsize); - hdr =3D (void *)old_data.dptr; - acc->memory =3D old_data.dsize; + acc->memory =3D size; acc->domid =3D hdr->perms[0].id; } - talloc_free(old_data.dptr); + talloc_free(hdr); } } =20 @@ -698,7 +712,7 @@ int db_delete(struct connection *conn, const char *name, struct node *read_node(struct connection *conn, const void *ctx, const char *name) { - TDB_DATA key, data; + size_t size; struct xs_tdb_record_hdr *hdr; struct node *node; const char *db_name; @@ -717,29 +731,24 @@ struct node *read_node(struct connection *conn, const= void *ctx, } =20 db_name =3D transaction_prepend(conn, name); - set_tdb_key(db_name, &key); + hdr =3D db_fetch(db_name, &size); =20 - data =3D tdb_fetch(tdb_ctx, key); - - if (data.dptr =3D=3D NULL) { - if (tdb_error(tdb_ctx) =3D=3D TDB_ERR_NOEXIST) { + if (hdr =3D=3D NULL) { + if (errno =3D=3D ENOENT) { node->generation =3D NO_GENERATION; err =3D access_node(conn, node, NODE_ACCESS_READ, NULL); errno =3D err ? : ENOENT; } else { - log("TDB error on read: %s", tdb_errorstr(tdb_ctx)); + log("DB error on read: %s", strerror(errno)); errno =3D EIO; } goto error; } =20 - trace_tdb("read %s size %zu\n", key.dptr, data.dsize + key.dsize); - node->parent =3D NULL; - talloc_steal(node, data.dptr); + talloc_steal(node, hdr); =20 /* Datalen, childlen, number of permissions */ - hdr =3D (void *)data.dptr; node->generation =3D hdr->generation; node->perms.num =3D hdr->num_perms; node->datalen =3D hdr->datalen; @@ -748,7 +757,7 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, /* Permissions are struct xs_permissions. */ node->perms.p =3D hdr->perms; node->acc.domid =3D get_node_owner(node); - node->acc.memory =3D data.dsize; + node->acc.memory =3D size; if (domain_adjust_node_perms(node)) goto error; =20 diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 875ee5192b..f5aa8d51a0 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -315,7 +315,6 @@ do { \ trace("tdb: " __VA_ARGS__); \ } while (0) =20 -extern TDB_CONTEXT *tdb_ctx; extern int dom0_domid; extern int dom0_event; extern int priv_domid; @@ -364,7 +363,7 @@ extern xengnttab_handle **xgt_handle; int remember_string(struct hashtable *hash, const char *str); =20 /* Data base access functions. */ -void set_tdb_key(const char *name, TDB_DATA *key); +struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size); int db_write(struct connection *conn, const char *db_name, 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/xensto= red_transaction.c index 1961aef0d7..1981d1d55d 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -356,25 +356,21 @@ static int finalize_transaction(struct connection *co= nn, struct transaction *trans, bool *is_corrupt) { struct accessed_node *i, *n; - TDB_DATA key, ta_key, data; + size_t size; struct xs_tdb_record_hdr *hdr; uint64_t gen; =20 list_for_each_entry_safe(i, n, &trans->accessed, list) { if (i->check_gen) { - set_tdb_key(i->node, &key); - data =3D tdb_fetch(tdb_ctx, key); - hdr =3D (void *)data.dptr; - if (!data.dptr) { - if (tdb_error(tdb_ctx) !=3D TDB_ERR_NOEXIST) - return EIO; + hdr =3D db_fetch(i->node, &size); + if (!hdr) { + if (errno !=3D ENOENT) + return errno; gen =3D NO_GENERATION; } else { - trace_tdb("read %s size %zu\n", key.dptr, - key.dsize + data.dsize); gen =3D hdr->generation; } - talloc_free(data.dptr); + talloc_free(hdr); if (i->generation !=3D gen) return EAGAIN; } @@ -392,21 +388,16 @@ static int finalize_transaction(struct connection *co= nn, =20 while ((i =3D list_top(&trans->accessed, struct accessed_node, list))) { if (i->ta_node) { - set_tdb_key(i->trans_name, &ta_key); - data =3D tdb_fetch(tdb_ctx, ta_key); - if (data.dptr) { + hdr =3D db_fetch(i->trans_name, &size); + if (hdr) { enum write_node_mode mode; =20 - trace_tdb("read %s size %zu\n", ta_key.dptr, - ta_key.dsize + data.dsize); - hdr =3D (void *)data.dptr; hdr->generation =3D ++generation; mode =3D (i->generation =3D=3D NO_GENERATION) ? NODE_CREATE : NODE_MODIFY; - *is_corrupt |=3D db_write(conn, i->node, - data.dptr, data.dsize, - NULL, mode, true); - talloc_free(data.dptr); + *is_corrupt |=3D db_write(conn, i->node, hdr, + size, NULL, mode, true); + talloc_free(hdr); if (db_delete(conn, i->trans_name, NULL)) *is_corrupt =3D true; } else { --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196895; cv=none; d=zohomail.com; s=zohoarc; b=CI5YT0VJ+RneMQtIW1ByTdkm9ZjXfpprJn9FklCFmTUx0DNNK+kAdafk9o9SlZ18ajI/UsZfjSVnvNYC7hz+Gena0fyGny68kw8I9yjLXUF1NDZRVmsSczMRr27nNwhEFZSztj690vNOX57hynzVbVIUC01QH8oy+tD14wcU1oo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196895; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=siTlKvWMR/RT4S0FnCgR+ZpOvEYZZwpHnARxR3tmw/4=; b=RdQ2ZxADNPro9OMSQpBYatK4x9m7zGA4afrm2HEC3Ku9+OUBfNkXajH+6kVJ2QKVI9fh/Nboslo/XmJ+B0BbYfnFYUXSNME99fFcu4CMzGy8yBAww6V4Bda3oKT9yMEWzZGz/YvXtSTjj/HB17UExP41Qri83Xr145Mgf77uWYI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196895828400.19815578592613; Mon, 24 Jul 2023 04:08:15 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568760.888712 (Exim 4.92) (envelope-from ) id 1qNtQ1-0000LQ-RN; Mon, 24 Jul 2023 11:08:01 +0000 Received: by outflank-mailman (output) from mailman id 568760.888712; Mon, 24 Jul 2023 11:08:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQ1-0000LJ-OU; Mon, 24 Jul 2023 11:08:01 +0000 Received: by outflank-mailman (input) for mailman id 568760; Mon, 24 Jul 2023 11:08:00 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLl-0008WC-0G for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:37 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bc13b991-2a11-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 13:03:35 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 3F31522971; Mon, 24 Jul 2023 11:03:35 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1404513476; Mon, 24 Jul 2023 11:03:35 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 5EluA4davmSdYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: bc13b991-2a11-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196615; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=siTlKvWMR/RT4S0FnCgR+ZpOvEYZZwpHnARxR3tmw/4=; b=GhkPau+GxIQNm74gzCxgvTBrLWE2MgNHxieKxWzLiPjFgsNIFEOgi7RytaTF7pPUsSfRza DCLsvzH7D+ukWicqelez/plpzkItVdogW46c3rM6PBa4p9wCdzdgJCRobpbNsqNz51o33/ ETy0NnKk3qhcCynU9B/BCCLdpaAS/hk= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 08/25] tools/xenstore: make hashtable key and value parameters const Date: Mon, 24 Jul 2023 13:02:30 +0200 Message-Id: <20230724110247.10520-9-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196897736100003 Content-Type: text/plain; charset="utf-8" The key and value are never modified by hashtable code, so they should be marked as const. Signed-off-by: Juergen Gross --- V3: - make value const, too. --- tools/xenstore/hashtable.c | 7 ++++--- tools/xenstore/hashtable.h | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c index 11f6bf8f15..670dc01003 100644 --- a/tools/xenstore/hashtable.c +++ b/tools/xenstore/hashtable.c @@ -11,7 +11,8 @@ =20 struct entry { - void *k, *v; + const void *k; + void *v; unsigned int h; struct entry *next; }; @@ -140,7 +141,7 @@ static int hashtable_expand(struct hashtable *h) return 0; } =20 -int hashtable_add(struct hashtable *h, void *k, void *v) +int hashtable_add(struct hashtable *h, const void *k, const void *v) { /* This method allows duplicate keys - but they shouldn't be used */ unsigned int index; @@ -164,7 +165,7 @@ int hashtable_add(struct hashtable *h, void *k, void *v) e->k =3D k; if (h->flags & HASHTABLE_FREE_KEY) talloc_steal(e, k); - e->v =3D v; + e->v =3D (void *)v; if (h->flags & HASHTABLE_FREE_VALUE) talloc_steal(e, v); e->next =3D h->table[index]; diff --git a/tools/xenstore/hashtable.h b/tools/xenstore/hashtable.h index 5a2cc4a4be..1da3af2648 100644 --- a/tools/xenstore/hashtable.h +++ b/tools/xenstore/hashtable.h @@ -48,8 +48,8 @@ create_hashtable(const void *ctx, const char *name, * If in doubt, remove before insert. */ =20 -int=20 -hashtable_add(struct hashtable *h, void *k, void *v); +int +hashtable_add(struct hashtable *h, const void *k, const void *v); =20 /*************************************************************************= **** * hashtable_search --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196922; cv=none; d=zohomail.com; s=zohoarc; b=dR3W3u3UCSqeGk5cp3zVaXi1anJmnkWpxIoUPW0IWPA/VI4tDzfZCgTI5nWX8rR4XtcMSFVXxwDr/kvOjV1HcAEb25J02FydC5UU8kDDDnocC40sZFcy1oqMBbJbvtDDfrEHuXfzYDXtAOdnUfjALSY6PRGYLP45UK04ibMfqEE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196922; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6oZMjq2xxg3d8sR1/S0XLcsbOGgm4Oh3G9kg5yfwE0M=; b=Q3FfISYe/yZZIm21LmrJUS2meKnnzJU8Dud5NfnNcdLlrr2wxfq2nF5a9YG5w0GeHi+JY7jl9+0d2eJarYk5iIyIGIizCnoIzrwGXCsgSd7bSC6TEy9K1xrZ8w4yFq+KyM5+roRR1nRxuKyT2qsnV0F0BKsVx83cWH5SyxKc2YY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 16901969220231.4023558571941521; Mon, 24 Jul 2023 04:08:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568765.888738 (Exim 4.92) (envelope-from ) id 1qNtQE-0001MM-QG; Mon, 24 Jul 2023 11:08:14 +0000 Received: by outflank-mailman (output) from mailman id 568765.888738; Mon, 24 Jul 2023 11:08:14 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQE-0001Lp-Is; Mon, 24 Jul 2023 11:08:14 +0000 Received: by outflank-mailman (input) for mailman id 568765; Mon, 24 Jul 2023 11:08:13 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLq-0008WC-Kf for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:42 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id bf697012-2a11-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 13:03:41 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id D1FEB20694; Mon, 24 Jul 2023 11:03:40 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A05C513476; Mon, 24 Jul 2023 11:03:40 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id bmTGJYxavmSpYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:40 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: bf697012-2a11-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196620; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6oZMjq2xxg3d8sR1/S0XLcsbOGgm4Oh3G9kg5yfwE0M=; b=XmoiRpW2LA+tvUODIHXnnBo0mEDqcrgYmuO8FeT9cTmSxX9nIeSBlnd8k8VCvOoDc7VU2b blQ0mHyin7ThS4wzgT+MVFBax99W4f0q6pQWWWmI8J2O2WKWgd8HVrpkJMW3TWP239I+0J MiWV0mSf2DzoIji5YhGxx4NmK8McRm8= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 09/25] tools/xenstore: let hashtable_add() fail in case of existing entry Date: Mon, 24 Jul 2023 13:02:31 +0200 Message-Id: <20230724110247.10520-10-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196923911100003 Content-Type: text/plain; charset="utf-8" Silently adding another entry with the same key to a hashtable is a perfect receipt for later failure with hard to diagnose symptoms. Let hashtable_add() fail in case another entry with the same key is already existing. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - split off from next patch (Julien Grall) - fix coding style (Julien Grall) - use for () loop (Julien Grall) --- tools/xenstore/hashtable.c | 40 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c index 670dc01003..0409725060 100644 --- a/tools/xenstore/hashtable.c +++ b/tools/xenstore/hashtable.c @@ -141,11 +141,34 @@ static int hashtable_expand(struct hashtable *h) return 0; } =20 +static struct entry *hashtable_search_entry(const struct hashtable *h, + const void *k) +{ + struct entry *e; + unsigned int hashvalue, index; + + hashvalue =3D hash(h, k); + index =3D indexFor(h->tablelength, hashvalue); + e =3D h->table[index]; + + for (e =3D h->table[index]; e; e =3D e->next) + { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue =3D=3D e->h) && (h->eqfn(k, e->k))) + return e; + } + + return NULL; +} + int hashtable_add(struct hashtable *h, const void *k, const void *v) { - /* This method allows duplicate keys - but they shouldn't be used */ unsigned int index; struct entry *e; + + if (hashtable_search_entry(h, k)) + return EEXIST; + if (++(h->entrycount) > h->loadlimit) { /* Ignore the return value. If expand fails, we should @@ -176,17 +199,10 @@ int hashtable_add(struct hashtable *h, const void *k,= const void *v) void *hashtable_search(const struct hashtable *h, const void *k) { struct entry *e; - unsigned int hashvalue, index; - hashvalue =3D hash(h,k); - index =3D indexFor(h->tablelength,hashvalue); - e =3D h->table[index]; - while (NULL !=3D e) - { - /* Check hash value to short circuit heavier comparison */ - if ((hashvalue =3D=3D e->h) && (h->eqfn(k, e->k))) return e->v; - e =3D e->next; - } - return NULL; + + e =3D hashtable_search_entry(h, k); + + return e ? e->v : NULL; } =20 void --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196646; cv=none; d=zohomail.com; s=zohoarc; b=OL6uUEKCwOyKRU+rzWlGujOzLayJDqw6GAX7ZMGZnG4kBOExgqUKoZptJuIxxFXX88tmAh25LqTNDlmkW55eSRTChMHuxWbrapBwfwZf9n/4CpnKxlh73LhO0SeTdXbgObBY+nZkg4mHXoPX1moOWJrErASDdxD1U9N1O5Y7p5U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196646; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0xB6oTYH0jaatpxlvCW+IH+bGX/Y8NZRC6aQFlKjAIU=; b=EmK9B+zD2VuANBFUhzV3WrzfMMYgGs5zcIs0Tdhduz16evz12BFmBujx01hZQ2o0HcMR7zP2UXOkGQ3aON0O2G7qyhmAD1iUoXTz1qeJdM6KDG5Jt7Ks1BcW7ZDVGl9AURmK1HWGO4q+F58ejofxBWWvgmlZyC6RX/n85IH8XcA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196646313330.068844383208; Mon, 24 Jul 2023 04:04:06 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568747.888642 (Exim 4.92) (envelope-from ) id 1qNtLw-0004Yf-NB; Mon, 24 Jul 2023 11:03:48 +0000 Received: by outflank-mailman (output) from mailman id 568747.888642; Mon, 24 Jul 2023 11:03:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLw-0004YU-Kd; Mon, 24 Jul 2023 11:03:48 +0000 Received: by outflank-mailman (input) for mailman id 568747; Mon, 24 Jul 2023 11:03:47 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtLv-0000KR-76 for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:47 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id c2c02585-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:03:46 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 7385B20694; Mon, 24 Jul 2023 11:03:46 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 4295A13476; Mon, 24 Jul 2023 11:03:46 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id QU7oDpJavmSyYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:46 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c2c02585-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196626; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0xB6oTYH0jaatpxlvCW+IH+bGX/Y8NZRC6aQFlKjAIU=; b=IVp6ZksGh83WYzXRuRC9WvkUdC8TvLXiGMtuhXl2luo1OFl4cnh8CbKidF+Mzb+GoJOANJ n1/iwQOOuJxAO6sKE+EX6k0E0gx7kZOy15Uoh9K2y7NIcTZnWxkcHqE6u39oxZqV3V9ImY mQbSBNcn48EwXpmMBK9sXUt78SndYs0= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 10/25] tools/xenstore: add hashtable_replace() function Date: Mon, 24 Jul 2023 13:02:32 +0200 Message-Id: <20230724110247.10520-11-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196647800100001 Content-Type: text/plain; charset="utf-8" For an effective way to replace a hashtable entry add a new function hashtable_replace(). This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross Acked-by: Julien Grall --- V3: - fix commit message (Julien Grall) - move unrelated change to previous patch (Julien Grall) - make value parameter const --- tools/xenstore/hashtable.c | 19 +++++++++++++++++++ tools/xenstore/hashtable.h | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c index 0409725060..f85b5a71f1 100644 --- a/tools/xenstore/hashtable.c +++ b/tools/xenstore/hashtable.c @@ -205,6 +205,25 @@ void *hashtable_search(const struct hashtable *h, cons= t void *k) return e ? e->v : NULL; } =20 +int hashtable_replace(struct hashtable *h, const void *k, const void *v) +{ + struct entry *e; + + e =3D hashtable_search_entry(h, k); + if (!e) + return ENOENT; + + if (h->flags & HASHTABLE_FREE_VALUE) + { + talloc_free(e->v); + talloc_steal(e, v); + } + + e->v =3D (void *)v; + + return 0; +} + void hashtable_remove(struct hashtable *h, const void *k) { diff --git a/tools/xenstore/hashtable.h b/tools/xenstore/hashtable.h index 1da3af2648..125de0cfa2 100644 --- a/tools/xenstore/hashtable.h +++ b/tools/xenstore/hashtable.h @@ -51,6 +51,22 @@ create_hashtable(const void *ctx, const char *name, int hashtable_add(struct hashtable *h, const void *k, const void *v); =20 +/*************************************************************************= **** + * hashtable_replace + + * @name hashtable_nsert + * @param h the hashtable to insert into + * @param k the key - hashtable claims ownership and will free on remo= val + * @param v the value - does not claim ownership + * @return zero for successful insertion + * + * This function does check for an entry being present before replacing it + * with a new value. + */ + +int +hashtable_replace(struct hashtable *h, const void *k, const void *v); + /*************************************************************************= **** * hashtable_search =20 --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196648; cv=none; d=zohomail.com; s=zohoarc; b=M4rEAwchwN7J495g4WN1NoU9DDj+xMyAy0b7dg74Lv1MnigJaXn8U8vLawLRNBpP/AenVdnoi1W/0HM5o76/Il4RoSjr1hNFqVp3KbdMGzE/3tUYkjJXZCyRCuuajSX7AdDo40Q/D1rho8KX6TkexqGWK0koRLLA0i3IFX8D/5I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196648; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lKQAmb4KsaU7Y9dxsAA9D22NxH3ixedN67bIExwynMA=; b=LjoygfhwStLCH2ALyBjWU+OC25deVLpCBlKwNoCWbHB4Qu4ydn1mnAKqweMSbEe9fKgPTwg57r0+w8YvDHF8RIA502X6XCjSeVsBYkg9SgAaSiXIBe/uunJrwsO3W8TOn8KqZRYl/qKfRc2Pm7ghPreeO4hVOiXw2YLIGST3bIU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 16901966480751014.4886088427143; Mon, 24 Jul 2023 04:04:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568748.888653 (Exim 4.92) (envelope-from ) id 1qNtM2-0004yl-Vs; Mon, 24 Jul 2023 11:03:54 +0000 Received: by outflank-mailman (output) from mailman id 568748.888653; Mon, 24 Jul 2023 11:03:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtM2-0004yc-Sm; Mon, 24 Jul 2023 11:03:54 +0000 Received: by outflank-mailman (input) for mailman id 568748; Mon, 24 Jul 2023 11:03:53 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtM1-0000KR-8G for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:03:53 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id c61a9ab5-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:03:52 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 16E7322988; Mon, 24 Jul 2023 11:03:52 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id DB9D913476; Mon, 24 Jul 2023 11:03:51 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id btBANJdavmS+YQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:51 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c61a9ab5-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196632; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lKQAmb4KsaU7Y9dxsAA9D22NxH3ixedN67bIExwynMA=; b=lHqgtjHyM3uhZFBaJX0z7wDFva8+2WtcncKcas1+7Pl8JZcTt20uDPR5wf4hwum8UlCANv bjHPw2nkYL1cnmk9easo8T5J4D9f7s17h95qigO3PUR42vriIDdow8ZEN8WI/l6f83u+VB SKWXlZC7WVpw9JpY68SyiycyWvYfgsQ= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 11/25] tools/xenstore: drop use of tdb Date: Mon, 24 Jul 2023 13:02:33 +0200 Message-Id: <20230724110247.10520-12-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196648363100003 Content-Type: text/plain; charset="utf-8" Today all Xenstore nodes are stored in a TDB data base. This data base has several disadvantages: - It is using a fixed sized hash table, resulting in high memory overhead for small installations with only very few VMs, and a rather large performance hit for systems with lots of VMs due to many collisions. The hash table size today is 7919 entries. This means that e.g. in case of a simple desktop use case with 2 or 3 VMs probably far less than 10% of the entries will be used (assuming roughly 100 nodes per VM). OTOH a setup on a large server with 500 VMs would result in heavy conflicts in the hash list with 5-10 nodes per hash table entry. - TDB is using a single large memory area for storing the nodes. It only ever increases this area and will never shrink it afterwards. This will result in more memory usage than necessary after a peak of Xenstore usage. - Xenstore is only single-threaded, while TDB is designed to be fit for multi-threaded use cases, resulting in much higher code complexity than needed. - Special use cases of Xenstore are not possible to implement with TDB in an effective way, while an implementation of a data base tailored for Xenstore could simplify some handling (e.g. transactions) a lot. So drop using TDB and store the nodes directly in memory making them easily accessible. Use a hash-based lookup mechanism for fast lookup of nodes by their full path. For now only replace TDB keeping the current access functions. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - add const (Julien Grall) - use specific pointer type instead of void * (Julien Grall) - add comment to db_fetch() (Julien Grall) V3: - use talloc_memdup() (Julien Grall) --- tools/xenstore/xenstored_core.c | 156 ++++++++++--------------- tools/xenstore/xenstored_core.h | 5 +- tools/xenstore/xenstored_transaction.c | 1 - 3 files changed, 62 insertions(+), 100 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index a12ede147c..2b94392fd4 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -53,7 +53,6 @@ #include "xenstored_domain.h" #include "xenstored_control.h" #include "xenstored_lu.h" -#include "tdb.h" =20 #ifndef NO_SOCKETS #if defined(HAVE_SYSTEMD) @@ -85,7 +84,7 @@ bool keep_orphans =3D false; static int reopen_log_pipe[2]; static int reopen_log_pipe0_pollfd_idx =3D -1; char *tracefile =3D NULL; -static TDB_CONTEXT *tdb_ctx =3D NULL; +static struct hashtable *nodes; unsigned int trace_flags =3D TRACE_OBJ | TRACE_IO; =20 static const char *sockmsg_string(enum xsd_sockmsg_type type); @@ -556,32 +555,30 @@ static void initialize_fds(int *p_sock_pollfd_idx, in= t *ptimeout) } } =20 -static void set_tdb_key(const char *name, TDB_DATA *key) -{ - /* - * Dropping const is fine here, as the key will never be modified - * by TDB. - */ - key->dptr =3D (char *)name; - key->dsize =3D strlen(name); -} - struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size) { - TDB_DATA key, data; + const struct xs_tdb_record_hdr *hdr; + struct xs_tdb_record_hdr *p; =20 - set_tdb_key(db_name, &key); - data =3D tdb_fetch(tdb_ctx, key); - if (!data.dptr) { - errno =3D (tdb_error(tdb_ctx) =3D=3D TDB_ERR_NOEXIST) ? ENOENT : EIO; - *size =3D 0; - } else { - *size =3D data.dsize; - trace_tdb("read %s size %zu\n", db_name, - *size + strlen(db_name)); + hdr =3D hashtable_search(nodes, db_name); + if (!hdr) { + errno =3D ENOENT; + return NULL; } =20 - return (struct xs_tdb_record_hdr *)data.dptr; + *size =3D sizeof(*hdr) + hdr->num_perms * sizeof(hdr->perms[0]) + + hdr->datalen + hdr->childlen; + + /* Return a copy, avoiding a potential modification in the DB. */ + p =3D talloc_memdup(NULL, hdr, *size); + if (!p) { + errno =3D ENOMEM; + return NULL; + } + + trace_tdb("read %s size %zu\n", db_name, *size + strlen(db_name)); + + return p; } =20 static void get_acc_data(const char *name, struct node_account_data *acc) @@ -621,12 +618,10 @@ int db_write(struct connection *conn, const char *db_= name, void *data, struct xs_tdb_record_hdr *hdr =3D data; struct node_account_data old_acc =3D {}; unsigned int old_domid, new_domid; + size_t name_len =3D strlen(db_name); + const char *name; int ret; - TDB_DATA key, dat; =20 - set_tdb_key(db_name, &key); - dat.dptr =3D data; - dat.dsize =3D size; if (!acc) old_acc.memory =3D -1; else @@ -642,29 +637,36 @@ int db_write(struct connection *conn, const char *db_= name, void *data, */ if (old_acc.memory) domain_memory_add_nochk(conn, old_domid, - -old_acc.memory - key.dsize); - ret =3D domain_memory_add(conn, new_domid, size + key.dsize, + -old_acc.memory - name_len); + ret =3D domain_memory_add(conn, new_domid, size + name_len, no_quota_check); if (ret) { /* Error path, so no quota check. */ if (old_acc.memory) domain_memory_add_nochk(conn, old_domid, - old_acc.memory + key.dsize); + old_acc.memory + name_len); return ret; } =20 - /* TDB should set errno, but doesn't even set ecode AFAICT. */ - if (tdb_store(tdb_ctx, key, dat, - (mode =3D=3D NODE_CREATE) ? TDB_INSERT : TDB_MODIFY) !=3D 0) { - domain_memory_add_nochk(conn, new_domid, -size - key.dsize); + if (mode =3D=3D NODE_CREATE) { + /* db_name could be modified later, so allocate a copy. */ + name =3D talloc_strdup(data, db_name); + ret =3D name ? hashtable_add(nodes, name, data) : ENOMEM; + } else + ret =3D hashtable_replace(nodes, db_name, data); + + if (ret) { + /* Free data, as it isn't owned by hashtable now. */ + talloc_free(data); + domain_memory_add_nochk(conn, new_domid, -size - name_len); /* Error path, so no quota check. */ if (old_acc.memory) domain_memory_add_nochk(conn, old_domid, - old_acc.memory + key.dsize); - errno =3D EIO; + old_acc.memory + name_len); + errno =3D ret; return errno; } - trace_tdb("store %s size %zu\n", db_name, size + key.dsize); + trace_tdb("store %s size %zu\n", db_name, size + name_len); =20 if (acc) { /* Don't use new_domid, as it might be a transaction node. */ @@ -680,9 +682,6 @@ int db_delete(struct connection *conn, const char *name, { struct node_account_data tmp_acc; unsigned int domid; - TDB_DATA key; - - set_tdb_key(name, &key); =20 if (!acc) { acc =3D &tmp_acc; @@ -691,15 +690,13 @@ int db_delete(struct connection *conn, const char *na= me, =20 get_acc_data(name, acc); =20 - if (tdb_delete(tdb_ctx, key)) { - errno =3D EIO; - return errno; - } + hashtable_remove(nodes, name); trace_tdb("delete %s\n", name); =20 if (acc->memory) { domid =3D get_acc_domid(conn, name, acc->domid); - domain_memory_add_nochk(conn, domid, -acc->memory - key.dsize); + domain_memory_add_nochk(conn, domid, + -acc->memory - strlen(name)); } =20 return 0; @@ -2354,43 +2351,29 @@ static void manual_node(const char *name, const cha= r *child) talloc_free(node); } =20 -static void tdb_logger(TDB_CONTEXT *tdb, int level, const char * fmt, ...) +static unsigned int hash_from_key_fn(const void *k) { - va_list ap; - char *s; - int saved_errno =3D errno; + const char *str =3D k; + unsigned int hash =3D 5381; + char c; =20 - va_start(ap, fmt); - s =3D talloc_vasprintf(NULL, fmt, ap); - va_end(ap); + while ((c =3D *str++)) + hash =3D ((hash << 5) + hash) + (unsigned int)c; =20 - if (s) { - trace("TDB: %s\n", s); - syslog(LOG_ERR, "TDB: %s", s); - if (verbose) - xprintf("TDB: %s", s); - talloc_free(s); - } else { - trace("talloc failure during logging\n"); - syslog(LOG_ERR, "talloc failure during logging\n"); - } + return hash; +} =20 - errno =3D saved_errno; +static int keys_equal_fn(const void *key1, const void *key2) +{ + return 0 =3D=3D strcmp(key1, key2); } =20 void setup_structure(bool live_update) { - char *tdbname; - - tdbname =3D talloc_strdup(talloc_autofree_context(), "/dev/mem"); - if (!tdbname) - barf_perror("Could not create tdbname"); - - tdb_ctx =3D tdb_open_ex(tdbname, 7919, TDB_INTERNAL | TDB_NOLOCK, - O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, - 0640, &tdb_logger, NULL); - if (!tdb_ctx) - barf_perror("Could not create tdb file %s", tdbname); + nodes =3D create_hashtable(NULL, "nodes", hash_from_key_fn, keys_equal_fn, + HASHTABLE_FREE_KEY | HASHTABLE_FREE_VALUE); + if (!nodes) + barf_perror("Could not create nodes hashtable"); =20 if (live_update) manual_node("/", NULL); @@ -2404,24 +2387,6 @@ void setup_structure(bool live_update) } } =20 -static unsigned int hash_from_key_fn(const void *k) -{ - const char *str =3D k; - unsigned int hash =3D 5381; - char c; - - while ((c =3D *str++)) - hash =3D ((hash << 5) + hash) + (unsigned int)c; - - return hash; -} - - -static int keys_equal_fn(const void *key1, const void *key2) -{ - return 0 =3D=3D strcmp(key1, key2); -} - int remember_string(struct hashtable *hash, const char *str) { char *k =3D talloc_strdup(NULL, str); @@ -2481,12 +2446,11 @@ static int check_store_enoent(const void *ctx, stru= ct connection *conn, /** * Helper to clean_store below. */ -static int clean_store_(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA val, - void *private) +static int clean_store_(const void *key, void *val, void *private) { struct hashtable *reachable =3D private; char *slash; - char * name =3D talloc_strndup(NULL, key.dptr, key.dsize); + char *name =3D talloc_strdup(NULL, key); =20 if (!name) { log("clean_store: ENOMEM"); @@ -2516,7 +2480,7 @@ static int clean_store_(TDB_CONTEXT *tdb, TDB_DATA ke= y, TDB_DATA val, */ static void clean_store(struct check_store_data *data) { - tdb_traverse(tdb_ctx, &clean_store_, data->reachable); + hashtable_iterate(nodes, clean_store_, data->reachable); domain_check_acc(data->domains); } =20 diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index f5aa8d51a0..ce40c61f44 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -33,7 +33,6 @@ #include "xenstore_lib.h" #include "xenstore_state.h" #include "list.h" -#include "tdb.h" #include "hashtable.h" =20 #ifndef O_CLOEXEC @@ -237,7 +236,7 @@ static inline unsigned int get_node_owner(const struct = node *node) return node->perms.p[0].id; } =20 -/* Write a node to the tdb data base. */ +/* Write a node to the data base. */ enum write_node_mode { NODE_CREATE, NODE_MODIFY @@ -247,7 +246,7 @@ int write_node_raw(struct connection *conn, const char = *db_name, struct node *node, enum write_node_mode mode, bool no_quota_check); =20 -/* Get a node from the tdb data base. */ +/* Get a node from the data base. */ struct node *read_node(struct connection *conn, const void *ctx, const char *name); =20 diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 1981d1d55d..378fe79763 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -397,7 +397,6 @@ static int finalize_transaction(struct connection *conn, ? NODE_CREATE : NODE_MODIFY; *is_corrupt |=3D db_write(conn, i->node, hdr, size, NULL, mode, true); - talloc_free(hdr); if (db_delete(conn, i->trans_name, NULL)) *is_corrupt =3D true; } else { --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196925; cv=none; d=zohomail.com; s=zohoarc; b=QdvSqPCzEN4neZfA5cDNWjQvGnaoYV7RDEzXQ9TCk/2ikXMeqJxePtTCO8ic1vrz3ALdLa1nG/f0ABSseQNfv8ZjkR3UhpfYUYckMesylHUPZ4/yfYDtfzsCPowGzn/ff6cwio4HGOeAystC8ZfdBRwfYFuOpQM21S5/d7TBtZI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196925; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=f8c0WZkiA2sRwo/5ZKpm9mv0MPjHkD48eHCwmRexE5o=; b=iGMPZT7DOFdSbOqFFFEbMYElOLUKQmV7YLnwMZjwE7w6De6a6/Oe1DnW3TnA3u7QbZ4zMetaJzxdNG9lvPu+3qTfZiwbQwLvw/hKmXQdgkzZoYCckoNBSPqlTew2BJ0uI9An7ToHfrN65VjxGh2kIfDXkkL8sFgoxuU1DxjDxyQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196925148340.14263276156987; Mon, 24 Jul 2023 04:08:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568772.888777 (Exim 4.92) (envelope-from ) id 1qNtQI-0002Np-2m; Mon, 24 Jul 2023 11:08:18 +0000 Received: by outflank-mailman (output) from mailman id 568772.888777; Mon, 24 Jul 2023 11:08:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQH-0002K8-Mr; Mon, 24 Jul 2023 11:08:17 +0000 Received: by outflank-mailman (input) for mailman id 568772; Mon, 24 Jul 2023 11:08:14 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtM7-0000KR-Vt for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:00 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id c97ddb0c-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:03:57 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id BADE820699; Mon, 24 Jul 2023 11:03:57 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 797C713476; Mon, 24 Jul 2023 11:03:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id WLkiHJ1avmTOYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:03:57 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c97ddb0c-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196637; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f8c0WZkiA2sRwo/5ZKpm9mv0MPjHkD48eHCwmRexE5o=; b=XIPS18LQJz0Zwm18sOO04x2cLl02TTQCJkWqtd8y13UXyUohk+bFru8mzGUpROayqzWmwt M1mVDEliLAVZnnXr9o9UaaoNqPeQUP8up13PGq6V1lD09762LmX6WlMtMDjCi55V8ZEDNl KXZ06UND1Zlv1lhVna7HumjpQZxOYWE= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 12/25] tools/xenstore: remove tdb code Date: Mon, 24 Jul 2023 13:02:34 +0200 Message-Id: <20230724110247.10520-13-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196926034100007 Content-Type: text/plain; charset="utf-8" Now that TDB isn't used anymore, remove it. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- tools/xenstore/Makefile.common | 2 +- tools/xenstore/tdb.c | 1748 -------------------------------- tools/xenstore/tdb.h | 132 --- 3 files changed, 1 insertion(+), 1881 deletions(-) delete mode 100644 tools/xenstore/tdb.c delete mode 100644 tools/xenstore/tdb.h diff --git a/tools/xenstore/Makefile.common b/tools/xenstore/Makefile.common index 657a16849e..3259ab51e6 100644 --- a/tools/xenstore/Makefile.common +++ b/tools/xenstore/Makefile.common @@ -2,7 +2,7 @@ =20 XENSTORED_OBJS-y :=3D xenstored_core.o xenstored_watch.o xenstored_domain.o XENSTORED_OBJS-y +=3D xenstored_transaction.o xenstored_control.o xenstore= d_lu.o -XENSTORED_OBJS-y +=3D talloc.o utils.o tdb.o hashtable.o +XENSTORED_OBJS-y +=3D talloc.o utils.o hashtable.o =20 XENSTORED_OBJS-$(CONFIG_Linux) +=3D xenstored_posix.o xenstored_lu_daemon.o XENSTORED_OBJS-$(CONFIG_NetBSD) +=3D xenstored_posix.o xenstored_lu_daemon= .o diff --git a/tools/xenstore/tdb.c b/tools/xenstore/tdb.c deleted file mode 100644 index 29593b76c3..0000000000 --- a/tools/xenstore/tdb.c +++ /dev/null @@ -1,1748 +0,0 @@ - /*=20 - Unix SMB/CIFS implementation. - - trivial database library - - Copyright (C) Andrew Tridgell 1999-2004 - Copyright (C) Paul `Rusty' Russell 2000 - Copyright (C) Jeremy Allison 2000-2003 - =20 - ** NOTE! The following LGPL license applies to the tdb - ** library. This does NOT imply that all of Samba is released - ** under the LGPL - =20 - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; If not, see . -*/ - - -#ifndef _SAMBA_BUILD_ -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "tdb.h" -#include -#include "talloc.h" -#undef HAVE_MMAP -#else -#include "includes.h" -#include "lib/tdb/include/tdb.h" -#include "system/time.h" -#include "system/shmem.h" -#include "system/filesys.h" -#endif - -#define TDB_MAGIC_FOOD "TDB file\n" -#define TDB_VERSION (0x26011967 + 7) -#define TDB_MAGIC (0x26011999U) -#define TDB_FREE_MAGIC (~TDB_MAGIC) -#define TDB_DEAD_MAGIC (0xFEE1DEAD) -#define TDB_ALIGNMENT 4 -#define MIN_REC_SIZE (2*sizeof(struct list_struct) + TDB_ALIGNMENT) -#define DEFAULT_HASH_SIZE 131 -#define TDB_PAGE_SIZE 0x2000 -#define FREELIST_TOP (sizeof(struct tdb_header)) -#define TDB_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1)) -#define TDB_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF0= 0)|((x)>>24)) -#define TDB_DEAD(r) ((r)->magic =3D=3D TDB_DEAD_MAGIC) -#define TDB_BAD_MAGIC(r) ((r)->magic !=3D TDB_MAGIC && !TDB_DEAD(r)) -#define TDB_HASH_TOP(hash) (FREELIST_TOP + (BUCKET(hash)+1)*sizeof(tdb_off= )) -#define TDB_DATA_START(hash_size) (TDB_HASH_TOP(hash_size-1)) - - -/* NB assumes there is a local variable called "tdb" that is the - * current context, also takes doubly-parenthesized print-style - * argument. */ -#define TDB_LOG(x) tdb->log_fn x - -/* lock offsets */ -#define GLOBAL_LOCK 0 -#define ACTIVE_LOCK 4 - -#ifndef MAP_FILE -#define MAP_FILE 0 -#endif - -#ifndef MAP_FAILED -#define MAP_FAILED ((void *)-1) -#endif - -#ifndef discard_const_p -# if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T) -# define discard_const(ptr) ((void *)((intptr_t)(ptr))) -# else -# define discard_const(ptr) ((void *)(ptr)) -# endif -# define discard_const_p(type, ptr) ((type *)discard_const(ptr)) -#endif - -/* free memory if the pointer is valid and zero the pointer */ -#ifndef SAFE_FREE -#define SAFE_FREE(x) do { if ((x) !=3D NULL) {talloc_free(discard_const_p(= void *, (x))); (x)=3DNULL;} } while(0) -#endif - -#define BUCKET(hash) ((hash) % tdb->header.hash_size) -static TDB_DATA tdb_null; - -/* all contexts, to ensure no double-opens (fcntl locks don't nest!) */ -static TDB_CONTEXT *tdbs =3D NULL; - -static int tdb_munmap(TDB_CONTEXT *tdb) -{ - if (tdb->flags & TDB_INTERNAL) - return 0; - -#ifdef HAVE_MMAP - if (tdb->map_ptr) { - int ret =3D munmap(tdb->map_ptr, tdb->map_size); - if (ret !=3D 0) - return ret; - } -#endif - tdb->map_ptr =3D NULL; - return 0; -} - -static void tdb_mmap(TDB_CONTEXT *tdb) -{ - if (tdb->flags & TDB_INTERNAL) - return; - -#ifdef HAVE_MMAP - if (!(tdb->flags & TDB_NOMMAP)) { - tdb->map_ptr =3D mmap(NULL, tdb->map_size,=20 - PROT_READ|(tdb->read_only? 0:PROT_WRITE),=20 - MAP_SHARED|MAP_FILE, tdb->fd, 0); - - /* - * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!! - */ - - if (tdb->map_ptr =3D=3D MAP_FAILED) { - tdb->map_ptr =3D NULL; - TDB_LOG((tdb, 2, "tdb_mmap failed for size %d (%s)\n",=20 - tdb->map_size, strerror(errno))); - } - } else { - tdb->map_ptr =3D NULL; - } -#else - tdb->map_ptr =3D NULL; -#endif -} - -/* Endian conversion: we only ever deal with 4 byte quantities */ -static void *convert(void *buf, uint32_t size) -{ - uint32_t i, *p =3D buf; - for (i =3D 0; i < size / 4; i++) - p[i] =3D TDB_BYTEREV(p[i]); - return buf; -} -#define DOCONV() (tdb->flags & TDB_CONVERT) -#define CONVERT(x) (DOCONV() ? convert(&x, sizeof(x)) : &x) - -/* the body of the database is made of one list_struct for the free space - plus a separate data list for each hash value */ -struct list_struct { - tdb_off next; /* offset of the next record in the list */ - tdb_len rec_len; /* total byte length of record */ - tdb_len key_len; /* byte length of key */ - tdb_len data_len; /* byte length of data */ - uint32_t full_hash; /* the full 32 bit hash of the key */ - uint32_t magic; /* try to catch errors */ - /* the following union is implied: - union { - char record[rec_len]; - struct { - char key[key_len]; - char data[data_len]; - } - uint32_t totalsize; (tailer) - } - */ -}; - -/* a byte range locking function - return 0 on success - this functions locks/unlocks 1 byte at the specified offset. - - On error, errno is also set so that errors are passed back properly - through tdb_open(). */ -static int tdb_brlock(TDB_CONTEXT *tdb, tdb_off offset,=20 - int rw_type, int lck_type, int probe) -{ - struct flock fl; - int ret; - - if (tdb->flags & TDB_NOLOCK) - return 0; - if ((rw_type =3D=3D F_WRLCK) && (tdb->read_only)) { - errno =3D EACCES; - return -1; - } - - fl.l_type =3D rw_type; - fl.l_whence =3D SEEK_SET; - fl.l_start =3D offset; - fl.l_len =3D 1; - fl.l_pid =3D 0; - - do { - ret =3D fcntl(tdb->fd,lck_type,&fl); - } while (ret =3D=3D -1 && errno =3D=3D EINTR); - - if (ret =3D=3D -1) { - if (!probe && lck_type !=3D F_SETLK) { - /* Ensure error code is set for log fun to examine. */ - tdb->ecode =3D TDB_ERR_LOCK; - TDB_LOG((tdb, 5,"tdb_brlock failed (fd=3D%d) at offset %d rw_type=3D%d = lck_type=3D%d\n",=20 - tdb->fd, offset, rw_type, lck_type)); - } - /* Generic lock error. errno set by fcntl. - * EAGAIN is an expected return from non-blocking - * locks. */ - if (errno !=3D EAGAIN) { - TDB_LOG((tdb, 5, "tdb_brlock failed (fd=3D%d) at offset %d rw_type=3D%d = lck_type=3D%d: %s\n",=20 - tdb->fd, offset, rw_type, lck_type,=20 - strerror(errno))); - } - return TDB_ERRCODE(TDB_ERR_LOCK, -1); - } - return 0; -} - -/* lock a list in the database. list -1 is the alloc list */ -static int tdb_lock(TDB_CONTEXT *tdb, int list, int ltype) -{ - if (list < -1 || list >=3D (int)tdb->header.hash_size) { - TDB_LOG((tdb, 0,"tdb_lock: invalid list %d for ltype=3D%d\n",=20 - list, ltype)); - return -1; - } - if (tdb->flags & TDB_NOLOCK) - return 0; - - /* Since fcntl locks don't nest, we do a lock for the first one, - and simply bump the count for future ones */ - if (tdb->locked[list+1].count =3D=3D 0) { - if (tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW, 0)) { - TDB_LOG((tdb, 0,"tdb_lock failed on list %d ltype=3D%d (%s)\n",=20 - list, ltype, strerror(errno))); - return -1; - } - tdb->locked[list+1].ltype =3D ltype; - } - tdb->locked[list+1].count++; - return 0; -} - -/* unlock the database: returns void because it's too late for errors. */ - /* changed to return int it may be interesting to know there - has been an error --simo */ -static int tdb_unlock(TDB_CONTEXT *tdb, int list, - int ltype __attribute__((unused))) -{ - int ret =3D -1; - - if (tdb->flags & TDB_NOLOCK) - return 0; - - /* Sanity checks */ - if (list < -1 || list >=3D (int)tdb->header.hash_size) { - TDB_LOG((tdb, 0, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header= .hash_size)); - return ret; - } - - if (tdb->locked[list+1].count=3D=3D0) { - TDB_LOG((tdb, 0, "tdb_unlock: count is 0\n")); - return ret; - } - - if (tdb->locked[list+1].count =3D=3D 1) { - /* Down to last nested lock: unlock underneath */ - ret =3D tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK, F_SETLKW, 0); - } else { - ret =3D 0; - } - tdb->locked[list+1].count--; - - if (ret) - TDB_LOG((tdb, 0,"tdb_unlock: An error occurred unlocking!\n"));=20 - return ret; -} - -/* This is based on the hash algorithm from gdbm */ -static uint32_t default_tdb_hash(TDB_DATA *key) -{ - uint32_t value; /* Used to compute the hash value. */ - uint32_t i; /* Used to cycle through random values. */ - - /* Set the initial value from the key size. */ - for (value =3D 0x238F13AF * key->dsize, i=3D0; i < key->dsize; i++) - value =3D (value + (key->dptr[i] << (i*5 % 24))); - - return (1103515243 * value + 12345); =20 -} - -/* check for an out of bounds access - if it is out of bounds then - see if the database has been expanded by someone else and expand - if necessary=20 - note that "len" is the minimum length needed for the db -*/ -static int tdb_oob(TDB_CONTEXT *tdb, tdb_off len, int probe) -{ - struct stat st; - if (len <=3D tdb->map_size) - return 0; - if (tdb->flags & TDB_INTERNAL) { - if (!probe) { - /* Ensure ecode is set for log fn. */ - tdb->ecode =3D TDB_ERR_IO; - TDB_LOG((tdb, 0,"tdb_oob len %d beyond internal malloc size %d\n", - (int)len, (int)tdb->map_size)); - } - return TDB_ERRCODE(TDB_ERR_IO, -1); - } - - if (fstat(tdb->fd, &st) =3D=3D -1) - return TDB_ERRCODE(TDB_ERR_IO, -1); - - if (st.st_size < (off_t)len) { - if (!probe) { - /* Ensure ecode is set for log fn. */ - tdb->ecode =3D TDB_ERR_IO; - TDB_LOG((tdb, 0,"tdb_oob len %d beyond eof at %d\n", - (int)len, (int)st.st_size)); - } - return TDB_ERRCODE(TDB_ERR_IO, -1); - } - - /* Unmap, update size, remap */ - if (tdb_munmap(tdb) =3D=3D -1) - return TDB_ERRCODE(TDB_ERR_IO, -1); - tdb->map_size =3D st.st_size; - tdb_mmap(tdb); - return 0; -} - -/* write a lump of data at a specified offset */ -static int tdb_write(TDB_CONTEXT *tdb, tdb_off off, void *buf, tdb_len len) -{ - if (tdb_oob(tdb, off + len, 0) !=3D 0) - return -1; - - if (tdb->map_ptr) - memcpy(off + (char *)tdb->map_ptr, buf, len); -#ifdef HAVE_PWRITE - else if (pwrite(tdb->fd, buf, len, off) !=3D (ssize_t)len) { -#else - else if (lseek(tdb->fd, off, SEEK_SET) !=3D (off_t)off - || write(tdb->fd, buf, len) !=3D (off_t)len) { -#endif - /* Ensure ecode is set for log fn. */ - tdb->ecode =3D TDB_ERR_IO; - TDB_LOG((tdb, 0,"tdb_write failed at %d len=3D%d (%s)\n", - off, len, strerror(errno))); - return TDB_ERRCODE(TDB_ERR_IO, -1); - } - return 0; -} - -/* read a lump of data at a specified offset, maybe convert */ -static int tdb_read(TDB_CONTEXT *tdb,tdb_off off,void *buf,tdb_len len,int= cv) -{ - if (tdb_oob(tdb, off + len, 0) !=3D 0) - return -1; - - if (tdb->map_ptr) - memcpy(buf, off + (char *)tdb->map_ptr, len); -#ifdef HAVE_PREAD - else if (pread(tdb->fd, buf, len, off) !=3D (off_t)len) { -#else - else if (lseek(tdb->fd, off, SEEK_SET) !=3D (off_t)off - || read(tdb->fd, buf, len) !=3D (off_t)len) { -#endif - /* Ensure ecode is set for log fn. */ - tdb->ecode =3D TDB_ERR_IO; - TDB_LOG((tdb, 0,"tdb_read failed at %d len=3D%d (%s)\n", - off, len, strerror(errno))); - return TDB_ERRCODE(TDB_ERR_IO, -1); - } - if (cv) - convert(buf, len); - return 0; -} - -/* don't allocate memory: used in tdb_delete path. */ -static int tdb_key_eq(TDB_CONTEXT *tdb, tdb_off off, TDB_DATA key) -{ - char buf[64]; - uint32_t len; - - if (tdb_oob(tdb, off + key.dsize, 0) !=3D 0) - return -1; - - if (tdb->map_ptr) - return !memcmp(off + (char*)tdb->map_ptr, key.dptr, key.dsize); - - while (key.dsize) { - len =3D key.dsize; - if (len > sizeof(buf)) - len =3D sizeof(buf); - if (tdb_read(tdb, off, buf, len, 0) !=3D 0) - return -1; - if (memcmp(buf, key.dptr, len) !=3D 0) - return 0; - key.dptr +=3D len; - key.dsize -=3D len; - off +=3D len; - } - return 1; -} - -/* read a lump of data, allocating the space for it */ -static char *tdb_alloc_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_len len) -{ - char *buf; - - if (!(buf =3D talloc_size(tdb, len))) { - /* Ensure ecode is set for log fn. */ - tdb->ecode =3D TDB_ERR_OOM; - TDB_LOG((tdb, 0,"tdb_alloc_read malloc failed len=3D%d (%s)\n", - len, strerror(errno))); - return TDB_ERRCODE(TDB_ERR_OOM, buf); - } - if (tdb_read(tdb, offset, buf, len, 0) =3D=3D -1) { - SAFE_FREE(buf); - return NULL; - } - return buf; -} - -/* read/write a tdb_off */ -static int ofs_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d) -{ - return tdb_read(tdb, offset, (char*)d, sizeof(*d), DOCONV()); -} -static int ofs_write(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d) -{ - tdb_off off =3D *d; - return tdb_write(tdb, offset, CONVERT(off), sizeof(*d)); -} - -/* read/write a record */ -static int rec_read(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *= rec) -{ - if (tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) =3D=3D -1) - return -1; - if (TDB_BAD_MAGIC(rec)) { - /* Ensure ecode is set for log fn. */ - tdb->ecode =3D TDB_ERR_CORRUPT; - TDB_LOG((tdb, 0,"rec_read bad magic 0x%x at offset=3D%d\n", rec->magic, = offset)); - return TDB_ERRCODE(TDB_ERR_CORRUPT, -1); - } - return tdb_oob(tdb, rec->next+sizeof(*rec), 0); -} -static int rec_write(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct = *rec) -{ - struct list_struct r =3D *rec; - return tdb_write(tdb, offset, CONVERT(r), sizeof(r)); -} - -/* read a freelist record and check for simple errors */ -static int rec_free_read(TDB_CONTEXT *tdb, tdb_off off, struct list_struct= *rec) -{ - if (tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) =3D=3D -1) - return -1; - - if (rec->magic =3D=3D TDB_MAGIC) { - /* this happens when a app is showdown while deleting a record - we shou= ld - not completely fail when this happens */ - TDB_LOG((tdb, 0,"rec_free_read non-free magic 0x%x at offset=3D%d - fixi= ng\n",=20 - rec->magic, off)); - rec->magic =3D TDB_FREE_MAGIC; - if (tdb_write(tdb, off, rec, sizeof(*rec)) =3D=3D -1) - return -1; - } - - if (rec->magic !=3D TDB_FREE_MAGIC) { - /* Ensure ecode is set for log fn. */ - tdb->ecode =3D TDB_ERR_CORRUPT; - TDB_LOG((tdb, 0,"rec_free_read bad magic 0x%x at offset=3D%d\n",=20 - rec->magic, off)); - return TDB_ERRCODE(TDB_ERR_CORRUPT, -1); - } - if (tdb_oob(tdb, rec->next+sizeof(*rec), 0) !=3D 0) - return -1; - return 0; -} - -/* update a record tailer (must hold allocation lock) */ -static int update_tailer(TDB_CONTEXT *tdb, tdb_off offset, - const struct list_struct *rec) -{ - tdb_off totalsize; - - /* Offset of tailer from record header */ - totalsize =3D sizeof(*rec) + rec->rec_len; - return ofs_write(tdb, offset + totalsize - sizeof(tdb_off), - &totalsize); -} - -/* Remove an element from the freelist. Must have alloc lock. */ -static int remove_from_freelist(TDB_CONTEXT *tdb, tdb_off off, tdb_off nex= t) -{ - tdb_off last_ptr, i; - - /* read in the freelist top */ - last_ptr =3D FREELIST_TOP; - while (ofs_read(tdb, last_ptr, &i) !=3D -1 && i !=3D 0) { - if (i =3D=3D off) { - /* We've found it! */ - return ofs_write(tdb, last_ptr, &next); - } - /* Follow chain (next offset is at start of record) */ - last_ptr =3D i; - } - TDB_LOG((tdb, 0,"remove_from_freelist: not on list at off=3D%d\n", off)); - return TDB_ERRCODE(TDB_ERR_CORRUPT, -1); -} - -/* Add an element into the freelist. Merge adjacent records if - neccessary. */ -static int tdb_free(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *= rec) -{ - tdb_off right, left; - - /* Allocation and tailer lock */ - if (tdb_lock(tdb, -1, F_WRLCK) !=3D 0) - return -1; - - /* set an initial tailer, so if we fail we don't leave a bogus record */ - if (update_tailer(tdb, offset, rec) !=3D 0) { - TDB_LOG((tdb, 0, "tdb_free: upfate_tailer failed!\n")); - goto fail; - } - - /* Look right first (I'm an Australian, dammit) */ - right =3D offset + sizeof(*rec) + rec->rec_len; - if (right + sizeof(*rec) <=3D tdb->map_size) { - struct list_struct r; - - if (tdb_read(tdb, right, &r, sizeof(r), DOCONV()) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_free: right read failed at %u\n", right)); - goto left; - } - - /* If it's free, expand to include it. */ - if (r.magic =3D=3D TDB_FREE_MAGIC) { - if (remove_from_freelist(tdb, right, r.next) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_free: right free failed at %u\n", right)); - goto left; - } - rec->rec_len +=3D sizeof(r) + r.rec_len; - } - } - -left: - /* Look left */ - left =3D offset - sizeof(tdb_off); - if (left > TDB_DATA_START(tdb->header.hash_size)) { - struct list_struct l; - tdb_off leftsize; - =09 - /* Read in tailer and jump back to header */ - if (ofs_read(tdb, left, &leftsize) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_free: left offset read failed at %u\n", left)); - goto update; - } - left =3D offset - leftsize; - - /* Now read in record */ - if (tdb_read(tdb, left, &l, sizeof(l), DOCONV()) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_free: left read failed at %u (%u)\n", left, lefts= ize)); - goto update; - } - - /* If it's free, expand to include it. */ - if (l.magic =3D=3D TDB_FREE_MAGIC) { - if (remove_from_freelist(tdb, left, l.next) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_free: left free failed at %u\n", left)); - goto update; - } else { - offset =3D left; - rec->rec_len +=3D leftsize; - } - } - } - -update: - if (update_tailer(tdb, offset, rec) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_free: update_tailer failed at %u\n", offset)); - goto fail; - } - - /* Now, prepend to free list */ - rec->magic =3D TDB_FREE_MAGIC; - - if (ofs_read(tdb, FREELIST_TOP, &rec->next) =3D=3D -1 || - rec_write(tdb, offset, rec) =3D=3D -1 || - ofs_write(tdb, FREELIST_TOP, &offset) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_free record write failed at offset=3D%d\n", offset= )); - goto fail; - } - - /* And we're done. */ - tdb_unlock(tdb, -1, F_WRLCK); - return 0; - - fail: - tdb_unlock(tdb, -1, F_WRLCK); - return -1; -} - - -/* expand a file. we prefer to use ftruncate, as that is what posix - says to use for mmap expansion */ -static int expand_file(TDB_CONTEXT *tdb, tdb_off size, tdb_off addition) -{ - char buf[1024]; -#ifdef HAVE_FTRUNCATE_EXTEND - if (ftruncate(tdb->fd, size+addition) !=3D 0) { - TDB_LOG((tdb, 0, "expand_file ftruncate to %d failed (%s)\n",=20 - size+addition, strerror(errno))); - return -1; - } -#else - char b =3D 0; - -#ifdef HAVE_PWRITE - if (pwrite(tdb->fd, &b, 1, (size+addition) - 1) !=3D 1) { -#else - if (lseek(tdb->fd, (size+addition) - 1, SEEK_SET) !=3D (off_t)(size+addit= ion) - 1 ||=20 - write(tdb->fd, &b, 1) !=3D 1) { -#endif - TDB_LOG((tdb, 0, "expand_file to %d failed (%s)\n",=20 - size+addition, strerror(errno))); - return -1; - } -#endif - - /* now fill the file with something. This ensures that the file isn't spa= rse, which would be - very bad if we ran out of disk. This must be done with write, not via = mmap */ - memset(buf, 0x42, sizeof(buf)); - while (addition) { - int n =3D addition>sizeof(buf)?sizeof(buf):addition; -#ifdef HAVE_PWRITE - int ret =3D pwrite(tdb->fd, buf, n, size); -#else - int ret; - if (lseek(tdb->fd, size, SEEK_SET) !=3D (off_t)size) - return -1; - ret =3D write(tdb->fd, buf, n); -#endif - if (ret !=3D n) { - TDB_LOG((tdb, 0, "expand_file write of %d failed (%s)\n",=20 - n, strerror(errno))); - return -1; - } - addition -=3D n; - size +=3D n; - } - return 0; -} - - -/* expand the database at least size bytes by expanding the underlying - file and doing the mmap again if necessary */ -static int tdb_expand(TDB_CONTEXT *tdb, tdb_off size) -{ - struct list_struct rec; - tdb_off offset; - - if (tdb_lock(tdb, -1, F_WRLCK) =3D=3D -1) { - TDB_LOG((tdb, 0, "lock failed in tdb_expand\n")); - return -1; - } - - /* must know about any previous expansions by another process */ - tdb_oob(tdb, tdb->map_size + 1, 1); - - /* always make room for at least 10 more records, and round - the database up to a multiple of TDB_PAGE_SIZE */ - size =3D TDB_ALIGN(tdb->map_size + size*10, TDB_PAGE_SIZE) - tdb->map_siz= e; - - if (!(tdb->flags & TDB_INTERNAL)) - tdb_munmap(tdb); - - /* - * We must ensure the file is unmapped before doing this - * to ensure consistency with systems like OpenBSD where - * writes and mmaps are not consistent. - */ - - /* expand the file itself */ - if (!(tdb->flags & TDB_INTERNAL)) { - if (expand_file(tdb, tdb->map_size, size) !=3D 0) - goto fail; - } - - tdb->map_size +=3D size; - - if (tdb->flags & TDB_INTERNAL) { - char *new_map_ptr =3D talloc_realloc_size(tdb, tdb->map_ptr, - tdb->map_size); - if (!new_map_ptr) { - tdb->map_size -=3D size; - goto fail; - } - tdb->map_ptr =3D new_map_ptr; - } else { - /* - * We must ensure the file is remapped before adding the space - * to ensure consistency with systems like OpenBSD where - * writes and mmaps are not consistent. - */ - - /* We're ok if the mmap fails as we'll fallback to read/write */ - tdb_mmap(tdb); - } - - /* form a new freelist record */ - memset(&rec,'\0',sizeof(rec)); - rec.rec_len =3D size - sizeof(rec); - - /* link it into the free list */ - offset =3D tdb->map_size - size; - if (tdb_free(tdb, offset, &rec) =3D=3D -1) - goto fail; - - tdb_unlock(tdb, -1, F_WRLCK); - return 0; - fail: - tdb_unlock(tdb, -1, F_WRLCK); - return -1; -} - - -/*=20 - the core of tdb_allocate - called when we have decided which - free list entry to use - */ -static tdb_off tdb_allocate_ofs(TDB_CONTEXT *tdb, tdb_len length, tdb_off = rec_ptr, - struct list_struct *rec, tdb_off last_ptr) -{ - struct list_struct newrec; - tdb_off newrec_ptr; - - memset(&newrec, '\0', sizeof(newrec)); - - /* found it - now possibly split it up */ - if (rec->rec_len > length + MIN_REC_SIZE) { - /* Length of left piece */ - length =3D TDB_ALIGN(length, TDB_ALIGNMENT); - =09 - /* Right piece to go on free list */ - newrec.rec_len =3D rec->rec_len - (sizeof(*rec) + length); - newrec_ptr =3D rec_ptr + sizeof(*rec) + length; - =09 - /* And left record is shortened */ - rec->rec_len =3D length; - } else { - newrec_ptr =3D 0; - } -=09 - /* Remove allocated record from the free list */ - if (ofs_write(tdb, last_ptr, &rec->next) =3D=3D -1) { - return 0; - } -=09 - /* Update header: do this before we drop alloc - lock, otherwise tdb_free() might try to - merge with us, thinking we're free. - (Thanks Jeremy Allison). */ - rec->magic =3D TDB_MAGIC; - if (rec_write(tdb, rec_ptr, rec) =3D=3D -1) { - return 0; - } -=09 - /* Did we create new block? */ - if (newrec_ptr) { - /* Update allocated record tailer (we - shortened it). */ - if (update_tailer(tdb, rec_ptr, rec) =3D=3D -1) { - return 0; - } - =09 - /* Free new record */ - if (tdb_free(tdb, newrec_ptr, &newrec) =3D=3D -1) { - return 0; - } - } -=09 - /* all done - return the new record offset */ - return rec_ptr; -} - -/* allocate some space from the free list. The offset returned points - to a unconnected list_struct within the database with room for at - least length bytes of total data - - 0 is returned if the space could not be allocated - */ -static tdb_off tdb_allocate(TDB_CONTEXT *tdb, tdb_len length, - struct list_struct *rec) -{ - tdb_off rec_ptr, last_ptr, newrec_ptr; - struct { - tdb_off rec_ptr, last_ptr; - tdb_len rec_len; - } bestfit =3D { 0, 0, 0 }; - - if (tdb_lock(tdb, -1, F_WRLCK) =3D=3D -1) - return 0; - - /* Extra bytes required for tailer */ - length +=3D sizeof(tdb_off); - - again: - last_ptr =3D FREELIST_TOP; - - /* read in the freelist top */ - if (ofs_read(tdb, FREELIST_TOP, &rec_ptr) =3D=3D -1) - goto fail; - - bestfit.rec_ptr =3D 0; - - /*=20 - this is a best fit allocation strategy. Originally we used - a first fit strategy, but it suffered from massive fragmentation - issues when faced with a slowly increasing record size. - */ - while (rec_ptr) { - if (rec_free_read(tdb, rec_ptr, rec) =3D=3D -1) { - goto fail; - } - - if (rec->rec_len >=3D length) { - if (bestfit.rec_ptr =3D=3D 0 || - rec->rec_len < bestfit.rec_len) { - bestfit.rec_len =3D rec->rec_len; - bestfit.rec_ptr =3D rec_ptr; - bestfit.last_ptr =3D last_ptr; - /* consider a fit to be good enough if we aren't wasting more than hal= f the space */ - if (bestfit.rec_len < 2*length) { - break; - } - } - } - - /* move to the next record */ - last_ptr =3D rec_ptr; - rec_ptr =3D rec->next; - } - - if (bestfit.rec_ptr !=3D 0) { - if (rec_free_read(tdb, bestfit.rec_ptr, rec) =3D=3D -1) { - goto fail; - } - - newrec_ptr =3D tdb_allocate_ofs(tdb, length, bestfit.rec_ptr, rec, bestf= it.last_ptr); - tdb_unlock(tdb, -1, F_WRLCK); - return newrec_ptr; - } - - /* we didn't find enough space. See if we can expand the - database and if we can then try again */ - if (tdb_expand(tdb, length + sizeof(*rec)) =3D=3D 0) - goto again; - fail: - tdb_unlock(tdb, -1, F_WRLCK); - return 0; -} - -/* initialise a new database with a specified hash size */ -static int tdb_new_database(TDB_CONTEXT *tdb, int hash_size) -{ - struct tdb_header *newdb; - int size, ret =3D -1; - - /* We make it up in memory, then write it out if not internal */ - size =3D sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off); - if (!(newdb =3D talloc_zero_size(tdb, size))) - return TDB_ERRCODE(TDB_ERR_OOM, -1); - - /* Fill in the header */ - newdb->version =3D TDB_VERSION; - newdb->hash_size =3D hash_size; - if (tdb->flags & TDB_INTERNAL) { - tdb->map_size =3D size; - tdb->map_ptr =3D (char *)newdb; - memcpy(&tdb->header, newdb, sizeof(tdb->header)); - /* Convert the `ondisk' version if asked. */ - CONVERT(*newdb); - return 0; - } - if (lseek(tdb->fd, 0, SEEK_SET) =3D=3D -1) - goto fail; - - if (ftruncate(tdb->fd, 0) =3D=3D -1) - goto fail; - - /* This creates an endian-converted header, as if read from disk */ - CONVERT(*newdb); - memcpy(&tdb->header, newdb, sizeof(tdb->header)); - /* Don't endian-convert the magic food! */ - memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1); - if (write(tdb->fd, newdb, size) !=3D size) - ret =3D -1; - else - ret =3D 0; - - fail: - SAFE_FREE(newdb); - return ret; -} - -/* Returns 0 on fail. On success, return offset of record, and fills - in rec */ -static tdb_off tdb_find(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash, - struct list_struct *r) -{ - tdb_off rec_ptr; -=09 - /* read in the hash top */ - if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) =3D=3D -1) - return 0; - - /* keep looking until we find the right record */ - while (rec_ptr) { - if (rec_read(tdb, rec_ptr, r) =3D=3D -1) - return 0; - - if (!TDB_DEAD(r) && hash=3D=3Dr->full_hash && key.dsize=3D=3Dr->key_len)= { - /* a very likely hit - read the key */ - int cmp =3D tdb_key_eq(tdb, rec_ptr + sizeof(*r), key); - if (cmp < 0) - return 0; - else if (cmp > 0) - return rec_ptr; - } - rec_ptr =3D r->next; - } - return TDB_ERRCODE(TDB_ERR_NOEXIST, 0); -} - -/* As tdb_find, but if you succeed, keep the lock */ -static tdb_off tdb_find_lock_hash(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t= hash, int locktype, - struct list_struct *rec) -{ - uint32_t rec_ptr; - - if (tdb_lock(tdb, BUCKET(hash), locktype) =3D=3D -1) - return 0; - if (!(rec_ptr =3D tdb_find(tdb, key, hash, rec))) - tdb_unlock(tdb, BUCKET(hash), locktype); - return rec_ptr; -} - -enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb) -{ - return tdb->ecode; -} - -static struct tdb_errname { - enum TDB_ERROR ecode; const char *estring; -} emap[] =3D { {TDB_SUCCESS, "Success"}, - {TDB_ERR_CORRUPT, "Corrupt database"}, - {TDB_ERR_IO, "IO Error"}, - {TDB_ERR_LOCK, "Locking error"}, - {TDB_ERR_OOM, "Out of memory"}, - {TDB_ERR_EXISTS, "Record exists"}, - {TDB_ERR_NOLOCK, "Lock exists on other keys"}, - {TDB_ERR_NOEXIST, "Record does not exist"} }; - -/* Error string for the last tdb error */ -const char *tdb_errorstr(TDB_CONTEXT *tdb) -{ - uint32_t i; - for (i =3D 0; i < sizeof(emap) / sizeof(struct tdb_errname); i++) - if (tdb->ecode =3D=3D emap[i].ecode) - return emap[i].estring; - return "Invalid error code"; -} - -/* update an entry in place - this only works if the new data size - is <=3D the old data size and the key exists. - on failure return -1. -*/ - -static int tdb_update_hash(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash, = TDB_DATA dbuf) -{ - struct list_struct rec; - tdb_off rec_ptr; - - /* find entry */ - if (!(rec_ptr =3D tdb_find(tdb, key, hash, &rec))) - return -1; - - /* must be long enough key, data and tailer */ - if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off)) { - tdb->ecode =3D TDB_SUCCESS; /* Not really an error */ - return -1; - } - - if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len, - dbuf.dptr, dbuf.dsize) =3D=3D -1) - return -1; - - if (dbuf.dsize !=3D rec.data_len) { - /* update size */ - rec.data_len =3D dbuf.dsize; - return rec_write(tdb, rec_ptr, &rec); - } -=20 - return 0; -} - -/* find an entry in the database given a key */ -/* If an entry doesn't exist tdb_err will be set to - * TDB_ERR_NOEXIST. If a key has no data attached - * then the TDB_DATA will have zero length but - * a non-zero pointer - */ - -TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key) -{ - tdb_off rec_ptr; - struct list_struct rec; - TDB_DATA ret; - uint32_t hash; - - /* find which hash bucket it is in */ - hash =3D tdb->hash_fn(&key); - if (!(rec_ptr =3D tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) - return tdb_null; - - ret.dptr =3D tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len, - rec.data_len); - ret.dsize =3D rec.data_len; - tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK); - return ret; -} - -/* check if an entry in the database exists=20 - - note that 1 is returned if the key is found and 0 is returned if not fo= und - this doesn't match the conventions in the rest of this module, but is - compatible with gdbm -*/ -static int tdb_exists_hash(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash) -{ - struct list_struct rec; -=09 - if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) =3D=3D 0) - return 0; - tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK); - return 1; -} - -/* record lock stops delete underneath */ -static int lock_record(TDB_CONTEXT *tdb, tdb_off off) -{ - return off ? tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0) : 0; -} -/* - Write locks override our own fcntl readlocks, so check it here. - Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not - an error to fail to get the lock here. -*/ -=20 -static int write_lock_record(TDB_CONTEXT *tdb, tdb_off off) -{ - struct tdb_traverse_lock *i; - for (i =3D &tdb->travlocks; i; i =3D i->next) - if (i->off =3D=3D off) - return -1; - return tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1); -} - -/* - Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not - an error to fail to get the lock here. -*/ - -static int write_unlock_record(TDB_CONTEXT *tdb, tdb_off off) -{ - return tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0); -} -/* fcntl locks don't stack: avoid unlocking someone else's */ -static int unlock_record(TDB_CONTEXT *tdb, tdb_off off) -{ - struct tdb_traverse_lock *i; - uint32_t count =3D 0; - - if (off =3D=3D 0) - return 0; - for (i =3D &tdb->travlocks; i; i =3D i->next) - if (i->off =3D=3D off) - count++; - return (count =3D=3D 1 ? tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0) : 0); -} - -/* actually delete an entry in the database given the offset */ -static int do_delete(TDB_CONTEXT *tdb, tdb_off rec_ptr, struct list_struct= *rec) -{ - tdb_off last_ptr, i; - struct list_struct lastrec; - - if (tdb->read_only) return -1; - - if (write_lock_record(tdb, rec_ptr) =3D=3D -1) { - /* Someone traversing here: mark it as dead */ - rec->magic =3D TDB_DEAD_MAGIC; - return rec_write(tdb, rec_ptr, rec); - } - if (write_unlock_record(tdb, rec_ptr) !=3D 0) - return -1; - - /* find previous record in hash chain */ - if (ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) =3D=3D -1) - return -1; - for (last_ptr =3D 0; i !=3D rec_ptr; last_ptr =3D i, i =3D lastrec.next) - if (rec_read(tdb, i, &lastrec) =3D=3D -1) - return -1; - - /* unlink it: next ptr is at start of record. */ - if (last_ptr =3D=3D 0) - last_ptr =3D TDB_HASH_TOP(rec->full_hash); - if (ofs_write(tdb, last_ptr, &rec->next) =3D=3D -1) - return -1; - - /* recover the space */ - if (tdb_free(tdb, rec_ptr, rec) =3D=3D -1) - return -1; - return 0; -} - -/* Uses traverse lock: 0 =3D finish, -1 =3D error, other =3D record offset= */ -static int tdb_next_lock(TDB_CONTEXT *tdb, struct tdb_traverse_lock *tlock, - struct list_struct *rec) -{ - int want_next =3D (tlock->off !=3D 0); - - /* Lock each chain from the start one. */ - for (; tlock->hash < tdb->header.hash_size; tlock->hash++) { - - /* this is an optimisation for the common case where - the hash chain is empty, which is particularly - common for the use of tdb with ldb, where large - hashes are used. In that case we spend most of our - time in tdb_brlock(), locking empty hash chains. - - To avoid this, we do an unlocked pre-check to see - if the hash chain is empty before starting to look - inside it. If it is empty then we can avoid that - hash chain. If it isn't empty then we can't believe - the value we get back, as we read it without a - lock, so instead we get the lock and re-fetch the - value below. - - Notice that not doing this optimisation on the - first hash chain is critical. We must guarantee - that we have done at least one fcntl lock at the - start of a search to guarantee that memory is - coherent on SMP systems. If records are added by - others during the search then thats OK, and we - could possibly miss those with this trick, but we - could miss them anyway without this trick, so the - semantics don't change. - - With a non-indexed ldb search this trick gains us a - factor of around 80 in speed on a linux 2.6.x - system (testing using ldbtest). - */ - if (!tlock->off && tlock->hash !=3D 0) { - uint32_t off; - if (tdb->map_ptr) { - for (;tlock->hash < tdb->header.hash_size;tlock->hash++) { - if (0 !=3D *(uint32_t *)(TDB_HASH_TOP(tlock->hash) + (unsigned char *= )tdb->map_ptr)) { - break; - } - } - if (tlock->hash =3D=3D tdb->header.hash_size) { - continue; - } - } else { - if (ofs_read(tdb, TDB_HASH_TOP(tlock->hash), &off) =3D=3D 0 && - off =3D=3D 0) { - continue; - } - } - } - - if (tdb_lock(tdb, tlock->hash, F_WRLCK) =3D=3D -1) - return -1; - - /* No previous record? Start at top of chain. */ - if (!tlock->off) { - if (ofs_read(tdb, TDB_HASH_TOP(tlock->hash), - &tlock->off) =3D=3D -1) - goto fail; - } else { - /* Otherwise unlock the previous record. */ - if (unlock_record(tdb, tlock->off) !=3D 0) - goto fail; - } - - if (want_next) { - /* We have offset of old record: grab next */ - if (rec_read(tdb, tlock->off, rec) =3D=3D -1) - goto fail; - tlock->off =3D rec->next; - } - - /* Iterate through chain */ - while( tlock->off) { - tdb_off current; - if (rec_read(tdb, tlock->off, rec) =3D=3D -1) - goto fail; - - /* Detect infinite loops. From "Shlomi Yaakobovich" = . */ - if (tlock->off =3D=3D rec->next) { - TDB_LOG((tdb, 0, "tdb_next_lock: loop detected.\n")); - goto fail; - } - - if (!TDB_DEAD(rec)) { - /* Woohoo: we found one! */ - if (lock_record(tdb, tlock->off) !=3D 0) - goto fail; - return tlock->off; - } - - /* Try to clean dead ones from old traverses */ - current =3D tlock->off; - tlock->off =3D rec->next; - if (!tdb->read_only &&=20 - do_delete(tdb, current, rec) !=3D 0) - goto fail; - } - tdb_unlock(tdb, tlock->hash, F_WRLCK); - want_next =3D 0; - } - /* We finished iteration without finding anything */ - return TDB_ERRCODE(TDB_SUCCESS, 0); - - fail: - tlock->off =3D 0; - if (tdb_unlock(tdb, tlock->hash, F_WRLCK) !=3D 0) - TDB_LOG((tdb, 0, "tdb_next_lock: On error unlock failed!\n")); - return -1; -} - -/* traverse the entire database - calling fn(tdb, key, data) on each eleme= nt. - return -1 on error or the record count traversed - if fn is NULL then it is not called - a non-zero return value from fn() indicates that the traversal should s= top - */ -int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *private) -{ - TDB_DATA key, dbuf; - struct list_struct rec; - struct tdb_traverse_lock tl =3D { NULL, 0, 0 }; - int ret, count =3D 0; - - /* This was in the initializaton, above, but the IRIX compiler - * did not like it. crh - */ - tl.next =3D tdb->travlocks.next; - - /* fcntl locks don't stack: beware traverse inside traverse */ - tdb->travlocks.next =3D &tl; - - /* tdb_next_lock places locks on the record returned, and its chain */ - while ((ret =3D tdb_next_lock(tdb, &tl, &rec)) > 0) { - count++; - /* now read the full record */ - key.dptr =3D tdb_alloc_read(tdb, tl.off + sizeof(rec),=20 - rec.key_len + rec.data_len); - if (!key.dptr) { - ret =3D -1; - if (tdb_unlock(tdb, tl.hash, F_WRLCK) !=3D 0) - goto out; - if (unlock_record(tdb, tl.off) !=3D 0) - TDB_LOG((tdb, 0, "tdb_traverse: key.dptr =3D=3D NULL and unlock_record= failed!\n")); - goto out; - } - key.dsize =3D rec.key_len; - dbuf.dptr =3D key.dptr + rec.key_len; - dbuf.dsize =3D rec.data_len; - - /* Drop chain lock, call out */ - if (tdb_unlock(tdb, tl.hash, F_WRLCK) !=3D 0) { - ret =3D -1; - goto out; - } - if (fn && fn(tdb, key, dbuf, private)) { - /* They want us to terminate traversal */ - ret =3D count; - if (unlock_record(tdb, tl.off) !=3D 0) { - TDB_LOG((tdb, 0, "tdb_traverse: unlock_record failed!\n")); - ret =3D -1; - } - tdb->travlocks.next =3D tl.next; - SAFE_FREE(key.dptr); - return count; - } - SAFE_FREE(key.dptr); - } -out: - tdb->travlocks.next =3D tl.next; - if (ret < 0) - return -1; - else - return count; -} - -/* find the first entry in the database and return its key */ -TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb) -{ - TDB_DATA key; - struct list_struct rec; - - /* release any old lock */ - if (unlock_record(tdb, tdb->travlocks.off) !=3D 0) - return tdb_null; - tdb->travlocks.off =3D tdb->travlocks.hash =3D 0; - - if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <=3D 0) - return tdb_null; - /* now read the key */ - key.dsize =3D rec.key_len; - key.dptr =3Dtdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize); - if (tdb_unlock(tdb, BUCKET(tdb->travlocks.hash), F_WRLCK) !=3D 0) - TDB_LOG((tdb, 0, "tdb_firstkey: error occurred while tdb_unlocking!\n")); - return key; -} - -/* find the next entry in the database, returning its key */ -TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA oldkey) -{ - uint32_t oldhash; - TDB_DATA key =3D tdb_null; - struct list_struct rec; - char *k =3D NULL; - - /* Is locked key the old key? If so, traverse will be reliable. */ - if (tdb->travlocks.off) { - if (tdb_lock(tdb,tdb->travlocks.hash,F_WRLCK)) - return tdb_null; - if (rec_read(tdb, tdb->travlocks.off, &rec) =3D=3D -1 - || !(k =3D tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec), - rec.key_len)) - || memcmp(k, oldkey.dptr, oldkey.dsize) !=3D 0) { - /* No, it wasn't: unlock it and start from scratch */ - if (unlock_record(tdb, tdb->travlocks.off) !=3D 0) - return tdb_null; - if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) !=3D 0) - return tdb_null; - tdb->travlocks.off =3D 0; - } - - SAFE_FREE(k); - } - - if (!tdb->travlocks.off) { - /* No previous element: do normal find, and lock record */ - tdb->travlocks.off =3D tdb_find_lock_hash(tdb, oldkey, tdb->hash_fn(&old= key), F_WRLCK, &rec); - if (!tdb->travlocks.off) - return tdb_null; - tdb->travlocks.hash =3D BUCKET(rec.full_hash); - if (lock_record(tdb, tdb->travlocks.off) !=3D 0) { - TDB_LOG((tdb, 0, "tdb_nextkey: lock_record failed (%s)!\n", strerror(er= rno))); - return tdb_null; - } - } - oldhash =3D tdb->travlocks.hash; - - /* Grab next record: locks chain and returned record, - unlocks old record */ - if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) { - key.dsize =3D rec.key_len; - key.dptr =3D tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec), - key.dsize); - /* Unlock the chain of this new record */ - if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) !=3D 0) - TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n")); - } - /* Unlock the chain of old record */ - if (tdb_unlock(tdb, BUCKET(oldhash), F_WRLCK) !=3D 0) - TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n")); - return key; -} - -/* delete an entry in the database given a key */ -static int tdb_delete_hash(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash) -{ - tdb_off rec_ptr; - struct list_struct rec; - int ret; - - if (!(rec_ptr =3D tdb_find_lock_hash(tdb, key, hash, F_WRLCK, &rec))) - return -1; - ret =3D do_delete(tdb, rec_ptr, &rec); - if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) !=3D 0) - TDB_LOG((tdb, 0, "tdb_delete: WARNING tdb_unlock failed!\n")); - return ret; -} - -int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key) -{ - uint32_t hash =3D tdb->hash_fn(&key); - return tdb_delete_hash(tdb, key, hash); -} - -/* store an element in the database, replacing any existing element - with the same key=20 - - return 0 on success, -1 on failure -*/ -int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) -{ - struct list_struct rec; - uint32_t hash; - tdb_off rec_ptr; - char *p =3D NULL; - int ret =3D 0; - - /* find which hash bucket it is in */ - hash =3D tdb->hash_fn(&key); - if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) =3D=3D -1) - return -1; - - /* check for it existing, on insert. */ - if (flag =3D=3D TDB_INSERT) { - if (tdb_exists_hash(tdb, key, hash)) { - tdb->ecode =3D TDB_ERR_EXISTS; - goto fail; - } - } else { - /* first try in-place update, on modify or replace. */ - if (tdb_update_hash(tdb, key, hash, dbuf) =3D=3D 0) - goto out; - if (tdb->ecode =3D=3D TDB_ERR_NOEXIST && - flag =3D=3D TDB_MODIFY) { - /* if the record doesn't exist and we are in TDB_MODIFY mode then - we should fail the store */ - goto fail; - } - } - /* reset the error code potentially set by the tdb_update() */ - tdb->ecode =3D TDB_SUCCESS; - - /* delete any existing record - if it doesn't exist we don't - care. Doing this first reduces fragmentation, and avoids - coalescing with `allocated' block before it's updated. */ - if (flag !=3D TDB_INSERT) - tdb_delete_hash(tdb, key, hash); - - /* Copy key+value *before* allocating free space in case malloc - fails and we are left with a dead spot in the tdb. */ - - if (!(p =3D (char *)talloc_size(tdb, key.dsize + dbuf.dsize))) { - tdb->ecode =3D TDB_ERR_OOM; - goto fail; - } - - memcpy(p, key.dptr, key.dsize); - if (dbuf.dsize) - memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize); - - /* we have to allocate some space */ - if (!(rec_ptr =3D tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec))) - goto fail; - - /* Read hash top into next ptr */ - if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) =3D=3D -1) - goto fail; - - rec.key_len =3D key.dsize; - rec.data_len =3D dbuf.dsize; - rec.full_hash =3D hash; - rec.magic =3D TDB_MAGIC; - - /* write out and point the top of the hash chain at it */ - if (rec_write(tdb, rec_ptr, &rec) =3D=3D -1 - || tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)=3D=3D= -1 - || ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) =3D=3D -1) { - /* Need to tdb_unallocate() here */ - goto fail; - } - out: - SAFE_FREE(p);=20 - tdb_unlock(tdb, BUCKET(hash), F_WRLCK); - return ret; -fail: - ret =3D -1; - goto out; -} - -static int tdb_already_open(dev_t device, - ino_t ino) -{ - TDB_CONTEXT *i; -=09 - for (i =3D tdbs; i; i =3D i->next) { - if (i->device =3D=3D device && i->inode =3D=3D ino) { - return 1; - } - } - - return 0; -} - -/* a default logging function */ -static void null_log_fn(TDB_CONTEXT *tdb __attribute__((unused)), - int level __attribute__((unused)), - const char *fmt __attribute__((unused)), ...) -{ -} - - -TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags, - int open_flags, mode_t mode, - tdb_log_func log_fn, - tdb_hash_func hash_fn) -{ - TDB_CONTEXT *tdb; - struct stat st; - int rev =3D 0, locked =3D 0; - uint8_t *vp; - uint32_t vertest; - - if (!(tdb =3D talloc_zero(name, TDB_CONTEXT))) { - /* Can't log this */ - errno =3D ENOMEM; - goto fail; - } - tdb->fd =3D -1; - tdb->name =3D NULL; - tdb->map_ptr =3D NULL; - tdb->flags =3D tdb_flags; - tdb->open_flags =3D open_flags; - tdb->log_fn =3D log_fn?log_fn:null_log_fn; - tdb->hash_fn =3D hash_fn ? hash_fn : default_tdb_hash; - - if ((open_flags & O_ACCMODE) =3D=3D O_WRONLY) { - TDB_LOG((tdb, 0, "tdb_open_ex: can't open tdb %s write-only\n", - name)); - errno =3D EINVAL; - goto fail; - } -=09 - if (hash_size =3D=3D 0) - hash_size =3D DEFAULT_HASH_SIZE; - if ((open_flags & O_ACCMODE) =3D=3D O_RDONLY) { - tdb->read_only =3D 1; - /* read only databases don't do locking or clear if first */ - tdb->flags |=3D TDB_NOLOCK; - tdb->flags &=3D ~TDB_CLEAR_IF_FIRST; - } - - /* internal databases don't mmap or lock, and start off cleared */ - if (tdb->flags & TDB_INTERNAL) { - tdb->flags |=3D (TDB_NOLOCK | TDB_NOMMAP); - tdb->flags &=3D ~TDB_CLEAR_IF_FIRST; - if (tdb_new_database(tdb, hash_size) !=3D 0) { - TDB_LOG((tdb, 0, "tdb_open_ex: tdb_new_database failed!")); - goto fail; - } - goto internal; - } - - if ((tdb->fd =3D open(name, open_flags, mode)) =3D=3D -1) { - TDB_LOG((tdb, 5, "tdb_open_ex: could not open file %s: %s\n", - name, strerror(errno))); - goto fail; /* errno set by open(2) */ - } - - /* ensure there is only one process initialising at once */ - if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_open_ex: failed to get global lock on %s: %s\n", - name, strerror(errno))); - goto fail; /* errno set by tdb_brlock */ - } - - /* we need to zero database if we are the only one with it open */ - if ((tdb_flags & TDB_CLEAR_IF_FIRST) && - (locked =3D (tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0) =3D=3D 0)= )) { - open_flags |=3D O_CREAT; - if (ftruncate(tdb->fd, 0) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_open_ex: " - "failed to truncate %s: %s\n", - name, strerror(errno))); - goto fail; /* errno set by ftruncate */ - } - } - - if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) !=3D sizeof(tdb->hea= der) - || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) !=3D 0 - || (tdb->header.version !=3D TDB_VERSION - && !(rev =3D (tdb->header.version=3D=3DTDB_BYTEREV(TDB_VERSION))))) { - /* its not a valid database - possibly initialise it */ - if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) =3D=3D -= 1) { - errno =3D EIO; /* ie bad format or something */ - goto fail; - } - rev =3D (tdb->flags & TDB_CONVERT); - } - vp =3D (uint8_t *)&tdb->header.version; - vertest =3D (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) | - (((uint32_t)vp[2]) << 8) | (uint32_t)vp[3]; - tdb->flags |=3D (vertest=3D=3DTDB_VERSION) ? TDB_BIGENDIAN : 0; - if (!rev) - tdb->flags &=3D ~TDB_CONVERT; - else { - tdb->flags |=3D TDB_CONVERT; - convert(&tdb->header, sizeof(tdb->header)); - } - if (fstat(tdb->fd, &st) =3D=3D -1) - goto fail; - - /* Is it already in the open list? If so, fail. */ - if (tdb_already_open(st.st_dev, st.st_ino)) { - TDB_LOG((tdb, 2, "tdb_open_ex: " - "%s (%d,%d) is already open in this process\n", - name, (int)st.st_dev, (int)st.st_ino)); - errno =3D EBUSY; - goto fail; - } - - if (!(tdb->name =3D (char *)talloc_strdup(tdb, name))) { - errno =3D ENOMEM; - goto fail; - } - - tdb->map_size =3D st.st_size; - tdb->device =3D st.st_dev; - tdb->inode =3D st.st_ino; - tdb->locked =3D talloc_zero_array(tdb, struct tdb_lock_type, - tdb->header.hash_size+1); - if (!tdb->locked) { - TDB_LOG((tdb, 2, "tdb_open_ex: " - "failed to allocate lock structure for %s\n", - name)); - errno =3D ENOMEM; - goto fail; - } - tdb_mmap(tdb); - if (locked) { - if (tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0) =3D=3D -1) { - TDB_LOG((tdb, 0, "tdb_open_ex: " - "failed to take ACTIVE_LOCK on %s: %s\n", - name, strerror(errno))); - goto fail; - } - - } - - /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if - we didn't get the initial exclusive lock as we need to let all other - users know we're using it. */ - - if (tdb_flags & TDB_CLEAR_IF_FIRST) { - /* leave this lock in place to indicate it's in use */ - if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) =3D=3D -1) - goto fail; - } - - - internal: - /* Internal (memory-only) databases skip all the code above to - * do with disk files, and resume here by releasing their - * global lock and hooking into the active list. */ - if (tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0) =3D=3D -1) - goto fail; - tdb->next =3D tdbs; - tdbs =3D tdb; - return tdb; - - fail: - { int save_errno =3D errno; - - if (!tdb) - return NULL; -=09 - if (tdb->map_ptr) { - if (tdb->flags & TDB_INTERNAL) - SAFE_FREE(tdb->map_ptr); - else - tdb_munmap(tdb); - } - SAFE_FREE(tdb->name); - if (tdb->fd !=3D -1) - if (close(tdb->fd) !=3D 0) - TDB_LOG((tdb, 5, "tdb_open_ex: failed to close tdb->fd on error!\n")); - SAFE_FREE(tdb->locked); - SAFE_FREE(tdb); - errno =3D save_errno; - return NULL; - } -} - -/** - * Close a database. - * - * @returns -1 for error; 0 for success. - **/ -int tdb_close(TDB_CONTEXT *tdb) -{ - TDB_CONTEXT **i; - int ret =3D 0; - - if (tdb->map_ptr) { - if (tdb->flags & TDB_INTERNAL) - SAFE_FREE(tdb->map_ptr); - else - tdb_munmap(tdb); - } - SAFE_FREE(tdb->name); - if (tdb->fd !=3D -1) - ret =3D close(tdb->fd); - SAFE_FREE(tdb->locked); - - /* Remove from contexts list */ - for (i =3D &tdbs; *i; i =3D &(*i)->next) { - if (*i =3D=3D tdb) { - *i =3D tdb->next; - break; - } - } - - memset(tdb, 0, sizeof(*tdb)); - SAFE_FREE(tdb); - - return ret; -} diff --git a/tools/xenstore/tdb.h b/tools/xenstore/tdb.h deleted file mode 100644 index ce3c7339f8..0000000000 --- a/tools/xenstore/tdb.h +++ /dev/null @@ -1,132 +0,0 @@ -#ifndef __TDB_H__ -#define __TDB_H__ - -#include "utils.h" - -/*=20 - Unix SMB/CIFS implementation. - - trivial database library - - Copyright (C) Andrew Tridgell 1999-2004 - =20 - ** NOTE! The following LGPL license applies to the tdb - ** library. This does NOT imply that all of Samba is released - ** under the LGPL - =20 - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; If not, see . -*/ - -#ifdef __cplusplus -extern "C" { -#endif - - -/* flags to tdb_store() */ -#define TDB_REPLACE 1 -#define TDB_INSERT 2 -#define TDB_MODIFY 3 - -/* flags for tdb_open() */ -#define TDB_DEFAULT 0 /* just a readability place holder */ -#define TDB_CLEAR_IF_FIRST 1 -#define TDB_INTERNAL 2 /* don't store on disk */ -#define TDB_NOLOCK 4 /* don't do any locking */ -#define TDB_NOMMAP 8 /* don't use mmap */ -#define TDB_CONVERT 16 /* convert endian (internal use) */ -#define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */ - -#define TDB_ERRCODE(code, ret) ((tdb->ecode =3D (code)), ret) - -/* error codes */ -enum TDB_ERROR {TDB_SUCCESS=3D0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK= ,=20 - TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT, - TDB_ERR_NOEXIST}; - -#ifndef uint32_t -#define uint32_t unsigned -#endif - -typedef struct TDB_DATA { - char *dptr; - size_t dsize; -} TDB_DATA; - -typedef uint32_t tdb_len; -typedef uint32_t tdb_off; - -/* this is stored at the front of every database */ -struct tdb_header { - char magic_food[32]; /* for /etc/magic */ - uint32_t version; /* version of the code */ - uint32_t hash_size; /* number of hash entries */ - tdb_off rwlocks; - tdb_off reserved[31]; -}; - -struct tdb_lock_type { - uint32_t count; - uint32_t ltype; -}; - -struct tdb_traverse_lock { - struct tdb_traverse_lock *next; - uint32_t off; - uint32_t hash; -}; - -/* this is the context structure that is returned from a db open */ -typedef struct tdb_context { - char *name; /* the name of the database */ - void *map_ptr; /* where it is currently mapped */ - int fd; /* open file descriptor for the database */ - tdb_len map_size; /* how much space has been mapped */ - int read_only; /* opened read-only */ - struct tdb_lock_type *locked; /* array of chain locks */ - enum TDB_ERROR ecode; /* error code for last tdb error */ - struct tdb_header header; /* a cached copy of the header */ - uint32_t flags; /* the flags passed to tdb_open */ - struct tdb_traverse_lock travlocks; /* current traversal locks */ - struct tdb_context *next; /* all tdbs to avoid multiple opens */ - dev_t device; /* uniquely identifies this tdb */ - ino_t inode; /* uniquely identifies this tdb */ - void (*log_fn)(struct tdb_context *tdb, int level, const char *, ...) PRI= NTF_ATTRIBUTE(3,4); /* logging function */ - uint32_t (*hash_fn)(TDB_DATA *key); - int open_flags; /* flags used in the open - needed by reopen */ -} TDB_CONTEXT; - -typedef int (*tdb_traverse_func)(TDB_CONTEXT *, TDB_DATA, TDB_DATA, void *= ); -typedef void (*tdb_log_func)(TDB_CONTEXT *, int , const char *, ...); -typedef uint32_t (*tdb_hash_func)(TDB_DATA *key); - -TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags, - int open_flags, mode_t mode, - tdb_log_func log_fn, - tdb_hash_func hash_fn); - -enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb); -const char *tdb_errorstr(TDB_CONTEXT *tdb); -TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key); -int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key); -int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag); -int tdb_close(TDB_CONTEXT *tdb); -TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb); -TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key); -int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *); - -#ifdef __cplusplus -} -#endif - -#endif /* tdb.h */ --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196926; cv=none; d=zohomail.com; s=zohoarc; b=m77W1vrjyCPmCiGpleNELDWvWBd7bpGGCuLHH+l9niSrNd9J3RgLf32wpT/oROI8qJ6YWbEn13pVocV63aXOh/RP5chfyO9F7C4YCpf99ffdUBv7ejrE+ltuoNLWAe+kqIlLMsXtDpfRitoekZ+Fps4mPPC50A3D5C5wiSXbDJQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196926; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=BG6qvkTJomUkTWnj+OlsG8s/EdIwcHngnOtZ7JpxP6M=; b=gvXRN/9XuCko2HAuyLUZOVYbNfA90eBYq/4wAiExbdE8yxWC9a3HlPapV1U6SEck37qnIeFr1enuTpZ3+6As31MmHjL2XO0x98W2B4W7MW2StjJft6IRxYjGAnu2pf4YKtHz1A78ioOlVNDKmfM4Schpg7zSp/7SDKBcoV1Q1nA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 169019692687719.684688537766306; Mon, 24 Jul 2023 04:08:46 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568771.888767 (Exim 4.92) (envelope-from ) id 1qNtQH-0002Aa-DE; Mon, 24 Jul 2023 11:08:17 +0000 Received: by outflank-mailman (output) from mailman id 568771.888767; Mon, 24 Jul 2023 11:08:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQH-00027s-0l; Mon, 24 Jul 2023 11:08:17 +0000 Received: by outflank-mailman (input) for mailman id 568771; Mon, 24 Jul 2023 11:08:14 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMD-0008WC-AG for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:05 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [2001:67c:2178:6::1c]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id ccdf6191-2a11-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 13:04:03 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 6C06922988; Mon, 24 Jul 2023 11:04:03 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 319AC13476; Mon, 24 Jul 2023 11:04:03 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id z6nMCqNavmTZYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:03 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: ccdf6191-2a11-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196643; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BG6qvkTJomUkTWnj+OlsG8s/EdIwcHngnOtZ7JpxP6M=; b=Tt5SMJfeLWx1BWFx36kasi9KVt1fCKh2DDYH4qPyw9rsaeMC0iwQbF9BEmprvhdmPwuTab olOZstmmimgT1yOlsx7PVzRdHJrqVzp3S+w+4KimH5Vjio4XJChW2EZ9+c5TURsWW2kuld byy8DQom6qstPLNENEiGCu1SSI+67Es= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD , Julien Grall Subject: [PATCH v3 13/25] tools/xenstore: let db_delete() return void Date: Mon, 24 Jul 2023 13:02:35 +0200 Message-Id: <20230724110247.10520-14-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196927942100009 Content-Type: text/plain; charset="utf-8" db_delete() only ever is returning 0. Switch it to return void and remove all the error handling dealing wit a non-zero return value. Suggested-by: Julien Grall Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V2: - new patch --- tools/xenstore/xenstored_core.c | 11 ++++------- tools/xenstore/xenstored_core.h | 4 ++-- tools/xenstore/xenstored_transaction.c | 14 +++++--------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 2b94392fd4..a08962c3ea 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -677,8 +677,8 @@ int db_write(struct connection *conn, const char *db_na= me, void *data, return 0; } =20 -int db_delete(struct connection *conn, const char *name, - struct node_account_data *acc) +void db_delete(struct connection *conn, const char *name, + struct node_account_data *acc) { struct node_account_data tmp_acc; unsigned int domid; @@ -698,8 +698,6 @@ int db_delete(struct connection *conn, const char *name, domain_memory_add_nochk(conn, domid, -acc->memory - strlen(name)); } - - return 0; } =20 /* @@ -1670,9 +1668,8 @@ static int delnode_sub(const void *ctx, struct connec= tion *conn, if (domain_nbentry_dec(conn, get_node_owner(node))) return WALK_TREE_ERROR_STOP; =20 - /* In case of error stop the walk. */ - if (!ret && db_delete(conn, db_name, &node->acc)) - return WALK_TREE_ERROR_STOP; + if (!ret) + db_delete(conn, db_name, &node->acc); =20 /* * Fire the watches now, when we can still see the node permissions. diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index ce40c61f44..e1aeb4aecd 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -366,8 +366,8 @@ struct xs_tdb_record_hdr *db_fetch(const char *db_name,= size_t *size); int db_write(struct connection *conn, const char *db_name, void *data, size_t size, struct node_account_data *acc, enum write_node_mode mode, bool no_quota_check); -int db_delete(struct connection *conn, const char *name, - struct node_account_data *acc); +void db_delete(struct connection *conn, const char *name, + struct node_account_data *acc); =20 void conn_free_buffered_data(struct connection *conn); =20 diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 378fe79763..fbcea3663e 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -377,10 +377,8 @@ static int finalize_transaction(struct connection *con= n, =20 /* Entries for unmodified nodes can be removed early. */ if (!i->modified) { - if (i->ta_node) { - if (db_delete(conn, i->trans_name, NULL)) - return EIO; - } + if (i->ta_node) + db_delete(conn, i->trans_name, NULL); list_del(&i->list); talloc_free(i); } @@ -397,8 +395,7 @@ static int finalize_transaction(struct connection *conn, ? NODE_CREATE : NODE_MODIFY; *is_corrupt |=3D db_write(conn, i->node, hdr, size, NULL, mode, true); - if (db_delete(conn, i->trans_name, NULL)) - *is_corrupt =3D true; + db_delete(conn, i->trans_name, NULL); } else { *is_corrupt =3D true; } @@ -408,9 +405,8 @@ static int finalize_transaction(struct connection *conn, * in this transaction will have no generation * information stored. */ - *is_corrupt |=3D (i->generation =3D=3D NO_GENERATION) - ? false - : db_delete(conn, i->node, NULL); + if (i->generation !=3D NO_GENERATION) + db_delete(conn, i->node, NULL); } if (i->fire_watch) fire_watches(conn, trans, i->node, NULL, i->watch_exact, --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196904; cv=none; d=zohomail.com; s=zohoarc; b=VFY94g7zpy9je3M2VeGfistEopF1DRE6fvFWciZfdSRgGToOy/Ck/Pq3hTla2o6RJ65fEbkSUhnxXNQaqwM160BXSP7rJRLc7at+SqYSFAV1SODwfBR+kX2nYtCP+jx2JYuGEZpSSwy7nArmesG8/lJMu+3/mgKAVMktDWY95Qw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196904; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ybmSsNxeUNBDfbwoafuFdaqkBsZjOSyQ+1w8Dv8MPBA=; b=lJBLanRQiIDFY9EmuPwLLOnG8OzJkk4b6qoJe8A9YHY0i9FuxXUxta4YJFQg17NxfV97574HLSHw5KxbmfAjayynDJmI8mAUx9HrcwTHHlWSK7dK/1YvvQaYRWi2L2deu+32FMB+uSmy/dTzD/NoRMhRVGdLhCB4+kuYNhd9yO0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196904445769.021500259649; Mon, 24 Jul 2023 04:08:24 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568761.888723 (Exim 4.92) (envelope-from ) id 1qNtQ5-0000eX-4A; Mon, 24 Jul 2023 11:08:05 +0000 Received: by outflank-mailman (output) from mailman id 568761.888723; Mon, 24 Jul 2023 11:08:05 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQ4-0000eM-WD; Mon, 24 Jul 2023 11:08:05 +0000 Received: by outflank-mailman (input) for mailman id 568761; Mon, 24 Jul 2023 11:08:03 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMH-0000KR-Vb for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:09 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id d05cbdf6-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:04:09 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 45ABF20697; Mon, 24 Jul 2023 11:04:09 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0C7D713476; Mon, 24 Jul 2023 11:04:09 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id TaAkAalavmTxYQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:09 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: d05cbdf6-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196649; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ybmSsNxeUNBDfbwoafuFdaqkBsZjOSyQ+1w8Dv8MPBA=; b=A/+QxO4eCzEmjzHMxT+ihNVdWzxr23vis3Q0qbpNKOW1152yipSC9ZR4tXOG8BWTybH0aY 9nrT9YRxvzWTGdXrw/DW0rYz8g8aZgcrEFmTjFcRtaG5ChKRzaWJ4z53RKkeFVOrvBxZOm 5CUF2So2ScZF3HnjW2o241v+gK5OiaU= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 14/25] tools/xenstore: change talloc_free() to take a const pointer Date: Mon, 24 Jul 2023 13:02:36 +0200 Message-Id: <20230724110247.10520-15-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196905822100001 Content-Type: text/plain; charset="utf-8" With talloc_free() and related functions not taking a ponter to const it is tedious to use the const attribute for talloc()-ed memory in many cases. Change the related prototypes to use "const void *" instead of "void *". Signed-off-by: Juergen Gross --- V3: - new patch --- tools/xenstore/talloc.c | 8 ++++---- tools/xenstore/talloc.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/xenstore/talloc.c b/tools/xenstore/talloc.c index 23c3a23b19..4f08dbec59 100644 --- a/tools/xenstore/talloc.c +++ b/tools/xenstore/talloc.c @@ -319,7 +319,7 @@ static int talloc_unreference(const void *context, cons= t void *ptr) remove a specific parent context from a pointer. This is a more controlled varient of talloc_free() */ -int talloc_unlink(const void *context, void *ptr) +int talloc_unlink(const void *context, const void *ptr) { struct talloc_chunk *tc_p, *new_p; void *new_parent; @@ -499,7 +499,7 @@ void *talloc_init(const char *fmt, ...) should probably not be used in new code. It's in here to keep the talloc code consistent across Samba 3 and 4. */ -static void talloc_free_children(void *ptr) +static void talloc_free_children(const void *ptr) { struct talloc_chunk *tc; =20 @@ -539,7 +539,7 @@ static void talloc_free_children(void *ptr) will not be freed if the ref_count is > 1 or the destructor (if any) returns non-zero */ -int talloc_free(void *ptr) +int talloc_free(const void *ptr) { int saved_errno =3D errno; struct talloc_chunk *tc; @@ -571,7 +571,7 @@ int talloc_free(void *ptr) goto err; } tc->destructor =3D (talloc_destructor_t)-1; - if (d(ptr) =3D=3D -1) { + if (d((void *)ptr) =3D=3D -1) { tc->destructor =3D d; goto err; } diff --git a/tools/xenstore/talloc.h b/tools/xenstore/talloc.h index 518fcac151..32cee63d4d 100644 --- a/tools/xenstore/talloc.h +++ b/tools/xenstore/talloc.h @@ -92,7 +92,7 @@ void *_talloc(const void *context, size_t size); void talloc_set_destructor(const void *ptr, int (*destructor)(void *)); void talloc_increase_ref_count(const void *ptr); void *talloc_reference(const void *context, const void *ptr); -int talloc_unlink(const void *context, void *ptr); +int talloc_unlink(const void *context, const void *ptr); void talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBU= TE(2,3); void talloc_set_name_const(const void *ptr, const char *name); void *talloc_named(const void *context, size_t size,=20 @@ -103,7 +103,7 @@ void *talloc_check_name(const void *ptr, const char *na= me); void talloc_report_depth(const void *ptr, FILE *f, int depth); void *talloc_parent(const void *ptr); void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2); -int talloc_free(void *ptr); +int talloc_free(const void *ptr); void *_talloc_realloc(const void *context, void *ptr, size_t size, const c= har *name); void *talloc_steal(const void *new_ctx, const void *ptr); off_t talloc_total_size(const void *ptr); --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196874; cv=none; d=zohomail.com; s=zohoarc; b=F7uO4luNsnNMdpXQhw6bLw5u22xTdef8tsv61MnFDIl9gWEarxtc2KWRZ6mWA69VePuxIH+ccpMrZMOw6mY98ZQD30gU8JV9kAF75VBDW2v35oVRI3HQKhx3jf/063VFs4kV5pTGdARkdJAk2neJX+gCzSjEc6Fbad+pYok4JX4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196874; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=chevwYuBSR7V/TZ3mWJmpGZB7vbjU7lZFq1Lv4K/AXA=; b=buYd+SuQCuUVy3xlPfGpF/mM+Um3lXbpx2mcS2yv/xfb1DqwcO2iuqQ0eo4Ehj2f+LbYP0yMTv2Kvkdq4F2Og1dmEHFGzrf35ixirex71Orgwy7435noOPIr1oWo7ITrcsYZBV+ULjJojtjSaXc8RxgF1GfjtpaPbBmbYlmQjao= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196874769398.21436177226053; Mon, 24 Jul 2023 04:07:54 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568754.888693 (Exim 4.92) (envelope-from ) id 1qNtPg-0007gf-8W; Mon, 24 Jul 2023 11:07:40 +0000 Received: by outflank-mailman (output) from mailman id 568754.888693; Mon, 24 Jul 2023 11:07:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtPg-0007g3-5F; Mon, 24 Jul 2023 11:07:40 +0000 Received: by outflank-mailman (input) for mailman id 568754; Mon, 24 Jul 2023 11:07:38 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMN-0000KR-QI for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:15 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [2001:67c:2178:6::1c]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id d3c20f8b-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:04:15 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 0111522988; Mon, 24 Jul 2023 11:04:15 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B3DDC13476; Mon, 24 Jul 2023 11:04:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id L474Ka5avmT9YQAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:14 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: d3c20f8b-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196655; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=chevwYuBSR7V/TZ3mWJmpGZB7vbjU7lZFq1Lv4K/AXA=; b=YZYVabAiVdDjvCZJmBZB5BX/DuaLdGTjVphEZ2IKaqs7qdf4zaR092HvBx9z4v9LttF6vA qb417PToMB/XOz8b1R85JC8dhfnBBUVseldkoLyXnly+pygjRGdNErFnstIZoKdACjlFLS cHJiD8tT7v2UG4FOxDMpz8N5jRPWopg= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 15/25] tools/xenstore: make data parameter of db_write() const Date: Mon, 24 Jul 2023 13:02:37 +0200 Message-Id: <20230724110247.10520-16-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196876046100001 Content-Type: text/plain; charset="utf-8" db_write() doesn't change the data written to the data base, so the data parameter can be const. Signed-off-by: Juergen Gross --- V3: - new patch --- tools/xenstore/xenstored_core.c | 4 ++-- tools/xenstore/xenstored_core.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index a08962c3ea..9f88914149 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -611,11 +611,11 @@ static unsigned int get_acc_domid(struct connection *= conn, const char *name, return (!conn || name[0] =3D=3D '/' || name[0] =3D=3D '@') ? domid : conn= ->id; } =20 -int db_write(struct connection *conn, const char *db_name, void *data, +int db_write(struct connection *conn, const char *db_name, const void *dat= a, size_t size, struct node_account_data *acc, enum write_node_mode mode, bool no_quota_check) { - struct xs_tdb_record_hdr *hdr =3D data; + const struct xs_tdb_record_hdr *hdr =3D data; struct node_account_data old_acc =3D {}; unsigned int old_domid, new_domid; size_t name_len =3D strlen(db_name); diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index e1aeb4aecd..1aa3cc0936 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -363,7 +363,7 @@ int remember_string(struct hashtable *hash, const char = *str); =20 /* Data base access functions. */ struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size); -int db_write(struct connection *conn, const char *db_name, void *data, +int db_write(struct connection *conn, const char *db_name, const void *dat= a, size_t size, struct node_account_data *acc, enum write_node_mode mode, bool no_quota_check); void db_delete(struct connection *conn, const char *name, --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196910; cv=none; d=zohomail.com; s=zohoarc; b=QhIa60fAFg5HxFcvz14EV0/xcw3Bq5GFWaQPgTaS8etu4rS/7UUlCCvOg4yZG+Lj7LjqK6AgqPIi0uWmwtgYXup1li1XiKb0FYtmpnjFbUpQF+GoeRjrSUz9CmWTxMOA8AmIBXX16+5Xc7WNrXw276I2Mk0jhjtx3c5OngDZ2KM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196910; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HAhSQtZ7+yH9+PPmPI+Vq3s5YrV0tpckJJuWvE/UBsg=; b=BCDKn960/KJCPCFB7MSKLKoEW0chLBfPIakVbtoPjrDSd36yqhM7prtFg7Tvk05QTwYX9KNsEvT4p6kDg0Fv2KcvBPDmclEN7e2ag59MFlwZdHWzReNje39ih31w0vkb2p9znIRGbHJb3vFG3JvltRQeFtsX18MTT7/jmhIEsiw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196909978687.9504934291369; Mon, 24 Jul 2023 04:08:29 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568766.888744 (Exim 4.92) (envelope-from ) id 1qNtQF-0001UP-Bc; Mon, 24 Jul 2023 11:08:15 +0000 Received: by outflank-mailman (output) from mailman id 568766.888744; Mon, 24 Jul 2023 11:08:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQE-0001RH-VV; Mon, 24 Jul 2023 11:08:14 +0000 Received: by outflank-mailman (input) for mailman id 568766; Mon, 24 Jul 2023 11:08:13 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMT-0000KR-Eq for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:21 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id d712c6f2-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:04:20 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 8A28722988; Mon, 24 Jul 2023 11:04:20 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 5D52213476; Mon, 24 Jul 2023 11:04:20 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id o5lqFbRavmQHYgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:20 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: d712c6f2-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196660; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HAhSQtZ7+yH9+PPmPI+Vq3s5YrV0tpckJJuWvE/UBsg=; b=GjH1NJNhqgvJ+tD/UjOJTRHgxTCdUkmTezfUL/GyNM37sXtPZN00PK5Z6iwSH0FduCuBMT AwNjyqRLj87xxI8p4kFuXdO+GS7736DSaYuuH1Z49zJTj/eVjBey3xJ4OHtfUOikNgHztY YbAk6+ZoeOra1YSlM6rc8+b4/I3kii8= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 16/25] tools/xenstore: move copying of node data out of db_fetch() Date: Mon, 24 Jul 2023 13:02:38 +0200 Message-Id: <20230724110247.10520-17-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196911927100001 Content-Type: text/plain; charset="utf-8" Today the node data is copied in db_fetch() on each data base read in order to avoid accidental data base modifications when working on a node. read_node() is the only caller of db_fetch() which isn't freeing the returned data area immediately after using it. The other callers don't modify the returned data, so they don't need the data to be copied. Move copying of the data into read_node(), resulting in a speedup of the other callers due to no memory allocation and no copying being needed anymore. This allows to let db_fetch() return a pointer to const data. As db_fetch() can't return any error other than ENOENT now, error handling for the callers can be simplified. Signed-off-by: Juergen Gross --- V2: - new patch V3: - modify return type of db_fetch() to return a pointer to const (Julien Grall) - drop stale comment (Julien Grall) - fix transaction handling --- tools/xenstore/xenstored_core.c | 45 +++++++++++--------------- tools/xenstore/xenstored_core.h | 2 +- tools/xenstore/xenstored_transaction.c | 23 +++++++++---- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 9f88914149..1f5f118f1c 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -555,10 +555,9 @@ static void initialize_fds(int *p_sock_pollfd_idx, int= *ptimeout) } } =20 -struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size) +const struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size) { - const struct xs_tdb_record_hdr *hdr; - struct xs_tdb_record_hdr *p; + struct xs_tdb_record_hdr *hdr; =20 hdr =3D hashtable_search(nodes, db_name); if (!hdr) { @@ -569,22 +568,15 @@ struct xs_tdb_record_hdr *db_fetch(const char *db_nam= e, size_t *size) *size =3D sizeof(*hdr) + hdr->num_perms * sizeof(hdr->perms[0]) + hdr->datalen + hdr->childlen; =20 - /* Return a copy, avoiding a potential modification in the DB. */ - p =3D talloc_memdup(NULL, hdr, *size); - if (!p) { - errno =3D ENOMEM; - return NULL; - } - trace_tdb("read %s size %zu\n", db_name, *size + strlen(db_name)); =20 - return p; + return hdr; } =20 static void get_acc_data(const char *name, struct node_account_data *acc) { size_t size; - struct xs_tdb_record_hdr *hdr; + const struct xs_tdb_record_hdr *hdr; =20 if (acc->memory < 0) { hdr =3D db_fetch(name, &size); @@ -595,7 +587,6 @@ static void get_acc_data(const char *name, struct node_= account_data *acc) acc->memory =3D size; acc->domid =3D hdr->perms[0].id; } - talloc_free(hdr); } } =20 @@ -708,7 +699,7 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, const char *name) { size_t size; - struct xs_tdb_record_hdr *hdr; + const struct xs_tdb_record_hdr *hdr; struct node *node; const char *db_name; int err; @@ -729,30 +720,30 @@ struct node *read_node(struct connection *conn, const= void *ctx, hdr =3D db_fetch(db_name, &size); =20 if (hdr =3D=3D NULL) { - if (errno =3D=3D ENOENT) { - node->generation =3D NO_GENERATION; - err =3D access_node(conn, node, NODE_ACCESS_READ, NULL); - errno =3D err ? : ENOENT; - } else { - log("DB error on read: %s", strerror(errno)); - errno =3D EIO; - } + node->generation =3D NO_GENERATION; + err =3D access_node(conn, node, NODE_ACCESS_READ, NULL); + errno =3D err ? : ENOENT; goto error; } =20 node->parent =3D NULL; - talloc_steal(node, hdr); =20 /* Datalen, childlen, number of permissions */ node->generation =3D hdr->generation; node->perms.num =3D hdr->num_perms; node->datalen =3D hdr->datalen; node->childlen =3D hdr->childlen; - - /* Permissions are struct xs_permissions. */ - node->perms.p =3D hdr->perms; - node->acc.domid =3D get_node_owner(node); + node->acc.domid =3D hdr->perms[0].id; node->acc.memory =3D size; + + /* Copy node data to new memory area, starting with permissions. */ + size -=3D sizeof(*hdr); + node->perms.p =3D talloc_memdup(node, hdr->perms, size); + if (node->perms.p =3D=3D NULL) { + errno =3D ENOMEM; + goto error; + } + if (domain_adjust_node_perms(node)) goto error; =20 diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 1aa3cc0936..6d1578ce97 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -362,7 +362,7 @@ extern xengnttab_handle **xgt_handle; int remember_string(struct hashtable *hash, const char *str); =20 /* Data base access functions. */ -struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size); +const struct xs_tdb_record_hdr *db_fetch(const char *db_name, size_t *size= ); int db_write(struct connection *conn, const char *db_name, const void *dat= a, 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/xensto= red_transaction.c index fbcea3663e..a90283dcc5 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -357,20 +357,17 @@ static int finalize_transaction(struct connection *co= nn, { struct accessed_node *i, *n; size_t size; - struct xs_tdb_record_hdr *hdr; + const struct xs_tdb_record_hdr *hdr; uint64_t gen; =20 list_for_each_entry_safe(i, n, &trans->accessed, list) { if (i->check_gen) { hdr =3D db_fetch(i->node, &size); if (!hdr) { - if (errno !=3D ENOENT) - return errno; gen =3D NO_GENERATION; } else { gen =3D hdr->generation; } - talloc_free(hdr); if (i->generation !=3D gen) return EAGAIN; } @@ -388,14 +385,26 @@ static int finalize_transaction(struct connection *co= nn, if (i->ta_node) { hdr =3D db_fetch(i->trans_name, &size); if (hdr) { + /* + * Delete transaction entry and write it as + * no-TA entry. As we only hold a reference + * to the data, increment its ref count, then + * delete it from the DB. Now we own it and can + * drop the const attribute for changing the + * generation count. + */ enum write_node_mode mode; + struct xs_tdb_record_hdr *own; =20 - hdr->generation =3D ++generation; + talloc_increase_ref_count(hdr); + db_delete(conn, i->trans_name, NULL); + + own =3D (struct xs_tdb_record_hdr *)hdr; + own->generation =3D ++generation; mode =3D (i->generation =3D=3D NO_GENERATION) ? NODE_CREATE : NODE_MODIFY; - *is_corrupt |=3D db_write(conn, i->node, hdr, + *is_corrupt |=3D db_write(conn, i->node, own, size, NULL, mode, true); - db_delete(conn, i->trans_name, NULL); } else { *is_corrupt =3D true; } --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196861; cv=none; d=zohomail.com; s=zohoarc; b=Xn0/tRIGiRqTykPdOOVcOzeHi4DnDkRxxUSnuMT58i0mkZUbKe3TenF0x6XkpzaOlRMG0ZBp6iCYC+e/rqR48MKCPZhk3QINIYYSTtFs/Bd3qrf3byqWqupfQ7YAYKpZFZu9Mn7pP/89VhpCqWiZPO7JhLDP1f7+oxnDDwSSNmk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196861; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HbG6vFfvfpnBEfnaaQaQ12ffiTPqhKHDZgquwOgUpOo=; b=acxwDqch3587fKtTyjLxg/X9qe4tGk1wBnrgPaHwwMsaIqX+fRwFGbL1W730y4v9GZWm9ZY/4XwOkydB1nXta3yujLdW71YN+K3gpY/33gXfntGQBRZpzq+Og4VLZgG9DqjKVIec+Rl64a0CferU9XRb9jvngJzJjDrDeAA/rGA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196861397389.24828864089193; Mon, 24 Jul 2023 04:07:41 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568752.888673 (Exim 4.92) (envelope-from ) id 1qNtPT-0006xN-Ri; Mon, 24 Jul 2023 11:07:27 +0000 Received: by outflank-mailman (output) from mailman id 568752.888673; Mon, 24 Jul 2023 11:07:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtPT-0006xE-Nt; Mon, 24 Jul 2023 11:07:27 +0000 Received: by outflank-mailman (input) for mailman id 568752; Mon, 24 Jul 2023 11:07:25 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMZ-0000KR-0e for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:27 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id da691f06-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:04:26 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 2673A2298F; Mon, 24 Jul 2023 11:04:26 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id EC43613476; Mon, 24 Jul 2023 11:04:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id X8JKOLlavmQZYgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:25 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: da691f06-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196666; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HbG6vFfvfpnBEfnaaQaQ12ffiTPqhKHDZgquwOgUpOo=; b=lZQCbaqclyrLeW7Qk45h0NM5eK5D7aWNYWZkIEjznaHuVvQhWzLS/6hNsyXfCkYebr4QcY /af6jMVjIgKjxv3/cmUE8B/NTSXBHaXGbmWeB6qDLDr+p1Q37j3B2msfrrWU5l0VXzBiSl fRmhVGMr4Z0lqkE6w4mkKCpYP7QaCAU= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 17/25] tools/xenstore: rework struct xs_tdb_record_hdr Date: Mon, 24 Jul 2023 13:02:39 +0200 Message-Id: <20230724110247.10520-18-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196863643100001 Content-Type: text/plain; charset="utf-8" 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 --- 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 @@ =20 #include "xenstore_lib.h" =20 -/* 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 =3D=3D B ? */ #define streq(a,b) (strcmp((a),(b)) =3D=3D 0) =20 diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.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) } } =20 -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; =20 hdr =3D 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; } =20 - *size =3D sizeof(*hdr) + hdr->num_perms * sizeof(hdr->perms[0]) + + *size =3D sizeof(*hdr) + hdr->num_perms * sizeof(struct xs_permissions) + hdr->datalen + hdr->childlen; =20 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; } =20 +static struct xs_permissions *perms_from_node_hdr(const struct node_hdr *h= dr) +{ + 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; =20 if (acc->memory < 0) { hdr =3D db_fetch(name, &size); @@ -585,7 +590,7 @@ static void get_acc_data(const char *name, struct node_= account_data *acc) acc->memory =3D 0; } else { acc->memory =3D size; - acc->domid =3D hdr->perms[0].id; + acc->domid =3D perms_from_node_hdr(hdr)->id; } } } @@ -606,7 +611,7 @@ int db_write(struct connection *conn, const char *db_na= me, 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 =3D data; + const struct node_hdr *hdr =3D data; struct node_account_data old_acc =3D {}; unsigned int old_domid, new_domid; size_t name_len =3D strlen(db_name); @@ -620,7 +625,7 @@ int db_write(struct connection *conn, const char *db_na= me, const void *data, =20 get_acc_data(db_name, &old_acc); old_domid =3D get_acc_domid(conn, db_name, old_acc.domid); - new_domid =3D get_acc_domid(conn, db_name, hdr->perms[0].id); + new_domid =3D get_acc_domid(conn, db_name, perms_from_node_hdr(hdr)->id); =20 /* * 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_na= me, const void *data, =20 if (acc) { /* Don't use new_domid, as it might be a transaction node. */ - acc->domid =3D hdr->perms[0].id; + acc->domid =3D perms_from_node_hdr(hdr)->id; acc->memory =3D size; } =20 @@ -699,7 +704,7 @@ struct node *read_node(struct connection *conn, const v= oid *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 =3D hdr->num_perms; node->datalen =3D hdr->datalen; node->childlen =3D hdr->childlen; - node->acc.domid =3D hdr->perms[0].id; + node->acc.domid =3D perms_from_node_hdr(hdr)->id; node->acc.memory =3D size; =20 /* Copy node data to new memory area, starting with permissions. */ size -=3D sizeof(*hdr); - node->perms.p =3D talloc_memdup(node, hdr->perms, size); + node->perms.p =3D talloc_memdup(node, perms_from_node_hdr(hdr), size); if (node->perms.p =3D=3D NULL) { errno =3D 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; =20 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 =3D node->datalen; hdr->childlen =3D node->childlen; =20 - memcpy(hdr->perms, node->perms.p, - node->perms.num * sizeof(*node->perms.p)); - p =3D hdr->perms + node->perms.num; + p =3D perms_from_node_hdr(hdr); + memcpy(p, node->perms.p, node->perms.num * sizeof(*node->perms.p)); + p +=3D node->perms.num * sizeof(*node->perms.p); memcpy(p, node->data, node->datalen); p +=3D node->datalen; memcpy(p, node->children, node->childlen); diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.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; =20 +/* + * Header of the node record in the data base. + * In the data base the memory of the node is a single memory chunk with t= he + * 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); =20 /* 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 *dat= a, 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/xensto= red_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; =20 list_for_each_entry_safe(i, n, &trans->accessed, list) { @@ -394,12 +394,12 @@ static int finalize_transaction(struct connection *co= nn, * generation count. */ enum write_node_mode mode; - struct xs_tdb_record_hdr *own; + struct node_hdr *own; =20 talloc_increase_ref_count(hdr); db_delete(conn, i->trans_name, NULL); =20 - own =3D (struct xs_tdb_record_hdr *)hdr; + own =3D (struct node_hdr *)hdr; own->generation =3D ++generation; mode =3D (i->generation =3D=3D NO_GENERATION) ? NODE_CREATE : NODE_MODIFY; --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196894; cv=none; d=zohomail.com; s=zohoarc; b=Cn/C+KuqgrGWUWtfIeRyB9gdT/zIWfuUQwhY2DRjMN2EC6xqgcEwS6g1+SwhEVejRwAF+PFN8L9alP6IgNRqhaV9thNnKz1dOZ2q0pe8y53cQSs4KR5L+/OyGHWi/rDiuy1o5CeSJ2auG3XbmMFak7ZjSonOg38zMq3oNeBuYxU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196894; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HP5J6XIp0DWqkQx8z5cq/ygHh6Oytu7p/EOreeJK8xE=; b=EfoV1/bCQUCYrAN9oP4UqI51E9OLxGGu9APe3ZIH6wFj0c2zedB18CFLBKc3kQVvj/m/cjfOGYuPRfHpbz9pWHZmCZG5DiuGTN5wn7RvUD33HGsUU+NXhI6AEMbkPo+RR99ngSGG9tuhgU81Huhagk5aVzzoiQwGliSFJHjqg4w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196894192563.9555171822026; Mon, 24 Jul 2023 04:08:14 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568757.888703 (Exim 4.92) (envelope-from ) id 1qNtPv-0008P9-GN; Mon, 24 Jul 2023 11:07:55 +0000 Received: by outflank-mailman (output) from mailman id 568757.888703; Mon, 24 Jul 2023 11:07:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtPv-0008P2-DY; Mon, 24 Jul 2023 11:07:55 +0000 Received: by outflank-mailman (input) for mailman id 568757; Mon, 24 Jul 2023 11:07:54 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMf-0000KR-0n for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:33 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2001:67c:2178:6::1d]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id ddbf1902-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:04:31 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id B87651FDF0; Mon, 24 Jul 2023 11:04:31 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 8C1A513476; Mon, 24 Jul 2023 11:04:31 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id K//TIL9avmQrYgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:31 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: ddbf1902-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196671; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HP5J6XIp0DWqkQx8z5cq/ygHh6Oytu7p/EOreeJK8xE=; b=MenFa1wuLALtZWgUlCFQ57wC3MAWua2vaPS0StOYNTAJQHWhCwQyEYIhFkKmYaP+m8abSQ HKa8CsnytuKkKCYjLD6mRWn0yCXxNfIKmV7UWWqnUozNqA5YdoJU4bOqlnFe+AKlDh9uUc Z4qnS6c6+YYbLAYjtb+NMiCGOiJag2g= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 18/25] tools/xenstore: don't use struct node_perms in struct node Date: Mon, 24 Jul 2023 13:02:40 +0200 Message-Id: <20230724110247.10520-19-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196895716100001 Content-Type: text/plain; charset="utf-8" Open code struct node_perms in struct node in order to prepare using struct node_hdr in struct node. Add two helpers to transfer permissions between struct node and struct node_perms. Signed-off-by: Juergen Gross --- V2: - new patch --- tools/xenstore/xenstored_core.c | 76 ++++++++++++++------------ tools/xenstore/xenstored_core.h | 21 ++++++- tools/xenstore/xenstored_domain.c | 13 ++--- tools/xenstore/xenstored_transaction.c | 8 +-- tools/xenstore/xenstored_watch.c | 7 ++- 5 files changed, 75 insertions(+), 50 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 86b7c9bf36..c72fc0c725 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -735,7 +735,7 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, =20 /* Datalen, childlen, number of permissions */ node->generation =3D hdr->generation; - node->perms.num =3D hdr->num_perms; + node->num_perms =3D hdr->num_perms; node->datalen =3D hdr->datalen; node->childlen =3D hdr->childlen; node->acc.domid =3D perms_from_node_hdr(hdr)->id; @@ -743,8 +743,8 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, =20 /* Copy node data to new memory area, starting with permissions. */ size -=3D sizeof(*hdr); - node->perms.p =3D talloc_memdup(node, perms_from_node_hdr(hdr), size); - if (node->perms.p =3D=3D NULL) { + node->perms =3D talloc_memdup(node, perms_from_node_hdr(hdr), size); + if (node->perms =3D=3D NULL) { errno =3D ENOMEM; goto error; } @@ -757,7 +757,7 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, node->acc.memory =3D 0; =20 /* Data is binary blob (usually ascii, no nul). */ - node->data =3D node->perms.p + hdr->num_perms; + node->data =3D node->perms + hdr->num_perms; /* Children is strings, nul separated. */ node->children =3D node->data + node->datalen; =20 @@ -796,7 +796,7 @@ int write_node_raw(struct connection *conn, const char = *db_name, return errno; =20 size =3D sizeof(*hdr) - + node->perms.num * sizeof(node->perms.p[0]) + + node->num_perms * sizeof(node->perms[0]) + node->datalen + node->childlen; =20 /* Call domain_max_chk() in any case in order to record max values. */ @@ -813,13 +813,13 @@ int write_node_raw(struct connection *conn, const cha= r *db_name, =20 hdr =3D data; hdr->generation =3D node->generation; - hdr->num_perms =3D node->perms.num; + hdr->num_perms =3D node->num_perms; hdr->datalen =3D node->datalen; hdr->childlen =3D node->childlen; =20 p =3D perms_from_node_hdr(hdr); - memcpy(p, node->perms.p, node->perms.num * sizeof(*node->perms.p)); - p +=3D node->perms.num * sizeof(*node->perms.p); + memcpy(p, node->perms, node->num_perms * sizeof(*node->perms)); + p +=3D node->num_perms * sizeof(*node->perms); memcpy(p, node->data, node->datalen); p +=3D node->datalen; memcpy(p, node->children, node->childlen); @@ -900,6 +900,7 @@ static int ask_parents(struct connection *conn, const v= oid *ctx, const char *name, unsigned int *perm) { struct node *node; + struct node_perms perms; =20 do { name =3D get_parent(ctx, name); @@ -919,7 +920,8 @@ static int ask_parents(struct connection *conn, const v= oid *ctx, return 0; } =20 - *perm =3D perm_for_conn(conn, &node->perms); + node_to_node_perms(node, &perms); + *perm =3D perm_for_conn(conn, &perms); return 0; } =20 @@ -956,11 +958,13 @@ static struct node *get_node(struct connection *conn, unsigned int perm) { struct node *node; + struct node_perms perms; =20 node =3D read_node(conn, ctx, name); /* If we don't have permission, we don't have node. */ if (node) { - if ((perm_for_conn(conn, &node->perms) & perm) !=3D perm) { + node_to_node_perms(node, &perms); + if ((perm_for_conn(conn, &perms) & perm) !=3D perm) { errno =3D EACCES; node =3D NULL; } @@ -1434,14 +1438,14 @@ static struct node *construct_node(struct connectio= n *conn, const void *ctx, node->name =3D talloc_steal(node, names[levels - 1]); =20 /* Inherit permissions, unpriv domains own what they create. */ - node->perms.num =3D parent->perms.num; - node->perms.p =3D talloc_memdup(node, parent->perms.p, - node->perms.num * - sizeof(*node->perms.p)); - if (!node->perms.p) + node->num_perms =3D parent->num_perms; + node->perms =3D talloc_memdup(node, parent->perms, + node->num_perms * + sizeof(*node->perms)); + if (!node->perms) goto nomem; if (domain_is_unprivileged(conn)) - node->perms.p[0].id =3D conn->id; + node->perms[0].id =3D conn->id; =20 /* No children, no data */ node->children =3D node->data =3D NULL; @@ -1764,12 +1768,14 @@ static int do_get_perms(const void *ctx, struct con= nection *conn, struct node *node; char *strings; unsigned int len; + struct node_perms perms; =20 node =3D get_spec_node(conn, ctx, onearg(in), NULL, XS_PERM_READ); if (!node) return errno; =20 - strings =3D perms_to_strings(node, &node->perms, &len); + node_to_node_perms(node, &perms); + strings =3D perms_to_strings(node, &perms, &len); if (!strings) return errno; =20 @@ -1818,10 +1824,10 @@ static int do_set_perms(const void *ctx, struct con= nection *conn, perms.p[0].id !=3D get_node_owner(node)) return EPERM; =20 - old_perms =3D node->perms; + node_to_node_perms(node, &old_perms); if (domain_nbentry_dec(conn, get_node_owner(node))) return ENOMEM; - node->perms =3D perms; + node_perms_to_node(&perms, node); if (domain_nbentry_inc(conn, get_node_owner(node))) return ENOMEM; =20 @@ -2333,8 +2339,8 @@ static void manual_node(const char *name, const char = *child) barf_perror("Could not allocate initial node %s", name); =20 node->name =3D name; - node->perms.p =3D &perms; - node->perms.num =3D 1; + node->perms =3D &perms; + node->num_perms =3D 1; node->children =3D (char *)child; if (child) node->childlen =3D strlen(child) + 1; @@ -3205,10 +3211,10 @@ static int dump_state_node(const void *ctx, struct = connection *conn, sn.conn_id =3D 0; sn.ta_id =3D 0; sn.ta_access =3D 0; - sn.perm_n =3D node->perms.num; + sn.perm_n =3D node->num_perms; sn.path_len =3D pathlen; sn.data_len =3D node->datalen; - head.length +=3D node->perms.num * sizeof(*sn.perms); + head.length +=3D node->num_perms * sizeof(*sn.perms); head.length +=3D pathlen; head.length +=3D node->datalen; head.length =3D ROUNDUP(head.length, 3); @@ -3218,7 +3224,7 @@ static int dump_state_node(const void *ctx, struct co= nnection *conn, if (fwrite(&sn, sizeof(sn), 1, fp) !=3D 1) return dump_state_node_err(data, "Dump node state error"); =20 - ret =3D dump_state_node_perms(fp, node->perms.p, node->perms.num); + ret =3D dump_state_node_perms(fp, node->perms, node->num_perms); if (ret) return dump_state_node_err(data, ret); =20 @@ -3415,29 +3421,29 @@ void read_state_node(const void *ctx, const void *s= tate) node->data =3D name + sn->path_len; node->childlen =3D 0; node->children =3D NULL; - node->perms.num =3D sn->perm_n; - node->perms.p =3D talloc_array(node, struct xs_permissions, - node->perms.num); - if (!node->perms.p) + node->num_perms =3D sn->perm_n; + node->perms =3D talloc_array(node, struct xs_permissions, + node->num_perms); + if (!node->perms) barf("allocation error restoring node"); - for (i =3D 0; i < node->perms.num; i++) { + for (i =3D 0; i < node->num_perms; i++) { switch (sn->perms[i].access) { case 'r': - node->perms.p[i].perms =3D XS_PERM_READ; + node->perms[i].perms =3D XS_PERM_READ; break; case 'w': - node->perms.p[i].perms =3D XS_PERM_WRITE; + node->perms[i].perms =3D XS_PERM_WRITE; break; case 'b': - node->perms.p[i].perms =3D XS_PERM_READ | XS_PERM_WRITE; + node->perms[i].perms =3D XS_PERM_READ | XS_PERM_WRITE; break; default: - node->perms.p[i].perms =3D XS_PERM_NONE; + node->perms[i].perms =3D XS_PERM_NONE; break; } if (sn->perms[i].flags & XS_STATE_NODE_PERM_IGNORE) - node->perms.p[i].perms |=3D XS_PERM_IGNORE; - node->perms.p[i].id =3D sn->perms[i].domid; + node->perms[i].perms |=3D XS_PERM_IGNORE; + node->perms[i].id =3D sn->perms[i].domid; } =20 if (!strstarts(name, "@")) { diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index c965709090..9cb4c2f3eb 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -209,7 +209,8 @@ struct node { #define NO_GENERATION ~((uint64_t)0) =20 /* Permissions. */ - struct node_perms perms; + unsigned int num_perms; + struct xs_permissions *perms; =20 /* Contents. */ unsigned int datalen; @@ -251,7 +252,23 @@ unsigned int perm_for_conn(struct connection *conn, /* Get owner of a node. */ static inline unsigned int get_node_owner(const struct node *node) { - return node->perms.p[0].id; + return node->perms[0].id; +} + +/* Transfer permissions from node to struct node_perms. */ +static inline void node_to_node_perms(const struct node *node, + struct node_perms *perms) +{ + perms->num =3D node->num_perms; + perms->p =3D node->perms; +} + +/* Transfer permissions from struct node_perms to node. */ +static inline void node_perms_to_node(const struct node_perms *perms, + struct node *node) +{ + node->num_perms =3D perms->num; + node->perms =3D perms->p; } =20 /* Write a node to the data base. */ diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index 10d2280f84..1ba73d9db2 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -513,12 +513,12 @@ static int domain_tree_remove_sub(const void *ctx, st= ruct connection *conn, struct domain *domain =3D arg; int ret =3D WALK_TREE_OK; =20 - if (node->perms.p[0].id !=3D domain->domid) + if (node->perms[0].id !=3D domain->domid) return WALK_TREE_OK; =20 if (keep_orphans) { domain_nbentry_dec(NULL, domain->domid); - node->perms.p[0].id =3D priv_domid; + node->perms[0].id =3D priv_domid; node->acc.memory =3D 0; domain_nbentry_inc(NULL, priv_domid); if (write_node_raw(NULL, node->name, node, NODE_MODIFY, true)) { @@ -1335,12 +1335,11 @@ int domain_adjust_node_perms(struct node *node) { unsigned int i; =20 - for (i =3D 1; i < node->perms.num; i++) { - if (node->perms.p[i].perms & XS_PERM_IGNORE) + for (i =3D 1; i < node->num_perms; i++) { + if (node->perms[i].perms & XS_PERM_IGNORE) continue; - if (!chk_domain_generation(node->perms.p[i].id, - node->generation)) - node->perms.p[i].perms |=3D XS_PERM_IGNORE; + if (!chk_domain_generation(node->perms[i].id, node->generation)) + node->perms[i].perms |=3D XS_PERM_IGNORE; } =20 return 0; diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 9ca73b9874..213a2c436c 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -259,13 +259,13 @@ int access_node(struct connection *conn, struct node = *node, if (!i->trans_name) goto nomem; i->node =3D strchr(i->trans_name, '/') + 1; - if (node->generation !=3D NO_GENERATION && node->perms.num) { + if (node->generation !=3D NO_GENERATION && node->num_perms) { i->perms.p =3D talloc_array(i, struct xs_permissions, - node->perms.num); + node->num_perms); if (!i->perms.p) goto nomem; - i->perms.num =3D node->perms.num; - memcpy(i->perms.p, node->perms.p, + i->perms.num =3D node->num_perms; + memcpy(i->perms.p, node->perms, i->perms.num * sizeof(*i->perms.p)); } =20 diff --git a/tools/xenstore/xenstored_watch.c b/tools/xenstore/xenstored_wa= tch.c index fefbf56ab2..c161385f89 100644 --- a/tools/xenstore/xenstored_watch.c +++ b/tools/xenstore/xenstored_watch.c @@ -79,6 +79,7 @@ static bool watch_permitted(struct connection *conn, cons= t void *ctx, unsigned int perm; struct node *parent; char *parent_name; + struct node_perms node_perms; =20 if (perms) { perm =3D perm_for_conn(conn, perms); @@ -92,7 +93,8 @@ static bool watch_permitted(struct connection *conn, cons= t void *ctx, return false; } =20 - perm =3D perm_for_conn(conn, &node->perms); + node_to_node_perms(node, &node_perms); + perm =3D perm_for_conn(conn, &node_perms); if (perm & XS_PERM_READ) return true; =20 @@ -106,7 +108,8 @@ static bool watch_permitted(struct connection *conn, co= nst void *ctx, return false; } =20 - perm =3D perm_for_conn(conn, &parent->perms); + node_to_node_perms(parent, &node_perms); + perm =3D perm_for_conn(conn, &node_perms); =20 return perm & XS_PERM_READ; } --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196925; cv=none; d=zohomail.com; s=zohoarc; b=mZDsse1D/XHMNWTbDzG0v4qKivqlRBwBywYxTQtJHwZMWLaVXfjDYJiuD4GuE3X3h/sfj9gXEurLMajwSczHPQRO/qCmnvzeK7CA3CBt2IzYBKCKhBm3071XTJDUSQEDjAyJuL4hU4UDb5PtOArsljHzf9kqRHPR9JBwDQ0sSnA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196925; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bjbZ4eQM53YUxH4Mwab1m/FMELiUx/tvg5hjmsBzQfA=; b=nV20NPOlUISjA1NwSg9+j/0toeIcKfaMbzKQNf5wJBq+MsClxV9+Q5P4QrJCSD8ONxjm0WfwkU8f7YKgg9eLO/QE6j2QF5UNeoM17WEyREteinqpXzCpFYAMYIzOREf3Cq1w7JQnM5wXpYVFa0liLm2GSrce9313rJs3Kg/n0NU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196925021483.41112114246766; Mon, 24 Jul 2023 04:08:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568776.888801 (Exim 4.92) (envelope-from ) id 1qNtQK-00037V-Pn; Mon, 24 Jul 2023 11:08:20 +0000 Received: by outflank-mailman (output) from mailman id 568776.888801; Mon, 24 Jul 2023 11:08:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQK-00034C-2x; Mon, 24 Jul 2023 11:08:20 +0000 Received: by outflank-mailman (input) for mailman id 568776; Mon, 24 Jul 2023 11:08:15 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMl-0008WC-IS for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:39 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id e11d2c96-2a11-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 13:04:37 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 5FE0D1FDF0; Mon, 24 Jul 2023 11:04:37 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 2C72813476; Mon, 24 Jul 2023 11:04:37 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Y0+DCcVavmRJYgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:37 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: e11d2c96-2a11-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196677; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bjbZ4eQM53YUxH4Mwab1m/FMELiUx/tvg5hjmsBzQfA=; b=REgmbadv2ZYEEJ+8i0VNYzemUkhDDAQZkG1vyXXfH7mgJpGQ2Dv9tcbEf2Nb5czzEaXzYX LGIqeDwFaJNfmCCPZfn9SNDTO1p503isF3HNwNFK+WVAf2dYkNjZnhT9HBTj0C1FEU3sh+ Izc9Gm+ei6JMT1hefMYbE6NlYJy91j0= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 19/25] tools/xenstore: use struct node_hdr in struct node Date: Mon, 24 Jul 2023 13:02:41 +0200 Message-Id: <20230724110247.10520-20-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196925944100005 Content-Type: text/plain; charset="utf-8" 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 --- tools/xenstore/xenstored_core.c | 108 ++++++++++++------------- tools/xenstore/xenstored_core.h | 16 ++-- tools/xenstore/xenstored_domain.c | 5 +- tools/xenstore/xenstored_transaction.c | 13 +-- 4 files changed, 71 insertions(+), 71 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index c72fc0c725..404ecd0c62 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) } } =20 +static size_t calc_node_acc_size(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) { struct node_hdr *hdr; @@ -565,8 +571,7 @@ const struct node_hdr *db_fetch(const char *db_name, si= ze_t *size) return NULL; } =20 - *size =3D sizeof(*hdr) + hdr->num_perms * sizeof(struct xs_permissions) + - hdr->datalen + hdr->childlen; + *size =3D calc_node_acc_size(hdr); =20 trace_tdb("read %s size %zu\n", db_name, *size + strlen(db_name)); =20 @@ -725,7 +730,7 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, hdr =3D db_fetch(db_name, &size); =20 if (hdr =3D=3D NULL) { - node->generation =3D NO_GENERATION; + node->hdr.generation =3D NO_GENERATION; err =3D access_node(conn, node, NODE_ACCESS_READ, NULL); errno =3D err ? : ENOENT; goto error; @@ -734,10 +739,7 @@ struct node *read_node(struct connection *conn, const = void *ctx, node->parent =3D NULL; =20 /* Datalen, childlen, number of permissions */ - node->generation =3D hdr->generation; - node->num_perms =3D hdr->num_perms; - node->datalen =3D hdr->datalen; - node->childlen =3D hdr->childlen; + node->hdr =3D *hdr; node->acc.domid =3D perms_from_node_hdr(hdr)->id; node->acc.memory =3D size; =20 @@ -759,7 +761,7 @@ struct node *read_node(struct connection *conn, const v= oid *ctx, /* Data is binary blob (usually ascii, no nul). */ node->data =3D node->perms + hdr->num_perms; /* Children is strings, nul separated. */ - node->children =3D node->data + node->datalen; + node->children =3D node->data + node->hdr.datalen; =20 if (access_node(conn, node, NODE_ACCESS_READ, NULL)) goto error; @@ -795,9 +797,7 @@ int write_node_raw(struct connection *conn, const char = *db_name, if (domain_adjust_node_perms(node)) return errno; =20 - size =3D sizeof(*hdr) - + node->num_perms * sizeof(node->perms[0]) - + node->datalen + node->childlen; + size =3D calc_node_acc_size(&node->hdr); =20 /* Call domain_max_chk() in any case in order to record max values. */ if (domain_max_chk(conn, ACC_NODESZ, size) && !no_quota_check) { @@ -812,17 +812,14 @@ int write_node_raw(struct connection *conn, const cha= r *db_name, } =20 hdr =3D data; - hdr->generation =3D node->generation; - hdr->num_perms =3D node->num_perms; - hdr->datalen =3D node->datalen; - hdr->childlen =3D node->childlen; + *hdr =3D node->hdr; =20 p =3D perms_from_node_hdr(hdr); - memcpy(p, node->perms, node->num_perms * sizeof(*node->perms)); - p +=3D node->num_perms * sizeof(*node->perms); - memcpy(p, node->data, node->datalen); - p +=3D node->datalen; - memcpy(p, node->children, node->childlen); + memcpy(p, node->perms, node->hdr.num_perms * sizeof(*node->perms)); + p +=3D node->hdr.num_perms * sizeof(*node->perms); + memcpy(p, node->data, node->hdr.datalen); + p +=3D node->hdr.datalen; + memcpy(p, node->children, node->hdr.childlen); =20 if (db_write(conn, db_name, data, size, &node->acc, mode, no_quota_check)) @@ -1289,7 +1286,7 @@ static int send_directory(const void *ctx, struct con= nection *conn, if (!node) return errno; =20 - send_reply(conn, XS_DIRECTORY, node->children, node->childlen); + send_reply(conn, XS_DIRECTORY, node->children, node->hdr.childlen); =20 return 0; } @@ -1314,10 +1311,11 @@ static int send_directory_part(const void *ctx, str= uct connection *conn, /* Second arg is childlist offset. */ off =3D atoi(in->buffer + strlen(in->buffer) + 1); =20 - genlen =3D snprintf(gen, sizeof(gen), "%"PRIu64, node->generation) + 1; + genlen =3D snprintf(gen, sizeof(gen), "%"PRIu64, node->hdr.generation) + + 1; =20 /* Offset behind list: just return a list with an empty string. */ - if (off >=3D node->childlen) { + if (off >=3D node->hdr.childlen) { gen[genlen] =3D 0; send_reply(conn, XS_DIRECTORY_PART, gen, genlen + 1); return 0; @@ -1330,7 +1328,7 @@ static int send_directory_part(const void *ctx, struc= t connection *conn, while (len + strlen(child) < maxlen) { len +=3D strlen(child) + 1; child +=3D strlen(child) + 1; - if (off + len =3D=3D node->childlen) + if (off + len =3D=3D node->hdr.childlen) break; } =20 @@ -1340,7 +1338,7 @@ static int send_directory_part(const void *ctx, struc= t connection *conn, =20 memcpy(data, gen, genlen); memcpy(data + genlen, node->children + off, len); - if (off + len =3D=3D node->childlen) { + if (off + len =3D=3D node->hdr.childlen) { data[genlen + len] =3D 0; len++; } @@ -1360,7 +1358,7 @@ static int do_read(const void *ctx, struct connection= *conn, if (!node) return errno; =20 - send_reply(conn, XS_READ, node->data, node->datalen); + send_reply(conn, XS_READ, node->data, node->hdr.datalen); =20 return 0; } @@ -1379,13 +1377,13 @@ static int add_child(const void *ctx, struct node *= parent, const char *name) =20 base =3D basename(name); baselen =3D strlen(base) + 1; - children =3D talloc_array(ctx, char, parent->childlen + baselen); + children =3D talloc_array(ctx, char, parent->hdr.childlen + baselen); if (!children) return ENOMEM; - memcpy(children, parent->children, parent->childlen); - memcpy(children + parent->childlen, base, baselen); + memcpy(children, parent->children, parent->hdr.childlen); + memcpy(children + parent->hdr.childlen, base, baselen); parent->children =3D children; - parent->childlen +=3D baselen; + parent->hdr.childlen +=3D baselen; =20 return 0; } @@ -1438,9 +1436,9 @@ static struct node *construct_node(struct connection = *conn, const void *ctx, node->name =3D talloc_steal(node, names[levels - 1]); =20 /* Inherit permissions, unpriv domains own what they create. */ - node->num_perms =3D parent->num_perms; + node->hdr.num_perms =3D parent->hdr.num_perms; node->perms =3D talloc_memdup(node, parent->perms, - node->num_perms * + node->hdr.num_perms * sizeof(*node->perms)); if (!node->perms) goto nomem; @@ -1449,7 +1447,7 @@ static struct node *construct_node(struct connection = *conn, const void *ctx, =20 /* No children, no data */ node->children =3D node->data =3D NULL; - node->childlen =3D node->datalen =3D 0; + node->hdr.childlen =3D node->hdr.datalen =3D 0; node->acc.memory =3D 0; node->parent =3D parent; =20 @@ -1501,7 +1499,7 @@ static struct node *create_node(struct connection *co= nn, const void *ctx, ta_node_created(conn->transaction); =20 node->data =3D data; - node->datalen =3D datalen; + node->hdr.datalen =3D datalen; =20 /* * We write out the nodes bottom up. @@ -1581,7 +1579,7 @@ static int do_write(const void *ctx, struct connectio= n *conn, return errno; } else { node->data =3D in->buffer + offset; - node->datalen =3D datalen; + node->hdr.datalen =3D datalen; if (write_node(conn, node, NODE_MODIFY, false)) return errno; } @@ -1629,8 +1627,8 @@ static int remove_child_entry(struct connection *conn= , struct node *node, { size_t childlen =3D strlen(node->children + offset); =20 - memdel(node->children, offset, childlen + 1, node->childlen); - node->childlen -=3D childlen + 1; + memdel(node->children, offset, childlen + 1, node->hdr.childlen); + node->hdr.childlen -=3D childlen + 1; =20 return write_node(conn, node, NODE_MODIFY, true); } @@ -1640,8 +1638,9 @@ static int delete_child(struct connection *conn, { unsigned int i; =20 - for (i =3D 0; i < node->childlen; i +=3D strlen(node->children+i) + 1) { - if (streq(node->children+i, childname)) { + for (i =3D 0; i < node->hdr.childlen; + i +=3D strlen(node->children + i) + 1) { + if (streq(node->children + i, childname)) { errno =3D remove_child_entry(conn, node, i) ? EIO : 0; return errno; } @@ -1910,7 +1909,7 @@ int walk_node_tree(const void *ctx, struct connection= *conn, const char *root, /* node =3D=3D NULL possible only for the initial loop iteration. */ if (node) { /* Go one step up if ret or if last child finished. */ - if (ret || node->childoff >=3D node->childlen) { + if (ret || node->childoff >=3D node->hdr.childlen) { parent =3D node->parent; /* Call function AFTER processing a node. */ ret =3D walk_call_func(ctx, conn, node, parent, @@ -2340,10 +2339,10 @@ static void manual_node(const char *name, const cha= r *child) =20 node->name =3D name; node->perms =3D &perms; - node->num_perms =3D 1; + node->hdr.num_perms =3D 1; node->children =3D (char *)child; if (child) - node->childlen =3D strlen(child) + 1; + node->hdr.childlen =3D strlen(child) + 1; =20 if (write_node(NULL, node, NODE_CREATE, false)) barf_perror("Could not create initial node %s", name); @@ -3211,12 +3210,12 @@ static int dump_state_node(const void *ctx, struct = connection *conn, sn.conn_id =3D 0; sn.ta_id =3D 0; sn.ta_access =3D 0; - sn.perm_n =3D node->num_perms; + sn.perm_n =3D node->hdr.num_perms; sn.path_len =3D pathlen; - sn.data_len =3D node->datalen; - head.length +=3D node->num_perms * sizeof(*sn.perms); + sn.data_len =3D node->hdr.datalen; + head.length +=3D node->hdr.num_perms * sizeof(*sn.perms); head.length +=3D pathlen; - head.length +=3D node->datalen; + head.length +=3D node->hdr.datalen; head.length =3D ROUNDUP(head.length, 3); =20 if (fwrite(&head, sizeof(head), 1, fp) !=3D 1) @@ -3224,14 +3223,15 @@ static int dump_state_node(const void *ctx, struct = connection *conn, if (fwrite(&sn, sizeof(sn), 1, fp) !=3D 1) return dump_state_node_err(data, "Dump node state error"); =20 - ret =3D dump_state_node_perms(fp, node->perms, node->num_perms); + ret =3D dump_state_node_perms(fp, node->perms, node->hdr.num_perms); if (ret) return dump_state_node_err(data, ret); =20 if (fwrite(node->name, pathlen, 1, fp) !=3D 1) return dump_state_node_err(data, "Dump node path error"); =20 - if (node->datalen && fwrite(node->data, node->datalen, 1, fp) !=3D 1) + if (node->hdr.datalen && + fwrite(node->data, node->hdr.datalen, 1, fp) !=3D 1) return dump_state_node_err(data, "Dump node data error"); =20 ret =3D dump_state_align(fp); @@ -3416,17 +3416,17 @@ void read_state_node(const void *ctx, const void *s= tate) =20 node->acc.memory =3D 0; node->name =3D name; - node->generation =3D ++generation; - node->datalen =3D sn->data_len; + node->hdr.generation =3D ++generation; + node->hdr.datalen =3D sn->data_len; node->data =3D name + sn->path_len; - node->childlen =3D 0; + node->hdr.childlen =3D 0; node->children =3D NULL; - node->num_perms =3D sn->perm_n; + node->hdr.num_perms =3D sn->perm_n; node->perms =3D talloc_array(node, struct xs_permissions, - node->num_perms); + node->hdr.num_perms); if (!node->perms) barf("allocation error restoring node"); - for (i =3D 0; i < node->num_perms; i++) { + for (i =3D 0; i < node->hdr.num_perms; i++) { switch (sn->perms[i].access) { case 'r': node->perms[i].perms =3D XS_PERM_READ; diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 9cb4c2f3eb..adf8a785fc 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -181,6 +181,7 @@ extern struct list_head connections; */ struct node_hdr { uint64_t generation; +#define NO_GENERATION ~((uint64_t)0) uint16_t num_perms; uint16_t datalen; uint32_t childlen; @@ -197,6 +198,10 @@ struct node_account_data { }; =20 struct node { + /* Data direct for data base. */ + struct node_hdr hdr; + + /* Xenstore path. */ const char *name; /* Name used to access data base. */ const char *db_name; @@ -204,20 +209,13 @@ struct node { /* Parent (optional) */ struct node *parent; =20 - /* Generation count. */ - uint64_t generation; -#define NO_GENERATION ~((uint64_t)0) - /* Permissions. */ - unsigned int num_perms; struct xs_permissions *perms; =20 /* Contents. */ - unsigned int datalen; void *data; =20 /* Children, each nul-terminated. */ - unsigned int childlen; unsigned int childoff; /* Used by walk_node_tree() internally. */ char *children; =20 @@ -259,7 +257,7 @@ static inline unsigned int get_node_owner(const struct = node *node) static inline void node_to_node_perms(const struct node *node, struct node_perms *perms) { - perms->num =3D node->num_perms; + perms->num =3D node->hdr.num_perms; perms->p =3D node->perms; } =20 @@ -267,7 +265,7 @@ static inline void node_to_node_perms(const struct node= *node, static inline void node_perms_to_node(const struct node_perms *perms, struct node *node) { - node->num_perms =3D perms->num; + node->hdr.num_perms =3D perms->num; node->perms =3D perms->p; } =20 diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index 1ba73d9db2..fdf1095acb 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -1335,10 +1335,11 @@ int domain_adjust_node_perms(struct node *node) { unsigned int i; =20 - for (i =3D 1; i < node->num_perms; i++) { + for (i =3D 1; i < node->hdr.num_perms; i++) { if (node->perms[i].perms & XS_PERM_IGNORE) continue; - if (!chk_domain_generation(node->perms[i].id, node->generation)) + if (!chk_domain_generation(node->perms[i].id, + node->hdr.generation)) node->perms[i].perms |=3D XS_PERM_IGNORE; } =20 diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 213a2c436c..1f892b002d 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -232,7 +232,7 @@ int access_node(struct connection *conn, struct node *n= ode, bool introduce =3D false; =20 if (type !=3D NODE_ACCESS_READ) { - node->generation =3D ++generation; + node->hdr.generation =3D ++generation; if (conn && !conn->transaction) wrl_apply_debit_direct(conn); } @@ -259,12 +259,13 @@ int access_node(struct connection *conn, struct node = *node, if (!i->trans_name) goto nomem; i->node =3D strchr(i->trans_name, '/') + 1; - if (node->generation !=3D NO_GENERATION && node->num_perms) { + if (node->hdr.generation !=3D NO_GENERATION && + node->hdr.num_perms) { i->perms.p =3D talloc_array(i, struct xs_permissions, - node->num_perms); + node->hdr.num_perms); if (!i->perms.p) goto nomem; - i->perms.num =3D node->num_perms; + i->perms.num =3D node->hdr.num_perms; memcpy(i->perms.p, node->perms, i->perms.num * sizeof(*i->perms.p)); } @@ -282,9 +283,9 @@ int access_node(struct connection *conn, struct node *n= ode, * from the write types. */ if (type =3D=3D NODE_ACCESS_READ) { - i->generation =3D node->generation; + i->generation =3D node->hdr.generation; i->check_gen =3D true; - if (node->generation !=3D NO_GENERATION) { + if (node->hdr.generation !=3D NO_GENERATION) { ret =3D write_node_raw(conn, i->trans_name, node, NODE_CREATE, true); if (ret) --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196911; cv=none; d=zohomail.com; s=zohoarc; b=YNTu6jafSOGWVE3KRV4wXTORlgIz3L016rrakZdV4mFvU2kkxS4P60fGs2gMzCPVOBYlU3jCT7SLpB21QeR3AK0KwRlKceYLj0624bSVVwPL6JQBzlHhU3HNwUrbBxJunR0StraXrBAgInJJy/7BkqIBqRJZ9gVlTx7zzKxQrJw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196911; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=1v6Xlk3y4teF71x4mrmlG0ix0/+qV1VkkWZlz39L56s=; b=dQlK2vo3SVm98zxijXExq+Yn7DvXuHqKRwGtHCZ1/wIFst9TFF1LQwzP9oTYRjCM8pBXLgG7QeSh481YvsVpzwqEalzVaK+wdIL0LVYhTTTY3la0vVEVrnsrltSJEsIkCrMAjuPTfUo4m25Fe4Q6Onomi5zqP2uF+u8nOESWaQs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 169019691181775.10327038592641; Mon, 24 Jul 2023 04:08:31 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568768.888755 (Exim 4.92) (envelope-from ) id 1qNtQG-0001pb-8Z; Mon, 24 Jul 2023 11:08:16 +0000 Received: by outflank-mailman (output) from mailman id 568768.888755; Mon, 24 Jul 2023 11:08:16 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQF-0001kl-Vb; Mon, 24 Jul 2023 11:08:15 +0000 Received: by outflank-mailman (input) for mailman id 568768; Mon, 24 Jul 2023 11:08:13 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMp-0000KR-L9 for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:43 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [2001:67c:2178:6::1c]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id e470076b-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:04:43 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id F14B222991; Mon, 24 Jul 2023 11:04:42 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C2E7D13476; Mon, 24 Jul 2023 11:04:42 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id YTwkLspavmRZYgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:42 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: e470076b-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196682; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1v6Xlk3y4teF71x4mrmlG0ix0/+qV1VkkWZlz39L56s=; b=I3nc6rnJV2WdpYF0W9m5iv5wAf/LEjQ4Ypzm/0pbSr65JNx1NBheQj3abiiUJPkhG0c8/Z +aiW+8lHOzVPdwllXOtMuKkEayaiZNanK6OVRK1DFiTUcIJuwAeoeeLJqz3AK3ZBLX9KUq 3s5J4/B9jG4nk2cuWIlKS527C1DyFQA= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 20/25] tools/xenstore: alloc new memory in domain_adjust_node_perms() Date: Mon, 24 Jul 2023 13:02:42 +0200 Message-Id: <20230724110247.10520-21-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196913753100003 Content-Type: text/plain; charset="utf-8" In order to avoid modifying the node data in the data base in case a domain is gone, let domain_adjust_node_perms() allocate new memory for the permissions in case they need to be modified. As this should happen only in very rare cases, it is fine to do this even when having copied the node data already. Signed-off-by: Juergen Gross --- V3: - new patch --- tools/xenstore/xenstored_core.c | 10 +++++----- tools/xenstore/xenstored_domain.c | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 404ecd0c62..ea3d20a372 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -751,6 +751,11 @@ struct node *read_node(struct connection *conn, const = void *ctx, goto error; } =20 + /* Data is binary blob (usually ascii, no nul). */ + node->data =3D node->perms + hdr->num_perms; + /* Children is strings, nul separated. */ + node->children =3D node->data + node->hdr.datalen; + if (domain_adjust_node_perms(node)) goto error; =20 @@ -758,11 +763,6 @@ struct node *read_node(struct connection *conn, const = void *ctx, if (node->acc.domid !=3D get_node_owner(node)) node->acc.memory =3D 0; =20 - /* Data is binary blob (usually ascii, no nul). */ - node->data =3D node->perms + hdr->num_perms; - /* Children is strings, nul separated. */ - node->children =3D node->data + node->hdr.datalen; - if (access_node(conn, node, NODE_ACCESS_READ, NULL)) goto error; =20 diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_d= omain.c index fdf1095acb..cdef6efef4 100644 --- a/tools/xenstore/xenstored_domain.c +++ b/tools/xenstore/xenstored_domain.c @@ -1334,13 +1334,24 @@ int domain_alloc_permrefs(struct node_perms *perms) int domain_adjust_node_perms(struct node *node) { unsigned int i; + struct xs_permissions *perms =3D node->perms; + bool copied =3D false; =20 for (i =3D 1; i < node->hdr.num_perms; i++) { - if (node->perms[i].perms & XS_PERM_IGNORE) + if ((perms[i].perms & XS_PERM_IGNORE) || + chk_domain_generation(perms[i].id, node->hdr.generation)) continue; - if (!chk_domain_generation(node->perms[i].id, - node->hdr.generation)) - node->perms[i].perms |=3D XS_PERM_IGNORE; + + if (!copied) { + perms =3D talloc_memdup(node, node->perms, + node->hdr.num_perms * sizeof(*perms)); + if (!perms) + return ENOMEM; + node->perms =3D perms; + copied =3D true; + } + + perms[i].perms |=3D XS_PERM_IGNORE; } =20 return 0; --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196920; cv=none; d=zohomail.com; s=zohoarc; b=EOu51DiARShKLw300pjMS1F5tY6vt/nbhaBjt7IQMjJhrrandvNRGDq5kVvJKeCnF0B93YK/N8aHSdAlvRCAYe6CnYKqe3S3d48fgoLnafDXXwMPbSpqltOhjT+Zf7j5HcwBy/NCLDibLiVO/F0fS/EkqiQe5+Cd/8WWVKRpvgs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196920; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=tZobPrK7ENIq2LYLKVZ55LBjmihB+bKrO8uvn/+a32A=; b=nfKIQL73fPywmR5rnc9fxxz2ZuESFjyAO/QQtFS0LGT0O1ClCg2UtGa4Qj/fqrBybxAFNZWzGT7B6FLbDimyozIK9mzvlxIj/krVRSR5anWsMjTxzUHGztdDt81qb9/v+0fZPY4Wf/PGk7f38mQJBdTs+iwtwmQzmPwFWNnztOM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196920946169.23481891114818; Mon, 24 Jul 2023 04:08:40 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568767.888749 (Exim 4.92) (envelope-from ) id 1qNtQF-0001d1-OZ; Mon, 24 Jul 2023 11:08:15 +0000 Received: by outflank-mailman (output) from mailman id 568767.888749; Mon, 24 Jul 2023 11:08:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQF-0001bP-Du; Mon, 24 Jul 2023 11:08:15 +0000 Received: by outflank-mailman (input) for mailman id 568767; Mon, 24 Jul 2023 11:08:13 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtMv-0000KR-Gh for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:49 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id e7cef7ff-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:04:48 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 9AE2C22973; Mon, 24 Jul 2023 11:04:48 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 62B8713476; Mon, 24 Jul 2023 11:04:48 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 2DaxFtBavmRiYgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:48 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: e7cef7ff-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196688; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tZobPrK7ENIq2LYLKVZ55LBjmihB+bKrO8uvn/+a32A=; b=ik3qFlDDMYiRPGNQW0ZOd4kCMuYHHFUQ4x/do2uc6JMSIAWmIDCwEvucRXMYFlvj6h/LJu FDxUKOfI+hw+GQ/MilaaUuCt5VKZ/SQki7zTLMcwVAh7F88ZrsJCkA/xYsxVxHwu8tPNTy uFUg+X3f9wfpYAU040NoxYK77TkXVjA= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 21/25] tools/xenstore: introduce read_node_nocopy() Date: Mon, 24 Jul 2023 13:02:43 +0200 Message-Id: <20230724110247.10520-22-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196922119100001 Content-Type: text/plain; charset="utf-8" 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 --- 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_cor= e.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 *n= ame, * 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 *c= tx, + 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 =3D ENOMEM; return NULL; } + node->name =3D talloc_strdup(node, name); if (!node->name) { - talloc_free(node); errno =3D ENOMEM; - return NULL; + goto error; } =20 db_name =3D transaction_prepend(conn, name); - hdr =3D db_fetch(db_name, &size); - - if (hdr =3D=3D NULL) { + *hdr =3D db_fetch(db_name, &size); + if (*hdr =3D=3D NULL) { node->hdr.generation =3D NO_GENERATION; err =3D access_node(conn, node, NODE_ACCESS_READ, NULL); errno =3D err ? : ENOENT; @@ -739,31 +738,79 @@ struct node *read_node(struct connection *conn, const= void *ctx, node->parent =3D NULL; =20 /* Datalen, childlen, number of permissions */ - node->hdr =3D *hdr; - node->acc.domid =3D perms_from_node_hdr(hdr)->id; + node->hdr =3D **hdr; + node->acc.domid =3D perms_from_node_hdr(*hdr)->id; node->acc.memory =3D size; =20 - /* Copy node data to new memory area, starting with permissions. */ - size -=3D sizeof(*hdr); - node->perms =3D talloc_memdup(node, perms_from_node_hdr(hdr), size); - if (node->perms =3D=3D NULL) { - errno =3D ENOMEM; - goto error; - } + return node; =20 + 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 =3D node->perms + hdr->num_perms; + node->data =3D node->perms + node->hdr.num_perms; /* Children is strings, nul separated. */ node->children =3D node->data + node->hdr.datalen; =20 if (domain_adjust_node_perms(node)) - goto error; + return -1; =20 /* If owner is gone reset currently accounted memory size. */ if (node->acc.domid !=3D get_node_owner(node)) node->acc.memory =3D 0; =20 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 =3D read_node_alloc(conn, ctx, name, &hdr); + if (!node) + return NULL; + + /* Copy node data to new memory area, starting with permissions. */ + size =3D node->acc.memory - sizeof(*hdr); + node->perms =3D talloc_memdup(node, perms_from_node_hdr(hdr), size); + if (node->perms =3D=3D NULL) { + errno =3D 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 *ct= x, + const char *name) +{ + const struct node_hdr *hdr; + struct node *node; + + node =3D read_node_alloc(conn, ctx, name, &hdr); + if (!node) + return NULL; + + node->perms =3D perms_from_node_hdr(hdr); + + if (read_node_helper(conn, node)) goto error; =20 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; =20 do { name =3D get_parent(ctx, name); if (!name) return errno; - node =3D read_node(conn, ctx, name); + node =3D 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) } =20 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 =3D arg; FILE *fp =3D 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; } =20 +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; =20 - node =3D read_node(NULL, ctx, name); + node =3D read_node_const(NULL, ctx, name); if (!node) return dump_state_node_err(data, "Dump node read node error"); =20 @@ -3264,7 +3316,7 @@ const char *dump_state_nodes(FILE *fp, const void *ct= x) .fp =3D fp, .err =3D "Dump node walk error" }; - struct walk_funcs walkfuncs =3D { .enter =3D dump_state_node }; + struct walk_funcs walkfuncs =3D { .enter =3D dump_state_node_enter }; =20 if (walk_node_tree(ctx, NULL, "/", &walkfuncs, &data)) return data.err; diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.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 *ct= x, + const char *name); =20 /* 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_d= omain.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 =3D talloc_new(NULL); - struct node *node; + const struct node *node; =20 if (!ctx) return; =20 - node =3D read_node(NULL, ctx, name); + node =3D read_node_const(NULL, ctx, name); =20 if (node) fire_watches(NULL, ctx, name, node, true, NULL); diff --git a/tools/xenstore/xenstored_watch.c b/tools/xenstore/xenstored_wa= tch.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 *w= atch, const char *name) * changed permissions we need to take the old permissions into account, t= oo. */ 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; =20 @@ -88,7 +88,7 @@ static bool watch_permitted(struct connection *conn, cons= t void *ctx, } =20 if (!node) { - node =3D read_node(conn, ctx, name); + node =3D read_node_const(conn, ctx, name); if (!node) return false; } @@ -103,7 +103,7 @@ static bool watch_permitted(struct connection *conn, co= nst void *ctx, parent_name =3D get_parent(ctx, node->name); if (!parent_name) return false; - parent =3D read_node(conn, ctx, parent_name); + parent =3D read_node_const(conn, ctx, parent_name); if (!parent) return false; } @@ -122,7 +122,7 @@ static bool watch_permitted(struct connection *conn, co= nst void *ctx, * watch event, too. */ void fire_watches(struct connection *conn, const void *ctx, const char *na= me, - 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_wa= tch.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, =20 /* Fire all watches: !exact means all the children are affected (ie. rm). = */ void fire_watches(struct connection *conn, const void *tmp, const char *na= me, - struct node *node, bool exact, struct node_perms *perms); + const struct node *node, bool exact, + struct node_perms *perms); =20 void conn_delete_all_watches(struct connection *conn); =20 --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196931; cv=none; d=zohomail.com; s=zohoarc; b=DFluzpULD0NkDVJiYM9ivZzS6KBvnfT003Zy1prVxdjnI5/mtyiiVLge2J7hRYRw7IQL6emWMpqP+ZfyYmmjc15YTIQsiYZjNDOnEaWFzsa+CDPyj+vZwPrRHE8pWnwDE386+37G6zTTv5omuXsXDq2ZEVMMs1jVbpqWiitRp8Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196931; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=UBNOYQXOYuzBuhgnYVP50MH6bXF9LZNypAn0rVMR8hc=; b=MXE7dI2vbNAmpgMji8pO1PwKseHQH5x3ZcTFlWy9mqesfWAAgTfk9X/CI6iz1tPFGynVqnbX/Mni5YfHfEDtNxZ616JrgkwsDwQ04vbpdyyLpn+riU2rj0K2hVrdeikGcUwD+Sc5i3fGYRHwXCb2W8KfNCRzaiNHZwkHrhUy8IE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 16901969314441009.5649903290246; Mon, 24 Jul 2023 04:08:51 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568778.888807 (Exim 4.92) (envelope-from ) id 1qNtQL-0003IG-9R; Mon, 24 Jul 2023 11:08:21 +0000 Received: by outflank-mailman (output) from mailman id 568778.888807; Mon, 24 Jul 2023 11:08:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQK-0003CB-NM; Mon, 24 Jul 2023 11:08:20 +0000 Received: by outflank-mailman (input) for mailman id 568778; Mon, 24 Jul 2023 11:08:16 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtN0-0000KR-V2 for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:04:54 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id eb1f1a33-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:04:54 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 2FD4422973; Mon, 24 Jul 2023 11:04:54 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 02BA113476; Mon, 24 Jul 2023 11:04:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 3WikOtVavmR1YgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:53 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: eb1f1a33-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196694; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UBNOYQXOYuzBuhgnYVP50MH6bXF9LZNypAn0rVMR8hc=; b=bI4nrkTogltF5ovLAboNZ0JNknZm7FprlbX1RXtaRrtAb8TzYzmkMiN3a2AlH68rNQb6AJ TANac/dPHsOJ2I8IB2CZp6ZjaYCRDwxUUT/UAEt5LCeG+rmQh6ERwKqQHNQkOxQLkR2AKc qSZ13gSybpgiYlCWNhWPVasGKiWfScE= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 22/25] tools/xenstore: merge get_spec_node() into get_node_canonicalized() Date: Mon, 24 Jul 2023 13:02:44 +0200 Message-Id: <20230724110247.10520-23-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196931866100001 Content-Type: text/plain; charset="utf-8" Add a "allow_special" parameter to get_node_canonicalized() allowing to merge get_spec_node() into get_node_canonicalized(). Add the same parameter to is_valid_nodename(), as this will simplify check_watch_path(). This is done in preparation to introducing a get_node() variant returning a pointer to const struct node. Signed-off-by: Juergen Gross --- V3: - new patch --- tools/xenstore/xenstored_core.c | 45 +++++++++++++------------------- tools/xenstore/xenstored_core.h | 3 ++- tools/xenstore/xenstored_watch.c | 19 +++++--------- 3 files changed, 26 insertions(+), 41 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 102be92a43..ea5a1a9cce 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -1219,13 +1219,14 @@ static bool valid_chars(const char *node) "0123456789-/_@") =3D=3D strlen(node)); } =20 -bool is_valid_nodename(const struct connection *conn, const char *node) +bool is_valid_nodename(const struct connection *conn, const char *node, + bool allow_special) { int local_off =3D 0; unsigned int domid; =20 - /* Must start in /. */ - if (!strstarts(node, "/")) + /* Must start in / or - if special nodes are allowed - in @. */ + if (!strstarts(node, "/") && (!allow_special || !strstarts(node, "@"))) return false; =20 /* Cannot end in / (unless it's just "/"). */ @@ -1294,7 +1295,8 @@ static struct node *get_node_canonicalized(struct con= nection *conn, const void *ctx, const char *name, const char **canonical_name, - unsigned int perm) + unsigned int perm, + bool allow_special) { const char *tmp_name; =20 @@ -1303,33 +1305,20 @@ static struct node *get_node_canonicalized(struct c= onnection *conn, *canonical_name =3D canonicalize(conn, ctx, name); if (!*canonical_name) return NULL; - if (!is_valid_nodename(conn, *canonical_name)) { + if (!is_valid_nodename(conn, *canonical_name, allow_special)) { errno =3D EINVAL; return NULL; } return get_node(conn, ctx, *canonical_name, perm); } =20 -static struct node *get_spec_node(struct connection *conn, const void *ctx, - const char *name, const char **canonical_name, - unsigned int perm) -{ - if (name[0] =3D=3D '@') { - if (canonical_name) - *canonical_name =3D name; - return get_node(conn, ctx, name, perm); - } - - return get_node_canonicalized(conn, ctx, name, canonical_name, perm); -} - static int send_directory(const void *ctx, struct connection *conn, struct buffered_data *in) { struct node *node; =20 node =3D get_node_canonicalized(conn, ctx, onearg(in), NULL, - XS_PERM_READ); + XS_PERM_READ, false); if (!node) return errno; =20 @@ -1351,7 +1340,7 @@ static int send_directory_part(const void *ctx, struc= t connection *conn, =20 /* First arg is node name. */ node =3D get_node_canonicalized(conn, ctx, in->buffer, NULL, - XS_PERM_READ); + XS_PERM_READ, false); if (!node) return errno; =20 @@ -1401,7 +1390,7 @@ static int do_read(const void *ctx, struct connection= *conn, struct node *node; =20 node =3D get_node_canonicalized(conn, ctx, onearg(in), NULL, - XS_PERM_READ); + XS_PERM_READ, false); if (!node) return errno; =20 @@ -1615,7 +1604,8 @@ static int do_write(const void *ctx, struct connectio= n *conn, offset =3D strlen(vec[0]) + 1; datalen =3D in->used - offset; =20 - node =3D get_node_canonicalized(conn, ctx, vec[0], &name, XS_PERM_WRITE); + node =3D get_node_canonicalized(conn, ctx, vec[0], &name, XS_PERM_WRITE, + false); if (!node) { /* No permissions, invalid input? */ if (errno !=3D ENOENT) @@ -1644,7 +1634,7 @@ static int do_mkdir(const void *ctx, struct connectio= n *conn, const char *name; =20 node =3D get_node_canonicalized(conn, ctx, onearg(in), &name, - XS_PERM_WRITE); + XS_PERM_WRITE, false); =20 /* If it already exists, fine. */ if (!node) { @@ -1774,7 +1764,7 @@ static int do_rm(const void *ctx, struct connection *= conn, char *parentname; =20 node =3D get_node_canonicalized(conn, ctx, onearg(in), &name, - XS_PERM_WRITE); + XS_PERM_WRITE, false); if (!node) { /* Didn't exist already? Fine, if parent exists. */ if (errno =3D=3D ENOENT) { @@ -1816,7 +1806,8 @@ static int do_get_perms(const void *ctx, struct conne= ction *conn, unsigned int len; struct node_perms perms; =20 - node =3D get_spec_node(conn, ctx, onearg(in), NULL, XS_PERM_READ); + node =3D get_node_canonicalized(conn, ctx, onearg(in), NULL, XS_PERM_READ, + true); if (!node) return errno; =20 @@ -1860,8 +1851,8 @@ static int do_set_perms(const void *ctx, struct conne= ction *conn, return ENOENT; =20 /* We must own node to do this (tools can do this too). */ - node =3D get_spec_node(conn, ctx, in->buffer, &name, - XS_PERM_WRITE | XS_PERM_OWNER); + node =3D get_node_canonicalized(conn, ctx, in->buffer, &name, + XS_PERM_WRITE | XS_PERM_OWNER, true); if (!node) return errno; =20 diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index 65782c559d..f3a83efce8 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -295,7 +295,8 @@ void check_store(void); void corrupt(struct connection *conn, const char *fmt, ...); =20 /* Is this a valid node name? */ -bool is_valid_nodename(const struct connection *conn, const char *node); +bool is_valid_nodename(const struct connection *conn, const char *node, + bool allow_special); =20 /* Get name of parent node. */ char *get_parent(const void *ctx, const char *node); diff --git a/tools/xenstore/xenstored_watch.c b/tools/xenstore/xenstored_wa= tch.c index 86cf8322b4..2662a3fa49 100644 --- a/tools/xenstore/xenstored_watch.c +++ b/tools/xenstore/xenstored_watch.c @@ -166,19 +166,12 @@ static int destroy_watch(void *_watch) static int check_watch_path(struct connection *conn, const void *ctx, const char **path, bool *relative) { - /* Check if valid event. */ - if (strstarts(*path, "@")) { - *relative =3D false; - if (strlen(*path) > XENSTORE_REL_PATH_MAX) - goto inval; - } else { - *relative =3D !strstarts(*path, "/"); - *path =3D canonicalize(conn, ctx, *path); - if (!*path) - return errno; - if (!is_valid_nodename(conn, *path)) - goto inval; - } + *relative =3D !strstarts(*path, "/") && !strstarts(*path, "@"); + *path =3D canonicalize(conn, ctx, *path); + if (!*path) + return errno; + if (!is_valid_nodename(conn, *path, true)) + goto inval; =20 return 0; =20 --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196865; cv=none; d=zohomail.com; s=zohoarc; b=Nujj/F5CTJv1nLRPRmMIGo/DyQLAhNCtaeRrvYalmO2wj1teUM67I+9Oaz5yCn6KqwHK1WuacvJbl2doQnDqiVy/7cyghMPV11N3OUS8/2XHh+t8jam1bmcyxcNcsOu2ijZBphTFGAmsveXJZ9feGe5heTWMt9oVVTv//FpojCs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196865; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9aPawA5ESo5CRMIgOFiPZjmhXxpcDENIO0dImei+Tbo=; b=LWuzqtqAexbcKPK3RYZ82hlT5N9W3XTHN7TAyYr/xz19qwCoVgxuSbDmwtUecvGRtolLnBRyxZ/hjMfr4t5QX8TORKClkMkzC9wIZBetf0iZz2Zl/qocXMwmcgtvAARQNMWIOpDyEFNOhRDJBYuVfkK5+140ccIYJzPD4TcmCdo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196865349410.35305529237235; Mon, 24 Jul 2023 04:07:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568753.888678 (Exim 4.92) (envelope-from ) id 1qNtPU-0006z8-2m; Mon, 24 Jul 2023 11:07:28 +0000 Received: by outflank-mailman (output) from mailman id 568753.888678; Mon, 24 Jul 2023 11:07:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtPT-0006yt-UR; Mon, 24 Jul 2023 11:07:27 +0000 Received: by outflank-mailman (input) for mailman id 568753; Mon, 24 Jul 2023 11:07:26 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtN7-0008WC-O3 for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:05:01 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id ee780650-2a11-11ee-8612-37d641c3527e; Mon, 24 Jul 2023 13:05:00 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C40F01FDF0; Mon, 24 Jul 2023 11:04:59 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 973BA13476; Mon, 24 Jul 2023 11:04:59 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id lvtGI9tavmR+YgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:04:59 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: ee780650-2a11-11ee-8612-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196699; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9aPawA5ESo5CRMIgOFiPZjmhXxpcDENIO0dImei+Tbo=; b=MOWePWqF3Qw+uHN0dIhCwibDKqjlcE2Xt2ucz5KA96XZstUbGtqdSW04vakXkOhyBwirwB /tiVphEvE9PeTZyDLm0TF3HKMIPK8GzHWakj8DFL41y7h4M2ruQ/C6Dc8dTTWlU1ZN+rOH R4xM3ZTgbox+vNfJwveEgxDSvh32HkY= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 23/25] tools/xenstore: merge is_valid_nodename() into canonicalize() Date: Mon, 24 Jul 2023 13:02:45 +0200 Message-Id: <20230724110247.10520-24-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196867749100001 Content-Type: text/plain; charset="utf-8" Today is_valid_nodename() is always called directly after calling canonicalize(), with the exception of do_unwatch(), where the call is missing (which is not correct, but results just in a wrong error reason being returned). Merge is_valid_nodename() into canonicalize(). While at it merge valid_chars() into it, too. Signed-off-by: Juergen Gross --- V3: - new patch --- tools/xenstore/xenstored_core.c | 89 ++++++++++++++------------------ tools/xenstore/xenstored_core.h | 6 +-- tools/xenstore/xenstored_watch.c | 16 ++---- 3 files changed, 45 insertions(+), 66 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index ea5a1a9cce..ec20bc042d 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -1210,42 +1210,6 @@ void send_ack(struct connection *conn, enum xsd_sock= msg_type type) send_reply(conn, type, "OK", sizeof("OK")); } =20 -static bool valid_chars(const char *node) -{ - /* Nodes can have lots of crap. */ - return (strspn(node,=20 - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789-/_@") =3D=3D strlen(node)); -} - -bool is_valid_nodename(const struct connection *conn, const char *node, - bool allow_special) -{ - int local_off =3D 0; - unsigned int domid; - - /* Must start in / or - if special nodes are allowed - in @. */ - if (!strstarts(node, "/") && (!allow_special || !strstarts(node, "@"))) - return false; - - /* Cannot end in / (unless it's just "/"). */ - if (strends(node, "/") && !streq(node, "/")) - return false; - - /* No double //. */ - if (strstr(node, "//")) - return false; - - if (sscanf(node, "/local/domain/%5u/%n", &domid, &local_off) !=3D 1) - local_off =3D 0; - - if (domain_max_chk(conn, ACC_PATHLEN, strlen(node) - local_off)) - return false; - - return valid_chars(node); -} - /* We expect one arg in the input: return NULL otherwise. * The payload must contain exactly one nul, at the end. */ @@ -1279,16 +1243,46 @@ static char *perms_to_strings(const void *ctx, cons= t struct node_perms *perms, } =20 const char *canonicalize(struct connection *conn, const void *ctx, - const char *node) + const char *node, bool allow_special) { - const char *prefix; + char *name; + int local_off =3D 0; + unsigned int domid; =20 - if (!node || (node[0] =3D=3D '/') || (node[0] =3D=3D '@')) - return node; - prefix =3D get_implicit_path(conn); - if (prefix) - return talloc_asprintf(ctx, "%s/%s", prefix, node); - return node; + errno =3D EINVAL; + if (!node) + return NULL; + + if (strspn(node, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789-/_@") !=3D strlen(node)) + return NULL; + + if (node[0] =3D=3D '@' && !allow_special) + return NULL; + + if (node[0] !=3D '/' && node[0] !=3D '@') { + name =3D talloc_asprintf(ctx, "%s/%s", get_implicit_path(conn), + node); + if (!name) + return NULL; + } else + name =3D (char *)node; + + /* Cannot end in / (unless it's just "/"). */ + if (strends(name, "/") && !streq(name, "/")) + return NULL; + + /* No double //. */ + if (strstr(name, "//")) + return NULL; + + if (sscanf(name, "/local/domain/%5u/%n", &domid, &local_off) !=3D 1) + local_off =3D 0; + + if (domain_max_chk(conn, ACC_PATHLEN, strlen(name) - local_off)) + return NULL; + + return name; } =20 static struct node *get_node_canonicalized(struct connection *conn, @@ -1302,13 +1296,10 @@ static struct node *get_node_canonicalized(struct c= onnection *conn, =20 if (!canonical_name) canonical_name =3D &tmp_name; - *canonical_name =3D canonicalize(conn, ctx, name); + *canonical_name =3D canonicalize(conn, ctx, name, allow_special); if (!*canonical_name) return NULL; - if (!is_valid_nodename(conn, *canonical_name, allow_special)) { - errno =3D EINVAL; - return NULL; - } + return get_node(conn, ctx, *canonical_name, perm); } =20 diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_cor= e.h index f3a83efce8..ec1d6aac27 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -241,7 +241,7 @@ void send_ack(struct connection *conn, enum xsd_sockmsg= _type type); =20 /* Canonicalize this path if possible. */ const char *canonicalize(struct connection *conn, const void *ctx, - const char *node); + const char *node, bool allow_special); =20 /* Get access permissions. */ unsigned int perm_for_conn(struct connection *conn, @@ -294,10 +294,6 @@ struct connection *get_connection_by_id(unsigned int c= onn_id); void check_store(void); void corrupt(struct connection *conn, const char *fmt, ...); =20 -/* Is this a valid node name? */ -bool is_valid_nodename(const struct connection *conn, const char *node, - bool allow_special); - /* Get name of parent node. */ char *get_parent(const void *ctx, const char *node); =20 diff --git a/tools/xenstore/xenstored_watch.c b/tools/xenstore/xenstored_wa= tch.c index 2662a3fa49..247d37e80f 100644 --- a/tools/xenstore/xenstored_watch.c +++ b/tools/xenstore/xenstored_watch.c @@ -167,17 +167,9 @@ static int check_watch_path(struct connection *conn, c= onst void *ctx, const char **path, bool *relative) { *relative =3D !strstarts(*path, "/") && !strstarts(*path, "@"); - *path =3D canonicalize(conn, ctx, *path); - if (!*path) - return errno; - if (!is_valid_nodename(conn, *path, true)) - goto inval; - - return 0; + *path =3D canonicalize(conn, ctx, *path, true); =20 - inval: - errno =3D EINVAL; - return errno; + return *path ? 0 : errno; } =20 static struct watch *add_watch(struct connection *conn, const char *path, @@ -261,9 +253,9 @@ int do_unwatch(const void *ctx, struct connection *conn, if (get_strings(in, vec, ARRAY_SIZE(vec)) !=3D ARRAY_SIZE(vec)) return EINVAL; =20 - node =3D canonicalize(conn, ctx, vec[0]); + node =3D canonicalize(conn, ctx, vec[0], true); if (!node) - return ENOMEM; + return errno; list_for_each_entry(watch, &conn->watches, list) { if (streq(watch->node, node) && streq(watch->token, vec[1])) { list_del(&watch->list); --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196929; cv=none; d=zohomail.com; s=zohoarc; b=lIveE9/W2BWhZDD1gtp9rcmmAqpzIukGg4zcg5PuWq8WJxkOurRaFfylsJaD197YhIxG8BxrsDyd6XVyE7Bbjo1a42D9yBMFzra+v5xbmwy0JaCPS8AkMyAY8DoMqDz411Jxiz1a7sZTXeH4bwvaDrjzibeNmarcJ65g0PVjeZ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196929; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Q9VQYVzWF+hzExhRy+1hXjgi0IQDCM4dRNBtJbobDzo=; b=XBPv8CSRPdlvYirgTWHr6uusypvhMJXvXdCCullfM2SJs6ZcXhs3I3IvqIFz6bTVjjIGF7fxqriTEEo+oKatoUT1bGjS0kLK6Hg6OybijWFkhLXonhfCow0l7YmGWqJ+1QolJ7H21OzbavEx2XWgI2RUqLHTXlZMuqTYN0eLYhw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196929961981.2830725923258; Mon, 24 Jul 2023 04:08:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568773.888784 (Exim 4.92) (envelope-from ) id 1qNtQJ-0002fv-5F; Mon, 24 Jul 2023 11:08:19 +0000 Received: by outflank-mailman (output) from mailman id 568773.888784; Mon, 24 Jul 2023 11:08:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQI-0002ba-N9; Mon, 24 Jul 2023 11:08:18 +0000 Received: by outflank-mailman (input) for mailman id 568773; Mon, 24 Jul 2023 11:08:14 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtNC-0000KR-6f for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:05:06 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f1ce6737-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:05:05 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 648F820698; Mon, 24 Jul 2023 11:05:05 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 329F613476; Mon, 24 Jul 2023 11:05:05 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id eiHwCuFavmSSYgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:05:05 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f1ce6737-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196705; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q9VQYVzWF+hzExhRy+1hXjgi0IQDCM4dRNBtJbobDzo=; b=WY7uqR9cSk04kvTpBVpiHW4mxeMmeADXxXA9URdgoksfu5hTrCO9jHjqmHHbVB8qUIswzy JsgIGRF0owIAEv4dXc9Jw3pJcNG5M9JIXFabwzuZcyPqu4UqDGNgM1m5JoFU/WCn+HIx3q Oj/EvucfWQTQZ+1yt2lXJz/RfGx/Luc= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 24/25] tools/xenstore: rework get_node() Date: Mon, 24 Jul 2023 13:02:46 +0200 Message-Id: <20230724110247.10520-25-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196931960100003 Content-Type: text/plain; charset="utf-8" Today get_node_canonicalized() is the only caller of get_node(). In order to prepare introducing a get_node() variant returning a pointer to const struct node, do the following restructuring: - move the call of read_node() from get_node() into get_node_canonicalized() - rename get_node() to get_node_chk_perm() - rename get_node_canonicalized() to get_node() Signed-off-by: Juergen Gross --- V3: - new patch --- tools/xenstore/xenstored_core.c | 57 +++++++++++++++------------------ 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index ec20bc042d..fa07bc0c31 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -996,27 +996,26 @@ static int errno_from_parents(struct connection *conn= , const void *ctx, * If it fails, returns NULL and sets errno. * Temporary memory allocations are done with ctx. */ -static struct node *get_node(struct connection *conn, - const void *ctx, - const char *name, - unsigned int perm) +static bool get_node_chk_perm(struct connection *conn, const void *ctx, + const struct node *node, const char *name, + unsigned int perm) { - struct node *node; struct node_perms perms; + bool err =3D false; =20 - node =3D read_node(conn, ctx, name); /* If we don't have permission, we don't have node. */ if (node) { node_to_node_perms(node, &perms); if ((perm_for_conn(conn, &perms) & perm) !=3D perm) { errno =3D EACCES; - node =3D NULL; + err =3D true; } } /* Clean up errno if they weren't supposed to know. */ - if (!node && !read_node_can_propagate_errno()) + if (err && !read_node_can_propagate_errno()) errno =3D errno_from_parents(conn, ctx, name, errno, perm); - return node; + + return err; } =20 static struct buffered_data *new_buffer(void *ctx) @@ -1285,14 +1284,12 @@ const char *canonicalize(struct connection *conn, c= onst void *ctx, return name; } =20 -static struct node *get_node_canonicalized(struct connection *conn, - const void *ctx, - const char *name, - const char **canonical_name, - unsigned int perm, - bool allow_special) +static struct node *get_node(struct connection *conn, const void *ctx, + const char *name, const char **canonical_name, + unsigned int perm, bool allow_special) { const char *tmp_name; + struct node *node; =20 if (!canonical_name) canonical_name =3D &tmp_name; @@ -1300,7 +1297,10 @@ static struct node *get_node_canonicalized(struct co= nnection *conn, if (!*canonical_name) return NULL; =20 - return get_node(conn, ctx, *canonical_name, perm); + node =3D read_node(conn, ctx, *canonical_name); + + return get_node_chk_perm(conn, ctx, node, *canonical_name, perm) + ? NULL : node; } =20 static int send_directory(const void *ctx, struct connection *conn, @@ -1308,8 +1308,7 @@ static int send_directory(const void *ctx, struct con= nection *conn, { struct node *node; =20 - node =3D get_node_canonicalized(conn, ctx, onearg(in), NULL, - XS_PERM_READ, false); + node =3D get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, false); if (!node) return errno; =20 @@ -1330,8 +1329,7 @@ static int send_directory_part(const void *ctx, struc= t connection *conn, return EINVAL; =20 /* First arg is node name. */ - node =3D get_node_canonicalized(conn, ctx, in->buffer, NULL, - XS_PERM_READ, false); + node =3D get_node(conn, ctx, in->buffer, NULL, XS_PERM_READ, false); if (!node) return errno; =20 @@ -1380,8 +1378,7 @@ static int do_read(const void *ctx, struct connection= *conn, { struct node *node; =20 - node =3D get_node_canonicalized(conn, ctx, onearg(in), NULL, - XS_PERM_READ, false); + node =3D get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, false); if (!node) return errno; =20 @@ -1595,8 +1592,7 @@ static int do_write(const void *ctx, struct connectio= n *conn, offset =3D strlen(vec[0]) + 1; datalen =3D in->used - offset; =20 - node =3D get_node_canonicalized(conn, ctx, vec[0], &name, XS_PERM_WRITE, - false); + node =3D get_node(conn, ctx, vec[0], &name, XS_PERM_WRITE, false); if (!node) { /* No permissions, invalid input? */ if (errno !=3D ENOENT) @@ -1624,8 +1620,7 @@ static int do_mkdir(const void *ctx, struct connectio= n *conn, struct node *node; const char *name; =20 - node =3D get_node_canonicalized(conn, ctx, onearg(in), &name, - XS_PERM_WRITE, false); + node =3D get_node(conn, ctx, onearg(in), &name, XS_PERM_WRITE, false); =20 /* If it already exists, fine. */ if (!node) { @@ -1754,8 +1749,7 @@ static int do_rm(const void *ctx, struct connection *= conn, const char *name; char *parentname; =20 - node =3D get_node_canonicalized(conn, ctx, onearg(in), &name, - XS_PERM_WRITE, false); + node =3D get_node(conn, ctx, onearg(in), &name, XS_PERM_WRITE, false); if (!node) { /* Didn't exist already? Fine, if parent exists. */ if (errno =3D=3D ENOENT) { @@ -1797,8 +1791,7 @@ static int do_get_perms(const void *ctx, struct conne= ction *conn, unsigned int len; struct node_perms perms; =20 - node =3D get_node_canonicalized(conn, ctx, onearg(in), NULL, XS_PERM_READ, - true); + node =3D get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, true); if (!node) return errno; =20 @@ -1842,8 +1835,8 @@ static int do_set_perms(const void *ctx, struct conne= ction *conn, return ENOENT; =20 /* We must own node to do this (tools can do this too). */ - node =3D get_node_canonicalized(conn, ctx, in->buffer, &name, - XS_PERM_WRITE | XS_PERM_OWNER, true); + node =3D get_node(conn, ctx, in->buffer, &name, + XS_PERM_WRITE | XS_PERM_OWNER, true); if (!node) return errno; =20 --=20 2.35.3 From nobody Mon May 13 07:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1690196913; cv=none; d=zohomail.com; s=zohoarc; b=e8hLW0QNSXKqPEU3rsuT2QAW4Oozb3Iu8e/zW13GX64Y3Lg8o03HaORQ/7oF6oWh8W5fOlBNYongIxHSBFwB+4XYJsKub5qIsclhrjfsKRE0JmTzqe5Z3XpK2StxbJ8keYk5PivtbEoV4tRrWvIkYyBT9QusQ9TphAHk5xLL8BE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1690196913; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=g1YD4gZAxPlT1/QwlSIA+d4ZRSzaspiY2J7SO8xP21Y=; b=VOCnDgxO0fI+qlyuqeycAG3GhmAwLJ1eKjEuv0Qj5ISGH7Y36mnmkgY5oQilvDMKqNWKO7CBBDN2us54wjZhuh8q4sI/xc6Z8gB5fcv0qe+Mz33cPalgOI0WhzW8c0lJZIy8/twWrg5A8vnJQkTqa8tvcJRS/HpCxiMBI2vseyY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1690196913744395.22317227748897; Mon, 24 Jul 2023 04:08:33 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.568764.888733 (Exim 4.92) (envelope-from ) id 1qNtQE-0001Iv-ET; Mon, 24 Jul 2023 11:08:14 +0000 Received: by outflank-mailman (output) from mailman id 568764.888733; Mon, 24 Jul 2023 11:08:14 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtQE-0001IZ-8X; Mon, 24 Jul 2023 11:08:14 +0000 Received: by outflank-mailman (input) for mailman id 568764; Mon, 24 Jul 2023 11:08:12 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qNtNH-0000KR-Mm for xen-devel@lists.xenproject.org; Mon, 24 Jul 2023 11:05:11 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f52651f7-2a11-11ee-b23a-6b7b168915f2; Mon, 24 Jul 2023 13:05:11 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 055401FDF0; Mon, 24 Jul 2023 11:05:11 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C3F4813476; Mon, 24 Jul 2023 11:05:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id aIaOLuZavmSrYgAAMHmgww (envelope-from ); Mon, 24 Jul 2023 11:05:10 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f52651f7-2a11-11ee-b23a-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1690196711; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g1YD4gZAxPlT1/QwlSIA+d4ZRSzaspiY2J7SO8xP21Y=; b=OJiF5x/VaWqiMiyGMrMqra8+bueuk7mXfo6z/Nrd1MpPdnXaX32UM4ef/K2QYf5BKYTZvP JYSeDqgofbQCcF6hH0kcbsv5rWcSCvxl7nH0SVsMI6ObddeoSRsvxgDliGcqJAky9mDBrv +C3j1TrPBnDr86kjxXjvMsb01YzcEcY= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH v3 25/25] tools/xenstore: introduce get_node_const() Date: Mon, 24 Jul 2023 13:02:47 +0200 Message-Id: <20230724110247.10520-26-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230724110247.10520-1-jgross@suse.com> References: <20230724110247.10520-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1690196915863100001 Content-Type: text/plain; charset="utf-8" Add a variant of get_node() returning a const struct node pointer. Note that all callers of this new variant don't supply a pointer where to store the canonical node name, while all callers needing a non-const node do supply this pointer. This results in an asymmetric simplification of the two variants. Signed-off-by: Juergen Gross --- V3: - new approach (Julien Grall) --- tools/xenstore/xenstored_core.c | 35 ++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index fa07bc0c31..ed4e83d67d 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -1288,11 +1288,8 @@ static struct node *get_node(struct connection *conn= , const void *ctx, const char *name, const char **canonical_name, unsigned int perm, bool allow_special) { - const char *tmp_name; struct node *node; =20 - if (!canonical_name) - canonical_name =3D &tmp_name; *canonical_name =3D canonicalize(conn, ctx, name, allow_special); if (!*canonical_name) return NULL; @@ -1303,12 +1300,28 @@ static struct node *get_node(struct connection *con= n, const void *ctx, ? NULL : node; } =20 +static const struct node *get_node_const(struct connection *conn, + const void *ctx, const char *name, + unsigned int perm, bool allow_special) +{ + const char *tmp_name; + const struct node *node; + + tmp_name =3D canonicalize(conn, ctx, name, allow_special); + if (!tmp_name) + return NULL; + + node =3D read_node_const(conn, ctx, tmp_name); + + return get_node_chk_perm(conn, ctx, node, tmp_name, perm) ? NULL : node; +} + static int send_directory(const void *ctx, struct connection *conn, struct buffered_data *in) { - struct node *node; + const struct node *node; =20 - node =3D get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, false); + node =3D get_node_const(conn, ctx, onearg(in), XS_PERM_READ, false); if (!node) return errno; =20 @@ -1322,14 +1335,14 @@ static int send_directory_part(const void *ctx, str= uct connection *conn, { unsigned int off, len, maxlen, genlen; char *child, *data; - struct node *node; + const struct node *node; char gen[24]; =20 if (xenstore_count_strings(in->buffer, in->used) !=3D 2) return EINVAL; =20 /* First arg is node name. */ - node =3D get_node(conn, ctx, in->buffer, NULL, XS_PERM_READ, false); + node =3D get_node_const(conn, ctx, in->buffer, XS_PERM_READ, false); if (!node) return errno; =20 @@ -1376,9 +1389,9 @@ static int send_directory_part(const void *ctx, struc= t connection *conn, static int do_read(const void *ctx, struct connection *conn, struct buffered_data *in) { - struct node *node; + const struct node *node; =20 - node =3D get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, false); + node =3D get_node_const(conn, ctx, onearg(in), XS_PERM_READ, false); if (!node) return errno; =20 @@ -1786,12 +1799,12 @@ static int do_rm(const void *ctx, struct connection= *conn, static int do_get_perms(const void *ctx, struct connection *conn, struct buffered_data *in) { - struct node *node; + const struct node *node; char *strings; unsigned int len; struct node_perms perms; =20 - node =3D get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, true); + node =3D get_node_const(conn, ctx, onearg(in), XS_PERM_READ, true); if (!node) return errno; =20 --=20 2.35.3