Skip to content

Commit 8974fec

Browse files
guaneryutytso
authored andcommittedJul 4, 2015
ext4: correctly migrate a file with a hole at the beginning
Currently ext4_ind_migrate() doesn't correctly handle a file which contains a hole at the beginning of the file. This caused the migration to be done incorrectly, and then if there is a subsequent following delayed allocation write to the "hole", this would reclaim the same data blocks again and results in fs corruption. # assmuing 4k block size ext4, with delalloc enabled # skip the first block and write to the second block xfs_io -fc "pwrite 4k 4k" -c "fsync" /mnt/ext4/testfile # converting to indirect-mapped file, which would move the data blocks # to the beginning of the file, but extent status cache still marks # that region as a hole chattr -e /mnt/ext4/testfile # delayed allocation writes to the "hole", reclaim the same data block # again, results in i_blocks corruption xfs_io -c "pwrite 0 4k" /mnt/ext4/testfile umount /mnt/ext4 e2fsck -nf /dev/sda6 ... Inode 53, i_blocks is 16, should be 8. Fix? no ... Signed-off-by: Eryu Guan <[email protected]> Signed-off-by: Theodore Ts'o <[email protected]> Cc: [email protected]
1 parent d6f123a commit 8974fec

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed
 

‎fs/ext4/migrate.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ int ext4_ind_migrate(struct inode *inode)
620620
struct ext4_inode_info *ei = EXT4_I(inode);
621621
struct ext4_extent *ex;
622622
unsigned int i, len;
623-
ext4_lblk_t end;
623+
ext4_lblk_t start, end;
624624
ext4_fsblk_t blk;
625625
handle_t *handle;
626626
int ret;
@@ -659,11 +659,12 @@ int ext4_ind_migrate(struct inode *inode)
659659
goto errout;
660660
}
661661
if (eh->eh_entries == 0)
662-
blk = len = 0;
662+
blk = len = start = end = 0;
663663
else {
664664
len = le16_to_cpu(ex->ee_len);
665665
blk = ext4_ext_pblock(ex);
666-
end = le32_to_cpu(ex->ee_block) + len - 1;
666+
start = le32_to_cpu(ex->ee_block);
667+
end = start + len - 1;
667668
if (end >= EXT4_NDIR_BLOCKS) {
668669
ret = -EOPNOTSUPP;
669670
goto errout;
@@ -672,7 +673,7 @@ int ext4_ind_migrate(struct inode *inode)
672673

673674
ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
674675
memset(ei->i_data, 0, sizeof(ei->i_data));
675-
for (i=0; i < len; i++)
676+
for (i = start; i <= end; i++)
676677
ei->i_data[i] = cpu_to_le32(blk++);
677678
ext4_mark_inode_dirty(handle, inode);
678679
errout:

0 commit comments

Comments
 (0)
Please sign in to comment.