From nobody Sat Apr 27 23:54:08 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=1685438048; cv=none; d=zohomail.com; s=zohoarc; b=TIdCB7UxDtNFRPSeKWbVCEyCX0hEfh5gDAtv31zOvRtGSSRf2/UZY6NtLDQnrbBOfCZCWV4cq9QGEJxHBGGa+IY/CjYZsEGvmLXA9V9G4JEEcDCSq61xMJjpVp20X33MA92sQNWhAq4qmlAkBPr0xzGCx1D3EXT1EYOvXCe9NjQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438048; 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=IKNSjaTn7WbCWwMDq2fO8Wq3Ke/9ONYC9bEL6rJ5Ge0=; b=HTcAYOBlBTyrgxKPiT/KymrBIxRFEFnzOvwaE38DxciKm3SICSI8RcwcKfX6859+lXbyhu2+Ry4IRkCRxSzVy8aUBABgg/hPHdNxxKVJX9EQgjd4VngnSyIILK+96aMSuEJirsjTSvL4bo8B3CJHRmN9LYrJYa7utsfRNRp24iI= 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 1685438048023821.3395613005301; Tue, 30 May 2023 02:14:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540960.843161 (Exim 4.92) (envelope-from ) id 1q3vQG-0007Eo-LU; Tue, 30 May 2023 09:13:44 +0000 Received: by outflank-mailman (output) from mailman id 540960.843161; Tue, 30 May 2023 09:13:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3vQG-0007Eh-Ga; Tue, 30 May 2023 09:13:44 +0000 Received: by outflank-mailman (input) for mailman id 540960; Tue, 30 May 2023 09:13:43 +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 1q3vQF-0006QB-JI for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:13:43 +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 453695ae-feca-11ed-8611-37d641c3527e; Tue, 30 May 2023 11:13:41 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 895B51FD7C; Tue, 30 May 2023 09:13:41 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 5FD0D1341B; Tue, 30 May 2023 09:13:41 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id ITIkFkW+dWTQIgAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:13:41 +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: 453695ae-feca-11ed-8611-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438021; 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=IKNSjaTn7WbCWwMDq2fO8Wq3Ke/9ONYC9bEL6rJ5Ge0=; b=iXhDEYXNm+bDS7KIBFmBvDi1PcqStCSx/QzGLMrf2qb2Wk6YynCjoQnn1B2bvfmkQfQyQx 6jzqU0i40BUxHxLJDolaKiko/WLf8y9z5nMqxIENJGo4hZNqy5QIdXZJDkoOaMZNGsRMxy TB/LcP1VQ0m3skbgAnkvrw+wKVHuZXA= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 01/11] tools/xenstore: explicitly specify create or modify for tdb_store() Date: Tue, 30 May 2023 11:13:23 +0200 Message-Id: <20230530091333.7678-2-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438050305100001 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 flag values NODE_CREATE and NODE_MODIFY. This is for preparing to get rid of TDB. Signed-off-by: Juergen Gross --- tools/xenstore/xenstored_core.c | 29 ++++++++++++++------------ tools/xenstore/xenstored_core.h | 6 ++++-- tools/xenstore/xenstored_domain.c | 2 +- tools/xenstore/xenstored_transaction.c | 8 +++++-- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 31a862b715..90c0bc7423 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -601,7 +601,7 @@ 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, int flag, bool no_quota_check) { struct xs_tdb_record_hdr *hdr =3D (void *)data->dptr; struct node_account_data old_acc =3D {}; @@ -635,7 +635,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, + (flag =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 +775,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) + int flag, bool no_quota_check) { TDB_DATA data; void *p; @@ -812,7 +813,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, flag, no_quota_check)) return EIO; =20 return 0; @@ -823,14 +824,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) + int flag, 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, flag, no_quota_check); if (ret && conn && conn->transaction) { /* * Reverting access_node() is hard, so just fail the @@ -1496,7 +1497,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 @@ -1560,7 +1562,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 @@ -1610,7 +1612,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, @@ -1807,7 +1809,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); @@ -2321,7 +2323,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); } @@ -3469,12 +3471,13 @@ 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)) + 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 84a611cbb5..9291efec17 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -238,7 +238,9 @@ static inline unsigned int get_node_owner(const struct = node *node) =20 /* Write a node to the tdb data base. */ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *no= de, - bool no_quota_check); + int flag, bool no_quota_check); +#define NODE_CREATE 0 +#define NODE_MODIFY 1 =20 /* Get a node from the tdb data base. */ struct node *read_node(struct connection *conn, const void *ctx, @@ -358,7 +360,7 @@ 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, int flag, 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 60d3aa1ddb..7bc49ec97c 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..0655073de7 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; @@ -363,6 +364,7 @@ static int finalize_transaction(struct connection *conn, TDB_DATA key, ta_key, data; struct xs_tdb_record_hdr *hdr; uint64_t gen; + int flag; =20 list_for_each_entry_safe(i, n, &trans->accessed, list) { if (i->check_gen) { @@ -405,8 +407,10 @@ static int finalize_transaction(struct connection *con= n, ta_key.dsize + data.dsize); hdr =3D (void *)data.dptr; hdr->generation =3D ++generation; + flag =3D (i->generation =3D=3D NO_GENERATION) + ? NODE_CREATE : NODE_MODIFY; *is_corrupt |=3D do_tdb_write(conn, &key, &data, - NULL, true); + NULL, flag, true); talloc_free(data.dptr); if (do_tdb_delete(conn, &ta_key, NULL)) *is_corrupt =3D true; --=20 2.35.3 From nobody Sat Apr 27 23:54:08 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=1685438055; cv=none; d=zohomail.com; s=zohoarc; b=QBjwM0alLQCOWwJrCMK1EgvItNe7Qa/vLJgtoacIWBpiiH/aPl8SdSXhsK6THm+2Ai67HTDtD3we5hWP02074YIGxlbuOkdbxTHIMlZcnqBJQVGPi0cgdHBq6ezgmNd0kMDY96HbVozCdrVzZ4I2klxcdJxkdAtzBLjG1zA46kM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438055; 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=+ClC9PU2wIUP/OJxhDjXBevd2pA0sV7fWT1Nek6XR94=; b=kA0Jse+K+yHmXTl/8mcxjUUOy6rKkVx946yU1zOgpi1bbb+ZJarYWtHeL/A7uEaqNvr7qv2hyWZWzsD1CIL4AE7k/d5cx3MYqhrG3MQLHL61fbiH6QzdlN1xfb3fdF6Jyju0cHUgreja1I9bJEr8G807thiNYFC6+Kk7Jv5C9dE= 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 1685438055344908.4071141178676; Tue, 30 May 2023 02:14:15 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540963.843169 (Exim 4.92) (envelope-from ) id 1q3vQM-0007bT-Rx; Tue, 30 May 2023 09:13:50 +0000 Received: by outflank-mailman (output) from mailman id 540963.843169; Tue, 30 May 2023 09:13:50 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3vQM-0007bK-PG; Tue, 30 May 2023 09:13:50 +0000 Received: by outflank-mailman (input) for mailman id 540963; Tue, 30 May 2023 09:13:49 +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 1q3vQL-0006QB-7d for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:13:49 +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 48905308-feca-11ed-8611-37d641c3527e; Tue, 30 May 2023 11:13:47 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 2CB1621AC5; Tue, 30 May 2023 09:13:47 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 016C01341B; Tue, 30 May 2023 09:13:46 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id mh57Okq+dWTcIgAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:13: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: 48905308-feca-11ed-8611-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438027; 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=+ClC9PU2wIUP/OJxhDjXBevd2pA0sV7fWT1Nek6XR94=; b=C3KZ57ybgGsu8Z0V+4N2Of16LDwlYaRYUWTtM/LtKwReuOqT2Z7sr1SI4lg4Ap3xxCae3x Yp70pj7tmoeIMr8BbgTfolyUTD2IOZjEIMICY1NOE5i3ZJfa7Qgc8rIL/4iatb/UbsIHlf EzCjgW7j3lbvdYv+3kCxJujgyFds42I= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 02/11] tools/xenstore: replace key in struct node with data base name Date: Tue, 30 May 2023 11:13:24 +0200 Message-Id: <20230530091333.7678-3-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438056133100001 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 90c0bc7423..a1d5d4a419 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -820,18 +820,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, int flag, 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, flag, no_quota_check); + set_tdb_key(node->db_name, &key); + ret =3D write_node_raw(conn, &key, node, flag, no_quota_check); if (ret && conn && conn->transaction) { /* * Reverting access_node() is hard, so just fail the @@ -1445,10 +1447,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) @@ -1638,10 +1643,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 @@ -1649,6 +1655,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 9291efec17..f7cb035f26 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 0655073de7..9dab0cd165 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 Sat Apr 27 23:54:08 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=1685438058; cv=none; d=zohomail.com; s=zohoarc; b=HGGjsgCDfEhQdWWnnBqCJjinP8+pPrg4lpuiktTvdf3DlKa60Y6GSFYpbzfBENZFzQ2whEQtJ82r69lZJ5F70gQJt0FKMqG0BjxkBCkAlg2A2gzNirhibyoTojCH5YcIDC84kT91v40LmS5gvITqDrlloAvTAjH3mSsJOBm4Bbo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438058; 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=LaigVqKga5DdOUggEAXiuJzbZ/VBvKJTFm/Qf2fJTXM=; b=az+rE4es1LM75SLGobNjN2MWuXOIs25GnYfN3syAg5+47YVT9tj2lSunl6OXlpnb+8SScM4x1uaK3rpiZQxb81lxepWG7QB+eySsxW1qSigUQMUhXg7r81osac7rOUtQ0T3l8LUnNWf0IfBW13E8fi9ze8dRtxjfMD4K1ZUp1WE= 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 1685438058046313.5072491070865; Tue, 30 May 2023 02:14:18 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540964.843180 (Exim 4.92) (envelope-from ) id 1q3vQQ-0007v0-53; Tue, 30 May 2023 09:13:54 +0000 Received: by outflank-mailman (output) from mailman id 540964.843180; Tue, 30 May 2023 09:13: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 1q3vQQ-0007up-1T; Tue, 30 May 2023 09:13:54 +0000 Received: by outflank-mailman (input) for mailman id 540964; Tue, 30 May 2023 09:13: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 1q3vQP-0006nv-Gl for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:13: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 4be8ab9d-feca-11ed-b231-6b7b168915f2; Tue, 30 May 2023 11:13:53 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 B862121AC5; Tue, 30 May 2023 09:13:52 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 89F981341B; Tue, 30 May 2023 09:13:52 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id 56orIFC+dWTiIgAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:13:52 +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: 4be8ab9d-feca-11ed-b231-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438032; 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=LaigVqKga5DdOUggEAXiuJzbZ/VBvKJTFm/Qf2fJTXM=; b=suJmawvgPL32cthBV2CL4Jm3HF+dPY+NmhUQT7AcVgmXFdxKlAMvQbpYmlXtb0HIZ49TVu B+guJMw4FLRODOtsKtw0KdKXhSzg9sOGbW4nTXkydWTFxlDj8t+GqEynI3P+oyiBZdyyHP l1J0m368p8h7hUqkDij2ZmZwcXNiKro= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 03/11] tools/xenstore: let transaction_prepend() return the name for access Date: Tue, 30 May 2023 11:13:25 +0200 Message-Id: <20230530091333.7678-4-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438060139100001 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 a1d5d4a419..239f8c6bc4 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -694,6 +694,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); @@ -708,7 +709,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 9dab0cd165..1646c07040 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 Sat Apr 27 23:54:08 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=1685438064; cv=none; d=zohomail.com; s=zohoarc; b=Fu24LYe8YcL64GioWyA0dyGGebGGp0Tfw/Vi+XE36pNBZkH44axS+zD4PwZIPFtg0lwYC9tvU0qODOq8y6/w8gHTXA7LtleC1geNESgV5cgHspwypndK9nYq0QYb+Pz4Ls8VFc4bqBoUww9G2Tq19TYNoFvES7MnYtl5TSlUr9s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438064; 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=Ddci7abEBVlfL/pNul+H+iyRqLJOJhYkViCfX8p+eww=; b=RnI6uuz8W9a7tdPaF+QkjAh4Eg8w+cc8SxDrpC0Bw4UjgD/XAi4nik/obeIYiSNiKw9WvPadRjGSysCP881536SOMmaHe6AwDyj2On/AWAeBDbmIwXc9UIfonFqwLKFE6r+OBWwqAlCddTWR2b3qQvbvuXwiFhspd99og9pITDA= 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 1685438064364720.2651019374188; Tue, 30 May 2023 02:14:24 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540965.843190 (Exim 4.92) (envelope-from ) id 1q3vQX-0008Rr-Ib; Tue, 30 May 2023 09:14:01 +0000 Received: by outflank-mailman (output) from mailman id 540965.843190; Tue, 30 May 2023 09:14: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 1q3vQX-0008Qt-Em; Tue, 30 May 2023 09:14:01 +0000 Received: by outflank-mailman (input) for mailman id 540965; Tue, 30 May 2023 09:14: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 1q3vQW-0006QB-9G for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:14:00 +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 4f390beb-feca-11ed-8611-37d641c3527e; Tue, 30 May 2023 11:13:58 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 55F191FD68; Tue, 30 May 2023 09:13:58 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 28F691341B; Tue, 30 May 2023 09:13:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id PqSlCFa+dWTpIgAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:13:58 +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: 4f390beb-feca-11ed-8611-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438038; 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=Ddci7abEBVlfL/pNul+H+iyRqLJOJhYkViCfX8p+eww=; b=pxxH4+oyCrMS287AX/MDVfgu7yb8HwHOOmqajtEaY9ZQ9NCdWbawDaEh7XoQs8uKm3bb+T sfmNjQepUoqnNKpoTkUe0Dx8hBcSj7A+nl/414isPNxY4m6koGqVAGVSN6Os6bhoy/+8+4 ijJI79LQ3j/DFAaJKlulCT30M2Sx/3U= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 04/11] tools/xenstore: rename do_tdb_delete() and change parameter type Date: Tue, 30 May 2023 11:13:26 +0200 Message-Id: <20230530091333.7678-5-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438066569100001 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 --- tools/xenstore/xenstored_core.c | 31 ++++++++++++-------------- tools/xenstore/xenstored_core.h | 5 +++-- tools/xenstore/xenstored_transaction.c | 18 ++++++--------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 239f8c6bc4..a2454ad24d 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -657,28 +657,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; @@ -1449,13 +1452,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) @@ -1646,7 +1646,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); @@ -1657,8 +1656,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 /* @@ -2483,9 +2481,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 f7cb035f26..7fc6d73e5a 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -358,11 +358,12 @@ 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, int flag, 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 1646c07040..bf173f3d1d 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -385,8 +385,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); @@ -395,21 +394,21 @@ static int finalize_transaction(struct connection *co= nn, } =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); if (data.dptr) { - trace_tdb("read %s size %zu\n", ta_key.dptr, + trace_tdb("read %s size %zu\n", i->trans_name, ta_key.dsize + data.dsize); hdr =3D (void *)data.dptr; hdr->generation =3D ++generation; flag =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, flag, 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; @@ -422,7 +421,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, @@ -439,15 +438,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 Sat Apr 27 23:54:08 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=1685438070; cv=none; d=zohomail.com; s=zohoarc; b=ORcSJlFOXOq19FGtSn1ljt0g6hZs9E6AykSe4zjgM/NYDb0i+gc8FPVokFb4h/yKJezAX7XkIavwL0dmdcfw+rm+4+onMRuM7FeouA4quqPnvmDffmqaQ03PT0nbMDtEuxhKAs46UIq+ltJErKyHiTkwfi6hmMPn6tZDpiiLrLk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438070; 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=yvze7UvbZmnFIXQucfaUCARD9SmDS6V0ehB8xJuM3Kk=; b=kSKush6AD9aDGTeNERNZgyrSUTcdGjla+coGB0yKN9PME4AI0kNrwWJzl6Lpt7KlxMGwVjoWf0ic4BRhkqV8MjMsuWhvYzsmBG7CqDXduCH0cBieQIjmOgxGC3RoOCSgFoM8oMiPQbTBW32/qhrcLDgCMkNgM6gxDHzDdbddsLI= 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 1685438070207500.5011906804209; Tue, 30 May 2023 02:14:30 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540970.843200 (Exim 4.92) (envelope-from ) id 1q3vQc-0000Xp-Rf; Tue, 30 May 2023 09:14:06 +0000 Received: by outflank-mailman (output) from mailman id 540970.843200; Tue, 30 May 2023 09:14:06 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3vQc-0000Xh-OE; Tue, 30 May 2023 09:14:06 +0000 Received: by outflank-mailman (input) for mailman id 540970; Tue, 30 May 2023 09:14:05 +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 1q3vQb-0006nv-3u for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:14:05 +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 52899e5e-feca-11ed-b231-6b7b168915f2; Tue, 30 May 2023 11:14:04 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 E51281FD68; Tue, 30 May 2023 09:14:03 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id B80581341B; Tue, 30 May 2023 09:14:03 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id hqiBK1u+dWTvIgAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:14: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: 52899e5e-feca-11ed-b231-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438043; 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=yvze7UvbZmnFIXQucfaUCARD9SmDS6V0ehB8xJuM3Kk=; b=HEzaLiVLJ8hT9VjON0PqE8qdC8Q5droOX0FZbiLslhJ6HUpFhx70Tof9dSsnwQ681W4R7v U/F4O7E/F/5C/1JEV6mDdYKtoM+s70O6l0h12hmr21lP4/nGzRf3KWPC+1J+gF1zHrzy2G u6NCMehIlXWxRe8jdsEX8uMe4zvROGs= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 05/11] tools/xenstore: rename do_tdb_write() and change parameter type Date: Tue, 30 May 2023 11:13:27 +0200 Message-Id: <20230530091333.7678-6-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438072219100001 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 --- tools/xenstore/xenstored_core.c | 67 +++++++++++++------------- tools/xenstore/xenstored_core.h | 9 ++-- tools/xenstore/xenstored_domain.c | 4 +- tools/xenstore/xenstored_transaction.c | 20 ++++---- 4 files changed, 49 insertions(+), 51 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index a2454ad24d..8fbf686331 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -600,22 +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, int flag, bool no_quota_check) +int db_write(struct connection *conn, const char *db_name, void *data, + size_t size, struct node_account_data *acc, int flag, + 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 @@ -623,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, (flag =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; @@ -779,33 +783,34 @@ 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, - int flag, bool no_quota_check) +int write_node_raw(struct connection *conn, const char *db_name, + struct node *node, int flag, 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; @@ -818,7 +823,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, flag, no_quota_check)) + if (db_write(conn, db_name, data, size, &node->acc, flag, + no_quota_check)) return EIO; =20 return 0; @@ -832,13 +838,11 @@ static int write_node(struct connection *conn, struct= node *node, int flag, 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, flag, no_quota_check); + ret =3D write_node_raw(conn, node->db_name, node, flag, no_quota_check); if (ret && conn && conn->transaction) { /* * Reverting access_node() is hard, so just fail the @@ -3423,7 +3427,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 }; @@ -3476,13 +3479,11 @@ 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); - 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 7fc6d73e5a..c4a995f745 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -237,8 +237,8 @@ static inline unsigned int get_node_owner(const struct = node *node) } =20 /* Write a node to the tdb data base. */ -int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *no= de, - int flag, bool no_quota_check); +int write_node_raw(struct connection *conn, const char *db_name, + struct node *node, int flag, bool no_quota_check); #define NODE_CREATE 0 #define NODE_MODIFY 1 =20 @@ -360,8 +360,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, int flag, bool no_quota_check); +int db_write(struct connection *conn, const char *db_name, void *data, + size_t size, struct node_account_data *acc, int flag, + 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 7bc49ec97c..e405ee31d3 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 bf173f3d1d..029819e90c 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; @@ -358,7 +356,7 @@ static int finalize_transaction(struct connection *conn, struct transaction *trans, bool *is_corrupt) { struct accessed_node *i, *n; - TDB_DATA key, ta_key, data; + TDB_DATA key, data; struct xs_tdb_record_hdr *hdr; uint64_t gen; int flag; @@ -373,7 +371,7 @@ static int finalize_transaction(struct connection *conn, return EIO; gen =3D NO_GENERATION; } else { - trace_tdb("read %s size %zu\n", key.dptr, + trace_tdb("read %s size %zu\n", i->node, key.dsize + data.dsize); gen =3D hdr->generation; } @@ -395,18 +393,18 @@ 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); + set_tdb_key(i->trans_name, &key); + data =3D tdb_fetch(tdb_ctx, key); if (data.dptr) { trace_tdb("read %s size %zu\n", i->trans_name, - ta_key.dsize + data.dsize); + key.dsize + data.dsize); hdr =3D (void *)data.dptr; hdr->generation =3D ++generation; flag =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, flag, true); + *is_corrupt |=3D db_write(conn, i->node, + data.dptr, data.dsize, + NULL, flag, true); talloc_free(data.dptr); if (db_delete(conn, i->trans_name, NULL)) *is_corrupt =3D true; --=20 2.35.3 From nobody Sat Apr 27 23:54:08 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=1685438073; cv=none; d=zohomail.com; s=zohoarc; b=KXluijDsMfBThShX7lv1h5IwGW++06b0/X2Vr6IHYnpbqSRCuBJQFchIUO7XVNSPMvMc+b/J7Fyx2i8cE1/oue2yl9jbl+BNNFONOdyqTGo+aL+XZd5jNpg04buJnb1cfqkHu9mta+OYtL+RvaRtJSfMhzXWgNqqNns2Hl5gWLE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438073; 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=2qS4AOkAcsCiQqUZmpFoLkz4Si4Jg5kN7awE7TAatN8=; b=ivhq8fExujUeTpKouZbbRDwcdLL7gKKSa242vtdRMU8M4d4iDXXUs47PflsCM2nRVLiPc0pWIT7rW9eQ75HEobIjt5x1/HqyBU9nWHlahDeug3wkq4sKv3yjvDqWLxyFvdfeEQoeZTocyjsmeUA1F0AWx9ChOCihjy1IQNO0MrM= 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 1685438073393878.995525403069; Tue, 30 May 2023 02:14:33 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540972.843209 (Exim 4.92) (envelope-from ) id 1q3vQh-00011C-6k; Tue, 30 May 2023 09:14:11 +0000 Received: by outflank-mailman (output) from mailman id 540972.843209; Tue, 30 May 2023 09:14:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3vQh-00010D-3F; Tue, 30 May 2023 09:14:11 +0000 Received: by outflank-mailman (input) for mailman id 540972; Tue, 30 May 2023 09:14:10 +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 1q3vQg-0006nv-Bf for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:14:10 +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 55e4061e-feca-11ed-b231-6b7b168915f2; Tue, 30 May 2023 11:14:09 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 8B26B21A78; Tue, 30 May 2023 09:14:09 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 61E351341B; Tue, 30 May 2023 09:14:09 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id MrCaFmG+dWT8IgAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:14: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: 55e4061e-feca-11ed-b231-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438049; 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=2qS4AOkAcsCiQqUZmpFoLkz4Si4Jg5kN7awE7TAatN8=; b=G9Ai5i1flNhm3A3LcvbIXQk6brAg2AMTkkjRCmSvAElodZ4CWvFqAX1ztlU+5jTnscqsHQ FSaYfw2QQEw9ZBvTbvlyoJED30dvHjDYuSYcuFJWkR2qHwEWZHZZddu9FR4RAoyJwoNeid tt7i1/bo+HEf42H/BG/IU/79q5y0AZc= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 06/11] tools/xenstore: switch get_acc_data() to use name instead of key Date: Tue, 30 May 2023 11:13:28 +0200 Message-Id: <20230530091333.7678-7-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438074199100003 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 8fbf686331..522b2bbf5f 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 Sat Apr 27 23:54:08 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=1685438290; cv=none; d=zohomail.com; s=zohoarc; b=ejZmKHUm7IDoWuol0hvel3FWvOnVZcY7l+yMVckfuaB4u3vxk+FIfNkN3ig4V3hsUKUImD7i59M5Thjyhl44C6iYu8k79jHW5VY8+XTNXkB6odczxvORNoxxArstGNTioO57UzZx4Hf3uUUkcYRJBtALf8MlXRy5HeaBWlppRbA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438290; 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=vR1hVHAjF1Inn8SNEDI2a5KzsJccXEFhtgtfIPwvc80=; b=gVv13KZu0riwHiY3cCgBi8fQvN9SAd4Un/5IVclBDCAhKFL7+2li7+WaZa6EnJD9N8sbOVatio1tjwa72YN+Tl0AhZvWWs2iubqU6QL3DiIH43P0p9ZzRtAi9NfcvZC4H6UmQsnBrg082qHYGz8aPpAKVmOQ4WNBxvjRSYBrunw= 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 1685438290438625.733915950852; Tue, 30 May 2023 02:18:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540994.843240 (Exim 4.92) (envelope-from ) id 1q3vUA-0003hq-Dc; Tue, 30 May 2023 09:17:46 +0000 Received: by outflank-mailman (output) from mailman id 540994.843240; Tue, 30 May 2023 09:17:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3vUA-0003hj-AZ; Tue, 30 May 2023 09:17:46 +0000 Received: by outflank-mailman (input) for mailman id 540994; Tue, 30 May 2023 09:17:44 +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 1q3vQn-0006QB-8W for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:14:17 +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 593d96f0-feca-11ed-8611-37d641c3527e; Tue, 30 May 2023 11:14:15 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 27F531FD85; Tue, 30 May 2023 09:14:15 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id EF9EE1341B; Tue, 30 May 2023 09:14:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id qDIuOWa+dWQKIwAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:14: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: 593d96f0-feca-11ed-8611-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438055; 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=vR1hVHAjF1Inn8SNEDI2a5KzsJccXEFhtgtfIPwvc80=; b=CU+qGmFXQnb8N863IkBKFvZ/j9B3dsYmV6uk3b3CPw0QaolDHtrl9wCL74Xc1xEFKDMm96 UKwlgdtwqBjZVtXL9cOyfe0lXHGz9C4LotYyt/ewG0QwHHZUURKV2INkGpc4ecFnQORhCP cOCNGOLEUOp5d0dbU0Pr4lq8NtT64Dc= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 07/11] tools/xenstore: add wrapper for tdb_fetch() Date: Tue, 30 May 2023 11:13:29 +0200 Message-Id: <20230530091333.7678-8-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438292568100001 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. This enables to make set_tdb_key() static again. This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross --- tools/xenstore/xenstored_core.c | 55 ++++++++++++++++---------- tools/xenstore/xenstored_core.h | 3 +- tools/xenstore/xenstored_transaction.c | 34 ++++++++-------- 3 files changed, 51 insertions(+), 41 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 522b2bbf5f..12c584f09b 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 +void *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; + else + *size =3D data.dsize; + + return data.dptr; +} + static void get_acc_data(const char *name, struct node_account_data *acc) { - TDB_DATA key, old_data; + void *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); + old_data =3D db_fetch(name, &size); /* No check for error, as the node might not exist. */ - if (old_data.dptr =3D=3D NULL) { + if (old_data =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; + size + strlen(name)); + hdr =3D old_data; + acc->memory =3D size; acc->domid =3D hdr->perms[0].id; } - talloc_free(old_data.dptr); + talloc_free(old_data); } } =20 @@ -698,7 +712,8 @@ 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; + void *data; + size_t size; struct xs_tdb_record_hdr *hdr; struct node *node; const char *db_name; @@ -717,29 +732,27 @@ struct node *read_node(struct connection *conn, const= void *ctx, } =20 db_name =3D transaction_prepend(conn, name); - set_tdb_key(db_name, &key); + data =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 (data =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); + trace_tdb("read %s size %zu\n", db_name, size + strlen(db_name)); =20 node->parent =3D NULL; - talloc_steal(node, data.dptr); + talloc_steal(node, data); =20 /* Datalen, childlen, number of permissions */ - hdr =3D (void *)data.dptr; + hdr =3D data; node->generation =3D hdr->generation; node->perms.num =3D hdr->num_perms; node->datalen =3D hdr->datalen; @@ -748,7 +761,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 c4a995f745..e922dce775 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -310,7 +310,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; @@ -359,7 +358,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); +void *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, int flag, bool no_quota_check); diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xensto= red_transaction.c index 029819e90c..c51edf432f 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -356,26 +356,26 @@ static int finalize_transaction(struct connection *co= nn, struct transaction *trans, bool *is_corrupt) { struct accessed_node *i, *n; - TDB_DATA key, data; + void *data; + size_t size; struct xs_tdb_record_hdr *hdr; uint64_t gen; int flag; =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; + data =3D db_fetch(i->node, &size); + hdr =3D data; + if (!data) { + if (errno !=3D ENOENT) + return errno; gen =3D NO_GENERATION; } else { trace_tdb("read %s size %zu\n", i->node, - key.dsize + data.dsize); + strlen(i->node) + size); gen =3D hdr->generation; } - talloc_free(data.dptr); + talloc_free(data); if (i->generation !=3D gen) return EAGAIN; } @@ -393,19 +393,17 @@ 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, &key); - data =3D tdb_fetch(tdb_ctx, key); - if (data.dptr) { + data =3D db_fetch(i->trans_name, &size); + if (data) { trace_tdb("read %s size %zu\n", i->trans_name, - key.dsize + data.dsize); - hdr =3D (void *)data.dptr; + strlen(i->trans_name) + size); + hdr =3D data; hdr->generation =3D ++generation; flag =3D (i->generation =3D=3D NO_GENERATION) ? NODE_CREATE : NODE_MODIFY; - *is_corrupt |=3D db_write(conn, i->node, - data.dptr, data.dsize, - NULL, flag, true); - talloc_free(data.dptr); + *is_corrupt |=3D db_write(conn, i->node, data, + size, NULL, flag, true); + talloc_free(data); if (db_delete(conn, i->trans_name, NULL)) *is_corrupt =3D true; } else { --=20 2.35.3 From nobody Sat Apr 27 23:54:08 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=1685438087; cv=none; d=zohomail.com; s=zohoarc; b=Vwld1MWTfZtf8sRlBpm7f2Xx6mLOaTVclsPeNQQnO4uHMVORye3Ma8BtNRG082eNDgJCkTOnWMDv0GncUlatHVFBDcJRugNI6vqdFJjiNepGGbrUdUo5ZkuHFDbrr0tEIO4SR9wQjWjro9kfGQwruO0hxct938DoGL1ieLXyrbk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438087; 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=pjORuoarGwKPikziqKKH/AVmLzsVU2PLqxuLJ4KIqno=; b=VIDlwp7IPJhzBUbwHZzxM2EO8txtOku06xDfrZaqfiUgoYcwHIFuOzS7i/k5oqVR+oh01ihin5Ej35+vXCItZSQLmsODZatB2zBI1D8srAZFyL6Qyh6RFSKlRqWZAmDh8gggTirNbxF3Sm2A/PRvHMXrOJnHBqzFZ6lV43TsgVM= 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 1685438087891103.66584540262033; Tue, 30 May 2023 02:14:47 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540978.843220 (Exim 4.92) (envelope-from ) id 1q3vQs-0001mg-JN; Tue, 30 May 2023 09:14:22 +0000 Received: by outflank-mailman (output) from mailman id 540978.843220; Tue, 30 May 2023 09:14:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3vQs-0001lo-Em; Tue, 30 May 2023 09:14:22 +0000 Received: by outflank-mailman (input) for mailman id 540978; Tue, 30 May 2023 09:14:21 +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 1q3vQr-0006nv-Cq for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:14:21 +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 5c8f7838-feca-11ed-b231-6b7b168915f2; Tue, 30 May 2023 11:14:20 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 B92371FD68; Tue, 30 May 2023 09:14:20 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 8CABA1341B; Tue, 30 May 2023 09:14:20 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id jE0BIWy+dWQXIwAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:14: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: 5c8f7838-feca-11ed-b231-6b7b168915f2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438060; 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=pjORuoarGwKPikziqKKH/AVmLzsVU2PLqxuLJ4KIqno=; b=eyzcjnB5RKUIttvDnaGpYy2j7xiggCSmWo8L6UwkBD3P+ZAQI4yt/NzO41yrot2UNjVKsF 2HksuslRvKaueUwAKL19RRk6LKSozXfNBF27+f/b655fqpotLsUhekZKD51uiZSXW/SFdo nTBa22MPJyH00p+1z5Mf0JxQQ/Vl5Dw= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 08/11] tools/xenstore: make hashtable key and value parameters const Date: Tue, 30 May 2023 11:13:30 +0200 Message-Id: <20230530091333.7678-9-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438089979100001 Content-Type: text/plain; charset="utf-8" The key is never modified by hashtable code, so it should be marked as const. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- tools/xenstore/hashtable.c | 5 +++-- tools/xenstore/hashtable.h | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c index 11f6bf8f15..9daddd9782 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, void *v) { /* This method allows duplicate keys - but they shouldn't be used */ unsigned int index; diff --git a/tools/xenstore/hashtable.h b/tools/xenstore/hashtable.h index 5a2cc4a4be..792f6cda7b 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, void *v); =20 /*************************************************************************= **** * hashtable_search --=20 2.35.3 From nobody Sat Apr 27 23:54:08 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=1685438327; cv=none; d=zohomail.com; s=zohoarc; b=ZXbS0qyJCb8JrPrMc5sCSCol/wD63UiFq0ixuGryTz0ASY5IVLGdRXU2bSC21GvgwRoPO9I4Dz634PIXaK3EwQuYE9EK7vEGKoy02X2v5xPfF3OOuiHwF7pi21oKHkKgOHX8EqRdbXdLPAaaWDLbzn7Ne2B9zu5IyqGw61tu8dI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438327; 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=Nn+RzqKwoWELmwzFwHD2wqJWR2WvrgIjhm6QTn0HxkY=; b=NSLsjAJUI8HUYDd7IavHe3CZCgKIrXzkN6DkENtiE5DYa5mkB6nQqL+5wV40Z/6MnR8Vdk+vw7pg118acB9NeTbHD4w3m0emKKWTrbUTTTiQkORsJy3pWEw1/gykfLHdklj9/DkieLJP8NUaYSu3RM3NAHwRy4hOnonGKYtrOYE= 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 1685438327109779.7260286604426; Tue, 30 May 2023 02:18:47 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.540999.843249 (Exim 4.92) (envelope-from ) id 1q3vUi-0004Df-Le; Tue, 30 May 2023 09:18:20 +0000 Received: by outflank-mailman (output) from mailman id 540999.843249; Tue, 30 May 2023 09:18: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 1q3vUi-0004DY-Iu; Tue, 30 May 2023 09:18:20 +0000 Received: by outflank-mailman (input) for mailman id 540999; Tue, 30 May 2023 09:18:19 +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 1q3vQy-0006QB-6Z for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:14:28 +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 5fe6ce42-feca-11ed-8611-37d641c3527e; Tue, 30 May 2023 11:14:26 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 52AE11FD68; Tue, 30 May 2023 09:14:26 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 255AB1341B; Tue, 30 May 2023 09:14:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id Wky5B3K+dWQgIwAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:14:26 +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: 5fe6ce42-feca-11ed-8611-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438066; 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=Nn+RzqKwoWELmwzFwHD2wqJWR2WvrgIjhm6QTn0HxkY=; b=XR7ELocM9rR1j1jqXlfZ/vmeXZBCtZKD9ux/KqGv0tgb1PX5OkqsxSO/nIk1+WEXmR8OJL R4oPf6R8j6kh5cZQU9AdXitfloUxNJan7syXnfzmmaB+yS0T40ywiYMBzwWBHTLMC2rNpM JWz2l1YSdXWbOr7ZYh5RWuNg/IMDmGI= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 09/11] tools/xenstore: add hashtable_replace() function Date: Tue, 30 May 2023 11:13:31 +0200 Message-Id: <20230530091333.7678-10-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438328564100001 Content-Type: text/plain; charset="utf-8" For an effective way to replace a hashtable entry add a new function hashtable_replace(). While at it let hashtable_add() fail if an entry with the specified key does already exist. This is in preparation to replace TDB with a more simple data storage. Signed-off-by: Juergen Gross --- tools/xenstore/hashtable.c | 52 ++++++++++++++++++++++++++++++-------- tools/xenstore/hashtable.h | 16 ++++++++++++ 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c index 9daddd9782..f358bec5ae 100644 --- a/tools/xenstore/hashtable.c +++ b/tools/xenstore/hashtable.c @@ -141,11 +141,32 @@ 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]; + 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; + e =3D e->next; + } + return NULL; +} + int hashtable_add(struct hashtable *h, const void *k, 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 +197,28 @@ int hashtable_add(struct hashtable *h, const void *k,= 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) + + e =3D hashtable_search_entry(h, k); + return e ? e->v : NULL; +} + +int hashtable_replace(struct hashtable *h, const void *k, void *v) +{ + struct entry *e; + + e =3D hashtable_search_entry(h, k); + if (!e) + return ENOENT; + + if (h->flags & HASHTABLE_FREE_VALUE) { - /* 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; + talloc_free(e->v); + talloc_steal(e, v); } - return NULL; + + e->v =3D v; + + return 0; } =20 void diff --git a/tools/xenstore/hashtable.h b/tools/xenstore/hashtable.h index 792f6cda7b..214aea1b3d 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, 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, void *v); + /*************************************************************************= **** * hashtable_search =20 --=20 2.35.3 From nobody Sat Apr 27 23:54:08 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=1685438329; cv=none; d=zohomail.com; s=zohoarc; b=VkO8As/Jhyt4RW1cGz1AQ0IRpNKz/nFjbdhNXnknbGIEGVwmUAShZcgAETbuzT5x5xLAb1OsF6V6jpyGz1QKgkfCHL0vaqGn2S/VUKqnn1nktVzGSluLdM2ZNk33OGjKLSMyqnOIPX0HPwufLJ3qJmSAJDUXMKfk7VtnAipUjmE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438329; 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=jGBTv27wqYZxcCghWenxih8jO9ehPYJw0cIhib+j0Oc=; b=bpVrjjZFFNJr9lSRrmEZbrDxHKYMDbvfEUz9RIHJe7IGd4Q+D5PgmbEEePJ0I2soMci/yvp+bi/U26tIY7GsM5KQ4a5cVtawviJ+IZINZaxhyBTaLm+ZN7Wr+LyHr8y4drpd9UoZ02eClHXlz2nzCi4JbPwOqnrWuehjqw38/ak= 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 1685438329885493.10420335518904; Tue, 30 May 2023 02:18:49 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.541000.843255 (Exim 4.92) (envelope-from ) id 1q3vUj-0004IJ-0H; Tue, 30 May 2023 09:18:21 +0000 Received: by outflank-mailman (output) from mailman id 541000.843255; Tue, 30 May 2023 09:18: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 1q3vUi-0004HO-Rq; Tue, 30 May 2023 09:18:20 +0000 Received: by outflank-mailman (input) for mailman id 541000; Tue, 30 May 2023 09:18:19 +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 1q3vR5-0006QB-1o for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:14:35 +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 634ac66b-feca-11ed-8611-37d641c3527e; Tue, 30 May 2023 11:14:32 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 052C021A78; Tue, 30 May 2023 09:14:32 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id C1CA21341B; Tue, 30 May 2023 09:14:31 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id lCScLXe+dWQvIwAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:14: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: 634ac66b-feca-11ed-8611-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438072; 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=jGBTv27wqYZxcCghWenxih8jO9ehPYJw0cIhib+j0Oc=; b=mSZ1z3JEaFcs4msBCAWI/Aiw2gq/fY+swh5rs+4zk0IhAWi/TntiN+GH0zLttJhewPO5Mw VQyVJOYyAal4Sab6zcXEuLP/ewFpJwgoYI4ixKgchkoP/q/UL4Sfv3OQ28MBUC/mj6n0jy jSQv0W05KooLMdXhjvlc+5NLebo2skE= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 10/11] tools/xenstore: drop use of tdb Date: Tue, 30 May 2023 11:13:32 +0200 Message-Id: <20230530091333.7678-11-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438331537100001 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 - 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 --- tools/xenstore/xenstored_core.c | 153 ++++++++++--------------- tools/xenstore/xenstored_core.h | 5 +- tools/xenstore/xenstored_transaction.c | 1 - 3 files changed, 62 insertions(+), 97 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_cor= e.c index 12c584f09b..9b44de9d31 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,28 +555,29 @@ 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); -} - void *db_fetch(const char *db_name, size_t *size) { - TDB_DATA key, data; + struct xs_tdb_record_hdr *hdr; + void *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; - else - *size =3D data.dsize; + hdr =3D hashtable_search(nodes, db_name); + if (!hdr) { + errno =3D ENOENT; + return NULL; + } + + *size =3D sizeof(*hdr) + hdr->num_perms * sizeof(hdr->perms[0]) + + hdr->datalen + hdr->childlen; + + p =3D talloc_size(NULL, *size); + if (!p) { + errno =3D ENOMEM; + return NULL; + } =20 - return data.dptr; + memcpy(p, hdr, *size); + + return p; } =20 static void get_acc_data(const char *name, struct node_account_data *acc) @@ -621,12 +621,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 +640,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, - (flag =3D=3D NODE_CREATE) ? TDB_INSERT : TDB_MODIFY) !=3D 0) { - domain_memory_add_nochk(conn, new_domid, -size - key.dsize); + if (flag =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 +685,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 +693,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; @@ -2352,43 +2352,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); @@ -2402,24 +2388,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); @@ -2479,12 +2447,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"); @@ -2514,7 +2481,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 e922dce775..63c2110135 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 @@ -236,13 +235,13 @@ static inline unsigned int get_node_owner(const struc= t node *node) return node->perms.p[0].id; } =20 -/* Write a node to the tdb data base. */ +/* Write a node to the data base. */ int write_node_raw(struct connection *conn, const char *db_name, struct node *node, int flag, bool no_quota_check); #define NODE_CREATE 0 #define NODE_MODIFY 1 =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 c51edf432f..21700c2e84 100644 --- a/tools/xenstore/xenstored_transaction.c +++ b/tools/xenstore/xenstored_transaction.c @@ -403,7 +403,6 @@ static int finalize_transaction(struct connection *conn, ? NODE_CREATE : NODE_MODIFY; *is_corrupt |=3D db_write(conn, i->node, data, size, NULL, flag, true); - talloc_free(data); if (db_delete(conn, i->trans_name, NULL)) *is_corrupt =3D true; } else { --=20 2.35.3 From nobody Sat Apr 27 23:54:08 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=1685438349; cv=none; d=zohomail.com; s=zohoarc; b=aN4j7d0zQNcvfic6z9NO1hdK52g3v9DaJKfDc76PqLbislZwASqz1r2A02kdtW0NYEXhsxkSrY2iQG/2j9fyTW5/ZYb3Ml/xzmFL43kRFADGLxniNtohJyesyiSjot2lrpem//dJWWMXaViWGw/XII84HSjvxXKqMGa4L19E8Jg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685438349; 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=aSLXfU/AoPiTGzsb+RuZdDli8vNIfyTTVNUUuWSxHFY=; b=Ad7rCTXzcbrF230XFE33E/w9j0mq6yqEsdB69ycuzEqRITUpA+fmno9l825CRWLv3M/IUvarYzYVTJ7zEEfJKK0LJiM2igLR6KGoFNaBV3jjg4dFz9DMu9jk5BxOLfN0sQrw6sdmDx7+utdfF36r48+XLhF4iiL+4h6EofaKI/s= 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 1685438349211558.7205232154029; Tue, 30 May 2023 02:19:09 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.541010.843270 (Exim 4.92) (envelope-from ) id 1q3vVB-0005Ju-An; Tue, 30 May 2023 09:18:49 +0000 Received: by outflank-mailman (output) from mailman id 541010.843270; Tue, 30 May 2023 09:18:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1q3vVB-0005Jn-7K; Tue, 30 May 2023 09:18:49 +0000 Received: by outflank-mailman (input) for mailman id 541010; Tue, 30 May 2023 09:18:48 +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 1q3vRC-0006QB-8J for xen-devel@lists.xenproject.org; Tue, 30 May 2023 09:14:42 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2001:67c:2178:6::1d]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 66ac050e-feca-11ed-8611-37d641c3527e; Tue, 30 May 2023 11:14:37 +0200 (CEST) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 A7AB41FD68; Tue, 30 May 2023 09:14:37 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (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 imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 6BD831341B; Tue, 30 May 2023 09:14:37 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id dLv8GH2+dWQzIwAAGKfGzw (envelope-from ); Tue, 30 May 2023 09:14: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: 66ac050e-feca-11ed-8611-37d641c3527e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1685438077; 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=aSLXfU/AoPiTGzsb+RuZdDli8vNIfyTTVNUUuWSxHFY=; b=biqWcJIlFsLu8hU7Vvr2crtHmjVSONPR0njNfPOpYnmJDSGLCubhaApiyIdFkLxdYCuZZV bjxqUYE2pgIfquy8lMTKorkG4c11BPWyJL6oF1WD77IXbwgMjis/ln+Bhuod/1Br7VcdSD 0V2M8pdnJwTTwLVpvFflohLDoIlj7tE= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Julien Grall , Anthony PERARD Subject: [PATCH 11/11] tools/xenstore: remove tdb code Date: Tue, 30 May 2023 11:13:33 +0200 Message-Id: <20230530091333.7678-12-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230530091333.7678-1-jgross@suse.com> References: <20230530091333.7678-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1685438349917100001 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