Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ecryptfs: streamline truncate_upper

Use a few strategic gotos to reduce indentation and keep the main flow
outside of branches. Switch all touched comments to normal kernel style
and avoid breaks in printed strings for all the code touched.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Tyler Hicks <code@tyhicks.com>

authored by

Christoph Hellwig and committed by
Tyler Hicks
b1091873 8f613643

+61 -56
+61 -56
fs/ecryptfs/inode.c
··· 725 725 static int truncate_upper(struct dentry *dentry, struct iattr *ia, 726 726 struct iattr *lower_ia) 727 727 { 728 - int rc = 0; 729 728 struct inode *inode = d_inode(dentry); 730 729 struct ecryptfs_crypt_stat *crypt_stat; 731 730 loff_t i_size = i_size_read(inode); 732 731 loff_t lower_size_before_truncate; 733 732 loff_t lower_size_after_truncate; 733 + size_t num_zeros; 734 + int rc; 734 735 735 736 if (unlikely((ia->ia_size == i_size))) { 736 737 lower_ia->ia_valid &= ~ATTR_SIZE; 737 738 return 0; 738 739 } 740 + 739 741 rc = ecryptfs_get_lower_file(dentry, inode); 740 742 if (rc) 741 743 return rc; 742 - crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat; 743 - /* Switch on growing or shrinking file */ 744 + 744 745 if (ia->ia_size > i_size) { 745 746 char zero[] = { 0x00 }; 746 747 748 + /* 749 + * Write a single 0 at the last position of the file; this 750 + * triggers code that will fill in 0's throughout the 751 + * intermediate portion of the previous end of the file and the 752 + * new end of the file. 753 + */ 754 + rc = ecryptfs_write(inode, zero, ia->ia_size - 1, 1); 747 755 lower_ia->ia_valid &= ~ATTR_SIZE; 748 - /* Write a single 0 at the last position of the file; 749 - * this triggers code that will fill in 0's throughout 750 - * the intermediate portion of the previous end of the 751 - * file and the new and of the file */ 752 - rc = ecryptfs_write(inode, zero, 753 - (ia->ia_size - 1), 1); 754 - } else { /* ia->ia_size < i_size_read(inode) */ 755 - /* We're chopping off all the pages down to the page 756 - * in which ia->ia_size is located. Fill in the end of 757 - * that page from (ia->ia_size & ~PAGE_MASK) to 758 - * PAGE_SIZE with zeros. */ 759 - size_t num_zeros = (PAGE_SIZE 760 - - (ia->ia_size & ~PAGE_MASK)); 756 + goto out; 757 + } 761 758 762 - if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 763 - truncate_setsize(inode, ia->ia_size); 764 - lower_ia->ia_size = ia->ia_size; 765 - lower_ia->ia_valid |= ATTR_SIZE; 766 - goto out; 767 - } 768 - if (num_zeros) { 769 - char *zeros_virt; 770 - 771 - zeros_virt = kzalloc(num_zeros, GFP_KERNEL); 772 - if (!zeros_virt) { 773 - rc = -ENOMEM; 774 - goto out; 775 - } 776 - rc = ecryptfs_write(inode, zeros_virt, 777 - ia->ia_size, num_zeros); 778 - kfree(zeros_virt); 779 - if (rc) { 780 - printk(KERN_ERR "Error attempting to zero out " 781 - "the remainder of the end page on " 782 - "reducing truncate; rc = [%d]\n", rc); 783 - goto out; 784 - } 785 - } 759 + crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat; 760 + if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { 786 761 truncate_setsize(inode, ia->ia_size); 787 - rc = ecryptfs_write_inode_size_to_metadata(inode); 788 - if (rc) { 789 - printk(KERN_ERR "Problem with " 790 - "ecryptfs_write_inode_size_to_metadata; " 791 - "rc = [%d]\n", rc); 762 + lower_ia->ia_size = ia->ia_size; 763 + lower_ia->ia_valid |= ATTR_SIZE; 764 + goto out; 765 + } 766 + 767 + /* 768 + * We're chopping off all the pages down to the page in which 769 + * ia->ia_size is located. Fill in the end of that page from 770 + * (ia->ia_size & ~PAGE_MASK) to PAGE_SIZE with zeros. 771 + */ 772 + num_zeros = PAGE_SIZE - (ia->ia_size & ~PAGE_MASK); 773 + if (num_zeros) { 774 + char *zeros_virt; 775 + 776 + zeros_virt = kzalloc(num_zeros, GFP_KERNEL); 777 + if (!zeros_virt) { 778 + rc = -ENOMEM; 792 779 goto out; 793 780 } 794 - /* We are reducing the size of the ecryptfs file, and need to 795 - * know if we need to reduce the size of the lower file. */ 796 - lower_size_before_truncate = 797 - upper_size_to_lower_size(crypt_stat, i_size); 798 - lower_size_after_truncate = 799 - upper_size_to_lower_size(crypt_stat, ia->ia_size); 800 - if (lower_size_after_truncate < lower_size_before_truncate) { 801 - lower_ia->ia_size = lower_size_after_truncate; 802 - lower_ia->ia_valid |= ATTR_SIZE; 803 - } else 804 - lower_ia->ia_valid &= ~ATTR_SIZE; 781 + rc = ecryptfs_write(inode, zeros_virt, ia->ia_size, num_zeros); 782 + kfree(zeros_virt); 783 + if (rc) { 784 + pr_err("Error attempting to zero out the remainder of the end page on reducing truncate; rc = [%d]\n", 785 + rc); 786 + goto out; 787 + } 788 + } 789 + truncate_setsize(inode, ia->ia_size); 790 + rc = ecryptfs_write_inode_size_to_metadata(inode); 791 + if (rc) { 792 + pr_err("Problem with ecryptfs_write_inode_size_to_metadata; rc = [%d]\n", 793 + rc); 794 + goto out; 795 + } 796 + 797 + /* 798 + * We are reducing the size of the ecryptfs file, and need to know if we 799 + * need to reduce the size of the lower file. 800 + */ 801 + lower_size_before_truncate = 802 + upper_size_to_lower_size(crypt_stat, i_size); 803 + lower_size_after_truncate = 804 + upper_size_to_lower_size(crypt_stat, ia->ia_size); 805 + if (lower_size_after_truncate < lower_size_before_truncate) { 806 + lower_ia->ia_size = lower_size_after_truncate; 807 + lower_ia->ia_valid |= ATTR_SIZE; 808 + } else { 809 + lower_ia->ia_valid &= ~ATTR_SIZE; 805 810 } 806 811 out: 807 812 ecryptfs_put_lower_file(inode);