From patchwork Sat Dec 28 17:55:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13922483 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 D98DDE7718F for ; Sat, 28 Dec 2024 17:55:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF73E6B008C; Sat, 28 Dec 2024 12:55:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AAA3A6B0092; Sat, 28 Dec 2024 12:55:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8ADBF6B0093; Sat, 28 Dec 2024 12:55:36 -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 69AB76B008C for ; Sat, 28 Dec 2024 12:55:36 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E98411C8833 for ; Sat, 28 Dec 2024 17:55:35 +0000 (UTC) X-FDA: 82945119390.16.6A47A36 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf07.hostedemail.com (Postfix) with ESMTP id B680D40011 for ; Sat, 28 Dec 2024 17:54:16 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EaponY1D; 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=1735408514; 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=ZB9NnR+dvpOpm5G2CXIYrlSh5WxxB0L0F8mkccpeRU8=; b=unppP/dbh1VrGiYipApybqH/ZxHQ5CsFjvtgfBtC9DwSfggpZXGsRb3+GTPuX3WrrQJ/wf fvE1OOvTHsHaTimFJU3VmtNItdSN4ATkL20ZwsrwTTpOqBpc9/QgWKpd+YMqc64NdmaDW0 tWFhVUphfAUVEGD7cprSCC5l4EkQj4A= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735408514; a=rsa-sha256; cv=none; b=PoWdozP8DkfAuOFWh7PU9ofNU/VcsxzVyJWPCIPfIYcLF5et4E8/yJWVg/afyTPQryKJYx bEn7Axy06QB2r1tHwXcZ86jvxUypV37yVrcA+7xR41S0qnP9Zv9N03Ql/3la225B56bapC c8WzT+nBcEsIPRe0SLrQ/ezELgV/hFo= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EaponY1D; 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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id F25485C48DC; Sat, 28 Dec 2024 17:54:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75879C4CED3; Sat, 28 Dec 2024 17:55:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735408533; bh=PJrR9iTSkLO7Ad1cW7Xy5tTxsQkFEv8En0/HpmU5uaA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EaponY1DCwvK9kO+sQHd8NmTQhrL3/6Ul2UEQonLWbZ3xmzGWlGLjdUA7Ppd2DgSx xItpQE3/E9HoJzUY6ayuKUxVdAhqk1F3mcSD9WK89Sy8YfMzhMkz4aCzzk0xID/d2X DQ4GH3HL6nZwcwXToopqV3OC1uLtoh8fJMtROQDlEN24kh1ktJZ16HBCiOmLjvtzOy Y9Wm3AyOnAZ+hmM62midzMME6KAUP1wR5T0ZrVs3239RgKlMLEaXEcVvi1ct0eLXkC RASz6cY7NBmfVwjEndQ17MQEGopknO+ZnXy1KLKJnvzMaH3OzBhk4iXSzLnTIUsAcO wXTaNZNNnJp4Q== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Liam.Howlett@oracle.com, Chuck Lever Subject: [PATCH v7 5/5] libfs: Use d_children list to iterate simple_offset directories Date: Sat, 28 Dec 2024 12:55:21 -0500 Message-ID: <20241228175522.1854234-6-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241228175522.1854234-1-cel@kernel.org> References: <20241228175522.1854234-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: nj7xs6mjzayfpuw9kocp5jc6hpyi7syc X-Rspamd-Queue-Id: B680D40011 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1735408456-6084 X-HE-Meta: U2FsdGVkX1+mwc+bgtHTa/ycJgtP1+EBCMBoBcttI9y0OOuoYU+qNI0wgwlPgkx6oKedAFM3a7MFls0qirxbJBmERS0sJcDpd3REwWGnQYAmpjMQ8ItAHFfLyOOsAoYAni3itmUl+nvpp81u58nQDBSrxEE/gKNWAUDFAnbDa8l3PIQb3frvdQlMRMMQ053+QnBAN/A0ruCJn/vfBy6q1fh+XJ5TieM0zm/BeNSXfAz8glS8POS31Giw9prJfJl4jy0ziDV1yBviju3ea7EFQTk625okhkjPcU8GMy5gcleYj+hm3+ZlCQT2/e7H/y8zjKVsLeGol39sKPJoRa6BvGHmvs77Lb6bxPibr+NgMLzqeZLkyljDhWCH5/rKUAvRN7ECurTNZ/62Ts9sdHT5YX00C7CT+qwF6YjmiGrGhybKlHFV1l0lVB8jsHYlWI1XoTcpTlh15Q/96Q2xP9M0CYPU4122wYHpXfjzARbikJuywK3wPx0c3LxhQImzMNHGRFngd3EyEZUu2nEztYxQ3Tr/8Xqs7zsP6GdOKoODylRY7AnJSS8wI8xaVj0bPbN97JMArpRyOtODyKk7RKOzJR3G7f/X5cQ4W+5cJv36OLOZ7JsqAS6Q7XLthDD3d6Me8bF0+Q87rzkxI/C3pZeQO+DnXowvKlOpJYzaS23VaAn2HQjRDKdlkxb7ne5WCLEm1Nm5WR6T3CfbTOkobTwL0BbGcwA4K0GOFsVSnXJ9ftjs5MSbaG29YLfs3veZX5bDLWZYo5C7+rRnQjCQoSUCU0ozcU8EV5kAVx/LCbz5VNOciaa2Oycemz18DlQvZUUZRquAnkPW+p5792X+lfgTKhBinBRM19aX4wHY2JNoXopK0tDLRmGLxCz8JpWCJST4keIrUqJU4mreEuZsvSH/0IN3wBCAsrs+/KGRKC+VFX2vatl0mVWtJd6AccmRGkxPwVzdMMONpWvaT5/sMJH vlSiuqvV CV4+KXGY7lb+mBUdIbmJo0DEyeOt2wEeyeoAPv21JmgnWm1GByYvkfor8egtkNc7r8TMMNVDhod+LhbHz9nYSddXzjFbhgw6Ls6HwPpH2SE7YOVJjF6RIGwlrN6UusJIg7x2uB93L3QB4cv1EJcr+Qhrhp2wTcfoakIsotDaagg8FJT8dbcsKhnRNnVY91rF91f3avvsRpFHb5r22dcAXaC+pZZ0mNiHdVjFwzvv4He/NeSZSPuRi6vXUi8xleZHAdrTJAEv4oF/3vzHWFxT7g6DCwg== 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 47399f90511a..279442b1fe96 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -247,12 +247,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, }; @@ -457,51 +458,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: @@ -540,7 +572,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; }