xref: /openbmc/linux/fs/ecryptfs/inode.c (revision 88569546)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d17074acSLee Jones /*
3237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
4237fead6SMichael Halcrow  *
5237fead6SMichael Halcrow  * Copyright (C) 1997-2004 Erez Zadok
6237fead6SMichael Halcrow  * Copyright (C) 2001-2004 Stony Brook University
7dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
8237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
9237fead6SMichael Halcrow  *              Michael C. Thompsion <mcthomps@us.ibm.com>
10237fead6SMichael Halcrow  */
11237fead6SMichael Halcrow 
12237fead6SMichael Halcrow #include <linux/file.h>
13237fead6SMichael Halcrow #include <linux/vmalloc.h>
14237fead6SMichael Halcrow #include <linux/pagemap.h>
15237fead6SMichael Halcrow #include <linux/dcache.h>
16237fead6SMichael Halcrow #include <linux/namei.h>
17237fead6SMichael Halcrow #include <linux/mount.h>
180cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
2048b512e6SRoberto Sassu #include <linux/xattr.h>
2197e2dee9SMiklos Szeredi #include <linux/fileattr.h>
220a688ad7SHarvey Harrison #include <asm/unaligned.h>
23237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
24237fead6SMichael Halcrow 
25b2648d51SAl Viro static int lock_parent(struct dentry *dentry,
26b2648d51SAl Viro 		       struct dentry **lower_dentry,
27b2648d51SAl Viro 		       struct inode **lower_dir)
28237fead6SMichael Halcrow {
29b2648d51SAl Viro 	struct dentry *lower_dir_dentry;
30237fead6SMichael Halcrow 
31b2648d51SAl Viro 	lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
32b2648d51SAl Viro 	*lower_dir = d_inode(lower_dir_dentry);
33b2648d51SAl Viro 	*lower_dentry = ecryptfs_dentry_to_lower(dentry);
34237fead6SMichael Halcrow 
35b2648d51SAl Viro 	inode_lock_nested(*lower_dir, I_MUTEX_PARENT);
36b2648d51SAl Viro 	return (*lower_dentry)->d_parent == lower_dir_dentry ? 0 : -EINVAL;
37237fead6SMichael Halcrow }
38237fead6SMichael Halcrow 
39c4f79073STyler Hicks static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
40c4f79073STyler Hicks {
41c4cf3ba4SHimangi Saraogi 	return ecryptfs_inode_to_lower(inode) == lower_inode;
42c4f79073STyler Hicks }
43c4f79073STyler Hicks 
445ccf9203STyler Hicks static int ecryptfs_inode_set(struct inode *inode, void *opaque)
45c4f79073STyler Hicks {
465ccf9203STyler Hicks 	struct inode *lower_inode = opaque;
475ccf9203STyler Hicks 
485ccf9203STyler Hicks 	ecryptfs_set_inode_lower(inode, lower_inode);
495ccf9203STyler Hicks 	fsstack_copy_attr_all(inode, lower_inode);
505ccf9203STyler Hicks 	/* i_size will be overwritten for encrypted regular files */
515ccf9203STyler Hicks 	fsstack_copy_inode_size(inode, lower_inode);
525ccf9203STyler Hicks 	inode->i_ino = lower_inode->i_ino;
53c4f79073STyler Hicks 	inode->i_mapping->a_ops = &ecryptfs_aops;
545ccf9203STyler Hicks 
555ccf9203STyler Hicks 	if (S_ISLNK(inode->i_mode))
565ccf9203STyler Hicks 		inode->i_op = &ecryptfs_symlink_iops;
575ccf9203STyler Hicks 	else if (S_ISDIR(inode->i_mode))
585ccf9203STyler Hicks 		inode->i_op = &ecryptfs_dir_iops;
595ccf9203STyler Hicks 	else
605ccf9203STyler Hicks 		inode->i_op = &ecryptfs_main_iops;
615ccf9203STyler Hicks 
625ccf9203STyler Hicks 	if (S_ISDIR(inode->i_mode))
635ccf9203STyler Hicks 		inode->i_fop = &ecryptfs_dir_fops;
645ccf9203STyler Hicks 	else if (special_file(inode->i_mode))
655ccf9203STyler Hicks 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
665ccf9203STyler Hicks 	else
675ccf9203STyler Hicks 		inode->i_fop = &ecryptfs_main_fops;
685ccf9203STyler Hicks 
69c4f79073STyler Hicks 	return 0;
70c4f79073STyler Hicks }
71c4f79073STyler Hicks 
725ccf9203STyler Hicks static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
735ccf9203STyler Hicks 					  struct super_block *sb)
745ccf9203STyler Hicks {
755ccf9203STyler Hicks 	struct inode *inode;
765ccf9203STyler Hicks 
775ccf9203STyler Hicks 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
785ccf9203STyler Hicks 		return ERR_PTR(-EXDEV);
795ccf9203STyler Hicks 	if (!igrab(lower_inode))
805ccf9203STyler Hicks 		return ERR_PTR(-ESTALE);
815ccf9203STyler Hicks 	inode = iget5_locked(sb, (unsigned long)lower_inode,
825ccf9203STyler Hicks 			     ecryptfs_inode_test, ecryptfs_inode_set,
835ccf9203STyler Hicks 			     lower_inode);
845ccf9203STyler Hicks 	if (!inode) {
855ccf9203STyler Hicks 		iput(lower_inode);
865ccf9203STyler Hicks 		return ERR_PTR(-EACCES);
875ccf9203STyler Hicks 	}
885ccf9203STyler Hicks 	if (!(inode->i_state & I_NEW))
895ccf9203STyler Hicks 		iput(lower_inode);
905ccf9203STyler Hicks 
915ccf9203STyler Hicks 	return inode;
925ccf9203STyler Hicks }
935ccf9203STyler Hicks 
94c4f79073STyler Hicks struct inode *ecryptfs_get_inode(struct inode *lower_inode,
95c4f79073STyler Hicks 				 struct super_block *sb)
96c4f79073STyler Hicks {
975ccf9203STyler Hicks 	struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
98c4f79073STyler Hicks 
995ccf9203STyler Hicks 	if (!IS_ERR(inode) && (inode->i_state & I_NEW))
100c4f79073STyler Hicks 		unlock_new_inode(inode);
1015ccf9203STyler Hicks 
102c4f79073STyler Hicks 	return inode;
103c4f79073STyler Hicks }
104c4f79073STyler Hicks 
105c4f79073STyler Hicks /**
106c4f79073STyler Hicks  * ecryptfs_interpose
107c4f79073STyler Hicks  * @lower_dentry: Existing dentry in the lower filesystem
108c4f79073STyler Hicks  * @dentry: ecryptfs' dentry
109c4f79073STyler Hicks  * @sb: ecryptfs's super_block
110c4f79073STyler Hicks  *
111c4f79073STyler Hicks  * Interposes upper and lower dentries.
112c4f79073STyler Hicks  *
113c4f79073STyler Hicks  * Returns zero on success; non-zero otherwise
114c4f79073STyler Hicks  */
115c4f79073STyler Hicks static int ecryptfs_interpose(struct dentry *lower_dentry,
1165ccf9203STyler Hicks 			      struct dentry *dentry, struct super_block *sb)
117c4f79073STyler Hicks {
1182b0143b5SDavid Howells 	struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
1195ccf9203STyler Hicks 
120c4f79073STyler Hicks 	if (IS_ERR(inode))
121c4f79073STyler Hicks 		return PTR_ERR(inode);
122c4f79073STyler Hicks 	d_instantiate(dentry, inode);
1235ccf9203STyler Hicks 
124c4f79073STyler Hicks 	return 0;
125c4f79073STyler Hicks }
126c4f79073STyler Hicks 
1278bc2d3cfSTyler Hicks static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
1288bc2d3cfSTyler Hicks 			      struct inode *inode)
1298bc2d3cfSTyler Hicks {
130b2648d51SAl Viro 	struct dentry *lower_dentry;
131b2648d51SAl Viro 	struct inode *lower_dir;
1328bc2d3cfSTyler Hicks 	int rc;
1338bc2d3cfSTyler Hicks 
134b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
135bcf0d9d4SAl Viro 	dget(lower_dentry);	// don't even try to make the lower negative
136b2648d51SAl Viro 	if (!rc) {
137b2648d51SAl Viro 		if (d_unhashed(lower_dentry))
138bcf0d9d4SAl Viro 			rc = -EINVAL;
139bcf0d9d4SAl Viro 		else
140b2648d51SAl Viro 			rc = vfs_unlink(&init_user_ns, lower_dir, lower_dentry,
1416521f891SChristian Brauner 					NULL);
142b2648d51SAl Viro 	}
1438bc2d3cfSTyler Hicks 	if (rc) {
1448bc2d3cfSTyler Hicks 		printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
1458bc2d3cfSTyler Hicks 		goto out_unlock;
1468bc2d3cfSTyler Hicks 	}
147b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
1488bc2d3cfSTyler Hicks 	set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
1498bc2d3cfSTyler Hicks 	inode->i_ctime = dir->i_ctime;
1508bc2d3cfSTyler Hicks out_unlock:
1518bc2d3cfSTyler Hicks 	dput(lower_dentry);
152b2648d51SAl Viro 	inode_unlock(lower_dir);
153bcf0d9d4SAl Viro 	if (!rc)
154bcf0d9d4SAl Viro 		d_drop(dentry);
1558bc2d3cfSTyler Hicks 	return rc;
1568bc2d3cfSTyler Hicks }
1578bc2d3cfSTyler Hicks 
158237fead6SMichael Halcrow /**
159237fead6SMichael Halcrow  * ecryptfs_do_create
160237fead6SMichael Halcrow  * @directory_inode: inode of the new file's dentry's parent in ecryptfs
161237fead6SMichael Halcrow  * @ecryptfs_dentry: New file's dentry in ecryptfs
162237fead6SMichael Halcrow  * @mode: The mode of the new file
163237fead6SMichael Halcrow  *
164237fead6SMichael Halcrow  * Creates the underlying file and the eCryptfs inode which will link to
165237fead6SMichael Halcrow  * it. It will also update the eCryptfs directory inode to mimic the
166237fead6SMichael Halcrow  * stat of the lower directory inode.
167237fead6SMichael Halcrow  *
168b59db43aSTyler Hicks  * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
169237fead6SMichael Halcrow  */
170b59db43aSTyler Hicks static struct inode *
171237fead6SMichael Halcrow ecryptfs_do_create(struct inode *directory_inode,
172175a4eb7SAl Viro 		   struct dentry *ecryptfs_dentry, umode_t mode)
173237fead6SMichael Halcrow {
174237fead6SMichael Halcrow 	int rc;
175237fead6SMichael Halcrow 	struct dentry *lower_dentry;
176b2648d51SAl Viro 	struct inode *lower_dir;
177b59db43aSTyler Hicks 	struct inode *inode;
178237fead6SMichael Halcrow 
179b2648d51SAl Viro 	rc = lock_parent(ecryptfs_dentry, &lower_dentry, &lower_dir);
180b2648d51SAl Viro 	if (!rc)
181b2648d51SAl Viro 		rc = vfs_create(&init_user_ns, lower_dir,
182b2648d51SAl Viro 				lower_dentry, mode, true);
1834981e081SMichael Halcrow 	if (rc) {
184caeeeecfSMichael Halcrow 		printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
18518d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
186b59db43aSTyler Hicks 		inode = ERR_PTR(rc);
187237fead6SMichael Halcrow 		goto out_lock;
188237fead6SMichael Halcrow 	}
1892b0143b5SDavid Howells 	inode = __ecryptfs_get_inode(d_inode(lower_dentry),
1905ccf9203STyler Hicks 				     directory_inode->i_sb);
1918bc2d3cfSTyler Hicks 	if (IS_ERR(inode)) {
192b2648d51SAl Viro 		vfs_unlink(&init_user_ns, lower_dir, lower_dentry, NULL);
193237fead6SMichael Halcrow 		goto out_lock;
1948bc2d3cfSTyler Hicks 	}
195b2648d51SAl Viro 	fsstack_copy_attr_times(directory_inode, lower_dir);
196b2648d51SAl Viro 	fsstack_copy_inode_size(directory_inode, lower_dir);
197237fead6SMichael Halcrow out_lock:
198b2648d51SAl Viro 	inode_unlock(lower_dir);
199b59db43aSTyler Hicks 	return inode;
200237fead6SMichael Halcrow }
201237fead6SMichael Halcrow 
202d17074acSLee Jones /*
203237fead6SMichael Halcrow  * ecryptfs_initialize_file
204237fead6SMichael Halcrow  *
205237fead6SMichael Halcrow  * Cause the file to be changed from a basic empty file to an ecryptfs
206237fead6SMichael Halcrow  * file with a header and first data page.
207237fead6SMichael Halcrow  *
208237fead6SMichael Halcrow  * Returns zero on success
209237fead6SMichael Halcrow  */
210e3ccaa97STyler Hicks int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
211b59db43aSTyler Hicks 			     struct inode *ecryptfs_inode)
212237fead6SMichael Halcrow {
213d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
214b59db43aSTyler Hicks 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
215237fead6SMichael Halcrow 	int rc = 0;
216237fead6SMichael Halcrow 
217b59db43aSTyler Hicks 	if (S_ISDIR(ecryptfs_inode->i_mode)) {
218237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
219e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
220d7cdc5feSMichael Halcrow 		goto out;
221237fead6SMichael Halcrow 	}
222237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
223b59db43aSTyler Hicks 	rc = ecryptfs_new_file_context(ecryptfs_inode);
224237fead6SMichael Halcrow 	if (rc) {
225d7cdc5feSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error creating new file "
226d7cdc5feSMichael Halcrow 				"context; rc = [%d]\n", rc);
227d7cdc5feSMichael Halcrow 		goto out;
228237fead6SMichael Halcrow 	}
229b59db43aSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
230391b52f9SMichael Halcrow 	if (rc) {
231391b52f9SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
232332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2339e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2349e78d14aSDavid Howells 			ecryptfs_dentry, rc);
235391b52f9SMichael Halcrow 		goto out;
236391b52f9SMichael Halcrow 	}
237b59db43aSTyler Hicks 	rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
238332ab16fSTyler Hicks 	if (rc)
239d7cdc5feSMichael Halcrow 		printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
240b59db43aSTyler Hicks 	ecryptfs_put_lower_file(ecryptfs_inode);
241237fead6SMichael Halcrow out:
242237fead6SMichael Halcrow 	return rc;
243237fead6SMichael Halcrow }
244237fead6SMichael Halcrow 
245d17074acSLee Jones /*
246237fead6SMichael Halcrow  * ecryptfs_create
247237fead6SMichael Halcrow  * @mode: The mode of the new file.
248237fead6SMichael Halcrow  *
249237fead6SMichael Halcrow  * Creates a new file.
250237fead6SMichael Halcrow  *
251237fead6SMichael Halcrow  * Returns zero on success; non-zero on error condition
252237fead6SMichael Halcrow  */
253237fead6SMichael Halcrow static int
254549c7297SChristian Brauner ecryptfs_create(struct user_namespace *mnt_userns,
255549c7297SChristian Brauner 		struct inode *directory_inode, struct dentry *ecryptfs_dentry,
256ebfc3b49SAl Viro 		umode_t mode, bool excl)
257237fead6SMichael Halcrow {
258b59db43aSTyler Hicks 	struct inode *ecryptfs_inode;
259237fead6SMichael Halcrow 	int rc;
260237fead6SMichael Halcrow 
261b59db43aSTyler Hicks 	ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
262b59db43aSTyler Hicks 					    mode);
263a1c83681SViresh Kumar 	if (IS_ERR(ecryptfs_inode)) {
264237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Failed to create file in"
265237fead6SMichael Halcrow 				"lower filesystem\n");
266b59db43aSTyler Hicks 		rc = PTR_ERR(ecryptfs_inode);
267237fead6SMichael Halcrow 		goto out;
268237fead6SMichael Halcrow 	}
269237fead6SMichael Halcrow 	/* At this point, a file exists on "disk"; we need to make sure
270237fead6SMichael Halcrow 	 * that this on disk file is prepared to be an ecryptfs file */
271b59db43aSTyler Hicks 	rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
272b59db43aSTyler Hicks 	if (rc) {
2738bc2d3cfSTyler Hicks 		ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
2748bc2d3cfSTyler Hicks 				   ecryptfs_inode);
2750e81ba23SAl Viro 		iget_failed(ecryptfs_inode);
276b59db43aSTyler Hicks 		goto out;
277b59db43aSTyler Hicks 	}
2781e2e547aSAl Viro 	d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
279237fead6SMichael Halcrow out:
280237fead6SMichael Halcrow 	return rc;
281237fead6SMichael Halcrow }
282237fead6SMichael Halcrow 
283778aeb42STyler Hicks static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
284237fead6SMichael Halcrow {
2852aac0cf8STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
286778aeb42STyler Hicks 	int rc;
287237fead6SMichael Halcrow 
288778aeb42STyler Hicks 	rc = ecryptfs_get_lower_file(dentry, inode);
289391b52f9SMichael Halcrow 	if (rc) {
290391b52f9SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
291332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2929e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2939e78d14aSDavid Howells 			dentry, rc);
294778aeb42STyler Hicks 		return rc;
295391b52f9SMichael Halcrow 	}
296778aeb42STyler Hicks 
2973b06b3ebSTyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
2982aac0cf8STyler Hicks 	/* TODO: lock for crypt_stat comparison */
2992aac0cf8STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
3002aac0cf8STyler Hicks 		ecryptfs_set_default_sizes(crypt_stat);
301778aeb42STyler Hicks 
302778aeb42STyler Hicks 	rc = ecryptfs_read_and_validate_header_region(inode);
303778aeb42STyler Hicks 	ecryptfs_put_lower_file(inode);
304237fead6SMichael Halcrow 	if (rc) {
305778aeb42STyler Hicks 		rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
306778aeb42STyler Hicks 		if (!rc)
307dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
308dd2a3b7aSMichael Halcrow 	}
309778aeb42STyler Hicks 
310778aeb42STyler Hicks 	/* Must return 0 to allow non-eCryptfs files to be looked up, too */
311778aeb42STyler Hicks 	return 0;
312778aeb42STyler Hicks }
313778aeb42STyler Hicks 
314d17074acSLee Jones /*
315778aeb42STyler Hicks  * ecryptfs_lookup_interpose - Dentry interposition for a lookup
316778aeb42STyler Hicks  */
317b1168a92SAl Viro static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
318b1168a92SAl Viro 				     struct dentry *lower_dentry)
319778aeb42STyler Hicks {
320*88569546SAl Viro 	const struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
321e72b9dd6SAl Viro 	struct inode *inode, *lower_inode;
322778aeb42STyler Hicks 	struct ecryptfs_dentry_info *dentry_info;
323778aeb42STyler Hicks 	int rc = 0;
324778aeb42STyler Hicks 
325778aeb42STyler Hicks 	dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
326778aeb42STyler Hicks 	if (!dentry_info) {
327237fead6SMichael Halcrow 		dput(lower_dentry);
328b1168a92SAl Viro 		return ERR_PTR(-ENOMEM);
329778aeb42STyler Hicks 	}
3300b1d9011SAl Viro 
331b1168a92SAl Viro 	fsstack_copy_attr_atime(d_inode(dentry->d_parent),
332762c6968SAl Viro 				d_inode(path->dentry));
33384d08fa8SAl Viro 	BUG_ON(!d_count(lower_dentry));
3340b1d9011SAl Viro 
3350b1d9011SAl Viro 	ecryptfs_set_dentry_private(dentry, dentry_info);
336762c6968SAl Viro 	dentry_info->lower_path.mnt = mntget(path->mnt);
33792dd1230SAl Viro 	dentry_info->lower_path.dentry = lower_dentry;
338778aeb42STyler Hicks 
339e72b9dd6SAl Viro 	/*
340e72b9dd6SAl Viro 	 * negative dentry can go positive under us here - its parent is not
341e72b9dd6SAl Viro 	 * locked.  That's OK and that could happen just as we return from
342e72b9dd6SAl Viro 	 * ecryptfs_lookup() anyway.  Just need to be careful and fetch
343e72b9dd6SAl Viro 	 * ->d_inode only once - it's not stable here.
344e72b9dd6SAl Viro 	 */
345e72b9dd6SAl Viro 	lower_inode = READ_ONCE(lower_dentry->d_inode);
346e72b9dd6SAl Viro 
347e72b9dd6SAl Viro 	if (!lower_inode) {
348778aeb42STyler Hicks 		/* We want to add because we couldn't find in lower */
349778aeb42STyler Hicks 		d_add(dentry, NULL);
350b1168a92SAl Viro 		return NULL;
351778aeb42STyler Hicks 	}
352b1168a92SAl Viro 	inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
353778aeb42STyler Hicks 	if (IS_ERR(inode)) {
354778aeb42STyler Hicks 		printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
355778aeb42STyler Hicks 		       __func__, PTR_ERR(inode));
356b1168a92SAl Viro 		return ERR_CAST(inode);
357778aeb42STyler Hicks 	}
358778aeb42STyler Hicks 	if (S_ISREG(inode->i_mode)) {
359778aeb42STyler Hicks 		rc = ecryptfs_i_size_read(dentry, inode);
360778aeb42STyler Hicks 		if (rc) {
361778aeb42STyler Hicks 			make_bad_inode(inode);
362b1168a92SAl Viro 			return ERR_PTR(rc);
363778aeb42STyler Hicks 		}
364778aeb42STyler Hicks 	}
365778aeb42STyler Hicks 
366778aeb42STyler Hicks 	if (inode->i_state & I_NEW)
367778aeb42STyler Hicks 		unlock_new_inode(inode);
368b1168a92SAl Viro 	return d_splice_alias(inode, dentry);
369addd65adSMichael Halcrow }
370addd65adSMichael Halcrow 
371addd65adSMichael Halcrow /**
372addd65adSMichael Halcrow  * ecryptfs_lookup
373addd65adSMichael Halcrow  * @ecryptfs_dir_inode: The eCryptfs directory inode
374addd65adSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
37589076bc3SAl Viro  * @flags: lookup flags
376addd65adSMichael Halcrow  *
377addd65adSMichael Halcrow  * Find a file on disk. If the file does not exist, then we'll add it to the
378addd65adSMichael Halcrow  * dentry cache and continue on to read it from the disk.
379addd65adSMichael Halcrow  */
380addd65adSMichael Halcrow static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
381addd65adSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
38200cd8dd3SAl Viro 				      unsigned int flags)
383addd65adSMichael Halcrow {
384addd65adSMichael Halcrow 	char *encrypted_and_encoded_name = NULL;
38588ae4ab9SAl Viro 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
386addd65adSMichael Halcrow 	struct dentry *lower_dir_dentry, *lower_dentry;
38788ae4ab9SAl Viro 	const char *name = ecryptfs_dentry->d_name.name;
38888ae4ab9SAl Viro 	size_t len = ecryptfs_dentry->d_name.len;
389b1168a92SAl Viro 	struct dentry *res;
390addd65adSMichael Halcrow 	int rc = 0;
391addd65adSMichael Halcrow 
392addd65adSMichael Halcrow 	lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
39388ae4ab9SAl Viro 
394addd65adSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
395addd65adSMichael Halcrow 				ecryptfs_dentry->d_sb)->mount_crypt_stat;
396ab13a921SGuenter Roeck 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
397addd65adSMichael Halcrow 		rc = ecryptfs_encrypt_and_encode_filename(
39888ae4ab9SAl Viro 			&encrypted_and_encoded_name, &len,
39988ae4ab9SAl Viro 			mount_crypt_stat, name, len);
400addd65adSMichael Halcrow 		if (rc) {
401addd65adSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encrypt and encode "
402addd65adSMichael Halcrow 			       "filename; rc = [%d]\n", __func__, rc);
403237fead6SMichael Halcrow 			return ERR_PTR(rc);
404237fead6SMichael Halcrow 		}
40588ae4ab9SAl Viro 		name = encrypted_and_encoded_name;
40688ae4ab9SAl Viro 	}
40788ae4ab9SAl Viro 
40888ae4ab9SAl Viro 	lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
409237fead6SMichael Halcrow 	if (IS_ERR(lower_dentry)) {
410237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
411b1168a92SAl Viro 				"[%ld] on lower_dentry = [%s]\n", __func__,
412b1168a92SAl Viro 				PTR_ERR(lower_dentry),
41388ae4ab9SAl Viro 				name);
414b1168a92SAl Viro 		res = ERR_CAST(lower_dentry);
41588ae4ab9SAl Viro 	} else {
416b1168a92SAl Viro 		res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
41788ae4ab9SAl Viro 	}
418237fead6SMichael Halcrow 	kfree(encrypted_and_encoded_name);
419b1168a92SAl Viro 	return res;
420237fead6SMichael Halcrow }
421237fead6SMichael Halcrow 
422237fead6SMichael Halcrow static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
423237fead6SMichael Halcrow 			 struct dentry *new_dentry)
424237fead6SMichael Halcrow {
425237fead6SMichael Halcrow 	struct dentry *lower_old_dentry;
426237fead6SMichael Halcrow 	struct dentry *lower_new_dentry;
427b2648d51SAl Viro 	struct inode *lower_dir;
428237fead6SMichael Halcrow 	u64 file_size_save;
429237fead6SMichael Halcrow 	int rc;
430237fead6SMichael Halcrow 
4312b0143b5SDavid Howells 	file_size_save = i_size_read(d_inode(old_dentry));
432237fead6SMichael Halcrow 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
433b2648d51SAl Viro 	rc = lock_parent(new_dentry, &lower_new_dentry, &lower_dir);
434b2648d51SAl Viro 	if (!rc)
435b2648d51SAl Viro 		rc = vfs_link(lower_old_dentry, &init_user_ns, lower_dir,
436b2648d51SAl Viro 			      lower_new_dentry, NULL);
4372b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_new_dentry))
438237fead6SMichael Halcrow 		goto out_lock;
4395ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
440237fead6SMichael Halcrow 	if (rc)
441237fead6SMichael Halcrow 		goto out_lock;
442b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
443b2648d51SAl Viro 	fsstack_copy_inode_size(dir, lower_dir);
4442b0143b5SDavid Howells 	set_nlink(d_inode(old_dentry),
4452b0143b5SDavid Howells 		  ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
4462b0143b5SDavid Howells 	i_size_write(d_inode(new_dentry), file_size_save);
447237fead6SMichael Halcrow out_lock:
448b2648d51SAl Viro 	inode_unlock(lower_dir);
449237fead6SMichael Halcrow 	return rc;
450237fead6SMichael Halcrow }
451237fead6SMichael Halcrow 
452237fead6SMichael Halcrow static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
453237fead6SMichael Halcrow {
4542b0143b5SDavid Howells 	return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
455237fead6SMichael Halcrow }
456237fead6SMichael Halcrow 
457549c7297SChristian Brauner static int ecryptfs_symlink(struct user_namespace *mnt_userns,
458549c7297SChristian Brauner 			    struct inode *dir, struct dentry *dentry,
459237fead6SMichael Halcrow 			    const char *symname)
460237fead6SMichael Halcrow {
461237fead6SMichael Halcrow 	int rc;
462237fead6SMichael Halcrow 	struct dentry *lower_dentry;
463b2648d51SAl Viro 	struct inode *lower_dir;
464237fead6SMichael Halcrow 	char *encoded_symname;
465addd65adSMichael Halcrow 	size_t encoded_symlen;
466addd65adSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
467237fead6SMichael Halcrow 
468b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
469b2648d51SAl Viro 	if (rc)
470b2648d51SAl Viro 		goto out_lock;
471addd65adSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
472addd65adSMichael Halcrow 		dir->i_sb)->mount_crypt_stat;
473addd65adSMichael Halcrow 	rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
474addd65adSMichael Halcrow 						  &encoded_symlen,
475addd65adSMichael Halcrow 						  mount_crypt_stat, symname,
476addd65adSMichael Halcrow 						  strlen(symname));
477addd65adSMichael Halcrow 	if (rc)
478237fead6SMichael Halcrow 		goto out_lock;
479b2648d51SAl Viro 	rc = vfs_symlink(&init_user_ns, lower_dir, lower_dentry,
480db2e747bSMiklos Szeredi 			 encoded_symname);
481237fead6SMichael Halcrow 	kfree(encoded_symname);
4822b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
483237fead6SMichael Halcrow 		goto out_lock;
4845ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
485237fead6SMichael Halcrow 	if (rc)
486237fead6SMichael Halcrow 		goto out_lock;
487b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
488b2648d51SAl Viro 	fsstack_copy_inode_size(dir, lower_dir);
489237fead6SMichael Halcrow out_lock:
490b2648d51SAl Viro 	inode_unlock(lower_dir);
4912b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
492237fead6SMichael Halcrow 		d_drop(dentry);
493237fead6SMichael Halcrow 	return rc;
494237fead6SMichael Halcrow }
495237fead6SMichael Halcrow 
496549c7297SChristian Brauner static int ecryptfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
497549c7297SChristian Brauner 			  struct dentry *dentry, umode_t mode)
498237fead6SMichael Halcrow {
499237fead6SMichael Halcrow 	int rc;
500237fead6SMichael Halcrow 	struct dentry *lower_dentry;
501b2648d51SAl Viro 	struct inode *lower_dir;
502237fead6SMichael Halcrow 
503b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
504b2648d51SAl Viro 	if (!rc)
505b2648d51SAl Viro 		rc = vfs_mkdir(&init_user_ns, lower_dir,
506b2648d51SAl Viro 			       lower_dentry, mode);
5072b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
508237fead6SMichael Halcrow 		goto out;
5095ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
510237fead6SMichael Halcrow 	if (rc)
511237fead6SMichael Halcrow 		goto out;
512b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
513b2648d51SAl Viro 	fsstack_copy_inode_size(dir, lower_dir);
514b2648d51SAl Viro 	set_nlink(dir, lower_dir->i_nlink);
515237fead6SMichael Halcrow out:
516b2648d51SAl Viro 	inode_unlock(lower_dir);
5172b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
518237fead6SMichael Halcrow 		d_drop(dentry);
519237fead6SMichael Halcrow 	return rc;
520237fead6SMichael Halcrow }
521237fead6SMichael Halcrow 
522237fead6SMichael Halcrow static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
523237fead6SMichael Halcrow {
524237fead6SMichael Halcrow 	struct dentry *lower_dentry;
525b2648d51SAl Viro 	struct inode *lower_dir;
52645ec4abaSMichael Halcrow 	int rc;
527237fead6SMichael Halcrow 
528b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
529bcf0d9d4SAl Viro 	dget(lower_dentry);	// don't even try to make the lower negative
530b2648d51SAl Viro 	if (!rc) {
531b2648d51SAl Viro 		if (d_unhashed(lower_dentry))
532bcf0d9d4SAl Viro 			rc = -EINVAL;
533bcf0d9d4SAl Viro 		else
534b2648d51SAl Viro 			rc = vfs_rmdir(&init_user_ns, lower_dir, lower_dentry);
535b2648d51SAl Viro 	}
536bcf0d9d4SAl Viro 	if (!rc) {
5372b0143b5SDavid Howells 		clear_nlink(d_inode(dentry));
538b2648d51SAl Viro 		fsstack_copy_attr_times(dir, lower_dir);
539b2648d51SAl Viro 		set_nlink(dir, lower_dir->i_nlink);
540bcf0d9d4SAl Viro 	}
541bcf0d9d4SAl Viro 	dput(lower_dentry);
542b2648d51SAl Viro 	inode_unlock(lower_dir);
543237fead6SMichael Halcrow 	if (!rc)
544237fead6SMichael Halcrow 		d_drop(dentry);
545237fead6SMichael Halcrow 	return rc;
546237fead6SMichael Halcrow }
547237fead6SMichael Halcrow 
548237fead6SMichael Halcrow static int
549549c7297SChristian Brauner ecryptfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
550549c7297SChristian Brauner 	       struct dentry *dentry, umode_t mode, dev_t dev)
551237fead6SMichael Halcrow {
552237fead6SMichael Halcrow 	int rc;
553237fead6SMichael Halcrow 	struct dentry *lower_dentry;
554b2648d51SAl Viro 	struct inode *lower_dir;
555237fead6SMichael Halcrow 
556b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
557b2648d51SAl Viro 	if (!rc)
558b2648d51SAl Viro 		rc = vfs_mknod(&init_user_ns, lower_dir,
559b2648d51SAl Viro 			       lower_dentry, mode, dev);
5602b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
561237fead6SMichael Halcrow 		goto out;
5625ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
563237fead6SMichael Halcrow 	if (rc)
564237fead6SMichael Halcrow 		goto out;
565b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
566b2648d51SAl Viro 	fsstack_copy_inode_size(dir, lower_dir);
567237fead6SMichael Halcrow out:
568b2648d51SAl Viro 	inode_unlock(lower_dir);
5692b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
570237fead6SMichael Halcrow 		d_drop(dentry);
571237fead6SMichael Halcrow 	return rc;
572237fead6SMichael Halcrow }
573237fead6SMichael Halcrow 
574237fead6SMichael Halcrow static int
575549c7297SChristian Brauner ecryptfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
576549c7297SChristian Brauner 		struct dentry *old_dentry, struct inode *new_dir,
577549c7297SChristian Brauner 		struct dentry *new_dentry, unsigned int flags)
578237fead6SMichael Halcrow {
579237fead6SMichael Halcrow 	int rc;
580237fead6SMichael Halcrow 	struct dentry *lower_old_dentry;
581237fead6SMichael Halcrow 	struct dentry *lower_new_dentry;
582237fead6SMichael Halcrow 	struct dentry *lower_old_dir_dentry;
583237fead6SMichael Halcrow 	struct dentry *lower_new_dir_dentry;
584bcf0d9d4SAl Viro 	struct dentry *trap;
5858335eafcSTyler Hicks 	struct inode *target_inode;
5869fe61450SChristian Brauner 	struct renamedata rd = {};
587237fead6SMichael Halcrow 
5881cd66c93SMiklos Szeredi 	if (flags)
5891cd66c93SMiklos Szeredi 		return -EINVAL;
5901cd66c93SMiklos Szeredi 
591bcf0d9d4SAl Viro 	lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent);
592bcf0d9d4SAl Viro 	lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent);
593bcf0d9d4SAl Viro 
594237fead6SMichael Halcrow 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
595237fead6SMichael Halcrow 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
596bcf0d9d4SAl Viro 
5972b0143b5SDavid Howells 	target_inode = d_inode(new_dentry);
598bcf0d9d4SAl Viro 
5990d132f73SErez Zadok 	trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
600bcf0d9d4SAl Viro 	dget(lower_new_dentry);
6010d132f73SErez Zadok 	rc = -EINVAL;
60274dd7c97SAl Viro 	if (lower_old_dentry->d_parent != lower_old_dir_dentry)
6030d132f73SErez Zadok 		goto out_lock;
60474dd7c97SAl Viro 	if (lower_new_dentry->d_parent != lower_new_dir_dentry)
60574dd7c97SAl Viro 		goto out_lock;
60674dd7c97SAl Viro 	if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry))
60774dd7c97SAl Viro 		goto out_lock;
60874dd7c97SAl Viro 	/* source should not be ancestor of target */
60974dd7c97SAl Viro 	if (trap == lower_old_dentry)
61074dd7c97SAl Viro 		goto out_lock;
6110d132f73SErez Zadok 	/* target should not be ancestor of source */
6120d132f73SErez Zadok 	if (trap == lower_new_dentry) {
6130d132f73SErez Zadok 		rc = -ENOTEMPTY;
6140d132f73SErez Zadok 		goto out_lock;
6150d132f73SErez Zadok 	}
6169fe61450SChristian Brauner 
6176521f891SChristian Brauner 	rd.old_mnt_userns	= &init_user_ns;
6189fe61450SChristian Brauner 	rd.old_dir		= d_inode(lower_old_dir_dentry);
6199fe61450SChristian Brauner 	rd.old_dentry		= lower_old_dentry;
6206521f891SChristian Brauner 	rd.new_mnt_userns	= &init_user_ns;
6219fe61450SChristian Brauner 	rd.new_dir		= d_inode(lower_new_dir_dentry);
6229fe61450SChristian Brauner 	rd.new_dentry		= lower_new_dentry;
6239fe61450SChristian Brauner 	rc = vfs_rename(&rd);
624237fead6SMichael Halcrow 	if (rc)
625237fead6SMichael Halcrow 		goto out_lock;
6268335eafcSTyler Hicks 	if (target_inode)
6278335eafcSTyler Hicks 		fsstack_copy_attr_all(target_inode,
6288335eafcSTyler Hicks 				      ecryptfs_inode_to_lower(target_inode));
6292b0143b5SDavid Howells 	fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
630237fead6SMichael Halcrow 	if (new_dir != old_dir)
6312b0143b5SDavid Howells 		fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
632237fead6SMichael Halcrow out_lock:
633237fead6SMichael Halcrow 	dput(lower_new_dentry);
634bcf0d9d4SAl Viro 	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
635237fead6SMichael Halcrow 	return rc;
636237fead6SMichael Halcrow }
637237fead6SMichael Halcrow 
638b22e8fedSAl Viro static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
639237fead6SMichael Halcrow {
6406c988f57SMiklos Szeredi 	DEFINE_DELAYED_CALL(done);
6413a60a168STyler Hicks 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
6426c988f57SMiklos Szeredi 	const char *link;
643b22e8fedSAl Viro 	char *buf;
644addd65adSMichael Halcrow 	int rc;
645237fead6SMichael Halcrow 
6466c988f57SMiklos Szeredi 	link = vfs_get_link(lower_dentry, &done);
6476c988f57SMiklos Szeredi 	if (IS_ERR(link))
6486c988f57SMiklos Szeredi 		return ERR_CAST(link);
6496c988f57SMiklos Szeredi 
650b22e8fedSAl Viro 	rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
6516c988f57SMiklos Szeredi 						  link, strlen(link));
6526c988f57SMiklos Szeredi 	do_delayed_call(&done);
6536c988f57SMiklos Szeredi 	if (rc)
6546c988f57SMiklos Szeredi 		return ERR_PTR(rc);
6556c988f57SMiklos Szeredi 
6566c988f57SMiklos Szeredi 	return buf;
6573a60a168STyler Hicks }
6583a60a168STyler Hicks 
6596b255391SAl Viro static const char *ecryptfs_get_link(struct dentry *dentry,
660fceef393SAl Viro 				     struct inode *inode,
661fceef393SAl Viro 				     struct delayed_call *done)
662237fead6SMichael Halcrow {
663b22e8fedSAl Viro 	size_t len;
6646b255391SAl Viro 	char *buf;
6656b255391SAl Viro 
6666b255391SAl Viro 	if (!dentry)
6676b255391SAl Viro 		return ERR_PTR(-ECHILD);
6686b255391SAl Viro 
6696b255391SAl Viro 	buf = ecryptfs_readlink_lower(dentry, &len);
670b22e8fedSAl Viro 	if (IS_ERR(buf))
671680baacbSAl Viro 		return buf;
6722b0143b5SDavid Howells 	fsstack_copy_attr_atime(d_inode(dentry),
6732b0143b5SDavid Howells 				d_inode(ecryptfs_dentry_to_lower(dentry)));
674408bd629SAl Viro 	buf[len] = '\0';
675fceef393SAl Viro 	set_delayed_call(done, kfree_link, buf);
676fceef393SAl Viro 	return buf;
677237fead6SMichael Halcrow }
678237fead6SMichael Halcrow 
679237fead6SMichael Halcrow /**
680237fead6SMichael Halcrow  * upper_size_to_lower_size
681237fead6SMichael Halcrow  * @crypt_stat: Crypt_stat associated with file
682237fead6SMichael Halcrow  * @upper_size: Size of the upper file
683237fead6SMichael Halcrow  *
684cc11beffSMichael Halcrow  * Calculate the required size of the lower file based on the
685237fead6SMichael Halcrow  * specified size of the upper file. This calculation is based on the
686237fead6SMichael Halcrow  * number of headers in the underlying file and the extent size.
687237fead6SMichael Halcrow  *
688237fead6SMichael Halcrow  * Returns Calculated size of the lower file.
689237fead6SMichael Halcrow  */
690237fead6SMichael Halcrow static loff_t
691237fead6SMichael Halcrow upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
692237fead6SMichael Halcrow 			 loff_t upper_size)
693237fead6SMichael Halcrow {
694237fead6SMichael Halcrow 	loff_t lower_size;
695237fead6SMichael Halcrow 
696157f1071STyler Hicks 	lower_size = ecryptfs_lower_header_size(crypt_stat);
697237fead6SMichael Halcrow 	if (upper_size != 0) {
698237fead6SMichael Halcrow 		loff_t num_extents;
699237fead6SMichael Halcrow 
700237fead6SMichael Halcrow 		num_extents = upper_size >> crypt_stat->extent_shift;
701237fead6SMichael Halcrow 		if (upper_size & ~crypt_stat->extent_mask)
702237fead6SMichael Halcrow 			num_extents++;
703237fead6SMichael Halcrow 		lower_size += (num_extents * crypt_stat->extent_size);
704237fead6SMichael Halcrow 	}
705237fead6SMichael Halcrow 	return lower_size;
706237fead6SMichael Halcrow }
707237fead6SMichael Halcrow 
708237fead6SMichael Halcrow /**
7095f3ef64fSTyler Hicks  * truncate_upper
710237fead6SMichael Halcrow  * @dentry: The ecryptfs layer dentry
7115f3ef64fSTyler Hicks  * @ia: Address of the ecryptfs inode's attributes
7125f3ef64fSTyler Hicks  * @lower_ia: Address of the lower inode's attributes
713237fead6SMichael Halcrow  *
714237fead6SMichael Halcrow  * Function to handle truncations modifying the size of the file. Note
715237fead6SMichael Halcrow  * that the file sizes are interpolated. When expanding, we are simply
7165f3ef64fSTyler Hicks  * writing strings of 0's out. When truncating, we truncate the upper
7175f3ef64fSTyler Hicks  * inode and update the lower_ia according to the page index
7185f3ef64fSTyler Hicks  * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
7195f3ef64fSTyler Hicks  * the caller must use lower_ia in a call to notify_change() to perform
7205f3ef64fSTyler Hicks  * the truncation of the lower inode.
721237fead6SMichael Halcrow  *
722237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
723237fead6SMichael Halcrow  */
7245f3ef64fSTyler Hicks static int truncate_upper(struct dentry *dentry, struct iattr *ia,
7255f3ef64fSTyler Hicks 			  struct iattr *lower_ia)
726237fead6SMichael Halcrow {
727237fead6SMichael Halcrow 	int rc = 0;
7282b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
729237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
730237fead6SMichael Halcrow 	loff_t i_size = i_size_read(inode);
731237fead6SMichael Halcrow 	loff_t lower_size_before_truncate;
732237fead6SMichael Halcrow 	loff_t lower_size_after_truncate;
733237fead6SMichael Halcrow 
7345f3ef64fSTyler Hicks 	if (unlikely((ia->ia_size == i_size))) {
7355f3ef64fSTyler Hicks 		lower_ia->ia_valid &= ~ATTR_SIZE;
736332ab16fSTyler Hicks 		return 0;
7375f3ef64fSTyler Hicks 	}
7383b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(dentry, inode);
739332ab16fSTyler Hicks 	if (rc)
740332ab16fSTyler Hicks 		return rc;
7412b0143b5SDavid Howells 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
742237fead6SMichael Halcrow 	/* Switch on growing or shrinking file */
7435f3ef64fSTyler Hicks 	if (ia->ia_size > i_size) {
7442ed92554SMichael Halcrow 		char zero[] = { 0x00 };
745240e2df5SMichael Halcrow 
7465f3ef64fSTyler Hicks 		lower_ia->ia_valid &= ~ATTR_SIZE;
7472ed92554SMichael Halcrow 		/* Write a single 0 at the last position of the file;
7482ed92554SMichael Halcrow 		 * this triggers code that will fill in 0's throughout
7492ed92554SMichael Halcrow 		 * the intermediate portion of the previous end of the
7502ed92554SMichael Halcrow 		 * file and the new and of the file */
75148c1e44aSAl Viro 		rc = ecryptfs_write(inode, zero,
7525f3ef64fSTyler Hicks 				    (ia->ia_size - 1), 1);
7535f3ef64fSTyler Hicks 	} else { /* ia->ia_size < i_size_read(inode) */
7545f3ef64fSTyler Hicks 		/* We're chopping off all the pages down to the page
7555f3ef64fSTyler Hicks 		 * in which ia->ia_size is located. Fill in the end of
756ea1754a0SKirill A. Shutemov 		 * that page from (ia->ia_size & ~PAGE_MASK) to
757ea1754a0SKirill A. Shutemov 		 * PAGE_SIZE with zeros. */
75809cbfeafSKirill A. Shutemov 		size_t num_zeros = (PAGE_SIZE
75909cbfeafSKirill A. Shutemov 				    - (ia->ia_size & ~PAGE_MASK));
7602ed92554SMichael Halcrow 
7612c27c65eSChristoph Hellwig 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
7622c27c65eSChristoph Hellwig 			truncate_setsize(inode, ia->ia_size);
7635f3ef64fSTyler Hicks 			lower_ia->ia_size = ia->ia_size;
7645f3ef64fSTyler Hicks 			lower_ia->ia_valid |= ATTR_SIZE;
76548c1e44aSAl Viro 			goto out;
76613a791b4STyler Hicks 		}
7672ed92554SMichael Halcrow 		if (num_zeros) {
7682ed92554SMichael Halcrow 			char *zeros_virt;
7692ed92554SMichael Halcrow 
7702ed92554SMichael Halcrow 			zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
7712ed92554SMichael Halcrow 			if (!zeros_virt) {
7722ed92554SMichael Halcrow 				rc = -ENOMEM;
77348c1e44aSAl Viro 				goto out;
774240e2df5SMichael Halcrow 			}
77548c1e44aSAl Viro 			rc = ecryptfs_write(inode, zeros_virt,
7765f3ef64fSTyler Hicks 					    ia->ia_size, num_zeros);
7772ed92554SMichael Halcrow 			kfree(zeros_virt);
7785dda6992SMichael Halcrow 			if (rc) {
779240e2df5SMichael Halcrow 				printk(KERN_ERR "Error attempting to zero out "
780240e2df5SMichael Halcrow 				       "the remainder of the end page on "
781240e2df5SMichael Halcrow 				       "reducing truncate; rc = [%d]\n", rc);
78248c1e44aSAl Viro 				goto out;
783240e2df5SMichael Halcrow 			}
784240e2df5SMichael Halcrow 		}
7852c27c65eSChristoph Hellwig 		truncate_setsize(inode, ia->ia_size);
7860216f7f7SMichael Halcrow 		rc = ecryptfs_write_inode_size_to_metadata(inode);
787dd2a3b7aSMichael Halcrow 		if (rc) {
788dd2a3b7aSMichael Halcrow 			printk(KERN_ERR	"Problem with "
789dd2a3b7aSMichael Halcrow 			       "ecryptfs_write_inode_size_to_metadata; "
790dd2a3b7aSMichael Halcrow 			       "rc = [%d]\n", rc);
79148c1e44aSAl Viro 			goto out;
792dd2a3b7aSMichael Halcrow 		}
793237fead6SMichael Halcrow 		/* We are reducing the size of the ecryptfs file, and need to
794237fead6SMichael Halcrow 		 * know if we need to reduce the size of the lower file. */
795237fead6SMichael Halcrow 		lower_size_before_truncate =
796237fead6SMichael Halcrow 		    upper_size_to_lower_size(crypt_stat, i_size);
797237fead6SMichael Halcrow 		lower_size_after_truncate =
7985f3ef64fSTyler Hicks 		    upper_size_to_lower_size(crypt_stat, ia->ia_size);
7995f3ef64fSTyler Hicks 		if (lower_size_after_truncate < lower_size_before_truncate) {
8005f3ef64fSTyler Hicks 			lower_ia->ia_size = lower_size_after_truncate;
8015f3ef64fSTyler Hicks 			lower_ia->ia_valid |= ATTR_SIZE;
8025f3ef64fSTyler Hicks 		} else
8035f3ef64fSTyler Hicks 			lower_ia->ia_valid &= ~ATTR_SIZE;
804237fead6SMichael Halcrow 	}
805237fead6SMichael Halcrow out:
806332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
807237fead6SMichael Halcrow 	return rc;
808237fead6SMichael Halcrow }
809237fead6SMichael Halcrow 
810a261a039STyler Hicks static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
811a261a039STyler Hicks {
812a261a039STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
813a261a039STyler Hicks 	loff_t lower_oldsize, lower_newsize;
814a261a039STyler Hicks 
815a261a039STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
816a261a039STyler Hicks 	lower_oldsize = upper_size_to_lower_size(crypt_stat,
817a261a039STyler Hicks 						 i_size_read(inode));
818a261a039STyler Hicks 	lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
819a261a039STyler Hicks 	if (lower_newsize > lower_oldsize) {
820a261a039STyler Hicks 		/*
821a261a039STyler Hicks 		 * The eCryptfs inode and the new *lower* size are mixed here
822a261a039STyler Hicks 		 * because we may not have the lower i_mutex held and/or it may
823a261a039STyler Hicks 		 * not be appropriate to call inode_newsize_ok() with inodes
824a261a039STyler Hicks 		 * from other filesystems.
825a261a039STyler Hicks 		 */
826a261a039STyler Hicks 		return inode_newsize_ok(inode, lower_newsize);
827a261a039STyler Hicks 	}
828a261a039STyler Hicks 
829a261a039STyler Hicks 	return 0;
830a261a039STyler Hicks }
831a261a039STyler Hicks 
8325f3ef64fSTyler Hicks /**
8335f3ef64fSTyler Hicks  * ecryptfs_truncate
8345f3ef64fSTyler Hicks  * @dentry: The ecryptfs layer dentry
8355f3ef64fSTyler Hicks  * @new_length: The length to expand the file to
8365f3ef64fSTyler Hicks  *
8375f3ef64fSTyler Hicks  * Simple function that handles the truncation of an eCryptfs inode and
8385f3ef64fSTyler Hicks  * its corresponding lower inode.
8395f3ef64fSTyler Hicks  *
8405f3ef64fSTyler Hicks  * Returns zero on success; non-zero otherwise
8415f3ef64fSTyler Hicks  */
8425f3ef64fSTyler Hicks int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
8435f3ef64fSTyler Hicks {
8445f3ef64fSTyler Hicks 	struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
8455f3ef64fSTyler Hicks 	struct iattr lower_ia = { .ia_valid = 0 };
8465f3ef64fSTyler Hicks 	int rc;
8475f3ef64fSTyler Hicks 
8482b0143b5SDavid Howells 	rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
849a261a039STyler Hicks 	if (rc)
850a261a039STyler Hicks 		return rc;
851a261a039STyler Hicks 
8525f3ef64fSTyler Hicks 	rc = truncate_upper(dentry, &ia, &lower_ia);
8535f3ef64fSTyler Hicks 	if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
8545f3ef64fSTyler Hicks 		struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
8555f3ef64fSTyler Hicks 
8565955102cSAl Viro 		inode_lock(d_inode(lower_dentry));
8572f221d6fSChristian Brauner 		rc = notify_change(&init_user_ns, lower_dentry,
8582f221d6fSChristian Brauner 				   &lower_ia, NULL);
8595955102cSAl Viro 		inode_unlock(d_inode(lower_dentry));
8605f3ef64fSTyler Hicks 	}
8615f3ef64fSTyler Hicks 	return rc;
8625f3ef64fSTyler Hicks }
8635f3ef64fSTyler Hicks 
864237fead6SMichael Halcrow static int
865549c7297SChristian Brauner ecryptfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
866549c7297SChristian Brauner 		    int mask)
867237fead6SMichael Halcrow {
86847291baaSChristian Brauner 	return inode_permission(&init_user_ns,
86947291baaSChristian Brauner 				ecryptfs_inode_to_lower(inode), mask);
870237fead6SMichael Halcrow }
871237fead6SMichael Halcrow 
872237fead6SMichael Halcrow /**
873237fead6SMichael Halcrow  * ecryptfs_setattr
874d17074acSLee Jones  * @mnt_userns: user namespace of the target mount
875237fead6SMichael Halcrow  * @dentry: dentry handle to the inode to modify
876237fead6SMichael Halcrow  * @ia: Structure with flags of what to change and values
877237fead6SMichael Halcrow  *
878237fead6SMichael Halcrow  * Updates the metadata of an inode. If the update is to the size
879237fead6SMichael Halcrow  * i.e. truncation, then ecryptfs_truncate will handle the size modification
880237fead6SMichael Halcrow  * of both the ecryptfs inode and the lower inode.
881237fead6SMichael Halcrow  *
882237fead6SMichael Halcrow  * All other metadata changes will be passed right to the lower filesystem,
883237fead6SMichael Halcrow  * and we will just update our inode to look like the lower.
884237fead6SMichael Halcrow  */
885549c7297SChristian Brauner static int ecryptfs_setattr(struct user_namespace *mnt_userns,
886549c7297SChristian Brauner 			    struct dentry *dentry, struct iattr *ia)
887237fead6SMichael Halcrow {
888237fead6SMichael Halcrow 	int rc = 0;
889237fead6SMichael Halcrow 	struct dentry *lower_dentry;
8905f3ef64fSTyler Hicks 	struct iattr lower_ia;
891237fead6SMichael Halcrow 	struct inode *inode;
892237fead6SMichael Halcrow 	struct inode *lower_inode;
893237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
894237fead6SMichael Halcrow 
8952b0143b5SDavid Howells 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
896e81f3340SHerbert Xu 	if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
897e81f3340SHerbert Xu 		rc = ecryptfs_init_crypt_stat(crypt_stat);
898e81f3340SHerbert Xu 		if (rc)
899e81f3340SHerbert Xu 			return rc;
900e81f3340SHerbert Xu 	}
9012b0143b5SDavid Howells 	inode = d_inode(dentry);
902237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
903e10f281bSMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
904e10f281bSMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
905e36cb0b8SDavid Howells 	if (d_is_dir(dentry))
906e10f281bSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
907e36cb0b8SDavid Howells 	else if (d_is_reg(dentry)
90864ee4808SMichael Halcrow 		 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
90964ee4808SMichael Halcrow 		     || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
910e10f281bSMichael Halcrow 		struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
911e10f281bSMichael Halcrow 
912e10f281bSMichael Halcrow 		mount_crypt_stat = &ecryptfs_superblock_to_private(
913e10f281bSMichael Halcrow 			dentry->d_sb)->mount_crypt_stat;
9143b06b3ebSTyler Hicks 		rc = ecryptfs_get_lower_file(dentry, inode);
915332ab16fSTyler Hicks 		if (rc) {
916332ab16fSTyler Hicks 			mutex_unlock(&crypt_stat->cs_mutex);
917332ab16fSTyler Hicks 			goto out;
918332ab16fSTyler Hicks 		}
919d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_metadata(dentry);
920332ab16fSTyler Hicks 		ecryptfs_put_lower_file(inode);
9215dda6992SMichael Halcrow 		if (rc) {
922e10f281bSMichael Halcrow 			if (!(mount_crypt_stat->flags
923e10f281bSMichael Halcrow 			      & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
924e10f281bSMichael Halcrow 				rc = -EIO;
92525bd8174SMichael Halcrow 				printk(KERN_WARNING "Either the lower file "
926e10f281bSMichael Halcrow 				       "is not in a valid eCryptfs format, "
92725bd8174SMichael Halcrow 				       "or the key could not be retrieved. "
92825bd8174SMichael Halcrow 				       "Plaintext passthrough mode is not "
929e10f281bSMichael Halcrow 				       "enabled; returning -EIO\n");
930e10f281bSMichael Halcrow 				mutex_unlock(&crypt_stat->cs_mutex);
931e10f281bSMichael Halcrow 				goto out;
932e10f281bSMichael Halcrow 			}
933e10f281bSMichael Halcrow 			rc = 0;
9343aeb86eaSTyler Hicks 			crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
9353aeb86eaSTyler Hicks 					       | ECRYPTFS_ENCRYPTED);
936e10f281bSMichael Halcrow 		}
937e10f281bSMichael Halcrow 	}
938e10f281bSMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
939a261a039STyler Hicks 
9402f221d6fSChristian Brauner 	rc = setattr_prepare(&init_user_ns, dentry, ia);
941a261a039STyler Hicks 	if (rc)
942a261a039STyler Hicks 		goto out;
943a261a039STyler Hicks 	if (ia->ia_valid & ATTR_SIZE) {
944a261a039STyler Hicks 		rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
945a261a039STyler Hicks 		if (rc)
946a261a039STyler Hicks 			goto out;
947a261a039STyler Hicks 	}
948a261a039STyler Hicks 
9495f3ef64fSTyler Hicks 	memcpy(&lower_ia, ia, sizeof(lower_ia));
9505f3ef64fSTyler Hicks 	if (ia->ia_valid & ATTR_FILE)
9515f3ef64fSTyler Hicks 		lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
952237fead6SMichael Halcrow 	if (ia->ia_valid & ATTR_SIZE) {
9535f3ef64fSTyler Hicks 		rc = truncate_upper(dentry, ia, &lower_ia);
954237fead6SMichael Halcrow 		if (rc < 0)
955237fead6SMichael Halcrow 			goto out;
956237fead6SMichael Halcrow 	}
9571ac564ecSJeff Layton 
9581ac564ecSJeff Layton 	/*
9591ac564ecSJeff Layton 	 * mode change is for clearing setuid/setgid bits. Allow lower fs
9601ac564ecSJeff Layton 	 * to interpret this in its own way.
9611ac564ecSJeff Layton 	 */
9625f3ef64fSTyler Hicks 	if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
9635f3ef64fSTyler Hicks 		lower_ia.ia_valid &= ~ATTR_MODE;
9641ac564ecSJeff Layton 
9655955102cSAl Viro 	inode_lock(d_inode(lower_dentry));
9662f221d6fSChristian Brauner 	rc = notify_change(&init_user_ns, lower_dentry, &lower_ia, NULL);
9675955102cSAl Viro 	inode_unlock(d_inode(lower_dentry));
968237fead6SMichael Halcrow out:
9699afa2fb6SErez Zadok 	fsstack_copy_attr_all(inode, lower_inode);
970237fead6SMichael Halcrow 	return rc;
971237fead6SMichael Halcrow }
972237fead6SMichael Halcrow 
973549c7297SChristian Brauner static int ecryptfs_getattr_link(struct user_namespace *mnt_userns,
974549c7297SChristian Brauner 				 const struct path *path, struct kstat *stat,
975a528d35eSDavid Howells 				 u32 request_mask, unsigned int flags)
9763a60a168STyler Hicks {
977a528d35eSDavid Howells 	struct dentry *dentry = path->dentry;
9783a60a168STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
9793a60a168STyler Hicks 	int rc = 0;
9803a60a168STyler Hicks 
9813a60a168STyler Hicks 	mount_crypt_stat = &ecryptfs_superblock_to_private(
9823a60a168STyler Hicks 						dentry->d_sb)->mount_crypt_stat;
9830d56a451SChristian Brauner 	generic_fillattr(&init_user_ns, d_inode(dentry), stat);
9843a60a168STyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
9853a60a168STyler Hicks 		char *target;
9863a60a168STyler Hicks 		size_t targetsiz;
9873a60a168STyler Hicks 
988b22e8fedSAl Viro 		target = ecryptfs_readlink_lower(dentry, &targetsiz);
989b22e8fedSAl Viro 		if (!IS_ERR(target)) {
9903a60a168STyler Hicks 			kfree(target);
9913a60a168STyler Hicks 			stat->size = targetsiz;
992b22e8fedSAl Viro 		} else {
993b22e8fedSAl Viro 			rc = PTR_ERR(target);
9943a60a168STyler Hicks 		}
9953a60a168STyler Hicks 	}
9963a60a168STyler Hicks 	return rc;
9973a60a168STyler Hicks }
9983a60a168STyler Hicks 
999549c7297SChristian Brauner static int ecryptfs_getattr(struct user_namespace *mnt_userns,
1000549c7297SChristian Brauner 			    const struct path *path, struct kstat *stat,
1001a528d35eSDavid Howells 			    u32 request_mask, unsigned int flags)
1002f8f484d1STyler Hicks {
1003a528d35eSDavid Howells 	struct dentry *dentry = path->dentry;
1004f8f484d1STyler Hicks 	struct kstat lower_stat;
1005f8f484d1STyler Hicks 	int rc;
1006f8f484d1STyler Hicks 
1007a528d35eSDavid Howells 	rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat,
1008a528d35eSDavid Howells 			 request_mask, flags);
1009f8f484d1STyler Hicks 	if (!rc) {
10102b0143b5SDavid Howells 		fsstack_copy_attr_all(d_inode(dentry),
10112b0143b5SDavid Howells 				      ecryptfs_inode_to_lower(d_inode(dentry)));
10120d56a451SChristian Brauner 		generic_fillattr(&init_user_ns, d_inode(dentry), stat);
1013f8f484d1STyler Hicks 		stat->blocks = lower_stat.blocks;
1014f8f484d1STyler Hicks 	}
1015f8f484d1STyler Hicks 	return rc;
1016f8f484d1STyler Hicks }
1017f8f484d1STyler Hicks 
1018dd2a3b7aSMichael Halcrow int
10193767e255SAl Viro ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
10203767e255SAl Viro 		  const char *name, const void *value,
1021237fead6SMichael Halcrow 		  size_t size, int flags)
1022237fead6SMichael Halcrow {
10235d6c3191SAndreas Gruenbacher 	int rc;
1024237fead6SMichael Halcrow 	struct dentry *lower_dentry;
10250b964446SMiklos Szeredi 	struct inode *lower_inode;
1026237fead6SMichael Halcrow 
1027237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
10280b964446SMiklos Szeredi 	lower_inode = d_inode(lower_dentry);
10290b964446SMiklos Szeredi 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1030cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1031237fead6SMichael Halcrow 		goto out;
1032237fead6SMichael Halcrow 	}
10330b964446SMiklos Szeredi 	inode_lock(lower_inode);
10347d6beb71SLinus Torvalds 	rc = __vfs_setxattr_locked(&init_user_ns, lower_dentry, name, value, size, flags, NULL);
10350b964446SMiklos Szeredi 	inode_unlock(lower_inode);
10363767e255SAl Viro 	if (!rc && inode)
10370b964446SMiklos Szeredi 		fsstack_copy_attr_all(inode, lower_inode);
1038237fead6SMichael Halcrow out:
1039237fead6SMichael Halcrow 	return rc;
1040237fead6SMichael Halcrow }
1041237fead6SMichael Halcrow 
1042dd2a3b7aSMichael Halcrow ssize_t
1043ce23e640SAl Viro ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
1044ce23e640SAl Viro 			const char *name, void *value, size_t size)
1045d7cdc5feSMichael Halcrow {
10465d6c3191SAndreas Gruenbacher 	int rc;
1047d7cdc5feSMichael Halcrow 
10485d6c3191SAndreas Gruenbacher 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1049cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1050d7cdc5feSMichael Halcrow 		goto out;
1051d7cdc5feSMichael Halcrow 	}
1052ce23e640SAl Viro 	inode_lock(lower_inode);
10535d6c3191SAndreas Gruenbacher 	rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
1054ce23e640SAl Viro 	inode_unlock(lower_inode);
1055d7cdc5feSMichael Halcrow out:
1056d7cdc5feSMichael Halcrow 	return rc;
1057d7cdc5feSMichael Halcrow }
1058d7cdc5feSMichael Halcrow 
10597896b631SAdrian Bunk static ssize_t
1060ce23e640SAl Viro ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
1061ce23e640SAl Viro 		  const char *name, void *value, size_t size)
1062237fead6SMichael Halcrow {
1063ce23e640SAl Viro 	return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1064ce23e640SAl Viro 				       ecryptfs_inode_to_lower(inode),
1065ce23e640SAl Viro 				       name, value, size);
1066237fead6SMichael Halcrow }
1067237fead6SMichael Halcrow 
1068237fead6SMichael Halcrow static ssize_t
1069237fead6SMichael Halcrow ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1070237fead6SMichael Halcrow {
1071237fead6SMichael Halcrow 	int rc = 0;
1072237fead6SMichael Halcrow 	struct dentry *lower_dentry;
1073237fead6SMichael Halcrow 
1074237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
10752b0143b5SDavid Howells 	if (!d_inode(lower_dentry)->i_op->listxattr) {
1076cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1077237fead6SMichael Halcrow 		goto out;
1078237fead6SMichael Halcrow 	}
10795955102cSAl Viro 	inode_lock(d_inode(lower_dentry));
10802b0143b5SDavid Howells 	rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
10815955102cSAl Viro 	inode_unlock(d_inode(lower_dentry));
1082237fead6SMichael Halcrow out:
1083237fead6SMichael Halcrow 	return rc;
1084237fead6SMichael Halcrow }
1085237fead6SMichael Halcrow 
10864b899da5SAndreas Gruenbacher static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
10874b899da5SAndreas Gruenbacher 				const char *name)
1088237fead6SMichael Halcrow {
10895d6c3191SAndreas Gruenbacher 	int rc;
1090237fead6SMichael Halcrow 	struct dentry *lower_dentry;
10914b899da5SAndreas Gruenbacher 	struct inode *lower_inode;
1092237fead6SMichael Halcrow 
1093237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
10944b899da5SAndreas Gruenbacher 	lower_inode = ecryptfs_inode_to_lower(inode);
10955d6c3191SAndreas Gruenbacher 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1096cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1097237fead6SMichael Halcrow 		goto out;
1098237fead6SMichael Halcrow 	}
10994b899da5SAndreas Gruenbacher 	inode_lock(lower_inode);
1100c7c7a1a1STycho Andersen 	rc = __vfs_removexattr(&init_user_ns, lower_dentry, name);
11014b899da5SAndreas Gruenbacher 	inode_unlock(lower_inode);
1102237fead6SMichael Halcrow out:
1103237fead6SMichael Halcrow 	return rc;
1104237fead6SMichael Halcrow }
1105237fead6SMichael Halcrow 
110697e2dee9SMiklos Szeredi static int ecryptfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
110797e2dee9SMiklos Szeredi {
110897e2dee9SMiklos Szeredi 	return vfs_fileattr_get(ecryptfs_dentry_to_lower(dentry), fa);
110997e2dee9SMiklos Szeredi }
111097e2dee9SMiklos Szeredi 
111197e2dee9SMiklos Szeredi static int ecryptfs_fileattr_set(struct user_namespace *mnt_userns,
111297e2dee9SMiklos Szeredi 				 struct dentry *dentry, struct fileattr *fa)
111397e2dee9SMiklos Szeredi {
111497e2dee9SMiklos Szeredi 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
111597e2dee9SMiklos Szeredi 	int rc;
111697e2dee9SMiklos Szeredi 
111797e2dee9SMiklos Szeredi 	rc = vfs_fileattr_set(&init_user_ns, lower_dentry, fa);
111897e2dee9SMiklos Szeredi 	fsstack_copy_attr_all(d_inode(dentry), d_inode(lower_dentry));
111997e2dee9SMiklos Szeredi 
112097e2dee9SMiklos Szeredi 	return rc;
112197e2dee9SMiklos Szeredi }
112297e2dee9SMiklos Szeredi 
1123754661f1SArjan van de Ven const struct inode_operations ecryptfs_symlink_iops = {
11246b255391SAl Viro 	.get_link = ecryptfs_get_link,
1125237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1126237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
11273a60a168STyler Hicks 	.getattr = ecryptfs_getattr_link,
1128237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
1129237fead6SMichael Halcrow };
1130237fead6SMichael Halcrow 
1131754661f1SArjan van de Ven const struct inode_operations ecryptfs_dir_iops = {
1132237fead6SMichael Halcrow 	.create = ecryptfs_create,
1133237fead6SMichael Halcrow 	.lookup = ecryptfs_lookup,
1134237fead6SMichael Halcrow 	.link = ecryptfs_link,
1135237fead6SMichael Halcrow 	.unlink = ecryptfs_unlink,
1136237fead6SMichael Halcrow 	.symlink = ecryptfs_symlink,
1137237fead6SMichael Halcrow 	.mkdir = ecryptfs_mkdir,
1138237fead6SMichael Halcrow 	.rmdir = ecryptfs_rmdir,
1139237fead6SMichael Halcrow 	.mknod = ecryptfs_mknod,
1140237fead6SMichael Halcrow 	.rename = ecryptfs_rename,
1141237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1142237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
1143237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
114497e2dee9SMiklos Szeredi 	.fileattr_get = ecryptfs_fileattr_get,
114597e2dee9SMiklos Szeredi 	.fileattr_set = ecryptfs_fileattr_set,
1146237fead6SMichael Halcrow };
1147237fead6SMichael Halcrow 
1148754661f1SArjan van de Ven const struct inode_operations ecryptfs_main_iops = {
1149237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1150237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
1151f8f484d1STyler Hicks 	.getattr = ecryptfs_getattr,
1152237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
115397e2dee9SMiklos Szeredi 	.fileattr_get = ecryptfs_fileattr_get,
115497e2dee9SMiklos Szeredi 	.fileattr_set = ecryptfs_fileattr_set,
11554b899da5SAndreas Gruenbacher };
11564b899da5SAndreas Gruenbacher 
11574b899da5SAndreas Gruenbacher static int ecryptfs_xattr_get(const struct xattr_handler *handler,
11584b899da5SAndreas Gruenbacher 			      struct dentry *dentry, struct inode *inode,
11594b899da5SAndreas Gruenbacher 			      const char *name, void *buffer, size_t size)
11604b899da5SAndreas Gruenbacher {
11614b899da5SAndreas Gruenbacher 	return ecryptfs_getxattr(dentry, inode, name, buffer, size);
11624b899da5SAndreas Gruenbacher }
11634b899da5SAndreas Gruenbacher 
11644b899da5SAndreas Gruenbacher static int ecryptfs_xattr_set(const struct xattr_handler *handler,
1165e65ce2a5SChristian Brauner 			      struct user_namespace *mnt_userns,
11664b899da5SAndreas Gruenbacher 			      struct dentry *dentry, struct inode *inode,
11674b899da5SAndreas Gruenbacher 			      const char *name, const void *value, size_t size,
11684b899da5SAndreas Gruenbacher 			      int flags)
11694b899da5SAndreas Gruenbacher {
11704b899da5SAndreas Gruenbacher 	if (value)
11714b899da5SAndreas Gruenbacher 		return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
11724b899da5SAndreas Gruenbacher 	else {
11734b899da5SAndreas Gruenbacher 		BUG_ON(flags != XATTR_REPLACE);
11744b899da5SAndreas Gruenbacher 		return ecryptfs_removexattr(dentry, inode, name);
11754b899da5SAndreas Gruenbacher 	}
11764b899da5SAndreas Gruenbacher }
11774b899da5SAndreas Gruenbacher 
1178c036061bSYueHaibing static const struct xattr_handler ecryptfs_xattr_handler = {
11794b899da5SAndreas Gruenbacher 	.prefix = "",  /* match anything */
11804b899da5SAndreas Gruenbacher 	.get = ecryptfs_xattr_get,
11814b899da5SAndreas Gruenbacher 	.set = ecryptfs_xattr_set,
11824b899da5SAndreas Gruenbacher };
11834b899da5SAndreas Gruenbacher 
11844b899da5SAndreas Gruenbacher const struct xattr_handler *ecryptfs_xattr_handlers[] = {
11854b899da5SAndreas Gruenbacher 	&ecryptfs_xattr_handler,
11864b899da5SAndreas Gruenbacher 	NULL
1187237fead6SMichael Halcrow };
1188