xref: /openbmc/linux/fs/ecryptfs/file.c (revision 25885a35a72007cf28ec5f9ba7169c5c798f7167)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a62187ebSLee Jones /*
3237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
4237fead6SMichael Halcrow  *
5237fead6SMichael Halcrow  * Copyright (C) 1997-2004 Erez Zadok
6237fead6SMichael Halcrow  * Copyright (C) 2001-2004 Stony Brook University
7dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
8237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9237fead6SMichael Halcrow  *   		Michael C. Thompson <mcthomps@us.ibm.com>
10237fead6SMichael Halcrow  */
11237fead6SMichael Halcrow 
12237fead6SMichael Halcrow #include <linux/file.h>
13237fead6SMichael Halcrow #include <linux/poll.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15237fead6SMichael Halcrow #include <linux/mount.h>
16237fead6SMichael Halcrow #include <linux/pagemap.h>
17237fead6SMichael Halcrow #include <linux/security.h>
18237fead6SMichael Halcrow #include <linux/compat.h>
190cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
20237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
21237fead6SMichael Halcrow 
22a62187ebSLee Jones /*
23237fead6SMichael Halcrow  * ecryptfs_read_update_atime
24237fead6SMichael Halcrow  *
25237fead6SMichael Halcrow  * generic_file_read updates the atime of upper layer inode.  But, it
26237fead6SMichael Halcrow  * doesn't give us a chance to update the atime of the lower layer
27237fead6SMichael Halcrow  * inode.  This function is a wrapper to generic_file_read.  It
28237fead6SMichael Halcrow  * updates the atime of the lower level inode if generic_file_read
29237fead6SMichael Halcrow  * returns without any errors. This is to be used only for file reads.
30237fead6SMichael Halcrow  * The function to be used for directory reads is ecryptfs_read.
31237fead6SMichael Halcrow  */
32237fead6SMichael Halcrow static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
3302797829SAl Viro 				struct iov_iter *to)
34237fead6SMichael Halcrow {
3538a708d7SEdward Shishkin 	ssize_t rc;
36cc18ec3cSMatthew Wilcox 	struct path *path;
37237fead6SMichael Halcrow 	struct file *file = iocb->ki_filp;
38237fead6SMichael Halcrow 
3902797829SAl Viro 	rc = generic_file_read_iter(iocb, to);
40237fead6SMichael Halcrow 	if (rc >= 0) {
41cc18ec3cSMatthew Wilcox 		path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
42cc18ec3cSMatthew Wilcox 		touch_atime(path);
43237fead6SMichael Halcrow 	}
44237fead6SMichael Halcrow 	return rc;
45237fead6SMichael Halcrow }
46237fead6SMichael Halcrow 
47237fead6SMichael Halcrow struct ecryptfs_getdents_callback {
485c0ba4e0SAl Viro 	struct dir_context ctx;
492de5f059SAl Viro 	struct dir_context *caller;
500747fdb2SAl Viro 	struct super_block *sb;
51237fead6SMichael Halcrow 	int filldir_called;
52237fead6SMichael Halcrow 	int entries_written;
53237fead6SMichael Halcrow };
54237fead6SMichael Halcrow 
557d6c7045SMichael Halcrow /* Inspired by generic filldir in fs/readdir.c */
56*25885a35SAl Viro static bool
57ac7576f4SMiklos Szeredi ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
58ac7576f4SMiklos Szeredi 		 int lower_namelen, loff_t offset, u64 ino, unsigned int d_type)
59237fead6SMichael Halcrow {
60237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback *buf =
61ac7576f4SMiklos Szeredi 		container_of(ctx, struct ecryptfs_getdents_callback, ctx);
62a8f12864SMichael Halcrow 	size_t name_size;
63addd65adSMichael Halcrow 	char *name;
64*25885a35SAl Viro 	int err;
65*25885a35SAl Viro 	bool res;
66237fead6SMichael Halcrow 
67237fead6SMichael Halcrow 	buf->filldir_called++;
68*25885a35SAl Viro 	err = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
690747fdb2SAl Viro 						   buf->sb, lower_name,
70addd65adSMichael Halcrow 						   lower_namelen);
71*25885a35SAl Viro 	if (err) {
72*25885a35SAl Viro 		if (err != -EINVAL) {
73e86281e7STyler Hicks 			ecryptfs_printk(KERN_DEBUG,
74e86281e7STyler Hicks 					"%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n",
75*25885a35SAl Viro 					__func__, lower_name, err);
76*25885a35SAl Viro 			return false;
77237fead6SMichael Halcrow 		}
78e86281e7STyler Hicks 
79e86281e7STyler Hicks 		/* Mask -EINVAL errors as these are most likely due a plaintext
80e86281e7STyler Hicks 		 * filename present in the lower filesystem despite filename
81e86281e7STyler Hicks 		 * encryption being enabled. One unavoidable example would be
82e86281e7STyler Hicks 		 * the "lost+found" dentry in the root directory of an Ext4
83e86281e7STyler Hicks 		 * filesystem.
84e86281e7STyler Hicks 		 */
85*25885a35SAl Viro 		return true;
86e86281e7STyler Hicks 	}
87e86281e7STyler Hicks 
882de5f059SAl Viro 	buf->caller->pos = buf->ctx.pos;
89*25885a35SAl Viro 	res = dir_emit(buf->caller, name, name_size, ino, d_type);
90addd65adSMichael Halcrow 	kfree(name);
91*25885a35SAl Viro 	if (res)
92237fead6SMichael Halcrow 		buf->entries_written++;
93*25885a35SAl Viro 	return res;
94237fead6SMichael Halcrow }
95237fead6SMichael Halcrow 
96237fead6SMichael Halcrow /**
97237fead6SMichael Halcrow  * ecryptfs_readdir
98addd65adSMichael Halcrow  * @file: The eCryptfs directory file
992de5f059SAl Viro  * @ctx: The actor to feed the entries to
100237fead6SMichael Halcrow  */
1012de5f059SAl Viro static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
102237fead6SMichael Halcrow {
103237fead6SMichael Halcrow 	int rc;
104237fead6SMichael Halcrow 	struct file *lower_file;
1050747fdb2SAl Viro 	struct inode *inode = file_inode(file);
1062de5f059SAl Viro 	struct ecryptfs_getdents_callback buf = {
1072de5f059SAl Viro 		.ctx.actor = ecryptfs_filldir,
1082de5f059SAl Viro 		.caller = ctx,
1090747fdb2SAl Viro 		.sb = inode->i_sb,
1102de5f059SAl Viro 	};
111237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
1125c0ba4e0SAl Viro 	rc = iterate_dir(lower_file, &buf.ctx);
1132de5f059SAl Viro 	ctx->pos = buf.ctx.pos;
114*25885a35SAl Viro 	if (rc >= 0 && (buf.entries_written || !buf.filldir_called))
115*25885a35SAl Viro 		fsstack_copy_attr_atime(inode, file_inode(lower_file));
116237fead6SMichael Halcrow 	return rc;
117237fead6SMichael Halcrow }
118237fead6SMichael Halcrow 
119237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache;
120237fead6SMichael Halcrow 
121e3ccaa97STyler Hicks static int read_or_initialize_metadata(struct dentry *dentry)
122e3ccaa97STyler Hicks {
1232b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
124e3ccaa97STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
125e3ccaa97STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
126e3ccaa97STyler Hicks 	int rc;
127e3ccaa97STyler Hicks 
128e3ccaa97STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
129e3ccaa97STyler Hicks 	mount_crypt_stat = &ecryptfs_superblock_to_private(
130e3ccaa97STyler Hicks 						inode->i_sb)->mount_crypt_stat;
131e3ccaa97STyler Hicks 	mutex_lock(&crypt_stat->cs_mutex);
132e3ccaa97STyler Hicks 
133e3ccaa97STyler Hicks 	if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
134e3ccaa97STyler Hicks 	    crypt_stat->flags & ECRYPTFS_KEY_VALID) {
135e3ccaa97STyler Hicks 		rc = 0;
136e3ccaa97STyler Hicks 		goto out;
137e3ccaa97STyler Hicks 	}
138e3ccaa97STyler Hicks 
139e3ccaa97STyler Hicks 	rc = ecryptfs_read_metadata(dentry);
140e3ccaa97STyler Hicks 	if (!rc)
141e3ccaa97STyler Hicks 		goto out;
142e3ccaa97STyler Hicks 
143e3ccaa97STyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
144e3ccaa97STyler Hicks 		crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
145e3ccaa97STyler Hicks 				       | ECRYPTFS_ENCRYPTED);
146e3ccaa97STyler Hicks 		rc = 0;
147e3ccaa97STyler Hicks 		goto out;
148e3ccaa97STyler Hicks 	}
149e3ccaa97STyler Hicks 
150e3ccaa97STyler Hicks 	if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
151e3ccaa97STyler Hicks 	    !i_size_read(ecryptfs_inode_to_lower(inode))) {
152e3ccaa97STyler Hicks 		rc = ecryptfs_initialize_file(dentry, inode);
153e3ccaa97STyler Hicks 		if (!rc)
154e3ccaa97STyler Hicks 			goto out;
155e3ccaa97STyler Hicks 	}
156e3ccaa97STyler Hicks 
157e3ccaa97STyler Hicks 	rc = -EIO;
158e3ccaa97STyler Hicks out:
159e3ccaa97STyler Hicks 	mutex_unlock(&crypt_stat->cs_mutex);
160e3ccaa97STyler Hicks 	return rc;
161e3ccaa97STyler Hicks }
162e3ccaa97STyler Hicks 
163f0fe970dSJeff Mahoney static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
164f0fe970dSJeff Mahoney {
165f0fe970dSJeff Mahoney 	struct file *lower_file = ecryptfs_file_to_lower(file);
166f0fe970dSJeff Mahoney 	/*
167f0fe970dSJeff Mahoney 	 * Don't allow mmap on top of file systems that don't support it
168f0fe970dSJeff Mahoney 	 * natively.  If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
169f0fe970dSJeff Mahoney 	 * allows recursive mounting, this will need to be extended.
170f0fe970dSJeff Mahoney 	 */
171f0fe970dSJeff Mahoney 	if (!lower_file->f_op->mmap)
172f0fe970dSJeff Mahoney 		return -ENODEV;
173f0fe970dSJeff Mahoney 	return generic_file_mmap(file, vma);
174f0fe970dSJeff Mahoney }
175f0fe970dSJeff Mahoney 
176237fead6SMichael Halcrow /**
177237fead6SMichael Halcrow  * ecryptfs_open
17840f0fd37SChris J Arges  * @inode: inode specifying file to open
179237fead6SMichael Halcrow  * @file: Structure to return filled in
180237fead6SMichael Halcrow  *
181237fead6SMichael Halcrow  * Opens the file specified by inode.
182237fead6SMichael Halcrow  *
183237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
184237fead6SMichael Halcrow  */
185237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file)
186237fead6SMichael Halcrow {
187237fead6SMichael Halcrow 	int rc = 0;
188237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
189bd243a4bSJosef "Jeff" Sipek 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
190237fead6SMichael Halcrow 	/* Private value of ecryptfs_dentry allocated in
191237fead6SMichael Halcrow 	 * ecryptfs_lookup() */
192237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info;
193237fead6SMichael Halcrow 
194237fead6SMichael Halcrow 	/* Released in ecryptfs_release or end of function if failure */
195c3762229SRobert P. J. Day 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
196237fead6SMichael Halcrow 	ecryptfs_set_file_private(file, file_info);
197237fead6SMichael Halcrow 	if (!file_info) {
198237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
199237fead6SMichael Halcrow 				"Error attempting to allocate memory\n");
200237fead6SMichael Halcrow 		rc = -ENOMEM;
201237fead6SMichael Halcrow 		goto out;
202237fead6SMichael Halcrow 	}
203237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
204237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
205e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
206237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
207237fead6SMichael Halcrow 		/* Policy code enabled in future release */
2082ed92554SMichael Halcrow 		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
2092ed92554SMichael Halcrow 				      | ECRYPTFS_ENCRYPTED);
210237fead6SMichael Halcrow 	}
211237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
2123b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
21372b55fffSMichael Halcrow 	if (rc) {
21472b55fffSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
215332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2169e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2179e78d14aSDavid Howells 			ecryptfs_dentry, rc);
218ceeab929SJulia Lawall 		goto out_free;
21972b55fffSMichael Halcrow 	}
2200abe1169SRoberto Sassu 	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
2210abe1169SRoberto Sassu 	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
222e27759d7SErez Zadok 		rc = -EPERM;
223332ab16fSTyler Hicks 		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
224e27759d7SErez Zadok 		       "file must hence be opened RO\n", __func__);
225332ab16fSTyler Hicks 		goto out_put;
226e27759d7SErez Zadok 	}
2272ed92554SMichael Halcrow 	ecryptfs_set_file_lower(
2282ed92554SMichael Halcrow 		file, ecryptfs_inode_to_private(inode)->lower_file);
229e3ccaa97STyler Hicks 	rc = read_or_initialize_metadata(ecryptfs_dentry);
230e3ccaa97STyler Hicks 	if (rc)
231332ab16fSTyler Hicks 		goto out_put;
232888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
233888d57bbSJoe Perches 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
234888d57bbSJoe Perches 			(unsigned long long)i_size_read(inode));
235237fead6SMichael Halcrow 	goto out;
236332ab16fSTyler Hicks out_put:
237332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2382ed92554SMichael Halcrow out_free:
239237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
240237fead6SMichael Halcrow 			ecryptfs_file_to_private(file));
241237fead6SMichael Halcrow out:
242237fead6SMichael Halcrow 	return rc;
243237fead6SMichael Halcrow }
244237fead6SMichael Halcrow 
2456a480a78SAl Viro /**
2466a480a78SAl Viro  * ecryptfs_dir_open
24740f0fd37SChris J Arges  * @inode: inode specifying file to open
2486a480a78SAl Viro  * @file: Structure to return filled in
2496a480a78SAl Viro  *
2506a480a78SAl Viro  * Opens the file specified by inode.
2516a480a78SAl Viro  *
2526a480a78SAl Viro  * Returns zero on success; non-zero otherwise
2536a480a78SAl Viro  */
2546a480a78SAl Viro static int ecryptfs_dir_open(struct inode *inode, struct file *file)
2556a480a78SAl Viro {
2566a480a78SAl Viro 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
2576a480a78SAl Viro 	/* Private value of ecryptfs_dentry allocated in
2586a480a78SAl Viro 	 * ecryptfs_lookup() */
2596a480a78SAl Viro 	struct ecryptfs_file_info *file_info;
2606a480a78SAl Viro 	struct file *lower_file;
2616a480a78SAl Viro 
2626a480a78SAl Viro 	/* Released in ecryptfs_release or end of function if failure */
2636a480a78SAl Viro 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
2646a480a78SAl Viro 	ecryptfs_set_file_private(file, file_info);
2656a480a78SAl Viro 	if (unlikely(!file_info)) {
2666a480a78SAl Viro 		ecryptfs_printk(KERN_ERR,
2676a480a78SAl Viro 				"Error attempting to allocate memory\n");
2686a480a78SAl Viro 		return -ENOMEM;
2696a480a78SAl Viro 	}
2706a480a78SAl Viro 	lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
2716a480a78SAl Viro 				 file->f_flags, current_cred());
2726a480a78SAl Viro 	if (IS_ERR(lower_file)) {
2736a480a78SAl Viro 		printk(KERN_ERR "%s: Error attempting to initialize "
2746a480a78SAl Viro 			"the lower file for the dentry with name "
2756a480a78SAl Viro 			"[%pd]; rc = [%ld]\n", __func__,
2766a480a78SAl Viro 			ecryptfs_dentry, PTR_ERR(lower_file));
2776a480a78SAl Viro 		kmem_cache_free(ecryptfs_file_info_cache, file_info);
2786a480a78SAl Viro 		return PTR_ERR(lower_file);
2796a480a78SAl Viro 	}
2806a480a78SAl Viro 	ecryptfs_set_file_lower(file, lower_file);
2816a480a78SAl Viro 	return 0;
2826a480a78SAl Viro }
2836a480a78SAl Viro 
284237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td)
285237fead6SMichael Halcrow {
28664e6651dSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
28764e6651dSTyler Hicks 
28872c2d531SAl Viro 	if (lower_file->f_op->flush) {
28964e6651dSTyler Hicks 		filemap_write_and_wait(file->f_mapping);
29064e6651dSTyler Hicks 		return lower_file->f_op->flush(lower_file, td);
29164e6651dSTyler Hicks 	}
29264e6651dSTyler Hicks 
29364e6651dSTyler Hicks 	return 0;
294237fead6SMichael Halcrow }
295237fead6SMichael Halcrow 
296237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file)
297237fead6SMichael Halcrow {
298332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2992ed92554SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
3002ed92554SMichael Halcrow 			ecryptfs_file_to_private(file));
3012ed92554SMichael Halcrow 	return 0;
302237fead6SMichael Halcrow }
303237fead6SMichael Halcrow 
3046a480a78SAl Viro static int ecryptfs_dir_release(struct inode *inode, struct file *file)
3056a480a78SAl Viro {
3066a480a78SAl Viro 	fput(ecryptfs_file_to_lower(file));
3076a480a78SAl Viro 	kmem_cache_free(ecryptfs_file_info_cache,
3086a480a78SAl Viro 			ecryptfs_file_to_private(file));
3096a480a78SAl Viro 	return 0;
3106a480a78SAl Viro }
3116a480a78SAl Viro 
3126a480a78SAl Viro static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
3136a480a78SAl Viro {
3146a480a78SAl Viro 	return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
3156a480a78SAl Viro }
3166a480a78SAl Viro 
317237fead6SMichael Halcrow static int
31802c24a82SJosef Bacik ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
319237fead6SMichael Halcrow {
320bc5abcf7STyler Hicks 	int rc;
321bc5abcf7STyler Hicks 
3226d4b5124SJeff Layton 	rc = file_write_and_wait(file);
323bc5abcf7STyler Hicks 	if (rc)
324bc5abcf7STyler Hicks 		return rc;
325bc5abcf7STyler Hicks 
326821f7494STyler Hicks 	return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
327237fead6SMichael Halcrow }
328237fead6SMichael Halcrow 
329237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag)
330237fead6SMichael Halcrow {
331237fead6SMichael Halcrow 	int rc = 0;
332237fead6SMichael Halcrow 	struct file *lower_file = NULL;
333237fead6SMichael Halcrow 
334237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
33572c2d531SAl Viro 	if (lower_file->f_op->fasync)
336237fead6SMichael Halcrow 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
337237fead6SMichael Halcrow 	return rc;
338237fead6SMichael Halcrow }
339237fead6SMichael Halcrow 
340c43f7b8fSTyler Hicks static long
341c43f7b8fSTyler Hicks ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
342c43f7b8fSTyler Hicks {
3432000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
344c43f7b8fSTyler Hicks 	long rc = -ENOTTY;
345c43f7b8fSTyler Hicks 
3466d65261aSTyler Hicks 	if (!lower_file->f_op->unlocked_ioctl)
347c43f7b8fSTyler Hicks 		return rc;
3486d65261aSTyler Hicks 
3496d65261aSTyler Hicks 	switch (cmd) {
3506d65261aSTyler Hicks 	case FITRIM:
3516d65261aSTyler Hicks 	case FS_IOC_GETFLAGS:
3526d65261aSTyler Hicks 	case FS_IOC_SETFLAGS:
3536d65261aSTyler Hicks 	case FS_IOC_GETVERSION:
3546d65261aSTyler Hicks 	case FS_IOC_SETVERSION:
3556d65261aSTyler Hicks 		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
3566d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
3576d65261aSTyler Hicks 
3586d65261aSTyler Hicks 		return rc;
3596d65261aSTyler Hicks 	default:
3606d65261aSTyler Hicks 		return rc;
3616d65261aSTyler Hicks 	}
362c43f7b8fSTyler Hicks }
363c43f7b8fSTyler Hicks 
364c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
365c43f7b8fSTyler Hicks static long
366c43f7b8fSTyler Hicks ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
367c43f7b8fSTyler Hicks {
3682000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
369c43f7b8fSTyler Hicks 	long rc = -ENOIOCTLCMD;
370c43f7b8fSTyler Hicks 
3716d65261aSTyler Hicks 	if (!lower_file->f_op->compat_ioctl)
372c43f7b8fSTyler Hicks 		return rc;
3736d65261aSTyler Hicks 
3746d65261aSTyler Hicks 	switch (cmd) {
375314999dcSArnd Bergmann 	case FITRIM:
3766d65261aSTyler Hicks 	case FS_IOC32_GETFLAGS:
3776d65261aSTyler Hicks 	case FS_IOC32_SETFLAGS:
3786d65261aSTyler Hicks 	case FS_IOC32_GETVERSION:
3796d65261aSTyler Hicks 	case FS_IOC32_SETVERSION:
3806d65261aSTyler Hicks 		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
3816d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
3826d65261aSTyler Hicks 
3836d65261aSTyler Hicks 		return rc;
3846d65261aSTyler Hicks 	default:
3856d65261aSTyler Hicks 		return rc;
3866d65261aSTyler Hicks 	}
387c43f7b8fSTyler Hicks }
388c43f7b8fSTyler Hicks #endif
389237fead6SMichael Halcrow 
390237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = {
39151a16a9cSAl Viro 	.iterate_shared = ecryptfs_readdir,
392323ef68fSAndy Whitcroft 	.read = generic_read_dir,
393c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
394c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
395c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
396c43f7b8fSTyler Hicks #endif
3976a480a78SAl Viro 	.open = ecryptfs_dir_open,
3986a480a78SAl Viro 	.release = ecryptfs_dir_release,
399237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
4006a480a78SAl Viro 	.llseek = ecryptfs_dir_llseek,
401237fead6SMichael Halcrow };
402237fead6SMichael Halcrow 
403237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = {
40453a2731fSMichael Halcrow 	.llseek = generic_file_llseek,
40502797829SAl Viro 	.read_iter = ecryptfs_read_update_atime,
4068174202bSAl Viro 	.write_iter = generic_file_write_iter,
407c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
408c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
409c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
410c43f7b8fSTyler Hicks #endif
411f0fe970dSJeff Mahoney 	.mmap = ecryptfs_mmap,
412237fead6SMichael Halcrow 	.open = ecryptfs_open,
413237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
414237fead6SMichael Halcrow 	.release = ecryptfs_release,
415237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
416237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
417e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
418237fead6SMichael Halcrow };
419