From patchwork Wed Nov 15 20:22:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13457277 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 86043C2BB3F for ; Wed, 15 Nov 2023 20:22:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 22B3E80028; Wed, 15 Nov 2023 15:22:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1DB7480022; Wed, 15 Nov 2023 15:22:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07CF080028; Wed, 15 Nov 2023 15:22:58 -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 E7F2D80022 for ; Wed, 15 Nov 2023 15:22:57 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A582040249 for ; Wed, 15 Nov 2023 20:22:57 +0000 (UTC) X-FDA: 81461312394.21.29F7E85 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id 082A840007 for ; Wed, 15 Nov 2023 20:22:55 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dRWJNFfg; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.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=1700079776; 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: references:dkim-signature; bh=9AQ6ylcLmxiS0Yo0n1E+vFqZnqSbEMXvhzx5iGA4oz8=; b=nZU3Usf7IyTjmy6+lr7HdS+DWtGBDoEChuhbCnrEIvKbI6miUY7t0qeXxe8AEsdUSIknqU CcWhUHANAae8A0vMqABRJVlI/LHgp3XSUuk6pLpR2WUiMGzFiS3JbSgEc801BIy3wExstJ 5o0rcNzOPxDCLgLn/N+RFXC4X0tBNjk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dRWJNFfg; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.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=1700079776; a=rsa-sha256; cv=none; b=AIpEjLxhtuuKSasxilriPVpPhgkWq/hg1B4g32UhO//B50e/1t3Hr3M89xdX6qQT75dGW+ sO4/h4zYy/uesdY6dYEfkBYKOylcL27l7TglqQLcmbcVZxPDfv33fwmpQhbWTGuWR4dyaH d3VomIRAsE/Hyvyd+EZRnxDDJfgKRTk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 28D77617AC; Wed, 15 Nov 2023 20:22:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A665C433C8; Wed, 15 Nov 2023 20:22:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700079774; bh=lzWdHL95vklKr9aA+uM9xzDRXnQaZXwtYaUbQTqi7aA=; h=Subject:From:To:Cc:Date:From; b=dRWJNFfgA9LJs/tMIfQosa2ubKY4wfJUTkQ999MakUB0dGfzcDMPAUmvqQ90dSd7+ e2LF7iyNQoJBPuW68dp5U2VcOd0PUo+dNAQSu+mepjRK+gzeP5CbpMoWE/T2wQ/aLR JFSV16QE7se6MS52J60QbGfLTjQkyPU1fa+6BKJ3f1sXmPNEgiqQTyLzhXTQLPlEQY 6kOsA4afWIFjBdrjT0Jb+NLUZRB7UpOYfVMJMjx5IDoFCwiq4IENCrD6hPC60E1wVl ELa9qYGqRYcqEQBxv4iVEU23yTtdO73o8pdFwHjfA8Q+4WokH1Iyj8cBAlspj97ODN PTCXGMFPbv0lA== Subject: [PATCH v2] libfs: getdents() should return 0 after reaching EOD From: Chuck Lever To: akpm@linux-foundation.org, brauner@kernel.org, hughd@google.com, jlayton@redhat.com, viro@zeniv.linux.org.uk Cc: Tavian Barnes , Chuck Lever , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Date: Wed, 15 Nov 2023 15:22:52 -0500 Message-ID: <170007970281.4975.12356401645395490640.stgit@bazille.1015granger.net> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 082A840007 X-Stat-Signature: qh54koruk1dxgwxmacyoo1qeokboh9j7 X-Rspam-User: X-HE-Tag: 1700079775-305048 X-HE-Meta: U2FsdGVkX18PMOzwxwtfoK8uPj3yeCsTVm+0m4x26VexIJ86iT7Ly6tQ3a0/4YV24bm5dkx0JCj9J48t4IRSGA3P7UXAWQjWKE32CX4xovxwE1/PH+n0SBNQ2k0YKAJsCVtSqqr7nu/inlwnxWNg9tnkng4Ih4HEBMnuQ/FgEaeBlmLZ4K+MxoIb44F53BFG/MumEfAVnarVGe2KOCxraaA+177K231hJ5+Fvdho0Mf8r5F9G576ouYb3x4ccjgkVISDsX0zv1YlnnfK5wWzlwVzkqRgXYo5ebJsUDGanaw2x4DKYmKuAd1Sq8RUhJ/+QdpU7NnhFgTBIEgqPYTE3b5wJCpZZAcAN5SdKpiLNKeGF33feRAfIlFNrIinEZIp5Q/E+WwQPKkn8SQfcGpWLViIGLHWBUy/LCxCHWwe4PoYH56YhDgiiw7hvMouDaVwwLciwIROmE+s4XQKBFhZlV5QvaCWgItqOXroAHsiUW+8abiGfqmMrKdVE4PmlGTxAEXNeNCHt7jINMLm2zZyEmCgqSx0dQ/RM6FFMzQ+vhUH+M8r0DUhqEViAXPEhsEIfV85bFVtd6aGUsmeOSuzJiq1sOh7VS+1Aeh90S4KkNZI5Ih28ApcT4IxLxP1HjNaaQi9gbYDXmn8LW0fdjCaTezQzlKAKsygPo0R5zJcp33+aYo0zHCSABY4AnmlQIZ2louJAs0Vgz05GU9NxwD5v+RK10lomy+E/Wtf6J/Zeavh1nC0VWufnBagvjzbbb6yo/D04H2P+yTsY6drTzkXuvamn1qCKjLv/Oee7zYvAQt6FzAG2eUft+RWl3FGIglkfXfcjyY+FZOsMrYlQhOTnuvJbzEgeMDPcgVtjbWQzuOcX0QMEQMXFC64vsPLnMl+3tcCf21ULUQtH6ZnLKIjnxUFKqG5/7z5dB1QTQ/orpmAB6izvHTELVVHUCjkBVJJ9Jk77n6VkjEadomOPIe aL+KuDdo 3pwS38RYEFMgdPi6YIzGvDHeqRy+FZ0201yHb3GrDCSeOmouLy5aFEI1J3P3IQKLFrDV4+hEp4Enx1WpO3TbCFssLWwRfZwcMkF8j5aIl3JpSnNTvHaozF4UUvyrA9Kncb3G3VkhO1xfV7r9mjl5hPJIZvhnEJ6WStq2qQ1v4hGMCLqwWaMrMkPBWUqMZzfllpj5haYBSrYqi6bgvkvL1abFp5p52lrq3aRmIwTVay9Zj4lenhClz67WkqQx69QGD+cVpHhDO8a+6S0KGWYF4sUKPvJv0iVrfFGPs 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 new directory offset helpers don't conform with the convention of getdents() returning no more entries once a directory file descriptor has reached the current end-of-directory. To address this, copy the logic from dcache_readdir() to mark the open directory file descriptor once EOD has been reached. Rewinding resets the mark. Reported-by: Tavian Barnes Closes: https://lore.kernel.org/linux-fsdevel/20231113180616.2831430-1-tavianator@tavianator.com/ Fixes: 6faddda69f62 ("libfs: Add directory operations for stable offsets") Signed-off-by: Chuck Lever --- fs/libfs.c | 16 +++++++++++++--- include/linux/dcache.h | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) Changes since RFC: - Keep file->private_data stable while directory descriptor remains open diff --git a/fs/libfs.c b/fs/libfs.c index e9440d55073c..d4df19b1b666 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -428,7 +428,7 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } -static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx) +static bool offset_iterate_dir(struct inode *inode, struct dir_context *ctx) { struct offset_ctx *so_ctx = inode->i_op->get_offset_ctx(inode); XA_STATE(xas, &so_ctx->xa, ctx->pos); @@ -437,7 +437,7 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx) while (true) { dentry = offset_find_next(&xas); if (!dentry) - break; + return true; if (!offset_dir_emit(ctx, dentry)) { dput(dentry); @@ -447,6 +447,7 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx) dput(dentry); ctx->pos = xas.xa_index + 1; } + return false; } /** @@ -472,6 +473,7 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx) */ static int offset_readdir(struct file *file, struct dir_context *ctx) { + struct dentry *cursor = file->private_data; struct dentry *dir = file->f_path.dentry; lockdep_assert_held(&d_inode(dir)->i_rwsem); @@ -479,11 +481,19 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; - offset_iterate_dir(d_inode(dir), ctx); + if (ctx->pos == 2) + cursor->d_flags &= ~DCACHE_EOD; + else if (cursor->d_flags & DCACHE_EOD) + return 0; + + if (offset_iterate_dir(d_inode(dir), ctx)) + cursor->d_flags |= DCACHE_EOD; return 0; } const struct file_operations simple_offset_dir_operations = { + .open = dcache_dir_open, + .release = dcache_dir_close, .llseek = offset_dir_llseek, .iterate_shared = offset_readdir, .read = generic_read_dir, diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 3da2f0545d5d..ee1757001583 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -208,6 +208,7 @@ struct dentry_operations { #define DCACHE_FALLTHRU 0x01000000 /* Fall through to lower layer */ #define DCACHE_NOKEY_NAME 0x02000000 /* Encrypted name encoded without key */ #define DCACHE_OP_REAL 0x04000000 +#define DCACHE_EOD 0x08000000 /* Reached end-of-directory */ #define DCACHE_PAR_LOOKUP 0x10000000 /* being looked up (with parent locked shared) */ #define DCACHE_DENTRY_CURSOR 0x20000000