Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszer...
[sfrench/cifs-2.6.git] / fs / overlayfs / copy_up.c
index 36795eed40b09ee1bbb6766e65019b8d1c6695f0..906ea6c93260179c9c4947abe97c35750162c3e3 100644 (file)
 #include <linux/xattr.h>
 #include <linux/security.h>
 #include <linux/uaccess.h>
-#include <linux/sched.h>
+#include <linux/sched/signal.h>
+#include <linux/cred.h>
 #include <linux/namei.h>
 #include <linux/fdtable.h>
 #include <linux/ratelimit.h>
 #include "overlayfs.h"
+#include "ovl_entry.h"
 
 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
 
@@ -33,7 +35,7 @@ static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
 {
        const struct dentry *dentry = data;
 
-       if (f->f_inode == d_inode(dentry))
+       if (file_inode(f) == d_inode(dentry))
                pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
                                    f, fd, current->pid, current->comm);
        return 0;
@@ -153,6 +155,13 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
                goto out_fput;
        }
 
+       /* Try to use clone_file_range to clone up within the same fs */
+       error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
+       if (!error)
+               goto out;
+       /* Couldn't clone, so now we try to copy the data */
+       error = 0;
+
        /* FIXME: copy up sparse files efficiently */
        while (len) {
                size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
@@ -177,7 +186,7 @@ static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
 
                len -= bytes;
        }
-
+out:
        if (!error)
                error = vfs_fsync(new_file, 0);
        fput(new_file);
@@ -225,39 +234,48 @@ int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
 
 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
                              struct dentry *dentry, struct path *lowerpath,
-                             struct kstat *stat, const char *link)
+                             struct kstat *stat, const char *link,
+                             struct kstat *pstat, bool tmpfile)
 {
        struct inode *wdir = workdir->d_inode;
        struct inode *udir = upperdir->d_inode;
        struct dentry *newdentry = NULL;
        struct dentry *upper = NULL;
-       umode_t mode = stat->mode;
+       struct dentry *temp = NULL;
        int err;
        const struct cred *old_creds = NULL;
        struct cred *new_creds = NULL;
-
-       newdentry = ovl_lookup_temp(workdir, dentry);
-       err = PTR_ERR(newdentry);
-       if (IS_ERR(newdentry))
-               goto out;
+       struct cattr cattr = {
+               /* Can't properly set mode on creation because of the umask */
+               .mode = stat->mode & S_IFMT,
+               .rdev = stat->rdev,
+               .link = link
+       };
 
        upper = lookup_one_len(dentry->d_name.name, upperdir,
                               dentry->d_name.len);
        err = PTR_ERR(upper);
        if (IS_ERR(upper))
-               goto out1;
+               goto out;
 
        err = security_inode_copy_up(dentry, &new_creds);
        if (err < 0)
-               goto out2;
+               goto out1;
 
        if (new_creds)
                old_creds = override_creds(new_creds);
 
-       /* Can't properly set mode on creation because of the umask */
-       stat->mode &= S_IFMT;
-       err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
-       stat->mode = mode;
+       if (tmpfile)
+               temp = ovl_do_tmpfile(upperdir, stat->mode);
+       else
+               temp = ovl_lookup_temp(workdir, dentry);
+       err = PTR_ERR(temp);
+       if (IS_ERR(temp))
+               goto out1;
+
+       err = 0;
+       if (!tmpfile)
+               err = ovl_create_real(wdir, temp, &cattr, NULL, true);
 
        if (new_creds) {
                revert_creds(old_creds);
@@ -272,65 +290,69 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
 
                ovl_path_upper(dentry, &upperpath);
                BUG_ON(upperpath.dentry != NULL);
-               upperpath.dentry = newdentry;
+               upperpath.dentry = temp;
+
+               if (tmpfile) {
+                       inode_unlock(udir);
+                       err = ovl_copy_up_data(lowerpath, &upperpath,
+                                              stat->size);
+                       inode_lock_nested(udir, I_MUTEX_PARENT);
+               } else {
+                       err = ovl_copy_up_data(lowerpath, &upperpath,
+                                              stat->size);
+               }
 
-               err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
                if (err)
                        goto out_cleanup;
        }
 
-       err = ovl_copy_xattr(lowerpath->dentry, newdentry);
+       err = ovl_copy_xattr(lowerpath->dentry, temp);
        if (err)
                goto out_cleanup;
 
-       inode_lock(newdentry->d_inode);
-       err = ovl_set_attr(newdentry, stat);
-       inode_unlock(newdentry->d_inode);
+       inode_lock(temp->d_inode);
+       err = ovl_set_attr(temp, stat);
+       inode_unlock(temp->d_inode);
        if (err)
                goto out_cleanup;
 
-       err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
+       if (tmpfile)
+               err = ovl_do_link(temp, udir, upper, true);
+       else
+               err = ovl_do_rename(wdir, temp, udir, upper, 0);
        if (err)
                goto out_cleanup;
 
+       newdentry = dget(tmpfile ? upper : temp);
        ovl_dentry_update(dentry, newdentry);
        ovl_inode_update(d_inode(dentry), d_inode(newdentry));
-       newdentry = NULL;
 
-       /*
-        * Non-directores become opaque when copied up.
-        */
-       if (!S_ISDIR(stat->mode))
-               ovl_dentry_set_opaque(dentry, true);
+       /* Restore timestamps on parent (best effort) */
+       ovl_set_timestamps(upperdir, pstat);
 out2:
-       dput(upper);
+       dput(temp);
 out1:
-       dput(newdentry);
+       dput(upper);
 out:
        return err;
 
 out_cleanup:
-       ovl_cleanup(wdir, newdentry);
+       if (!tmpfile)
+               ovl_cleanup(wdir, temp);
        goto out2;
 }
 
 /*
  * Copy up a single dentry
  *
- * Directory renames only allowed on "pure upper" (already created on
- * upper filesystem, never copied up).  Directories which are on lower or
- * are merged may not be renamed.  For these -EXDEV is returned and
- * userspace has to deal with it.  This means, when copying up a
- * directory we can rely on it and ancestors being stable.
- *
- * Non-directory renames start with copy up of source if necessary.  The
- * actual rename will only proceed once the copy up was successful.  Copy
- * up uses upper parent i_mutex for exclusion.  Since rename can change
- * d_parent it is possible that the copy up will lock the old parent.  At
- * that point the file will have already been copied up anyway.
+ * All renames start with copy up of source if necessary.  The actual
+ * rename will only proceed once the copy up was successful.  Copy up uses
+ * upper parent i_mutex for exclusion.  Since rename can change d_parent it
+ * is possible that the copy up will lock the old parent.  At that point
+ * the file will have already been copied up anyway.
  */
-int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
-                   struct path *lowerpath, struct kstat *stat)
+static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
+                          struct path *lowerpath, struct kstat *stat)
 {
        DEFINE_DELAYED_CALL(done);
        struct dentry *workdir = ovl_workdir(dentry);
@@ -339,8 +361,8 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
        struct path parentpath;
        struct dentry *lowerdentry = lowerpath->dentry;
        struct dentry *upperdir;
-       struct dentry *upperdentry;
        const char *link = NULL;
+       struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
 
        if (WARN_ON(!workdir))
                return -EROFS;
@@ -350,7 +372,8 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
        ovl_path_upper(parent, &parentpath);
        upperdir = parentpath.dentry;
 
-       err = vfs_getattr(&parentpath, &pstat);
+       err = vfs_getattr(&parentpath, &pstat,
+                         STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
        if (err)
                return err;
 
@@ -360,32 +383,47 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
                        return PTR_ERR(link);
        }
 
+       /* Should we copyup with O_TMPFILE or with workdir? */
+       if (S_ISREG(stat->mode) && ofs->tmpfile) {
+               err = ovl_copy_up_start(dentry);
+               /* err < 0: interrupted, err > 0: raced with another copy-up */
+               if (unlikely(err)) {
+                       pr_debug("ovl_copy_up_start(%pd2) = %i\n", dentry, err);
+                       if (err > 0)
+                               err = 0;
+                       goto out_done;
+               }
+
+               inode_lock_nested(upperdir->d_inode, I_MUTEX_PARENT);
+               err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
+                                        stat, link, &pstat, true);
+               inode_unlock(upperdir->d_inode);
+               ovl_copy_up_end(dentry);
+               goto out_done;
+       }
+
        err = -EIO;
        if (lock_rename(workdir, upperdir) != NULL) {
                pr_err("overlayfs: failed to lock workdir+upperdir\n");
                goto out_unlock;
        }
-       upperdentry = ovl_dentry_upper(dentry);
-       if (upperdentry) {
+       if (ovl_dentry_upper(dentry)) {
                /* Raced with another copy-up?  Nothing to do, then... */
                err = 0;
                goto out_unlock;
        }
 
        err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
-                                stat, link);
-       if (!err) {
-               /* Restore timestamps on parent (best effort) */
-               ovl_set_timestamps(upperdir, &pstat);
-       }
+                                stat, link, &pstat, false);
 out_unlock:
        unlock_rename(workdir, upperdir);
+out_done:
        do_delayed_call(&done);
 
        return err;
 }
 
-int ovl_copy_up(struct dentry *dentry)
+int ovl_copy_up_flags(struct dentry *dentry, int flags)
 {
        int err = 0;
        const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
@@ -414,7 +452,11 @@ int ovl_copy_up(struct dentry *dentry)
                }
 
                ovl_path_lower(next, &lowerpath);
-               err = vfs_getattr(&lowerpath, &stat);
+               err = vfs_getattr(&lowerpath, &stat,
+                                 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
+               /* maybe truncate regular file. this has no effect on dirs */
+               if (flags & O_TRUNC)
+                       stat.size = 0;
                if (!err)
                        err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
 
@@ -425,3 +467,8 @@ int ovl_copy_up(struct dentry *dentry)
 
        return err;
 }
+
+int ovl_copy_up(struct dentry *dentry)
+{
+       return ovl_copy_up_flags(dentry, 0);
+}