xref: /openbmc/linux/fs/ecryptfs/file.c (revision 68ac1234fb949b66941d94dce4157742799fc581)
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,
47237fead6SMichael Halcrow 				const struct iovec *iov,
48237fead6SMichael Halcrow 				unsigned long nr_segs, loff_t pos)
49237fead6SMichael Halcrow {
5038a708d7SEdward Shishkin 	ssize_t rc;
51*68ac1234SAl Viro 	struct path lower;
52237fead6SMichael Halcrow 	struct file *file = iocb->ki_filp;
53237fead6SMichael Halcrow 
54237fead6SMichael Halcrow 	rc = generic_file_aio_read(iocb, iov, nr_segs, pos);
55237fead6SMichael Halcrow 	/*
56237fead6SMichael Halcrow 	 * Even though this is a async interface, we need to wait
57237fead6SMichael Halcrow 	 * for IO to finish to update atime
58237fead6SMichael Halcrow 	 */
59237fead6SMichael Halcrow 	if (-EIOCBQUEUED == rc)
60237fead6SMichael Halcrow 		rc = wait_on_sync_kiocb(iocb);
61237fead6SMichael Halcrow 	if (rc >= 0) {
62*68ac1234SAl Viro 		lower.dentry = ecryptfs_dentry_to_lower(file->f_path.dentry);
63*68ac1234SAl Viro 		lower.mnt = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry);
64*68ac1234SAl Viro 		touch_atime(&lower);
65237fead6SMichael Halcrow 	}
66237fead6SMichael Halcrow 	return rc;
67237fead6SMichael Halcrow }
68237fead6SMichael Halcrow 
69237fead6SMichael Halcrow struct ecryptfs_getdents_callback {
70237fead6SMichael Halcrow 	void *dirent;
71237fead6SMichael Halcrow 	struct dentry *dentry;
72237fead6SMichael Halcrow 	filldir_t filldir;
73237fead6SMichael Halcrow 	int filldir_called;
74237fead6SMichael Halcrow 	int entries_written;
75237fead6SMichael Halcrow };
76237fead6SMichael Halcrow 
777d6c7045SMichael Halcrow /* Inspired by generic filldir in fs/readdir.c */
78237fead6SMichael Halcrow static int
79addd65adSMichael Halcrow ecryptfs_filldir(void *dirent, const char *lower_name, int lower_namelen,
80addd65adSMichael Halcrow 		 loff_t offset, u64 ino, unsigned int d_type)
81237fead6SMichael Halcrow {
82237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback *buf =
83237fead6SMichael Halcrow 	    (struct ecryptfs_getdents_callback *)dirent;
84a8f12864SMichael Halcrow 	size_t name_size;
85addd65adSMichael Halcrow 	char *name;
86237fead6SMichael Halcrow 	int rc;
87237fead6SMichael Halcrow 
88237fead6SMichael Halcrow 	buf->filldir_called++;
89addd65adSMichael Halcrow 	rc = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
90addd65adSMichael Halcrow 						  buf->dentry, lower_name,
91addd65adSMichael Halcrow 						  lower_namelen);
92addd65adSMichael Halcrow 	if (rc) {
93addd65adSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to decode and decrypt "
94addd65adSMichael Halcrow 		       "filename [%s]; rc = [%d]\n", __func__, lower_name,
95addd65adSMichael Halcrow 		       rc);
96237fead6SMichael Halcrow 		goto out;
97237fead6SMichael Halcrow 	}
98addd65adSMichael Halcrow 	rc = buf->filldir(buf->dirent, name, name_size, offset, ino, d_type);
99addd65adSMichael Halcrow 	kfree(name);
100237fead6SMichael Halcrow 	if (rc >= 0)
101237fead6SMichael Halcrow 		buf->entries_written++;
102237fead6SMichael Halcrow out:
103237fead6SMichael Halcrow 	return rc;
104237fead6SMichael Halcrow }
105237fead6SMichael Halcrow 
106237fead6SMichael Halcrow /**
107237fead6SMichael Halcrow  * ecryptfs_readdir
108addd65adSMichael Halcrow  * @file: The eCryptfs directory file
109addd65adSMichael Halcrow  * @dirent: Directory entry handle
110237fead6SMichael Halcrow  * @filldir: The filldir callback function
111237fead6SMichael Halcrow  */
112237fead6SMichael Halcrow static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
113237fead6SMichael Halcrow {
114237fead6SMichael Halcrow 	int rc;
115237fead6SMichael Halcrow 	struct file *lower_file;
116237fead6SMichael Halcrow 	struct inode *inode;
117237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback buf;
118237fead6SMichael Halcrow 
119237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
120237fead6SMichael Halcrow 	lower_file->f_pos = file->f_pos;
121bd243a4bSJosef "Jeff" Sipek 	inode = file->f_path.dentry->d_inode;
122237fead6SMichael Halcrow 	memset(&buf, 0, sizeof(buf));
123237fead6SMichael Halcrow 	buf.dirent = dirent;
124bd243a4bSJosef "Jeff" Sipek 	buf.dentry = file->f_path.dentry;
125237fead6SMichael Halcrow 	buf.filldir = filldir;
126237fead6SMichael Halcrow 	buf.filldir_called = 0;
127237fead6SMichael Halcrow 	buf.entries_written = 0;
128237fead6SMichael Halcrow 	rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
129237fead6SMichael Halcrow 	file->f_pos = lower_file->f_pos;
1307d6c7045SMichael Halcrow 	if (rc < 0)
1317d6c7045SMichael Halcrow 		goto out;
1327d6c7045SMichael Halcrow 	if (buf.filldir_called && !buf.entries_written)
1337d6c7045SMichael Halcrow 		goto out;
134237fead6SMichael Halcrow 	if (rc >= 0)
1357d6c7045SMichael Halcrow 		fsstack_copy_attr_atime(inode,
1367d6c7045SMichael Halcrow 					lower_file->f_path.dentry->d_inode);
1377d6c7045SMichael Halcrow out:
138237fead6SMichael Halcrow 	return rc;
139237fead6SMichael Halcrow }
140237fead6SMichael Halcrow 
14132001d6fSTyler Hicks static void ecryptfs_vma_close(struct vm_area_struct *vma)
14232001d6fSTyler Hicks {
14332001d6fSTyler Hicks 	filemap_write_and_wait(vma->vm_file->f_mapping);
14432001d6fSTyler Hicks }
14532001d6fSTyler Hicks 
14632001d6fSTyler Hicks static const struct vm_operations_struct ecryptfs_file_vm_ops = {
14732001d6fSTyler Hicks 	.close		= ecryptfs_vma_close,
14832001d6fSTyler Hicks 	.fault		= filemap_fault,
14932001d6fSTyler Hicks };
15032001d6fSTyler Hicks 
15132001d6fSTyler Hicks static int ecryptfs_file_mmap(struct file *file, struct vm_area_struct *vma)
15232001d6fSTyler Hicks {
15332001d6fSTyler Hicks 	int rc;
15432001d6fSTyler Hicks 
15532001d6fSTyler Hicks 	rc = generic_file_mmap(file, vma);
15632001d6fSTyler Hicks 	if (!rc)
15732001d6fSTyler Hicks 		vma->vm_ops = &ecryptfs_file_vm_ops;
15832001d6fSTyler Hicks 
15932001d6fSTyler Hicks 	return rc;
16032001d6fSTyler Hicks }
16132001d6fSTyler Hicks 
162237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache;
163237fead6SMichael Halcrow 
164237fead6SMichael Halcrow /**
165237fead6SMichael Halcrow  * ecryptfs_open
166237fead6SMichael Halcrow  * @inode: inode speciying file to open
167237fead6SMichael Halcrow  * @file: Structure to return filled in
168237fead6SMichael Halcrow  *
169237fead6SMichael Halcrow  * Opens the file specified by inode.
170237fead6SMichael Halcrow  *
171237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
172237fead6SMichael Halcrow  */
173237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file)
174237fead6SMichael Halcrow {
175237fead6SMichael Halcrow 	int rc = 0;
176237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
177237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
178bd243a4bSJosef "Jeff" Sipek 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
179237fead6SMichael Halcrow 	/* Private value of ecryptfs_dentry allocated in
180237fead6SMichael Halcrow 	 * ecryptfs_lookup() */
1814aa25bcbSJulia Lawall 	struct dentry *lower_dentry;
182237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info;
183237fead6SMichael Halcrow 
184e77a56ddSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
185e77a56ddSMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
186e77a56ddSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
187e77a56ddSMichael Halcrow 	    && ((file->f_flags & O_WRONLY) || (file->f_flags & O_RDWR)
188e77a56ddSMichael Halcrow 		|| (file->f_flags & O_CREAT) || (file->f_flags & O_TRUNC)
189e77a56ddSMichael Halcrow 		|| (file->f_flags & O_APPEND))) {
190e77a56ddSMichael Halcrow 		printk(KERN_WARNING "Mount has encrypted view enabled; "
191e77a56ddSMichael Halcrow 		       "files may only be read\n");
192e77a56ddSMichael Halcrow 		rc = -EPERM;
193e77a56ddSMichael Halcrow 		goto out;
194e77a56ddSMichael Halcrow 	}
195237fead6SMichael Halcrow 	/* Released in ecryptfs_release or end of function if failure */
196c3762229SRobert P. J. Day 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
197237fead6SMichael Halcrow 	ecryptfs_set_file_private(file, file_info);
198237fead6SMichael Halcrow 	if (!file_info) {
199237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
200237fead6SMichael Halcrow 				"Error attempting to allocate memory\n");
201237fead6SMichael Halcrow 		rc = -ENOMEM;
202237fead6SMichael Halcrow 		goto out;
203237fead6SMichael Halcrow 	}
204237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
205237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
206237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
207e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
208237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
209237fead6SMichael Halcrow 		/* Policy code enabled in future release */
2102ed92554SMichael Halcrow 		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
2112ed92554SMichael Halcrow 				      | ECRYPTFS_ENCRYPTED);
212237fead6SMichael Halcrow 	}
213237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
2143b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
21572b55fffSMichael Halcrow 	if (rc) {
21672b55fffSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
217332ab16fSTyler Hicks 			"the lower file for the dentry with name "
21872b55fffSMichael Halcrow 			"[%s]; rc = [%d]\n", __func__,
21972b55fffSMichael Halcrow 			ecryptfs_dentry->d_name.name, rc);
220ceeab929SJulia Lawall 		goto out_free;
22172b55fffSMichael Halcrow 	}
2220abe1169SRoberto Sassu 	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
2230abe1169SRoberto Sassu 	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
224e27759d7SErez Zadok 		rc = -EPERM;
225332ab16fSTyler Hicks 		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
226e27759d7SErez Zadok 		       "file must hence be opened RO\n", __func__);
227332ab16fSTyler Hicks 		goto out_put;
228e27759d7SErez Zadok 	}
2292ed92554SMichael Halcrow 	ecryptfs_set_file_lower(
2302ed92554SMichael Halcrow 		file, ecryptfs_inode_to_private(inode)->lower_file);
231237fead6SMichael Halcrow 	if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
232237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
2332f9b12a3SMichael Halcrow 		mutex_lock(&crypt_stat->cs_mutex);
234e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
2352f9b12a3SMichael Halcrow 		mutex_unlock(&crypt_stat->cs_mutex);
236237fead6SMichael Halcrow 		rc = 0;
237237fead6SMichael Halcrow 		goto out;
238237fead6SMichael Halcrow 	}
239237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
240e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
241e2bd99ecSMichael Halcrow 	    || !(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
242d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_metadata(ecryptfs_dentry);
243237fead6SMichael Halcrow 		if (rc) {
244237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
245237fead6SMichael Halcrow 					"Valid headers not found\n");
246237fead6SMichael Halcrow 			if (!(mount_crypt_stat->flags
247237fead6SMichael Halcrow 			      & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
248237fead6SMichael Halcrow 				rc = -EIO;
24925bd8174SMichael Halcrow 				printk(KERN_WARNING "Either the lower file "
250237fead6SMichael Halcrow 				       "is not in a valid eCryptfs format, "
25125bd8174SMichael Halcrow 				       "or the key could not be retrieved. "
25225bd8174SMichael Halcrow 				       "Plaintext passthrough mode is not "
253237fead6SMichael Halcrow 				       "enabled; returning -EIO\n");
254237fead6SMichael Halcrow 				mutex_unlock(&crypt_stat->cs_mutex);
255332ab16fSTyler Hicks 				goto out_put;
256237fead6SMichael Halcrow 			}
257237fead6SMichael Halcrow 			rc = 0;
2583aeb86eaSTyler Hicks 			crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
2593aeb86eaSTyler Hicks 					       | ECRYPTFS_ENCRYPTED);
260237fead6SMichael Halcrow 			mutex_unlock(&crypt_stat->cs_mutex);
261237fead6SMichael Halcrow 			goto out;
262237fead6SMichael Halcrow 		}
263237fead6SMichael Halcrow 	}
264237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
265888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
266888d57bbSJoe Perches 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
267888d57bbSJoe Perches 			(unsigned long long)i_size_read(inode));
268237fead6SMichael Halcrow 	goto out;
269332ab16fSTyler Hicks out_put:
270332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2712ed92554SMichael Halcrow out_free:
272237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
273237fead6SMichael Halcrow 			ecryptfs_file_to_private(file));
274237fead6SMichael Halcrow out:
275237fead6SMichael Halcrow 	return rc;
276237fead6SMichael Halcrow }
277237fead6SMichael Halcrow 
278237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td)
279237fead6SMichael Halcrow {
280332ab16fSTyler Hicks 	return file->f_mode & FMODE_WRITE
281332ab16fSTyler Hicks 	       ? filemap_write_and_wait(file->f_mapping) : 0;
282237fead6SMichael Halcrow }
283237fead6SMichael Halcrow 
284237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file)
285237fead6SMichael Halcrow {
286332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2872ed92554SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
2882ed92554SMichael Halcrow 			ecryptfs_file_to_private(file));
2892ed92554SMichael Halcrow 	return 0;
290237fead6SMichael Halcrow }
291237fead6SMichael Halcrow 
292237fead6SMichael Halcrow static int
29302c24a82SJosef Bacik ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
294237fead6SMichael Halcrow {
29557db4e8dSThieu Le 	int rc = 0;
29657db4e8dSThieu Le 
29702c24a82SJosef Bacik 	rc = generic_file_fsync(file, start, end, datasync);
29857db4e8dSThieu Le 	if (rc)
29957db4e8dSThieu Le 		goto out;
30002c24a82SJosef Bacik 	rc = vfs_fsync_range(ecryptfs_file_to_lower(file), start, end,
30102c24a82SJosef Bacik 			     datasync);
30257db4e8dSThieu Le out:
30357db4e8dSThieu Le 	return rc;
304237fead6SMichael Halcrow }
305237fead6SMichael Halcrow 
306237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag)
307237fead6SMichael Halcrow {
308237fead6SMichael Halcrow 	int rc = 0;
309237fead6SMichael Halcrow 	struct file *lower_file = NULL;
310237fead6SMichael Halcrow 
311237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
312237fead6SMichael Halcrow 	if (lower_file->f_op && lower_file->f_op->fasync)
313237fead6SMichael Halcrow 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
314237fead6SMichael Halcrow 	return rc;
315237fead6SMichael Halcrow }
316237fead6SMichael Halcrow 
317c43f7b8fSTyler Hicks static long
318c43f7b8fSTyler Hicks ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
319c43f7b8fSTyler Hicks {
320c43f7b8fSTyler Hicks 	struct file *lower_file = NULL;
321c43f7b8fSTyler Hicks 	long rc = -ENOTTY;
322c43f7b8fSTyler Hicks 
323c43f7b8fSTyler Hicks 	if (ecryptfs_file_to_private(file))
324c43f7b8fSTyler Hicks 		lower_file = ecryptfs_file_to_lower(file);
325c43f7b8fSTyler Hicks 	if (lower_file && lower_file->f_op && lower_file->f_op->unlocked_ioctl)
326c43f7b8fSTyler Hicks 		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
327c43f7b8fSTyler Hicks 	return rc;
328c43f7b8fSTyler Hicks }
329c43f7b8fSTyler Hicks 
330c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
331c43f7b8fSTyler Hicks static long
332c43f7b8fSTyler Hicks ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
333c43f7b8fSTyler Hicks {
334c43f7b8fSTyler Hicks 	struct file *lower_file = NULL;
335c43f7b8fSTyler Hicks 	long rc = -ENOIOCTLCMD;
336c43f7b8fSTyler Hicks 
337c43f7b8fSTyler Hicks 	if (ecryptfs_file_to_private(file))
338c43f7b8fSTyler Hicks 		lower_file = ecryptfs_file_to_lower(file);
339c43f7b8fSTyler Hicks 	if (lower_file && lower_file->f_op && lower_file->f_op->compat_ioctl)
340c43f7b8fSTyler Hicks 		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
341c43f7b8fSTyler Hicks 	return rc;
342c43f7b8fSTyler Hicks }
343c43f7b8fSTyler Hicks #endif
344237fead6SMichael Halcrow 
345237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = {
346237fead6SMichael Halcrow 	.readdir = ecryptfs_readdir,
347323ef68fSAndy Whitcroft 	.read = generic_read_dir,
348c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
349c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
350c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
351c43f7b8fSTyler Hicks #endif
352237fead6SMichael Halcrow 	.open = ecryptfs_open,
353237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
354237fead6SMichael Halcrow 	.release = ecryptfs_release,
355237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
356237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
357e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
3586038f373SArnd Bergmann 	.llseek = default_llseek,
359237fead6SMichael Halcrow };
360237fead6SMichael Halcrow 
361237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = {
36253a2731fSMichael Halcrow 	.llseek = generic_file_llseek,
363237fead6SMichael Halcrow 	.read = do_sync_read,
364237fead6SMichael Halcrow 	.aio_read = ecryptfs_read_update_atime,
365237fead6SMichael Halcrow 	.write = do_sync_write,
366237fead6SMichael Halcrow 	.aio_write = generic_file_aio_write,
367237fead6SMichael Halcrow 	.readdir = ecryptfs_readdir,
368c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
369c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
370c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
371c43f7b8fSTyler Hicks #endif
37232001d6fSTyler Hicks 	.mmap = ecryptfs_file_mmap,
373237fead6SMichael Halcrow 	.open = ecryptfs_open,
374237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
375237fead6SMichael Halcrow 	.release = ecryptfs_release,
376237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
377237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
378e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
379237fead6SMichael Halcrow };
380