xref: /openbmc/linux/fs/ecryptfs/file.c (revision f0fe970df3838c202ef6c07a4c2b36838ef0a88b)
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) {
85addd65adSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to decode and decrypt "
86addd65adSMichael Halcrow 		       "filename [%s]; rc = [%d]\n", __func__, lower_name,
87addd65adSMichael Halcrow 		       rc);
88237fead6SMichael Halcrow 		goto out;
89237fead6SMichael Halcrow 	}
902de5f059SAl Viro 	buf->caller->pos = buf->ctx.pos;
912de5f059SAl Viro 	rc = !dir_emit(buf->caller, name, name_size, ino, d_type);
92addd65adSMichael Halcrow 	kfree(name);
932de5f059SAl Viro 	if (!rc)
94237fead6SMichael Halcrow 		buf->entries_written++;
95237fead6SMichael Halcrow out:
96237fead6SMichael Halcrow 	return rc;
97237fead6SMichael Halcrow }
98237fead6SMichael Halcrow 
99237fead6SMichael Halcrow /**
100237fead6SMichael Halcrow  * ecryptfs_readdir
101addd65adSMichael Halcrow  * @file: The eCryptfs directory file
1022de5f059SAl Viro  * @ctx: The actor to feed the entries to
103237fead6SMichael Halcrow  */
1042de5f059SAl Viro static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
105237fead6SMichael Halcrow {
106237fead6SMichael Halcrow 	int rc;
107237fead6SMichael Halcrow 	struct file *lower_file;
1080747fdb2SAl Viro 	struct inode *inode = file_inode(file);
1092de5f059SAl Viro 	struct ecryptfs_getdents_callback buf = {
1102de5f059SAl Viro 		.ctx.actor = ecryptfs_filldir,
1112de5f059SAl Viro 		.caller = ctx,
1120747fdb2SAl Viro 		.sb = inode->i_sb,
1132de5f059SAl Viro 	};
114237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
1155c0ba4e0SAl Viro 	rc = iterate_dir(lower_file, &buf.ctx);
1162de5f059SAl Viro 	ctx->pos = buf.ctx.pos;
1177d6c7045SMichael Halcrow 	if (rc < 0)
1187d6c7045SMichael Halcrow 		goto out;
1197d6c7045SMichael Halcrow 	if (buf.filldir_called && !buf.entries_written)
1207d6c7045SMichael Halcrow 		goto out;
121237fead6SMichael Halcrow 	if (rc >= 0)
1227d6c7045SMichael Halcrow 		fsstack_copy_attr_atime(inode,
123496ad9aaSAl Viro 					file_inode(lower_file));
1247d6c7045SMichael Halcrow out:
125237fead6SMichael Halcrow 	return rc;
126237fead6SMichael Halcrow }
127237fead6SMichael Halcrow 
128237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache;
129237fead6SMichael Halcrow 
130e3ccaa97STyler Hicks static int read_or_initialize_metadata(struct dentry *dentry)
131e3ccaa97STyler Hicks {
1322b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
133e3ccaa97STyler Hicks 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
134e3ccaa97STyler Hicks 	struct ecryptfs_crypt_stat *crypt_stat;
135e3ccaa97STyler Hicks 	int rc;
136e3ccaa97STyler Hicks 
137e3ccaa97STyler Hicks 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
138e3ccaa97STyler Hicks 	mount_crypt_stat = &ecryptfs_superblock_to_private(
139e3ccaa97STyler Hicks 						inode->i_sb)->mount_crypt_stat;
140e3ccaa97STyler Hicks 	mutex_lock(&crypt_stat->cs_mutex);
141e3ccaa97STyler Hicks 
142e3ccaa97STyler Hicks 	if (crypt_stat->flags & ECRYPTFS_POLICY_APPLIED &&
143e3ccaa97STyler Hicks 	    crypt_stat->flags & ECRYPTFS_KEY_VALID) {
144e3ccaa97STyler Hicks 		rc = 0;
145e3ccaa97STyler Hicks 		goto out;
146e3ccaa97STyler Hicks 	}
147e3ccaa97STyler Hicks 
148e3ccaa97STyler Hicks 	rc = ecryptfs_read_metadata(dentry);
149e3ccaa97STyler Hicks 	if (!rc)
150e3ccaa97STyler Hicks 		goto out;
151e3ccaa97STyler Hicks 
152e3ccaa97STyler Hicks 	if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) {
153e3ccaa97STyler Hicks 		crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
154e3ccaa97STyler Hicks 				       | ECRYPTFS_ENCRYPTED);
155e3ccaa97STyler Hicks 		rc = 0;
156e3ccaa97STyler Hicks 		goto out;
157e3ccaa97STyler Hicks 	}
158e3ccaa97STyler Hicks 
159e3ccaa97STyler Hicks 	if (!(mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) &&
160e3ccaa97STyler Hicks 	    !i_size_read(ecryptfs_inode_to_lower(inode))) {
161e3ccaa97STyler Hicks 		rc = ecryptfs_initialize_file(dentry, inode);
162e3ccaa97STyler Hicks 		if (!rc)
163e3ccaa97STyler Hicks 			goto out;
164e3ccaa97STyler Hicks 	}
165e3ccaa97STyler Hicks 
166e3ccaa97STyler Hicks 	rc = -EIO;
167e3ccaa97STyler Hicks out:
168e3ccaa97STyler Hicks 	mutex_unlock(&crypt_stat->cs_mutex);
169e3ccaa97STyler Hicks 	return rc;
170e3ccaa97STyler Hicks }
171e3ccaa97STyler Hicks 
172*f0fe970dSJeff Mahoney static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
173*f0fe970dSJeff Mahoney {
174*f0fe970dSJeff Mahoney 	struct file *lower_file = ecryptfs_file_to_lower(file);
175*f0fe970dSJeff Mahoney 	/*
176*f0fe970dSJeff Mahoney 	 * Don't allow mmap on top of file systems that don't support it
177*f0fe970dSJeff Mahoney 	 * natively.  If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
178*f0fe970dSJeff Mahoney 	 * allows recursive mounting, this will need to be extended.
179*f0fe970dSJeff Mahoney 	 */
180*f0fe970dSJeff Mahoney 	if (!lower_file->f_op->mmap)
181*f0fe970dSJeff Mahoney 		return -ENODEV;
182*f0fe970dSJeff Mahoney 	return generic_file_mmap(file, vma);
183*f0fe970dSJeff Mahoney }
184*f0fe970dSJeff Mahoney 
185237fead6SMichael Halcrow /**
186237fead6SMichael Halcrow  * ecryptfs_open
18740f0fd37SChris J Arges  * @inode: inode specifying file to open
188237fead6SMichael Halcrow  * @file: Structure to return filled in
189237fead6SMichael Halcrow  *
190237fead6SMichael Halcrow  * Opens the file specified by inode.
191237fead6SMichael Halcrow  *
192237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
193237fead6SMichael Halcrow  */
194237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file)
195237fead6SMichael Halcrow {
196237fead6SMichael Halcrow 	int rc = 0;
197237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
198bd243a4bSJosef "Jeff" Sipek 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
199237fead6SMichael Halcrow 	/* Private value of ecryptfs_dentry allocated in
200237fead6SMichael Halcrow 	 * ecryptfs_lookup() */
201237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info;
202237fead6SMichael Halcrow 
203237fead6SMichael Halcrow 	/* Released in ecryptfs_release or end of function if failure */
204c3762229SRobert P. J. Day 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
205237fead6SMichael Halcrow 	ecryptfs_set_file_private(file, file_info);
206237fead6SMichael Halcrow 	if (!file_info) {
207237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
208237fead6SMichael Halcrow 				"Error attempting to allocate memory\n");
209237fead6SMichael Halcrow 		rc = -ENOMEM;
210237fead6SMichael Halcrow 		goto out;
211237fead6SMichael Halcrow 	}
212237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
213237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
214e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
215237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
216237fead6SMichael Halcrow 		/* Policy code enabled in future release */
2172ed92554SMichael Halcrow 		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
2182ed92554SMichael Halcrow 				      | ECRYPTFS_ENCRYPTED);
219237fead6SMichael Halcrow 	}
220237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
2213b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
22272b55fffSMichael Halcrow 	if (rc) {
22372b55fffSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
224332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2259e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2269e78d14aSDavid Howells 			ecryptfs_dentry, rc);
227ceeab929SJulia Lawall 		goto out_free;
22872b55fffSMichael Halcrow 	}
2290abe1169SRoberto Sassu 	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
2300abe1169SRoberto Sassu 	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
231e27759d7SErez Zadok 		rc = -EPERM;
232332ab16fSTyler Hicks 		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
233e27759d7SErez Zadok 		       "file must hence be opened RO\n", __func__);
234332ab16fSTyler Hicks 		goto out_put;
235e27759d7SErez Zadok 	}
2362ed92554SMichael Halcrow 	ecryptfs_set_file_lower(
2372ed92554SMichael Halcrow 		file, ecryptfs_inode_to_private(inode)->lower_file);
238e3ccaa97STyler Hicks 	rc = read_or_initialize_metadata(ecryptfs_dentry);
239e3ccaa97STyler Hicks 	if (rc)
240332ab16fSTyler Hicks 		goto out_put;
241888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
242888d57bbSJoe Perches 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
243888d57bbSJoe Perches 			(unsigned long long)i_size_read(inode));
244237fead6SMichael Halcrow 	goto out;
245332ab16fSTyler Hicks out_put:
246332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2472ed92554SMichael Halcrow out_free:
248237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
249237fead6SMichael Halcrow 			ecryptfs_file_to_private(file));
250237fead6SMichael Halcrow out:
251237fead6SMichael Halcrow 	return rc;
252237fead6SMichael Halcrow }
253237fead6SMichael Halcrow 
2546a480a78SAl Viro /**
2556a480a78SAl Viro  * ecryptfs_dir_open
25640f0fd37SChris J Arges  * @inode: inode specifying file to open
2576a480a78SAl Viro  * @file: Structure to return filled in
2586a480a78SAl Viro  *
2596a480a78SAl Viro  * Opens the file specified by inode.
2606a480a78SAl Viro  *
2616a480a78SAl Viro  * Returns zero on success; non-zero otherwise
2626a480a78SAl Viro  */
2636a480a78SAl Viro static int ecryptfs_dir_open(struct inode *inode, struct file *file)
2646a480a78SAl Viro {
2656a480a78SAl Viro 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
2666a480a78SAl Viro 	/* Private value of ecryptfs_dentry allocated in
2676a480a78SAl Viro 	 * ecryptfs_lookup() */
2686a480a78SAl Viro 	struct ecryptfs_file_info *file_info;
2696a480a78SAl Viro 	struct file *lower_file;
2706a480a78SAl Viro 
2716a480a78SAl Viro 	/* Released in ecryptfs_release or end of function if failure */
2726a480a78SAl Viro 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
2736a480a78SAl Viro 	ecryptfs_set_file_private(file, file_info);
2746a480a78SAl Viro 	if (unlikely(!file_info)) {
2756a480a78SAl Viro 		ecryptfs_printk(KERN_ERR,
2766a480a78SAl Viro 				"Error attempting to allocate memory\n");
2776a480a78SAl Viro 		return -ENOMEM;
2786a480a78SAl Viro 	}
2796a480a78SAl Viro 	lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
2806a480a78SAl Viro 				 file->f_flags, current_cred());
2816a480a78SAl Viro 	if (IS_ERR(lower_file)) {
2826a480a78SAl Viro 		printk(KERN_ERR "%s: Error attempting to initialize "
2836a480a78SAl Viro 			"the lower file for the dentry with name "
2846a480a78SAl Viro 			"[%pd]; rc = [%ld]\n", __func__,
2856a480a78SAl Viro 			ecryptfs_dentry, PTR_ERR(lower_file));
2866a480a78SAl Viro 		kmem_cache_free(ecryptfs_file_info_cache, file_info);
2876a480a78SAl Viro 		return PTR_ERR(lower_file);
2886a480a78SAl Viro 	}
2896a480a78SAl Viro 	ecryptfs_set_file_lower(file, lower_file);
2906a480a78SAl Viro 	return 0;
2916a480a78SAl Viro }
2926a480a78SAl Viro 
293237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td)
294237fead6SMichael Halcrow {
29564e6651dSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
29664e6651dSTyler Hicks 
29772c2d531SAl Viro 	if (lower_file->f_op->flush) {
29864e6651dSTyler Hicks 		filemap_write_and_wait(file->f_mapping);
29964e6651dSTyler Hicks 		return lower_file->f_op->flush(lower_file, td);
30064e6651dSTyler Hicks 	}
30164e6651dSTyler Hicks 
30264e6651dSTyler Hicks 	return 0;
303237fead6SMichael Halcrow }
304237fead6SMichael Halcrow 
305237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file)
306237fead6SMichael Halcrow {
307332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
3082ed92554SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
3092ed92554SMichael Halcrow 			ecryptfs_file_to_private(file));
3102ed92554SMichael Halcrow 	return 0;
311237fead6SMichael Halcrow }
312237fead6SMichael Halcrow 
3136a480a78SAl Viro static int ecryptfs_dir_release(struct inode *inode, struct file *file)
3146a480a78SAl Viro {
3156a480a78SAl Viro 	fput(ecryptfs_file_to_lower(file));
3166a480a78SAl Viro 	kmem_cache_free(ecryptfs_file_info_cache,
3176a480a78SAl Viro 			ecryptfs_file_to_private(file));
3186a480a78SAl Viro 	return 0;
3196a480a78SAl Viro }
3206a480a78SAl Viro 
3216a480a78SAl Viro static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
3226a480a78SAl Viro {
3236a480a78SAl Viro 	return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
3246a480a78SAl Viro }
3256a480a78SAl Viro 
326237fead6SMichael Halcrow static int
32702c24a82SJosef Bacik ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
328237fead6SMichael Halcrow {
329bc5abcf7STyler Hicks 	int rc;
330bc5abcf7STyler Hicks 
331bc5abcf7STyler Hicks 	rc = filemap_write_and_wait(file->f_mapping);
332bc5abcf7STyler Hicks 	if (rc)
333bc5abcf7STyler Hicks 		return rc;
334bc5abcf7STyler Hicks 
335821f7494STyler Hicks 	return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
336237fead6SMichael Halcrow }
337237fead6SMichael Halcrow 
338237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag)
339237fead6SMichael Halcrow {
340237fead6SMichael Halcrow 	int rc = 0;
341237fead6SMichael Halcrow 	struct file *lower_file = NULL;
342237fead6SMichael Halcrow 
343237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
34472c2d531SAl Viro 	if (lower_file->f_op->fasync)
345237fead6SMichael Halcrow 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
346237fead6SMichael Halcrow 	return rc;
347237fead6SMichael Halcrow }
348237fead6SMichael Halcrow 
349c43f7b8fSTyler Hicks static long
350c43f7b8fSTyler Hicks ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
351c43f7b8fSTyler Hicks {
3522000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
353c43f7b8fSTyler Hicks 	long rc = -ENOTTY;
354c43f7b8fSTyler Hicks 
3556d65261aSTyler Hicks 	if (!lower_file->f_op->unlocked_ioctl)
356c43f7b8fSTyler Hicks 		return rc;
3576d65261aSTyler Hicks 
3586d65261aSTyler Hicks 	switch (cmd) {
3596d65261aSTyler Hicks 	case FITRIM:
3606d65261aSTyler Hicks 	case FS_IOC_GETFLAGS:
3616d65261aSTyler Hicks 	case FS_IOC_SETFLAGS:
3626d65261aSTyler Hicks 	case FS_IOC_GETVERSION:
3636d65261aSTyler Hicks 	case FS_IOC_SETVERSION:
3646d65261aSTyler Hicks 		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
3656d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
3666d65261aSTyler Hicks 
3676d65261aSTyler Hicks 		return rc;
3686d65261aSTyler Hicks 	default:
3696d65261aSTyler Hicks 		return rc;
3706d65261aSTyler Hicks 	}
371c43f7b8fSTyler Hicks }
372c43f7b8fSTyler Hicks 
373c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
374c43f7b8fSTyler Hicks static long
375c43f7b8fSTyler Hicks ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
376c43f7b8fSTyler Hicks {
3772000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
378c43f7b8fSTyler Hicks 	long rc = -ENOIOCTLCMD;
379c43f7b8fSTyler Hicks 
3806d65261aSTyler Hicks 	if (!lower_file->f_op->compat_ioctl)
381c43f7b8fSTyler Hicks 		return rc;
3826d65261aSTyler Hicks 
3836d65261aSTyler Hicks 	switch (cmd) {
3846d65261aSTyler Hicks 	case FS_IOC32_GETFLAGS:
3856d65261aSTyler Hicks 	case FS_IOC32_SETFLAGS:
3866d65261aSTyler Hicks 	case FS_IOC32_GETVERSION:
3876d65261aSTyler Hicks 	case FS_IOC32_SETVERSION:
3886d65261aSTyler Hicks 		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
3896d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
3906d65261aSTyler Hicks 
3916d65261aSTyler Hicks 		return rc;
3926d65261aSTyler Hicks 	default:
3936d65261aSTyler Hicks 		return rc;
3946d65261aSTyler Hicks 	}
395c43f7b8fSTyler Hicks }
396c43f7b8fSTyler Hicks #endif
397237fead6SMichael Halcrow 
398237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = {
39951a16a9cSAl Viro 	.iterate_shared = ecryptfs_readdir,
400323ef68fSAndy Whitcroft 	.read = generic_read_dir,
401c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
402c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
403c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
404c43f7b8fSTyler Hicks #endif
4056a480a78SAl Viro 	.open = ecryptfs_dir_open,
4066a480a78SAl Viro 	.release = ecryptfs_dir_release,
407237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
4086a480a78SAl Viro 	.llseek = ecryptfs_dir_llseek,
409237fead6SMichael Halcrow };
410237fead6SMichael Halcrow 
411237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = {
41253a2731fSMichael Halcrow 	.llseek = generic_file_llseek,
41302797829SAl Viro 	.read_iter = ecryptfs_read_update_atime,
4148174202bSAl Viro 	.write_iter = generic_file_write_iter,
415c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
416c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
417c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
418c43f7b8fSTyler Hicks #endif
419*f0fe970dSJeff Mahoney 	.mmap = ecryptfs_mmap,
420237fead6SMichael Halcrow 	.open = ecryptfs_open,
421237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
422237fead6SMichael Halcrow 	.release = ecryptfs_release,
423237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
424237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
425e9f6a99cSMichael Halcrow 	.splice_read = generic_file_splice_read,
426237fead6SMichael Halcrow };
427