ext4: store cookie in private data

Store the cookie to detect concurrent seeks on directories in
file->private_data.

Link: https://lore.kernel.org/r/20240830-vfs-file-f_version-v1-11-6d3e4816aa7b@kernel.org
Acked-by: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
Christian Brauner 2024-08-30 15:04:52 +02:00
parent 794576e075
commit 4f05ee2f82
No known key found for this signature in database
GPG Key ID: 91C61BC06578DCA2
3 changed files with 33 additions and 24 deletions

View File

@ -133,6 +133,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
struct super_block *sb = inode->i_sb; struct super_block *sb = inode->i_sb;
struct buffer_head *bh = NULL; struct buffer_head *bh = NULL;
struct fscrypt_str fstr = FSTR_INIT(NULL, 0); struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
struct dir_private_info *info = file->private_data;
err = fscrypt_prepare_readdir(inode); err = fscrypt_prepare_readdir(inode);
if (err) if (err)
@ -229,7 +230,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
* readdir(2), then we might be pointing to an invalid * readdir(2), then we might be pointing to an invalid
* dirent right now. Scan from the start of the block * dirent right now. Scan from the start of the block
* to make sure. */ * to make sure. */
if (!inode_eq_iversion(inode, file->f_version)) { if (!inode_eq_iversion(inode, info->cookie)) {
for (i = 0; i < sb->s_blocksize && i < offset; ) { for (i = 0; i < sb->s_blocksize && i < offset; ) {
de = (struct ext4_dir_entry_2 *) de = (struct ext4_dir_entry_2 *)
(bh->b_data + i); (bh->b_data + i);
@ -249,7 +250,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
offset = i; offset = i;
ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1)) ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
| offset; | offset;
file->f_version = inode_query_iversion(inode); info->cookie = inode_query_iversion(inode);
} }
while (ctx->pos < inode->i_size while (ctx->pos < inode->i_size
@ -384,6 +385,7 @@ static inline loff_t ext4_get_htree_eof(struct file *filp)
static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence) static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
{ {
struct inode *inode = file->f_mapping->host; struct inode *inode = file->f_mapping->host;
struct dir_private_info *info = file->private_data;
int dx_dir = is_dx_dir(inode); int dx_dir = is_dx_dir(inode);
loff_t ret, htree_max = ext4_get_htree_eof(file); loff_t ret, htree_max = ext4_get_htree_eof(file);
@ -392,7 +394,7 @@ static loff_t ext4_dir_llseek(struct file *file, loff_t offset, int whence)
htree_max, htree_max); htree_max, htree_max);
else else
ret = ext4_llseek(file, offset, whence); ret = ext4_llseek(file, offset, whence);
file->f_version = inode_peek_iversion(inode) - 1; info->cookie = inode_peek_iversion(inode) - 1;
return ret; return ret;
} }
@ -429,18 +431,15 @@ static void free_rb_tree_fname(struct rb_root *root)
*root = RB_ROOT; *root = RB_ROOT;
} }
static void ext4_htree_init_dir_info(struct file *filp, loff_t pos)
static struct dir_private_info *ext4_htree_create_dir_info(struct file *filp,
loff_t pos)
{ {
struct dir_private_info *p; struct dir_private_info *p = filp->private_data;
p = kzalloc(sizeof(*p), GFP_KERNEL); if (is_dx_dir(file_inode(filp)) && !p->initialized) {
if (!p)
return NULL;
p->curr_hash = pos2maj_hash(filp, pos); p->curr_hash = pos2maj_hash(filp, pos);
p->curr_minor_hash = pos2min_hash(filp, pos); p->curr_minor_hash = pos2min_hash(filp, pos);
return p; p->initialized = true;
}
} }
void ext4_htree_free_dir_info(struct dir_private_info *p) void ext4_htree_free_dir_info(struct dir_private_info *p)
@ -552,12 +551,7 @@ static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
struct fname *fname; struct fname *fname;
int ret = 0; int ret = 0;
if (!info) { ext4_htree_init_dir_info(file, ctx->pos);
info = ext4_htree_create_dir_info(file, ctx->pos);
if (!info)
return -ENOMEM;
file->private_data = info;
}
if (ctx->pos == ext4_get_htree_eof(file)) if (ctx->pos == ext4_get_htree_eof(file))
return 0; /* EOF */ return 0; /* EOF */
@ -590,10 +584,10 @@ static int ext4_dx_readdir(struct file *file, struct dir_context *ctx)
* cached entries. * cached entries.
*/ */
if ((!info->curr_node) || if ((!info->curr_node) ||
!inode_eq_iversion(inode, file->f_version)) { !inode_eq_iversion(inode, info->cookie)) {
info->curr_node = NULL; info->curr_node = NULL;
free_rb_tree_fname(&info->root); free_rb_tree_fname(&info->root);
file->f_version = inode_query_iversion(inode); info->cookie = inode_query_iversion(inode);
ret = ext4_htree_fill_tree(file, info->curr_hash, ret = ext4_htree_fill_tree(file, info->curr_hash,
info->curr_minor_hash, info->curr_minor_hash,
&info->next_hash); &info->next_hash);
@ -664,7 +658,19 @@ int ext4_check_all_de(struct inode *dir, struct buffer_head *bh, void *buf,
return 0; return 0;
} }
static int ext4_dir_open(struct inode *inode, struct file *file)
{
struct dir_private_info *info;
info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info)
return -ENOMEM;
file->private_data = info;
return 0;
}
const struct file_operations ext4_dir_operations = { const struct file_operations ext4_dir_operations = {
.open = ext4_dir_open,
.llseek = ext4_dir_llseek, .llseek = ext4_dir_llseek,
.read = generic_read_dir, .read = generic_read_dir,
.iterate_shared = ext4_readdir, .iterate_shared = ext4_readdir,

View File

@ -2553,6 +2553,8 @@ struct dir_private_info {
__u32 curr_hash; __u32 curr_hash;
__u32 curr_minor_hash; __u32 curr_minor_hash;
__u32 next_hash; __u32 next_hash;
u64 cookie;
bool initialized;
}; };
/* calculate the first block number of the group */ /* calculate the first block number of the group */

View File

@ -1460,6 +1460,7 @@ int ext4_read_inline_dir(struct file *file,
struct ext4_iloc iloc; struct ext4_iloc iloc;
void *dir_buf = NULL; void *dir_buf = NULL;
int dotdot_offset, dotdot_size, extra_offset, extra_size; int dotdot_offset, dotdot_size, extra_offset, extra_size;
struct dir_private_info *info = file->private_data;
ret = ext4_get_inode_loc(inode, &iloc); ret = ext4_get_inode_loc(inode, &iloc);
if (ret) if (ret)
@ -1503,12 +1504,12 @@ int ext4_read_inline_dir(struct file *file,
extra_size = extra_offset + inline_size; extra_size = extra_offset + inline_size;
/* /*
* If the version has changed since the last call to * If the cookie has changed since the last call to
* readdir(2), then we might be pointing to an invalid * readdir(2), then we might be pointing to an invalid
* dirent right now. Scan from the start of the inline * dirent right now. Scan from the start of the inline
* dir to make sure. * dir to make sure.
*/ */
if (!inode_eq_iversion(inode, file->f_version)) { if (!inode_eq_iversion(inode, info->cookie)) {
for (i = 0; i < extra_size && i < offset;) { for (i = 0; i < extra_size && i < offset;) {
/* /*
* "." is with offset 0 and * "." is with offset 0 and
@ -1540,7 +1541,7 @@ int ext4_read_inline_dir(struct file *file,
} }
offset = i; offset = i;
ctx->pos = offset; ctx->pos = offset;
file->f_version = inode_query_iversion(inode); info->cookie = inode_query_iversion(inode);
} }
while (ctx->pos < extra_size) { while (ctx->pos < extra_size) {