aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Halcrow <mhalcrow@us.ibm.com>2007-10-16 04:28:08 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-10-16 12:43:12 -0400
commit0216f7f7921759211e48e8b940eae29f0fe43902 (patch)
tree44999b1dfb49944bfd83881c8d9e0cbe3a90e2b7
parentda0102a10aed2244d8fc34f289e81e502622b81e (diff)
eCryptfs: replace encrypt, decrypt, and inode size write
Replace page encryption and decryption routines and inode size write routine with versions that utilize the read_write.c functions. Signed-off-by: Michael Halcrow <mhalcrow@us.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--fs/ecryptfs/crypto.c427
-rw-r--r--fs/ecryptfs/ecryptfs_kernel.h14
-rw-r--r--fs/ecryptfs/inode.c12
-rw-r--r--fs/ecryptfs/mmap.c131
-rw-r--r--fs/ecryptfs/read_write.c12
5 files changed, 290 insertions, 306 deletions
diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c
index e3d2118fafad..7f788ea5da2b 100644
--- a/fs/ecryptfs/crypto.c
+++ b/fs/ecryptfs/crypto.c
@@ -467,8 +467,91 @@ out:
467} 467}
468 468
469/** 469/**
470 * ecryptfs_lower_offset_for_extent
471 *
472 * Convert an eCryptfs page index into a lower byte offset
473 */
474void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
475 struct ecryptfs_crypt_stat *crypt_stat)
476{
477 (*offset) = ((crypt_stat->extent_size
478 * crypt_stat->num_header_extents_at_front)
479 + (crypt_stat->extent_size * extent_num));
480}
481
482/**
483 * ecryptfs_encrypt_extent
484 * @enc_extent_page: Allocated page into which to encrypt the data in
485 * @page
486 * @crypt_stat: crypt_stat containing cryptographic context for the
487 * encryption operation
488 * @page: Page containing plaintext data extent to encrypt
489 * @extent_offset: Page extent offset for use in generating IV
490 *
491 * Encrypts one extent of data.
492 *
493 * Return zero on success; non-zero otherwise
494 */
495static int ecryptfs_encrypt_extent(struct page *enc_extent_page,
496 struct ecryptfs_crypt_stat *crypt_stat,
497 struct page *page,
498 unsigned long extent_offset)
499{
500 unsigned long extent_base;
501 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
502 int rc;
503
504 extent_base = (page->index
505 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
506 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
507 (extent_base + extent_offset));
508 if (rc) {
509 ecryptfs_printk(KERN_ERR, "Error attempting to "
510 "derive IV for extent [0x%.16x]; "
511 "rc = [%d]\n", (extent_base + extent_offset),
512 rc);
513 goto out;
514 }
515 if (unlikely(ecryptfs_verbosity > 0)) {
516 ecryptfs_printk(KERN_DEBUG, "Encrypting extent "
517 "with iv:\n");
518 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
519 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
520 "encryption:\n");
521 ecryptfs_dump_hex((char *)
522 (page_address(page)
523 + (extent_offset * crypt_stat->extent_size)),
524 8);
525 }
526 rc = ecryptfs_encrypt_page_offset(crypt_stat, enc_extent_page, 0,
527 page, (extent_offset
528 * crypt_stat->extent_size),
529 crypt_stat->extent_size, extent_iv);
530 if (rc < 0) {
531 printk(KERN_ERR "%s: Error attempting to encrypt page with "
532 "page->index = [%ld], extent_offset = [%ld]; "
533 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
534 rc);
535 goto out;
536 }
537 rc = 0;
538 if (unlikely(ecryptfs_verbosity > 0)) {
539 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
540 "rc = [%d]\n", (extent_base + extent_offset),
541 rc);
542 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
543 "encryption:\n");
544 ecryptfs_dump_hex((char *)(page_address(enc_extent_page)), 8);
545 }
546out:
547 return rc;
548}
549
550/**
470 * ecryptfs_encrypt_page 551 * ecryptfs_encrypt_page
471 * @ctx: The context of the page 552 * @page: Page mapped from the eCryptfs inode for the file; contains
553 * decrypted content that needs to be encrypted (to a temporary
554 * page; not in place) and written out to the lower file
472 * 555 *
473 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note 556 * Encrypt an eCryptfs page. This is done on a per-extent basis. Note
474 * that eCryptfs pages may straddle the lower pages -- for instance, 557 * that eCryptfs pages may straddle the lower pages -- for instance,
@@ -478,128 +561,121 @@ out:
478 * file, 24K of page 0 of the lower file will be read and decrypted, 561 * file, 24K of page 0 of the lower file will be read and decrypted,
479 * and then 8K of page 1 of the lower file will be read and decrypted. 562 * and then 8K of page 1 of the lower file will be read and decrypted.
480 * 563 *
481 * The actual operations performed on each page depends on the
482 * contents of the ecryptfs_page_crypt_context struct.
483 *
484 * Returns zero on success; negative on error 564 * Returns zero on success; negative on error
485 */ 565 */
486int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx) 566int ecryptfs_encrypt_page(struct page *page)
487{ 567{
488 char extent_iv[ECRYPTFS_MAX_IV_BYTES]; 568 struct inode *ecryptfs_inode;
489 unsigned long base_extent;
490 unsigned long extent_offset = 0;
491 unsigned long lower_page_idx = 0;
492 unsigned long prior_lower_page_idx = 0;
493 struct page *lower_page;
494 struct inode *lower_inode;
495 struct ecryptfs_inode_info *inode_info;
496 struct ecryptfs_crypt_stat *crypt_stat; 569 struct ecryptfs_crypt_stat *crypt_stat;
570 char *enc_extent_virt = NULL;
571 struct page *enc_extent_page;
572 loff_t extent_offset;
497 int rc = 0; 573 int rc = 0;
498 int lower_byte_offset = 0; 574
499 int orig_byte_offset = 0; 575 ecryptfs_inode = page->mapping->host;
500 int num_extents_per_page; 576 crypt_stat =
501#define ECRYPTFS_PAGE_STATE_UNREAD 0 577 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
502#define ECRYPTFS_PAGE_STATE_READ 1
503#define ECRYPTFS_PAGE_STATE_MODIFIED 2
504#define ECRYPTFS_PAGE_STATE_WRITTEN 3
505 int page_state;
506
507 lower_inode = ecryptfs_inode_to_lower(ctx->page->mapping->host);
508 inode_info = ecryptfs_inode_to_private(ctx->page->mapping->host);
509 crypt_stat = &inode_info->crypt_stat;
510 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 578 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
511 rc = ecryptfs_copy_page_to_lower(ctx->page, lower_inode, 579 rc = ecryptfs_write_lower_page_segment(ecryptfs_inode, page,
512 ctx->param.lower_file); 580 0, PAGE_CACHE_SIZE);
513 if (rc) 581 if (rc)
514 ecryptfs_printk(KERN_ERR, "Error attempting to copy " 582 printk(KERN_ERR "%s: Error attempting to copy "
515 "page at index [0x%.16x]\n", 583 "page at index [%ld]\n", __FUNCTION__,
516 ctx->page->index); 584 page->index);
517 goto out; 585 goto out;
518 } 586 }
519 num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size; 587 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
520 base_extent = (ctx->page->index * num_extents_per_page); 588 if (!enc_extent_virt) {
521 page_state = ECRYPTFS_PAGE_STATE_UNREAD; 589 rc = -ENOMEM;
522 while (extent_offset < num_extents_per_page) { 590 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
523 ecryptfs_extent_to_lwr_pg_idx_and_offset( 591 "encrypted extent\n");
524 &lower_page_idx, &lower_byte_offset, crypt_stat, 592 goto out;
525 (base_extent + extent_offset)); 593 }
526 if (prior_lower_page_idx != lower_page_idx 594 enc_extent_page = virt_to_page(enc_extent_virt);
527 && page_state == ECRYPTFS_PAGE_STATE_MODIFIED) { 595 for (extent_offset = 0;
528 rc = ecryptfs_write_out_page(ctx, lower_page, 596 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
529 lower_inode, 597 extent_offset++) {
530 orig_byte_offset, 598 loff_t offset;
531 (PAGE_CACHE_SIZE 599
532 - orig_byte_offset)); 600 rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page,
533 if (rc) { 601 extent_offset);
534 ecryptfs_printk(KERN_ERR, "Error attempting "
535 "to write out page; rc = [%d]"
536 "\n", rc);
537 goto out;
538 }
539 page_state = ECRYPTFS_PAGE_STATE_WRITTEN;
540 }
541 if (page_state == ECRYPTFS_PAGE_STATE_UNREAD
542 || page_state == ECRYPTFS_PAGE_STATE_WRITTEN) {
543 rc = ecryptfs_read_in_page(ctx, &lower_page,
544 lower_inode, lower_page_idx,
545 lower_byte_offset);
546 if (rc) {
547 ecryptfs_printk(KERN_ERR, "Error attempting "
548 "to read in lower page with "
549 "index [0x%.16x]; rc = [%d]\n",
550 lower_page_idx, rc);
551 goto out;
552 }
553 orig_byte_offset = lower_byte_offset;
554 prior_lower_page_idx = lower_page_idx;
555 page_state = ECRYPTFS_PAGE_STATE_READ;
556 }
557 BUG_ON(!(page_state == ECRYPTFS_PAGE_STATE_MODIFIED
558 || page_state == ECRYPTFS_PAGE_STATE_READ));
559 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
560 (base_extent + extent_offset));
561 if (rc) { 602 if (rc) {
562 ecryptfs_printk(KERN_ERR, "Error attempting to " 603 printk(KERN_ERR "%s: Error encrypting extent; "
563 "derive IV for extent [0x%.16x]; " 604 "rc = [%d]\n", __FUNCTION__, rc);
564 "rc = [%d]\n",
565 (base_extent + extent_offset), rc);
566 goto out; 605 goto out;
567 } 606 }
568 if (unlikely(ecryptfs_verbosity > 0)) { 607 ecryptfs_lower_offset_for_extent(
569 ecryptfs_printk(KERN_DEBUG, "Encrypting extent " 608 &offset, ((page->index * (PAGE_CACHE_SIZE
570 "with iv:\n"); 609 / crypt_stat->extent_size))
571 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes); 610 + extent_offset), crypt_stat);
572 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before " 611 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt,
573 "encryption:\n"); 612 offset, crypt_stat->extent_size);
574 ecryptfs_dump_hex((char *) 613 if (rc) {
575 (page_address(ctx->page) 614 ecryptfs_printk(KERN_ERR, "Error attempting "
576 + (extent_offset 615 "to write lower page; rc = [%d]"
577 * crypt_stat->extent_size)), 8); 616 "\n", rc);
578 } 617 goto out;
579 rc = ecryptfs_encrypt_page_offset(
580 crypt_stat, lower_page, lower_byte_offset, ctx->page,
581 (extent_offset * crypt_stat->extent_size),
582 crypt_stat->extent_size, extent_iv);
583 ecryptfs_printk(KERN_DEBUG, "Encrypt extent [0x%.16x]; "
584 "rc = [%d]\n",
585 (base_extent + extent_offset), rc);
586 if (unlikely(ecryptfs_verbosity > 0)) {
587 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
588 "encryption:\n");
589 ecryptfs_dump_hex((char *)(page_address(lower_page)
590 + lower_byte_offset), 8);
591 } 618 }
592 page_state = ECRYPTFS_PAGE_STATE_MODIFIED;
593 extent_offset++; 619 extent_offset++;
594 } 620 }
595 BUG_ON(orig_byte_offset != 0); 621out:
596 rc = ecryptfs_write_out_page(ctx, lower_page, lower_inode, 0, 622 kfree(enc_extent_virt);
597 (lower_byte_offset 623 return rc;
598 + crypt_stat->extent_size)); 624}
625
626static int ecryptfs_decrypt_extent(struct page *page,
627 struct ecryptfs_crypt_stat *crypt_stat,
628 struct page *enc_extent_page,
629 unsigned long extent_offset)
630{
631 unsigned long extent_base;
632 char extent_iv[ECRYPTFS_MAX_IV_BYTES];
633 int rc;
634
635 extent_base = (page->index
636 * (PAGE_CACHE_SIZE / crypt_stat->extent_size));
637 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
638 (extent_base + extent_offset));
599 if (rc) { 639 if (rc) {
600 ecryptfs_printk(KERN_ERR, "Error attempting to write out " 640 ecryptfs_printk(KERN_ERR, "Error attempting to "
601 "page; rc = [%d]\n", rc); 641 "derive IV for extent [0x%.16x]; "
602 goto out; 642 "rc = [%d]\n", (extent_base + extent_offset),
643 rc);
644 goto out;
645 }
646 if (unlikely(ecryptfs_verbosity > 0)) {
647 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
648 "with iv:\n");
649 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
650 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
651 "decryption:\n");
652 ecryptfs_dump_hex((char *)
653 (page_address(enc_extent_page)
654 + (extent_offset * crypt_stat->extent_size)),
655 8);
656 }
657 rc = ecryptfs_decrypt_page_offset(crypt_stat, page,
658 (extent_offset
659 * crypt_stat->extent_size),
660 enc_extent_page, 0,
661 crypt_stat->extent_size, extent_iv);
662 if (rc < 0) {
663 printk(KERN_ERR "%s: Error attempting to decrypt to page with "
664 "page->index = [%ld], extent_offset = [%ld]; "
665 "rc = [%d]\n", __FUNCTION__, page->index, extent_offset,
666 rc);
667 goto out;
668 }
669 rc = 0;
670 if (unlikely(ecryptfs_verbosity > 0)) {
671 ecryptfs_printk(KERN_DEBUG, "Decrypt extent [0x%.16x]; "
672 "rc = [%d]\n", (extent_base + extent_offset),
673 rc);
674 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
675 "decryption:\n");
676 ecryptfs_dump_hex((char *)(page_address(page)
677 + (extent_offset
678 * crypt_stat->extent_size)), 8);
603 } 679 }
604out: 680out:
605 return rc; 681 return rc;
@@ -607,8 +683,9 @@ out:
607 683
608/** 684/**
609 * ecryptfs_decrypt_page 685 * ecryptfs_decrypt_page
610 * @file: The ecryptfs file 686 * @page: Page mapped from the eCryptfs inode for the file; data read
611 * @page: The page in ecryptfs to decrypt 687 * and decrypted from the lower file will be written into this
688 * page
612 * 689 *
613 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note 690 * Decrypt an eCryptfs page. This is done on a per-extent basis. Note
614 * that eCryptfs pages may straddle the lower pages -- for instance, 691 * that eCryptfs pages may straddle the lower pages -- for instance,
@@ -620,103 +697,69 @@ out:
620 * 697 *
621 * Returns zero on success; negative on error 698 * Returns zero on success; negative on error
622 */ 699 */
623int ecryptfs_decrypt_page(struct file *file, struct page *page) 700int ecryptfs_decrypt_page(struct page *page)
624{ 701{
625 char extent_iv[ECRYPTFS_MAX_IV_BYTES]; 702 struct inode *ecryptfs_inode;
626 unsigned long base_extent;
627 unsigned long extent_offset = 0;
628 unsigned long lower_page_idx = 0;
629 unsigned long prior_lower_page_idx = 0;
630 struct page *lower_page;
631 char *lower_page_virt = NULL;
632 struct inode *lower_inode;
633 struct ecryptfs_crypt_stat *crypt_stat; 703 struct ecryptfs_crypt_stat *crypt_stat;
704 char *enc_extent_virt = NULL;
705 struct page *enc_extent_page;
706 unsigned long extent_offset;
634 int rc = 0; 707 int rc = 0;
635 int byte_offset;
636 int num_extents_per_page;
637 int page_state;
638 708
639 crypt_stat = &(ecryptfs_inode_to_private( 709 ecryptfs_inode = page->mapping->host;
640 page->mapping->host)->crypt_stat); 710 crypt_stat =
641 lower_inode = ecryptfs_inode_to_lower(page->mapping->host); 711 &(ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat);
642 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 712 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
643 rc = ecryptfs_do_readpage(file, page, page->index); 713 rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
714 PAGE_CACHE_SIZE,
715 ecryptfs_inode);
644 if (rc) 716 if (rc)
645 ecryptfs_printk(KERN_ERR, "Error attempting to copy " 717 printk(KERN_ERR "%s: Error attempting to copy "
646 "page at index [0x%.16x]\n", 718 "page at index [%ld]\n", __FUNCTION__,
647 page->index); 719 page->index);
648 goto out; 720 goto out_clear_uptodate;
649 } 721 }
650 num_extents_per_page = PAGE_CACHE_SIZE / crypt_stat->extent_size; 722 enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER);
651 base_extent = (page->index * num_extents_per_page); 723 if (!enc_extent_virt) {
652 lower_page_virt = kmem_cache_alloc(ecryptfs_lower_page_cache,
653 GFP_KERNEL);
654 if (!lower_page_virt) {
655 rc = -ENOMEM; 724 rc = -ENOMEM;
656 ecryptfs_printk(KERN_ERR, "Error getting page for encrypted " 725 ecryptfs_printk(KERN_ERR, "Error allocating memory for "
657 "lower page(s)\n"); 726 "encrypted extent\n");
658 goto out; 727 goto out_clear_uptodate;
659 } 728 }
660 lower_page = virt_to_page(lower_page_virt); 729 enc_extent_page = virt_to_page(enc_extent_virt);
661 page_state = ECRYPTFS_PAGE_STATE_UNREAD; 730 for (extent_offset = 0;
662 while (extent_offset < num_extents_per_page) { 731 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size);
663 ecryptfs_extent_to_lwr_pg_idx_and_offset( 732 extent_offset++) {
664 &lower_page_idx, &byte_offset, crypt_stat, 733 loff_t offset;
665 (base_extent + extent_offset)); 734
666 if (prior_lower_page_idx != lower_page_idx 735 ecryptfs_lower_offset_for_extent(
667 || page_state == ECRYPTFS_PAGE_STATE_UNREAD) { 736 &offset, ((page->index * (PAGE_CACHE_SIZE
668 rc = ecryptfs_do_readpage(file, lower_page, 737 / crypt_stat->extent_size))
669 lower_page_idx); 738 + extent_offset), crypt_stat);
670 if (rc) { 739 rc = ecryptfs_read_lower(enc_extent_virt, offset,
671 ecryptfs_printk(KERN_ERR, "Error reading " 740 crypt_stat->extent_size,
672 "lower encrypted page; rc = " 741 ecryptfs_inode);
673 "[%d]\n", rc);
674 goto out;
675 }
676 prior_lower_page_idx = lower_page_idx;
677 page_state = ECRYPTFS_PAGE_STATE_READ;
678 }
679 rc = ecryptfs_derive_iv(extent_iv, crypt_stat,
680 (base_extent + extent_offset));
681 if (rc) { 742 if (rc) {
682 ecryptfs_printk(KERN_ERR, "Error attempting to " 743 ecryptfs_printk(KERN_ERR, "Error attempting "
683 "derive IV for extent [0x%.16x]; rc = " 744 "to read lower page; rc = [%d]"
684 "[%d]\n", 745 "\n", rc);
685 (base_extent + extent_offset), rc); 746 goto out_clear_uptodate;
686 goto out;
687 }
688 if (unlikely(ecryptfs_verbosity > 0)) {
689 ecryptfs_printk(KERN_DEBUG, "Decrypting extent "
690 "with iv:\n");
691 ecryptfs_dump_hex(extent_iv, crypt_stat->iv_bytes);
692 ecryptfs_printk(KERN_DEBUG, "First 8 bytes before "
693 "decryption:\n");
694 ecryptfs_dump_hex((lower_page_virt + byte_offset), 8);
695 } 747 }
696 rc = ecryptfs_decrypt_page_offset(crypt_stat, page, 748 rc = ecryptfs_decrypt_extent(page, crypt_stat, enc_extent_page,
697 (extent_offset 749 extent_offset);
698 * crypt_stat->extent_size), 750 if (rc) {
699 lower_page, byte_offset, 751 printk(KERN_ERR "%s: Error encrypting extent; "
700 crypt_stat->extent_size, 752 "rc = [%d]\n", __FUNCTION__, rc);
701 extent_iv); 753 goto out_clear_uptodate;
702 if (rc != crypt_stat->extent_size) {
703 ecryptfs_printk(KERN_ERR, "Error attempting to "
704 "decrypt extent [0x%.16x]\n",
705 (base_extent + extent_offset));
706 goto out;
707 }
708 rc = 0;
709 if (unlikely(ecryptfs_verbosity > 0)) {
710 ecryptfs_printk(KERN_DEBUG, "First 8 bytes after "
711 "decryption:\n");
712 ecryptfs_dump_hex((char *)(page_address(page)
713 + byte_offset), 8);
714 } 754 }
715 extent_offset++; 755 extent_offset++;
716 } 756 }
757 SetPageUptodate(page);
758 goto out;
759out_clear_uptodate:
760 ClearPageUptodate(page);
717out: 761out:
718 if (lower_page_virt) 762 kfree(enc_extent_virt);
719 kmem_cache_free(ecryptfs_lower_page_cache, lower_page_virt);
720 return rc; 763 return rc;
721} 764}
722 765
diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h
index e6a68a87b5df..65f7ddfd4d4a 100644
--- a/fs/ecryptfs/ecryptfs_kernel.h
+++ b/fs/ecryptfs/ecryptfs_kernel.h
@@ -552,13 +552,7 @@ void ecryptfs_destroy_crypt_stat(struct ecryptfs_crypt_stat *crypt_stat);
552void ecryptfs_destroy_mount_crypt_stat( 552void ecryptfs_destroy_mount_crypt_stat(
553 struct ecryptfs_mount_crypt_stat *mount_crypt_stat); 553 struct ecryptfs_mount_crypt_stat *mount_crypt_stat);
554int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat); 554int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat);
555#define ECRYPTFS_LOWER_I_MUTEX_NOT_HELD 0 555int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptefs_inode);
556#define ECRYPTFS_LOWER_I_MUTEX_HELD 1
557int ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
558 struct inode *lower_inode,
559 struct inode *inode,
560 struct dentry *ecryptfs_dentry,
561 int lower_i_mutex_held);
562int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, 556int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
563 struct file *lower_file, 557 struct file *lower_file,
564 unsigned long lower_page_index, int byte_offset, 558 unsigned long lower_page_index, int byte_offset,
@@ -574,8 +568,8 @@ int ecryptfs_do_readpage(struct file *file, struct page *page,
574int ecryptfs_writepage_and_release_lower_page(struct page *lower_page, 568int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
575 struct inode *lower_inode, 569 struct inode *lower_inode,
576 struct writeback_control *wbc); 570 struct writeback_control *wbc);
577int ecryptfs_encrypt_page(struct ecryptfs_page_crypt_context *ctx); 571int ecryptfs_encrypt_page(struct page *page);
578int ecryptfs_decrypt_page(struct file *file, struct page *page); 572int ecryptfs_decrypt_page(struct page *page);
579int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry, 573int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
580 struct file *lower_file); 574 struct file *lower_file);
581int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry, 575int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry,
@@ -655,6 +649,8 @@ int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
655 char *sig); 649 char *sig);
656int ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, 650int ecryptfs_write_zeros(struct file *file, pgoff_t index, int start,
657 int num_zeros); 651 int num_zeros);
652void ecryptfs_lower_offset_for_extent(loff_t *offset, loff_t extent_num,
653 struct ecryptfs_crypt_stat *crypt_stat);
658int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data, 654int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
659 loff_t offset, size_t size); 655 loff_t offset, size_t size);
660int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode, 656int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
index d70f5994ba51..7192a810bbe6 100644
--- a/fs/ecryptfs/inode.c
+++ b/fs/ecryptfs/inode.c
@@ -168,9 +168,7 @@ static int grow_file(struct dentry *ecryptfs_dentry, struct file *lower_file,
168 goto out; 168 goto out;
169 } 169 }
170 i_size_write(inode, 0); 170 i_size_write(inode, 0);
171 rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode, 171 rc = ecryptfs_write_inode_size_to_metadata(inode);
172 inode, ecryptfs_dentry,
173 ECRYPTFS_LOWER_I_MUTEX_NOT_HELD);
174 ecryptfs_inode_to_private(inode)->crypt_stat.flags |= ECRYPTFS_NEW_FILE; 172 ecryptfs_inode_to_private(inode)->crypt_stat.flags |= ECRYPTFS_NEW_FILE;
175out: 173out:
176 return rc; 174 return rc;
@@ -798,9 +796,7 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
798 goto out_fput; 796 goto out_fput;
799 } 797 }
800 i_size_write(inode, new_length); 798 i_size_write(inode, new_length);
801 rc = ecryptfs_write_inode_size_to_metadata( 799 rc = ecryptfs_write_inode_size_to_metadata(inode);
802 lower_file, lower_dentry->d_inode, inode, dentry,
803 ECRYPTFS_LOWER_I_MUTEX_NOT_HELD);
804 if (rc) { 800 if (rc) {
805 printk(KERN_ERR "Problem with " 801 printk(KERN_ERR "Problem with "
806 "ecryptfs_write_inode_size_to_metadata; " 802 "ecryptfs_write_inode_size_to_metadata; "
@@ -829,9 +825,7 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
829 } 825 }
830 } 826 }
831 vmtruncate(inode, new_length); 827 vmtruncate(inode, new_length);
832 rc = ecryptfs_write_inode_size_to_metadata( 828 rc = ecryptfs_write_inode_size_to_metadata(inode);
833 lower_file, lower_dentry->d_inode, inode, dentry,
834 ECRYPTFS_LOWER_I_MUTEX_NOT_HELD);
835 if (rc) { 829 if (rc) {
836 printk(KERN_ERR "Problem with " 830 printk(KERN_ERR "Problem with "
837 "ecryptfs_write_inode_size_to_metadata; " 831 "ecryptfs_write_inode_size_to_metadata; "
diff --git a/fs/ecryptfs/mmap.c b/fs/ecryptfs/mmap.c
index 0c53320b9eb8..9b621108d5c1 100644
--- a/fs/ecryptfs/mmap.c
+++ b/fs/ecryptfs/mmap.c
@@ -171,13 +171,9 @@ out:
171 */ 171 */
172static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) 172static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
173{ 173{
174 struct ecryptfs_page_crypt_context ctx;
175 int rc; 174 int rc;
176 175
177 ctx.page = page; 176 rc = ecryptfs_encrypt_page(page);
178 ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
179 ctx.param.wbc = wbc;
180 rc = ecryptfs_encrypt_page(&ctx);
181 if (rc) { 177 if (rc) {
182 ecryptfs_printk(KERN_WARNING, "Error encrypting " 178 ecryptfs_printk(KERN_WARNING, "Error encrypting "
183 "page (upper index [0x%.16x])\n", page->index); 179 "page (upper index [0x%.16x])\n", page->index);
@@ -341,7 +337,7 @@ static int ecryptfs_readpage(struct file *file, struct page *page)
341 } 337 }
342 } 338 }
343 } else { 339 } else {
344 rc = ecryptfs_decrypt_page(file, page); 340 rc = ecryptfs_decrypt_page(page);
345 if (rc) { 341 if (rc) {
346 ecryptfs_printk(KERN_ERR, "Error decrypting page; " 342 ecryptfs_printk(KERN_ERR, "Error decrypting page; "
347 "rc = [%d]\n", rc); 343 "rc = [%d]\n", rc);
@@ -459,58 +455,48 @@ static void ecryptfs_release_lower_page(struct page *lower_page)
459 * 455 *
460 * Returns zero on success; non-zero on error. 456 * Returns zero on success; non-zero on error.
461 */ 457 */
462static int ecryptfs_write_inode_size_to_header(struct file *lower_file, 458static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
463 struct inode *lower_inode,
464 struct inode *inode)
465{ 459{
466 int rc = 0;
467 struct page *header_page;
468 char *header_virt;
469 const struct address_space_operations *lower_a_ops;
470 u64 file_size; 460 u64 file_size;
461 char *file_size_virt;
462 int rc;
471 463
472 header_page = grab_cache_page(lower_inode->i_mapping, 0); 464 file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
473 if (!header_page) { 465 if (!file_size_virt) {
474 ecryptfs_printk(KERN_ERR, "grab_cache_page for " 466 rc = -ENOMEM;
475 "lower_page_index 0 failed\n");
476 rc = -EINVAL;
477 goto out;
478 }
479 lower_a_ops = lower_inode->i_mapping->a_ops;
480 rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
481 if (rc) {
482 ecryptfs_release_lower_page(header_page);
483 goto out; 467 goto out;
484 } 468 }
485 file_size = (u64)i_size_read(inode); 469 file_size = (u64)i_size_read(ecryptfs_inode);
486 ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
487 file_size = cpu_to_be64(file_size); 470 file_size = cpu_to_be64(file_size);
488 header_virt = kmap_atomic(header_page, KM_USER0); 471 memcpy(file_size_virt, &file_size, sizeof(u64));
489 memcpy(header_virt, &file_size, sizeof(u64)); 472 rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
490 kunmap_atomic(header_virt, KM_USER0); 473 sizeof(u64));
491 flush_dcache_page(header_page); 474 kfree(file_size_virt);
492 rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8); 475 if (rc)
493 if (rc < 0) 476 printk(KERN_ERR "%s: Error writing file size to header; "
494 ecryptfs_printk(KERN_ERR, "Error commiting header page " 477 "rc = [%d]\n", __FUNCTION__, rc);
495 "write\n");
496 ecryptfs_release_lower_page(header_page);
497 lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
498 mark_inode_dirty_sync(inode);
499out: 478out:
500 return rc; 479 return rc;
501} 480}
502 481
503static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode, 482struct kmem_cache *ecryptfs_xattr_cache;
504 struct inode *inode, 483
505 struct dentry *ecryptfs_dentry, 484static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
506 int lower_i_mutex_held)
507{ 485{
508 ssize_t size; 486 ssize_t size;
509 void *xattr_virt; 487 void *xattr_virt;
510 struct dentry *lower_dentry; 488 struct dentry *lower_dentry =
489 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
490 struct inode *lower_inode = lower_dentry->d_inode;
511 u64 file_size; 491 u64 file_size;
512 int rc; 492 int rc;
513 493
494 if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
495 printk(KERN_WARNING
496 "No support for setting xattr in lower filesystem\n");
497 rc = -ENOSYS;
498 goto out;
499 }
514 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL); 500 xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
515 if (!xattr_virt) { 501 if (!xattr_virt) {
516 printk(KERN_ERR "Out of memory whilst attempting to write " 502 printk(KERN_ERR "Out of memory whilst attempting to write "
@@ -518,35 +504,17 @@ static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
518 rc = -ENOMEM; 504 rc = -ENOMEM;
519 goto out; 505 goto out;
520 } 506 }
521 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); 507 mutex_lock(&lower_inode->i_mutex);
522 if (!lower_dentry->d_inode->i_op->getxattr || 508 size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
523 !lower_dentry->d_inode->i_op->setxattr) { 509 xattr_virt, PAGE_CACHE_SIZE);
524 printk(KERN_WARNING
525 "No support for setting xattr in lower filesystem\n");
526 rc = -ENOSYS;
527 kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
528 goto out;
529 }
530 if (!lower_i_mutex_held)
531 mutex_lock(&lower_dentry->d_inode->i_mutex);
532 size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
533 ECRYPTFS_XATTR_NAME,
534 xattr_virt,
535 PAGE_CACHE_SIZE);
536 if (!lower_i_mutex_held)
537 mutex_unlock(&lower_dentry->d_inode->i_mutex);
538 if (size < 0) 510 if (size < 0)
539 size = 8; 511 size = 8;
540 file_size = (u64)i_size_read(inode); 512 file_size = (u64)i_size_read(ecryptfs_inode);
541 file_size = cpu_to_be64(file_size); 513 file_size = cpu_to_be64(file_size);
542 memcpy(xattr_virt, &file_size, sizeof(u64)); 514 memcpy(xattr_virt, &file_size, sizeof(u64));
543 if (!lower_i_mutex_held) 515 rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
544 mutex_lock(&lower_dentry->d_inode->i_mutex); 516 xattr_virt, size, 0);
545 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, 517 mutex_unlock(&lower_inode->i_mutex);
546 ECRYPTFS_XATTR_NAME,
547 xattr_virt, size, 0);
548 if (!lower_i_mutex_held)
549 mutex_unlock(&lower_dentry->d_inode->i_mutex);
550 if (rc) 518 if (rc)
551 printk(KERN_ERR "Error whilst attempting to write inode size " 519 printk(KERN_ERR "Error whilst attempting to write inode size "
552 "to lower file xattr; rc = [%d]\n", rc); 520 "to lower file xattr; rc = [%d]\n", rc);
@@ -555,24 +523,15 @@ out:
555 return rc; 523 return rc;
556} 524}
557 525
558int 526int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
559ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
560 struct inode *lower_inode,
561 struct inode *inode,
562 struct dentry *ecryptfs_dentry,
563 int lower_i_mutex_held)
564{ 527{
565 struct ecryptfs_crypt_stat *crypt_stat; 528 struct ecryptfs_crypt_stat *crypt_stat;
566 529
567 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; 530 crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
568 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) 531 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
569 return ecryptfs_write_inode_size_to_xattr(lower_inode, inode, 532 return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
570 ecryptfs_dentry,
571 lower_i_mutex_held);
572 else 533 else
573 return ecryptfs_write_inode_size_to_header(lower_file, 534 return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
574 lower_inode,
575 inode);
576} 535}
577 536
578int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode, 537int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
@@ -659,8 +618,6 @@ out:
659 return rc; 618 return rc;
660} 619}
661 620
662struct kmem_cache *ecryptfs_xattr_cache;
663
664/** 621/**
665 * ecryptfs_commit_write 622 * ecryptfs_commit_write
666 * @file: The eCryptfs file object 623 * @file: The eCryptfs file object
@@ -675,7 +632,6 @@ struct kmem_cache *ecryptfs_xattr_cache;
675static int ecryptfs_commit_write(struct file *file, struct page *page, 632static int ecryptfs_commit_write(struct file *file, struct page *page,
676 unsigned from, unsigned to) 633 unsigned from, unsigned to)
677{ 634{
678 struct ecryptfs_page_crypt_context ctx;
679 loff_t pos; 635 loff_t pos;
680 struct inode *inode; 636 struct inode *inode;
681 struct inode *lower_inode; 637 struct inode *lower_inode;
@@ -705,10 +661,7 @@ static int ecryptfs_commit_write(struct file *file, struct page *page,
705 page->index); 661 page->index);
706 goto out; 662 goto out;
707 } 663 }
708 ctx.page = page; 664 rc = ecryptfs_encrypt_page(page);
709 ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
710 ctx.param.lower_file = lower_file;
711 rc = ecryptfs_encrypt_page(&ctx);
712 if (rc) { 665 if (rc) {
713 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " 666 ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
714 "index [0x%.16x])\n", page->index); 667 "index [0x%.16x])\n", page->index);
@@ -721,9 +674,7 @@ static int ecryptfs_commit_write(struct file *file, struct page *page,
721 ecryptfs_printk(KERN_DEBUG, "Expanded file size to " 674 ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
722 "[0x%.16x]\n", i_size_read(inode)); 675 "[0x%.16x]\n", i_size_read(inode));
723 } 676 }
724 rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode, 677 rc = ecryptfs_write_inode_size_to_metadata(inode);
725 inode, file->f_dentry,
726 ECRYPTFS_LOWER_I_MUTEX_HELD);
727 if (rc) 678 if (rc)
728 printk(KERN_ERR "Error writing inode size to metadata; " 679 printk(KERN_ERR "Error writing inode size to metadata; "
729 "rc = [%d]\n", rc); 680 "rc = [%d]\n", rc);
diff --git a/fs/ecryptfs/read_write.c b/fs/ecryptfs/read_write.c
index e59c94add127..ccd2599e0a4f 100644
--- a/fs/ecryptfs/read_write.c
+++ b/fs/ecryptfs/read_write.c
@@ -154,8 +154,8 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
154 /* Read in the page from the lower 154 /* Read in the page from the lower
155 * into the eCryptfs inode page cache, 155 * into the eCryptfs inode page cache,
156 * decrypting */ 156 * decrypting */
157 if ((rc = ecryptfs_decrypt_page(NULL, /* placeholder for git-bisect */ 157 rc = ecryptfs_decrypt_page(ecryptfs_page);
158 ecryptfs_page))) { 158 if (rc) {
159 printk(KERN_ERR "%s: Error decrypting " 159 printk(KERN_ERR "%s: Error decrypting "
160 "page; rc = [%d]\n", 160 "page; rc = [%d]\n",
161 __FUNCTION__, rc); 161 __FUNCTION__, rc);
@@ -178,7 +178,7 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
178 } 178 }
179 kunmap_atomic(ecryptfs_page_virt, KM_USER0); 179 kunmap_atomic(ecryptfs_page_virt, KM_USER0);
180 flush_dcache_page(ecryptfs_page); 180 flush_dcache_page(ecryptfs_page);
181 rc = ecryptfs_encrypt_page(NULL /* placeholder for git-bisect */); 181 rc = ecryptfs_encrypt_page(ecryptfs_page);
182 if (rc) { 182 if (rc) {
183 printk(KERN_ERR "%s: Error encrypting " 183 printk(KERN_ERR "%s: Error encrypting "
184 "page; rc = [%d]\n", __FUNCTION__, rc); 184 "page; rc = [%d]\n", __FUNCTION__, rc);
@@ -190,8 +190,8 @@ int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
190 } 190 }
191 if ((offset + size) > ecryptfs_file_size) { 191 if ((offset + size) > ecryptfs_file_size) {
192 i_size_write(ecryptfs_file->f_dentry->d_inode, (offset + size)); 192 i_size_write(ecryptfs_file->f_dentry->d_inode, (offset + size));
193 rc = ecryptfs_write_inode_size_to_metadata(NULL, NULL, NULL, 193 rc = ecryptfs_write_inode_size_to_metadata(
194 NULL, 0); /* placeholders for git-bisect */ 194 ecryptfs_file->f_dentry->d_inode);
195 if (rc) { 195 if (rc) {
196 printk(KERN_ERR "Problem with " 196 printk(KERN_ERR "Problem with "
197 "ecryptfs_write_inode_size_to_metadata; " 197 "ecryptfs_write_inode_size_to_metadata; "
@@ -338,7 +338,7 @@ int ecryptfs_read(char *data, loff_t offset, size_t size,
338 ecryptfs_page_idx, rc); 338 ecryptfs_page_idx, rc);
339 goto out; 339 goto out;
340 } 340 }
341 rc = ecryptfs_decrypt_page(NULL /* placeholder for git-bisect */, ecryptfs_page); 341 rc = ecryptfs_decrypt_page(ecryptfs_page);
342 if (rc) { 342 if (rc) {
343 printk(KERN_ERR "%s: Error decrypting " 343 printk(KERN_ERR "%s: Error decrypting "
344 "page; rc = [%d]\n", __FUNCTION__, rc); 344 "page; rc = [%d]\n", __FUNCTION__, rc);