]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
Revert "fs: fold open_check_o_direct into do_dentry_open"
authorAl Viro <viro@zeniv.linux.org.uk>
Sat, 2 Jun 2018 05:31:02 +0000 (01:31 -0400)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sun, 3 Jun 2018 17:58:23 +0000 (10:58 -0700)
This reverts commit cab64df194667dc5d9d786f0a895f647f5501c0d.

Having vfs_open() in some cases drop the reference to
struct file combined with

error = vfs_open(path, f, cred);
if (error) {
put_filp(f);
return ERR_PTR(error);
}
return f;

is flat-out wrong.  It used to be

error = vfs_open(path, f, cred);
if (!error) {
/* from now on we need fput() to dispose of f */
error = open_check_o_direct(f);
if (error) {
fput(f);
f = ERR_PTR(error);
}
} else {
put_filp(f);
f = ERR_PTR(error);
}

and sure, having that open_check_o_direct() boilerplate gotten rid of is
nice, but not that way...

Worse, another call chain (via finish_open()) is FUBAR now wrt
FILE_OPENED handling - in that case we get error returned, with file
already hit by fput() *AND* FILE_OPENED not set.  Guess what happens in
path_openat(), when it hits

if (!(opened & FILE_OPENED)) {
BUG_ON(!error);
put_filp(file);
}

The root cause of all that crap is that the callers of do_dentry_open()
have no way to tell which way did it fail; while that could be fixed up
(by passing something like int *opened to do_dentry_open() and have it
marked if we'd called ->open()), it's probably much too late in the
cycle to do so right now.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/internal.h
fs/namei.c
fs/open.c

index e08972db030366c7616606b53f91ddeb50ef4ba1..980d005b21b4111084ab7a56b17fda7e5b1320f5 100644 (file)
@@ -125,6 +125,7 @@ int do_fchmodat(int dfd, const char __user *filename, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
                int flag);
 
+extern int open_check_o_direct(struct file *f);
 extern int vfs_open(const struct path *, struct file *, const struct cred *);
 extern struct file *filp_clone_open(struct file *);
 
index 186bd2464fd5a842480a37c55b57d60d9164cf86..4eb916996345d78bd87b5262ff11769d20d4d29f 100644 (file)
@@ -3367,7 +3367,9 @@ static int do_last(struct nameidata *nd,
                goto out;
        *opened |= FILE_OPENED;
 opened:
-       error = ima_file_check(file, op->acc_mode, *opened);
+       error = open_check_o_direct(file);
+       if (!error)
+               error = ima_file_check(file, op->acc_mode, *opened);
        if (!error && will_truncate)
                error = handle_truncate(file);
 out:
@@ -3447,6 +3449,9 @@ static int do_tmpfile(struct nameidata *nd, unsigned flags,
        error = finish_open(file, child, NULL, opened);
        if (error)
                goto out2;
+       error = open_check_o_direct(file);
+       if (error)
+               fput(file);
 out2:
        mnt_drop_write(path.mnt);
 out:
index c5ee7cd60424097aea1607f6f17b4f070dd58e5b..d0e955b558ad84ce2d6f98f6ccb490185bc36cc2 100644 (file)
--- a/fs/open.c
+++ b/fs/open.c
@@ -724,6 +724,16 @@ SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
        return ksys_fchown(fd, user, group);
 }
 
+int open_check_o_direct(struct file *f)
+{
+       /* NB: we're sure to have correct a_ops only after f_op->open */
+       if (f->f_flags & O_DIRECT) {
+               if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
+                       return -EINVAL;
+       }
+       return 0;
+}
+
 static int do_dentry_open(struct file *f,
                          struct inode *inode,
                          int (*open)(struct inode *, struct file *),
@@ -745,7 +755,7 @@ static int do_dentry_open(struct file *f,
        if (unlikely(f->f_flags & O_PATH)) {
                f->f_mode = FMODE_PATH;
                f->f_op = &empty_fops;
-               goto done;
+               return 0;
        }
 
        if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
@@ -798,12 +808,7 @@ static int do_dentry_open(struct file *f,
        f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
 
        file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
-done:
-       /* NB: we're sure to have correct a_ops only after f_op->open */
-       error = -EINVAL;
-       if ((f->f_flags & O_DIRECT) &&
-           (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO))
-               goto out_fput;
+
        return 0;
 
 cleanup_all:
@@ -818,9 +823,6 @@ static int do_dentry_open(struct file *f,
        f->f_path.dentry = NULL;
        f->f_inode = NULL;
        return error;
-out_fput:
-       fput(f);
-       return error;
 }
 
 /**
@@ -918,14 +920,20 @@ struct file *dentry_open(const struct path *path, int flags,
        BUG_ON(!path->mnt);
 
        f = get_empty_filp();
-       if (IS_ERR(f))
-               return f;
-
-       f->f_flags = flags;
-       error = vfs_open(path, f, cred);
-       if (error) {
-               put_filp(f);
-               return ERR_PTR(error);
+       if (!IS_ERR(f)) {
+               f->f_flags = flags;
+               error = vfs_open(path, f, cred);
+               if (!error) {
+                       /* from now on we need fput() to dispose of f */
+                       error = open_check_o_direct(f);
+                       if (error) {
+                               fput(f);
+                               f = ERR_PTR(error);
+                       }
+               } else { 
+                       put_filp(f);
+                       f = ERR_PTR(error);
+               }
        }
        return f;
 }