From patchwork Wed Dec 4 15:52:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13893981 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 12A99E77170 for ; Wed, 4 Dec 2024 15:53:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EF62E6B008C; Wed, 4 Dec 2024 10:53:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E7D9E6B0092; Wed, 4 Dec 2024 10:53:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CAC6D6B0093; Wed, 4 Dec 2024 10:53:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9D0836B008C for ; Wed, 4 Dec 2024 10:53:10 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 49B69C10B1 for ; Wed, 4 Dec 2024 15:53:10 +0000 (UTC) X-FDA: 82857720120.22.F4D4038 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id E6B541C001C for ; Wed, 4 Dec 2024 15:53:01 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FfMP5EMF; spf=pass (imf18.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=1733327581; 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=akvFEsoE62y2jlk3CZL5+Wc/+jvljrH7xjvrjCmp2qA=; b=scTmOPqrppwp5FJt8SbAmFciBqorDSlIwKy4IZkzk7wZSsfcG7x0KfOvcUpPSjXkI3FZp5 4sEH7b5vibE+Y4F9VyvlbU3JEuuZS5+DoYqkdGPjBB7avNnhjFDhUH1Bj9GDqyo7j1Tt5B fy1U8SZsA/KUhSptPVyZDQPOVBrvzEc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733327581; a=rsa-sha256; cv=none; b=lqzxzmvj6JWP5P6GpZnj8MiWZQYzTq6C5XF3f1VJFIZzrkXVfI0W7NKqHEchkxrZV8Rkew wrJ6KXBjAEj7X7On8Rwofse4gARZE7Ad90KGLRQxKk5eug2PqF0Xngq82feACuPyn+eFXY XXNCXKKeu51R4YgLtG0h3TB1ffKfJKQ= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FfMP5EMF; spf=pass (imf18.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 D3FFC5C6EE2; Wed, 4 Dec 2024 15:52:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF0DFC4CEDF; Wed, 4 Dec 2024 15:53:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733327587; bh=uvo1HVcXQPvOFc+Tys1OpqZ+zabfYR8WnzY6RxmyeLw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FfMP5EMFpAz6PQ9Cece/cfrswMGjeexM20kMaqoYqE76POVWpZCG0qVk7E/WxkBUW qYytJOTAKTZwUXhNfqVNydYqn+YQYz/30IALiV0Lm71vw5Ngfl5tvh8YpsMFRDcBck S4Ju5KlZSgq6lQAevI0+FLKrOCQk5Lx9W0WZyQMOHWS7abB729iS/H7JU1epZE9xQ5 RnzkOykEvT+xUv3uAQoZLDWXIZ+SZrF0EDdrWDThwntBXSaw6gccKJzT/rAW1KVVwV 6EDlv7bivXJ3bqgDZ3eFB02qvqc9+kT0g0OUff0la0HYoGAUObBmoUv5iqB0EDB044 5I6Jf7s9pIxDA== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v4 5/5] libfs: Use d_children list to iterate simple_offset directories Date: Wed, 4 Dec 2024 10:52:56 -0500 Message-ID: <20241204155257.1110338-6-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241204155257.1110338-1-cel@kernel.org> References: <20241204155257.1110338-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: E6B541C001C X-Stat-Signature: jaqnuxg67kjccbywfuzw4gbhguff7d47 X-Rspam-User: X-HE-Tag: 1733327581-556984 X-HE-Meta: U2FsdGVkX19wLUldw+fsElvYITy606HyjBfOTnmtdRPpGqH02Vt1NL1YzMmAS2tPhDAUI63RSGM3+SiFWNuQ/UydVzNjp9zvzZ+0LU0L2q+LHBBK6TKwJXyMop/fH7Xo68yOEyhjqoAyVOHDq9qpDc8RJu68Yk+qu3vukxwXHSX+Au7OHwmiUsnTqSdgY1CDQy79QWIF8zdcGimk+xwxlvqnSRPZRgGZRz11VVnPgq8gUMNmXeu0HNbbiSMQDTRfE82BNzqNxFwf4yeofNsY+ger4Zm3mSUAOTtbZLXjTwjCeFc6VahgSg67u7SorjUFEw3Cl2GHNHmyV1fnBsPeZGG6m/6/7OYn1uJyIkUzbJ36Yp8gDatHiFzjyv53w7egiie9m3/jCmbWh93/AhNXH0jZjme/UIrt40PhcF35nNhsSY2AjLkbyD6f33XSfUwrcRbgeZ9RB0fhEFMBaNo/Gj1yX6Qa0mircUTlUCPfnpW7zHFlWOjaXmKJfHhCoVt5J5V6VwfZG4RyiVrFoabo0X7xsUci0dzuBC6bUBNZ3KqrWxy7AbtawRKiI7mXEdpMUCOV0378jXJD5akWHEMbb2IAsJsmEc+f/xUEFQ0VHbCQmy0b7BCYxm9eFXOIlPETbMAp6HljDn+YHncpts8Q7ve9Ekilau9u7o5qnhiu5RaD0Vyi/bvwOG7W7SbN1ZJwBfKDMVZmr3udUh4p7plQS2gjkw/Ocy9EP93eZsu/vOmHSv3KJmnv1B25Nq3LW0cR1AOYctmYIWA6SbZQs+Rk+DigFFYgVuxT/Hh/xNYe5db7pZSDZ6aUD8SKjoYWzTRWYWYMTQQgsvgC2QpwEU8+GSzHJD8x1bMubgqBu7BzfZiT7WRAfekCa6mmb+FLTLiIx0laSmz9nl+G5IYtdfR9ndrAqnIuuLWIZk9+Qb7+UzYqMgPfdRh3z/9cMLNnFzu+ZXgKhsSiFZZ5d6Fn51T QtwIZCDi vrFHRa8GIsJiQ6m8J2HkH3PYStBIWTl9kPYMC1JaEt4VwglKOaqwAwr1UtldV5630WGTsnY2wgZP2giTgdNPD4ZRuVg+k4Mw4jGzItY+z/U1EEIA/bVt6ege6eUCFHQTBVX4h80t4wD171MhEvvv8wtxzwNeS7ZL+ni2JaXs7E0112eY5sqNxsrj9Cz7KGzV+RlB9NotS31VxMKtSOxv/u0vhzxMqq847amOcsvEQrqVZxZzuu4JdhRGwfu2shKSVXNpKNM51UGa8J/mH69yZwTtz1A== 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 | 77 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index fcb2cdf6e3f3..398eac385094 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -243,12 +243,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 */ }; @@ -456,19 +457,43 @@ 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) +/* Cf. find_next_child() */ +static struct dentry *find_next_sibling_locked(struct dentry *parent, + struct dentry *dentry) { - MA_STATE(mas, &octx->mt, offset, offset); + struct dentry *found = NULL; + + 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; + } + return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup(struct file *file, loff_t offset) +{ + struct dentry *parent = file->f_path.dentry; struct dentry *child, *found = NULL; + struct inode *inode = d_inode(parent); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); + + MA_STATE(mas, &octx->mt, offset, offset); 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); + + spin_lock(&parent->d_lock); + found = find_next_sibling_locked(parent, child); + spin_unlock(&parent->d_lock); out: rcu_read_unlock(); return found; @@ -477,30 +502,42 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) 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; + if (ctx->pos == DIR_OFFSET_FIRST) { + spin_lock(&dir->d_lock); + dentry = find_next_sibling_locked(dir, d_first_child(dir)); + spin_unlock(&dir->d_lock); + } else + dentry = offset_dir_lookup(file, 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; + spin_lock(&dir->d_lock); + next = find_next_sibling_locked(dir, d_next_sibling(dentry)); + spin_unlock(&dir->d_lock); dput(dentry); + + if (!next) + goto out_eod; + dentry = next; } + dput(dentry); return; out_eod: @@ -539,7 +576,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; }