From patchwork Sat Dec 28 17:55:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13922482 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 29814E7718F for ; Sat, 28 Dec 2024 17:55:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B2296B0089; Sat, 28 Dec 2024 12:55:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5ED2E6B008A; Sat, 28 Dec 2024 12:55:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 418A46B008C; Sat, 28 Dec 2024 12:55:35 -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 209136B0089 for ; Sat, 28 Dec 2024 12:55:35 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CC91C45B9E for ; Sat, 28 Dec 2024 17:55:34 +0000 (UTC) X-FDA: 82945118886.22.C52587D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf17.hostedemail.com (Postfix) with ESMTP id 5581840019 for ; Sat, 28 Dec 2024 17:54:59 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RlmhKUWi; spf=pass (imf17.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=1735408512; 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=onYsBNYTJ3eFnLyQd/69vKumzVxyQk76hPC035yJM8A=; b=Hchbm9Aex1Y9x92A9bckihYeFP9WExdVg0ErxAclCknJdJ/Sp8/0z8p/jtWVNN3Iakv4uy EuvOYnGL2egFuKSbZXrASdZlQPHKvt5EIzI93fnyKjdzDmj4ZPjQA7zpwS+NnQLlB5nvku iHm1fQPoJh1oxHX32iTD/47wQghcsOo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735408512; a=rsa-sha256; cv=none; b=vzos1s3TPAFfokmhU5Cdzue3/4qH4Dkn8fZZwI9x9pWAWt2ty7m0l6HtgpZtpeIvGhyRkD Gj5nMqfU0wZOOuw/HLFDHTZNpI42S3DVr2X5nLB172VojTAKD+MXqXB0c3WFpixjyitjjF b5stg4S3OMQMiaZBT6HkzOy6LP2bTFE= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RlmhKUWi; spf=pass (imf17.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 CCB485C550A; Sat, 28 Dec 2024 17:54:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A20CC4CED4; Sat, 28 Dec 2024 17:55:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735408532; bh=XpZx49NMKe8Iab6ZNIPRaujrlYKZPG1faqm8qgLlKjk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RlmhKUWismY/UYg2iYq0gl8G+wL6aSxsW4j5MteG3t0++FvwK67M5HjF2fdQFlb6K q+ZLp+37FC0TRGRnrHVBwiDqrmsC/oMc8YUL4z0G8tOKyDhBX0Zh+0wqdGiV+A7IQs t2ErUSTK9rMCU4vEKfzjVnA3dEVrS+diNvrcSlmB5K3ZxE4Vf1CoKKhYM4RJoUnqiy qtwwVK9IxUnoETpTmre8bURLJBdfKOlfRI8Dg+xGGOSXZG6iHZ+TEapEz7Fq4yr8dZ igQ4ZRIr8b/6ME+U9NKUkonSJ/vDmDEe69NVgTJnpvre5YSr5E+O4T5627WZZYFaQ0 zLTWOBG/jWtXw== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Liam.Howlett@oracle.com, Chuck Lever Subject: [PATCH v7 4/5] libfs: Replace simple_offset end-of-directory detection Date: Sat, 28 Dec 2024 12:55:20 -0500 Message-ID: <20241228175522.1854234-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241228175522.1854234-1-cel@kernel.org> References: <20241228175522.1854234-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 5581840019 X-Stat-Signature: nr9wzfxootb5pk7q4cffyu5p8dh6xokg X-Rspam-User: X-HE-Tag: 1735408499-350572 X-HE-Meta: U2FsdGVkX18Z/NuDOfz/XA8oDBwJd1L8QEzi0aKXBckfzaH0BKj0YXT7KylpP7JbcwlLskLfa5I5xbw24J3Ua8NgGFQpCchG2TezjLYEbPVCMorctTSIgud7VUmbIOF4KHz2XfoW5MJmzkayRAOZmmikX9tAXIcks0G841e0S4YtkM2zchf+m3G6o8iiO9vPTaMlD81Wy68wqDMgdyRvcmvAipH3/lb5FwFzm1HVpXc0keVL5KqmHBypkAt51b6nYD/2Vd/MFnAwtWhmekBzAYmTNvXvfyUrL2n9rXGxPwySaNMn6JSoxJFMOtMZqMtg3Su1hyfOhlzwBUs4se6ggVC4eN2Q3D9St3PZWrzvsd//54DXJO/43ARTRyY6/SjB9H9YERsrROzQLak1MX4o0iUNQbuUHZcLS8u8Yl5O9jzxQAqu72Ta2UrBdxHmRRmAIV933deWsYCo7TNN3RuH+Fiy9a78Oyt0zDMb869qeDAWZAxZkUUnf7v2d/U82Y58/5uBtoMU27Yj783/1LxjEA9rtwNPyciGY6/wz5rzvEFaqg+ysV3+DkfrWzC+juk5DfwBZLjgj1xjdYXdrvG4T5/FdYPM2kOIBdU1PvwD1CLxQ1BoePbiRc2JlN9naldMWBgcJVTtTte5fb5Y5eK0DfuzZrDnZ1/NJAURsKSbTfX4pZb4yUZS5sKfaGzngp47MJ+uYtmJPq20gWt3tjXCll2+bhXuxtCISu8oaWpwhqn1Hc2ZJRYSF06oMrAZ0MhOwAg+KDC7VkpkkWmXL/Eotu27Axo3/loQot8GAiNdYt6/Vmw6MD7PLpx7RgQHX4XmyM6Sz4Wa9tYJnqOrdm62Lej6YrJP6hb2UqEFbff1o8aV80Gu2HxEedc0YhhsF/oq04kVLYpHtfXT/ccld1Yaiio9u1YlQ+bpGVWIqJJvVdc8Iz0h1Iq2sduOb9oJ7ehqIOpmaDpSZ+NykPCwjIT 3fcIsOjj WdzOR6LJs0YPSr+8bCG5qAQk9XHMOHY/NFecqvJPyURWoWf7I68IrlUftEUBywhM0SV4fm0nLOB4sW7ZTKbkiCxwCHdAeNuh8zq/42h6dPiC2rzZg1CdDFuREki6z+EGtL1JrzxtnwFRKiom5teZtOC06kd+AesF309oBUh8gl37Ku9UTRsTHuf+vyUJzYGAJOhGxWCnM1g9ZpIFKxIZu8IC8i8e/FGNRXsUAhg19O8L7oGsjgpLASX9VOnbI+KLZhUFsYGreA/Xx0vQH5jdGJ4EvnWN1JiqxIQQC9c+8tXIUxgFY4ZgD7+5IS7cyeH4zXDSC 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 valid 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). For broad compatibility with 32-bit user space, the largest tmpfs directory cookie value is now S32_MAX. Fixes: 796432efab1e ("libfs: getdents() should return 0 after reaching EOD") Signed-off-by: Chuck Lever --- fs/libfs.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 8c9364a0174c..47399f90511a 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -245,9 +245,15 @@ 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() never assigns these to a dentry */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_EOD = S32_MAX, +}; + +/* simple_offset_add() allocation range */ +enum { + DIR_OFFSET_MIN = 2, + DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1, }; static void offset_set(struct dentry *dentry, long offset) @@ -291,7 +297,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 +454,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 +463,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 +484,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 +492,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 +502,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 +525,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 +539,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; }