Skip to content

Commit b109187

Browse files
Christoph Hellwigtyhicks
authored andcommitted
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>
1 parent 8f61364 commit b109187

1 file changed

Lines changed: 60 additions & 55 deletions

File tree

fs/ecryptfs/inode.c

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -725,83 +725,88 @@ upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
725725
static int truncate_upper(struct dentry *dentry, struct iattr *ia,
726726
struct iattr *lower_ia)
727727
{
728-
int rc = 0;
729728
struct inode *inode = d_inode(dentry);
730729
struct ecryptfs_crypt_stat *crypt_stat;
731730
loff_t i_size = i_size_read(inode);
732731
loff_t lower_size_before_truncate;
733732
loff_t lower_size_after_truncate;
733+
size_t num_zeros;
734+
int rc;
734735

735736
if (unlikely((ia->ia_size == i_size))) {
736737
lower_ia->ia_valid &= ~ATTR_SIZE;
737738
return 0;
738739
}
740+
739741
rc = ecryptfs_get_lower_file(dentry, inode);
740742
if (rc)
741743
return rc;
742-
crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
743-
/* Switch on growing or shrinking file */
744+
744745
if (ia->ia_size > i_size) {
745746
char zero[] = { 0x00 };
746747

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);
747755
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));
761-
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;
756+
goto out;
757+
}
770758

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)) {
786761
truncate_setsize(inode, ia->ia_size);
787-
rc = ecryptfs_write_inode_size_to_metadata(inode);
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;
779+
goto out;
780+
}
781+
rc = ecryptfs_write(inode, zeros_virt, ia->ia_size, num_zeros);
782+
kfree(zeros_virt);
788783
if (rc) {
789-
printk(KERN_ERR "Problem with "
790-
"ecryptfs_write_inode_size_to_metadata; "
791-
"rc = [%d]\n", rc);
784+
pr_err("Error attempting to zero out the remainder of the end page on reducing truncate; rc = [%d]\n",
785+
rc);
792786
goto out;
793787
}
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;
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;
805810
}
806811
out:
807812
ecryptfs_put_lower_file(inode);

0 commit comments

Comments
 (0)