Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[firefly-linux-kernel-4.4.55.git] / fs / fuse / dir.c
index 72a5d5b04494ded2468bc4a2f5f30e30c9ca12f7..3ac91086f41f528c9e570f6925389d968a4076ff 100644 (file)
@@ -182,10 +182,11 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
        struct inode *inode;
        struct dentry *parent;
        struct fuse_conn *fc;
+       int ret;
 
        inode = ACCESS_ONCE(entry->d_inode);
        if (inode && is_bad_inode(inode))
-               return 0;
+               goto invalid;
        else if (fuse_dentry_time(entry) < get_jiffies_64()) {
                int err;
                struct fuse_entry_out outarg;
@@ -195,20 +196,23 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
 
                /* For negative dentries, always do a fresh lookup */
                if (!inode)
-                       return 0;
+                       goto invalid;
 
+               ret = -ECHILD;
                if (flags & LOOKUP_RCU)
-                       return -ECHILD;
+                       goto out;
 
                fc = get_fuse_conn(inode);
                req = fuse_get_req_nopages(fc);
+               ret = PTR_ERR(req);
                if (IS_ERR(req))
-                       return 0;
+                       goto out;
 
                forget = fuse_alloc_forget();
                if (!forget) {
                        fuse_put_request(fc, req);
-                       return 0;
+                       ret = -ENOMEM;
+                       goto out;
                }
 
                attr_version = fuse_get_attr_version(fc);
@@ -227,7 +231,7 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
                        struct fuse_inode *fi = get_fuse_inode(inode);
                        if (outarg.nodeid != get_node_id(inode)) {
                                fuse_queue_forget(fc, forget, outarg.nodeid, 1);
-                               return 0;
+                               goto invalid;
                        }
                        spin_lock(&fc->lock);
                        fi->nlookup++;
@@ -235,7 +239,7 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
                }
                kfree(forget);
                if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
-                       return 0;
+                       goto invalid;
 
                fuse_change_attributes(inode, &outarg.attr,
                                       entry_attr_timeout(&outarg),
@@ -249,7 +253,15 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
                        dput(parent);
                }
        }
-       return 1;
+       ret = 1;
+out:
+       return ret;
+
+invalid:
+       ret = 0;
+       if (check_submounts_and_drop(entry) != 0)
+               ret = 1;
+       goto out;
 }
 
 static int invalid_nodeid(u64 nodeid)
@@ -267,26 +279,6 @@ int fuse_valid_type(int m)
                S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
 }
 
-/*
- * Add a directory inode to a dentry, ensuring that no other dentry
- * refers to this inode.  Called with fc->inst_mutex.
- */
-static struct dentry *fuse_d_add_directory(struct dentry *entry,
-                                          struct inode *inode)
-{
-       struct dentry *alias = d_find_alias(inode);
-       if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
-               /* This tries to shrink the subtree below alias */
-               fuse_invalidate_entry(alias);
-               dput(alias);
-               if (!hlist_empty(&inode->i_dentry))
-                       return ERR_PTR(-EBUSY);
-       } else {
-               dput(alias);
-       }
-       return d_splice_alias(inode, entry);
-}
-
 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
                     struct fuse_entry_out *outarg, struct inode **inode)
 {
@@ -345,6 +337,24 @@ int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
        return err;
 }
 
+static struct dentry *fuse_materialise_dentry(struct dentry *dentry,
+                                             struct inode *inode)
+{
+       struct dentry *newent;
+
+       if (inode && S_ISDIR(inode->i_mode)) {
+               struct fuse_conn *fc = get_fuse_conn(inode);
+
+               mutex_lock(&fc->inst_mutex);
+               newent = d_materialise_unique(dentry, inode);
+               mutex_unlock(&fc->inst_mutex);
+       } else {
+               newent = d_materialise_unique(dentry, inode);
+       }
+
+       return newent;
+}
+
 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
                                  unsigned int flags)
 {
@@ -352,7 +362,6 @@ static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
        struct fuse_entry_out outarg;
        struct inode *inode;
        struct dentry *newent;
-       struct fuse_conn *fc = get_fuse_conn(dir);
        bool outarg_valid = true;
 
        err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
@@ -368,16 +377,10 @@ static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
        if (inode && get_node_id(inode) == FUSE_ROOT_ID)
                goto out_iput;
 
-       if (inode && S_ISDIR(inode->i_mode)) {
-               mutex_lock(&fc->inst_mutex);
-               newent = fuse_d_add_directory(entry, inode);
-               mutex_unlock(&fc->inst_mutex);
-               err = PTR_ERR(newent);
-               if (IS_ERR(newent))
-                       goto out_iput;
-       } else {
-               newent = d_splice_alias(inode, entry);
-       }
+       newent = fuse_materialise_dentry(entry, inode);
+       err = PTR_ERR(newent);
+       if (IS_ERR(newent))
+               goto out_err;
 
        entry = newent ? newent : entry;
        if (outarg_valid)
@@ -1174,6 +1177,8 @@ static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
                        return -EIO;
                if (reclen > nbytes)
                        break;
+               if (memchr(dirent->name, '/', dirent->namelen) != NULL)
+                       return -EIO;
 
                if (!dir_emit(ctx, dirent->name, dirent->namelen,
                               dirent->ino, dirent->type))
@@ -1275,18 +1280,10 @@ static int fuse_direntplus_link(struct file *file,
        if (!inode)
                goto out;
 
-       if (S_ISDIR(inode->i_mode)) {
-               mutex_lock(&fc->inst_mutex);
-               alias = fuse_d_add_directory(dentry, inode);
-               mutex_unlock(&fc->inst_mutex);
-               err = PTR_ERR(alias);
-               if (IS_ERR(alias)) {
-                       iput(inode);
-                       goto out;
-               }
-       } else {
-               alias = d_splice_alias(inode, dentry);
-       }
+       alias = fuse_materialise_dentry(dentry, inode);
+       err = PTR_ERR(alias);
+       if (IS_ERR(alias))
+               goto out;
 
        if (alias) {
                dput(dentry);
@@ -1320,6 +1317,8 @@ static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
                        return -EIO;
                if (reclen > nbytes)
                        break;
+               if (memchr(dirent->name, '/', dirent->namelen) != NULL)
+                       return -EIO;
 
                if (!over) {
                        /* We fill entries into dstbuf only as much as
@@ -1590,6 +1589,7 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
                    struct file *file)
 {
        struct fuse_conn *fc = get_fuse_conn(inode);
+       struct fuse_inode *fi = get_fuse_inode(inode);
        struct fuse_req *req;
        struct fuse_setattr_in inarg;
        struct fuse_attr_out outarg;
@@ -1617,8 +1617,10 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
        if (IS_ERR(req))
                return PTR_ERR(req);
 
-       if (is_truncate)
+       if (is_truncate) {
                fuse_set_nowrite(inode);
+               set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
+       }
 
        memset(&inarg, 0, sizeof(inarg));
        memset(&outarg, 0, sizeof(outarg));
@@ -1680,12 +1682,14 @@ int fuse_do_setattr(struct inode *inode, struct iattr *attr,
                invalidate_inode_pages2(inode->i_mapping);
        }
 
+       clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
        return 0;
 
 error:
        if (is_truncate)
                fuse_release_nowrite(inode);
 
+       clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
        return err;
 }
 
@@ -1749,6 +1753,8 @@ static int fuse_setxattr(struct dentry *entry, const char *name,
                fc->no_setxattr = 1;
                err = -EOPNOTSUPP;
        }
+       if (!err)
+               fuse_invalidate_attr(inode);
        return err;
 }
 
@@ -1878,6 +1884,8 @@ static int fuse_removexattr(struct dentry *entry, const char *name)
                fc->no_removexattr = 1;
                err = -EOPNOTSUPP;
        }
+       if (!err)
+               fuse_invalidate_attr(inode);
        return err;
 }