xref: /linux/fs/ecryptfs/file.c (revision 5c0ba4e0762e6dabd14a5c276652e2defec38de7)
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>
34a27bb332SKent Overstreet #include <linux/aio.h>
35237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
36237fead6SMichael Halcrow 
37237fead6SMichael Halcrow /**
38237fead6SMichael Halcrow  * ecryptfs_read_update_atime
39237fead6SMichael Halcrow  *
40237fead6SMichael Halcrow  * generic_file_read updates the atime of upper layer inode.  But, it
41237fead6SMichael Halcrow  * doesn't give us a chance to update the atime of the lower layer
42237fead6SMichael Halcrow  * inode.  This function is a wrapper to generic_file_read.  It
43237fead6SMichael Halcrow  * updates the atime of the lower level inode if generic_file_read
44237fead6SMichael Halcrow  * returns without any errors. This is to be used only for file reads.
45237fead6SMichael Halcrow  * The function to be used for directory reads is ecryptfs_read.
46237fead6SMichael Halcrow  */
47237fead6SMichael Halcrow static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
48237fead6SMichael Halcrow 				const struct iovec *iov,
49237fead6SMichael Halcrow 				unsigned long nr_segs, loff_t pos)
50237fead6SMichael Halcrow {
5138a708d7SEdward Shishkin 	ssize_t rc;
5268ac1234SAl Viro 	struct path lower;
53237fead6SMichael Halcrow 	struct file *file = iocb->ki_filp;
54237fead6SMichael Halcrow 
55237fead6SMichael Halcrow 	rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
56237fead6SMichael Halcrow 	/*
57237fead6SMichael Halcrow 	 * Even though this is a async interface, we need to wait
58237fead6SMichael Halcrow 	 * for IO to finish to update atime
59237fead6SMichael Halcrow 	 */
60237fead6SMichael Halcrow 	if (-EIOCBQUEUED == rc)
61237fead6SMichael Halcrow 		rc = wait_on_sync_kiocb(iocb);
62237fead6SMichael Halcrow 	if (rc >= 0) {
6368ac1234SAl Viro 		lower.dentry = ecryptfs_dentry_to_lower(file->f_path.dentry);
6468ac1234SAl Viro 		lower.mnt = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry);
6568ac1234SAl Viro 		touch_atime(&lower);
66237fead6SMichael Halcrow 	}
67237fead6SMichael Halcrow 	return rc;
68237fead6SMichael Halcrow }
69237fead6SMichael Halcrow 
70237fead6SMichael Halcrow struct ecryptfs_getdents_callback {
71*5c0ba4e0SAl Viro 	struct dir_context ctx;
72237fead6SMichael Halcrow 	void *dirent;
73237fead6SMichael Halcrow 	struct dentry *dentry;
74237fead6SMichael Halcrow 	filldir_t filldir;
75237fead6SMichael Halcrow 	int filldir_called;
76237fead6SMichael Halcrow 	int entries_written;
77237fead6SMichael Halcrow };
78237fead6SMichael Halcrow 
797d6c7045SMichael Halcrow /* Inspired by generic filldir in fs/readdir.c */
80237fead6SMichael Halcrow static int
81addd65adSMichael Halcrow ecryptfs_filldir(void *dirent, const char *lower_name, int lower_namelen,
82addd65adSMichael Halcrow 		 loff_t offset, u64 ino, unsigned int d_type)
83237fead6SMichael Halcrow {
84237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback *buf =
85237fead6SMichael Halcrow 	    (struct ecryptfs_getdents_callback *)dirent;
86a8f12864SMichael Halcrow 	size_t name_size;
87addd65adSMichael Halcrow 	char *name;
88237fead6SMichael Halcrow 	int rc;
89237fead6SMichael Halcrow 
90237fead6SMichael Halcrow 	buf->filldir_called++;
91addd65adSMichael Halcrow 	rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
92addd65adSMichael Halcrow 						  buf->dentry, lower_name,
93addd65adSMichael Halcrow 						  lower_namelen);
94addd65adSMichael Halcrow 	if (rc) {
95addd65adSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to decode and decrypt "
96addd65adSMichael Halcrow 		       "filename [%s]; rc = [%d]\n", __func__, lower_name,
97addd65adSMichael Halcrow 		       rc);
98237fead6SMichael Halcrow 		goto out;
99237fead6SMichael Halcrow 	}
100addd65adSMichael Halcrow 	rc = buf->filldir(buf->dirent, name, name_size, offset, ino, d_type);
101addd65adSMichael Halcrow 	kfree(name);
102237fead6SMichael Halcrow 	if (rc >= 0)
103237fead6SMichael Halcrow 		buf->entries_written++;
104237fead6SMichael Halcrow out:
105237fead6SMichael Halcrow 	return rc;
106237fead6SMichael Halcrow }
107237fead6SMichael Halcrow 
108237fead6SMichael Halcrow /**
109237fead6SMichael Halcrow  * ecryptfs_readdir
110addd65adSMichael Halcrow  * @file: The eCryptfs directory file
111addd65adSMichael Halcrow  * @dirent: Directory entry handle
112237fead6SMichael Halcrow  * @filldir: The filldir callback function
113237fead6SMichael Halcrow  */
114237fead6SMichael Halcrow static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
115237fead6SMichael Halcrow {
116237fead6SMichael Halcrow 	int rc;
117237fead6SMichael Halcrow 	struct file *lower_file;
118237fead6SMichael Halcrow 	struct inode *inode;
119237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback buf;
120237fead6SMichael Halcrow 
121237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
122237fead6SMichael Halcrow 	lower_file->f_pos = file->f_pos;
123496ad9aaSAl Viro 	inode = file_inode(file);
124237fead6SMichael Halcrow 	memset(&buf, 0, sizeof(buf));
125237fead6SMichael Halcrow 	buf.dirent = dirent;
126bd243a4bSJosef "Jeff" Sipek 	buf.dentry = file->f_path.dentry;
127237fead6SMichael Halcrow 	buf.filldir = filldir;
128237fead6SMichael Halcrow 	buf.filldir_called = 0;
129237fead6SMichael Halcrow 	buf.entries_written = 0;
130*5c0ba4e0SAl Viro 	buf.ctx.actor = ecryptfs_filldir;
131*5c0ba4e0SAl Viro 	rc = iterate_dir(lower_file, &buf.ctx);
132237fead6SMichael Halcrow 	file->f_pos = lower_file->f_pos;
1337d6c7045SMichael Halcrow 	if (rc < 0)
1347d6c7045SMichael Halcrow 		goto out;
1357d6c7045SMichael Halcrow 	if (buf.filldir_called && !buf.entries_written)
1367d6c7045SMichael Halcrow 		goto out;
137237fead6SMichael Halcrow 	if (rc >= 0)
1387d6c7045SMichael Halcrow 		fsstack_copy_attr_atime(inode,
139496ad9aaSAl Viro 					file_inode(lower_file));
1407d6c7045SMichael Halcrow out:
141237fead6SMichael Halcrow 	return rc;
142237fead6SMichael Halcrow }
143237fead6SMichael Halcrow 
144237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache;
145237fead6SMichael Halcrow 
146e3ccaa97STyler Hicks static int read_or_initialize_metadata(struct dentry *dentry)
147e3ccaa97STyler Hicks {
148e3ccaa97STyler Hicks 	struct inode *inode = dentry->d_inode;
149e3ccaa97STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
150e3ccaa97STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
151e3ccaa97STyler Hicks 	int rc;
152e3ccaa97STyler Hicks 
153e3ccaa97STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
154e3ccaa97STyler Hicks 	mount_crypt_stat = &ecryptfs_superblock_to_private(
155e3ccaa97STyler Hicks 						inode->i_sb)->mount_crypt_stat;
156e3ccaa97STyler Hicks 	mutex_lock(&crypt_stat->cs_mutex);
157e3ccaa97STyler Hicks 
158e3ccaa97STyler Hicks 	if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
159e3ccaa97STyler Hicks 	    crypt_stat->flags & ECRYPTFS_KEY_VALID) {
160e3ccaa97STyler Hicks 		rc = 0;
161e3ccaa97STyler Hicks 		goto out;
162e3ccaa97STyler Hicks 	}
163e3ccaa97STyler Hicks 
164e3ccaa97STyler Hicks 	rc = ecryptfs_read_metadata(dentry);
165e3ccaa97STyler Hicks 	if (!rc)
166e3ccaa97STyler Hicks 		goto out;
167e3ccaa97STyler Hicks 
168e3ccaa97STyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
169e3ccaa97STyler Hicks 		crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
170e3ccaa97STyler Hicks 				       | ECRYPTFS_ENCRYPTED);
171e3ccaa97STyler Hicks 		rc = 0;
172e3ccaa97STyler Hicks 		goto out;
173e3ccaa97STyler Hicks 	}
174e3ccaa97STyler Hicks 
175e3ccaa97STyler Hicks 	if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
176e3ccaa97STyler Hicks 	    !i_size_read(ecryptfs_inode_to_lower(inode))) {
177e3ccaa97STyler Hicks 		rc = ecryptfs_initialize_file(dentry, inode);
178e3ccaa97STyler Hicks 		if (!rc)
179e3ccaa97STyler Hicks 			goto out;
180e3ccaa97STyler Hicks 	}
181e3ccaa97STyler Hicks 
182e3ccaa97STyler Hicks 	rc = -EIO;
183e3ccaa97STyler Hicks out:
184e3ccaa97STyler Hicks 	mutex_unlock(&crypt_stat->cs_mutex);
185e3ccaa97STyler Hicks 	return rc;
186e3ccaa97STyler Hicks }
187e3ccaa97STyler Hicks 
188237fead6SMichael Halcrow /**
189237fead6SMichael Halcrow  * ecryptfs_open
190237fead6SMichael Halcrow  * @inode: inode speciying file to open
191237fead6SMichael Halcrow  * @file: Structure to return filled in
192237fead6SMichael Halcrow  *
193237fead6SMichael Halcrow  * Opens the file specified by inode.
194237fead6SMichael Halcrow  *
195237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
196237fead6SMichael Halcrow  */
197237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file)
198237fead6SMichael Halcrow {
199237fead6SMichael Halcrow 	int rc = 0;
200237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
201237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
202bd243a4bSJosef "Jeff" Sipek 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
203237fead6SMichael Halcrow 	/* Private value of ecryptfs_dentry allocated in
204237fead6SMichael Halcrow 	 * ecryptfs_lookup() */
205237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info;
206237fead6SMichael Halcrow 
207e77a56ddSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
208e77a56ddSMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
209e77a56ddSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
210e77a56ddSMichael Halcrow 	    && ((file->f_flags & O_WRONLY) || (file->f_flags & O_RDWR)
211e77a56ddSMichael Halcrow 		|| (file->f_flags & O_CREAT) || (file->f_flags & O_TRUNC)
212e77a56ddSMichael Halcrow 		|| (file->f_flags & O_APPEND))) {
213e77a56ddSMichael Halcrow 		printk(KERN_WARNING "Mount has encrypted view enabled; "
214e77a56ddSMichael Halcrow 		       "files may only be read\n");
215e77a56ddSMichael Halcrow 		rc = -EPERM;
216e77a56ddSMichael Halcrow 		goto out;
217e77a56ddSMichael Halcrow 	}
218237fead6SMichael Halcrow 	/* Released in ecryptfs_release or end of function if failure */
219c3762229SRobert P. J. Day 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
220237fead6SMichael Halcrow 	ecryptfs_set_file_private(file, file_info);
221237fead6SMichael Halcrow 	if (!file_info) {
222237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
223237fead6SMichael Halcrow 				"Error attempting to allocate memory\n");
224237fead6SMichael Halcrow 		rc = -ENOMEM;
225237fead6SMichael Halcrow 		goto out;
226237fead6SMichael Halcrow 	}
227237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
228237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
229e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
230237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
231237fead6SMichael Halcrow 		/* Policy code enabled in future release */
2322ed92554SMichael Halcrow 		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
2332ed92554SMichael Halcrow 				      | ECRYPTFS_ENCRYPTED);
234237fead6SMichael Halcrow 	}
235237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
2363b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
23772b55fffSMichael Halcrow 	if (rc) {
23872b55fffSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
239332ab16fSTyler Hicks 			"the lower file for the dentry with name "
24072b55fffSMichael Halcrow 			"[%s]; rc = [%d]\n", __func__,
24172b55fffSMichael Halcrow 			ecryptfs_dentry->d_name.name, rc);
242ceeab929SJulia Lawall 		goto out_free;
24372b55fffSMichael Halcrow 	}
2440abe1169SRoberto Sassu 	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
2450abe1169SRoberto Sassu 	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
246e27759d7SErez Zadok 		rc = -EPERM;
247332ab16fSTyler Hicks 		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
248e27759d7SErez Zadok 		       "file must hence be opened RO\n", __func__);
249332ab16fSTyler Hicks 		goto out_put;
250e27759d7SErez Zadok 	}
2512ed92554SMichael Halcrow 	ecryptfs_set_file_lower(
2522ed92554SMichael Halcrow 		file, ecryptfs_inode_to_private(inode)->lower_file);
253237fead6SMichael Halcrow 	if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
254237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
2552f9b12a3SMichael Halcrow 		mutex_lock(&crypt_stat->cs_mutex);
256e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
2572f9b12a3SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_mutex);
258237fead6SMichael Halcrow 		rc = 0;
259237fead6SMichael Halcrow 		goto out;
260237fead6SMichael Halcrow 	}
261e3ccaa97STyler Hicks 	rc = read_or_initialize_metadata(ecryptfs_dentry);
262e3ccaa97STyler Hicks 	if (rc)
263332ab16fSTyler Hicks 		goto out_put;
264888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
265888d57bbSJoe Perches 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
266888d57bbSJoe Perches 			(unsigned long long)i_size_read(inode));
267237fead6SMichael Halcrow 	goto out;
268332ab16fSTyler Hicks out_put:
269332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2702ed92554SMichael Halcrow out_free:
271237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
272237fead6SMichael Halcrow 			ecryptfs_file_to_private(file));
273237fead6SMichael Halcrow out:
274237fead6SMichael Halcrow 	return rc;
275237fead6SMichael Halcrow }
276237fead6SMichael Halcrow 
277237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td)
278237fead6SMichael Halcrow {
27964e6651dSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
28064e6651dSTyler Hicks 
28164e6651dSTyler Hicks 	if (lower_file->f_op && lower_file->f_op->flush) {
28264e6651dSTyler Hicks 		filemap_write_and_wait(file->f_mapping);
28364e6651dSTyler Hicks 		return lower_file->f_op->flush(lower_file, td);
28464e6651dSTyler Hicks 	}
28564e6651dSTyler Hicks 
28664e6651dSTyler Hicks 	return 0;
287237fead6SMichael Halcrow }
288237fead6SMichael Halcrow 
289237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file)
290237fead6SMichael Halcrow {
291332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2922ed92554SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
2932ed92554SMichael Halcrow 			ecryptfs_file_to_private(file));
2942ed92554SMichael Halcrow 	return 0;
295237fead6SMichael Halcrow }
296237fead6SMichael Halcrow 
297237fead6SMichael Halcrow static int
29802c24a82SJosef Bacik ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
299237fead6SMichael Halcrow {
300bc5abcf7STyler Hicks 	int rc;
301bc5abcf7STyler Hicks 
302bc5abcf7STyler Hicks 	rc = filemap_write_and_wait(file->f_mapping);
303bc5abcf7STyler Hicks 	if (rc)
304bc5abcf7STyler Hicks 		return rc;
305bc5abcf7STyler Hicks 
306821f7494STyler Hicks 	return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
307237fead6SMichael Halcrow }
308237fead6SMichael Halcrow 
309237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag)
310237fead6SMichael Halcrow {
311237fead6SMichael Halcrow 	int rc = 0;
312237fead6SMichael Halcrow 	struct file *lower_file = NULL;
313237fead6SMichael Halcrow 
314237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
315237fead6SMichael Halcrow 	if (lower_file->f_op && lower_file->f_op->fasync)
316237fead6SMichael Halcrow 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
317237fead6SMichael Halcrow 	return rc;
318237fead6SMichael Halcrow }
319237fead6SMichael Halcrow 
320c43f7b8fSTyler Hicks static long
321c43f7b8fSTyler Hicks ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
322c43f7b8fSTyler Hicks {
323c43f7b8fSTyler Hicks 	struct file *lower_file = NULL;
324c43f7b8fSTyler Hicks 	long rc = -ENOTTY;
325c43f7b8fSTyler Hicks 
326c43f7b8fSTyler Hicks 	if (ecryptfs_file_to_private(file))
327c43f7b8fSTyler Hicks 		lower_file = ecryptfs_file_to_lower(file);
328c43f7b8fSTyler Hicks 	if (lower_file && lower_file->f_op && lower_file->f_op->unlocked_ioctl)
329c43f7b8fSTyler Hicks 		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
330c43f7b8fSTyler Hicks 	return rc;
331c43f7b8fSTyler Hicks }
332c43f7b8fSTyler Hicks 
333c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
334c43f7b8fSTyler Hicks static long
335c43f7b8fSTyler Hicks ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
336c43f7b8fSTyler Hicks {
337c43f7b8fSTyler Hicks 	struct file *lower_file = NULL;
338c43f7b8fSTyler Hicks 	long rc = -ENOIOCTLCMD;
339c43f7b8fSTyler Hicks 
340c43f7b8fSTyler Hicks 	if (ecryptfs_file_to_private(file))
341c43f7b8fSTyler Hicks 		lower_file = ecryptfs_file_to_lower(file);
342c43f7b8fSTyler Hicks 	if (lower_file && lower_file->f_op && lower_file->f_op->compat_ioctl)
343c43f7b8fSTyler Hicks 		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
344c43f7b8fSTyler Hicks 	return rc;
345c43f7b8fSTyler Hicks }
346c43f7b8fSTyler Hicks #endif
347237fead6SMichael Halcrow 
348237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = {
349237fead6SMichael Halcrow 	.readdir = ecryptfs_readdir,
350323ef68fSAndy Whitcroft 	.read = generic_read_dir,
351c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
352c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
353c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
354c43f7b8fSTyler Hicks #endif
355237fead6SMichael Halcrow 	.open = ecryptfs_open,
356237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
357237fead6SMichael Halcrow 	.release = ecryptfs_release,
358237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
359237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
360e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
3616038f373SArnd Bergmann 	.llseek = default_llseek,
362237fead6SMichael Halcrow };
363237fead6SMichael Halcrow 
364237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = {
36553a2731fSMichael Halcrow 	.llseek = generic_file_llseek,
366237fead6SMichael Halcrow 	.read = do_sync_read,
367237fead6SMichael Halcrow 	.aio_read = ecryptfs_read_update_atime,
368237fead6SMichael Halcrow 	.write = do_sync_write,
369237fead6SMichael Halcrow 	.aio_write = generic_file_aio_write,
370237fead6SMichael Halcrow 	.readdir = ecryptfs_readdir,
371c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
372c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
373c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
374c43f7b8fSTyler Hicks #endif
375821f7494STyler Hicks 	.mmap = generic_file_mmap,
376237fead6SMichael Halcrow 	.open = ecryptfs_open,
377237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
378237fead6SMichael Halcrow 	.release = ecryptfs_release,
379237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
380237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
381e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
382237fead6SMichael Halcrow };
383