From patchwork Fri Dec 20 15:33:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13916891 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82B1BE77188 for ; Fri, 20 Dec 2024 15:33:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD20C6B008C; Fri, 20 Dec 2024 10:33:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A5AB06B0092; Fri, 20 Dec 2024 10:33:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AE906B0093; Fri, 20 Dec 2024 10:33:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 69B876B008C for ; Fri, 20 Dec 2024 10:33:26 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EF4CD80ADA for ; Fri, 20 Dec 2024 15:33:25 +0000 (UTC) X-FDA: 82915730814.05.ECA8D94 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf25.hostedemail.com (Postfix) with ESMTP id 9DFE2A0018 for ; Fri, 20 Dec 2024 15:32:59 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IDsNQlbg; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734708788; a=rsa-sha256; cv=none; b=DFTeuNo+1YI7HcZikMydZaBtqB2OouKw4PCF9O3LYR/FtfPkRLl2Ovy+tX2u/TdATDFZtr WRsIYratk73olEaT61fNJMFkfrQfr5UtLdlLVSfjytO98/djRB8cnsCLF5pO0EJ0S58xmU 25gf9URqeZMLwsdfBCDSPDTPWZcFEG8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IDsNQlbg; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734708788; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bmGtV7mTGrpwrYWYrwo7RfamUmQFLG9UDcb7skUIJmU=; b=W1sS5r7sMMERdDc39AdXFvlrqrJT2r2+Qu6P+/BS+rEiMxA98ByZECIJmzQJ6MVYGbxgqb l/Rl59w99b/rO5CuAKvC5HQ59cpSFR770NyvJVuaartlJZywUnvKoszxMjhB00zBO2GW1j rd0F7wzJ6E8YCUUOagsIWZBL0b7lPKA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 153B6A41CC0; Fri, 20 Dec 2024 15:31:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B8A0C4CED7; Fri, 20 Dec 2024 15:33:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734708803; bh=psAnJLeu3hNfs59sG2ggPtKJEHtiA7mAWB4G4Xrk6tA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IDsNQlbgHXeuReqCHgXioyS/pdHOy3vGgcJZ6fSGc9D/qnEjc0eOFQPfe+BuxoI8K B2/lmZm1UCzAnbVKGIyiRccGs9V2Rhi6oAUdInqd8PB9bF0tX5vlTMmP/VT0U6uGz/ IG+2j9WlhYs4AIhpsAaCYHa4zdwa68C0PcsyeIJW+eIsx92hA3aGuj3S6LeWCDmJMI x1fZ+2072BuqEj0WN/ZUHKPBECkawlGjckWWT31YVtMKaZEV+Zopts0zBRZcEf9F/H 8w9VxVd6M0CVDsCrn+/O/EPfgaXYTpjpXsri39GrSWgZksQs46QzD5XQzbt4o6YXeu ZqZizLwzEsVmw== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v6 5/5] libfs: Use d_children list to iterate simple_offset directories Date: Fri, 20 Dec 2024 10:33:14 -0500 Message-ID: <20241220153314.5237-6-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241220153314.5237-1-cel@kernel.org> References: <20241220153314.5237-1-cel@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 9DFE2A0018 X-Stat-Signature: z8dudfr7ofkzwey4n9j1r6oya98txokt X-HE-Tag: 1734708779-435160 X-HE-Meta: U2FsdGVkX1/GuGE9XbPG9cOF4v5+26/iTsQcHjduZOk6sT+BOD4d06czebipb+KRUrEsxetK39igIBdERWFElsudftWeg3hCrFXJ7lxZexDiN2Uz4GLXo1AYQb+tlFMBfRxhTcusRjhbkmNEmUjR2y72UW5sbXq6iuAEfb8NudXSYfXSIMVSRnyZUQFrHaxsPtaIwTyHWsg29M06cOXs6RdjIGntGjTOlsDTWSeFNNXljMtZF0w6Ii8Bh1msh7LsI6+bgmmEvrQiBgxQ3+c721XUFQiIUT0ykD64QsHZJZ2ItdGRwyvOG/9YxfGXgFPPWo8oQRV4eRKeXy2XstJ4yN8Tl5olCU7XmTN1R1GSYSXbtYt0/KcCFn2eJO1R5Vk09TZH8pVMJ69POCeQLWf/r1FliCUn0ALhqHLTq1BAZYCppVqGBIyQdHNCyG7T8DHO107NZqXjT9Kgg676Hk50UsPp12b2AyYmrYQ4RARjsEnTZQXZwm9DP2gqdMnuMRVj3mK6Pu608h/RZKf2/NFgfo9XixX7fynFARP2ZvaVxXKdKOT7n3Nw4YvtUQPSMn4WCI5FjFeoa8cvBrbJkqxFVJyijO5IHHz60OQhn60At0GZmKeTT94gPmVX1Gfkf4rPmVsRJRpSfTv6zOKekR21Z3DPFslznZCMi1hxgH/LogJPcrBRGJQZM0TN7TpCB3mQr8/gkFYFah/2ESR5na/lTUIfd3zpIscdlJFwJUF+GE4/FoDxJs6lY9zNVITb2PUKwmcYczvukJRz5hUJf5+13p/8vbHWdR//cDRWw5375jAJaL/SK62HxlZ3dIXdrQYbVVidOf/KRK0N+1bydQPt9hpfFhiMoCn+7ETGIGTlyu0n4gi1TXaV0PMbO5dnTmKqZP5H7ryL2fqdUu82wmCpBlMR2w2w/rNrH4pZWzGQQ582XRftn/LsFJHiB4KN3X9Ax66AThfhV5dIO9NdsXj YK7H2oqy lDj4yPOTpX8d7LxGvb8asDTMT054gMdmdNK3vQcrsc/I/ORNLoSJWE6+unTHntCeJ328zvnBJNl3gvYeBaJb3V4xOVnOdNTZ81GqpnbExNrEq3qKuS2LEqcF7pFBS7G2w4oVbQrdN+rZv8rfSdkU6qdANkj440GtaTxQ7HHSg1bifW4+LJ2+bZGH2X6laWMvK+fVnz9g3v6Ic73ILVAHz4NCWe4eTW1mIKB59Fd6864ZIlPDucqHtl5LNRFJ4H7ybYSvhFYcCO2eJOcygHIOGbdXHmg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever The mtree mechanism has been effective at creating directory offsets that are stable over multiple opendir instances. However, it has not been able to handle the subtleties of renames that are concurrent with readdir. Instead of using the mtree to emit entries in the order of their offset values, use it only to map incoming ctx->pos to a starting entry. Then use the directory's d_children list, which is already maintained properly by the dcache, to find the next child to emit. One of the sneaky things about this is that when the mtree-allocated offset value wraps (which is very rare), looking up ctx->pos++ is not going to find the next entry; it will return NULL. Instead, by following the d_children list, the offset values can appear in any order but all of the entries in the directory will be visited eventually. Note also that the readdir() is guaranteed to reach the tail of this list. Entries are added only at the head of d_children, and readdir walks from its current position in that list towards its tail. Signed-off-by: Chuck Lever --- fs/libfs.c | 84 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 5c56783c03a5..f7ead02062ad 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -247,12 +247,13 @@ EXPORT_SYMBOL(simple_dir_inode_operations); /* simple_offset_add() allocation range */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_MIN = 3, DIR_OFFSET_MAX = LONG_MAX - 1, }; /* simple_offset_add() never assigns these to a dentry */ enum { + DIR_OFFSET_FIRST = 2, /* Find first real entry */ DIR_OFFSET_EOD = LONG_MAX, /* Marks EOD */ }; @@ -458,51 +459,82 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, LONG_MAX); } -static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) +static struct dentry *find_positive_dentry(struct dentry *parent, + struct dentry *dentry, + bool next) { - MA_STATE(mas, &octx->mt, offset, offset); + struct dentry *found = NULL; + + spin_lock(&parent->d_lock); + if (next) + dentry = d_next_sibling(dentry); + else if (!dentry) + dentry = d_first_child(parent); + hlist_for_each_entry_from(dentry, d_sib) { + if (!simple_positive(dentry)) + continue; + spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); + if (simple_positive(dentry)) + found = dget_dlock(dentry); + spin_unlock(&dentry->d_lock); + if (likely(found)) + break; + } + spin_unlock(&parent->d_lock); + return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup(struct dentry *parent, loff_t offset) +{ + struct inode *inode = d_inode(parent); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *child, *found = NULL; - rcu_read_lock(); - child = mas_find(&mas, DIR_OFFSET_MAX); - if (!child) - goto out; - spin_lock(&child->d_lock); - if (simple_positive(child)) - found = dget_dlock(child); - spin_unlock(&child->d_lock); -out: - rcu_read_unlock(); + MA_STATE(mas, &octx->mt, offset, offset); + + if (offset == DIR_OFFSET_FIRST) + found = find_positive_dentry(parent, NULL, false); + else { + rcu_read_lock(); + child = mas_find(&mas, DIR_OFFSET_MAX); + found = find_positive_dentry(parent, child, false); + rcu_read_unlock(); + } return found; } static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) { struct inode *inode = d_inode(dentry); - long offset = dentry2offset(dentry); - return ctx->actor(ctx, dentry->d_name.name, dentry->d_name.len, offset, - inode->i_ino, fs_umode_to_dtype(inode->i_mode)); + return dir_emit(ctx, dentry->d_name.name, dentry->d_name.len, + inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } -static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx) +static void offset_iterate_dir(struct file *file, struct dir_context *ctx) { - struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); + struct dentry *dir = file->f_path.dentry; struct dentry *dentry; + dentry = offset_dir_lookup(dir, ctx->pos); + if (!dentry) + goto out_eod; while (true) { - dentry = offset_find_next(octx, ctx->pos); - if (!dentry) - goto out_eod; + struct dentry *next; - if (!offset_dir_emit(ctx, dentry)) { - dput(dentry); + ctx->pos = dentry2offset(dentry); + if (!offset_dir_emit(ctx, dentry)) break; - } - ctx->pos = dentry2offset(dentry) + 1; + next = find_positive_dentry(dir, dentry, true); dput(dentry); + + if (!next) + goto out_eod; + dentry = next; } + dput(dentry); return; out_eod: @@ -541,7 +573,7 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; if (ctx->pos != DIR_OFFSET_EOD) - offset_iterate_dir(d_inode(dir), ctx); + offset_iterate_dir(file, ctx); return 0; }