xref: /linux/fs/ecryptfs/file.c (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a62187ebSLee Jones /*
3237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
4237fead6SMichael Halcrow  *
5237fead6SMichael Halcrow  * Copyright (C) 1997-2004 Erez Zadok
6237fead6SMichael Halcrow  * Copyright (C) 2001-2004 Stony Brook University
7dd2a3b7aSMichael Halcrow  * Copyright (C) 2004-2007 International Business Machines Corp.
8237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9237fead6SMichael Halcrow  *   		Michael C. Thompson <mcthomps@us.ibm.com>
10237fead6SMichael Halcrow  */
11237fead6SMichael Halcrow 
12237fead6SMichael Halcrow #include <linux/file.h>
13237fead6SMichael Halcrow #include <linux/poll.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15237fead6SMichael Halcrow #include <linux/mount.h>
16237fead6SMichael Halcrow #include <linux/pagemap.h>
17237fead6SMichael Halcrow #include <linux/security.h>
18237fead6SMichael Halcrow #include <linux/compat.h>
190cc72dc7SJosef "Jeff" Sipek #include <linux/fs_stack.h>
20237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
21237fead6SMichael Halcrow 
22a62187ebSLee Jones /*
23237fead6SMichael Halcrow  * ecryptfs_read_update_atime
24237fead6SMichael Halcrow  *
25237fead6SMichael Halcrow  * generic_file_read updates the atime of upper layer inode.  But, it
26237fead6SMichael Halcrow  * doesn't give us a chance to update the atime of the lower layer
27237fead6SMichael Halcrow  * inode.  This function is a wrapper to generic_file_read.  It
28237fead6SMichael Halcrow  * updates the atime of the lower level inode if generic_file_read
29237fead6SMichael Halcrow  * returns without any errors. This is to be used only for file reads.
30237fead6SMichael Halcrow  * The function to be used for directory reads is ecryptfs_read.
31237fead6SMichael Halcrow  */
ecryptfs_read_update_atime(struct kiocb * iocb,struct iov_iter * to)32237fead6SMichael Halcrow static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb,
3302797829SAl Viro 				struct iov_iter *to)
34237fead6SMichael Halcrow {
3538a708d7SEdward Shishkin 	ssize_t rc;
3688569546SAl Viro 	const struct path *path;
37237fead6SMichael Halcrow 	struct file *file = iocb->ki_filp;
38237fead6SMichael Halcrow 
3902797829SAl Viro 	rc = generic_file_read_iter(iocb, to);
40237fead6SMichael Halcrow 	if (rc >= 0) {
41cc18ec3cSMatthew Wilcox 		path = ecryptfs_dentry_to_lower_path(file->f_path.dentry);
42cc18ec3cSMatthew Wilcox 		touch_atime(path);
43237fead6SMichael Halcrow 	}
44237fead6SMichael Halcrow 	return rc;
45237fead6SMichael Halcrow }
46237fead6SMichael Halcrow 
47390df3b8SDavid Howells /*
48390df3b8SDavid Howells  * ecryptfs_splice_read_update_atime
49390df3b8SDavid Howells  *
50*2cb1e089SDavid Howells  * filemap_splice_read updates the atime of upper layer inode.  But, it
51390df3b8SDavid Howells  * doesn't give us a chance to update the atime of the lower layer inode.  This
52390df3b8SDavid Howells  * function is a wrapper to generic_file_read.  It updates the atime of the
53390df3b8SDavid Howells  * lower level inode if generic_file_read returns without any errors. This is
54390df3b8SDavid Howells  * to be used only for file reads.  The function to be used for directory reads
55390df3b8SDavid Howells  * is ecryptfs_read.
56390df3b8SDavid Howells  */
ecryptfs_splice_read_update_atime(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)57390df3b8SDavid Howells static ssize_t ecryptfs_splice_read_update_atime(struct file *in, loff_t *ppos,
58390df3b8SDavid Howells 						 struct pipe_inode_info *pipe,
59390df3b8SDavid Howells 						 size_t len, unsigned int flags)
60390df3b8SDavid Howells {
61390df3b8SDavid Howells 	ssize_t rc;
62390df3b8SDavid Howells 	const struct path *path;
63390df3b8SDavid Howells 
64*2cb1e089SDavid Howells 	rc = filemap_splice_read(in, ppos, pipe, len, flags);
65390df3b8SDavid Howells 	if (rc >= 0) {
66390df3b8SDavid Howells 		path = ecryptfs_dentry_to_lower_path(in->f_path.dentry);
67390df3b8SDavid Howells 		touch_atime(path);
68390df3b8SDavid Howells 	}
69390df3b8SDavid Howells 	return rc;
70390df3b8SDavid Howells }
71390df3b8SDavid Howells 
72237fead6SMichael Halcrow struct ecryptfs_getdents_callback {
735c0ba4e0SAl Viro 	struct dir_context ctx;
742de5f059SAl Viro 	struct dir_context *caller;
750747fdb2SAl Viro 	struct super_block *sb;
76237fead6SMichael Halcrow 	int filldir_called;
77237fead6SMichael Halcrow 	int entries_written;
78237fead6SMichael Halcrow };
79237fead6SMichael Halcrow 
807d6c7045SMichael Halcrow /* Inspired by generic filldir in fs/readdir.c */
8125885a35SAl Viro static bool
ecryptfs_filldir(struct dir_context * ctx,const char * lower_name,int lower_namelen,loff_t offset,u64 ino,unsigned int d_type)82ac7576f4SMiklos Szeredi ecryptfs_filldir(struct dir_context *ctx, const char *lower_name,
83ac7576f4SMiklos Szeredi 		 int lower_namelen, loff_t offset, u64 ino, unsigned int d_type)
84237fead6SMichael Halcrow {
85237fead6SMichael Halcrow 	struct ecryptfs_getdents_callback *buf =
86ac7576f4SMiklos Szeredi 		container_of(ctx, struct ecryptfs_getdents_callback, ctx);
87a8f12864SMichael Halcrow 	size_t name_size;
88addd65adSMichael Halcrow 	char *name;
8925885a35SAl Viro 	int err;
9025885a35SAl Viro 	bool res;
91237fead6SMichael Halcrow 
92237fead6SMichael Halcrow 	buf->filldir_called++;
9325885a35SAl Viro 	err = ecryptfs_decode_and_decrypt_filename(&name, &name_size,
940747fdb2SAl Viro 						   buf->sb, lower_name,
95addd65adSMichael Halcrow 						   lower_namelen);
9625885a35SAl Viro 	if (err) {
9725885a35SAl Viro 		if (err != -EINVAL) {
98e86281e7STyler Hicks 			ecryptfs_printk(KERN_DEBUG,
99e86281e7STyler Hicks 					"%s: Error attempting to decode and decrypt filename [%s]; rc = [%d]\n",
10025885a35SAl Viro 					__func__, lower_name, err);
10125885a35SAl Viro 			return false;
102237fead6SMichael Halcrow 		}
103e86281e7STyler Hicks 
104e86281e7STyler Hicks 		/* Mask -EINVAL errors as these are most likely due a plaintext
105e86281e7STyler Hicks 		 * filename present in the lower filesystem despite filename
106e86281e7STyler Hicks 		 * encryption being enabled. One unavoidable example would be
107e86281e7STyler Hicks 		 * the "lost+found" dentry in the root directory of an Ext4
108e86281e7STyler Hicks 		 * filesystem.
109e86281e7STyler Hicks 		 */
11025885a35SAl Viro 		return true;
111e86281e7STyler Hicks 	}
112e86281e7STyler Hicks 
1132de5f059SAl Viro 	buf->caller->pos = buf->ctx.pos;
11425885a35SAl Viro 	res = dir_emit(buf->caller, name, name_size, ino, d_type);
115addd65adSMichael Halcrow 	kfree(name);
11625885a35SAl Viro 	if (res)
117237fead6SMichael Halcrow 		buf->entries_written++;
11825885a35SAl Viro 	return res;
119237fead6SMichael Halcrow }
120237fead6SMichael Halcrow 
121237fead6SMichael Halcrow /**
122237fead6SMichael Halcrow  * ecryptfs_readdir
123addd65adSMichael Halcrow  * @file: The eCryptfs directory file
1242de5f059SAl Viro  * @ctx: The actor to feed the entries to
125237fead6SMichael Halcrow  */
ecryptfs_readdir(struct file * file,struct dir_context * ctx)1262de5f059SAl Viro static int ecryptfs_readdir(struct file *file, struct dir_context *ctx)
127237fead6SMichael Halcrow {
128237fead6SMichael Halcrow 	int rc;
129237fead6SMichael Halcrow 	struct file *lower_file;
1300747fdb2SAl Viro 	struct inode *inode = file_inode(file);
1312de5f059SAl Viro 	struct ecryptfs_getdents_callback buf = {
1322de5f059SAl Viro 		.ctx.actor = ecryptfs_filldir,
1332de5f059SAl Viro 		.caller = ctx,
1340747fdb2SAl Viro 		.sb = inode->i_sb,
1352de5f059SAl Viro 	};
136237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
1375c0ba4e0SAl Viro 	rc = iterate_dir(lower_file, &buf.ctx);
1382de5f059SAl Viro 	ctx->pos = buf.ctx.pos;
13925885a35SAl Viro 	if (rc >= 0 && (buf.entries_written || !buf.filldir_called))
14025885a35SAl Viro 		fsstack_copy_attr_atime(inode, file_inode(lower_file));
141237fead6SMichael Halcrow 	return rc;
142237fead6SMichael Halcrow }
143237fead6SMichael Halcrow 
144237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache;
145237fead6SMichael Halcrow 
read_or_initialize_metadata(struct dentry * dentry)146e3ccaa97STyler Hicks static int read_or_initialize_metadata(struct dentry *dentry)
147e3ccaa97STyler Hicks {
1482b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
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 
ecryptfs_mmap(struct file * file,struct vm_area_struct * vma)188f0fe970dSJeff Mahoney static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
189f0fe970dSJeff Mahoney {
190f0fe970dSJeff Mahoney 	struct file *lower_file = ecryptfs_file_to_lower(file);
191f0fe970dSJeff Mahoney 	/*
192f0fe970dSJeff Mahoney 	 * Don't allow mmap on top of file systems that don't support it
193f0fe970dSJeff Mahoney 	 * natively.  If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
194f0fe970dSJeff Mahoney 	 * allows recursive mounting, this will need to be extended.
195f0fe970dSJeff Mahoney 	 */
196f0fe970dSJeff Mahoney 	if (!lower_file->f_op->mmap)
197f0fe970dSJeff Mahoney 		return -ENODEV;
198f0fe970dSJeff Mahoney 	return generic_file_mmap(file, vma);
199f0fe970dSJeff Mahoney }
200f0fe970dSJeff Mahoney 
201237fead6SMichael Halcrow /**
202237fead6SMichael Halcrow  * ecryptfs_open
20340f0fd37SChris J Arges  * @inode: inode specifying file to open
204237fead6SMichael Halcrow  * @file: Structure to return filled in
205237fead6SMichael Halcrow  *
206237fead6SMichael Halcrow  * Opens the file specified by inode.
207237fead6SMichael Halcrow  *
208237fead6SMichael Halcrow  * Returns zero on success; non-zero otherwise
209237fead6SMichael Halcrow  */
ecryptfs_open(struct inode * inode,struct file * file)210237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file)
211237fead6SMichael Halcrow {
212237fead6SMichael Halcrow 	int rc = 0;
213237fead6SMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat = NULL;
214bd243a4bSJosef "Jeff" Sipek 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
215237fead6SMichael Halcrow 	/* Private value of ecryptfs_dentry allocated in
216237fead6SMichael Halcrow 	 * ecryptfs_lookup() */
217237fead6SMichael Halcrow 	struct ecryptfs_file_info *file_info;
218237fead6SMichael Halcrow 
219237fead6SMichael Halcrow 	/* Released in ecryptfs_release or end of function if failure */
220c3762229SRobert P. J. Day 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
221237fead6SMichael Halcrow 	ecryptfs_set_file_private(file, file_info);
222237fead6SMichael Halcrow 	if (!file_info) {
223237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
224237fead6SMichael Halcrow 				"Error attempting to allocate memory\n");
225237fead6SMichael Halcrow 		rc = -ENOMEM;
226237fead6SMichael Halcrow 		goto out;
227237fead6SMichael Halcrow 	}
228237fead6SMichael Halcrow 	crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
229237fead6SMichael Halcrow 	mutex_lock(&crypt_stat->cs_mutex);
230e2bd99ecSMichael Halcrow 	if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) {
231237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n");
232237fead6SMichael Halcrow 		/* Policy code enabled in future release */
2332ed92554SMichael Halcrow 		crypt_stat->flags |= (ECRYPTFS_POLICY_APPLIED
2342ed92554SMichael Halcrow 				      | ECRYPTFS_ENCRYPTED);
235237fead6SMichael Halcrow 	}
236237fead6SMichael Halcrow 	mutex_unlock(&crypt_stat->cs_mutex);
2373b06b3ebSTyler Hicks 	rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
23872b55fffSMichael Halcrow 	if (rc) {
23972b55fffSMichael Halcrow 		printk(KERN_ERR "%s: Error attempting to initialize "
240332ab16fSTyler Hicks 			"the lower file for the dentry with name "
2419e78d14aSDavid Howells 			"[%pd]; rc = [%d]\n", __func__,
2429e78d14aSDavid Howells 			ecryptfs_dentry, rc);
243ceeab929SJulia Lawall 		goto out_free;
24472b55fffSMichael Halcrow 	}
2450abe1169SRoberto Sassu 	if ((ecryptfs_inode_to_private(inode)->lower_file->f_flags & O_ACCMODE)
2460abe1169SRoberto Sassu 	    == O_RDONLY && (file->f_flags & O_ACCMODE) != O_RDONLY) {
247e27759d7SErez Zadok 		rc = -EPERM;
248332ab16fSTyler Hicks 		printk(KERN_WARNING "%s: Lower file is RO; eCryptfs "
249e27759d7SErez Zadok 		       "file must hence be opened RO\n", __func__);
250332ab16fSTyler Hicks 		goto out_put;
251e27759d7SErez Zadok 	}
2522ed92554SMichael Halcrow 	ecryptfs_set_file_lower(
2532ed92554SMichael Halcrow 		file, ecryptfs_inode_to_private(inode)->lower_file);
254e3ccaa97STyler Hicks 	rc = read_or_initialize_metadata(ecryptfs_dentry);
255e3ccaa97STyler Hicks 	if (rc)
256332ab16fSTyler Hicks 		goto out_put;
257888d57bbSJoe Perches 	ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = "
258888d57bbSJoe Perches 			"[0x%.16lx] size: [0x%.16llx]\n", inode, inode->i_ino,
259888d57bbSJoe Perches 			(unsigned long long)i_size_read(inode));
260237fead6SMichael Halcrow 	goto out;
261332ab16fSTyler Hicks out_put:
262332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
2632ed92554SMichael Halcrow out_free:
264237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
265237fead6SMichael Halcrow 			ecryptfs_file_to_private(file));
266237fead6SMichael Halcrow out:
267237fead6SMichael Halcrow 	return rc;
268237fead6SMichael Halcrow }
269237fead6SMichael Halcrow 
2706a480a78SAl Viro /**
2716a480a78SAl Viro  * ecryptfs_dir_open
27240f0fd37SChris J Arges  * @inode: inode specifying file to open
2736a480a78SAl Viro  * @file: Structure to return filled in
2746a480a78SAl Viro  *
2756a480a78SAl Viro  * Opens the file specified by inode.
2766a480a78SAl Viro  *
2776a480a78SAl Viro  * Returns zero on success; non-zero otherwise
2786a480a78SAl Viro  */
ecryptfs_dir_open(struct inode * inode,struct file * file)2796a480a78SAl Viro static int ecryptfs_dir_open(struct inode *inode, struct file *file)
2806a480a78SAl Viro {
2816a480a78SAl Viro 	struct dentry *ecryptfs_dentry = file->f_path.dentry;
2826a480a78SAl Viro 	/* Private value of ecryptfs_dentry allocated in
2836a480a78SAl Viro 	 * ecryptfs_lookup() */
2846a480a78SAl Viro 	struct ecryptfs_file_info *file_info;
2856a480a78SAl Viro 	struct file *lower_file;
2866a480a78SAl Viro 
2876a480a78SAl Viro 	/* Released in ecryptfs_release or end of function if failure */
2886a480a78SAl Viro 	file_info = kmem_cache_zalloc(ecryptfs_file_info_cache, GFP_KERNEL);
2896a480a78SAl Viro 	ecryptfs_set_file_private(file, file_info);
2906a480a78SAl Viro 	if (unlikely(!file_info)) {
2916a480a78SAl Viro 		ecryptfs_printk(KERN_ERR,
2926a480a78SAl Viro 				"Error attempting to allocate memory\n");
2936a480a78SAl Viro 		return -ENOMEM;
2946a480a78SAl Viro 	}
2956a480a78SAl Viro 	lower_file = dentry_open(ecryptfs_dentry_to_lower_path(ecryptfs_dentry),
2966a480a78SAl Viro 				 file->f_flags, current_cred());
2976a480a78SAl Viro 	if (IS_ERR(lower_file)) {
2986a480a78SAl Viro 		printk(KERN_ERR "%s: Error attempting to initialize "
2996a480a78SAl Viro 			"the lower file for the dentry with name "
3006a480a78SAl Viro 			"[%pd]; rc = [%ld]\n", __func__,
3016a480a78SAl Viro 			ecryptfs_dentry, PTR_ERR(lower_file));
3026a480a78SAl Viro 		kmem_cache_free(ecryptfs_file_info_cache, file_info);
3036a480a78SAl Viro 		return PTR_ERR(lower_file);
3046a480a78SAl Viro 	}
3056a480a78SAl Viro 	ecryptfs_set_file_lower(file, lower_file);
3066a480a78SAl Viro 	return 0;
3076a480a78SAl Viro }
3086a480a78SAl Viro 
ecryptfs_flush(struct file * file,fl_owner_t td)309237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td)
310237fead6SMichael Halcrow {
31164e6651dSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
31264e6651dSTyler Hicks 
31372c2d531SAl Viro 	if (lower_file->f_op->flush) {
31464e6651dSTyler Hicks 		filemap_write_and_wait(file->f_mapping);
31564e6651dSTyler Hicks 		return lower_file->f_op->flush(lower_file, td);
31664e6651dSTyler Hicks 	}
31764e6651dSTyler Hicks 
31864e6651dSTyler Hicks 	return 0;
319237fead6SMichael Halcrow }
320237fead6SMichael Halcrow 
ecryptfs_release(struct inode * inode,struct file * file)321237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file)
322237fead6SMichael Halcrow {
323332ab16fSTyler Hicks 	ecryptfs_put_lower_file(inode);
3242ed92554SMichael Halcrow 	kmem_cache_free(ecryptfs_file_info_cache,
3252ed92554SMichael Halcrow 			ecryptfs_file_to_private(file));
3262ed92554SMichael Halcrow 	return 0;
327237fead6SMichael Halcrow }
328237fead6SMichael Halcrow 
ecryptfs_dir_release(struct inode * inode,struct file * file)3296a480a78SAl Viro static int ecryptfs_dir_release(struct inode *inode, struct file *file)
3306a480a78SAl Viro {
3316a480a78SAl Viro 	fput(ecryptfs_file_to_lower(file));
3326a480a78SAl Viro 	kmem_cache_free(ecryptfs_file_info_cache,
3336a480a78SAl Viro 			ecryptfs_file_to_private(file));
3346a480a78SAl Viro 	return 0;
3356a480a78SAl Viro }
3366a480a78SAl Viro 
ecryptfs_dir_llseek(struct file * file,loff_t offset,int whence)3376a480a78SAl Viro static loff_t ecryptfs_dir_llseek(struct file *file, loff_t offset, int whence)
3386a480a78SAl Viro {
3396a480a78SAl Viro 	return vfs_llseek(ecryptfs_file_to_lower(file), offset, whence);
3406a480a78SAl Viro }
3416a480a78SAl Viro 
342237fead6SMichael Halcrow static int
ecryptfs_fsync(struct file * file,loff_t start,loff_t end,int datasync)34302c24a82SJosef Bacik ecryptfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
344237fead6SMichael Halcrow {
345bc5abcf7STyler Hicks 	int rc;
346bc5abcf7STyler Hicks 
3476d4b5124SJeff Layton 	rc = file_write_and_wait(file);
348bc5abcf7STyler Hicks 	if (rc)
349bc5abcf7STyler Hicks 		return rc;
350bc5abcf7STyler Hicks 
351821f7494STyler Hicks 	return vfs_fsync(ecryptfs_file_to_lower(file), datasync);
352237fead6SMichael Halcrow }
353237fead6SMichael Halcrow 
ecryptfs_fasync(int fd,struct file * file,int flag)354237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag)
355237fead6SMichael Halcrow {
356237fead6SMichael Halcrow 	int rc = 0;
357237fead6SMichael Halcrow 	struct file *lower_file = NULL;
358237fead6SMichael Halcrow 
359237fead6SMichael Halcrow 	lower_file = ecryptfs_file_to_lower(file);
36072c2d531SAl Viro 	if (lower_file->f_op->fasync)
361237fead6SMichael Halcrow 		rc = lower_file->f_op->fasync(fd, lower_file, flag);
362237fead6SMichael Halcrow 	return rc;
363237fead6SMichael Halcrow }
364237fead6SMichael Halcrow 
365c43f7b8fSTyler Hicks static long
ecryptfs_unlocked_ioctl(struct file * file,unsigned int cmd,unsigned long arg)366c43f7b8fSTyler Hicks ecryptfs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
367c43f7b8fSTyler Hicks {
3682000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
369c43f7b8fSTyler Hicks 	long rc = -ENOTTY;
370c43f7b8fSTyler Hicks 
3716d65261aSTyler Hicks 	if (!lower_file->f_op->unlocked_ioctl)
372c43f7b8fSTyler Hicks 		return rc;
3736d65261aSTyler Hicks 
3746d65261aSTyler Hicks 	switch (cmd) {
3756d65261aSTyler Hicks 	case FITRIM:
3766d65261aSTyler Hicks 	case FS_IOC_GETFLAGS:
3776d65261aSTyler Hicks 	case FS_IOC_SETFLAGS:
3786d65261aSTyler Hicks 	case FS_IOC_GETVERSION:
3796d65261aSTyler Hicks 	case FS_IOC_SETVERSION:
3806d65261aSTyler Hicks 		rc = lower_file->f_op->unlocked_ioctl(lower_file, cmd, arg);
3816d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
3826d65261aSTyler Hicks 
3836d65261aSTyler Hicks 		return rc;
3846d65261aSTyler Hicks 	default:
3856d65261aSTyler Hicks 		return rc;
3866d65261aSTyler Hicks 	}
387c43f7b8fSTyler Hicks }
388c43f7b8fSTyler Hicks 
389c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
390c43f7b8fSTyler Hicks static long
ecryptfs_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)391c43f7b8fSTyler Hicks ecryptfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
392c43f7b8fSTyler Hicks {
3932000f5beSTyler Hicks 	struct file *lower_file = ecryptfs_file_to_lower(file);
394c43f7b8fSTyler Hicks 	long rc = -ENOIOCTLCMD;
395c43f7b8fSTyler Hicks 
3966d65261aSTyler Hicks 	if (!lower_file->f_op->compat_ioctl)
397c43f7b8fSTyler Hicks 		return rc;
3986d65261aSTyler Hicks 
3996d65261aSTyler Hicks 	switch (cmd) {
400314999dcSArnd Bergmann 	case FITRIM:
4016d65261aSTyler Hicks 	case FS_IOC32_GETFLAGS:
4026d65261aSTyler Hicks 	case FS_IOC32_SETFLAGS:
4036d65261aSTyler Hicks 	case FS_IOC32_GETVERSION:
4046d65261aSTyler Hicks 	case FS_IOC32_SETVERSION:
4056d65261aSTyler Hicks 		rc = lower_file->f_op->compat_ioctl(lower_file, cmd, arg);
4066d65261aSTyler Hicks 		fsstack_copy_attr_all(file_inode(file), file_inode(lower_file));
4076d65261aSTyler Hicks 
4086d65261aSTyler Hicks 		return rc;
4096d65261aSTyler Hicks 	default:
4106d65261aSTyler Hicks 		return rc;
4116d65261aSTyler Hicks 	}
412c43f7b8fSTyler Hicks }
413c43f7b8fSTyler Hicks #endif
414237fead6SMichael Halcrow 
415237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = {
41651a16a9cSAl Viro 	.iterate_shared = ecryptfs_readdir,
417323ef68fSAndy Whitcroft 	.read = generic_read_dir,
418c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
419c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
420c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
421c43f7b8fSTyler Hicks #endif
4226a480a78SAl Viro 	.open = ecryptfs_dir_open,
4236a480a78SAl Viro 	.release = ecryptfs_dir_release,
424237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
4256a480a78SAl Viro 	.llseek = ecryptfs_dir_llseek,
426237fead6SMichael Halcrow };
427237fead6SMichael Halcrow 
428237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = {
42953a2731fSMichael Halcrow 	.llseek = generic_file_llseek,
43002797829SAl Viro 	.read_iter = ecryptfs_read_update_atime,
4318174202bSAl Viro 	.write_iter = generic_file_write_iter,
432c43f7b8fSTyler Hicks 	.unlocked_ioctl = ecryptfs_unlocked_ioctl,
433c43f7b8fSTyler Hicks #ifdef CONFIG_COMPAT
434c43f7b8fSTyler Hicks 	.compat_ioctl = ecryptfs_compat_ioctl,
435c43f7b8fSTyler Hicks #endif
436f0fe970dSJeff Mahoney 	.mmap = ecryptfs_mmap,
437237fead6SMichael Halcrow 	.open = ecryptfs_open,
438237fead6SMichael Halcrow 	.flush = ecryptfs_flush,
439237fead6SMichael Halcrow 	.release = ecryptfs_release,
440237fead6SMichael Halcrow 	.fsync = ecryptfs_fsync,
441237fead6SMichael Halcrow 	.fasync = ecryptfs_fasync,
442390df3b8SDavid Howells 	.splice_read = ecryptfs_splice_read_update_atime,
443237fead6SMichael Halcrow };
444