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 6237fead6SMichael Halcrow * Copyright (C) 2004-2006 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/smp_lock.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_llseek 38237fead6SMichael Halcrow * @file: File we are seeking in 39237fead6SMichael Halcrow * @offset: The offset to seek to 40237fead6SMichael Halcrow * @origin: 2 - offset from i_size; 1 - offset from f_pos 41237fead6SMichael Halcrow * 42237fead6SMichael Halcrow * Returns the position we have seeked to, or negative on error 43237fead6SMichael Halcrow */ 44237fead6SMichael Halcrow static loff_t ecryptfs_llseek(struct file *file, loff_t offset, int origin) 45237fead6SMichael Halcrow { 46237fead6SMichael Halcrow loff_t rv; 47237fead6SMichael Halcrow loff_t new_end_pos; 48237fead6SMichael Halcrow int rc; 49237fead6SMichael Halcrow int expanding_file = 0; 50237fead6SMichael Halcrow struct inode *inode = file->f_mapping->host; 51237fead6SMichael Halcrow 52237fead6SMichael Halcrow /* If our offset is past the end of our file, we're going to 53237fead6SMichael Halcrow * need to grow it so we have a valid length of 0's */ 54237fead6SMichael Halcrow new_end_pos = offset; 55237fead6SMichael Halcrow switch (origin) { 56237fead6SMichael Halcrow case 2: 57237fead6SMichael Halcrow new_end_pos += i_size_read(inode); 58237fead6SMichael Halcrow expanding_file = 1; 59237fead6SMichael Halcrow break; 60237fead6SMichael Halcrow case 1: 61237fead6SMichael Halcrow new_end_pos += file->f_pos; 62237fead6SMichael Halcrow if (new_end_pos > i_size_read(inode)) { 63237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "new_end_pos(=[0x%.16x]) " 64237fead6SMichael Halcrow "> i_size_read(inode)(=[0x%.16x])\n", 65237fead6SMichael Halcrow new_end_pos, i_size_read(inode)); 66237fead6SMichael Halcrow expanding_file = 1; 67237fead6SMichael Halcrow } 68237fead6SMichael Halcrow break; 69237fead6SMichael Halcrow default: 70237fead6SMichael Halcrow if (new_end_pos > i_size_read(inode)) { 71237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "new_end_pos(=[0x%.16x]) " 72237fead6SMichael Halcrow "> i_size_read(inode)(=[0x%.16x])\n", 73237fead6SMichael Halcrow new_end_pos, i_size_read(inode)); 74237fead6SMichael Halcrow expanding_file = 1; 75237fead6SMichael Halcrow } 76237fead6SMichael Halcrow } 77237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "new_end_pos = [0x%.16x]\n", new_end_pos); 78237fead6SMichael Halcrow if (expanding_file) { 79*bd243a4bSJosef "Jeff" Sipek rc = ecryptfs_truncate(file->f_path.dentry, new_end_pos); 80237fead6SMichael Halcrow if (rc) { 81237fead6SMichael Halcrow rv = rc; 82237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error on attempt to " 83237fead6SMichael Halcrow "truncate to (higher) offset [0x%.16x];" 84237fead6SMichael Halcrow " rc = [%d]\n", new_end_pos, rc); 85237fead6SMichael Halcrow goto out; 86237fead6SMichael Halcrow } 87237fead6SMichael Halcrow } 88237fead6SMichael Halcrow rv = generic_file_llseek(file, offset, origin); 89237fead6SMichael Halcrow out: 90237fead6SMichael Halcrow return rv; 91237fead6SMichael Halcrow } 92237fead6SMichael Halcrow 93237fead6SMichael Halcrow /** 94237fead6SMichael Halcrow * ecryptfs_read_update_atime 95237fead6SMichael Halcrow * 96237fead6SMichael Halcrow * generic_file_read updates the atime of upper layer inode. But, it 97237fead6SMichael Halcrow * doesn't give us a chance to update the atime of the lower layer 98237fead6SMichael Halcrow * inode. This function is a wrapper to generic_file_read. It 99237fead6SMichael Halcrow * updates the atime of the lower level inode if generic_file_read 100237fead6SMichael Halcrow * returns without any errors. This is to be used only for file reads. 101237fead6SMichael Halcrow * The function to be used for directory reads is ecryptfs_read. 102237fead6SMichael Halcrow */ 103237fead6SMichael Halcrow static ssize_t ecryptfs_read_update_atime(struct kiocb *iocb, 104237fead6SMichael Halcrow const struct iovec *iov, 105237fead6SMichael Halcrow unsigned long nr_segs, loff_t pos) 106237fead6SMichael Halcrow { 107237fead6SMichael Halcrow int rc; 108237fead6SMichael Halcrow struct dentry *lower_dentry; 109237fead6SMichael Halcrow struct vfsmount *lower_vfsmount; 110237fead6SMichael Halcrow struct file *file = iocb->ki_filp; 111237fead6SMichael Halcrow 112237fead6SMichael Halcrow rc = generic_file_aio_read(iocb, iov, nr_segs, pos); 113237fead6SMichael Halcrow /* 114237fead6SMichael Halcrow * Even though this is a async interface, we need to wait 115237fead6SMichael Halcrow * for IO to finish to update atime 116237fead6SMichael Halcrow */ 117237fead6SMichael Halcrow if (-EIOCBQUEUED == rc) 118237fead6SMichael Halcrow rc = wait_on_sync_kiocb(iocb); 119237fead6SMichael Halcrow if (rc >= 0) { 120*bd243a4bSJosef "Jeff" Sipek lower_dentry = ecryptfs_dentry_to_lower(file->f_path.dentry); 121*bd243a4bSJosef "Jeff" Sipek lower_vfsmount = ecryptfs_dentry_to_lower_mnt(file->f_path.dentry); 122237fead6SMichael Halcrow touch_atime(lower_vfsmount, lower_dentry); 123237fead6SMichael Halcrow } 124237fead6SMichael Halcrow return rc; 125237fead6SMichael Halcrow } 126237fead6SMichael Halcrow 127237fead6SMichael Halcrow struct ecryptfs_getdents_callback { 128237fead6SMichael Halcrow void *dirent; 129237fead6SMichael Halcrow struct dentry *dentry; 130237fead6SMichael Halcrow filldir_t filldir; 131237fead6SMichael Halcrow int err; 132237fead6SMichael Halcrow int filldir_called; 133237fead6SMichael Halcrow int entries_written; 134237fead6SMichael Halcrow }; 135237fead6SMichael Halcrow 136237fead6SMichael Halcrow /* Inspired by generic filldir in fs/readir.c */ 137237fead6SMichael Halcrow static int 138237fead6SMichael Halcrow ecryptfs_filldir(void *dirent, const char *name, int namelen, loff_t offset, 139237fead6SMichael Halcrow u64 ino, unsigned int d_type) 140237fead6SMichael Halcrow { 141237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 142237fead6SMichael Halcrow struct ecryptfs_getdents_callback *buf = 143237fead6SMichael Halcrow (struct ecryptfs_getdents_callback *)dirent; 144237fead6SMichael Halcrow int rc; 145237fead6SMichael Halcrow int decoded_length; 146237fead6SMichael Halcrow char *decoded_name; 147237fead6SMichael Halcrow 148237fead6SMichael Halcrow crypt_stat = ecryptfs_dentry_to_private(buf->dentry)->crypt_stat; 149237fead6SMichael Halcrow buf->filldir_called++; 150237fead6SMichael Halcrow decoded_length = ecryptfs_decode_filename(crypt_stat, name, namelen, 151237fead6SMichael Halcrow &decoded_name); 152237fead6SMichael Halcrow if (decoded_length < 0) { 153237fead6SMichael Halcrow rc = decoded_length; 154237fead6SMichael Halcrow goto out; 155237fead6SMichael Halcrow } 156237fead6SMichael Halcrow rc = buf->filldir(buf->dirent, decoded_name, decoded_length, offset, 157237fead6SMichael Halcrow ino, d_type); 158237fead6SMichael Halcrow kfree(decoded_name); 159237fead6SMichael Halcrow if (rc >= 0) 160237fead6SMichael Halcrow buf->entries_written++; 161237fead6SMichael Halcrow out: 162237fead6SMichael Halcrow return rc; 163237fead6SMichael Halcrow } 164237fead6SMichael Halcrow 165237fead6SMichael Halcrow /** 166237fead6SMichael Halcrow * ecryptfs_readdir 167237fead6SMichael Halcrow * @file: The ecryptfs file struct 168237fead6SMichael Halcrow * @dirent: Directory entry 169237fead6SMichael Halcrow * @filldir: The filldir callback function 170237fead6SMichael Halcrow */ 171237fead6SMichael Halcrow static int ecryptfs_readdir(struct file *file, void *dirent, filldir_t filldir) 172237fead6SMichael Halcrow { 173237fead6SMichael Halcrow int rc; 174237fead6SMichael Halcrow struct file *lower_file; 175237fead6SMichael Halcrow struct inode *inode; 176237fead6SMichael Halcrow struct ecryptfs_getdents_callback buf; 177237fead6SMichael Halcrow 178237fead6SMichael Halcrow lower_file = ecryptfs_file_to_lower(file); 179237fead6SMichael Halcrow lower_file->f_pos = file->f_pos; 180*bd243a4bSJosef "Jeff" Sipek inode = file->f_path.dentry->d_inode; 181237fead6SMichael Halcrow memset(&buf, 0, sizeof(buf)); 182237fead6SMichael Halcrow buf.dirent = dirent; 183*bd243a4bSJosef "Jeff" Sipek buf.dentry = file->f_path.dentry; 184237fead6SMichael Halcrow buf.filldir = filldir; 185237fead6SMichael Halcrow retry: 186237fead6SMichael Halcrow buf.filldir_called = 0; 187237fead6SMichael Halcrow buf.entries_written = 0; 188237fead6SMichael Halcrow buf.err = 0; 189237fead6SMichael Halcrow rc = vfs_readdir(lower_file, ecryptfs_filldir, (void *)&buf); 190237fead6SMichael Halcrow if (buf.err) 191237fead6SMichael Halcrow rc = buf.err; 192237fead6SMichael Halcrow if (buf.filldir_called && !buf.entries_written) 193237fead6SMichael Halcrow goto retry; 194237fead6SMichael Halcrow file->f_pos = lower_file->f_pos; 195237fead6SMichael Halcrow if (rc >= 0) 196*bd243a4bSJosef "Jeff" Sipek fsstack_copy_attr_atime(inode, lower_file->f_path.dentry->d_inode); 197237fead6SMichael Halcrow return rc; 198237fead6SMichael Halcrow } 199237fead6SMichael Halcrow 200237fead6SMichael Halcrow struct kmem_cache *ecryptfs_file_info_cache; 201237fead6SMichael Halcrow 2027ff1d74fSMichael Halcrow int ecryptfs_open_lower_file(struct file **lower_file, 2037ff1d74fSMichael Halcrow struct dentry *lower_dentry, 2047ff1d74fSMichael Halcrow struct vfsmount *lower_mnt, int flags) 2057ff1d74fSMichael Halcrow { 2067ff1d74fSMichael Halcrow int rc = 0; 2077ff1d74fSMichael Halcrow 2087ff1d74fSMichael Halcrow dget(lower_dentry); 2097ff1d74fSMichael Halcrow mntget(lower_mnt); 2107ff1d74fSMichael Halcrow *lower_file = dentry_open(lower_dentry, lower_mnt, flags); 2117ff1d74fSMichael Halcrow if (IS_ERR(*lower_file)) { 2127ff1d74fSMichael Halcrow printk(KERN_ERR "Error opening lower file for lower_dentry " 2137ff1d74fSMichael Halcrow "[0x%p], lower_mnt [0x%p], and flags [0x%x]\n", 2147ff1d74fSMichael Halcrow lower_dentry, lower_mnt, flags); 2157ff1d74fSMichael Halcrow rc = PTR_ERR(*lower_file); 2167ff1d74fSMichael Halcrow *lower_file = NULL; 2177ff1d74fSMichael Halcrow goto out; 2187ff1d74fSMichael Halcrow } 2197ff1d74fSMichael Halcrow out: 2207ff1d74fSMichael Halcrow return rc; 2217ff1d74fSMichael Halcrow } 2227ff1d74fSMichael Halcrow 2237ff1d74fSMichael Halcrow int ecryptfs_close_lower_file(struct file *lower_file) 2247ff1d74fSMichael Halcrow { 2257ff1d74fSMichael Halcrow fput(lower_file); 2267ff1d74fSMichael Halcrow return 0; 2277ff1d74fSMichael Halcrow } 2287ff1d74fSMichael Halcrow 229237fead6SMichael Halcrow /** 230237fead6SMichael Halcrow * ecryptfs_open 231237fead6SMichael Halcrow * @inode: inode speciying file to open 232237fead6SMichael Halcrow * @file: Structure to return filled in 233237fead6SMichael Halcrow * 234237fead6SMichael Halcrow * Opens the file specified by inode. 235237fead6SMichael Halcrow * 236237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 237237fead6SMichael Halcrow */ 238237fead6SMichael Halcrow static int ecryptfs_open(struct inode *inode, struct file *file) 239237fead6SMichael Halcrow { 240237fead6SMichael Halcrow int rc = 0; 241237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = NULL; 242237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat; 243*bd243a4bSJosef "Jeff" Sipek struct dentry *ecryptfs_dentry = file->f_path.dentry; 244237fead6SMichael Halcrow /* Private value of ecryptfs_dentry allocated in 245237fead6SMichael Halcrow * ecryptfs_lookup() */ 246237fead6SMichael Halcrow struct dentry *lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); 247237fead6SMichael Halcrow struct inode *lower_inode = NULL; 248237fead6SMichael Halcrow struct file *lower_file = NULL; 249237fead6SMichael Halcrow struct vfsmount *lower_mnt; 250237fead6SMichael Halcrow struct ecryptfs_file_info *file_info; 251237fead6SMichael Halcrow int lower_flags; 252237fead6SMichael Halcrow 253237fead6SMichael Halcrow /* Released in ecryptfs_release or end of function if failure */ 254e94b1766SChristoph Lameter file_info = kmem_cache_alloc(ecryptfs_file_info_cache, GFP_KERNEL); 255237fead6SMichael Halcrow ecryptfs_set_file_private(file, file_info); 256237fead6SMichael Halcrow if (!file_info) { 257237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, 258237fead6SMichael Halcrow "Error attempting to allocate memory\n"); 259237fead6SMichael Halcrow rc = -ENOMEM; 260237fead6SMichael Halcrow goto out; 261237fead6SMichael Halcrow } 262237fead6SMichael Halcrow memset(file_info, 0, sizeof(*file_info)); 263237fead6SMichael Halcrow lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); 264237fead6SMichael Halcrow crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; 265237fead6SMichael Halcrow mount_crypt_stat = &ecryptfs_superblock_to_private( 266237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 267237fead6SMichael Halcrow mutex_lock(&crypt_stat->cs_mutex); 268237fead6SMichael Halcrow if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED)) { 269237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Setting flags for stat...\n"); 270237fead6SMichael Halcrow /* Policy code enabled in future release */ 271237fead6SMichael Halcrow ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED); 272237fead6SMichael Halcrow ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED); 273237fead6SMichael Halcrow } 274237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_mutex); 275237fead6SMichael Halcrow lower_flags = file->f_flags; 276237fead6SMichael Halcrow if ((lower_flags & O_ACCMODE) == O_WRONLY) 277237fead6SMichael Halcrow lower_flags = (lower_flags & O_ACCMODE) | O_RDWR; 278237fead6SMichael Halcrow if (file->f_flags & O_APPEND) 279237fead6SMichael Halcrow lower_flags &= ~O_APPEND; 280237fead6SMichael Halcrow lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry); 281237fead6SMichael Halcrow /* Corresponding fput() in ecryptfs_release() */ 2827ff1d74fSMichael Halcrow if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt, 2837ff1d74fSMichael Halcrow lower_flags))) { 284237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error opening lower file\n"); 285237fead6SMichael Halcrow goto out_puts; 286237fead6SMichael Halcrow } 287237fead6SMichael Halcrow ecryptfs_set_file_lower(file, lower_file); 288237fead6SMichael Halcrow /* Isn't this check the same as the one in lookup? */ 289237fead6SMichael Halcrow lower_inode = lower_dentry->d_inode; 290237fead6SMichael Halcrow if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) { 291237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "This is a directory\n"); 292237fead6SMichael Halcrow ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED); 293237fead6SMichael Halcrow rc = 0; 294237fead6SMichael Halcrow goto out; 295237fead6SMichael Halcrow } 296237fead6SMichael Halcrow mutex_lock(&crypt_stat->cs_mutex); 297237fead6SMichael Halcrow if (i_size_read(lower_inode) < ECRYPTFS_MINIMUM_HEADER_EXTENT_SIZE) { 298237fead6SMichael Halcrow if (!(mount_crypt_stat->flags 299237fead6SMichael Halcrow & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) { 300237fead6SMichael Halcrow rc = -EIO; 301237fead6SMichael Halcrow printk(KERN_WARNING "Attempt to read file that is " 302237fead6SMichael Halcrow "not in a valid eCryptfs format, and plaintext " 303237fead6SMichael Halcrow "passthrough mode is not enabled; returning " 304237fead6SMichael Halcrow "-EIO\n"); 305237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_mutex); 306237fead6SMichael Halcrow goto out_puts; 307237fead6SMichael Halcrow } 308237fead6SMichael Halcrow crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); 309237fead6SMichael Halcrow rc = 0; 310237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_mutex); 311237fead6SMichael Halcrow goto out; 312237fead6SMichael Halcrow } else if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, 313237fead6SMichael Halcrow ECRYPTFS_POLICY_APPLIED) 314237fead6SMichael Halcrow || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, 315237fead6SMichael Halcrow ECRYPTFS_KEY_VALID)) { 316237fead6SMichael Halcrow rc = ecryptfs_read_headers(ecryptfs_dentry, lower_file); 317237fead6SMichael Halcrow if (rc) { 318237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 319237fead6SMichael Halcrow "Valid headers not found\n"); 320237fead6SMichael Halcrow if (!(mount_crypt_stat->flags 321237fead6SMichael Halcrow & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) { 322237fead6SMichael Halcrow rc = -EIO; 323237fead6SMichael Halcrow printk(KERN_WARNING "Attempt to read file that " 324237fead6SMichael Halcrow "is not in a valid eCryptfs format, " 325237fead6SMichael Halcrow "and plaintext passthrough mode is not " 326237fead6SMichael Halcrow "enabled; returning -EIO\n"); 327237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_mutex); 328237fead6SMichael Halcrow goto out_puts; 329237fead6SMichael Halcrow } 330237fead6SMichael Halcrow ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, 331237fead6SMichael Halcrow ECRYPTFS_ENCRYPTED); 332237fead6SMichael Halcrow rc = 0; 333237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_mutex); 334237fead6SMichael Halcrow goto out; 335237fead6SMichael Halcrow } 336237fead6SMichael Halcrow } 337237fead6SMichael Halcrow mutex_unlock(&crypt_stat->cs_mutex); 338237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "inode w/ addr = [0x%p], i_ino = [0x%.16x] " 339237fead6SMichael Halcrow "size: [0x%.16x]\n", inode, inode->i_ino, 340237fead6SMichael Halcrow i_size_read(inode)); 341237fead6SMichael Halcrow ecryptfs_set_file_lower(file, lower_file); 342237fead6SMichael Halcrow goto out; 343237fead6SMichael Halcrow out_puts: 344237fead6SMichael Halcrow mntput(lower_mnt); 345237fead6SMichael Halcrow dput(lower_dentry); 346237fead6SMichael Halcrow kmem_cache_free(ecryptfs_file_info_cache, 347237fead6SMichael Halcrow ecryptfs_file_to_private(file)); 348237fead6SMichael Halcrow out: 349237fead6SMichael Halcrow return rc; 350237fead6SMichael Halcrow } 351237fead6SMichael Halcrow 352237fead6SMichael Halcrow static int ecryptfs_flush(struct file *file, fl_owner_t td) 353237fead6SMichael Halcrow { 354237fead6SMichael Halcrow int rc = 0; 355237fead6SMichael Halcrow struct file *lower_file = NULL; 356237fead6SMichael Halcrow 357237fead6SMichael Halcrow lower_file = ecryptfs_file_to_lower(file); 358237fead6SMichael Halcrow if (lower_file->f_op && lower_file->f_op->flush) 359237fead6SMichael Halcrow rc = lower_file->f_op->flush(lower_file, td); 360237fead6SMichael Halcrow return rc; 361237fead6SMichael Halcrow } 362237fead6SMichael Halcrow 363237fead6SMichael Halcrow static int ecryptfs_release(struct inode *inode, struct file *file) 364237fead6SMichael Halcrow { 365237fead6SMichael Halcrow struct file *lower_file = ecryptfs_file_to_lower(file); 366237fead6SMichael Halcrow struct ecryptfs_file_info *file_info = ecryptfs_file_to_private(file); 367237fead6SMichael Halcrow struct inode *lower_inode = ecryptfs_inode_to_lower(inode); 3687ff1d74fSMichael Halcrow int rc; 369237fead6SMichael Halcrow 3707ff1d74fSMichael Halcrow if ((rc = ecryptfs_close_lower_file(lower_file))) { 3717ff1d74fSMichael Halcrow printk(KERN_ERR "Error closing lower_file\n"); 3727ff1d74fSMichael Halcrow goto out; 3737ff1d74fSMichael Halcrow } 374237fead6SMichael Halcrow inode->i_blocks = lower_inode->i_blocks; 375237fead6SMichael Halcrow kmem_cache_free(ecryptfs_file_info_cache, file_info); 3767ff1d74fSMichael Halcrow out: 3777ff1d74fSMichael Halcrow return rc; 378237fead6SMichael Halcrow } 379237fead6SMichael Halcrow 380237fead6SMichael Halcrow static int 381237fead6SMichael Halcrow ecryptfs_fsync(struct file *file, struct dentry *dentry, int datasync) 382237fead6SMichael Halcrow { 383237fead6SMichael Halcrow struct file *lower_file = ecryptfs_file_to_lower(file); 384237fead6SMichael Halcrow struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 385237fead6SMichael Halcrow struct inode *lower_inode = lower_dentry->d_inode; 386237fead6SMichael Halcrow int rc = -EINVAL; 387237fead6SMichael Halcrow 388237fead6SMichael Halcrow if (lower_inode->i_fop->fsync) { 389237fead6SMichael Halcrow mutex_lock(&lower_inode->i_mutex); 390237fead6SMichael Halcrow rc = lower_inode->i_fop->fsync(lower_file, lower_dentry, 391237fead6SMichael Halcrow datasync); 392237fead6SMichael Halcrow mutex_unlock(&lower_inode->i_mutex); 393237fead6SMichael Halcrow } 394237fead6SMichael Halcrow return rc; 395237fead6SMichael Halcrow } 396237fead6SMichael Halcrow 397237fead6SMichael Halcrow static int ecryptfs_fasync(int fd, struct file *file, int flag) 398237fead6SMichael Halcrow { 399237fead6SMichael Halcrow int rc = 0; 400237fead6SMichael Halcrow struct file *lower_file = NULL; 401237fead6SMichael Halcrow 402237fead6SMichael Halcrow lower_file = ecryptfs_file_to_lower(file); 403237fead6SMichael Halcrow if (lower_file->f_op && lower_file->f_op->fasync) 404237fead6SMichael Halcrow rc = lower_file->f_op->fasync(fd, lower_file, flag); 405237fead6SMichael Halcrow return rc; 406237fead6SMichael Halcrow } 407237fead6SMichael Halcrow 408237fead6SMichael Halcrow static ssize_t ecryptfs_sendfile(struct file *file, loff_t * ppos, 409237fead6SMichael Halcrow size_t count, read_actor_t actor, void *target) 410237fead6SMichael Halcrow { 411237fead6SMichael Halcrow struct file *lower_file = NULL; 412237fead6SMichael Halcrow int rc = -EINVAL; 413237fead6SMichael Halcrow 414237fead6SMichael Halcrow lower_file = ecryptfs_file_to_lower(file); 415237fead6SMichael Halcrow if (lower_file->f_op && lower_file->f_op->sendfile) 416237fead6SMichael Halcrow rc = lower_file->f_op->sendfile(lower_file, ppos, count, 417237fead6SMichael Halcrow actor, target); 418237fead6SMichael Halcrow 419237fead6SMichael Halcrow return rc; 420237fead6SMichael Halcrow } 421237fead6SMichael Halcrow 422237fead6SMichael Halcrow static int ecryptfs_ioctl(struct inode *inode, struct file *file, 423237fead6SMichael Halcrow unsigned int cmd, unsigned long arg); 424237fead6SMichael Halcrow 425237fead6SMichael Halcrow const struct file_operations ecryptfs_dir_fops = { 426237fead6SMichael Halcrow .readdir = ecryptfs_readdir, 427237fead6SMichael Halcrow .ioctl = ecryptfs_ioctl, 428237fead6SMichael Halcrow .mmap = generic_file_mmap, 429237fead6SMichael Halcrow .open = ecryptfs_open, 430237fead6SMichael Halcrow .flush = ecryptfs_flush, 431237fead6SMichael Halcrow .release = ecryptfs_release, 432237fead6SMichael Halcrow .fsync = ecryptfs_fsync, 433237fead6SMichael Halcrow .fasync = ecryptfs_fasync, 434237fead6SMichael Halcrow .sendfile = ecryptfs_sendfile, 435237fead6SMichael Halcrow }; 436237fead6SMichael Halcrow 437237fead6SMichael Halcrow const struct file_operations ecryptfs_main_fops = { 438237fead6SMichael Halcrow .llseek = ecryptfs_llseek, 439237fead6SMichael Halcrow .read = do_sync_read, 440237fead6SMichael Halcrow .aio_read = ecryptfs_read_update_atime, 441237fead6SMichael Halcrow .write = do_sync_write, 442237fead6SMichael Halcrow .aio_write = generic_file_aio_write, 443237fead6SMichael Halcrow .readdir = ecryptfs_readdir, 444237fead6SMichael Halcrow .ioctl = ecryptfs_ioctl, 445237fead6SMichael Halcrow .mmap = generic_file_mmap, 446237fead6SMichael Halcrow .open = ecryptfs_open, 447237fead6SMichael Halcrow .flush = ecryptfs_flush, 448237fead6SMichael Halcrow .release = ecryptfs_release, 449237fead6SMichael Halcrow .fsync = ecryptfs_fsync, 450237fead6SMichael Halcrow .fasync = ecryptfs_fasync, 451237fead6SMichael Halcrow .sendfile = ecryptfs_sendfile, 452237fead6SMichael Halcrow }; 453237fead6SMichael Halcrow 454237fead6SMichael Halcrow static int 455237fead6SMichael Halcrow ecryptfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd, 456237fead6SMichael Halcrow unsigned long arg) 457237fead6SMichael Halcrow { 458237fead6SMichael Halcrow int rc = 0; 459237fead6SMichael Halcrow struct file *lower_file = NULL; 460237fead6SMichael Halcrow 461237fead6SMichael Halcrow if (ecryptfs_file_to_private(file)) 462237fead6SMichael Halcrow lower_file = ecryptfs_file_to_lower(file); 463237fead6SMichael Halcrow if (lower_file && lower_file->f_op && lower_file->f_op->ioctl) 464237fead6SMichael Halcrow rc = lower_file->f_op->ioctl(ecryptfs_inode_to_lower(inode), 465237fead6SMichael Halcrow lower_file, cmd, arg); 466237fead6SMichael Halcrow else 467237fead6SMichael Halcrow rc = -ENOTTY; 468237fead6SMichael Halcrow return rc; 469237fead6SMichael Halcrow } 470