Skip to content

Commit 1146e5a

Browse files
committed
ubifs: store cookie in private data
Store the cookie to detect concurrent seeks on directories in file->private_data. Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 0bea828 commit 1146e5a

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

fs/ubifs/dir.c

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,11 @@ static unsigned int vfs_dent_type(uint8_t type)
555555
return 0;
556556
}
557557

558+
struct ubifs_dir_data {
559+
struct ubifs_dent_node *dent;
560+
u64 cookie;
561+
};
562+
558563
/*
559564
* The classical Unix view for directory is that it is a linear array of
560565
* (name, inode number) entries. Linux/VFS assumes this model as well.
@@ -582,6 +587,7 @@ static int ubifs_readdir(struct file *file, struct dir_context *ctx)
582587
struct inode *dir = file_inode(file);
583588
struct ubifs_info *c = dir->i_sb->s_fs_info;
584589
bool encrypted = IS_ENCRYPTED(dir);
590+
struct ubifs_dir_data *data = file->private_data;
585591

586592
dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
587593

@@ -604,27 +610,27 @@ static int ubifs_readdir(struct file *file, struct dir_context *ctx)
604610
fstr_real_len = fstr.len;
605611
}
606612

607-
if (file->f_version == 0) {
613+
if (data->cookie == 0) {
608614
/*
609-
* The file was seek'ed, which means that @file->private_data
615+
* The file was seek'ed, which means that @data->dent
610616
* is now invalid. This may also be just the first
611617
* 'ubifs_readdir()' invocation, in which case
612-
* @file->private_data is NULL, and the below code is
618+
* @data->dent is NULL, and the below code is
613619
* basically a no-op.
614620
*/
615-
kfree(file->private_data);
616-
file->private_data = NULL;
621+
kfree(data->dent);
622+
data->dent = NULL;
617623
}
618624

619625
/*
620-
* 'generic_file_llseek()' unconditionally sets @file->f_version to
621-
* zero, and we use this for detecting whether the file was seek'ed.
626+
* 'ubifs_dir_llseek()' sets @data->cookie to zero, and we use this
627+
* for detecting whether the file was seek'ed.
622628
*/
623-
file->f_version = 1;
629+
data->cookie = 1;
624630

625631
/* File positions 0 and 1 correspond to "." and ".." */
626632
if (ctx->pos < 2) {
627-
ubifs_assert(c, !file->private_data);
633+
ubifs_assert(c, !data->dent);
628634
if (!dir_emit_dots(file, ctx)) {
629635
if (encrypted)
630636
fscrypt_fname_free_buffer(&fstr);
@@ -641,10 +647,10 @@ static int ubifs_readdir(struct file *file, struct dir_context *ctx)
641647
}
642648

643649
ctx->pos = key_hash_flash(c, &dent->key);
644-
file->private_data = dent;
650+
data->dent = dent;
645651
}
646652

647-
dent = file->private_data;
653+
dent = data->dent;
648654
if (!dent) {
649655
/*
650656
* The directory was seek'ed to and is now readdir'ed.
@@ -658,7 +664,7 @@ static int ubifs_readdir(struct file *file, struct dir_context *ctx)
658664
goto out;
659665
}
660666
ctx->pos = key_hash_flash(c, &dent->key);
661-
file->private_data = dent;
667+
data->dent = dent;
662668
}
663669

664670
while (1) {
@@ -701,15 +707,15 @@ static int ubifs_readdir(struct file *file, struct dir_context *ctx)
701707
goto out;
702708
}
703709

704-
kfree(file->private_data);
710+
kfree(data->dent);
705711
ctx->pos = key_hash_flash(c, &dent->key);
706-
file->private_data = dent;
712+
data->dent = dent;
707713
cond_resched();
708714
}
709715

710716
out:
711-
kfree(file->private_data);
712-
file->private_data = NULL;
717+
kfree(data->dent);
718+
data->dent = NULL;
713719

714720
if (encrypted)
715721
fscrypt_fname_free_buffer(&fstr);
@@ -733,7 +739,10 @@ static int ubifs_readdir(struct file *file, struct dir_context *ctx)
733739
/* Free saved readdir() state when the directory is closed */
734740
static int ubifs_dir_release(struct inode *dir, struct file *file)
735741
{
736-
kfree(file->private_data);
742+
struct ubifs_dir_data *data = file->private_data;
743+
744+
kfree(data->dent);
745+
kfree(data);
737746
file->private_data = NULL;
738747
return 0;
739748
}
@@ -1712,6 +1721,24 @@ int ubifs_getattr(struct mnt_idmap *idmap, const struct path *path,
17121721
return 0;
17131722
}
17141723

1724+
static int ubifs_dir_open(struct inode *inode, struct file *file)
1725+
{
1726+
struct ubifs_dir_data *data;
1727+
1728+
data = kzalloc(sizeof(struct ubifs_dir_data), GFP_KERNEL);
1729+
if (!data)
1730+
return -ENOMEM;
1731+
file->private_data = data;
1732+
return 0;
1733+
}
1734+
1735+
static loff_t ubifs_dir_llseek(struct file *file, loff_t offset, int whence)
1736+
{
1737+
struct ubifs_dir_data *data = file->private_data;
1738+
1739+
return generic_llseek_cookie(file, offset, whence, &data->cookie);
1740+
}
1741+
17151742
const struct inode_operations ubifs_dir_inode_operations = {
17161743
.lookup = ubifs_lookup,
17171744
.create = ubifs_create,
@@ -1732,7 +1759,8 @@ const struct inode_operations ubifs_dir_inode_operations = {
17321759
};
17331760

17341761
const struct file_operations ubifs_dir_operations = {
1735-
.llseek = generic_file_llseek,
1762+
.open = ubifs_dir_open,
1763+
.llseek = ubifs_dir_llseek,
17361764
.release = ubifs_dir_release,
17371765
.read = generic_read_dir,
17381766
.iterate_shared = ubifs_readdir,

0 commit comments

Comments
 (0)