From patchwork Fri Dec 20 15:33:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13916886 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 92A27E7718B for ; Fri, 20 Dec 2024 15:33:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21D686B007B; Fri, 20 Dec 2024 10:33:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A61B6B0082; Fri, 20 Dec 2024 10:33:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 06E486B0083; Fri, 20 Dec 2024 10:33:22 -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 DD1326B007B for ; Fri, 20 Dec 2024 10:33:21 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 89E01A0B93 for ; Fri, 20 Dec 2024 15:33:21 +0000 (UTC) X-FDA: 82915728966.17.2F762DC Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id 44F401C0011 for ; Fri, 20 Dec 2024 15:33:04 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=O2ZRPhRi; spf=pass (imf18.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=1734708764; 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=rtEIOXSYejtFTXTHI6ihz7mrnbnMF5r34b6HRlDEink=; b=6Ez2aIaa0vO6yTmjg3yoS/sKD+CdcFHuCezEtSNLG1COpMPaRWxkzIWpBX8XuoO7QkIi+Q /PyCLIiQap6C8EQCjId6cGGr+wDj4MB6iyz/kbsoyTwvjBAryAycjYWNxXzHfFz2CJmSqC 7xoKxoLGzd3kYtF+Cx453Y/Xj8uy5bI= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=O2ZRPhRi; spf=pass (imf18.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=1734708764; a=rsa-sha256; cv=none; b=IbC21OGmkQMcwAJmrsSQ/79RHlZ7WcXMv2sy/Hzcm6ij0NmghwVjWPf/W8QyAlmqVXK0iB 4z6POozSVVbQRxRWj4TIiJ/CmDGlzG1obb7eBAAv0E8Trw8X67bZqvijDgUArVkg9kJYGn +fM3tI5vDq0Y3qnlDUSfBgRw+MCUyfY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id F1DC65C621F; Fri, 20 Dec 2024 15:32:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AB35AC4CECD; Fri, 20 Dec 2024 15:33:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734708798; bh=EECtu5EOQPP9WpkyAeyEzP3b+8wuy+Niq71T6d9EZWc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O2ZRPhRiASStEU5s/qWXh04LDWsiGjNPKUEulej0MWKn/cJxk5FsBJ+q9c+2xnrp+ iEXCDgelMCJMK66KZI+qiZDL+LgrM1fWELgH9owlLAaDFlotlcNi2y41Gm5vEbW4YC hycvZzYjXnPVLrDq3FfkrcVJ8uUhtyzAbOZqE+x52eZY1zPGP/nQV5nnxOc/g9kUnJ bHItBtmY2eBLwNO63tPWFmfKSgX8z2nVUzjnFrwD7a5KopNGDN5K5pM46WX6nrIN9i KRp7CxTH5sembrL3X3Dcy3mFD0DQ8clz5c9abI6gYfbv4gA9jj8pO+wHpMpJiQQMDM G+SvL/EzaYILw== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever , stable@vger.kernel.org, Jeff Layton , Yang Erkun Subject: [PATCH v6 1/5] libfs: Return ENOSPC when the directory offset range is exhausted Date: Fri, 20 Dec 2024 10:33:10 -0500 Message-ID: <20241220153314.5237-2-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241220153314.5237-1-cel@kernel.org> References: <20241220153314.5237-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 44F401C0011 X-Rspam-User: X-Stat-Signature: w47n3c8qsuowz8ezkd14xgy7amhe4iwd X-HE-Tag: 1734708784-211009 X-HE-Meta: U2FsdGVkX1/L3s/4HV9j//FZFbA4EGvMdjEgkFeLxkUfIlnQ6AbAOxqN71yCUmzoqCacsO0AQXf8u0+VJO9AJhPz/ZBvWzDZjemvYTYym1oX6p6b0OS8JMqOafimagtiewcgXZXhJgjI/D29StHSMY/jq3xEggK5MFV70Ml5n0kbCFTB4gUOhdON5J3xDnxv+4V2ncEtG27AJl9xWzhHvuIRVcV8dJ3jWCvtPQynnf9DBRUxbWiJEyMJBhlk5K21cpcnwdrnuo0r6MLSx6H4mRRbkZK3BkLD8XDy/LUs6YVZkCgqMYKrhZCAZ6Ysy7vSgtexh8f7z/XKGuJ0I4C4TRXf8DdVVtzRJ+YIx0sHrdeyJsNQllVnHgvP1BiCQh0/iOyQhnOUD6pnyqGmnIiA6OuoyoFeZScV9FiprPRBmq1fiXA3DB2EfyqsY2Rst+AUO27JeXpddpvpDX21ByDPJeoehNavfrDIpuTBC2dKPpVBn7MAWxV1jXAFrQcy/yhvGrNCJnAuAKLKMY7lawE5e8edC3oC4THraVjHqVF3TxZDwhmT8FHb6t1L9wx7/33Z7rsODnuNISfrSSMGhRi/QkoDFkK9WBUdYbucuTZZjl8RoKFMAlbr15E44XfA0jZrda1783WM757Hmxq1hkTOdvkLR7dwZgWYizFw9I0UDMfLZF49Hdl3RmlOIvyOXSqikhAUWudKJoF6Qou9Gi8GJCa6kJ6nbkPZQFX4GGBlpAfIGjZMTTxtT232cqUA1h9AZx17VZu2M/1vbwxKnkzsuWriD6mILV3wQBnEXGCui7Gzy5BwQtWrdrJvMnmoy2nfdB05/loVofnTts8JXYExP3zdZ1t23zLY44eCGFpMQ1WL7esa61VCWONPX1yg608MFYpYCX9k6SN2rVUyH3sNqDwVobJmTZSE6uKI2Yc0AhSQl75HFWNFJMySUNs+Lgs+IUWHC4futgVXSErtm1D N7sqcA9k X25vGLUHoqHq/F6PT/acYke2c5zcVl8R3fG22ZAW9IeDPjdXgmSFDkC95XTpUyJGU+NQ7fSB5ddrDm4BWHnzQZGkhnD6xjXQyw9R/n45hqKyzILP3eDCfWoVAqjTTotK593B6liY2A647svIkpQyFVguwELjOZzmnRw/h6KBnH5n6Mp8N857AURL3/dx1/6S8od1vAUKTo6vZi4sA8GzkQaew5GWLPA4YUqUO6wLLeNfVgsBDjBZklOOoLXD6t4cFygdz8O7BtmaQu+U+OpP3e3B+bA== 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, 2 insertions(+), 2 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 748ac5923154..3da58a92f48f 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -292,8 +292,8 @@ 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) - return ret; + if (unlikely(ret < 0)) + return ret == -EBUSY ? -ENOSPC : ret; offset_set(dentry, offset); return 0; From patchwork Fri Dec 20 15:33: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: 13916888 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 98BE0E7718B for ; Fri, 20 Dec 2024 15:33:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3CC4B6B0083; Fri, 20 Dec 2024 10:33:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 32D606B0088; Fri, 20 Dec 2024 10:33:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17FAB6B0089; Fri, 20 Dec 2024 10:33:23 -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 F02C66B0083 for ; Fri, 20 Dec 2024 10:33:22 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A0B93160B0E for ; Fri, 20 Dec 2024 15:33:22 +0000 (UTC) X-FDA: 82915730310.21.A73CCF7 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id C63B1140002 for ; Fri, 20 Dec 2024 15:32:54 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=F3exKZ2T; spf=pass (imf26.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=1734708774; 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=EvGnTa8al1EYy5YKK16KcjK1WmCT04bBJ6JnFuu97WQ=; b=qANeWQXAjcTOmuFL2sM3Q1mgs0ZGVFwl0x03cJaBDUgEqF94Xt51lO7Siphxh4P26RvMsJ LNGLxoOu3dLyopde+MZAjkO2VyVUllDhT4j3qmvAjeJdrxoSooUl9fFQ9xR0rxp8bHZ4ra r2Hhy0gGidYRUVhg39toDZAS1MS0pe0= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=F3exKZ2T; spf=pass (imf26.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=1734708774; a=rsa-sha256; cv=none; b=agLBLWWutsZEBZ/YbvO8+dLg2f6zx3hMnzKRfrI8smJEWi+j1+iA6vsjnMQGGnqN0gknK0 EbqjECpW37arN0YzQOvBwLps0qE7E1+xC/QxmXx/MC9y97pCwzJEftBZN/m03J/H6V36og w4frf+FcmasgjqzwjZIOHBC/7p+dBOs= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 24BDA5C65A3; Fri, 20 Dec 2024 15:32:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBC47C4CEDC; Fri, 20 Dec 2024 15:33:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734708799; bh=ZyCXpRT455r2S07+QOUD9SLmwrfNZHhbqdLqERRMUuA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F3exKZ2T5TgdU+zC58sBpE4t5ZjksAHSgtcv+GepWfxEqTmJyAJFq7FrwKp2iubcV NT6tJPsTRr06rcD6138Ya+4zZNhbLXJawNOtTiO5xXvQk5GVtY/HPLONeKs+QVkdug kxGsdY5zU8XA6Tzut1cA3LYOWYlT7H55zz+OBqA9QfqPJvgS6ziryU6gY5n4QXAHhV J7xMHac3Nn+/JSYu4zevHf5pSeXqYcLmzz7dpIUPC/8JWkRxAofdZkqLIyRhGrnt0z 1iHnTsuS3kGJXivaJ3EuItrypzffShYARESNujqmZV3+LaTaWr+z61Kzs5TVSMI5Z7 hfO4MHZ5gOmGA== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v6 2/5] Revert "libfs: Add simple_offset_empty()" Date: Fri, 20 Dec 2024 10:33:11 -0500 Message-ID: <20241220153314.5237-3-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241220153314.5237-1-cel@kernel.org> References: <20241220153314.5237-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C63B1140002 X-Rspamd-Server: rspam12 X-Stat-Signature: giye17fx4n7tkgr5ya3finb64emrwrea X-Rspam-User: X-HE-Tag: 1734708774-70806 X-HE-Meta: U2FsdGVkX18QMIPEmS8qIPn5r7EXSEUZO8jUxDA4/TqUPhVFMP9P+bNvkJ0nI+J3CiX4inLaq/UX/97/B+CWHz8q81VpbKICFvZkR+Xt9E75Dv6nFAGMbYJs51+zl5Z7toFRR+E1Z6BIoiZTbMIK2rFrmmHOxpGu3ATV1o07fOIQ6fK7EGeFj6vxoi+2n/2kTWt2e7p8tBanropCRlRpq56gPMetuUQKFS4vEXnC6gY+WgtlRKw7iNm9TrY9mNYvIJdVjgmHzcul7PhFWuKjjOpfa5r3Nw2OShIdEnSJD1iwEfYZO7SdqQXjg/1iyuaHreqaq7JfCqWv1/q1pbYhlCzjgT5jw5EHYqFPiW/wq5d+nIcUqoSwS+5pRlYD3aER3ySMFCCEoj4h2a8E4yJMllR2t0kH4y1gDIAN3etscRpEuVLjSNx15HTHswXpO/LrezIlZjyPThCGIl6gFXx/9KwzrgYROzVPI9n1AAOZuCuU33cnk/Bc2CuS1/fkJM4Vq4+4KkNa31kX2UYVPkHbgdLtAmJuS9RI0AmMbxAwJK9szr7AlMMLYmzADo7zLnLnRCmtLgdHALzk4Om3b9PhY46M4gWfaj4b2UBiZk+35pET5mUDQ5hBmOTotBtgPhqu8seqKsO3AOo5FgCmEM+MGp0GXFm0n9T8nHcleUxMpMdQ54MjgdB+yy/FFNwYQHVd09lG97pcl1uYkcLEG0d4XKZmgpL9NWRCZO5s6Kfw37Rd2pS3XgtX/1i1adXUFAvVLIxoS98K/yUU6VZWKBRVFxSoqUkcK/Q3PH3p1lm0tz5KCpNaI8QbYdp95ANSXRzk4nbd+aS5PEXePa0lFGZgXjECqlBKGWn0GP+VJFVdd87LOShEZUCeIFg4/IJ43RAxA6ZGGYyX/67cScoV1tyV5gasSzqxkcMWah80A/ikKSy1trTPcjVV+umc2ZkxWS2ISqZY1rcgFw4UmxB+WVF /I43ZPmy LEE6i5Wn6gSpOCdpNhsfNVFzcsOer3Ds8uw/oDWAwaKBYeyIwdVPkfxCBfrn1lRf6gYBIaXV2TK7IMgj6DPoShbSj7Vnx2uk6R1Vzt675P0sEd968hHi1xLi/u3VdwqAnJuDQ3bqQELKmg5/FMPb6Crrj7/7QXOVypPREGUv2hIG0MYxCmgVVikbJFVBdmgOL1b6z695k9TEOIzBuaIAVS43GvhovfAQSIJDPGlygMcQjADGQcXAH4hweh4iF9v5gMgDRG9zCMCko8vLfxRo1joYkgw== 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 simple_empty() and simple_offset_empty() perform the same task. The latter's use as a canary to find bugs has not found any new issues. A subsequent patch will remove the use of the mtree for iterating directory contents, so revert back to using a similar mechanism for determining whether a directory is indeed empty. Only one such mechanism is ever needed. Signed-off-by: Chuck Lever --- fs/libfs.c | 32 -------------------------------- include/linux/fs.h | 1 - mm/shmem.c | 4 ++-- 3 files changed, 2 insertions(+), 35 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 3da58a92f48f..8380d9314ebd 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -329,38 +329,6 @@ void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry) offset_set(dentry, 0); } -/** - * simple_offset_empty - Check if a dentry can be unlinked - * @dentry: dentry to be tested - * - * Returns 0 if @dentry is a non-empty directory; otherwise returns 1. - */ -int simple_offset_empty(struct dentry *dentry) -{ - struct inode *inode = d_inode(dentry); - struct offset_ctx *octx; - struct dentry *child; - unsigned long index; - int ret = 1; - - if (!inode || !S_ISDIR(inode->i_mode)) - return ret; - - 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; -} - /** * simple_offset_rename - handle directory offsets for rename * @old_dir: parent directory of source entry diff --git a/include/linux/fs.h b/include/linux/fs.h index 7e29433c5ecc..f7efc6866ebc 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3468,7 +3468,6 @@ struct offset_ctx { void simple_offset_init(struct offset_ctx *octx); int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry); void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry); -int simple_offset_empty(struct dentry *dentry); int simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry); int simple_offset_rename_exchange(struct inode *old_dir, diff --git a/mm/shmem.c b/mm/shmem.c index ccb9629a0f70..274c2666f457 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3818,7 +3818,7 @@ static int shmem_unlink(struct inode *dir, struct dentry *dentry) static int shmem_rmdir(struct inode *dir, struct dentry *dentry) { - if (!simple_offset_empty(dentry)) + if (!simple_empty(dentry)) return -ENOTEMPTY; drop_nlink(d_inode(dentry)); @@ -3875,7 +3875,7 @@ static int shmem_rename2(struct mnt_idmap *idmap, return simple_offset_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); - if (!simple_offset_empty(new_dentry)) + if (!simple_empty(new_dentry)) return -ENOTEMPTY; if (flags & RENAME_WHITEOUT) { From patchwork Fri Dec 20 15:33: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: 13916889 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 63A10E7718B for ; Fri, 20 Dec 2024 15:33:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F8E76B0088; Fri, 20 Dec 2024 10:33:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 133BD6B0089; Fri, 20 Dec 2024 10:33:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9F9E6B008A; Fri, 20 Dec 2024 10:33:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C73DD6B0088 for ; Fri, 20 Dec 2024 10:33:23 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 75537160B53 for ; Fri, 20 Dec 2024 15:33:23 +0000 (UTC) X-FDA: 82915730814.11.22C6A33 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 944A4180012 for ; Fri, 20 Dec 2024 15:32:57 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EfUEtpDi; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 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=1734708786; 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=JxFIpqOpdjB1+S22VVzGR8djYmE33wkv0eUTvhRFOZI=; b=fKzvnvyhl35eQtNEl94aVk7mLEzivnLhD2AeYmiIViYy28dpn+KlgiV+iEFEhiXwZmHvIO k49rsy6B6PiITrynGRf94Uam1JI1BtWo/41gWvFjMFcAFzmBdzv5iq3YtQmnfp3tIBjldT /vUc41HflxjTgVDLOKjKiM3XqgM8Jyg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734708786; a=rsa-sha256; cv=none; b=yFEu0fCd1PP0mxYOovu8CxRV1YZFip1Pl2Q3dwkg/sMBBS55yCRjEVC/zB1uwhQMWjCTyQ Gm4C+wrpY3ZLEKXtnkKZQnArHxJFogHBTzCX//3Qybg15cjU+ralkIdTsCJm1JvcJPjnB9 BN6gBUKr2WPa0w60gYAkwuT8FTKSS+w= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EfUEtpDi; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 1D0905C65AD; Fri, 20 Dec 2024 15:32:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2A038C4CED7; Fri, 20 Dec 2024 15:33:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734708800; bh=44QvGk8cIKJkG2AbH47kX36uIs9FlMTaiShWTtz/ZcE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EfUEtpDihevl5FaH91uNvj3qNDv+vCgXN/+wQmjpCG+c95edl47dkT2mQSFMPQklp K+JIysQ1B01PYNBuNlda1NkEHbGvmjoclasvpYJQ1dboVZjyLiCpwrPgiWsSSx0U3O TjzlwRlMr9b9qb7iskCxs+FhFpgP0rXHVTKnbKdOT22i5VKYwR6WJh0A1TWex93mT7 NPmCUXC/tb+p6kDt61+M94b++o/yFr7+57PHIaHBJaS/emfM6tCViLuEhn2cc1poi2 qUVbH19l2VoDV1BPh+LHZTenKyKN5BwsH7x2gHw2T8v/O7ER3MZ0Ftv/SZ6ni7GYca SQ0VIffdoEkNw== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v6 3/5] Revert "libfs: fix infinite directory reads for offset dir" Date: Fri, 20 Dec 2024 10:33:12 -0500 Message-ID: <20241220153314.5237-4-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241220153314.5237-1-cel@kernel.org> References: <20241220153314.5237-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: xobwbze7id7d61wjiufwgitiffpu1qsa X-Rspamd-Queue-Id: 944A4180012 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1734708777-448357 X-HE-Meta: U2FsdGVkX19uUFvDJUVv6nUKwZcyfrUyb/cqEyhnPrR9YT9o1yNJV6Vl11JcXjfG/3fQE4h/ezFyPU0L8mtQDP2c7buJkJwyQXPVWXGPE6Jt+yy8E+0ploIPfcGakyRezOgkBAuZb5h93eVJZqrLKLeUR2LjyOP7Sb94mUxg4Vq1/ccwLwyXJolozymOvh+JtQFHbWuLlqeRYRYmdBMWsZ78gl+afmArPzgfboyvjJD7EtF4kF+1stFwTjCJSucevSnq+d6pVOdvhyX35XghlHRizPB5MlazAo/klthBT6bXPMUSLPcw6E0OrgDIvlBQ1RGwBA58gKpNORsIL5O4/P6pSx9LdzVdaAtm5oyYKlSuMDhJU7GC7T9TxhATEDxxeiiW8cT0Gtn7TZPIyCA1+tfEIKsHcMOMYtVQSs8kZ8rRq0u+HtNtmZlDX2yRa8nEDDoG3bTtowfLhZs3BZWHmsIDRGY3X4vAc8rEtSMDtP1S4AhSlYqfVlW4PEWGXk6NFkdnFZaRnz0faUv5WiLY5IumKahyP3Z6cxAQZI7qZqsbUXiE5ASXtslzVuzHjmiNu8IfIqRzUG7+xIPW8CtSaRreCj4y1pQ46tWKytNEZhZ9KNcNr/rSRsIo3PJOTQMbw4hPK0Z1Ntpt/iUS5qhCHD9z9KDXwu/6lNbLpCc+L2QHbTN8EFQuWc/jnwxjg1oiItvBfAnP9vsoLYnOr+utU48QyuUGCjNZce4pIPalX32616Bvc8JDEuXMd1UBN7xUNRATAi5pnghrPflQTryEtFhgMwff4B9mSKnVQGlktYKX95cLrQlX+alrwXfbK+oYCy7ZqGbMz3Zt9S7oUK58NKoc7EYUWclHQeFryl20CriSBxtj68V8CIzQrA1/44mCK40b2Nm+VRvGhoJuQTGy9pKcLrdktiyY+nfudml7/oa5u874Dx/h/4FwlGxhO2QHagzEhRi4pEenczuwkv3 4nE3dq02 SWzsSVP/+qE4tSAroWI5/gjWE++wZXykkopAfzn7buCRbY+/4SPb4a42HKh564mF5Y8ZyeeXzRek5yCmHGYBE8oyeWe/+ZTuAL6BVh+dvdSjmYEGP3WtEUNOKpY7FUaoyiboBHMI3LeJeesfxSZVscSp+VKDNtOgKoEGDmbOmQfyC2uSbI41veAoH0wyO4eo0SkUMfihug+rsZNCuhd84cOgoCOvm1oM0+EVXEWoJwSxVAnnnzBTWYm+kiNOPKGz/aBpW 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 The current directory offset allocator (based on mtree_alloc_cyclic) stores the next offset value to return in octx->next_offset. This mechanism typically returns values that increase monotonically over time. Eventually, though, the newly allocated offset value wraps back to a low number (say, 2) which is smaller than other already- allocated offset values. Yu Kuai reports that, after commit 64a7ce76fb90 ("libfs: fix infinite directory reads for offset dir"), if a directory's offset allocator wraps, existing entries are no longer visible via readdir/getdents because offset_readdir() stops listing entries once an entry's offset is larger than octx->next_offset. These entries vanish persistently -- they can be looked up, but will never again appear in readdir(3) output. The reason for this is that the commit treats directory offsets as monotonically increasing integer values rather than opaque cookies, and introduces this comparison: if (dentry2offset(dentry) >= last_index) { On 64-bit platforms, the directory offset value upper bound is 2^63 - 1. Directory offsets will monotonically increase for millions of years without wrapping. On 32-bit platforms, however, LONG_MAX is 2^31 - 1. The allocator can wrap after only a few weeks (at worst). Revert commit 64a7ce76fb90 ("libfs: fix infinite directory reads for offset dir") to prepare for a fix that can work properly on 32-bit systems and might apply to recent LTS kernels where shmem employs the simple_offset mechanism. Reported-by: Yu Kuai 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 8380d9314ebd..8c9364a0174c 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -422,14 +422,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 @@ -443,9 +435,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; @@ -459,8 +448,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); } @@ -491,7 +479,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; @@ -499,21 +487,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; } /** @@ -540,19 +524,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 Fri Dec 20 15:33: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: 13916890 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 F03A2E7718C for ; Fri, 20 Dec 2024 15:33:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41A496B008A; Fri, 20 Dec 2024 10:33:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A4286B008C; Fri, 20 Dec 2024 10:33:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21D506B0092; Fri, 20 Dec 2024 10:33:25 -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 E4CE36B008A for ; Fri, 20 Dec 2024 10:33:24 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 96B3CAF9C2 for ; Fri, 20 Dec 2024 15:33:24 +0000 (UTC) X-FDA: 82915730688.15.7C65FC1 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf03.hostedemail.com (Postfix) with ESMTP id 48F9120003 for ; Fri, 20 Dec 2024 15:33:07 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aj8RMztE; spf=pass (imf03.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=1734708786; 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=lSJKQihNyW88qfaDal04lqiqUo1bye99I44K3+UECXQ=; b=exGqeM70TKV2q9vcaEPR1+rsOxxIqJj3/IUDNeQPiEC/9cHJoJHZDHXNUdPbcWaMcBJVGr tI58zB4TH3gKq5M/nJMISKw8NxgNREUIwUjzbu9P6P9rGtYNQ+Typ9jdnxdu32a7MzmMlx ORQInVAdypw11F2GvYdE2LcRrtALKvU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734708786; a=rsa-sha256; cv=none; b=KWGpDGqkdEXs29gwragGDSHv1fLVqJTgmhunu3Jrp1C2Qe0GaVkjN2r2Rl7CDcTFr1NyHu ScabIn86jKjq2LZVY4u+f84NO+ti4Je9es2cfJpSrEYecBt/Dg/WAc/oO2NOI6IP7cYd7n 5XSlNF1RQVk2qTEeb+QMe9CrHV6nKHM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aj8RMztE; spf=pass (imf03.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 172355C65DE; Fri, 20 Dec 2024 15:32:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 223FCC4CEDD; Fri, 20 Dec 2024 15:33:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734708801; bh=vLEn4J1l626ylhS0DfsGUzEUbw5fV8eKjCFu5JUH/qI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aj8RMztE980ugTjA7NrNCBFI7OOZYEE+7YN6DXqfOHNsiuWkl7FevtLfS2hmNamXC 9WxbM/JoYH9wxSD1GHxwXX1kvGAt8wUfYtZdIG4bVK5dDU8NT9bTYCZNs4FaJE5Msr QC7JW44DF595w6Bf/lA8JQS5LDIbMxpaNoBnZj1P7qNYZAIMS1rwmGPWs9X2U/YCJ8 ys4OS56o4iLffoxTFSdFI1UjHNhDmF3pbzfbJUAxkvAP2u3zN3vTK32hIKAItNxgdY YoQA77Wts8jmUD421POPc9qp3GSrdrRXUvGVeSVSh06rg11gF2xFG3CgSA1eGsxH67 3We8dzOSubMlg== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v6 4/5] libfs: Replace simple_offset end-of-directory detection Date: Fri, 20 Dec 2024 10:33:13 -0500 Message-ID: <20241220153314.5237-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241220153314.5237-1-cel@kernel.org> References: <20241220153314.5237-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 48F9120003 X-Stat-Signature: h4c579ffy9xo8136xz4bdta1sq9f95py X-Rspam-User: X-HE-Tag: 1734708787-829067 X-HE-Meta: U2FsdGVkX18lWimbD5397JK7yf1x8WFQtiYfx+tEbdkP16ACJZiOVArv0Qdcje9Sr9HHu8ilDPweLouhxVgygn+4hAzrS0ROdlmG5NJ0CiRsUIaHXb2tQu3b9e6QM4JveYk4mBxfsuMxlkPQFibTWpQJhCWdq8/vrtCgwdM4c0o27BWEyVXqYj01Q82VdPp00UykGKjVK33tZ69jlEUFWE2uVMhWLKcfgK18iTtcSz2Uovryiim/kvjjxxZQbbAcMEolSTbaGBYoS4gil+YER5XqColqaC+dk0C7dYBFQmfRVcksmjrptDvFkkXc6J7vgliOELSDYvivi5GQMk355iKbPTPySP9JaTYY20/kO5iEZwDfpmfKBRpUJ3e8ZdxmIafFdAUBK/wUhgsUN5OBeOcuIz3rIv9IcbMibpwDQR3ya2gaBz3U6+zLj5RPrmO4QMfoUjyWypszGwGje47+zPt3vILPy6Yrh+nnYN3lTngAYAohd3kHR6oW03KLdBUbWCl/SOXFbBA7GM9Qke+kW1KEPpBlZu6yi50bfT2K/bpSF5LEO7Ur93BzF3Tm0X84cdjYteBXRO/xbFb4HCrBDCEm/N0IQFvPGdW2CamyBhaUnXRhG21YK06eHjzRUur104YTQKEqf13vB2p8twXa8jK/JHtm6QtAk9pvxOfoBAj5b/rd0Y6Fguw74F/QzFbNLameBk9bauPdY2SnEV/yhnbazt4WqMeUQtKW98aksINA+4TYbBJ8K7N1HoJB/Z310dXP75alEME45m6Mzdjvga2a2XKB2gWkx4ZExAs9a2uPRYZ7fLfwvWcf9Dg/sDgtyKfacpFYuPymZOi0jdHM4YbO+cqXYMly+UTh0xa0KqyBLwkH803IBjx/7PwYgH23WAG+mM9N1Chl6gif2OmMg2SwnAXW6wfDgCGksi6mcMAJttGPrkFUCHGNs3O7V5cjVmTEp5WdNp3J73jb1fb zIpEI3Uk l8avwlzYePgTxIkE74etgfJ6nKnjWLBrFYchljrCIQclZudxH4W5nH04F0jhvpCjrB3Y4DmE07o7GUVFwzKozVOB2uVXJ+xZ74pJc503FeFlkmEo53dD6XLv7DbkpVxhW/dDNJGq/dEdWglLulL4igXNk/wK8k6Hs5GWMDk2CuDdTeiMMHFzvXeJitgyLhTVb4YuKEtEYCgffzYYrsB4XpANbE2O57pqywNTJLF+84M3ALtnFmykP/YZI1qMCljrooFLVeMQsr0q1uNgS3+f7oUOVmwo+iDs6dl21vYJbek9oGWhij10wIG/6LQV+WAhI+Dog 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 According to getdents(3), the d_off field in each returned directory entry points to the next entry in the directory. The d_off field in the last returned entry in the readdir buffer must contain a valid offset value, but if it points to an actual directory entry, then readdir/getdents can loop. This patch introduces a specific fixed offset value that is placed in the d_off field of the last entry in a directory. Some user space applications assume that the EOD offset value is larger than the offsets of real directory entries, so the largest possible offset value is reserved for this purpose. This new value is never allocated by simple_offset_add(). When ->iterate_dir() returns, getdents{64} inserts the ctx->pos value into the d_off field of the last valid entry in the readdir buffer. When it hits EOD, offset_readdir() sets ctx->pos to the EOD offset value so the last entry is updated to point to the EOD marker. When trying to read the entry at the EOD offset, offset_readdir() terminates immediately. It is worth noting that using a Maple tree for directory offset value allocation does not guarantee a 63-bit range of values -- on platforms where "long" is a 32-bit type, the directory offset value range is still 0..(2^31 - 1). Fixes: 796432efab1e ("libfs: getdents() should return 0 after reaching EOD") Signed-off-by: Chuck Lever --- fs/libfs.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 8c9364a0174c..5c56783c03a5 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -245,9 +245,16 @@ 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 */ +/* simple_offset_add() allocation range */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_MIN = 2, + DIR_OFFSET_MAX = LONG_MAX - 1, +}; + +/* simple_offset_add() never assigns these to a dentry */ +enum { + DIR_OFFSET_EOD = LONG_MAX, /* Marks EOD */ + }; static void offset_set(struct dentry *dentry, long offset) @@ -291,7 +298,8 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) return -EBUSY; ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN, - LONG_MAX, &octx->next_offset, GFP_KERNEL); + DIR_OFFSET_MAX, &octx->next_offset, + GFP_KERNEL); if (unlikely(ret < 0)) return ret == -EBUSY ? -ENOSPC : ret; @@ -447,8 +455,6 @@ 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; return vfs_setpos(file, offset, LONG_MAX); } @@ -458,7 +464,7 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) struct dentry *child, *found = NULL; rcu_read_lock(); - child = mas_find(&mas, LONG_MAX); + child = mas_find(&mas, DIR_OFFSET_MAX); if (!child) goto out; spin_lock(&child->d_lock); @@ -479,7 +485,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) +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; @@ -487,7 +493,7 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) while (true) { dentry = offset_find_next(octx, ctx->pos); if (!dentry) - return ERR_PTR(-ENOENT); + goto out_eod; if (!offset_dir_emit(ctx, dentry)) { dput(dentry); @@ -497,7 +503,10 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } - return NULL; + return; + +out_eod: + ctx->pos = DIR_OFFSET_EOD; } /** @@ -517,6 +526,8 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) * * On return, @ctx->pos contains an offset that will read the next entry * in this directory when offset_readdir() is called again with @ctx. + * Caller places this value in the d_off field of the last entry in the + * user's buffer. * * Return values: * %0 - Complete @@ -529,13 +540,8 @@ 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); + if (ctx->pos != DIR_OFFSET_EOD) + offset_iterate_dir(d_inode(dir), ctx); return 0; } From patchwork Fri Dec 20 15:33: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: 13916891 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 82B1BE77188 for ; Fri, 20 Dec 2024 15:33:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD20C6B008C; Fri, 20 Dec 2024 10:33:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A5AB06B0092; Fri, 20 Dec 2024 10:33:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AE906B0093; Fri, 20 Dec 2024 10:33:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 69B876B008C for ; Fri, 20 Dec 2024 10:33:26 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EF4CD80ADA for ; Fri, 20 Dec 2024 15:33:25 +0000 (UTC) X-FDA: 82915730814.05.ECA8D94 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf25.hostedemail.com (Postfix) with ESMTP id 9DFE2A0018 for ; Fri, 20 Dec 2024 15:32:59 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IDsNQlbg; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.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=1734708788; a=rsa-sha256; cv=none; b=DFTeuNo+1YI7HcZikMydZaBtqB2OouKw4PCF9O3LYR/FtfPkRLl2Ovy+tX2u/TdATDFZtr WRsIYratk73olEaT61fNJMFkfrQfr5UtLdlLVSfjytO98/djRB8cnsCLF5pO0EJ0S58xmU 25gf9URqeZMLwsdfBCDSPDTPWZcFEG8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IDsNQlbg; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.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=1734708788; 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=bmGtV7mTGrpwrYWYrwo7RfamUmQFLG9UDcb7skUIJmU=; b=W1sS5r7sMMERdDc39AdXFvlrqrJT2r2+Qu6P+/BS+rEiMxA98ByZECIJmzQJ6MVYGbxgqb l/Rl59w99b/rO5CuAKvC5HQ59cpSFR770NyvJVuaartlJZywUnvKoszxMjhB00zBO2GW1j rd0F7wzJ6E8YCUUOagsIWZBL0b7lPKA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 153B6A41CC0; Fri, 20 Dec 2024 15:31:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B8A0C4CED7; Fri, 20 Dec 2024 15:33:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734708803; bh=psAnJLeu3hNfs59sG2ggPtKJEHtiA7mAWB4G4Xrk6tA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IDsNQlbgHXeuReqCHgXioyS/pdHOy3vGgcJZ6fSGc9D/qnEjc0eOFQPfe+BuxoI8K B2/lmZm1UCzAnbVKGIyiRccGs9V2Rhi6oAUdInqd8PB9bF0tX5vlTMmP/VT0U6uGz/ IG+2j9WlhYs4AIhpsAaCYHa4zdwa68C0PcsyeIJW+eIsx92hA3aGuj3S6LeWCDmJMI x1fZ+2072BuqEj0WN/ZUHKPBECkawlGjckWWT31YVtMKaZEV+Zopts0zBRZcEf9F/H 8w9VxVd6M0CVDsCrn+/O/EPfgaXYTpjpXsri39GrSWgZksQs46QzD5XQzbt4o6YXeu ZqZizLwzEsVmw== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v6 5/5] libfs: Use d_children list to iterate simple_offset directories Date: Fri, 20 Dec 2024 10:33:14 -0500 Message-ID: <20241220153314.5237-6-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241220153314.5237-1-cel@kernel.org> References: <20241220153314.5237-1-cel@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 9DFE2A0018 X-Stat-Signature: z8dudfr7ofkzwey4n9j1r6oya98txokt X-HE-Tag: 1734708779-435160 X-HE-Meta: U2FsdGVkX1/GuGE9XbPG9cOF4v5+26/iTsQcHjduZOk6sT+BOD4d06czebipb+KRUrEsxetK39igIBdERWFElsudftWeg3hCrFXJ7lxZexDiN2Uz4GLXo1AYQb+tlFMBfRxhTcusRjhbkmNEmUjR2y72UW5sbXq6iuAEfb8NudXSYfXSIMVSRnyZUQFrHaxsPtaIwTyHWsg29M06cOXs6RdjIGntGjTOlsDTWSeFNNXljMtZF0w6Ii8Bh1msh7LsI6+bgmmEvrQiBgxQ3+c721XUFQiIUT0ykD64QsHZJZ2ItdGRwyvOG/9YxfGXgFPPWo8oQRV4eRKeXy2XstJ4yN8Tl5olCU7XmTN1R1GSYSXbtYt0/KcCFn2eJO1R5Vk09TZH8pVMJ69POCeQLWf/r1FliCUn0ALhqHLTq1BAZYCppVqGBIyQdHNCyG7T8DHO107NZqXjT9Kgg676Hk50UsPp12b2AyYmrYQ4RARjsEnTZQXZwm9DP2gqdMnuMRVj3mK6Pu608h/RZKf2/NFgfo9XixX7fynFARP2ZvaVxXKdKOT7n3Nw4YvtUQPSMn4WCI5FjFeoa8cvBrbJkqxFVJyijO5IHHz60OQhn60At0GZmKeTT94gPmVX1Gfkf4rPmVsRJRpSfTv6zOKekR21Z3DPFslznZCMi1hxgH/LogJPcrBRGJQZM0TN7TpCB3mQr8/gkFYFah/2ESR5na/lTUIfd3zpIscdlJFwJUF+GE4/FoDxJs6lY9zNVITb2PUKwmcYczvukJRz5hUJf5+13p/8vbHWdR//cDRWw5375jAJaL/SK62HxlZ3dIXdrQYbVVidOf/KRK0N+1bydQPt9hpfFhiMoCn+7ETGIGTlyu0n4gi1TXaV0PMbO5dnTmKqZP5H7ryL2fqdUu82wmCpBlMR2w2w/rNrH4pZWzGQQ582XRftn/LsFJHiB4KN3X9Ax66AThfhV5dIO9NdsXj YK7H2oqy lDj4yPOTpX8d7LxGvb8asDTMT054gMdmdNK3vQcrsc/I/ORNLoSJWE6+unTHntCeJ328zvnBJNl3gvYeBaJb3V4xOVnOdNTZ81GqpnbExNrEq3qKuS2LEqcF7pFBS7G2w4oVbQrdN+rZv8rfSdkU6qdANkj440GtaTxQ7HHSg1bifW4+LJ2+bZGH2X6laWMvK+fVnz9g3v6Ic73ILVAHz4NCWe4eTW1mIKB59Fd6864ZIlPDucqHtl5LNRFJ4H7ybYSvhFYcCO2eJOcygHIOGbdXHmg== 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 The mtree mechanism has been effective at creating directory offsets that are stable over multiple opendir instances. However, it has not been able to handle the subtleties of renames that are concurrent with readdir. Instead of using the mtree to emit entries in the order of their offset values, use it only to map incoming ctx->pos to a starting entry. Then use the directory's d_children list, which is already maintained properly by the dcache, to find the next child to emit. One of the sneaky things about this is that when the mtree-allocated offset value wraps (which is very rare), looking up ctx->pos++ is not going to find the next entry; it will return NULL. Instead, by following the d_children list, the offset values can appear in any order but all of the entries in the directory will be visited eventually. Note also that the readdir() is guaranteed to reach the tail of this list. Entries are added only at the head of d_children, and readdir walks from its current position in that list towards its tail. Signed-off-by: Chuck Lever --- fs/libfs.c | 84 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 5c56783c03a5..f7ead02062ad 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -247,12 +247,13 @@ EXPORT_SYMBOL(simple_dir_inode_operations); /* simple_offset_add() allocation range */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_MIN = 3, DIR_OFFSET_MAX = LONG_MAX - 1, }; /* simple_offset_add() never assigns these to a dentry */ enum { + DIR_OFFSET_FIRST = 2, /* Find first real entry */ DIR_OFFSET_EOD = LONG_MAX, /* Marks EOD */ }; @@ -458,51 +459,82 @@ 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) +static struct dentry *find_positive_dentry(struct dentry *parent, + struct dentry *dentry, + bool next) { - MA_STATE(mas, &octx->mt, offset, offset); + struct dentry *found = NULL; + + spin_lock(&parent->d_lock); + if (next) + dentry = d_next_sibling(dentry); + else if (!dentry) + dentry = d_first_child(parent); + 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; + } + spin_unlock(&parent->d_lock); + return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup(struct dentry *parent, loff_t offset) +{ + struct inode *inode = d_inode(parent); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *child, *found = NULL; - rcu_read_lock(); - child = mas_find(&mas, DIR_OFFSET_MAX); - if (!child) - goto out; - spin_lock(&child->d_lock); - if (simple_positive(child)) - found = dget_dlock(child); - spin_unlock(&child->d_lock); -out: - rcu_read_unlock(); + MA_STATE(mas, &octx->mt, offset, offset); + + if (offset == DIR_OFFSET_FIRST) + found = find_positive_dentry(parent, NULL, false); + else { + rcu_read_lock(); + child = mas_find(&mas, DIR_OFFSET_MAX); + found = find_positive_dentry(parent, child, false); + rcu_read_unlock(); + } return found; } static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) { struct inode *inode = d_inode(dentry); - long offset = dentry2offset(dentry); - return ctx->actor(ctx, dentry->d_name.name, dentry->d_name.len, offset, - inode->i_ino, fs_umode_to_dtype(inode->i_mode)); + return dir_emit(ctx, dentry->d_name.name, dentry->d_name.len, + 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 offset_ctx *octx = inode->i_op->get_offset_ctx(inode); + struct dentry *dir = file->f_path.dentry; struct dentry *dentry; + dentry = offset_dir_lookup(dir, ctx->pos); + if (!dentry) + goto out_eod; while (true) { - dentry = offset_find_next(octx, ctx->pos); - if (!dentry) - goto out_eod; + struct dentry *next; - if (!offset_dir_emit(ctx, dentry)) { - dput(dentry); + ctx->pos = dentry2offset(dentry); + if (!offset_dir_emit(ctx, dentry)) break; - } - ctx->pos = dentry2offset(dentry) + 1; + next = find_positive_dentry(dir, dentry, true); dput(dentry); + + if (!next) + goto out_eod; + dentry = next; } + dput(dentry); return; out_eod: @@ -541,7 +573,7 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; if (ctx->pos != DIR_OFFSET_EOD) - offset_iterate_dir(d_inode(dir), ctx); + offset_iterate_dir(file, ctx); return 0; }