From patchwork Wed Jul 31 14:15:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13748837 Received: from fout8-smtp.messagingengine.com (fout8-smtp.messagingengine.com [103.168.172.151]) (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 EC9591B1512 for ; Wed, 31 Jul 2024 14:15:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722435322; cv=none; b=V0dbu6Y4oQ1wNroOL0fTCuCgpboc7q7vptZnDhtkbIP9mtaOUy4rnJ1K88xuwhPd/GO0WB3oU9EnGWlDJQjgocLngy41v/U8WvRzF94rPSjVtHKCmdkIitu1WB+ENrv0RLX0ENgOWpT7fW6+Hnhm8mEaE3oaPPvyKl0QVaHhzzc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722435322; c=relaxed/simple; bh=+x4xHX5edfHpQX+vj2n6XFJJRzH9zhxGX4ho8aKMwEA=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=qH7v30AtoK0EkwX47swWu8Es07rl3wFtOXigzYXjz3V/CnzKGjMix4qwAxE30zjLjttxODtr2iWDLcz6Ist7QNaCEyk39hQbmkBDrqSEfU2KUotQIEQ+lttFnYeDGuYi1dF5ZaKsE0MFQWBzoISq1IGB1cb6pp1iqQZWTnYAhYM= 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=UKhllHAv; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=t/lbT6mg; arc=none smtp.client-ip=103.168.172.151 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="UKhllHAv"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="t/lbT6mg" Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfout.nyi.internal (Postfix) with ESMTP id 5E714138215B for ; Wed, 31 Jul 2024 10:15:19 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Wed, 31 Jul 2024 10:15:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=1722435319; x=1722521719; bh=tRTLmq+AjB oBCztpJO2rvaf7ZsfDOIGoj0+C0AXkxII=; b=UKhllHAvkbGhXGxpstTOCY7Mfo OfbP1c6JFxViTwNxlnimQIApTO1+SKVpMoGgybzlAPIHm8Tw93JWN/Mu7phtz8Pk o/Mg5NEDd9bi0y7LTzzyvzR3uVY+vqitzkb0eXx+qoCTM5b+Wygy76Nz8JMLlmcY 0HNa95x5veB8uM5ISqeLR1JyUDqHzSQ6ti1eik2uYauX/Te11LPy3IyJkr3+7hSs rdwcnscuMuGwRLOb6WbyAGVrxJeppHXPdTqKc9FEUASGcKChaNh/Gv9qbZa/TBoc mCHC78NUOytlwXMpJWJGnXGZN4M6b5WhSnhOPNfQaO5DWNrvl39goyDGnpog== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1722435319; x=1722521719; bh=tRTLmq+AjBoBCztpJO2rvaf7ZsfD OIGoj0+C0AXkxII=; b=t/lbT6mgIh8YtrU/JAilc62d/W2YtoN+lJUAYC/WIoyw h4IBQ3uvK5nIOLeXwl1OSxOXZyEh/CddK0pIjf2GGYoBET+TRy/4V9KSDejWUhdz P5TTudP9AqoT6cty56Mn6CKNcKKdU4F3IsZCy1ecag9ET6PjHTrgMI8aCHsfAImh TaFUzkFi3fnsLAUBKoBVqFHdVnoR8VID+eKzJq1Ht4mFZVUTuVAfEzlMfqz5PPJv U2YbRIKTdURDaXsLhXMIdqpCFAjgp1pGz5M1ht20+1XZRR6p+Cq3oguidabj9k82 T1nCpnuWz2RVQSQ1Mf1CciqItVnJJfmw+7IoNaKVeg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrjeeigdejhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepfffhvffukfhfgggtuggjsehgtderre dttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhk shdrihhmqeenucggtffrrghtthgvrhhnpeehgefhtdefueffheekgfffudelffejtdfhvd ejkedthfehvdelgfetgfdvtedthfenucevlhhushhtvghrufhiiigvpedunecurfgrrhgr mhepmhgrihhlfhhrohhmpehpshesphhkshdrihhmpdhnsggprhgtphhtthhopedt X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 31 Jul 2024 10:15:18 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 70f74262 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Wed, 31 Jul 2024 14:13:48 +0000 (UTC) Date: Wed, 31 Jul 2024 16:15:15 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 6/8] reftable/stack: use lock_file when adding table to "tables.list" Message-ID: <9703246156152e1c8c92b4237c8dc3a9ef59901a.1722435214.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 consistenly lock "tables.list" via the `lock_file` subsytem. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 9ca549294f..9cc91a262c 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; @@ -674,13 +675,14 @@ int reftable_addition_commit(struct reftable_addition *add) goto done; } - err = fsync_component(FSYNC_COMPONENT_REFERENCE, lock_file_fd); + err = fsync_component(FSYNC_COMPONENT_REFERENCE, + get_lock_file_fd(&add->tables_list_lock)); if (err < 0) { err = REFTABLE_IO_ERROR; 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;