From patchwork Sat Dec 28 17:55:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13922473 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 412EA194ACC; Sat, 28 Dec 2024 17:55:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408529; cv=none; b=CqdVBIhM5SX0Fnw/dgO2DLnlIdu7V3ALS+HJ6SKRXV9Y+vMx63wB4gMOOy6CWcdSTiNXK1IObKmr0tv3SldOh/CA8S1aHV3cctBXx4iC9SK0k2vesOwC3OQ5jJMoik+IPa5F7Rei6sQre3EENiz8rfNPQYw2TeZub/WbAlgKvqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408529; c=relaxed/simple; bh=EECtu5EOQPP9WpkyAeyEzP3b+8wuy+Niq71T6d9EZWc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VH8/bKV/WGZELIKfOSElj16aHhC6Arsb7QALTFrynF8PhxolvCyQ22YNyYeFu1HH3CILHq3huwe+KI/5zFzE6Dhc4FldTySwpdzggDr4hz05vh1Zv8UWv1E+fIVR/GbMSLASQVnZzgXCDAI5DjxrQV5+x6j6owZZtiKPHk0mZ4c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Uma4kQ2k; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Uma4kQ2k" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0B35BC4CED3; Sat, 28 Dec 2024 17:55:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735408529; bh=EECtu5EOQPP9WpkyAeyEzP3b+8wuy+Niq71T6d9EZWc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Uma4kQ2kNj/TOCp8GofUQATKtCXg9pKPiiaPxerbHBR1J1MtjzVlVou7vExRGYGtA 65BSwylOREkYANw6gZKPDWebHMXN4k54oQ/kIWEaY/Tgi6t6zOjpKC/K5hq/JaKl8G WJ3Fq5btLxjxUIm9QXgeg6JEOg3z1AHfae9l+B9RTK2SGB3zJQUQNpb0OZQsq/iQ3r BjoMXzIKW60jDKbAY5trHgHFPHh6VVByIH3v5JCNoZrhwJDCuOnSTjBmT3hCC1c16T AhH8e3ewz2IJ54b5YjFJ1RfSnphPqXvPxHQkWYWOq9A4kLiOB/+TecXx07wZl7E7Fb FasZ5Fxn2KSyg== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Liam.Howlett@oracle.com, Chuck Lever , stable@vger.kernel.org, Jeff Layton , Yang Erkun Subject: [PATCH v7 1/5] libfs: Return ENOSPC when the directory offset range is exhausted Date: Sat, 28 Dec 2024 12:55:17 -0500 Message-ID: <20241228175522.1854234-2-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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Chuck Lever Testing shows that the EBUSY error return from mtree_alloc_cyclic() leaks into user space. The ERRORS section of "man creat(2)" says: > EBUSY O_EXCL was specified in flags and pathname refers > to a block device that is in use by the system > (e.g., it is mounted). ENOSPC is closer to what applications expect in this situation. Note that the normal range of simple directory offset values is 2..2^63, so hitting this error is going to be rare to impossible. Fixes: 6faddda69f62 ("libfs: Add directory operations for stable offsets") Cc: # v6.9+ Reviewed-by: Jeff Layton Reviewed-by: Yang Erkun Signed-off-by: Chuck Lever --- fs/libfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 748ac5923154..3da58a92f48f 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -292,8 +292,8 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN, LONG_MAX, &octx->next_offset, GFP_KERNEL); - if (ret < 0) - return ret; + if (unlikely(ret < 0)) + return ret == -EBUSY ? -ENOSPC : ret; offset_set(dentry, offset); return 0; From patchwork Sat Dec 28 17:55:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13922474 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98556194ACC for ; Sat, 28 Dec 2024 17:55:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408530; cv=none; b=IacxErluOiQUzNQqio5MGtnFs7lGHTmCmmhW9IU25XON1ZrF3yclvKFHYCU6tX85lqrhW9Wkhrzinnvsl5gbz6SxGb0l6ato2EijN7tCSq5OTZ0ZXW25lOrPYir/W1UpLRNEtAlp/cuKjx5z9pVof7/2eZHrRWRm/MDEggwgWKA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408530; c=relaxed/simple; bh=WTZSoNUmlpCr3xI8k1Ogjd81zfFoaOgkluDOdOMlomc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Og+sLTGcZ3aYgqz8bm5Syu3EIH5IA18LVMNlmxu5lkt/oJV890yiisHP5RwQCDvBEmAr02Km7oImO7GVg2y8bQ/8kppGcnHa7fZWJIBDmrUVgA0ClxG4oQD0pGiMXb7tFRPNYObkIrxI1yYrz/tAh0DdJDehUnbxyrdfPR6ROtU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QkZyB+e+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QkZyB+e+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5054DC4CED4; Sat, 28 Dec 2024 17:55:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735408530; bh=WTZSoNUmlpCr3xI8k1Ogjd81zfFoaOgkluDOdOMlomc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QkZyB+e+oXkkoiHfk2e90Exd3floAYt11DNoTBTvESvt94QQxkC2Teg8+ggq+xFly hh6n3Io4qJeO2eJVX5/oiBoIaxWwI02BBTFu5hPJ9BIyXL5O7WhzGYEuAG5/7DjY3g 00G1EdCX5yStiA+UmPqSLYUoZhviIviS79CEIjDM8eXfWkIhl3QZuys+KfLNEKduK5 r7pXgWrri3WFqa68vMdNuqQyRDNbj/P1mL2CpqIKBpOR9iVmY2GyKq6LW/KawYnc2q x9NRV17i8nJIEKHRy990w/ibPDPP0H73lUGAZxny5q4qBC7LIs3xDnfBu4mt7EJs9o UaUDHrVVugSsg== 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 2/5] Revert "libfs: Add simple_offset_empty()" Date: Sat, 28 Dec 2024 12:55:18 -0500 Message-ID: <20241228175522.1854234-3-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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Chuck Lever simple_empty() and simple_offset_empty() perform the same task. The latter's use as a canary to find bugs has not found any new issues. A subsequent patch will remove the use of the mtree for iterating directory contents, so revert back to using a similar mechanism for determining whether a directory is indeed empty. Only one such mechanism is ever needed. Signed-off-by: Chuck Lever --- fs/libfs.c | 32 -------------------------------- include/linux/fs.h | 1 - mm/shmem.c | 4 ++-- 3 files changed, 2 insertions(+), 35 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 3da58a92f48f..8380d9314ebd 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -329,38 +329,6 @@ void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry) offset_set(dentry, 0); } -/** - * simple_offset_empty - Check if a dentry can be unlinked - * @dentry: dentry to be tested - * - * Returns 0 if @dentry is a non-empty directory; otherwise returns 1. - */ -int simple_offset_empty(struct dentry *dentry) -{ - struct inode *inode = d_inode(dentry); - struct offset_ctx *octx; - struct dentry *child; - unsigned long index; - int ret = 1; - - if (!inode || !S_ISDIR(inode->i_mode)) - return ret; - - index = DIR_OFFSET_MIN; - octx = inode->i_op->get_offset_ctx(inode); - mt_for_each(&octx->mt, child, index, LONG_MAX) { - spin_lock(&child->d_lock); - if (simple_positive(child)) { - spin_unlock(&child->d_lock); - ret = 0; - break; - } - spin_unlock(&child->d_lock); - } - - return ret; -} - /** * simple_offset_rename - handle directory offsets for rename * @old_dir: parent directory of source entry diff --git a/include/linux/fs.h b/include/linux/fs.h index 7e29433c5ecc..f7efc6866ebc 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3468,7 +3468,6 @@ struct offset_ctx { void simple_offset_init(struct offset_ctx *octx); int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry); void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry); -int simple_offset_empty(struct dentry *dentry); int simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry); int simple_offset_rename_exchange(struct inode *old_dir, diff --git a/mm/shmem.c b/mm/shmem.c index f6fb053ac50d..fe5613a5ae20 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3820,7 +3820,7 @@ static int shmem_unlink(struct inode *dir, struct dentry *dentry) static int shmem_rmdir(struct inode *dir, struct dentry *dentry) { - if (!simple_offset_empty(dentry)) + if (!simple_empty(dentry)) return -ENOTEMPTY; drop_nlink(d_inode(dentry)); @@ -3877,7 +3877,7 @@ static int shmem_rename2(struct mnt_idmap *idmap, return simple_offset_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); - if (!simple_offset_empty(new_dentry)) + if (!simple_empty(new_dentry)) return -ENOTEMPTY; if (flags & RENAME_WHITEOUT) { From patchwork Sat Dec 28 17:55:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13922475 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56B9C198833 for ; Sat, 28 Dec 2024 17:55:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408531; cv=none; b=Yfqe+x4+kSmg06ujIo4D1YSlVspFYQkjreiMuyoT16qaFeE+XNaJYYMnLj10v2162hlRxoQrZoRU8snBLxP++oQwga4iZ5uutd/yNcCVgkMU7Kn6MzCMixZELedXEduaJT8FTJknsKLtG81ZaROKDR34TJ0GmhoI1XRqnNkEZwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408531; c=relaxed/simple; bh=44QvGk8cIKJkG2AbH47kX36uIs9FlMTaiShWTtz/ZcE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pkoCNwCWgFA1x++IDz/cRO+C2FkX8mG7YMI1DnZrZL8Z0woEfJVB1A7b53XiIO8x8OLIZZ5aEZqV6k+j052iQsx+mWUF7n9odRbyYDEYllIn0tPUruPbBxr4d4dDT6V6CxvD7VsZP9odWthHaVvFEqoyKe/eFR6N41U2ksUdY/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aGHkO4sy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="aGHkO4sy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E53DC4CEDD; Sat, 28 Dec 2024 17:55:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735408531; bh=44QvGk8cIKJkG2AbH47kX36uIs9FlMTaiShWTtz/ZcE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aGHkO4sy4HrHvpB8SLIsBftKGlob3Y95jIV20+LqDjkuubgCgBp8qFAcAK5Fyj8G7 brz14rgKpeBEykZnI3EHT6iMSv2V5q/3SB7jCob0Bo64VO5FG1UmkL9Mq8mBRdnslm LTd6np81J77a2AJ2+9DlXJSAQiKQehAT/UxsopL35XOJ4iCJD7gyfwKJpfLq2NUMX3 /EdIhLT6f2fKRwpfPRC8USHkCUXZoI92E6Oj/VKi/9iAg5E0qJ5QQoEo/CU5PNtoIy CYqin619Ts4VWpGorEiJiKrlXr8Aac9zFI+VLdJr7zTo8JrOSijNrppBVh9usbkXTL BWQ+I5KvQXstg== 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 3/5] Revert "libfs: fix infinite directory reads for offset dir" Date: Sat, 28 Dec 2024 12:55:19 -0500 Message-ID: <20241228175522.1854234-4-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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Chuck Lever The current directory offset allocator (based on mtree_alloc_cyclic) stores the next offset value to return in octx->next_offset. This mechanism typically returns values that increase monotonically over time. Eventually, though, the newly allocated offset value wraps back to a low number (say, 2) which is smaller than other already- allocated offset values. Yu Kuai reports that, after commit 64a7ce76fb90 ("libfs: fix infinite directory reads for offset dir"), if a directory's offset allocator wraps, existing entries are no longer visible via readdir/getdents because offset_readdir() stops listing entries once an entry's offset is larger than octx->next_offset. These entries vanish persistently -- they can be looked up, but will never again appear in readdir(3) output. The reason for this is that the commit treats directory offsets as monotonically increasing integer values rather than opaque cookies, and introduces this comparison: if (dentry2offset(dentry) >= last_index) { On 64-bit platforms, the directory offset value upper bound is 2^63 - 1. Directory offsets will monotonically increase for millions of years without wrapping. On 32-bit platforms, however, LONG_MAX is 2^31 - 1. The allocator can wrap after only a few weeks (at worst). Revert commit 64a7ce76fb90 ("libfs: fix infinite directory reads for offset dir") to prepare for a fix that can work properly on 32-bit systems and might apply to recent LTS kernels where shmem employs the simple_offset mechanism. Reported-by: Yu Kuai Signed-off-by: Chuck Lever --- fs/libfs.c | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 8380d9314ebd..8c9364a0174c 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -422,14 +422,6 @@ void simple_offset_destroy(struct offset_ctx *octx) mtree_destroy(&octx->mt); } -static int offset_dir_open(struct inode *inode, struct file *file) -{ - struct offset_ctx *ctx = inode->i_op->get_offset_ctx(inode); - - file->private_data = (void *)ctx->next_offset; - return 0; -} - /** * offset_dir_llseek - Advance the read position of a directory descriptor * @file: an open directory whose position is to be updated @@ -443,9 +435,6 @@ static int offset_dir_open(struct inode *inode, struct file *file) */ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) { - struct inode *inode = file->f_inode; - struct offset_ctx *ctx = inode->i_op->get_offset_ctx(inode); - switch (whence) { case SEEK_CUR: offset += file->f_pos; @@ -459,8 +448,7 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) } /* In this case, ->private_data is protected by f_pos_lock */ - if (!offset) - file->private_data = (void *)ctx->next_offset; + file->private_data = NULL; return vfs_setpos(file, offset, LONG_MAX); } @@ -491,7 +479,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, long last_index) +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; @@ -499,21 +487,17 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, lon while (true) { dentry = offset_find_next(octx, ctx->pos); if (!dentry) - return; - - if (dentry2offset(dentry) >= last_index) { - dput(dentry); - return; - } + return ERR_PTR(-ENOENT); if (!offset_dir_emit(ctx, dentry)) { dput(dentry); - return; + break; } ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } + return NULL; } /** @@ -540,19 +524,22 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, lon static int offset_readdir(struct file *file, struct dir_context *ctx) { struct dentry *dir = file->f_path.dentry; - long last_index = (long)file->private_data; lockdep_assert_held(&d_inode(dir)->i_rwsem); if (!dir_emit_dots(file, ctx)) return 0; - offset_iterate_dir(d_inode(dir), ctx, last_index); + /* 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); 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, 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: 13922476 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A12F2198833 for ; Sat, 28 Dec 2024 17:55:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408532; cv=none; b=A2cRWeBpqkilX09xaR8j3Y3PTeAIuV2pr2grJvUmkshMMR4RugP04AprTlIn7j2Spbb2cSSoi6Zolgno45ZN2+z8xla/wutXY+oVTCzShJ9ODaDkt/mrXLJWEUvmkqxJIo3Rz1ftm2ajor9036IBMxo5Ik8yZKYKN2w5VFH/1NA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408532; c=relaxed/simple; bh=XpZx49NMKe8Iab6ZNIPRaujrlYKZPG1faqm8qgLlKjk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OSN7gBg8A8lzeLKM6yandgmwPHKPS4x90mvNj7qI8nt9ZEIrd7HO+k4CIZmMWC9uHs8LoPXcA+kXOr8AvMfObpr5zvy+LCQxQmXR/3GtrHr/8FONR7nbJG9atlKYXHpB+XwvZWYz6EhEX+juLrKnafCsO39lAC5lAMukN97Ye2Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RlmhKUWi; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RlmhKUWi" 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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; } From patchwork Sat Dec 28 17:55:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13922477 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67986198A22 for ; Sat, 28 Dec 2024 17:55:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408533; cv=none; b=AcHe2hwcV3+RV4sfI4W68ETg+85FfkdRIthg3ZTu/CJC3i0PhpddemvHODi0lMHK+kSFtYfV4vz6JP3KwzZcQB0GR8OzDZ9DznFYT0pGmsB1iBmSDV+oUkVHkkzBRNNvbvvvmq3LVUNuxxMxFJrgbgNCrDvbvplDE8CP4xPe/d4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735408533; c=relaxed/simple; bh=PJrR9iTSkLO7Ad1cW7Xy5tTxsQkFEv8En0/HpmU5uaA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ODkVZ+/8NdtShRpXD0nP+3ZNYkrD+cL8sX00V2BsI7S1L6+iKSUD7cpMpK/9X0t0pRYMCagpVsyTuzpn6OBUcO6xW0ZwCGOXE/XPbwdrr5zRJ233NN6ox8aMKto/JilGepbqpIlFAq5h9drsz1C7yS2HnQce1J2G41vgH7N2spI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EaponY1D; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EaponY1D" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75879C4CED3; Sat, 28 Dec 2024 17:55:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735408533; bh=PJrR9iTSkLO7Ad1cW7Xy5tTxsQkFEv8En0/HpmU5uaA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EaponY1DCwvK9kO+sQHd8NmTQhrL3/6Ul2UEQonLWbZ3xmzGWlGLjdUA7Ppd2DgSx xItpQE3/E9HoJzUY6ayuKUxVdAhqk1F3mcSD9WK89Sy8YfMzhMkz4aCzzk0xID/d2X DQ4GH3HL6nZwcwXToopqV3OC1uLtoh8fJMtROQDlEN24kh1ktJZ16HBCiOmLjvtzOy Y9Wm3AyOnAZ+hmM62midzMME6KAUP1wR5T0ZrVs3239RgKlMLEaXEcVvi1ct0eLXkC RASz6cY7NBmfVwjEndQ17MQEGopknO+ZnXy1KLKJnvzMaH3OzBhk4iXSzLnTIUsAcO wXTaNZNNnJp4Q== 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 5/5] libfs: Use d_children list to iterate simple_offset directories Date: Sat, 28 Dec 2024 12:55:21 -0500 Message-ID: <20241228175522.1854234-6-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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Chuck Lever The mtree mechanism has been effective at creating directory offsets that are stable over multiple opendir instances. However, it has not been able to handle the subtleties of renames that are concurrent with readdir. Instead of using the mtree to emit entries in the order of their offset values, use it only to map incoming ctx->pos to a starting entry. Then use the directory's d_children list, which is already maintained properly by the dcache, to find the next child to emit. One of the sneaky things about this is that when the mtree-allocated offset value wraps (which is very rare), looking up ctx->pos++ is not going to find the next entry; it will return NULL. Instead, by following the d_children list, the offset values can appear in any order but all of the entries in the directory will be visited eventually. Note also that the readdir() is guaranteed to reach the tail of this list. Entries are added only at the head of d_children, and readdir walks from its current position in that list towards its tail. Signed-off-by: Chuck Lever --- fs/libfs.c | 84 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 47399f90511a..279442b1fe96 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -247,12 +247,13 @@ EXPORT_SYMBOL(simple_dir_inode_operations); /* simple_offset_add() never assigns these to a dentry */ enum { + DIR_OFFSET_FIRST = 2, /* Find first real entry */ DIR_OFFSET_EOD = S32_MAX, }; /* simple_offset_add() allocation range */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_MIN = DIR_OFFSET_FIRST + 1, DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1, }; @@ -457,51 +458,82 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, LONG_MAX); } -static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) +static struct dentry *find_positive_dentry(struct dentry *parent, + struct dentry *dentry, + bool next) { - MA_STATE(mas, &octx->mt, offset, offset); + struct dentry *found = NULL; + + spin_lock(&parent->d_lock); + if (next) + dentry = d_next_sibling(dentry); + else if (!dentry) + dentry = d_first_child(parent); + hlist_for_each_entry_from(dentry, d_sib) { + if (!simple_positive(dentry)) + continue; + spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); + if (simple_positive(dentry)) + found = dget_dlock(dentry); + spin_unlock(&dentry->d_lock); + if (likely(found)) + break; + } + spin_unlock(&parent->d_lock); + return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup(struct dentry *parent, loff_t offset) +{ + struct inode *inode = d_inode(parent); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *child, *found = NULL; - rcu_read_lock(); - child = mas_find(&mas, DIR_OFFSET_MAX); - if (!child) - goto out; - spin_lock(&child->d_lock); - if (simple_positive(child)) - found = dget_dlock(child); - spin_unlock(&child->d_lock); -out: - rcu_read_unlock(); + MA_STATE(mas, &octx->mt, offset, offset); + + if (offset == DIR_OFFSET_FIRST) + found = find_positive_dentry(parent, NULL, false); + else { + rcu_read_lock(); + child = mas_find(&mas, DIR_OFFSET_MAX); + found = find_positive_dentry(parent, child, false); + rcu_read_unlock(); + } return found; } static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) { struct inode *inode = d_inode(dentry); - long offset = dentry2offset(dentry); - return ctx->actor(ctx, dentry->d_name.name, dentry->d_name.len, offset, - inode->i_ino, fs_umode_to_dtype(inode->i_mode)); + return dir_emit(ctx, dentry->d_name.name, dentry->d_name.len, + 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 offset_ctx *octx = inode->i_op->get_offset_ctx(inode); + struct dentry *dir = file->f_path.dentry; struct dentry *dentry; + dentry = offset_dir_lookup(dir, ctx->pos); + if (!dentry) + goto out_eod; while (true) { - dentry = offset_find_next(octx, ctx->pos); - if (!dentry) - goto out_eod; + struct dentry *next; - if (!offset_dir_emit(ctx, dentry)) { - dput(dentry); + ctx->pos = dentry2offset(dentry); + if (!offset_dir_emit(ctx, dentry)) break; - } - ctx->pos = dentry2offset(dentry) + 1; + next = find_positive_dentry(dir, dentry, true); dput(dentry); + + if (!next) + goto out_eod; + dentry = next; } + dput(dentry); return; out_eod: @@ -540,7 +572,7 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; if (ctx->pos != DIR_OFFSET_EOD) - offset_iterate_dir(d_inode(dir), ctx); + offset_iterate_dir(file, ctx); return 0; }