From patchwork Sun Dec 15 18:58: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: 13908872 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 AEDC9E77183 for ; Sun, 15 Dec 2024 18:58:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BDB96B0083; Sun, 15 Dec 2024 13:58:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 71EBC6B0085; Sun, 15 Dec 2024 13:58:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 570836B0088; Sun, 15 Dec 2024 13:58:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3A8FE6B0083 for ; Sun, 15 Dec 2024 13:58:24 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D0BA5B2CBF for ; Sun, 15 Dec 2024 18:58:23 +0000 (UTC) X-FDA: 82898103792.28.2D44D69 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf21.hostedemail.com (Postfix) with ESMTP id 1A0521C0002 for ; Sun, 15 Dec 2024 18:57:27 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dABbN62e; spf=pass (imf21.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=1734289083; 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=NfGfjIQWmViBy4pOGS11NqIGxn2Me3U9TpY7ruVA6Zw=; b=20KRvX5c2i+MhAqK+kZAj2X/hh/d19C0zecmRCf1gnT+RAj7ahXk7/X1j+FlvrAibM4hRW Is6cJO9Sl/nspgDWn7NKNqErDIhXeAAWhFGkG8DfQced1GwrkYsYUWhEk97tjrO+5mpGk+ Veua3vzo6x5W1EZqq79jwtvvSjrNBl0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734289083; a=rsa-sha256; cv=none; b=T7oR797PqB5pnx77N87W51OCC3NA9rBp9SUvHkD0d+3wtmPD+iiBPf9VEAbsAr987rnwjB t5z5MCv2xt71xrBHfajmaZDCCShOr2Jmp5uE6D2OnGF0C7xam0IzukVxnlN2INOvq3p8Zv W6I/sMgfPM2hjUbXM7LhIoXlE+QDL10= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dABbN62e; spf=pass (imf21.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 48AF05C5B59; Sun, 15 Dec 2024 18:57:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35DFFC4CEDE; Sun, 15 Dec 2024 18:58:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734289101; bh=JO3wJ67L+bBQYON3juM3zuVAaoxUR6DOyzoX+5ATl2k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dABbN62e5zHRGjy3VKyLbWhYQ0+3u7z3KN/O3aLiFX/HRzHwAI/exDTn9N9DOEm3f yaVrrvx5KhPcK038AiKFW3gHmMgMLEEVE56tFRDrNYrAGcbTeP3ZY+kNFXPncSPnOj ntnD1GrCD+LebkyB8GGRpCcP/9ZnMjTqV85JjnVO9O2WVAAhK2b/U8lBb5sYwpEqbb wUlJtremoJDGKJE9lZCC+UnPZlrlbxEAsd2pGrkXXYom27BJGYMDRheoWXMQqL6vEM EOlNdo/K4AcdkhTCxGTC5tW6uhGWJ7VdAOjpMfgL6DqGG29C1+y/kDRtPsVfPrvqsK YVBIWqSL4tyjw== 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 v5 1/5] libfs: Return ENOSPC when the directory offset range is exhausted Date: Sun, 15 Dec 2024 13:58:12 -0500 Message-ID: <20241215185816.1826975-2-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241215185816.1826975-1-cel@kernel.org> References: <20241215185816.1826975-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1A0521C0002 X-Stat-Signature: f5wqu5u1afd3qtjgjxbat3pngrfnq9i6 X-Rspam-User: X-HE-Tag: 1734289047-957005 X-HE-Meta: U2FsdGVkX1/wHVEuS2VWlZC/aSs2xX0Cq6eYy/rDqj4PuouNcki2C9y9mOv1TG+N1ioif+PHXWN0xS8jViHTHIUpi7ZTMbLF5k62EDzCT+iSLnaRMvqtxJUExfLNhg7FFDRBUDE9teHOaV3rzZKW8pOSgDqRb9jHpnUPYNKqcCpmfmq9vRVwUBiw1sOg3t2oOzvDSERdtDPPTQntR1TzOOt+XXMR8+od3qnWNyFo9yYwHFZHiIHQNovKqQhbF0N4R7SzT9hrbIsnNiJ4kiogfGIatqyQQZDS+6PRglJWLHUoW02acQirZ4D9h3TC/wBqrbPU/mhRyoDxSBHKDT4LdTNXZLhnI1oXAbT0sGDDAlCa0c2LBAbHThaLBDjZFSsrM9yQmlKb5nMo97eGL9dEes0GOLD7kNCFy8XfktgqnlmLBRwZc43JqMiW9BieB9R1hJV8CO5+10GLB9EalHmFeU8HrRChJZfPE+1bLlRC1k5i5lIEegIOORmeIkTdkC9xEy2PE9phcKpYgF5D5PyvCjvglei5pQUFXYApfJS/tE4Mdl0cl0D+adVzC2uJ5pMfsFEkZpvpvY7jQv7ZRsUoNF/Sm6ngylD2SvTbsdf8gD9PHsvM7dW6hN0wQQEhKOiE472z+LDBZi5cTnG+usWw3VemRWn2AZgKMi4jHpK2xinA3IdlWaHRhiC6cb0F5lO1WaAnzgiRpBdIltPby+AwsHhUAJyul+iOKQZi7c7XzEx0hBIYTAzN+IBgDiC06TVICJ5D3g/qjrW0YT1PKsxI/QVdKUryG42uK39Hm2xQ7YxTOs06K0vn49EWwYXopEkE+UKHMadf0DwiaarXjcuWdbloI2sza2SZ1TMHDy5DDWuYM5dTLCfUMIo6OklPHIMsyNHB8Gxrz+YobqRwEBsP3nb5/3y29nsH4+ALNzG0KxCejxw/FO0WWTFhKApOeaWGEIC2A328NwAU26627qt SnkKiDIY E7CW95mfq796bQtOjZP3gd1aaencQuMIxBNNGffyLDk2E/Jw5oeTlRq+YTTRdaqjKaElbR2fvx/uZzRU+xjHE1Su0NgnhP9f73DSO0wCNSrxkl5xs5U3A4jVMetmqNuZ+1+RA8tPUwRGxttGNfKvAg44Lu+n7H4/0lEtdQwPYfYsvz4oJZkwPMCiCUpyZExiowmyX5pWFKera3A62JIUDYptxBC5W01CrMwBpqsqqgcRl4+1nA+fv+PCQs+17ymFdLmCAEBbtNZCV6hM2hcB9B+nyUA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever Testing shows that the EBUSY error return from mtree_alloc_cyclic() leaks into user space. The ERRORS section of "man creat(2)" says: > EBUSY O_EXCL was specified in flags and pathname refers > to a block device that is in use by the system > (e.g., it is mounted). ENOSPC is closer to what applications expect in this situation. Note that the normal range of simple directory offset values is 2..2^63, so hitting this error is going to be rare to impossible. Fixes: 6faddda69f62 ("libfs: Add directory operations for stable offsets") Cc: # v6.9+ Reviewed-by: Jeff Layton Reviewed-by: Yang Erkun Signed-off-by: Chuck Lever --- fs/libfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/libfs.c b/fs/libfs.c index 748ac5923154..f6d04c69f195 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -292,7 +292,9 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN, LONG_MAX, &octx->next_offset, GFP_KERNEL); - if (ret < 0) + if (unlikely(ret == -EBUSY)) + return -ENOSPC; + if (unlikely(ret < 0)) return ret; offset_set(dentry, offset); From patchwork Sun Dec 15 18:58: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: 13908873 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 331B3E7717F for ; Sun, 15 Dec 2024 18:58:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6D4C56B0085; Sun, 15 Dec 2024 13:58:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 635066B0088; Sun, 15 Dec 2024 13:58:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4AD0C6B0089; Sun, 15 Dec 2024 13:58: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 2D4436B0085 for ; Sun, 15 Dec 2024 13:58:25 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CD779A3C8D for ; Sun, 15 Dec 2024 18:58:24 +0000 (UTC) X-FDA: 82898103204.21.793922E Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id D0BB3140010 for ; Sun, 15 Dec 2024 18:58:03 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RWfkeu17; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.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=1734289090; 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=smC4UxGETgvC/ylD53lkuwmBZ7b2vnbUI16yw6ptyRg=; b=xh2yZ6XkcApmTzsRnB3j0+mk3JvGproYiuXSW5AkA9wZY9+smPPzqgsT93kJnqeha2haaq 6v4PhHr4Pt+IDcXOmdppSNmdKRkHaXcM1YbZiCkkvfkXEzkmOfsoji60mTFF9zeBxTsY5k dnjGgGERkilSVbUUz79tr4ChFKpkld4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734289090; a=rsa-sha256; cv=none; b=cGhxfziZ0A4gyqIdU5FbQFbfcuo5qiIpRm4fBmBJYppSRxYNL9vZhO0u/t+ghRzABcXyA4 B4wE1So+1Z2a9IJvMsA5xfUrxU/5hJQluGk3g0UY2K7PpOWVkmlkJT9StI50ovG/PhcqVo m3gON5ApCpq+j3JdYk4kTxIKrW3bvBc= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RWfkeu17; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.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 3BA475C5B7E; Sun, 15 Dec 2024 18:57:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C8ABC4CEE2; Sun, 15 Dec 2024 18:58:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734289102; bh=8uJ2Pr2s2e4IY7lhSwlQ1U0qNB24Hr3dfGxCSwH7i3Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RWfkeu17XsQ+VEfxJGvSI1fYGGkfbSlT2wtA6f7qr/YCmDZmIILW5Y7tthYzlzTjj +f739dSbqQXnNixYLSpa/VJWdd/z4Lw4NmTKq4c2rPSur2mK8Qd6WJqcbN7dMoua6l 3hIXUCg94IHunYxZwUmbgaheKN9dkL/Jpxz1FYF6ldyB6gKg4P2V7DUGIEuaTvu67g U+tprO9LReZW8Jz6fxc8s64cDP6lP7UrykVNSQdV3C//s4wPhLBxsvCi9uEcQBzBco JM0GEkPZSaToe/PoMuv7e7WSQ6seR1DI8J4lLITiGagTpCgTvjRQhUQ6VwZYtod69Y JgEZLwf+wpBSg== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v5 2/5] Revert "libfs: Add simple_offset_empty()" Date: Sun, 15 Dec 2024 13:58:13 -0500 Message-ID: <20241215185816.1826975-3-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241215185816.1826975-1-cel@kernel.org> References: <20241215185816.1826975-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 953958bm7t5rhw9xhnzknnjyda3zr3nx X-Rspamd-Queue-Id: D0BB3140010 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1734289083-202241 X-HE-Meta: U2FsdGVkX1+90IunZ8Zsh7nIzBCA0lYDT7WqMMDe7i3orIyRJKY5430a1N4HZqkiVqgvk308sIvgNcQ/yPRnh+pYeA4VUceLo6WHyoKob80TiSCWu1wO9xOW+otN4ZtMHwgunScEeGidsajTPj4f2lErtm3hkcjtWqS7nmzaj0oUuqai/IDhq5FW5PMZavyArH776SzaoOm/zdj828uCkz8IoM0lEvoyJ6CrqfGgwenF/9ai2M52otX7n+utLDRYcTSXvxOGwHvK1o1uLMJDqtDmhjbVAVl56i8JJgd51Zqy2Nv6UOeKIDybdxdiEk79hXwfN/hU5SNZ4dCdN2h4K4lh0AFukeBDigFm+e02mcDGDxqIQ3at4P7MhxKuMlwVkcKU4eZhpA9B9ygWA792aDcjjg8H+UdsqKvTFXhEsPMWtk1mKCCQ6SiYgmgYLkQcSmKOK9p3mvpD0cgoUwqGwobKsJ1d6LihKbzC7pTBpQGEvx743d4f8JxntyVJRKvpAn31MWnkdpb+pVbbZ/Tq9gZBDzkhuLQsNoXocGln/s0U+xOXLEse+bFyUnM6RZeH/CgonnBMTbGcv9rngrnC04Cy+OKx61L0Fbw4nyOO6LosOfrewwkkG5IAZRoau38vxU6ORqx5f2XO85rpH5T23orR5ys1U7KdIGDDpTGH/1Ld96vJBGgSbr3Ib++2CPR7/1E0m1T5XAwDr1NCJYviCznuT+HAIq9CvD3uIFrhKBXWbGM0qnXukJKPBvsDQhATOzLAnhBiHUDITFHVzDIAoOLLUYGaehRRSMCK7vtUqNDEoT37gNs0PyQXg8U3gHBgaauHsk6MS3UKSKkFja8U5l0YEaS52BQzCHPf8X0Sl7aFl3GMx5h5IJ06Quvxtxi7OBeFDLGEyFbVb+D2Vp742fTL3XP+t5675BRTtdVnu3pxXG8Ai1WXvsrk1rajXmKCSId6Xi8pJ9QcXIZEtAW KPrI/EH9 SieXU800LM3MBQ5uU1uGPLehBcmKzsAWCSm1Jifi7N9FeD1NHyp7ls5Jtk2WwmA6bwzKF33jcqviZexXUbce9UVpB4RIej6xzr+vIqwhJBDevRizqrRpStffdTiE0o4PAtEBaHnpLogMN/+YUgvj1C3T8g8nTh0LkR6dugojCs7lIBG9kLbEQTBrbXReGu9NrCgcA+0J8jxqyapqze2icfcoPGCOf25nq7D43QLIxhkXAvNC4nLwYnL2mZZT2hJIuNYSv9WVbI6YUkIy8vv69zN+IWg== 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 f6d04c69f195..9bcc97ffae48 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -331,38 +331,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 Sun Dec 15 18:58: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: 13908874 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 0BFB9E7716A for ; Sun, 15 Dec 2024 18:58:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 660176B0088; Sun, 15 Dec 2024 13:58:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 573246B0089; Sun, 15 Dec 2024 13:58:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34FC76B008A; Sun, 15 Dec 2024 13:58: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 11C086B0088 for ; Sun, 15 Dec 2024 13:58:26 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BCB8C8099E for ; Sun, 15 Dec 2024 18:58:25 +0000 (UTC) X-FDA: 82898102868.03.A4B50FD Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id E8655A000A for ; Sun, 15 Dec 2024 18:57:45 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=C5ntpeov; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf15.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=1734289077; a=rsa-sha256; cv=none; b=X4N71yP6wEeUA+n+yVPqDUMZkkbnp2SUckMEiEGAPY1i3OT7l6RZpWvTPi9ZLJB60IwCcb wMqrn+1XR+nLo4k5f5wYYV5L0ioQ7bW5/a6PfVWsFtuBuYxF6QigowuNGNpUTBR2G92cGO bqVOk9A34Sq/NmAd8yVcJMN+HbuSIHk= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=C5ntpeov; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf15.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=1734289077; 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=VdPjGR2E2+UExHEiFw1Cf8BOOWcYLNG4Eq+jmFMchvU=; b=EV3LJmXh9FJo5XX75rFDmbUjBh1VesTGdF3tSlsU5PEzG4D+jKe3acRXW3CzUbLTxzZh8Q JLhtsjim7XGujRPjGsf1mN8bHA1zyjPVZKXKFFL4Z9p5NjMpphtWeiXGWQ5BDBRY4tcYAP DQci8Gyncv5YyT81QGyp97uscUeSHdo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 38DB45C5BB3; Sun, 15 Dec 2024 18:57:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68EE3C4CEDE; Sun, 15 Dec 2024 18:58:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734289103; bh=kCPvW5FxPOYga8gMnuTxx4jZhRkpftnsIXUFxGq60Z0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C5ntpeov+8jzlqBsYyFs3l5HVfXviB9nAAzmvK64xTn1UYiKjw1SB7c0JlaVKBJfS G4XcptzghgKtD1BQkJVfe1RECm3Yl7yGtai5oWP6sH+q4dHej1RwnoS8tga8aIZXEH GVOG+3GrmojLwShFeV8wgrqymn4csMiqrfXErD9klwvBYpIJU8p9eZtnhNQsCSd+Er Upd1Rch8Kw4JHMtW4+celCN3TcIs0W6PzhzqyPRI6rn65f7k0YHvnZJlWZCgmeuAwU 1DKJnhYRR9wpZ/zrq+lDRaTyF/GlFc5f9Y2JApXvWSzz1oKZFXQvXbgYH8j50dwpid KvaqMwEteUZhg== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v5 3/5] Revert "libfs: fix infinite directory reads for offset dir" Date: Sun, 15 Dec 2024 13:58:14 -0500 Message-ID: <20241215185816.1826975-4-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241215185816.1826975-1-cel@kernel.org> References: <20241215185816.1826975-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: ytj9remne9oziikxmnhbtwohwpwqfwtg X-Rspam-User: X-Rspamd-Queue-Id: E8655A000A X-Rspamd-Server: rspam08 X-HE-Tag: 1734289065-351266 X-HE-Meta: U2FsdGVkX19nXt1jz7aUkLMiKqTcx40CWsi2+JmTxPT4wlVQCRgueU1auaQYU4YMQpOAptm0e/t5DQahwRGdMr1Ozggwr16HFbIfsiFs0rsHFWXyqRDkzCnNeKS8053eNCdsHMYO9fIkpYy5m3CgPONn+w6O4oZrDIMpx6QyPvgUvzZbqNURm8WU/xZXhhPzw02llDwHvcGBzLRyETrgZfogK7GBbWZkTXcWOJKuACAd6NAoZzXO3gY+KRFPFrgQk2KE4IPp666Lne/e1cKLLn99beOZ8bACGOMz2hFf/+dGAFuw86zS9csFRHrplOY2iwX0F181CwqUJ1qJg4kLLcn9cejfFgdj9E3L51tiqtBRZNt4q6SuIX0rZXJZWNTGzemRVlOaMqX/P0gE+AzDDO+E4Q7qKHznXTbVMgRkYtHD5BiIZpgrUpmeI+9NobsaJZ+f5HNydEdN456RA/r4nP4DUW1tbrI9TpHCaFejgGaBDAs3CnkwEryz9HC1TD7nkskIYUkzNzLgixplgDzVLafKqLRQj0Eaw8BhzpAIgVyKuleppFIhJBk/ga4asyI0P+CjryCopToeEuTcBDqeCv6Oc/WGAY45rWFprlDFgL1TJfPkvrJV+W7xg26S64Bxpcc2gkMo4qDL5tBz3Te8Ol+Lt80NDFZ0UMvhlUyzdlRmoog8JzuR9kYUKm5VEmJ0S5wIQfkLVXlhC1O4uK5oar9fO3mk6YFdj4SHtD/Z64Ige1liX3idfZ+Lwv7QeZtdphRpjW2nRS7D27k68bqrhUUN2FNUiq+qkcm67In4KGgN4GjyHSBFwbJAw5ZIHHJy9vB2uQy4dx9kldDlQnmY5p6GwlwXp3YPJUsuNOa2pG1uaA1uwmkVOlLfrmUcPyOopuA8Pm6RLDkmG96um4mS/trAF1wFCWCk4rnAxnHssRHJ7bCy7zsF0XrfjEVjcbQ69uXmcUMRfQOWAtjQ1CR qiH9Ymik wc8DnumHl18JJvPHRsbCBYqqbTTJTT1wShpcM7nUyfzxDe2WYHlTXGR80rd7XLtgm3WZ4pJNbcm637bbsUXwUIBxTdjaokNoGkug7X7sgImzf5J+Xzi9+eXeMoIiKjGjN6yMBDqH4Yq6IEemSjVs1HibvHIEl/agdtbf8uflFjnY5aWY3Bf1zQCOzXdWgIRXvcCi7E92DH18tvcAm+5UNGbKZxBMfgCaTS8xqzkkWHAXToSvS6vpdVVHx8HK4snOI6LvY 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 9bcc97ffae48..fdf6ba305112 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -424,14 +424,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 @@ -445,9 +437,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; @@ -461,8 +450,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); } @@ -493,7 +481,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; @@ -501,21 +489,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; } /** @@ -542,19 +526,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 Sun Dec 15 18:58:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13908875 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 C3A02E77183 for ; Sun, 15 Dec 2024 18:58:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 943AA6B008A; Sun, 15 Dec 2024 13:58:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A7276B008C; Sun, 15 Dec 2024 13:58:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D3696B0092; Sun, 15 Dec 2024 13:58:27 -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 43DC96B008A for ; Sun, 15 Dec 2024 13:58:27 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B429E143D98 for ; Sun, 15 Dec 2024 18:58:26 +0000 (UTC) X-FDA: 82898103960.14.09D59AC Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id 419C0C000A for ; Sun, 15 Dec 2024 18:57:55 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TFNIkygx; spf=pass (imf22.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=1734289085; 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=aAvguEMWO+oTZARsOnJKICf5vN1eH41dlsezwlFxfZ8=; b=Ev0kigNGl9SgcZ/FfpLfKqEzjSeckAyGjZevwxAzfVzblYLgMqHu2SJgOAPKOOCvkHglum Lnr4YNE0INYdfQaJEjjvGJZgUvBfccZ1F5ErLCsxIQZ9LldKhCwG1BWlOJPQR0lbBZ8Mvl thaVZBdA1a4HoBCQpPopz1H4xKkQSvc= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TFNIkygx; spf=pass (imf22.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=1734289085; a=rsa-sha256; cv=none; b=rQTTJjH/zKmjkOq65Wr8x5UNPOw7T946WMHrS0ZXCakYSOLnUdjGWtLEAM6gEjgx3ZC3Me aNwaYTZxRQG1uBpejdKEJNtUFLbBeHFWEhhRsNeaRfP8yQmMihNzWnNv6O8WKxujxck7Xu nNC12hc1vhuAcvcNGxDKfWPBXGiWT6s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 381655C5BCF; Sun, 15 Dec 2024 18:57:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67B3AC4CEDD; Sun, 15 Dec 2024 18:58:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734289104; bh=Tq/R8xawPLnGo8zVwtB0/wJRvW6e6MlVfxOtKQzQZB4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TFNIkygxYg9NNwhU5zG2gWvQ5bGYpwy0Q4sSDv94OIAKu0Zzn6kHknu10sOtrBnPF bcv3Z58YLBc0XHfJUNBHPIXXbuSHCc82SIN2ugR5NEaoivReeP+HFz+oeUpMpKJnoo 9eTOdX2fDuQ3SzqU0IeI4lfY4cukyYUFDFGqdBYRDXZsDabXj+ALcbOD6PwA0ObMdQ NjrkEf7rJrQARagY2MyMnIdzFTQ9najo79jQhN/Swb8v02DaKvUVJm8W6pBldI83j2 i9BgRNXB86h8PVQJzlW88rEyHX+AWQG8bl5SHkg/Dr/5n472H+1f5mv2+vHWGJ0/r7 a4YEOykfeIWaw== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v5 4/5] libfs: Replace simple_offset end-of-directory detection Date: Sun, 15 Dec 2024 13:58:15 -0500 Message-ID: <20241215185816.1826975-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241215185816.1826975-1-cel@kernel.org> References: <20241215185816.1826975-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Stat-Signature: 45u9k9duekij9nz3juaxh7nhb6n7sa54 X-Rspamd-Queue-Id: 419C0C000A X-Rspam-User: X-HE-Tag: 1734289075-899110 X-HE-Meta: U2FsdGVkX18Jvy039aPUTEk/Or8NDyFno1xOB0NM7idDGcZHo0n27mp5Ly8MkzBM2NUHnCyex1d98V2GAMmU1vTle75jWSMPEcTP4hQpoQMySXEAfip7Gj+P76L5Cc4PWK6LRrb3habUtDQbuuVMZItqfFNn5GBh6Iw/+K2brSYJkzuVdR1h7tVkZI4rRGc2R/tMjDi+RTW4kGiIwkX9XvNDlb9sZxhmE+obdoT3QWMeXnCQQvshac0HOqvRKqr66GcIPIiAsZVegV7EjjJ0yjGOGF+k8DiU2nWh25v3OqRl1oNbSOQy5/J8R9o5jC6n73eC1ix5xd/7fRw3AmQZ7LQFZu2iEnGkqDxyhTOcfFT0mYNRLimGGBSuifIXb/KqRXlbNS8v+j+0O476FZliWHJb+4FQbshW+YJ1VBuVsJbi1Bzrs7lkloHmxz/4MjS3+sEGSZ6jQflj7li6J2Z5jRQB4tnePNlbB/gihBELE/aDxGGCMyXonNCS/atY4HOs3rQGmrOQfDp3X45dWjD0V+iA/MpMSHp9pcrOSiHARvp8TtE7Ify7g0lVnFGiLCikvWsb7BSGKJugOpzGMzclA8Y4aWkskzy63D2xGESG2hbhuEW37wJLdRtJ2sYYFJ6wxQPu9dK2c7qcQWqRcQChmNYwwB9BqRDYqsPR+nhK2jFJQWCgve4GZG4hdH9IZQSe38CxexCV8oo3ziKh0OfT7dZFEn0KGYqSqdlwURdrKjErvuhVA5y90qeQpSOCQzNUi5ppkygS7CwaeJeYXzY1rGhJsmszJ6mYJaamA8Op38QCjeKDtGvG2hklzqDibqZwJJV+/rl8K13NtAycWBWb8tarK8Cc2oFn2hUx5ZUGWeQDVaecb9d+1ckO4Cacs3HtuFCKUY+vaHo7iiulJz2Bfo8JucB5/j3Y3rtCl8WW1mJc9swEsCaNc9Bq0N/biLCh+Kh3FKvl7nAN3Nyaf7e DZt1nEYI uQDnqC6hPx42cfJ6u+5k8988IO0Ky21rEkHxGS6ntEnhPkQ8EW2nmquhy+nEnZ6TmB5h2xMXwj8yz4SMJa37vEQwDTNvlUIEV55NoiD39RsS6aL/ZLmy4PBryiMu3HbMV+dfTGu0nE0IWUpoDukyaq5bXNfFbguCSHcRmoXVWaxjfojViPW2+rpQcF5EzpwC2NAhTH3pOGkA4gfBcWYGmy9cTA29QfbOKvIfuWTi4nF4ThBT9UXwuimFO8akAjfZpyef6GCQbE+wTC2jIpkd2T3ZuAZU5q08zZeq7dEOhanh4Yrc4BMZRGiFbfYmUjjLVupxy 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 fdf6ba305112..00dfcfa97edf 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 == -EBUSY)) return -ENOSPC; if (unlikely(ret < 0)) @@ -449,8 +457,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); } @@ -460,7 +466,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); @@ -481,7 +487,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; @@ -489,7 +495,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); @@ -499,7 +505,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; } /** @@ -519,6 +528,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 @@ -531,13 +542,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 Sun Dec 15 18:58:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13908876 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 5B266E7717F for ; Sun, 15 Dec 2024 18:58:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60F5F6B008C; Sun, 15 Dec 2024 13:58:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5993A6B0092; Sun, 15 Dec 2024 13:58:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 414F56B0093; Sun, 15 Dec 2024 13:58:28 -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 22A776B008C for ; Sun, 15 Dec 2024 13:58:28 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C2771B387C for ; Sun, 15 Dec 2024 18:58:27 +0000 (UTC) X-FDA: 82898103666.18.88403E5 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf05.hostedemail.com (Postfix) with ESMTP id 0184E100003 for ; Sun, 15 Dec 2024 18:57:31 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qKIYgvaS; spf=pass (imf05.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=1734289093; 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=BDiRmuV4c2JwfhxobVpvzJpooaU7raV7eG/Hh2mjsKs=; b=dclyPyhfIAtkY6Vy6CJMzdNgiZZeX+12CNcQLUAJw/vCVnBbMlJkSjR0B14E8oTWsiHgnx 84UmXiS8NDtj/1Y7so0sAIst0FQmJda2JhLl8Pc2LpVDfkTwcb/I/b1e+tnXyxL5Uz7mQ1 EckL7YcR2xbXgD0rH6bWZF0vBx/06f0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qKIYgvaS; spf=pass (imf05.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=1734289093; a=rsa-sha256; cv=none; b=aWm8Voyfp9gwr7ZR0FoBtJurC0qIHaRXrjJh4cmdIe6Dbe2toEHB1FoR7ZJlIpuQw/7sRF PMwchjuBRte9oqDFuWklrrO0qr/pNW4axeAcfOwBQyt/y0PNtbVxIQ44cf3hMdUuUsYj9M NGsjLHJyMv3Byg7acNJjOHqXZaNhOyU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 98F62A40B1F; Sun, 15 Dec 2024 18:56:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66157C4CED3; Sun, 15 Dec 2024 18:58:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734289105; bh=gZ/vdeUfX1ITmM/5YeE1NO9zxKDtaZH7Hh+ponD2bvw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qKIYgvaSi1t/oFLOXTgmCAA8i1OvGy7hYuHOv13rDisURe4v4Eead6cymYUM9azb8 /ah1eOWYif1CAwSPtaCdAN3cGd9RZnUvurbb7Q5+TYX8BsVpouyEn7oQOTXviC5XDw zRT2sVdssLVu1legtdpzpFRiSHSuc5dyyjxxwb+1r1nWVyexbZmfwAwPcPpkIsOM9l F+wY7tjf0BZ19JZvuSHaQdLY9cgfLg5z3HCAdxoqiaft/znHQ0vbzQT21KpBAZeHlT TOIvYqjKzZ7+e1/RzCgFHBwX16/+WZ0pOlrRBbgjYmbrgIhbtlSLR8Jza5Qv43u+UP MMPF45m82iuaQ== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v5 5/5] libfs: Use d_children list to iterate simple_offset directories Date: Sun, 15 Dec 2024 13:58:16 -0500 Message-ID: <20241215185816.1826975-6-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241215185816.1826975-1-cel@kernel.org> References: <20241215185816.1826975-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0184E100003 X-Stat-Signature: k1x5xiwa9s9qwryw47o8bpor91q9cb9p X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1734289051-969440 X-HE-Meta: U2FsdGVkX192v+I0Re6/LNIydcr8XwDUNIqkxD2XA8l2JKTLExZlvfMyq82ullrrqAOjCnoUa/Ce/L3FVhvfnUcznQuU8ZSu56zj3D3AE3FvuQ+fLXmUGwo/03M+pddStynmkP5+RdvLjwlO5/UUvxApY3LIP6Fo6L9rFKBSrDazUpBdgL+7/Z7W/W84l5mfbLpBLC78LTO74Fb4scPto6S/lJHG3U5DirQDFi8TZ6eaJ9VZWNoQfqoXpZaAGQ4tWfSayR1GXd+k+6c2uWoJ0aMhTWooAhJOsF/fVBhM9zo1pZKHab71sxHl0zOvwsfcyofaWd+yZ+bjYXP4m2EOMaPgoLUkrrbFo04HoVA8Rb07hATKKpBajyx+NsKlGpBd6pPSEE1kvYPQ6Cdz+GXNZ85iIM6PEKvJvhl5F5S02CAfsiqqW2r/alrdHkaCRLur1/a4XRh8N1TSTJX0ym6GesgxUUs4nz4+mmJpjFa3E4eJ8hO1P/h4hD2qTR4TIrLqUuHFgiqR9qqL+KXS/3aw1KZ1gJlKOpmmp2aaf6AL54sgN/zmKqA2+FvcmWqBg+GqkXjf0n0fiYCnB8S8F3RY2WRRAG/KKFXC4+ttMC3F4HxxZMSdekqG4SP4qf63S65qnccdWYJl+jnnnLCfpx7LAGj+iBwekeHRBAI7OF/gV2v6jGUWzKEBZyXTKrANHoeqV9PXGUeVbYK79o1NuvtO+JJmDQ/6lBPHFKADCUwSgQaLAocGHYswvdAH2U8YdOq79w0LPBWd+h03mHhaEPFbacj0rdTSCHWsm9swX4Wp3cE/oLHO+0pyjDZ0N1+GoC+XqPXmVkojl3FSYeI7wynortXYBiPhvnW1vrZb3Ws21PQeDrLGZAK1i0ZKCCz+m8htsvY+uttFmlCsRkrp/kHTAn1YxCKr+/JTDAywopXIKRLLvx9qk/xsH1ob71AQcdWW38i8sze1pC9cHPYl4sg LK1/HcWr aoxepcqGSgnMb0RaloOFqUZvQZE5W8I2n6vDsQSUQ9TLN2x8h/5GcM8GLQTFY/ulfIxKOm8gU8+6uU6Mm5ROiuUIP+1fQG4uK79YPm3xSOEg8mSy/A8Z9wxJcsOCN1qFJx3pYgIAFRvCHZr1LbpitZhga7OsDYHN9Y5zPV6m9xML4dvSQYnoVZvy36wbZTMqwaVEu/oPijiMWOCPEl3ET2yc+wLB0IkxsD6nngdUNqPR8sbUOK8uicQ451jJKp8fw3R9G5WOZvQOa3XVNTuR/UxnBew== 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 00dfcfa97edf..cc1226bf2b56 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 */ }; @@ -460,51 +461,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: @@ -543,7 +575,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; }