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/scatterlist.h> 345a0e3ad6STejun Heo #include <linux/slab.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 */ 4602bd9799SAl Viro struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index) 47237fead6SMichael Halcrow { 4802bd9799SAl Viro struct page *page = read_mapping_page(inode->i_mapping, index, NULL); 4916a72c45SMichael Halcrow if (!IS_ERR(page)) 5016a72c45SMichael Halcrow lock_page(page); 5116a72c45SMichael Halcrow return page; 52237fead6SMichael Halcrow } 53237fead6SMichael Halcrow 54237fead6SMichael Halcrow /** 55237fead6SMichael Halcrow * ecryptfs_writepage 56237fead6SMichael Halcrow * @page: Page that is locked before this call is made 57237fead6SMichael Halcrow * 58237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 591589cb1aSLi Wang * 601589cb1aSLi Wang * This is where we encrypt the data and pass the encrypted data to 611589cb1aSLi Wang * the lower filesystem. In OpenPGP-compatible mode, we operate on 621589cb1aSLi Wang * entire underlying packets. 63237fead6SMichael Halcrow */ 64237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) 65237fead6SMichael Halcrow { 66237fead6SMichael Halcrow int rc; 67237fead6SMichael Halcrow 680216f7f7SMichael Halcrow rc = ecryptfs_encrypt_page(page); 69237fead6SMichael Halcrow if (rc) { 70237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error encrypting " 71888d57bbSJoe Perches "page (upper index [0x%.16lx])\n", page->index); 72237fead6SMichael Halcrow ClearPageUptodate(page); 73237fead6SMichael Halcrow goto out; 74237fead6SMichael Halcrow } 75237fead6SMichael Halcrow SetPageUptodate(page); 76237fead6SMichael Halcrow out: 7757db4e8dSThieu Le unlock_page(page); 78237fead6SMichael Halcrow return rc; 79237fead6SMichael Halcrow } 80237fead6SMichael Halcrow 81f4e60e6bSTyler Hicks static void strip_xattr_flag(char *page_virt, 82f4e60e6bSTyler Hicks struct ecryptfs_crypt_stat *crypt_stat) 83f4e60e6bSTyler Hicks { 84f4e60e6bSTyler Hicks if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 85f4e60e6bSTyler Hicks size_t written; 86f4e60e6bSTyler Hicks 87f4e60e6bSTyler Hicks crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR; 88f4e60e6bSTyler Hicks ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat, 89f4e60e6bSTyler Hicks &written); 90f4e60e6bSTyler Hicks crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; 91f4e60e6bSTyler Hicks } 92f4e60e6bSTyler Hicks } 93f4e60e6bSTyler Hicks 94237fead6SMichael Halcrow /** 95e77a56ddSMichael Halcrow * Header Extent: 96e77a56ddSMichael Halcrow * Octets 0-7: Unencrypted file size (big-endian) 97e77a56ddSMichael Halcrow * Octets 8-15: eCryptfs special marker 98e77a56ddSMichael Halcrow * Octets 16-19: Flags 99e77a56ddSMichael Halcrow * Octet 16: File format version number (between 0 and 255) 100e77a56ddSMichael Halcrow * Octets 17-18: Reserved 101e77a56ddSMichael Halcrow * Octet 19: Bit 1 (lsb): Reserved 102e77a56ddSMichael Halcrow * Bit 2: Encrypted? 103e77a56ddSMichael Halcrow * Bits 3-8: Reserved 104e77a56ddSMichael Halcrow * Octets 20-23: Header extent size (big-endian) 105e77a56ddSMichael Halcrow * Octets 24-25: Number of header extents at front of file 106e77a56ddSMichael Halcrow * (big-endian) 107e77a56ddSMichael Halcrow * Octet 26: Begin RFC 2440 authentication token packet set 108e77a56ddSMichael Halcrow */ 109237fead6SMichael Halcrow 110237fead6SMichael Halcrow /** 111bf12be1cSMichael Halcrow * ecryptfs_copy_up_encrypted_with_header 112bf12be1cSMichael Halcrow * @page: Sort of a ``virtual'' representation of the encrypted lower 113bf12be1cSMichael Halcrow * file. The actual lower file does not have the metadata in 114bf12be1cSMichael Halcrow * the header. This is locked. 115bf12be1cSMichael Halcrow * @crypt_stat: The eCryptfs inode's cryptographic context 116237fead6SMichael Halcrow * 117bf12be1cSMichael Halcrow * The ``view'' is the version of the file that userspace winds up 118bf12be1cSMichael Halcrow * seeing, with the header information inserted. 119237fead6SMichael Halcrow */ 120bf12be1cSMichael Halcrow static int 121bf12be1cSMichael Halcrow ecryptfs_copy_up_encrypted_with_header(struct page *page, 122bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 123237fead6SMichael Halcrow { 124bf12be1cSMichael Halcrow loff_t extent_num_in_page = 0; 125*09cbfeafSKirill A. Shutemov loff_t num_extents_per_page = (PAGE_SIZE 126bf12be1cSMichael Halcrow / crypt_stat->extent_size); 127237fead6SMichael Halcrow int rc = 0; 128237fead6SMichael Halcrow 129bf12be1cSMichael Halcrow while (extent_num_in_page < num_extents_per_page) { 130d6a13c17SMichael Halcrow loff_t view_extent_num = ((((loff_t)page->index) 131d6a13c17SMichael Halcrow * num_extents_per_page) 132bf12be1cSMichael Halcrow + extent_num_in_page); 133cc11beffSMichael Halcrow size_t num_header_extents_at_front = 134fa3ef1cbSTyler Hicks (crypt_stat->metadata_size / crypt_stat->extent_size); 135e77a56ddSMichael Halcrow 136cc11beffSMichael Halcrow if (view_extent_num < num_header_extents_at_front) { 137bf12be1cSMichael Halcrow /* This is a header extent */ 138e77a56ddSMichael Halcrow char *page_virt; 139e77a56ddSMichael Halcrow 140465c9343SCong Wang page_virt = kmap_atomic(page); 141*09cbfeafSKirill A. Shutemov memset(page_virt, 0, PAGE_SIZE); 142bf12be1cSMichael Halcrow /* TODO: Support more than one header extent */ 143bf12be1cSMichael Halcrow if (view_extent_num == 0) { 144157f1071STyler Hicks size_t written; 145157f1071STyler Hicks 146e77a56ddSMichael Halcrow rc = ecryptfs_read_xattr_region( 147d7cdc5feSMichael Halcrow page_virt, page->mapping->host); 148f4e60e6bSTyler Hicks strip_xattr_flag(page_virt + 16, crypt_stat); 149157f1071STyler Hicks ecryptfs_write_header_metadata(page_virt + 20, 150157f1071STyler Hicks crypt_stat, 151157f1071STyler Hicks &written); 152e77a56ddSMichael Halcrow } 153465c9343SCong Wang kunmap_atomic(page_virt); 1540a9ac382SMichael Halcrow flush_dcache_page(page); 155e77a56ddSMichael Halcrow if (rc) { 156bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error reading xattr " 15718d1dbf1SHarvey Harrison "region; rc = [%d]\n", __func__, rc); 158e77a56ddSMichael Halcrow goto out; 159e77a56ddSMichael Halcrow } 160e77a56ddSMichael Halcrow } else { 161bf12be1cSMichael Halcrow /* This is an encrypted data extent */ 162bf12be1cSMichael Halcrow loff_t lower_offset = 163cc11beffSMichael Halcrow ((view_extent_num * crypt_stat->extent_size) 164fa3ef1cbSTyler Hicks - crypt_stat->metadata_size); 165bf12be1cSMichael Halcrow 166bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 167*09cbfeafSKirill A. Shutemov page, (lower_offset >> PAGE_SHIFT), 168*09cbfeafSKirill A. Shutemov (lower_offset & ~PAGE_MASK), 169bf12be1cSMichael Halcrow crypt_stat->extent_size, page->mapping->host); 170e77a56ddSMichael Halcrow if (rc) { 171bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error attempting to read " 172bf12be1cSMichael Halcrow "extent at offset [%lld] in the lower " 17318d1dbf1SHarvey Harrison "file; rc = [%d]\n", __func__, 174bf12be1cSMichael Halcrow lower_offset, rc); 175e77a56ddSMichael Halcrow goto out; 176e77a56ddSMichael Halcrow } 177e77a56ddSMichael Halcrow } 178bf12be1cSMichael Halcrow extent_num_in_page++; 179bf12be1cSMichael Halcrow } 180bf12be1cSMichael Halcrow out: 181bf12be1cSMichael Halcrow return rc; 182bf12be1cSMichael Halcrow } 183bf12be1cSMichael Halcrow 184bf12be1cSMichael Halcrow /** 185bf12be1cSMichael Halcrow * ecryptfs_readpage 186bf12be1cSMichael Halcrow * @file: An eCryptfs file 187bf12be1cSMichael Halcrow * @page: Page from eCryptfs inode mapping into which to stick the read data 188bf12be1cSMichael Halcrow * 189bf12be1cSMichael Halcrow * Read in a page, decrypting if necessary. 190bf12be1cSMichael Halcrow * 191bf12be1cSMichael Halcrow * Returns zero on success; non-zero on error. 192bf12be1cSMichael Halcrow */ 193bf12be1cSMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page) 194bf12be1cSMichael Halcrow { 195bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 196bef5bc24SAl Viro &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat; 197bf12be1cSMichael Halcrow int rc = 0; 198bf12be1cSMichael Halcrow 199fed8859bSTyler Hicks if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 200bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment(page, page->index, 0, 201*09cbfeafSKirill A. Shutemov PAGE_SIZE, 202bf12be1cSMichael Halcrow page->mapping->host); 203bf12be1cSMichael Halcrow } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { 204bf12be1cSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 205bf12be1cSMichael Halcrow rc = ecryptfs_copy_up_encrypted_with_header(page, 206bf12be1cSMichael Halcrow crypt_stat); 207bf12be1cSMichael Halcrow if (rc) { 208bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error attempting to copy " 209bf12be1cSMichael Halcrow "the encrypted content from the lower " 210bf12be1cSMichael Halcrow "file whilst inserting the metadata " 211bf12be1cSMichael Halcrow "from the xattr into the header; rc = " 21218d1dbf1SHarvey Harrison "[%d]\n", __func__, rc); 213bf12be1cSMichael Halcrow goto out; 214bf12be1cSMichael Halcrow } 215bf12be1cSMichael Halcrow 216e77a56ddSMichael Halcrow } else { 217bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 218*09cbfeafSKirill A. Shutemov page, page->index, 0, PAGE_SIZE, 219bf12be1cSMichael Halcrow page->mapping->host); 220e77a56ddSMichael Halcrow if (rc) { 221e77a56ddSMichael Halcrow printk(KERN_ERR "Error reading page; rc = " 222e77a56ddSMichael Halcrow "[%d]\n", rc); 223e77a56ddSMichael Halcrow goto out; 224e77a56ddSMichael Halcrow } 225e77a56ddSMichael Halcrow } 226237fead6SMichael Halcrow } else { 2270216f7f7SMichael Halcrow rc = ecryptfs_decrypt_page(page); 228237fead6SMichael Halcrow if (rc) { 229237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error decrypting page; " 230237fead6SMichael Halcrow "rc = [%d]\n", rc); 231237fead6SMichael Halcrow goto out; 232237fead6SMichael Halcrow } 233237fead6SMichael Halcrow } 234237fead6SMichael Halcrow out: 23516a72c45SMichael Halcrow if (rc) 23616a72c45SMichael Halcrow ClearPageUptodate(page); 23716a72c45SMichael Halcrow else 23816a72c45SMichael Halcrow SetPageUptodate(page); 239888d57bbSJoe Perches ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n", 240237fead6SMichael Halcrow page->index); 241237fead6SMichael Halcrow unlock_page(page); 242237fead6SMichael Halcrow return rc; 243237fead6SMichael Halcrow } 244237fead6SMichael Halcrow 245dd2a3b7aSMichael Halcrow /** 246dd2a3b7aSMichael Halcrow * Called with lower inode mutex held. 247dd2a3b7aSMichael Halcrow */ 248237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) 249237fead6SMichael Halcrow { 250237fead6SMichael Halcrow struct inode *inode = page->mapping->host; 251237fead6SMichael Halcrow int end_byte_in_page; 252237fead6SMichael Halcrow 253*09cbfeafSKirill A. Shutemov if ((i_size_read(inode) / PAGE_SIZE) != page->index) 2549d8b8ce5SMichael Halcrow goto out; 255*09cbfeafSKirill A. Shutemov end_byte_in_page = i_size_read(inode) % PAGE_SIZE; 256237fead6SMichael Halcrow if (to > end_byte_in_page) 257237fead6SMichael Halcrow end_byte_in_page = to; 258*09cbfeafSKirill A. Shutemov zero_user_segment(page, end_byte_in_page, PAGE_SIZE); 259237fead6SMichael Halcrow out: 2609d8b8ce5SMichael Halcrow return 0; 261237fead6SMichael Halcrow } 262237fead6SMichael Halcrow 263e4465fdaSMichael Halcrow /** 264807b7ebeSBadari Pulavarty * ecryptfs_write_begin 265e4465fdaSMichael Halcrow * @file: The eCryptfs file 266807b7ebeSBadari Pulavarty * @mapping: The eCryptfs object 267807b7ebeSBadari Pulavarty * @pos: The file offset at which to start writing 268807b7ebeSBadari Pulavarty * @len: Length of the write 269807b7ebeSBadari Pulavarty * @flags: Various flags 270807b7ebeSBadari Pulavarty * @pagep: Pointer to return the page 271807b7ebeSBadari Pulavarty * @fsdata: Pointer to return fs data (unused) 272e4465fdaSMichael Halcrow * 273e4465fdaSMichael Halcrow * This function must zero any hole we create 274e4465fdaSMichael Halcrow * 275e4465fdaSMichael Halcrow * Returns zero on success; non-zero otherwise 276e4465fdaSMichael Halcrow */ 277807b7ebeSBadari Pulavarty static int ecryptfs_write_begin(struct file *file, 278807b7ebeSBadari Pulavarty struct address_space *mapping, 279807b7ebeSBadari Pulavarty loff_t pos, unsigned len, unsigned flags, 280807b7ebeSBadari Pulavarty struct page **pagep, void **fsdata) 28153a2731fSMichael Halcrow { 282*09cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 283807b7ebeSBadari Pulavarty struct page *page; 2847a3f595cSEric Sandeen loff_t prev_page_end_size; 285e4465fdaSMichael Halcrow int rc = 0; 28653a2731fSMichael Halcrow 28754566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 288807b7ebeSBadari Pulavarty if (!page) 289807b7ebeSBadari Pulavarty return -ENOMEM; 290807b7ebeSBadari Pulavarty *pagep = page; 291807b7ebeSBadari Pulavarty 292*09cbfeafSKirill A. Shutemov prev_page_end_size = ((loff_t)index << PAGE_SHIFT); 29316a72c45SMichael Halcrow if (!PageUptodate(page)) { 294e4465fdaSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 295bef5bc24SAl Viro &ecryptfs_inode_to_private(mapping->host)->crypt_stat; 296e4465fdaSMichael Halcrow 297fed8859bSTyler Hicks if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 298e4465fdaSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 299*09cbfeafSKirill A. Shutemov page, index, 0, PAGE_SIZE, mapping->host); 30016a72c45SMichael Halcrow if (rc) { 301971bd8faSMasanari Iida printk(KERN_ERR "%s: Error attempting to read " 302e4465fdaSMichael Halcrow "lower page segment; rc = [%d]\n", 30318d1dbf1SHarvey Harrison __func__, rc); 30416a72c45SMichael Halcrow ClearPageUptodate(page); 30516a72c45SMichael Halcrow goto out; 30616a72c45SMichael Halcrow } else 30716a72c45SMichael Halcrow SetPageUptodate(page); 308e4465fdaSMichael Halcrow } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { 309e4465fdaSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 310e4465fdaSMichael Halcrow rc = ecryptfs_copy_up_encrypted_with_header( 311e4465fdaSMichael Halcrow page, crypt_stat); 312e4465fdaSMichael Halcrow if (rc) { 313e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error attempting " 314e4465fdaSMichael Halcrow "to copy the encrypted content " 315e4465fdaSMichael Halcrow "from the lower file whilst " 316e4465fdaSMichael Halcrow "inserting the metadata from " 317e4465fdaSMichael Halcrow "the xattr into the header; rc " 31818d1dbf1SHarvey Harrison "= [%d]\n", __func__, rc); 319e4465fdaSMichael Halcrow ClearPageUptodate(page); 320e4465fdaSMichael Halcrow goto out; 32116a72c45SMichael Halcrow } 322e4465fdaSMichael Halcrow SetPageUptodate(page); 323e4465fdaSMichael Halcrow } else { 324e4465fdaSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 325*09cbfeafSKirill A. Shutemov page, index, 0, PAGE_SIZE, 326807b7ebeSBadari Pulavarty mapping->host); 327e4465fdaSMichael Halcrow if (rc) { 328e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error reading " 329e4465fdaSMichael Halcrow "page; rc = [%d]\n", 33018d1dbf1SHarvey Harrison __func__, rc); 331e4465fdaSMichael Halcrow ClearPageUptodate(page); 332e4465fdaSMichael Halcrow goto out; 333e4465fdaSMichael Halcrow } 334e4465fdaSMichael Halcrow SetPageUptodate(page); 335e4465fdaSMichael Halcrow } 336e4465fdaSMichael Halcrow } else { 33724562486SFrank Swiderski if (prev_page_end_size 33824562486SFrank Swiderski >= i_size_read(page->mapping->host)) { 339*09cbfeafSKirill A. Shutemov zero_user(page, 0, PAGE_SIZE); 340e4bc6522SLi Wang SetPageUptodate(page); 341*09cbfeafSKirill A. Shutemov } else if (len < PAGE_SIZE) { 342e4465fdaSMichael Halcrow rc = ecryptfs_decrypt_page(page); 343e4465fdaSMichael Halcrow if (rc) { 34424562486SFrank Swiderski printk(KERN_ERR "%s: Error decrypting " 34524562486SFrank Swiderski "page at index [%ld]; " 34624562486SFrank Swiderski "rc = [%d]\n", 34718d1dbf1SHarvey Harrison __func__, page->index, rc); 348e4465fdaSMichael Halcrow ClearPageUptodate(page); 349e4465fdaSMichael Halcrow goto out; 350e4465fdaSMichael Halcrow } 351e4465fdaSMichael Halcrow SetPageUptodate(page); 352e4465fdaSMichael Halcrow } 353e4465fdaSMichael Halcrow } 354e4bc6522SLi Wang } 355e4465fdaSMichael Halcrow /* If creating a page or more of holes, zero them out via truncate. 356e4465fdaSMichael Halcrow * Note, this will increase i_size. */ 357807b7ebeSBadari Pulavarty if (index != 0) { 3587a3f595cSEric Sandeen if (prev_page_end_size > i_size_read(page->mapping->host)) { 359240e2df5SMichael Halcrow rc = ecryptfs_truncate(file->f_path.dentry, 3607a3f595cSEric Sandeen prev_page_end_size); 36153a2731fSMichael Halcrow if (rc) { 362e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error on attempt to " 36353a2731fSMichael Halcrow "truncate to (higher) offset [%lld];" 36418d1dbf1SHarvey Harrison " rc = [%d]\n", __func__, 365e4465fdaSMichael Halcrow prev_page_end_size, rc); 36653a2731fSMichael Halcrow goto out; 36753a2731fSMichael Halcrow } 36853a2731fSMichael Halcrow } 3697a3f595cSEric Sandeen } 370e4465fdaSMichael Halcrow /* Writing to a new page, and creating a small hole from start 371e4465fdaSMichael Halcrow * of page? Zero it out. */ 372807b7ebeSBadari Pulavarty if ((i_size_read(mapping->host) == prev_page_end_size) 373807b7ebeSBadari Pulavarty && (pos != 0)) 374*09cbfeafSKirill A. Shutemov zero_user(page, 0, PAGE_SIZE); 37553a2731fSMichael Halcrow out: 37650f198aeSTyler Hicks if (unlikely(rc)) { 37750f198aeSTyler Hicks unlock_page(page); 378*09cbfeafSKirill A. Shutemov put_page(page); 37950f198aeSTyler Hicks *pagep = NULL; 38050f198aeSTyler Hicks } 38153a2731fSMichael Halcrow return rc; 38253a2731fSMichael Halcrow } 38353a2731fSMichael Halcrow 384237fead6SMichael Halcrow /** 385237fead6SMichael Halcrow * ecryptfs_write_inode_size_to_header 386237fead6SMichael Halcrow * 387237fead6SMichael Halcrow * Writes the lower file size to the first 8 bytes of the header. 388237fead6SMichael Halcrow * 389237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 390237fead6SMichael Halcrow */ 3910216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode) 392237fead6SMichael Halcrow { 3930216f7f7SMichael Halcrow char *file_size_virt; 3940216f7f7SMichael Halcrow int rc; 395237fead6SMichael Halcrow 3960216f7f7SMichael Halcrow file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL); 3970216f7f7SMichael Halcrow if (!file_size_virt) { 3980216f7f7SMichael Halcrow rc = -ENOMEM; 399237fead6SMichael Halcrow goto out; 400237fead6SMichael Halcrow } 4010a688ad7SHarvey Harrison put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt); 4020216f7f7SMichael Halcrow rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0, 4030216f7f7SMichael Halcrow sizeof(u64)); 4040216f7f7SMichael Halcrow kfree(file_size_virt); 40596a7b9c2STyler Hicks if (rc < 0) 4060216f7f7SMichael Halcrow printk(KERN_ERR "%s: Error writing file size to header; " 40718d1dbf1SHarvey Harrison "rc = [%d]\n", __func__, rc); 40896a7b9c2STyler Hicks else 40996a7b9c2STyler Hicks rc = 0; 410237fead6SMichael Halcrow out: 411237fead6SMichael Halcrow return rc; 412237fead6SMichael Halcrow } 413237fead6SMichael Halcrow 4140216f7f7SMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache; 4150216f7f7SMichael Halcrow 4160216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode) 417dd2a3b7aSMichael Halcrow { 418dd2a3b7aSMichael Halcrow ssize_t size; 419dd2a3b7aSMichael Halcrow void *xattr_virt; 4200216f7f7SMichael Halcrow struct dentry *lower_dentry = 421b583043eSAl Viro ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry; 4222b0143b5SDavid Howells struct inode *lower_inode = d_inode(lower_dentry); 423dd2a3b7aSMichael Halcrow int rc; 424dd2a3b7aSMichael Halcrow 4250216f7f7SMichael Halcrow if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) { 4260216f7f7SMichael Halcrow printk(KERN_WARNING 4270216f7f7SMichael Halcrow "No support for setting xattr in lower filesystem\n"); 4280216f7f7SMichael Halcrow rc = -ENOSYS; 4290216f7f7SMichael Halcrow goto out; 4300216f7f7SMichael Halcrow } 431dd2a3b7aSMichael Halcrow xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); 432dd2a3b7aSMichael Halcrow if (!xattr_virt) { 433dd2a3b7aSMichael Halcrow printk(KERN_ERR "Out of memory whilst attempting to write " 434dd2a3b7aSMichael Halcrow "inode size to xattr\n"); 435dd2a3b7aSMichael Halcrow rc = -ENOMEM; 436dd2a3b7aSMichael Halcrow goto out; 437dd2a3b7aSMichael Halcrow } 4385955102cSAl Viro inode_lock(lower_inode); 4390216f7f7SMichael Halcrow size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME, 440*09cbfeafSKirill A. Shutemov xattr_virt, PAGE_SIZE); 441dd2a3b7aSMichael Halcrow if (size < 0) 442dd2a3b7aSMichael Halcrow size = 8; 4430a688ad7SHarvey Harrison put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt); 4440216f7f7SMichael Halcrow rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME, 445dd2a3b7aSMichael Halcrow xattr_virt, size, 0); 4465955102cSAl Viro inode_unlock(lower_inode); 447dd2a3b7aSMichael Halcrow if (rc) 448dd2a3b7aSMichael Halcrow printk(KERN_ERR "Error whilst attempting to write inode size " 449dd2a3b7aSMichael Halcrow "to lower file xattr; rc = [%d]\n", rc); 450dd2a3b7aSMichael Halcrow kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); 451dd2a3b7aSMichael Halcrow out: 452dd2a3b7aSMichael Halcrow return rc; 453dd2a3b7aSMichael Halcrow } 454dd2a3b7aSMichael Halcrow 4550216f7f7SMichael Halcrow int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode) 456dd2a3b7aSMichael Halcrow { 457dd2a3b7aSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 458dd2a3b7aSMichael Halcrow 4590216f7f7SMichael Halcrow crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 46013a791b4STyler Hicks BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); 461dd2a3b7aSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 4620216f7f7SMichael Halcrow return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode); 463dd2a3b7aSMichael Halcrow else 4640216f7f7SMichael Halcrow return ecryptfs_write_inode_size_to_header(ecryptfs_inode); 465dd2a3b7aSMichael Halcrow } 466dd2a3b7aSMichael Halcrow 467237fead6SMichael Halcrow /** 468807b7ebeSBadari Pulavarty * ecryptfs_write_end 469237fead6SMichael Halcrow * @file: The eCryptfs file object 470807b7ebeSBadari Pulavarty * @mapping: The eCryptfs object 471807b7ebeSBadari Pulavarty * @pos: The file position 472807b7ebeSBadari Pulavarty * @len: The length of the data (unused) 473807b7ebeSBadari Pulavarty * @copied: The amount of data copied 474237fead6SMichael Halcrow * @page: The eCryptfs page 475807b7ebeSBadari Pulavarty * @fsdata: The fsdata (unused) 476237fead6SMichael Halcrow */ 477807b7ebeSBadari Pulavarty static int ecryptfs_write_end(struct file *file, 478807b7ebeSBadari Pulavarty struct address_space *mapping, 479807b7ebeSBadari Pulavarty loff_t pos, unsigned len, unsigned copied, 480807b7ebeSBadari Pulavarty struct page *page, void *fsdata) 481237fead6SMichael Halcrow { 482*09cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 483*09cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 484807b7ebeSBadari Pulavarty unsigned to = from + copied; 485807b7ebeSBadari Pulavarty struct inode *ecryptfs_inode = mapping->host; 486bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 487bef5bc24SAl Viro &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 488237fead6SMichael Halcrow int rc; 489237fead6SMichael Halcrow 490237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" 491888d57bbSJoe Perches "(page w/ index = [0x%.16lx], to = [%d])\n", index, to); 49213a791b4STyler Hicks if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 49313a791b4STyler Hicks rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0, 49413a791b4STyler Hicks to); 49513a791b4STyler Hicks if (!rc) { 49613a791b4STyler Hicks rc = copied; 49713a791b4STyler Hicks fsstack_copy_inode_size(ecryptfs_inode, 49813a791b4STyler Hicks ecryptfs_inode_to_lower(ecryptfs_inode)); 49913a791b4STyler Hicks } 50013a791b4STyler Hicks goto out; 50113a791b4STyler Hicks } 502e4bc6522SLi Wang if (!PageUptodate(page)) { 503*09cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 504e4bc6522SLi Wang rc = 0; 505e4bc6522SLi Wang goto out; 506e4bc6522SLi Wang } 507e4bc6522SLi Wang SetPageUptodate(page); 508e4bc6522SLi Wang } 509bf12be1cSMichael Halcrow /* Fills in zeros if 'to' goes beyond inode size */ 510237fead6SMichael Halcrow rc = fill_zeros_to_end_of_page(page, to); 511237fead6SMichael Halcrow if (rc) { 512237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to fill " 513888d57bbSJoe Perches "zeros in page with index = [0x%.16lx]\n", index); 514237fead6SMichael Halcrow goto out; 515237fead6SMichael Halcrow } 516821f7494STyler Hicks rc = ecryptfs_encrypt_page(page); 517821f7494STyler Hicks if (rc) { 518821f7494STyler Hicks ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " 519821f7494STyler Hicks "index [0x%.16lx])\n", index); 520821f7494STyler Hicks goto out; 521821f7494STyler Hicks } 522807b7ebeSBadari Pulavarty if (pos + copied > i_size_read(ecryptfs_inode)) { 523807b7ebeSBadari Pulavarty i_size_write(ecryptfs_inode, pos + copied); 524237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Expanded file size to " 525888d57bbSJoe Perches "[0x%.16llx]\n", 526888d57bbSJoe Perches (unsigned long long)i_size_read(ecryptfs_inode)); 527821f7494STyler Hicks } 528bf12be1cSMichael Halcrow rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); 529821f7494STyler Hicks if (rc) 530dd2a3b7aSMichael Halcrow printk(KERN_ERR "Error writing inode size to metadata; " 531dd2a3b7aSMichael Halcrow "rc = [%d]\n", rc); 532821f7494STyler Hicks else 533807b7ebeSBadari Pulavarty rc = copied; 534237fead6SMichael Halcrow out: 535807b7ebeSBadari Pulavarty unlock_page(page); 536*09cbfeafSKirill A. Shutemov put_page(page); 537237fead6SMichael Halcrow return rc; 538237fead6SMichael Halcrow } 539237fead6SMichael Halcrow 540237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) 541237fead6SMichael Halcrow { 542237fead6SMichael Halcrow int rc = 0; 543237fead6SMichael Halcrow struct inode *inode; 544237fead6SMichael Halcrow struct inode *lower_inode; 545237fead6SMichael Halcrow 546237fead6SMichael Halcrow inode = (struct inode *)mapping->host; 547237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(inode); 548237fead6SMichael Halcrow if (lower_inode->i_mapping->a_ops->bmap) 549237fead6SMichael Halcrow rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, 550237fead6SMichael Halcrow block); 551237fead6SMichael Halcrow return rc; 552237fead6SMichael Halcrow } 553237fead6SMichael Halcrow 5547f09410bSAlexey Dobriyan const struct address_space_operations ecryptfs_aops = { 555237fead6SMichael Halcrow .writepage = ecryptfs_writepage, 556237fead6SMichael Halcrow .readpage = ecryptfs_readpage, 557807b7ebeSBadari Pulavarty .write_begin = ecryptfs_write_begin, 558807b7ebeSBadari Pulavarty .write_end = ecryptfs_write_end, 559237fead6SMichael Halcrow .bmap = ecryptfs_bmap, 560237fead6SMichael Halcrow }; 561