xref: /openbmc/linux/fs/ecryptfs/inode.c (revision b2648d51)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2237fead6SMichael Halcrow /**
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 
24*b2648d51SAl Viro static int lock_parent(struct dentry *dentry,
25*b2648d51SAl Viro 		       struct dentry **lower_dentry,
26*b2648d51SAl Viro 		       struct inode **lower_dir)
27237fead6SMichael Halcrow {
28*b2648d51SAl Viro 	struct dentry *lower_dir_dentry;
29237fead6SMichael Halcrow 
30*b2648d51SAl Viro 	lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
31*b2648d51SAl Viro 	*lower_dir = d_inode(lower_dir_dentry);
32*b2648d51SAl Viro 	*lower_dentry = ecryptfs_dentry_to_lower(dentry);
33237fead6SMichael Halcrow 
34*b2648d51SAl Viro 	inode_lock_nested(*lower_dir, I_MUTEX_PARENT);
35*b2648d51SAl Viro 	return (*lower_dentry)->d_parent == lower_dir_dentry ? 0 : -EINVAL;
36237fead6SMichael Halcrow }
37237fead6SMichael Halcrow 
38c4f79073STyler Hicks static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
39c4f79073STyler Hicks {
40c4cf3ba4SHimangi Saraogi 	return ecryptfs_inode_to_lower(inode) == lower_inode;
41c4f79073STyler Hicks }
42c4f79073STyler Hicks 
435ccf9203STyler Hicks static int ecryptfs_inode_set(struct inode *inode, void *opaque)
44c4f79073STyler Hicks {
455ccf9203STyler Hicks 	struct inode *lower_inode = opaque;
465ccf9203STyler Hicks 
475ccf9203STyler Hicks 	ecryptfs_set_inode_lower(inode, lower_inode);
485ccf9203STyler Hicks 	fsstack_copy_attr_all(inode, lower_inode);
495ccf9203STyler Hicks 	/* i_size will be overwritten for encrypted regular files */
505ccf9203STyler Hicks 	fsstack_copy_inode_size(inode, lower_inode);
515ccf9203STyler Hicks 	inode->i_ino = lower_inode->i_ino;
52c4f79073STyler Hicks 	inode->i_mapping->a_ops = &ecryptfs_aops;
535ccf9203STyler Hicks 
545ccf9203STyler Hicks 	if (S_ISLNK(inode->i_mode))
555ccf9203STyler Hicks 		inode->i_op = &ecryptfs_symlink_iops;
565ccf9203STyler Hicks 	else if (S_ISDIR(inode->i_mode))
575ccf9203STyler Hicks 		inode->i_op = &ecryptfs_dir_iops;
585ccf9203STyler Hicks 	else
595ccf9203STyler Hicks 		inode->i_op = &ecryptfs_main_iops;
605ccf9203STyler Hicks 
615ccf9203STyler Hicks 	if (S_ISDIR(inode->i_mode))
625ccf9203STyler Hicks 		inode->i_fop = &ecryptfs_dir_fops;
635ccf9203STyler Hicks 	else if (special_file(inode->i_mode))
645ccf9203STyler Hicks 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
655ccf9203STyler Hicks 	else
665ccf9203STyler Hicks 		inode->i_fop = &ecryptfs_main_fops;
675ccf9203STyler Hicks 
68c4f79073STyler Hicks 	return 0;
69c4f79073STyler Hicks }
70c4f79073STyler Hicks 
715ccf9203STyler Hicks static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
725ccf9203STyler Hicks 					  struct super_block *sb)
735ccf9203STyler Hicks {
745ccf9203STyler Hicks 	struct inode *inode;
755ccf9203STyler Hicks 
765ccf9203STyler Hicks 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
775ccf9203STyler Hicks 		return ERR_PTR(-EXDEV);
785ccf9203STyler Hicks 	if (!igrab(lower_inode))
795ccf9203STyler Hicks 		return ERR_PTR(-ESTALE);
805ccf9203STyler Hicks 	inode = iget5_locked(sb, (unsigned long)lower_inode,
815ccf9203STyler Hicks 			     ecryptfs_inode_test, ecryptfs_inode_set,
825ccf9203STyler Hicks 			     lower_inode);
835ccf9203STyler Hicks 	if (!inode) {
845ccf9203STyler Hicks 		iput(lower_inode);
855ccf9203STyler Hicks 		return ERR_PTR(-EACCES);
865ccf9203STyler Hicks 	}
875ccf9203STyler Hicks 	if (!(inode->i_state & I_NEW))
885ccf9203STyler Hicks 		iput(lower_inode);
895ccf9203STyler Hicks 
905ccf9203STyler Hicks 	return inode;
915ccf9203STyler Hicks }
925ccf9203STyler Hicks 
93c4f79073STyler Hicks struct inode *ecryptfs_get_inode(struct inode *lower_inode,
94c4f79073STyler Hicks 				 struct super_block *sb)
95c4f79073STyler Hicks {
965ccf9203STyler Hicks 	struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
97c4f79073STyler Hicks 
985ccf9203STyler Hicks 	if (!IS_ERR(inode) && (inode->i_state & I_NEW))
99c4f79073STyler Hicks 		unlock_new_inode(inode);
1005ccf9203STyler Hicks 
101c4f79073STyler Hicks 	return inode;
102c4f79073STyler Hicks }
103c4f79073STyler Hicks 
104c4f79073STyler Hicks /**
105c4f79073STyler Hicks  * ecryptfs_interpose
106c4f79073STyler Hicks  * @lower_dentry: Existing dentry in the lower filesystem
107c4f79073STyler Hicks  * @dentry: ecryptfs' dentry
108c4f79073STyler Hicks  * @sb: ecryptfs's super_block
109c4f79073STyler Hicks  *
110c4f79073STyler Hicks  * Interposes upper and lower dentries.
111c4f79073STyler Hicks  *
112c4f79073STyler Hicks  * Returns zero on success; non-zero otherwise
113c4f79073STyler Hicks  */
114c4f79073STyler Hicks static int ecryptfs_interpose(struct dentry *lower_dentry,
1155ccf9203STyler Hicks 			      struct dentry *dentry, struct super_block *sb)
116c4f79073STyler Hicks {
1172b0143b5SDavid Howells 	struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
1185ccf9203STyler Hicks 
119c4f79073STyler Hicks 	if (IS_ERR(inode))
120c4f79073STyler Hicks 		return PTR_ERR(inode);
121c4f79073STyler Hicks 	d_instantiate(dentry, inode);
1225ccf9203STyler Hicks 
123c4f79073STyler Hicks 	return 0;
124c4f79073STyler Hicks }
125c4f79073STyler Hicks 
1268bc2d3cfSTyler Hicks static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
1278bc2d3cfSTyler Hicks 			      struct inode *inode)
1288bc2d3cfSTyler Hicks {
129*b2648d51SAl Viro 	struct dentry *lower_dentry;
130*b2648d51SAl Viro 	struct inode *lower_dir;
1318bc2d3cfSTyler Hicks 	int rc;
1328bc2d3cfSTyler Hicks 
133*b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
134bcf0d9d4SAl Viro 	dget(lower_dentry);	// don't even try to make the lower negative
135*b2648d51SAl Viro 	if (!rc) {
136*b2648d51SAl Viro 		if (d_unhashed(lower_dentry))
137bcf0d9d4SAl Viro 			rc = -EINVAL;
138bcf0d9d4SAl Viro 		else
139*b2648d51SAl Viro 			rc = vfs_unlink(&init_user_ns, lower_dir, lower_dentry,
1406521f891SChristian Brauner 					NULL);
141*b2648d51SAl Viro 	}
1428bc2d3cfSTyler Hicks 	if (rc) {
1438bc2d3cfSTyler Hicks 		printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
1448bc2d3cfSTyler Hicks 		goto out_unlock;
1458bc2d3cfSTyler Hicks 	}
146*b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
1478bc2d3cfSTyler Hicks 	set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
1488bc2d3cfSTyler Hicks 	inode->i_ctime = dir->i_ctime;
1498bc2d3cfSTyler Hicks out_unlock:
1508bc2d3cfSTyler Hicks 	dput(lower_dentry);
151*b2648d51SAl Viro 	inode_unlock(lower_dir);
152bcf0d9d4SAl Viro 	if (!rc)
153bcf0d9d4SAl Viro 		d_drop(dentry);
1548bc2d3cfSTyler Hicks 	return rc;
1558bc2d3cfSTyler Hicks }
1568bc2d3cfSTyler Hicks 
157237fead6SMichael Halcrow /**
158237fead6SMichael Halcrow  * ecryptfs_do_create
159237fead6SMichael Halcrow  * @directory_inode: inode of the new file's dentry's parent in ecryptfs
160237fead6SMichael Halcrow  * @ecryptfs_dentry: New file's dentry in ecryptfs
161237fead6SMichael Halcrow  * @mode: The mode of the new file
162237fead6SMichael Halcrow  *
163237fead6SMichael Halcrow  * Creates the underlying file and the eCryptfs inode which will link to
164237fead6SMichael Halcrow  * it. It will also update the eCryptfs directory inode to mimic the
165237fead6SMichael Halcrow  * stat of the lower directory inode.
166237fead6SMichael Halcrow  *
167b59db43aSTyler Hicks  * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
168237fead6SMichael Halcrow  */
169b59db43aSTyler Hicks static struct inode *
170237fead6SMichael Halcrow ecryptfs_do_create(struct inode *directory_inode,
171175a4eb7SAl Viro 		   struct dentry *ecryptfs_dentry, umode_t mode)
172237fead6SMichael Halcrow {
173237fead6SMichael Halcrow 	int rc;
174237fead6SMichael Halcrow 	struct dentry *lower_dentry;
175*b2648d51SAl Viro 	struct inode *lower_dir;
176b59db43aSTyler Hicks 	struct inode *inode;
177237fead6SMichael Halcrow 
178*b2648d51SAl Viro 	rc = lock_parent(ecryptfs_dentry, &lower_dentry, &lower_dir);
179*b2648d51SAl Viro 	if (!rc)
180*b2648d51SAl Viro 		rc = vfs_create(&init_user_ns, lower_dir,
181*b2648d51SAl Viro 				lower_dentry, mode, true);
1824981e081SMichael Halcrow 	if (rc) {
183caeeeecfSMichael Halcrow 		printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
18418d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
185b59db43aSTyler Hicks 		inode = ERR_PTR(rc);
186237fead6SMichael Halcrow 		goto out_lock;
187237fead6SMichael Halcrow 	}
1882b0143b5SDavid Howells 	inode = __ecryptfs_get_inode(d_inode(lower_dentry),
1895ccf9203STyler Hicks 				     directory_inode->i_sb);
1908bc2d3cfSTyler Hicks 	if (IS_ERR(inode)) {
191*b2648d51SAl Viro 		vfs_unlink(&init_user_ns, lower_dir, lower_dentry, NULL);
192237fead6SMichael Halcrow 		goto out_lock;
1938bc2d3cfSTyler Hicks 	}
194*b2648d51SAl Viro 	fsstack_copy_attr_times(directory_inode, lower_dir);
195*b2648d51SAl Viro 	fsstack_copy_inode_size(directory_inode, lower_dir);
196237fead6SMichael Halcrow out_lock:
197*b2648d51SAl Viro 	inode_unlock(lower_dir);
198b59db43aSTyler Hicks 	return inode;
199237fead6SMichael Halcrow }
200237fead6SMichael Halcrow 
201237fead6SMichael Halcrow /**
202237fead6SMichael Halcrow  * ecryptfs_initialize_file
203237fead6SMichael Halcrow  *
204237fead6SMichael Halcrow  * Cause the file to be changed from a basic empty file to an ecryptfs
205237fead6SMichael Halcrow  * file with a header and first data page.
206237fead6SMichael Halcrow  *
207237fead6SMichael Halcrow  * Returns zero on success
208237fead6SMichael Halcrow  */
209e3ccaa97STyler Hicks int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
210b59db43aSTyler Hicks 			     struct inode *ecryptfs_inode)
211237fead6SMichael Halcrow {
212d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
213b59db43aSTyler Hicks 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
214237fead6SMichael Halcrow 	int rc = 0;
215237fead6SMichael Halcrow 
216b59db43aSTyler Hicks 	if (S_ISDIR(ecryptfs_inode->i_mode)) {
217237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
218e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
219d7cdc5feSMichael Halcrow 		goto out;
220237fead6SMichael Halcrow 	}
221237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
222b59db43aSTyler Hicks 	rc = ecryptfs_new_file_context(ecryptfs_inode);
223237fead6SMichael Halcrow 	if (rc) {
224d7cdc5feSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error creating new file "
225d7cdc5feSMichael Halcrow 				"context; rc = [%d]\n", rc);
226d7cdc5feSMichael Halcrow 		goto out;
227237fead6SMichael Halcrow 	}
228b59db43aSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
229391b52f9SMichael Halcrow 	if (rc) {
230391b52f9SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
231332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2329e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2339e78d14aSDavid Howells 			ecryptfs_dentry, rc);
234391b52f9SMichael Halcrow 		goto out;
235391b52f9SMichael Halcrow 	}
236b59db43aSTyler Hicks 	rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
237332ab16fSTyler Hicks 	if (rc)
238d7cdc5feSMichael Halcrow 		printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
239b59db43aSTyler Hicks 	ecryptfs_put_lower_file(ecryptfs_inode);
240237fead6SMichael Halcrow out:
241237fead6SMichael Halcrow 	return rc;
242237fead6SMichael Halcrow }
243237fead6SMichael Halcrow 
244237fead6SMichael Halcrow /**
245237fead6SMichael Halcrow  * ecryptfs_create
246237fead6SMichael Halcrow  * @dir: The inode of the directory in which to create the file.
247237fead6SMichael Halcrow  * @dentry: The eCryptfs dentry
248237fead6SMichael Halcrow  * @mode: The mode of the new file.
249237fead6SMichael Halcrow  *
250237fead6SMichael Halcrow  * Creates a new file.
251237fead6SMichael Halcrow  *
252237fead6SMichael Halcrow  * Returns zero on success; non-zero on error condition
253237fead6SMichael Halcrow  */
254237fead6SMichael Halcrow static int
255549c7297SChristian Brauner ecryptfs_create(struct user_namespace *mnt_userns,
256549c7297SChristian Brauner 		struct inode *directory_inode, struct dentry *ecryptfs_dentry,
257ebfc3b49SAl Viro 		umode_t mode, bool excl)
258237fead6SMichael Halcrow {
259b59db43aSTyler Hicks 	struct inode *ecryptfs_inode;
260237fead6SMichael Halcrow 	int rc;
261237fead6SMichael Halcrow 
262b59db43aSTyler Hicks 	ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
263b59db43aSTyler Hicks 					    mode);
264a1c83681SViresh Kumar 	if (IS_ERR(ecryptfs_inode)) {
265237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Failed to create file in"
266237fead6SMichael Halcrow 				"lower filesystem\n");
267b59db43aSTyler Hicks 		rc = PTR_ERR(ecryptfs_inode);
268237fead6SMichael Halcrow 		goto out;
269237fead6SMichael Halcrow 	}
270237fead6SMichael Halcrow 	/* At this point, a file exists on "disk"; we need to make sure
271237fead6SMichael Halcrow 	 * that this on disk file is prepared to be an ecryptfs file */
272b59db43aSTyler Hicks 	rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
273b59db43aSTyler Hicks 	if (rc) {
2748bc2d3cfSTyler Hicks 		ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
2758bc2d3cfSTyler Hicks 				   ecryptfs_inode);
2760e81ba23SAl Viro 		iget_failed(ecryptfs_inode);
277b59db43aSTyler Hicks 		goto out;
278b59db43aSTyler Hicks 	}
2791e2e547aSAl Viro 	d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
280237fead6SMichael Halcrow out:
281237fead6SMichael Halcrow 	return rc;
282237fead6SMichael Halcrow }
283237fead6SMichael Halcrow 
284778aeb42STyler Hicks static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
285237fead6SMichael Halcrow {
2862aac0cf8STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
287778aeb42STyler Hicks 	int rc;
288237fead6SMichael Halcrow 
289778aeb42STyler Hicks 	rc = ecryptfs_get_lower_file(dentry, inode);
290391b52f9SMichael Halcrow 	if (rc) {
291391b52f9SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
292332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2939e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2949e78d14aSDavid Howells 			dentry, rc);
295778aeb42STyler Hicks 		return rc;
296391b52f9SMichael Halcrow 	}
297778aeb42STyler Hicks 
2983b06b3ebSTyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
2992aac0cf8STyler Hicks 	/* TODO: lock for crypt_stat comparison */
3002aac0cf8STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
3012aac0cf8STyler Hicks 		ecryptfs_set_default_sizes(crypt_stat);
302778aeb42STyler Hicks 
303778aeb42STyler Hicks 	rc = ecryptfs_read_and_validate_header_region(inode);
304778aeb42STyler Hicks 	ecryptfs_put_lower_file(inode);
305237fead6SMichael Halcrow 	if (rc) {
306778aeb42STyler Hicks 		rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
307778aeb42STyler Hicks 		if (!rc)
308dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
309dd2a3b7aSMichael Halcrow 	}
310778aeb42STyler Hicks 
311778aeb42STyler Hicks 	/* Must return 0 to allow non-eCryptfs files to be looked up, too */
312778aeb42STyler Hicks 	return 0;
313778aeb42STyler Hicks }
314778aeb42STyler Hicks 
315778aeb42STyler Hicks /**
316778aeb42STyler Hicks  * ecryptfs_lookup_interpose - Dentry interposition for a lookup
317778aeb42STyler Hicks  */
318b1168a92SAl Viro static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
319b1168a92SAl Viro 				     struct dentry *lower_dentry)
320778aeb42STyler Hicks {
321762c6968SAl Viro 	struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
322e72b9dd6SAl Viro 	struct inode *inode, *lower_inode;
323778aeb42STyler Hicks 	struct ecryptfs_dentry_info *dentry_info;
324778aeb42STyler Hicks 	int rc = 0;
325778aeb42STyler Hicks 
326778aeb42STyler Hicks 	dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
327778aeb42STyler Hicks 	if (!dentry_info) {
328237fead6SMichael Halcrow 		dput(lower_dentry);
329b1168a92SAl Viro 		return ERR_PTR(-ENOMEM);
330778aeb42STyler Hicks 	}
3310b1d9011SAl Viro 
332b1168a92SAl Viro 	fsstack_copy_attr_atime(d_inode(dentry->d_parent),
333762c6968SAl Viro 				d_inode(path->dentry));
33484d08fa8SAl Viro 	BUG_ON(!d_count(lower_dentry));
3350b1d9011SAl Viro 
3360b1d9011SAl Viro 	ecryptfs_set_dentry_private(dentry, dentry_info);
337762c6968SAl Viro 	dentry_info->lower_path.mnt = mntget(path->mnt);
33892dd1230SAl Viro 	dentry_info->lower_path.dentry = lower_dentry;
339778aeb42STyler Hicks 
340e72b9dd6SAl Viro 	/*
341e72b9dd6SAl Viro 	 * negative dentry can go positive under us here - its parent is not
342e72b9dd6SAl Viro 	 * locked.  That's OK and that could happen just as we return from
343e72b9dd6SAl Viro 	 * ecryptfs_lookup() anyway.  Just need to be careful and fetch
344e72b9dd6SAl Viro 	 * ->d_inode only once - it's not stable here.
345e72b9dd6SAl Viro 	 */
346e72b9dd6SAl Viro 	lower_inode = READ_ONCE(lower_dentry->d_inode);
347e72b9dd6SAl Viro 
348e72b9dd6SAl Viro 	if (!lower_inode) {
349778aeb42STyler Hicks 		/* We want to add because we couldn't find in lower */
350778aeb42STyler Hicks 		d_add(dentry, NULL);
351b1168a92SAl Viro 		return NULL;
352778aeb42STyler Hicks 	}
353b1168a92SAl Viro 	inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
354778aeb42STyler Hicks 	if (IS_ERR(inode)) {
355778aeb42STyler Hicks 		printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
356778aeb42STyler Hicks 		       __func__, PTR_ERR(inode));
357b1168a92SAl Viro 		return ERR_CAST(inode);
358778aeb42STyler Hicks 	}
359778aeb42STyler Hicks 	if (S_ISREG(inode->i_mode)) {
360778aeb42STyler Hicks 		rc = ecryptfs_i_size_read(dentry, inode);
361778aeb42STyler Hicks 		if (rc) {
362778aeb42STyler Hicks 			make_bad_inode(inode);
363b1168a92SAl Viro 			return ERR_PTR(rc);
364778aeb42STyler Hicks 		}
365778aeb42STyler Hicks 	}
366778aeb42STyler Hicks 
367778aeb42STyler Hicks 	if (inode->i_state & I_NEW)
368778aeb42STyler Hicks 		unlock_new_inode(inode);
369b1168a92SAl Viro 	return d_splice_alias(inode, dentry);
370addd65adSMichael Halcrow }
371addd65adSMichael Halcrow 
372addd65adSMichael Halcrow /**
373addd65adSMichael Halcrow  * ecryptfs_lookup
374addd65adSMichael Halcrow  * @ecryptfs_dir_inode: The eCryptfs directory inode
375addd65adSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
37689076bc3SAl Viro  * @flags: lookup flags
377addd65adSMichael Halcrow  *
378addd65adSMichael Halcrow  * Find a file on disk. If the file does not exist, then we'll add it to the
379addd65adSMichael Halcrow  * dentry cache and continue on to read it from the disk.
380addd65adSMichael Halcrow  */
381addd65adSMichael Halcrow static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
382addd65adSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
38300cd8dd3SAl Viro 				      unsigned int flags)
384addd65adSMichael Halcrow {
385addd65adSMichael Halcrow 	char *encrypted_and_encoded_name = NULL;
38688ae4ab9SAl Viro 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
387addd65adSMichael Halcrow 	struct dentry *lower_dir_dentry, *lower_dentry;
38888ae4ab9SAl Viro 	const char *name = ecryptfs_dentry->d_name.name;
38988ae4ab9SAl Viro 	size_t len = ecryptfs_dentry->d_name.len;
390b1168a92SAl Viro 	struct dentry *res;
391addd65adSMichael Halcrow 	int rc = 0;
392addd65adSMichael Halcrow 
393addd65adSMichael Halcrow 	lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
39488ae4ab9SAl Viro 
395addd65adSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
396addd65adSMichael Halcrow 				ecryptfs_dentry->d_sb)->mount_crypt_stat;
397ab13a921SGuenter Roeck 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
398addd65adSMichael Halcrow 		rc = ecryptfs_encrypt_and_encode_filename(
39988ae4ab9SAl Viro 			&encrypted_and_encoded_name, &len,
40088ae4ab9SAl Viro 			mount_crypt_stat, name, len);
401addd65adSMichael Halcrow 		if (rc) {
402addd65adSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encrypt and encode "
403addd65adSMichael Halcrow 			       "filename; rc = [%d]\n", __func__, rc);
404237fead6SMichael Halcrow 			return ERR_PTR(rc);
405237fead6SMichael Halcrow 		}
40688ae4ab9SAl Viro 		name = encrypted_and_encoded_name;
40788ae4ab9SAl Viro 	}
40888ae4ab9SAl Viro 
40988ae4ab9SAl Viro 	lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
410237fead6SMichael Halcrow 	if (IS_ERR(lower_dentry)) {
411237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
412b1168a92SAl Viro 				"[%ld] on lower_dentry = [%s]\n", __func__,
413b1168a92SAl Viro 				PTR_ERR(lower_dentry),
41488ae4ab9SAl Viro 				name);
415b1168a92SAl Viro 		res = ERR_CAST(lower_dentry);
41688ae4ab9SAl Viro 	} else {
417b1168a92SAl Viro 		res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
41888ae4ab9SAl Viro 	}
419237fead6SMichael Halcrow 	kfree(encrypted_and_encoded_name);
420b1168a92SAl Viro 	return res;
421237fead6SMichael Halcrow }
422237fead6SMichael Halcrow 
423237fead6SMichael Halcrow static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
424237fead6SMichael Halcrow 			 struct dentry *new_dentry)
425237fead6SMichael Halcrow {
426237fead6SMichael Halcrow 	struct dentry *lower_old_dentry;
427237fead6SMichael Halcrow 	struct dentry *lower_new_dentry;
428*b2648d51SAl Viro 	struct inode *lower_dir;
429237fead6SMichael Halcrow 	u64 file_size_save;
430237fead6SMichael Halcrow 	int rc;
431237fead6SMichael Halcrow 
4322b0143b5SDavid Howells 	file_size_save = i_size_read(d_inode(old_dentry));
433237fead6SMichael Halcrow 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
434*b2648d51SAl Viro 	rc = lock_parent(new_dentry, &lower_new_dentry, &lower_dir);
435*b2648d51SAl Viro 	if (!rc)
436*b2648d51SAl Viro 		rc = vfs_link(lower_old_dentry, &init_user_ns, lower_dir,
437*b2648d51SAl Viro 			      lower_new_dentry, NULL);
4382b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_new_dentry))
439237fead6SMichael Halcrow 		goto out_lock;
4405ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
441237fead6SMichael Halcrow 	if (rc)
442237fead6SMichael Halcrow 		goto out_lock;
443*b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
444*b2648d51SAl Viro 	fsstack_copy_inode_size(dir, lower_dir);
4452b0143b5SDavid Howells 	set_nlink(d_inode(old_dentry),
4462b0143b5SDavid Howells 		  ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
4472b0143b5SDavid Howells 	i_size_write(d_inode(new_dentry), file_size_save);
448237fead6SMichael Halcrow out_lock:
449*b2648d51SAl Viro 	inode_unlock(lower_dir);
450237fead6SMichael Halcrow 	return rc;
451237fead6SMichael Halcrow }
452237fead6SMichael Halcrow 
453237fead6SMichael Halcrow static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
454237fead6SMichael Halcrow {
4552b0143b5SDavid Howells 	return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
456237fead6SMichael Halcrow }
457237fead6SMichael Halcrow 
458549c7297SChristian Brauner static int ecryptfs_symlink(struct user_namespace *mnt_userns,
459549c7297SChristian Brauner 			    struct inode *dir, struct dentry *dentry,
460237fead6SMichael Halcrow 			    const char *symname)
461237fead6SMichael Halcrow {
462237fead6SMichael Halcrow 	int rc;
463237fead6SMichael Halcrow 	struct dentry *lower_dentry;
464*b2648d51SAl Viro 	struct inode *lower_dir;
465237fead6SMichael Halcrow 	char *encoded_symname;
466addd65adSMichael Halcrow 	size_t encoded_symlen;
467addd65adSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
468237fead6SMichael Halcrow 
469*b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
470*b2648d51SAl Viro 	if (rc)
471*b2648d51SAl Viro 		goto out_lock;
472addd65adSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
473addd65adSMichael Halcrow 		dir->i_sb)->mount_crypt_stat;
474addd65adSMichael Halcrow 	rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
475addd65adSMichael Halcrow 						  &encoded_symlen,
476addd65adSMichael Halcrow 						  mount_crypt_stat, symname,
477addd65adSMichael Halcrow 						  strlen(symname));
478addd65adSMichael Halcrow 	if (rc)
479237fead6SMichael Halcrow 		goto out_lock;
480*b2648d51SAl Viro 	rc = vfs_symlink(&init_user_ns, lower_dir, lower_dentry,
481db2e747bSMiklos Szeredi 			 encoded_symname);
482237fead6SMichael Halcrow 	kfree(encoded_symname);
4832b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
484237fead6SMichael Halcrow 		goto out_lock;
4855ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
486237fead6SMichael Halcrow 	if (rc)
487237fead6SMichael Halcrow 		goto out_lock;
488*b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
489*b2648d51SAl Viro 	fsstack_copy_inode_size(dir, lower_dir);
490237fead6SMichael Halcrow out_lock:
491*b2648d51SAl Viro 	inode_unlock(lower_dir);
4922b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
493237fead6SMichael Halcrow 		d_drop(dentry);
494237fead6SMichael Halcrow 	return rc;
495237fead6SMichael Halcrow }
496237fead6SMichael Halcrow 
497549c7297SChristian Brauner static int ecryptfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
498549c7297SChristian Brauner 			  struct dentry *dentry, umode_t mode)
499237fead6SMichael Halcrow {
500237fead6SMichael Halcrow 	int rc;
501237fead6SMichael Halcrow 	struct dentry *lower_dentry;
502*b2648d51SAl Viro 	struct inode *lower_dir;
503237fead6SMichael Halcrow 
504*b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
505*b2648d51SAl Viro 	if (!rc)
506*b2648d51SAl Viro 		rc = vfs_mkdir(&init_user_ns, lower_dir,
507*b2648d51SAl Viro 			       lower_dentry, mode);
5082b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
509237fead6SMichael Halcrow 		goto out;
5105ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
511237fead6SMichael Halcrow 	if (rc)
512237fead6SMichael Halcrow 		goto out;
513*b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
514*b2648d51SAl Viro 	fsstack_copy_inode_size(dir, lower_dir);
515*b2648d51SAl Viro 	set_nlink(dir, lower_dir->i_nlink);
516237fead6SMichael Halcrow out:
517*b2648d51SAl Viro 	inode_unlock(lower_dir);
5182b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
519237fead6SMichael Halcrow 		d_drop(dentry);
520237fead6SMichael Halcrow 	return rc;
521237fead6SMichael Halcrow }
522237fead6SMichael Halcrow 
523237fead6SMichael Halcrow static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
524237fead6SMichael Halcrow {
525237fead6SMichael Halcrow 	struct dentry *lower_dentry;
526*b2648d51SAl Viro 	struct inode *lower_dir;
52745ec4abaSMichael Halcrow 	int rc;
528237fead6SMichael Halcrow 
529*b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
530bcf0d9d4SAl Viro 	dget(lower_dentry);	// don't even try to make the lower negative
531*b2648d51SAl Viro 	if (!rc) {
532*b2648d51SAl Viro 		if (d_unhashed(lower_dentry))
533bcf0d9d4SAl Viro 			rc = -EINVAL;
534bcf0d9d4SAl Viro 		else
535*b2648d51SAl Viro 			rc = vfs_rmdir(&init_user_ns, lower_dir, lower_dentry);
536*b2648d51SAl Viro 	}
537bcf0d9d4SAl Viro 	if (!rc) {
5382b0143b5SDavid Howells 		clear_nlink(d_inode(dentry));
539*b2648d51SAl Viro 		fsstack_copy_attr_times(dir, lower_dir);
540*b2648d51SAl Viro 		set_nlink(dir, lower_dir->i_nlink);
541bcf0d9d4SAl Viro 	}
542bcf0d9d4SAl Viro 	dput(lower_dentry);
543*b2648d51SAl Viro 	inode_unlock(lower_dir);
544237fead6SMichael Halcrow 	if (!rc)
545237fead6SMichael Halcrow 		d_drop(dentry);
546237fead6SMichael Halcrow 	return rc;
547237fead6SMichael Halcrow }
548237fead6SMichael Halcrow 
549237fead6SMichael Halcrow static int
550549c7297SChristian Brauner ecryptfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
551549c7297SChristian Brauner 	       struct dentry *dentry, umode_t mode, dev_t dev)
552237fead6SMichael Halcrow {
553237fead6SMichael Halcrow 	int rc;
554237fead6SMichael Halcrow 	struct dentry *lower_dentry;
555*b2648d51SAl Viro 	struct inode *lower_dir;
556237fead6SMichael Halcrow 
557*b2648d51SAl Viro 	rc = lock_parent(dentry, &lower_dentry, &lower_dir);
558*b2648d51SAl Viro 	if (!rc)
559*b2648d51SAl Viro 		rc = vfs_mknod(&init_user_ns, lower_dir,
560*b2648d51SAl Viro 			       lower_dentry, mode, dev);
5612b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
562237fead6SMichael Halcrow 		goto out;
5635ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
564237fead6SMichael Halcrow 	if (rc)
565237fead6SMichael Halcrow 		goto out;
566*b2648d51SAl Viro 	fsstack_copy_attr_times(dir, lower_dir);
567*b2648d51SAl Viro 	fsstack_copy_inode_size(dir, lower_dir);
568237fead6SMichael Halcrow out:
569*b2648d51SAl Viro 	inode_unlock(lower_dir);
5702b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
571237fead6SMichael Halcrow 		d_drop(dentry);
572237fead6SMichael Halcrow 	return rc;
573237fead6SMichael Halcrow }
574237fead6SMichael Halcrow 
575237fead6SMichael Halcrow static int
576549c7297SChristian Brauner ecryptfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
577549c7297SChristian Brauner 		struct dentry *old_dentry, struct inode *new_dir,
578549c7297SChristian Brauner 		struct dentry *new_dentry, unsigned int flags)
579237fead6SMichael Halcrow {
580237fead6SMichael Halcrow 	int rc;
581237fead6SMichael Halcrow 	struct dentry *lower_old_dentry;
582237fead6SMichael Halcrow 	struct dentry *lower_new_dentry;
583237fead6SMichael Halcrow 	struct dentry *lower_old_dir_dentry;
584237fead6SMichael Halcrow 	struct dentry *lower_new_dir_dentry;
585bcf0d9d4SAl Viro 	struct dentry *trap;
5868335eafcSTyler Hicks 	struct inode *target_inode;
5879fe61450SChristian Brauner 	struct renamedata rd = {};
588237fead6SMichael Halcrow 
5891cd66c93SMiklos Szeredi 	if (flags)
5901cd66c93SMiklos Szeredi 		return -EINVAL;
5911cd66c93SMiklos Szeredi 
592bcf0d9d4SAl Viro 	lower_old_dir_dentry = ecryptfs_dentry_to_lower(old_dentry->d_parent);
593bcf0d9d4SAl Viro 	lower_new_dir_dentry = ecryptfs_dentry_to_lower(new_dentry->d_parent);
594bcf0d9d4SAl Viro 
595237fead6SMichael Halcrow 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
596237fead6SMichael Halcrow 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
597bcf0d9d4SAl Viro 
5982b0143b5SDavid Howells 	target_inode = d_inode(new_dentry);
599bcf0d9d4SAl Viro 
6000d132f73SErez Zadok 	trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
601bcf0d9d4SAl Viro 	dget(lower_new_dentry);
6020d132f73SErez Zadok 	rc = -EINVAL;
60374dd7c97SAl Viro 	if (lower_old_dentry->d_parent != lower_old_dir_dentry)
6040d132f73SErez Zadok 		goto out_lock;
60574dd7c97SAl Viro 	if (lower_new_dentry->d_parent != lower_new_dir_dentry)
60674dd7c97SAl Viro 		goto out_lock;
60774dd7c97SAl Viro 	if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry))
60874dd7c97SAl Viro 		goto out_lock;
60974dd7c97SAl Viro 	/* source should not be ancestor of target */
61074dd7c97SAl Viro 	if (trap == lower_old_dentry)
61174dd7c97SAl Viro 		goto out_lock;
6120d132f73SErez Zadok 	/* target should not be ancestor of source */
6130d132f73SErez Zadok 	if (trap == lower_new_dentry) {
6140d132f73SErez Zadok 		rc = -ENOTEMPTY;
6150d132f73SErez Zadok 		goto out_lock;
6160d132f73SErez Zadok 	}
6179fe61450SChristian Brauner 
6186521f891SChristian Brauner 	rd.old_mnt_userns	= &init_user_ns;
6199fe61450SChristian Brauner 	rd.old_dir		= d_inode(lower_old_dir_dentry);
6209fe61450SChristian Brauner 	rd.old_dentry		= lower_old_dentry;
6216521f891SChristian Brauner 	rd.new_mnt_userns	= &init_user_ns;
6229fe61450SChristian Brauner 	rd.new_dir		= d_inode(lower_new_dir_dentry);
6239fe61450SChristian Brauner 	rd.new_dentry		= lower_new_dentry;
6249fe61450SChristian Brauner 	rc = vfs_rename(&rd);
625237fead6SMichael Halcrow 	if (rc)
626237fead6SMichael Halcrow 		goto out_lock;
6278335eafcSTyler Hicks 	if (target_inode)
6288335eafcSTyler Hicks 		fsstack_copy_attr_all(target_inode,
6298335eafcSTyler Hicks 				      ecryptfs_inode_to_lower(target_inode));
6302b0143b5SDavid Howells 	fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
631237fead6SMichael Halcrow 	if (new_dir != old_dir)
6322b0143b5SDavid Howells 		fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
633237fead6SMichael Halcrow out_lock:
634237fead6SMichael Halcrow 	dput(lower_new_dentry);
635bcf0d9d4SAl Viro 	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
636237fead6SMichael Halcrow 	return rc;
637237fead6SMichael Halcrow }
638237fead6SMichael Halcrow 
639b22e8fedSAl Viro static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
640237fead6SMichael Halcrow {
6416c988f57SMiklos Szeredi 	DEFINE_DELAYED_CALL(done);
6423a60a168STyler Hicks 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
6436c988f57SMiklos Szeredi 	const char *link;
644b22e8fedSAl Viro 	char *buf;
645addd65adSMichael Halcrow 	int rc;
646237fead6SMichael Halcrow 
6476c988f57SMiklos Szeredi 	link = vfs_get_link(lower_dentry, &done);
6486c988f57SMiklos Szeredi 	if (IS_ERR(link))
6496c988f57SMiklos Szeredi 		return ERR_CAST(link);
6506c988f57SMiklos Szeredi 
651b22e8fedSAl Viro 	rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
6526c988f57SMiklos Szeredi 						  link, strlen(link));
6536c988f57SMiklos Szeredi 	do_delayed_call(&done);
6546c988f57SMiklos Szeredi 	if (rc)
6556c988f57SMiklos Szeredi 		return ERR_PTR(rc);
6566c988f57SMiklos Szeredi 
6576c988f57SMiklos Szeredi 	return buf;
6583a60a168STyler Hicks }
6593a60a168STyler Hicks 
6606b255391SAl Viro static const char *ecryptfs_get_link(struct dentry *dentry,
661fceef393SAl Viro 				     struct inode *inode,
662fceef393SAl Viro 				     struct delayed_call *done)
663237fead6SMichael Halcrow {
664b22e8fedSAl Viro 	size_t len;
6656b255391SAl Viro 	char *buf;
6666b255391SAl Viro 
6676b255391SAl Viro 	if (!dentry)
6686b255391SAl Viro 		return ERR_PTR(-ECHILD);
6696b255391SAl Viro 
6706b255391SAl Viro 	buf = ecryptfs_readlink_lower(dentry, &len);
671b22e8fedSAl Viro 	if (IS_ERR(buf))
672680baacbSAl Viro 		return buf;
6732b0143b5SDavid Howells 	fsstack_copy_attr_atime(d_inode(dentry),
6742b0143b5SDavid Howells 				d_inode(ecryptfs_dentry_to_lower(dentry)));
675408bd629SAl Viro 	buf[len] = '\0';
676fceef393SAl Viro 	set_delayed_call(done, kfree_link, buf);
677fceef393SAl Viro 	return buf;
678237fead6SMichael Halcrow }
679237fead6SMichael Halcrow 
680237fead6SMichael Halcrow /**
681237fead6SMichael Halcrow  * upper_size_to_lower_size
682237fead6SMichael Halcrow  * @crypt_stat: Crypt_stat associated with file
683237fead6SMichael Halcrow  * @upper_size: Size of the upper file
684237fead6SMichael Halcrow  *
685cc11beffSMichael Halcrow  * Calculate the required size of the lower file based on the
686237fead6SMichael Halcrow  * specified size of the upper file. This calculation is based on the
687237fead6SMichael Halcrow  * number of headers in the underlying file and the extent size.
688237fead6SMichael Halcrow  *
689237fead6SMichael Halcrow  * Returns Calculated size of the lower file.
690237fead6SMichael Halcrow  */
691237fead6SMichael Halcrow static loff_t
692237fead6SMichael Halcrow upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
693237fead6SMichael Halcrow 			 loff_t upper_size)
694237fead6SMichael Halcrow {
695237fead6SMichael Halcrow 	loff_t lower_size;
696237fead6SMichael Halcrow 
697157f1071STyler Hicks 	lower_size = ecryptfs_lower_header_size(crypt_stat);
698237fead6SMichael Halcrow 	if (upper_size != 0) {
699237fead6SMichael Halcrow 		loff_t num_extents;
700237fead6SMichael Halcrow 
701237fead6SMichael Halcrow 		num_extents = upper_size >> crypt_stat->extent_shift;
702237fead6SMichael Halcrow 		if (upper_size & ~crypt_stat->extent_mask)
703237fead6SMichael Halcrow 			num_extents++;
704237fead6SMichael Halcrow 		lower_size += (num_extents * crypt_stat->extent_size);
705237fead6SMichael Halcrow 	}
706237fead6SMichael Halcrow 	return lower_size;
707237fead6SMichael Halcrow }
708237fead6SMichael Halcrow 
709237fead6SMichael Halcrow /**
7105f3ef64fSTyler Hicks  * truncate_upper
711237fead6SMichael Halcrow  * @dentry: The ecryptfs layer dentry
7125f3ef64fSTyler Hicks  * @ia: Address of the ecryptfs inode's attributes
7135f3ef64fSTyler Hicks  * @lower_ia: Address of the lower inode's attributes
714237fead6SMichael Halcrow  *
715237fead6SMichael Halcrow  * Function to handle truncations modifying the size of the file. Note
716237fead6SMichael Halcrow  * that the file sizes are interpolated. When expanding, we are simply
7175f3ef64fSTyler Hicks  * writing strings of 0's out. When truncating, we truncate the upper
7185f3ef64fSTyler Hicks  * inode and update the lower_ia according to the page index
7195f3ef64fSTyler Hicks  * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
7205f3ef64fSTyler Hicks  * the caller must use lower_ia in a call to notify_change() to perform
7215f3ef64fSTyler Hicks  * the truncation of the lower inode.
722237fead6SMichael Halcrow  *
723237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
724237fead6SMichael Halcrow  */
7255f3ef64fSTyler Hicks static int truncate_upper(struct dentry *dentry, struct iattr *ia,
7265f3ef64fSTyler Hicks 			  struct iattr *lower_ia)
727237fead6SMichael Halcrow {
728237fead6SMichael Halcrow 	int rc = 0;
7292b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
730237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
731237fead6SMichael Halcrow 	loff_t i_size = i_size_read(inode);
732237fead6SMichael Halcrow 	loff_t lower_size_before_truncate;
733237fead6SMichael Halcrow 	loff_t lower_size_after_truncate;
734237fead6SMichael Halcrow 
7355f3ef64fSTyler Hicks 	if (unlikely((ia->ia_size == i_size))) {
7365f3ef64fSTyler Hicks 		lower_ia->ia_valid &= ~ATTR_SIZE;
737332ab16fSTyler Hicks 		return 0;
7385f3ef64fSTyler Hicks 	}
7393b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(dentry, inode);
740332ab16fSTyler Hicks 	if (rc)
741332ab16fSTyler Hicks 		return rc;
7422b0143b5SDavid Howells 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
743237fead6SMichael Halcrow 	/* Switch on growing or shrinking file */
7445f3ef64fSTyler Hicks 	if (ia->ia_size > i_size) {
7452ed92554SMichael Halcrow 		char zero[] = { 0x00 };
746240e2df5SMichael Halcrow 
7475f3ef64fSTyler Hicks 		lower_ia->ia_valid &= ~ATTR_SIZE;
7482ed92554SMichael Halcrow 		/* Write a single 0 at the last position of the file;
7492ed92554SMichael Halcrow 		 * this triggers code that will fill in 0's throughout
7502ed92554SMichael Halcrow 		 * the intermediate portion of the previous end of the
7512ed92554SMichael Halcrow 		 * file and the new and of the file */
75248c1e44aSAl Viro 		rc = ecryptfs_write(inode, zero,
7535f3ef64fSTyler Hicks 				    (ia->ia_size - 1), 1);
7545f3ef64fSTyler Hicks 	} else { /* ia->ia_size < i_size_read(inode) */
7555f3ef64fSTyler Hicks 		/* We're chopping off all the pages down to the page
7565f3ef64fSTyler Hicks 		 * in which ia->ia_size is located. Fill in the end of
757ea1754a0SKirill A. Shutemov 		 * that page from (ia->ia_size & ~PAGE_MASK) to
758ea1754a0SKirill A. Shutemov 		 * PAGE_SIZE with zeros. */
75909cbfeafSKirill A. Shutemov 		size_t num_zeros = (PAGE_SIZE
76009cbfeafSKirill A. Shutemov 				    - (ia->ia_size & ~PAGE_MASK));
7612ed92554SMichael Halcrow 
7622c27c65eSChristoph Hellwig 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
7632c27c65eSChristoph Hellwig 			truncate_setsize(inode, ia->ia_size);
7645f3ef64fSTyler Hicks 			lower_ia->ia_size = ia->ia_size;
7655f3ef64fSTyler Hicks 			lower_ia->ia_valid |= ATTR_SIZE;
76648c1e44aSAl Viro 			goto out;
76713a791b4STyler Hicks 		}
7682ed92554SMichael Halcrow 		if (num_zeros) {
7692ed92554SMichael Halcrow 			char *zeros_virt;
7702ed92554SMichael Halcrow 
7712ed92554SMichael Halcrow 			zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
7722ed92554SMichael Halcrow 			if (!zeros_virt) {
7732ed92554SMichael Halcrow 				rc = -ENOMEM;
77448c1e44aSAl Viro 				goto out;
775240e2df5SMichael Halcrow 			}
77648c1e44aSAl Viro 			rc = ecryptfs_write(inode, zeros_virt,
7775f3ef64fSTyler Hicks 					    ia->ia_size, num_zeros);
7782ed92554SMichael Halcrow 			kfree(zeros_virt);
7795dda6992SMichael Halcrow 			if (rc) {
780240e2df5SMichael Halcrow 				printk(KERN_ERR "Error attempting to zero out "
781240e2df5SMichael Halcrow 				       "the remainder of the end page on "
782240e2df5SMichael Halcrow 				       "reducing truncate; rc = [%d]\n", rc);
78348c1e44aSAl Viro 				goto out;
784240e2df5SMichael Halcrow 			}
785240e2df5SMichael Halcrow 		}
7862c27c65eSChristoph Hellwig 		truncate_setsize(inode, ia->ia_size);
7870216f7f7SMichael Halcrow 		rc = ecryptfs_write_inode_size_to_metadata(inode);
788dd2a3b7aSMichael Halcrow 		if (rc) {
789dd2a3b7aSMichael Halcrow 			printk(KERN_ERR	"Problem with "
790dd2a3b7aSMichael Halcrow 			       "ecryptfs_write_inode_size_to_metadata; "
791dd2a3b7aSMichael Halcrow 			       "rc = [%d]\n", rc);
79248c1e44aSAl Viro 			goto out;
793dd2a3b7aSMichael Halcrow 		}
794237fead6SMichael Halcrow 		/* We are reducing the size of the ecryptfs file, and need to
795237fead6SMichael Halcrow 		 * know if we need to reduce the size of the lower file. */
796237fead6SMichael Halcrow 		lower_size_before_truncate =
797237fead6SMichael Halcrow 		    upper_size_to_lower_size(crypt_stat, i_size);
798237fead6SMichael Halcrow 		lower_size_after_truncate =
7995f3ef64fSTyler Hicks 		    upper_size_to_lower_size(crypt_stat, ia->ia_size);
8005f3ef64fSTyler Hicks 		if (lower_size_after_truncate < lower_size_before_truncate) {
8015f3ef64fSTyler Hicks 			lower_ia->ia_size = lower_size_after_truncate;
8025f3ef64fSTyler Hicks 			lower_ia->ia_valid |= ATTR_SIZE;
8035f3ef64fSTyler Hicks 		} else
8045f3ef64fSTyler Hicks 			lower_ia->ia_valid &= ~ATTR_SIZE;
805237fead6SMichael Halcrow 	}
806237fead6SMichael Halcrow out:
807332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
808237fead6SMichael Halcrow 	return rc;
809237fead6SMichael Halcrow }
810237fead6SMichael Halcrow 
811a261a039STyler Hicks static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
812a261a039STyler Hicks {
813a261a039STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
814a261a039STyler Hicks 	loff_t lower_oldsize, lower_newsize;
815a261a039STyler Hicks 
816a261a039STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
817a261a039STyler Hicks 	lower_oldsize = upper_size_to_lower_size(crypt_stat,
818a261a039STyler Hicks 						 i_size_read(inode));
819a261a039STyler Hicks 	lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
820a261a039STyler Hicks 	if (lower_newsize > lower_oldsize) {
821a261a039STyler Hicks 		/*
822a261a039STyler Hicks 		 * The eCryptfs inode and the new *lower* size are mixed here
823a261a039STyler Hicks 		 * because we may not have the lower i_mutex held and/or it may
824a261a039STyler Hicks 		 * not be appropriate to call inode_newsize_ok() with inodes
825a261a039STyler Hicks 		 * from other filesystems.
826a261a039STyler Hicks 		 */
827a261a039STyler Hicks 		return inode_newsize_ok(inode, lower_newsize);
828a261a039STyler Hicks 	}
829a261a039STyler Hicks 
830a261a039STyler Hicks 	return 0;
831a261a039STyler Hicks }
832a261a039STyler Hicks 
8335f3ef64fSTyler Hicks /**
8345f3ef64fSTyler Hicks  * ecryptfs_truncate
8355f3ef64fSTyler Hicks  * @dentry: The ecryptfs layer dentry
8365f3ef64fSTyler Hicks  * @new_length: The length to expand the file to
8375f3ef64fSTyler Hicks  *
8385f3ef64fSTyler Hicks  * Simple function that handles the truncation of an eCryptfs inode and
8395f3ef64fSTyler Hicks  * its corresponding lower inode.
8405f3ef64fSTyler Hicks  *
8415f3ef64fSTyler Hicks  * Returns zero on success; non-zero otherwise
8425f3ef64fSTyler Hicks  */
8435f3ef64fSTyler Hicks int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
8445f3ef64fSTyler Hicks {
8455f3ef64fSTyler Hicks 	struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
8465f3ef64fSTyler Hicks 	struct iattr lower_ia = { .ia_valid = 0 };
8475f3ef64fSTyler Hicks 	int rc;
8485f3ef64fSTyler Hicks 
8492b0143b5SDavid Howells 	rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
850a261a039STyler Hicks 	if (rc)
851a261a039STyler Hicks 		return rc;
852a261a039STyler Hicks 
8535f3ef64fSTyler Hicks 	rc = truncate_upper(dentry, &ia, &lower_ia);
8545f3ef64fSTyler Hicks 	if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
8555f3ef64fSTyler Hicks 		struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
8565f3ef64fSTyler Hicks 
8575955102cSAl Viro 		inode_lock(d_inode(lower_dentry));
8582f221d6fSChristian Brauner 		rc = notify_change(&init_user_ns, lower_dentry,
8592f221d6fSChristian Brauner 				   &lower_ia, NULL);
8605955102cSAl Viro 		inode_unlock(d_inode(lower_dentry));
8615f3ef64fSTyler Hicks 	}
8625f3ef64fSTyler Hicks 	return rc;
8635f3ef64fSTyler Hicks }
8645f3ef64fSTyler Hicks 
865237fead6SMichael Halcrow static int
866549c7297SChristian Brauner ecryptfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
867549c7297SChristian Brauner 		    int mask)
868237fead6SMichael Halcrow {
86947291baaSChristian Brauner 	return inode_permission(&init_user_ns,
87047291baaSChristian Brauner 				ecryptfs_inode_to_lower(inode), mask);
871237fead6SMichael Halcrow }
872237fead6SMichael Halcrow 
873237fead6SMichael Halcrow /**
874237fead6SMichael Halcrow  * ecryptfs_setattr
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 
1106754661f1SArjan van de Ven const struct inode_operations ecryptfs_symlink_iops = {
11076b255391SAl Viro 	.get_link = ecryptfs_get_link,
1108237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1109237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
11103a60a168STyler Hicks 	.getattr = ecryptfs_getattr_link,
1111237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
1112237fead6SMichael Halcrow };
1113237fead6SMichael Halcrow 
1114754661f1SArjan van de Ven const struct inode_operations ecryptfs_dir_iops = {
1115237fead6SMichael Halcrow 	.create = ecryptfs_create,
1116237fead6SMichael Halcrow 	.lookup = ecryptfs_lookup,
1117237fead6SMichael Halcrow 	.link = ecryptfs_link,
1118237fead6SMichael Halcrow 	.unlink = ecryptfs_unlink,
1119237fead6SMichael Halcrow 	.symlink = ecryptfs_symlink,
1120237fead6SMichael Halcrow 	.mkdir = ecryptfs_mkdir,
1121237fead6SMichael Halcrow 	.rmdir = ecryptfs_rmdir,
1122237fead6SMichael Halcrow 	.mknod = ecryptfs_mknod,
1123237fead6SMichael Halcrow 	.rename = ecryptfs_rename,
1124237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1125237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
1126237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
1127237fead6SMichael Halcrow };
1128237fead6SMichael Halcrow 
1129754661f1SArjan van de Ven const struct inode_operations ecryptfs_main_iops = {
1130237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1131237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
1132f8f484d1STyler Hicks 	.getattr = ecryptfs_getattr,
1133237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
11344b899da5SAndreas Gruenbacher };
11354b899da5SAndreas Gruenbacher 
11364b899da5SAndreas Gruenbacher static int ecryptfs_xattr_get(const struct xattr_handler *handler,
11374b899da5SAndreas Gruenbacher 			      struct dentry *dentry, struct inode *inode,
11384b899da5SAndreas Gruenbacher 			      const char *name, void *buffer, size_t size)
11394b899da5SAndreas Gruenbacher {
11404b899da5SAndreas Gruenbacher 	return ecryptfs_getxattr(dentry, inode, name, buffer, size);
11414b899da5SAndreas Gruenbacher }
11424b899da5SAndreas Gruenbacher 
11434b899da5SAndreas Gruenbacher static int ecryptfs_xattr_set(const struct xattr_handler *handler,
1144e65ce2a5SChristian Brauner 			      struct user_namespace *mnt_userns,
11454b899da5SAndreas Gruenbacher 			      struct dentry *dentry, struct inode *inode,
11464b899da5SAndreas Gruenbacher 			      const char *name, const void *value, size_t size,
11474b899da5SAndreas Gruenbacher 			      int flags)
11484b899da5SAndreas Gruenbacher {
11494b899da5SAndreas Gruenbacher 	if (value)
11504b899da5SAndreas Gruenbacher 		return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
11514b899da5SAndreas Gruenbacher 	else {
11524b899da5SAndreas Gruenbacher 		BUG_ON(flags != XATTR_REPLACE);
11534b899da5SAndreas Gruenbacher 		return ecryptfs_removexattr(dentry, inode, name);
11544b899da5SAndreas Gruenbacher 	}
11554b899da5SAndreas Gruenbacher }
11564b899da5SAndreas Gruenbacher 
1157c036061bSYueHaibing static const struct xattr_handler ecryptfs_xattr_handler = {
11584b899da5SAndreas Gruenbacher 	.prefix = "",  /* match anything */
11594b899da5SAndreas Gruenbacher 	.get = ecryptfs_xattr_get,
11604b899da5SAndreas Gruenbacher 	.set = ecryptfs_xattr_set,
11614b899da5SAndreas Gruenbacher };
11624b899da5SAndreas Gruenbacher 
11634b899da5SAndreas Gruenbacher const struct xattr_handler *ecryptfs_xattr_handlers[] = {
11644b899da5SAndreas Gruenbacher 	&ecryptfs_xattr_handler,
11654b899da5SAndreas Gruenbacher 	NULL
1166237fead6SMichael Halcrow };
1167