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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * eCryptfs: Linux filesystem encryption layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 1997-2004 Erez Zadok
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 2001-2004 Stony Brook University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Copyright (C) 2004-2007 International Business Machines Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *              Michael C. Thompsion <mcthomps@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/dcache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/fs_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "ecryptfs_kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) static struct dentry *lock_parent(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 	struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	dir = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	return dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) static void unlock_dir(struct dentry *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	inode_unlock(d_inode(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	dput(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	return ecryptfs_inode_to_lower(inode) == lower_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) static int ecryptfs_inode_set(struct inode *inode, void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	struct inode *lower_inode = opaque;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	ecryptfs_set_inode_lower(inode, lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	fsstack_copy_attr_all(inode, lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	/* i_size will be overwritten for encrypted regular files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	fsstack_copy_inode_size(inode, lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	inode->i_ino = lower_inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	inode->i_mapping->a_ops = &ecryptfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	if (S_ISLNK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		inode->i_op = &ecryptfs_symlink_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	else if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 		inode->i_op = &ecryptfs_dir_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		inode->i_op = &ecryptfs_main_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		inode->i_fop = &ecryptfs_dir_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	else if (special_file(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		inode->i_fop = &ecryptfs_main_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 					  struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		return ERR_PTR(-EXDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	if (!igrab(lower_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	inode = iget5_locked(sb, (unsigned long)lower_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 			     ecryptfs_inode_test, ecryptfs_inode_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			     lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		iput(lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 		return ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	if (!(inode->i_state & I_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		iput(lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) struct inode *ecryptfs_get_inode(struct inode *lower_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 				 struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	if (!IS_ERR(inode) && (inode->i_state & I_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	return inode;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * ecryptfs_interpose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  * @lower_dentry: Existing dentry in the lower filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * @dentry: ecryptfs' dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * @sb: ecryptfs's super_block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * Interposes upper and lower dentries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) static int ecryptfs_interpose(struct dentry *lower_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 			      struct dentry *dentry, struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		return PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	return 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) static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 			      struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct inode *lower_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	lower_dir_inode = d_inode(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	inode_lock_nested(lower_dir_inode, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	dget(lower_dentry);	// don't even try to make the lower negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	if (lower_dentry->d_parent != lower_dir_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	else if (d_unhashed(lower_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		rc = vfs_unlink(lower_dir_inode, lower_dentry, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	fsstack_copy_attr_times(dir, lower_dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	inode->i_ctime = dir->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	dput(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	inode_unlock(lower_dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  * ecryptfs_do_create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  * @directory_inode: inode of the new file's dentry's parent in ecryptfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  * @ecryptfs_dentry: New file's dentry in ecryptfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * @mode: The mode of the new file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * Creates the underlying file and the eCryptfs inode which will link to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * it. It will also update the eCryptfs directory inode to mimic the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  * stat of the lower directory inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) static struct inode *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) ecryptfs_do_create(struct inode *directory_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		   struct dentry *ecryptfs_dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	lower_dir_dentry = lock_parent(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	rc = vfs_create(d_inode(lower_dir_dentry), lower_dentry, mode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		       "rc = [%d]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		inode = ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	inode = __ecryptfs_get_inode(d_inode(lower_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 				     directory_inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		vfs_unlink(d_inode(lower_dir_dentry), lower_dentry, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	fsstack_copy_attr_times(directory_inode, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	fsstack_copy_inode_size(directory_inode, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) out_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	unlock_dir(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * ecryptfs_initialize_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  * Cause the file to be changed from a basic empty file to an ecryptfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * file with a header and first data page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * Returns zero on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 			     struct inode *ecryptfs_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	struct ecryptfs_crypt_stat *crypt_stat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	if (S_ISDIR(ecryptfs_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	rc = ecryptfs_new_file_context(ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		ecryptfs_printk(KERN_ERR, "Error creating new file "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 				"context; rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		printk(KERN_ERR "%s: Error attempting to initialize "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 			"the lower file for the dentry with name "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 			"[%pd]; rc = [%d]\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			ecryptfs_dentry, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	ecryptfs_put_lower_file(ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) }
^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)  * ecryptfs_create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248)  * @dir: The inode of the directory in which to create the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249)  * @dentry: The eCryptfs dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250)  * @mode: The mode of the new file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252)  * Creates a new file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254)  * Returns zero on success; non-zero on error condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		umode_t mode, bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct inode *ecryptfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 					    mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (IS_ERR(ecryptfs_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		ecryptfs_printk(KERN_WARNING, "Failed to create file in"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 				"lower filesystem\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		rc = PTR_ERR(ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	/* At this point, a file exists on "disk"; we need to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	 * that this on disk file is prepared to be an ecryptfs file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 				   ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		iget_failed(ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	struct ecryptfs_crypt_stat *crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	rc = ecryptfs_get_lower_file(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		printk(KERN_ERR "%s: Error attempting to initialize "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			"the lower file for the dentry with name "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 			"[%pd]; rc = [%d]\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 			dentry, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	/* TODO: lock for crypt_stat comparison */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		ecryptfs_set_default_sizes(crypt_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	rc = ecryptfs_read_and_validate_header_region(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	ecryptfs_put_lower_file(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	/* Must return 0 to allow non-eCryptfs files to be looked up, too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317)  * ecryptfs_lookup_interpose - Dentry interposition for a lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 				     struct dentry *lower_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	struct inode *inode, *lower_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	struct ecryptfs_dentry_info *dentry_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	if (!dentry_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		dput(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	fsstack_copy_attr_atime(d_inode(dentry->d_parent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 				d_inode(path->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	BUG_ON(!d_count(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	ecryptfs_set_dentry_private(dentry, dentry_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	dentry_info->lower_path.mnt = mntget(path->mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	dentry_info->lower_path.dentry = lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	 * negative dentry can go positive under us here - its parent is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	 * locked.  That's OK and that could happen just as we return from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	 * ecryptfs_lookup() anyway.  Just need to be careful and fetch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	 * ->d_inode only once - it's not stable here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	lower_inode = READ_ONCE(lower_dentry->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	if (!lower_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		/* We want to add because we couldn't find in lower */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		d_add(dentry, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		       __func__, PTR_ERR(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		return ERR_CAST(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		rc = ecryptfs_i_size_read(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 			return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	if (inode->i_state & I_NEW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374)  * ecryptfs_lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375)  * @ecryptfs_dir_inode: The eCryptfs directory inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  * @flags: lookup flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  * Find a file on disk. If the file does not exist, then we'll add it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  * dentry cache and continue on to read it from the disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 				      struct dentry *ecryptfs_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 				      unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	char *encrypted_and_encoded_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	struct dentry *lower_dir_dentry, *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	const char *name = ecryptfs_dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	size_t len = ecryptfs_dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	struct dentry *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	mount_crypt_stat = &ecryptfs_superblock_to_private(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 				ecryptfs_dentry->d_sb)->mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		rc = ecryptfs_encrypt_and_encode_filename(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			&encrypted_and_encoded_name, &len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 			mount_crypt_stat, name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 			printk(KERN_ERR "%s: Error attempting to encrypt and encode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 			       "filename; rc = [%d]\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 			return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		name = encrypted_and_encoded_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	if (IS_ERR(lower_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 				"[%ld] on lower_dentry = [%s]\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 				PTR_ERR(lower_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 				name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		res = ERR_CAST(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	kfree(encrypted_and_encoded_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			 struct dentry *new_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	struct dentry *lower_old_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	struct dentry *lower_new_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	u64 file_size_save;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	file_size_save = i_size_read(d_inode(old_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	dget(lower_old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	dget(lower_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	lower_dir_dentry = lock_parent(lower_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	rc = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		      lower_new_dentry, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (rc || d_really_is_negative(lower_new_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	set_nlink(d_inode(old_dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		  ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	i_size_write(d_inode(new_dentry), file_size_save);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) out_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	unlock_dir(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	dput(lower_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	dput(lower_old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			    const char *symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	char *encoded_symname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	size_t encoded_symlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	dget(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	lower_dir_dentry = lock_parent(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	mount_crypt_stat = &ecryptfs_superblock_to_private(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		dir->i_sb)->mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 						  &encoded_symlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 						  mount_crypt_stat, symname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 						  strlen(symname));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	rc = vfs_symlink(d_inode(lower_dir_dentry), lower_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			 encoded_symname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	kfree(encoded_symname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (rc || d_really_is_negative(lower_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) out_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	unlock_dir(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	dput(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (d_really_is_negative(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	lower_dir_dentry = lock_parent(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	rc = vfs_mkdir(d_inode(lower_dir_dentry), lower_dentry, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	if (rc || d_really_is_negative(lower_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	unlock_dir(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	if (d_really_is_negative(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct inode *lower_dir_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	lower_dir_inode = d_inode(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	inode_lock_nested(lower_dir_inode, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	dget(lower_dentry);	// don't even try to make the lower negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if (lower_dentry->d_parent != lower_dir_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	else if (d_unhashed(lower_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		rc = vfs_rmdir(lower_dir_inode, lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		clear_nlink(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		fsstack_copy_attr_times(dir, lower_dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		set_nlink(dir, lower_dir_inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	dput(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	inode_unlock(lower_dir_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) ecryptfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	struct dentry *lower_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	lower_dir_dentry = lock_parent(lower_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	rc = vfs_mknod(d_inode(lower_dir_dentry), lower_dentry, mode, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	if (rc || d_really_is_negative(lower_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	unlock_dir(lower_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	if (d_really_is_negative(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	struct dentry *lower_old_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	struct dentry *lower_new_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	struct dentry *lower_old_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	struct dentry *lower_new_dir_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	struct dentry *trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	struct inode *target_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	target_inode = d_inode(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	dget(lower_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	if (lower_old_dentry->d_parent != lower_old_dir_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (lower_new_dentry->d_parent != lower_new_dir_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	/* source should not be ancestor of target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	if (trap == lower_old_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	/* target should not be ancestor of source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (trap == lower_new_dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		rc = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	rc = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			d_inode(lower_new_dir_dentry), lower_new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if (target_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		fsstack_copy_attr_all(target_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 				      ecryptfs_inode_to_lower(target_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	if (new_dir != old_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) out_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	dput(lower_new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	DEFINE_DELAYED_CALL(done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	const char *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	link = vfs_get_link(lower_dentry, &done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	if (IS_ERR(link))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		return ERR_CAST(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 						  link, strlen(link));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	do_delayed_call(&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) static const char *ecryptfs_get_link(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 				     struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 				     struct delayed_call *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		return ERR_PTR(-ECHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	buf = ecryptfs_readlink_lower(dentry, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	if (IS_ERR(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	fsstack_copy_attr_atime(d_inode(dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 				d_inode(ecryptfs_dentry_to_lower(dentry)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	buf[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	set_delayed_call(done, kfree_link, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681)  * upper_size_to_lower_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682)  * @crypt_stat: Crypt_stat associated with file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683)  * @upper_size: Size of the upper file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685)  * Calculate the required size of the lower file based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686)  * specified size of the upper file. This calculation is based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687)  * number of headers in the underlying file and the extent size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689)  * Returns Calculated size of the lower file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) static loff_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 			 loff_t upper_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	loff_t lower_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	lower_size = ecryptfs_lower_header_size(crypt_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	if (upper_size != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		loff_t num_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		num_extents = upper_size >> crypt_stat->extent_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		if (upper_size & ~crypt_stat->extent_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			num_extents++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		lower_size += (num_extents * crypt_stat->extent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	return lower_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  * truncate_upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * @dentry: The ecryptfs layer dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  * @ia: Address of the ecryptfs inode's attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713)  * @lower_ia: Address of the lower inode's attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715)  * Function to handle truncations modifying the size of the file. Note
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716)  * that the file sizes are interpolated. When expanding, we are simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717)  * writing strings of 0's out. When truncating, we truncate the upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718)  * inode and update the lower_ia according to the page index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719)  * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720)  * the caller must use lower_ia in a call to notify_change() to perform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721)  * the truncation of the lower inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) static int truncate_upper(struct dentry *dentry, struct iattr *ia,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			  struct iattr *lower_ia)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	struct ecryptfs_crypt_stat *crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	loff_t i_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	loff_t lower_size_before_truncate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	loff_t lower_size_after_truncate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	if (unlikely((ia->ia_size == i_size))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		lower_ia->ia_valid &= ~ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	rc = ecryptfs_get_lower_file(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	/* Switch on growing or shrinking file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	if (ia->ia_size > i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		char zero[] = { 0x00 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		lower_ia->ia_valid &= ~ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		/* Write a single 0 at the last position of the file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		 * this triggers code that will fill in 0's throughout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		 * the intermediate portion of the previous end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		 * file and the new and of the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		rc = ecryptfs_write(inode, zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 				    (ia->ia_size - 1), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	} else { /* ia->ia_size < i_size_read(inode) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		/* We're chopping off all the pages down to the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		 * in which ia->ia_size is located. Fill in the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		 * that page from (ia->ia_size & ~PAGE_MASK) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		 * PAGE_SIZE with zeros. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		size_t num_zeros = (PAGE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 				    - (ia->ia_size & ~PAGE_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			truncate_setsize(inode, ia->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			lower_ia->ia_size = ia->ia_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			lower_ia->ia_valid |= ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		if (num_zeros) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 			char *zeros_virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 			if (!zeros_virt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 				rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			rc = ecryptfs_write(inode, zeros_virt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 					    ia->ia_size, num_zeros);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			kfree(zeros_virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 				printk(KERN_ERR "Error attempting to zero out "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 				       "the remainder of the end page on "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 				       "reducing truncate; rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		truncate_setsize(inode, ia->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		rc = ecryptfs_write_inode_size_to_metadata(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			printk(KERN_ERR	"Problem with "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			       "ecryptfs_write_inode_size_to_metadata; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 			       "rc = [%d]\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		/* We are reducing the size of the ecryptfs file, and need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		 * know if we need to reduce the size of the lower file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		lower_size_before_truncate =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		    upper_size_to_lower_size(crypt_stat, i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		lower_size_after_truncate =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		    upper_size_to_lower_size(crypt_stat, ia->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		if (lower_size_after_truncate < lower_size_before_truncate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			lower_ia->ia_size = lower_size_after_truncate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			lower_ia->ia_valid |= ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 			lower_ia->ia_valid &= ~ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	ecryptfs_put_lower_file(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	struct ecryptfs_crypt_stat *crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	loff_t lower_oldsize, lower_newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	lower_oldsize = upper_size_to_lower_size(crypt_stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 						 i_size_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	if (lower_newsize > lower_oldsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		 * The eCryptfs inode and the new *lower* size are mixed here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		 * because we may not have the lower i_mutex held and/or it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		 * not be appropriate to call inode_newsize_ok() with inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		 * from other filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		return inode_newsize_ok(inode, lower_newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) }
^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)  * ecryptfs_truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835)  * @dentry: The ecryptfs layer dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836)  * @new_length: The length to expand the file to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838)  * Simple function that handles the truncation of an eCryptfs inode and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * its corresponding lower inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841)  * Returns zero on success; non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	struct iattr lower_ia = { .ia_valid = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	rc = truncate_upper(dentry, &ia, &lower_ia);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		inode_lock(d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		rc = notify_change(lower_dentry, &lower_ia, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		inode_unlock(d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	return rc;
^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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) ecryptfs_permission(struct inode *inode, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	return inode_permission(ecryptfs_inode_to_lower(inode), mask);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871)  * ecryptfs_setattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872)  * @dentry: dentry handle to the inode to modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873)  * @ia: Structure with flags of what to change and values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875)  * Updates the metadata of an inode. If the update is to the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876)  * i.e. truncation, then ecryptfs_truncate will handle the size modification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877)  * of both the ecryptfs inode and the lower inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879)  * All other metadata changes will be passed right to the lower filesystem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880)  * and we will just update our inode to look like the lower.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	struct iattr lower_ia;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	struct inode *lower_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	struct ecryptfs_crypt_stat *crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		rc = ecryptfs_init_crypt_stat(crypt_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	lower_inode = ecryptfs_inode_to_lower(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	mutex_lock(&crypt_stat->cs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	if (d_is_dir(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	else if (d_is_reg(dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		     || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		mount_crypt_stat = &ecryptfs_superblock_to_private(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			dentry->d_sb)->mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		rc = ecryptfs_get_lower_file(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 			mutex_unlock(&crypt_stat->cs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		rc = ecryptfs_read_metadata(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		ecryptfs_put_lower_file(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			if (!(mount_crypt_stat->flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			      & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 				rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 				printk(KERN_WARNING "Either the lower file "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 				       "is not in a valid eCryptfs format, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 				       "or the key could not be retrieved. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 				       "Plaintext passthrough mode is not "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 				       "enabled; returning -EIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 				mutex_unlock(&crypt_stat->cs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 					       | ECRYPTFS_ENCRYPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	mutex_unlock(&crypt_stat->cs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	rc = setattr_prepare(dentry, ia);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (ia->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	memcpy(&lower_ia, ia, sizeof(lower_ia));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	if (ia->ia_valid & ATTR_FILE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	if (ia->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		rc = truncate_upper(dentry, ia, &lower_ia);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	 * mode change is for clearing setuid/setgid bits. Allow lower fs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	 * to interpret this in its own way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		lower_ia.ia_valid &= ~ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	inode_lock(d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	rc = notify_change(lower_dentry, &lower_ia, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	inode_unlock(d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	fsstack_copy_attr_all(inode, lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) static int ecryptfs_getattr_link(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 				 u32 request_mask, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	struct dentry *dentry = path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	mount_crypt_stat = &ecryptfs_superblock_to_private(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 						dentry->d_sb)->mount_crypt_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	generic_fillattr(d_inode(dentry), stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		char *target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		size_t targetsiz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		target = ecryptfs_readlink_lower(dentry, &targetsiz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		if (!IS_ERR(target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			kfree(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			stat->size = targetsiz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			rc = PTR_ERR(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) static int ecryptfs_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			    u32 request_mask, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	struct dentry *dentry = path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	struct kstat lower_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			 request_mask, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		fsstack_copy_attr_all(d_inode(dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 				      ecryptfs_inode_to_lower(d_inode(dentry)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		generic_fillattr(d_inode(dentry), stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		stat->blocks = lower_stat.blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		  const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		  size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	if (!(d_inode(lower_dentry)->i_opflags & IOP_XATTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		rc = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	rc = vfs_setxattr(lower_dentry, name, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	if (!rc && inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		fsstack_copy_attr_all(inode, d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			const char *name, void *value, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		rc = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	inode_lock(lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			    XATTR_NOSECURITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	inode_unlock(lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		  const char *name, void *value, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				       ecryptfs_inode_to_lower(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 				       name, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	if (!d_inode(lower_dentry)->i_op->listxattr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		rc = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	inode_lock(d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	inode_unlock(d_inode(lower_dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 				const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	struct dentry *lower_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	struct inode *lower_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	lower_inode = ecryptfs_inode_to_lower(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		rc = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	inode_lock(lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	rc = __vfs_removexattr(lower_dentry, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	inode_unlock(lower_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) const struct inode_operations ecryptfs_symlink_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	.get_link = ecryptfs_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	.permission = ecryptfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	.setattr = ecryptfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	.getattr = ecryptfs_getattr_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	.listxattr = ecryptfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) const struct inode_operations ecryptfs_dir_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	.create = ecryptfs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	.lookup = ecryptfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	.link = ecryptfs_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	.unlink = ecryptfs_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	.symlink = ecryptfs_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	.mkdir = ecryptfs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	.rmdir = ecryptfs_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	.mknod = ecryptfs_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	.rename = ecryptfs_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	.permission = ecryptfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	.setattr = ecryptfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	.listxattr = ecryptfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) const struct inode_operations ecryptfs_main_iops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	.permission = ecryptfs_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	.setattr = ecryptfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	.getattr = ecryptfs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	.listxattr = ecryptfs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) static int ecryptfs_xattr_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 			      struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			      const char *name, void *buffer, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 			      int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	return ecryptfs_getxattr(dentry, inode, name, buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) static int ecryptfs_xattr_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			      struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 			      const char *name, const void *value, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			      int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		BUG_ON(flags != XATTR_REPLACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		return ecryptfs_removexattr(dentry, inode, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) static const struct xattr_handler ecryptfs_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	.prefix = "",  /* match anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	.get = ecryptfs_xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	.set = ecryptfs_xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) const struct xattr_handler *ecryptfs_xattr_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	&ecryptfs_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) };