FS_IOC_WRITE_STREAM_* ioctls involve common handling, such as argument validation, permission and uapi semantics checks etc. Centralize the common handling so that each filesystem does not have to reimplement. Suggested-by: Hans Holmberg Signed-off-by: Kanchan Joshi --- fs/write_streams.c | 115 ++++++++++++++++++++++++++++++++++ include/linux/write_streams.h | 6 ++ 2 files changed, 121 insertions(+) diff --git a/fs/write_streams.c b/fs/write_streams.c index 50b0b0769c38..da9d4a62d63b 100644 --- a/fs/write_streams.c +++ b/fs/write_streams.c @@ -2,11 +2,13 @@ /* Generic write-stream fd management. */ #include #include +#include #include #include #include #include #include +#include #include #include "internal.h" @@ -162,3 +164,116 @@ int inode_set_write_stream(struct inode *inode, u16 id) spin_unlock(&inode->i_lock); return 0; } + +static int ioctl_write_stream_get_max(struct file *file, __u32 __user *argp, + write_stream_pool_fn pool_fn) +{ + struct write_stream_pool *pool = pool_fn(file_inode(file)); + __u32 max = 0; + + if (pool) + max = write_stream_pool_count(pool); + return put_user(max, argp); +} + +static int ioctl_write_stream_alloc(struct file *file, + write_stream_pool_fn pool_fn) +{ + struct write_stream_pool *pool; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + pool = pool_fn(file_inode(file)); + if (!pool) + return -EOPNOTSUPP; + return write_stream_alloc_fd(pool, file); +} + +static int ioctl_write_stream_attach(struct inode *inode, int stream_fd, + write_stream_pool_fn pool_fn) +{ + CLASS(fd, f)(stream_fd); + struct write_stream_pool *pool; + int id, error; + + if (fd_empty(f)) + return -EBADF; + + /* + * Serialize against vfs_fileattr_set(), which takes the same lock to + * change the flags that select the pool. + */ + inode_lock(inode); + pool = pool_fn(inode); + if (!pool) { + error = -EINVAL; + goto out_unlock; + } + id = write_stream_get_id(fd_file(f), pool); + if (id < 0) { + error = id; + goto out_unlock; + } + error = inode_set_write_stream(inode, id); +out_unlock: + inode_unlock(inode); + return error; +} + +static int ioctl_write_stream_set(struct file *file, + struct fs_write_stream_set __user *argp, + write_stream_pool_fn pool_fn) +{ + struct inode *inode = file_inode(file); + struct fs_write_stream_set set; + + if (!(file->f_mode & FMODE_WRITE)) + return -EBADF; + if (copy_from_user(&set, argp, sizeof(set))) + return -EFAULT; + if (set.flags & ~FS_WRITE_STREAM_SET_CLEAR) + return -EINVAL; + if (!S_ISREG(inode->i_mode)) + return -EINVAL; + if (IS_IMMUTABLE(inode)) + return -EPERM; + if (!inode_owner_or_capable(file_mnt_idmap(file), inode)) + return -EPERM; + + if (!(set.flags & FS_WRITE_STREAM_SET_CLEAR)) + return ioctl_write_stream_attach(inode, set.stream_fd, pool_fn); + if (set.stream_fd != -1) + return -EINVAL; + + inode_lock(inode); + inode_set_write_stream(inode, WRITE_STREAM_NOT_SET); + inode_unlock(inode); + return 0; +} + +/** + * write_stream_ioctl - handle the FS_IOC_WRITE_STREAM_* ioctls + * @file: file the ioctl is issued on + * @cmd: ioctl command + * @argp: ioctl argument + * @pool_fn: callback that returns the pool serving an inode, or + * NULL if that inode cannot use write streams; may sleep, and is + * called with the inode lock held for FS_IOC_WRITE_STREAM_SET + * + * Called by a filesystem from its ioctl handler. GET_MAX and ALLOC work on + * any file the filesystem passes in, SET on regular files only. + */ +long write_stream_ioctl(struct file *file, unsigned int cmd, + void __user *argp, write_stream_pool_fn pool_fn) +{ + switch (cmd) { + case FS_IOC_WRITE_STREAM_GET_MAX: + return ioctl_write_stream_get_max(file, argp, pool_fn); + case FS_IOC_WRITE_STREAM_ALLOC: + return ioctl_write_stream_alloc(file, pool_fn); + case FS_IOC_WRITE_STREAM_SET: + return ioctl_write_stream_set(file, argp, pool_fn); + } + return -ENOTTY; +} +EXPORT_SYMBOL_GPL(write_stream_ioctl); diff --git a/include/linux/write_streams.h b/include/linux/write_streams.h index de561540548a..c5a63422a534 100644 --- a/include/linux/write_streams.h +++ b/include/linux/write_streams.h @@ -6,6 +6,7 @@ #include struct file; +struct inode; /* i_write_stream value of an inode that has no write stream */ #define WRITE_STREAM_NOT_SET 0 @@ -29,4 +30,9 @@ write_stream_pool_count(const struct write_stream_pool *pool) int write_stream_pool_init(struct write_stream_pool *pool, unsigned int nr); void write_stream_pool_destroy(struct write_stream_pool *pool); +typedef struct write_stream_pool *(*write_stream_pool_fn)(struct inode *inode); + +long write_stream_ioctl(struct file *file, unsigned int cmd, + void __user *argp, write_stream_pool_fn pool_fn); + #endif /* _LINUX_WRITE_STREAMS_H */ -- 2.25.1