LCOV - code coverage report
Current view: top level - fs/overlayfs - dir.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 41 740 5.5 %
Date: 2021-04-22 12:43:58 Functions: 5 32 15.6 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  *
       4             :  * Copyright (C) 2011 Novell Inc.
       5             :  */
       6             : 
       7             : #include <linux/fs.h>
       8             : #include <linux/namei.h>
       9             : #include <linux/xattr.h>
      10             : #include <linux/security.h>
      11             : #include <linux/cred.h>
      12             : #include <linux/module.h>
      13             : #include <linux/posix_acl.h>
      14             : #include <linux/posix_acl_xattr.h>
      15             : #include <linux/atomic.h>
      16             : #include <linux/ratelimit.h>
      17             : #include "overlayfs.h"
      18             : 
      19             : static unsigned short ovl_redirect_max = 256;
      20             : module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
      21             : MODULE_PARM_DESC(redirect_max,
      22             :                  "Maximum length of absolute redirect xattr value");
      23             : 
      24             : static int ovl_set_redirect(struct dentry *dentry, bool samedir);
      25             : 
      26           4 : int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
      27             : {
      28           4 :         int err;
      29             : 
      30           4 :         dget(wdentry);
      31           8 :         if (d_is_dir(wdentry))
      32           0 :                 err = ovl_do_rmdir(wdir, wdentry);
      33             :         else
      34           4 :                 err = ovl_do_unlink(wdir, wdentry);
      35           4 :         dput(wdentry);
      36             : 
      37           4 :         if (err) {
      38           0 :                 pr_err("cleanup of '%pd2' failed (%i)\n",
      39             :                        wdentry, err);
      40             :         }
      41             : 
      42           4 :         return err;
      43             : }
      44             : 
      45           5 : struct dentry *ovl_lookup_temp(struct dentry *workdir)
      46             : {
      47           5 :         struct dentry *temp;
      48           5 :         char name[20];
      49           5 :         static atomic_t temp_id = ATOMIC_INIT(0);
      50             : 
      51             :         /* counter is allowed to wrap, since temp dentries are ephemeral */
      52           5 :         snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
      53             : 
      54           5 :         temp = lookup_one_len(name, workdir, strlen(name));
      55           5 :         if (!IS_ERR(temp) && temp->d_inode) {
      56           0 :                 pr_err("workdir/%s already exists\n", name);
      57           0 :                 dput(temp);
      58           0 :                 temp = ERR_PTR(-EIO);
      59             :         }
      60             : 
      61           5 :         return temp;
      62             : }
      63             : 
      64             : /* caller holds i_mutex on workdir */
      65           0 : static struct dentry *ovl_whiteout(struct ovl_fs *ofs)
      66             : {
      67           0 :         int err;
      68           0 :         struct dentry *whiteout;
      69           0 :         struct dentry *workdir = ofs->workdir;
      70           0 :         struct inode *wdir = workdir->d_inode;
      71             : 
      72           0 :         if (!ofs->whiteout) {
      73           0 :                 whiteout = ovl_lookup_temp(workdir);
      74           0 :                 if (IS_ERR(whiteout))
      75           0 :                         goto out;
      76             : 
      77           0 :                 err = ovl_do_whiteout(wdir, whiteout);
      78           0 :                 if (err) {
      79           0 :                         dput(whiteout);
      80           0 :                         whiteout = ERR_PTR(err);
      81           0 :                         goto out;
      82             :                 }
      83           0 :                 ofs->whiteout = whiteout;
      84             :         }
      85             : 
      86           0 :         if (ofs->share_whiteout) {
      87           0 :                 whiteout = ovl_lookup_temp(workdir);
      88           0 :                 if (IS_ERR(whiteout))
      89           0 :                         goto out;
      90             : 
      91           0 :                 err = ovl_do_link(ofs->whiteout, wdir, whiteout);
      92           0 :                 if (!err)
      93           0 :                         goto out;
      94             : 
      95           0 :                 if (err != -EMLINK) {
      96           0 :                         pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%i)\n",
      97             :                                 ofs->whiteout->d_inode->i_nlink, err);
      98           0 :                         ofs->share_whiteout = false;
      99             :                 }
     100           0 :                 dput(whiteout);
     101             :         }
     102           0 :         whiteout = ofs->whiteout;
     103           0 :         ofs->whiteout = NULL;
     104           0 : out:
     105           0 :         return whiteout;
     106             : }
     107             : 
     108             : /* Caller must hold i_mutex on both workdir and dir */
     109           0 : int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
     110             :                              struct dentry *dentry)
     111             : {
     112           0 :         struct inode *wdir = ofs->workdir->d_inode;
     113           0 :         struct dentry *whiteout;
     114           0 :         int err;
     115           0 :         int flags = 0;
     116             : 
     117           0 :         whiteout = ovl_whiteout(ofs);
     118           0 :         err = PTR_ERR(whiteout);
     119           0 :         if (IS_ERR(whiteout))
     120             :                 return err;
     121             : 
     122           0 :         if (d_is_dir(dentry))
     123           0 :                 flags = RENAME_EXCHANGE;
     124             : 
     125           0 :         err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
     126           0 :         if (err)
     127           0 :                 goto kill_whiteout;
     128           0 :         if (flags)
     129           0 :                 ovl_cleanup(wdir, dentry);
     130             : 
     131           0 : out:
     132           0 :         dput(whiteout);
     133           0 :         return err;
     134             : 
     135           0 : kill_whiteout:
     136           0 :         ovl_cleanup(wdir, whiteout);
     137           0 :         goto out;
     138             : }
     139             : 
     140           3 : static int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry,
     141             :                           umode_t mode)
     142             : {
     143           3 :         int err;
     144           3 :         struct dentry *d, *dentry = *newdentry;
     145             : 
     146           3 :         err = ovl_do_mkdir(dir, dentry, mode);
     147           3 :         if (err)
     148             :                 return err;
     149             : 
     150           3 :         if (likely(!d_unhashed(dentry)))
     151             :                 return 0;
     152             : 
     153             :         /*
     154             :          * vfs_mkdir() may succeed and leave the dentry passed
     155             :          * to it unhashed and negative. If that happens, try to
     156             :          * lookup a new hashed and positive dentry.
     157             :          */
     158           0 :         d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
     159           0 :                            dentry->d_name.len);
     160           0 :         if (IS_ERR(d)) {
     161           0 :                 pr_warn("failed lookup after mkdir (%pd2, err=%i).\n",
     162             :                         dentry, err);
     163           0 :                 return PTR_ERR(d);
     164             :         }
     165           0 :         dput(dentry);
     166           0 :         *newdentry = d;
     167             : 
     168           0 :         return 0;
     169             : }
     170             : 
     171           5 : struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
     172             :                                struct ovl_cattr *attr)
     173             : {
     174           5 :         int err;
     175             : 
     176           5 :         if (IS_ERR(newdentry))
     177             :                 return newdentry;
     178             : 
     179           5 :         err = -ESTALE;
     180           5 :         if (newdentry->d_inode)
     181           0 :                 goto out;
     182             : 
     183           5 :         if (attr->hardlink) {
     184           0 :                 err = ovl_do_link(attr->hardlink, dir, newdentry);
     185             :         } else {
     186           5 :                 switch (attr->mode & S_IFMT) {
     187           2 :                 case S_IFREG:
     188           2 :                         err = ovl_do_create(dir, newdentry, attr->mode);
     189           2 :                         break;
     190             : 
     191           3 :                 case S_IFDIR:
     192             :                         /* mkdir is special... */
     193           3 :                         err =  ovl_mkdir_real(dir, &newdentry, attr->mode);
     194           3 :                         break;
     195             : 
     196           0 :                 case S_IFCHR:
     197             :                 case S_IFBLK:
     198             :                 case S_IFIFO:
     199             :                 case S_IFSOCK:
     200           0 :                         err = ovl_do_mknod(dir, newdentry, attr->mode,
     201             :                                            attr->rdev);
     202           0 :                         break;
     203             : 
     204           0 :                 case S_IFLNK:
     205           0 :                         err = ovl_do_symlink(dir, newdentry, attr->link);
     206           0 :                         break;
     207             : 
     208             :                 default:
     209             :                         err = -EPERM;
     210             :                 }
     211             :         }
     212           5 :         if (!err && WARN_ON(!newdentry->d_inode)) {
     213             :                 /*
     214             :                  * Not quite sure if non-instantiated dentry is legal or not.
     215             :                  * VFS doesn't seem to care so check and warn here.
     216             :                  */
     217             :                 err = -EIO;
     218             :         }
     219           5 : out:
     220           5 :         if (err) {
     221           0 :                 dput(newdentry);
     222           0 :                 return ERR_PTR(err);
     223             :         }
     224           5 :         return newdentry;
     225             : }
     226             : 
     227           3 : struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
     228             : {
     229           3 :         return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
     230             :                                attr);
     231             : }
     232             : 
     233           0 : static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
     234             :                                int xerr)
     235             : {
     236           0 :         int err;
     237             : 
     238           0 :         err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
     239           0 :         if (!err)
     240           0 :                 ovl_dentry_set_opaque(dentry);
     241             : 
     242           0 :         return err;
     243             : }
     244             : 
     245           0 : static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
     246             : {
     247             :         /*
     248             :          * Fail with -EIO when trying to create opaque dir and upper doesn't
     249             :          * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
     250             :          * return a specific error for noxattr case.
     251             :          */
     252           0 :         return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
     253             : }
     254             : 
     255             : /*
     256             :  * Common operations required to be done after creation of file on upper.
     257             :  * If @hardlink is false, then @inode is a pre-allocated inode, we may or
     258             :  * may not use to instantiate the new dentry.
     259             :  */
     260           0 : static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
     261             :                            struct dentry *newdentry, bool hardlink)
     262             : {
     263           0 :         struct ovl_inode_params oip = {
     264             :                 .upperdentry = newdentry,
     265             :                 .newinode = inode,
     266             :         };
     267             : 
     268           0 :         ovl_dir_modified(dentry->d_parent, false);
     269           0 :         ovl_dentry_set_upper_alias(dentry);
     270           0 :         ovl_dentry_update_reval(dentry, newdentry,
     271             :                         DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
     272             : 
     273           0 :         if (!hardlink) {
     274             :                 /*
     275             :                  * ovl_obtain_alias() can be called after ovl_create_real()
     276             :                  * and before we get here, so we may get an inode from cache
     277             :                  * with the same real upperdentry that is not the inode we
     278             :                  * pre-allocated.  In this case we will use the cached inode
     279             :                  * to instantiate the new dentry.
     280             :                  *
     281             :                  * XXX: if we ever use ovl_obtain_alias() to decode directory
     282             :                  * file handles, need to use ovl_get_inode_locked() and
     283             :                  * d_instantiate_new() here to prevent from creating two
     284             :                  * hashed directory inode aliases.
     285             :                  */
     286           0 :                 inode = ovl_get_inode(dentry->d_sb, &oip);
     287           0 :                 if (IS_ERR(inode))
     288           0 :                         return PTR_ERR(inode);
     289           0 :                 if (inode == oip.newinode)
     290           0 :                         ovl_set_flag(OVL_UPPERDATA, inode);
     291             :         } else {
     292           0 :                 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
     293           0 :                 dput(newdentry);
     294           0 :                 inc_nlink(inode);
     295             :         }
     296             : 
     297           0 :         d_instantiate(dentry, inode);
     298           0 :         if (inode != oip.newinode) {
     299           0 :                 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
     300             :                                     dentry);
     301             :         }
     302             : 
     303             :         /* Force lookup of new upper hardlink to find its lower */
     304           0 :         if (hardlink)
     305           0 :                 d_drop(dentry);
     306             : 
     307             :         return 0;
     308             : }
     309             : 
     310           0 : static bool ovl_type_merge(struct dentry *dentry)
     311             : {
     312           0 :         return OVL_TYPE_MERGE(ovl_path_type(dentry));
     313             : }
     314             : 
     315           0 : static bool ovl_type_origin(struct dentry *dentry)
     316             : {
     317           0 :         return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
     318             : }
     319             : 
     320           0 : static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
     321             :                             struct ovl_cattr *attr)
     322             : {
     323           0 :         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
     324           0 :         struct inode *udir = upperdir->d_inode;
     325           0 :         struct dentry *newdentry;
     326           0 :         int err;
     327             : 
     328           0 :         if (!attr->hardlink && !IS_POSIXACL(udir))
     329           0 :                 attr->mode &= ~current_umask();
     330             : 
     331           0 :         inode_lock_nested(udir, I_MUTEX_PARENT);
     332           0 :         newdentry = ovl_create_real(udir,
     333           0 :                                     lookup_one_len(dentry->d_name.name,
     334             :                                                    upperdir,
     335           0 :                                                    dentry->d_name.len),
     336             :                                     attr);
     337           0 :         err = PTR_ERR(newdentry);
     338           0 :         if (IS_ERR(newdentry))
     339           0 :                 goto out_unlock;
     340             : 
     341           0 :         if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
     342             :                 /* Setting opaque here is just an optimization, allow to fail */
     343           0 :                 ovl_set_opaque(dentry, newdentry);
     344             :         }
     345             : 
     346           0 :         err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
     347           0 :         if (err)
     348           0 :                 goto out_cleanup;
     349           0 : out_unlock:
     350           0 :         inode_unlock(udir);
     351           0 :         return err;
     352             : 
     353           0 : out_cleanup:
     354           0 :         ovl_cleanup(udir, newdentry);
     355           0 :         dput(newdentry);
     356           0 :         goto out_unlock;
     357             : }
     358             : 
     359           0 : static struct dentry *ovl_clear_empty(struct dentry *dentry,
     360             :                                       struct list_head *list)
     361             : {
     362           0 :         struct dentry *workdir = ovl_workdir(dentry);
     363           0 :         struct inode *wdir = workdir->d_inode;
     364           0 :         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
     365           0 :         struct inode *udir = upperdir->d_inode;
     366           0 :         struct path upperpath;
     367           0 :         struct dentry *upper;
     368           0 :         struct dentry *opaquedir;
     369           0 :         struct kstat stat;
     370           0 :         int err;
     371             : 
     372           0 :         if (WARN_ON(!workdir))
     373           0 :                 return ERR_PTR(-EROFS);
     374             : 
     375           0 :         err = ovl_lock_rename_workdir(workdir, upperdir);
     376           0 :         if (err)
     377           0 :                 goto out;
     378             : 
     379           0 :         ovl_path_upper(dentry, &upperpath);
     380           0 :         err = vfs_getattr(&upperpath, &stat,
     381             :                           STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
     382           0 :         if (err)
     383           0 :                 goto out_unlock;
     384             : 
     385           0 :         err = -ESTALE;
     386           0 :         if (!S_ISDIR(stat.mode))
     387           0 :                 goto out_unlock;
     388           0 :         upper = upperpath.dentry;
     389           0 :         if (upper->d_parent->d_inode != udir)
     390           0 :                 goto out_unlock;
     391             : 
     392           0 :         opaquedir = ovl_create_temp(workdir, OVL_CATTR(stat.mode));
     393           0 :         err = PTR_ERR(opaquedir);
     394           0 :         if (IS_ERR(opaquedir))
     395           0 :                 goto out_unlock;
     396             : 
     397           0 :         err = ovl_copy_xattr(dentry->d_sb, upper, opaquedir);
     398           0 :         if (err)
     399           0 :                 goto out_cleanup;
     400             : 
     401           0 :         err = ovl_set_opaque(dentry, opaquedir);
     402           0 :         if (err)
     403           0 :                 goto out_cleanup;
     404             : 
     405           0 :         inode_lock(opaquedir->d_inode);
     406           0 :         err = ovl_set_attr(opaquedir, &stat);
     407           0 :         inode_unlock(opaquedir->d_inode);
     408           0 :         if (err)
     409           0 :                 goto out_cleanup;
     410             : 
     411           0 :         err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
     412           0 :         if (err)
     413           0 :                 goto out_cleanup;
     414             : 
     415           0 :         ovl_cleanup_whiteouts(upper, list);
     416           0 :         ovl_cleanup(wdir, upper);
     417           0 :         unlock_rename(workdir, upperdir);
     418             : 
     419             :         /* dentry's upper doesn't match now, get rid of it */
     420           0 :         d_drop(dentry);
     421             : 
     422           0 :         return opaquedir;
     423             : 
     424           0 : out_cleanup:
     425           0 :         ovl_cleanup(wdir, opaquedir);
     426           0 :         dput(opaquedir);
     427           0 : out_unlock:
     428           0 :         unlock_rename(workdir, upperdir);
     429           0 : out:
     430           0 :         return ERR_PTR(err);
     431             : }
     432             : 
     433             : static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
     434             :                              const struct posix_acl *acl)
     435             : {
     436             :         void *buffer;
     437             :         size_t size;
     438             :         int err;
     439             : 
     440             :         if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
     441             :                 return 0;
     442             : 
     443             :         size = posix_acl_xattr_size(acl->a_count);
     444             :         buffer = kmalloc(size, GFP_KERNEL);
     445             :         if (!buffer)
     446             :                 return -ENOMEM;
     447             : 
     448             :         err = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
     449             :         if (err < 0)
     450             :                 goto out_free;
     451             : 
     452             :         err = vfs_setxattr(&init_user_ns, upperdentry, name, buffer, size, XATTR_CREATE);
     453             : out_free:
     454             :         kfree(buffer);
     455             :         return err;
     456             : }
     457             : 
     458           0 : static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
     459             :                                     struct ovl_cattr *cattr)
     460             : {
     461           0 :         struct dentry *workdir = ovl_workdir(dentry);
     462           0 :         struct inode *wdir = workdir->d_inode;
     463           0 :         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
     464           0 :         struct inode *udir = upperdir->d_inode;
     465           0 :         struct dentry *upper;
     466           0 :         struct dentry *newdentry;
     467           0 :         int err;
     468           0 :         struct posix_acl *acl, *default_acl;
     469           0 :         bool hardlink = !!cattr->hardlink;
     470             : 
     471           0 :         if (WARN_ON(!workdir))
     472             :                 return -EROFS;
     473             : 
     474           0 :         if (!hardlink) {
     475             :                 err = posix_acl_create(dentry->d_parent->d_inode,
     476             :                                        &cattr->mode, &default_acl, &acl);
     477             :                 if (err)
     478             :                         return err;
     479             :         }
     480             : 
     481           0 :         err = ovl_lock_rename_workdir(workdir, upperdir);
     482           0 :         if (err)
     483           0 :                 goto out;
     484             : 
     485           0 :         upper = lookup_one_len(dentry->d_name.name, upperdir,
     486           0 :                                dentry->d_name.len);
     487           0 :         err = PTR_ERR(upper);
     488           0 :         if (IS_ERR(upper))
     489           0 :                 goto out_unlock;
     490             : 
     491           0 :         err = -ESTALE;
     492           0 :         if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
     493           0 :                 goto out_dput;
     494             : 
     495           0 :         newdentry = ovl_create_temp(workdir, cattr);
     496           0 :         err = PTR_ERR(newdentry);
     497           0 :         if (IS_ERR(newdentry))
     498           0 :                 goto out_dput;
     499             : 
     500             :         /*
     501             :          * mode could have been mutilated due to umask (e.g. sgid directory)
     502             :          */
     503           0 :         if (!hardlink &&
     504           0 :             !S_ISLNK(cattr->mode) &&
     505           0 :             newdentry->d_inode->i_mode != cattr->mode) {
     506           0 :                 struct iattr attr = {
     507             :                         .ia_valid = ATTR_MODE,
     508             :                         .ia_mode = cattr->mode,
     509             :                 };
     510           0 :                 inode_lock(newdentry->d_inode);
     511           0 :                 err = notify_change(&init_user_ns, newdentry, &attr, NULL);
     512           0 :                 inode_unlock(newdentry->d_inode);
     513           0 :                 if (err)
     514           0 :                         goto out_cleanup;
     515             :         }
     516           0 :         if (!hardlink) {
     517           0 :                 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
     518             :                                         acl);
     519             :                 if (err)
     520             :                         goto out_cleanup;
     521             : 
     522           0 :                 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
     523             :                                         default_acl);
     524             :                 if (err)
     525             :                         goto out_cleanup;
     526             :         }
     527             : 
     528           0 :         if (!hardlink && S_ISDIR(cattr->mode)) {
     529           0 :                 err = ovl_set_opaque(dentry, newdentry);
     530           0 :                 if (err)
     531           0 :                         goto out_cleanup;
     532             : 
     533           0 :                 err = ovl_do_rename(wdir, newdentry, udir, upper,
     534             :                                     RENAME_EXCHANGE);
     535           0 :                 if (err)
     536           0 :                         goto out_cleanup;
     537             : 
     538           0 :                 ovl_cleanup(wdir, upper);
     539             :         } else {
     540           0 :                 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
     541           0 :                 if (err)
     542           0 :                         goto out_cleanup;
     543             :         }
     544           0 :         err = ovl_instantiate(dentry, inode, newdentry, hardlink);
     545           0 :         if (err)
     546           0 :                 goto out_cleanup;
     547           0 : out_dput:
     548           0 :         dput(upper);
     549           0 : out_unlock:
     550           0 :         unlock_rename(workdir, upperdir);
     551           0 : out:
     552           0 :         if (!hardlink) {
     553           0 :                 posix_acl_release(acl);
     554           0 :                 posix_acl_release(default_acl);
     555             :         }
     556             :         return err;
     557             : 
     558           0 : out_cleanup:
     559           0 :         ovl_cleanup(wdir, newdentry);
     560           0 :         dput(newdentry);
     561           0 :         goto out_dput;
     562             : }
     563             : 
     564           0 : static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
     565             :                               struct ovl_cattr *attr, bool origin)
     566             : {
     567           0 :         int err;
     568           0 :         const struct cred *old_cred;
     569           0 :         struct cred *override_cred;
     570           0 :         struct dentry *parent = dentry->d_parent;
     571             : 
     572           0 :         err = ovl_copy_up(parent);
     573           0 :         if (err)
     574             :                 return err;
     575             : 
     576           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     577             : 
     578             :         /*
     579             :          * When linking a file with copy up origin into a new parent, mark the
     580             :          * new parent dir "impure".
     581             :          */
     582           0 :         if (origin) {
     583           0 :                 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
     584           0 :                 if (err)
     585           0 :                         goto out_revert_creds;
     586             :         }
     587             : 
     588           0 :         err = -ENOMEM;
     589           0 :         override_cred = prepare_creds();
     590           0 :         if (override_cred) {
     591           0 :                 override_cred->fsuid = inode->i_uid;
     592           0 :                 override_cred->fsgid = inode->i_gid;
     593           0 :                 if (!attr->hardlink) {
     594           0 :                         err = security_dentry_create_files_as(dentry,
     595           0 :                                         attr->mode, &dentry->d_name, old_cred,
     596             :                                         override_cred);
     597           0 :                         if (err) {
     598           0 :                                 put_cred(override_cred);
     599           0 :                                 goto out_revert_creds;
     600             :                         }
     601             :                 }
     602           0 :                 put_cred(override_creds(override_cred));
     603           0 :                 put_cred(override_cred);
     604             : 
     605           0 :                 if (!ovl_dentry_is_whiteout(dentry))
     606           0 :                         err = ovl_create_upper(dentry, inode, attr);
     607             :                 else
     608           0 :                         err = ovl_create_over_whiteout(dentry, inode, attr);
     609             :         }
     610           0 : out_revert_creds:
     611           0 :         revert_creds(old_cred);
     612           0 :         return err;
     613             : }
     614             : 
     615           0 : static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
     616             :                              const char *link)
     617             : {
     618           0 :         int err;
     619           0 :         struct inode *inode;
     620           0 :         struct ovl_cattr attr = {
     621             :                 .rdev = rdev,
     622             :                 .link = link,
     623             :         };
     624             : 
     625           0 :         err = ovl_want_write(dentry);
     626           0 :         if (err)
     627           0 :                 goto out;
     628             : 
     629             :         /* Preallocate inode to be used by ovl_get_inode() */
     630           0 :         err = -ENOMEM;
     631           0 :         inode = ovl_new_inode(dentry->d_sb, mode, rdev);
     632           0 :         if (!inode)
     633           0 :                 goto out_drop_write;
     634             : 
     635           0 :         spin_lock(&inode->i_lock);
     636           0 :         inode->i_state |= I_CREATING;
     637           0 :         spin_unlock(&inode->i_lock);
     638             : 
     639           0 :         inode_init_owner(&init_user_ns, inode, dentry->d_parent->d_inode, mode);
     640           0 :         attr.mode = inode->i_mode;
     641             : 
     642           0 :         err = ovl_create_or_link(dentry, inode, &attr, false);
     643             :         /* Did we end up using the preallocated inode? */
     644           0 :         if (inode != d_inode(dentry))
     645           0 :                 iput(inode);
     646             : 
     647           0 : out_drop_write:
     648           0 :         ovl_drop_write(dentry);
     649           0 : out:
     650           0 :         return err;
     651             : }
     652             : 
     653           0 : static int ovl_create(struct user_namespace *mnt_userns, struct inode *dir,
     654             :                       struct dentry *dentry, umode_t mode, bool excl)
     655             : {
     656           0 :         return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
     657             : }
     658             : 
     659           0 : static int ovl_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
     660             :                      struct dentry *dentry, umode_t mode)
     661             : {
     662           0 :         return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
     663             : }
     664             : 
     665           0 : static int ovl_mknod(struct user_namespace *mnt_userns, struct inode *dir,
     666             :                      struct dentry *dentry, umode_t mode, dev_t rdev)
     667             : {
     668             :         /* Don't allow creation of "whiteout" on overlay */
     669           0 :         if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
     670             :                 return -EPERM;
     671             : 
     672           0 :         return ovl_create_object(dentry, mode, rdev, NULL);
     673             : }
     674             : 
     675           0 : static int ovl_symlink(struct user_namespace *mnt_userns, struct inode *dir,
     676             :                        struct dentry *dentry, const char *link)
     677             : {
     678           0 :         return ovl_create_object(dentry, S_IFLNK, 0, link);
     679             : }
     680             : 
     681           0 : static int ovl_set_link_redirect(struct dentry *dentry)
     682             : {
     683           0 :         const struct cred *old_cred;
     684           0 :         int err;
     685             : 
     686           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     687           0 :         err = ovl_set_redirect(dentry, false);
     688           0 :         revert_creds(old_cred);
     689             : 
     690           0 :         return err;
     691             : }
     692             : 
     693           0 : static int ovl_link(struct dentry *old, struct inode *newdir,
     694             :                     struct dentry *new)
     695             : {
     696           0 :         int err;
     697           0 :         struct inode *inode;
     698             : 
     699           0 :         err = ovl_want_write(old);
     700           0 :         if (err)
     701           0 :                 goto out;
     702             : 
     703           0 :         err = ovl_copy_up(old);
     704           0 :         if (err)
     705           0 :                 goto out_drop_write;
     706             : 
     707           0 :         err = ovl_copy_up(new->d_parent);
     708           0 :         if (err)
     709           0 :                 goto out_drop_write;
     710             : 
     711           0 :         if (ovl_is_metacopy_dentry(old)) {
     712           0 :                 err = ovl_set_link_redirect(old);
     713           0 :                 if (err)
     714           0 :                         goto out_drop_write;
     715             :         }
     716             : 
     717           0 :         err = ovl_nlink_start(old);
     718           0 :         if (err)
     719           0 :                 goto out_drop_write;
     720             : 
     721           0 :         inode = d_inode(old);
     722           0 :         ihold(inode);
     723             : 
     724           0 :         err = ovl_create_or_link(new, inode,
     725           0 :                         &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
     726           0 :                         ovl_type_origin(old));
     727           0 :         if (err)
     728           0 :                 iput(inode);
     729             : 
     730           0 :         ovl_nlink_end(old);
     731           0 : out_drop_write:
     732           0 :         ovl_drop_write(old);
     733           0 : out:
     734           0 :         return err;
     735             : }
     736             : 
     737           0 : static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
     738             : {
     739           0 :         return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
     740             : }
     741             : 
     742           0 : static int ovl_remove_and_whiteout(struct dentry *dentry,
     743             :                                    struct list_head *list)
     744             : {
     745           0 :         struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
     746           0 :         struct dentry *workdir = ovl_workdir(dentry);
     747           0 :         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
     748           0 :         struct dentry *upper;
     749           0 :         struct dentry *opaquedir = NULL;
     750           0 :         int err;
     751             : 
     752           0 :         if (WARN_ON(!workdir))
     753             :                 return -EROFS;
     754             : 
     755           0 :         if (!list_empty(list)) {
     756           0 :                 opaquedir = ovl_clear_empty(dentry, list);
     757           0 :                 err = PTR_ERR(opaquedir);
     758           0 :                 if (IS_ERR(opaquedir))
     759           0 :                         goto out;
     760             :         }
     761             : 
     762           0 :         err = ovl_lock_rename_workdir(workdir, upperdir);
     763           0 :         if (err)
     764           0 :                 goto out_dput;
     765             : 
     766           0 :         upper = lookup_one_len(dentry->d_name.name, upperdir,
     767           0 :                                dentry->d_name.len);
     768           0 :         err = PTR_ERR(upper);
     769           0 :         if (IS_ERR(upper))
     770           0 :                 goto out_unlock;
     771             : 
     772           0 :         err = -ESTALE;
     773           0 :         if ((opaquedir && upper != opaquedir) ||
     774           0 :             (!opaquedir && ovl_dentry_upper(dentry) &&
     775           0 :              !ovl_matches_upper(dentry, upper))) {
     776           0 :                 goto out_dput_upper;
     777             :         }
     778             : 
     779           0 :         err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper);
     780           0 :         if (err)
     781           0 :                 goto out_d_drop;
     782             : 
     783           0 :         ovl_dir_modified(dentry->d_parent, true);
     784           0 : out_d_drop:
     785           0 :         d_drop(dentry);
     786           0 : out_dput_upper:
     787           0 :         dput(upper);
     788           0 : out_unlock:
     789           0 :         unlock_rename(workdir, upperdir);
     790           0 : out_dput:
     791           0 :         dput(opaquedir);
     792             : out:
     793             :         return err;
     794             : }
     795             : 
     796           0 : static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
     797             :                             struct list_head *list)
     798             : {
     799           0 :         struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
     800           0 :         struct inode *dir = upperdir->d_inode;
     801           0 :         struct dentry *upper;
     802           0 :         struct dentry *opaquedir = NULL;
     803           0 :         int err;
     804             : 
     805           0 :         if (!list_empty(list)) {
     806           0 :                 opaquedir = ovl_clear_empty(dentry, list);
     807           0 :                 err = PTR_ERR(opaquedir);
     808           0 :                 if (IS_ERR(opaquedir))
     809           0 :                         goto out;
     810             :         }
     811             : 
     812           0 :         inode_lock_nested(dir, I_MUTEX_PARENT);
     813           0 :         upper = lookup_one_len(dentry->d_name.name, upperdir,
     814           0 :                                dentry->d_name.len);
     815           0 :         err = PTR_ERR(upper);
     816           0 :         if (IS_ERR(upper))
     817           0 :                 goto out_unlock;
     818             : 
     819           0 :         err = -ESTALE;
     820           0 :         if ((opaquedir && upper != opaquedir) ||
     821           0 :             (!opaquedir && !ovl_matches_upper(dentry, upper)))
     822           0 :                 goto out_dput_upper;
     823             : 
     824           0 :         if (is_dir)
     825           0 :                 err = vfs_rmdir(&init_user_ns, dir, upper);
     826             :         else
     827           0 :                 err = vfs_unlink(&init_user_ns, dir, upper, NULL);
     828           0 :         ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
     829             : 
     830             :         /*
     831             :          * Keeping this dentry hashed would mean having to release
     832             :          * upperpath/lowerpath, which could only be done if we are the
     833             :          * sole user of this dentry.  Too tricky...  Just unhash for
     834             :          * now.
     835             :          */
     836           0 :         if (!err)
     837           0 :                 d_drop(dentry);
     838           0 : out_dput_upper:
     839           0 :         dput(upper);
     840           0 : out_unlock:
     841           0 :         inode_unlock(dir);
     842           0 :         dput(opaquedir);
     843           0 : out:
     844           0 :         return err;
     845             : }
     846             : 
     847           0 : static bool ovl_pure_upper(struct dentry *dentry)
     848             : {
     849           0 :         return !ovl_dentry_lower(dentry) &&
     850           0 :                !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
     851             : }
     852             : 
     853           0 : static void ovl_drop_nlink(struct dentry *dentry)
     854             : {
     855           0 :         struct inode *inode = d_inode(dentry);
     856           0 :         struct dentry *alias;
     857             : 
     858             :         /* Try to find another, hashed alias */
     859           0 :         spin_lock(&inode->i_lock);
     860           0 :         hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
     861           0 :                 if (alias != dentry && !d_unhashed(alias))
     862             :                         break;
     863             :         }
     864           0 :         spin_unlock(&inode->i_lock);
     865             : 
     866             :         /*
     867             :          * Changes to underlying layers may cause i_nlink to lose sync with
     868             :          * reality.  In this case prevent the link count from going to zero
     869             :          * prematurely.
     870             :          */
     871           0 :         if (inode->i_nlink > !!alias)
     872           0 :                 drop_nlink(inode);
     873           0 : }
     874             : 
     875           0 : static int ovl_do_remove(struct dentry *dentry, bool is_dir)
     876             : {
     877           0 :         int err;
     878           0 :         const struct cred *old_cred;
     879           0 :         struct dentry *upperdentry;
     880           0 :         bool lower_positive = ovl_lower_positive(dentry);
     881           0 :         LIST_HEAD(list);
     882             : 
     883             :         /* No need to clean pure upper removed by vfs_rmdir() */
     884           0 :         if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
     885           0 :                 err = ovl_check_empty_dir(dentry, &list);
     886           0 :                 if (err)
     887           0 :                         goto out;
     888             :         }
     889             : 
     890           0 :         err = ovl_want_write(dentry);
     891           0 :         if (err)
     892           0 :                 goto out;
     893             : 
     894           0 :         err = ovl_copy_up(dentry->d_parent);
     895           0 :         if (err)
     896           0 :                 goto out_drop_write;
     897             : 
     898           0 :         err = ovl_nlink_start(dentry);
     899           0 :         if (err)
     900           0 :                 goto out_drop_write;
     901             : 
     902           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     903           0 :         if (!lower_positive)
     904           0 :                 err = ovl_remove_upper(dentry, is_dir, &list);
     905             :         else
     906           0 :                 err = ovl_remove_and_whiteout(dentry, &list);
     907           0 :         revert_creds(old_cred);
     908           0 :         if (!err) {
     909           0 :                 if (is_dir)
     910           0 :                         clear_nlink(dentry->d_inode);
     911             :                 else
     912           0 :                         ovl_drop_nlink(dentry);
     913             :         }
     914           0 :         ovl_nlink_end(dentry);
     915             : 
     916             :         /*
     917             :          * Copy ctime
     918             :          *
     919             :          * Note: we fail to update ctime if there was no copy-up, only a
     920             :          * whiteout
     921             :          */
     922           0 :         upperdentry = ovl_dentry_upper(dentry);
     923           0 :         if (upperdentry)
     924           0 :                 ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
     925             : 
     926           0 : out_drop_write:
     927           0 :         ovl_drop_write(dentry);
     928           0 : out:
     929           0 :         ovl_cache_free(&list);
     930           0 :         return err;
     931             : }
     932             : 
     933           0 : static int ovl_unlink(struct inode *dir, struct dentry *dentry)
     934             : {
     935           0 :         return ovl_do_remove(dentry, false);
     936             : }
     937             : 
     938           0 : static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
     939             : {
     940           0 :         return ovl_do_remove(dentry, true);
     941             : }
     942             : 
     943           0 : static bool ovl_type_merge_or_lower(struct dentry *dentry)
     944             : {
     945           0 :         enum ovl_path_type type = ovl_path_type(dentry);
     946             : 
     947           0 :         return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
     948             : }
     949             : 
     950           0 : static bool ovl_can_move(struct dentry *dentry)
     951             : {
     952           0 :         return ovl_redirect_dir(dentry->d_sb) ||
     953           0 :                 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
     954             : }
     955             : 
     956           0 : static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
     957             : {
     958           0 :         char *buf, *ret;
     959           0 :         struct dentry *d, *tmp;
     960           0 :         int buflen = ovl_redirect_max + 1;
     961             : 
     962           0 :         if (!abs_redirect) {
     963           0 :                 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
     964             :                                GFP_KERNEL);
     965           0 :                 goto out;
     966             :         }
     967             : 
     968           0 :         buf = ret = kmalloc(buflen, GFP_KERNEL);
     969           0 :         if (!buf)
     970           0 :                 goto out;
     971             : 
     972           0 :         buflen--;
     973           0 :         buf[buflen] = '\0';
     974           0 :         for (d = dget(dentry); !IS_ROOT(d);) {
     975           0 :                 const char *name;
     976           0 :                 int thislen;
     977             : 
     978           0 :                 spin_lock(&d->d_lock);
     979           0 :                 name = ovl_dentry_get_redirect(d);
     980           0 :                 if (name) {
     981           0 :                         thislen = strlen(name);
     982             :                 } else {
     983           0 :                         name = d->d_name.name;
     984           0 :                         thislen = d->d_name.len;
     985             :                 }
     986             : 
     987             :                 /* If path is too long, fall back to userspace move */
     988           0 :                 if (thislen + (name[0] != '/') > buflen) {
     989           0 :                         ret = ERR_PTR(-EXDEV);
     990           0 :                         spin_unlock(&d->d_lock);
     991           0 :                         goto out_put;
     992             :                 }
     993             : 
     994           0 :                 buflen -= thislen;
     995           0 :                 memcpy(&buf[buflen], name, thislen);
     996           0 :                 spin_unlock(&d->d_lock);
     997           0 :                 tmp = dget_parent(d);
     998             : 
     999           0 :                 dput(d);
    1000           0 :                 d = tmp;
    1001             : 
    1002             :                 /* Absolute redirect: finished */
    1003           0 :                 if (buf[buflen] == '/')
    1004             :                         break;
    1005           0 :                 buflen--;
    1006           0 :                 buf[buflen] = '/';
    1007             :         }
    1008           0 :         ret = kstrdup(&buf[buflen], GFP_KERNEL);
    1009           0 : out_put:
    1010           0 :         dput(d);
    1011           0 :         kfree(buf);
    1012           0 : out:
    1013           0 :         return ret ? ret : ERR_PTR(-ENOMEM);
    1014             : }
    1015             : 
    1016           0 : static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
    1017             : {
    1018           0 :         struct dentry *lowerdentry;
    1019             : 
    1020           0 :         if (!samedir)
    1021             :                 return true;
    1022             : 
    1023           0 :         if (d_is_dir(dentry))
    1024             :                 return false;
    1025             : 
    1026             :         /*
    1027             :          * For non-dir hardlinked files, we need absolute redirects
    1028             :          * in general as two upper hardlinks could be in different
    1029             :          * dirs. We could put a relative redirect now and convert
    1030             :          * it to absolute redirect later. But when nlink > 1 and
    1031             :          * indexing is on, that means relative redirect needs to be
    1032             :          * converted to absolute during copy up of another lower
    1033             :          * hardllink as well.
    1034             :          *
    1035             :          * So without optimizing too much, just check if lower is
    1036             :          * a hard link or not. If lower is hard link, put absolute
    1037             :          * redirect.
    1038             :          */
    1039           0 :         lowerdentry = ovl_dentry_lower(dentry);
    1040           0 :         return (d_inode(lowerdentry)->i_nlink > 1);
    1041             : }
    1042             : 
    1043           0 : static int ovl_set_redirect(struct dentry *dentry, bool samedir)
    1044             : {
    1045           0 :         int err;
    1046           0 :         const char *redirect = ovl_dentry_get_redirect(dentry);
    1047           0 :         bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
    1048             : 
    1049           0 :         if (redirect && (!absolute_redirect || redirect[0] == '/'))
    1050             :                 return 0;
    1051             : 
    1052           0 :         redirect = ovl_get_redirect(dentry, absolute_redirect);
    1053           0 :         if (IS_ERR(redirect))
    1054           0 :                 return PTR_ERR(redirect);
    1055             : 
    1056           0 :         err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
    1057             :                                  OVL_XATTR_REDIRECT,
    1058             :                                  redirect, strlen(redirect), -EXDEV);
    1059           0 :         if (!err) {
    1060           0 :                 spin_lock(&dentry->d_lock);
    1061           0 :                 ovl_dentry_set_redirect(dentry, redirect);
    1062           0 :                 spin_unlock(&dentry->d_lock);
    1063             :         } else {
    1064           0 :                 kfree(redirect);
    1065           0 :                 pr_warn_ratelimited("failed to set redirect (%i)\n",
    1066             :                                     err);
    1067             :                 /* Fall back to userspace copy-up */
    1068             :                 err = -EXDEV;
    1069             :         }
    1070             :         return err;
    1071             : }
    1072             : 
    1073           0 : static int ovl_rename(struct user_namespace *mnt_userns, struct inode *olddir,
    1074             :                       struct dentry *old, struct inode *newdir,
    1075             :                       struct dentry *new, unsigned int flags)
    1076             : {
    1077           0 :         int err;
    1078           0 :         struct dentry *old_upperdir;
    1079           0 :         struct dentry *new_upperdir;
    1080           0 :         struct dentry *olddentry;
    1081           0 :         struct dentry *newdentry;
    1082           0 :         struct dentry *trap;
    1083           0 :         bool old_opaque;
    1084           0 :         bool new_opaque;
    1085           0 :         bool cleanup_whiteout = false;
    1086           0 :         bool update_nlink = false;
    1087           0 :         bool overwrite = !(flags & RENAME_EXCHANGE);
    1088           0 :         bool is_dir = d_is_dir(old);
    1089           0 :         bool new_is_dir = d_is_dir(new);
    1090           0 :         bool samedir = olddir == newdir;
    1091           0 :         struct dentry *opaquedir = NULL;
    1092           0 :         const struct cred *old_cred = NULL;
    1093           0 :         LIST_HEAD(list);
    1094             : 
    1095           0 :         err = -EINVAL;
    1096           0 :         if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
    1097           0 :                 goto out;
    1098             : 
    1099           0 :         flags &= ~RENAME_NOREPLACE;
    1100             : 
    1101             :         /* Don't copy up directory trees */
    1102           0 :         err = -EXDEV;
    1103           0 :         if (!ovl_can_move(old))
    1104           0 :                 goto out;
    1105           0 :         if (!overwrite && !ovl_can_move(new))
    1106           0 :                 goto out;
    1107             : 
    1108           0 :         if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
    1109           0 :                 err = ovl_check_empty_dir(new, &list);
    1110           0 :                 if (err)
    1111           0 :                         goto out;
    1112             :         }
    1113             : 
    1114           0 :         if (overwrite) {
    1115           0 :                 if (ovl_lower_positive(old)) {
    1116           0 :                         if (!ovl_dentry_is_whiteout(new)) {
    1117             :                                 /* Whiteout source */
    1118           0 :                                 flags |= RENAME_WHITEOUT;
    1119             :                         } else {
    1120             :                                 /* Switch whiteouts */
    1121           0 :                                 flags |= RENAME_EXCHANGE;
    1122             :                         }
    1123           0 :                 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
    1124           0 :                         flags |= RENAME_EXCHANGE;
    1125           0 :                         cleanup_whiteout = true;
    1126             :                 }
    1127             :         }
    1128             : 
    1129           0 :         err = ovl_want_write(old);
    1130           0 :         if (err)
    1131           0 :                 goto out;
    1132             : 
    1133           0 :         err = ovl_copy_up(old);
    1134           0 :         if (err)
    1135           0 :                 goto out_drop_write;
    1136             : 
    1137           0 :         err = ovl_copy_up(new->d_parent);
    1138           0 :         if (err)
    1139           0 :                 goto out_drop_write;
    1140           0 :         if (!overwrite) {
    1141           0 :                 err = ovl_copy_up(new);
    1142           0 :                 if (err)
    1143           0 :                         goto out_drop_write;
    1144           0 :         } else if (d_inode(new)) {
    1145           0 :                 err = ovl_nlink_start(new);
    1146           0 :                 if (err)
    1147           0 :                         goto out_drop_write;
    1148             : 
    1149             :                 update_nlink = true;
    1150             :         }
    1151             : 
    1152           0 :         old_cred = ovl_override_creds(old->d_sb);
    1153             : 
    1154           0 :         if (!list_empty(&list)) {
    1155           0 :                 opaquedir = ovl_clear_empty(new, &list);
    1156           0 :                 err = PTR_ERR(opaquedir);
    1157           0 :                 if (IS_ERR(opaquedir)) {
    1158           0 :                         opaquedir = NULL;
    1159           0 :                         goto out_revert_creds;
    1160             :                 }
    1161             :         }
    1162             : 
    1163           0 :         old_upperdir = ovl_dentry_upper(old->d_parent);
    1164           0 :         new_upperdir = ovl_dentry_upper(new->d_parent);
    1165             : 
    1166           0 :         if (!samedir) {
    1167             :                 /*
    1168             :                  * When moving a merge dir or non-dir with copy up origin into
    1169             :                  * a new parent, we are marking the new parent dir "impure".
    1170             :                  * When ovl_iterate() iterates an "impure" upper dir, it will
    1171             :                  * lookup the origin inodes of the entries to fill d_ino.
    1172             :                  */
    1173           0 :                 if (ovl_type_origin(old)) {
    1174           0 :                         err = ovl_set_impure(new->d_parent, new_upperdir);
    1175           0 :                         if (err)
    1176           0 :                                 goto out_revert_creds;
    1177             :                 }
    1178           0 :                 if (!overwrite && ovl_type_origin(new)) {
    1179           0 :                         err = ovl_set_impure(old->d_parent, old_upperdir);
    1180           0 :                         if (err)
    1181           0 :                                 goto out_revert_creds;
    1182             :                 }
    1183             :         }
    1184             : 
    1185           0 :         trap = lock_rename(new_upperdir, old_upperdir);
    1186             : 
    1187           0 :         olddentry = lookup_one_len(old->d_name.name, old_upperdir,
    1188           0 :                                    old->d_name.len);
    1189           0 :         err = PTR_ERR(olddentry);
    1190           0 :         if (IS_ERR(olddentry))
    1191           0 :                 goto out_unlock;
    1192             : 
    1193           0 :         err = -ESTALE;
    1194           0 :         if (!ovl_matches_upper(old, olddentry))
    1195           0 :                 goto out_dput_old;
    1196             : 
    1197           0 :         newdentry = lookup_one_len(new->d_name.name, new_upperdir,
    1198           0 :                                    new->d_name.len);
    1199           0 :         err = PTR_ERR(newdentry);
    1200           0 :         if (IS_ERR(newdentry))
    1201           0 :                 goto out_dput_old;
    1202             : 
    1203           0 :         old_opaque = ovl_dentry_is_opaque(old);
    1204           0 :         new_opaque = ovl_dentry_is_opaque(new);
    1205             : 
    1206           0 :         err = -ESTALE;
    1207           0 :         if (d_inode(new) && ovl_dentry_upper(new)) {
    1208           0 :                 if (opaquedir) {
    1209           0 :                         if (newdentry != opaquedir)
    1210           0 :                                 goto out_dput;
    1211             :                 } else {
    1212           0 :                         if (!ovl_matches_upper(new, newdentry))
    1213           0 :                                 goto out_dput;
    1214             :                 }
    1215             :         } else {
    1216           0 :                 if (!d_is_negative(newdentry) &&
    1217           0 :                     (!new_opaque || !ovl_is_whiteout(newdentry)))
    1218           0 :                         goto out_dput;
    1219             :         }
    1220             : 
    1221           0 :         if (olddentry == trap)
    1222           0 :                 goto out_dput;
    1223           0 :         if (newdentry == trap)
    1224           0 :                 goto out_dput;
    1225             : 
    1226           0 :         if (olddentry->d_inode == newdentry->d_inode)
    1227           0 :                 goto out_dput;
    1228             : 
    1229           0 :         err = 0;
    1230           0 :         if (ovl_type_merge_or_lower(old))
    1231           0 :                 err = ovl_set_redirect(old, samedir);
    1232           0 :         else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
    1233           0 :                 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
    1234           0 :         if (err)
    1235           0 :                 goto out_dput;
    1236             : 
    1237           0 :         if (!overwrite && ovl_type_merge_or_lower(new))
    1238           0 :                 err = ovl_set_redirect(new, samedir);
    1239           0 :         else if (!overwrite && new_is_dir && !new_opaque &&
    1240           0 :                  ovl_type_merge(old->d_parent))
    1241           0 :                 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
    1242           0 :         if (err)
    1243           0 :                 goto out_dput;
    1244             : 
    1245           0 :         err = ovl_do_rename(old_upperdir->d_inode, olddentry,
    1246             :                             new_upperdir->d_inode, newdentry, flags);
    1247           0 :         if (err)
    1248           0 :                 goto out_dput;
    1249             : 
    1250           0 :         if (cleanup_whiteout)
    1251           0 :                 ovl_cleanup(old_upperdir->d_inode, newdentry);
    1252             : 
    1253           0 :         if (overwrite && d_inode(new)) {
    1254           0 :                 if (new_is_dir)
    1255           0 :                         clear_nlink(d_inode(new));
    1256             :                 else
    1257           0 :                         ovl_drop_nlink(new);
    1258             :         }
    1259             : 
    1260           0 :         ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
    1261           0 :                          (!overwrite && ovl_type_origin(new)));
    1262           0 :         ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
    1263           0 :                          (d_inode(new) && ovl_type_origin(new)));
    1264             : 
    1265             :         /* copy ctime: */
    1266           0 :         ovl_copyattr(d_inode(olddentry), d_inode(old));
    1267           0 :         if (d_inode(new) && ovl_dentry_upper(new))
    1268           0 :                 ovl_copyattr(d_inode(newdentry), d_inode(new));
    1269             : 
    1270           0 : out_dput:
    1271           0 :         dput(newdentry);
    1272           0 : out_dput_old:
    1273           0 :         dput(olddentry);
    1274           0 : out_unlock:
    1275           0 :         unlock_rename(new_upperdir, old_upperdir);
    1276           0 : out_revert_creds:
    1277           0 :         revert_creds(old_cred);
    1278           0 :         if (update_nlink)
    1279           0 :                 ovl_nlink_end(new);
    1280           0 : out_drop_write:
    1281           0 :         ovl_drop_write(old);
    1282           0 : out:
    1283           0 :         dput(opaquedir);
    1284           0 :         ovl_cache_free(&list);
    1285           0 :         return err;
    1286             : }
    1287             : 
    1288             : const struct inode_operations ovl_dir_inode_operations = {
    1289             :         .lookup         = ovl_lookup,
    1290             :         .mkdir          = ovl_mkdir,
    1291             :         .symlink        = ovl_symlink,
    1292             :         .unlink         = ovl_unlink,
    1293             :         .rmdir          = ovl_rmdir,
    1294             :         .rename         = ovl_rename,
    1295             :         .link           = ovl_link,
    1296             :         .setattr        = ovl_setattr,
    1297             :         .create         = ovl_create,
    1298             :         .mknod          = ovl_mknod,
    1299             :         .permission     = ovl_permission,
    1300             :         .getattr        = ovl_getattr,
    1301             :         .listxattr      = ovl_listxattr,
    1302             :         .get_acl        = ovl_get_acl,
    1303             :         .update_time    = ovl_update_time,
    1304             : };

Generated by: LCOV version 1.14