xref: /linux/fs/ecryptfs/file.c (revision 314999dcbca75c3ca8aaba102875d51ab409cf87)
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 <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 
22237fead6SMichael Halcrow /**
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 */
56237fead6SMichael Halcrow static int
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;
64237fead6SMichael Halcrow 	int rc;
65237fead6SMichael Halcrow 
66237fead6SMichael Halcrow 	buf->filldir_called++;
67addd65adSMichael Halcrow 	rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
680747fdb2SAl Viro 						  buf->sb, lower_name,
69addd65adSMichael Halcrow 						  lower_namelen);
70addd65adSMichael Halcrow 	if (rc) {
71e86281e7STyler Hicks 		if (rc != -EINVAL) {
72e86281e7STyler Hicks 			ecryptfs_printk(KERN_DEBUG,
73e86281e7STyler Hicks 					"%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n",
74e86281e7STyler Hicks 					__func__, lower_name, rc);
75e86281e7STyler Hicks 			return rc;
76237fead6SMichael Halcrow 		}
77e86281e7STyler Hicks 
78e86281e7STyler Hicks 		/* Mask -EINVAL errors as these are most likely due a plaintext
79e86281e7STyler Hicks 		 * filename present in the lower filesystem despite filename
80e86281e7STyler Hicks 		 * encryption being enabled. One unavoidable example would be
81e86281e7STyler Hicks 		 * the "lost+found" dentry in the root directory of an Ext4
82e86281e7STyler Hicks 		 * filesystem.
83e86281e7STyler Hicks 		 */
84e86281e7STyler Hicks 		return 0;
85e86281e7STyler Hicks 	}
86e86281e7STyler Hicks 
872de5f059SAl Viro 	buf->caller->pos = buf->ctx.pos;
882de5f059SAl Viro 	rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
89addd65adSMichael Halcrow 	kfree(name);
902de5f059SAl Viro 	if (!rc)
91237fead6SMichael Halcrow 		buf->entries_written++;
92e86281e7STyler Hicks 
93237fead6SMichael Halcrow 	return rc;
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;
1147d6c7045SMichael Halcrow 	if (rc < 0)
1157d6c7045SMichael Halcrow 		goto out;
1167d6c7045SMichael Halcrow 	if (buf.filldir_called && !buf.entries_written)
1177d6c7045SMichael Halcrow 		goto out;
118237fead6SMichael Halcrow 	if (rc >= 0)
1197d6c7045SMichael Halcrow 		fsstack_copy_attr_atime(inode,
120496ad9aaSAl Viro 					file_inode(lower_file));
1217d6c7045SMichael Halcrow out:
122237fead6SMichael Halcrow 	return rc;
123237fead6SMichael Halcrow }
124237fead6SMichael Halcrow 
125237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache;
126237fead6SMichael Halcrow 
127e3ccaa97STyler Hicks static int read_or_initialize_metadata(struct dentry *dentry)
128e3ccaa97STyler Hicks {
1292b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
130e3ccaa97STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
131e3ccaa97STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
132e3ccaa97STyler Hicks 	int rc;
133e3ccaa97STyler Hicks 
134e3ccaa97STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
135e3ccaa97STyler Hicks 	mount_crypt_stat = &ecryptfs_superblock_to_private(
136e3ccaa97STyler Hicks 						inode->i_sb)->mount_crypt_stat;
137e3ccaa97STyler Hicks 	mutex_lock(&crypt_stat->cs_mutex);
138e3ccaa97STyler Hicks 
139e3ccaa97STyler Hicks 	if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
140e3ccaa97STyler Hicks 	    crypt_stat->flags & ECRYPTFS_KEY_VALID) {
141e3ccaa97STyler Hicks 		rc = 0;
142e3ccaa97STyler Hicks 		goto out;
143e3ccaa97STyler Hicks 	}
144e3ccaa97STyler Hicks 
145e3ccaa97STyler Hicks 	rc = ecryptfs_read_metadata(dentry);
146e3ccaa97STyler Hicks 	if (!rc)
147e3ccaa97STyler Hicks 		goto out;
148e3ccaa97STyler Hicks 
149e3ccaa97STyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
150e3ccaa97STyler Hicks 		crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
151e3ccaa97STyler Hicks 				       | ECRYPTFS_ENCRYPTED);
152e3ccaa97STyler Hicks 		rc = 0;
153e3ccaa97STyler Hicks 		goto out;
154e3ccaa97STyler Hicks 	}
155e3ccaa97STyler Hicks 
156e3ccaa97STyler Hicks 	if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
157e3ccaa97STyler Hicks 	    !i_size_read(ecryptfs_inode_to_lower(inode))) {
158e3ccaa97STyler Hicks 		rc = ecryptfs_initialize_file(dentry, inode);
159e3ccaa97STyler Hicks 		if (!rc)
160e3ccaa97STyler Hicks 			goto out;
161e3ccaa97STyler Hicks 	}
162e3ccaa97STyler Hicks 
163e3ccaa97STyler Hicks 	rc = -EIO;
164e3ccaa97STyler Hicks out:
165e3ccaa97STyler Hicks 	mutex_unlock(&crypt_stat->cs_mutex);
166e3ccaa97STyler Hicks 	return rc;
167e3ccaa97STyler Hicks }
168e3ccaa97STyler Hicks 
169f0fe970dSJeff Mahoney static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
170f0fe970dSJeff Mahoney {
171f0fe970dSJeff Mahoney 	struct file *lower_file = ecryptfs_file_to_lower(file);
172f0fe970dSJeff Mahoney 	/*
173f0fe970dSJeff Mahoney 	 * Don't allow mmap on top of file systems that don't support it
174f0fe970dSJeff Mahoney 	 * natively.  If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
175f0fe970dSJeff Mahoney 	 * allows recursive mounting, this will need to be extended.
176f0fe970dSJeff Mahoney 	 */
177f0fe970dSJeff Mahoney 	if (!lower_file->f_op->mmap)
178f0fe970dSJeff Mahoney 		return -ENODEV;
179f0fe970dSJeff Mahoney 	return generic_file_mmap(file, vma);
180f0fe970dSJeff Mahoney }
181f0fe970dSJeff Mahoney 
182237fead6SMichael Halcrow /**
183237fead6SMichael Halcrow  * ecryptfs_open
18440f0fd37SChris J Arges  * @inode: inode specifying file to open
185237fead6SMichael Halcrow  * @file: Structure to return filled in
186237fead6SMichael Halcrow  *
187237fead6SMichael Halcrow  * Opens the file specified by inode.
188237fead6SMichael Halcrow  *
189237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
190237fead6SMichael Halcrow  */
191237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file)
192237fead6SMichael Halcrow {
193237fead6SMichael Halcrow 	int rc = 0;
194237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
195bd243a4bSJosef "Jeff" Sipek 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
196237fead6SMichael Halcrow 	/* Private value of ecryptfs_dentry allocated in
197237fead6SMichael Halcrow 	 * ecryptfs_lookup() */
198237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info;
199237fead6SMichael Halcrow 
200237fead6SMichael Halcrow 	/* Released in ecryptfs_release or end of function if failure */
201c3762229SRobert P. J. Day 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
202237fead6SMichael Halcrow 	ecryptfs_set_file_private(file, file_info);
203237fead6SMichael Halcrow 	if (!file_info) {
204237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
205237fead6SMichael Halcrow 				"Error attempting to allocate memory\n");
206237fead6SMichael Halcrow 		rc = -ENOMEM;
207237fead6SMichael Halcrow 		goto out;
208237fead6SMichael Halcrow 	}
209237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
210237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
211e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
212237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
213237fead6SMichael Halcrow 		/* Policy code enabled in future release */
2142ed92554SMichael Halcrow 		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
2152ed92554SMichael Halcrow 				      | ECRYPTFS_ENCRYPTED);
216237fead6SMichael Halcrow 	}
217237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
2183b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
21972b55fffSMichael Halcrow 	if (rc) {
22072b55fffSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
221332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2229e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2239e78d14aSDavid Howells 			ecryptfs_dentry, rc);
224ceeab929SJulia Lawall 		goto out_free;
22572b55fffSMichael Halcrow 	}
2260abe1169SRoberto Sassu 	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
2270abe1169SRoberto Sassu 	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
228e27759d7SErez Zadok 		rc = -EPERM;
229332ab16fSTyler Hicks 		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
230e27759d7SErez Zadok 		       "file must hence be opened RO\n", __func__);
231332ab16fSTyler Hicks 		goto out_put;
232e27759d7SErez Zadok 	}
2332ed92554SMichael Halcrow 	ecryptfs_set_file_lower(
2342ed92554SMichael Halcrow 		file, ecryptfs_inode_to_private(inode)->lower_file);
235e3ccaa97STyler Hicks 	rc = read_or_initialize_metadata(ecryptfs_dentry);
236e3ccaa97STyler Hicks 	if (rc)
237332ab16fSTyler Hicks 		goto out_put;
238888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
239888d57bbSJoe Perches 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
240888d57bbSJoe Perches 			(unsigned long long)i_size_read(inode));
241237fead6SMichael Halcrow 	goto out;
242332ab16fSTyler Hicks out_put:
243332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2442ed92554SMichael Halcrow out_free:
245237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
246237fead6SMichael Halcrow 			ecryptfs_file_to_private(file));
247237fead6SMichael Halcrow out:
248237fead6SMichael Halcrow 	return rc;
249237fead6SMichael Halcrow }
250237fead6SMichael Halcrow 
2516a480a78SAl Viro /**
2526a480a78SAl Viro  * ecryptfs_dir_open
25340f0fd37SChris J Arges  * @inode: inode specifying file to open
2546a480a78SAl Viro  * @file: Structure to return filled in
2556a480a78SAl Viro  *
2566a480a78SAl Viro  * Opens the file specified by inode.
2576a480a78SAl Viro  *
2586a480a78SAl Viro  * Returns zero on success; non-zero otherwise
2596a480a78SAl Viro  */
2606a480a78SAl Viro static int ecryptfs_dir_open(struct inode *inode, struct file *file)
2616a480a78SAl Viro {
2626a480a78SAl Viro 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
2636a480a78SAl Viro 	/* Private value of ecryptfs_dentry allocated in
2646a480a78SAl Viro 	 * ecryptfs_lookup() */
2656a480a78SAl Viro 	struct ecryptfs_file_info *file_info;
2666a480a78SAl Viro 	struct file *lower_file;
2676a480a78SAl Viro 
2686a480a78SAl Viro 	/* Released in ecryptfs_release or end of function if failure */
2696a480a78SAl Viro 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
2706a480a78SAl Viro 	ecryptfs_set_file_private(file, file_info);
2716a480a78SAl Viro 	if (unlikely(!file_info)) {
2726a480a78SAl Viro 		ecryptfs_printk(KERN_ERR,
2736a480a78SAl Viro 				"Error attempting to allocate memory\n");
2746a480a78SAl Viro 		return -ENOMEM;
2756a480a78SAl Viro 	}
2766a480a78SAl Viro 	lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
2776a480a78SAl Viro 				 file->f_flags, current_cred());
2786a480a78SAl Viro 	if (IS_ERR(lower_file)) {
2796a480a78SAl Viro 		printk(KERN_ERR "%s: Error attempting to initialize "
2806a480a78SAl Viro 			"the lower file for the dentry with name "
2816a480a78SAl Viro 			"[%pd]; rc = [%ld]\n", __func__,
2826a480a78SAl Viro 			ecryptfs_dentry, PTR_ERR(lower_file));
2836a480a78SAl Viro 		kmem_cache_free(ecryptfs_file_info_cache, file_info);
2846a480a78SAl Viro 		return PTR_ERR(lower_file);
2856a480a78SAl Viro 	}
2866a480a78SAl Viro 	ecryptfs_set_file_lower(file, lower_file);
2876a480a78SAl Viro 	return 0;
2886a480a78SAl Viro }
2896a480a78SAl Viro 
290237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td)
291237fead6SMichael Halcrow {
29264e6651dSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
29364e6651dSTyler Hicks 
29472c2d531SAl Viro 	if (lower_file->f_op->flush) {
29564e6651dSTyler Hicks 		filemap_write_and_wait(file->f_mapping);
29664e6651dSTyler Hicks 		return lower_file->f_op->flush(lower_file, td);
29764e6651dSTyler Hicks 	}
29864e6651dSTyler Hicks 
29964e6651dSTyler Hicks 	return 0;
300237fead6SMichael Halcrow }
301237fead6SMichael Halcrow 
302237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file)
303237fead6SMichael Halcrow {
304332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
3052ed92554SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
3062ed92554SMichael Halcrow 			ecryptfs_file_to_private(file));
3072ed92554SMichael Halcrow 	return 0;
308237fead6SMichael Halcrow }
309237fead6SMichael Halcrow 
3106a480a78SAl Viro static int ecryptfs_dir_release(struct inode *inode, struct file *file)
3116a480a78SAl Viro {
3126a480a78SAl Viro 	fput(ecryptfs_file_to_lower(file));
3136a480a78SAl Viro 	kmem_cache_free(ecryptfs_file_info_cache,
3146a480a78SAl Viro 			ecryptfs_file_to_private(file));
3156a480a78SAl Viro 	return 0;
3166a480a78SAl Viro }
3176a480a78SAl Viro 
3186a480a78SAl Viro static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
3196a480a78SAl Viro {
3206a480a78SAl Viro 	return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
3216a480a78SAl Viro }
3226a480a78SAl Viro 
323237fead6SMichael Halcrow static int
32402c24a82SJosef Bacik ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
325237fead6SMichael Halcrow {
326bc5abcf7STyler Hicks 	int rc;
327bc5abcf7STyler Hicks 
3286d4b5124SJeff Layton 	rc = file_write_and_wait(file);
329bc5abcf7STyler Hicks 	if (rc)
330bc5abcf7STyler Hicks 		return rc;
331bc5abcf7STyler Hicks 
332821f7494STyler Hicks 	return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
333237fead6SMichael Halcrow }
334237fead6SMichael Halcrow 
335237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag)
336237fead6SMichael Halcrow {
337237fead6SMichael Halcrow 	int rc = 0;
338237fead6SMichael Halcrow 	struct file *lower_file = NULL;
339237fead6SMichael Halcrow 
340237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
34172c2d531SAl Viro 	if (lower_file->f_op->fasync)
342237fead6SMichael Halcrow 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
343237fead6SMichael Halcrow 	return rc;
344237fead6SMichael Halcrow }
345237fead6SMichael Halcrow 
346c43f7b8fSTyler Hicks static long
347c43f7b8fSTyler Hicks ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
348c43f7b8fSTyler Hicks {
3492000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
350c43f7b8fSTyler Hicks 	long rc = -ENOTTY;
351c43f7b8fSTyler Hicks 
3526d65261aSTyler Hicks 	if (!lower_file->f_op->unlocked_ioctl)
353c43f7b8fSTyler Hicks 		return rc;
3546d65261aSTyler Hicks 
3556d65261aSTyler Hicks 	switch (cmd) {
3566d65261aSTyler Hicks 	case FITRIM:
3576d65261aSTyler Hicks 	case FS_IOC_GETFLAGS:
3586d65261aSTyler Hicks 	case FS_IOC_SETFLAGS:
3596d65261aSTyler Hicks 	case FS_IOC_GETVERSION:
3606d65261aSTyler Hicks 	case FS_IOC_SETVERSION:
3616d65261aSTyler Hicks 		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
3626d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
3636d65261aSTyler Hicks 
3646d65261aSTyler Hicks 		return rc;
3656d65261aSTyler Hicks 	default:
3666d65261aSTyler Hicks 		return rc;
3676d65261aSTyler Hicks 	}
368c43f7b8fSTyler Hicks }
369c43f7b8fSTyler Hicks 
370c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
371c43f7b8fSTyler Hicks static long
372c43f7b8fSTyler Hicks ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
373c43f7b8fSTyler Hicks {
3742000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
375c43f7b8fSTyler Hicks 	long rc = -ENOIOCTLCMD;
376c43f7b8fSTyler Hicks 
3776d65261aSTyler Hicks 	if (!lower_file->f_op->compat_ioctl)
378c43f7b8fSTyler Hicks 		return rc;
3796d65261aSTyler Hicks 
3806d65261aSTyler Hicks 	switch (cmd) {
381*314999dcSArnd Bergmann 	case FITRIM:
3826d65261aSTyler Hicks 	case FS_IOC32_GETFLAGS:
3836d65261aSTyler Hicks 	case FS_IOC32_SETFLAGS:
3846d65261aSTyler Hicks 	case FS_IOC32_GETVERSION:
3856d65261aSTyler Hicks 	case FS_IOC32_SETVERSION:
3866d65261aSTyler Hicks 		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
3876d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
3886d65261aSTyler Hicks 
3896d65261aSTyler Hicks 		return rc;
3906d65261aSTyler Hicks 	default:
3916d65261aSTyler Hicks 		return rc;
3926d65261aSTyler Hicks 	}
393c43f7b8fSTyler Hicks }
394c43f7b8fSTyler Hicks #endif
395237fead6SMichael Halcrow 
396237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = {
39751a16a9cSAl Viro 	.iterate_shared = ecryptfs_readdir,
398323ef68fSAndy Whitcroft 	.read = generic_read_dir,
399c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
400c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
401c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
402c43f7b8fSTyler Hicks #endif
4036a480a78SAl Viro 	.open = ecryptfs_dir_open,
4046a480a78SAl Viro 	.release = ecryptfs_dir_release,
405237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
4066a480a78SAl Viro 	.llseek = ecryptfs_dir_llseek,
407237fead6SMichael Halcrow };
408237fead6SMichael Halcrow 
409237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = {
41053a2731fSMichael Halcrow 	.llseek = generic_file_llseek,
41102797829SAl Viro 	.read_iter = ecryptfs_read_update_atime,
4128174202bSAl Viro 	.write_iter = generic_file_write_iter,
413c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
414c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
415c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
416c43f7b8fSTyler Hicks #endif
417f0fe970dSJeff Mahoney 	.mmap = ecryptfs_mmap,
418237fead6SMichael Halcrow 	.open = ecryptfs_open,
419237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
420237fead6SMichael Halcrow 	.release = ecryptfs_release,
421237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
422237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
423e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
424237fead6SMichael Halcrow };
425