vfs: introduce generic_file_rw_checks()
Factor out helper with some checks on in/out file that are common to clone_file_range and copy_file_range. Suggested-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Amir Goldstein <amir73il@gmail.com> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>alistair/sunxi64-5.4-dsi
parent
64bf5ff58d
commit
a31713517d
|
@ -1617,17 +1617,18 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
|
||||||
struct file *file_out, loff_t pos_out,
|
struct file *file_out, loff_t pos_out,
|
||||||
size_t len, unsigned int flags)
|
size_t len, unsigned int flags)
|
||||||
{
|
{
|
||||||
struct inode *inode_in = file_inode(file_in);
|
|
||||||
struct inode *inode_out = file_inode(file_out);
|
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
|
|
||||||
if (flags != 0)
|
if (flags != 0)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
|
/* this could be relaxed once a method supports cross-fs copies */
|
||||||
return -EISDIR;
|
if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
|
||||||
if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
|
return -EXDEV;
|
||||||
return -EINVAL;
|
|
||||||
|
ret = generic_file_rw_checks(file_in, file_out);
|
||||||
|
if (unlikely(ret))
|
||||||
|
return ret;
|
||||||
|
|
||||||
ret = rw_verify_area(READ, file_in, &pos_in, len);
|
ret = rw_verify_area(READ, file_in, &pos_in, len);
|
||||||
if (unlikely(ret))
|
if (unlikely(ret))
|
||||||
|
@ -1637,15 +1638,6 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
|
||||||
if (unlikely(ret))
|
if (unlikely(ret))
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
if (!(file_in->f_mode & FMODE_READ) ||
|
|
||||||
!(file_out->f_mode & FMODE_WRITE) ||
|
|
||||||
(file_out->f_flags & O_APPEND))
|
|
||||||
return -EBADF;
|
|
||||||
|
|
||||||
/* this could be relaxed once a method supports cross-fs copies */
|
|
||||||
if (inode_in->i_sb != inode_out->i_sb)
|
|
||||||
return -EXDEV;
|
|
||||||
|
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -2013,29 +2005,21 @@ loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
|
||||||
struct file *file_out, loff_t pos_out,
|
struct file *file_out, loff_t pos_out,
|
||||||
loff_t len, unsigned int remap_flags)
|
loff_t len, unsigned int remap_flags)
|
||||||
{
|
{
|
||||||
struct inode *inode_in = file_inode(file_in);
|
|
||||||
struct inode *inode_out = file_inode(file_out);
|
|
||||||
loff_t ret;
|
loff_t ret;
|
||||||
|
|
||||||
WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP);
|
WARN_ON_ONCE(remap_flags & REMAP_FILE_DEDUP);
|
||||||
|
|
||||||
if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
|
|
||||||
return -EISDIR;
|
|
||||||
if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
|
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
|
* FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
|
||||||
* the same mount. Practically, they only need to be on the same file
|
* the same mount. Practically, they only need to be on the same file
|
||||||
* system.
|
* system.
|
||||||
*/
|
*/
|
||||||
if (inode_in->i_sb != inode_out->i_sb)
|
if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
|
||||||
return -EXDEV;
|
return -EXDEV;
|
||||||
|
|
||||||
if (!(file_in->f_mode & FMODE_READ) ||
|
ret = generic_file_rw_checks(file_in, file_out);
|
||||||
!(file_out->f_mode & FMODE_WRITE) ||
|
if (ret < 0)
|
||||||
(file_out->f_flags & O_APPEND))
|
return ret;
|
||||||
return -EBADF;
|
|
||||||
|
|
||||||
if (!file_in->f_op->remap_file_range)
|
if (!file_in->f_op->remap_file_range)
|
||||||
return -EOPNOTSUPP;
|
return -EOPNOTSUPP;
|
||||||
|
|
|
@ -3049,6 +3049,7 @@ extern ssize_t generic_write_checks(struct kiocb *, struct iov_iter *);
|
||||||
extern int generic_remap_checks(struct file *file_in, loff_t pos_in,
|
extern int generic_remap_checks(struct file *file_in, loff_t pos_in,
|
||||||
struct file *file_out, loff_t pos_out,
|
struct file *file_out, loff_t pos_out,
|
||||||
loff_t *count, unsigned int remap_flags);
|
loff_t *count, unsigned int remap_flags);
|
||||||
|
extern int generic_file_rw_checks(struct file *file_in, struct file *file_out);
|
||||||
extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
|
extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
|
||||||
extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
|
extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
|
||||||
extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
|
extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
|
||||||
|
|
24
mm/filemap.c
24
mm/filemap.c
|
@ -3041,6 +3041,30 @@ int generic_remap_checks(struct file *file_in, loff_t pos_in,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Performs common checks before doing a file copy/clone
|
||||||
|
* from @file_in to @file_out.
|
||||||
|
*/
|
||||||
|
int generic_file_rw_checks(struct file *file_in, struct file *file_out)
|
||||||
|
{
|
||||||
|
struct inode *inode_in = file_inode(file_in);
|
||||||
|
struct inode *inode_out = file_inode(file_out);
|
||||||
|
|
||||||
|
/* Don't copy dirs, pipes, sockets... */
|
||||||
|
if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
|
||||||
|
return -EISDIR;
|
||||||
|
if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (!(file_in->f_mode & FMODE_READ) ||
|
||||||
|
!(file_out->f_mode & FMODE_WRITE) ||
|
||||||
|
(file_out->f_flags & O_APPEND))
|
||||||
|
return -EBADF;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int pagecache_write_begin(struct file *file, struct address_space *mapping,
|
int pagecache_write_begin(struct file *file, struct address_space *mapping,
|
||||||
loff_t pos, unsigned len, unsigned flags,
|
loff_t pos, unsigned len, unsigned flags,
|
||||||
struct page **pagep, void **fsdata)
|
struct page **pagep, void **fsdata)
|
||||||
|
|
Loading…
Reference in New Issue