fs: add vfs_setpos_cookie()

Add a new helper and make vfs_setpos() call it. We will use it in
follow-up patches.

Link: https://lore.kernel.org/r/20240830-vfs-file-f_version-v1-5-6d3e4816aa7b@kernel.org
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:46 +02:00
parent 1c5a54af47
commit d095a5be75
No known key found for this signature in database
GPG Key ID: 91C61BC06578DCA2

View File

@ -39,6 +39,34 @@ static inline bool unsigned_offsets(struct file *file)
return file->f_mode & FMODE_UNSIGNED_OFFSET;
}
/**
* vfs_setpos_cookie - update the file offset for lseek and reset cookie
* @file: file structure in question
* @offset: file offset to seek to
* @maxsize: maximum file size
* @cookie: cookie to reset
*
* Update the file offset to the value specified by @offset if the given
* offset is valid and it is not equal to the current file offset and
* reset the specified cookie to indicate that a seek happened.
*
* Return the specified offset on success and -EINVAL on invalid offset.
*/
static loff_t vfs_setpos_cookie(struct file *file, loff_t offset,
loff_t maxsize, u64 *cookie)
{
if (offset < 0 && !unsigned_offsets(file))
return -EINVAL;
if (offset > maxsize)
return -EINVAL;
if (offset != file->f_pos) {
file->f_pos = offset;
*cookie = 0;
}
return offset;
}
/**
* vfs_setpos - update the file offset for lseek
* @file: file structure in question
@ -53,16 +81,7 @@ static inline bool unsigned_offsets(struct file *file)
*/
loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
{
if (offset < 0 && !unsigned_offsets(file))
return -EINVAL;
if (offset > maxsize)
return -EINVAL;
if (offset != file->f_pos) {
file->f_pos = offset;
file->f_version = 0;
}
return offset;
return vfs_setpos_cookie(file, offset, maxsize, &file->f_version);
}
EXPORT_SYMBOL(vfs_setpos);