xref: /openbmc/linux/fs/ecryptfs/inode.c (revision a528d35e)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  *
4237fead6SMichael Halcrow  * Copyright (C) 1997-2004 Erez Zadok
5237fead6SMichael Halcrow  * Copyright (C) 2001-2004 Stony Brook University
6dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
7237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8237fead6SMichael Halcrow  *              Michael C. Thompsion <mcthomps@us.ibm.com>
9237fead6SMichael Halcrow  *
10237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
11237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
12237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
13237fead6SMichael Halcrow  * License, or (at your option) any later version.
14237fead6SMichael Halcrow  *
15237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
16237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
17237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18237fead6SMichael Halcrow  * General Public License for more details.
19237fead6SMichael Halcrow  *
20237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
21237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
22237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23237fead6SMichael Halcrow  * 02111-1307, USA.
24237fead6SMichael Halcrow  */
25237fead6SMichael Halcrow 
26237fead6SMichael Halcrow #include <linux/file.h>
27237fead6SMichael Halcrow #include <linux/vmalloc.h>
28237fead6SMichael Halcrow #include <linux/pagemap.h>
29237fead6SMichael Halcrow #include <linux/dcache.h>
30237fead6SMichael Halcrow #include <linux/namei.h>
31237fead6SMichael Halcrow #include <linux/mount.h>
320cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
3448b512e6SRoberto Sassu #include <linux/xattr.h>
350a688ad7SHarvey Harrison #include <asm/unaligned.h>
36237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
37237fead6SMichael Halcrow 
38237fead6SMichael Halcrow static struct dentry *lock_parent(struct dentry *dentry)
39237fead6SMichael Halcrow {
40237fead6SMichael Halcrow 	struct dentry *dir;
41237fead6SMichael Halcrow 
428dc4e373SMiklos Szeredi 	dir = dget_parent(dentry);
435955102cSAl Viro 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
44237fead6SMichael Halcrow 	return dir;
45237fead6SMichael Halcrow }
46237fead6SMichael Halcrow 
47237fead6SMichael Halcrow static void unlock_dir(struct dentry *dir)
48237fead6SMichael Halcrow {
495955102cSAl Viro 	inode_unlock(d_inode(dir));
50237fead6SMichael Halcrow 	dput(dir);
51237fead6SMichael Halcrow }
52237fead6SMichael Halcrow 
53c4f79073STyler Hicks static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
54c4f79073STyler Hicks {
55c4cf3ba4SHimangi Saraogi 	return ecryptfs_inode_to_lower(inode) == lower_inode;
56c4f79073STyler Hicks }
57c4f79073STyler Hicks 
585ccf9203STyler Hicks static int ecryptfs_inode_set(struct inode *inode, void *opaque)
59c4f79073STyler Hicks {
605ccf9203STyler Hicks 	struct inode *lower_inode = opaque;
615ccf9203STyler Hicks 
625ccf9203STyler Hicks 	ecryptfs_set_inode_lower(inode, lower_inode);
635ccf9203STyler Hicks 	fsstack_copy_attr_all(inode, lower_inode);
645ccf9203STyler Hicks 	/* i_size will be overwritten for encrypted regular files */
655ccf9203STyler Hicks 	fsstack_copy_inode_size(inode, lower_inode);
665ccf9203STyler Hicks 	inode->i_ino = lower_inode->i_ino;
67c4f79073STyler Hicks 	inode->i_version++;
68c4f79073STyler Hicks 	inode->i_mapping->a_ops = &ecryptfs_aops;
695ccf9203STyler Hicks 
705ccf9203STyler Hicks 	if (S_ISLNK(inode->i_mode))
715ccf9203STyler Hicks 		inode->i_op = &ecryptfs_symlink_iops;
725ccf9203STyler Hicks 	else if (S_ISDIR(inode->i_mode))
735ccf9203STyler Hicks 		inode->i_op = &ecryptfs_dir_iops;
745ccf9203STyler Hicks 	else
755ccf9203STyler Hicks 		inode->i_op = &ecryptfs_main_iops;
765ccf9203STyler Hicks 
775ccf9203STyler Hicks 	if (S_ISDIR(inode->i_mode))
785ccf9203STyler Hicks 		inode->i_fop = &ecryptfs_dir_fops;
795ccf9203STyler Hicks 	else if (special_file(inode->i_mode))
805ccf9203STyler Hicks 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
815ccf9203STyler Hicks 	else
825ccf9203STyler Hicks 		inode->i_fop = &ecryptfs_main_fops;
835ccf9203STyler Hicks 
84c4f79073STyler Hicks 	return 0;
85c4f79073STyler Hicks }
86c4f79073STyler Hicks 
875ccf9203STyler Hicks static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
885ccf9203STyler Hicks 					  struct super_block *sb)
895ccf9203STyler Hicks {
905ccf9203STyler Hicks 	struct inode *inode;
915ccf9203STyler Hicks 
925ccf9203STyler Hicks 	if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
935ccf9203STyler Hicks 		return ERR_PTR(-EXDEV);
945ccf9203STyler Hicks 	if (!igrab(lower_inode))
955ccf9203STyler Hicks 		return ERR_PTR(-ESTALE);
965ccf9203STyler Hicks 	inode = iget5_locked(sb, (unsigned long)lower_inode,
975ccf9203STyler Hicks 			     ecryptfs_inode_test, ecryptfs_inode_set,
985ccf9203STyler Hicks 			     lower_inode);
995ccf9203STyler Hicks 	if (!inode) {
1005ccf9203STyler Hicks 		iput(lower_inode);
1015ccf9203STyler Hicks 		return ERR_PTR(-EACCES);
1025ccf9203STyler Hicks 	}
1035ccf9203STyler Hicks 	if (!(inode->i_state & I_NEW))
1045ccf9203STyler Hicks 		iput(lower_inode);
1055ccf9203STyler Hicks 
1065ccf9203STyler Hicks 	return inode;
1075ccf9203STyler Hicks }
1085ccf9203STyler Hicks 
109c4f79073STyler Hicks struct inode *ecryptfs_get_inode(struct inode *lower_inode,
110c4f79073STyler Hicks 				 struct super_block *sb)
111c4f79073STyler Hicks {
1125ccf9203STyler Hicks 	struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
113c4f79073STyler Hicks 
1145ccf9203STyler Hicks 	if (!IS_ERR(inode) && (inode->i_state & I_NEW))
115c4f79073STyler Hicks 		unlock_new_inode(inode);
1165ccf9203STyler Hicks 
117c4f79073STyler Hicks 	return inode;
118c4f79073STyler Hicks }
119c4f79073STyler Hicks 
120c4f79073STyler Hicks /**
121c4f79073STyler Hicks  * ecryptfs_interpose
122c4f79073STyler Hicks  * @lower_dentry: Existing dentry in the lower filesystem
123c4f79073STyler Hicks  * @dentry: ecryptfs' dentry
124c4f79073STyler Hicks  * @sb: ecryptfs's super_block
125c4f79073STyler Hicks  *
126c4f79073STyler Hicks  * Interposes upper and lower dentries.
127c4f79073STyler Hicks  *
128c4f79073STyler Hicks  * Returns zero on success; non-zero otherwise
129c4f79073STyler Hicks  */
130c4f79073STyler Hicks static int ecryptfs_interpose(struct dentry *lower_dentry,
1315ccf9203STyler Hicks 			      struct dentry *dentry, struct super_block *sb)
132c4f79073STyler Hicks {
1332b0143b5SDavid Howells 	struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
1345ccf9203STyler Hicks 
135c4f79073STyler Hicks 	if (IS_ERR(inode))
136c4f79073STyler Hicks 		return PTR_ERR(inode);
137c4f79073STyler Hicks 	d_instantiate(dentry, inode);
1385ccf9203STyler Hicks 
139c4f79073STyler Hicks 	return 0;
140c4f79073STyler Hicks }
141c4f79073STyler Hicks 
1428bc2d3cfSTyler Hicks static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
1438bc2d3cfSTyler Hicks 			      struct inode *inode)
1448bc2d3cfSTyler Hicks {
1458bc2d3cfSTyler Hicks 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
1468bc2d3cfSTyler Hicks 	struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
1478bc2d3cfSTyler Hicks 	struct dentry *lower_dir_dentry;
1488bc2d3cfSTyler Hicks 	int rc;
1498bc2d3cfSTyler Hicks 
1508bc2d3cfSTyler Hicks 	dget(lower_dentry);
1518bc2d3cfSTyler Hicks 	lower_dir_dentry = lock_parent(lower_dentry);
152b21996e3SJ. Bruce Fields 	rc = vfs_unlink(lower_dir_inode, lower_dentry, NULL);
1538bc2d3cfSTyler Hicks 	if (rc) {
1548bc2d3cfSTyler Hicks 		printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
1558bc2d3cfSTyler Hicks 		goto out_unlock;
1568bc2d3cfSTyler Hicks 	}
1578bc2d3cfSTyler Hicks 	fsstack_copy_attr_times(dir, lower_dir_inode);
1588bc2d3cfSTyler Hicks 	set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
1598bc2d3cfSTyler Hicks 	inode->i_ctime = dir->i_ctime;
1608bc2d3cfSTyler Hicks 	d_drop(dentry);
1618bc2d3cfSTyler Hicks out_unlock:
1628bc2d3cfSTyler Hicks 	unlock_dir(lower_dir_dentry);
1638bc2d3cfSTyler Hicks 	dput(lower_dentry);
1648bc2d3cfSTyler Hicks 	return rc;
1658bc2d3cfSTyler Hicks }
1668bc2d3cfSTyler Hicks 
167237fead6SMichael Halcrow /**
168237fead6SMichael Halcrow  * ecryptfs_do_create
169237fead6SMichael Halcrow  * @directory_inode: inode of the new file's dentry's parent in ecryptfs
170237fead6SMichael Halcrow  * @ecryptfs_dentry: New file's dentry in ecryptfs
171237fead6SMichael Halcrow  * @mode: The mode of the new file
172237fead6SMichael Halcrow  *
173237fead6SMichael Halcrow  * Creates the underlying file and the eCryptfs inode which will link to
174237fead6SMichael Halcrow  * it. It will also update the eCryptfs directory inode to mimic the
175237fead6SMichael Halcrow  * stat of the lower directory inode.
176237fead6SMichael Halcrow  *
177b59db43aSTyler Hicks  * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
178237fead6SMichael Halcrow  */
179b59db43aSTyler Hicks static struct inode *
180237fead6SMichael Halcrow ecryptfs_do_create(struct inode *directory_inode,
181175a4eb7SAl Viro 		   struct dentry *ecryptfs_dentry, umode_t mode)
182237fead6SMichael Halcrow {
183237fead6SMichael Halcrow 	int rc;
184237fead6SMichael Halcrow 	struct dentry *lower_dentry;
185237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
186b59db43aSTyler Hicks 	struct inode *inode;
187237fead6SMichael Halcrow 
188237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
189237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
1902b0143b5SDavid Howells 	rc = vfs_create(d_inode(lower_dir_dentry), lower_dentry, mode, true);
1914981e081SMichael Halcrow 	if (rc) {
192caeeeecfSMichael Halcrow 		printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
19318d1dbf1SHarvey Harrison 		       "rc = [%d]\n", __func__, rc);
194b59db43aSTyler Hicks 		inode = ERR_PTR(rc);
195237fead6SMichael Halcrow 		goto out_lock;
196237fead6SMichael Halcrow 	}
1972b0143b5SDavid Howells 	inode = __ecryptfs_get_inode(d_inode(lower_dentry),
1985ccf9203STyler Hicks 				     directory_inode->i_sb);
1998bc2d3cfSTyler Hicks 	if (IS_ERR(inode)) {
2002b0143b5SDavid Howells 		vfs_unlink(d_inode(lower_dir_dentry), lower_dentry, NULL);
201237fead6SMichael Halcrow 		goto out_lock;
2028bc2d3cfSTyler Hicks 	}
2032b0143b5SDavid Howells 	fsstack_copy_attr_times(directory_inode, d_inode(lower_dir_dentry));
2042b0143b5SDavid Howells 	fsstack_copy_inode_size(directory_inode, d_inode(lower_dir_dentry));
205237fead6SMichael Halcrow out_lock:
206237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
207b59db43aSTyler Hicks 	return inode;
208237fead6SMichael Halcrow }
209237fead6SMichael Halcrow 
210237fead6SMichael Halcrow /**
211237fead6SMichael Halcrow  * ecryptfs_initialize_file
212237fead6SMichael Halcrow  *
213237fead6SMichael Halcrow  * Cause the file to be changed from a basic empty file to an ecryptfs
214237fead6SMichael Halcrow  * file with a header and first data page.
215237fead6SMichael Halcrow  *
216237fead6SMichael Halcrow  * Returns zero on success
217237fead6SMichael Halcrow  */
218e3ccaa97STyler Hicks int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
219b59db43aSTyler Hicks 			     struct inode *ecryptfs_inode)
220237fead6SMichael Halcrow {
221d7cdc5feSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat =
222b59db43aSTyler Hicks 		&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
223237fead6SMichael Halcrow 	int rc = 0;
224237fead6SMichael Halcrow 
225b59db43aSTyler Hicks 	if (S_ISDIR(ecryptfs_inode->i_mode)) {
226237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
227e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
228d7cdc5feSMichael Halcrow 		goto out;
229237fead6SMichael Halcrow 	}
230237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
231b59db43aSTyler Hicks 	rc = ecryptfs_new_file_context(ecryptfs_inode);
232237fead6SMichael Halcrow 	if (rc) {
233d7cdc5feSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error creating new file "
234d7cdc5feSMichael Halcrow 				"context; rc = [%d]\n", rc);
235d7cdc5feSMichael Halcrow 		goto out;
236237fead6SMichael Halcrow 	}
237b59db43aSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
238391b52f9SMichael Halcrow 	if (rc) {
239391b52f9SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
240332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2419e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2429e78d14aSDavid Howells 			ecryptfs_dentry, rc);
243391b52f9SMichael Halcrow 		goto out;
244391b52f9SMichael Halcrow 	}
245b59db43aSTyler Hicks 	rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
246332ab16fSTyler Hicks 	if (rc)
247d7cdc5feSMichael Halcrow 		printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
248b59db43aSTyler Hicks 	ecryptfs_put_lower_file(ecryptfs_inode);
249237fead6SMichael Halcrow out:
250237fead6SMichael Halcrow 	return rc;
251237fead6SMichael Halcrow }
252237fead6SMichael Halcrow 
253237fead6SMichael Halcrow /**
254237fead6SMichael Halcrow  * ecryptfs_create
255237fead6SMichael Halcrow  * @dir: The inode of the directory in which to create the file.
256237fead6SMichael Halcrow  * @dentry: The eCryptfs dentry
257237fead6SMichael Halcrow  * @mode: The mode of the new file.
258237fead6SMichael Halcrow  *
259237fead6SMichael Halcrow  * Creates a new file.
260237fead6SMichael Halcrow  *
261237fead6SMichael Halcrow  * Returns zero on success; non-zero on error condition
262237fead6SMichael Halcrow  */
263237fead6SMichael Halcrow static int
264237fead6SMichael Halcrow ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
265ebfc3b49SAl Viro 		umode_t mode, bool excl)
266237fead6SMichael Halcrow {
267b59db43aSTyler Hicks 	struct inode *ecryptfs_inode;
268237fead6SMichael Halcrow 	int rc;
269237fead6SMichael Halcrow 
270b59db43aSTyler Hicks 	ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
271b59db43aSTyler Hicks 					    mode);
272a1c83681SViresh Kumar 	if (IS_ERR(ecryptfs_inode)) {
273237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Failed to create file in"
274237fead6SMichael Halcrow 				"lower filesystem\n");
275b59db43aSTyler Hicks 		rc = PTR_ERR(ecryptfs_inode);
276237fead6SMichael Halcrow 		goto out;
277237fead6SMichael Halcrow 	}
278237fead6SMichael Halcrow 	/* At this point, a file exists on "disk"; we need to make sure
279237fead6SMichael Halcrow 	 * that this on disk file is prepared to be an ecryptfs file */
280b59db43aSTyler Hicks 	rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
281b59db43aSTyler Hicks 	if (rc) {
2828bc2d3cfSTyler Hicks 		ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
2838bc2d3cfSTyler Hicks 				   ecryptfs_inode);
2840e81ba23SAl Viro 		iget_failed(ecryptfs_inode);
285b59db43aSTyler Hicks 		goto out;
286b59db43aSTyler Hicks 	}
287b59db43aSTyler Hicks 	unlock_new_inode(ecryptfs_inode);
2888fc37ec5SAl Viro 	d_instantiate(ecryptfs_dentry, ecryptfs_inode);
289237fead6SMichael Halcrow out:
290237fead6SMichael Halcrow 	return rc;
291237fead6SMichael Halcrow }
292237fead6SMichael Halcrow 
293778aeb42STyler Hicks static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
294237fead6SMichael Halcrow {
2952aac0cf8STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
296778aeb42STyler Hicks 	int rc;
297237fead6SMichael Halcrow 
298778aeb42STyler Hicks 	rc = ecryptfs_get_lower_file(dentry, inode);
299391b52f9SMichael Halcrow 	if (rc) {
300391b52f9SMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
301332ab16fSTyler Hicks 			"the lower file for the dentry with name "
3029e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
3039e78d14aSDavid Howells 			dentry, rc);
304778aeb42STyler Hicks 		return rc;
305391b52f9SMichael Halcrow 	}
306778aeb42STyler Hicks 
3073b06b3ebSTyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
3082aac0cf8STyler Hicks 	/* TODO: lock for crypt_stat comparison */
3092aac0cf8STyler Hicks 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
3102aac0cf8STyler Hicks 		ecryptfs_set_default_sizes(crypt_stat);
311778aeb42STyler Hicks 
312778aeb42STyler Hicks 	rc = ecryptfs_read_and_validate_header_region(inode);
313778aeb42STyler Hicks 	ecryptfs_put_lower_file(inode);
314237fead6SMichael Halcrow 	if (rc) {
315778aeb42STyler Hicks 		rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
316778aeb42STyler Hicks 		if (!rc)
317dd2a3b7aSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
318dd2a3b7aSMichael Halcrow 	}
319778aeb42STyler Hicks 
320778aeb42STyler Hicks 	/* Must return 0 to allow non-eCryptfs files to be looked up, too */
321778aeb42STyler Hicks 	return 0;
322778aeb42STyler Hicks }
323778aeb42STyler Hicks 
324778aeb42STyler Hicks /**
325778aeb42STyler Hicks  * ecryptfs_lookup_interpose - Dentry interposition for a lookup
326778aeb42STyler Hicks  */
327b1168a92SAl Viro static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
328b1168a92SAl Viro 				     struct dentry *lower_dentry)
329778aeb42STyler Hicks {
3302b0143b5SDavid Howells 	struct inode *inode, *lower_inode = d_inode(lower_dentry);
331778aeb42STyler Hicks 	struct ecryptfs_dentry_info *dentry_info;
332778aeb42STyler Hicks 	struct vfsmount *lower_mnt;
333778aeb42STyler Hicks 	int rc = 0;
334778aeb42STyler Hicks 
335778aeb42STyler Hicks 	dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
336778aeb42STyler Hicks 	if (!dentry_info) {
337778aeb42STyler Hicks 		printk(KERN_ERR "%s: Out of memory whilst attempting "
338778aeb42STyler Hicks 		       "to allocate ecryptfs_dentry_info struct\n",
339778aeb42STyler Hicks 			__func__);
340237fead6SMichael Halcrow 		dput(lower_dentry);
341b1168a92SAl Viro 		return ERR_PTR(-ENOMEM);
342778aeb42STyler Hicks 	}
3430b1d9011SAl Viro 
3440b1d9011SAl Viro 	lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
345b1168a92SAl Viro 	fsstack_copy_attr_atime(d_inode(dentry->d_parent),
346b1168a92SAl Viro 				d_inode(lower_dentry->d_parent));
34784d08fa8SAl Viro 	BUG_ON(!d_count(lower_dentry));
3480b1d9011SAl Viro 
3490b1d9011SAl Viro 	ecryptfs_set_dentry_private(dentry, dentry_info);
35092dd1230SAl Viro 	dentry_info->lower_path.mnt = lower_mnt;
35192dd1230SAl Viro 	dentry_info->lower_path.dentry = lower_dentry;
352778aeb42STyler Hicks 
3532b0143b5SDavid Howells 	if (d_really_is_negative(lower_dentry)) {
354778aeb42STyler Hicks 		/* We want to add because we couldn't find in lower */
355778aeb42STyler Hicks 		d_add(dentry, NULL);
356b1168a92SAl Viro 		return NULL;
357778aeb42STyler Hicks 	}
358b1168a92SAl Viro 	inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
359778aeb42STyler Hicks 	if (IS_ERR(inode)) {
360778aeb42STyler Hicks 		printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
361778aeb42STyler Hicks 		       __func__, PTR_ERR(inode));
362b1168a92SAl Viro 		return ERR_CAST(inode);
363778aeb42STyler Hicks 	}
364778aeb42STyler Hicks 	if (S_ISREG(inode->i_mode)) {
365778aeb42STyler Hicks 		rc = ecryptfs_i_size_read(dentry, inode);
366778aeb42STyler Hicks 		if (rc) {
367778aeb42STyler Hicks 			make_bad_inode(inode);
368b1168a92SAl Viro 			return ERR_PTR(rc);
369778aeb42STyler Hicks 		}
370778aeb42STyler Hicks 	}
371778aeb42STyler Hicks 
372778aeb42STyler Hicks 	if (inode->i_state & I_NEW)
373778aeb42STyler Hicks 		unlock_new_inode(inode);
374b1168a92SAl Viro 	return d_splice_alias(inode, dentry);
375addd65adSMichael Halcrow }
376addd65adSMichael Halcrow 
377addd65adSMichael Halcrow /**
378addd65adSMichael Halcrow  * ecryptfs_lookup
379addd65adSMichael Halcrow  * @ecryptfs_dir_inode: The eCryptfs directory inode
380addd65adSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
38189076bc3SAl Viro  * @flags: lookup flags
382addd65adSMichael Halcrow  *
383addd65adSMichael Halcrow  * Find a file on disk. If the file does not exist, then we'll add it to the
384addd65adSMichael Halcrow  * dentry cache and continue on to read it from the disk.
385addd65adSMichael Halcrow  */
386addd65adSMichael Halcrow static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
387addd65adSMichael Halcrow 				      struct dentry *ecryptfs_dentry,
38800cd8dd3SAl Viro 				      unsigned int flags)
389addd65adSMichael Halcrow {
390addd65adSMichael Halcrow 	char *encrypted_and_encoded_name = NULL;
39188ae4ab9SAl Viro 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
392addd65adSMichael Halcrow 	struct dentry *lower_dir_dentry, *lower_dentry;
39388ae4ab9SAl Viro 	const char *name = ecryptfs_dentry->d_name.name;
39488ae4ab9SAl Viro 	size_t len = ecryptfs_dentry->d_name.len;
395b1168a92SAl Viro 	struct dentry *res;
396addd65adSMichael Halcrow 	int rc = 0;
397addd65adSMichael Halcrow 
398addd65adSMichael Halcrow 	lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
39988ae4ab9SAl Viro 
400addd65adSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
401addd65adSMichael Halcrow 				ecryptfs_dentry->d_sb)->mount_crypt_stat;
40288ae4ab9SAl Viro 	if (mount_crypt_stat
40388ae4ab9SAl Viro 	    && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
404addd65adSMichael Halcrow 		rc = ecryptfs_encrypt_and_encode_filename(
40588ae4ab9SAl Viro 			&encrypted_and_encoded_name, &len,
40688ae4ab9SAl Viro 			mount_crypt_stat, name, len);
407addd65adSMichael Halcrow 		if (rc) {
408addd65adSMichael Halcrow 			printk(KERN_ERR "%s: Error attempting to encrypt and encode "
409addd65adSMichael Halcrow 			       "filename; rc = [%d]\n", __func__, rc);
410237fead6SMichael Halcrow 			return ERR_PTR(rc);
411237fead6SMichael Halcrow 		}
41288ae4ab9SAl Viro 		name = encrypted_and_encoded_name;
41388ae4ab9SAl Viro 	}
41488ae4ab9SAl Viro 
41588ae4ab9SAl Viro 	lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
416237fead6SMichael Halcrow 	if (IS_ERR(lower_dentry)) {
417237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
418b1168a92SAl Viro 				"[%ld] on lower_dentry = [%s]\n", __func__,
419b1168a92SAl Viro 				PTR_ERR(lower_dentry),
42088ae4ab9SAl Viro 				name);
421b1168a92SAl Viro 		res = ERR_CAST(lower_dentry);
42288ae4ab9SAl Viro 	} else {
423b1168a92SAl Viro 		res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
42488ae4ab9SAl Viro 	}
425237fead6SMichael Halcrow 	kfree(encrypted_and_encoded_name);
426b1168a92SAl Viro 	return res;
427237fead6SMichael Halcrow }
428237fead6SMichael Halcrow 
429237fead6SMichael Halcrow static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
430237fead6SMichael Halcrow 			 struct dentry *new_dentry)
431237fead6SMichael Halcrow {
432237fead6SMichael Halcrow 	struct dentry *lower_old_dentry;
433237fead6SMichael Halcrow 	struct dentry *lower_new_dentry;
434237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
435237fead6SMichael Halcrow 	u64 file_size_save;
436237fead6SMichael Halcrow 	int rc;
437237fead6SMichael Halcrow 
4382b0143b5SDavid Howells 	file_size_save = i_size_read(d_inode(old_dentry));
439237fead6SMichael Halcrow 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
440237fead6SMichael Halcrow 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
441237fead6SMichael Halcrow 	dget(lower_old_dentry);
442237fead6SMichael Halcrow 	dget(lower_new_dentry);
443237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_new_dentry);
4442b0143b5SDavid Howells 	rc = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
445146a8595SJ. Bruce Fields 		      lower_new_dentry, NULL);
4462b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_new_dentry))
447237fead6SMichael Halcrow 		goto out_lock;
4485ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
449237fead6SMichael Halcrow 	if (rc)
450237fead6SMichael Halcrow 		goto out_lock;
4512b0143b5SDavid Howells 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
4522b0143b5SDavid Howells 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
4532b0143b5SDavid Howells 	set_nlink(d_inode(old_dentry),
4542b0143b5SDavid Howells 		  ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
4552b0143b5SDavid Howells 	i_size_write(d_inode(new_dentry), file_size_save);
456237fead6SMichael Halcrow out_lock:
457237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
458237fead6SMichael Halcrow 	dput(lower_new_dentry);
459237fead6SMichael Halcrow 	dput(lower_old_dentry);
460237fead6SMichael Halcrow 	return rc;
461237fead6SMichael Halcrow }
462237fead6SMichael Halcrow 
463237fead6SMichael Halcrow static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
464237fead6SMichael Halcrow {
4652b0143b5SDavid Howells 	return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
466237fead6SMichael Halcrow }
467237fead6SMichael Halcrow 
468237fead6SMichael Halcrow static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
469237fead6SMichael Halcrow 			    const char *symname)
470237fead6SMichael Halcrow {
471237fead6SMichael Halcrow 	int rc;
472237fead6SMichael Halcrow 	struct dentry *lower_dentry;
473237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
474237fead6SMichael Halcrow 	char *encoded_symname;
475addd65adSMichael Halcrow 	size_t encoded_symlen;
476addd65adSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
477237fead6SMichael Halcrow 
478237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
479237fead6SMichael Halcrow 	dget(lower_dentry);
480237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
481addd65adSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
482addd65adSMichael Halcrow 		dir->i_sb)->mount_crypt_stat;
483addd65adSMichael Halcrow 	rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
484addd65adSMichael Halcrow 						  &encoded_symlen,
485addd65adSMichael Halcrow 						  mount_crypt_stat, symname,
486addd65adSMichael Halcrow 						  strlen(symname));
487addd65adSMichael Halcrow 	if (rc)
488237fead6SMichael Halcrow 		goto out_lock;
4892b0143b5SDavid Howells 	rc = vfs_symlink(d_inode(lower_dir_dentry), lower_dentry,
490db2e747bSMiklos Szeredi 			 encoded_symname);
491237fead6SMichael Halcrow 	kfree(encoded_symname);
4922b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
493237fead6SMichael Halcrow 		goto out_lock;
4945ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
495237fead6SMichael Halcrow 	if (rc)
496237fead6SMichael Halcrow 		goto out_lock;
4972b0143b5SDavid Howells 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
4982b0143b5SDavid Howells 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
499237fead6SMichael Halcrow out_lock:
500237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
501237fead6SMichael Halcrow 	dput(lower_dentry);
5022b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
503237fead6SMichael Halcrow 		d_drop(dentry);
504237fead6SMichael Halcrow 	return rc;
505237fead6SMichael Halcrow }
506237fead6SMichael Halcrow 
50718bb1db3SAl Viro static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
508237fead6SMichael Halcrow {
509237fead6SMichael Halcrow 	int rc;
510237fead6SMichael Halcrow 	struct dentry *lower_dentry;
511237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
512237fead6SMichael Halcrow 
513237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
514237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
5152b0143b5SDavid Howells 	rc = vfs_mkdir(d_inode(lower_dir_dentry), lower_dentry, 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;
53545ec4abaSMichael Halcrow 	int rc;
536237fead6SMichael Halcrow 
537237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
53845ec4abaSMichael Halcrow 	dget(dentry);
539237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
54045ec4abaSMichael Halcrow 	dget(lower_dentry);
5412b0143b5SDavid Howells 	rc = vfs_rmdir(d_inode(lower_dir_dentry), lower_dentry);
54245ec4abaSMichael Halcrow 	dput(lower_dentry);
5432b0143b5SDavid Howells 	if (!rc && d_really_is_positive(dentry))
5442b0143b5SDavid Howells 		clear_nlink(d_inode(dentry));
5452b0143b5SDavid Howells 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
5462b0143b5SDavid Howells 	set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
547237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
548237fead6SMichael Halcrow 	if (!rc)
549237fead6SMichael Halcrow 		d_drop(dentry);
55045ec4abaSMichael Halcrow 	dput(dentry);
551237fead6SMichael Halcrow 	return rc;
552237fead6SMichael Halcrow }
553237fead6SMichael Halcrow 
554237fead6SMichael Halcrow static int
5551a67aafbSAl Viro ecryptfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
556237fead6SMichael Halcrow {
557237fead6SMichael Halcrow 	int rc;
558237fead6SMichael Halcrow 	struct dentry *lower_dentry;
559237fead6SMichael Halcrow 	struct dentry *lower_dir_dentry;
560237fead6SMichael Halcrow 
561237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
562237fead6SMichael Halcrow 	lower_dir_dentry = lock_parent(lower_dentry);
5632b0143b5SDavid Howells 	rc = vfs_mknod(d_inode(lower_dir_dentry), lower_dentry, mode, dev);
5642b0143b5SDavid Howells 	if (rc || d_really_is_negative(lower_dentry))
565237fead6SMichael Halcrow 		goto out;
5665ccf9203STyler Hicks 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
567237fead6SMichael Halcrow 	if (rc)
568237fead6SMichael Halcrow 		goto out;
5692b0143b5SDavid Howells 	fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
5702b0143b5SDavid Howells 	fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
571237fead6SMichael Halcrow out:
572237fead6SMichael Halcrow 	unlock_dir(lower_dir_dentry);
5732b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
574237fead6SMichael Halcrow 		d_drop(dentry);
575237fead6SMichael Halcrow 	return rc;
576237fead6SMichael Halcrow }
577237fead6SMichael Halcrow 
578237fead6SMichael Halcrow static int
579237fead6SMichael Halcrow ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5801cd66c93SMiklos Szeredi 		struct inode *new_dir, struct dentry *new_dentry,
5811cd66c93SMiklos Szeredi 		unsigned int flags)
582237fead6SMichael Halcrow {
583237fead6SMichael Halcrow 	int rc;
584237fead6SMichael Halcrow 	struct dentry *lower_old_dentry;
585237fead6SMichael Halcrow 	struct dentry *lower_new_dentry;
586237fead6SMichael Halcrow 	struct dentry *lower_old_dir_dentry;
587237fead6SMichael Halcrow 	struct dentry *lower_new_dir_dentry;
5880d132f73SErez Zadok 	struct dentry *trap = NULL;
5898335eafcSTyler Hicks 	struct inode *target_inode;
590237fead6SMichael Halcrow 
5911cd66c93SMiklos Szeredi 	if (flags)
5921cd66c93SMiklos Szeredi 		return -EINVAL;
5931cd66c93SMiklos Szeredi 
594237fead6SMichael Halcrow 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
595237fead6SMichael Halcrow 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
596237fead6SMichael Halcrow 	dget(lower_old_dentry);
597237fead6SMichael Halcrow 	dget(lower_new_dentry);
598237fead6SMichael Halcrow 	lower_old_dir_dentry = dget_parent(lower_old_dentry);
599237fead6SMichael Halcrow 	lower_new_dir_dentry = dget_parent(lower_new_dentry);
6002b0143b5SDavid Howells 	target_inode = d_inode(new_dentry);
6010d132f73SErez Zadok 	trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
6020d132f73SErez Zadok 	/* source should not be ancestor of target */
6030d132f73SErez Zadok 	if (trap == lower_old_dentry) {
6040d132f73SErez Zadok 		rc = -EINVAL;
6050d132f73SErez Zadok 		goto out_lock;
6060d132f73SErez Zadok 	}
6070d132f73SErez Zadok 	/* target should not be ancestor of source */
6080d132f73SErez Zadok 	if (trap == lower_new_dentry) {
6090d132f73SErez Zadok 		rc = -ENOTEMPTY;
6100d132f73SErez Zadok 		goto out_lock;
6110d132f73SErez Zadok 	}
6122b0143b5SDavid Howells 	rc = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
6132b0143b5SDavid Howells 			d_inode(lower_new_dir_dentry), lower_new_dentry,
614520c8b16SMiklos Szeredi 			NULL, 0);
615237fead6SMichael Halcrow 	if (rc)
616237fead6SMichael Halcrow 		goto out_lock;
6178335eafcSTyler Hicks 	if (target_inode)
6188335eafcSTyler Hicks 		fsstack_copy_attr_all(target_inode,
6198335eafcSTyler Hicks 				      ecryptfs_inode_to_lower(target_inode));
6202b0143b5SDavid Howells 	fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
621237fead6SMichael Halcrow 	if (new_dir != old_dir)
6222b0143b5SDavid Howells 		fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
623237fead6SMichael Halcrow out_lock:
624237fead6SMichael Halcrow 	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
625dd55c898STyler Hicks 	dput(lower_new_dir_dentry);
626dd55c898STyler Hicks 	dput(lower_old_dir_dentry);
627237fead6SMichael Halcrow 	dput(lower_new_dentry);
628237fead6SMichael Halcrow 	dput(lower_old_dentry);
629237fead6SMichael Halcrow 	return rc;
630237fead6SMichael Halcrow }
631237fead6SMichael Halcrow 
632b22e8fedSAl Viro static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
633237fead6SMichael Halcrow {
6346c988f57SMiklos Szeredi 	DEFINE_DELAYED_CALL(done);
6353a60a168STyler Hicks 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
6366c988f57SMiklos Szeredi 	const char *link;
637b22e8fedSAl Viro 	char *buf;
638addd65adSMichael Halcrow 	int rc;
639237fead6SMichael Halcrow 
6406c988f57SMiklos Szeredi 	link = vfs_get_link(lower_dentry, &done);
6416c988f57SMiklos Szeredi 	if (IS_ERR(link))
6426c988f57SMiklos Szeredi 		return ERR_CAST(link);
6436c988f57SMiklos Szeredi 
644b22e8fedSAl Viro 	rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
6456c988f57SMiklos Szeredi 						  link, strlen(link));
6466c988f57SMiklos Szeredi 	do_delayed_call(&done);
6476c988f57SMiklos Szeredi 	if (rc)
6486c988f57SMiklos Szeredi 		return ERR_PTR(rc);
6496c988f57SMiklos Szeredi 
6506c988f57SMiklos Szeredi 	return buf;
6513a60a168STyler Hicks }
6523a60a168STyler Hicks 
6536b255391SAl Viro static const char *ecryptfs_get_link(struct dentry *dentry,
654fceef393SAl Viro 				     struct inode *inode,
655fceef393SAl Viro 				     struct delayed_call *done)
656237fead6SMichael Halcrow {
657b22e8fedSAl Viro 	size_t len;
6586b255391SAl Viro 	char *buf;
6596b255391SAl Viro 
6606b255391SAl Viro 	if (!dentry)
6616b255391SAl Viro 		return ERR_PTR(-ECHILD);
6626b255391SAl Viro 
6636b255391SAl Viro 	buf = ecryptfs_readlink_lower(dentry, &len);
664b22e8fedSAl Viro 	if (IS_ERR(buf))
665680baacbSAl Viro 		return buf;
6662b0143b5SDavid Howells 	fsstack_copy_attr_atime(d_inode(dentry),
6672b0143b5SDavid Howells 				d_inode(ecryptfs_dentry_to_lower(dentry)));
668408bd629SAl Viro 	buf[len] = '\0';
669fceef393SAl Viro 	set_delayed_call(done, kfree_link, buf);
670fceef393SAl Viro 	return buf;
671237fead6SMichael Halcrow }
672237fead6SMichael Halcrow 
673237fead6SMichael Halcrow /**
674237fead6SMichael Halcrow  * upper_size_to_lower_size
675237fead6SMichael Halcrow  * @crypt_stat: Crypt_stat associated with file
676237fead6SMichael Halcrow  * @upper_size: Size of the upper file
677237fead6SMichael Halcrow  *
678cc11beffSMichael Halcrow  * Calculate the required size of the lower file based on the
679237fead6SMichael Halcrow  * specified size of the upper file. This calculation is based on the
680237fead6SMichael Halcrow  * number of headers in the underlying file and the extent size.
681237fead6SMichael Halcrow  *
682237fead6SMichael Halcrow  * Returns Calculated size of the lower file.
683237fead6SMichael Halcrow  */
684237fead6SMichael Halcrow static loff_t
685237fead6SMichael Halcrow upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
686237fead6SMichael Halcrow 			 loff_t upper_size)
687237fead6SMichael Halcrow {
688237fead6SMichael Halcrow 	loff_t lower_size;
689237fead6SMichael Halcrow 
690157f1071STyler Hicks 	lower_size = ecryptfs_lower_header_size(crypt_stat);
691237fead6SMichael Halcrow 	if (upper_size != 0) {
692237fead6SMichael Halcrow 		loff_t num_extents;
693237fead6SMichael Halcrow 
694237fead6SMichael Halcrow 		num_extents = upper_size >> crypt_stat->extent_shift;
695237fead6SMichael Halcrow 		if (upper_size & ~crypt_stat->extent_mask)
696237fead6SMichael Halcrow 			num_extents++;
697237fead6SMichael Halcrow 		lower_size += (num_extents * crypt_stat->extent_size);
698237fead6SMichael Halcrow 	}
699237fead6SMichael Halcrow 	return lower_size;
700237fead6SMichael Halcrow }
701237fead6SMichael Halcrow 
702237fead6SMichael Halcrow /**
7035f3ef64fSTyler Hicks  * truncate_upper
704237fead6SMichael Halcrow  * @dentry: The ecryptfs layer dentry
7055f3ef64fSTyler Hicks  * @ia: Address of the ecryptfs inode's attributes
7065f3ef64fSTyler Hicks  * @lower_ia: Address of the lower inode's attributes
707237fead6SMichael Halcrow  *
708237fead6SMichael Halcrow  * Function to handle truncations modifying the size of the file. Note
709237fead6SMichael Halcrow  * that the file sizes are interpolated. When expanding, we are simply
7105f3ef64fSTyler Hicks  * writing strings of 0's out. When truncating, we truncate the upper
7115f3ef64fSTyler Hicks  * inode and update the lower_ia according to the page index
7125f3ef64fSTyler Hicks  * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
7135f3ef64fSTyler Hicks  * the caller must use lower_ia in a call to notify_change() to perform
7145f3ef64fSTyler Hicks  * the truncation of the lower inode.
715237fead6SMichael Halcrow  *
716237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
717237fead6SMichael Halcrow  */
7185f3ef64fSTyler Hicks static int truncate_upper(struct dentry *dentry, struct iattr *ia,
7195f3ef64fSTyler Hicks 			  struct iattr *lower_ia)
720237fead6SMichael Halcrow {
721237fead6SMichael Halcrow 	int rc = 0;
7222b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
723237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
724237fead6SMichael Halcrow 	loff_t i_size = i_size_read(inode);
725237fead6SMichael Halcrow 	loff_t lower_size_before_truncate;
726237fead6SMichael Halcrow 	loff_t lower_size_after_truncate;
727237fead6SMichael Halcrow 
7285f3ef64fSTyler Hicks 	if (unlikely((ia->ia_size == i_size))) {
7295f3ef64fSTyler Hicks 		lower_ia->ia_valid &= ~ATTR_SIZE;
730332ab16fSTyler Hicks 		return 0;
7315f3ef64fSTyler Hicks 	}
7323b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(dentry, inode);
733332ab16fSTyler Hicks 	if (rc)
734332ab16fSTyler Hicks 		return rc;
7352b0143b5SDavid Howells 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
736237fead6SMichael Halcrow 	/* Switch on growing or shrinking file */
7375f3ef64fSTyler Hicks 	if (ia->ia_size > i_size) {
7382ed92554SMichael Halcrow 		char zero[] = { 0x00 };
739240e2df5SMichael Halcrow 
7405f3ef64fSTyler Hicks 		lower_ia->ia_valid &= ~ATTR_SIZE;
7412ed92554SMichael Halcrow 		/* Write a single 0 at the last position of the file;
7422ed92554SMichael Halcrow 		 * this triggers code that will fill in 0's throughout
7432ed92554SMichael Halcrow 		 * the intermediate portion of the previous end of the
7442ed92554SMichael Halcrow 		 * file and the new and of the file */
74548c1e44aSAl Viro 		rc = ecryptfs_write(inode, zero,
7465f3ef64fSTyler Hicks 				    (ia->ia_size - 1), 1);
7475f3ef64fSTyler Hicks 	} else { /* ia->ia_size < i_size_read(inode) */
7485f3ef64fSTyler Hicks 		/* We're chopping off all the pages down to the page
7495f3ef64fSTyler Hicks 		 * in which ia->ia_size is located. Fill in the end of
750ea1754a0SKirill A. Shutemov 		 * that page from (ia->ia_size & ~PAGE_MASK) to
751ea1754a0SKirill A. Shutemov 		 * PAGE_SIZE with zeros. */
75209cbfeafSKirill A. Shutemov 		size_t num_zeros = (PAGE_SIZE
75309cbfeafSKirill A. Shutemov 				    - (ia->ia_size & ~PAGE_MASK));
7542ed92554SMichael Halcrow 
7552c27c65eSChristoph Hellwig 		if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
7562c27c65eSChristoph Hellwig 			truncate_setsize(inode, ia->ia_size);
7575f3ef64fSTyler Hicks 			lower_ia->ia_size = ia->ia_size;
7585f3ef64fSTyler Hicks 			lower_ia->ia_valid |= ATTR_SIZE;
75948c1e44aSAl Viro 			goto out;
76013a791b4STyler Hicks 		}
7612ed92554SMichael Halcrow 		if (num_zeros) {
7622ed92554SMichael Halcrow 			char *zeros_virt;
7632ed92554SMichael Halcrow 
7642ed92554SMichael Halcrow 			zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
7652ed92554SMichael Halcrow 			if (!zeros_virt) {
7662ed92554SMichael Halcrow 				rc = -ENOMEM;
76748c1e44aSAl Viro 				goto out;
768240e2df5SMichael Halcrow 			}
76948c1e44aSAl Viro 			rc = ecryptfs_write(inode, zeros_virt,
7705f3ef64fSTyler Hicks 					    ia->ia_size, num_zeros);
7712ed92554SMichael Halcrow 			kfree(zeros_virt);
7725dda6992SMichael Halcrow 			if (rc) {
773240e2df5SMichael Halcrow 				printk(KERN_ERR "Error attempting to zero out "
774240e2df5SMichael Halcrow 				       "the remainder of the end page on "
775240e2df5SMichael Halcrow 				       "reducing truncate; rc = [%d]\n", rc);
77648c1e44aSAl Viro 				goto out;
777240e2df5SMichael Halcrow 			}
778240e2df5SMichael Halcrow 		}
7792c27c65eSChristoph Hellwig 		truncate_setsize(inode, ia->ia_size);
7800216f7f7SMichael Halcrow 		rc = ecryptfs_write_inode_size_to_metadata(inode);
781dd2a3b7aSMichael Halcrow 		if (rc) {
782dd2a3b7aSMichael Halcrow 			printk(KERN_ERR	"Problem with "
783dd2a3b7aSMichael Halcrow 			       "ecryptfs_write_inode_size_to_metadata; "
784dd2a3b7aSMichael Halcrow 			       "rc = [%d]\n", rc);
78548c1e44aSAl Viro 			goto out;
786dd2a3b7aSMichael Halcrow 		}
787237fead6SMichael Halcrow 		/* We are reducing the size of the ecryptfs file, and need to
788237fead6SMichael Halcrow 		 * know if we need to reduce the size of the lower file. */
789237fead6SMichael Halcrow 		lower_size_before_truncate =
790237fead6SMichael Halcrow 		    upper_size_to_lower_size(crypt_stat, i_size);
791237fead6SMichael Halcrow 		lower_size_after_truncate =
7925f3ef64fSTyler Hicks 		    upper_size_to_lower_size(crypt_stat, ia->ia_size);
7935f3ef64fSTyler Hicks 		if (lower_size_after_truncate < lower_size_before_truncate) {
7945f3ef64fSTyler Hicks 			lower_ia->ia_size = lower_size_after_truncate;
7955f3ef64fSTyler Hicks 			lower_ia->ia_valid |= ATTR_SIZE;
7965f3ef64fSTyler Hicks 		} else
7975f3ef64fSTyler Hicks 			lower_ia->ia_valid &= ~ATTR_SIZE;
798237fead6SMichael Halcrow 	}
799237fead6SMichael Halcrow out:
800332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
801237fead6SMichael Halcrow 	return rc;
802237fead6SMichael Halcrow }
803237fead6SMichael Halcrow 
804a261a039STyler Hicks static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
805a261a039STyler Hicks {
806a261a039STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
807a261a039STyler Hicks 	loff_t lower_oldsize, lower_newsize;
808a261a039STyler Hicks 
809a261a039STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
810a261a039STyler Hicks 	lower_oldsize = upper_size_to_lower_size(crypt_stat,
811a261a039STyler Hicks 						 i_size_read(inode));
812a261a039STyler Hicks 	lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
813a261a039STyler Hicks 	if (lower_newsize > lower_oldsize) {
814a261a039STyler Hicks 		/*
815a261a039STyler Hicks 		 * The eCryptfs inode and the new *lower* size are mixed here
816a261a039STyler Hicks 		 * because we may not have the lower i_mutex held and/or it may
817a261a039STyler Hicks 		 * not be appropriate to call inode_newsize_ok() with inodes
818a261a039STyler Hicks 		 * from other filesystems.
819a261a039STyler Hicks 		 */
820a261a039STyler Hicks 		return inode_newsize_ok(inode, lower_newsize);
821a261a039STyler Hicks 	}
822a261a039STyler Hicks 
823a261a039STyler Hicks 	return 0;
824a261a039STyler Hicks }
825a261a039STyler Hicks 
8265f3ef64fSTyler Hicks /**
8275f3ef64fSTyler Hicks  * ecryptfs_truncate
8285f3ef64fSTyler Hicks  * @dentry: The ecryptfs layer dentry
8295f3ef64fSTyler Hicks  * @new_length: The length to expand the file to
8305f3ef64fSTyler Hicks  *
8315f3ef64fSTyler Hicks  * Simple function that handles the truncation of an eCryptfs inode and
8325f3ef64fSTyler Hicks  * its corresponding lower inode.
8335f3ef64fSTyler Hicks  *
8345f3ef64fSTyler Hicks  * Returns zero on success; non-zero otherwise
8355f3ef64fSTyler Hicks  */
8365f3ef64fSTyler Hicks int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
8375f3ef64fSTyler Hicks {
8385f3ef64fSTyler Hicks 	struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
8395f3ef64fSTyler Hicks 	struct iattr lower_ia = { .ia_valid = 0 };
8405f3ef64fSTyler Hicks 	int rc;
8415f3ef64fSTyler Hicks 
8422b0143b5SDavid Howells 	rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
843a261a039STyler Hicks 	if (rc)
844a261a039STyler Hicks 		return rc;
845a261a039STyler Hicks 
8465f3ef64fSTyler Hicks 	rc = truncate_upper(dentry, &ia, &lower_ia);
8475f3ef64fSTyler Hicks 	if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
8485f3ef64fSTyler Hicks 		struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
8495f3ef64fSTyler Hicks 
8505955102cSAl Viro 		inode_lock(d_inode(lower_dentry));
85127ac0ffeSJ. Bruce Fields 		rc = notify_change(lower_dentry, &lower_ia, NULL);
8525955102cSAl Viro 		inode_unlock(d_inode(lower_dentry));
8535f3ef64fSTyler Hicks 	}
8545f3ef64fSTyler Hicks 	return rc;
8555f3ef64fSTyler Hicks }
8565f3ef64fSTyler Hicks 
857237fead6SMichael Halcrow static int
85810556cb2SAl Viro ecryptfs_permission(struct inode *inode, int mask)
859237fead6SMichael Halcrow {
860f419a2e3SAl Viro 	return inode_permission(ecryptfs_inode_to_lower(inode), mask);
861237fead6SMichael Halcrow }
862237fead6SMichael Halcrow 
863237fead6SMichael Halcrow /**
864237fead6SMichael Halcrow  * ecryptfs_setattr
865237fead6SMichael Halcrow  * @dentry: dentry handle to the inode to modify
866237fead6SMichael Halcrow  * @ia: Structure with flags of what to change and values
867237fead6SMichael Halcrow  *
868237fead6SMichael Halcrow  * Updates the metadata of an inode. If the update is to the size
869237fead6SMichael Halcrow  * i.e. truncation, then ecryptfs_truncate will handle the size modification
870237fead6SMichael Halcrow  * of both the ecryptfs inode and the lower inode.
871237fead6SMichael Halcrow  *
872237fead6SMichael Halcrow  * All other metadata changes will be passed right to the lower filesystem,
873237fead6SMichael Halcrow  * and we will just update our inode to look like the lower.
874237fead6SMichael Halcrow  */
875237fead6SMichael Halcrow static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
876237fead6SMichael Halcrow {
877237fead6SMichael Halcrow 	int rc = 0;
878237fead6SMichael Halcrow 	struct dentry *lower_dentry;
8795f3ef64fSTyler Hicks 	struct iattr lower_ia;
880237fead6SMichael Halcrow 	struct inode *inode;
881237fead6SMichael Halcrow 	struct inode *lower_inode;
882237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
883237fead6SMichael Halcrow 
8842b0143b5SDavid Howells 	crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
885e81f3340SHerbert Xu 	if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
886e81f3340SHerbert Xu 		rc = ecryptfs_init_crypt_stat(crypt_stat);
887e81f3340SHerbert Xu 		if (rc)
888e81f3340SHerbert Xu 			return rc;
889e81f3340SHerbert Xu 	}
8902b0143b5SDavid Howells 	inode = d_inode(dentry);
891237fead6SMichael Halcrow 	lower_inode = ecryptfs_inode_to_lower(inode);
892e10f281bSMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
893e10f281bSMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
894e36cb0b8SDavid Howells 	if (d_is_dir(dentry))
895e10f281bSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
896e36cb0b8SDavid Howells 	else if (d_is_reg(dentry)
89764ee4808SMichael Halcrow 		 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
89864ee4808SMichael Halcrow 		     || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
899e10f281bSMichael Halcrow 		struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
900e10f281bSMichael Halcrow 
901e10f281bSMichael Halcrow 		mount_crypt_stat = &ecryptfs_superblock_to_private(
902e10f281bSMichael Halcrow 			dentry->d_sb)->mount_crypt_stat;
9033b06b3ebSTyler Hicks 		rc = ecryptfs_get_lower_file(dentry, inode);
904332ab16fSTyler Hicks 		if (rc) {
905332ab16fSTyler Hicks 			mutex_unlock(&crypt_stat->cs_mutex);
906332ab16fSTyler Hicks 			goto out;
907332ab16fSTyler Hicks 		}
908d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_metadata(dentry);
909332ab16fSTyler Hicks 		ecryptfs_put_lower_file(inode);
9105dda6992SMichael Halcrow 		if (rc) {
911e10f281bSMichael Halcrow 			if (!(mount_crypt_stat->flags
912e10f281bSMichael Halcrow 			      & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
913e10f281bSMichael Halcrow 				rc = -EIO;
91425bd8174SMichael Halcrow 				printk(KERN_WARNING "Either the lower file "
915e10f281bSMichael Halcrow 				       "is not in a valid eCryptfs format, "
91625bd8174SMichael Halcrow 				       "or the key could not be retrieved. "
91725bd8174SMichael Halcrow 				       "Plaintext passthrough mode is not "
918e10f281bSMichael Halcrow 				       "enabled; returning -EIO\n");
919e10f281bSMichael Halcrow 				mutex_unlock(&crypt_stat->cs_mutex);
920e10f281bSMichael Halcrow 				goto out;
921e10f281bSMichael Halcrow 			}
922e10f281bSMichael Halcrow 			rc = 0;
9233aeb86eaSTyler Hicks 			crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
9243aeb86eaSTyler Hicks 					       | ECRYPTFS_ENCRYPTED);
925e10f281bSMichael Halcrow 		}
926e10f281bSMichael Halcrow 	}
927e10f281bSMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
928a261a039STyler Hicks 
92931051c85SJan Kara 	rc = setattr_prepare(dentry, ia);
930a261a039STyler Hicks 	if (rc)
931a261a039STyler Hicks 		goto out;
932a261a039STyler Hicks 	if (ia->ia_valid & ATTR_SIZE) {
933a261a039STyler Hicks 		rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
934a261a039STyler Hicks 		if (rc)
935a261a039STyler Hicks 			goto out;
936a261a039STyler Hicks 	}
937a261a039STyler Hicks 
9385f3ef64fSTyler Hicks 	memcpy(&lower_ia, ia, sizeof(lower_ia));
9395f3ef64fSTyler Hicks 	if (ia->ia_valid & ATTR_FILE)
9405f3ef64fSTyler Hicks 		lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
941237fead6SMichael Halcrow 	if (ia->ia_valid & ATTR_SIZE) {
9425f3ef64fSTyler Hicks 		rc = truncate_upper(dentry, ia, &lower_ia);
943237fead6SMichael Halcrow 		if (rc < 0)
944237fead6SMichael Halcrow 			goto out;
945237fead6SMichael Halcrow 	}
9461ac564ecSJeff Layton 
9471ac564ecSJeff Layton 	/*
9481ac564ecSJeff Layton 	 * mode change is for clearing setuid/setgid bits. Allow lower fs
9491ac564ecSJeff Layton 	 * to interpret this in its own way.
9501ac564ecSJeff Layton 	 */
9515f3ef64fSTyler Hicks 	if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
9525f3ef64fSTyler Hicks 		lower_ia.ia_valid &= ~ATTR_MODE;
9531ac564ecSJeff Layton 
9545955102cSAl Viro 	inode_lock(d_inode(lower_dentry));
95527ac0ffeSJ. Bruce Fields 	rc = notify_change(lower_dentry, &lower_ia, NULL);
9565955102cSAl Viro 	inode_unlock(d_inode(lower_dentry));
957237fead6SMichael Halcrow out:
9589afa2fb6SErez Zadok 	fsstack_copy_attr_all(inode, lower_inode);
959237fead6SMichael Halcrow 	return rc;
960237fead6SMichael Halcrow }
961237fead6SMichael Halcrow 
962a528d35eSDavid Howells static int ecryptfs_getattr_link(const struct path *path, struct kstat *stat,
963a528d35eSDavid Howells 				 u32 request_mask, unsigned int flags)
9643a60a168STyler Hicks {
965a528d35eSDavid Howells 	struct dentry *dentry = path->dentry;
9663a60a168STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
9673a60a168STyler Hicks 	int rc = 0;
9683a60a168STyler Hicks 
9693a60a168STyler Hicks 	mount_crypt_stat = &ecryptfs_superblock_to_private(
9703a60a168STyler Hicks 						dentry->d_sb)->mount_crypt_stat;
9712b0143b5SDavid Howells 	generic_fillattr(d_inode(dentry), stat);
9723a60a168STyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
9733a60a168STyler Hicks 		char *target;
9743a60a168STyler Hicks 		size_t targetsiz;
9753a60a168STyler Hicks 
976b22e8fedSAl Viro 		target = ecryptfs_readlink_lower(dentry, &targetsiz);
977b22e8fedSAl Viro 		if (!IS_ERR(target)) {
9783a60a168STyler Hicks 			kfree(target);
9793a60a168STyler Hicks 			stat->size = targetsiz;
980b22e8fedSAl Viro 		} else {
981b22e8fedSAl Viro 			rc = PTR_ERR(target);
9823a60a168STyler Hicks 		}
9833a60a168STyler Hicks 	}
9843a60a168STyler Hicks 	return rc;
9853a60a168STyler Hicks }
9863a60a168STyler Hicks 
987a528d35eSDavid Howells static int ecryptfs_getattr(const struct path *path, struct kstat *stat,
988a528d35eSDavid Howells 			    u32 request_mask, unsigned int flags)
989f8f484d1STyler Hicks {
990a528d35eSDavid Howells 	struct dentry *dentry = path->dentry;
991f8f484d1STyler Hicks 	struct kstat lower_stat;
992f8f484d1STyler Hicks 	int rc;
993f8f484d1STyler Hicks 
994a528d35eSDavid Howells 	rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat,
995a528d35eSDavid Howells 			 request_mask, flags);
996f8f484d1STyler Hicks 	if (!rc) {
9972b0143b5SDavid Howells 		fsstack_copy_attr_all(d_inode(dentry),
9982b0143b5SDavid Howells 				      ecryptfs_inode_to_lower(d_inode(dentry)));
9992b0143b5SDavid Howells 		generic_fillattr(d_inode(dentry), stat);
1000f8f484d1STyler Hicks 		stat->blocks = lower_stat.blocks;
1001f8f484d1STyler Hicks 	}
1002f8f484d1STyler Hicks 	return rc;
1003f8f484d1STyler Hicks }
1004f8f484d1STyler Hicks 
1005dd2a3b7aSMichael Halcrow int
10063767e255SAl Viro ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
10073767e255SAl Viro 		  const char *name, const void *value,
1008237fead6SMichael Halcrow 		  size_t size, int flags)
1009237fead6SMichael Halcrow {
10105d6c3191SAndreas Gruenbacher 	int rc;
1011237fead6SMichael Halcrow 	struct dentry *lower_dentry;
1012237fead6SMichael Halcrow 
1013237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
10145d6c3191SAndreas Gruenbacher 	if (!(d_inode(lower_dentry)->i_opflags & IOP_XATTR)) {
1015cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1016237fead6SMichael Halcrow 		goto out;
1017237fead6SMichael Halcrow 	}
101848b512e6SRoberto Sassu 	rc = vfs_setxattr(lower_dentry, name, value, size, flags);
10193767e255SAl Viro 	if (!rc && inode)
10203767e255SAl Viro 		fsstack_copy_attr_all(inode, d_inode(lower_dentry));
1021237fead6SMichael Halcrow out:
1022237fead6SMichael Halcrow 	return rc;
1023237fead6SMichael Halcrow }
1024237fead6SMichael Halcrow 
1025dd2a3b7aSMichael Halcrow ssize_t
1026ce23e640SAl Viro ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
1027ce23e640SAl Viro 			const char *name, void *value, size_t size)
1028d7cdc5feSMichael Halcrow {
10295d6c3191SAndreas Gruenbacher 	int rc;
1030d7cdc5feSMichael Halcrow 
10315d6c3191SAndreas Gruenbacher 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1032cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1033d7cdc5feSMichael Halcrow 		goto out;
1034d7cdc5feSMichael Halcrow 	}
1035ce23e640SAl Viro 	inode_lock(lower_inode);
10365d6c3191SAndreas Gruenbacher 	rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
1037ce23e640SAl Viro 	inode_unlock(lower_inode);
1038d7cdc5feSMichael Halcrow out:
1039d7cdc5feSMichael Halcrow 	return rc;
1040d7cdc5feSMichael Halcrow }
1041d7cdc5feSMichael Halcrow 
10427896b631SAdrian Bunk static ssize_t
1043ce23e640SAl Viro ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
1044ce23e640SAl Viro 		  const char *name, void *value, size_t size)
1045237fead6SMichael Halcrow {
1046ce23e640SAl Viro 	return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
1047ce23e640SAl Viro 				       ecryptfs_inode_to_lower(inode),
1048ce23e640SAl Viro 				       name, value, size);
1049237fead6SMichael Halcrow }
1050237fead6SMichael Halcrow 
1051237fead6SMichael Halcrow static ssize_t
1052237fead6SMichael Halcrow ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1053237fead6SMichael Halcrow {
1054237fead6SMichael Halcrow 	int rc = 0;
1055237fead6SMichael Halcrow 	struct dentry *lower_dentry;
1056237fead6SMichael Halcrow 
1057237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
10582b0143b5SDavid Howells 	if (!d_inode(lower_dentry)->i_op->listxattr) {
1059cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1060237fead6SMichael Halcrow 		goto out;
1061237fead6SMichael Halcrow 	}
10625955102cSAl Viro 	inode_lock(d_inode(lower_dentry));
10632b0143b5SDavid Howells 	rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
10645955102cSAl Viro 	inode_unlock(d_inode(lower_dentry));
1065237fead6SMichael Halcrow out:
1066237fead6SMichael Halcrow 	return rc;
1067237fead6SMichael Halcrow }
1068237fead6SMichael Halcrow 
10694b899da5SAndreas Gruenbacher static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
10704b899da5SAndreas Gruenbacher 				const char *name)
1071237fead6SMichael Halcrow {
10725d6c3191SAndreas Gruenbacher 	int rc;
1073237fead6SMichael Halcrow 	struct dentry *lower_dentry;
10744b899da5SAndreas Gruenbacher 	struct inode *lower_inode;
1075237fead6SMichael Halcrow 
1076237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
10774b899da5SAndreas Gruenbacher 	lower_inode = ecryptfs_inode_to_lower(inode);
10785d6c3191SAndreas Gruenbacher 	if (!(lower_inode->i_opflags & IOP_XATTR)) {
1079cfce08c6SChristian Pulvermacher 		rc = -EOPNOTSUPP;
1080237fead6SMichael Halcrow 		goto out;
1081237fead6SMichael Halcrow 	}
10824b899da5SAndreas Gruenbacher 	inode_lock(lower_inode);
10835d6c3191SAndreas Gruenbacher 	rc = __vfs_removexattr(lower_dentry, name);
10844b899da5SAndreas Gruenbacher 	inode_unlock(lower_inode);
1085237fead6SMichael Halcrow out:
1086237fead6SMichael Halcrow 	return rc;
1087237fead6SMichael Halcrow }
1088237fead6SMichael Halcrow 
1089754661f1SArjan van de Ven const struct inode_operations ecryptfs_symlink_iops = {
10906b255391SAl Viro 	.get_link = ecryptfs_get_link,
1091237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1092237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
10933a60a168STyler Hicks 	.getattr = ecryptfs_getattr_link,
1094237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
1095237fead6SMichael Halcrow };
1096237fead6SMichael Halcrow 
1097754661f1SArjan van de Ven const struct inode_operations ecryptfs_dir_iops = {
1098237fead6SMichael Halcrow 	.create = ecryptfs_create,
1099237fead6SMichael Halcrow 	.lookup = ecryptfs_lookup,
1100237fead6SMichael Halcrow 	.link = ecryptfs_link,
1101237fead6SMichael Halcrow 	.unlink = ecryptfs_unlink,
1102237fead6SMichael Halcrow 	.symlink = ecryptfs_symlink,
1103237fead6SMichael Halcrow 	.mkdir = ecryptfs_mkdir,
1104237fead6SMichael Halcrow 	.rmdir = ecryptfs_rmdir,
1105237fead6SMichael Halcrow 	.mknod = ecryptfs_mknod,
1106237fead6SMichael Halcrow 	.rename = ecryptfs_rename,
1107237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1108237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
1109237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
1110237fead6SMichael Halcrow };
1111237fead6SMichael Halcrow 
1112754661f1SArjan van de Ven const struct inode_operations ecryptfs_main_iops = {
1113237fead6SMichael Halcrow 	.permission = ecryptfs_permission,
1114237fead6SMichael Halcrow 	.setattr = ecryptfs_setattr,
1115f8f484d1STyler Hicks 	.getattr = ecryptfs_getattr,
1116237fead6SMichael Halcrow 	.listxattr = ecryptfs_listxattr,
11174b899da5SAndreas Gruenbacher };
11184b899da5SAndreas Gruenbacher 
11194b899da5SAndreas Gruenbacher static int ecryptfs_xattr_get(const struct xattr_handler *handler,
11204b899da5SAndreas Gruenbacher 			      struct dentry *dentry, struct inode *inode,
11214b899da5SAndreas Gruenbacher 			      const char *name, void *buffer, size_t size)
11224b899da5SAndreas Gruenbacher {
11234b899da5SAndreas Gruenbacher 	return ecryptfs_getxattr(dentry, inode, name, buffer, size);
11244b899da5SAndreas Gruenbacher }
11254b899da5SAndreas Gruenbacher 
11264b899da5SAndreas Gruenbacher static int ecryptfs_xattr_set(const struct xattr_handler *handler,
11274b899da5SAndreas Gruenbacher 			      struct dentry *dentry, struct inode *inode,
11284b899da5SAndreas Gruenbacher 			      const char *name, const void *value, size_t size,
11294b899da5SAndreas Gruenbacher 			      int flags)
11304b899da5SAndreas Gruenbacher {
11314b899da5SAndreas Gruenbacher 	if (value)
11324b899da5SAndreas Gruenbacher 		return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
11334b899da5SAndreas Gruenbacher 	else {
11344b899da5SAndreas Gruenbacher 		BUG_ON(flags != XATTR_REPLACE);
11354b899da5SAndreas Gruenbacher 		return ecryptfs_removexattr(dentry, inode, name);
11364b899da5SAndreas Gruenbacher 	}
11374b899da5SAndreas Gruenbacher }
11384b899da5SAndreas Gruenbacher 
11394b899da5SAndreas Gruenbacher const struct xattr_handler ecryptfs_xattr_handler = {
11404b899da5SAndreas Gruenbacher 	.prefix = "",  /* match anything */
11414b899da5SAndreas Gruenbacher 	.get = ecryptfs_xattr_get,
11424b899da5SAndreas Gruenbacher 	.set = ecryptfs_xattr_set,
11434b899da5SAndreas Gruenbacher };
11444b899da5SAndreas Gruenbacher 
11454b899da5SAndreas Gruenbacher const struct xattr_handler *ecryptfs_xattr_handlers[] = {
11464b899da5SAndreas Gruenbacher 	&ecryptfs_xattr_handler,
11474b899da5SAndreas Gruenbacher 	NULL
1148237fead6SMichael Halcrow };
1149