Skip to content

Commit e60aa5d

Browse files
fdmananakdave
authored andcommitted
btrfs: refresh dir last index during a rewinddir(3) call
When opening a directory we find what's the index of its last entry and then store it in the directory's file handle private data (struct btrfs_file_private::last_index), so that in the case new directory entries are added to a directory after an opendir(3) call we don't end up in an infinite loop (see commit 9b378f6 ("btrfs: fix infinite directory reads")) when calling readdir(3). However once rewinddir(3) is called, POSIX states [1] that any new directory entries added after the previous opendir(3) call, must be returned by subsequent calls to readdir(3): "The rewinddir() function shall reset the position of the directory stream to which dirp refers to the beginning of the directory. It shall also cause the directory stream to refer to the current state of the corresponding directory, as a call to opendir() would have done." We currently don't refresh the last_index field of the struct btrfs_file_private associated to the directory, so after a rewinddir(3) we are not returning any new entries added after the opendir(3) call. Fix this by finding the current last index of the directory when llseek is called against the directory. This can be reproduced by the following C program provided by Ian Johnson: #include <dirent.h> #include <stdio.h> int main(void) { DIR *dir = opendir("test"); FILE *file; file = fopen("test/1", "w"); fwrite("1", 1, 1, file); fclose(file); file = fopen("test/2", "w"); fwrite("2", 1, 1, file); fclose(file); rewinddir(dir); struct dirent *entry; while ((entry = readdir(dir))) { printf("%s\n", entry->d_name); } closedir(dir); return 0; } Reported-by: Ian Johnson <ian@ianjohnson.dev> Link: https://lore.kernel.org/linux-btrfs/YR1P0S.NGASEG570GJ8@ianjohnson.dev/ Fixes: 9b378f6 ("btrfs: fix infinite directory reads") CC: stable@vger.kernel.org # 6.5+ Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 3579503 commit e60aa5d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

fs/btrfs/inode.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5818,6 +5818,19 @@ static int btrfs_opendir(struct inode *inode, struct file *file)
58185818
return 0;
58195819
}
58205820

5821+
static loff_t btrfs_dir_llseek(struct file *file, loff_t offset, int whence)
5822+
{
5823+
struct btrfs_file_private *private = file->private_data;
5824+
int ret;
5825+
5826+
ret = btrfs_get_dir_last_index(BTRFS_I(file_inode(file)),
5827+
&private->last_index);
5828+
if (ret)
5829+
return ret;
5830+
5831+
return generic_file_llseek(file, offset, whence);
5832+
}
5833+
58215834
struct dir_entry {
58225835
u64 ino;
58235836
u64 offset;
@@ -10891,7 +10904,7 @@ static const struct inode_operations btrfs_dir_inode_operations = {
1089110904
};
1089210905

1089310906
static const struct file_operations btrfs_dir_file_operations = {
10894-
.llseek = generic_file_llseek,
10907+
.llseek = btrfs_dir_llseek,
1089510908
.read = generic_read_dir,
1089610909
.iterate_shared = btrfs_real_readdir,
1089710910
.open = btrfs_opendir,

0 commit comments

Comments
 (0)