1*237fead6SMichael Halcrow /** 2*237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 3*237fead6SMichael Halcrow * This is where eCryptfs coordinates the symmetric encryption and 4*237fead6SMichael Halcrow * decryption of the file data as it passes between the lower 5*237fead6SMichael Halcrow * encrypted file and the upper decrypted file. 6*237fead6SMichael Halcrow * 7*237fead6SMichael Halcrow * Copyright (C) 1997-2003 Erez Zadok 8*237fead6SMichael Halcrow * Copyright (C) 2001-2003 Stony Brook University 9*237fead6SMichael Halcrow * Copyright (C) 2004-2006 International Business Machines Corp. 10*237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 11*237fead6SMichael Halcrow * 12*237fead6SMichael Halcrow * This program is free software; you can redistribute it and/or 13*237fead6SMichael Halcrow * modify it under the terms of the GNU General Public License as 14*237fead6SMichael Halcrow * published by the Free Software Foundation; either version 2 of the 15*237fead6SMichael Halcrow * License, or (at your option) any later version. 16*237fead6SMichael Halcrow * 17*237fead6SMichael Halcrow * This program is distributed in the hope that it will be useful, but 18*237fead6SMichael Halcrow * WITHOUT ANY WARRANTY; without even the implied warranty of 19*237fead6SMichael Halcrow * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20*237fead6SMichael Halcrow * General Public License for more details. 21*237fead6SMichael Halcrow * 22*237fead6SMichael Halcrow * You should have received a copy of the GNU General Public License 23*237fead6SMichael Halcrow * along with this program; if not, write to the Free Software 24*237fead6SMichael Halcrow * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 25*237fead6SMichael Halcrow * 02111-1307, USA. 26*237fead6SMichael Halcrow */ 27*237fead6SMichael Halcrow 28*237fead6SMichael Halcrow #include <linux/pagemap.h> 29*237fead6SMichael Halcrow #include <linux/writeback.h> 30*237fead6SMichael Halcrow #include <linux/page-flags.h> 31*237fead6SMichael Halcrow #include <linux/mount.h> 32*237fead6SMichael Halcrow #include <linux/file.h> 33*237fead6SMichael Halcrow #include <linux/crypto.h> 34*237fead6SMichael Halcrow #include <linux/scatterlist.h> 35*237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 36*237fead6SMichael Halcrow 37*237fead6SMichael Halcrow struct kmem_cache *ecryptfs_lower_page_cache; 38*237fead6SMichael Halcrow 39*237fead6SMichael Halcrow /** 40*237fead6SMichael Halcrow * ecryptfs_get1page 41*237fead6SMichael Halcrow * 42*237fead6SMichael Halcrow * Get one page from cache or lower f/s, return error otherwise. 43*237fead6SMichael Halcrow * 44*237fead6SMichael Halcrow * Returns unlocked and up-to-date page (if ok), with increased 45*237fead6SMichael Halcrow * refcnt. 46*237fead6SMichael Halcrow */ 47*237fead6SMichael Halcrow static struct page *ecryptfs_get1page(struct file *file, int index) 48*237fead6SMichael Halcrow { 49*237fead6SMichael Halcrow struct page *page; 50*237fead6SMichael Halcrow struct dentry *dentry; 51*237fead6SMichael Halcrow struct inode *inode; 52*237fead6SMichael Halcrow struct address_space *mapping; 53*237fead6SMichael Halcrow 54*237fead6SMichael Halcrow dentry = file->f_dentry; 55*237fead6SMichael Halcrow inode = dentry->d_inode; 56*237fead6SMichael Halcrow mapping = inode->i_mapping; 57*237fead6SMichael Halcrow page = read_cache_page(mapping, index, 58*237fead6SMichael Halcrow (filler_t *)mapping->a_ops->readpage, 59*237fead6SMichael Halcrow (void *)file); 60*237fead6SMichael Halcrow if (IS_ERR(page)) 61*237fead6SMichael Halcrow goto out; 62*237fead6SMichael Halcrow wait_on_page_locked(page); 63*237fead6SMichael Halcrow out: 64*237fead6SMichael Halcrow return page; 65*237fead6SMichael Halcrow } 66*237fead6SMichael Halcrow 67*237fead6SMichael Halcrow static 68*237fead6SMichael Halcrow int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros); 69*237fead6SMichael Halcrow 70*237fead6SMichael Halcrow /** 71*237fead6SMichael Halcrow * ecryptfs_fill_zeros 72*237fead6SMichael Halcrow * @file: The ecryptfs file 73*237fead6SMichael Halcrow * @new_length: The new length of the data in the underlying file; 74*237fead6SMichael Halcrow * everything between the prior end of the file and the 75*237fead6SMichael Halcrow * new end of the file will be filled with zero's. 76*237fead6SMichael Halcrow * new_length must be greater than current length 77*237fead6SMichael Halcrow * 78*237fead6SMichael Halcrow * Function for handling lseek-ing past the end of the file. 79*237fead6SMichael Halcrow * 80*237fead6SMichael Halcrow * This function does not support shrinking, only growing a file. 81*237fead6SMichael Halcrow * 82*237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise. 83*237fead6SMichael Halcrow */ 84*237fead6SMichael Halcrow int ecryptfs_fill_zeros(struct file *file, loff_t new_length) 85*237fead6SMichael Halcrow { 86*237fead6SMichael Halcrow int rc = 0; 87*237fead6SMichael Halcrow struct dentry *dentry = file->f_dentry; 88*237fead6SMichael Halcrow struct inode *inode = dentry->d_inode; 89*237fead6SMichael Halcrow pgoff_t old_end_page_index = 0; 90*237fead6SMichael Halcrow pgoff_t index = old_end_page_index; 91*237fead6SMichael Halcrow int old_end_pos_in_page = -1; 92*237fead6SMichael Halcrow pgoff_t new_end_page_index; 93*237fead6SMichael Halcrow int new_end_pos_in_page; 94*237fead6SMichael Halcrow loff_t cur_length = i_size_read(inode); 95*237fead6SMichael Halcrow 96*237fead6SMichael Halcrow if (cur_length != 0) { 97*237fead6SMichael Halcrow index = old_end_page_index = 98*237fead6SMichael Halcrow ((cur_length - 1) >> PAGE_CACHE_SHIFT); 99*237fead6SMichael Halcrow old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK); 100*237fead6SMichael Halcrow } 101*237fead6SMichael Halcrow new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT); 102*237fead6SMichael Halcrow new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK); 103*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; " 104*237fead6SMichael Halcrow "old_end_pos_in_page = [%d]; " 105*237fead6SMichael Halcrow "new_end_page_index = [0x%.16x]; " 106*237fead6SMichael Halcrow "new_end_pos_in_page = [%d]\n", 107*237fead6SMichael Halcrow old_end_page_index, old_end_pos_in_page, 108*237fead6SMichael Halcrow new_end_page_index, new_end_pos_in_page); 109*237fead6SMichael Halcrow if (old_end_page_index == new_end_page_index) { 110*237fead6SMichael Halcrow /* Start and end are in the same page; we just need to 111*237fead6SMichael Halcrow * set a portion of the existing page to zero's */ 112*237fead6SMichael Halcrow rc = write_zeros(file, index, (old_end_pos_in_page + 1), 113*237fead6SMichael Halcrow (new_end_pos_in_page - old_end_pos_in_page)); 114*237fead6SMichael Halcrow if (rc) 115*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], " 116*237fead6SMichael Halcrow "index=[0x%.16x], " 117*237fead6SMichael Halcrow "old_end_pos_in_page=[d], " 118*237fead6SMichael Halcrow "(PAGE_CACHE_SIZE - new_end_pos_in_page" 119*237fead6SMichael Halcrow "=[%d]" 120*237fead6SMichael Halcrow ")=[d]) returned [%d]\n", file, index, 121*237fead6SMichael Halcrow old_end_pos_in_page, 122*237fead6SMichael Halcrow new_end_pos_in_page, 123*237fead6SMichael Halcrow (PAGE_CACHE_SIZE - new_end_pos_in_page), 124*237fead6SMichael Halcrow rc); 125*237fead6SMichael Halcrow goto out; 126*237fead6SMichael Halcrow } 127*237fead6SMichael Halcrow /* Fill the remainder of the previous last page with zeros */ 128*237fead6SMichael Halcrow rc = write_zeros(file, index, (old_end_pos_in_page + 1), 129*237fead6SMichael Halcrow ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page)); 130*237fead6SMichael Halcrow if (rc) { 131*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], " 132*237fead6SMichael Halcrow "index=[0x%.16x], old_end_pos_in_page=[d], " 133*237fead6SMichael Halcrow "(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) " 134*237fead6SMichael Halcrow "returned [%d]\n", file, index, 135*237fead6SMichael Halcrow old_end_pos_in_page, 136*237fead6SMichael Halcrow (PAGE_CACHE_SIZE - old_end_pos_in_page), rc); 137*237fead6SMichael Halcrow goto out; 138*237fead6SMichael Halcrow } 139*237fead6SMichael Halcrow index++; 140*237fead6SMichael Halcrow while (index < new_end_page_index) { 141*237fead6SMichael Halcrow /* Fill all intermediate pages with zeros */ 142*237fead6SMichael Halcrow rc = write_zeros(file, index, 0, PAGE_CACHE_SIZE); 143*237fead6SMichael Halcrow if (rc) { 144*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], " 145*237fead6SMichael Halcrow "index=[0x%.16x], " 146*237fead6SMichael Halcrow "old_end_pos_in_page=[d], " 147*237fead6SMichael Halcrow "(PAGE_CACHE_SIZE - new_end_pos_in_page" 148*237fead6SMichael Halcrow "=[%d]" 149*237fead6SMichael Halcrow ")=[d]) returned [%d]\n", file, index, 150*237fead6SMichael Halcrow old_end_pos_in_page, 151*237fead6SMichael Halcrow new_end_pos_in_page, 152*237fead6SMichael Halcrow (PAGE_CACHE_SIZE - new_end_pos_in_page), 153*237fead6SMichael Halcrow rc); 154*237fead6SMichael Halcrow goto out; 155*237fead6SMichael Halcrow } 156*237fead6SMichael Halcrow index++; 157*237fead6SMichael Halcrow } 158*237fead6SMichael Halcrow /* Fill the portion at the beginning of the last new page with 159*237fead6SMichael Halcrow * zero's */ 160*237fead6SMichael Halcrow rc = write_zeros(file, index, 0, (new_end_pos_in_page + 1)); 161*237fead6SMichael Halcrow if (rc) { 162*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "write_zeros(file=" 163*237fead6SMichael Halcrow "[%p], index=[0x%.16x], 0, " 164*237fead6SMichael Halcrow "new_end_pos_in_page=[%d]" 165*237fead6SMichael Halcrow "returned [%d]\n", file, index, 166*237fead6SMichael Halcrow new_end_pos_in_page, rc); 167*237fead6SMichael Halcrow goto out; 168*237fead6SMichael Halcrow } 169*237fead6SMichael Halcrow out: 170*237fead6SMichael Halcrow return rc; 171*237fead6SMichael Halcrow } 172*237fead6SMichael Halcrow 173*237fead6SMichael Halcrow /** 174*237fead6SMichael Halcrow * ecryptfs_writepage 175*237fead6SMichael Halcrow * @page: Page that is locked before this call is made 176*237fead6SMichael Halcrow * 177*237fead6SMichael Halcrow * Returns zero on success; non-zero otherwise 178*237fead6SMichael Halcrow */ 179*237fead6SMichael Halcrow static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) 180*237fead6SMichael Halcrow { 181*237fead6SMichael Halcrow struct ecryptfs_page_crypt_context ctx; 182*237fead6SMichael Halcrow int rc; 183*237fead6SMichael Halcrow 184*237fead6SMichael Halcrow ctx.page = page; 185*237fead6SMichael Halcrow ctx.mode = ECRYPTFS_WRITEPAGE_MODE; 186*237fead6SMichael Halcrow ctx.param.wbc = wbc; 187*237fead6SMichael Halcrow rc = ecryptfs_encrypt_page(&ctx); 188*237fead6SMichael Halcrow if (rc) { 189*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error encrypting " 190*237fead6SMichael Halcrow "page (upper index [0x%.16x])\n", page->index); 191*237fead6SMichael Halcrow ClearPageUptodate(page); 192*237fead6SMichael Halcrow goto out; 193*237fead6SMichael Halcrow } 194*237fead6SMichael Halcrow SetPageUptodate(page); 195*237fead6SMichael Halcrow unlock_page(page); 196*237fead6SMichael Halcrow out: 197*237fead6SMichael Halcrow return rc; 198*237fead6SMichael Halcrow } 199*237fead6SMichael Halcrow 200*237fead6SMichael Halcrow /** 201*237fead6SMichael Halcrow * Reads the data from the lower file file at index lower_page_index 202*237fead6SMichael Halcrow * and copies that data into page. 203*237fead6SMichael Halcrow * 204*237fead6SMichael Halcrow * @param page Page to fill 205*237fead6SMichael Halcrow * @param lower_page_index Index of the page in the lower file to get 206*237fead6SMichael Halcrow */ 207*237fead6SMichael Halcrow int ecryptfs_do_readpage(struct file *file, struct page *page, 208*237fead6SMichael Halcrow pgoff_t lower_page_index) 209*237fead6SMichael Halcrow { 210*237fead6SMichael Halcrow int rc; 211*237fead6SMichael Halcrow struct dentry *dentry; 212*237fead6SMichael Halcrow struct file *lower_file; 213*237fead6SMichael Halcrow struct dentry *lower_dentry; 214*237fead6SMichael Halcrow struct inode *inode; 215*237fead6SMichael Halcrow struct inode *lower_inode; 216*237fead6SMichael Halcrow char *page_data; 217*237fead6SMichael Halcrow struct page *lower_page = NULL; 218*237fead6SMichael Halcrow char *lower_page_data; 219*237fead6SMichael Halcrow const struct address_space_operations *lower_a_ops; 220*237fead6SMichael Halcrow 221*237fead6SMichael Halcrow dentry = file->f_dentry; 222*237fead6SMichael Halcrow lower_file = ecryptfs_file_to_lower(file); 223*237fead6SMichael Halcrow lower_dentry = ecryptfs_dentry_to_lower(dentry); 224*237fead6SMichael Halcrow inode = dentry->d_inode; 225*237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(inode); 226*237fead6SMichael Halcrow lower_a_ops = lower_inode->i_mapping->a_ops; 227*237fead6SMichael Halcrow lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index, 228*237fead6SMichael Halcrow (filler_t *)lower_a_ops->readpage, 229*237fead6SMichael Halcrow (void *)lower_file); 230*237fead6SMichael Halcrow if (IS_ERR(lower_page)) { 231*237fead6SMichael Halcrow rc = PTR_ERR(lower_page); 232*237fead6SMichael Halcrow lower_page = NULL; 233*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error reading from page cache\n"); 234*237fead6SMichael Halcrow goto out; 235*237fead6SMichael Halcrow } 236*237fead6SMichael Halcrow wait_on_page_locked(lower_page); 237*237fead6SMichael Halcrow page_data = (char *)kmap(page); 238*237fead6SMichael Halcrow if (!page_data) { 239*237fead6SMichael Halcrow rc = -ENOMEM; 240*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error mapping page\n"); 241*237fead6SMichael Halcrow goto out; 242*237fead6SMichael Halcrow } 243*237fead6SMichael Halcrow lower_page_data = (char *)kmap(lower_page); 244*237fead6SMichael Halcrow if (!lower_page_data) { 245*237fead6SMichael Halcrow rc = -ENOMEM; 246*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error mapping page\n"); 247*237fead6SMichael Halcrow kunmap(page); 248*237fead6SMichael Halcrow goto out; 249*237fead6SMichael Halcrow } 250*237fead6SMichael Halcrow memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE); 251*237fead6SMichael Halcrow kunmap(lower_page); 252*237fead6SMichael Halcrow kunmap(page); 253*237fead6SMichael Halcrow rc = 0; 254*237fead6SMichael Halcrow out: 255*237fead6SMichael Halcrow if (likely(lower_page)) 256*237fead6SMichael Halcrow page_cache_release(lower_page); 257*237fead6SMichael Halcrow if (rc == 0) 258*237fead6SMichael Halcrow SetPageUptodate(page); 259*237fead6SMichael Halcrow else 260*237fead6SMichael Halcrow ClearPageUptodate(page); 261*237fead6SMichael Halcrow return rc; 262*237fead6SMichael Halcrow } 263*237fead6SMichael Halcrow 264*237fead6SMichael Halcrow /** 265*237fead6SMichael Halcrow * ecryptfs_readpage 266*237fead6SMichael Halcrow * @file: This is an ecryptfs file 267*237fead6SMichael Halcrow * @page: ecryptfs associated page to stick the read data into 268*237fead6SMichael Halcrow * 269*237fead6SMichael Halcrow * Read in a page, decrypting if necessary. 270*237fead6SMichael Halcrow * 271*237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 272*237fead6SMichael Halcrow */ 273*237fead6SMichael Halcrow static int ecryptfs_readpage(struct file *file, struct page *page) 274*237fead6SMichael Halcrow { 275*237fead6SMichael Halcrow int rc = 0; 276*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 277*237fead6SMichael Halcrow 278*237fead6SMichael Halcrow BUG_ON(!(file && file->f_dentry && file->f_dentry->d_inode)); 279*237fead6SMichael Halcrow crypt_stat = 280*237fead6SMichael Halcrow &ecryptfs_inode_to_private(file->f_dentry->d_inode)->crypt_stat; 281*237fead6SMichael Halcrow if (!crypt_stat 282*237fead6SMichael Halcrow || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED) 283*237fead6SMichael Halcrow || ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) { 284*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 285*237fead6SMichael Halcrow "Passing through unencrypted page\n"); 286*237fead6SMichael Halcrow rc = ecryptfs_do_readpage(file, page, page->index); 287*237fead6SMichael Halcrow if (rc) { 288*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error reading page; rc = " 289*237fead6SMichael Halcrow "[%d]\n", rc); 290*237fead6SMichael Halcrow goto out; 291*237fead6SMichael Halcrow } 292*237fead6SMichael Halcrow } else { 293*237fead6SMichael Halcrow rc = ecryptfs_decrypt_page(file, page); 294*237fead6SMichael Halcrow if (rc) { 295*237fead6SMichael Halcrow 296*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error decrypting page; " 297*237fead6SMichael Halcrow "rc = [%d]\n", rc); 298*237fead6SMichael Halcrow goto out; 299*237fead6SMichael Halcrow } 300*237fead6SMichael Halcrow } 301*237fead6SMichael Halcrow SetPageUptodate(page); 302*237fead6SMichael Halcrow out: 303*237fead6SMichael Halcrow if (rc) 304*237fead6SMichael Halcrow ClearPageUptodate(page); 305*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", 306*237fead6SMichael Halcrow page->index); 307*237fead6SMichael Halcrow unlock_page(page); 308*237fead6SMichael Halcrow return rc; 309*237fead6SMichael Halcrow } 310*237fead6SMichael Halcrow 311*237fead6SMichael Halcrow static int fill_zeros_to_end_of_page(struct page *page, unsigned int to) 312*237fead6SMichael Halcrow { 313*237fead6SMichael Halcrow struct inode *inode = page->mapping->host; 314*237fead6SMichael Halcrow int end_byte_in_page; 315*237fead6SMichael Halcrow int rc = 0; 316*237fead6SMichael Halcrow char *page_virt; 317*237fead6SMichael Halcrow 318*237fead6SMichael Halcrow if ((i_size_read(inode) / PAGE_CACHE_SIZE) == page->index) { 319*237fead6SMichael Halcrow end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE; 320*237fead6SMichael Halcrow if (to > end_byte_in_page) 321*237fead6SMichael Halcrow end_byte_in_page = to; 322*237fead6SMichael Halcrow page_virt = kmap(page); 323*237fead6SMichael Halcrow if (!page_virt) { 324*237fead6SMichael Halcrow rc = -ENOMEM; 325*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, 326*237fead6SMichael Halcrow "Could not map page\n"); 327*237fead6SMichael Halcrow goto out; 328*237fead6SMichael Halcrow } 329*237fead6SMichael Halcrow memset((page_virt + end_byte_in_page), 0, 330*237fead6SMichael Halcrow (PAGE_CACHE_SIZE - end_byte_in_page)); 331*237fead6SMichael Halcrow kunmap(page); 332*237fead6SMichael Halcrow } 333*237fead6SMichael Halcrow out: 334*237fead6SMichael Halcrow return rc; 335*237fead6SMichael Halcrow } 336*237fead6SMichael Halcrow 337*237fead6SMichael Halcrow static int ecryptfs_prepare_write(struct file *file, struct page *page, 338*237fead6SMichael Halcrow unsigned from, unsigned to) 339*237fead6SMichael Halcrow { 340*237fead6SMichael Halcrow int rc = 0; 341*237fead6SMichael Halcrow 342*237fead6SMichael Halcrow kmap(page); 343*237fead6SMichael Halcrow if (from == 0 && to == PAGE_CACHE_SIZE) 344*237fead6SMichael Halcrow goto out; /* If we are writing a full page, it will be 345*237fead6SMichael Halcrow up to date. */ 346*237fead6SMichael Halcrow if (!PageUptodate(page)) 347*237fead6SMichael Halcrow rc = ecryptfs_do_readpage(file, page, page->index); 348*237fead6SMichael Halcrow out: 349*237fead6SMichael Halcrow return rc; 350*237fead6SMichael Halcrow } 351*237fead6SMichael Halcrow 352*237fead6SMichael Halcrow int ecryptfs_grab_and_map_lower_page(struct page **lower_page, 353*237fead6SMichael Halcrow char **lower_virt, 354*237fead6SMichael Halcrow struct inode *lower_inode, 355*237fead6SMichael Halcrow unsigned long lower_page_index) 356*237fead6SMichael Halcrow { 357*237fead6SMichael Halcrow int rc = 0; 358*237fead6SMichael Halcrow 359*237fead6SMichael Halcrow (*lower_page) = grab_cache_page(lower_inode->i_mapping, 360*237fead6SMichael Halcrow lower_page_index); 361*237fead6SMichael Halcrow if (!(*lower_page)) { 362*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "grab_cache_page for " 363*237fead6SMichael Halcrow "lower_page_index = [0x%.16x] failed\n", 364*237fead6SMichael Halcrow lower_page_index); 365*237fead6SMichael Halcrow rc = -EINVAL; 366*237fead6SMichael Halcrow goto out; 367*237fead6SMichael Halcrow } 368*237fead6SMichael Halcrow if (lower_virt) 369*237fead6SMichael Halcrow (*lower_virt) = kmap((*lower_page)); 370*237fead6SMichael Halcrow else 371*237fead6SMichael Halcrow kmap((*lower_page)); 372*237fead6SMichael Halcrow out: 373*237fead6SMichael Halcrow return rc; 374*237fead6SMichael Halcrow } 375*237fead6SMichael Halcrow 376*237fead6SMichael Halcrow int ecryptfs_writepage_and_release_lower_page(struct page *lower_page, 377*237fead6SMichael Halcrow struct inode *lower_inode, 378*237fead6SMichael Halcrow struct writeback_control *wbc) 379*237fead6SMichael Halcrow { 380*237fead6SMichael Halcrow int rc = 0; 381*237fead6SMichael Halcrow 382*237fead6SMichael Halcrow rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc); 383*237fead6SMichael Halcrow if (rc) { 384*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); " 385*237fead6SMichael Halcrow "rc = [%d]\n", rc); 386*237fead6SMichael Halcrow goto out; 387*237fead6SMichael Halcrow } 388*237fead6SMichael Halcrow lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; 389*237fead6SMichael Halcrow page_cache_release(lower_page); 390*237fead6SMichael Halcrow out: 391*237fead6SMichael Halcrow return rc; 392*237fead6SMichael Halcrow } 393*237fead6SMichael Halcrow 394*237fead6SMichael Halcrow static void ecryptfs_unmap_and_release_lower_page(struct page *lower_page) 395*237fead6SMichael Halcrow { 396*237fead6SMichael Halcrow kunmap(lower_page); 397*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Unlocking lower page with index = " 398*237fead6SMichael Halcrow "[0x%.16x]\n", lower_page->index); 399*237fead6SMichael Halcrow unlock_page(lower_page); 400*237fead6SMichael Halcrow page_cache_release(lower_page); 401*237fead6SMichael Halcrow } 402*237fead6SMichael Halcrow 403*237fead6SMichael Halcrow /** 404*237fead6SMichael Halcrow * ecryptfs_write_inode_size_to_header 405*237fead6SMichael Halcrow * 406*237fead6SMichael Halcrow * Writes the lower file size to the first 8 bytes of the header. 407*237fead6SMichael Halcrow * 408*237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 409*237fead6SMichael Halcrow */ 410*237fead6SMichael Halcrow int 411*237fead6SMichael Halcrow ecryptfs_write_inode_size_to_header(struct file *lower_file, 412*237fead6SMichael Halcrow struct inode *lower_inode, 413*237fead6SMichael Halcrow struct inode *inode) 414*237fead6SMichael Halcrow { 415*237fead6SMichael Halcrow int rc = 0; 416*237fead6SMichael Halcrow struct page *header_page; 417*237fead6SMichael Halcrow char *header_virt; 418*237fead6SMichael Halcrow const struct address_space_operations *lower_a_ops; 419*237fead6SMichael Halcrow u64 file_size; 420*237fead6SMichael Halcrow 421*237fead6SMichael Halcrow rc = ecryptfs_grab_and_map_lower_page(&header_page, &header_virt, 422*237fead6SMichael Halcrow lower_inode, 0); 423*237fead6SMichael Halcrow if (rc) { 424*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "grab_cache_page for header page " 425*237fead6SMichael Halcrow "failed\n"); 426*237fead6SMichael Halcrow goto out; 427*237fead6SMichael Halcrow } 428*237fead6SMichael Halcrow lower_a_ops = lower_inode->i_mapping->a_ops; 429*237fead6SMichael Halcrow rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8); 430*237fead6SMichael Halcrow file_size = (u64)i_size_read(inode); 431*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size); 432*237fead6SMichael Halcrow file_size = cpu_to_be64(file_size); 433*237fead6SMichael Halcrow memcpy(header_virt, &file_size, sizeof(u64)); 434*237fead6SMichael Halcrow rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8); 435*237fead6SMichael Halcrow if (rc < 0) 436*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error commiting header page " 437*237fead6SMichael Halcrow "write\n"); 438*237fead6SMichael Halcrow ecryptfs_unmap_and_release_lower_page(header_page); 439*237fead6SMichael Halcrow lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; 440*237fead6SMichael Halcrow mark_inode_dirty_sync(inode); 441*237fead6SMichael Halcrow out: 442*237fead6SMichael Halcrow return rc; 443*237fead6SMichael Halcrow } 444*237fead6SMichael Halcrow 445*237fead6SMichael Halcrow int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, 446*237fead6SMichael Halcrow struct file *lower_file, 447*237fead6SMichael Halcrow unsigned long lower_page_index, int byte_offset, 448*237fead6SMichael Halcrow int region_bytes) 449*237fead6SMichael Halcrow { 450*237fead6SMichael Halcrow int rc = 0; 451*237fead6SMichael Halcrow 452*237fead6SMichael Halcrow rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, lower_inode, 453*237fead6SMichael Halcrow lower_page_index); 454*237fead6SMichael Halcrow if (rc) { 455*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to grab and map " 456*237fead6SMichael Halcrow "lower page with index [0x%.16x]\n", 457*237fead6SMichael Halcrow lower_page_index); 458*237fead6SMichael Halcrow goto out; 459*237fead6SMichael Halcrow } 460*237fead6SMichael Halcrow rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file, 461*237fead6SMichael Halcrow (*lower_page), 462*237fead6SMichael Halcrow byte_offset, 463*237fead6SMichael Halcrow region_bytes); 464*237fead6SMichael Halcrow if (rc) { 465*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "prepare_write for " 466*237fead6SMichael Halcrow "lower_page_index = [0x%.16x] failed; rc = " 467*237fead6SMichael Halcrow "[%d]\n", lower_page_index, rc); 468*237fead6SMichael Halcrow } 469*237fead6SMichael Halcrow out: 470*237fead6SMichael Halcrow if (rc && (*lower_page)) { 471*237fead6SMichael Halcrow ecryptfs_unmap_and_release_lower_page(*lower_page); 472*237fead6SMichael Halcrow (*lower_page) = NULL; 473*237fead6SMichael Halcrow } 474*237fead6SMichael Halcrow return rc; 475*237fead6SMichael Halcrow } 476*237fead6SMichael Halcrow 477*237fead6SMichael Halcrow /** 478*237fead6SMichael Halcrow * ecryptfs_commit_lower_page 479*237fead6SMichael Halcrow * 480*237fead6SMichael Halcrow * Returns zero on success; non-zero on error 481*237fead6SMichael Halcrow */ 482*237fead6SMichael Halcrow int 483*237fead6SMichael Halcrow ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode, 484*237fead6SMichael Halcrow struct file *lower_file, int byte_offset, 485*237fead6SMichael Halcrow int region_size) 486*237fead6SMichael Halcrow { 487*237fead6SMichael Halcrow int rc = 0; 488*237fead6SMichael Halcrow 489*237fead6SMichael Halcrow rc = lower_inode->i_mapping->a_ops->commit_write( 490*237fead6SMichael Halcrow lower_file, lower_page, byte_offset, region_size); 491*237fead6SMichael Halcrow if (rc < 0) { 492*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, 493*237fead6SMichael Halcrow "Error committing write; rc = [%d]\n", rc); 494*237fead6SMichael Halcrow } else 495*237fead6SMichael Halcrow rc = 0; 496*237fead6SMichael Halcrow ecryptfs_unmap_and_release_lower_page(lower_page); 497*237fead6SMichael Halcrow return rc; 498*237fead6SMichael Halcrow } 499*237fead6SMichael Halcrow 500*237fead6SMichael Halcrow /** 501*237fead6SMichael Halcrow * ecryptfs_copy_page_to_lower 502*237fead6SMichael Halcrow * 503*237fead6SMichael Halcrow * Used for plaintext pass-through; no page index interpolation 504*237fead6SMichael Halcrow * required. 505*237fead6SMichael Halcrow */ 506*237fead6SMichael Halcrow int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode, 507*237fead6SMichael Halcrow struct file *lower_file) 508*237fead6SMichael Halcrow { 509*237fead6SMichael Halcrow int rc = 0; 510*237fead6SMichael Halcrow struct page *lower_page; 511*237fead6SMichael Halcrow 512*237fead6SMichael Halcrow rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file, 513*237fead6SMichael Halcrow page->index, 0, PAGE_CACHE_SIZE); 514*237fead6SMichael Halcrow if (rc) { 515*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to get page " 516*237fead6SMichael Halcrow "at index [0x%.16x]\n", page->index); 517*237fead6SMichael Halcrow goto out; 518*237fead6SMichael Halcrow } 519*237fead6SMichael Halcrow /* TODO: aops */ 520*237fead6SMichael Halcrow memcpy((char *)page_address(lower_page), page_address(page), 521*237fead6SMichael Halcrow PAGE_CACHE_SIZE); 522*237fead6SMichael Halcrow rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file, 523*237fead6SMichael Halcrow 0, PAGE_CACHE_SIZE); 524*237fead6SMichael Halcrow if (rc) 525*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to commit page " 526*237fead6SMichael Halcrow "at index [0x%.16x]\n", page->index); 527*237fead6SMichael Halcrow out: 528*237fead6SMichael Halcrow return rc; 529*237fead6SMichael Halcrow } 530*237fead6SMichael Halcrow 531*237fead6SMichael Halcrow static int 532*237fead6SMichael Halcrow process_new_file(struct ecryptfs_crypt_stat *crypt_stat, 533*237fead6SMichael Halcrow struct file *file, struct inode *inode) 534*237fead6SMichael Halcrow { 535*237fead6SMichael Halcrow struct page *header_page; 536*237fead6SMichael Halcrow const struct address_space_operations *lower_a_ops; 537*237fead6SMichael Halcrow struct inode *lower_inode; 538*237fead6SMichael Halcrow struct file *lower_file; 539*237fead6SMichael Halcrow char *header_virt; 540*237fead6SMichael Halcrow int rc = 0; 541*237fead6SMichael Halcrow int current_header_page = 0; 542*237fead6SMichael Halcrow int header_pages; 543*237fead6SMichael Halcrow int more_header_data_to_be_written = 1; 544*237fead6SMichael Halcrow 545*237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(inode); 546*237fead6SMichael Halcrow lower_file = ecryptfs_file_to_lower(file); 547*237fead6SMichael Halcrow lower_a_ops = lower_inode->i_mapping->a_ops; 548*237fead6SMichael Halcrow header_pages = ((crypt_stat->header_extent_size 549*237fead6SMichael Halcrow * crypt_stat->num_header_extents_at_front) 550*237fead6SMichael Halcrow / PAGE_CACHE_SIZE); 551*237fead6SMichael Halcrow BUG_ON(header_pages < 1); 552*237fead6SMichael Halcrow while (current_header_page < header_pages) { 553*237fead6SMichael Halcrow rc = ecryptfs_grab_and_map_lower_page(&header_page, 554*237fead6SMichael Halcrow &header_virt, 555*237fead6SMichael Halcrow lower_inode, 556*237fead6SMichael Halcrow current_header_page); 557*237fead6SMichael Halcrow if (rc) { 558*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "grab_cache_page for " 559*237fead6SMichael Halcrow "header page [%d] failed; rc = [%d]\n", 560*237fead6SMichael Halcrow current_header_page, rc); 561*237fead6SMichael Halcrow goto out; 562*237fead6SMichael Halcrow } 563*237fead6SMichael Halcrow rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 564*237fead6SMichael Halcrow PAGE_CACHE_SIZE); 565*237fead6SMichael Halcrow if (rc) { 566*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error preparing to write " 567*237fead6SMichael Halcrow "header page out; rc = [%d]\n", rc); 568*237fead6SMichael Halcrow goto out; 569*237fead6SMichael Halcrow } 570*237fead6SMichael Halcrow memset(header_virt, 0, PAGE_CACHE_SIZE); 571*237fead6SMichael Halcrow if (more_header_data_to_be_written) { 572*237fead6SMichael Halcrow rc = ecryptfs_write_headers_virt(header_virt, 573*237fead6SMichael Halcrow crypt_stat, 574*237fead6SMichael Halcrow file->f_dentry); 575*237fead6SMichael Halcrow if (rc) { 576*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 577*237fead6SMichael Halcrow "generating header; rc = " 578*237fead6SMichael Halcrow "[%d]\n", rc); 579*237fead6SMichael Halcrow rc = -EIO; 580*237fead6SMichael Halcrow memset(header_virt, 0, PAGE_CACHE_SIZE); 581*237fead6SMichael Halcrow ecryptfs_unmap_and_release_lower_page( 582*237fead6SMichael Halcrow header_page); 583*237fead6SMichael Halcrow goto out; 584*237fead6SMichael Halcrow } 585*237fead6SMichael Halcrow if (current_header_page == 0) 586*237fead6SMichael Halcrow memset(header_virt, 0, 8); 587*237fead6SMichael Halcrow more_header_data_to_be_written = 0; 588*237fead6SMichael Halcrow } 589*237fead6SMichael Halcrow rc = lower_a_ops->commit_write(lower_file, header_page, 0, 590*237fead6SMichael Halcrow PAGE_CACHE_SIZE); 591*237fead6SMichael Halcrow ecryptfs_unmap_and_release_lower_page(header_page); 592*237fead6SMichael Halcrow if (rc < 0) { 593*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, 594*237fead6SMichael Halcrow "Error commiting header page write; " 595*237fead6SMichael Halcrow "rc = [%d]\n", rc); 596*237fead6SMichael Halcrow break; 597*237fead6SMichael Halcrow } 598*237fead6SMichael Halcrow current_header_page++; 599*237fead6SMichael Halcrow } 600*237fead6SMichael Halcrow if (rc >= 0) { 601*237fead6SMichael Halcrow rc = 0; 602*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "lower_inode->i_blocks = " 603*237fead6SMichael Halcrow "[0x%.16x]\n", lower_inode->i_blocks); 604*237fead6SMichael Halcrow i_size_write(inode, 0); 605*237fead6SMichael Halcrow lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; 606*237fead6SMichael Halcrow mark_inode_dirty_sync(inode); 607*237fead6SMichael Halcrow } 608*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Clearing ECRYPTFS_NEW_FILE flag in " 609*237fead6SMichael Halcrow "crypt_stat at memory location [%p]\n", crypt_stat); 610*237fead6SMichael Halcrow ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE); 611*237fead6SMichael Halcrow out: 612*237fead6SMichael Halcrow return rc; 613*237fead6SMichael Halcrow } 614*237fead6SMichael Halcrow 615*237fead6SMichael Halcrow /** 616*237fead6SMichael Halcrow * ecryptfs_commit_write 617*237fead6SMichael Halcrow * @file: The eCryptfs file object 618*237fead6SMichael Halcrow * @page: The eCryptfs page 619*237fead6SMichael Halcrow * @from: Ignored (we rotate the page IV on each write) 620*237fead6SMichael Halcrow * @to: Ignored 621*237fead6SMichael Halcrow * 622*237fead6SMichael Halcrow * This is where we encrypt the data and pass the encrypted data to 623*237fead6SMichael Halcrow * the lower filesystem. In OpenPGP-compatible mode, we operate on 624*237fead6SMichael Halcrow * entire underlying packets. 625*237fead6SMichael Halcrow */ 626*237fead6SMichael Halcrow static int ecryptfs_commit_write(struct file *file, struct page *page, 627*237fead6SMichael Halcrow unsigned from, unsigned to) 628*237fead6SMichael Halcrow { 629*237fead6SMichael Halcrow struct ecryptfs_page_crypt_context ctx; 630*237fead6SMichael Halcrow loff_t pos; 631*237fead6SMichael Halcrow struct inode *inode; 632*237fead6SMichael Halcrow struct inode *lower_inode; 633*237fead6SMichael Halcrow struct file *lower_file; 634*237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat; 635*237fead6SMichael Halcrow int rc; 636*237fead6SMichael Halcrow 637*237fead6SMichael Halcrow inode = page->mapping->host; 638*237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(inode); 639*237fead6SMichael Halcrow lower_file = ecryptfs_file_to_lower(file); 640*237fead6SMichael Halcrow mutex_lock(&lower_inode->i_mutex); 641*237fead6SMichael Halcrow crypt_stat = 642*237fead6SMichael Halcrow &ecryptfs_inode_to_private(file->f_dentry->d_inode)->crypt_stat; 643*237fead6SMichael Halcrow if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) { 644*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in " 645*237fead6SMichael Halcrow "crypt_stat at memory location [%p]\n", crypt_stat); 646*237fead6SMichael Halcrow rc = process_new_file(crypt_stat, file, inode); 647*237fead6SMichael Halcrow if (rc) { 648*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error processing new " 649*237fead6SMichael Halcrow "file; rc = [%d]\n", rc); 650*237fead6SMichael Halcrow goto out; 651*237fead6SMichael Halcrow } 652*237fead6SMichael Halcrow } else 653*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Not a new file\n"); 654*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" 655*237fead6SMichael Halcrow "(page w/ index = [0x%.16x], to = [%d])\n", page->index, 656*237fead6SMichael Halcrow to); 657*237fead6SMichael Halcrow rc = fill_zeros_to_end_of_page(page, to); 658*237fead6SMichael Halcrow if (rc) { 659*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error attempting to fill " 660*237fead6SMichael Halcrow "zeros in page with index = [0x%.16x]\n", 661*237fead6SMichael Halcrow page->index); 662*237fead6SMichael Halcrow goto out; 663*237fead6SMichael Halcrow } 664*237fead6SMichael Halcrow ctx.page = page; 665*237fead6SMichael Halcrow ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE; 666*237fead6SMichael Halcrow ctx.param.lower_file = lower_file; 667*237fead6SMichael Halcrow rc = ecryptfs_encrypt_page(&ctx); 668*237fead6SMichael Halcrow if (rc) { 669*237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " 670*237fead6SMichael Halcrow "index [0x%.16x])\n", page->index); 671*237fead6SMichael Halcrow goto out; 672*237fead6SMichael Halcrow } 673*237fead6SMichael Halcrow rc = 0; 674*237fead6SMichael Halcrow inode->i_blocks = lower_inode->i_blocks; 675*237fead6SMichael Halcrow pos = (page->index << PAGE_CACHE_SHIFT) + to; 676*237fead6SMichael Halcrow if (pos > i_size_read(inode)) { 677*237fead6SMichael Halcrow i_size_write(inode, pos); 678*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Expanded file size to " 679*237fead6SMichael Halcrow "[0x%.16x]\n", i_size_read(inode)); 680*237fead6SMichael Halcrow } 681*237fead6SMichael Halcrow ecryptfs_write_inode_size_to_header(lower_file, lower_inode, inode); 682*237fead6SMichael Halcrow lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME; 683*237fead6SMichael Halcrow mark_inode_dirty_sync(inode); 684*237fead6SMichael Halcrow out: 685*237fead6SMichael Halcrow kunmap(page); /* mapped in prior call (prepare_write) */ 686*237fead6SMichael Halcrow if (rc < 0) 687*237fead6SMichael Halcrow ClearPageUptodate(page); 688*237fead6SMichael Halcrow else 689*237fead6SMichael Halcrow SetPageUptodate(page); 690*237fead6SMichael Halcrow mutex_unlock(&lower_inode->i_mutex); 691*237fead6SMichael Halcrow return rc; 692*237fead6SMichael Halcrow } 693*237fead6SMichael Halcrow 694*237fead6SMichael Halcrow /** 695*237fead6SMichael Halcrow * write_zeros 696*237fead6SMichael Halcrow * @file: The ecryptfs file 697*237fead6SMichael Halcrow * @index: The index in which we are writing 698*237fead6SMichael Halcrow * @start: The position after the last block of data 699*237fead6SMichael Halcrow * @num_zeros: The number of zeros to write 700*237fead6SMichael Halcrow * 701*237fead6SMichael Halcrow * Write a specified number of zero's to a page. 702*237fead6SMichael Halcrow * 703*237fead6SMichael Halcrow * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE 704*237fead6SMichael Halcrow */ 705*237fead6SMichael Halcrow static 706*237fead6SMichael Halcrow int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros) 707*237fead6SMichael Halcrow { 708*237fead6SMichael Halcrow int rc = 0; 709*237fead6SMichael Halcrow struct page *tmp_page; 710*237fead6SMichael Halcrow 711*237fead6SMichael Halcrow tmp_page = ecryptfs_get1page(file, index); 712*237fead6SMichael Halcrow if (IS_ERR(tmp_page)) { 713*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error getting page at index " 714*237fead6SMichael Halcrow "[0x%.16x]\n", index); 715*237fead6SMichael Halcrow rc = PTR_ERR(tmp_page); 716*237fead6SMichael Halcrow goto out; 717*237fead6SMichael Halcrow } 718*237fead6SMichael Halcrow kmap(tmp_page); 719*237fead6SMichael Halcrow rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros); 720*237fead6SMichael Halcrow if (rc) { 721*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error preparing to write zero's " 722*237fead6SMichael Halcrow "to remainder of page at index [0x%.16x]\n", 723*237fead6SMichael Halcrow index); 724*237fead6SMichael Halcrow kunmap(tmp_page); 725*237fead6SMichael Halcrow page_cache_release(tmp_page); 726*237fead6SMichael Halcrow goto out; 727*237fead6SMichael Halcrow } 728*237fead6SMichael Halcrow memset(((char *)page_address(tmp_page) + start), 0, num_zeros); 729*237fead6SMichael Halcrow rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros); 730*237fead6SMichael Halcrow if (rc < 0) { 731*237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error attempting to write zero's " 732*237fead6SMichael Halcrow "to remainder of page at index [0x%.16x]\n", 733*237fead6SMichael Halcrow index); 734*237fead6SMichael Halcrow kunmap(tmp_page); 735*237fead6SMichael Halcrow page_cache_release(tmp_page); 736*237fead6SMichael Halcrow goto out; 737*237fead6SMichael Halcrow } 738*237fead6SMichael Halcrow rc = 0; 739*237fead6SMichael Halcrow kunmap(tmp_page); 740*237fead6SMichael Halcrow page_cache_release(tmp_page); 741*237fead6SMichael Halcrow out: 742*237fead6SMichael Halcrow return rc; 743*237fead6SMichael Halcrow } 744*237fead6SMichael Halcrow 745*237fead6SMichael Halcrow static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) 746*237fead6SMichael Halcrow { 747*237fead6SMichael Halcrow int rc = 0; 748*237fead6SMichael Halcrow struct inode *inode; 749*237fead6SMichael Halcrow struct inode *lower_inode; 750*237fead6SMichael Halcrow 751*237fead6SMichael Halcrow inode = (struct inode *)mapping->host; 752*237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(inode); 753*237fead6SMichael Halcrow if (lower_inode->i_mapping->a_ops->bmap) 754*237fead6SMichael Halcrow rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, 755*237fead6SMichael Halcrow block); 756*237fead6SMichael Halcrow return rc; 757*237fead6SMichael Halcrow } 758*237fead6SMichael Halcrow 759*237fead6SMichael Halcrow static void ecryptfs_sync_page(struct page *page) 760*237fead6SMichael Halcrow { 761*237fead6SMichael Halcrow struct inode *inode; 762*237fead6SMichael Halcrow struct inode *lower_inode; 763*237fead6SMichael Halcrow struct page *lower_page; 764*237fead6SMichael Halcrow 765*237fead6SMichael Halcrow inode = page->mapping->host; 766*237fead6SMichael Halcrow lower_inode = ecryptfs_inode_to_lower(inode); 767*237fead6SMichael Halcrow /* NOTE: Recently swapped with grab_cache_page(), since 768*237fead6SMichael Halcrow * sync_page() just makes sure that pending I/O gets done. */ 769*237fead6SMichael Halcrow lower_page = find_lock_page(lower_inode->i_mapping, page->index); 770*237fead6SMichael Halcrow if (!lower_page) { 771*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n"); 772*237fead6SMichael Halcrow return; 773*237fead6SMichael Halcrow } 774*237fead6SMichael Halcrow lower_page->mapping->a_ops->sync_page(lower_page); 775*237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n", 776*237fead6SMichael Halcrow lower_page->index); 777*237fead6SMichael Halcrow unlock_page(lower_page); 778*237fead6SMichael Halcrow page_cache_release(lower_page); 779*237fead6SMichael Halcrow } 780*237fead6SMichael Halcrow 781*237fead6SMichael Halcrow struct address_space_operations ecryptfs_aops = { 782*237fead6SMichael Halcrow .writepage = ecryptfs_writepage, 783*237fead6SMichael Halcrow .readpage = ecryptfs_readpage, 784*237fead6SMichael Halcrow .prepare_write = ecryptfs_prepare_write, 785*237fead6SMichael Halcrow .commit_write = ecryptfs_commit_write, 786*237fead6SMichael Halcrow .bmap = ecryptfs_bmap, 787*237fead6SMichael Halcrow .sync_page = ecryptfs_sync_page, 788*237fead6SMichael Halcrow }; 789