libext2fs: fix block-mapped file punch
PATCH 1/2 libext2fs: fix block-mapped file punch
If ext2fs_punch() was called with "end = ~0ULL" to indicate truncate to the end of file it tried to compute "count" for ext2fs_punch_ind() based on "start" and "end", but incorrectly passed "count = ~0U" even when "start" was non-zero, causing an overflow in some cases.
The calling convention for ext2fs_punch_ind() was also gratuitously different from ext2fs_punch() and ext2fs_punch_extent(), passing "count" instead of "end" as the last parameter. Fix this by passing it "end" like the other functions, and handle "count" internally.
Add checks to ext2fs_punch_ind() if "end" is at or beyond the 2^32 indirect block limit so the 32-bit internal variables don't overflow.
--- lib/ext2fs/punch.c | 48 +++++++++++++++++++++++++++--------------------- 1 files changed, 27 insertions(+), 21 deletions(-)
lib/ext2fs/punch.c +21 -12
@@ -47,7 +47,7 @@ static int check_zero_block(char *buf, int blocksize)*/ static errcode_t ind_punch (ext2_filsys fs,struct ext2_inode *inode,char *block_buf,blk_t *p,int level, -blk_t start,blk_t count,int max) +blk64_t start,blk64_t count,int max) {errcode_t retval;blk_t b;
@@ -56,11 +56,11 @@ static errcode_t ind_punch(ext2_filsys fs, struct ext2_inode *inode,int freed =0 ;#ifdef PUNCH_DEBUG -printf ("Entering ind_punch, level %d, start %u, count %u, " +printf ("Entering ind_punch, level %d, start %llu, count %llu, " "max %d\n" , level, start, count, max);#endif - incr =1ULL << ((EXT2_BLOCK_SIZE_BITS (fs->super)-2 )*level); -for (i=0 , offset=0 ; i < max; i++, p++, offset += incr) { + incr =1ULL << ((EXT2_BLOCK_SIZE_BITS (fs->super) -2 ) * level); +for (i =0 , offset =0 ; i < max; i++, p++, offset += incr) {if (offset >= start + count)break ;if (*p ==0 || (offset+incr) <= start)
@@ -100,8 +100,9 @@ static errcode_t ind_punch(ext2_filsys fs, struct ext2_inode *inode,return ext2fs_iblk_sub_blocks (fs, inode, freed); } +#define BLK_T_MAX ((blk_t)~0ULL)static errcode_t ext2fs_punch_ind (ext2_filsys fs,struct ext2_inode *inode, -char *block_buf,blk_t start,blk_t count) +char *block_buf,blk64_t start,blk64_t end) {errcode_t retval;char *buf =0 ;
@@ -110,6 +111,15 @@ static errcode_t ext2fs_punch_ind(ext2_filsys fs, struct ext2_inode *inode,blk_t *bp = inode->i_block;blk_t addr_per_block;blk64_t max =EXT2_NDIR_BLOCKS ; +blk_t count; + +/* Check start/end don't overflow the 2^32-1 indirect block limit */ +if (start >BLK_T_MAX ) +return 0 ; +if (end >=BLK_T_MAX || end - start +1 >=BLK_T_MAX ) + count =BLK_T_MAX - start; +else + count = end - start +1 ;if (!block_buf) { retval =ext2fs_get_array (3 , fs->blocksize, &buf);
@@ -118,11 +128,11 @@ static errcode_t ext2fs_punch_ind(ext2_filsys fs, struct ext2_inode *inode, block_buf = buf; } - addr_per_block = (blk_t ) fs->blocksize >>2 ; + addr_per_block = (blk_t )fs->blocksize >>2 ;for (level =0 ; level <4 ; level++, max *= (blk64_t )addr_per_block) {#ifdef PUNCH_DEBUG -printf ("Main loop level %d, start %u count %u " +printf ("Main loop level %d, start %llu count %u " "max %llu num %d\n" , level, start, count, max, num);#endif if (start < max) {
@@ -149,6 +159,7 @@ errout:ext2fs_free_mem (&buf);return retval; } +#undef BLK_T_MAX#ifdef PUNCH_DEBUG
@@ -428,10 +439,10 @@ errout:ext2fs_extent_free (handle);return retval; } - +/* -* Deallocate all logical blocks starting at start to end, inclusive. -* If end is ~0, then this is effectively truncate. +* Deallocate all logical _blocks_ starting at start to end, inclusive. +* If end is ~0ULL, then this is effectively truncate. */ errcode_t ext2fs_punch(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
@@ -453,19 +464,14 @@ errcode_t ext2fs_punch(ext2_filsys fs, ext2_ino_t ino, }if (inode->i_flags &EXT4_EXTENTS_FL ) retval =ext2fs_punch_extent (fs, ino, inode, start, end); -else { -blk_t count;
PATCH 2/2 e2fsck: fix e2fsck -fD directory truncation
When an extent-mapped directory is compacted by "e2fsck -fD" and frees enough leaf blocks that it loses an extent tree index block, the old e2fsck_rehash_dir->ext2fs_block_iterate3->write_dir_block() code would not free the extent block, which would result in the extent tree becoming corrupted when it is written out.
Pass 1: Checking inodes, blocks, and sizes
Inode 17825800, end of extent exceeds allowed value
(logical block 710, physical block 570459684, len 1019)
This results in loss of a whole index block of directory leaf blocks and maybe thousands or millions of files in lost+found.
Fix e2fsck_rehash_dir() to call ext2fs_punch() to free the blocks at the end of the directory instead of trying to handle this itself while writing out the directory. That properly handles all of the cases of updating the extent tree as well as accounting for blocks that are released (both leaf blocks and index blocks).
Add a test case for compacting the directory to be smaller than the index block that originally caused the corruption.
--- e2fsck/rehash.c | 72 ++++++++++++++++++++++------------------- tests/f_extent_htree/expect.1 | 29 ++++++++++++++++ tests/f_extent_htree/expect.2 | 7 ++++ tests/f_extent_htree/image.gz | Bin 0 -> 10101 bytes tests/f_extent_htree/name | 1 + tests/f_extent_htree/script | 69 +++++++++++++++++++++++++++++++++++++++ tests/f_h_badnode/expect.1 | 2 +- tests/f_h_badnode/expect.2 | 2 +- 8 files changed, 147 insertions(+), 35 deletions(-) create mode 100644 tests/f_extent_htree/expect.1 create mode 100644 tests/f_extent_htree/expect.2 create mode 100644 tests/f_extent_htree/image.gz create mode 100644 tests/f_extent_htree/name create mode 100644 tests/f_extent_htree/script
e2fsck/rehash.c +39 -33
@@ -52,10 +52,13 @@#include "e2fsck.h" #include "problem.h" +#undef REHASH_DEBUG +struct fill_dir_struct {char *buf;struct ext2_inode *inode;errcode_t err; +ext2_ino_t ino;e2fsck_t ctx;struct hash_entry *harray;int max_array, num_array;
@@ -625,8 +628,8 @@ static errcode_t calculate_tree(ext2_filsys fs,struct write_dir_struct {struct out_dir *outdir;errcode_t err; +ext2_ino_t ino;e2fsck_t ctx; -blk64_t cleared; };/*
@@ -643,28 +646,35 @@ static int write_dir_block(ext2_filsys fs,blk64_t blk;char *dir; -if (*block_nr ==0 ) +#ifdef REHASH_DEBUG +printf ("%u: write_dir_block %lld:%lld" , wd->ino, blockcnt, *block_nr); +#endif +if (*block_nr ==0 ) { +#ifdef REHASH_DEBUG +printf (" - skip\n" ); +#endif return 0 ; + } +/* Don't free blocks at the end of the directory, they will be +* truncated by the caller. */ if (blockcnt >= wd->outdir->num) { -e2fsck_read_bitmaps (wd->ctx); - blk = *block_nr; -/* -* In theory, we only release blocks from the end of the -* directory file, so it's fine to clobber a whole cluster at -* once. -*/ -if (blk %EXT2FS_CLUSTER_RATIO (fs) ==0 ) { -ext2fs_block_alloc_stats2 (fs, blk,-1 ); - wd->cleared++; - } - *block_nr =0 ; -return BLOCK_CHANGED ; +#ifdef REHASH_DEBUG +printf (" - not freed\n" ); +#endif +return 0 ; } -if (blockcnt <0 ) +if (blockcnt <0 ) { +#ifdef REHASH_DEBUG +printf (" - skip\n" ); +#endif return 0 ; + } dir = wd->outdir->buf + (blockcnt * fs->blocksize); wd->err =ext2fs_write_dir_block3 (fs, *block_nr, dir,0 ); +#ifdef REHASH_DEBUG +printf (" - write (%d)\n" , wd->err); +#endif if (wd->err)return BLOCK_ABORT ;return 0 ;
@@ -684,10 +694,10 @@ static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs, wd.outdir = outdir; wd.err =0 ; + wd.ino = ino; wd.ctx = ctx; - wd.cleared =0 ; - retval =ext2fs_block_iterate3 (fs, ino,0 ,0 , + retval =ext2fs_block_iterate3 (fs, ino,0 ,NULL , write_dir_block, &wd);if (retval)return retval;
@@ -699,14 +709,17 @@ static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs, inode.i_flags &= ~EXT2_INDEX_FL ; else inode.i_flags |= EXT2_INDEX_FL; - retval =ext2fs_inode_size_set (fs, &inode, - outdir->num * fs->blocksize); +#ifdef REHASH_DEBUG +printf ("%u: set inode size to %u blocks = %u bytes\n" , + ino, outdir->num, outdir->num * fs->blocksize); +#endif + retval =ext2fs_inode_size_set (fs, &inode, (ext2_off64_t )outdir->num * + fs->blocksize);if (retval)return retval; -ext2fs_iblk_sub_blocks (fs, &inode, wd.cleared); -e2fsck_write_inode (ctx, ino, &inode,"rehash_dir" ); -return 0 ; +/* ext2fs_punch() calls ext2fs_write_inode() which writes the size */ +return ext2fs_punch (fs, ino, &inode,NULL , outdir->num, ~0ULL ); }errcode_t e2fsck_rehash_dir (e2fsck_t ctx,ext2_ino_t ino)
@@ -715,32 +728,25 @@ errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)errcode_t retval;struct ext2_inode inode;char *dir_buf =0 ; -struct fill_dir_struct fd; -struct out_dir outdir; +struct fill_dir_struct fd = {NULL }; +struct out_dir outdir = {0 }; - outdir.max = outdir.num =0 ; - outdir.buf =0 ; - outdir.hashes =0 ;e2fsck_read_inode (ctx, ino, &inode,"rehash_dir" ); retval =ENOMEM ; - fd.harray =0 ; dir_buf =malloc (inode.i_size);if (!dir_buf) goto errout; fd.max_array = inode.i_size /32 ; - fd.num_array =0 ; fd.harray =malloc (fd.max_array *sizeof (struct hash_entry ));if (!fd.harray) goto errout; + fd.ino = ino; fd.ctx = ctx; fd.buf = dir_buf; fd.inode = &inode; - fd.err =0 ; - fd.dir_size =0 ; - fd.compress =0 ;if (!(fs->super->s_feature_compat &EXT2_FEATURE_COMPAT_DIR_INDEX ) || (inode.i_size / fs->blocksize) <2 ) fd.compress =1 ;
tests/f_extent_htree/expect.1 new file +29 -0
@@ -0,0 +1,29 @@ +Pass 1: Checking inodes, blocks, and sizes +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Pass 3A: Optimizing directories +Pass 4: Checking reference counts +Pass 5: Checking group summary information + +test_filesys: ***** FILE SYSTEM WAS MODIFIED ***** + + 352 inodes used (41.12%, out of 856) + 0 non-contiguous files (0.0%) + 1 non-contiguous directory (0.3%) + # of inodes with ind/dind/tind blocks: 0/0/0 + Extent depth histogram: 342/1 + 586 blocks used (68.94%, out of 850) + 0 bad blocks + 0 large files + + 340 regular files + 3 directories + 0 character device files + 0 block device files + 0 fifos + 0 links + 0 symbolic links (0 fast symbolic links) + 0 sockets +------------ + 343 files +Exit status is 1
tests/f_extent_htree/expect.2 new file +7 -0
@@ -0,0 +1,7 @@ +Pass 1: Checking inodes, blocks, and sizes +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Pass 4: Checking reference counts +Pass 5: Checking group summary information +test_filesys: 352/856 files (0.3% non-contiguous), 586/850 blocks +Exit status is 0
tests/f_extent_htree/image.gz new file binary +0 -0
tests/f_extent_htree/name new file +1 -0
@@ -0,0 +1 @@ +htree extent compression
tests/f_extent_htree/script new file +69 -0
69 lines not on this page · raw message
tests/f_h_badnode/expect.1 +1 -1
@@ -14,5 +14,5 @@ Pass 4: Checking reference counts Pass 5: Checking group summary information test_filesys: ***** FILE SYSTEM WAS MODIFIED ***** -test_filesys: 47730/100192 files (0.0% non-contiguous), 13551/31745 blocks +test_filesys: 47730/100192 files (0.0% non-contiguous), 13550/31745 blocks Exit status is 1
tests/f_h_badnode/expect.2 +1 -1
@@ -3,5 +3,5 @@ Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information -test_filesys: 47730/100192 files (0.0% non-contiguous), 13551/31745 blocks +test_filesys: 47730/100192 files (0.0% non-contiguous), 13550/31745 blocks Exit status is 0
Signature · 1 line
1.7.3.4
Theodore Ts'o
AppliedRe: [PATCH 2/2] e2fsck: fix e2fsck -fD directory truncation
Quoted 19 lines · Andreas Dilger
When an extent-mapped directory is compacted by "e2fsck -fD" and frees enough leaf blocks that it loses an extent tree index block, the old
e2fsck_rehash_dir->ext2fs_block_iterate3->write_dir_block()code would not free the extent block, which would result in the extent tree becoming corrupted when it is written out.Pass 1: Checking inodes, blocks, and sizes
Inode 17825800, end of extent exceeds allowed value
(logical block 710, physical block 570459684, len 1019)This results in loss of a whole index block of directory leaf blocks and maybe thousands or millions of files in lost+found.
Fix
e2fsck_rehash_dir()to callext2fs_punch()to free the blocks at the end of the directory instead of trying to handle this itself while writing out the directory. That properly handles all of the cases of updating the extent tree as well as accounting for blocks that are released (both leaf blocks and index blocks).
Quoted 4 lines
Add a test case for compacting the directory to be smaller than the index block that originally caused the corruption.
Signed-off-by Andreas Dilger <[email protected]>
Thanks, applied.
- Ted
Theodore Ts'o
AppliedQuoted 10 lines · Andreas Dilger
If
ext2fs_punch()was called with "end = ~0ULL" to indicate truncate to the end of file it tried to compute "count" forext2fs_punch_ind()based on "start" and "end", but incorrectly passed "count = ~0U" even when "start" was non-zero, causing an overflow in some cases.The calling convention for
ext2fs_punch_ind()was also gratuitously different fromext2fs_punch()andext2fs_punch_extent(), passing "count" instead of "end" as the last parameter. Fix this by passing it "end" like the other functions, and handle "count" internally.
Quoted 4 lines
Add checks to
ext2fs_punch_ind()if "end" is at or beyond the 2^32 indirect block limit so the 32-bit internal variables don't overflow.Signed-off-by Andreas Dilger <[email protected]>
Thanks, applied.
- Ted