From mboxrd@z Thu Jan 1 00:00:00 1970 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 9CD31C0218A for ; Thu, 30 Jan 2025 08:59:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F22C280276; Thu, 30 Jan 2025 03:59:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 708C12800D0; Thu, 30 Jan 2025 03:59:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 580F0280276; Thu, 30 Jan 2025 03:59:55 -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 16D702800D0 for ; Thu, 30 Jan 2025 03:59:55 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BC1D3140AD6 for ; Thu, 30 Jan 2025 08:59:54 +0000 (UTC) X-FDA: 83063520708.17.E6F4FD9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id 13FB240004 for ; Thu, 30 Jan 2025 08:59:52 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=d8gzzU7I; spf=pass (imf12.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738227593; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:dkim-signature; bh=raxuyw0n32oiWki67Ph8IzXZVh3+flgb0Bi0srxoKNM=; b=Lc6/ceSKRbrRKbk6JOFnAdDOgZU4MQKUPEc8zg8jzuuuyW/K6Iv5dZSb0qUL0sY1U6UZeH x++jAWeAvKZa064sKJSYsBQHAcH36NnYxPzG6NB3uLNtVocCVre30RzdGdJykwzKxe7zT7 dYRSz4WUP/GVm1RktgJQhG7KAWgGRwU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=d8gzzU7I; spf=pass (imf12.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738227593; a=rsa-sha256; cv=none; b=V4h0bkuCZpD2vbK6h0D99nQaOvcgv6dgPmUMTf2gOhnIYkkFWIp/DPWJGXfUa7EcWguAwZ IclontpVNhAbWpy7eet3o+gCUbm0/YGCxus4SpBDbTRUOIk+AySUzfcDZkpkc9IJSVVIi8 mBpXInR2u/6fDEo1ni+2K17YtdggUYA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 527AF5C5EF3; Thu, 30 Jan 2025 08:59:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F27BC4CED2; Thu, 30 Jan 2025 08:59:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1738227591; bh=ymj2jlJjMTqOY+B+L/elI7KQMZWvuu2cTXrXQ+CnhkQ=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=d8gzzU7IHx38TYZj8hQUAwKQ0bajSDfL7Gh+BDUTQzOg7Zs8eapVXGJGXdMwhTBOY mOo07CW19gZMWbyu4IPhnMr+mEzye6aw40joPaEuqDvLeXaI+fNLxAdfrgBvc29T9s l14N+1OhYlGP5U1tF9M8tdl6DULtUo+6aRuXbSNc= Subject: Patch "libfs: Use d_children list to iterate simple_offset directories" has been added to the 6.6-stable tree To: akpm@linux-foundation.org,brauner@kernel.org,cel@kernel.org,chuck.lever@oracle.com,gregkh@linuxfoundation.org,hughd@google.com,linux-mm@kvack.org,sashal@kernel.org,viro@zeniv.linux.org.uk,yangerkun@huawei.com,yukuai3@huawei.com Cc: From: Date: Thu, 30 Jan 2025 09:59:31 +0100 In-Reply-To: <20250124191946.22308-11-cel@kernel.org> Message-ID: <2025013031-unfold-leotard-022a@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-stable: commit X-Patchwork-Hint: ignore X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 13FB240004 X-Stat-Signature: hgxe8gxhfnaa5tgs4mbogotfb3r9wk6r X-Rspam-User: X-HE-Tag: 1738227592-26572 X-HE-Meta: U2FsdGVkX19/9YqsjN1lUw7km5chbCPFaqRTF4nLSqgci3IO3/7taOQKCNg83gcT0MI4EhaEc2TvnSOi8GSC4j4ABfV+8eaIdseya5xjw0pPbUWnnL8wTZpXs3YoDZmnNkE2KkVnFbjJg6/CIibEpnOE6ABmDNyZyzh+bivkqT45X1475Z+YRk8lJJpHZfAYIGktqZjj6W2ykliDwysjozaVAYXxQ5rz0ISvnDVjTx73fOZ5clh66Qr6vy1LKtdNpSZ004KHbSIt1MNaDhNE0GHxNQvLCfhDuzq8z+I8hnTdmU9tsmSbGCGTW2u3tiFydpjACeHcgIwQId86ZGsglPTSTsx8HzpKVV4uOEI2uCv+BUycESsRbn6oIUrMFO8n1/+QMiiBh4hDQ0ESl1owpYYOexpgKO5Mirm2nS/LV8sz9Z4y22nB3vNVcGGiqpGOpCW9UQvNWk2XmI+jf6ytVAFamWEpFCsJAcKomgqHWCVqX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WgkL34wCXyWsEndMrHXeRkGjLXTqdtDcsgOhxTEg4HnrBD6JgBnGjx1+ddsoR/c3husuHDHlOz3CwrWJ1k7yOdrqqFHUFL414aTjjyZYPmO/sn0OziF7/4uiBaGBtIjSth04lafdCNSIHEL1V4NDpEEa/SSEJH5TnWAU1 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: This is a note to let you know that I've just added the patch titled libfs: Use d_children list to iterate simple_offset directories to the 6.6-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: libfs-use-d_children-list-to-iterate-simple_offset-directories.patch and it can be found in the queue-6.6 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From stable+bounces-110410-greg=kroah.com@vger.kernel.org Fri Jan 24 20:22:00 2025 From: cel@kernel.org Date: Fri, 24 Jan 2025 14:19:45 -0500 Subject: libfs: Use d_children list to iterate simple_offset directories To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever Message-ID: <20250124191946.22308-11-cel@kernel.org> 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 Signed-off-by: Greg Kroah-Hartman --- fs/libfs.c | 84 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 25 deletions(-) --- a/fs/libfs.c +++ b/fs/libfs.c @@ -241,12 +241,13 @@ EXPORT_SYMBOL(simple_dir_inode_operation /* 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 f 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 *f 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; } Patches currently in stable-queue which might be from cel@kernel.org are queue-6.6/libfs-replace-simple_offset-end-of-directory-detection.patch queue-6.6/libfs-re-arrange-locking-in-offset_iterate_dir.patch queue-6.6/libfs-add-simple_offset_empty.patch queue-6.6/shmem-fix-shmem_rename2.patch queue-6.6/revert-libfs-add-simple_offset_empty.patch queue-6.6/libfs-use-d_children-list-to-iterate-simple_offset-directories.patch queue-6.6/libfs-define-a-minimum-directory-offset.patch queue-6.6/libfs-return-enospc-when-the-directory-offset-range-is-exhausted.patch queue-6.6/libfs-fix-simple_offset_rename_exchange.patch queue-6.6/libfs-add-simple_offset_rename-api.patch