|
17 | 17 | #include "xfs_fsops.h" |
18 | 18 | #include "xfs_trans_space.h" |
19 | 19 | #include "xfs_log.h" |
| 20 | +#include "xfs_log_priv.h" |
20 | 21 | #include "xfs_ag.h" |
21 | 22 | #include "xfs_ag_resv.h" |
22 | 23 | #include "xfs_trace.h" |
@@ -347,7 +348,7 @@ xfs_fs_counts( |
347 | 348 | cnt->allocino = percpu_counter_read_positive(&mp->m_icount); |
348 | 349 | cnt->freeino = percpu_counter_read_positive(&mp->m_ifree); |
349 | 350 | cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) - |
350 | | - mp->m_alloc_set_aside; |
| 351 | + xfs_fdblocks_unavailable(mp); |
351 | 352 |
|
352 | 353 | spin_lock(&mp->m_sb_lock); |
353 | 354 | cnt->freertx = mp->m_sb.sb_frextents; |
@@ -430,46 +431,36 @@ xfs_reserve_blocks( |
430 | 431 | * If the request is larger than the current reservation, reserve the |
431 | 432 | * blocks before we update the reserve counters. Sample m_fdblocks and |
432 | 433 | * perform a partial reservation if the request exceeds free space. |
| 434 | + * |
| 435 | + * The code below estimates how many blocks it can request from |
| 436 | + * fdblocks to stash in the reserve pool. This is a classic TOCTOU |
| 437 | + * race since fdblocks updates are not always coordinated via |
| 438 | + * m_sb_lock. Set the reserve size even if there's not enough free |
| 439 | + * space to fill it because mod_fdblocks will refill an undersized |
| 440 | + * reserve when it can. |
433 | 441 | */ |
434 | | - error = -ENOSPC; |
435 | | - do { |
436 | | - free = percpu_counter_sum(&mp->m_fdblocks) - |
437 | | - mp->m_alloc_set_aside; |
438 | | - if (free <= 0) |
439 | | - break; |
440 | | - |
441 | | - delta = request - mp->m_resblks; |
442 | | - lcounter = free - delta; |
443 | | - if (lcounter < 0) |
444 | | - /* We can't satisfy the request, just get what we can */ |
445 | | - fdblks_delta = free; |
446 | | - else |
447 | | - fdblks_delta = delta; |
448 | | - |
| 442 | + free = percpu_counter_sum(&mp->m_fdblocks) - |
| 443 | + xfs_fdblocks_unavailable(mp); |
| 444 | + delta = request - mp->m_resblks; |
| 445 | + mp->m_resblks = request; |
| 446 | + if (delta > 0 && free > 0) { |
449 | 447 | /* |
450 | 448 | * We'll either succeed in getting space from the free block |
451 | | - * count or we'll get an ENOSPC. If we get a ENOSPC, it means |
452 | | - * things changed while we were calculating fdblks_delta and so |
453 | | - * we should try again to see if there is anything left to |
454 | | - * reserve. |
| 449 | + * count or we'll get an ENOSPC. Don't set the reserved flag |
| 450 | + * here - we don't want to reserve the extra reserve blocks |
| 451 | + * from the reserve. |
455 | 452 | * |
456 | | - * Don't set the reserved flag here - we don't want to reserve |
457 | | - * the extra reserve blocks from the reserve..... |
| 453 | + * The desired reserve size can change after we drop the lock. |
| 454 | + * Use mod_fdblocks to put the space into the reserve or into |
| 455 | + * fdblocks as appropriate. |
458 | 456 | */ |
| 457 | + fdblks_delta = min(free, delta); |
459 | 458 | spin_unlock(&mp->m_sb_lock); |
460 | 459 | error = xfs_mod_fdblocks(mp, -fdblks_delta, 0); |
| 460 | + if (!error) |
| 461 | + xfs_mod_fdblocks(mp, fdblks_delta, 0); |
461 | 462 | spin_lock(&mp->m_sb_lock); |
462 | | - } while (error == -ENOSPC); |
463 | | - |
464 | | - /* |
465 | | - * Update the reserve counters if blocks have been successfully |
466 | | - * allocated. |
467 | | - */ |
468 | | - if (!error && fdblks_delta) { |
469 | | - mp->m_resblks += fdblks_delta; |
470 | | - mp->m_resblks_avail += fdblks_delta; |
471 | 463 | } |
472 | | - |
473 | 464 | out: |
474 | 465 | if (outval) { |
475 | 466 | outval->resblks = mp->m_resblks; |
@@ -528,8 +519,11 @@ xfs_do_force_shutdown( |
528 | 519 | int tag; |
529 | 520 | const char *why; |
530 | 521 |
|
531 | | - if (test_and_set_bit(XFS_OPSTATE_SHUTDOWN, &mp->m_opstate)) |
| 522 | + |
| 523 | + if (test_and_set_bit(XFS_OPSTATE_SHUTDOWN, &mp->m_opstate)) { |
| 524 | + xlog_shutdown_wait(mp->m_log); |
532 | 525 | return; |
| 526 | + } |
533 | 527 | if (mp->m_sb_bp) |
534 | 528 | mp->m_sb_bp->b_flags |= XBF_DONE; |
535 | 529 |
|
|
0 commit comments