xref: /openbmc/linux/fs/ecryptfs/inode.c (revision d17074ac)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2*d17074acSLee 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>
210a688ad7SHarvey Harrison #include <asm/unaligned.h>
22237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
23237fead6SMichael Halcrow 
24237fead6SMichael Halcrow static struct dentry *lock_parent(struct dentry *dentry)
25237fead6SMichael Halcrow {
26237fead6SMichael Halcrow 	struct dentry *dir;
27237fead6SMichael Halcrow 
288dc4e373SMiklos Szeredi 	dir = dget_parent(dentry);
295955102cSAl Viro 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
30237fead6SMichael Halcrow 	return dir;
31237fead6SMichael Halcrow }
32237fead6SMichael Halcrow 
33237fead6SMichael Halcrow static void unlock_dir(struct dentry *dir)
34237fead6SMichael Halcrow {
355955102cSAl Viro 	inode_unlock(d_inode(dir));
36237fead6SMichael Halcrow 	dput(dir);
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 {
1308bc2d3cfSTyler Hicks 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
1318bc2d3cfSTyler Hicks 	struct dentry *lower_dir_dentry;
132bcf0d9d4SAl Viro 	struct inode *lower_dir_inode;
1338bc2d3cfSTyler Hicks 	int rc;
1348bc2d3cfSTyler Hicks 
135bcf0d9d4SAl Viro 	lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
136bcf0d9d4SAl Viro 	lower_dir_inode = d_inode(lower_dir_dentry);
137bcf0d9d4SAl Viro 	inode_lock_nested(lower_dir_inode, I_MUTEX_PARENT);
138bcf0d9d4SAl Viro 	dget(lower_dentry);	// don't even try to make the lower negative
139bcf0d9d4SAl Viro 	if (lower_dentry->d_parent != lower_dir_dentry)
140bcf0d9d4SAl Viro 		rc = -EINVAL;
141bcf0d9d4SAl Viro 	else if (d_unhashed(lower_dentry))
142bcf0d9d4SAl Viro 		rc = -EINVAL;
143bcf0d9d4SAl Viro 	else
1446521f891SChristian Brauner 		rc = vfs_unlink(&init_user_ns, lower_dir_inode, lower_dentry,
1456521f891SChristian Brauner 				NULL);
1468bc2d3cfSTyler Hicks 	if (rc) {
1478bc2d3cfSTyler Hicks 		printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
1488bc2d3cfSTyler Hicks 		goto out_unlock;
1498bc2d3cfSTyler Hicks 	}
1508bc2d3cfSTyler Hicks 	fsstack_copy_attr_times(dir, lower_dir_inode);
1518bc2d3cfSTyler Hicks 	set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
1528bc2d3cfSTyler Hicks 	inode->i_ctime = dir->i_ctime;
1538bc2d3cfSTyler Hicks out_unlock:
1548bc2d3cfSTyler Hicks 	dput(lower_dentry);
155bcf0d9d4SAl Viro 	inode_unlock(lower_dir_inode);
156bcf0d9d4SAl Viro 	if (!rc)
157bcf0d9d4SAl Viro 		d_drop(dentry);
1588bc2d3cfSTyler Hicks 	return rc;
1598bc2d3cfSTyler Hicks }
1608bc2d3cfSTyler Hicks 
161237fead6SMichael Halcrow /**
162237fead6SMichael Halcrow  * ecryptfs_do_create
163237fead6SMichael Halcrow  * @directory_inode: inode of the new file's dentry's parent in ecryptfs
164237fead6SMichael Halcrow  * @ecryptfs_dentry: New file's dentry in ecryptfs
165237fead6SMichael Halcrow  * @mode: The mode of the new file
166237fead6SMichael Halcrow  *
167237fead6SMichael Halcrow  * Creates the underlying file and the eCryptfs inode which will link to
168237fead6SMichael Halcrow  * it. It will also update the eCryptfs directory inode to mimic the
169237fead6SMichael Halcrow  * stat of the lower directory inode.
170237fead6SMichael Halcrow  *
171b59db43aSTyler Hicks  * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
172237fead6SMichael Halcrow  */
173b59db43aSTyler Hicks static struct inode *
174237fead6SMichael Halcrow ecryptfs_do_create(struct inode *directory_inode,
175175a4eb7SAl Viro 		   struct dentry *ecryptfs_dentry, umode_t mode)
176237fead6SMichael Halcrow {
177237fead6SMichael Halcrow 	int rc;
178237fead6SMichael Halcrow 	struct dentry *lower_dentry;
179237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
180b59db43aSTyler Hicks 	struct inode *inode;
181237fead6SMichael Halcrow 
182237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
183237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
1846521f891SChristian Brauner 	rc = vfs_create(&init_user_ns, d_inode(lower_dir_dentry), lower_dentry,
1856521f891SChristian Brauner 			mode, true);
1864981e081SMichael Halcrow 	if (rc) {
187caeeeecfSMichael Halcrow 		printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
18818d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
189b59db43aSTyler Hicks 		inode = ERR_PTR(rc);
190237fead6SMichael Halcrow 		goto out_lock;
191237fead6SMichael Halcrow 	}
1922b0143b5SDavid Howells 	inode = __ecryptfs_get_inode(d_inode(lower_dentry),
1935ccf9203STyler Hicks 				     directory_inode->i_sb);
1948bc2d3cfSTyler Hicks 	if (IS_ERR(inode)) {
1956521f891SChristian Brauner 		vfs_unlink(&init_user_ns, d_inode(lower_dir_dentry),
1966521f891SChristian Brauner 			   lower_dentry, NULL);
197237fead6SMichael Halcrow 		goto out_lock;
1988bc2d3cfSTyler Hicks 	}
1992b0143b5SDavid Howells 	fsstack_copy_attr_times(directory_inode, d_inode(lower_dir_dentry));
2002b0143b5SDavid Howells 	fsstack_copy_inode_size(directory_inode, d_inode(lower_dir_dentry));
201237fead6SMichael Halcrow out_lock:
202237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
203b59db43aSTyler Hicks 	return inode;
204237fead6SMichael Halcrow }
205237fead6SMichael Halcrow 
206*d17074acSLee Jones /*
207237fead6SMichael Halcrow  * ecryptfs_initialize_file
208237fead6SMichael Halcrow  *
209237fead6SMichael Halcrow  * Cause the file to be changed from a basic empty file to an ecryptfs
210237fead6SMichael Halcrow  * file with a header and first data page.
211237fead6SMichael Halcrow  *
212237fead6SMichael Halcrow  * Returns zero on success
213237fead6SMichael Halcrow  */
214e3ccaa97STyler Hicks int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
215b59db43aSTyler Hicks 			     struct inode *ecryptfs_inode)
216237fead6SMichael Halcrow {
217d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
218b59db43aSTyler Hicks 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
219237fead6SMichael Halcrow 	int rc = 0;
220237fead6SMichael Halcrow 
221b59db43aSTyler Hicks 	if (S_ISDIR(ecryptfs_inode->i_mode)) {
222237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
223e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
224d7cdc5feSMichael Halcrow 		goto out;
225237fead6SMichael Halcrow 	}
226237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
227b59db43aSTyler Hicks 	rc = ecryptfs_new_file_context(ecryptfs_inode);
228237fead6SMichael Halcrow 	if (rc) {
229d7cdc5feSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error creating new file "
230d7cdc5feSMichael Halcrow 				"context; rc = [%d]\n", rc);
231d7cdc5feSMichael Halcrow 		goto out;
232237fead6SMichael Halcrow 	}
233b59db43aSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
234391b52f9SMichael Halcrow 	if (rc) {
235391b52f9SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
236332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2379e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2389e78d14aSDavid Howells 			ecryptfs_dentry, rc);
239391b52f9SMichael Halcrow 		goto out;
240391b52f9SMichael Halcrow 	}
241b59db43aSTyler Hicks 	rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
242332ab16fSTyler Hicks 	if (rc)
243d7cdc5feSMichael Halcrow 		printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
244b59db43aSTyler Hicks 	ecryptfs_put_lower_file(ecryptfs_inode);
245237fead6SMichael Halcrow out:
246237fead6SMichael Halcrow 	return rc;
247237fead6SMichael Halcrow }
248237fead6SMichael Halcrow 
249*d17074acSLee Jones /*
250237fead6SMichael Halcrow  * ecryptfs_create
251237fead6SMichael Halcrow  * @mode: The mode of the new file.
252237fead6SMichael Halcrow  *
253237fead6SMichael Halcrow  * Creates a new file.
254237fead6SMichael Halcrow  *
255237fead6SMichael Halcrow  * Returns zero on success; non-zero on error condition
256237fead6SMichael Halcrow  */
257237fead6SMichael Halcrow static int
258549c7297SChristian Brauner ecryptfs_create(struct user_namespace *mnt_userns,
259549c7297SChristian Brauner 		struct inode *directory_inode, struct dentry *ecryptfs_dentry,
260ebfc3b49SAl Viro 		umode_t mode, bool excl)
261237fead6SMichael Halcrow {
262b59db43aSTyler Hicks 	struct inode *ecryptfs_inode;
263237fead6SMichael Halcrow 	int rc;
264237fead6SMichael Halcrow 
265b59db43aSTyler Hicks 	ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
266b59db43aSTyler Hicks 					    mode);
267a1c83681SViresh Kumar 	if (IS_ERR(ecryptfs_inode)) {
268237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Failed to create file in"
269237fead6SMichael Halcrow 				"lower filesystem\n");
270b59db43aSTyler Hicks 		rc = PTR_ERR(ecryptfs_inode);
271237fead6SMichael Halcrow 		goto out;
272237fead6SMichael Halcrow 	}
273237fead6SMichael Halcrow 	/* At this point, a file exists on "disk"; we need to make sure
274237fead6SMichael Halcrow 	 * that this on disk file is prepared to be an ecryptfs file */
275b59db43aSTyler Hicks 	rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
276b59db43aSTyler Hicks 	if (rc) {
2778bc2d3cfSTyler Hicks 		ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
2788bc2d3cfSTyler Hicks 				   ecryptfs_inode);
2790e81ba23SAl Viro 		iget_failed(ecryptfs_inode);
280b59db43aSTyler Hicks 		goto out;
281b59db43aSTyler Hicks 	}
2821e2e547aSAl Viro 	d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
283237fead6SMichael Halcrow out:
284237fead6SMichael Halcrow 	return rc;
285237fead6SMichael Halcrow }
286237fead6SMichael Halcrow 
287778aeb42STyler Hicks static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
288237fead6SMichael Halcrow {
2892aac0cf8STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
290778aeb42STyler Hicks 	int rc;
291237fead6SMichael Halcrow 
292778aeb42STyler Hicks 	rc = ecryptfs_get_lower_file(dentry, inode);
293391b52f9SMichael Halcrow 	if (rc) {
294391b52f9SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
295332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2969e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2979e78d14aSDavid Howells 			dentry, rc);
298778aeb42STyler Hicks 		return rc;
299391b52f9SMichael Halcrow 	}
300778aeb42STyler Hicks 
3013b06b3ebSTyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
3022aac0cf8STyler Hicks 	/* TODO: lock for crypt_stat comparison */
3032aac0cf8STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
3042aac0cf8STyler Hicks 		ecryptfs_set_default_sizes(crypt_stat);
305778aeb42STyler Hicks 
306778aeb42STyler Hicks 	rc = ecryptfs_read_and_validate_header_region(inode);
307778aeb42STyler Hicks 	ecryptfs_put_lower_file(inode);
308237fead6SMichael Halcrow 	if (rc) {
309778aeb42STyler Hicks 		rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
310778aeb42STyler Hicks 		if (!rc)
311dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
312dd2a3b7aSMichael Halcrow 	}
313778aeb42STyler Hicks 
314778aeb42STyler Hicks 	/* Must return 0 to allow non-eCryptfs files to be looked up, too */
315778aeb42STyler Hicks 	return 0;
316778aeb42STyler Hicks }
317778aeb42STyler Hicks 
318*d17074acSLee Jones /*
319778aeb42STyler Hicks  * ecryptfs_lookup_interpose - Dentry interposition for a lookup
320778aeb42STyler Hicks  */
321b1168a92SAl Viro static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
322b1168a92SAl Viro 				     struct dentry *lower_dentry)
323778aeb42STyler Hicks {
324762c6968SAl Viro 	struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
325e72b9dd6SAl Viro 	struct inode *inode, *lower_inode;
326778aeb42STyler Hicks 	struct ecryptfs_dentry_info *dentry_info;
327778aeb42STyler Hicks 	int rc = 0;
328778aeb42STyler Hicks 
329778aeb42STyler Hicks 	dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
330778aeb42STyler Hicks 	if (!dentry_info) {
331237fead6SMichael Halcrow 		dput(lower_dentry);
332b1168a92SAl Viro 		return ERR_PTR(-ENOMEM);
333778aeb42STyler Hicks 	}
3340b1d9011SAl Viro 
335b1168a92SAl Viro 	fsstack_copy_attr_atime(d_inode(dentry->d_parent),
336762c6968SAl Viro 				d_inode(path->dentry));
33784d08fa8SAl Viro 	BUG_ON(!d_count(lower_dentry));
3380b1d9011SAl Viro 
3390b1d9011SAl Viro 	ecryptfs_set_dentry_private(dentry, dentry_info);
340762c6968SAl Viro 	dentry_info->lower_path.mnt = mntget(path->mnt);
34192dd1230SAl Viro 	dentry_info->lower_path.dentry = lower_dentry;
342778aeb42STyler Hicks 
343e72b9dd6SAl Viro 	/*
344e72b9dd6SAl Viro 	 * negative dentry can go positive under us here - its parent is not
345e72b9dd6SAl Viro 	 * locked.  That's OK and that could happen just as we return from
346e72b9dd6SAl Viro 	 * ecryptfs_lookup() anyway.  Just need to be careful and fetch
347e72b9dd6SAl Viro 	 * ->d_inode only once - it's not stable here.
348e72b9dd6SAl Viro 	 */
349e72b9dd6SAl Viro 	lower_inode = READ_ONCE(lower_dentry->d_inode);
350e72b9dd6SAl Viro 
351e72b9dd6SAl Viro 	if (!lower_inode) {
352778aeb42STyler Hicks 		/* We want to add because we couldn't find in lower */
353778aeb42STyler Hicks 		d_add(dentry, NULL);
354b1168a92SAl Viro 		return NULL;
355778aeb42STyler Hicks 	}
356b1168a92SAl Viro 	inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
357778aeb42STyler Hicks 	if (IS_ERR(inode)) {
358778aeb42STyler Hicks 		printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
359778aeb42STyler Hicks 		       __func__, PTR_ERR(inode));
360b1168a92SAl Viro 		return ERR_CAST(inode);
361778aeb42STyler Hicks 	}
362778aeb42STyler Hicks 	if (S_ISREG(inode->i_mode)) {
363778aeb42STyler Hicks 		rc = ecryptfs_i_size_read(dentry, inode);
364778aeb42STyler Hicks 		if (rc) {
365778aeb42STyler Hicks 			make_bad_inode(inode);
366b1168a92SAl Viro 			return ERR_PTR(rc);
367778aeb42STyler Hicks 		}
368778aeb42STyler Hicks 	}
369778aeb42STyler Hicks 
370778aeb42STyler Hicks 	if (inode->i_state & I_NEW)
371778aeb42STyler Hicks 		unlock_new_inode(inode);
372b1168a92SAl Viro 	return d_splice_alias(inode, dentry);
373addd65adSMichael Halcrow }
374addd65adSMichael Halcrow 
375addd65adSMichael Halcrow /**
376addd65adSMichael Halcrow  * ecryptfs_lookup
377addd65adSMichael Halcrow  * @ecryptfs_dir_inode: The eCryptfs directory inode
378addd65adSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
37989076bc3SAl Viro  * @flags: lookup flags
380addd65adSMichael Halcrow  *
381addd65adSMichael Halcrow  * Find a file on disk. If the file does not exist, then we'll add it to the
382addd65adSMichael Halcrow  * dentry cache and continue on to read it from the disk.
383addd65adSMichael Halcrow  */
384addd65adSMichael Halcrow static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
385addd65adSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
38600cd8dd3SAl Viro 				      unsigned int flags)
387addd65adSMichael Halcrow {
388addd65adSMichael Halcrow 	char *encrypted_and_encoded_name = NULL;
38988ae4ab9SAl Viro 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
390addd65adSMichael Halcrow 	struct dentry *lower_dir_dentry, *lower_dentry;
39188ae4ab9SAl Viro 	const char *name = ecryptfs_dentry->d_name.name;
39288ae4ab9SAl Viro 	size_t len = ecryptfs_dentry->d_name.len;
393b1168a92SAl Viro 	struct dentry *res;
394addd65adSMichael Halcrow 	int rc = 0;
395addd65adSMichael Halcrow 
396addd65adSMichael Halcrow 	lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
39788ae4ab9SAl Viro 
398addd65adSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
399addd65adSMichael Halcrow 				ecryptfs_dentry->d_sb)->mount_crypt_stat;
400ab13a921SGuenter Roeck 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
401addd65adSMichael Halcrow 		rc = ecryptfs_encrypt_and_encode_filename(
40288ae4ab9SAl Viro 			&encrypted_and_encoded_name, &len,
40388ae4ab9SAl Viro 			mount_crypt_stat, name, len);
404addd65adSMichael Halcrow 		if (rc) {
405addd65adSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encrypt and encode "
406addd65adSMichael Halcrow 			       "filename; rc = [%d]\n", __func__, rc);
407237fead6SMichael Halcrow 			return ERR_PTR(rc);
408237fead6SMichael Halcrow 		}
40988ae4ab9SAl Viro 		name = encrypted_and_encoded_name;
41088ae4ab9SAl Viro 	}
41188ae4ab9SAl Viro 
41288ae4ab9SAl Viro 	lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
413237fead6SMichael Halcrow 	if (IS_ERR(lower_dentry)) {
414237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
415b1168a92SAl Viro 				"[%ld] on lower_dentry = [%s]\n", __func__,
416b1168a92SAl Viro 				PTR_ERR(lower_dentry),
41788ae4ab9SAl Viro 				name);
418b1168a92SAl Viro 		res = ERR_CAST(lower_dentry);
41988ae4ab9SAl Viro 	} else {
420b1168a92SAl Viro 		res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
42188ae4ab9SAl Viro 	}
422237fead6SMichael Halcrow 	kfree(encrypted_and_encoded_name);
423b1168a92SAl Viro 	return res;
424237fead6SMichael Halcrow }
425237fead6SMichael Halcrow 
426237fead6SMichael Halcrow static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
427237fead6SMichael Halcrow 			 struct dentry *new_dentry)
428237fead6SMichael Halcrow {
429237fead6SMichael Halcrow 	struct dentry *lower_old_dentry;
430237fead6SMichael Halcrow 	struct dentry *lower_new_dentry;
431237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
432237fead6SMichael Halcrow 	u64 file_size_save;
433237fead6SMichael Halcrow 	int rc;
434237fead6SMichael Halcrow 
4352b0143b5SDavid Howells 	file_size_save = i_size_read(d_inode(old_dentry));
436237fead6SMichael Halcrow 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
437237fead6SMichael Halcrow 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
438237fead6SMichael Halcrow 	dget(lower_old_dentry);
439237fead6SMichael Halcrow 	dget(lower_new_dentry);
440237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_new_dentry);
4416521f891SChristian Brauner 	rc = vfs_link(lower_old_dentry, &init_user_ns,
4426521f891SChristian Brauner 		      d_inode(lower_dir_dentry), lower_new_dentry, NULL);
4432b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_new_dentry))
444237fead6SMichael Halcrow 		goto out_lock;
4455ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
446237fead6SMichael Halcrow 	if (rc)
447237fead6SMichael Halcrow 		goto out_lock;
4482b0143b5SDavid Howells 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
4492b0143b5SDavid Howells 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
4502b0143b5SDavid Howells 	set_nlink(d_inode(old_dentry),
4512b0143b5SDavid Howells 		  ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
4522b0143b5SDavid Howells 	i_size_write(d_inode(new_dentry), file_size_save);
453237fead6SMichael Halcrow out_lock:
454237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
455237fead6SMichael Halcrow 	dput(lower_new_dentry);
456237fead6SMichael Halcrow 	dput(lower_old_dentry);
457237fead6SMichael Halcrow 	return rc;
458237fead6SMichael Halcrow }
459237fead6SMichael Halcrow 
460237fead6SMichael Halcrow static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
461237fead6SMichael Halcrow {
4622b0143b5SDavid Howells 	return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
463237fead6SMichael Halcrow }
464237fead6SMichael Halcrow 
465549c7297SChristian Brauner static int ecryptfs_symlink(struct user_namespace *mnt_userns,
466549c7297SChristian Brauner 			    struct inode *dir, struct dentry *dentry,
467237fead6SMichael Halcrow 			    const char *symname)
468237fead6SMichael Halcrow {
469237fead6SMichael Halcrow 	int rc;
470237fead6SMichael Halcrow 	struct dentry *lower_dentry;
471237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
472237fead6SMichael Halcrow 	char *encoded_symname;
473addd65adSMichael Halcrow 	size_t encoded_symlen;
474addd65adSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
475237fead6SMichael Halcrow 
476237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
477237fead6SMichael Halcrow 	dget(lower_dentry);
478237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
479addd65adSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
480addd65adSMichael Halcrow 		dir->i_sb)->mount_crypt_stat;
481addd65adSMichael Halcrow 	rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
482addd65adSMichael Halcrow 						  &encoded_symlen,
483addd65adSMichael Halcrow 						  mount_crypt_stat, symname,
484addd65adSMichael Halcrow 						  strlen(symname));
485addd65adSMichael Halcrow 	if (rc)
486237fead6SMichael Halcrow 		goto out_lock;
4876521f891SChristian Brauner 	rc = vfs_symlink(&init_user_ns, d_inode(lower_dir_dentry), lower_dentry,
488db2e747bSMiklos Szeredi 			 encoded_symname);
489237fead6SMichael Halcrow 	kfree(encoded_symname);
4902b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
491237fead6SMichael Halcrow 		goto out_lock;
4925ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
493237fead6SMichael Halcrow 	if (rc)
494237fead6SMichael Halcrow 		goto out_lock;
4952b0143b5SDavid Howells 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
4962b0143b5SDavid Howells 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
497237fead6SMichael Halcrow out_lock:
498237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
499237fead6SMichael Halcrow 	dput(lower_dentry);
5002b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
501237fead6SMichael Halcrow 		d_drop(dentry);
502237fead6SMichael Halcrow 	return rc;
503237fead6SMichael Halcrow }
504237fead6SMichael Halcrow 
505549c7297SChristian Brauner static int ecryptfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
506549c7297SChristian Brauner 			  struct dentry *dentry, umode_t mode)
507237fead6SMichael Halcrow {
508237fead6SMichael Halcrow 	int rc;
509237fead6SMichael Halcrow 	struct dentry *lower_dentry;
510237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
511237fead6SMichael Halcrow 
512237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
513237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
5146521f891SChristian Brauner 	rc = vfs_mkdir(&init_user_ns, d_inode(lower_dir_dentry), lower_dentry,
5156521f891SChristian Brauner 		       mode);
5162b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
517237fead6SMichael Halcrow 		goto out;
5185ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
519237fead6SMichael Halcrow 	if (rc)
520237fead6SMichael Halcrow 		goto out;
5212b0143b5SDavid Howells 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
5222b0143b5SDavid Howells 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
5232b0143b5SDavid Howells 	set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
524237fead6SMichael Halcrow out:
525237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
5262b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
527237fead6SMichael Halcrow 		d_drop(dentry);
528237fead6SMichael Halcrow 	return rc;
529237fead6SMichael Halcrow }
530237fead6SMichael Halcrow 
531237fead6SMichael Halcrow static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
532237fead6SMichael Halcrow {
533237fead6SMichael Halcrow 	struct dentry *lower_dentry;
534237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
535bcf0d9d4SAl Viro 	struct inode *lower_dir_inode;
53645ec4abaSMichael Halcrow 	int rc;
537237fead6SMichael Halcrow 
538237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
539bcf0d9d4SAl Viro 	lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
540bcf0d9d4SAl Viro 	lower_dir_inode = d_inode(lower_dir_dentry);
541bcf0d9d4SAl Viro 
542bcf0d9d4SAl Viro 	inode_lock_nested(lower_dir_inode, I_MUTEX_PARENT);
543bcf0d9d4SAl Viro 	dget(lower_dentry);	// don't even try to make the lower negative
544bcf0d9d4SAl Viro 	if (lower_dentry->d_parent != lower_dir_dentry)
545bcf0d9d4SAl Viro 		rc = -EINVAL;
546bcf0d9d4SAl Viro 	else if (d_unhashed(lower_dentry))
547bcf0d9d4SAl Viro 		rc = -EINVAL;
548bcf0d9d4SAl Viro 	else
5496521f891SChristian Brauner 		rc = vfs_rmdir(&init_user_ns, lower_dir_inode, lower_dentry);
550bcf0d9d4SAl Viro 	if (!rc) {
5512b0143b5SDavid Howells 		clear_nlink(d_inode(dentry));
552bcf0d9d4SAl Viro 		fsstack_copy_attr_times(dir, lower_dir_inode);
553bcf0d9d4SAl Viro 		set_nlink(dir, lower_dir_inode->i_nlink);
554bcf0d9d4SAl Viro 	}
555bcf0d9d4SAl Viro 	dput(lower_dentry);
556bcf0d9d4SAl Viro 	inode_unlock(lower_dir_inode);
557237fead6SMichael Halcrow 	if (!rc)
558237fead6SMichael Halcrow 		d_drop(dentry);
559237fead6SMichael Halcrow 	return rc;
560237fead6SMichael Halcrow }
561237fead6SMichael Halcrow 
562237fead6SMichael Halcrow static int
563549c7297SChristian Brauner ecryptfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
564549c7297SChristian Brauner 	       struct dentry *dentry, umode_t mode, dev_t dev)
565237fead6SMichael Halcrow {
566237fead6SMichael Halcrow 	int rc;
567237fead6SMichael Halcrow 	struct dentry *lower_dentry;
568237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
569237fead6SMichael Halcrow 
570237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
571237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
5726521f891SChristian Brauner 	rc = vfs_mknod(&init_user_ns, d_inode(lower_dir_dentry), lower_dentry,
5736521f891SChristian Brauner 		       mode, dev);
5742b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
575237fead6SMichael Halcrow 		goto out;
5765ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
577237fead6SMichael Halcrow 	if (rc)
578237fead6SMichael Halcrow 		goto out;
5792b0143b5SDavid Howells 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
5802b0143b5SDavid Howells 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
581237fead6SMichael Halcrow out:
582237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
5832b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
584237fead6SMichael Halcrow 		d_drop(dentry);
585237fead6SMichael Halcrow 	return rc;
586237fead6SMichael Halcrow }
587237fead6SMichael Halcrow 
588237fead6SMichael Halcrow static int
589549c7297SChristian Brauner ecryptfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
590549c7297SChristian Brauner 		struct dentry *old_dentry, struct inode *new_dir,
591549c7297SChristian Brauner 		struct dentry *new_dentry, unsigned int flags)
592237fead6SMichael Halcrow {
593237fead6SMichael Halcrow 	int rc;
594237fead6SMichael Halcrow 	struct dentry *lower_old_dentry;
595237fead6SMichael Halcrow 	struct dentry *lower_new_dentry;
596237fead6SMichael Halcrow 	struct dentry *lower_old_dir_dentry;
597237fead6SMichael Halcrow 	struct dentry *lower_new_dir_dentry;
598bcf0d9d4SAl Viro 	struct dentry *trap;
5998335eafcSTyler Hicks 	struct inode *target_inode;
6009fe61450SChristian Brauner 	struct renamedata rd = {};
601237fead6SMichael Halcrow 
6021cd66c93SMiklos Szeredi 	if (flags)
6031cd66c93SMiklos Szeredi 		return -EINVAL;
6041cd66c93SMiklos Szeredi 
605bcf0d9d4SAl Viro 	lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent);
606bcf0d9d4SAl Viro 	lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent);
607bcf0d9d4SAl Viro 
608237fead6SMichael Halcrow 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
609237fead6SMichael Halcrow 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
610bcf0d9d4SAl Viro 
6112b0143b5SDavid Howells 	target_inode = d_inode(new_dentry);
612bcf0d9d4SAl Viro 
6130d132f73SErez Zadok 	trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
614bcf0d9d4SAl Viro 	dget(lower_new_dentry);
6150d132f73SErez Zadok 	rc = -EINVAL;
61674dd7c97SAl Viro 	if (lower_old_dentry->d_parent != lower_old_dir_dentry)
6170d132f73SErez Zadok 		goto out_lock;
61874dd7c97SAl Viro 	if (lower_new_dentry->d_parent != lower_new_dir_dentry)
61974dd7c97SAl Viro 		goto out_lock;
62074dd7c97SAl Viro 	if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry))
62174dd7c97SAl Viro 		goto out_lock;
62274dd7c97SAl Viro 	/* source should not be ancestor of target */
62374dd7c97SAl Viro 	if (trap == lower_old_dentry)
62474dd7c97SAl Viro 		goto out_lock;
6250d132f73SErez Zadok 	/* target should not be ancestor of source */
6260d132f73SErez Zadok 	if (trap == lower_new_dentry) {
6270d132f73SErez Zadok 		rc = -ENOTEMPTY;
6280d132f73SErez Zadok 		goto out_lock;
6290d132f73SErez Zadok 	}
6309fe61450SChristian Brauner 
6316521f891SChristian Brauner 	rd.old_mnt_userns	= &init_user_ns;
6329fe61450SChristian Brauner 	rd.old_dir		= d_inode(lower_old_dir_dentry);
6339fe61450SChristian Brauner 	rd.old_dentry		= lower_old_dentry;
6346521f891SChristian Brauner 	rd.new_mnt_userns	= &init_user_ns;
6359fe61450SChristian Brauner 	rd.new_dir		= d_inode(lower_new_dir_dentry);
6369fe61450SChristian Brauner 	rd.new_dentry		= lower_new_dentry;
6379fe61450SChristian Brauner 	rc = vfs_rename(&rd);
638237fead6SMichael Halcrow 	if (rc)
639237fead6SMichael Halcrow 		goto out_lock;
6408335eafcSTyler Hicks 	if (target_inode)
6418335eafcSTyler Hicks 		fsstack_copy_attr_all(target_inode,
6428335eafcSTyler Hicks 				      ecryptfs_inode_to_lower(target_inode));
6432b0143b5SDavid Howells 	fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
644237fead6SMichael Halcrow 	if (new_dir != old_dir)
6452b0143b5SDavid Howells 		fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
646237fead6SMichael Halcrow out_lock:
647237fead6SMichael Halcrow 	dput(lower_new_dentry);
648bcf0d9d4SAl Viro 	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
649237fead6SMichael Halcrow 	return rc;
650237fead6SMichael Halcrow }
651237fead6SMichael Halcrow 
652b22e8fedSAl Viro static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
653237fead6SMichael Halcrow {
6546c988f57SMiklos Szeredi 	DEFINE_DELAYED_CALL(done);
6553a60a168STyler Hicks 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
6566c988f57SMiklos Szeredi 	const char *link;
657b22e8fedSAl Viro 	char *buf;
658addd65adSMichael Halcrow 	int rc;
659237fead6SMichael Halcrow 
6606c988f57SMiklos Szeredi 	link = vfs_get_link(lower_dentry, &done);
6616c988f57SMiklos Szeredi 	if (IS_ERR(link))
6626c988f57SMiklos Szeredi 		return ERR_CAST(link);
6636c988f57SMiklos Szeredi 
664b22e8fedSAl Viro 	rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
6656c988f57SMiklos Szeredi 						  link, strlen(link));
6666c988f57SMiklos Szeredi 	do_delayed_call(&done);
6676c988f57SMiklos Szeredi 	if (rc)
6686c988f57SMiklos Szeredi 		return ERR_PTR(rc);
6696c988f57SMiklos Szeredi 
6706c988f57SMiklos Szeredi 	return buf;
6713a60a168STyler Hicks }
6723a60a168STyler Hicks 
6736b255391SAl Viro static const char *ecryptfs_get_link(struct dentry *dentry,
674fceef393SAl Viro 				     struct inode *inode,
675fceef393SAl Viro 				     struct delayed_call *done)
676237fead6SMichael Halcrow {
677b22e8fedSAl Viro 	size_t len;
6786b255391SAl Viro 	char *buf;
6796b255391SAl Viro 
6806b255391SAl Viro 	if (!dentry)
6816b255391SAl Viro 		return ERR_PTR(-ECHILD);
6826b255391SAl Viro 
6836b255391SAl Viro 	buf = ecryptfs_readlink_lower(dentry, &len);
684b22e8fedSAl Viro 	if (IS_ERR(buf))
685680baacbSAl Viro 		return buf;
6862b0143b5SDavid Howells 	fsstack_copy_attr_atime(d_inode(dentry),
6872b0143b5SDavid Howells 				d_inode(ecryptfs_dentry_to_lower(dentry)));
688408bd629SAl Viro 	buf[len] = '\0';
689fceef393SAl Viro 	set_delayed_call(done, kfree_link, buf);
690fceef393SAl Viro 	return buf;
691237fead6SMichael Halcrow }
692237fead6SMichael Halcrow 
693237fead6SMichael Halcrow /**
694237fead6SMichael Halcrow  * upper_size_to_lower_size
695237fead6SMichael Halcrow  * @crypt_stat: Crypt_stat associated with file
696237fead6SMichael Halcrow  * @upper_size: Size of the upper file
697237fead6SMichael Halcrow  *
698cc11beffSMichael Halcrow  * Calculate the required size of the lower file based on the
699237fead6SMichael Halcrow  * specified size of the upper file. This calculation is based on the
700237fead6SMichael Halcrow  * number of headers in the underlying file and the extent size.
701237fead6SMichael Halcrow  *
702237fead6SMichael Halcrow  * Returns Calculated size of the lower file.
703237fead6SMichael Halcrow  */
704237fead6SMichael Halcrow static loff_t
705237fead6SMichael Halcrow upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
706237fead6SMichael Halcrow 			 loff_t upper_size)
707237fead6SMichael Halcrow {
708237fead6SMichael Halcrow 	loff_t lower_size;
709237fead6SMichael Halcrow 
710157f1071STyler Hicks 	lower_size = ecryptfs_lower_header_size(crypt_stat);
711237fead6SMichael Halcrow 	if (upper_size != 0) {
712237fead6SMichael Halcrow 		loff_t num_extents;
713237fead6SMichael Halcrow 
714237fead6SMichael Halcrow 		num_extents = upper_size >> crypt_stat->extent_shift;
715237fead6SMichael Halcrow 		if (upper_size & ~crypt_stat->extent_mask)
716237fead6SMichael Halcrow 			num_extents++;
717237fead6SMichael Halcrow 		lower_size += (num_extents * crypt_stat->extent_size);
718237fead6SMichael Halcrow 	}
719237fead6SMichael Halcrow 	return lower_size;
720237fead6SMichael Halcrow }
721237fead6SMichael Halcrow 
722237fead6SMichael Halcrow /**
7235f3ef64fSTyler Hicks  * truncate_upper
724237fead6SMichael Halcrow  * @dentry: The ecryptfs layer dentry
7255f3ef64fSTyler Hicks  * @ia: Address of the ecryptfs inode's attributes
7265f3ef64fSTyler Hicks  * @lower_ia: Address of the lower inode's attributes
727237fead6SMichael Halcrow  *
728237fead6SMichael Halcrow  * Function to handle truncations modifying the size of the file. Note
729237fead6SMichael Halcrow  * that the file sizes are interpolated. When expanding, we are simply
7305f3ef64fSTyler Hicks  * writing strings of 0's out. When truncating, we truncate the upper
7315f3ef64fSTyler Hicks  * inode and update the lower_ia according to the page index
7325f3ef64fSTyler Hicks  * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
7335f3ef64fSTyler Hicks  * the caller must use lower_ia in a call to notify_change() to perform
7345f3ef64fSTyler Hicks  * the truncation of the lower inode.
735237fead6SMichael Halcrow  *
736237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
737237fead6SMichael Halcrow  */
7385f3ef64fSTyler Hicks static int truncate_upper(struct dentry *dentry, struct iattr *ia,
7395f3ef64fSTyler Hicks 			  struct iattr *lower_ia)
740237fead6SMichael Halcrow {
741237fead6SMichael Halcrow 	int rc = 0;
7422b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
743237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
744237fead6SMichael Halcrow 	loff_t i_size = i_size_read(inode);
745237fead6SMichael Halcrow 	loff_t lower_size_before_truncate;
746237fead6SMichael Halcrow 	loff_t lower_size_after_truncate;
747237fead6SMichael Halcrow 
7485f3ef64fSTyler Hicks 	if (unlikely((ia->ia_size == i_size))) {
7495f3ef64fSTyler Hicks 		lower_ia->ia_valid &= ~ATTR_SIZE;
750332ab16fSTyler Hicks 		return 0;
7515f3ef64fSTyler Hicks 	}
7523b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(dentry, inode);
753332ab16fSTyler Hicks 	if (rc)
754332ab16fSTyler Hicks 		return rc;
7552b0143b5SDavid Howells 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
756237fead6SMichael Halcrow 	/* Switch on growing or shrinking file */
7575f3ef64fSTyler Hicks 	if (ia->ia_size > i_size) {
7582ed92554SMichael Halcrow 		char zero[] = { 0x00 };
759240e2df5SMichael Halcrow 
7605f3ef64fSTyler Hicks 		lower_ia->ia_valid &= ~ATTR_SIZE;
7612ed92554SMichael Halcrow 		/* Write a single 0 at the last position of the file;
7622ed92554SMichael Halcrow 		 * this triggers code that will fill in 0's throughout
7632ed92554SMichael Halcrow 		 * the intermediate portion of the previous end of the
7642ed92554SMichael Halcrow 		 * file and the new and of the file */
76548c1e44aSAl Viro 		rc = ecryptfs_write(inode, zero,
7665f3ef64fSTyler Hicks 				    (ia->ia_size - 1), 1);
7675f3ef64fSTyler Hicks 	} else { /* ia->ia_size < i_size_read(inode) */
7685f3ef64fSTyler Hicks 		/* We're chopping off all the pages down to the page
7695f3ef64fSTyler Hicks 		 * in which ia->ia_size is located. Fill in the end of
770ea1754a0SKirill A. Shutemov 		 * that page from (ia->ia_size & ~PAGE_MASK) to
771ea1754a0SKirill A. Shutemov 		 * PAGE_SIZE with zeros. */
77209cbfeafSKirill A. Shutemov 		size_t num_zeros = (PAGE_SIZE
77309cbfeafSKirill A. Shutemov 				    - (ia->ia_size & ~PAGE_MASK));
7742ed92554SMichael Halcrow 
7752c27c65eSChristoph Hellwig 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
7762c27c65eSChristoph Hellwig 			truncate_setsize(inode, ia->ia_size);
7775f3ef64fSTyler Hicks 			lower_ia->ia_size = ia->ia_size;
7785f3ef64fSTyler Hicks 			lower_ia->ia_valid |= ATTR_SIZE;
77948c1e44aSAl Viro 			goto out;
78013a791b4STyler Hicks 		}
7812ed92554SMichael Halcrow 		if (num_zeros) {
7822ed92554SMichael Halcrow 			char *zeros_virt;
7832ed92554SMichael Halcrow 
7842ed92554SMichael Halcrow 			zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
7852ed92554SMichael Halcrow 			if (!zeros_virt) {
7862ed92554SMichael Halcrow 				rc = -ENOMEM;
78748c1e44aSAl Viro 				goto out;
788240e2df5SMichael Halcrow 			}
78948c1e44aSAl Viro 			rc = ecryptfs_write(inode, zeros_virt,
7905f3ef64fSTyler Hicks 					    ia->ia_size, num_zeros);
7912ed92554SMichael Halcrow 			kfree(zeros_virt);
7925dda6992SMichael Halcrow 			if (rc) {
793240e2df5SMichael Halcrow 				printk(KERN_ERR "Error attempting to zero out "
794240e2df5SMichael Halcrow 				       "the remainder of the end page on "
795240e2df5SMichael Halcrow 				       "reducing truncate; rc = [%d]\n", rc);
79648c1e44aSAl Viro 				goto out;
797240e2df5SMichael Halcrow 			}
798240e2df5SMichael Halcrow 		}
7992c27c65eSChristoph Hellwig 		truncate_setsize(inode, ia->ia_size);
8000216f7f7SMichael Halcrow 		rc = ecryptfs_write_inode_size_to_metadata(inode);
801dd2a3b7aSMichael Halcrow 		if (rc) {
802dd2a3b7aSMichael Halcrow 			printk(KERN_ERR	"Problem with "
803dd2a3b7aSMichael Halcrow 			       "ecryptfs_write_inode_size_to_metadata; "
804dd2a3b7aSMichael Halcrow 			       "rc = [%d]\n", rc);
80548c1e44aSAl Viro 			goto out;
806dd2a3b7aSMichael Halcrow 		}
807237fead6SMichael Halcrow 		/* We are reducing the size of the ecryptfs file, and need to
808237fead6SMichael Halcrow 		 * know if we need to reduce the size of the lower file. */
809237fead6SMichael Halcrow 		lower_size_before_truncate =
810237fead6SMichael Halcrow 		    upper_size_to_lower_size(crypt_stat, i_size);
811237fead6SMichael Halcrow 		lower_size_after_truncate =
8125f3ef64fSTyler Hicks 		    upper_size_to_lower_size(crypt_stat, ia->ia_size);
8135f3ef64fSTyler Hicks 		if (lower_size_after_truncate < lower_size_before_truncate) {
8145f3ef64fSTyler Hicks 			lower_ia->ia_size = lower_size_after_truncate;
8155f3ef64fSTyler Hicks 			lower_ia->ia_valid |= ATTR_SIZE;
8165f3ef64fSTyler Hicks 		} else
8175f3ef64fSTyler Hicks 			lower_ia->ia_valid &= ~ATTR_SIZE;
818237fead6SMichael Halcrow 	}
819237fead6SMichael Halcrow out:
820332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
821237fead6SMichael Halcrow 	return rc;
822237fead6SMichael Halcrow }
823237fead6SMichael Halcrow 
824a261a039STyler Hicks static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
825a261a039STyler Hicks {
826a261a039STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
827a261a039STyler Hicks 	loff_t lower_oldsize, lower_newsize;
828a261a039STyler Hicks 
829a261a039STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
830a261a039STyler Hicks 	lower_oldsize = upper_size_to_lower_size(crypt_stat,
831a261a039STyler Hicks 						 i_size_read(inode));
832a261a039STyler Hicks 	lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
833a261a039STyler Hicks 	if (lower_newsize > lower_oldsize) {
834a261a039STyler Hicks 		/*
835a261a039STyler Hicks 		 * The eCryptfs inode and the new *lower* size are mixed here
836a261a039STyler Hicks 		 * because we may not have the lower i_mutex held and/or it may
837a261a039STyler Hicks 		 * not be appropriate to call inode_newsize_ok() with inodes
838a261a039STyler Hicks 		 * from other filesystems.
839a261a039STyler Hicks 		 */
840a261a039STyler Hicks 		return inode_newsize_ok(inode, lower_newsize);
841a261a039STyler Hicks 	}
842a261a039STyler Hicks 
843a261a039STyler Hicks 	return 0;
844a261a039STyler Hicks }
845a261a039STyler Hicks 
8465f3ef64fSTyler Hicks /**
8475f3ef64fSTyler Hicks  * ecryptfs_truncate
8485f3ef64fSTyler Hicks  * @dentry: The ecryptfs layer dentry
8495f3ef64fSTyler Hicks  * @new_length: The length to expand the file to
8505f3ef64fSTyler Hicks  *
8515f3ef64fSTyler Hicks  * Simple function that handles the truncation of an eCryptfs inode and
8525f3ef64fSTyler Hicks  * its corresponding lower inode.
8535f3ef64fSTyler Hicks  *
8545f3ef64fSTyler Hicks  * Returns zero on success; non-zero otherwise
8555f3ef64fSTyler Hicks  */
8565f3ef64fSTyler Hicks int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
8575f3ef64fSTyler Hicks {
8585f3ef64fSTyler Hicks 	struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
8595f3ef64fSTyler Hicks 	struct iattr lower_ia = { .ia_valid = 0 };
8605f3ef64fSTyler Hicks 	int rc;
8615f3ef64fSTyler Hicks 
8622b0143b5SDavid Howells 	rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
863a261a039STyler Hicks 	if (rc)
864a261a039STyler Hicks 		return rc;
865a261a039STyler Hicks 
8665f3ef64fSTyler Hicks 	rc = truncate_upper(dentry, &ia, &lower_ia);
8675f3ef64fSTyler Hicks 	if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
8685f3ef64fSTyler Hicks 		struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
8695f3ef64fSTyler Hicks 
8705955102cSAl Viro 		inode_lock(d_inode(lower_dentry));
8712f221d6fSChristian Brauner 		rc = notify_change(&init_user_ns, lower_dentry,
8722f221d6fSChristian Brauner 				   &lower_ia, NULL);
8735955102cSAl Viro 		inode_unlock(d_inode(lower_dentry));
8745f3ef64fSTyler Hicks 	}
8755f3ef64fSTyler Hicks 	return rc;
8765f3ef64fSTyler Hicks }
8775f3ef64fSTyler Hicks 
878237fead6SMichael Halcrow static int
879549c7297SChristian Brauner ecryptfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
880549c7297SChristian Brauner 		    int mask)
881237fead6SMichael Halcrow {
88247291baaSChristian Brauner 	return inode_permission(&init_user_ns,
88347291baaSChristian Brauner 				ecryptfs_inode_to_lower(inode), mask);
884237fead6SMichael Halcrow }
885237fead6SMichael Halcrow 
886237fead6SMichael Halcrow /**
887237fead6SMichael Halcrow  * ecryptfs_setattr
888*d17074acSLee Jones  * @mnt_userns: user namespace of the target mount
889237fead6SMichael Halcrow  * @dentry: dentry handle to the inode to modify
890237fead6SMichael Halcrow  * @ia: Structure with flags of what to change and values
891237fead6SMichael Halcrow  *
892237fead6SMichael Halcrow  * Updates the metadata of an inode. If the update is to the size
893237fead6SMichael Halcrow  * i.e. truncation, then ecryptfs_truncate will handle the size modification
894237fead6SMichael Halcrow  * of both the ecryptfs inode and the lower inode.
895237fead6SMichael Halcrow  *
896237fead6SMichael Halcrow  * All other metadata changes will be passed right to the lower filesystem,
897237fead6SMichael Halcrow  * and we will just update our inode to look like the lower.
898237fead6SMichael Halcrow  */
899549c7297SChristian Brauner static int ecryptfs_setattr(struct user_namespace *mnt_userns,
900549c7297SChristian Brauner 			    struct dentry *dentry, struct iattr *ia)
901237fead6SMichael Halcrow {
902237fead6SMichael Halcrow 	int rc = 0;
903237fead6SMichael Halcrow 	struct dentry *lower_dentry;
9045f3ef64fSTyler Hicks 	struct iattr lower_ia;
905237fead6SMichael Halcrow 	struct inode *inode;
906237fead6SMichael Halcrow 	struct inode *lower_inode;
907237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
908237fead6SMichael Halcrow 
9092b0143b5SDavid Howells 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
910e81f3340SHerbert Xu 	if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
911e81f3340SHerbert Xu 		rc = ecryptfs_init_crypt_stat(crypt_stat);
912e81f3340SHerbert Xu 		if (rc)
913e81f3340SHerbert Xu 			return rc;
914e81f3340SHerbert Xu 	}
9152b0143b5SDavid Howells 	inode = d_inode(dentry);
916237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
917e10f281bSMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
918e10f281bSMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
919e36cb0b8SDavid Howells 	if (d_is_dir(dentry))
920e10f281bSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
921e36cb0b8SDavid Howells 	else if (d_is_reg(dentry)
92264ee4808SMichael Halcrow 		 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
92364ee4808SMichael Halcrow 		     || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
924e10f281bSMichael Halcrow 		struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
925e10f281bSMichael Halcrow 
926e10f281bSMichael Halcrow 		mount_crypt_stat = &ecryptfs_superblock_to_private(
927e10f281bSMichael Halcrow 			dentry->d_sb)->mount_crypt_stat;
9283b06b3ebSTyler Hicks 		rc = ecryptfs_get_lower_file(dentry, inode);
929332ab16fSTyler Hicks 		if (rc) {
930332ab16fSTyler Hicks 			mutex_unlock(&crypt_stat->cs_mutex);
931332ab16fSTyler Hicks 			goto out;
932332ab16fSTyler Hicks 		}
933d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_metadata(dentry);
934332ab16fSTyler Hicks 		ecryptfs_put_lower_file(inode);
9355dda6992SMichael Halcrow 		if (rc) {
936e10f281bSMichael Halcrow 			if (!(mount_crypt_stat->flags
937e10f281bSMichael Halcrow 			      & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
938e10f281bSMichael Halcrow 				rc = -EIO;
93925bd8174SMichael Halcrow 				printk(KERN_WARNING "Either the lower file "
940e10f281bSMichael Halcrow 				       "is not in a valid eCryptfs format, "
94125bd8174SMichael Halcrow 				       "or the key could not be retrieved. "
94225bd8174SMichael Halcrow 				       "Plaintext passthrough mode is not "
943e10f281bSMichael Halcrow 				       "enabled; returning -EIO\n");
944e10f281bSMichael Halcrow 				mutex_unlock(&crypt_stat->cs_mutex);
945e10f281bSMichael Halcrow 				goto out;
946e10f281bSMichael Halcrow 			}
947e10f281bSMichael Halcrow 			rc = 0;
9483aeb86eaSTyler Hicks 			crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
9493aeb86eaSTyler Hicks 					       | ECRYPTFS_ENCRYPTED);
950e10f281bSMichael Halcrow 		}
951e10f281bSMichael Halcrow 	}
952e10f281bSMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
953a261a039STyler Hicks 
9542f221d6fSChristian Brauner 	rc = setattr_prepare(&init_user_ns, dentry, ia);
955a261a039STyler Hicks 	if (rc)
956a261a039STyler Hicks 		goto out;
957a261a039STyler Hicks 	if (ia->ia_valid & ATTR_SIZE) {
958a261a039STyler Hicks 		rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
959a261a039STyler Hicks 		if (rc)
960a261a039STyler Hicks 			goto out;
961a261a039STyler Hicks 	}
962a261a039STyler Hicks 
9635f3ef64fSTyler Hicks 	memcpy(&lower_ia, ia, sizeof(lower_ia));
9645f3ef64fSTyler Hicks 	if (ia->ia_valid & ATTR_FILE)
9655f3ef64fSTyler Hicks 		lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
966237fead6SMichael Halcrow 	if (ia->ia_valid & ATTR_SIZE) {
9675f3ef64fSTyler Hicks 		rc = truncate_upper(dentry, ia, &lower_ia);
968237fead6SMichael Halcrow 		if (rc < 0)
969237fead6SMichael Halcrow 			goto out;
970237fead6SMichael Halcrow 	}
9711ac564ecSJeff Layton 
9721ac564ecSJeff Layton 	/*
9731ac564ecSJeff Layton 	 * mode change is for clearing setuid/setgid bits. Allow lower fs
9741ac564ecSJeff Layton 	 * to interpret this in its own way.
9751ac564ecSJeff Layton 	 */
9765f3ef64fSTyler Hicks 	if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
9775f3ef64fSTyler Hicks 		lower_ia.ia_valid &= ~ATTR_MODE;
9781ac564ecSJeff Layton 
9795955102cSAl Viro 	inode_lock(d_inode(lower_dentry));
9802f221d6fSChristian Brauner 	rc = notify_change(&init_user_ns, lower_dentry, &lower_ia, NULL);
9815955102cSAl Viro 	inode_unlock(d_inode(lower_dentry));
982237fead6SMichael Halcrow out:
9839afa2fb6SErez Zadok 	fsstack_copy_attr_all(inode, lower_inode);
984237fead6SMichael Halcrow 	return rc;
985237fead6SMichael Halcrow }
986237fead6SMichael Halcrow 
987549c7297SChristian Brauner static int ecryptfs_getattr_link(struct user_namespace *mnt_userns,
988549c7297SChristian Brauner 				 const struct path *path, struct kstat *stat,
989a528d35eSDavid Howells 				 u32 request_mask, unsigned int flags)
9903a60a168STyler Hicks {
991a528d35eSDavid Howells 	struct dentry *dentry = path->dentry;
9923a60a168STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
9933a60a168STyler Hicks 	int rc = 0;
9943a60a168STyler Hicks 
9953a60a168STyler Hicks 	mount_crypt_stat = &ecryptfs_superblock_to_private(
9963a60a168STyler Hicks 						dentry->d_sb)->mount_crypt_stat;
9970d56a451SChristian Brauner 	generic_fillattr(&init_user_ns, d_inode(dentry), stat);
9983a60a168STyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
9993a60a168STyler Hicks 		char *target;
10003a60a168STyler Hicks 		size_t targetsiz;
10013a60a168STyler Hicks 
1002b22e8fedSAl Viro 		target = ecryptfs_readlink_lower(dentry, &targetsiz);
1003b22e8fedSAl Viro 		if (!IS_ERR(target)) {
10043a60a168STyler Hicks 			kfree(target);
10053a60a168STyler Hicks 			stat->size = targetsiz;
1006b22e8fedSAl Viro 		} else {
1007b22e8fedSAl Viro 			rc = PTR_ERR(target);
10083a60a168STyler Hicks 		}
10093a60a168STyler Hicks 	}
10103a60a168STyler Hicks 	return rc;
10113a60a168STyler Hicks }
10123a60a168STyler Hicks 
1013549c7297SChristian Brauner static int ecryptfs_getattr(struct user_namespace *mnt_userns,
1014549c7297SChristian Brauner 			    const struct path *path, struct kstat *stat,
1015a528d35eSDavid Howells 			    u32 request_mask, unsigned int flags)
1016f8f484d1STyler Hicks {
1017a528d35eSDavid Howells 	struct dentry *dentry = path->dentry;
1018f8f484d1STyler Hicks 	struct kstat lower_stat;
1019f8f484d1STyler Hicks 	int rc;
1020f8f484d1STyler Hicks 
1021a528d35eSDavid Howells 	rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat,
1022a528d35eSDavid Howells 			 request_mask, flags);
1023f8f484d1STyler Hicks 	if (!rc) {
10242b0143b5SDavid Howells 		fsstack_copy_attr_all(d_inode(dentry),
10252b0143b5SDavid Howells 				      ecryptfs_inode_to_lower(d_inode(dentry)));
10260d56a451SChristian Brauner 		generic_fillattr(&init_user_ns, d_inode(dentry), stat);
1027f8f484d1STyler Hicks 		stat->blocks = lower_stat.blocks;
1028f8f484d1STyler Hicks 	}
1029f8f484d1STyler Hicks 	return rc;
1030f8f484d1STyler Hicks }
1031f8f484d1STyler Hicks 
1032dd2a3b7aSMichael Halcrow int
10333767e255SAl Viro ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
10343767e255SAl Viro 		  const char *name, const void *value,
1035237fead6SMichael Halcrow 		  size_t size, int flags)
1036237fead6SMichael Halcrow {
10375d6c3191SAndreas Gruenbacher 	int rc;
1038237fead6SMichael Halcrow 	struct dentry *lower_dentry;
10390b964446SMiklos Szeredi 	struct inode *lower_inode;
1040237fead6SMichael Halcrow 
1041237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
10420b964446SMiklos Szeredi 	lower_inode = d_inode(lower_dentry);
10430b964446SMiklos Szeredi 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1044cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1045237fead6SMichael Halcrow 		goto out;
1046237fead6SMichael Halcrow 	}
10470b964446SMiklos Szeredi 	inode_lock(lower_inode);
10487d6beb71SLinus Torvalds 	rc = __vfs_setxattr_locked(&init_user_ns, lower_dentry, name, value, size, flags, NULL);
10490b964446SMiklos Szeredi 	inode_unlock(lower_inode);
10503767e255SAl Viro 	if (!rc && inode)
10510b964446SMiklos Szeredi 		fsstack_copy_attr_all(inode, lower_inode);
1052237fead6SMichael Halcrow out:
1053237fead6SMichael Halcrow 	return rc;
1054237fead6SMichael Halcrow }
1055237fead6SMichael Halcrow 
1056dd2a3b7aSMichael Halcrow ssize_t
1057ce23e640SAl Viro ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
1058ce23e640SAl Viro 			const char *name, void *value, size_t size)
1059d7cdc5feSMichael Halcrow {
10605d6c3191SAndreas Gruenbacher 	int rc;
1061d7cdc5feSMichael Halcrow 
10625d6c3191SAndreas Gruenbacher 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1063cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1064d7cdc5feSMichael Halcrow 		goto out;
1065d7cdc5feSMichael Halcrow 	}
1066ce23e640SAl Viro 	inode_lock(lower_inode);
10675d6c3191SAndreas Gruenbacher 	rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
1068ce23e640SAl Viro 	inode_unlock(lower_inode);
1069d7cdc5feSMichael Halcrow out:
1070d7cdc5feSMichael Halcrow 	return rc;
1071d7cdc5feSMichael Halcrow }
1072d7cdc5feSMichael Halcrow 
10737896b631SAdrian Bunk static ssize_t
1074ce23e640SAl Viro ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
1075ce23e640SAl Viro 		  const char *name, void *value, size_t size)
1076237fead6SMichael Halcrow {
1077ce23e640SAl Viro 	return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1078ce23e640SAl Viro 				       ecryptfs_inode_to_lower(inode),
1079ce23e640SAl Viro 				       name, value, size);
1080237fead6SMichael Halcrow }
1081237fead6SMichael Halcrow 
1082237fead6SMichael Halcrow static ssize_t
1083237fead6SMichael Halcrow ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1084237fead6SMichael Halcrow {
1085237fead6SMichael Halcrow 	int rc = 0;
1086237fead6SMichael Halcrow 	struct dentry *lower_dentry;
1087237fead6SMichael Halcrow 
1088237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
10892b0143b5SDavid Howells 	if (!d_inode(lower_dentry)->i_op->listxattr) {
1090cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1091237fead6SMichael Halcrow 		goto out;
1092237fead6SMichael Halcrow 	}
10935955102cSAl Viro 	inode_lock(d_inode(lower_dentry));
10942b0143b5SDavid Howells 	rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
10955955102cSAl Viro 	inode_unlock(d_inode(lower_dentry));
1096237fead6SMichael Halcrow out:
1097237fead6SMichael Halcrow 	return rc;
1098237fead6SMichael Halcrow }
1099237fead6SMichael Halcrow 
11004b899da5SAndreas Gruenbacher static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
11014b899da5SAndreas Gruenbacher 				const char *name)
1102237fead6SMichael Halcrow {
11035d6c3191SAndreas Gruenbacher 	int rc;
1104237fead6SMichael Halcrow 	struct dentry *lower_dentry;
11054b899da5SAndreas Gruenbacher 	struct inode *lower_inode;
1106237fead6SMichael Halcrow 
1107237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
11084b899da5SAndreas Gruenbacher 	lower_inode = ecryptfs_inode_to_lower(inode);
11095d6c3191SAndreas Gruenbacher 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1110cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1111237fead6SMichael Halcrow 		goto out;
1112237fead6SMichael Halcrow 	}
11134b899da5SAndreas Gruenbacher 	inode_lock(lower_inode);
1114c7c7a1a1STycho Andersen 	rc = __vfs_removexattr(&init_user_ns, lower_dentry, name);
11154b899da5SAndreas Gruenbacher 	inode_unlock(lower_inode);
1116237fead6SMichael Halcrow out:
1117237fead6SMichael Halcrow 	return rc;
1118237fead6SMichael Halcrow }
1119237fead6SMichael Halcrow 
1120754661f1SArjan van de Ven const struct inode_operations ecryptfs_symlink_iops = {
11216b255391SAl Viro 	.get_link = ecryptfs_get_link,
1122237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1123237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
11243a60a168STyler Hicks 	.getattr = ecryptfs_getattr_link,
1125237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
1126237fead6SMichael Halcrow };
1127237fead6SMichael Halcrow 
1128754661f1SArjan van de Ven const struct inode_operations ecryptfs_dir_iops = {
1129237fead6SMichael Halcrow 	.create = ecryptfs_create,
1130237fead6SMichael Halcrow 	.lookup = ecryptfs_lookup,
1131237fead6SMichael Halcrow 	.link = ecryptfs_link,
1132237fead6SMichael Halcrow 	.unlink = ecryptfs_unlink,
1133237fead6SMichael Halcrow 	.symlink = ecryptfs_symlink,
1134237fead6SMichael Halcrow 	.mkdir = ecryptfs_mkdir,
1135237fead6SMichael Halcrow 	.rmdir = ecryptfs_rmdir,
1136237fead6SMichael Halcrow 	.mknod = ecryptfs_mknod,
1137237fead6SMichael Halcrow 	.rename = ecryptfs_rename,
1138237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1139237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
1140237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
1141237fead6SMichael Halcrow };
1142237fead6SMichael Halcrow 
1143754661f1SArjan van de Ven const struct inode_operations ecryptfs_main_iops = {
1144237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1145237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
1146f8f484d1STyler Hicks 	.getattr = ecryptfs_getattr,
1147237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
11484b899da5SAndreas Gruenbacher };
11494b899da5SAndreas Gruenbacher 
11504b899da5SAndreas Gruenbacher static int ecryptfs_xattr_get(const struct xattr_handler *handler,
11514b899da5SAndreas Gruenbacher 			      struct dentry *dentry, struct inode *inode,
11524b899da5SAndreas Gruenbacher 			      const char *name, void *buffer, size_t size)
11534b899da5SAndreas Gruenbacher {
11544b899da5SAndreas Gruenbacher 	return ecryptfs_getxattr(dentry, inode, name, buffer, size);
11554b899da5SAndreas Gruenbacher }
11564b899da5SAndreas Gruenbacher 
11574b899da5SAndreas Gruenbacher static int ecryptfs_xattr_set(const struct xattr_handler *handler,
1158e65ce2a5SChristian Brauner 			      struct user_namespace *mnt_userns,
11594b899da5SAndreas Gruenbacher 			      struct dentry *dentry, struct inode *inode,
11604b899da5SAndreas Gruenbacher 			      const char *name, const void *value, size_t size,
11614b899da5SAndreas Gruenbacher 			      int flags)
11624b899da5SAndreas Gruenbacher {
11634b899da5SAndreas Gruenbacher 	if (value)
11644b899da5SAndreas Gruenbacher 		return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
11654b899da5SAndreas Gruenbacher 	else {
11664b899da5SAndreas Gruenbacher 		BUG_ON(flags != XATTR_REPLACE);
11674b899da5SAndreas Gruenbacher 		return ecryptfs_removexattr(dentry, inode, name);
11684b899da5SAndreas Gruenbacher 	}
11694b899da5SAndreas Gruenbacher }
11704b899da5SAndreas Gruenbacher 
1171c036061bSYueHaibing static const struct xattr_handler ecryptfs_xattr_handler = {
11724b899da5SAndreas Gruenbacher 	.prefix = "",  /* match anything */
11734b899da5SAndreas Gruenbacher 	.get = ecryptfs_xattr_get,
11744b899da5SAndreas Gruenbacher 	.set = ecryptfs_xattr_set,
11754b899da5SAndreas Gruenbacher };
11764b899da5SAndreas Gruenbacher 
11774b899da5SAndreas Gruenbacher const struct xattr_handler *ecryptfs_xattr_handlers[] = {
11784b899da5SAndreas Gruenbacher 	&ecryptfs_xattr_handler,
11794b899da5SAndreas Gruenbacher 	NULL
1180237fead6SMichael Halcrow };
1181