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; }