@@ -772,18 +772,23 @@ upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
772772}
773773
774774/**
775- * ecryptfs_truncate
775+ * truncate_upper
776776 * @dentry: The ecryptfs layer dentry
777- * @new_length: The length to expand the file to
777+ * @ia: Address of the ecryptfs inode's attributes
778+ * @lower_ia: Address of the lower inode's attributes
778779 *
779780 * Function to handle truncations modifying the size of the file. Note
780781 * that the file sizes are interpolated. When expanding, we are simply
781- * writing strings of 0's out. When truncating, we need to modify the
782- * underlying file size according to the page index interpolations.
782+ * writing strings of 0's out. When truncating, we truncate the upper
783+ * inode and update the lower_ia according to the page index
784+ * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
785+ * the caller must use lower_ia in a call to notify_change() to perform
786+ * the truncation of the lower inode.
783787 *
784788 * Returns zero on success; non-zero otherwise
785789 */
786- int ecryptfs_truncate (struct dentry * dentry , loff_t new_length )
790+ static int truncate_upper (struct dentry * dentry , struct iattr * ia ,
791+ struct iattr * lower_ia )
787792{
788793 int rc = 0 ;
789794 struct inode * inode = dentry -> d_inode ;
@@ -794,8 +799,10 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
794799 loff_t lower_size_before_truncate ;
795800 loff_t lower_size_after_truncate ;
796801
797- if (unlikely ((new_length == i_size )))
802+ if (unlikely ((ia -> ia_size == i_size ))) {
803+ lower_ia -> ia_valid &= ~ATTR_SIZE ;
798804 goto out ;
805+ }
799806 crypt_stat = & ecryptfs_inode_to_private (dentry -> d_inode )-> crypt_stat ;
800807 /* Set up a fake ecryptfs file, this is used to interface with
801808 * the file in the underlying filesystem so that the
@@ -815,28 +822,30 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
815822 & fake_ecryptfs_file ,
816823 ecryptfs_inode_to_private (dentry -> d_inode )-> lower_file );
817824 /* Switch on growing or shrinking file */
818- if (new_length > i_size ) {
825+ if (ia -> ia_size > i_size ) {
819826 char zero [] = { 0x00 };
820827
828+ lower_ia -> ia_valid &= ~ATTR_SIZE ;
821829 /* Write a single 0 at the last position of the file;
822830 * this triggers code that will fill in 0's throughout
823831 * the intermediate portion of the previous end of the
824832 * file and the new and of the file */
825833 rc = ecryptfs_write (& fake_ecryptfs_file , zero ,
826- (new_length - 1 ), 1 );
827- } else { /* new_length < i_size_read(inode) */
828- /* We're chopping off all the pages down do the page
829- * in which new_length is located. Fill in the end of
830- * that page from (new_length & ~PAGE_CACHE_MASK) to
834+ (ia -> ia_size - 1 ), 1 );
835+ } else { /* ia->ia_size < i_size_read(inode) */
836+ /* We're chopping off all the pages down to the page
837+ * in which ia->ia_size is located. Fill in the end of
838+ * that page from (ia->ia_size & ~PAGE_CACHE_MASK) to
831839 * PAGE_CACHE_SIZE with zeros. */
832840 size_t num_zeros = (PAGE_CACHE_SIZE
833- - (new_length & ~PAGE_CACHE_MASK ));
841+ - (ia -> ia_size & ~PAGE_CACHE_MASK ));
834842
835843 if (!(crypt_stat -> flags & ECRYPTFS_ENCRYPTED )) {
836- rc = vmtruncate (inode , new_length );
844+ rc = vmtruncate (inode , ia -> ia_size );
837845 if (rc )
838846 goto out_free ;
839- rc = vmtruncate (lower_dentry -> d_inode , new_length );
847+ lower_ia -> ia_size = ia -> ia_size ;
848+ lower_ia -> ia_valid |= ATTR_SIZE ;
840849 goto out_free ;
841850 }
842851 if (num_zeros ) {
@@ -848,7 +857,7 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
848857 goto out_free ;
849858 }
850859 rc = ecryptfs_write (& fake_ecryptfs_file , zeros_virt ,
851- new_length , num_zeros );
860+ ia -> ia_size , num_zeros );
852861 kfree (zeros_virt );
853862 if (rc ) {
854863 printk (KERN_ERR "Error attempting to zero out "
@@ -857,7 +866,7 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
857866 goto out_free ;
858867 }
859868 }
860- vmtruncate (inode , new_length );
869+ vmtruncate (inode , ia -> ia_size );
861870 rc = ecryptfs_write_inode_size_to_metadata (inode );
862871 if (rc ) {
863872 printk (KERN_ERR "Problem with "
@@ -870,10 +879,12 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
870879 lower_size_before_truncate =
871880 upper_size_to_lower_size (crypt_stat , i_size );
872881 lower_size_after_truncate =
873- upper_size_to_lower_size (crypt_stat , new_length );
874- if (lower_size_after_truncate < lower_size_before_truncate )
875- vmtruncate (lower_dentry -> d_inode ,
876- lower_size_after_truncate );
882+ upper_size_to_lower_size (crypt_stat , ia -> ia_size );
883+ if (lower_size_after_truncate < lower_size_before_truncate ) {
884+ lower_ia -> ia_size = lower_size_after_truncate ;
885+ lower_ia -> ia_valid |= ATTR_SIZE ;
886+ } else
887+ lower_ia -> ia_valid &= ~ATTR_SIZE ;
877888 }
878889out_free :
879890 if (ecryptfs_file_to_private (& fake_ecryptfs_file ))
@@ -883,6 +894,33 @@ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
883894 return rc ;
884895}
885896
897+ /**
898+ * ecryptfs_truncate
899+ * @dentry: The ecryptfs layer dentry
900+ * @new_length: The length to expand the file to
901+ *
902+ * Simple function that handles the truncation of an eCryptfs inode and
903+ * its corresponding lower inode.
904+ *
905+ * Returns zero on success; non-zero otherwise
906+ */
907+ int ecryptfs_truncate (struct dentry * dentry , loff_t new_length )
908+ {
909+ struct iattr ia = { .ia_valid = ATTR_SIZE , .ia_size = new_length };
910+ struct iattr lower_ia = { .ia_valid = 0 };
911+ int rc ;
912+
913+ rc = truncate_upper (dentry , & ia , & lower_ia );
914+ if (!rc && lower_ia .ia_valid & ATTR_SIZE ) {
915+ struct dentry * lower_dentry = ecryptfs_dentry_to_lower (dentry );
916+
917+ mutex_lock (& lower_dentry -> d_inode -> i_mutex );
918+ rc = notify_change (lower_dentry , & lower_ia );
919+ mutex_unlock (& lower_dentry -> d_inode -> i_mutex );
920+ }
921+ return rc ;
922+ }
923+
886924static int
887925ecryptfs_permission (struct inode * inode , int mask )
888926{
@@ -905,6 +943,7 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
905943{
906944 int rc = 0 ;
907945 struct dentry * lower_dentry ;
946+ struct iattr lower_ia ;
908947 struct inode * inode ;
909948 struct inode * lower_inode ;
910949 struct ecryptfs_crypt_stat * crypt_stat ;
@@ -943,15 +982,11 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
943982 }
944983 }
945984 mutex_unlock (& crypt_stat -> cs_mutex );
985+ memcpy (& lower_ia , ia , sizeof (lower_ia ));
986+ if (ia -> ia_valid & ATTR_FILE )
987+ lower_ia .ia_file = ecryptfs_file_to_lower (ia -> ia_file );
946988 if (ia -> ia_valid & ATTR_SIZE ) {
947- ecryptfs_printk (KERN_DEBUG ,
948- "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n" ,
949- ia -> ia_valid , ATTR_SIZE );
950- rc = ecryptfs_truncate (dentry , ia -> ia_size );
951- /* ecryptfs_truncate handles resizing of the lower file */
952- ia -> ia_valid &= ~ATTR_SIZE ;
953- ecryptfs_printk (KERN_DEBUG , "ia->ia_valid = [%x]\n" ,
954- ia -> ia_valid );
989+ rc = truncate_upper (dentry , ia , & lower_ia );
955990 if (rc < 0 )
956991 goto out ;
957992 }
@@ -960,11 +995,11 @@ static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
960995 * mode change is for clearing setuid/setgid bits. Allow lower fs
961996 * to interpret this in its own way.
962997 */
963- if (ia -> ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID ))
964- ia -> ia_valid &= ~ATTR_MODE ;
998+ if (lower_ia . ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID ))
999+ lower_ia . ia_valid &= ~ATTR_MODE ;
9651000
9661001 mutex_lock (& lower_dentry -> d_inode -> i_mutex );
967- rc = notify_change (lower_dentry , ia );
1002+ rc = notify_change (lower_dentry , & lower_ia );
9681003 mutex_unlock (& lower_dentry -> d_inode -> i_mutex );
9691004out :
9701005 fsstack_copy_attr_all (inode , lower_inode );
0 commit comments