Skip to content

Commit

Permalink
vfs: export lseek_execute() to modules
Browse files Browse the repository at this point in the history
For those file systems(btrfs/ext4/ocfs2/tmpfs) that support
SEEK_DATA/SEEK_HOLE functions, we end up handling the similar
matter in lseek_execute() to update the current file offset
to the desired offset if it is valid, ceph also does the
simliar things at ceph_llseek().

To reduce the duplications, this patch make lseek_execute()
public accessible so that we can call it directly from the
underlying file systems.

Thanks Dave Chinner for this suggestion.

[AV: call it vfs_setpos(), don't bring the removed 'inode' argument back]

v2->v1:
- Add kernel-doc comments for lseek_execute()
- Call lseek_execute() in ceph->llseek()

Signed-off-by: Jie Liu <[email protected]>
Cc: Dave Chinner <[email protected]>
Cc: Al Viro <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Chris Mason <[email protected]>
Cc: Josef Bacik <[email protected]>
Cc: Ben Myers <[email protected]>
Cc: Ted Tso <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: Mark Fasheh <[email protected]>
Cc: Joel Becker <[email protected]>
Cc: Sage Weil <[email protected]>
Signed-off-by: Al Viro <[email protected]>
  • Loading branch information
pibroch authored and Al Viro committed Jul 3, 2013
1 parent 2142914 commit 46a1c2c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 68 deletions.
15 changes: 1 addition & 14 deletions fs/btrfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -2425,20 +2425,7 @@ static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
}
}

if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
offset = -EINVAL;
goto out;
}
if (offset > inode->i_sb->s_maxbytes) {
offset = -EINVAL;
goto out;
}

/* Special lock needed here? */
if (offset != file->f_pos) {
file->f_pos = offset;
file->f_version = 0;
}
offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
out:
mutex_unlock(&inode->i_mutex);
return offset;
Expand Down
11 changes: 1 addition & 10 deletions fs/ceph/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -866,16 +866,7 @@ static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
break;
}

if (offset < 0 || offset > inode->i_sb->s_maxbytes) {
offset = -EINVAL;
goto out;
}

/* Special lock needed here? */
if (offset != file->f_pos) {
file->f_pos = offset;
file->f_version = 0;
}
offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);

out:
mutex_unlock(&inode->i_mutex);
Expand Down
24 changes: 2 additions & 22 deletions fs/ext4/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,17 +494,7 @@ static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
if (dataoff > isize)
return -ENXIO;

if (dataoff < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
return -EINVAL;
if (dataoff > maxsize)
return -EINVAL;

if (dataoff != file->f_pos) {
file->f_pos = dataoff;
file->f_version = 0;
}

return dataoff;
return vfs_setpos(file, dataoff, maxsize);
}

/*
Expand Down Expand Up @@ -580,17 +570,7 @@ static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
if (holeoff > isize)
holeoff = isize;

if (holeoff < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
return -EINVAL;
if (holeoff > maxsize)
return -EINVAL;

if (holeoff != file->f_pos) {
file->f_pos = holeoff;
file->f_version = 0;
}

return holeoff;
return vfs_setpos(file, holeoff, maxsize);
}

/*
Expand Down
12 changes: 1 addition & 11 deletions fs/ocfs2/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -2646,17 +2646,7 @@ static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int whence)
goto out;
}

if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
ret = -EINVAL;
if (!ret && offset > inode->i_sb->s_maxbytes)
ret = -EINVAL;
if (ret)
goto out;

if (offset != file->f_pos) {
file->f_pos = offset;
file->f_version = 0;
}
offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);

out:
mutex_unlock(&inode->i_mutex);
Expand Down
19 changes: 16 additions & 3 deletions fs/read_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,19 @@ static inline int unsigned_offsets(struct file *file)
return file->f_mode & FMODE_UNSIGNED_OFFSET;
}

static loff_t lseek_execute(struct file *file, loff_t offset, loff_t maxsize)
/**
* vfs_setpos - update the file offset for lseek
* @file: file structure in question
* @offset: file offset to seek to
* @maxsize: maximum file size
*
* This is a low-level filesystem helper for updating 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.
*
* Return the specified offset on success and -EINVAL on invalid offset.
*/
loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
{
if (offset < 0 && !unsigned_offsets(file))
return -EINVAL;
Expand All @@ -54,6 +66,7 @@ static loff_t lseek_execute(struct file *file, loff_t offset, loff_t maxsize)
}
return offset;
}
EXPORT_SYMBOL(vfs_setpos);

/**
* generic_file_llseek_size - generic llseek implementation for regular files
Expand Down Expand Up @@ -94,7 +107,7 @@ generic_file_llseek_size(struct file *file, loff_t offset, int whence,
* like SEEK_SET.
*/
spin_lock(&file->f_lock);
offset = lseek_execute(file, file->f_pos + offset, maxsize);
offset = vfs_setpos(file, file->f_pos + offset, maxsize);
spin_unlock(&file->f_lock);
return offset;
case SEEK_DATA:
Expand All @@ -116,7 +129,7 @@ generic_file_llseek_size(struct file *file, loff_t offset, int whence,
break;
}

return lseek_execute(file, offset, maxsize);
return vfs_setpos(file, offset, maxsize);
}
EXPORT_SYMBOL(generic_file_llseek_size);

Expand Down
6 changes: 2 additions & 4 deletions fs/xfs/xfs_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,8 +1268,7 @@ xfs_seek_data(
}

out:
if (offset != file->f_pos)
file->f_pos = offset;
offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);

out_unlock:
xfs_iunlock_map_shared(ip, lock);
Expand Down Expand Up @@ -1377,8 +1376,7 @@ xfs_seek_hole(
* situation in particular.
*/
offset = min_t(loff_t, offset, isize);
if (offset != file->f_pos)
file->f_pos = offset;
offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);

out_unlock:
xfs_iunlock_map_shared(ip, lock);
Expand Down
1 change: 1 addition & 0 deletions include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,7 @@ extern void
file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
extern loff_t noop_llseek(struct file *file, loff_t offset, int whence);
extern loff_t no_llseek(struct file *file, loff_t offset, int whence);
extern loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize);
extern loff_t generic_file_llseek(struct file *file, loff_t offset, int whence);
extern loff_t generic_file_llseek_size(struct file *file, loff_t offset,
int whence, loff_t maxsize, loff_t eof);
Expand Down
5 changes: 1 addition & 4 deletions mm/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -1798,10 +1798,7 @@ static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
}
}

if (offset >= 0 && offset != file->f_pos) {
file->f_pos = offset;
file->f_version = 0;
}
offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
mutex_unlock(&inode->i_mutex);
return offset;
}
Expand Down

0 comments on commit 46a1c2c

Please sign in to comment.