From patchwork Wed Nov 27 15:28:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13887143 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B58FD6ACE9 for ; Wed, 27 Nov 2024 15:28:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF6C56B0085; Wed, 27 Nov 2024 10:28:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C58D86B0088; Wed, 27 Nov 2024 10:28:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD2A06B0089; Wed, 27 Nov 2024 10:28:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8FADD6B0085 for ; Wed, 27 Nov 2024 10:28:23 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CF3DD418D6 for ; Wed, 27 Nov 2024 15:28:22 +0000 (UTC) X-FDA: 82832256360.11.1954CD2 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf04.hostedemail.com (Postfix) with ESMTP id 3779140007 for ; Wed, 27 Nov 2024 15:28:14 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GqMjro8o; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732721295; a=rsa-sha256; cv=none; b=EuHuQLFAJuqmc+ODFawAMdoloAj5ZbZcl64txukjEQmu6K6Nuf6urbbHydzdNQI2FEg4Dx Jz5oNZ9HSyKv9KmxA/oKi3NDy465ANfGAJEG9/8WG6SHJzjqUa8ax8hxs4+1kW3DMlGTZv zX4P4KM6YViGAK7AfpEAJ4XY+SQfNkM= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GqMjro8o; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732721295; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vg3hQgOI0fVKCLF8XazcHrwtTTk7j4qHHHqA/NO5wZE=; b=ZQw86GzCmF6t8TGAHfCmpqCE3QN1HPySMBE7JncO+NXMkzmkLaX0HHapIqfHcwNrCehGP2 UBC1nJvdQXbUXOHw4Xi7leUWNvVlXWQb0FOl+JT7oHIja3mNiZbSOJhK3HXzPBhbuqtT7l Wy1p/OsspJvU6xdwC7xnHIjUarRdpoI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 84C53A4392A; Wed, 27 Nov 2024 15:26:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14A4EC4CED4; Wed, 27 Nov 2024 15:28:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732721300; bh=NPVXYksZkbK7cP6WTdZWPp256P2B4FMiN2imk2oaSkg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GqMjro8ojkY1tLpJcZlcUCbWHypMHq37p4mKe4wKeV9ANga4aylCBQE5UXv9iYKgk H1l1QixsymlDu3V1hiAXlqQ+p7QdbeKFcSDKwZLfBJkqKPEtAZbS/0zJ1A5o+Ud9qh u7JN52eMlk8g6rwlpELRbm82tdppU3TV3hpeD9TlLb3IYDi8RMJ7YE8OmDX2u0aEvf Niovao4OIXLG6o/hSiAPHrpsys5AJQjzRXjcxcosIpPWYpk2lyS7FAz/VE13TvCiwz vIVmGM4GwB0ZQ/IklvvUcFWv2pqPyGzdYlEZ9xUf4Ze0PrIz9bToqLxo+7KqJpD+I4 EWbJYlUQFuyzQ== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever , stable@vger.kernel.org, Jeff Layton , Yang Erkun Subject: [RFC PATCH v3 1/5] libfs: Return ENOSPC when the directory offset range is exhausted Date: Wed, 27 Nov 2024 10:28:11 -0500 Message-ID: <20241127152815.151781-2-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127152815.151781-1-cel@kernel.org> References: <20241127152815.151781-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: gzbks3i98br3wacdrcpa4trqg7dybdco X-Rspam-User: X-Rspamd-Queue-Id: 3779140007 X-Rspamd-Server: rspam08 X-HE-Tag: 1732721294-830363 X-HE-Meta: U2FsdGVkX184MJEoNxQ5xHnXT+lQ/vjxbFJYo+2h9U1/qMWRisG7mTSNYCM8otTG6TbsH1MMLFN4mTY9P2svCCgAkrQ8Flie1/12emB5keu0lFFzjJjhDWdYW7bu5FMpD9sd4fozVIOjfBz4IpNFqiRs+OH3C9qmo8cyo0jSuTfI2u8A2P7l08ffFY6VheUppqyJ2DGl2TP9zk5jBDTpSpDzTOJsJ9gg4RAE296Gu/Eep95P1yAhkH7k0EgT6VLb3uaR+9WmNXjCSUXTnwoGooR8cxM2wO01cBfx3ZGicEV5OIdbXCZNDtF9lKZKMVR266mBFzw2SBBRmZc8YNZlNici4WKwHNVEqdfGuNzVLxcusfS05Oj39rg0V5JPYlMCm74O+rLFfPKYagP9cJbZLMxNbPAvzY2oYR87ql9Ce4nGxHjtxEptZg6iaLbxSYiFAhRxKBTMG7ymsN70NrlEAtuvrRI76B+PH2Y8wUbw+dJSHPKRjgkb48iGs0p46UirmLl2KcNwVWjEpT0Z8T8CPt8KRmc9j7uYL1FICU9OZtYzxhKPHdlNsxIysIZBY+eEUZ9IwEekwH2BfoMgwow9vBPTKVOFcuEUauer8HNcq/rn5M6yYG2tUTh3BILE6lScSvUJAe0CZJcBKt+Mx2/X9JUmkse2A8vaPx0Qp9QxyHwCgjednG2tXQG2jOZTEa33wf6QRYRFPXdEFLx2m/4nUL1+sy8D/npcGMsSSZ5acuVWjz/7dvvC14gzrS9zTU16sDwche+OZzfuXleAOmdp92zBjZa/PAmVRXTi9qtxboyRjrTXH6fMWf23KU23eNOg9o86IUi7nAAjBK0kVLj7gRNk15LhbIR9hMlVbp+6MADSoqYlS6QN8SXcWgrDBbvSkKIagBdDaXT/oWsQoonNacXV+7RdYtBOt8GkkvIOk8Eam5VZQNwoW8U1NoZnrfMO27z2M5eLXtR4iaYVG7L mjt/c8vQ yfeH688tFN90eyjSbwN2HsnQGjQwwR8z7wo9ZKcn2I4gzKdDBIyo9QneC7qqHf5EEoIBOJYXoGFs54BD8AkKP538l3GC8mIc3zNx6jcyPfoSAWWOk1sfFs7rpwgxVi1D7vtP3nGoV9iSWv0i06uIkRWHGtzsSKS1jQzy3deanGVkrFfBh89PlhI86W0wNlQsrLf43+E0TLuBaHbCyelXdKvqEP1FjpcX88854lbpYBqqFhrR2bboWffQ/xrO0sNemVCtkd+stvt20d7eKNAvc9eIM8Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever Testing shows that the EBUSY error return from mtree_alloc_cyclic() leaks into user space. The ERRORS section of "man creat(2)" says: > EBUSY O_EXCL was specified in flags and pathname refers > to a block device that is in use by the system > (e.g., it is mounted). ENOSPC is closer to what applications expect in this situation. Note that the normal range of simple directory offset values is 2..2^63, so hitting this error is going to be rare to impossible. Fixes: 6faddda69f62 ("libfs: Add directory operations for stable offsets") Cc: # v6.9+ Reviewed-by: Jeff Layton Reviewed-by: Yang Erkun Signed-off-by: Chuck Lever --- fs/libfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/libfs.c b/fs/libfs.c index 46966fd8bcf9..bf67954b525b 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -288,7 +288,9 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN, LONG_MAX, &octx->next_offset, GFP_KERNEL); - if (ret < 0) + if (unlikely(ret == -EBUSY)) + return -ENOSPC; + if (unlikely(ret < 0)) return ret; offset_set(dentry, offset); From patchwork Wed Nov 27 15:28:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13887144 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3B0ED6ACE9 for ; Wed, 27 Nov 2024 15:28:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F5FE6B0088; Wed, 27 Nov 2024 10:28:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 37E486B0089; Wed, 27 Nov 2024 10:28:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D2566B008C; Wed, 27 Nov 2024 10:28:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id F0D686B0088 for ; Wed, 27 Nov 2024 10:28:23 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A9975418F0 for ; Wed, 27 Nov 2024 15:28:23 +0000 (UTC) X-FDA: 82832256318.30.A82F732 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id 27EBB40007 for ; Wed, 27 Nov 2024 15:28:13 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uzrIQLw4; spf=pass (imf27.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732721299; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XmcVEnMjwO3f2OOD9E6Y0+hH6fL+F7ARSgj9E9galIU=; b=xXAKkNvBYu4ebOkIpBwlKdLaJg2iFfR2DDtCHcjAwyvorg/YEPrXbrVnEmjMq1LB6IqfKH mztVTCjs/S/+plrD2skma1ZshJoZUIsRtZB33bK4TlErSWhlaN3ZUXSGQkSwq1pcd6fnFK qk6fzO6X5XpVsgifWrKbMmPumIF8C94= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732721299; a=rsa-sha256; cv=none; b=7HhJhVS0CIHDZ29b1TKNdWN/T+w0FLddnQrtMbom1YImV0R02WhMg0/aLVY5fDIENOdGQH 0GWms+Q8otZlxe1Q7O4D5av8JUgt/gEyzRLaHSsLKta+DR3o5lxNuxs2PTeT0bA3EY/Gvh vlNFA5poKVzVKP3Y9wvzBqYIxvWf86I= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uzrIQLw4; spf=pass (imf27.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 015F55C313E; Wed, 27 Nov 2024 15:27:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 49030C4CED2; Wed, 27 Nov 2024 15:28:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732721301; bh=aJTYjqVoUaXrqckTrck4WCU2AgH83lVUIj4TdCwyW2I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uzrIQLw4Jw7yzpy6ZXPe3lXaRaa6goSV9ivXdsUM8ksj/HydQlqJ4WWkEW9MZGjVR CRD7v+S9HNCXHNrp+a7juwjqJsekk1Obmyr5Cyzkj+KTZdBnCF1e2gExkRo0BOVWEL bONFucLAdSkZqFjbtHRnET8KdeZObE4OQR6y19wYXVqAD8zeYxroq3WdLeFDdbRxji Ma0fzQcfCDBgZ9xqaBtb+Co2HcLC6ddaEpir81jYH6hHGhjAsD0+UFvM5N5H8fI1MW le88zehs5l7qVf4fAV15yWU+OoLIs8rNVNBBlyFnUQPxVtY9RLvCKOlxtJpJJd3uox uF/pVBAOV68Wg== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v3 2/5] libfs: Remove unnecessary locking from simple_offset_empty() Date: Wed, 27 Nov 2024 10:28:12 -0500 Message-ID: <20241127152815.151781-3-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127152815.151781-1-cel@kernel.org> References: <20241127152815.151781-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 27EBB40007 X-Stat-Signature: n7sasw4n8i7pfkdtfnuddhaucs9wuwob X-Rspam-User: X-HE-Tag: 1732721293-99681 X-HE-Meta: U2FsdGVkX18azC7A+NQgqPE0LGgC3jTCb1NOuSrRwMw6UtVMX4H3x2nh/4vXUAOmj7BxhOSmEbfpQCDcNcBO0K8t9bU8vc8VfRqUi4KWV9ZmSZ9RUO+UyWHX1y6nsoRaZUHRKScoFRSq8XO4qCe205NwXAi8oyPw9XexGGdtEETIZ+/kP389Z1Su/gGpwZ6/a2wWDMUWnHAlBN79yhbxyV+4C9nvBJZ3tetrWRJL1gUV9VoiK80OUYVMGsTuZl89UwDpmpRD2hVyLKl3PCiI/fP2VS6C64B86DCa0rs9z98t93IQWxwMjj4ykSOrvVQaZYx8ecjlWgffYgd1qzVG1DZfbdD8wfPrXU0OThfijXsxvH2vEdgFsf5Z4p2FctmSmonpyouWIdjvkczIC44BxPnQSbiM5WcYkS+wKj6lAfvWyu5GXXJX49xdUSPcE7aSI5nSxa93gmiVrjmk7c8v8HgMylXxFMuv1aHW+voQ+NyvctxjJoUD+On/+FP4AAugoiRtL7Sltg/Krlc9W8cc8Malq6T/5ZyJEgpflnAotDleWlQRQdCD4vlCDpDG8sQhzkxxQMExcHNKlMNQ3fRr9W5k31n/X6R/JHAO1mdoFt95hz4NGETBAngUrFd4GVqAJV1XNBCWxEuqpdJ5OUeFed0k1vr9QsveOgIDiEG7nWiR0w+sK+6/887kBFNFxPOTR9DEJ4iXLiJbLENSM3guwdiHjhaIe2SWR5Mn0cQivjsnFH2H4moedRiCsXoIKXgSRi+RA87W7OH4vufXtkpezlqJlTO/q+E00tsoZtY3c2+bK9T8Ape2aS/6cJjsa0JoDqa74bDuyog9mO3LVbTj81wO4iTYivQzokPxHhC4yQWtwzCTZe2I3Uj4rJeLpriphkoVewmg7DxMbY0LF4hqakoSblLcMmccZ2OnYmQstRAAxsaAFgjXzdWoU1wEdIKsRoi3lHiW59/XZIvNPoD k84VF4fN Yl84Q8Vo2l7B547msG1nrrdslnR65jkfAlpJrUlEkktYn1tHrzdJci721klDeew184iaPecFPIVN4LgqP6caNLvsjhxew3xggT9AXY/N5z3vXwOL1IoKHmD11GFjayyolYoA6mf7eU9zdmevzvc1TJUfiBQLvrBeMfGFdtypaaLW3LfWGmfM0WbeXUruovU5956b/B83PRkekKL0yKfhSrgueW+HLo1CB0eoPzaXpDkXZzETxj5RLDlYu/4sM8k23cDp0DJCuTZHkZrMbb+OJrax19Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever I hit the DEBUG_LOCKS_WARN_ON() in hlock_class() several times during testing. This indicates that simple_offset_empty() is attempting to lock a negative dentry. That warning is of course silent on a kernel that is built without lock debugging. The simple_positive() check can be done without holding the child's d_lock. Fixes: ecba88a3b32d ("libfs: Add simple_offset_empty()") Signed-off-by: Chuck Lever --- fs/libfs.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index bf67954b525b..f686336489a3 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -347,13 +347,10 @@ int simple_offset_empty(struct dentry *dentry) index = DIR_OFFSET_MIN; octx = inode->i_op->get_offset_ctx(inode); mt_for_each(&octx->mt, child, index, LONG_MAX) { - spin_lock(&child->d_lock); if (simple_positive(child)) { - spin_unlock(&child->d_lock); ret = 0; break; } - spin_unlock(&child->d_lock); } return ret; From patchwork Wed Nov 27 15:28:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13887145 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76D18D6ACEB for ; Wed, 27 Nov 2024 15:28:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C2826B0092; Wed, 27 Nov 2024 10:28:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 024FB6B008C; Wed, 27 Nov 2024 10:28:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF4DF6B0092; Wed, 27 Nov 2024 10:28:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AE6836B0089 for ; Wed, 27 Nov 2024 10:28:24 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4D4B91C793B for ; Wed, 27 Nov 2024 15:28:24 +0000 (UTC) X-FDA: 82832256234.03.972722D Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf11.hostedemail.com (Postfix) with ESMTP id E0AD440016 for ; Wed, 27 Nov 2024 15:28:15 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OUF1FZnE; spf=pass (imf11.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732721296; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=faVLgSYD5zgubge24QYkBt9ct7o8n5VsqyIvRy82wAE=; b=Z6vYsJYTQjT9fh1Y03/RvKvljETcj3BVbklkqvaQvbhHdQEAhKbXcZi1jttZba92VOkK35 bwhSiRof8s3tj3EOb+Op3+o7g2LniFtJASbuhr9q2X+clpFTZ1HUeu1AygetgJc5CQkDJa Em3G1d5pjoqFCeDjxxQeU1SU7d4Gcu4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732721296; a=rsa-sha256; cv=none; b=3DmpRbEEhVXJSOJuZi891Q9PpcUfQ7HuS4I4CDSllXs9F5SW69HUgtogGpky5TmpjkimL0 3XcKuSHwwpuKjAKsjigVk4HAbUbtznFGP+ZhlAfUz8XI2wwuyuGM5UkJNFUpG1RRh8Udcg BZryoizqi9AZfncviSi/d0bgzi5Q+pU= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OUF1FZnE; spf=pass (imf11.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 51326A43930; Wed, 27 Nov 2024 15:26:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 417F9C4CED4; Wed, 27 Nov 2024 15:28:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732721302; bh=VKlx/q6S0TZnqYbdlXcPYo2H5Z6zTd8jSftplsjeB2k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OUF1FZnEbEpTpG79Oe/YjbbeeVfCsXJ/5xBFkexLOTBtxQVGlfx9MOJwh3G2/j/by UYpk2NVmI1OKMIJ7nv+n3tQnlk0qayYy4skYlF6AfnIk+FcSfxCKxwyse3Z2xLthro ljQctQEOJeORXjPDZnfmr7M2dgXbM0vocnmmVjq9ZueoUhhpTxPrY+f2lgZ9nUVTiy 1Df+8URO5rh2kPGypKBxQ5BMX6PiCCd4sMyxsMgQuHFJGZwkvZjvzY8AINLKwTQs/c DhXZdZysUC89taQn6jHsQa7UAL0gArN4EXazDKH6Q2UlEvpLDn71hi2vxH8QyVXmL8 4VrymohnnAQbQ== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v3 3/5] Revert "libfs: fix infinite directory reads for offset dir" Date: Wed, 27 Nov 2024 10:28:13 -0500 Message-ID: <20241127152815.151781-4-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127152815.151781-1-cel@kernel.org> References: <20241127152815.151781-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: E0AD440016 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: un69hux5t9m1wdbtodqa97y6xm75moya X-HE-Tag: 1732721295-911506 X-HE-Meta: U2FsdGVkX1+FcQET/6NCBHkdqCcYhqCbb89I0CO7k28onTRjlxl+1CtolgMlvQsX1sXgi4V14wqNMoA2LysCcZqdsLXupeCf/boIm7QmECdp3wZoiaEH/lOAfgQ402ogcRtj06rfetr6dPFJWCYEpT4UJA1Tr2JMfzZWaw5OKRBcWmzDAMCPUSiYeiUusogR569Qm80m/H2l3Vd3r1hzSPWqJE3cfNBFNVgn6VRpxrQf1/GquZPmDbAN2pU5O04fHwtjf9o5RPJf15mGqJLagdnyfQ0bfXv4oh7PHMAArCXVGHpw5JBZJM/NogIX/Pnst/YSGoGq5/HYSNe8SCF5DHdD9rQN5vdSY7bADQorjEV1rll1GrxaCaP1XSjFy2CVc/5kbD2CeFlostnU92altyXP6dnLuL7c9o4PXtx3jEGD8WK+tR1PDNNpuZdjt59bzlP97XpavcYGUCA2PvuUng2LldyIGVVIRy4VVt4nrgJnwpSte25fKflG0tBCiNyvizT700p9RCGk+z1ka8Z+vWQEwn5q3fZrHaLHNLt4soN6TcxE6DhsTshh199vv9OjCN4iaPb1Mg3PfXVVJHe6GUP23SxUr3uK/JCkIDGSJSvhtQ1ENhnLo8XUhs7ppjxPRBLZgMurlSeJ3JXmZWy6CZ2paJe60Xz+syGobFaUxnu+pKdw3xiqhSJv7nZrDFCUkATwcyONFX/e+bqrETwR3rqvhJenB6PoWmcPf+MXUHKSDg+Dmt0sQRemdMgkltZixPAPKUjlEYNdyD5UXZXJ1dJK5kqgxClzxGUWayDhdM3HYxjWXgm9DDc0BGL2xP5wQVc0jFsOgR9CI4v84Lnb19j+fermfB2CqBUAi1Z9m0a3+UfI8U6ai8bz1pmigAuRx5pdmfSangIQ7dDF+BGdYewDsyoAnf86h6QY8gotcETT25WwKSn22RIKclZQiufkaFg+e/OEbZaxrmTnB6x 6GWA99wh MsQRV/kC3sxWRVI94LNaehJGjzd/bdbC+A0MoC/vcJxrlmHn3fmc9/fMZUTzo2tYCnnYLvsipcuP5T8uBrN8qdzqS2z+aWn5bC/8Gm05BUaarzfcRjh3YTNLzayWoAhh3wtVFJbcWqraEJDEjjY83GAiciZts/T27gqnAk1VMvUP4K/toQ/Em8ErkjKYxkzq3h+1FgqCAlJOtyiC8uozpYr/ns0Dr9zbrYMyKWTEGgdY1/8A= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever Using octx->next_offset to determine the newest entries works only because the offset value range is 63-bits. If an offset were to wrap, existing entries are no longer visible to readdir because offset_readdir() stops listing entries once an entry's offset is larger than octx->next_offset. This fix is effective, but it would be better not to use next_offset at all when iterating a directory. Revert this fix to prepare for replacing the current offset_readdir() mechanism. Reverting also makes it easier to apply the replacement code to v6.6. Signed-off-by: Chuck Lever --- fs/libfs.c | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index f686336489a3..a673427d3416 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -449,14 +449,6 @@ void simple_offset_destroy(struct offset_ctx *octx) mtree_destroy(&octx->mt); } -static int offset_dir_open(struct inode *inode, struct file *file) -{ - struct offset_ctx *ctx = inode->i_op->get_offset_ctx(inode); - - file->private_data = (void *)ctx->next_offset; - return 0; -} - /** * offset_dir_llseek - Advance the read position of a directory descriptor * @file: an open directory whose position is to be updated @@ -470,9 +462,6 @@ static int offset_dir_open(struct inode *inode, struct file *file) */ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) { - struct inode *inode = file->f_inode; - struct offset_ctx *ctx = inode->i_op->get_offset_ctx(inode); - switch (whence) { case SEEK_CUR: offset += file->f_pos; @@ -486,8 +475,7 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) } /* In this case, ->private_data is protected by f_pos_lock */ - if (!offset) - file->private_data = (void *)ctx->next_offset; + file->private_data = NULL; return vfs_setpos(file, offset, LONG_MAX); } @@ -518,7 +506,7 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } -static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, long last_index) +static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) { struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; @@ -526,21 +514,17 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, lon while (true) { dentry = offset_find_next(octx, ctx->pos); if (!dentry) - return; - - if (dentry2offset(dentry) >= last_index) { - dput(dentry); - return; - } + return ERR_PTR(-ENOENT); if (!offset_dir_emit(ctx, dentry)) { dput(dentry); - return; + break; } ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } + return NULL; } /** @@ -567,19 +551,22 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, lon static int offset_readdir(struct file *file, struct dir_context *ctx) { struct dentry *dir = file->f_path.dentry; - long last_index = (long)file->private_data; lockdep_assert_held(&d_inode(dir)->i_rwsem); if (!dir_emit_dots(file, ctx)) return 0; - offset_iterate_dir(d_inode(dir), ctx, last_index); + /* In this case, ->private_data is protected by f_pos_lock */ + if (ctx->pos == DIR_OFFSET_MIN) + file->private_data = NULL; + else if (file->private_data == ERR_PTR(-ENOENT)) + return 0; + file->private_data = offset_iterate_dir(d_inode(dir), ctx); return 0; } const struct file_operations simple_offset_dir_operations = { - .open = offset_dir_open, .llseek = offset_dir_llseek, .iterate_shared = offset_readdir, .read = generic_read_dir, From patchwork Wed Nov 27 15:28:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13887146 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E4BFD6ACE9 for ; Wed, 27 Nov 2024 15:28:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BBA26B0089; Wed, 27 Nov 2024 10:28:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 640356B0093; Wed, 27 Nov 2024 10:28:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46C3E6B0095; Wed, 27 Nov 2024 10:28:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1EF0C6B0089 for ; Wed, 27 Nov 2024 10:28:26 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CD6FEA1670 for ; Wed, 27 Nov 2024 15:28:25 +0000 (UTC) X-FDA: 82832256150.17.AE0FBC1 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 3CC20160017 for ; Wed, 27 Nov 2024 15:28:20 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VT08HImq; spf=pass (imf08.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732721301; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MfHKVWe+Hwnavu/M/uaZW09l68m70KLsIeIu2aj05/c=; b=5Tdai74OPF36z718y93oX2NLIu3Uw1P1vpO6C5k+K9lkgZ0u19nHJ14HLeyJLxT9nn3fWV aCkQaf3eN1adM9ppAhjW6KqcE//FIooUsEkMwPEYvmD/y3csKJLHABDjt5hkXSXfo1cQKY 0Ezq8jmToH6XJtMxCAxXU77K3mQL6i0= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VT08HImq; spf=pass (imf08.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732721301; a=rsa-sha256; cv=none; b=FWd8ZW+HE6vLodOdB6t+1nlTs5gObWajQnUCNP7UtcPtHFYiYCLVzCztFBaRau+UqP0ZMz JR4EInlnTw8YNzJl5F5HrbOmUuyAVL1A7N/Jzn5iNQco4uBK/f6e8BTve0ZaaS7ihtxHku bStE7AIBPYWV9k0zS9CrVPQGxTanE9Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id E63925C3F2C; Wed, 27 Nov 2024 15:27:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A88EC4CED3; Wed, 27 Nov 2024 15:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732721303; bh=yaUgVsYqn/+UqS21Ay0PUdvP+2Ovp6VDQCczLqXNXYo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VT08HImqWvjHVpg0vUPPxkxWgmyqx6FPQSiQJRMNw0IopFVIATgz4coLvIXaQA5w+ HttmvTm9x2ggjv3ICKFLH2h80s+uaVVrnaMJSjwVnsR2/fYo0lVxOcpflRO32MDlHv A22VX+RXRrBbmm3pQFuLEaAmcJDB4WsVuQQUMC2PBkwbOD/R2h8tkSbikpz8uryCiI MAOIYikg4eQqyojGl+XUy2EA1W/0bAEc4kVDCJtU3EdmX6y+fDI28xs4E9sT8iNdKn JfH0jERkX0EQp1goCDny+sYUW9vwUuytQPtbRZbr5myfCmnE/xuWTfyt5dnYfvMqJW r9ZGIH9IRFzqQ== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v3 4/5] libfs: Refactor end-of-directory detection for simple_offset directories Date: Wed, 27 Nov 2024 10:28:14 -0500 Message-ID: <20241127152815.151781-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127152815.151781-1-cel@kernel.org> References: <20241127152815.151781-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 3CC20160017 X-Stat-Signature: ct8d63i55yzqcmzxihj45h64chpmxwns X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1732721300-816581 X-HE-Meta: U2FsdGVkX1/2uzoSwUwoiKg0lr+ysw61GJpPyzcMGYIryifk7HQ51L50Hubuv1t6ZkAPTiy2rfgGsggP9voER5yd2/WeSO7l0fKUA0sE2qCOzMVVgvwe98/Va8Pd5DpnIbesOKxQZds9TFE7tjxOd6TAgms7BEtd8XXQMxoVqiat0loIc8HRmrO/UBXUbnKBL2jedt/xY1cRw+KzJYc4dXoDG9o0yH0NJc/nAKDnKeyWlmIOfxijUHXAhvNvJcKYkzeXUGW1SJlLp3u8VqdnniZs3Es26Kko/xRrOrLviAg7h5ezjWdT5DH8VCXWyG0ZW31lazv1Lq+eUltUqe+elGTRTxKuJlBQtEbjuBMtciofUpgJ/wZThQ51FB0U7NK0k4XCuwveq+sutb29yp2CqSmhRnCxmZRNQyMmOOBpJMjK75/yZJrPvUagYjmduAhJEAMlCkATbgaPctsKvG/fVD0ixT82RE5glXSEpoqHrolBMqgDWKiQb5zCvLEJrMMSgficEtfRaLnRsM9glN3p9AkQ7GjaZeKDoAVsVfjfUq4VQq+YDonQxfX65sbGfWJKk8huqUIV9LEEDquVopF89O5lA6VPTs3XLHaizoitGInroCO1Rktl4rUGKT5xZsdPltt1LWjQGItKCg2K+9Md3lz9ZY1n6Br1FXKM/wqEGRDeILf2qaSwdUVc0xP6OOiNKy2n8KQOT4LXmQpFEA0zK3VnNuCsR1rIP3/7CVqb62JI20icF/RRK0UZUVE5S6VALKR87eiAOyoU38PIDdJnwb8CYWiB6JFN7xdoNHUXgM/mm33of841c6TsvNnVXd2LuZ5X+B+/ke/NCbhk4NUTHTl1mJH3hmHA5RJc/hhzBPB7Msh9quLFmB8l+sT1aIgXa50jCPfg/Wx+d+ohXZQ6bmVqtL47Sdi7crksnA0V9y+nnEW6Kn28s44fJPwzx82CSg/8vqShZQKIQ1XnBkL ktOS18xw CwU1DESiBNBAcj2K866gX9fUFeS5z+7DyeB9gmYUSJrDxsvGxJ8uK6KU8x7shGaXPHPEgKVWkEssz7gFrzuCfaPstj6ZwdA+fMAndZo6aQiewVbbFN2+GERfqFriSRsl/Cq7+WEExtuSuPt1TC8tDDb8yBfyWafi6mBy+CNQbZHHxYGuU8T3oRSOEB6iz0weW2+1GblxABT8K5dykVSIGKpl+zxr8uLtcBcri5eqTyTIc0VGGWdszdQvteuq8PumrsAFTlcO9IMuK5Co6W137rHDimQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever This mechanism seems have been misunderstood more than once. Make the code more self-documentary. Signed-off-by: Chuck Lever --- fs/libfs.c | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index a673427d3416..0deff5390abb 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -449,6 +449,34 @@ void simple_offset_destroy(struct offset_ctx *octx) mtree_destroy(&octx->mt); } +static void offset_set_eod(struct file *file) +{ + file->private_data = ERR_PTR(-ENOENT); +} + +static void offset_clear_eod(struct file *file) +{ + file->private_data = NULL; +} + +static bool offset_at_eod(struct file *file) +{ + return file->private_data == ERR_PTR(-ENOENT); +} + +/** + * offset_dir_open - Open a directory descriptor + * @inode: directory to be opened + * @file: struct file to instantiate + * + * Returns zero on success, or a negative errno value. + */ +static int offset_dir_open(struct inode *inode, struct file *file) +{ + offset_clear_eod(file); + return 0; +} + /** * offset_dir_llseek - Advance the read position of a directory descriptor * @file: an open directory whose position is to be updated @@ -474,8 +502,8 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return -EINVAL; } - /* In this case, ->private_data is protected by f_pos_lock */ - file->private_data = NULL; + /* ->private_data is protected by f_pos_lock */ + offset_clear_eod(file); return vfs_setpos(file, offset, LONG_MAX); } @@ -506,15 +534,20 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } -static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) +static void offset_iterate_dir(struct file *file, struct dir_context *ctx) { + struct dentry *dir = file->f_path.dentry; + struct inode *inode = d_inode(dir); struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; while (true) { dentry = offset_find_next(octx, ctx->pos); - if (!dentry) - return ERR_PTR(-ENOENT); + if (!dentry) { + /* ->private_data is protected by f_pos_lock */ + offset_set_eod(file); + return; + } if (!offset_dir_emit(ctx, dentry)) { dput(dentry); @@ -524,7 +557,6 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } - return NULL; } /** @@ -557,16 +589,14 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; - /* In this case, ->private_data is protected by f_pos_lock */ - if (ctx->pos == DIR_OFFSET_MIN) - file->private_data = NULL; - else if (file->private_data == ERR_PTR(-ENOENT)) - return 0; - file->private_data = offset_iterate_dir(d_inode(dir), ctx); + /* ->private_data is protected by f_pos_lock */ + if (!offset_at_eod(file)) + offset_iterate_dir(file, ctx); return 0; } const struct file_operations simple_offset_dir_operations = { + .open = offset_dir_open, .llseek = offset_dir_llseek, .iterate_shared = offset_readdir, .read = generic_read_dir, From patchwork Wed Nov 27 15:28:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13887147 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9FE03D6ACEB for ; Wed, 27 Nov 2024 15:28:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1B766B0093; Wed, 27 Nov 2024 10:28:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EA5246B0095; Wed, 27 Nov 2024 10:28:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF5126B0096; Wed, 27 Nov 2024 10:28:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A9C5D6B0093 for ; Wed, 27 Nov 2024 10:28:26 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 69FBE81830 for ; Wed, 27 Nov 2024 15:28:26 +0000 (UTC) X-FDA: 82832256234.09.46427F2 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf15.hostedemail.com (Postfix) with ESMTP id DD35EA0013 for ; Wed, 27 Nov 2024 15:28:18 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BAQrJMwO; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf15.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732721299; a=rsa-sha256; cv=none; b=S7eyzwhjuOBUC3v7MoOyTBOUCumr8KIs4+EQlN0o55KflM5NtggsbL9UtdYUxbndMEOvv4 5JAqU7YnBeOU+zFMRrXSrYulsZ997AgJJGtQE4/NuI5d35taY7XsYjrMVBSh9tlYDz88wK 3BKt5/Rvg0Fi6afBeU4kgiCZIoxBrm8= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BAQrJMwO; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf15.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732721299; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=c41AD2//6mZTVnR06FLjS9SBcMx53903xNe/tEbNKz0=; b=i5KEeeWihXl2kG42RdSNFY/4rSfe5z3/+5EVt6+NnWqNzUcfgl1dZ3T/8UMuWsCw8H+gg4 wHePd21tTPSqIkvaNTt3wvbExocCb55vu/IZSmv7fJQYam4hhmZyDyL3VK/0hue8DwLMu2 +PFxx6L/bDMozA7zkpMAgvpsI7Silpk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 6D36CA43938; Wed, 27 Nov 2024 15:26:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 338BBC4CED8; Wed, 27 Nov 2024 15:28:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732721303; bh=cm3XNveZKUOxWBzRqaJXCYdMMoTxeTEl5G9Suuio1jg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BAQrJMwO3HcTBcQJtl9J0sN0/3Y2EaWG1HoGPOJVdXnlxSk8tgna/zSNZUpzF60BQ 3dRUcDoFH67L8gUn89drpvzoZcl7it2YQwMoAFiks5/PUQxhDyxKmxYM+O6WWJEWlc FQIyCfAcqppQ5YYaKu+sco2Dy80Ug5cJOo/6T5axaEkQ0ldr4nCmcLxGs/KcL9CdM5 BlWKRM9OkdVQQzbHH6nPFhE7I9pZ907/6bqzK6KGTHGDpC9RAkEXw7UyaOuPFGjwcU SCblQFp8y4T4FtLnBq7x0hIIAL/rlW8ih3aD8uw47R+heJXIeKxMQXLr0mcP36v18G g0Y5KKEt0904g== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v3 5/5] libfs: Refactor offset_iterate_dir() Date: Wed, 27 Nov 2024 10:28:15 -0500 Message-ID: <20241127152815.151781-6-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127152815.151781-1-cel@kernel.org> References: <20241127152815.151781-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DD35EA0013 X-Stat-Signature: 1m9midk4imk3zphtw8ox7nwz6wtdzwk7 X-Rspam-User: X-HE-Tag: 1732721298-510220 X-HE-Meta: U2FsdGVkX19ZIMObBGnAsO4V3o7X4Y35cDloA9TqYQa+6NMa8QOwlQ6BASHgjZouK47XV1tWfiX3SASFaHgXAUSFjuuOUjqiU62+RZCsJoqs5rwbkd30NBMeJnFmQzbPQHCy9LVuip3LU4f3pFEksTKIwfGjOc4pjXDzeeNkGvP0ZBAsiVJwsEmJ8O9avDPLzAMMmOiDZtBlmYgsYe8gDqXNb58/Ws66kDRIX04y+KbI9jjmtip5A1DybpFc/Zu1xddgsUEVK1qixMi0L5ek70hePBokREaD0Gl5lBvjEhdluSkLxi4CisBHs58SIMGWpn9ykZsng4XD13084uC/RvTLdaA8HsuY2uqZYcB5IUlCRjL4hGfH0dBPYqTIbsxAUNFTSM/bcWQGAb+8woSTSsPOH/upTQRuXoECCzNm5di7w9jDcUS+iZ+O/vcbCQwXdzPrHmXx0J7Bi2zVtoYRUYbvbi6VR43xarWjrJlwn7CVIDHJiJ6acfDyB0Yj/iqLn8r0RwxoMKBib/hdbKYgMocPBsfBQ/yld1CM6aMr+N65SD1izYO9jY6zVLH5ZyYlD4/dQE2J0hexrVcjHiqxAzbkTuOMwNPzygy6+ktA2KJF+aNcOZoBuD62D7U8ZgnLX6IOWfGNY8IELu6EZCRtwXR8tV3dXZdotRx21WVB0lQpqHI6dAmwqpWY+GAkjC3aZrr8ZULgZNsJ4J1aLcUF6zC9ZTPkS3cfbM0Zc4ZFTciUn2JgrGEK1nbsknh3178yDU9NSX+jygMh716vQMj0JAlkmsvVEv2dUSzdDW7dDwxiXyrvMY8DAADiwtNh8OzjnPI6bcHmCHQxx6Pljbrt8qdqaD7QmfTifk38NxzP3j19BUo5KH5EEhoOeswPn0mCC1CJkWBRwmg92q55xCCB/5rh4SfPxXOCFlBlvFt19wuPZSoc1Qi58Uoo5c1TB2qMocbsIyyYMW00M8Chvvn Nz6CHzU7 nY0FthQe1r6Wxk4FPpTBYVTXaaF+QiJKa4B3j7zB9X9kVWigOQRLAB0NZtymXYCRli3vUGfSTm80SpbAuZOMdO6Zoo6+UpNf7Ft7Gds6apeqM9h3tOxZbBoAQpL7W9yZrOM3p6ONFDBHynmDQh7wXn4sENaj9dqfB7ZieUq1u+e7uk15eLzqsfyrqEswKC3O6BnaW+WD7STZh4Kx5yxkdmnwn+x8hvIgspqQmL+2rPj3+ME+hK5F4gUciddJr7LvvN0xKil9NPNSJLasujxW0GY+FKw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever This line in offset_iterate_dir(): ctx->pos = dentry2offset(dentry) + 1; assumes that the next child entry has an offset value that is greater than the current child entry. Since directory offsets are actually cookies, this heuristic is not always correct. We have tested the current code with a limited offset range to see if this is an operational problem. It doesn't seem to be, but doing a "+ 1" on what is supposed to be an opaque cookie is very likely wrong and brittle. Instead of using the mtree to emit entries in the order of their offset values, use it only to map the initial ctx->pos to a starting entry. Then use the directory's d_children list, which is already maintained by the dcache, to find the next child to emit, as the simple cursor-based implementation still does. Signed-off-by: Chuck Lever --- fs/libfs.c | 95 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 74 insertions(+), 21 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 0deff5390abb..2616421bbe0e 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -241,9 +241,9 @@ const struct inode_operations simple_dir_inode_operations = { }; EXPORT_SYMBOL(simple_dir_inode_operations); -/* 0 is '.', 1 is '..', so always start with offset 2 or more */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_FIRST = 2, /* seek to the first real entry */ + DIR_OFFSET_MIN = 3, /* minimum allocated offset value */ }; static void offset_set(struct dentry *dentry, long offset) @@ -507,19 +507,53 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, LONG_MAX); } -static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) +/* Cf. find_next_child() */ +static struct dentry *find_next_sibling_locked(struct dentry *dentry) { - MA_STATE(mas, &octx->mt, offset, offset); + struct dentry *found = NULL; + + hlist_for_each_entry_from(dentry, d_sib) { + if (!simple_positive(dentry)) + continue; + spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); + if (simple_positive(dentry)) + found = dget_dlock(dentry); + spin_unlock(&dentry->d_lock); + if (likely(found)) + break; + } + return found; +} + +static noinline_for_stack struct dentry *offset_dir_first(struct file *file) +{ + struct dentry *parent = file->f_path.dentry; + struct dentry *found; + + spin_lock(&parent->d_lock); + found = find_next_sibling_locked(d_first_child(parent)); + spin_unlock(&parent->d_lock); + return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup(struct file *file, loff_t offset) +{ + struct dentry *parent = file->f_path.dentry; struct dentry *child, *found = NULL; + struct inode *inode = d_inode(parent); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); + + MA_STATE(mas, &octx->mt, offset, offset); rcu_read_lock(); child = mas_find(&mas, LONG_MAX); if (!child) goto out; - spin_lock(&child->d_lock); - if (simple_positive(child)) - found = dget_dlock(child); - spin_unlock(&child->d_lock); + + spin_lock(&parent->d_lock); + found = find_next_sibling_locked(child); + spin_unlock(&parent->d_lock); out: rcu_read_unlock(); return found; @@ -534,29 +568,48 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } +static struct dentry *offset_dir_next(struct dentry *child) +{ + struct dentry *parent = child->d_parent; + struct dentry *found; + + spin_lock(&parent->d_lock); + found = find_next_sibling_locked(d_next_sibling(child)); + spin_unlock(&parent->d_lock); + return found; +} + static void offset_iterate_dir(struct file *file, struct dir_context *ctx) { - struct dentry *dir = file->f_path.dentry; - struct inode *inode = d_inode(dir); - struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); - struct dentry *dentry; + struct dentry *dentry, *next = NULL; + + if (ctx->pos == DIR_OFFSET_FIRST) + dentry = offset_dir_first(file); + else + dentry = offset_dir_lookup(file, ctx->pos); + if (!dentry) { + /* ->private_data is protected by f_pos_lock */ + offset_set_eod(file); + return; + } while (true) { - dentry = offset_find_next(octx, ctx->pos); - if (!dentry) { - /* ->private_data is protected by f_pos_lock */ - offset_set_eod(file); - return; - } - if (!offset_dir_emit(ctx, dentry)) { - dput(dentry); + ctx->pos = dentry2offset(dentry); + break; + } + + next = offset_dir_next(dentry); + if (!next) { + offset_set_eod(file); break; } - ctx->pos = dentry2offset(dentry) + 1; dput(dentry); + dentry = next; } + + dput(dentry); } /**