ovl: store ovl_entry in inode->i_private for all inodes
[cascardo/linux.git] / fs / overlayfs / inode.c
index c831c2e..32ae8b4 100644 (file)
@@ -80,6 +80,9 @@ int ovl_setattr(struct dentry *dentry, struct iattr *attr)
                                goto out_drop_write;
                }
 
+               if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
+                       attr->ia_valid &= ~ATTR_MODE;
+
                inode_lock(upperdentry->d_inode);
                err = notify_change(upperdentry, attr, NULL);
                if (!err)
@@ -106,31 +109,12 @@ static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
 
 int ovl_permission(struct inode *inode, int mask)
 {
-       struct ovl_entry *oe;
-       struct dentry *alias = NULL;
-       struct inode *realinode;
-       struct dentry *realdentry;
+       struct ovl_entry *oe = inode->i_private;
        bool is_upper;
+       struct dentry *realdentry = ovl_entry_real(oe, &is_upper);
+       struct inode *realinode;
        int err;
 
-       if (S_ISDIR(inode->i_mode)) {
-               oe = inode->i_private;
-       } else if (mask & MAY_NOT_BLOCK) {
-               return -ECHILD;
-       } else {
-               /*
-                * For non-directories find an alias and get the info
-                * from there.
-                */
-               alias = d_find_any_alias(inode);
-               if (WARN_ON(!alias))
-                       return -ENOENT;
-
-               oe = alias->d_fsdata;
-       }
-
-       realdentry = ovl_entry_real(oe, &is_upper);
-
        if (ovl_is_default_permissions(inode)) {
                struct kstat stat;
                struct path realpath = { .dentry = realdentry };
@@ -142,26 +126,23 @@ int ovl_permission(struct inode *inode, int mask)
 
                err = vfs_getattr(&realpath, &stat);
                if (err)
-                       goto out_dput;
+                       return err;
 
-               err = -ESTALE;
                if ((stat.mode ^ inode->i_mode) & S_IFMT)
-                       goto out_dput;
+                       return -ESTALE;
 
                inode->i_mode = stat.mode;
                inode->i_uid = stat.uid;
                inode->i_gid = stat.gid;
 
-               err = generic_permission(inode, mask);
-               goto out_dput;
+               return generic_permission(inode, mask);
        }
 
        /* Careful in RCU walk mode */
-       realinode = ACCESS_ONCE(realdentry->d_inode);
+       realinode = d_inode_rcu(realdentry);
        if (!realinode) {
                WARN_ON(!(mask & MAY_NOT_BLOCK));
-               err = -ENOENT;
-               goto out_dput;
+               return -ENOENT;
        }
 
        if (mask & MAY_WRITE) {
@@ -180,16 +161,12 @@ int ovl_permission(struct inode *inode, int mask)
                 * constructed return EROFS to prevent modification of
                 * upper layer.
                 */
-               err = -EROFS;
                if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
                    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
-                       goto out_dput;
+                       return -EROFS;
        }
 
-       err = __inode_permission(realinode, mask);
-out_dput:
-       dput(alias);
-       return err;
+       return __inode_permission(realinode, mask);
 }
 
 static const char *ovl_get_link(struct dentry *dentry,
@@ -348,36 +325,25 @@ static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
        return true;
 }
 
-struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
+int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
 {
-       int err;
+       int err = 0;
        struct path realpath;
        enum ovl_path_type type;
 
-       if (d_is_dir(dentry))
-               return d_backing_inode(dentry);
-
        type = ovl_path_real(dentry, &realpath);
        if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
                err = ovl_want_write(dentry);
-               if (err)
-                       return ERR_PTR(err);
-
-               if (file_flags & O_TRUNC)
-                       err = ovl_copy_up_truncate(dentry);
-               else
-                       err = ovl_copy_up(dentry);
-               ovl_drop_write(dentry);
-               if (err)
-                       return ERR_PTR(err);
-
-               ovl_path_upper(dentry, &realpath);
+               if (!err) {
+                       if (file_flags & O_TRUNC)
+                               err = ovl_copy_up_truncate(dentry);
+                       else
+                               err = ovl_copy_up(dentry);
+                       ovl_drop_write(dentry);
+               }
        }
 
-       if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
-               return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
-
-       return d_backing_inode(realpath.dentry);
+       return err;
 }
 
 static const struct inode_operations ovl_file_inode_operations = {
@@ -410,15 +376,14 @@ struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
        if (!inode)
                return NULL;
 
-       mode &= S_IFMT;
-
        inode->i_ino = get_next_ino();
        inode->i_mode = mode;
        inode->i_flags |= S_NOATIME | S_NOCMTIME;
+       inode->i_private = oe;
 
+       mode &= S_IFMT;
        switch (mode) {
        case S_IFDIR:
-               inode->i_private = oe;
                inode->i_op = &ovl_dir_inode_operations;
                inode->i_fop = &ovl_dir_operations;
                break;