1237fead6SMichael Halcrow /** 2237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 3237fead6SMichael Halcrow * This is where eCryptfs coordinates the symmetric encryption and 4237fead6SMichael Halcrow * decryption of the file data as it passes between the lower 5237fead6SMichael Halcrow * encrypted file and the upper decrypted file. 6237fead6SMichael Halcrow * 7237fead6SMichael Halcrow * Copyright (C) 1997-2003 Erez Zadok 8237fead6SMichael Halcrow * Copyright (C) 2001-2003 Stony Brook University 9dd2a3b7aSMichael Halcrow * Copyright (C) 2004-2007 International Business Machines Corp. 10237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 11237fead6SMichael Halcrow * 12237fead6SMichael Halcrow * This program is free software; you can redistribute it and/or 13237fead6SMichael Halcrow * modify it under the terms of the GNU General Public License as 14237fead6SMichael Halcrow * published by the Free Software Foundation; either version 2 of the 15237fead6SMichael Halcrow * License, or (at your option) any later version. 16237fead6SMichael Halcrow * 17237fead6SMichael Halcrow * This program is distributed in the hope that it will be useful, but 18237fead6SMichael Halcrow * WITHOUT ANY WARRANTY; without even the implied warranty of 19237fead6SMichael Halcrow * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20237fead6SMichael Halcrow * General Public License for more details. 21237fead6SMichael Halcrow * 22237fead6SMichael Halcrow * You should have received a copy of the GNU General Public License 23237fead6SMichael Halcrow * along with this program; if not, write to the Free Software 24237fead6SMichael Halcrow * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 25237fead6SMichael Halcrow * 02111-1307, USA. 26237fead6SMichael Halcrow */ 27237fead6SMichael Halcrow 28237fead6SMichael Halcrow #include <linux/pagemap.h> 29237fead6SMichael Halcrow #include <linux/writeback.h> 30237fead6SMichael Halcrow #include <linux/page-flags.h> 31237fead6SMichael Halcrow #include <linux/mount.h> 32237fead6SMichael Halcrow #include <linux/file.h> 33237fead6SMichael Halcrow #include <linux/crypto.h> 34237fead6SMichael Halcrow #include <linux/scatterlist.h> 35237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 36237fead6SMichael Halcrow 37237fead6SMichael Halcrow struct kmem_cache *ecryptfs_lower_page_cache; 38237fead6SMichael Halcrow 39237fead6SMichael Halcrow /** 40*16a72c45SMichael Halcrow * ecryptfs_get_locked_page 41237fead6SMichael Halcrow * 42237fead6SMichael Halcrow * Get one page from cache or lower f/s, return error otherwise. 43237fead6SMichael Halcrow * 44*16a72c45SMichael Halcrow * Returns locked and up-to-date page (if ok), with increased 45237fead6SMichael Halcrow * refcnt. 46237fead6SMichael Halcrow */ 47*16a72c45SMichael Halcrow struct page *ecryptfs_get_locked_page(struct file *file, loff_t index) 48237fead6SMichael Halcrow { 49237fead6SMichael Halcrow struct dentry *dentry; 50237fead6SMichael Halcrow struct inode *inode; 51237fead6SMichael Halcrow struct address_space *mapping; 52*16a72c45SMichael Halcrow struct page *page; 53237fead6SMichael Halcrow 54bd243a4bSJosef "Jeff" Sipek dentry = file->f_path.dentry; 55237fead6SMichael Halcrow inode = dentry->d_inode; 56237fead6SMichael Halcrow mapping = inode->i_mapping; 57*16a72c45SMichael Halcrow page = read_mapping_page(mapping, index, (void *)file); 58*16a72c45SMichael Halcrow if (!IS_ERR(page)) 59*16a72c45SMichael Halcrow lock_page(page); 60*16a72c45SMichael Halcrow return page; 61237fead6SMichael Halcrow } 62237fead6SMichael Halcrow 63237fead6SMichael Halcrow /** 64237fead6SMichael Halcrow * ecryptfs_writepage 65237fead6SMichael Halcrow * @page: Page that is locked before this call is made 66237fead6SMichael Halcrow * 67237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 68237fead6SMichael Halcrow */ 69237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) 70237fead6SMichael Halcrow { 71237fead6SMichael Halcrow int rc; 72237fead6SMichael Halcrow 730216f7f7SMichael Halcrow rc = ecryptfs_encrypt_page(page); 74237fead6SMichael Halcrow if (rc) { 75237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error encrypting " 76237fead6SMichael Halcrow "page (upper index [0x%.16x])\n", page->index); 77237fead6SMichael Halcrow ClearPageUptodate(page); 78237fead6SMichael Halcrow goto out; 79237fead6SMichael Halcrow } 80237fead6SMichael Halcrow SetPageUptodate(page); 81237fead6SMichael Halcrow unlock_page(page); 82237fead6SMichael Halcrow out: 83237fead6SMichael Halcrow return rc; 84237fead6SMichael Halcrow } 85237fead6SMichael Halcrow 86237fead6SMichael Halcrow /** 87e77a56ddSMichael Halcrow * Header Extent: 88e77a56ddSMichael Halcrow * Octets 0-7: Unencrypted file size (big-endian) 89e77a56ddSMichael Halcrow * Octets 8-15: eCryptfs special marker 90e77a56ddSMichael Halcrow * Octets 16-19: Flags 91e77a56ddSMichael Halcrow * Octet 16: File format version number (between 0 and 255) 92e77a56ddSMichael Halcrow * Octets 17-18: Reserved 93e77a56ddSMichael Halcrow * Octet 19: Bit 1 (lsb): Reserved 94e77a56ddSMichael Halcrow * Bit 2: Encrypted? 95e77a56ddSMichael Halcrow * Bits 3-8: Reserved 96e77a56ddSMichael Halcrow * Octets 20-23: Header extent size (big-endian) 97e77a56ddSMichael Halcrow * Octets 24-25: Number of header extents at front of file 98e77a56ddSMichael Halcrow * (big-endian) 99e77a56ddSMichael Halcrow * Octet 26: Begin RFC 2440 authentication token packet set 100e77a56ddSMichael Halcrow */ 101e77a56ddSMichael Halcrow static void set_header_info(char *page_virt, 102e77a56ddSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 103e77a56ddSMichael Halcrow { 104e77a56ddSMichael Halcrow size_t written; 105e77a56ddSMichael Halcrow int save_num_header_extents_at_front = 106e77a56ddSMichael Halcrow crypt_stat->num_header_extents_at_front; 107e77a56ddSMichael Halcrow 108e77a56ddSMichael Halcrow crypt_stat->num_header_extents_at_front = 1; 109e77a56ddSMichael Halcrow ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written); 110e77a56ddSMichael Halcrow crypt_stat->num_header_extents_at_front = 111e77a56ddSMichael Halcrow save_num_header_extents_at_front; 112e77a56ddSMichael Halcrow } 113237fead6SMichael Halcrow 114237fead6SMichael Halcrow /** 115bf12be1cSMichael Halcrow * ecryptfs_copy_up_encrypted_with_header 116bf12be1cSMichael Halcrow * @page: Sort of a ``virtual'' representation of the encrypted lower 117bf12be1cSMichael Halcrow * file. The actual lower file does not have the metadata in 118bf12be1cSMichael Halcrow * the header. This is locked. 119bf12be1cSMichael Halcrow * @crypt_stat: The eCryptfs inode's cryptographic context 120237fead6SMichael Halcrow * 121bf12be1cSMichael Halcrow * The ``view'' is the version of the file that userspace winds up 122bf12be1cSMichael Halcrow * seeing, with the header information inserted. 123237fead6SMichael Halcrow */ 124bf12be1cSMichael Halcrow static int 125bf12be1cSMichael Halcrow ecryptfs_copy_up_encrypted_with_header(struct page *page, 126bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 127237fead6SMichael Halcrow { 128bf12be1cSMichael Halcrow loff_t extent_num_in_page = 0; 129bf12be1cSMichael Halcrow loff_t num_extents_per_page = (PAGE_CACHE_SIZE 130bf12be1cSMichael Halcrow / crypt_stat->extent_size); 131237fead6SMichael Halcrow int rc = 0; 132237fead6SMichael Halcrow 133bf12be1cSMichael Halcrow while (extent_num_in_page < num_extents_per_page) { 134d6a13c17SMichael Halcrow loff_t view_extent_num = ((((loff_t)page->index) 135d6a13c17SMichael Halcrow * num_extents_per_page) 136bf12be1cSMichael Halcrow + extent_num_in_page); 137e77a56ddSMichael Halcrow 138bf12be1cSMichael Halcrow if (view_extent_num < crypt_stat->num_header_extents_at_front) { 139bf12be1cSMichael Halcrow /* This is a header extent */ 140e77a56ddSMichael Halcrow char *page_virt; 141e77a56ddSMichael Halcrow 1429d8b8ce5SMichael Halcrow page_virt = kmap_atomic(page, KM_USER0); 143e77a56ddSMichael Halcrow memset(page_virt, 0, PAGE_CACHE_SIZE); 144bf12be1cSMichael Halcrow /* TODO: Support more than one header extent */ 145bf12be1cSMichael Halcrow if (view_extent_num == 0) { 146e77a56ddSMichael Halcrow rc = ecryptfs_read_xattr_region( 147d7cdc5feSMichael Halcrow page_virt, page->mapping->host); 148e77a56ddSMichael Halcrow set_header_info(page_virt, crypt_stat); 149e77a56ddSMichael Halcrow } 1509d8b8ce5SMichael Halcrow kunmap_atomic(page_virt, KM_USER0); 1510a9ac382SMichael Halcrow flush_dcache_page(page); 152e77a56ddSMichael Halcrow if (rc) { 153bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error reading xattr " 154bf12be1cSMichael Halcrow "region; rc = [%d]\n", __FUNCTION__, rc); 155e77a56ddSMichael Halcrow goto out; 156e77a56ddSMichael Halcrow } 157e77a56ddSMichael Halcrow } else { 158bf12be1cSMichael Halcrow /* This is an encrypted data extent */ 159bf12be1cSMichael Halcrow loff_t lower_offset = 160bf12be1cSMichael Halcrow ((view_extent_num - 161bf12be1cSMichael Halcrow crypt_stat->num_header_extents_at_front) 162bf12be1cSMichael Halcrow * crypt_stat->extent_size); 163bf12be1cSMichael Halcrow 164bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 165bf12be1cSMichael Halcrow page, (lower_offset >> PAGE_CACHE_SHIFT), 166bf12be1cSMichael Halcrow (lower_offset & ~PAGE_CACHE_MASK), 167bf12be1cSMichael Halcrow crypt_stat->extent_size, page->mapping->host); 168e77a56ddSMichael Halcrow if (rc) { 169bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error attempting to read " 170bf12be1cSMichael Halcrow "extent at offset [%lld] in the lower " 171bf12be1cSMichael Halcrow "file; rc = [%d]\n", __FUNCTION__, 172bf12be1cSMichael Halcrow lower_offset, rc); 173e77a56ddSMichael Halcrow goto out; 174e77a56ddSMichael Halcrow } 175e77a56ddSMichael Halcrow } 176bf12be1cSMichael Halcrow extent_num_in_page++; 177bf12be1cSMichael Halcrow } 178bf12be1cSMichael Halcrow out: 179bf12be1cSMichael Halcrow return rc; 180bf12be1cSMichael Halcrow } 181bf12be1cSMichael Halcrow 182bf12be1cSMichael Halcrow /** 183bf12be1cSMichael Halcrow * ecryptfs_readpage 184bf12be1cSMichael Halcrow * @file: An eCryptfs file 185bf12be1cSMichael Halcrow * @page: Page from eCryptfs inode mapping into which to stick the read data 186bf12be1cSMichael Halcrow * 187bf12be1cSMichael Halcrow * Read in a page, decrypting if necessary. 188bf12be1cSMichael Halcrow * 189bf12be1cSMichael Halcrow * Returns zero on success; non-zero on error. 190bf12be1cSMichael Halcrow */ 191bf12be1cSMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page) 192bf12be1cSMichael Halcrow { 193bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 194bf12be1cSMichael Halcrow &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat; 195bf12be1cSMichael Halcrow int rc = 0; 196bf12be1cSMichael Halcrow 197bf12be1cSMichael Halcrow if (!crypt_stat 198bf12be1cSMichael Halcrow || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED) 199bf12be1cSMichael Halcrow || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { 200bf12be1cSMichael Halcrow ecryptfs_printk(KERN_DEBUG, 201bf12be1cSMichael Halcrow "Passing through unencrypted page\n"); 202bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment(page, page->index, 0, 203bf12be1cSMichael Halcrow PAGE_CACHE_SIZE, 204bf12be1cSMichael Halcrow page->mapping->host); 205bf12be1cSMichael Halcrow } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { 206bf12be1cSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 207bf12be1cSMichael Halcrow rc = ecryptfs_copy_up_encrypted_with_header(page, 208bf12be1cSMichael Halcrow crypt_stat); 209bf12be1cSMichael Halcrow if (rc) { 210bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error attempting to copy " 211bf12be1cSMichael Halcrow "the encrypted content from the lower " 212bf12be1cSMichael Halcrow "file whilst inserting the metadata " 213bf12be1cSMichael Halcrow "from the xattr into the header; rc = " 214bf12be1cSMichael Halcrow "[%d]\n", __FUNCTION__, rc); 215bf12be1cSMichael Halcrow goto out; 216bf12be1cSMichael Halcrow } 217bf12be1cSMichael Halcrow 218e77a56ddSMichael Halcrow } else { 219bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 220bf12be1cSMichael Halcrow page, page->index, 0, PAGE_CACHE_SIZE, 221bf12be1cSMichael Halcrow page->mapping->host); 222e77a56ddSMichael Halcrow if (rc) { 223e77a56ddSMichael Halcrow printk(KERN_ERR "Error reading page; rc = " 224e77a56ddSMichael Halcrow "[%d]\n", rc); 225e77a56ddSMichael Halcrow goto out; 226e77a56ddSMichael Halcrow } 227e77a56ddSMichael Halcrow } 228237fead6SMichael Halcrow } else { 2290216f7f7SMichael Halcrow rc = ecryptfs_decrypt_page(page); 230237fead6SMichael Halcrow if (rc) { 231237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error decrypting page; " 232237fead6SMichael Halcrow "rc = [%d]\n", rc); 233237fead6SMichael Halcrow goto out; 234237fead6SMichael Halcrow } 235237fead6SMichael Halcrow } 236237fead6SMichael Halcrow out: 237*16a72c45SMichael Halcrow if (rc) 238*16a72c45SMichael Halcrow ClearPageUptodate(page); 239*16a72c45SMichael Halcrow else 240*16a72c45SMichael Halcrow SetPageUptodate(page); 241237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", 242237fead6SMichael Halcrow page->index); 243237fead6SMichael Halcrow unlock_page(page); 244237fead6SMichael Halcrow return rc; 245237fead6SMichael Halcrow } 246237fead6SMichael Halcrow 247dd2a3b7aSMichael Halcrow /** 248dd2a3b7aSMichael Halcrow * Called with lower inode mutex held. 249dd2a3b7aSMichael Halcrow */ 250237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) 251237fead6SMichael Halcrow { 252237fead6SMichael Halcrow struct inode *inode = page->mapping->host; 253237fead6SMichael Halcrow int end_byte_in_page; 254237fead6SMichael Halcrow 2559d8b8ce5SMichael Halcrow if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index) 2569d8b8ce5SMichael Halcrow goto out; 257237fead6SMichael Halcrow end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; 258237fead6SMichael Halcrow if (to > end_byte_in_page) 259237fead6SMichael Halcrow end_byte_in_page = to; 260c9f2875bSNate Diller zero_user_page(page, end_byte_in_page, 261c9f2875bSNate Diller PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0); 262237fead6SMichael Halcrow out: 2639d8b8ce5SMichael Halcrow return 0; 264237fead6SMichael Halcrow } 265237fead6SMichael Halcrow 26653a2731fSMichael Halcrow static int ecryptfs_prepare_write(struct file *file, struct page *page, 26753a2731fSMichael Halcrow unsigned from, unsigned to) 26853a2731fSMichael Halcrow { 26953a2731fSMichael Halcrow int rc = 0; 27053a2731fSMichael Halcrow 27153a2731fSMichael Halcrow if (from == 0 && to == PAGE_CACHE_SIZE) 27253a2731fSMichael Halcrow goto out; /* If we are writing a full page, it will be 27353a2731fSMichael Halcrow up to date. */ 274*16a72c45SMichael Halcrow if (!PageUptodate(page)) { 275bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment(page, page->index, 0, 276bf12be1cSMichael Halcrow PAGE_CACHE_SIZE, 277bf12be1cSMichael Halcrow page->mapping->host); 278*16a72c45SMichael Halcrow if (rc) { 279*16a72c45SMichael Halcrow printk(KERN_ERR "%s: Error attemping to read lower " 280*16a72c45SMichael Halcrow "page segment; rc = [%d]\n", __FUNCTION__, rc); 281*16a72c45SMichael Halcrow ClearPageUptodate(page); 282*16a72c45SMichael Halcrow goto out; 283*16a72c45SMichael Halcrow } else 284*16a72c45SMichael Halcrow SetPageUptodate(page); 285*16a72c45SMichael Halcrow } 286240e2df5SMichael Halcrow if (page->index != 0) { 287bf12be1cSMichael Halcrow loff_t end_of_prev_pg_pos = 288bf12be1cSMichael Halcrow (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1); 289240e2df5SMichael Halcrow 290240e2df5SMichael Halcrow if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) { 291240e2df5SMichael Halcrow rc = ecryptfs_truncate(file->f_path.dentry, 292240e2df5SMichael Halcrow end_of_prev_pg_pos); 29353a2731fSMichael Halcrow if (rc) { 29453a2731fSMichael Halcrow printk(KERN_ERR "Error on attempt to " 29553a2731fSMichael Halcrow "truncate to (higher) offset [%lld];" 296240e2df5SMichael Halcrow " rc = [%d]\n", end_of_prev_pg_pos, rc); 29753a2731fSMichael Halcrow goto out; 29853a2731fSMichael Halcrow } 29953a2731fSMichael Halcrow } 300d4c5cdb3SMichael Halcrow if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host)) 301d4c5cdb3SMichael Halcrow zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0); 302240e2df5SMichael Halcrow } 30353a2731fSMichael Halcrow out: 30453a2731fSMichael Halcrow return rc; 30553a2731fSMichael Halcrow } 30653a2731fSMichael Halcrow 307237fead6SMichael Halcrow /** 308237fead6SMichael Halcrow * ecryptfs_write_inode_size_to_header 309237fead6SMichael Halcrow * 310237fead6SMichael Halcrow * Writes the lower file size to the first 8 bytes of the header. 311237fead6SMichael Halcrow * 312237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 313237fead6SMichael Halcrow */ 3140216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode) 315237fead6SMichael Halcrow { 316237fead6SMichael Halcrow u64 file_size; 3170216f7f7SMichael Halcrow char *file_size_virt; 3180216f7f7SMichael Halcrow int rc; 319237fead6SMichael Halcrow 3200216f7f7SMichael Halcrow file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL); 3210216f7f7SMichael Halcrow if (!file_size_virt) { 3220216f7f7SMichael Halcrow rc = -ENOMEM; 323237fead6SMichael Halcrow goto out; 324237fead6SMichael Halcrow } 3250216f7f7SMichael Halcrow file_size = (u64)i_size_read(ecryptfs_inode); 326237fead6SMichael Halcrow file_size = cpu_to_be64(file_size); 3270216f7f7SMichael Halcrow memcpy(file_size_virt, &file_size, sizeof(u64)); 3280216f7f7SMichael Halcrow rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0, 3290216f7f7SMichael Halcrow sizeof(u64)); 3300216f7f7SMichael Halcrow kfree(file_size_virt); 3310216f7f7SMichael Halcrow if (rc) 3320216f7f7SMichael Halcrow printk(KERN_ERR "%s: Error writing file size to header; " 3330216f7f7SMichael Halcrow "rc = [%d]\n", __FUNCTION__, rc); 334237fead6SMichael Halcrow out: 335237fead6SMichael Halcrow return rc; 336237fead6SMichael Halcrow } 337237fead6SMichael Halcrow 3380216f7f7SMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache; 3390216f7f7SMichael Halcrow 3400216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode) 341dd2a3b7aSMichael Halcrow { 342dd2a3b7aSMichael Halcrow ssize_t size; 343dd2a3b7aSMichael Halcrow void *xattr_virt; 3440216f7f7SMichael Halcrow struct dentry *lower_dentry = 3450216f7f7SMichael Halcrow ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; 3460216f7f7SMichael Halcrow struct inode *lower_inode = lower_dentry->d_inode; 347dd2a3b7aSMichael Halcrow u64 file_size; 348dd2a3b7aSMichael Halcrow int rc; 349dd2a3b7aSMichael Halcrow 3500216f7f7SMichael Halcrow if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) { 3510216f7f7SMichael Halcrow printk(KERN_WARNING 3520216f7f7SMichael Halcrow "No support for setting xattr in lower filesystem\n"); 3530216f7f7SMichael Halcrow rc = -ENOSYS; 3540216f7f7SMichael Halcrow goto out; 3550216f7f7SMichael Halcrow } 356dd2a3b7aSMichael Halcrow xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); 357dd2a3b7aSMichael Halcrow if (!xattr_virt) { 358dd2a3b7aSMichael Halcrow printk(KERN_ERR "Out of memory whilst attempting to write " 359dd2a3b7aSMichael Halcrow "inode size to xattr\n"); 360dd2a3b7aSMichael Halcrow rc = -ENOMEM; 361dd2a3b7aSMichael Halcrow goto out; 362dd2a3b7aSMichael Halcrow } 3630216f7f7SMichael Halcrow mutex_lock(&lower_inode->i_mutex); 3640216f7f7SMichael Halcrow size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME, 3650216f7f7SMichael Halcrow xattr_virt, PAGE_CACHE_SIZE); 366dd2a3b7aSMichael Halcrow if (size < 0) 367dd2a3b7aSMichael Halcrow size = 8; 3680216f7f7SMichael Halcrow file_size = (u64)i_size_read(ecryptfs_inode); 369dd2a3b7aSMichael Halcrow file_size = cpu_to_be64(file_size); 370dd2a3b7aSMichael Halcrow memcpy(xattr_virt, &file_size, sizeof(u64)); 3710216f7f7SMichael Halcrow rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME, 372dd2a3b7aSMichael Halcrow xattr_virt, size, 0); 3730216f7f7SMichael Halcrow mutex_unlock(&lower_inode->i_mutex); 374dd2a3b7aSMichael Halcrow if (rc) 375dd2a3b7aSMichael Halcrow printk(KERN_ERR "Error whilst attempting to write inode size " 376dd2a3b7aSMichael Halcrow "to lower file xattr; rc = [%d]\n", rc); 377dd2a3b7aSMichael Halcrow kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); 378dd2a3b7aSMichael Halcrow out: 379dd2a3b7aSMichael Halcrow return rc; 380dd2a3b7aSMichael Halcrow } 381dd2a3b7aSMichael Halcrow 3820216f7f7SMichael Halcrow int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode) 383dd2a3b7aSMichael Halcrow { 384dd2a3b7aSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 385dd2a3b7aSMichael Halcrow 3860216f7f7SMichael Halcrow crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 387dd2a3b7aSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 3880216f7f7SMichael Halcrow return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode); 389dd2a3b7aSMichael Halcrow else 3900216f7f7SMichael Halcrow return ecryptfs_write_inode_size_to_header(ecryptfs_inode); 391dd2a3b7aSMichael Halcrow } 392dd2a3b7aSMichael Halcrow 393237fead6SMichael Halcrow /** 394237fead6SMichael Halcrow * ecryptfs_commit_write 395237fead6SMichael Halcrow * @file: The eCryptfs file object 396237fead6SMichael Halcrow * @page: The eCryptfs page 397237fead6SMichael Halcrow * @from: Ignored (we rotate the page IV on each write) 398237fead6SMichael Halcrow * @to: Ignored 399237fead6SMichael Halcrow * 400237fead6SMichael Halcrow * This is where we encrypt the data and pass the encrypted data to 401237fead6SMichael Halcrow * the lower filesystem. In OpenPGP-compatible mode, we operate on 402237fead6SMichael Halcrow * entire underlying packets. 403237fead6SMichael Halcrow */ 404237fead6SMichael Halcrow static int ecryptfs_commit_write(struct file *file, struct page *page, 405237fead6SMichael Halcrow unsigned from, unsigned to) 406237fead6SMichael Halcrow { 407237fead6SMichael Halcrow loff_t pos; 408bf12be1cSMichael Halcrow struct inode *ecryptfs_inode = page->mapping->host; 409bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 410bf12be1cSMichael Halcrow &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat; 411237fead6SMichael Halcrow int rc; 412237fead6SMichael Halcrow 413e2bd99ecSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_NEW_FILE) { 414237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " 415237fead6SMichael Halcrow "crypt_stat at memory location [%p]\n", crypt_stat); 416e2bd99ecSMichael Halcrow crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE); 417237fead6SMichael Halcrow } else 418237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); 419237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" 420237fead6SMichael Halcrow "(page w/ index = [0x%.16x], to = [%d])\n", page->index, 421237fead6SMichael Halcrow to); 422bf12be1cSMichael Halcrow /* Fills in zeros if 'to' goes beyond inode size */ 423237fead6SMichael Halcrow rc = fill_zeros_to_end_of_page(page, to); 424237fead6SMichael Halcrow if (rc) { 425237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to fill " 426237fead6SMichael Halcrow "zeros in page with index = [0x%.16x]\n", 427237fead6SMichael Halcrow page->index); 428237fead6SMichael Halcrow goto out; 429237fead6SMichael Halcrow } 4300216f7f7SMichael Halcrow rc = ecryptfs_encrypt_page(page); 431237fead6SMichael Halcrow if (rc) { 432237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " 433237fead6SMichael Halcrow "index [0x%.16x])\n", page->index); 434237fead6SMichael Halcrow goto out; 435237fead6SMichael Halcrow } 436d6a13c17SMichael Halcrow pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to; 437bf12be1cSMichael Halcrow if (pos > i_size_read(ecryptfs_inode)) { 438bf12be1cSMichael Halcrow i_size_write(ecryptfs_inode, pos); 439237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Expanded file size to " 440bf12be1cSMichael Halcrow "[0x%.16x]\n", i_size_read(ecryptfs_inode)); 441237fead6SMichael Halcrow } 442bf12be1cSMichael Halcrow rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); 443dd2a3b7aSMichael Halcrow if (rc) 444dd2a3b7aSMichael Halcrow printk(KERN_ERR "Error writing inode size to metadata; " 445dd2a3b7aSMichael Halcrow "rc = [%d]\n", rc); 446237fead6SMichael Halcrow out: 447237fead6SMichael Halcrow return rc; 448237fead6SMichael Halcrow } 449237fead6SMichael Halcrow 450237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) 451237fead6SMichael Halcrow { 452237fead6SMichael Halcrow int rc = 0; 453237fead6SMichael Halcrow struct inode *inode; 454237fead6SMichael Halcrow struct inode *lower_inode; 455237fead6SMichael Halcrow 456237fead6SMichael Halcrow inode = (struct inode *)mapping->host; 457237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(inode); 458237fead6SMichael Halcrow if (lower_inode->i_mapping->a_ops->bmap) 459237fead6SMichael Halcrow rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, 460237fead6SMichael Halcrow block); 461237fead6SMichael Halcrow return rc; 462237fead6SMichael Halcrow } 463237fead6SMichael Halcrow 464237fead6SMichael Halcrow struct address_space_operations ecryptfs_aops = { 465237fead6SMichael Halcrow .writepage = ecryptfs_writepage, 466237fead6SMichael Halcrow .readpage = ecryptfs_readpage, 467237fead6SMichael Halcrow .prepare_write = ecryptfs_prepare_write, 468237fead6SMichael Halcrow .commit_write = ecryptfs_commit_write, 469237fead6SMichael Halcrow .bmap = ecryptfs_bmap, 470237fead6SMichael Halcrow }; 471