From patchwork Fri Dec 20 15:33:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13916890 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 F03A2E7718C for ; Fri, 20 Dec 2024 15:33:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41A496B008A; Fri, 20 Dec 2024 10:33:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A4286B008C; Fri, 20 Dec 2024 10:33:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21D506B0092; Fri, 20 Dec 2024 10:33:25 -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 E4CE36B008A for ; Fri, 20 Dec 2024 10:33:24 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 96B3CAF9C2 for ; Fri, 20 Dec 2024 15:33:24 +0000 (UTC) X-FDA: 82915730688.15.7C65FC1 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf03.hostedemail.com (Postfix) with ESMTP id 48F9120003 for ; Fri, 20 Dec 2024 15:33:07 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aj8RMztE; spf=pass (imf03.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=1734708786; 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=lSJKQihNyW88qfaDal04lqiqUo1bye99I44K3+UECXQ=; b=exGqeM70TKV2q9vcaEPR1+rsOxxIqJj3/IUDNeQPiEC/9cHJoJHZDHXNUdPbcWaMcBJVGr tI58zB4TH3gKq5M/nJMISKw8NxgNREUIwUjzbu9P6P9rGtYNQ+Typ9jdnxdu32a7MzmMlx ORQInVAdypw11F2GvYdE2LcRrtALKvU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734708786; a=rsa-sha256; cv=none; b=KWGpDGqkdEXs29gwragGDSHv1fLVqJTgmhunu3Jrp1C2Qe0GaVkjN2r2Rl7CDcTFr1NyHu ScabIn86jKjq2LZVY4u+f84NO+ti4Je9es2cfJpSrEYecBt/Dg/WAc/oO2NOI6IP7cYd7n 5XSlNF1RQVk2qTEeb+QMe9CrHV6nKHM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aj8RMztE; spf=pass (imf03.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 172355C65DE; Fri, 20 Dec 2024 15:32:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 223FCC4CEDD; Fri, 20 Dec 2024 15:33:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734708801; bh=vLEn4J1l626ylhS0DfsGUzEUbw5fV8eKjCFu5JUH/qI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aj8RMztE980ugTjA7NrNCBFI7OOZYEE+7YN6DXqfOHNsiuWkl7FevtLfS2hmNamXC 9WxbM/JoYH9wxSD1GHxwXX1kvGAt8wUfYtZdIG4bVK5dDU8NT9bTYCZNs4FaJE5Msr QC7JW44DF595w6Bf/lA8JQS5LDIbMxpaNoBnZj1P7qNYZAIMS1rwmGPWs9X2U/YCJ8 ys4OS56o4iLffoxTFSdFI1UjHNhDmF3pbzfbJUAxkvAP2u3zN3vTK32hIKAItNxgdY YoQA77Wts8jmUD421POPc9qp3GSrdrRXUvGVeSVSh06rg11gF2xFG3CgSA1eGsxH67 3We8dzOSubMlg== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v6 4/5] libfs: Replace simple_offset end-of-directory detection Date: Fri, 20 Dec 2024 10:33:13 -0500 Message-ID: <20241220153314.5237-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241220153314.5237-1-cel@kernel.org> References: <20241220153314.5237-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 48F9120003 X-Stat-Signature: h4c579ffy9xo8136xz4bdta1sq9f95py X-Rspam-User: X-HE-Tag: 1734708787-829067 X-HE-Meta: U2FsdGVkX18lWimbD5397JK7yf1x8WFQtiYfx+tEbdkP16ACJZiOVArv0Qdcje9Sr9HHu8ilDPweLouhxVgygn+4hAzrS0ROdlmG5NJ0CiRsUIaHXb2tQu3b9e6QM4JveYk4mBxfsuMxlkPQFibTWpQJhCWdq8/vrtCgwdM4c0o27BWEyVXqYj01Q82VdPp00UykGKjVK33tZ69jlEUFWE2uVMhWLKcfgK18iTtcSz2Uovryiim/kvjjxxZQbbAcMEolSTbaGBYoS4gil+YER5XqColqaC+dk0C7dYBFQmfRVcksmjrptDvFkkXc6J7vgliOELSDYvivi5GQMk355iKbPTPySP9JaTYY20/kO5iEZwDfpmfKBRpUJ3e8ZdxmIafFdAUBK/wUhgsUN5OBeOcuIz3rIv9IcbMibpwDQR3ya2gaBz3U6+zLj5RPrmO4QMfoUjyWypszGwGje47+zPt3vILPy6Yrh+nnYN3lTngAYAohd3kHR6oW03KLdBUbWCl/SOXFbBA7GM9Qke+kW1KEPpBlZu6yi50bfT2K/bpSF5LEO7Ur93BzF3Tm0X84cdjYteBXRO/xbFb4HCrBDCEm/N0IQFvPGdW2CamyBhaUnXRhG21YK06eHjzRUur104YTQKEqf13vB2p8twXa8jK/JHtm6QtAk9pvxOfoBAj5b/rd0Y6Fguw74F/QzFbNLameBk9bauPdY2SnEV/yhnbazt4WqMeUQtKW98aksINA+4TYbBJ8K7N1HoJB/Z310dXP75alEME45m6Mzdjvga2a2XKB2gWkx4ZExAs9a2uPRYZ7fLfwvWcf9Dg/sDgtyKfacpFYuPymZOi0jdHM4YbO+cqXYMly+UTh0xa0KqyBLwkH803IBjx/7PwYgH23WAG+mM9N1Chl6gif2OmMg2SwnAXW6wfDgCGksi6mcMAJttGPrkFUCHGNs3O7V5cjVmTEp5WdNp3J73jb1fb zIpEI3Uk l8avwlzYePgTxIkE74etgfJ6nKnjWLBrFYchljrCIQclZudxH4W5nH04F0jhvpCjrB3Y4DmE07o7GUVFwzKozVOB2uVXJ+xZ74pJc503FeFlkmEo53dD6XLv7DbkpVxhW/dDNJGq/dEdWglLulL4igXNk/wK8k6Hs5GWMDk2CuDdTeiMMHFzvXeJitgyLhTVb4YuKEtEYCgffzYYrsB4XpANbE2O57pqywNTJLF+84M3ALtnFmykP/YZI1qMCljrooFLVeMQsr0q1uNgS3+f7oUOVmwo+iDs6dl21vYJbek9oGWhij10wIG/6LQV+WAhI+Dog 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 According to getdents(3), the d_off field in each returned directory entry points to the next entry in the directory. The d_off field in the last returned entry in the readdir buffer must contain a valid offset value, but if it points to an actual directory entry, then readdir/getdents can loop. This patch introduces a specific fixed offset value that is placed in the d_off field of the last entry in a directory. Some user space applications assume that the EOD offset value is larger than the offsets of real directory entries, so the largest possible offset value is reserved for this purpose. This new value is never allocated by simple_offset_add(). When ->iterate_dir() returns, getdents{64} inserts the ctx->pos value into the d_off field of the last valid entry in the readdir buffer. When it hits EOD, offset_readdir() sets ctx->pos to the EOD offset value so the last entry is updated to point to the EOD marker. When trying to read the entry at the EOD offset, offset_readdir() terminates immediately. It is worth noting that using a Maple tree for directory offset value allocation does not guarantee a 63-bit range of values -- on platforms where "long" is a 32-bit type, the directory offset value range is still 0..(2^31 - 1). Fixes: 796432efab1e ("libfs: getdents() should return 0 after reaching EOD") Signed-off-by: Chuck Lever --- fs/libfs.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 8c9364a0174c..5c56783c03a5 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -245,9 +245,16 @@ const struct inode_operations simple_dir_inode_operations = { }; EXPORT_SYMBOL(simple_dir_inode_operations); -/* 0 is '.', 1 is '..', so always start with offset 2 or more */ +/* simple_offset_add() allocation range */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_MIN = 2, + DIR_OFFSET_MAX = LONG_MAX - 1, +}; + +/* simple_offset_add() never assigns these to a dentry */ +enum { + DIR_OFFSET_EOD = LONG_MAX, /* Marks EOD */ + }; static void offset_set(struct dentry *dentry, long offset) @@ -291,7 +298,8 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) return -EBUSY; ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN, - LONG_MAX, &octx->next_offset, GFP_KERNEL); + DIR_OFFSET_MAX, &octx->next_offset, + GFP_KERNEL); if (unlikely(ret < 0)) return ret == -EBUSY ? -ENOSPC : ret; @@ -447,8 +455,6 @@ 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; return vfs_setpos(file, offset, LONG_MAX); } @@ -458,7 +464,7 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) struct dentry *child, *found = NULL; rcu_read_lock(); - child = mas_find(&mas, LONG_MAX); + child = mas_find(&mas, DIR_OFFSET_MAX); if (!child) goto out; spin_lock(&child->d_lock); @@ -479,7 +485,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 void offset_iterate_dir(struct inode *inode, struct dir_context *ctx) { struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; @@ -487,7 +493,7 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) while (true) { dentry = offset_find_next(octx, ctx->pos); if (!dentry) - return ERR_PTR(-ENOENT); + goto out_eod; if (!offset_dir_emit(ctx, dentry)) { dput(dentry); @@ -497,7 +503,10 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } - return NULL; + return; + +out_eod: + ctx->pos = DIR_OFFSET_EOD; } /** @@ -517,6 +526,8 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) * * On return, @ctx->pos contains an offset that will read the next entry * in this directory when offset_readdir() is called again with @ctx. + * Caller places this value in the d_off field of the last entry in the + * user's buffer. * * Return values: * %0 - Complete @@ -529,13 +540,8 @@ 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); + if (ctx->pos != DIR_OFFSET_EOD) + offset_iterate_dir(d_inode(dir), ctx); return 0; }