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