Skip to content

Commit 0875873

Browse files
zhangyi089gregkh
authored andcommitted
ext4: check for inconsistent extents between index and leaf block
commit 9c6e071 upstream. Now that we can check out overlapping extents in leaf block and out-of-order index extents in index block. But the .ee_block in the first extent of one leaf block should equal to the .ei_block in it's parent index extent entry. This patch add a check to verify such inconsistent between the index and leaf block. Signed-off-by: Zhang Yi <yi.zhang@huawei.com> Link: https://lore.kernel.org/r/20210908120850.4012324-3-yi.zhang@huawei.com Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 76366c0 commit 0875873

1 file changed

Lines changed: 36 additions & 23 deletions

File tree

‎fs/ext4/extents.c‎

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ static int ext4_valid_extent_idx(struct inode *inode,
366366

367367
static int ext4_valid_extent_entries(struct inode *inode,
368368
struct ext4_extent_header *eh,
369-
ext4_fsblk_t *pblk, int depth)
369+
ext4_lblk_t lblk, ext4_fsblk_t *pblk,
370+
int depth)
370371
{
371372
unsigned short entries;
372373
ext4_lblk_t lblock = 0;
@@ -380,6 +381,14 @@ static int ext4_valid_extent_entries(struct inode *inode,
380381
if (depth == 0) {
381382
/* leaf entries */
382383
struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
384+
385+
/*
386+
* The logical block in the first entry should equal to
387+
* the number in the index block.
388+
*/
389+
if (depth != ext_depth(inode) &&
390+
lblk != le32_to_cpu(ext->ee_block))
391+
return 0;
383392
while (entries) {
384393
if (!ext4_valid_extent(inode, ext))
385394
return 0;
@@ -396,6 +405,14 @@ static int ext4_valid_extent_entries(struct inode *inode,
396405
}
397406
} else {
398407
struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
408+
409+
/*
410+
* The logical block in the first entry should equal to
411+
* the number in the parent index block.
412+
*/
413+
if (depth != ext_depth(inode) &&
414+
lblk != le32_to_cpu(ext_idx->ei_block))
415+
return 0;
399416
while (entries) {
400417
if (!ext4_valid_extent_idx(inode, ext_idx))
401418
return 0;
@@ -416,7 +433,7 @@ static int ext4_valid_extent_entries(struct inode *inode,
416433

417434
static int __ext4_ext_check(const char *function, unsigned int line,
418435
struct inode *inode, struct ext4_extent_header *eh,
419-
int depth, ext4_fsblk_t pblk)
436+
int depth, ext4_fsblk_t pblk, ext4_lblk_t lblk)
420437
{
421438
const char *error_msg;
422439
int max = 0, err = -EFSCORRUPTED;
@@ -442,7 +459,7 @@ static int __ext4_ext_check(const char *function, unsigned int line,
442459
error_msg = "invalid eh_entries";
443460
goto corrupted;
444461
}
445-
if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) {
462+
if (!ext4_valid_extent_entries(inode, eh, lblk, &pblk, depth)) {
446463
error_msg = "invalid extent entries";
447464
goto corrupted;
448465
}
@@ -472,7 +489,7 @@ static int __ext4_ext_check(const char *function, unsigned int line,
472489
}
473490

474491
#define ext4_ext_check(inode, eh, depth, pblk) \
475-
__ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
492+
__ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk), 0)
476493

477494
int ext4_ext_check_inode(struct inode *inode)
478495
{
@@ -505,16 +522,18 @@ static void ext4_cache_extents(struct inode *inode,
505522

506523
static struct buffer_head *
507524
__read_extent_tree_block(const char *function, unsigned int line,
508-
struct inode *inode, ext4_fsblk_t pblk, int depth,
509-
int flags)
525+
struct inode *inode, struct ext4_extent_idx *idx,
526+
int depth, int flags)
510527
{
511528
struct buffer_head *bh;
512529
int err;
513530
gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS;
531+
ext4_fsblk_t pblk;
514532

515533
if (flags & EXT4_EX_NOFAIL)
516534
gfp_flags |= __GFP_NOFAIL;
517535

536+
pblk = ext4_idx_pblock(idx);
518537
bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags);
519538
if (unlikely(!bh))
520539
return ERR_PTR(-ENOMEM);
@@ -527,8 +546,8 @@ __read_extent_tree_block(const char *function, unsigned int line,
527546
}
528547
if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
529548
return bh;
530-
err = __ext4_ext_check(function, line, inode,
531-
ext_block_hdr(bh), depth, pblk);
549+
err = __ext4_ext_check(function, line, inode, ext_block_hdr(bh),
550+
depth, pblk, le32_to_cpu(idx->ei_block));
532551
if (err)
533552
goto errout;
534553
set_buffer_verified(bh);
@@ -546,8 +565,8 @@ __read_extent_tree_block(const char *function, unsigned int line,
546565

547566
}
548567

549-
#define read_extent_tree_block(inode, pblk, depth, flags) \
550-
__read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
568+
#define read_extent_tree_block(inode, idx, depth, flags) \
569+
__read_extent_tree_block(__func__, __LINE__, (inode), (idx), \
551570
(depth), (flags))
552571

553572
/*
@@ -597,8 +616,7 @@ int ext4_ext_precache(struct inode *inode)
597616
i--;
598617
continue;
599618
}
600-
bh = read_extent_tree_block(inode,
601-
ext4_idx_pblock(path[i].p_idx++),
619+
bh = read_extent_tree_block(inode, path[i].p_idx++,
602620
depth - i - 1,
603621
EXT4_EX_FORCE_CACHE);
604622
if (IS_ERR(bh)) {
@@ -903,8 +921,7 @@ ext4_find_extent(struct inode *inode, ext4_lblk_t block,
903921
path[ppos].p_depth = i;
904922
path[ppos].p_ext = NULL;
905923

906-
bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
907-
flags);
924+
bh = read_extent_tree_block(inode, path[ppos].p_idx, --i, flags);
908925
if (IS_ERR(bh)) {
909926
ret = PTR_ERR(bh);
910927
goto err;
@@ -1509,7 +1526,6 @@ static int ext4_ext_search_right(struct inode *inode,
15091526
struct ext4_extent_header *eh;
15101527
struct ext4_extent_idx *ix;
15111528
struct ext4_extent *ex;
1512-
ext4_fsblk_t block;
15131529
int depth; /* Note, NOT eh_depth; depth from top of tree */
15141530
int ee_len;
15151531

@@ -1576,20 +1592,17 @@ static int ext4_ext_search_right(struct inode *inode,
15761592
* follow it and find the closest allocated
15771593
* block to the right */
15781594
ix++;
1579-
block = ext4_idx_pblock(ix);
15801595
while (++depth < path->p_depth) {
15811596
/* subtract from p_depth to get proper eh_depth */
1582-
bh = read_extent_tree_block(inode, block,
1583-
path->p_depth - depth, 0);
1597+
bh = read_extent_tree_block(inode, ix, path->p_depth - depth, 0);
15841598
if (IS_ERR(bh))
15851599
return PTR_ERR(bh);
15861600
eh = ext_block_hdr(bh);
15871601
ix = EXT_FIRST_INDEX(eh);
1588-
block = ext4_idx_pblock(ix);
15891602
put_bh(bh);
15901603
}
15911604

1592-
bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
1605+
bh = read_extent_tree_block(inode, ix, path->p_depth - depth, 0);
15931606
if (IS_ERR(bh))
15941607
return PTR_ERR(bh);
15951608
eh = ext_block_hdr(bh);
@@ -2968,9 +2981,9 @@ int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
29682981
ext_debug(inode, "move to level %d (block %llu)\n",
29692982
i + 1, ext4_idx_pblock(path[i].p_idx));
29702983
memset(path + i + 1, 0, sizeof(*path));
2971-
bh = read_extent_tree_block(inode,
2972-
ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2973-
EXT4_EX_NOCACHE);
2984+
bh = read_extent_tree_block(inode, path[i].p_idx,
2985+
depth - i - 1,
2986+
EXT4_EX_NOCACHE);
29742987
if (IS_ERR(bh)) {
29752988
/* should we reset i_size? */
29762989
err = PTR_ERR(bh);

0 commit comments

Comments
 (0)