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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/splice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/fdtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "overlayfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	pr_warn("\"check_copy_up\" module option is obsolete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) static int ovl_ccup_get(char *buf, const struct kernel_param *param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	return sprintf(buf, "N\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static bool ovl_must_copy_xattr(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	return !strcmp(name, XATTR_POSIX_ACL_ACCESS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	       !strcmp(name, XATTR_POSIX_ACL_DEFAULT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	       !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 		   struct dentry *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	ssize_t list_size, size, value_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	char *buf, *name, *value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	size_t slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	if (!(old->d_inode->i_opflags & IOP_XATTR) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	    !(new->d_inode->i_opflags & IOP_XATTR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	list_size = vfs_listxattr(old, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	if (list_size <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		if (list_size == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		return list_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	buf = kzalloc(list_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	list_size = vfs_listxattr(old, buf, list_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	if (list_size <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		error = list_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	for (name = buf; list_size; name += slen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		slen = strnlen(name, list_size) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		/* underlying fs providing us with an broken xattr list? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		if (WARN_ON(slen > list_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 			error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		list_size -= slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		if (ovl_is_private_xattr(sb, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		error = security_inode_copy_up_xattr(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		if (error < 0 && error != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		if (error == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 			continue; /* Discard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		size = vfs_getxattr(old, name, value, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		if (size == -ERANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 			size = vfs_getxattr(old, name, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		if (size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 			error = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		if (size > value_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 			void *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 			new = krealloc(value, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 			if (!new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 				error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 			value = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 			value_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		error = vfs_setxattr(new, name, value, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 			if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 			/* Ignore failure to copy unknown xattrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 			    struct path *new, loff_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct file *old_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct file *new_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	loff_t old_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	loff_t new_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	loff_t cloned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	loff_t data_pos = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	loff_t hole_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	bool skip_hole = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	if (IS_ERR(old_file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		return PTR_ERR(old_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	if (IS_ERR(new_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		error = PTR_ERR(new_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		goto out_fput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	/* Try to use clone_file_range to clone up within the same fs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	if (cloned == len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	/* Couldn't clone, so now we try to copy the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	/* Check if lower fs supports seek operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	if (old_file->f_mode & FMODE_LSEEK &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	    old_file->f_op->llseek)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		skip_hole = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		if (len < this_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			this_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		if (signal_pending_state(TASK_KILLABLE, current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 			error = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		 * Fill zero for hole will cost unnecessary disk space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		 * and meanwhile slow down the copy-up speed, so we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		 * an optimization for hole during copy-up, it relies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		 * on SEEK_DATA implementation in lower fs so if lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		 * fs does not support it, copy-up will behave as before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		 * Detail logic of hole detection as below:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		 * When we detect next data position is larger than current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		 * position we will skip that hole, otherwise we copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		 * it may not recognize all kind of holes and sometimes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		 * only skips partial of hole area. However, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		 * enough for most of the use cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		if (skip_hole && data_pos < old_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 			data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 			if (data_pos > old_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 				hole_len = data_pos - old_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 				len -= hole_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 				old_pos = new_pos = data_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			} else if (data_pos == -ENXIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			} else if (data_pos < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 				skip_hole = false;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		bytes = do_splice_direct(old_file, &old_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 					 new_file, &new_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 					 this_len, SPLICE_F_MOVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		if (bytes <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 			error = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		WARN_ON(old_pos != new_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		len -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (!error && ovl_should_sync(ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		error = vfs_fsync(new_file, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	fput(new_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) out_fput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	fput(old_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	return error;
^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_size(struct dentry *upperdentry, struct kstat *stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	struct iattr attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		.ia_valid = ATTR_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		.ia_size = stat->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	return notify_change(upperdentry, &attr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct iattr attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		.ia_valid =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		     ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		.ia_atime = stat->atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		.ia_mtime = stat->mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	return notify_change(upperdentry, &attr, NULL);
^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) int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	if (!S_ISLNK(stat->mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		struct iattr attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			.ia_valid = ATTR_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 			.ia_mode = stat->mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		err = notify_change(upperdentry, &attr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		struct iattr attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 			.ia_valid = ATTR_UID | ATTR_GID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			.ia_uid = stat->uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			.ia_gid = stat->gid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		err = notify_change(upperdentry, &attr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		ovl_set_timestamps(upperdentry, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	struct ovl_fh *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	int fh_type, dwords;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	int buflen = MAX_HANDLE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	uuid_t *uuid = &real->d_sb->s_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	/* Make sure the real fid stays 32bit aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	if (!fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		return ERR_PTR(-ENOMEM);
^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) 	 * We encode a non-connectable file handle for non-dir, because we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	 * only need to find the lower inode number and we don't want to pay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	 * the price or reconnecting the dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	dwords = buflen >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	buflen = (dwords << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	if (WARN_ON(fh_type < 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	    WARN_ON(buflen > MAX_HANDLE_SZ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	    WARN_ON(fh_type == FILEID_INVALID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	fh->fb.version = OVL_FH_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	fh->fb.magic = OVL_FH_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	fh->fb.type = fh_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	 * When we will want to decode an overlay dentry from this handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	 * and all layers are on the same fs, if we get a disconncted real
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	 * dentry when we decode fid, the only way to tell if we should assign
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	 * it to upperdentry or to lowerstack is by checking this flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	if (is_upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	fh->fb.len = sizeof(fh->fb) + buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	fh->fb.uuid = *uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	return fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		   struct dentry *upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	const struct ovl_fh *fh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	 * When lower layer doesn't support export operations store a 'null' fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	 * so we can use the overlay.origin xattr to distignuish between a copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	 * up and a pure upper inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if (ovl_can_decode_fh(lower->d_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		fh = ovl_encode_real_fh(lower, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		if (IS_ERR(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 			return PTR_ERR(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	 * Do not fail when upper doesn't support xattrs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 				 fh ? fh->fb.len : 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) /* Store file handle of @upper dir in @index dir entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			    struct dentry *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	const struct ovl_fh *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	fh = ovl_encode_real_fh(upper, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	if (IS_ERR(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		return PTR_ERR(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	err = ovl_do_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  * Create and install index entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  * Caller must hold i_mutex on indexdir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 			    struct dentry *upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct inode *dir = d_inode(indexdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	struct dentry *index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	struct dentry *temp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	struct qstr name = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	 * For now this is only used for creating index entry for directories,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	 * because non-dir are copied up directly to index and then hardlinked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	 * to upper dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	 * TODO: implement create index for non-dir, so we can call it when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	 * encoding file handle for non-dir in case index does not exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	if (WARN_ON(!d_is_dir(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	/* Directory not expected to be indexed before copy up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	err = ovl_get_index_name(origin, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	err = PTR_ERR(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	if (IS_ERR(temp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		goto free_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	err = ovl_set_upper_fh(OVL_FS(dentry->d_sb), upper, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	index = lookup_one_len(name.name, indexdir, name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	if (IS_ERR(index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		err = PTR_ERR(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		err = ovl_do_rename(dir, temp, dir, index, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		ovl_cleanup(dir, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	dput(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) free_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	kfree(name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) struct ovl_copy_up_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	struct dentry *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	struct path lowerpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	struct kstat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	struct kstat pstat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	const char *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	struct dentry *destdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	struct qstr destname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	struct dentry *workdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	bool origin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	bool indexed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	bool metacopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) static int ovl_link_up(struct ovl_copy_up_ctx *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	struct dentry *upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	struct dentry *upperdir = ovl_dentry_upper(c->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	struct inode *udir = d_inode(upperdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	/* Mark parent "impure" because it may now contain non-pure upper */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	err = ovl_set_impure(c->parent, upperdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	err = ovl_set_nlink_lower(c->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	inode_lock_nested(udir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	upper = lookup_one_len(c->dentry->d_name.name, upperdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			       c->dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	err = PTR_ERR(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	if (!IS_ERR(upper)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			/* Restore timestamps on parent (best effort) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			ovl_set_timestamps(upperdir, &c->pstat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			ovl_dentry_set_upper_alias(c->dentry);
^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) 	inode_unlock(udir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	err = ovl_set_nlink_upper(c->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	 * Copy up data first and then xattrs. Writing data after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	 * xattrs will remove security.capability xattr automatically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	if (S_ISREG(c->stat.mode) && !c->metacopy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		struct path upperpath, datapath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		ovl_path_upper(c->dentry, &upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		if (WARN_ON(upperpath.dentry != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		upperpath.dentry = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		ovl_path_lowerdata(c->dentry, &datapath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		err = ovl_copy_up_data(ofs, &datapath, &upperpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 				       c->stat.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	 * Store identifier of lower inode in upper inode xattr to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	 * allow lookup of the copy up origin inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	 * Don't set origin when we are breaking the association with a lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	 * hard link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	if (c->origin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (c->metacopy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 					 NULL, 0, -EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	inode_lock(temp->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (S_ISREG(c->stat.mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		err = ovl_set_size(temp, &c->stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		err = ovl_set_attr(temp, &c->stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	inode_unlock(temp->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) struct ovl_cu_creds {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	const struct cred *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	struct cred *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	cc->old = cc->new = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	err = security_inode_copy_up(dentry, &cc->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	if (cc->new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		cc->old = override_creds(cc->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	if (cc->new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		revert_creds(cc->old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		put_cred(cc->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576)  * Copyup using workdir to prepare temp file.  Used when copying up directories,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)  * special files or when upper fs doesn't support O_TMPFILE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	struct dentry *temp, *upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	struct ovl_cu_creds cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	struct ovl_cattr cattr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		/* Can't properly set mode on creation because of the umask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		.mode = c->stat.mode & S_IFMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		.rdev = c->stat.rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		.link = c->link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	/* workdir and destdir could be the same when copying up to indexdir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	if (lock_rename(c->workdir, c->destdir) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	err = ovl_prep_cu_creds(c->dentry, &cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	temp = ovl_create_temp(c->workdir, &cattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	ovl_revert_cu_creds(&cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	err = PTR_ERR(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	if (IS_ERR(temp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	err = ovl_copy_up_inode(c, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (S_ISDIR(c->stat.mode) && c->indexed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			goto cleanup;
^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) 	upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	err = PTR_ERR(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	if (IS_ERR(upper))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	err = ovl_do_rename(wdir, temp, udir, upper, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	if (!c->metacopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		ovl_set_upperdata(d_inode(c->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	inode = d_inode(c->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	ovl_inode_update(inode, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		ovl_set_flag(OVL_WHITEOUTS, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	unlock_rename(c->workdir, c->destdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	ovl_cleanup(wdir, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	dput(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) /* Copyup using O_TMPFILE which does not require cross dir locking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	struct inode *udir = d_inode(c->destdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	struct dentry *temp, *upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	struct ovl_cu_creds cc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	err = ovl_prep_cu_creds(c->dentry, &cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	ovl_revert_cu_creds(&cc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (IS_ERR(temp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		return PTR_ERR(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	err = ovl_copy_up_inode(c, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	inode_lock_nested(udir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	err = PTR_ERR(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (!IS_ERR(upper)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		err = ovl_do_link(temp, udir, upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	inode_unlock(udir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (!c->metacopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		ovl_set_upperdata(d_inode(c->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	ovl_inode_update(d_inode(c->dentry), temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) out_dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	dput(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693)  * Copy up a single dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695)  * All renames start with copy up of source if necessary.  The actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696)  * rename will only proceed once the copy up was successful.  Copy up uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697)  * upper parent i_mutex for exclusion.  Since rename can change d_parent it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698)  * is possible that the copy up will lock the old parent.  At that point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699)  * the file will have already been copied up anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	bool to_index = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	 * Indexed non-dir is copied up directly to the index entry and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	 * then index entry is created and then copied up dir installed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	 * Copying dir up to indexdir instead of workdir simplifies locking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	if (ovl_need_index(c->dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		c->indexed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		if (S_ISDIR(c->stat.mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 			c->workdir = ovl_indexdir(c->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			to_index = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		c->origin = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (to_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		c->destdir = ovl_indexdir(c->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	} else if (WARN_ON(!c->parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		/* Disconnected dentry must be copied up to index dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 * Mark parent "impure" because it may now contain non-pure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		 * upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		err = ovl_set_impure(c->parent, c->destdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	/* Should we copyup with O_TMPFILE or with workdir? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (S_ISREG(c->stat.mode) && ofs->tmpfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		err = ovl_copy_up_tmpfile(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		err = ovl_copy_up_workdir(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	if (c->indexed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	if (to_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		/* Initialize nlink for copy up of disconnected dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		err = ovl_set_nlink_upper(c->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		struct inode *udir = d_inode(c->destdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		/* Restore timestamps on parent (best effort) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		inode_lock(udir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		ovl_set_timestamps(c->destdir, &c->pstat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		inode_unlock(udir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		ovl_dentry_set_upper_alias(c->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (to_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		kfree(c->destname.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 				  int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	if (!ofs->config.metacopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (!S_ISREG(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	res = vfs_getxattr(dentry, name, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	if (res == -ENODATA || res == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	if (res > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		buf = kzalloc(res, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		res = vfs_getxattr(dentry, name, buf, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 			kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			*value = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) /* Copy up data of an inode which was copied up metadata only in the past. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	struct path upperpath, datapath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	char *capability = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	ssize_t cap_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	ovl_path_upper(c->dentry, &upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	if (WARN_ON(upperpath.dentry == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	ovl_path_lowerdata(c->dentry, &datapath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	if (WARN_ON(datapath.dentry == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (c->stat.size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 					      &capability);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		if (cap_size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	 * Writing to upper file will clear security.capability xattr. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	 * don't want that to happen for normal copy-up operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	if (capability) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		err = vfs_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 				   capability, cap_size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	err = ovl_do_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	ovl_set_upperdata(d_inode(c->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	kfree(capability);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			   int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	DEFINE_DELAYED_CALL(done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	struct path parentpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	struct ovl_copy_up_ctx ctx = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		.parent = parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		.dentry = dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		.workdir = ovl_workdir(dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	if (WARN_ON(!ctx.workdir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	ovl_path_lower(dentry, &ctx.lowerpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			  STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		ovl_path_upper(parent, &parentpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		ctx.destdir = parentpath.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		ctx.destname = dentry->d_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		err = vfs_getattr(&parentpath, &ctx.pstat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 				  STATX_ATIME | STATX_MTIME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 				  AT_STATX_SYNC_AS_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	/* maybe truncate regular file. this has no effect on dirs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if (flags & O_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		ctx.stat.size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	if (S_ISLNK(ctx.stat.mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		if (IS_ERR(ctx.link))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			return PTR_ERR(ctx.link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	err = ovl_copy_up_start(dentry, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	/* err < 0: interrupted, err > 0: raced with another copy-up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		if (!ovl_dentry_upper(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			err = ovl_do_copy_up(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			err = ovl_link_up(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			err = ovl_copy_up_meta_inode_data(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		ovl_copy_up_end(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	do_delayed_call(&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) static int ovl_copy_up_flags(struct dentry *dentry, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	 * With NFS export, copy up can get called for a disconnected non-dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	 * In this case, we will copy up lower inode to index dir without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	 * linking it to upper dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (WARN_ON(disconnected && d_is_dir(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	while (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		struct dentry *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		struct dentry *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		if (ovl_already_copied_up(dentry, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		next = dget(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		/* find the topmost dentry not yet copied up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		for (; !disconnected;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			parent = dget_parent(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			if (ovl_dentry_upper(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			next = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		err = ovl_copy_up_one(parent, next, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	/* Copy up of disconnected dentry does not set upper alias */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (ovl_already_copied_up(dentry, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	if (special_file(d_inode(dentry)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (!ovl_open_flags_need_copy_up(flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) int ovl_maybe_copy_up(struct dentry *dentry, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	if (ovl_open_need_copy_up(dentry, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		err = ovl_want_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			err = ovl_copy_up_flags(dentry, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			ovl_drop_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) int ovl_copy_up_with_data(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	return ovl_copy_up_flags(dentry, O_WRONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) int ovl_copy_up(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	return ovl_copy_up_flags(dentry, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) }