OSDN Git Service

alloc_file(): switch to passing O_... flags instead of FMODE_... mode
authorAl Viro <viro@zeniv.linux.org.uk>
Wed, 11 Jul 2018 18:19:04 +0000 (14:19 -0400)
committerAl Viro <viro@zeniv.linux.org.uk>
Thu, 12 Jul 2018 14:02:57 +0000 (10:02 -0400)
... so that it could set both ->f_flags and ->f_mode, without callers
having to set ->f_flags manually.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
12 files changed:
drivers/misc/cxl/api.c
drivers/scsi/cxlflash/ocxl_hw.c
fs/aio.c
fs/anon_inodes.c
fs/file_table.c
fs/hugetlbfs/inode.c
fs/pipe.c
include/linux/file.h
ipc/shm.c
mm/memfd.c
mm/shmem.c
net/socket.c

index 6b16946..0b5cb6c 100644 (file)
@@ -102,12 +102,11 @@ static struct file *cxl_getfile(const char *name,
        path.mnt = mntget(cxl_vfs_mount);
        d_instantiate(path.dentry, inode);
 
-       file = alloc_file(&path, OPEN_FMODE(flags), fops);
+       file = alloc_file(&path, flags & (O_ACCMODE | O_NONBLOCK), fops);
        if (IS_ERR(file)) {
                path_put(&path);
                goto err_fs;
        }
-       file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
        file->private_data = priv;
 
        return file;
index 497a683..99bb393 100644 (file)
@@ -129,7 +129,7 @@ static struct file *ocxlflash_getfile(struct device *dev, const char *name,
        path.mnt = mntget(ocxlflash_vfs_mount);
        d_instantiate(path.dentry, inode);
 
-       file = alloc_file(&path, OPEN_FMODE(flags), fops);
+       file = alloc_file(&path, flags & (O_ACCMODE | O_NONBLOCK), fops);
        if (IS_ERR(file)) {
                rc = PTR_ERR(file);
                dev_err(dev, "%s: alloc_file failed rc=%d\n",
@@ -138,7 +138,6 @@ static struct file *ocxlflash_getfile(struct device *dev, const char *name,
                goto err3;
        }
 
-       file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
        file->private_data = priv;
 out:
        return file;
index e1d2012..9eea538 100644 (file)
--- a/fs/aio.c
+++ b/fs/aio.c
@@ -234,13 +234,9 @@ static struct file *aio_private_file(struct kioctx *ctx, loff_t nr_pages)
        path.mnt = mntget(aio_mnt);
 
        d_instantiate(path.dentry, inode);
-       file = alloc_file(&path, FMODE_READ | FMODE_WRITE, &aio_ring_fops);
-       if (IS_ERR(file)) {
+       file = alloc_file(&path, O_RDWR, &aio_ring_fops);
+       if (IS_ERR(file))
                path_put(&path);
-               return file;
-       }
-
-       file->f_flags = O_RDWR;
        return file;
 }
 
index 3168ee4..6b235ab 100644 (file)
@@ -102,12 +102,11 @@ struct file *anon_inode_getfile(const char *name,
 
        d_instantiate(path.dentry, anon_inode_inode);
 
-       file = alloc_file(&path, OPEN_FMODE(flags), fops);
+       file = alloc_file(&path, flags & (O_ACCMODE | O_NONBLOCK), fops);
        if (IS_ERR(file))
                goto err_dput;
        file->f_mapping = anon_inode_inode->i_mapping;
 
-       file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
        file->private_data = priv;
 
        return file;
index eee7cf6..086c3f5 100644 (file)
@@ -153,10 +153,10 @@ over:
  * alloc_file - allocate and initialize a 'struct file'
  *
  * @path: the (dentry, vfsmount) pair for the new file
- * @mode: the mode with which the new file will be opened
+ * @flags: O_... flags with which the new file will be opened
  * @fop: the 'struct file_operations' for the new file
  */
-struct file *alloc_file(const struct path *path, fmode_t mode,
+struct file *alloc_file(const struct path *path, int flags,
                const struct file_operations *fop)
 {
        struct file *file;
@@ -165,19 +165,20 @@ struct file *alloc_file(const struct path *path, fmode_t mode,
        if (IS_ERR(file))
                return file;
 
+       file->f_mode = OPEN_FMODE(flags);
+       file->f_flags = flags;
        file->f_path = *path;
        file->f_inode = path->dentry->d_inode;
        file->f_mapping = path->dentry->d_inode->i_mapping;
        file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
-       if ((mode & FMODE_READ) &&
+       if ((file->f_mode & FMODE_READ) &&
             likely(fop->read || fop->read_iter))
-               mode |= FMODE_CAN_READ;
-       if ((mode & FMODE_WRITE) &&
+               file->f_mode |= FMODE_CAN_READ;
+       if ((file->f_mode & FMODE_WRITE) &&
             likely(fop->write || fop->write_iter))
-               mode |= FMODE_CAN_WRITE;
-       file->f_mode = mode;
+               file->f_mode |= FMODE_CAN_WRITE;
        file->f_op = fop;
-       if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
+       if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
                i_readcount_inc(path->dentry->d_inode);
        return file;
 }
index d508c78..71aed47 100644 (file)
@@ -1375,8 +1375,7 @@ struct file *hugetlb_file_setup(const char *name, size_t size,
        inode->i_size = size;
        clear_nlink(inode);
 
-       file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
-                       &hugetlbfs_file_operations);
+       file = alloc_file(&path, O_RDWR, &hugetlbfs_file_operations);
        if (IS_ERR(file))
                goto out_dentry; /* inode is already attached */
 
index 9405e45..1909422 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -760,16 +760,17 @@ int create_pipe_files(struct file **res, int flags)
 
        d_instantiate(path.dentry, inode);
 
-       f = alloc_file(&path, FMODE_WRITE, &pipefifo_fops);
+       f = alloc_file(&path, O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT)),
+                       &pipefifo_fops);
        if (IS_ERR(f)) {
                err = PTR_ERR(f);
                goto err_dentry;
        }
 
-       f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
        f->private_data = inode->i_pipe;
 
-       res[0] = alloc_file(&path, FMODE_READ, &pipefifo_fops);
+       res[0] = alloc_file(&path, O_RDONLY | (flags & O_NONBLOCK),
+                       &pipefifo_fops);
        if (IS_ERR(res[0])) {
                put_pipe_info(inode, inode->i_pipe);
                fput(f);
@@ -778,7 +779,6 @@ int create_pipe_files(struct file **res, int flags)
 
        path_get(&path);
        res[0]->private_data = inode->i_pipe;
-       res[0]->f_flags = O_RDONLY | (flags & O_NONBLOCK);
        res[1] = f;
        return 0;
 
index 279720d..6d34a12 100644 (file)
@@ -18,7 +18,7 @@ struct file_operations;
 struct vfsmount;
 struct dentry;
 struct path;
-extern struct file *alloc_file(const struct path *, fmode_t mode,
+extern struct file *alloc_file(const struct path *, int flags,
        const struct file_operations *fop);
 
 static inline void fput_light(struct file *file, int fput_needed)
index 051a3e1..c702abd 100644 (file)
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -1362,7 +1362,7 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
        struct ipc_namespace *ns;
        struct shm_file_data *sfd;
        struct path path;
-       fmode_t f_mode;
+       int f_flags;
        unsigned long populate = 0;
 
        err = -EINVAL;
@@ -1395,11 +1395,11 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
        if (shmflg & SHM_RDONLY) {
                prot = PROT_READ;
                acc_mode = S_IRUGO;
-               f_mode = FMODE_READ;
+               f_flags = O_RDONLY;
        } else {
                prot = PROT_READ | PROT_WRITE;
                acc_mode = S_IRUGO | S_IWUGO;
-               f_mode = FMODE_READ | FMODE_WRITE;
+               f_flags = O_RDWR;
        }
        if (shmflg & SHM_EXEC) {
                prot |= PROT_EXEC;
@@ -1449,7 +1449,7 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
                goto out_nattch;
        }
 
-       file = alloc_file(&path, f_mode,
+       file = alloc_file(&path, f_flags,
                          is_file_hugepages(shp->shm_file) ?
                                &shm_file_operations_huge :
                                &shm_file_operations);
index 2706951..2bb5e25 100644 (file)
@@ -326,7 +326,7 @@ SYSCALL_DEFINE2(memfd_create,
                goto err_fd;
        }
        file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
-       file->f_flags |= O_RDWR | O_LARGEFILE;
+       file->f_flags |= O_LARGEFILE;
 
        if (flags & MFD_ALLOW_SEALING) {
                file_seals = memfd_file_seals_ptr(file);
index 2cab844..84844e5 100644 (file)
@@ -3942,8 +3942,7 @@ static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, l
        if (IS_ERR(res))
                goto put_path;
 
-       res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
-                 &shmem_file_operations);
+       res = alloc_file(&path, O_RDWR, &shmem_file_operations);
        if (IS_ERR(res))
                goto put_path;
 
index 8a10901..2cdbe8f 100644 (file)
@@ -411,7 +411,7 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
 
        d_instantiate(path.dentry, SOCK_INODE(sock));
 
-       file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
+       file = alloc_file(&path, O_RDWR | (flags & O_NONBLOCK),
                  &socket_file_ops);
        if (IS_ERR(file)) {
                /* drop dentry, keep inode for a bit */
@@ -423,7 +423,6 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
        }
 
        sock->file = file;
-       file->f_flags = O_RDWR | (flags & O_NONBLOCK);
        file->private_data = sock;
        return file;
 }