11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2237fead6SMichael Halcrow /** 3237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 4237fead6SMichael Halcrow * This is where eCryptfs coordinates the symmetric encryption and 5237fead6SMichael Halcrow * decryption of the file data as it passes between the lower 6237fead6SMichael Halcrow * encrypted file and the upper decrypted file. 7237fead6SMichael Halcrow * 8237fead6SMichael Halcrow * Copyright (C) 1997-2003 Erez Zadok 9237fead6SMichael Halcrow * Copyright (C) 2001-2003 Stony Brook University 10dd2a3b7aSMichael Halcrow * Copyright (C) 2004-2007 International Business Machines Corp. 11237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 12237fead6SMichael Halcrow */ 13237fead6SMichael Halcrow 14237fead6SMichael Halcrow #include <linux/pagemap.h> 15237fead6SMichael Halcrow #include <linux/writeback.h> 16237fead6SMichael Halcrow #include <linux/page-flags.h> 17237fead6SMichael Halcrow #include <linux/mount.h> 18237fead6SMichael Halcrow #include <linux/file.h> 19237fead6SMichael Halcrow #include <linux/scatterlist.h> 205a0e3ad6STejun Heo #include <linux/slab.h> 215d6c3191SAndreas Gruenbacher #include <linux/xattr.h> 220a688ad7SHarvey Harrison #include <asm/unaligned.h> 23237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 24237fead6SMichael Halcrow 25237fead6SMichael Halcrow /** 2616a72c45SMichael Halcrow * ecryptfs_get_locked_page 27237fead6SMichael Halcrow * 28237fead6SMichael Halcrow * Get one page from cache or lower f/s, return error otherwise. 29237fead6SMichael Halcrow * 3016a72c45SMichael Halcrow * Returns locked and up-to-date page (if ok), with increased 31237fead6SMichael Halcrow * refcnt. 32237fead6SMichael Halcrow */ 3302bd9799SAl Viro struct page *ecryptfs_get_locked_page(struct inode *inode, loff_t index) 34237fead6SMichael Halcrow { 3502bd9799SAl Viro struct page *page = read_mapping_page(inode->i_mapping, index, NULL); 3616a72c45SMichael Halcrow if (!IS_ERR(page)) 3716a72c45SMichael Halcrow lock_page(page); 3816a72c45SMichael Halcrow return page; 39237fead6SMichael Halcrow } 40237fead6SMichael Halcrow 41237fead6SMichael Halcrow /** 42237fead6SMichael Halcrow * ecryptfs_writepage 43237fead6SMichael Halcrow * @page: Page that is locked before this call is made 44237fead6SMichael Halcrow * 45237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 461589cb1aSLi Wang * 471589cb1aSLi Wang * This is where we encrypt the data and pass the encrypted data to 481589cb1aSLi Wang * the lower filesystem. In OpenPGP-compatible mode, we operate on 491589cb1aSLi Wang * entire underlying packets. 50237fead6SMichael Halcrow */ 51237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) 52237fead6SMichael Halcrow { 53237fead6SMichael Halcrow int rc; 54237fead6SMichael Halcrow 550216f7f7SMichael Halcrow rc = ecryptfs_encrypt_page(page); 56237fead6SMichael Halcrow if (rc) { 57237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error encrypting " 58888d57bbSJoe Perches "page (upper index [0x%.16lx])\n", page->index); 59237fead6SMichael Halcrow ClearPageUptodate(page); 60237fead6SMichael Halcrow goto out; 61237fead6SMichael Halcrow } 62237fead6SMichael Halcrow SetPageUptodate(page); 63237fead6SMichael Halcrow out: 6457db4e8dSThieu Le unlock_page(page); 65237fead6SMichael Halcrow return rc; 66237fead6SMichael Halcrow } 67237fead6SMichael Halcrow 68f4e60e6bSTyler Hicks static void strip_xattr_flag(char *page_virt, 69f4e60e6bSTyler Hicks struct ecryptfs_crypt_stat *crypt_stat) 70f4e60e6bSTyler Hicks { 71f4e60e6bSTyler Hicks if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 72f4e60e6bSTyler Hicks size_t written; 73f4e60e6bSTyler Hicks 74f4e60e6bSTyler Hicks crypt_stat->flags &= ~ECRYPTFS_METADATA_IN_XATTR; 75f4e60e6bSTyler Hicks ecryptfs_write_crypt_stat_flags(page_virt, crypt_stat, 76f4e60e6bSTyler Hicks &written); 77f4e60e6bSTyler Hicks crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; 78f4e60e6bSTyler Hicks } 79f4e60e6bSTyler Hicks } 80f4e60e6bSTyler Hicks 81237fead6SMichael Halcrow /** 82e77a56ddSMichael Halcrow * Header Extent: 83e77a56ddSMichael Halcrow * Octets 0-7: Unencrypted file size (big-endian) 84e77a56ddSMichael Halcrow * Octets 8-15: eCryptfs special marker 85e77a56ddSMichael Halcrow * Octets 16-19: Flags 86e77a56ddSMichael Halcrow * Octet 16: File format version number (between 0 and 255) 87e77a56ddSMichael Halcrow * Octets 17-18: Reserved 88e77a56ddSMichael Halcrow * Octet 19: Bit 1 (lsb): Reserved 89e77a56ddSMichael Halcrow * Bit 2: Encrypted? 90e77a56ddSMichael Halcrow * Bits 3-8: Reserved 91e77a56ddSMichael Halcrow * Octets 20-23: Header extent size (big-endian) 92e77a56ddSMichael Halcrow * Octets 24-25: Number of header extents at front of file 93e77a56ddSMichael Halcrow * (big-endian) 94e77a56ddSMichael Halcrow * Octet 26: Begin RFC 2440 authentication token packet set 95e77a56ddSMichael Halcrow */ 96237fead6SMichael Halcrow 97237fead6SMichael Halcrow /** 98bf12be1cSMichael Halcrow * ecryptfs_copy_up_encrypted_with_header 99bf12be1cSMichael Halcrow * @page: Sort of a ``virtual'' representation of the encrypted lower 100bf12be1cSMichael Halcrow * file. The actual lower file does not have the metadata in 101bf12be1cSMichael Halcrow * the header. This is locked. 102bf12be1cSMichael Halcrow * @crypt_stat: The eCryptfs inode's cryptographic context 103237fead6SMichael Halcrow * 104bf12be1cSMichael Halcrow * The ``view'' is the version of the file that userspace winds up 105bf12be1cSMichael Halcrow * seeing, with the header information inserted. 106237fead6SMichael Halcrow */ 107bf12be1cSMichael Halcrow static int 108bf12be1cSMichael Halcrow ecryptfs_copy_up_encrypted_with_header(struct page *page, 109bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 110237fead6SMichael Halcrow { 111bf12be1cSMichael Halcrow loff_t extent_num_in_page = 0; 11209cbfeafSKirill A. Shutemov loff_t num_extents_per_page = (PAGE_SIZE 113bf12be1cSMichael Halcrow / crypt_stat->extent_size); 114237fead6SMichael Halcrow int rc = 0; 115237fead6SMichael Halcrow 116bf12be1cSMichael Halcrow while (extent_num_in_page < num_extents_per_page) { 117d6a13c17SMichael Halcrow loff_t view_extent_num = ((((loff_t)page->index) 118d6a13c17SMichael Halcrow * num_extents_per_page) 119bf12be1cSMichael Halcrow + extent_num_in_page); 120cc11beffSMichael Halcrow size_t num_header_extents_at_front = 121fa3ef1cbSTyler Hicks (crypt_stat->metadata_size / crypt_stat->extent_size); 122e77a56ddSMichael Halcrow 123cc11beffSMichael Halcrow if (view_extent_num < num_header_extents_at_front) { 124bf12be1cSMichael Halcrow /* This is a header extent */ 125e77a56ddSMichael Halcrow char *page_virt; 126e77a56ddSMichael Halcrow 127465c9343SCong Wang page_virt = kmap_atomic(page); 12809cbfeafSKirill A. Shutemov memset(page_virt, 0, PAGE_SIZE); 129bf12be1cSMichael Halcrow /* TODO: Support more than one header extent */ 130bf12be1cSMichael Halcrow if (view_extent_num == 0) { 131157f1071STyler Hicks size_t written; 132157f1071STyler Hicks 133e77a56ddSMichael Halcrow rc = ecryptfs_read_xattr_region( 134d7cdc5feSMichael Halcrow page_virt, page->mapping->host); 135f4e60e6bSTyler Hicks strip_xattr_flag(page_virt + 16, crypt_stat); 136157f1071STyler Hicks ecryptfs_write_header_metadata(page_virt + 20, 137157f1071STyler Hicks crypt_stat, 138157f1071STyler Hicks &written); 139e77a56ddSMichael Halcrow } 140465c9343SCong Wang kunmap_atomic(page_virt); 1410a9ac382SMichael Halcrow flush_dcache_page(page); 142e77a56ddSMichael Halcrow if (rc) { 143bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error reading xattr " 14418d1dbf1SHarvey Harrison "region; rc = [%d]\n", __func__, rc); 145e77a56ddSMichael Halcrow goto out; 146e77a56ddSMichael Halcrow } 147e77a56ddSMichael Halcrow } else { 148bf12be1cSMichael Halcrow /* This is an encrypted data extent */ 149bf12be1cSMichael Halcrow loff_t lower_offset = 150cc11beffSMichael Halcrow ((view_extent_num * crypt_stat->extent_size) 151fa3ef1cbSTyler Hicks - crypt_stat->metadata_size); 152bf12be1cSMichael Halcrow 153bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 15409cbfeafSKirill A. Shutemov page, (lower_offset >> PAGE_SHIFT), 15509cbfeafSKirill A. Shutemov (lower_offset & ~PAGE_MASK), 156bf12be1cSMichael Halcrow crypt_stat->extent_size, page->mapping->host); 157e77a56ddSMichael Halcrow if (rc) { 158bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error attempting to read " 159bf12be1cSMichael Halcrow "extent at offset [%lld] in the lower " 16018d1dbf1SHarvey Harrison "file; rc = [%d]\n", __func__, 161bf12be1cSMichael Halcrow lower_offset, rc); 162e77a56ddSMichael Halcrow goto out; 163e77a56ddSMichael Halcrow } 164e77a56ddSMichael Halcrow } 165bf12be1cSMichael Halcrow extent_num_in_page++; 166bf12be1cSMichael Halcrow } 167bf12be1cSMichael Halcrow out: 168bf12be1cSMichael Halcrow return rc; 169bf12be1cSMichael Halcrow } 170bf12be1cSMichael Halcrow 171bf12be1cSMichael Halcrow /** 172bf12be1cSMichael Halcrow * ecryptfs_readpage 173bf12be1cSMichael Halcrow * @file: An eCryptfs file 174bf12be1cSMichael Halcrow * @page: Page from eCryptfs inode mapping into which to stick the read data 175bf12be1cSMichael Halcrow * 176bf12be1cSMichael Halcrow * Read in a page, decrypting if necessary. 177bf12be1cSMichael Halcrow * 178bf12be1cSMichael Halcrow * Returns zero on success; non-zero on error. 179bf12be1cSMichael Halcrow */ 180bf12be1cSMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page) 181bf12be1cSMichael Halcrow { 182bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 183bef5bc24SAl Viro &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat; 184bf12be1cSMichael Halcrow int rc = 0; 185bf12be1cSMichael Halcrow 186fed8859bSTyler Hicks if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 187bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment(page, page->index, 0, 18809cbfeafSKirill A. Shutemov PAGE_SIZE, 189bf12be1cSMichael Halcrow page->mapping->host); 190bf12be1cSMichael Halcrow } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { 191bf12be1cSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 192bf12be1cSMichael Halcrow rc = ecryptfs_copy_up_encrypted_with_header(page, 193bf12be1cSMichael Halcrow crypt_stat); 194bf12be1cSMichael Halcrow if (rc) { 195bf12be1cSMichael Halcrow printk(KERN_ERR "%s: Error attempting to copy " 196bf12be1cSMichael Halcrow "the encrypted content from the lower " 197bf12be1cSMichael Halcrow "file whilst inserting the metadata " 198bf12be1cSMichael Halcrow "from the xattr into the header; rc = " 19918d1dbf1SHarvey Harrison "[%d]\n", __func__, rc); 200bf12be1cSMichael Halcrow goto out; 201bf12be1cSMichael Halcrow } 202bf12be1cSMichael Halcrow 203e77a56ddSMichael Halcrow } else { 204bf12be1cSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 20509cbfeafSKirill A. Shutemov page, page->index, 0, PAGE_SIZE, 206bf12be1cSMichael Halcrow page->mapping->host); 207e77a56ddSMichael Halcrow if (rc) { 208e77a56ddSMichael Halcrow printk(KERN_ERR "Error reading page; rc = " 209e77a56ddSMichael Halcrow "[%d]\n", rc); 210e77a56ddSMichael Halcrow goto out; 211e77a56ddSMichael Halcrow } 212e77a56ddSMichael Halcrow } 213237fead6SMichael Halcrow } else { 2140216f7f7SMichael Halcrow rc = ecryptfs_decrypt_page(page); 215237fead6SMichael Halcrow if (rc) { 216237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error decrypting page; " 217237fead6SMichael Halcrow "rc = [%d]\n", rc); 218237fead6SMichael Halcrow goto out; 219237fead6SMichael Halcrow } 220237fead6SMichael Halcrow } 221237fead6SMichael Halcrow out: 22216a72c45SMichael Halcrow if (rc) 22316a72c45SMichael Halcrow ClearPageUptodate(page); 22416a72c45SMichael Halcrow else 22516a72c45SMichael Halcrow SetPageUptodate(page); 226888d57bbSJoe Perches ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16lx]\n", 227237fead6SMichael Halcrow page->index); 228237fead6SMichael Halcrow unlock_page(page); 229237fead6SMichael Halcrow return rc; 230237fead6SMichael Halcrow } 231237fead6SMichael Halcrow 232dd2a3b7aSMichael Halcrow /** 233dd2a3b7aSMichael Halcrow * Called with lower inode mutex held. 234dd2a3b7aSMichael Halcrow */ 235237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) 236237fead6SMichael Halcrow { 237237fead6SMichael Halcrow struct inode *inode = page->mapping->host; 238237fead6SMichael Halcrow int end_byte_in_page; 239237fead6SMichael Halcrow 24009cbfeafSKirill A. Shutemov if ((i_size_read(inode) / PAGE_SIZE) != page->index) 2419d8b8ce5SMichael Halcrow goto out; 24209cbfeafSKirill A. Shutemov end_byte_in_page = i_size_read(inode) % PAGE_SIZE; 243237fead6SMichael Halcrow if (to > end_byte_in_page) 244237fead6SMichael Halcrow end_byte_in_page = to; 24509cbfeafSKirill A. Shutemov zero_user_segment(page, end_byte_in_page, PAGE_SIZE); 246237fead6SMichael Halcrow out: 2479d8b8ce5SMichael Halcrow return 0; 248237fead6SMichael Halcrow } 249237fead6SMichael Halcrow 250e4465fdaSMichael Halcrow /** 251807b7ebeSBadari Pulavarty * ecryptfs_write_begin 252e4465fdaSMichael Halcrow * @file: The eCryptfs file 253807b7ebeSBadari Pulavarty * @mapping: The eCryptfs object 254807b7ebeSBadari Pulavarty * @pos: The file offset at which to start writing 255807b7ebeSBadari Pulavarty * @len: Length of the write 256807b7ebeSBadari Pulavarty * @flags: Various flags 257807b7ebeSBadari Pulavarty * @pagep: Pointer to return the page 258807b7ebeSBadari Pulavarty * @fsdata: Pointer to return fs data (unused) 259e4465fdaSMichael Halcrow * 260e4465fdaSMichael Halcrow * This function must zero any hole we create 261e4465fdaSMichael Halcrow * 262e4465fdaSMichael Halcrow * Returns zero on success; non-zero otherwise 263e4465fdaSMichael Halcrow */ 264807b7ebeSBadari Pulavarty static int ecryptfs_write_begin(struct file *file, 265807b7ebeSBadari Pulavarty struct address_space *mapping, 266807b7ebeSBadari Pulavarty loff_t pos, unsigned len, unsigned flags, 267807b7ebeSBadari Pulavarty struct page **pagep, void **fsdata) 26853a2731fSMichael Halcrow { 26909cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 270807b7ebeSBadari Pulavarty struct page *page; 2717a3f595cSEric Sandeen loff_t prev_page_end_size; 272e4465fdaSMichael Halcrow int rc = 0; 27353a2731fSMichael Halcrow 27454566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 275807b7ebeSBadari Pulavarty if (!page) 276807b7ebeSBadari Pulavarty return -ENOMEM; 277807b7ebeSBadari Pulavarty *pagep = page; 278807b7ebeSBadari Pulavarty 27909cbfeafSKirill A. Shutemov prev_page_end_size = ((loff_t)index << PAGE_SHIFT); 28016a72c45SMichael Halcrow if (!PageUptodate(page)) { 281e4465fdaSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 282bef5bc24SAl Viro &ecryptfs_inode_to_private(mapping->host)->crypt_stat; 283e4465fdaSMichael Halcrow 284fed8859bSTyler Hicks if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 285e4465fdaSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 28609cbfeafSKirill A. Shutemov page, index, 0, PAGE_SIZE, mapping->host); 28716a72c45SMichael Halcrow if (rc) { 288971bd8faSMasanari Iida printk(KERN_ERR "%s: Error attempting to read " 289e4465fdaSMichael Halcrow "lower page segment; rc = [%d]\n", 29018d1dbf1SHarvey Harrison __func__, rc); 29116a72c45SMichael Halcrow ClearPageUptodate(page); 29216a72c45SMichael Halcrow goto out; 29316a72c45SMichael Halcrow } else 29416a72c45SMichael Halcrow SetPageUptodate(page); 295e4465fdaSMichael Halcrow } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) { 296e4465fdaSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) { 297e4465fdaSMichael Halcrow rc = ecryptfs_copy_up_encrypted_with_header( 298e4465fdaSMichael Halcrow page, crypt_stat); 299e4465fdaSMichael Halcrow if (rc) { 300e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error attempting " 301e4465fdaSMichael Halcrow "to copy the encrypted content " 302e4465fdaSMichael Halcrow "from the lower file whilst " 303e4465fdaSMichael Halcrow "inserting the metadata from " 304e4465fdaSMichael Halcrow "the xattr into the header; rc " 30518d1dbf1SHarvey Harrison "= [%d]\n", __func__, rc); 306e4465fdaSMichael Halcrow ClearPageUptodate(page); 307e4465fdaSMichael Halcrow goto out; 30816a72c45SMichael Halcrow } 309e4465fdaSMichael Halcrow SetPageUptodate(page); 310e4465fdaSMichael Halcrow } else { 311e4465fdaSMichael Halcrow rc = ecryptfs_read_lower_page_segment( 31209cbfeafSKirill A. Shutemov page, index, 0, PAGE_SIZE, 313807b7ebeSBadari Pulavarty mapping->host); 314e4465fdaSMichael Halcrow if (rc) { 315e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error reading " 316e4465fdaSMichael Halcrow "page; rc = [%d]\n", 31718d1dbf1SHarvey Harrison __func__, rc); 318e4465fdaSMichael Halcrow ClearPageUptodate(page); 319e4465fdaSMichael Halcrow goto out; 320e4465fdaSMichael Halcrow } 321e4465fdaSMichael Halcrow SetPageUptodate(page); 322e4465fdaSMichael Halcrow } 323e4465fdaSMichael Halcrow } else { 32424562486SFrank Swiderski if (prev_page_end_size 32524562486SFrank Swiderski >= i_size_read(page->mapping->host)) { 32609cbfeafSKirill A. Shutemov zero_user(page, 0, PAGE_SIZE); 327e4bc6522SLi Wang SetPageUptodate(page); 32809cbfeafSKirill A. Shutemov } else if (len < PAGE_SIZE) { 329e4465fdaSMichael Halcrow rc = ecryptfs_decrypt_page(page); 330e4465fdaSMichael Halcrow if (rc) { 33124562486SFrank Swiderski printk(KERN_ERR "%s: Error decrypting " 33224562486SFrank Swiderski "page at index [%ld]; " 33324562486SFrank Swiderski "rc = [%d]\n", 33418d1dbf1SHarvey Harrison __func__, page->index, rc); 335e4465fdaSMichael Halcrow ClearPageUptodate(page); 336e4465fdaSMichael Halcrow goto out; 337e4465fdaSMichael Halcrow } 338e4465fdaSMichael Halcrow SetPageUptodate(page); 339e4465fdaSMichael Halcrow } 340e4465fdaSMichael Halcrow } 341e4bc6522SLi Wang } 342e4465fdaSMichael Halcrow /* If creating a page or more of holes, zero them out via truncate. 343e4465fdaSMichael Halcrow * Note, this will increase i_size. */ 344807b7ebeSBadari Pulavarty if (index != 0) { 3457a3f595cSEric Sandeen if (prev_page_end_size > i_size_read(page->mapping->host)) { 346240e2df5SMichael Halcrow rc = ecryptfs_truncate(file->f_path.dentry, 3477a3f595cSEric Sandeen prev_page_end_size); 34853a2731fSMichael Halcrow if (rc) { 349e4465fdaSMichael Halcrow printk(KERN_ERR "%s: Error on attempt to " 35053a2731fSMichael Halcrow "truncate to (higher) offset [%lld];" 35118d1dbf1SHarvey Harrison " rc = [%d]\n", __func__, 352e4465fdaSMichael Halcrow prev_page_end_size, rc); 35353a2731fSMichael Halcrow goto out; 35453a2731fSMichael Halcrow } 35553a2731fSMichael Halcrow } 3567a3f595cSEric Sandeen } 357e4465fdaSMichael Halcrow /* Writing to a new page, and creating a small hole from start 358e4465fdaSMichael Halcrow * of page? Zero it out. */ 359807b7ebeSBadari Pulavarty if ((i_size_read(mapping->host) == prev_page_end_size) 360807b7ebeSBadari Pulavarty && (pos != 0)) 36109cbfeafSKirill A. Shutemov zero_user(page, 0, PAGE_SIZE); 36253a2731fSMichael Halcrow out: 36350f198aeSTyler Hicks if (unlikely(rc)) { 36450f198aeSTyler Hicks unlock_page(page); 36509cbfeafSKirill A. Shutemov put_page(page); 36650f198aeSTyler Hicks *pagep = NULL; 36750f198aeSTyler Hicks } 36853a2731fSMichael Halcrow return rc; 36953a2731fSMichael Halcrow } 37053a2731fSMichael Halcrow 371237fead6SMichael Halcrow /** 372237fead6SMichael Halcrow * ecryptfs_write_inode_size_to_header 373237fead6SMichael Halcrow * 374237fead6SMichael Halcrow * Writes the lower file size to the first 8 bytes of the header. 375237fead6SMichael Halcrow * 376237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 377237fead6SMichael Halcrow */ 3780216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode) 379237fead6SMichael Halcrow { 3800216f7f7SMichael Halcrow char *file_size_virt; 3810216f7f7SMichael Halcrow int rc; 382237fead6SMichael Halcrow 3830216f7f7SMichael Halcrow file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL); 3840216f7f7SMichael Halcrow if (!file_size_virt) { 3850216f7f7SMichael Halcrow rc = -ENOMEM; 386237fead6SMichael Halcrow goto out; 387237fead6SMichael Halcrow } 3880a688ad7SHarvey Harrison put_unaligned_be64(i_size_read(ecryptfs_inode), file_size_virt); 3890216f7f7SMichael Halcrow rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0, 3900216f7f7SMichael Halcrow sizeof(u64)); 3910216f7f7SMichael Halcrow kfree(file_size_virt); 39296a7b9c2STyler Hicks if (rc < 0) 3930216f7f7SMichael Halcrow printk(KERN_ERR "%s: Error writing file size to header; " 39418d1dbf1SHarvey Harrison "rc = [%d]\n", __func__, rc); 39596a7b9c2STyler Hicks else 39696a7b9c2STyler Hicks rc = 0; 397237fead6SMichael Halcrow out: 398237fead6SMichael Halcrow return rc; 399237fead6SMichael Halcrow } 400237fead6SMichael Halcrow 4010216f7f7SMichael Halcrow struct kmem_cache *ecryptfs_xattr_cache; 4020216f7f7SMichael Halcrow 4030216f7f7SMichael Halcrow static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode) 404dd2a3b7aSMichael Halcrow { 405dd2a3b7aSMichael Halcrow ssize_t size; 406dd2a3b7aSMichael Halcrow void *xattr_virt; 4070216f7f7SMichael Halcrow struct dentry *lower_dentry = 408b583043eSAl Viro ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_path.dentry; 4092b0143b5SDavid Howells struct inode *lower_inode = d_inode(lower_dentry); 410dd2a3b7aSMichael Halcrow int rc; 411dd2a3b7aSMichael Halcrow 4125d6c3191SAndreas Gruenbacher if (!(lower_inode->i_opflags & IOP_XATTR)) { 4130216f7f7SMichael Halcrow printk(KERN_WARNING 4140216f7f7SMichael Halcrow "No support for setting xattr in lower filesystem\n"); 4150216f7f7SMichael Halcrow rc = -ENOSYS; 4160216f7f7SMichael Halcrow goto out; 4170216f7f7SMichael Halcrow } 418dd2a3b7aSMichael Halcrow xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); 419dd2a3b7aSMichael Halcrow if (!xattr_virt) { 420dd2a3b7aSMichael Halcrow rc = -ENOMEM; 421dd2a3b7aSMichael Halcrow goto out; 422dd2a3b7aSMichael Halcrow } 4235955102cSAl Viro inode_lock(lower_inode); 4245d6c3191SAndreas Gruenbacher size = __vfs_getxattr(lower_dentry, lower_inode, ECRYPTFS_XATTR_NAME, 42509cbfeafSKirill A. Shutemov xattr_virt, PAGE_SIZE); 426dd2a3b7aSMichael Halcrow if (size < 0) 427dd2a3b7aSMichael Halcrow size = 8; 4280a688ad7SHarvey Harrison put_unaligned_be64(i_size_read(ecryptfs_inode), xattr_virt); 429*c7c7a1a1STycho Andersen rc = __vfs_setxattr(&init_user_ns, lower_dentry, lower_inode, 430*c7c7a1a1STycho Andersen ECRYPTFS_XATTR_NAME, xattr_virt, size, 0); 4315955102cSAl Viro inode_unlock(lower_inode); 432dd2a3b7aSMichael Halcrow if (rc) 433dd2a3b7aSMichael Halcrow printk(KERN_ERR "Error whilst attempting to write inode size " 434dd2a3b7aSMichael Halcrow "to lower file xattr; rc = [%d]\n", rc); 435dd2a3b7aSMichael Halcrow kmem_cache_free(ecryptfs_xattr_cache, xattr_virt); 436dd2a3b7aSMichael Halcrow out: 437dd2a3b7aSMichael Halcrow return rc; 438dd2a3b7aSMichael Halcrow } 439dd2a3b7aSMichael Halcrow 4400216f7f7SMichael Halcrow int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode) 441dd2a3b7aSMichael Halcrow { 442dd2a3b7aSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 443dd2a3b7aSMichael Halcrow 4440216f7f7SMichael Halcrow crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 44513a791b4STyler Hicks BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); 446dd2a3b7aSMichael Halcrow if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 4470216f7f7SMichael Halcrow return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode); 448dd2a3b7aSMichael Halcrow else 4490216f7f7SMichael Halcrow return ecryptfs_write_inode_size_to_header(ecryptfs_inode); 450dd2a3b7aSMichael Halcrow } 451dd2a3b7aSMichael Halcrow 452237fead6SMichael Halcrow /** 453807b7ebeSBadari Pulavarty * ecryptfs_write_end 454237fead6SMichael Halcrow * @file: The eCryptfs file object 455807b7ebeSBadari Pulavarty * @mapping: The eCryptfs object 456807b7ebeSBadari Pulavarty * @pos: The file position 457807b7ebeSBadari Pulavarty * @len: The length of the data (unused) 458807b7ebeSBadari Pulavarty * @copied: The amount of data copied 459237fead6SMichael Halcrow * @page: The eCryptfs page 460807b7ebeSBadari Pulavarty * @fsdata: The fsdata (unused) 461237fead6SMichael Halcrow */ 462807b7ebeSBadari Pulavarty static int ecryptfs_write_end(struct file *file, 463807b7ebeSBadari Pulavarty struct address_space *mapping, 464807b7ebeSBadari Pulavarty loff_t pos, unsigned len, unsigned copied, 465807b7ebeSBadari Pulavarty struct page *page, void *fsdata) 466237fead6SMichael Halcrow { 46709cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 46809cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 469807b7ebeSBadari Pulavarty unsigned to = from + copied; 470807b7ebeSBadari Pulavarty struct inode *ecryptfs_inode = mapping->host; 471bf12be1cSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat = 472bef5bc24SAl Viro &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; 473237fead6SMichael Halcrow int rc; 474237fead6SMichael Halcrow 475237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" 476888d57bbSJoe Perches "(page w/ index = [0x%.16lx], to = [%d])\n", index, to); 47713a791b4STyler Hicks if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 47813a791b4STyler Hicks rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page, 0, 47913a791b4STyler Hicks to); 48013a791b4STyler Hicks if (!rc) { 48113a791b4STyler Hicks rc = copied; 48213a791b4STyler Hicks fsstack_copy_inode_size(ecryptfs_inode, 48313a791b4STyler Hicks ecryptfs_inode_to_lower(ecryptfs_inode)); 48413a791b4STyler Hicks } 48513a791b4STyler Hicks goto out; 48613a791b4STyler Hicks } 487e4bc6522SLi Wang if (!PageUptodate(page)) { 48809cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 489e4bc6522SLi Wang rc = 0; 490e4bc6522SLi Wang goto out; 491e4bc6522SLi Wang } 492e4bc6522SLi Wang SetPageUptodate(page); 493e4bc6522SLi Wang } 494bf12be1cSMichael Halcrow /* Fills in zeros if 'to' goes beyond inode size */ 495237fead6SMichael Halcrow rc = fill_zeros_to_end_of_page(page, to); 496237fead6SMichael Halcrow if (rc) { 497237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to fill " 498888d57bbSJoe Perches "zeros in page with index = [0x%.16lx]\n", index); 499237fead6SMichael Halcrow goto out; 500237fead6SMichael Halcrow } 501821f7494STyler Hicks rc = ecryptfs_encrypt_page(page); 502821f7494STyler Hicks if (rc) { 503821f7494STyler Hicks ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " 504821f7494STyler Hicks "index [0x%.16lx])\n", index); 505821f7494STyler Hicks goto out; 506821f7494STyler Hicks } 507807b7ebeSBadari Pulavarty if (pos + copied > i_size_read(ecryptfs_inode)) { 508807b7ebeSBadari Pulavarty i_size_write(ecryptfs_inode, pos + copied); 509237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Expanded file size to " 510888d57bbSJoe Perches "[0x%.16llx]\n", 511888d57bbSJoe Perches (unsigned long long)i_size_read(ecryptfs_inode)); 512821f7494STyler Hicks } 513bf12be1cSMichael Halcrow rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); 514821f7494STyler Hicks if (rc) 515dd2a3b7aSMichael Halcrow printk(KERN_ERR "Error writing inode size to metadata; " 516dd2a3b7aSMichael Halcrow "rc = [%d]\n", rc); 517821f7494STyler Hicks else 518807b7ebeSBadari Pulavarty rc = copied; 519237fead6SMichael Halcrow out: 520807b7ebeSBadari Pulavarty unlock_page(page); 52109cbfeafSKirill A. Shutemov put_page(page); 522237fead6SMichael Halcrow return rc; 523237fead6SMichael Halcrow } 524237fead6SMichael Halcrow 525237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) 526237fead6SMichael Halcrow { 527569d2056SCarlos Maiolino struct inode *lower_inode = ecryptfs_inode_to_lower(mapping->host); 528569d2056SCarlos Maiolino int ret = bmap(lower_inode, &block); 529237fead6SMichael Halcrow 530569d2056SCarlos Maiolino if (ret) 531569d2056SCarlos Maiolino return 0; 532569d2056SCarlos Maiolino return block; 533237fead6SMichael Halcrow } 534237fead6SMichael Halcrow 5357f09410bSAlexey Dobriyan const struct address_space_operations ecryptfs_aops = { 536237fead6SMichael Halcrow .writepage = ecryptfs_writepage, 537237fead6SMichael Halcrow .readpage = ecryptfs_readpage, 538807b7ebeSBadari Pulavarty .write_begin = ecryptfs_write_begin, 539807b7ebeSBadari Pulavarty .write_end = ecryptfs_write_end, 540237fead6SMichael Halcrow .bmap = ecryptfs_bmap, 541237fead6SMichael Halcrow }; 542