do_last(): don't bother with keeping got_write in FMODE_OPENED case
authorAl Viro <viro@zeniv.linux.org.uk>
Sun, 26 Jan 2020 15:22:24 +0000 (10:22 -0500)
committerAl Viro <viro@zeniv.linux.org.uk>
Sat, 14 Mar 2020 01:09:12 +0000 (21:09 -0400)
it's easier to drop it right after lookup_open() and regain if
needed (i.e. if we will need to truncate).  On the non-FMODE_OPENED
path we do that anyway.  In case of FMODE_CREATED we won't be
needing it.  And it's easier to prove correctness that way,
especially since the initial failure to get write access is not
always fatal; proving that we'll never end up truncating in that
case is rather convoluted.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/namei.c

index 2f8a5d3..2eef41f 100644 (file)
@@ -3191,11 +3191,14 @@ static const char *do_last(struct nameidata *nd,
        else
                inode_unlock_shared(dir->d_inode);
 
-       if (IS_ERR(dentry)) {
-               error = PTR_ERR(dentry);
-               goto out;
+       if (got_write) {
+               mnt_drop_write(nd->path.mnt);
+               got_write = false;
        }
 
+       if (IS_ERR(dentry))
+               return ERR_CAST(dentry);
+
        if (file->f_mode & FMODE_OPENED) {
                if (file->f_mode & FMODE_CREATED) {
                        open_flag &= ~O_TRUNC;
@@ -3220,16 +3223,6 @@ static const char *do_last(struct nameidata *nd,
                goto finish_open_created;
        }
 
-       /*
-        * If atomic_open() acquired write access it is dropped now due to
-        * possible mount and symlink following (this might be optimized away if
-        * necessary...)
-        */
-       if (got_write) {
-               mnt_drop_write(nd->path.mnt);
-               got_write = false;
-       }
-
 finish_lookup:
        if (nd->depth)
                put_link(nd);
@@ -3250,27 +3243,25 @@ finish_open:
                return ERR_PTR(error);
        audit_inode(nd->name, nd->path.dentry, 0);
        if (open_flag & O_CREAT) {
-               error = -EISDIR;
                if (d_is_dir(nd->path.dentry))
-                       goto out;
+                       return ERR_PTR(-EISDIR);
                error = may_create_in_sticky(dir_mode, dir_uid,
                                             d_backing_inode(nd->path.dentry));
                if (unlikely(error))
-                       goto out;
+                       return ERR_PTR(error);
        }
-       error = -ENOTDIR;
        if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
-               goto out;
+               return ERR_PTR(-ENOTDIR);
        if (!d_is_reg(nd->path.dentry))
                will_truncate = false;
 
+finish_open_created:
        if (will_truncate) {
                error = mnt_want_write(nd->path.mnt);
                if (error)
-                       goto out;
+                       return ERR_PTR(error);
                got_write = true;
        }
-finish_open_created:
        error = may_open(&nd->path, acc_mode, open_flag);
        if (error)
                goto out;