From patchwork Tue Nov 26 15:54:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13886136 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 5AC55D3B9AC for ; Tue, 26 Nov 2024 15:55:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01FB46B008C; Tue, 26 Nov 2024 10:55:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9B646B0092; Tue, 26 Nov 2024 10:54:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9E086B0093; Tue, 26 Nov 2024 10:54:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id ADF776B008C for ; Tue, 26 Nov 2024 10:54:59 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 550F1A0754 for ; Tue, 26 Nov 2024 15:54:59 +0000 (UTC) X-FDA: 82828694592.05.D14D434 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf03.hostedemail.com (Postfix) with ESMTP id 9526420006 for ; Tue, 26 Nov 2024 15:54:55 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SsK6NV8j; spf=pass (imf03.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 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=1732636494; 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=4owM0JWNafDjFnRPWWpt2dnmgvCfRcrIWe73NDBkHyw=; b=pxaWPIwfVW2CBwN2NIhIQSvIwrdNlzlVleRgbwv6NagFcidlVL0HdV2qUrJeFVv1TyRWo6 Tx8dm7Mzc+hJ33Y2lvErkgMI5Vxh/kzhGxl5DduheJbky/vxvjs5WjSKYoHbfXb99w6G+f taCDDqzGcYaJp1eldsZo5gxSttZmEOc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732636494; a=rsa-sha256; cv=none; b=rMhyM07zpENn+t8v4lFIUgkUiusxJJc4epsF2zFj6jTRa3Z0+6lBxRpXqUNLSAZaHIrAba 2aee6lGwQqybRZc6C68kpWehkAu4GPZEUBhQ0lx1BvWSDZpj3V+6CgVxzxrG3selAYOLBb jAPg2B4XtQr6jfOSohIInO59X62U74A= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SsK6NV8j; spf=pass (imf03.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 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 nyc.source.kernel.org (Postfix) with ESMTP id 379C6A4080B; Tue, 26 Nov 2024 15:53:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3AA74C4CED3; Tue, 26 Nov 2024 15:54:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732636497; bh=tMz/47mxpsuDcyrl6vBlEBAasdrsXvapQPHriWB0vcU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SsK6NV8jA66kTudkpuY9LMmdM8pQatvG2B2mhSyAac4M/AEOF25/vatqmM9Falwci yj7mYfpwIf2l+hqSnEoF+aP5ks6Sa9qzjw4ccEmVHYRy4GL0VFDkDcsJNEqWotFeud presvLu/aV0ui65cWMC7eg09NrL7cNa2Nq2ua9M6mxyMZYUPfoiJYim0q/GAjg3Md5 VO5R3CDpPk9qijalnRWIXExd8XetVGsblXaetFQ8LqpSTHEuyY/f27mhUzno/q2yQp FjMxtZUy+R4esvZFh/CugOAt1+qwdk6nNVWCiKHQe0RHfsWh95kIV/HLyZBzAJto/H HCyI48l739NTg== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v2 4/5] libfs: Refactor end-of-directory detection for simple_offset directories Date: Tue, 26 Nov 2024 10:54:43 -0500 Message-ID: <20241126155444.2556-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241126155444.2556-1-cel@kernel.org> References: <20241126155444.2556-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: mhw7d77gwsncjyj66jtxc9eh1mf5cez3 X-Rspam-User: X-Rspamd-Queue-Id: 9526420006 X-Rspamd-Server: rspam02 X-HE-Tag: 1732636495-303477 X-HE-Meta: U2FsdGVkX19Yzj2v7dq6gWbmSU6wuV/7uHz2pEQwb3+VYcfNTGjcNR5cZDq/V2xa7PxlPHyyjuHg8LQukO3xVA3Rf0RO6xht6Ts44cm9j4kpQ1Vjb9G48r+bYZBIYrr2XVSwfMyt9tyiS2yFnrS93oq39YtAY5BbQlML9oCAywkojCByzAebHkD03/x8ZxN5D7Iu9WBWc+DBXwZf+otDUwGQ3mAgpQgNbX7XXjQ1DP4ZxeEpH/lWH8aZ6h+WaPhOYURv4y1B/x81XyoaMR21l6vRSIDV8aDdMIhmy0+hZy6CH3HAxtTZ6zuX3vgda/oEVF2m6HqRKBgpEiGe78bKB0v3epoITtuo/jfGXn4wT4ey+aMPuIhpkXE/c8irppBGsKH1XJ/PNeGMoB4QKIwnoesp41Fp6GeaiadLM+ymVHs21sbUAPx83fBE2IowdTPlbI98wnOY43IFzDomni3ofI6fvpCGYWsvGOjOYovkKZ40jbANDmwSOi4fIRk0RBIyO7gqGAGk3TxRUSv/XQqWeiifcuOq6avKPZsOAPTLwVrS/l3GrUoVfHQ2/4uShWzVg2Pzsc0t2er4n19mmMbZFeMp7xjiDOX4sg33Z5GOOEKRd8gLpb9yu5nIG5BQzLTokksmRdnVvv2VomiuC1LdB2BUJFe/Eey2QtmXJID0L1U13S61k33oexvHHuV8XYg3aSE+/dAe+sGxKn+hk2EHrdkUaJ+OQ+YNqW/LpO99x6W5naXN9LO+Y3UasEVw5coiK34CngfQKLxbJosprcVsHrN6J+TcMeu9V/FZI7qjvRwBT16KpK3YNvjF74mT8O7GHdbxT4TJMEoG6rb6MbuadKmAKQHDf+PCjXQpjGyWmdJ3D1MVnY2DyWANuL9z4DyJ0JZ5BxWGBWlpQvJ55u05Idme2vMwVbXlrVIY8jp7dIv2g9kW3l2fV4P30zHSiFoBnQf/VEgOjwbGbJeuoz0 Zo1ETAFG rCxufXDbck3+RFheL5m7xhj9NxO3R/9SkSEKfQae1q6Vki5t4QyXnUHnxCa+W+Vff7UeuB7uZD5KVz5TnbJpc0Gr5DwwKqxBDdBhhxa5qeD4GC80YMz2vVJeINLn/Y216ediGy3BwR69mcuVspQSE3LVz4WDJcdg/QGjxUxA3raTZGPPm90X6lvtHJQ9ZJBq2gIWGZkRtJWRK/2hR+1ClTFXxatq+v3vz/sqRT7LlfeMj6hozwgU7KzJ0vA+KEfnti4R5byCbjSxSAfGEAvvVkpZ0TA== 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 e6c46b13fc71..be641a84047a 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -453,6 +453,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 @@ -478,8 +506,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); } @@ -510,15 +538,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); @@ -528,7 +561,6 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } - return NULL; } /** @@ -561,16 +593,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,