xref: /openbmc/linux/fs/ecryptfs/file.c (revision e86281e700cca8a773f9a572fa406adf2784ba5c)
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 <mhalcrow@us.ibm.com>
8237fead6SMichael Halcrow  *   		Michael C. Thompson <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/poll.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
29237fead6SMichael Halcrow #include <linux/mount.h>
30237fead6SMichael Halcrow #include <linux/pagemap.h>
31237fead6SMichael Halcrow #include <linux/security.h>
32237fead6SMichael Halcrow #include <linux/compat.h>
330cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
34237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
35237fead6SMichael Halcrow 
36237fead6SMichael Halcrow /**
37237fead6SMichael Halcrow  * ecryptfs_read_update_atime
38237fead6SMichael Halcrow  *
39237fead6SMichael Halcrow  * generic_file_read updates the atime of upper layer inode.  But, it
40237fead6SMichael Halcrow  * doesn't give us a chance to update the atime of the lower layer
41237fead6SMichael Halcrow  * inode.  This function is a wrapper to generic_file_read.  It
42237fead6SMichael Halcrow  * updates the atime of the lower level inode if generic_file_read
43237fead6SMichael Halcrow  * returns without any errors. This is to be used only for file reads.
44237fead6SMichael Halcrow  * The function to be used for directory reads is ecryptfs_read.
45237fead6SMichael Halcrow  */
46237fead6SMichael Halcrow static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
4702797829SAl Viro 				struct iov_iter *to)
48237fead6SMichael Halcrow {
4938a708d7SEdward Shishkin 	ssize_t rc;
50cc18ec3cSMatthew Wilcox 	struct path *path;
51237fead6SMichael Halcrow 	struct file *file = iocb->ki_filp;
52237fead6SMichael Halcrow 
5302797829SAl Viro 	rc = generic_file_read_iter(iocb, to);
54237fead6SMichael Halcrow 	if (rc >= 0) {
55cc18ec3cSMatthew Wilcox 		path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
56cc18ec3cSMatthew Wilcox 		touch_atime(path);
57237fead6SMichael Halcrow 	}
58237fead6SMichael Halcrow 	return rc;
59237fead6SMichael Halcrow }
60237fead6SMichael Halcrow 
61237fead6SMichael Halcrow struct ecryptfs_getdents_callback {
625c0ba4e0SAl Viro 	struct dir_context ctx;
632de5f059SAl Viro 	struct dir_context *caller;
640747fdb2SAl Viro 	struct super_block *sb;
65237fead6SMichael Halcrow 	int filldir_called;
66237fead6SMichael Halcrow 	int entries_written;
67237fead6SMichael Halcrow };
68237fead6SMichael Halcrow 
697d6c7045SMichael Halcrow /* Inspired by generic filldir in fs/readdir.c */
70237fead6SMichael Halcrow static int
71ac7576f4SMiklos Szeredi ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
72ac7576f4SMiklos Szeredi 		 int lower_namelen, loff_t offset, u64 ino, unsigned int d_type)
73237fead6SMichael Halcrow {
74237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback *buf =
75ac7576f4SMiklos Szeredi 		container_of(ctx, struct ecryptfs_getdents_callback, ctx);
76a8f12864SMichael Halcrow 	size_t name_size;
77addd65adSMichael Halcrow 	char *name;
78237fead6SMichael Halcrow 	int rc;
79237fead6SMichael Halcrow 
80237fead6SMichael Halcrow 	buf->filldir_called++;
81addd65adSMichael Halcrow 	rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
820747fdb2SAl Viro 						  buf->sb, lower_name,
83addd65adSMichael Halcrow 						  lower_namelen);
84addd65adSMichael Halcrow 	if (rc) {
85*e86281e7STyler Hicks 		if (rc != -EINVAL) {
86*e86281e7STyler Hicks 			ecryptfs_printk(KERN_DEBUG,
87*e86281e7STyler Hicks 					"%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n",
88*e86281e7STyler Hicks 					__func__, lower_name, rc);
89*e86281e7STyler Hicks 			return rc;
90237fead6SMichael Halcrow 		}
91*e86281e7STyler Hicks 
92*e86281e7STyler Hicks 		/* Mask -EINVAL errors as these are most likely due a plaintext
93*e86281e7STyler Hicks 		 * filename present in the lower filesystem despite filename
94*e86281e7STyler Hicks 		 * encryption being enabled. One unavoidable example would be
95*e86281e7STyler Hicks 		 * the "lost+found" dentry in the root directory of an Ext4
96*e86281e7STyler Hicks 		 * filesystem.
97*e86281e7STyler Hicks 		 */
98*e86281e7STyler Hicks 		return 0;
99*e86281e7STyler Hicks 	}
100*e86281e7STyler Hicks 
1012de5f059SAl Viro 	buf->caller->pos = buf->ctx.pos;
1022de5f059SAl Viro 	rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
103addd65adSMichael Halcrow 	kfree(name);
1042de5f059SAl Viro 	if (!rc)
105237fead6SMichael Halcrow 		buf->entries_written++;
106*e86281e7STyler Hicks 
107237fead6SMichael Halcrow 	return rc;
108237fead6SMichael Halcrow }
109237fead6SMichael Halcrow 
110237fead6SMichael Halcrow /**
111237fead6SMichael Halcrow  * ecryptfs_readdir
112addd65adSMichael Halcrow  * @file: The eCryptfs directory file
1132de5f059SAl Viro  * @ctx: The actor to feed the entries to
114237fead6SMichael Halcrow  */
1152de5f059SAl Viro static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
116237fead6SMichael Halcrow {
117237fead6SMichael Halcrow 	int rc;
118237fead6SMichael Halcrow 	struct file *lower_file;
1190747fdb2SAl Viro 	struct inode *inode = file_inode(file);
1202de5f059SAl Viro 	struct ecryptfs_getdents_callback buf = {
1212de5f059SAl Viro 		.ctx.actor = ecryptfs_filldir,
1222de5f059SAl Viro 		.caller = ctx,
1230747fdb2SAl Viro 		.sb = inode->i_sb,
1242de5f059SAl Viro 	};
125237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
1265c0ba4e0SAl Viro 	rc = iterate_dir(lower_file, &buf.ctx);
1272de5f059SAl Viro 	ctx->pos = buf.ctx.pos;
1287d6c7045SMichael Halcrow 	if (rc < 0)
1297d6c7045SMichael Halcrow 		goto out;
1307d6c7045SMichael Halcrow 	if (buf.filldir_called && !buf.entries_written)
1317d6c7045SMichael Halcrow 		goto out;
132237fead6SMichael Halcrow 	if (rc >= 0)
1337d6c7045SMichael Halcrow 		fsstack_copy_attr_atime(inode,
134496ad9aaSAl Viro 					file_inode(lower_file));
1357d6c7045SMichael Halcrow out:
136237fead6SMichael Halcrow 	return rc;
137237fead6SMichael Halcrow }
138237fead6SMichael Halcrow 
139237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache;
140237fead6SMichael Halcrow 
141e3ccaa97STyler Hicks static int read_or_initialize_metadata(struct dentry *dentry)
142e3ccaa97STyler Hicks {
1432b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
144e3ccaa97STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
145e3ccaa97STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
146e3ccaa97STyler Hicks 	int rc;
147e3ccaa97STyler Hicks 
148e3ccaa97STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
149e3ccaa97STyler Hicks 	mount_crypt_stat = &ecryptfs_superblock_to_private(
150e3ccaa97STyler Hicks 						inode->i_sb)->mount_crypt_stat;
151e3ccaa97STyler Hicks 	mutex_lock(&crypt_stat->cs_mutex);
152e3ccaa97STyler Hicks 
153e3ccaa97STyler Hicks 	if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
154e3ccaa97STyler Hicks 	    crypt_stat->flags & ECRYPTFS_KEY_VALID) {
155e3ccaa97STyler Hicks 		rc = 0;
156e3ccaa97STyler Hicks 		goto out;
157e3ccaa97STyler Hicks 	}
158e3ccaa97STyler Hicks 
159e3ccaa97STyler Hicks 	rc = ecryptfs_read_metadata(dentry);
160e3ccaa97STyler Hicks 	if (!rc)
161e3ccaa97STyler Hicks 		goto out;
162e3ccaa97STyler Hicks 
163e3ccaa97STyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
164e3ccaa97STyler Hicks 		crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
165e3ccaa97STyler Hicks 				       | ECRYPTFS_ENCRYPTED);
166e3ccaa97STyler Hicks 		rc = 0;
167e3ccaa97STyler Hicks 		goto out;
168e3ccaa97STyler Hicks 	}
169e3ccaa97STyler Hicks 
170e3ccaa97STyler Hicks 	if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
171e3ccaa97STyler Hicks 	    !i_size_read(ecryptfs_inode_to_lower(inode))) {
172e3ccaa97STyler Hicks 		rc = ecryptfs_initialize_file(dentry, inode);
173e3ccaa97STyler Hicks 		if (!rc)
174e3ccaa97STyler Hicks 			goto out;
175e3ccaa97STyler Hicks 	}
176e3ccaa97STyler Hicks 
177e3ccaa97STyler Hicks 	rc = -EIO;
178e3ccaa97STyler Hicks out:
179e3ccaa97STyler Hicks 	mutex_unlock(&crypt_stat->cs_mutex);
180e3ccaa97STyler Hicks 	return rc;
181e3ccaa97STyler Hicks }
182e3ccaa97STyler Hicks 
183f0fe970dSJeff Mahoney static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
184f0fe970dSJeff Mahoney {
185f0fe970dSJeff Mahoney 	struct file *lower_file = ecryptfs_file_to_lower(file);
186f0fe970dSJeff Mahoney 	/*
187f0fe970dSJeff Mahoney 	 * Don't allow mmap on top of file systems that don't support it
188f0fe970dSJeff Mahoney 	 * natively.  If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
189f0fe970dSJeff Mahoney 	 * allows recursive mounting, this will need to be extended.
190f0fe970dSJeff Mahoney 	 */
191f0fe970dSJeff Mahoney 	if (!lower_file->f_op->mmap)
192f0fe970dSJeff Mahoney 		return -ENODEV;
193f0fe970dSJeff Mahoney 	return generic_file_mmap(file, vma);
194f0fe970dSJeff Mahoney }
195f0fe970dSJeff Mahoney 
196237fead6SMichael Halcrow /**
197237fead6SMichael Halcrow  * ecryptfs_open
19840f0fd37SChris J Arges  * @inode: inode specifying file to open
199237fead6SMichael Halcrow  * @file: Structure to return filled in
200237fead6SMichael Halcrow  *
201237fead6SMichael Halcrow  * Opens the file specified by inode.
202237fead6SMichael Halcrow  *
203237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
204237fead6SMichael Halcrow  */
205237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file)
206237fead6SMichael Halcrow {
207237fead6SMichael Halcrow 	int rc = 0;
208237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
209bd243a4bSJosef "Jeff" Sipek 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
210237fead6SMichael Halcrow 	/* Private value of ecryptfs_dentry allocated in
211237fead6SMichael Halcrow 	 * ecryptfs_lookup() */
212237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info;
213237fead6SMichael Halcrow 
214237fead6SMichael Halcrow 	/* Released in ecryptfs_release or end of function if failure */
215c3762229SRobert P. J. Day 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
216237fead6SMichael Halcrow 	ecryptfs_set_file_private(file, file_info);
217237fead6SMichael Halcrow 	if (!file_info) {
218237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
219237fead6SMichael Halcrow 				"Error attempting to allocate memory\n");
220237fead6SMichael Halcrow 		rc = -ENOMEM;
221237fead6SMichael Halcrow 		goto out;
222237fead6SMichael Halcrow 	}
223237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
224237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
225e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
226237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
227237fead6SMichael Halcrow 		/* Policy code enabled in future release */
2282ed92554SMichael Halcrow 		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
2292ed92554SMichael Halcrow 				      | ECRYPTFS_ENCRYPTED);
230237fead6SMichael Halcrow 	}
231237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
2323b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
23372b55fffSMichael Halcrow 	if (rc) {
23472b55fffSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
235332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2369e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2379e78d14aSDavid Howells 			ecryptfs_dentry, rc);
238ceeab929SJulia Lawall 		goto out_free;
23972b55fffSMichael Halcrow 	}
2400abe1169SRoberto Sassu 	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
2410abe1169SRoberto Sassu 	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
242e27759d7SErez Zadok 		rc = -EPERM;
243332ab16fSTyler Hicks 		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
244e27759d7SErez Zadok 		       "file must hence be opened RO\n", __func__);
245332ab16fSTyler Hicks 		goto out_put;
246e27759d7SErez Zadok 	}
2472ed92554SMichael Halcrow 	ecryptfs_set_file_lower(
2482ed92554SMichael Halcrow 		file, ecryptfs_inode_to_private(inode)->lower_file);
249e3ccaa97STyler Hicks 	rc = read_or_initialize_metadata(ecryptfs_dentry);
250e3ccaa97STyler Hicks 	if (rc)
251332ab16fSTyler Hicks 		goto out_put;
252888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
253888d57bbSJoe Perches 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
254888d57bbSJoe Perches 			(unsigned long long)i_size_read(inode));
255237fead6SMichael Halcrow 	goto out;
256332ab16fSTyler Hicks out_put:
257332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2582ed92554SMichael Halcrow out_free:
259237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
260237fead6SMichael Halcrow 			ecryptfs_file_to_private(file));
261237fead6SMichael Halcrow out:
262237fead6SMichael Halcrow 	return rc;
263237fead6SMichael Halcrow }
264237fead6SMichael Halcrow 
2656a480a78SAl Viro /**
2666a480a78SAl Viro  * ecryptfs_dir_open
26740f0fd37SChris J Arges  * @inode: inode specifying file to open
2686a480a78SAl Viro  * @file: Structure to return filled in
2696a480a78SAl Viro  *
2706a480a78SAl Viro  * Opens the file specified by inode.
2716a480a78SAl Viro  *
2726a480a78SAl Viro  * Returns zero on success; non-zero otherwise
2736a480a78SAl Viro  */
2746a480a78SAl Viro static int ecryptfs_dir_open(struct inode *inode, struct file *file)
2756a480a78SAl Viro {
2766a480a78SAl Viro 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
2776a480a78SAl Viro 	/* Private value of ecryptfs_dentry allocated in
2786a480a78SAl Viro 	 * ecryptfs_lookup() */
2796a480a78SAl Viro 	struct ecryptfs_file_info *file_info;
2806a480a78SAl Viro 	struct file *lower_file;
2816a480a78SAl Viro 
2826a480a78SAl Viro 	/* Released in ecryptfs_release or end of function if failure */
2836a480a78SAl Viro 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
2846a480a78SAl Viro 	ecryptfs_set_file_private(file, file_info);
2856a480a78SAl Viro 	if (unlikely(!file_info)) {
2866a480a78SAl Viro 		ecryptfs_printk(KERN_ERR,
2876a480a78SAl Viro 				"Error attempting to allocate memory\n");
2886a480a78SAl Viro 		return -ENOMEM;
2896a480a78SAl Viro 	}
2906a480a78SAl Viro 	lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
2916a480a78SAl Viro 				 file->f_flags, current_cred());
2926a480a78SAl Viro 	if (IS_ERR(lower_file)) {
2936a480a78SAl Viro 		printk(KERN_ERR "%s: Error attempting to initialize "
2946a480a78SAl Viro 			"the lower file for the dentry with name "
2956a480a78SAl Viro 			"[%pd]; rc = [%ld]\n", __func__,
2966a480a78SAl Viro 			ecryptfs_dentry, PTR_ERR(lower_file));
2976a480a78SAl Viro 		kmem_cache_free(ecryptfs_file_info_cache, file_info);
2986a480a78SAl Viro 		return PTR_ERR(lower_file);
2996a480a78SAl Viro 	}
3006a480a78SAl Viro 	ecryptfs_set_file_lower(file, lower_file);
3016a480a78SAl Viro 	return 0;
3026a480a78SAl Viro }
3036a480a78SAl Viro 
304237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td)
305237fead6SMichael Halcrow {
30664e6651dSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
30764e6651dSTyler Hicks 
30872c2d531SAl Viro 	if (lower_file->f_op->flush) {
30964e6651dSTyler Hicks 		filemap_write_and_wait(file->f_mapping);
31064e6651dSTyler Hicks 		return lower_file->f_op->flush(lower_file, td);
31164e6651dSTyler Hicks 	}
31264e6651dSTyler Hicks 
31364e6651dSTyler Hicks 	return 0;
314237fead6SMichael Halcrow }
315237fead6SMichael Halcrow 
316237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file)
317237fead6SMichael Halcrow {
318332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
3192ed92554SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
3202ed92554SMichael Halcrow 			ecryptfs_file_to_private(file));
3212ed92554SMichael Halcrow 	return 0;
322237fead6SMichael Halcrow }
323237fead6SMichael Halcrow 
3246a480a78SAl Viro static int ecryptfs_dir_release(struct inode *inode, struct file *file)
3256a480a78SAl Viro {
3266a480a78SAl Viro 	fput(ecryptfs_file_to_lower(file));
3276a480a78SAl Viro 	kmem_cache_free(ecryptfs_file_info_cache,
3286a480a78SAl Viro 			ecryptfs_file_to_private(file));
3296a480a78SAl Viro 	return 0;
3306a480a78SAl Viro }
3316a480a78SAl Viro 
3326a480a78SAl Viro static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
3336a480a78SAl Viro {
3346a480a78SAl Viro 	return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
3356a480a78SAl Viro }
3366a480a78SAl Viro 
337237fead6SMichael Halcrow static int
33802c24a82SJosef Bacik ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
339237fead6SMichael Halcrow {
340bc5abcf7STyler Hicks 	int rc;
341bc5abcf7STyler Hicks 
3426d4b5124SJeff Layton 	rc = file_write_and_wait(file);
343bc5abcf7STyler Hicks 	if (rc)
344bc5abcf7STyler Hicks 		return rc;
345bc5abcf7STyler Hicks 
346821f7494STyler Hicks 	return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
347237fead6SMichael Halcrow }
348237fead6SMichael Halcrow 
349237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag)
350237fead6SMichael Halcrow {
351237fead6SMichael Halcrow 	int rc = 0;
352237fead6SMichael Halcrow 	struct file *lower_file = NULL;
353237fead6SMichael Halcrow 
354237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
35572c2d531SAl Viro 	if (lower_file->f_op->fasync)
356237fead6SMichael Halcrow 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
357237fead6SMichael Halcrow 	return rc;
358237fead6SMichael Halcrow }
359237fead6SMichael Halcrow 
360c43f7b8fSTyler Hicks static long
361c43f7b8fSTyler Hicks ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
362c43f7b8fSTyler Hicks {
3632000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
364c43f7b8fSTyler Hicks 	long rc = -ENOTTY;
365c43f7b8fSTyler Hicks 
3666d65261aSTyler Hicks 	if (!lower_file->f_op->unlocked_ioctl)
367c43f7b8fSTyler Hicks 		return rc;
3686d65261aSTyler Hicks 
3696d65261aSTyler Hicks 	switch (cmd) {
3706d65261aSTyler Hicks 	case FITRIM:
3716d65261aSTyler Hicks 	case FS_IOC_GETFLAGS:
3726d65261aSTyler Hicks 	case FS_IOC_SETFLAGS:
3736d65261aSTyler Hicks 	case FS_IOC_GETVERSION:
3746d65261aSTyler Hicks 	case FS_IOC_SETVERSION:
3756d65261aSTyler Hicks 		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
3766d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
3776d65261aSTyler Hicks 
3786d65261aSTyler Hicks 		return rc;
3796d65261aSTyler Hicks 	default:
3806d65261aSTyler Hicks 		return rc;
3816d65261aSTyler Hicks 	}
382c43f7b8fSTyler Hicks }
383c43f7b8fSTyler Hicks 
384c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
385c43f7b8fSTyler Hicks static long
386c43f7b8fSTyler Hicks ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
387c43f7b8fSTyler Hicks {
3882000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
389c43f7b8fSTyler Hicks 	long rc = -ENOIOCTLCMD;
390c43f7b8fSTyler Hicks 
3916d65261aSTyler Hicks 	if (!lower_file->f_op->compat_ioctl)
392c43f7b8fSTyler Hicks 		return rc;
3936d65261aSTyler Hicks 
3946d65261aSTyler Hicks 	switch (cmd) {
3956d65261aSTyler Hicks 	case FS_IOC32_GETFLAGS:
3966d65261aSTyler Hicks 	case FS_IOC32_SETFLAGS:
3976d65261aSTyler Hicks 	case FS_IOC32_GETVERSION:
3986d65261aSTyler Hicks 	case FS_IOC32_SETVERSION:
3996d65261aSTyler Hicks 		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
4006d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
4016d65261aSTyler Hicks 
4026d65261aSTyler Hicks 		return rc;
4036d65261aSTyler Hicks 	default:
4046d65261aSTyler Hicks 		return rc;
4056d65261aSTyler Hicks 	}
406c43f7b8fSTyler Hicks }
407c43f7b8fSTyler Hicks #endif
408237fead6SMichael Halcrow 
409237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = {
41051a16a9cSAl Viro 	.iterate_shared = ecryptfs_readdir,
411323ef68fSAndy Whitcroft 	.read = generic_read_dir,
412c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
413c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
414c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
415c43f7b8fSTyler Hicks #endif
4166a480a78SAl Viro 	.open = ecryptfs_dir_open,
4176a480a78SAl Viro 	.release = ecryptfs_dir_release,
418237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
4196a480a78SAl Viro 	.llseek = ecryptfs_dir_llseek,
420237fead6SMichael Halcrow };
421237fead6SMichael Halcrow 
422237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = {
42353a2731fSMichael Halcrow 	.llseek = generic_file_llseek,
42402797829SAl Viro 	.read_iter = ecryptfs_read_update_atime,
4258174202bSAl Viro 	.write_iter = generic_file_write_iter,
426c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
427c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
428c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
429c43f7b8fSTyler Hicks #endif
430f0fe970dSJeff Mahoney 	.mmap = ecryptfs_mmap,
431237fead6SMichael Halcrow 	.open = ecryptfs_open,
432237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
433237fead6SMichael Halcrow 	.release = ecryptfs_release,
434237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
435237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
436e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
437237fead6SMichael Halcrow };
438