xref: /openbmc/linux/fs/ecryptfs/file.c (revision d7cdc5febf9f2664755002c3a2f84bd348389fe9)
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>
28237fead6SMichael Halcrow #include <linux/mount.h>
29237fead6SMichael Halcrow #include <linux/pagemap.h>
30237fead6SMichael Halcrow #include <linux/security.h>
31237fead6SMichael Halcrow #include <linux/compat.h>
320cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
33237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
34237fead6SMichael Halcrow 
35237fead6SMichael Halcrow /**
36237fead6SMichael Halcrow  * ecryptfs_read_update_atime
37237fead6SMichael Halcrow  *
38237fead6SMichael Halcrow  * generic_file_read updates the atime of upper layer inode.  But, it
39237fead6SMichael Halcrow  * doesn't give us a chance to update the atime of the lower layer
40237fead6SMichael Halcrow  * inode.  This function is a wrapper to generic_file_read.  It
41237fead6SMichael Halcrow  * updates the atime of the lower level inode if generic_file_read
42237fead6SMichael Halcrow  * returns without any errors. This is to be used only for file reads.
43237fead6SMichael Halcrow  * The function to be used for directory reads is ecryptfs_read.
44237fead6SMichael Halcrow  */
45237fead6SMichael Halcrow static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
46237fead6SMichael Halcrow 				const struct iovec *iov,
47237fead6SMichael Halcrow 				unsigned long nr_segs, loff_t pos)
48237fead6SMichael Halcrow {
49237fead6SMichael Halcrow 	int rc;
50237fead6SMichael Halcrow 	struct dentry *lower_dentry;
51237fead6SMichael Halcrow 	struct vfsmount *lower_vfsmount;
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) {
62bd243a4bSJosef "Jeff" Sipek 		lower_dentry = ecryptfs_dentry_to_lower(file->f_path.dentry);
63bd243a4bSJosef "Jeff" Sipek 		lower_vfsmount = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry);
64237fead6SMichael Halcrow 		touch_atime(lower_vfsmount, lower_dentry);
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 err;
74237fead6SMichael Halcrow 	int filldir_called;
75237fead6SMichael Halcrow 	int entries_written;
76237fead6SMichael Halcrow };
77237fead6SMichael Halcrow 
78237fead6SMichael Halcrow /* Inspired by generic filldir in fs/readir.c */
79237fead6SMichael Halcrow static int
80237fead6SMichael Halcrow ecryptfs_filldir(void *dirent, const char *name, int namelen, loff_t offset,
81237fead6SMichael Halcrow 		 u64 ino, unsigned int d_type)
82237fead6SMichael Halcrow {
83237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat;
84237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback *buf =
85237fead6SMichael Halcrow 	    (struct ecryptfs_getdents_callback *)dirent;
86237fead6SMichael Halcrow 	int rc;
87237fead6SMichael Halcrow 	int decoded_length;
88237fead6SMichael Halcrow 	char *decoded_name;
89237fead6SMichael Halcrow 
90237fead6SMichael Halcrow 	crypt_stat = ecryptfs_dentry_to_private(buf->dentry)->crypt_stat;
91237fead6SMichael Halcrow 	buf->filldir_called++;
92237fead6SMichael Halcrow 	decoded_length = ecryptfs_decode_filename(crypt_stat, name, namelen,
93237fead6SMichael Halcrow 						  &decoded_name);
94237fead6SMichael Halcrow 	if (decoded_length < 0) {
95237fead6SMichael Halcrow 		rc = decoded_length;
96237fead6SMichael Halcrow 		goto out;
97237fead6SMichael Halcrow 	}
98237fead6SMichael Halcrow 	rc = buf->filldir(buf->dirent, decoded_name, decoded_length, offset,
99237fead6SMichael Halcrow 			  ino, d_type);
100237fead6SMichael Halcrow 	kfree(decoded_name);
101237fead6SMichael Halcrow 	if (rc >= 0)
102237fead6SMichael Halcrow 		buf->entries_written++;
103237fead6SMichael Halcrow out:
104237fead6SMichael Halcrow 	return rc;
105237fead6SMichael Halcrow }
106237fead6SMichael Halcrow 
107237fead6SMichael Halcrow /**
108237fead6SMichael Halcrow  * ecryptfs_readdir
109237fead6SMichael Halcrow  * @file: The ecryptfs file struct
110237fead6SMichael Halcrow  * @dirent: Directory entry
111237fead6SMichael Halcrow  * @filldir: The filldir callback function
112237fead6SMichael Halcrow  */
113237fead6SMichael Halcrow static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir)
114237fead6SMichael Halcrow {
115237fead6SMichael Halcrow 	int rc;
116237fead6SMichael Halcrow 	struct file *lower_file;
117237fead6SMichael Halcrow 	struct inode *inode;
118237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback buf;
119237fead6SMichael Halcrow 
120237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
121237fead6SMichael Halcrow 	lower_file->f_pos = file->f_pos;
122bd243a4bSJosef "Jeff" Sipek 	inode = file->f_path.dentry->d_inode;
123237fead6SMichael Halcrow 	memset(&buf, 0, sizeof(buf));
124237fead6SMichael Halcrow 	buf.dirent = dirent;
125bd243a4bSJosef "Jeff" Sipek 	buf.dentry = file->f_path.dentry;
126237fead6SMichael Halcrow 	buf.filldir = filldir;
127237fead6SMichael Halcrow retry:
128237fead6SMichael Halcrow 	buf.filldir_called = 0;
129237fead6SMichael Halcrow 	buf.entries_written = 0;
130237fead6SMichael Halcrow 	buf.err = 0;
131237fead6SMichael Halcrow 	rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf);
132237fead6SMichael Halcrow 	if (buf.err)
133237fead6SMichael Halcrow 		rc = buf.err;
134237fead6SMichael Halcrow 	if (buf.filldir_called && !buf.entries_written)
135237fead6SMichael Halcrow 		goto retry;
136237fead6SMichael Halcrow 	file->f_pos = lower_file->f_pos;
137237fead6SMichael Halcrow 	if (rc >= 0)
138bd243a4bSJosef "Jeff" Sipek 		fsstack_copy_attr_atime(inode, lower_file->f_path.dentry->d_inode);
139237fead6SMichael Halcrow 	return rc;
140237fead6SMichael Halcrow }
141237fead6SMichael Halcrow 
142237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache;
143237fead6SMichael Halcrow 
1447ff1d74fSMichael Halcrow int ecryptfs_open_lower_file(struct file **lower_file,
1457ff1d74fSMichael Halcrow 			     struct dentry *lower_dentry,
1467ff1d74fSMichael Halcrow 			     struct vfsmount *lower_mnt, int flags)
1477ff1d74fSMichael Halcrow {
1487ff1d74fSMichael Halcrow 	int rc = 0;
1497ff1d74fSMichael Halcrow 
150a8d547d5SMichael Halcrow 	flags |= O_LARGEFILE;
1517ff1d74fSMichael Halcrow 	dget(lower_dentry);
1527ff1d74fSMichael Halcrow 	mntget(lower_mnt);
1537ff1d74fSMichael Halcrow 	*lower_file = dentry_open(lower_dentry, lower_mnt, flags);
1547ff1d74fSMichael Halcrow 	if (IS_ERR(*lower_file)) {
1557ff1d74fSMichael Halcrow 		printk(KERN_ERR "Error opening lower file for lower_dentry "
1567ff1d74fSMichael Halcrow 		       "[0x%p], lower_mnt [0x%p], and flags [0x%x]\n",
1577ff1d74fSMichael Halcrow 		       lower_dentry, lower_mnt, flags);
1587ff1d74fSMichael Halcrow 		rc = PTR_ERR(*lower_file);
1597ff1d74fSMichael Halcrow 		*lower_file = NULL;
1607ff1d74fSMichael Halcrow 		goto out;
1617ff1d74fSMichael Halcrow 	}
1627ff1d74fSMichael Halcrow out:
1637ff1d74fSMichael Halcrow 	return rc;
1647ff1d74fSMichael Halcrow }
1657ff1d74fSMichael Halcrow 
1667ff1d74fSMichael Halcrow int ecryptfs_close_lower_file(struct file *lower_file)
1677ff1d74fSMichael Halcrow {
1687ff1d74fSMichael Halcrow 	fput(lower_file);
1697ff1d74fSMichael Halcrow 	return 0;
1707ff1d74fSMichael Halcrow }
1717ff1d74fSMichael Halcrow 
172237fead6SMichael Halcrow /**
173237fead6SMichael Halcrow  * ecryptfs_open
174237fead6SMichael Halcrow  * @inode: inode speciying file to open
175237fead6SMichael Halcrow  * @file: Structure to return filled in
176237fead6SMichael Halcrow  *
177237fead6SMichael Halcrow  * Opens the file specified by inode.
178237fead6SMichael Halcrow  *
179237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
180237fead6SMichael Halcrow  */
181237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file)
182237fead6SMichael Halcrow {
183237fead6SMichael Halcrow 	int rc = 0;
184237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
185237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
186bd243a4bSJosef "Jeff" Sipek 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
187237fead6SMichael Halcrow 	/* Private value of ecryptfs_dentry allocated in
188237fead6SMichael Halcrow 	 * ecryptfs_lookup() */
189237fead6SMichael Halcrow 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
190237fead6SMichael Halcrow 	struct inode *lower_inode = NULL;
191237fead6SMichael Halcrow 	struct file *lower_file = NULL;
192237fead6SMichael Halcrow 	struct vfsmount *lower_mnt;
193237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info;
194237fead6SMichael Halcrow 	int lower_flags;
195237fead6SMichael Halcrow 
196e77a56ddSMichael Halcrow 	mount_crypt_stat = &ecryptfs_superblock_to_private(
197e77a56ddSMichael Halcrow 		ecryptfs_dentry->d_sb)->mount_crypt_stat;
198e77a56ddSMichael Halcrow 	if ((mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
199e77a56ddSMichael Halcrow 	    && ((file->f_flags & O_WRONLY) || (file->f_flags & O_RDWR)
200e77a56ddSMichael Halcrow 		|| (file->f_flags & O_CREAT) || (file->f_flags & O_TRUNC)
201e77a56ddSMichael Halcrow 		|| (file->f_flags & O_APPEND))) {
202e77a56ddSMichael Halcrow 		printk(KERN_WARNING "Mount has encrypted view enabled; "
203e77a56ddSMichael Halcrow 		       "files may only be read\n");
204e77a56ddSMichael Halcrow 		rc = -EPERM;
205e77a56ddSMichael Halcrow 		goto out;
206e77a56ddSMichael Halcrow 	}
207237fead6SMichael Halcrow 	/* Released in ecryptfs_release or end of function if failure */
208c3762229SRobert P. J. Day 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
209237fead6SMichael Halcrow 	ecryptfs_set_file_private(file, file_info);
210237fead6SMichael Halcrow 	if (!file_info) {
211237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
212237fead6SMichael Halcrow 				"Error attempting to allocate memory\n");
213237fead6SMichael Halcrow 		rc = -ENOMEM;
214237fead6SMichael Halcrow 		goto out;
215237fead6SMichael Halcrow 	}
216237fead6SMichael Halcrow 	lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
217237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
218237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
219e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
220237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
221237fead6SMichael Halcrow 		/* Policy code enabled in future release */
222e2bd99ecSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_POLICY_APPLIED;
223e2bd99ecSMichael Halcrow 		crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
224237fead6SMichael Halcrow 	}
225237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
226237fead6SMichael Halcrow 	lower_flags = file->f_flags;
227237fead6SMichael Halcrow 	if ((lower_flags & O_ACCMODE) == O_WRONLY)
228237fead6SMichael Halcrow 		lower_flags = (lower_flags & O_ACCMODE) | O_RDWR;
229237fead6SMichael Halcrow 	if (file->f_flags & O_APPEND)
230237fead6SMichael Halcrow 		lower_flags &= ~O_APPEND;
231237fead6SMichael Halcrow 	lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
232237fead6SMichael Halcrow 	/* Corresponding fput() in ecryptfs_release() */
2335dda6992SMichael Halcrow 	rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
2345dda6992SMichael Halcrow 				      lower_flags);
2355dda6992SMichael Halcrow 	if (rc) {
236237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error opening lower file\n");
237237fead6SMichael Halcrow 		goto out_puts;
238237fead6SMichael Halcrow 	}
239237fead6SMichael Halcrow 	ecryptfs_set_file_lower(file, lower_file);
240237fead6SMichael Halcrow 	/* Isn't this check the same as the one in lookup? */
241237fead6SMichael Halcrow 	lower_inode = lower_dentry->d_inode;
242237fead6SMichael Halcrow 	if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
243237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
244e2bd99ecSMichael Halcrow 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
245237fead6SMichael Halcrow 		rc = 0;
246237fead6SMichael Halcrow 		goto out;
247237fead6SMichael Halcrow 	}
248237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
249e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
250e2bd99ecSMichael Halcrow 	    || !(crypt_stat->flags & ECRYPTFS_KEY_VALID)) {
251*d7cdc5feSMichael Halcrow 		rc = ecryptfs_read_metadata(ecryptfs_dentry);
252237fead6SMichael Halcrow 		if (rc) {
253237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
254237fead6SMichael Halcrow 					"Valid headers not found\n");
255237fead6SMichael Halcrow 			if (!(mount_crypt_stat->flags
256237fead6SMichael Halcrow 			      & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
257237fead6SMichael Halcrow 				rc = -EIO;
258237fead6SMichael Halcrow 				printk(KERN_WARNING "Attempt to read file that "
259237fead6SMichael Halcrow 				       "is not in a valid eCryptfs format, "
260237fead6SMichael Halcrow 				       "and plaintext passthrough mode is not "
261237fead6SMichael Halcrow 				       "enabled; returning -EIO\n");
262237fead6SMichael Halcrow 				mutex_unlock(&crypt_stat->cs_mutex);
263237fead6SMichael Halcrow 				goto out_puts;
264237fead6SMichael Halcrow 			}
265237fead6SMichael Halcrow 			rc = 0;
266e2bd99ecSMichael Halcrow 			crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
267237fead6SMichael Halcrow 			mutex_unlock(&crypt_stat->cs_mutex);
268237fead6SMichael Halcrow 			goto out;
269237fead6SMichael Halcrow 		}
270237fead6SMichael Halcrow 	}
271237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
272237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] "
273237fead6SMichael Halcrow 			"size: [0x%.16x]\n", inode, inode->i_ino,
274237fead6SMichael Halcrow 			i_size_read(inode));
275237fead6SMichael Halcrow 	ecryptfs_set_file_lower(file, lower_file);
276237fead6SMichael Halcrow 	goto out;
277237fead6SMichael Halcrow out_puts:
278237fead6SMichael Halcrow 	mntput(lower_mnt);
279237fead6SMichael Halcrow 	dput(lower_dentry);
280237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
281237fead6SMichael Halcrow 			ecryptfs_file_to_private(file));
282237fead6SMichael Halcrow out:
283237fead6SMichael Halcrow 	return rc;
284237fead6SMichael Halcrow }
285237fead6SMichael Halcrow 
286237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td)
287237fead6SMichael Halcrow {
288237fead6SMichael Halcrow 	int rc = 0;
289237fead6SMichael Halcrow 	struct file *lower_file = NULL;
290237fead6SMichael Halcrow 
291237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
292237fead6SMichael Halcrow 	if (lower_file->f_op && lower_file->f_op->flush)
293237fead6SMichael Halcrow 		rc = lower_file->f_op->flush(lower_file, td);
294237fead6SMichael Halcrow 	return rc;
295237fead6SMichael Halcrow }
296237fead6SMichael Halcrow 
297237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file)
298237fead6SMichael Halcrow {
299237fead6SMichael Halcrow 	struct file *lower_file = ecryptfs_file_to_lower(file);
300237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info = ecryptfs_file_to_private(file);
301237fead6SMichael Halcrow 	struct inode *lower_inode = ecryptfs_inode_to_lower(inode);
3027ff1d74fSMichael Halcrow 	int rc;
303237fead6SMichael Halcrow 
3045dda6992SMichael Halcrow 	rc = ecryptfs_close_lower_file(lower_file);
3055dda6992SMichael Halcrow 	if (rc) {
3067ff1d74fSMichael Halcrow 		printk(KERN_ERR "Error closing lower_file\n");
3077ff1d74fSMichael Halcrow 		goto out;
3087ff1d74fSMichael Halcrow 	}
309237fead6SMichael Halcrow 	inode->i_blocks = lower_inode->i_blocks;
310237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache, file_info);
3117ff1d74fSMichael Halcrow out:
3127ff1d74fSMichael Halcrow 	return rc;
313237fead6SMichael Halcrow }
314237fead6SMichael Halcrow 
315237fead6SMichael Halcrow static int
316237fead6SMichael Halcrow ecryptfs_fsync(struct file *file, struct dentry *dentry, int datasync)
317237fead6SMichael Halcrow {
318237fead6SMichael Halcrow 	struct file *lower_file = ecryptfs_file_to_lower(file);
319237fead6SMichael Halcrow 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
320237fead6SMichael Halcrow 	struct inode *lower_inode = lower_dentry->d_inode;
321237fead6SMichael Halcrow 	int rc = -EINVAL;
322237fead6SMichael Halcrow 
323237fead6SMichael Halcrow 	if (lower_inode->i_fop->fsync) {
324237fead6SMichael Halcrow 		mutex_lock(&lower_inode->i_mutex);
325237fead6SMichael Halcrow 		rc = lower_inode->i_fop->fsync(lower_file, lower_dentry,
326237fead6SMichael Halcrow 					       datasync);
327237fead6SMichael Halcrow 		mutex_unlock(&lower_inode->i_mutex);
328237fead6SMichael Halcrow 	}
329237fead6SMichael Halcrow 	return rc;
330237fead6SMichael Halcrow }
331237fead6SMichael Halcrow 
332237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag)
333237fead6SMichael Halcrow {
334237fead6SMichael Halcrow 	int rc = 0;
335237fead6SMichael Halcrow 	struct file *lower_file = NULL;
336237fead6SMichael Halcrow 
337237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
338237fead6SMichael Halcrow 	if (lower_file->f_op && lower_file->f_op->fasync)
339237fead6SMichael Halcrow 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
340237fead6SMichael Halcrow 	return rc;
341237fead6SMichael Halcrow }
342237fead6SMichael Halcrow 
343237fead6SMichael Halcrow static int ecryptfs_ioctl(struct inode *inode, struct file *file,
344237fead6SMichael Halcrow 			  unsigned int cmd, unsigned long arg);
345237fead6SMichael Halcrow 
346237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = {
347237fead6SMichael Halcrow 	.readdir = ecryptfs_readdir,
348237fead6SMichael Halcrow 	.ioctl = ecryptfs_ioctl,
349237fead6SMichael Halcrow 	.mmap = generic_file_mmap,
350237fead6SMichael Halcrow 	.open = ecryptfs_open,
351237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
352237fead6SMichael Halcrow 	.release = ecryptfs_release,
353237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
354237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
355e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
356237fead6SMichael Halcrow };
357237fead6SMichael Halcrow 
358237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = {
35953a2731fSMichael Halcrow 	.llseek = generic_file_llseek,
360237fead6SMichael Halcrow 	.read = do_sync_read,
361237fead6SMichael Halcrow 	.aio_read = ecryptfs_read_update_atime,
362237fead6SMichael Halcrow 	.write = do_sync_write,
363237fead6SMichael Halcrow 	.aio_write = generic_file_aio_write,
364237fead6SMichael Halcrow 	.readdir = ecryptfs_readdir,
365237fead6SMichael Halcrow 	.ioctl = ecryptfs_ioctl,
366237fead6SMichael Halcrow 	.mmap = generic_file_mmap,
367237fead6SMichael Halcrow 	.open = ecryptfs_open,
368237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
369237fead6SMichael Halcrow 	.release = ecryptfs_release,
370237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
371237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
372e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
373237fead6SMichael Halcrow };
374237fead6SMichael Halcrow 
375237fead6SMichael Halcrow static int
376237fead6SMichael Halcrow ecryptfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
377237fead6SMichael Halcrow 	       unsigned long arg)
378237fead6SMichael Halcrow {
379237fead6SMichael Halcrow 	int rc = 0;
380237fead6SMichael Halcrow 	struct file *lower_file = NULL;
381237fead6SMichael Halcrow 
382237fead6SMichael Halcrow 	if (ecryptfs_file_to_private(file))
383237fead6SMichael Halcrow 		lower_file = ecryptfs_file_to_lower(file);
384237fead6SMichael Halcrow 	if (lower_file && lower_file->f_op && lower_file->f_op->ioctl)
385237fead6SMichael Halcrow 		rc = lower_file->f_op->ioctl(ecryptfs_inode_to_lower(inode),
386237fead6SMichael Halcrow 					     lower_file, cmd, arg);
387237fead6SMichael Halcrow 	else
388237fead6SMichael Halcrow 		rc = -ENOTTY;
389237fead6SMichael Halcrow 	return rc;
390237fead6SMichael Halcrow }
391