From patchwork Wed Nov 27 15:28: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: 13887146 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 0E4BFD6ACE9 for ; Wed, 27 Nov 2024 15:28:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BBA26B0089; Wed, 27 Nov 2024 10:28:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 640356B0093; Wed, 27 Nov 2024 10:28:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46C3E6B0095; Wed, 27 Nov 2024 10:28:26 -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 1EF0C6B0089 for ; Wed, 27 Nov 2024 10:28:26 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CD6FEA1670 for ; Wed, 27 Nov 2024 15:28:25 +0000 (UTC) X-FDA: 82832256150.17.AE0FBC1 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 3CC20160017 for ; Wed, 27 Nov 2024 15:28:20 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VT08HImq; spf=pass (imf08.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=1732721301; 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=MfHKVWe+Hwnavu/M/uaZW09l68m70KLsIeIu2aj05/c=; b=5Tdai74OPF36z718y93oX2NLIu3Uw1P1vpO6C5k+K9lkgZ0u19nHJ14HLeyJLxT9nn3fWV aCkQaf3eN1adM9ppAhjW6KqcE//FIooUsEkMwPEYvmD/y3csKJLHABDjt5hkXSXfo1cQKY 0Ezq8jmToH6XJtMxCAxXU77K3mQL6i0= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VT08HImq; spf=pass (imf08.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732721301; a=rsa-sha256; cv=none; b=FWd8ZW+HE6vLodOdB6t+1nlTs5gObWajQnUCNP7UtcPtHFYiYCLVzCztFBaRau+UqP0ZMz JR4EInlnTw8YNzJl5F5HrbOmUuyAVL1A7N/Jzn5iNQco4uBK/f6e8BTve0ZaaS7ihtxHku bStE7AIBPYWV9k0zS9CrVPQGxTanE9Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id E63925C3F2C; Wed, 27 Nov 2024 15:27:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A88EC4CED3; Wed, 27 Nov 2024 15:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732721303; bh=yaUgVsYqn/+UqS21Ay0PUdvP+2Ovp6VDQCczLqXNXYo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VT08HImqWvjHVpg0vUPPxkxWgmyqx6FPQSiQJRMNw0IopFVIATgz4coLvIXaQA5w+ HttmvTm9x2ggjv3ICKFLH2h80s+uaVVrnaMJSjwVnsR2/fYo0lVxOcpflRO32MDlHv A22VX+RXRrBbmm3pQFuLEaAmcJDB4WsVuQQUMC2PBkwbOD/R2h8tkSbikpz8uryCiI MAOIYikg4eQqyojGl+XUy2EA1W/0bAEc4kVDCJtU3EdmX6y+fDI28xs4E9sT8iNdKn JfH0jERkX0EQp1goCDny+sYUW9vwUuytQPtbRZbr5myfCmnE/xuWTfyt5dnYfvMqJW r9ZGIH9IRFzqQ== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v3 4/5] libfs: Refactor end-of-directory detection for simple_offset directories Date: Wed, 27 Nov 2024 10:28:14 -0500 Message-ID: <20241127152815.151781-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241127152815.151781-1-cel@kernel.org> References: <20241127152815.151781-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 3CC20160017 X-Stat-Signature: ct8d63i55yzqcmzxihj45h64chpmxwns X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1732721300-816581 X-HE-Meta: U2FsdGVkX1/2uzoSwUwoiKg0lr+ysw61GJpPyzcMGYIryifk7HQ51L50Hubuv1t6ZkAPTiy2rfgGsggP9voER5yd2/WeSO7l0fKUA0sE2qCOzMVVgvwe98/Va8Pd5DpnIbesOKxQZds9TFE7tjxOd6TAgms7BEtd8XXQMxoVqiat0loIc8HRmrO/UBXUbnKBL2jedt/xY1cRw+KzJYc4dXoDG9o0yH0NJc/nAKDnKeyWlmIOfxijUHXAhvNvJcKYkzeXUGW1SJlLp3u8VqdnniZs3Es26Kko/xRrOrLviAg7h5ezjWdT5DH8VCXWyG0ZW31lazv1Lq+eUltUqe+elGTRTxKuJlBQtEbjuBMtciofUpgJ/wZThQ51FB0U7NK0k4XCuwveq+sutb29yp2CqSmhRnCxmZRNQyMmOOBpJMjK75/yZJrPvUagYjmduAhJEAMlCkATbgaPctsKvG/fVD0ixT82RE5glXSEpoqHrolBMqgDWKiQb5zCvLEJrMMSgficEtfRaLnRsM9glN3p9AkQ7GjaZeKDoAVsVfjfUq4VQq+YDonQxfX65sbGfWJKk8huqUIV9LEEDquVopF89O5lA6VPTs3XLHaizoitGInroCO1Rktl4rUGKT5xZsdPltt1LWjQGItKCg2K+9Md3lz9ZY1n6Br1FXKM/wqEGRDeILf2qaSwdUVc0xP6OOiNKy2n8KQOT4LXmQpFEA0zK3VnNuCsR1rIP3/7CVqb62JI20icF/RRK0UZUVE5S6VALKR87eiAOyoU38PIDdJnwb8CYWiB6JFN7xdoNHUXgM/mm33of841c6TsvNnVXd2LuZ5X+B+/ke/NCbhk4NUTHTl1mJH3hmHA5RJc/hhzBPB7Msh9quLFmB8l+sT1aIgXa50jCPfg/Wx+d+ohXZQ6bmVqtL47Sdi7crksnA0V9y+nnEW6Kn28s44fJPwzx82CSg/8vqShZQKIQ1XnBkL ktOS18xw CwU1DESiBNBAcj2K866gX9fUFeS5z+7DyeB9gmYUSJrDxsvGxJ8uK6KU8x7shGaXPHPEgKVWkEssz7gFrzuCfaPstj6ZwdA+fMAndZo6aQiewVbbFN2+GERfqFriSRsl/Cq7+WEExtuSuPt1TC8tDDb8yBfyWafi6mBy+CNQbZHHxYGuU8T3oRSOEB6iz0weW2+1GblxABT8K5dykVSIGKpl+zxr8uLtcBcri5eqTyTIc0VGGWdszdQvteuq8PumrsAFTlcO9IMuK5Co6W137rHDimQ== 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 This mechanism seems have been misunderstood more than once. Make the code more self-documentary. Signed-off-by: Chuck Lever --- fs/libfs.c | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index a673427d3416..0deff5390abb 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -449,6 +449,34 @@ void simple_offset_destroy(struct offset_ctx *octx) mtree_destroy(&octx->mt); } +static void offset_set_eod(struct file *file) +{ + file->private_data = ERR_PTR(-ENOENT); +} + +static void offset_clear_eod(struct file *file) +{ + file->private_data = NULL; +} + +static bool offset_at_eod(struct file *file) +{ + return file->private_data == ERR_PTR(-ENOENT); +} + +/** + * offset_dir_open - Open a directory descriptor + * @inode: directory to be opened + * @file: struct file to instantiate + * + * Returns zero on success, or a negative errno value. + */ +static int offset_dir_open(struct inode *inode, struct file *file) +{ + offset_clear_eod(file); + return 0; +} + /** * offset_dir_llseek - Advance the read position of a directory descriptor * @file: an open directory whose position is to be updated @@ -474,8 +502,8 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return -EINVAL; } - /* In this case, ->private_data is protected by f_pos_lock */ - file->private_data = NULL; + /* ->private_data is protected by f_pos_lock */ + offset_clear_eod(file); return vfs_setpos(file, offset, LONG_MAX); } @@ -506,15 +534,20 @@ 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 void offset_iterate_dir(struct file *file, struct dir_context *ctx) { + struct dentry *dir = file->f_path.dentry; + struct inode *inode = d_inode(dir); struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; while (true) { dentry = offset_find_next(octx, ctx->pos); - if (!dentry) - return ERR_PTR(-ENOENT); + if (!dentry) { + /* ->private_data is protected by f_pos_lock */ + offset_set_eod(file); + return; + } if (!offset_dir_emit(ctx, dentry)) { dput(dentry); @@ -524,7 +557,6 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } - return NULL; } /** @@ -557,16 +589,14 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; - /* In this case, ->private_data is protected by f_pos_lock */ - if (ctx->pos == DIR_OFFSET_MIN) - file->private_data = NULL; - else if (file->private_data == ERR_PTR(-ENOENT)) - return 0; - file->private_data = offset_iterate_dir(d_inode(dir), ctx); + /* ->private_data is protected by f_pos_lock */ + if (!offset_at_eod(file)) + offset_iterate_dir(file, ctx); return 0; } const struct file_operations simple_offset_dir_operations = { + .open = offset_dir_open, .llseek = offset_dir_llseek, .iterate_shared = offset_readdir, .read = generic_read_dir,