Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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