Merge branch 'fixes-davem' of master.kernel.org:/pub/scm/linux/kernel/git/linville...
[powerpc.git] / fs / fuse / dir.c
index a058b66..3763757 100644 (file)
@@ -116,13 +116,19 @@ static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
                             struct dentry *entry,
                             struct fuse_entry_out *outarg)
 {
+       struct fuse_conn *fc = get_fuse_conn(dir);
+
+       memset(outarg, 0, sizeof(struct fuse_entry_out));
        req->in.h.opcode = FUSE_LOOKUP;
        req->in.h.nodeid = get_node_id(dir);
        req->in.numargs = 1;
        req->in.args[0].size = entry->d_name.len + 1;
        req->in.args[0].value = entry->d_name.name;
        req->out.numargs = 1;
-       req->out.args[0].size = sizeof(struct fuse_entry_out);
+       if (fc->minor < 9)
+               req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
+       else
+               req->out.args[0].size = sizeof(struct fuse_entry_out);
        req->out.args[0].value = outarg;
 }
 
@@ -356,6 +362,7 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
 
        flags &= ~O_NOCTTY;
        memset(&inarg, 0, sizeof(inarg));
+       memset(&outentry, 0, sizeof(outentry));
        inarg.flags = flags;
        inarg.mode = mode;
        req->in.h.opcode = FUSE_CREATE;
@@ -366,7 +373,10 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
        req->in.args[1].size = entry->d_name.len + 1;
        req->in.args[1].value = entry->d_name.name;
        req->out.numargs = 2;
-       req->out.args[0].size = sizeof(outentry);
+       if (fc->minor < 9)
+               req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
+       else
+               req->out.args[0].size = sizeof(outentry);
        req->out.args[0].value = &outentry;
        req->out.args[1].size = sizeof(outopen);
        req->out.args[1].value = &outopen;
@@ -431,9 +441,13 @@ static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
                return PTR_ERR(forget_req);
        }
 
+       memset(&outarg, 0, sizeof(outarg));
        req->in.h.nodeid = get_node_id(dir);
        req->out.numargs = 1;
-       req->out.args[0].size = sizeof(outarg);
+       if (fc->minor < 9)
+               req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
+       else
+               req->out.args[0].size = sizeof(outarg);
        req->out.args[0].value = &outarg;
        request_send(fc, req);
        err = req->out.h.error;
@@ -705,10 +719,12 @@ static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
        stat->blksize = (1 << inode->i_blkbits);
 }
 
-static int fuse_do_getattr(struct inode *inode, struct kstat *stat)
+static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
+                          struct file *file)
 {
        int err;
-       struct fuse_attr_out arg;
+       struct fuse_getattr_in inarg;
+       struct fuse_attr_out outarg;
        struct fuse_conn *fc = get_fuse_conn(inode);
        struct fuse_req *req;
        u64 attr_version;
@@ -721,24 +737,39 @@ static int fuse_do_getattr(struct inode *inode, struct kstat *stat)
        attr_version = fc->attr_version;
        spin_unlock(&fc->lock);
 
+       memset(&inarg, 0, sizeof(inarg));
+       memset(&outarg, 0, sizeof(outarg));
+       /* Directories have separate file-handle space */
+       if (file && S_ISREG(inode->i_mode)) {
+               struct fuse_file *ff = file->private_data;
+
+               inarg.getattr_flags |= FUSE_GETATTR_FH;
+               inarg.fh = ff->fh;
+       }
        req->in.h.opcode = FUSE_GETATTR;
        req->in.h.nodeid = get_node_id(inode);
+       req->in.numargs = 1;
+       req->in.args[0].size = sizeof(inarg);
+       req->in.args[0].value = &inarg;
        req->out.numargs = 1;
-       req->out.args[0].size = sizeof(arg);
-       req->out.args[0].value = &arg;
+       if (fc->minor < 9)
+               req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
+       else
+               req->out.args[0].size = sizeof(outarg);
+       req->out.args[0].value = &outarg;
        request_send(fc, req);
        err = req->out.h.error;
        fuse_put_request(fc, req);
        if (!err) {
-               if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
+               if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
                        make_bad_inode(inode);
                        err = -EIO;
                } else {
-                       fuse_change_attributes(inode, &arg.attr,
-                                              attr_timeout(&arg),
+                       fuse_change_attributes(inode, &outarg.attr,
+                                              attr_timeout(&outarg),
                                               attr_version);
                        if (stat)
-                               fuse_fillattr(inode, &arg.attr, stat);
+                               fuse_fillattr(inode, &outarg.attr, stat);
                }
        }
        return err;
@@ -833,7 +864,7 @@ static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
            ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
                struct fuse_inode *fi = get_fuse_inode(inode);
                if (fi->i_time < get_jiffies_64()) {
-                       err = fuse_do_getattr(inode, NULL);
+                       err = fuse_do_getattr(inode, NULL, NULL);
                        if (err)
                                return err;
 
@@ -848,7 +879,7 @@ static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
                   attributes.  This is also needed, because the root
                   node will at first have no permissions */
                if (err == -EACCES && !refreshed) {
-                       err = fuse_do_getattr(inode, NULL);
+                       err = fuse_do_getattr(inode, NULL, NULL);
                        if (!err)
                                err = generic_permission(inode, mask, NULL);
                }
@@ -864,7 +895,7 @@ static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
                        if (refreshed)
                                return -EACCES;
 
-                       err = fuse_do_getattr(inode, NULL);
+                       err = fuse_do_getattr(inode, NULL, NULL);
                        if (!err && !(inode->i_mode & S_IXUGO))
                                return -EACCES;
                }
@@ -1001,6 +1032,20 @@ static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
        return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
 }
 
+static bool update_mtime(unsigned ivalid)
+{
+       /* Always update if mtime is explicitly set  */
+       if (ivalid & ATTR_MTIME_SET)
+               return true;
+
+       /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
+       if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
+               return false;
+
+       /* In all other cases update */
+       return true;
+}
+
 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
 {
        unsigned ivalid = iattr->ia_valid;
@@ -1013,16 +1058,19 @@ static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
                arg->valid |= FATTR_GID,    arg->gid = iattr->ia_gid;
        if (ivalid & ATTR_SIZE)
                arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
-       /* You can only _set_ these together (they may change by themselves) */
-       if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
-               arg->valid |= FATTR_ATIME | FATTR_MTIME;
+       if (ivalid & ATTR_ATIME) {
+               arg->valid |= FATTR_ATIME;
                arg->atime = iattr->ia_atime.tv_sec;
-               arg->mtime = iattr->ia_mtime.tv_sec;
+               arg->atimensec = iattr->ia_atime.tv_nsec;
+               if (!(ivalid & ATTR_ATIME_SET))
+                       arg->valid |= FATTR_ATIME_NOW;
        }
-       if (ivalid & ATTR_FILE) {
-               struct fuse_file *ff = iattr->ia_file->private_data;
-               arg->valid |= FATTR_FH;
-               arg->fh = ff->fh;
+       if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
+               arg->valid |= FATTR_MTIME;
+               arg->mtime = iattr->ia_mtime.tv_sec;
+               arg->mtimensec = iattr->ia_mtime.tv_nsec;
+               if (!(ivalid & ATTR_MTIME_SET))
+                       arg->valid |= FATTR_MTIME_NOW;
        }
 }
 
@@ -1034,7 +1082,8 @@ static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
  * vmtruncate() doesn't allow for this case, so do the rlimit checking
  * and the actual truncation by hand.
  */
-static int fuse_setattr(struct dentry *entry, struct iattr *attr)
+static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
+                          struct file *file)
 {
        struct inode *inode = entry->d_inode;
        struct fuse_conn *fc = get_fuse_conn(inode);
@@ -1052,6 +1101,9 @@ static int fuse_setattr(struct dentry *entry, struct iattr *attr)
                        return err;
        }
 
+       if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc)
+               return 0;
+
        if (attr->ia_valid & ATTR_SIZE) {
                unsigned long limit;
                if (IS_SWAPFILE(inode))
@@ -1068,14 +1120,28 @@ static int fuse_setattr(struct dentry *entry, struct iattr *attr)
                return PTR_ERR(req);
 
        memset(&inarg, 0, sizeof(inarg));
+       memset(&outarg, 0, sizeof(outarg));
        iattr_to_fattr(attr, &inarg);
+       if (file) {
+               struct fuse_file *ff = file->private_data;
+               inarg.valid |= FATTR_FH;
+               inarg.fh = ff->fh;
+       }
+       if (attr->ia_valid & ATTR_SIZE) {
+               /* For mandatory locking in truncate */
+               inarg.valid |= FATTR_LOCKOWNER;
+               inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
+       }
        req->in.h.opcode = FUSE_SETATTR;
        req->in.h.nodeid = get_node_id(inode);
        req->in.numargs = 1;
        req->in.args[0].size = sizeof(inarg);
        req->in.args[0].value = &inarg;
        req->out.numargs = 1;
-       req->out.args[0].size = sizeof(outarg);
+       if (fc->minor < 9)
+               req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
+       else
+               req->out.args[0].size = sizeof(outarg);
        req->out.args[0].value = &outarg;
        request_send(fc, req);
        err = req->out.h.error;
@@ -1095,6 +1161,14 @@ static int fuse_setattr(struct dentry *entry, struct iattr *attr)
        return 0;
 }
 
+static int fuse_setattr(struct dentry *entry, struct iattr *attr)
+{
+       if (attr->ia_valid & ATTR_FILE)
+               return fuse_do_setattr(entry, attr, attr->ia_file);
+       else
+               return fuse_do_setattr(entry, attr, NULL);
+}
+
 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
                        struct kstat *stat)
 {
@@ -1107,7 +1181,7 @@ static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
                return -EACCES;
 
        if (fi->i_time < get_jiffies_64())
-               err = fuse_do_getattr(inode, stat);
+               err = fuse_do_getattr(inode, stat, NULL);
        else {
                err = 0;
                generic_fillattr(inode, stat);