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> 350a688ad7SHarvey Harrison #include <asm/unaligned.h> 36237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 37237fead6SMichael Halcrow 38237fead6SMichael Halcrow /** 3916a72c45SMichael Halcrow * ecryptfs_get_locked_page 40237fead6SMichael Halcrow * 41237fead6SMichael Halcrow * Get one page from cache or lower f/s, return error otherwise. 42237fead6SMichael Halcrow * 4316a72c45SMichael Halcrow * Returns locked and up-to-date page (if ok), with increased 44237fead6SMichael Halcrow * refcnt. 45237fead6SMichael Halcrow */ 4616a72c45SMichael Halcrow struct page *ecryptfs_get_locked_page(struct file *file, loff_t index) 47237fead6SMichael Halcrow { 48237fead6SMichael Halcrow struct dentry *dentry; 49237fead6SMichael Halcrow struct inode *inode; 50237fead6SMichael Halcrow struct address_space *mapping; 5116a72c45SMichael Halcrow struct page *page; 52237fead6SMichael Halcrow 53bd243a4bSJosef "Jeff" Sipek dentry = file->f_path.dentry; 54237fead6SMichael Halcrow inode = dentry->d_inode; 55237fead6SMichael Halcrow mapping = inode->i_mapping; 5616a72c45SMichael Halcrow page = read_mapping_page(mapping, index, (void *)file); 5716a72c45SMichael Halcrow if (!IS_ERR(page)) 5816a72c45SMichael Halcrow lock_page(page); 5916a72c45SMichael Halcrow return page; 60237fead6SMichael Halcrow } 61237fead6SMichael Halcrow 62237fead6SMichael Halcrow /** 63237fead6SMichael Halcrow * ecryptfs_writepage 64237fead6SMichael Halcrow * @page: Page that is locked before this call is made 65237fead6SMichael Halcrow * 66237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 67237fead6SMichael Halcrow */ 68237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) 69237fead6SMichael Halcrow { 70237fead6SMichael Halcrow int rc; 71237fead6SMichael Halcrow 720216f7f7SMichael Halcrow rc = ecryptfs_encrypt_page(page); 73237fead6SMichael Halcrow if (rc) { 74237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error encrypting " 75237fead6SMichael Halcrow "page (upper index [0x%.16x])\n", page->index); 76237fead6SMichael Halcrow ClearPageUptodate(page); 77237fead6SMichael Halcrow goto out; 78237fead6SMichael Halcrow } 79237fead6SMichael Halcrow SetPageUptodate(page); 80237fead6SMichael Halcrow unlock_page(page); 81237fead6SMichael Halcrow out: 82237fead6SMichael Halcrow return rc; 83237fead6SMichael Halcrow } 84237fead6SMichael Halcrow 85237fead6SMichael Halcrow /** 86e77a56ddSMichael Halcrow * Header Extent: 87e77a56ddSMichael Halcrow * Octets 0-7: Unencrypted file size (big-endian) 88e77a56ddSMichael Halcrow * Octets 8-15: eCryptfs special marker 89e77a56ddSMichael Halcrow * Octets 16-19: Flags 90e77a56ddSMichael Halcrow * Octet 16: File format version number (between 0 and 255) 91e77a56ddSMichael Halcrow * Octets 17-18: Reserved 92e77a56ddSMichael Halcrow * Octet 19: Bit 1 (lsb): Reserved 93e77a56ddSMichael Halcrow * Bit 2: Encrypted? 94e77a56ddSMichael Halcrow * Bits 3-8: Reserved 95e77a56ddSMichael Halcrow * Octets 20-23: Header extent size (big-endian) 96e77a56ddSMichael Halcrow * Octets 24-25: Number of header extents at front of file 97e77a56ddSMichael Halcrow * (big-endian) 98e77a56ddSMichael Halcrow * Octet 26: Begin RFC 2440 authentication token packet set 99e77a56ddSMichael Halcrow */ 100237fead6SMichael Halcrow 101237fead6SMichael Halcrow /** 102bf12be1cSMichael Halcrow * ecryptfs_copy_up_encrypted_with_header 103bf12be1cSMichael Halcrow * @page: Sort of a ``virtual'' representation of the encrypted lower 104bf12be1cSMichael Halcrow * file. The actual lower file does not have the metadata in 105bf12be1cSMichael Halcrow * the header. This is locked. 106bf12be1cSMichael Halcrow * @crypt_stat: The eCryptfs inode's cryptographic context 107237fead6SMichael Halcrow * 108bf12be1cSMichael Halcrow * The ``view'' is the version of the file that userspace winds up 109bf12be1cSMichael Halcrow * seeing, with the header information inserted. 110237fead6SMichael Halcrow */ 111bf12be1cSMichael Halcrow static int 112bf12be1cSMichael Halcrow ecryptfs_copy_up_encrypted_with_header(struct page *page, 113bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 114237fead6SMichael Halcrow { 115bf12be1cSMichael Halcrow loff_t extent_num_in_page = 0; 116bf12be1cSMichael Halcrow loff_t num_extents_per_page = (PAGE_CACHE_SIZE 117bf12be1cSMichael Halcrow / crypt_stat->extent_size); 118237fead6SMichael Halcrow int rc = 0; 119237fead6SMichael Halcrow 120bf12be1cSMichael Halcrow while (extent_num_in_page < num_extents_per_page) { 121d6a13c17SMichael Halcrow loff_t view_extent_num = ((((loff_t)page->index) 122d6a13c17SMichael Halcrow * num_extents_per_page) 123bf12be1cSMichael Halcrow + extent_num_in_page); 124cc11beffSMichael Halcrow size_t num_header_extents_at_front = 125*fa3ef1cbSTyler Hicks (crypt_stat->metadata_size / crypt_stat->extent_size); 126e77a56ddSMichael Halcrow 127cc11beffSMichael Halcrow if (view_extent_num < num_header_extents_at_front) { 128bf12be1cSMichael Halcrow /* This is a header extent */ 129e77a56ddSMichael Halcrow char *page_virt; 130e77a56ddSMichael Halcrow 1319d8b8ce5SMichael Halcrow page_virt = kmap_atomic(page, KM_USER0); 132e77a56ddSMichael Halcrow memset(page_virt, 0, PAGE_CACHE_SIZE); 133bf12be1cSMichael Halcrow /* TODO: Support more than one header extent */ 134bf12be1cSMichael Halcrow if (view_extent_num == 0) { 135157f1071STyler Hicks size_t written; 136157f1071STyler Hicks 137e77a56ddSMichael Halcrow rc = ecryptfs_read_xattr_region( 138d7cdc5feSMichael Halcrow page_virt, page->mapping->host); 139157f1071STyler Hicks ecryptfs_write_header_metadata(page_virt + 20, 140157f1071STyler Hicks crypt_stat, 141157f1071STyler Hicks &written); 142e77a56ddSMichael Halcrow } 1439d8b8ce5SMichael Halcrow kunmap_atomic(page_virt, KM_USER0); 1440a9ac382SMichael Halcrow flush_dcache_page(page); 145e77a56ddSMichael Halcrow if (rc) { 146bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error reading xattr " 14718d1dbf1SHarvey Harrison "region; rc = [%d]\n", __func__, rc); 148e77a56ddSMichael Halcrow goto out; 149e77a56ddSMichael Halcrow } 150e77a56ddSMichael Halcrow } else { 151bf12be1cSMichael Halcrow /* This is an encrypted data extent */ 152bf12be1cSMichael Halcrow loff_t lower_offset = 153cc11beffSMichael Halcrow ((view_extent_num * crypt_stat->extent_size) 154*fa3ef1cbSTyler Hicks - crypt_stat->metadata_size); 155bf12be1cSMichael Halcrow 156bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 157bf12be1cSMichael Halcrow page, (lower_offset >> PAGE_CACHE_SHIFT), 158bf12be1cSMichael Halcrow (lower_offset & ~PAGE_CACHE_MASK), 159bf12be1cSMichael Halcrow crypt_stat->extent_size, page->mapping->host); 160e77a56ddSMichael Halcrow if (rc) { 161bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error attempting to read " 162bf12be1cSMichael Halcrow "extent at offset [%lld] in the lower " 16318d1dbf1SHarvey Harrison "file; rc = [%d]\n", __func__, 164bf12be1cSMichael Halcrow lower_offset, rc); 165e77a56ddSMichael Halcrow goto out; 166e77a56ddSMichael Halcrow } 167e77a56ddSMichael Halcrow } 168bf12be1cSMichael Halcrow extent_num_in_page++; 169bf12be1cSMichael Halcrow } 170bf12be1cSMichael Halcrow out: 171bf12be1cSMichael Halcrow return rc; 172bf12be1cSMichael Halcrow } 173bf12be1cSMichael Halcrow 174bf12be1cSMichael Halcrow /** 175bf12be1cSMichael Halcrow * ecryptfs_readpage 176bf12be1cSMichael Halcrow * @file: An eCryptfs file 177bf12be1cSMichael Halcrow * @page: Page from eCryptfs inode mapping into which to stick the read data 178bf12be1cSMichael Halcrow * 179bf12be1cSMichael Halcrow * Read in a page, decrypting if necessary. 180bf12be1cSMichael Halcrow * 181bf12be1cSMichael Halcrow * Returns zero on success; non-zero on error. 182bf12be1cSMichael Halcrow */ 183bf12be1cSMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page) 184bf12be1cSMichael Halcrow { 185bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 186bf12be1cSMichael Halcrow &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat; 187bf12be1cSMichael Halcrow int rc = 0; 188bf12be1cSMichael Halcrow 189bf12be1cSMichael Halcrow if (!crypt_stat 190bf12be1cSMichael Halcrow || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED) 191bf12be1cSMichael Halcrow || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { 192bf12be1cSMichael Halcrow ecryptfs_printk(KERN_DEBUG, 193bf12be1cSMichael Halcrow "Passing through unencrypted page\n"); 194bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment(page, page->index, 0, 195bf12be1cSMichael Halcrow PAGE_CACHE_SIZE, 196bf12be1cSMichael Halcrow page->mapping->host); 197bf12be1cSMichael Halcrow } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { 198bf12be1cSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 199bf12be1cSMichael Halcrow rc = ecryptfs_copy_up_encrypted_with_header(page, 200bf12be1cSMichael Halcrow crypt_stat); 201bf12be1cSMichael Halcrow if (rc) { 202bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error attempting to copy " 203bf12be1cSMichael Halcrow "the encrypted content from the lower " 204bf12be1cSMichael Halcrow "file whilst inserting the metadata " 205bf12be1cSMichael Halcrow "from the xattr into the header; rc = " 20618d1dbf1SHarvey Harrison "[%d]\n", __func__, rc); 207bf12be1cSMichael Halcrow goto out; 208bf12be1cSMichael Halcrow } 209bf12be1cSMichael Halcrow 210e77a56ddSMichael Halcrow } else { 211bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 212bf12be1cSMichael Halcrow page, page->index, 0, PAGE_CACHE_SIZE, 213bf12be1cSMichael Halcrow page->mapping->host); 214e77a56ddSMichael Halcrow if (rc) { 215e77a56ddSMichael Halcrow printk(KERN_ERR "Error reading page; rc = " 216e77a56ddSMichael Halcrow "[%d]\n", rc); 217e77a56ddSMichael Halcrow goto out; 218e77a56ddSMichael Halcrow } 219e77a56ddSMichael Halcrow } 220237fead6SMichael Halcrow } else { 2210216f7f7SMichael Halcrow rc = ecryptfs_decrypt_page(page); 222237fead6SMichael Halcrow if (rc) { 223237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error decrypting page; " 224237fead6SMichael Halcrow "rc = [%d]\n", rc); 225237fead6SMichael Halcrow goto out; 226237fead6SMichael Halcrow } 227237fead6SMichael Halcrow } 228237fead6SMichael Halcrow out: 22916a72c45SMichael Halcrow if (rc) 23016a72c45SMichael Halcrow ClearPageUptodate(page); 23116a72c45SMichael Halcrow else 23216a72c45SMichael Halcrow SetPageUptodate(page); 233237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", 234237fead6SMichael Halcrow page->index); 235237fead6SMichael Halcrow unlock_page(page); 236237fead6SMichael Halcrow return rc; 237237fead6SMichael Halcrow } 238237fead6SMichael Halcrow 239dd2a3b7aSMichael Halcrow /** 240dd2a3b7aSMichael Halcrow * Called with lower inode mutex held. 241dd2a3b7aSMichael Halcrow */ 242237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) 243237fead6SMichael Halcrow { 244237fead6SMichael Halcrow struct inode *inode = page->mapping->host; 245237fead6SMichael Halcrow int end_byte_in_page; 246237fead6SMichael Halcrow 2479d8b8ce5SMichael Halcrow if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index) 2489d8b8ce5SMichael Halcrow goto out; 249237fead6SMichael Halcrow end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; 250237fead6SMichael Halcrow if (to > end_byte_in_page) 251237fead6SMichael Halcrow end_byte_in_page = to; 252eebd2aa3SChristoph Lameter zero_user_segment(page, end_byte_in_page, PAGE_CACHE_SIZE); 253237fead6SMichael Halcrow out: 2549d8b8ce5SMichael Halcrow return 0; 255237fead6SMichael Halcrow } 256237fead6SMichael Halcrow 257e4465fdaSMichael Halcrow /** 258807b7ebeSBadari Pulavarty * ecryptfs_write_begin 259e4465fdaSMichael Halcrow * @file: The eCryptfs file 260807b7ebeSBadari Pulavarty * @mapping: The eCryptfs object 261807b7ebeSBadari Pulavarty * @pos: The file offset at which to start writing 262807b7ebeSBadari Pulavarty * @len: Length of the write 263807b7ebeSBadari Pulavarty * @flags: Various flags 264807b7ebeSBadari Pulavarty * @pagep: Pointer to return the page 265807b7ebeSBadari Pulavarty * @fsdata: Pointer to return fs data (unused) 266e4465fdaSMichael Halcrow * 267e4465fdaSMichael Halcrow * This function must zero any hole we create 268e4465fdaSMichael Halcrow * 269e4465fdaSMichael Halcrow * Returns zero on success; non-zero otherwise 270e4465fdaSMichael Halcrow */ 271807b7ebeSBadari Pulavarty static int ecryptfs_write_begin(struct file *file, 272807b7ebeSBadari Pulavarty struct address_space *mapping, 273807b7ebeSBadari Pulavarty loff_t pos, unsigned len, unsigned flags, 274807b7ebeSBadari Pulavarty struct page **pagep, void **fsdata) 27553a2731fSMichael Halcrow { 276807b7ebeSBadari Pulavarty pgoff_t index = pos >> PAGE_CACHE_SHIFT; 277807b7ebeSBadari Pulavarty struct page *page; 2787a3f595cSEric Sandeen loff_t prev_page_end_size; 279e4465fdaSMichael Halcrow int rc = 0; 28053a2731fSMichael Halcrow 28154566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 282807b7ebeSBadari Pulavarty if (!page) 283807b7ebeSBadari Pulavarty return -ENOMEM; 284807b7ebeSBadari Pulavarty *pagep = page; 285807b7ebeSBadari Pulavarty 28616a72c45SMichael Halcrow if (!PageUptodate(page)) { 287e4465fdaSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 288e4465fdaSMichael Halcrow &ecryptfs_inode_to_private( 289e4465fdaSMichael Halcrow file->f_path.dentry->d_inode)->crypt_stat; 290e4465fdaSMichael Halcrow 291e4465fdaSMichael Halcrow if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED) 292e4465fdaSMichael Halcrow || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) { 293e4465fdaSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 294807b7ebeSBadari Pulavarty page, index, 0, PAGE_CACHE_SIZE, mapping->host); 29516a72c45SMichael Halcrow if (rc) { 296e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error attemping to read " 297e4465fdaSMichael Halcrow "lower page segment; rc = [%d]\n", 29818d1dbf1SHarvey Harrison __func__, rc); 29916a72c45SMichael Halcrow ClearPageUptodate(page); 30016a72c45SMichael Halcrow goto out; 30116a72c45SMichael Halcrow } else 30216a72c45SMichael Halcrow SetPageUptodate(page); 303e4465fdaSMichael Halcrow } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { 304e4465fdaSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 305e4465fdaSMichael Halcrow rc = ecryptfs_copy_up_encrypted_with_header( 306e4465fdaSMichael Halcrow page, crypt_stat); 307e4465fdaSMichael Halcrow if (rc) { 308e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error attempting " 309e4465fdaSMichael Halcrow "to copy the encrypted content " 310e4465fdaSMichael Halcrow "from the lower file whilst " 311e4465fdaSMichael Halcrow "inserting the metadata from " 312e4465fdaSMichael Halcrow "the xattr into the header; rc " 31318d1dbf1SHarvey Harrison "= [%d]\n", __func__, rc); 314e4465fdaSMichael Halcrow ClearPageUptodate(page); 315e4465fdaSMichael Halcrow goto out; 31616a72c45SMichael Halcrow } 317e4465fdaSMichael Halcrow SetPageUptodate(page); 318e4465fdaSMichael Halcrow } else { 319e4465fdaSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 320807b7ebeSBadari Pulavarty page, index, 0, PAGE_CACHE_SIZE, 321807b7ebeSBadari Pulavarty mapping->host); 322e4465fdaSMichael Halcrow if (rc) { 323e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error reading " 324e4465fdaSMichael Halcrow "page; rc = [%d]\n", 32518d1dbf1SHarvey Harrison __func__, rc); 326e4465fdaSMichael Halcrow ClearPageUptodate(page); 327e4465fdaSMichael Halcrow goto out; 328e4465fdaSMichael Halcrow } 329e4465fdaSMichael Halcrow SetPageUptodate(page); 330e4465fdaSMichael Halcrow } 331e4465fdaSMichael Halcrow } else { 332e4465fdaSMichael Halcrow rc = ecryptfs_decrypt_page(page); 333e4465fdaSMichael Halcrow if (rc) { 334e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error decrypting page " 335e4465fdaSMichael Halcrow "at index [%ld]; rc = [%d]\n", 33618d1dbf1SHarvey Harrison __func__, page->index, rc); 337e4465fdaSMichael Halcrow ClearPageUptodate(page); 338e4465fdaSMichael Halcrow goto out; 339e4465fdaSMichael Halcrow } 340e4465fdaSMichael Halcrow SetPageUptodate(page); 341e4465fdaSMichael Halcrow } 342e4465fdaSMichael Halcrow } 343807b7ebeSBadari Pulavarty prev_page_end_size = ((loff_t)index << PAGE_CACHE_SHIFT); 344e4465fdaSMichael Halcrow /* If creating a page or more of holes, zero them out via truncate. 345e4465fdaSMichael Halcrow * Note, this will increase i_size. */ 346807b7ebeSBadari Pulavarty if (index != 0) { 3477a3f595cSEric Sandeen if (prev_page_end_size > i_size_read(page->mapping->host)) { 348240e2df5SMichael Halcrow rc = ecryptfs_truncate(file->f_path.dentry, 3497a3f595cSEric Sandeen prev_page_end_size); 35053a2731fSMichael Halcrow if (rc) { 351e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error on attempt to " 35253a2731fSMichael Halcrow "truncate to (higher) offset [%lld];" 35318d1dbf1SHarvey Harrison " rc = [%d]\n", __func__, 354e4465fdaSMichael Halcrow prev_page_end_size, rc); 35553a2731fSMichael Halcrow goto out; 35653a2731fSMichael Halcrow } 35753a2731fSMichael Halcrow } 3587a3f595cSEric Sandeen } 359e4465fdaSMichael Halcrow /* Writing to a new page, and creating a small hole from start 360e4465fdaSMichael Halcrow * of page? Zero it out. */ 361807b7ebeSBadari Pulavarty if ((i_size_read(mapping->host) == prev_page_end_size) 362807b7ebeSBadari Pulavarty && (pos != 0)) 363eebd2aa3SChristoph Lameter zero_user(page, 0, PAGE_CACHE_SIZE); 36453a2731fSMichael Halcrow out: 36553a2731fSMichael Halcrow return rc; 36653a2731fSMichael Halcrow } 36753a2731fSMichael Halcrow 368237fead6SMichael Halcrow /** 369237fead6SMichael Halcrow * ecryptfs_write_inode_size_to_header 370237fead6SMichael Halcrow * 371237fead6SMichael Halcrow * Writes the lower file size to the first 8 bytes of the header. 372237fead6SMichael Halcrow * 373237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 374237fead6SMichael Halcrow */ 3750216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode) 376237fead6SMichael Halcrow { 3770216f7f7SMichael Halcrow char *file_size_virt; 3780216f7f7SMichael Halcrow int rc; 379237fead6SMichael Halcrow 3800216f7f7SMichael Halcrow file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL); 3810216f7f7SMichael Halcrow if (!file_size_virt) { 3820216f7f7SMichael Halcrow rc = -ENOMEM; 383237fead6SMichael Halcrow goto out; 384237fead6SMichael Halcrow } 3850a688ad7SHarvey Harrison put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt); 3860216f7f7SMichael Halcrow rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0, 3870216f7f7SMichael Halcrow sizeof(u64)); 3880216f7f7SMichael Halcrow kfree(file_size_virt); 38996a7b9c2STyler Hicks if (rc < 0) 3900216f7f7SMichael Halcrow printk(KERN_ERR "%s: Error writing file size to header; " 39118d1dbf1SHarvey Harrison "rc = [%d]\n", __func__, rc); 39296a7b9c2STyler Hicks else 39396a7b9c2STyler Hicks rc = 0; 394237fead6SMichael Halcrow out: 395237fead6SMichael Halcrow return rc; 396237fead6SMichael Halcrow } 397237fead6SMichael Halcrow 3980216f7f7SMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache; 3990216f7f7SMichael Halcrow 4000216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode) 401dd2a3b7aSMichael Halcrow { 402dd2a3b7aSMichael Halcrow ssize_t size; 403dd2a3b7aSMichael Halcrow void *xattr_virt; 4040216f7f7SMichael Halcrow struct dentry *lower_dentry = 4050216f7f7SMichael Halcrow ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry; 4060216f7f7SMichael Halcrow struct inode *lower_inode = lower_dentry->d_inode; 407dd2a3b7aSMichael Halcrow int rc; 408dd2a3b7aSMichael Halcrow 4090216f7f7SMichael Halcrow if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) { 4100216f7f7SMichael Halcrow printk(KERN_WARNING 4110216f7f7SMichael Halcrow "No support for setting xattr in lower filesystem\n"); 4120216f7f7SMichael Halcrow rc = -ENOSYS; 4130216f7f7SMichael Halcrow goto out; 4140216f7f7SMichael Halcrow } 415dd2a3b7aSMichael Halcrow xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); 416dd2a3b7aSMichael Halcrow if (!xattr_virt) { 417dd2a3b7aSMichael Halcrow printk(KERN_ERR "Out of memory whilst attempting to write " 418dd2a3b7aSMichael Halcrow "inode size to xattr\n"); 419dd2a3b7aSMichael Halcrow rc = -ENOMEM; 420dd2a3b7aSMichael Halcrow goto out; 421dd2a3b7aSMichael Halcrow } 4220216f7f7SMichael Halcrow mutex_lock(&lower_inode->i_mutex); 4230216f7f7SMichael Halcrow size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME, 4240216f7f7SMichael Halcrow xattr_virt, PAGE_CACHE_SIZE); 425dd2a3b7aSMichael Halcrow if (size < 0) 426dd2a3b7aSMichael Halcrow size = 8; 4270a688ad7SHarvey Harrison put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt); 4280216f7f7SMichael Halcrow rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME, 429dd2a3b7aSMichael Halcrow xattr_virt, size, 0); 4300216f7f7SMichael Halcrow mutex_unlock(&lower_inode->i_mutex); 431dd2a3b7aSMichael Halcrow if (rc) 432dd2a3b7aSMichael Halcrow printk(KERN_ERR "Error whilst attempting to write inode size " 433dd2a3b7aSMichael Halcrow "to lower file xattr; rc = [%d]\n", rc); 434dd2a3b7aSMichael Halcrow kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); 435dd2a3b7aSMichael Halcrow out: 436dd2a3b7aSMichael Halcrow return rc; 437dd2a3b7aSMichael Halcrow } 438dd2a3b7aSMichael Halcrow 4390216f7f7SMichael Halcrow int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode) 440dd2a3b7aSMichael Halcrow { 441dd2a3b7aSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 442dd2a3b7aSMichael Halcrow 4430216f7f7SMichael Halcrow crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 44413a791b4STyler Hicks BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); 445dd2a3b7aSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 4460216f7f7SMichael Halcrow return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode); 447dd2a3b7aSMichael Halcrow else 4480216f7f7SMichael Halcrow return ecryptfs_write_inode_size_to_header(ecryptfs_inode); 449dd2a3b7aSMichael Halcrow } 450dd2a3b7aSMichael Halcrow 451237fead6SMichael Halcrow /** 452807b7ebeSBadari Pulavarty * ecryptfs_write_end 453237fead6SMichael Halcrow * @file: The eCryptfs file object 454807b7ebeSBadari Pulavarty * @mapping: The eCryptfs object 455807b7ebeSBadari Pulavarty * @pos: The file position 456807b7ebeSBadari Pulavarty * @len: The length of the data (unused) 457807b7ebeSBadari Pulavarty * @copied: The amount of data copied 458237fead6SMichael Halcrow * @page: The eCryptfs page 459807b7ebeSBadari Pulavarty * @fsdata: The fsdata (unused) 460237fead6SMichael Halcrow * 461237fead6SMichael Halcrow * This is where we encrypt the data and pass the encrypted data to 462237fead6SMichael Halcrow * the lower filesystem. In OpenPGP-compatible mode, we operate on 463237fead6SMichael Halcrow * entire underlying packets. 464237fead6SMichael Halcrow */ 465807b7ebeSBadari Pulavarty static int ecryptfs_write_end(struct file *file, 466807b7ebeSBadari Pulavarty struct address_space *mapping, 467807b7ebeSBadari Pulavarty loff_t pos, unsigned len, unsigned copied, 468807b7ebeSBadari Pulavarty struct page *page, void *fsdata) 469237fead6SMichael Halcrow { 470807b7ebeSBadari Pulavarty pgoff_t index = pos >> PAGE_CACHE_SHIFT; 471807b7ebeSBadari Pulavarty unsigned from = pos & (PAGE_CACHE_SIZE - 1); 472807b7ebeSBadari Pulavarty unsigned to = from + copied; 473807b7ebeSBadari Pulavarty struct inode *ecryptfs_inode = mapping->host; 474bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 475bf12be1cSMichael Halcrow &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat; 476237fead6SMichael Halcrow int rc; 477237fead6SMichael Halcrow 478e2bd99ecSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_NEW_FILE) { 479237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " 480237fead6SMichael Halcrow "crypt_stat at memory location [%p]\n", crypt_stat); 481e2bd99ecSMichael Halcrow crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE); 482237fead6SMichael Halcrow } else 483237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); 484237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" 485807b7ebeSBadari Pulavarty "(page w/ index = [0x%.16x], to = [%d])\n", index, to); 48613a791b4STyler Hicks if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 48713a791b4STyler Hicks rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0, 48813a791b4STyler Hicks to); 48913a791b4STyler Hicks if (!rc) { 49013a791b4STyler Hicks rc = copied; 49113a791b4STyler Hicks fsstack_copy_inode_size(ecryptfs_inode, 49213a791b4STyler Hicks ecryptfs_inode_to_lower(ecryptfs_inode)); 49313a791b4STyler Hicks } 49413a791b4STyler Hicks goto out; 49513a791b4STyler Hicks } 496bf12be1cSMichael Halcrow /* Fills in zeros if 'to' goes beyond inode size */ 497237fead6SMichael Halcrow rc = fill_zeros_to_end_of_page(page, to); 498237fead6SMichael Halcrow if (rc) { 499237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to fill " 500807b7ebeSBadari Pulavarty "zeros in page with index = [0x%.16x]\n", index); 501237fead6SMichael Halcrow goto out; 502237fead6SMichael Halcrow } 5030216f7f7SMichael Halcrow rc = ecryptfs_encrypt_page(page); 504237fead6SMichael Halcrow if (rc) { 505237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " 506807b7ebeSBadari Pulavarty "index [0x%.16x])\n", index); 507237fead6SMichael Halcrow goto out; 508237fead6SMichael Halcrow } 509807b7ebeSBadari Pulavarty if (pos + copied > i_size_read(ecryptfs_inode)) { 510807b7ebeSBadari Pulavarty i_size_write(ecryptfs_inode, pos + copied); 511237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Expanded file size to " 512bf12be1cSMichael Halcrow "[0x%.16x]\n", i_size_read(ecryptfs_inode)); 513237fead6SMichael Halcrow } 514bf12be1cSMichael Halcrow rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); 515dd2a3b7aSMichael Halcrow if (rc) 516dd2a3b7aSMichael Halcrow printk(KERN_ERR "Error writing inode size to metadata; " 517dd2a3b7aSMichael Halcrow "rc = [%d]\n", rc); 518807b7ebeSBadari Pulavarty else 519807b7ebeSBadari Pulavarty rc = copied; 520237fead6SMichael Halcrow out: 521807b7ebeSBadari Pulavarty unlock_page(page); 522807b7ebeSBadari Pulavarty page_cache_release(page); 523237fead6SMichael Halcrow return rc; 524237fead6SMichael Halcrow } 525237fead6SMichael Halcrow 526237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) 527237fead6SMichael Halcrow { 528237fead6SMichael Halcrow int rc = 0; 529237fead6SMichael Halcrow struct inode *inode; 530237fead6SMichael Halcrow struct inode *lower_inode; 531237fead6SMichael Halcrow 532237fead6SMichael Halcrow inode = (struct inode *)mapping->host; 533237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(inode); 534237fead6SMichael Halcrow if (lower_inode->i_mapping->a_ops->bmap) 535237fead6SMichael Halcrow rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, 536237fead6SMichael Halcrow block); 537237fead6SMichael Halcrow return rc; 538237fead6SMichael Halcrow } 539237fead6SMichael Halcrow 5407f09410bSAlexey Dobriyan const struct address_space_operations ecryptfs_aops = { 541237fead6SMichael Halcrow .writepage = ecryptfs_writepage, 542237fead6SMichael Halcrow .readpage = ecryptfs_readpage, 543807b7ebeSBadari Pulavarty .write_begin = ecryptfs_write_begin, 544807b7ebeSBadari Pulavarty .write_end = ecryptfs_write_end, 545237fead6SMichael Halcrow .bmap = ecryptfs_bmap, 546237fead6SMichael Halcrow }; 547