@@ -628,6 +628,25 @@ xfs_alloc_fixup_trees(
628
628
return 0 ;
629
629
}
630
630
631
+ /*
632
+ * We do not verify the AGFL contents against AGF-based index counters here,
633
+ * even though we may have access to the perag that contains shadow copies. We
634
+ * don't know if the AGF based counters have been checked, and if they have they
635
+ * still may be inconsistent because they haven't yet been reset on the first
636
+ * allocation after the AGF has been read in.
637
+ *
638
+ * This means we can only check that all agfl entries contain valid or null
639
+ * values because we can't reliably determine the active range to exclude
640
+ * NULLAGBNO as a valid value.
641
+ *
642
+ * However, we can't even do that for v4 format filesystems because there are
643
+ * old versions of mkfs out there that does not initialise the AGFL to known,
644
+ * verifiable values. HEnce we can't tell the difference between a AGFL block
645
+ * allocated by mkfs and a corrupted AGFL block here on v4 filesystems.
646
+ *
647
+ * As a result, we can only fully validate AGFL block numbers when we pull them
648
+ * from the freelist in xfs_alloc_get_freelist().
649
+ */
631
650
static xfs_failaddr_t
632
651
xfs_agfl_verify (
633
652
struct xfs_buf * bp )
@@ -637,12 +656,6 @@ xfs_agfl_verify(
637
656
__be32 * agfl_bno = xfs_buf_to_agfl_bno (bp );
638
657
int i ;
639
658
640
- /*
641
- * There is no verification of non-crc AGFLs because mkfs does not
642
- * initialise the AGFL to zero or NULL. Hence the only valid part of the
643
- * AGFL is what the AGF says is active. We can't get to the AGF, so we
644
- * can't verify just those entries are valid.
645
- */
646
659
if (!xfs_has_crc (mp ))
647
660
return NULL ;
648
661
@@ -2321,12 +2334,16 @@ xfs_free_agfl_block(
2321
2334
}
2322
2335
2323
2336
/*
2324
- * Check the agfl fields of the agf for inconsistency or corruption. The purpose
2325
- * is to detect an agfl header padding mismatch between current and early v5
2326
- * kernels. This problem manifests as a 1-slot size difference between the
2327
- * on-disk flcount and the active [first, last] range of a wrapped agfl. This
2328
- * may also catch variants of agfl count corruption unrelated to padding. Either
2329
- * way, we'll reset the agfl and warn the user.
2337
+ * Check the agfl fields of the agf for inconsistency or corruption.
2338
+ *
2339
+ * The original purpose was to detect an agfl header padding mismatch between
2340
+ * current and early v5 kernels. This problem manifests as a 1-slot size
2341
+ * difference between the on-disk flcount and the active [first, last] range of
2342
+ * a wrapped agfl.
2343
+ *
2344
+ * However, we need to use these same checks to catch agfl count corruptions
2345
+ * unrelated to padding. This could occur on any v4 or v5 filesystem, so either
2346
+ * way, we need to reset the agfl and warn the user.
2330
2347
*
2331
2348
* Return true if a reset is required before the agfl can be used, false
2332
2349
* otherwise.
@@ -2342,10 +2359,6 @@ xfs_agfl_needs_reset(
2342
2359
int agfl_size = xfs_agfl_size (mp );
2343
2360
int active ;
2344
2361
2345
- /* no agfl header on v4 supers */
2346
- if (!xfs_has_crc (mp ))
2347
- return false;
2348
-
2349
2362
/*
2350
2363
* The agf read verifier catches severe corruption of these fields.
2351
2364
* Repeat some sanity checks to cover a packed -> unpacked mismatch if
@@ -2418,7 +2431,7 @@ xfs_agfl_reset(
2418
2431
* the real allocation can proceed. Deferring the free disconnects freeing up
2419
2432
* the AGFL slot from freeing the block.
2420
2433
*/
2421
- STATIC void
2434
+ static int
2422
2435
xfs_defer_agfl_block (
2423
2436
struct xfs_trans * tp ,
2424
2437
xfs_agnumber_t agno ,
@@ -2437,17 +2450,21 @@ xfs_defer_agfl_block(
2437
2450
xefi -> xefi_blockcount = 1 ;
2438
2451
xefi -> xefi_owner = oinfo -> oi_owner ;
2439
2452
2453
+ if (XFS_IS_CORRUPT (mp , !xfs_verify_fsbno (mp , xefi -> xefi_startblock )))
2454
+ return - EFSCORRUPTED ;
2455
+
2440
2456
trace_xfs_agfl_free_defer (mp , agno , 0 , agbno , 1 );
2441
2457
2442
2458
xfs_extent_free_get_group (mp , xefi );
2443
2459
xfs_defer_add (tp , XFS_DEFER_OPS_TYPE_AGFL_FREE , & xefi -> xefi_list );
2460
+ return 0 ;
2444
2461
}
2445
2462
2446
2463
/*
2447
2464
* Add the extent to the list of extents to be free at transaction end.
2448
2465
* The list is maintained sorted (by block number).
2449
2466
*/
2450
- void
2467
+ int
2451
2468
__xfs_free_extent_later (
2452
2469
struct xfs_trans * tp ,
2453
2470
xfs_fsblock_t bno ,
@@ -2474,6 +2491,9 @@ __xfs_free_extent_later(
2474
2491
#endif
2475
2492
ASSERT (xfs_extfree_item_cache != NULL );
2476
2493
2494
+ if (XFS_IS_CORRUPT (mp , !xfs_verify_fsbext (mp , bno , len )))
2495
+ return - EFSCORRUPTED ;
2496
+
2477
2497
xefi = kmem_cache_zalloc (xfs_extfree_item_cache ,
2478
2498
GFP_KERNEL | __GFP_NOFAIL );
2479
2499
xefi -> xefi_startblock = bno ;
@@ -2497,6 +2517,7 @@ __xfs_free_extent_later(
2497
2517
2498
2518
xfs_extent_free_get_group (mp , xefi );
2499
2519
xfs_defer_add (tp , XFS_DEFER_OPS_TYPE_FREE , & xefi -> xefi_list );
2520
+ return 0 ;
2500
2521
}
2501
2522
2502
2523
#ifdef DEBUG
@@ -2657,7 +2678,9 @@ xfs_alloc_fix_freelist(
2657
2678
goto out_agbp_relse ;
2658
2679
2659
2680
/* defer agfl frees */
2660
- xfs_defer_agfl_block (tp , args -> agno , bno , & targs .oinfo );
2681
+ error = xfs_defer_agfl_block (tp , args -> agno , bno , & targs .oinfo );
2682
+ if (error )
2683
+ goto out_agbp_relse ;
2661
2684
}
2662
2685
2663
2686
targs .tp = tp ;
@@ -2767,6 +2790,9 @@ xfs_alloc_get_freelist(
2767
2790
*/
2768
2791
agfl_bno = xfs_buf_to_agfl_bno (agflbp );
2769
2792
bno = be32_to_cpu (agfl_bno [be32_to_cpu (agf -> agf_flfirst )]);
2793
+ if (XFS_IS_CORRUPT (tp -> t_mountp , !xfs_verify_agbno (pag , bno )))
2794
+ return - EFSCORRUPTED ;
2795
+
2770
2796
be32_add_cpu (& agf -> agf_flfirst , 1 );
2771
2797
xfs_trans_brelse (tp , agflbp );
2772
2798
if (be32_to_cpu (agf -> agf_flfirst ) == xfs_agfl_size (mp ))
@@ -2889,6 +2915,19 @@ xfs_alloc_put_freelist(
2889
2915
return 0 ;
2890
2916
}
2891
2917
2918
+ /*
2919
+ * Verify the AGF is consistent.
2920
+ *
2921
+ * We do not verify the AGFL indexes in the AGF are fully consistent here
2922
+ * because of issues with variable on-disk structure sizes. Instead, we check
2923
+ * the agfl indexes for consistency when we initialise the perag from the AGF
2924
+ * information after a read completes.
2925
+ *
2926
+ * If the index is inconsistent, then we mark the perag as needing an AGFL
2927
+ * reset. The first AGFL update performed then resets the AGFL indexes and
2928
+ * refills the AGFL with known good free blocks, allowing the filesystem to
2929
+ * continue operating normally at the cost of a few leaked free space blocks.
2930
+ */
2892
2931
static xfs_failaddr_t
2893
2932
xfs_agf_verify (
2894
2933
struct xfs_buf * bp )
@@ -2962,7 +3001,6 @@ xfs_agf_verify(
2962
3001
return __this_address ;
2963
3002
2964
3003
return NULL ;
2965
-
2966
3004
}
2967
3005
2968
3006
static void
@@ -3187,7 +3225,8 @@ xfs_alloc_vextent_check_args(
3187
3225
*/
3188
3226
static int
3189
3227
xfs_alloc_vextent_prepare_ag (
3190
- struct xfs_alloc_arg * args )
3228
+ struct xfs_alloc_arg * args ,
3229
+ uint32_t flags )
3191
3230
{
3192
3231
bool need_pag = !args -> pag ;
3193
3232
int error ;
@@ -3196,7 +3235,7 @@ xfs_alloc_vextent_prepare_ag(
3196
3235
args -> pag = xfs_perag_get (args -> mp , args -> agno );
3197
3236
3198
3237
args -> agbp = NULL ;
3199
- error = xfs_alloc_fix_freelist (args , 0 );
3238
+ error = xfs_alloc_fix_freelist (args , flags );
3200
3239
if (error ) {
3201
3240
trace_xfs_alloc_vextent_nofix (args );
3202
3241
if (need_pag )
@@ -3336,7 +3375,7 @@ xfs_alloc_vextent_this_ag(
3336
3375
return error ;
3337
3376
}
3338
3377
3339
- error = xfs_alloc_vextent_prepare_ag (args );
3378
+ error = xfs_alloc_vextent_prepare_ag (args , 0 );
3340
3379
if (!error && args -> agbp )
3341
3380
error = xfs_alloc_ag_vextent_size (args );
3342
3381
@@ -3380,7 +3419,7 @@ xfs_alloc_vextent_iterate_ags(
3380
3419
for_each_perag_wrap_range (mp , start_agno , restart_agno ,
3381
3420
mp -> m_sb .sb_agcount , agno , args -> pag ) {
3382
3421
args -> agno = agno ;
3383
- error = xfs_alloc_vextent_prepare_ag (args );
3422
+ error = xfs_alloc_vextent_prepare_ag (args , flags );
3384
3423
if (error )
3385
3424
break ;
3386
3425
if (!args -> agbp ) {
@@ -3546,7 +3585,7 @@ xfs_alloc_vextent_exact_bno(
3546
3585
return error ;
3547
3586
}
3548
3587
3549
- error = xfs_alloc_vextent_prepare_ag (args );
3588
+ error = xfs_alloc_vextent_prepare_ag (args , 0 );
3550
3589
if (!error && args -> agbp )
3551
3590
error = xfs_alloc_ag_vextent_exact (args );
3552
3591
@@ -3587,7 +3626,7 @@ xfs_alloc_vextent_near_bno(
3587
3626
if (needs_perag )
3588
3627
args -> pag = xfs_perag_grab (mp , args -> agno );
3589
3628
3590
- error = xfs_alloc_vextent_prepare_ag (args );
3629
+ error = xfs_alloc_vextent_prepare_ag (args , 0 );
3591
3630
if (!error && args -> agbp )
3592
3631
error = xfs_alloc_ag_vextent_near (args );
3593
3632
0 commit comments