From patchwork Fri Jan 24 19:19:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949883 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 79321C0218C for ; Fri, 24 Jan 2025 19:19:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10B6F280096; Fri, 24 Jan 2025 14:19:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BA0F280092; Fri, 24 Jan 2025 14:19:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC55A280096; Fri, 24 Jan 2025 14:19:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C621F280092 for ; Fri, 24 Jan 2025 14:19:54 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7D00FA0FC5 for ; Fri, 24 Jan 2025 19:19:54 +0000 (UTC) X-FDA: 83043310308.14.377740F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id C4EA61C0015 for ; Fri, 24 Jan 2025 19:19:52 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QKo7eiYs; spf=pass (imf20.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=1737746392; 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=OG9Wcof/bc4xR3FPccrdiiZ/fDzrZduD5mlzSE5vVsI=; b=ZiQkwSS+dLrvU993HBZts5Uw6BGYsPOQL6AwvQOCE0YDCdBfs/qNgWdG4N5HgYn0LUoWH3 P3X70qYzGo+/+1x0s9aUjgWiyh6P67c6kJVlSgV1A7ju+s9sM8WgMjwfR6qVg2ljn0NNh4 5OeZb6GzHKYm4eQFkpqtyg1MZPMaEAo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737746392; a=rsa-sha256; cv=none; b=iVVehvfNamt/6QFI0O8nvjYItidtTdNnbD+SaGBy2lTkcS9G6Ug+9cC962cJ52VDio56FP EeLWuoWkrGzWkF9F67tPFmKfhbsNgZj94vOAUwUt/YYOnduVzauOUNXq+dnLZl2ugbXfDu dE3ty6/Az5Ya8of27uJWOTeclXmc8ZA= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QKo7eiYs; spf=pass (imf20.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 45AFD5C6196; Fri, 24 Jan 2025 19:19:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2853FC4CEE3; Fri, 24 Jan 2025 19:19:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746391; bh=AAimS1DXsD+1vqFtQxl+lQZkXctbDP9Ux3Ct0lAIRKY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QKo7eiYsDqb+VJ1oghRzIW9zWNc0+ukRB/vdi13rpjkOm/x9/En0p5WvGX9nbJBoE HKKpJWQKWEq9pjE+a5Gj5Vkn3ZJDWMNASXHPi/KNRrCFjDrc2JApS7dOBmshLFifcJ kQHpa+wwadPT1PZ35KhcpESCLAeXiAYkgJOpq7xYRwZjpO+AjnvksUFltUOinBZ/ys oDlVcHtv4t6rEoisItohXmncZXTf5yIiyu2U8gGGuNLFtRu8nCIFZZfjxilbyW7BD2 Sp6Py8Q3zyWbOYEDHjsfHF6GR4d6tifGaCioJUdrvRKF7xpSpxttbURQsEQhr1uQCZ QgUKGWdEXQJlQ== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever , "Liam R. Howlett" , Jan Kara Subject: [RFC PATCH v6.6 01/10] libfs: Re-arrange locking in offset_iterate_dir() Date: Fri, 24 Jan 2025 14:19:36 -0500 Message-ID: <20250124191946.22308-2-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C4EA61C0015 X-Stat-Signature: 5xztj3uayfgoozh8oegb6esbgrby8jz7 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737746392-201146 X-HE-Meta: U2FsdGVkX19YGJVg0n6Wx5iowPWF8oAKrGOQBQSZiWAXAYEEXAC+GYd7KmkNzNKHloiLweAwOJ83bKvoLDkqYMfjzancgC3WmwzEI/++suZ30DBuR+gFdo3KNO6LN7b08ALWyk/lzVybrsd9nieq8XOm00PIH5gFxFg1lRLBd5dFY7mL8GJT816nRGONwmCs0Ho6MSMkLEQPur6u5teKjSWcI/dnRn3vc5LmaFTdIxgXiFPFpqQ2xHt/9XJOH0e3cI37esWkvA1PmNw+lsOapG/Sso/SefotlfjTb7SSZrFrK5IrPnT65mdSdrpUL6MF/TcFDdscb3K+S0vGBjeZej9A1pkXZJNJMGJfqGeW31fk3ASO0+jJpQoQQj4w97wbeeU82Y6X1mREsI7/tlEj0Jgs1ZigfhbVuJwkbp58IeLlS3/Cse98lEE4gCL/pp/XGuaQLZSVCpUKadG+Ds+qisGpxsg6UHE5E+Diftvm+ho6VJE40Cfo2Wb3WLG2O03JzRc0icI5aVbTtKWLQ7TujuI9ewXUx7ylAL1XfLkounIUgU63rgTH+kxYXt3V7+V1OBv0yZpieFnd+eA+af5TUwFFQV5DuARg7TspocStDcA/xc+pvv6P0ZWqGvwF+gasxYGjz9QhSdnfh3/Xsf/8dWYoUxHByuRwI7ph7SGGjHBqRnAIuXE7gXBpkaLiLKuG5j/axHH8P5Zu95zzrR64GVxHLSbr/geFRT7zQnpkRre5m5qeXIJrZLGZA997UfFSH5QJ6LkivP78c0yme3JvPa01pUZ1dWXZLoPB3nz2COihGvCuzusIJ17gZaqCgw2YPNETQYomYrZbPJ5BrAytgTZW6nq/HTp1tBysdVnv3pIQ325NUHoeOOBk+g5ES52Hau5QWlGhL2lBO5FkNZceGlNWBXzkfIXPSIX4QSJilZVF7RlxraO4llHxL+d0uqeEWv4U7ChIk5uIQGygFPJ 61STOntw pkjfHj+N35i8jXIi02WT757Fk0srJN1LkTnS+e12uaJECEbG0aM0S1GCdAhdKAkhnB5xY2dI/sRZgZ9LPbnu5D45F3FMutPapKiQEJVwnAxayhpLAoerKvvF2F8VpL/39O+t17tds/0PamE3F4tUZwOgKgtuR+D6g/+CbwMJKKzyqceGQqij6ufR7XLXy5iKrR/hhtF1qI9awEsST4z5Dydo5vUOce1fOuQQKx+txtaQRNIKCjacegS6+mI1NXbEnox2WJhxrycARBL9mpS/G60HaC0234OfqCIrQ24zDflGY6JhYi5UGo9K/6BrhCfEO74oi7e5Xi/0sWkigv13QeEVPYrVRNDEWqJackq6ZDrHCy5w= 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 [ Upstream commit 3f6d810665dfde0d33785420618ceb03fba0619d ] Liam and Matthew say that once the RCU read lock is released, xa_state is not safe to re-use for the next xas_find() call. But the RCU read lock must be released on each loop iteration so that dput(), which might_sleep(), can be called safely. Thus we are forced to walk the offset tree with fresh state for each directory entry. xa_find() can do this for us, though it might be a little less efficient than maintaining xa_state locally. We believe that in the current code base, inode->i_rwsem provides protection for the xa_state maintained in offset_iterate_dir(). However, there is no guarantee that will continue to be the case in the future. Since offset_iterate_dir() doesn't build xa_state locally any more, there's no longer a strong need for offset_find_next(). Clean up by rolling these two helpers together. Suggested-by: Liam R. Howlett Message-ID: <170785993027.11135.8830043889278631735.stgit@91.116.238.104.host.secureserver.net> Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/170820142021.6328.15047865406275957018.stgit@91.116.238.104.host.secureserver.net Reviewed-by: Jan Kara Signed-off-by: Christian Brauner Signed-off-by: Chuck Lever --- fs/libfs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index dc0f7519045f..430f7c95336c 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -401,12 +401,13 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, U32_MAX); } -static struct dentry *offset_find_next(struct xa_state *xas) +static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) { struct dentry *child, *found = NULL; + XA_STATE(xas, &octx->xa, offset); rcu_read_lock(); - child = xas_next_entry(xas, U32_MAX); + child = xas_next_entry(&xas, U32_MAX); if (!child) goto out; spin_lock(&child->d_lock); @@ -429,12 +430,11 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) { - struct offset_ctx *so_ctx = inode->i_op->get_offset_ctx(inode); - XA_STATE(xas, &so_ctx->xa, ctx->pos); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; while (true) { - dentry = offset_find_next(&xas); + dentry = offset_find_next(octx, ctx->pos); if (!dentry) return ERR_PTR(-ENOENT); @@ -443,8 +443,8 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) break; } + ctx->pos = dentry2offset(dentry) + 1; dput(dentry); - ctx->pos = xas.xa_index + 1; } return NULL; } From patchwork Fri Jan 24 19:19:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949884 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 689DAC02181 for ; Fri, 24 Jan 2025 19:19:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0BD21280097; Fri, 24 Jan 2025 14:19:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F127A280092; Fri, 24 Jan 2025 14:19:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD9A4280097; Fri, 24 Jan 2025 14:19:55 -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 C281D280092 for ; Fri, 24 Jan 2025 14:19:55 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 740B11C8F9E for ; Fri, 24 Jan 2025 19:19:55 +0000 (UTC) X-FDA: 83043310350.25.CFB44A3 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id BF9F640011 for ; Fri, 24 Jan 2025 19:19:53 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RcLi3zNw; spf=pass (imf11.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=1737746393; 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=ihmQOTy6+BASqWfzRvTfXF5KdhKLJ9REmXJq/W81ygA=; b=b3nv+CG/Cf9tfIfUl/hEdo83B4Jqt07ffXBkiz8h3d+E8CnLflMtUR5F5IgiVtMnxPqGCg 4ehvz7QQpMdcgN6oe3pxtVGMx15cXcWOnQOxrHUw12WZeY76P6IZwmgwmJQnIlBSGANpT8 wBaIQkNroGcsln2R/+Gtr133Liq3OuI= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RcLi3zNw; spf=pass (imf11.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=1737746393; a=rsa-sha256; cv=none; b=JG2kNMO6KZAEh4YUFDNfBuML0/9/b9C77ZhL4QwSP/mrP6xZaRbUFsozWAkxtAWc7eKVqR 2FMsZj5krrhJMV/1yfKNQcaYTr6QJMKHq3W7wqFpBsBSCeGHho5rk7bvxqkjVqot5D8Q5A 9dx4GHLwJgKB0kfQ9lEZYTnHZV/Q9SU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9C7715C6197; Fri, 24 Jan 2025 19:19:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 972E7C4CEE1; Fri, 24 Jan 2025 19:19:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746392; bh=aTa/oPIRhiZqsiUQhL063GDzxrb6flC1H3iG2TVuGAg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RcLi3zNwUmn6+D7krNKMAsLu2+/e4oFjxsu0wxzCUBNfUbOBKRndiLIbvyh6gRVcK yvusJypoKUf3J07VZh/SGZ+ayRV9WfralmbkSzFaoaksAUv1YRtX70/pM43krhTpDs 3LYGPKs4zAtTllP4ZJs8pYaJNDQP8XUQkefM/Gs9hoBZhAwdzz/HfOTl+HY71Wli4x wsUg86mAIbqmASezLl0yPIkg+kzWr1fDNMm11d9FizatAu9iaQOefDGJj7l5qCO4+2 60xlPNVkfUYE2gUfVk5DkErDmcoOHDwLKFacFwmKPwyOcHcU1cz+UGzOG3sgYKKPT5 OiduZIrMtg68Q== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever , Jan Kara Subject: [RFC PATCH v6.6 02/10] libfs: Define a minimum directory offset Date: Fri, 24 Jan 2025 14:19:37 -0500 Message-ID: <20250124191946.22308-3-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: BF9F640011 X-Stat-Signature: foi7ixarrfoahnsoy5rk8bxz1xbgsgsn X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737746393-352392 X-HE-Meta: U2FsdGVkX1+wHxhA3fmDAGL/xXBXwC+/80rshKQtDKO0kG+h4RpkZYaSdkizJ+op8z9Spg08ErNbIxQKN41lnHK25dTCYZ4Ea5nj3twtFhrvQVOTtWavFbNTgUEf7IocdCNvqCj504CiiR+XF/SY4FRz0oP946AEoH8yQ+/KIqeRuyIz7pE+UYalBOUTrs9PtiSIahAX2RiA38AftrNmaygiiHiekHHXWGBwpVlV6M6VPMJGg66CUpi5Lx+R7z8KDhGJSmfiCY47KoZC7olekEsffReO4gUHuMPnQvWkM38JnK8EVXh1cPBwqWOxfA78D031YIyMRi/4vfagItT2NigVDDF93NQZVGdAknMrvaYlgsO47BJToxbz+IJg9paFlE9p9rPBw9xctcD42P5Ilta6QA4dz91lrJM3sQzzO2Lx2dhvPl3yPsL80b8wChFbimS/I21ZUtESbEUtLnR26F1qafJpizxJ2wULi/C0kVE8g7mcMcn+l8MMLBFx8+oLvcooYIxnUaPh/Sudg1oU/4k3mWZVrqUfdHt6zbuGKDEsY/V9XOiAj/2u3OYaFIcEl/5mKt9b5ITGgs25onu+maikFg/amKF/JTMC2PYmLaP63MVW9ogAV827bageFX/yffOMW8jf7nfQdYSyGHW9qOekU/lrrItZm7zg8sUvjd7b4OoH3EP2tGk6e1LaGgNoyjYlaZkBPQ7pzTnx0OOHk6GNbcBzXENKyy4mshva2xwldZa//iD4BsmNZ+ddqNpV2m0ey0SLq/9p1NWe5UA1CjcY4k2O6aXIHSPmaWDtDWxNMl4/aWAUV5tOmVvgGoIiDOeuKOp4C8qjuLzqsNm4ZLohSTZJqashHOGiaf3yGW4TJQWF01fOwZCiQv1iqtL9sTe0Uf5sNlBDm5h2CJCz0lqajqa8BcSkgtB/gqO2dAw02Mm4q12P+dZ6KxAkNBBJDTKUK8uqp8bBuB7g5Hf DXzbSYZ7 VAOlMUChhwlHrtt5aKr1LIhgT6E2myaxG4nczWt0OLLhlC8biOGyys6Ht5PYTnfM8Wtt3qDhrGz4g/OXa6VnYow9q2UCXoilJzgG5X2gYAhlsa7+cexv/IXPFtPJgAXBylIisUfU8KqKHsUGRVpHTQ65OBo+qcfnA0HJ3eANm+xXbtMzkACxwktIxhe8dlA1hWE98R5pKq7v6/gwFcw0+gjXPkKU7UCfAGYgBx7rKg1jH0Zd0tSmxM7Tq9/rQRdM2G0ACBlcqhCA9nobVH4ISj6HfAhRTOh+hAm5sU71yqOb3LkwHV4QrZDSdRA== 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 [ Upstream commit 7beea725a8ca412c6190090ce7c3a13b169592a1 ] This value is used in several places, so make it a symbolic constant. Reviewed-by: Jan Kara Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/170820142741.6328.12428356024575347885.stgit@91.116.238.104.host.secureserver.net Signed-off-by: Christian Brauner Stable-dep-of: ecba88a3b32d ("libfs: Add simple_offset_empty()") Signed-off-by: Chuck Lever --- fs/libfs.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 430f7c95336c..c3dc58e776f9 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -239,6 +239,11 @@ 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, +}; + static void offset_set(struct dentry *dentry, u32 offset) { dentry->d_fsdata = (void *)((uintptr_t)(offset)); @@ -260,9 +265,7 @@ void simple_offset_init(struct offset_ctx *octx) { xa_init_flags(&octx->xa, XA_FLAGS_ALLOC1); lockdep_set_class(&octx->xa.xa_lock, &simple_offset_xa_lock); - - /* 0 is '.', 1 is '..', so always start with offset 2 */ - octx->next_offset = 2; + octx->next_offset = DIR_OFFSET_MIN; } /** @@ -275,7 +278,7 @@ void simple_offset_init(struct offset_ctx *octx) */ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) { - static const struct xa_limit limit = XA_LIMIT(2, U32_MAX); + static const struct xa_limit limit = XA_LIMIT(DIR_OFFSET_MIN, U32_MAX); u32 offset; int ret; @@ -480,7 +483,7 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) return 0; /* In this case, ->private_data is protected by f_pos_lock */ - if (ctx->pos == 2) + if (ctx->pos == DIR_OFFSET_MIN) file->private_data = NULL; else if (file->private_data == ERR_PTR(-ENOENT)) return 0; From patchwork Fri Jan 24 19:19:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949885 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 620D4C02181 for ; Fri, 24 Jan 2025 19:20:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D3A5280098; Fri, 24 Jan 2025 14:19:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80D8D280092; Fri, 24 Jan 2025 14:19:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D4A1280098; Fri, 24 Jan 2025 14:19:57 -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 4F5E6280092 for ; Fri, 24 Jan 2025 14:19:57 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D8FD2A0621 for ; Fri, 24 Jan 2025 19:19:56 +0000 (UTC) X-FDA: 83043310392.20.C97D5C0 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id 35F971C000B for ; Fri, 24 Jan 2025 19:19:55 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kzuTtwb5; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf18.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737746395; a=rsa-sha256; cv=none; b=Sx3JDNSX3ZXOC0lFBwq+Th192LOdL3EvcjwU5KfKQ6R7joDpoqsjPhwuuRgWgobgglqFYJ LohXltN2OrMERiRGG7nIFfVzBpmjxrM+0gPP5BV4Cqel3LgleHOrXV7F8N0Gs/bHfJ6jR2 ShRE9WqtCqHFuUV0MKdFIvmQf5B71TM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kzuTtwb5; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf18.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=1737746395; 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=L1nfY3R39KZuQs0KcQdEhLOvZwTICwR85EsrnGFqJV8=; b=0+GgEPosdnFpSuiXr/2JWe+okYXcCN+kOO0XnQr9ZvpZPwe1guLloxj9+3C4CAcY8szGVs 72fZ95nTAVtAKbxgCxZtp+41W0W2fS4a+1Rwv2E9CD7i00MBb/44sd5C6vds0POD4KxhNi tKRY7gcYKyFOzL5p8W+SkUttU21/fL0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 07DBD5C619A; Fri, 24 Jan 2025 19:19:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F03A9C4CEE5; Fri, 24 Jan 2025 19:19:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746394; bh=kzHPpa2KwO/4QYlO3ISbItr5ch0/fuaXPOLFlhxSI4Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kzuTtwb5nFfTsInS53xDa6b64DCNsdRWaMcj6hDJUkWKBlrZXHEIYasBzIi5+sp/5 fmAWr5c1weaSlZ/q1je+J9GixE8pbr/1FxZbOTsA6hAEK3lVBPi7SdrYOpEzo/R0Vw 1FLASw+xfWjztHLBBsn3VGYvCAfh0sRdBni7A2mN9INCkcCDuo1HjjsFAYVuO40T9J GRwbxR48W5ymurrPq6KoNOpua101EBRDD3GaNGpo11XzXkG6zQZHTJUayLHa+ialwG DgaroVl03FtpTzP7mgPmDvki+KMdGcc9xnnzjJbz2XbTve+BLXiOR4AS9pyDT8mxRS cR/RyT1qdB5AQ== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever , Jan Kara Subject: [RFC PATCH v6.6 03/10] libfs: Add simple_offset_empty() Date: Fri, 24 Jan 2025 14:19:38 -0500 Message-ID: <20250124191946.22308-4-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 35F971C000B X-Rspamd-Server: rspam10 X-Stat-Signature: oqfpnwumwen3xe4eowth5qbrjzsi7q98 X-HE-Tag: 1737746395-750191 X-HE-Meta: U2FsdGVkX1+WHy5UWScVOT1mYWpnMyVhB5fmA0BHop0D83Ap8/Fkw5gjlCiX86a98vtnJkSKDq2IL/ZjuO3Q8MfncV6QRT1sK/OxhjzKguj6cozIgea4japL1aJGH31DVWP2CB9xbfnTr+4822Azu1gMpNvKI7BcKxaA4C5bn9sRag4X3+myn6m+gF2YvcJ/Np1D4Jl8yIhikD33ZYOxBa9+UPPEXQq8U7lPQ47iWBmkvWjnm2bzNYytJ/RvyiF5oAmWSvXCPgxuqHfhtgcSpTGsVEyk66EU8BdmRSUPWdqguyDNMriXw1aQ8/cxikXrxSV4mlE6yKep1uDYIaegrhMPA4p+y1ofCWoc3BRhniAD1RyeXje9st0a/50VmAX2K8b4YkbRRq0hg2NU2tkSKVaDRHBCFPzSrw/3I+C+FfHdDD28oKse5RU3mqI1c4BGlQSCdA0Z59pSC5tFYieCfVqUo5SIMc49S/TUrbezT0CxO2+AD7RBPvGevzpP2yn099SvtkRgLyi5JRusFViimBrT2rJxAhdXocEyyIebHVbR5JF2XvaCJIUMrEz2t/VrIowEunyzh6RGTq9NbAJbwUCWjekfsIKfv09LPOUW5Nrdcj/vHEPfQm/mGFbuXwLzhXWdlJiCfPJoMw2izV15Yoq6L+slLIHrYaoRXmro4E6nSzwn4Z+tGxksLT1Zq8GFbKEPNmRleqZlO3NRHH7Yp/K7vkQ7MRnS5RSsiogTPTu1DwiHPgh8DTRnS3Oa6C16NiWeCBi/PQvl9hMpVdp9D853JZovnkdaX+FMDVlGJ8QDMyKw41rnJG/xXH7TvRrJbbjkhPlOsl5NBQdo5cdcdX2/TSqTvvAz1bCKxaWXge4lbccgmXTzjmDTZpbottwLcvkLduVMSZIB3ij7IcUzZE1rY5gZUQTghyBPuRhuOIuHNTCQBVzWrASfNSTKdG64Vgx2H/Hj7bamGSnFntQ aU9IUXvc 6Jyknpp/A2HF0pp9fW2PF+HyGyWAgo9TqORJ0mbaa5JSgjPJNza7LDZ5Zy6uQuTdo238Yg9lyLJmx1wtDjVlalsBlFvOQLzsp0H29nFJyjN5ag1NpNvWrpXzfl9sT6cJnO9KIJ8DXI7qMtBkFu4KbCyi4sQe7dkpKdoOh6RNk5W/587nXnxz600guAyAljfqOe086PVptH8edO0/Ym8/JE/1GKq/eU0f2xrubawgC0Bf8t8pn5NSjV4SBMS7s3wsJw2fM7fj/+Jw4IkfUDVGGjwmFXY5OB2LTColA4HW94RNQv/skXiha5s7VHgpvI5A6TsV592lb/N/atlTt9eNei4I3DHyhbgzEh8GH/REXrSebZtw= 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 [ Upstream commit ecba88a3b32d733d41e27973e25b2bc580f64281 ] For simple filesystems that use directory offset mapping, rely strictly on the directory offset map to tell when a directory has no children. After this patch is applied, the emptiness test holds only the RCU read lock when the directory being tested has no children. In addition, this adds another layer of confirmation that simple_offset_add/remove() are working as expected. Reviewed-by: Jan Kara Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/170820143463.6328.7872919188371286951.stgit@91.116.238.104.host.secureserver.net Signed-off-by: Christian Brauner Stable-dep-of: 5a1a25be995e ("libfs: Add simple_offset_rename() API") Signed-off-by: Chuck Lever --- fs/libfs.c | 32 ++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + mm/shmem.c | 4 ++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index c3dc58e776f9..d7b901cb9af4 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -312,6 +312,38 @@ 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); + xa_for_each(&octx->xa, index, child) { + 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_exchange - exchange rename with directory offsets * @old_dir: parent of dentry being moved diff --git a/include/linux/fs.h b/include/linux/fs.h index 6c3d86532e3f..5104405ce3e6 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3197,6 +3197,7 @@ 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_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, diff --git a/mm/shmem.c b/mm/shmem.c index db7dd45c9181..aaf679976f3b 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3368,7 +3368,7 @@ static int shmem_unlink(struct inode *dir, struct dentry *dentry) static int shmem_rmdir(struct inode *dir, struct dentry *dentry) { - if (!simple_empty(dentry)) + if (!simple_offset_empty(dentry)) return -ENOTEMPTY; drop_nlink(d_inode(dentry)); @@ -3425,7 +3425,7 @@ static int shmem_rename2(struct mnt_idmap *idmap, return simple_offset_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); - if (!simple_empty(new_dentry)) + if (!simple_offset_empty(new_dentry)) return -ENOTEMPTY; if (flags & RENAME_WHITEOUT) { From patchwork Fri Jan 24 19:19:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949886 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 12009C0218B for ; Fri, 24 Jan 2025 19:20:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEC9C280099; Fri, 24 Jan 2025 14:19:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9C3D280092; Fri, 24 Jan 2025 14:19:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1717280099; Fri, 24 Jan 2025 14:19:58 -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 AC75B280092 for ; Fri, 24 Jan 2025 14:19:58 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5D1491C8FD0 for ; Fri, 24 Jan 2025 19:19:58 +0000 (UTC) X-FDA: 83043310476.16.56A3C66 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 9230214000B for ; Fri, 24 Jan 2025 19:19:56 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Bw2F45L6; 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=1737746396; 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=KIIV21lLVO3g9WZ61MfwaYlRfGjTNt4sT2ieQbcpK+A=; b=EkG34PaLhKde2ggAsD+tXrUxR3N7DLsJsyA8aA/wzQk+/KOX/C2iA7h+eYv4ASoJGhHfXw 9pxlxdDFlO2x/HkLL7uHc6PEdHYuA2vc83YvHxUeIYer3n0vYKP0HGCeSwBCkgOSEI+L7C UZoaCkJFaBklkZfITsqyJ/gWV3ntY28= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Bw2F45L6; 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=1737746396; a=rsa-sha256; cv=none; b=3w4dazxMuXMSWwkZ8PckfzxgoUljbiOuo91HVKQ4stDhaHrKSjhjoLvHIFADIcl4gwuVyg O6h1EEfapW39POXTLY47E9I2ZLl1wK7gqWhGC0KRdowP8kCLR5OtBvC2yNQelArYZTLsi5 mn0Aq25JR7IRevUoC4Hmi90ZJnqiqEM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 679FE5C6195; Fri, 24 Jan 2025 19:19:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55E93C4CED2; Fri, 24 Jan 2025 19:19:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746395; bh=S3TpyVqlQ6GbQ6WEv5W4bM5DS5m/bnNZcBueUm6fZKc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Bw2F45L6uyN95VvlhmGZ+g4u/m5LECYA5/LAsY1EmhxFlU2jq6MfD+cMGacR+Ld2v p0aTtj40pSk6001/wmYSKhZDxqdzalUgg29lktQoX7WM/It7P5JeL0DDTbi2BgarK1 aqg3rsJYNXlDSgEfdlA9HPITqPGdQ7b6a5B9okkPLIl12h8XQFGY32nmKeBdFDlJR/ eeS/6GfYh+zNGGZkiMitIsaKbpA9TBHhGrY0MZt6ziQZlAFXK7Qx27N/G5+G3q8TDh MFhqHTARUZwQCvDprCMXDiIXPzmJ/nbhOrwDP/bJk9U/7jIRCPya2sMAV9/1DS3yvb HfkpCW6XtoGBg== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever Subject: [RFC PATCH v6.6 04/10] libfs: Fix simple_offset_rename_exchange() Date: Fri, 24 Jan 2025 14:19:39 -0500 Message-ID: <20250124191946.22308-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9230214000B X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: m9dgrfh5yjho5yitrx1k3hta9auaf79o X-HE-Tag: 1737746396-627233 X-HE-Meta: U2FsdGVkX19xKf1Pn92Ynq1oMH0YY6GoGYr7k3C43u0vQV3ungeXeCaLh6cSuLGe1kARXT3FwO0Ub4GH11TSlxHxsJeDPJJp+R9zajDZtXk5zjTDbNbTxbzGOevW1Xie1QtW3jaIgaPdaXPt3Q/IVmEYtmzFynAumpNkytUwXKfzFEnaEkVJB33EQSNWFyCaHwRCA1IrkXYkCKVV2fUsAps86pPrLKcTltEjsvvG/ywe0EGIdA2rAroWjmQ+ZAz5/OIrPAAhVcbO8soCywD4dc85A232uf64WF7lJ9sfhlHe5Kne6Rv7+E7UQQYQkhPyzWh++HCSCL3CveHMmczX8IW4NUJ3oNkdmrHTfstFQ/UPT/TeUvGc4SQifZt5ApRF+Bb7OmYf7jXLF7O0zsp2fCVBpZaeSvxQtG7rCxXyx1Zo9/kRwIiPFhQg9FiPwm9cLsqwLooVPGK6QSqdZlMXQHTb3SzaOK3MFX8pR+dOSl08+uI7zsch4rcMgS3KdzrcW30he/20IjJ1IpAny0qAfb6mSsyw0go/p0Nw3IDlAOR0PHFvNEOygojwXExLZSCXMmYgujxCNHYczs+3pyZG/2Z4qOGgVx8YUcDaiArUDYf5hV5GkSA/Z8mItIsYp7yKKOH9WhMHU7Tb7Uq+EW+ZHS3Ec/I3OIUu8bTKYxbSCSmNF+phEk1wREz5SwNTb4Juwhdx5Y19uJ2kr6nze85yis4YJPEcEQPTGnF6bvRTjw4sDZO1QUZiEgx/Lvz3ijdvb4BdsXs3MlqDjZ8LsjcXTDUfKMfMg0WW48pecjIjGNq0Qo1hZCBpuDvx350ZkW+94EHdtROmHdtE5DRWaWJSKzG93itcq6dkylDdj1luxyQsRPexasEW4zn5aGvFUg6RcGpazFymti3PEz5F1j+T9nRz608czbwCCO4C0p1/+sbyb2xhRIb3xkZImbvrSIZfFKsCy3vDSSNHtK/MJMl v2hsmaEA z8Se2KfBRgL210hFWr4sP4bNGdcSBmwx4Y5vUpohQEFajuR0pesl7mEyCug/ivjihOipGaKDMLgiZ8X0fCJZMBavfFgx2wek/BnlpAb6xpKzTZ3S4119a516BnD3+DaqVHo4cn4VQOck5y6ujlbHY/CXuHZZaNiKAGmdvU7pmcIYD08f9aXmbzr3sWmuwjcr90UoryLrTrGWmGPgx8F8bkJ3NIPECsJHC7wORwGVM2hsblXDP1neLcdNwo3mrZaeXJ1VtjtiJaaKfx/Y0VCB6aEk6YMobaBS5uFxTf+vyeRFRIvA2iTrcD/hgIQ== 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 [ Upstream commit 23cdd0eed3f1fff3af323092b0b88945a7950d8e ] User space expects the replacement (old) directory entry to have the same directory offset after the rename. Suggested-by: Christian Brauner Fixes: a2e459555c5f ("shmem: stable directory offsets") Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/20240415152057.4605-2-cel@kernel.org Signed-off-by: Christian Brauner [ cel: adjusted to apply to origin/linux-6.6.y ] Signed-off-by: Chuck Lever --- fs/libfs.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index d7b901cb9af4..2029cb6a0e15 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -294,6 +294,18 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) return 0; } +static int simple_offset_replace(struct offset_ctx *octx, struct dentry *dentry, + long offset) +{ + void *ret; + + ret = xa_store(&octx->xa, offset, dentry, GFP_KERNEL); + if (xa_is_err(ret)) + return xa_err(ret); + offset_set(dentry, offset); + return 0; +} + /** * simple_offset_remove - Remove an entry to a directory's offset map * @octx: directory offset ctx to be updated @@ -351,6 +363,9 @@ int simple_offset_empty(struct dentry *dentry) * @new_dir: destination parent * @new_dentry: destination dentry * + * This API preserves the directory offset values. Caller provides + * appropriate serialization. + * * Returns zero on success. Otherwise a negative errno is returned and the * rename is rolled back. */ @@ -368,11 +383,11 @@ int simple_offset_rename_exchange(struct inode *old_dir, simple_offset_remove(old_ctx, old_dentry); simple_offset_remove(new_ctx, new_dentry); - ret = simple_offset_add(new_ctx, old_dentry); + ret = simple_offset_replace(new_ctx, old_dentry, new_index); if (ret) goto out_restore; - ret = simple_offset_add(old_ctx, new_dentry); + ret = simple_offset_replace(old_ctx, new_dentry, old_index); if (ret) { simple_offset_remove(new_ctx, old_dentry); goto out_restore; @@ -387,10 +402,8 @@ int simple_offset_rename_exchange(struct inode *old_dir, return 0; out_restore: - offset_set(old_dentry, old_index); - xa_store(&old_ctx->xa, old_index, old_dentry, GFP_KERNEL); - offset_set(new_dentry, new_index); - xa_store(&new_ctx->xa, new_index, new_dentry, GFP_KERNEL); + (void)simple_offset_replace(old_ctx, old_dentry, old_index); + (void)simple_offset_replace(new_ctx, new_dentry, new_index); return ret; } From patchwork Fri Jan 24 19:19:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949887 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 86FA1C0218B for ; Fri, 24 Jan 2025 19:20:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0208D28009A; Fri, 24 Jan 2025 14:20:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F1389280092; Fri, 24 Jan 2025 14:19:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD95D28009A; Fri, 24 Jan 2025 14:19:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C12B2280092 for ; Fri, 24 Jan 2025 14:19:59 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 6AA9B1409F4 for ; Fri, 24 Jan 2025 19:19:59 +0000 (UTC) X-FDA: 83043310518.24.3F11962 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf20.hostedemail.com (Postfix) with ESMTP id B20EC1C0017 for ; Fri, 24 Jan 2025 19:19:57 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Cv+scLBr; spf=pass (imf20.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=1737746397; 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=uIZgDGTjU47wL9R0w2LhiTH07zjHpzG6QL0BRwCgHJI=; b=FOFFN9hM1W8EbRRAcy9JGeuVDgGSHJk+8whv/vr5A6t/HFJjvKbCX8yAOmNT4gLncwdrWM yV6njPde/sYNjnDOE9MetxR/l3ecWNWOUqWyOHGtGnoeOlxbzLON0Iea484fiKJicYDSoo 0mCNAfkkrisAJ5a2uNBfJcqZRRSrhtE= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Cv+scLBr; spf=pass (imf20.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737746397; a=rsa-sha256; cv=none; b=5m3vigL629MP4NKU0gCDWMCaqsxkL9VY4qBxhWjptyD0WmB/S3FEK0gD+yfXJhFlMiAPjK /lnZpp0M2DRpD6JiQDfzLIpPcAMd8UI9DEH+N5Biv/l2PyU9VpBukUbiRfp6y5v8BsNsCO rIdFPUI1JkewUcZsKd62sAS86/qrT3Q= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id BEC93A41DB0; Fri, 24 Jan 2025 19:18:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E72CC4CEE5; Fri, 24 Jan 2025 19:19:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746396; bh=zxSHvTXBTLPfQg38mkpreNxvil2Vzc9/HC9eEuTBUzs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cv+scLBrqN+k9/W+qvAwD8sQxTVRLnaM5jdmvabrIKB/9GAQAsSBHaUyeMBWok4SX JcGAbXH636Br7W7QbTZB5O1npwacuax3JCvZ8HD3Vsh4diKEMf/zo0iJlzPaQ7oqh9 rqxvcBOrc9XobhsksfGqQcKnQ75rkDVAFFmIyOdRDHCoI89AUDP/rqWRBZOzm85UO3 hIHijzNeUhk8Aify8yQIke4Gix3/3m6hGBMUeWkWMPWFDnKen/vUt9M6xQxxyZcJv7 Bw1xfRP2jntdXYlZDRJz+8DZoUArKtMzWuXbnIGsDRfHnTu0+mKKj8meHKmCiqoKvk kaFysGbrahW4w== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever Subject: [RFC PATCH v6.6 05/10] libfs: Add simple_offset_rename() API Date: Fri, 24 Jan 2025 14:19:40 -0500 Message-ID: <20250124191946.22308-6-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: B20EC1C0017 X-Stat-Signature: bboseefbk55ciy8ibbap43wcgq7kg1ht X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737746397-915992 X-HE-Meta: U2FsdGVkX1/sqYerzVP4/lHRyNSJKpgDnnI2bygUZG0bbvZb4dLUoT2J5KLcC1hwi2BHg0UIIdkcl4ukpHZNmG9MkkimkgZP3D8l+Axm1hjtEMUQTp0EgVk/UJ2lwQf3UPt41V6Roqx73qJCrjtSUIiMdKIXxBSvtJUkDe16TmpvYQ86IU05Ti+RWh750QLSfFiOuVD5cFChyhtHi0S4SKETJyHnREnl6sjc5y2AUdInGEG2MRfwVc5Zj9NMFAj/KyNgs7voaA3xpNIbLR7L6Hq2TlilNmRbXI64x78vPoFm0taSabn37+AqBTqRdpuNaFmgbAwGWL7baP3HRzpdOWUm8q4WAD3Ojw8sDBmtfSUUxIrTmxoVt/NxZx3g9ZABTqNtBZnO1PuB5oMOHFiqc0KRitcAVCh9hbwmkika+pnVJcRx9+yCNNUU9Hg4WWdSdS890X5A7KtmpHwuETFxI7+r3RJeTHIuNE6k+hxSbvNq/f0xXoSLdvFsyv1jlbmIAVczkQFCQi/R1svO7d60jUVixmCa1ZshBX+VqtwrfNE8Tx1LYy4nHfB5qGuR/2G1L6sf8HfGgnYDeq8Ne3Nwwc/NvJYMGZDE09Yc7ualIodXwYyrOeb2di4pjU0ZFTPX/FANWxivBATMouJZRAM+ZWdeH01oSVqsyr8hQ3rPe0Fqy7SBFy2IvxnoHbz9gIYDFOP/WP1kbPSWhbfJHWt717MyuT3nbmAVLNRCum93mFpj6XdK+yLZO6U+x2E2y88xlx+wchnzM+N876z9A85MqAE+KudDzy0UN8nMBPkGecxMcRE9FpHfMnixSmn0d0rRMfNkJpHnKlvSFsOwYjf//GGs1c/PevWOVoCOfAd02nBkP2q6fLEtKeoDA/3neEmeISBaOsatC1YR6EzcbvMuUvW7ddsArJQE/GdJW6I5kw46xE45TmPePKKmRFHCCkN6YzQ65fVc+7HwEzsgqUE rMTLPsiJ PJ30VpX/1yexWU0gjGiX1UziB/lv6OMB/Ic+Lv53cZj5Igu/cxn4iLX98qusThUZIEiJ7VqugE9So209pdrlWr9YvLoRqfms9JU0qxUHMqbhVgHP4hLbzs4/AemndV6PG8hiVkLsMdoMSMykw5mm2JFbCCDG6lh/+uwYZrTVAHwNcAHCK9T5fQ3aYcJDgAu4xtUP6PKvNt61KynMfX91ym+3TvC/DejRII/PRWzm/eofpgX3loKPxWXkab+Vo+zO1YtuK0rWToC6cFOGX8Ai2TwzWEZh756qqAjK89LNeRXrxYrArAM5QBy9bzQ== 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 [ Upstream commit 5a1a25be995e1014abd01600479915683e356f5c ] I'm about to fix a tmpfs rename bug that requires the use of internal simple_offset helpers that are not available in mm/shmem.c Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/20240415152057.4605-3-cel@kernel.org Signed-off-by: Christian Brauner Signed-off-by: Chuck Lever --- fs/libfs.c | 21 +++++++++++++++++++++ include/linux/fs.h | 2 ++ mm/shmem.c | 3 +-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 2029cb6a0e15..15d8c3300dae 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -356,6 +356,27 @@ int simple_offset_empty(struct dentry *dentry) return ret; } +/** + * simple_offset_rename - handle directory offsets for rename + * @old_dir: parent directory of source entry + * @old_dentry: dentry of source entry + * @new_dir: parent_directory of destination entry + * @new_dentry: dentry of destination + * + * Caller provides appropriate serialization. + * + * Returns zero on success, a negative errno value on failure. + */ +int simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry, + struct inode *new_dir, struct dentry *new_dentry) +{ + struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir); + struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir); + + simple_offset_remove(old_ctx, old_dentry); + return simple_offset_add(new_ctx, old_dentry); +} + /** * simple_offset_rename_exchange - exchange rename with directory offsets * @old_dir: parent of dentry being moved diff --git a/include/linux/fs.h b/include/linux/fs.h index 5104405ce3e6..e4d139fcaad0 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3198,6 +3198,8 @@ 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, struct dentry *old_dentry, struct inode *new_dir, diff --git a/mm/shmem.c b/mm/shmem.c index aaf679976f3b..ab2b0e87b051 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3434,8 +3434,7 @@ static int shmem_rename2(struct mnt_idmap *idmap, return error; } - simple_offset_remove(shmem_get_offset_ctx(old_dir), old_dentry); - error = simple_offset_add(shmem_get_offset_ctx(new_dir), old_dentry); + error = simple_offset_rename(old_dir, old_dentry, new_dir, new_dentry); if (error) return error; From patchwork Fri Jan 24 19:19:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949888 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 02E79C0218B for ; Fri, 24 Jan 2025 19:20:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E2AD28009B; Fri, 24 Jan 2025 14:20:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 06CCD280092; Fri, 24 Jan 2025 14:20:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E764328009B; Fri, 24 Jan 2025 14:20:00 -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 C998A280092 for ; Fri, 24 Jan 2025 14:20:00 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8906EA0CFE for ; Fri, 24 Jan 2025 19:20:00 +0000 (UTC) X-FDA: 83043310560.28.AA345DF Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id F36D3160003 for ; Fri, 24 Jan 2025 19:19:58 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JmvAjPom; 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=1737746399; 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=+B4fdgo4GC1VJ3upIVmh+khf1AOGUc+90aykYpHwOAw=; b=DtmfofFLUPCq7ymDNQdVs/fE2VP5cwWtCXfA6c29jaggoM5lInRxc9biEvc0Rp+hGMVl6n zENKo9BWowxkgTsqBdsdEjVc1FjMn2J0J16XiexTzCv1mdPf/VHhwXzXpuy3x98GVLxRSt fld4Z49WhNbUwPmMGACkILjFgyTxzKQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JmvAjPom; 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=1737746399; a=rsa-sha256; cv=none; b=6MpCjyrE6VIH7TGeY09UnhkLHxXYm94t3kxzWb4JWclECYYE3Gqaa3Y4g4AphPL0OoVo0S yyq2v0EiL95INUwpWpE2F8sqLVq2yArPFFL6VSrgpnnxxmt9xaDaJitH9u8LX3K62IR7T7 BhUEx6G16EcNeOa/Qg2um4o3qN3mgHA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 0C7E35C61AB; Fri, 24 Jan 2025 19:19:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB29EC4CEE6; Fri, 24 Jan 2025 19:19:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746398; bh=PSeXT5OPmZ3CLHOWnsn06fMWMBlx3JfvOW356lmNS5Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JmvAjPomIA5ROix25gQqohqFQGadayGDEb/GVgwrMLYV3LfblJ1SQbdIFz86sl0xC XL/9Ei90N5aEa39nCIzzxj/gvmPgeB8HdFiaQJKSnRrkjt3FG4yrd2FEi/POCCrLx3 C5YgCEt9JpNtn6eTApz2lQKkl9g96Ff7iOmm4/1EW+fZKPQW3M1s7mvf9dbyk1TTsR bbmFDhNn0JK1sPZqpnwHe5IahIHFxnYxbO+mBOIaOO/SNFhdmkoWGKeTdEqdEmDN1G oh88dnCbSO0Dcnnxx+arF0++YHwKEVvPPOk/3mjyyu17Uv6ygJ1Qpv5H5BvT03Pcjy tw9+K+hGvT0jw== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever Subject: [RFC PATCH v6.6 06/10] shmem: Fix shmem_rename2() Date: Fri, 24 Jan 2025 14:19:41 -0500 Message-ID: <20250124191946.22308-7-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: F36D3160003 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 8b8m1bo6jffyzm3fi3ztcth8p6eda675 X-HE-Tag: 1737746398-681546 X-HE-Meta: U2FsdGVkX1/j7+UbE/jCet1e55s5D/elph1UY6nGmGdzvUWO6PGTwTHISQI5OtNi0+U1bYZAO+8F2ZMVqpmQuRGR6v/sQt/1H3MjZ+zkpSaoRCGso30grxlyWCSn6Djl+Fn/7sZovEtMxLSCzPK3fH79y0bq4KdVO1dF2WLr69OuKjlILGDCfzwoppGTnaaTT2A3EK/5PC1gFXGJTa3uq2hcZmw+NR2HM9wA4ddyCIu578jI784vfQPErUe7rL3W01PyB2q7D5l2OI39VuNP69WaOheKY7ry1xPB7WVSc2mB+Yf46SOIWcVxR6nahmKtzS4lMz0FihiaUjfS6c8Kg5q1S2aU/5vtWn1vFponEuEF4yehBSZoBONR8RG6ruHlTASCsc57T8F2mslI5hJ72PyQkGs1w+WkVY4k7AoFc+ZHcGMVdygj22t71YteXJs0u7h4NH9jMrsur57FQJf1FKctJR0VTrhguV1wZKoDkITXQjXE0W/vsWqKzP/TXheVuUE2VjIqyFBtlDeYXA5LuquI9bK2CvVp2vprPvTpKfNgQhuByh4Wj+Gue5ztj65LUHcmeEFcaiq9XpN7fF5fZnzcFHs4JbXmNK5A7ZOEJbLPFArrmgq8AJtiEWfsewg7P1PPfnd0U5NAtU7Z5CweVk5mAYJ1MpkHw6Zs19hLJSYljP73hPR5YFQSWlG7ZLHHfZKHEwNq0cJXLEjBNNbVofFYnp1Zl5E6zMriSG+e0PmQF4vgAFHoQScGiBRhhwKQg8Z4HdoPHnpt3+sFihqp4PApoq+MPwYXCT3OBaf+N360iq9+Zib9EVL/iKd6xcR9ydw9QsYCG2FJkynz07TryPI2B2y8ZDIrCIUmA1tPWAlYjH/PX56eC6htKt04DuEZhu81zY8owJX1gjx20/+HmjvUTlk7xoAjtYJ7J8qULTTW4FMZ9AftjzFEZj+1Nw7eAD03b9UoqGGaBbKRKHx 6tYjHn68 OFyZBeW6c0gJwtBxRIIsjz5S8lkE+faf+JMgFRfibeT4sIo+ZaZYZ5k6DPYg9BA9TXJfOTEPTcOsZ2IP6R5Zpqgt2MHykFbDqx4/lFDk2m/rxnWn3RqZ7LsXxDcdWrPJBrk1IL2pV7f7XmNcAFTXlOgd+DD2fNbWLcSb3HPMejJOKH5WxR8/u9ba4ojAoSr3SjX+LkAZLTUWNY3fFh2pC8xeG1x4AzGDwrlog632q7w51WhXa4DpeCKmBjlrbrXL13xeqz4mT7IV0HqtnSLdAWfKsRaupzopzBPnuimEszWgirCfbaWDHVJEIpjPaOrOfB3dkxutLLdIBbEGmC4RvZZyzYIROrhuGSYve7vcC/fmriM8= 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 [ Upstream commit ad191eb6d6942bb835a0b20b647f7c53c1d99ca4 ] When renaming onto an existing directory entry, user space expects the replacement entry to have the same directory offset as the original one. Link: https://gitlab.alpinelinux.org/alpine/aports/-/issues/15966 Fixes: a2e459555c5f ("shmem: stable directory offsets") Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/20240415152057.4605-4-cel@kernel.org Signed-off-by: Christian Brauner Signed-off-by: Chuck Lever --- fs/libfs.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index 15d8c3300dae..ab9fc182fd22 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -365,6 +365,9 @@ int simple_offset_empty(struct dentry *dentry) * * Caller provides appropriate serialization. * + * User space expects the directory offset value of the replaced + * (new) directory entry to be unchanged after a rename. + * * Returns zero on success, a negative errno value on failure. */ int simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry, @@ -372,8 +375,14 @@ int simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry, { struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir); struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir); + long new_offset = dentry2offset(new_dentry); simple_offset_remove(old_ctx, old_dentry); + + if (new_offset) { + offset_set(new_dentry, 0); + return simple_offset_replace(new_ctx, old_dentry, new_offset); + } return simple_offset_add(new_ctx, old_dentry); } From patchwork Fri Jan 24 19:19:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949889 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 C76FCC02181 for ; Fri, 24 Jan 2025 19:20:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8382F280092; Fri, 24 Jan 2025 14:20:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E702280065; Fri, 24 Jan 2025 14:20:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 688DE280092; Fri, 24 Jan 2025 14:20:02 -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 3C9A2280065 for ; Fri, 24 Jan 2025 14:20:02 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EDC9B1C8FDD for ; Fri, 24 Jan 2025 19:20:01 +0000 (UTC) X-FDA: 83043310602.05.86EF474 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 56FA34000B for ; Fri, 24 Jan 2025 19:20:00 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BUQULhBP; spf=pass (imf04.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=1737746400; 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=V9QQpk3QN8dO41maw5Dm8uhOKmRJsyT1Ghm9Hi512Bo=; b=kbwu4fGNEVPTQZfNoDCdeTv/HR+/khfP2A+RR13azpHMITHvrxNcvidmv0SYpSm+cppSlj 9nFA9wPiipT5FrLfrYa9bA59UcVeWvtx8HfEe0NjCSIkOC1B1bkuJzU5lnA3yBdK/FFKlL /NjC9k5qplz25PhH1/ijxkTI+gWOIyw= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BUQULhBP; spf=pass (imf04.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=1737746400; a=rsa-sha256; cv=none; b=no5tf8tlGVhJ/87F79dhs++X4AgN1IPnLaimI5Sj+3o4Oyk+pGh55NWWP9piWLIbWHOLpF er6cjdfuxWXKNlMd+SBKaiqRJbmkRdjJ6Ot6X+aqicdOjvfnM6wIpm97EFsTlA8ju5SEE4 zqgiQDSqyoX9ZamudoECVQMCbRVc+5U= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 68C335C6192; Fri, 24 Jan 2025 19:19:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 423B6C4CEE0; Fri, 24 Jan 2025 19:19:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746399; bh=+MlNwIFBadfJD8EMoOx2TqTDKMIrB7l68m+npdFSw20=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BUQULhBPqFOT/WhqvnY3sV0lAtBbbNP/wiyOPU3EEo9WSLCOAzMoLbGTftvUmLQ2Z olyO1ZMSyx1YOFhYOC2eOXp8d4oFAVYf78UT4oPTKwI/6olDxREnW2Xb2BNyMngNS1 8O13HK60trrGWcOzqhAv8yz5htOozX5ENWUczZaN1d8xzR55sFG0CBUFzI5gV+Vl6S 0rY/vX/heKu7yIJcmj9kWXV30qy7N1/tNV5cz4bRRp0MxxNWVxETCnW/R55Ob7TJsS 2Fij7yqZtieFohKbzUMBN6EpvjRpg75WykhEzrNDMUYir8bWF++XqIZIiacoikLoEO 8e8Xq3gMXpJoA== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever , Jeff Layton Subject: [RFC PATCH v6.6 07/10] libfs: Return ENOSPC when the directory offset range is exhausted Date: Fri, 24 Jan 2025 14:19:42 -0500 Message-ID: <20250124191946.22308-8-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 56FA34000B X-Stat-Signature: zyprird3p3nn4tif7bq9ea1ck3x49zex X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737746400-331203 X-HE-Meta: U2FsdGVkX182t1ru2Aj0TXMp29T13yjA/zunvw2ovKPkXZyaIBmH6iIbSj+JJw+QxbrGNtbYLwJSFhVRT6G5KdI8l6jOL7dGJvaiYFhdPBd9k0sd+5aZEVThyVtVj4HS3briMx2RyvqaEjB5UhAmetJsjqo61fzQVKxHNMZjMEYJggeDLaOZchbs3o/0yknTRXnWIp4yRWbFADPCVth/lHmyYpk84kG/f7sZkkmMVpFbdsuN/11Icn9loO2OAaue2pxGmhFfPvSX9ot7JIcRqSKvjjv2uSkK4reCayFiCiNxXLTfAfRyZazzJvhwJZeRVw4cmZ0dzzyVkO/eGQMJXD6/L83rsXJcMhthGofOOkRdKtQdgBkP4xTQ8VpccoKTO5RhWjHiHABsDal3ULmFbkvBSk7EFs08n7gT45eY/pI/3F1A5h0fhZQA5ckTtOAzGq98fp2TnKjxeL+dJY8odm8fpYC7ou2BwkTTKhLlYvtDYJCWSPFhmCgpcJyRFwHp7YlmJQrSR5zhV8rVbBGACWbc1n8HQoIxJY8Zrkb2QxWhhH7kLyZZrlGu8KBC6fb05mw7EK3oaFuYn1tFaEx05e/2WwPWC35eFSxftbYvqd5XyK8DqHBaO0xBNuDvVoxbc45ERQ24VaL/moAuzgVcncXOJJFESUgS6h6H1FsJW91ja+cfR8tbq1Oth6YWHFXusklPukvwsj+0XMOy8DAa5ORTyTgJvMdL7BALeASfQRcwMgJvSsyGwE2OFDz9s9J0RwGzwYstZIENurLB1M4w5KCfLqS14j4AS92S9qQGJDVlNIlHen0JXb7/JekHROnCQo3Bhpi9nQYU98pvosykQC3Oa2YUBK6xlPsGH8DY5iSKMy6/eJOtElcPG60LVtkUolPA89+4AxtKbLIlwN2uoG0BvKu83+7aI2bJJeAcGzqrauf+zg1/jhmhvfeddFvExjpayVAgzMMYtu3VShD VyvDuYmq oQ4NLSQg6/sm6zDuTMFGTPjpmxklXp2gDBNe6Y46ZEDpq7pthL1UFAB6JrTscw7ebkVZERtsgR4zgbPj/8Ah5Gyt8rORS07UC3MTW3j5G0iYd79kzfeRUXN70dnHaG+Ij5qWslg9lGbObrMVlBWPYYju+4E12PKQCyeppUoi9ByBik4u+WnUONCEJep4mwI70matGCQql1MYlTGqUgFFUDWoHYaSj3WJ9ovPiprwLWIsH88yJfRY0H/GKN7Sfpp5SwRy2TCxAWOvfs96cF6QU/huFTA== 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 [ Upstream commit 903dc9c43a155e0893280c7472d4a9a3a83d75a6 ] 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: stable@vger.kernel.org # v6.9+ Reviewed-by: Jeff Layton Reviewed-by: Yang Erkun Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/20241228175522.1854234-2-cel@kernel.org Signed-off-by: Christian Brauner [ cel: adjusted to apply to origin/linux-6.6.y ] 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 ab9fc182fd22..200bcfc2ac34 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -287,8 +287,8 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) ret = xa_alloc_cyclic(&octx->xa, &offset, dentry, limit, &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 Jan 24 19:19:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949890 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 08D29C02181 for ; Fri, 24 Jan 2025 19:20:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E29EE28009C; Fri, 24 Jan 2025 14:20:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DD9A4280065; Fri, 24 Jan 2025 14:20:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDD5528009C; Fri, 24 Jan 2025 14:20:03 -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 9F3F7280065 for ; Fri, 24 Jan 2025 14:20:03 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4E5CF141739 for ; Fri, 24 Jan 2025 19:20:03 +0000 (UTC) X-FDA: 83043310686.12.3E4CF7D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf14.hostedemail.com (Postfix) with ESMTP id A85EC10000E for ; Fri, 24 Jan 2025 19:20:01 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Q306l4ws; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf14.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737746401; a=rsa-sha256; cv=none; b=mKr2samOqISzG6Fvgn/Kjwhs7FgsTnTB8LAIINj4b3stpMW8BvKS9QqaXS5Tknt9ZIFg0D kO7U4EiciR5FlpNgrwfbtQJxw0dKuSClxsiXI5gn2LQkovvEuDhRAn/66RaqJEFpzhfrlL 5SuP+x4zoiRKR8OOZxEqBmhZrsudu4I= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Q306l4ws; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf14.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=1737746401; 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=BWSo0jNo15W5sZXDUrOELyAn1gO7K0iZELRReJKqbnU=; b=u40YynCtAS4f7fmlyaZc3Y/axPEOW1oyv9/SGJoFrDss0N/yHqbVZBv2ZNPnrE4erdq5pY YFZphiFwxmvXOWwn5ygPDhxr6YAeynauqDWwix/dPCt2Np2M6UiZDStjQX0sfRiLOW0qcB eJJUf2ylwGywgU7xeMTSV8brBZL8RvA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id B2B905C6195; Fri, 24 Jan 2025 19:19:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1325C4CEE3; Fri, 24 Jan 2025 19:19:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746400; bh=7i8ieMEYnSbXE7wG5048yyfg5P+RPVEiCcn7OJpp8XM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q306l4wsGt82CKQzJvzhCo3HcT3FOVHMDa4ucpXOi/awls8OhBeRzHF/cvjCiN/es TpjuBhJs8QKINqg3tPofapF/U7S59qv64UiRwDNps4Rn+6oEbcAONf+scmmvYG4mNe nEfrbvG9XNRWRWNLjoXHoBtXHpu3kAhE8RFN1bOacH8yxGWMfvwNggTrD4lUdBPv7R k31E1KBrrisj3Vo0UfhlGhwQ+3t7EgN9Z674QXx6Ss2S9DM6gYboYkLaS+pHszU2ih mNVa3Qn5iyQh7toBcbBlE15mLKn9yoHQ0DU2Z4aNaTrgOz0Ur0D6WmjFylrTUOtK9n 4BS4b24PehbQQ== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever Subject: [RFC PATCH v6.6 08/10] Revert "libfs: Add simple_offset_empty()" Date: Fri, 24 Jan 2025 14:19:43 -0500 Message-ID: <20250124191946.22308-9-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: A85EC10000E X-Stat-Signature: w3iirxfsdgfpahe48b7sgng5t34zm9gc X-HE-Tag: 1737746401-3359 X-HE-Meta: U2FsdGVkX19YU8toJPmr7MvqeDOrhxzSymMWoW8Iw5MWJFssI9ytQJWT8b0gAK5oXwdbw6f60NRfMNYsaVA7RxQUA0JEGfWrIlKOP4zBrphb0dfEI9DjrDqFacskufZ63r0Bw6pdveLrX0woqoLY2UNhI9CmLsM6YdDxulf1TyZw/KEKMrxabx1Hf9Pa1OGLtnNWflIRt0GOXS1zX0aYP5OMxuNX65Tgr5tJNUSNSFwbKx5hPp6S5FTEBqNmfTRZFabWku+oPwYuAKWclVyQp3o3omy5fqBcDuDPemeZCXAvbn/dptDjPQ9fLGuyxzsDqcx46fHilEOS8IzA9E0+cFEmkP1lqxmHiU2CG2SQe5eqsRtVu2w4QlY9DJO1Payp/Am5SlwF7d4QqKpqZAUF1udo857DuqGuAzRvt8gi6eRQdPm3H7tH8Mx3rhwOBzY+HF76gIsKjnbLtv9nemjEKRriPM9eDgEIaqRHFWKpN5Dx4LbJoD5NwSuhBLJxtn0ZuSoEqk5puYVdLRAcwX4PtFDxnF8Tdzfw0mrmciLdpOFrPop774i0Bj2lAtUG5f2Eem47pr1eLjkBHnx++MmezbSIZywwFP71boHvzhEmqJ78wIB1dFpIT1OSDWE5sr/wc/ZP8Sq4KesPHrD8lL62RghyJrVbgh15lMuhXfQChNtdZwaHse8N4rLg5Eg3iIxKym/HKSKuZpFBlsbxwjwWH0E+Cx+PyNKRREp5/rKpprff9Ea/RIC6rQEJYoQARvBvwkX7xGMyvTKq6XMRRhQ500rEt8c+lXVeeSJcD5/HG4FUS3JizJtPVwkp79DB2KzIUgvsBwDYI/rHGdwey1H4DhmBmd3epMj4SsY6mpzRjA6Fh/J7om2TIL4ifnzk9LSR0lltiavam6RE/XGOtM1pTQTxwtiNnvvOSTtf5roUqYct1ucAVOcnvPdU4hvao0Wpz21w9UBehx2HoWlciY2 ngyMTiVj 0N5B7Ziyw9ODponJ7baasNq4y40ZvbtOFgXwIHonpPdtVGzgppj4dXNp3AnjlAPQwk+WhKN92vF9Dcf/NopD2ktNGHQy3AJPxq4wm4Y+IsEcX/gNlvfh6WG1bZ8E9Cl1B76m1dB6kiodStt2XPQNFDenhAzYHPYuG/uW7965Kdu7l5Vs/niPnnpvMU7/ptoMze2z09M/1pnehQClHonpbxeQXgLgE2TDcIiPrLAJz/2sgO3eGClIOFhiHW2Zv3sE/VfX69B/mn1g3p6v1V6GBVaNiLA4YDdSC8L82D3kR19ANnKkJWD473MMVfF8a1leXQgpfx/M40/Smz98= 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 [ Upstream commit d7bde4f27ceef3dc6d72010a20d4da23db835a32 ] 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 Link: https://lore.kernel.org/r/20241228175522.1854234-3-cel@kernel.org Reviewed-by: Yang Erkun Signed-off-by: Christian Brauner [ cel: adjusted to apply to origin/linux-6.6.y ] 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 200bcfc2ac34..082bacf0b9e6 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -324,38 +324,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); - xa_for_each(&octx->xa, index, child) { - 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 e4d139fcaad0..e47596d354ff 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3197,7 +3197,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 ab2b0e87b051..283fb62084d4 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3368,7 +3368,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)); @@ -3425,7 +3425,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 Jan 24 19:19:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949891 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 24E61C02181 for ; Fri, 24 Jan 2025 19:20:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6126E28009D; Fri, 24 Jan 2025 14:20:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C27D280065; Fri, 24 Jan 2025 14:20:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B28F28009D; Fri, 24 Jan 2025 14:20:05 -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 31099280065 for ; Fri, 24 Jan 2025 14:20:05 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A7F98141108 for ; Fri, 24 Jan 2025 19:20:04 +0000 (UTC) X-FDA: 83043310728.19.0A28009 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf07.hostedemail.com (Postfix) with ESMTP id 1699A4000D for ; Fri, 24 Jan 2025 19:20:02 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IeyqBdZ1; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737746403; a=rsa-sha256; cv=none; b=nXUfQMygUcSEPHdpHZqo15pgx5nbgXXYQLl8PGB3QLKf+imYAsaNTIbZLdN2fgoUi22ERK 4JJj4xPTO5gvtH6ByELB+a4eL9jJoKV5prne6fpScyD4v0A7mXQTu9ks95KVlieien3BqC 0OsA2e02tNypwCx2L1V+/chRrOY45Y8= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IeyqBdZ1; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf07.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=1737746403; 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=IUTAPAgL12HT6rHZCBTh/gWbIMeLALpJL0mRgXI7pmk=; b=vgb0I+KRQUG25T5MJ73ncowIMzbcTjtCKsVjtd7Ly7/8h4oLjunjT4arv6q0PvAOcFHsEa iASnc52XbOKW0oYKJjtoHwNP+VwtO3twn851JLFkxk3KfY3xaYOzpMwIshcsBnkZvXDI+D zqOa1QmiDca+ijHCochlPAzVAcWciFA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id E44C25C6193; Fri, 24 Jan 2025 19:19:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E97BAC4CEE6; Fri, 24 Jan 2025 19:20:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746402; bh=gDiEr1u2XuRYm0Qux04y3g9m3w7wietr1o69aqd9elQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IeyqBdZ1u/OlXH/wzcuLOlGVvF6NtUyseSXU0gU7bu1wtCR98kWWfpPic68HjQu0N R9/NVgfv3AYfFQFs+NDWK4cQfm3LX5fgVanT1ozNYY9ltKdlMIt5kr8EOelN++/LKh Mfn5o9G6FMfVW7uwXPwYI6Ax73bna2tYyvhQ+JpPgJDx7rrOZ1dIz+doaecS1E5QVJ TrbxK+U9noFDj2kwPku8Prdn6hazAkfN+E8IIfIvcr1MRYza51RDIyg5TanCOG9b2y LiXpUuhVyyZNC4Kk2qYVI70rcbQ2ZjXItQSsHWrzeweJJ83UzNa8WJs3VdAwOfLr+X 3X9WtcPAHgFvw== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever Subject: [RFC PATCH v6.6 09/10] libfs: Replace simple_offset end-of-directory detection Date: Fri, 24 Jan 2025 14:19:44 -0500 Message-ID: <20250124191946.22308-10-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 1699A4000D X-Rspamd-Server: rspam10 X-Stat-Signature: dq6b57ac8d196b9expwyy51mm1isshxf X-HE-Tag: 1737746402-909766 X-HE-Meta: U2FsdGVkX1/kLegROdThsGdK/U2Qc6oJOLi/jNPjkt9hm+sZ0g6RJsbIMsaU9ggil+oKoNDY8rxmb8E8zxr5r7CcTt88wuJ2XcWEHy1DE0xxE19RkZYB132MWkSgOJ5aKC8rrEN0XWrsDLZSJyPIDH2B1214H7vRpP7V6hhM3RTOXMZnAU+OKajUyeSUSET/w+yxDbGJ8xYnVEtBj2Lw4ZGV+Vi3fkVvoRnz+lZIFGszn0ACxkfG55NjiMmMhvN8pcp4XlRScswa56LdyAIO1/jJIv46eXHUr2vb496jCQE+EqM3R/UM6zh9KaSMNU0DhzudTf38FQ7gqvb9GzR7ppwC6WNkSJOQc4lXtDFIgptdqE8FG6iX8S/lcmJMbHlfwd8sEl2avFXBnp8aFUUtKWC2rXY1c6gJqSo8/fT/A6ms1DyOEhYYO5k+kiyTV0jL+AUDwuqYgkSP8cj2pVGjKMSaDIeKUJgFlDv36ARyLaM/CTFLwFx9dzNWZMnmOfvZluJJWCAaBU7ocR6eR9PEK2qggOiwdxuW1qTOmrZSa5IqDq6gFAFiVymBMZRYlvgBbKb48nIu043VXiUjXTfX6CwsA1/eZ0G7S1/FrcBiUEeg+rE2iwCFd65pzdFGXH6kgriYVTrV/dMbEcWEh2BUimVBnkOwAgUsxuHTpuUnoV2dhtOqE0liQkteV6YzpVKCqPmTP6n/rEQ6Buk0CxYmiHGP67h2uz/JARaTLWk6fF++mWfZNf6h0SmMJs7RU5mQb2eg6OjsRbGYmiDfaMr4bYWh7kMbiz3MjGiGJJEjuqUgtU/EFEX3e/abv0P8daRCCz6HWWV2czpwCV3q91JYhBXI2K6TvD2eBK0qIhep1gTUdYLMCRoff8mDhE53ZSTuIg2QNS/OIjSY4WYOFIGBbFPgNKIiENdB36HNBoozGMOBnEXwIpNMRcQgzvn1LD74nTEnSmVgYmkPWaa+xda VrmlhyWi v0w4jtKHY8PyQQa4FD5qvT1UuCxzOXEFKDrsCYADyHMJMaovjuSNhdwj6AMiXsE3MKPohPjEtWbARGTYZ4Xiwn9MX4xVcOS2jBf0uJe79k6Ij7cIZI8Fb0eiO+k/brleNjjmO9Rs0eDtAUQ8B6f9H9AdyzZylWhvartl/C2VDFNcZD1ZM3rKhc+05g/ySjoLht2WcG1pu0O6n87ie5FZj3CHPX1miYnyTJvgtcqffnuijtwzbjwjbuamDNwf0utw53hOVSZ/qBGMNzORPImjsxaKh3QrZwFf5wtNAlngspiOIF+/n4DOCqsL5rw== 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 [ Upstream commit 68a3a65003145644efcbb651e91db249ccd96281 ] 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 valid 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). For broad compatibility with 32-bit user space, the largest tmpfs directory cookie value is now S32_MAX. Fixes: 796432efab1e ("libfs: getdents() should return 0 after reaching EOD") Signed-off-by: Chuck Lever Link: https://lore.kernel.org/r/20241228175522.1854234-5-cel@kernel.org Signed-off-by: Christian Brauner [ cel: adjusted to apply to origin/linux-6.6.y ] Signed-off-by: Chuck Lever --- fs/libfs.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 082bacf0b9e6..d546f3f0c036 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -239,9 +239,15 @@ 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() never assigns these to a dentry */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_EOD = S32_MAX, +}; + +/* simple_offset_add() allocation range */ +enum { + DIR_OFFSET_MIN = 2, + DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1, }; static void offset_set(struct dentry *dentry, u32 offset) @@ -278,7 +284,8 @@ void simple_offset_init(struct offset_ctx *octx) */ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) { - static const struct xa_limit limit = XA_LIMIT(DIR_OFFSET_MIN, U32_MAX); + static const struct xa_limit limit = XA_LIMIT(DIR_OFFSET_MIN, + DIR_OFFSET_MAX); u32 offset; int ret; @@ -442,8 +449,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, U32_MAX); } @@ -453,7 +458,7 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) XA_STATE(xas, &octx->xa, offset); rcu_read_lock(); - child = xas_next_entry(&xas, U32_MAX); + child = xas_next_entry(&xas, DIR_OFFSET_MAX); if (!child) goto out; spin_lock(&child->d_lock); @@ -474,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) +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; @@ -482,7 +487,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); @@ -492,7 +497,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; } /** @@ -512,6 +520,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 @@ -524,13 +534,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 Jan 24 19:19:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949892 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 BB5D3C02181 for ; Fri, 24 Jan 2025 19:20:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1820928009E; Fri, 24 Jan 2025 14:20:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1337A280065; Fri, 24 Jan 2025 14:20:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E0C5228009E; Fri, 24 Jan 2025 14:20:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BB86F280065 for ; Fri, 24 Jan 2025 14:20:05 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 79965A025B for ; Fri, 24 Jan 2025 19:20:05 +0000 (UTC) X-FDA: 83043310770.19.9F145F9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf14.hostedemail.com (Postfix) with ESMTP id C1E08100003 for ; Fri, 24 Jan 2025 19:20:03 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EhNPcXtL; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf14.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737746403; a=rsa-sha256; cv=none; b=u3WhzS+Ph23PdYxd6fQ2bb6XElSKZBJKSlnITdDmOUVNH4vEqOOpf4/3DTKe3P5cPbovyk ENiiF+dCcqBA60j3Hm5qEtnyP1O3qoAgHl0NTAyG+1SwqIOV0e/V135/adMhQRPw03cxfn er00RQrA0cepatr2WTamYHLreNlgPsM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EhNPcXtL; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf14.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=1737746403; 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=HL8jq6xGsEtakPXyocxH25rmGYQuvzmVpTHInEFkB/k=; b=ah31fkHB+PfM6NlUF7CkY4Z5WbS9t8kZgsW0nnf9VpkxBYrexHJ+5iy1vUjlWohHsQITXe /rtniIEcog//CDrjRk3ZtWtVhIQeH9ihUE0Y6fyhhfxfph+FXp0ssezaMgU2gEZG6JCRFB p8OFxsM9z8NtFuFUHudAvG2sjbnSIuo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 3A2A65C6198; Fri, 24 Jan 2025 19:19:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 432B7C4CEE4; Fri, 24 Jan 2025 19:20:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746403; bh=pXmfTZBQd8c0darSl23XS7KeuERbHI7o2nY6VDQrO7E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EhNPcXtLJspy0nprHTzSerMolchY0HHaEn7sAVhiyh/ixaJi7T7DX3CuJDHV/4/qQ 8RJKbgdDrLBvcrRI+iFITTG3+SZ6Mp4iLhxmMukjRcKd6Im+fmJisZe7tW29CqJ72P JOqeObPF/+FSbxtrEfNt+pxdkf0wW0kFqqM4CoIZkGcCWlfi406hpCLKIYc1AUMGGQ k5h1VvyWHg9z2bxpd/wf4LOUjfG+2jRK/TixlVua0IYfh+XuVmLHbWciTJdkmHgMlj vOY16NanQBHZURS9Mab22beJeXw8hMgxGlsJ/xNHZD2bsGju3Kc/gKITapmHe9NWNc lyjyFjQDNcbtA== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever Subject: [RFC PATCH v6.6 10/10] libfs: Use d_children list to iterate simple_offset directories Date: Fri, 24 Jan 2025 14:19:45 -0500 Message-ID: <20250124191946.22308-11-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: C1E08100003 X-Stat-Signature: 4bkik67msqqy7u9ba9dyi8p6tqrid6tw X-HE-Tag: 1737746403-698816 X-HE-Meta: U2FsdGVkX19XtIFrgCHvF8o4cl9xZCLD4doJOfvkQZMEk42troEmXDr+ETjAoXsCkxHALeNdObPwitUQvjzawTHLjxu5eSvLMBJpj1urfizYA+tNW6QEP0B79UJAl97uky5EAMqedUyBWan4ANrbR+13j+nEK/2co9vWYpth2vRO8bDJRw+4Abklkr7ebPbCmsFJq/4NVVVk8+UGzP/KuZrNWzMjQR1zQzaG+KTJi2E5gTI1Gs8Nb4ZcYel6TGzsXFOiiCvQWQuPQA8kIpj7avfU6VekmIzZwWhjfrpqIGYBJyD2yQII+2v/en2lhell95LqLuwj3T+/KRF5WcygDtBHl+XPgFEYSfmtIgGDZ/jhIB5NvoywzJbG8s6XYdwAtDPuc+pHS0fDmjY2fnN8l8/xwT/QQdravKvDS6dL47kkzX5q/QbSju1fSoYaQx6IGyxPTObwB8O2R77VytFufFAN1l7tKgQ1vVcziiBUgH9drMTPNyb1TDg41I0vfL5VgusWK33D7pEEoQJfIlVlC2BFsvjJ0ugvsmLVxA0hKjT8YJrdmFrrdJseQcnwcHUJ0uj0CE877xYLuhKOstw/bi73MWoUQUK/mKQTE0z7Dis4iPlDvTO1f6yp7jaMzJWY5hZcd7ea7xYK8vWNi7ISySj2x2AhLXGMAkDr0sOdrotcZLW/zPOCfgONUJxcQWyDT8eYQcH65cGFzx/mZqiv4hKfXUZ7ddIoD60BjUf6IR1z+cTixIfWDH331SgvJ0qyBSHm0Vqu9j3Q+xdG4U9DZJrEZdtVcW+gw+6oQCXY/hdrTWv/OSqcMXXPsPbVOabgE1yXccX598wRncDRNcaQcFSgmEpN+alDtUB5D44OBbCcMnsSMroLcCCyTBZFN6wt4JNplbX6U3NjHeOzYdNGWtDrUYSl89LF9Gvvhnq5e+2IklcpS9Io/4Mgt3Z7gLCdFLpBpub1E6ppPhaqBst 7ZaH95tH vMWyCFSJzagP3Li8GVoHff2PSGE4Ig938UBhaeXe2KmY9j5mg/ihHJT/xGLLPgrYJB+597oQgAZ0TZYYJ4nlSSIPeAEmK0GlnhlD5dkIj8rkCztF5Lp9mA41f/N0BxPQCFI9FFkz8bag2lrCNH79CuY5AzmAMcYmahcvwB7rlFZFtxEacpeV1Fe5VKLil3z5G3lwLYIN5p2LEU21PxfP0JsL86/pheWzdiY3U7uX5nLyCSj2w4wKq1Dgcn7WcM9Akb7x9bSavDuIJDmz+bMVfo6Q73tIq1aYb9m07UnK/vIBFQFJtcnK/MIO0TQ== 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 [ Upstream commit b9b588f22a0c049a14885399e27625635ae6ef91 ] 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 Link: https://lore.kernel.org/r/20241228175522.1854234-6-cel@kernel.org Signed-off-by: Christian Brauner [ cel: adjusted to apply to origin/linux-6.6.y ] Signed-off-by: Chuck Lever --- fs/libfs.c | 84 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 25 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index d546f3f0c036..f5566964aa7d 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -241,12 +241,13 @@ EXPORT_SYMBOL(simple_dir_inode_operations); /* simple_offset_add() never assigns these to a dentry */ enum { + DIR_OFFSET_FIRST = 2, /* Find first real entry */ DIR_OFFSET_EOD = S32_MAX, }; /* simple_offset_add() allocation range */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_MIN = DIR_OFFSET_FIRST + 1, DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1, }; @@ -452,51 +453,84 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, U32_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) { + struct dentry *found = NULL; + + spin_lock(&parent->d_lock); + if (next) + dentry = list_next_entry(dentry, d_child); + else if (!dentry) + dentry = list_first_entry_or_null(&parent->d_subdirs, + struct dentry, d_child); + for (; dentry && !list_entry_is_head(dentry, &parent->d_subdirs, d_child); + dentry = list_next_entry(dentry, d_child)) { + 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; + XA_STATE(xas, &octx->xa, offset); - rcu_read_lock(); - child = xas_next_entry(&xas, 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(); + if (offset == DIR_OFFSET_FIRST) + found = find_positive_dentry(parent, NULL, false); + else { + rcu_read_lock(); + child = xas_next_entry(&xas, 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) { - u32 offset = dentry2offset(dentry); struct inode *inode = d_inode(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: @@ -535,7 +569,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; }