* vmtruncate() doesn't allow for this case, so do the rlimit checking
  * and the actual truncation by hand.
  */
-static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
-                          struct file *file)
+int fuse_do_setattr(struct inode *inode, struct iattr *attr,
+                   struct file *file)
 {
-       struct inode *inode = entry->d_inode;
        struct fuse_conn *fc = get_fuse_conn(inode);
        struct fuse_req *req;
        struct fuse_setattr_in inarg;
        loff_t oldsize;
        int err;
 
-       if (!fuse_allow_current_process(fc))
-               return -EACCES;
-
        if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
                attr->ia_valid |= ATTR_FORCE;
 
 
 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
 {
+       struct inode *inode = entry->d_inode;
+
+       if (!fuse_allow_current_process(get_fuse_conn(inode)))
+               return -EACCES;
+
        if (attr->ia_valid & ATTR_FILE)
-               return fuse_do_setattr(entry, attr, attr->ia_file);
+               return fuse_do_setattr(inode, attr, attr->ia_file);
        else
-               return fuse_do_setattr(entry, attr, NULL);
+               return fuse_do_setattr(inode, attr, NULL);
 }
 
 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
 
        return 0;
 }
 
+static void fuse_do_truncate(struct file *file)
+{
+       struct inode *inode = file->f_mapping->host;
+       struct iattr attr;
+
+       attr.ia_valid = ATTR_SIZE;
+       attr.ia_size = i_size_read(inode);
+
+       attr.ia_file = file;
+       attr.ia_valid |= ATTR_FILE;
+
+       fuse_do_setattr(inode, &attr, file);
+}
+
 static ssize_t
 fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
                        loff_t offset, unsigned long nr_segs)
                kfree(io);
        }
 
-       if (rw == WRITE && ret > 0)
-               fuse_write_update_size(inode, pos);
+       if (rw == WRITE) {
+               if (ret > 0)
+                       fuse_write_update_size(inode, pos);
+               else if (ret < 0 && offset + count > i_size)
+                       fuse_do_truncate(file);
+       }
 
        return ret;
 }