From patchwork Thu Aug 8 14:06:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13757515 Received: from fhigh4-smtp.messagingengine.com (fhigh4-smtp.messagingengine.com [103.168.172.155]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14097148307 for ; Thu, 8 Aug 2024 14:06:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.155 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723126014; cv=none; b=X60rcroge+r82OxGUIAG2ucyymJ9e5Z9NZwpEol6f9wsygr3h0Sj2iinry0rRfrRm0NDRY0VZJjX87tf87D4Ivr84XFhm1DGCPD4zrVg0j8LanJqouIY0H6wF+nalKl0rEBGwSEZ3eJzFrjZwmB/Djaq+7xF2Uy3FxviyZQh3Cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723126014; c=relaxed/simple; bh=t4yAJCX+7IgPbw8zA5TXYoyHUrkDDHWQYBoBpEAHyKE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=kvJtG/xBIO+OcM0IvvxRfROWFLO8JX5U28Eay7pzZTpTSSxoY89CTIhMbkEAdXeT6pqO66uUfBzZPJPzosxq2WlvmozBwTnExvAx9K53vE6pmcr5ffmGpLlgouYGkC2ZZYlKlw5CYuY8hdGE3pFPnk4jWzDYY5hLMzhYKrpLR5Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=DnTPIh5y; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=rp+HHL3z; arc=none smtp.client-ip=103.168.172.155 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="DnTPIh5y"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="rp+HHL3z" Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 2E65411516C8; Thu, 8 Aug 2024 10:06:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 08 Aug 2024 10:06:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1723126012; x=1723212412; bh=/VNKo+zVFR BiaLdr9WUcsmyqqfXbhZljSe8XhVaIKG4=; b=DnTPIh5yqMah9hC/IlsdWACEOg AP3h/HFwp63gb+dFJjZiFfuJCH1kgUjeRMKYEALN2hMkK8AkMAZDl2GiKRYO8kAQ 1mpl6+pufcarWaYuQstrQ1kd8Yi9WovkMCjJrXFD2Aq4bUvBmbY9AMusmmZ/4a6s Ny5itFqdAxrcvzrVQ1C3FB9k9Ju9xXAKwvTh2JiIEkeq/qCwOkt+1ObtIWP1GPsL wUorKzD7hWuh8r4QnJpc8X8a46aECkneNg39q5lS43LqEcU5kkIhYLoAF5fzhYAI dF/lqP0E5uVmlmWHXOvA48cfXOFXYJusHLcsY0n2FI8iN4D7dbtvI2n+JrNw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1723126012; x=1723212412; bh=/VNKo+zVFRBiaLdr9WUcsmyqqfXb hZljSe8XhVaIKG4=; b=rp+HHL3zc8SEgZ5YvMk7zgZb+H4C28qkCTRHSBalLdkv 00Vodte1qEH9xoHAT3wALyY3DT4R3Pbgr8dgM+aExWOQyb8QZXJZHjdF/y2GBwn2 ko+E/dCpUnf3J18yImSW61xCT7ejo9AHYY7bX+y8cwGeQguke24TRvCOd4sZqzle IULLRS92sUUV1ukal1PgWkbTFewMTnUSNnNac4f7ObRm6roJWAJLHl6CE1McvxgL VzgvEUAGXwGvIms5Jv/hv+QstgvHCocFjVP4ZoblPdJGvFu/YYE/pNlmiwoR4L1I FWiQl2YT9U2jB0i60KS4mSWQCeGG9B4i13GN8KzW9A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrledvgdejvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecu hfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqe enucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeeh gfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedvnecurfgrrhgrmhepmhgrih hlfhhrohhmpehpshesphhkshdrihhmpdhnsggprhgtphhtthhopeegpdhmohguvgepshhm thhpohhuthdprhgtphhtthhopehjlhhtohgslhgvrhesghhmrghilhdrtghomhdprhgtph htthhopehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepkhgrrhht hhhikhdrudekkeesghhmrghilhdrtghomhdprhgtphhtthhopehgihhtshhtvghrsehpoh gsohigrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 8 Aug 2024 10:06:51 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id c7076626 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 8 Aug 2024 14:06:44 +0000 (UTC) Date: Thu, 8 Aug 2024 16:06:49 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Justin Tobler , Junio C Hamano , Karthik Nayak Subject: [PATCH v3 7/9] reftable/stack: use lock_file when adding table to "tables.list" Message-ID: <37a757649a621ec193e27ef2939f1d79aaba818d.1723123606.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: When modifying "tables.list", we need to lock the list before updating it to ensure that no concurrent writers modify the list at the same point in time. While we do this via the `lock_file` subsystem when compacting the stack, we manually handle the lock when adding a new table to it. While not wrong, it is at least inconsistent. Refactor the code to consistently lock "tables.list" via the `lock_file` subsytem. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 9ca549294f..54982e0f7d 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -567,7 +567,7 @@ static void format_name(struct strbuf *dest, uint64_t min, uint64_t max) } struct reftable_addition { - struct tempfile *lock_file; + struct lock_file tables_list_lock; struct reftable_stack *stack; char **new_tables; @@ -581,13 +581,13 @@ static int reftable_stack_init_addition(struct reftable_addition *add, struct reftable_stack *st) { struct strbuf lock_file_name = STRBUF_INIT; - int err = 0; - add->stack = st; + int err; - strbuf_addf(&lock_file_name, "%s.lock", st->list_file); + add->stack = st; - add->lock_file = create_tempfile(lock_file_name.buf); - if (!add->lock_file) { + err = hold_lock_file_for_update(&add->tables_list_lock, st->list_file, + LOCK_NO_DEREF); + if (err < 0) { if (errno == EEXIST) { err = REFTABLE_LOCK_ERROR; } else { @@ -596,7 +596,8 @@ static int reftable_stack_init_addition(struct reftable_addition *add, goto done; } if (st->opts.default_permissions) { - if (chmod(add->lock_file->filename.buf, st->opts.default_permissions) < 0) { + if (chmod(get_lock_file_path(&add->tables_list_lock), + st->opts.default_permissions) < 0) { err = REFTABLE_IO_ERROR; goto done; } @@ -635,7 +636,7 @@ static void reftable_addition_close(struct reftable_addition *add) add->new_tables_len = 0; add->new_tables_cap = 0; - delete_tempfile(&add->lock_file); + rollback_lock_file(&add->tables_list_lock); strbuf_release(&nm); } @@ -651,7 +652,7 @@ void reftable_addition_destroy(struct reftable_addition *add) int reftable_addition_commit(struct reftable_addition *add) { struct strbuf table_list = STRBUF_INIT; - int lock_file_fd = get_tempfile_fd(add->lock_file); + int lock_file_fd = get_lock_file_fd(&add->tables_list_lock); int err = 0; size_t i; @@ -680,7 +681,7 @@ int reftable_addition_commit(struct reftable_addition *add) goto done; } - err = rename_tempfile(&add->lock_file, add->stack->list_file); + err = commit_lock_file(&add->tables_list_lock); if (err < 0) { err = REFTABLE_IO_ERROR; goto done;