Accounting for accurate progress reporting
v2 cover Accounting for accurate progress reporting
This patch series introduces new per-btree accounting counters and uses them for (hopefully) accurate progress reporting in recovery passes. Also includes various assorted bugfixes.
The commit "Relax restrictions on the number of accounting counters" is optional, but will likely greatly improve the upgrade/tools version mismatch experience. Without it, all bree usage accounting will be thrown out and rebuilt on any version mismatch.
The commit "bcachefs_metadata_version_btree_node_accounting" introduces the format change along with upgrade/downgrade table entries.
The first ten commits are drive-by fixes/improvements, especially "Improve check_allocations pass speed not in fsck", which should make future accounting upgrades much faster (including this one).
---
Changes in v2:
- Reordered the format-change-dependent commits to the end
- Added the new metadata version, upgrade and downgrade entries
- Fixed an issue with hidden usage calculation after device removal
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Nikita Ofitserov (15):
bcachefs: Introduce btree_leaf_has_triggers_mask
bcachefs: Refactor bch2_gc_btree/bch2_gc_btrees
bcachefs: Improve check_allocations pass speed when not in fsck
bcachefs: Refactor/rename btree_type_has_ptrs
bcachefs: Fix progress reporting for unknown btrees
bcachefs: Partially fix old device removal with unknown btrees
bcachefs: Fix missing c->usage updates from early recovery
bcachefs: Optimize/fix bch2_dev_usage_remove
bcachefs: Fix online hidden (sb+journal) data accounting
bcachefs: Fix outdated documentation comment
bcachefs: Relax restrictions on the number of accounting counters
bcachefs: bcachefs_metadata_version_btree_node_accounting
bcachefs: Use explicit node counts in progress reporting
bcachefs: Better progress reporting for btree iteration without leaves
bcachefs: More accurate progress reporting for inner node iteration
fs/bcachefs/alloc_background.c | 2 +- fs/bcachefs/backpointers.c | 6 ++-- fs/bcachefs/bcachefs_format.h | 4 +-- fs/bcachefs/btree_gc.c | 28 ++++++++++----- fs/bcachefs/btree_trans_commit.c | 3 ++ fs/bcachefs/btree_types.h | 11 +++--- fs/bcachefs/buckets.c | 16 ++++++--- fs/bcachefs/disk_accounting.c | 70 ++++++++++++++++++++++++++---------- fs/bcachefs/disk_accounting.h | 16 +++++---- fs/bcachefs/disk_accounting_format.h | 10 +++++- fs/bcachefs/migrate.c | 18 ++++++---- fs/bcachefs/move.c | 2 +- fs/bcachefs/progress.c | 41 +++++++++++++++++---- fs/bcachefs/progress.h | 12 ++++++- fs/bcachefs/sb-downgrade.c | 11 ++++-- 15 files changed, 184 insertions(+), 66 deletions(-)
---
base-commit: 753b29cc79897dd7f776fa09654196f5b25f8b96
change-id: 20250826-better-progress-835d946a3c38
Best regards,
Signature · 1 line
Nikita Ofitserov
PATCH v2 1/15 bcachefs: Introduce btree_leaf_has_triggers_mask
--- fs/bcachefs/btree_gc.c | 2 +- fs/bcachefs/btree_types.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
fs/bcachefs/btree_gc.c +1 -1
@@ -720,7 +720,7 @@ static int bch2_gc_btree(struct btree_trans *trans, enum btree_id btree, bool initial) {struct bch_fs *c = trans->c; -unsigned target_depth =btree_node_type_has_triggers (__btree_node_type (0 , btree)) ?0 :1 ; +unsigned target_depth =BIT_ULL (btree) & btree_leaf_has_triggers_mask ?0 :1 ;int ret =0 ;/* We need to make sure every leaf node is readable before going RW */
fs/bcachefs/btree_types.h +3 -0
@@ -840,6 +840,9 @@ static inline bool btree_node_type_has_triggers(enum btree_node_type type)return BIT_ULL (type) &BTREE_NODE_TYPE_HAS_TRIGGERS ; } +/* A mask of btree id bits that have triggers for their leaves */ +static const u64 btree_leaf_has_triggers_mask =BTREE_NODE_TYPE_HAS_TRIGGERS >>1 ; + static const u64 btree_is_extents_mask = 0#define x (name, nr, flags, ...) |((!!((flags) & BTREE_IS_extents)) << nr)BCH_BTREE_IDS ()
Signature · 1 line
2.50.1
PATCH v2 2/15 bcachefs: Refactor bch2_gc_btree/bch2_gc_btrees
In reply to Nikita OfitserovConcentrate iteration policy inside the caller so that accurate progress reporting would be possible.
--- fs/bcachefs/btree_gc.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
fs/bcachefs/btree_gc.c +4 -7
@@ -717,16 +717,12 @@ static int bch2_gc_mark_key(struct btree_trans *trans, enum btree_id btree_id,static int bch2_gc_btree (struct btree_trans *trans,struct progress_indicator_state *progress, -enum btree_id btree,bool initial) +enum btree_id btree,unsigned target_depth, +bool initial) {struct bch_fs *c = trans->c; -unsigned target_depth =BIT_ULL (btree) & btree_leaf_has_triggers_mask ?0 :1 ;int ret =0 ; -/* We need to make sure every leaf node is readable before going RW */ -if (initial) - target_depth =0 ; -for (unsigned level = target_depth; level <BTREE_MAX_DEPTH ; level++) {struct btree *prev =NULL ;struct btree_iter iter;
@@ -797,7 +793,8 @@ static int bch2_gc_btrees(struct bch_fs *c)if (IS_ERR_OR_NULL (bch2_btree_id_root (c, btree)->b))continue ; - ret =bch2_gc_btree (trans, &progress, btree, true); +/* We need to make sure every leaf node is readable before going RW */ + ret =bch2_gc_btree (trans, &progress, btree,0 , true); }bch_err_fn (c, ret);
Signature · 1 line
2.50.1
PATCH v2 3/15 bcachefs: Improve check_allocations pass speed when not in fsck
In reply to Nikita OfitserovSkip reading unnecessary leaf btree nodes unless running fsck. For example, this should speed up version upgrade/downgrade when rebuilding accounting information.
--- fs/bcachefs/btree_gc.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
fs/bcachefs/btree_gc.c +15 -2
@@ -793,8 +793,21 @@ static int bch2_gc_btrees(struct bch_fs *c)if (IS_ERR_OR_NULL (bch2_btree_id_root (c, btree)->b))continue ; -/* We need to make sure every leaf node is readable before going RW */ - ret =bch2_gc_btree (trans, &progress, btree,0 , true); + +unsigned target_depth =BIT_ULL (btree) & btree_leaf_has_triggers_mask ?0 :1 ; + +/* +* In fsck, we need to make sure every leaf node is readable +* before going RW, otherwise we can no longer rewind inside +* btree_lost_data to repair during the current fsck run. +* +* Otherwise, we can delay the repair to the next +* mount or offline fsck. +*/ +if (test_bit (BCH_FS_in_fsck, &c->flags)) + target_depth =0 ; + + ret =bch2_gc_btree (trans, &progress, btree, target_depth, true); }bch_err_fn (c, ret);
Signature · 1 line
2.50.1
PATCH v2 4/15 bcachefs: Refactor/rename btree_type_has_ptrs
In reply to Nikita OfitserovThe new name (btree_type_has_data_ptrs) better reflects the meaning. Also introduce the explicit mask constant.
--- fs/bcachefs/backpointers.c | 2 +- fs/bcachefs/btree_gc.c | 2 +- fs/bcachefs/btree_types.h | 8 ++++---- fs/bcachefs/migrate.c | 2 +- fs/bcachefs/move.c | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-)
fs/bcachefs/backpointers.c +1 -1
@@ -809,7 +809,7 @@ static int bch2_check_extents_to_backpointers_pass(struct btree_trans *trans,for (enum btree_id btree_id =0 ; btree_id <btree_id_nr_alive (c); btree_id++) { -int level, depth =btree_type_has_ptrs (btree_id) ?0 :1 ; +int level, depth =btree_type_has_data_ptrs (btree_id) ?0 :1 ; ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
fs/bcachefs/btree_gc.c +1 -1
@@ -1238,7 +1238,7 @@ int bch2_gc_gens(struct bch_fs *c) }for (unsigned i =0 ; i <BTREE_ID_NR ; i++) -if (btree_type_has_ptrs (i)) { +if (btree_type_has_data_ptrs (i)) { c->gc_gens_btree = i; c->gc_gens_pos =POS_MIN ;
fs/bcachefs/btree_types.h +4 -4
@@ -886,15 +886,15 @@ static inline bool btree_type_has_snapshot_field(enum btree_id btree)return BIT_ULL (btree) & mask; } -static inline bool btree_type_has_ptrs (enum btree_id btree) -{ - const u64 mask = 0 +static const u64 btree_has_data_ptrs_mask = 0#define x (name, nr, flags, ...) |((!!((flags) & BTREE_IS_data)) << nr) BCH_BTREE_IDS()#undef x ; -return BIT_ULL (btree) & mask; +static inline bool btree_type_has_data_ptrs (enum btree_id btree) +{ +return BIT_ULL (btree) & btree_has_data_ptrs_mask; }static inline bool btree_type_uses_write_buffer (enum btree_id btree)
fs/bcachefs/migrate.c +1 -1
@@ -122,7 +122,7 @@ static int bch2_dev_usrdata_drop(struct bch_fs *c,CLASS (btree_trans, trans)(c);for (unsigned id =0 ; id <BTREE_ID_NR ; id++) { -if (!btree_type_has_ptrs (id)) +if (!btree_type_has_data_ptrs (id))continue ;/* Stripe keys have pointers, but are handled separately */
fs/bcachefs/move.c +1 -1
@@ -820,7 +820,7 @@ static int bch2_move_data(struct bch_fs *c,unsigned min_depth_this_btree = min_depth;/* Stripe keys have pointers, but are handled separately */ -if (!btree_type_has_ptrs (id) || +if (!btree_type_has_data_ptrs (id) || id == BTREE_ID_stripes) min_depth_this_btree =max (min_depth_this_btree,1 );
Signature · 1 line
2.50.1
PATCH v2 5/15 bcachefs: Fix progress reporting for unknown btrees
In reply to Nikita Ofitserov--- fs/bcachefs/progress.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/progress.c +1 -1
@@ -12,7 +12,7 @@ void bch2_progress_init(struct progress_indicator_state *s, s->next_print = jiffies +HZ *10 ; -for (unsigned i =0 ; i <BTREE_ID_NR ; i++) { +for (unsigned i =0 ; i <btree_id_nr_alive (c); i++) {if (!(btree_id_mask &BIT_ULL (i)))continue ;
Signature · 1 line
2.50.1
PATCH v2 6/15 bcachefs: Partially fix old device removal with unknown btrees
In reply to Nikita OfitserovHandle removal of unknown metadata only, data pointed to by unknown btrees cannot be supported currently.
--- fs/bcachefs/migrate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/bcachefs/migrate.c +2 -1
@@ -121,6 +121,7 @@ static int bch2_dev_usrdata_drop(struct bch_fs *c, {CLASS (btree_trans, trans)(c); +/* FIXME: this does not handle unknown btrees with data pointers */ for (unsigned id =0 ; id <BTREE_ID_NR ; id++) {if (!btree_type_has_data_ptrs (id))continue ;
@@ -161,7 +162,7 @@ static int bch2_dev_metadata_drop(struct bch_fs *c,bch2_bkey_buf_init (&k);closure_init_stack (&cl); -for (id =0 ; id <BTREE_ID_NR ; id++) { +for (id =0 ; id <btree_id_nr_alive (c); id++) {bch2_trans_node_iter_init (trans, &iter, id,POS_MIN ,0 ,0 , BTREE_ITER_prefetch); retry:
Signature · 1 line
2.50.1
PATCH v2 7/15 bcachefs: Fix missing c->usage updates from early recovery
In reply to Nikita OfitserovMake do_bch2_trans_commit_to_journal_replay apply global FS usage updates stored in trans->fs_usage_delta, too.
--- fs/bcachefs/btree_trans_commit.c | 3 +++ 1 file changed, 3 insertions(+)
fs/bcachefs/btree_trans_commit.c +3 -0
@@ -970,6 +970,7 @@ do_bch2_trans_commit_to_journal_replay(struct btree_trans *trans,struct bkey_i *accounting; retry: +memset (&trans->fs_usage_delta,0 ,sizeof (trans->fs_usage_delta));percpu_down_read (&c->mark_lock); for (accounting = btree_trans_subbuf_base(trans, &trans->accounting); accounting != btree_trans_subbuf_top(trans, &trans->accounting);
@@ -981,6 +982,8 @@ do_bch2_trans_commit_to_journal_replay(struct btree_trans *trans,if (ret) goto revert_fs_usage; } +/* Only fatal errors are possible later, so no need to revert this */ +bch2_trans_account_disk_usage_change (trans);percpu_up_read (&c->mark_lock);trans_for_each_update (trans, i) {
Signature · 1 line
2.50.1
Re: [PATCH v2 07/15] bcachefs: Fix missing c->usage updates from early recovery
Quoted 6 lines · On Thu, Aug 28, 2025 at 05:16:09PM +0300, Nikita Ofitserov via B4 Relay wrote:
From: Nikita Ofitserov <[email protected]>
Make
do_bch2_trans_commit_to_journal_replayapply global FS usage updates stored intrans->fs_usage_delta, too.Signed-off-by Nikita Ofitserov <[email protected]>
kicked this patch out for now - run a lockdep test
Quoted 30 lines
--- fs/bcachefs/btree_trans_commit.c | 3 +++ 1 file changed, 3 insertions(+)fs/bcachefs/btree_trans_commit.c +3
-0@@ -970,6 +970,7 @@ do_bch2_trans_commit_to_journal_replay(struct btree_trans *trans,struct bkey_i *accounting; retry: +memset (&trans->fs_usage_delta,0 ,sizeof (trans->fs_usage_delta));percpu_down_read (&c->mark_lock); for (accounting = btree_trans_subbuf_base(trans, &trans->accounting); accounting != btree_trans_subbuf_top(trans, &trans->accounting);@@ -981,6 +982,8 @@ do_bch2_trans_commit_to_journal_replay(struct btree_trans *trans,if (ret) goto revert_fs_usage; } +/* Only fatal errors are possible later, so no need to revert this */ +bch2_trans_account_disk_usage_change (trans);percpu_up_read (&c->mark_lock);trans_for_each_update (trans, i) {Signature · 2 lines
-- 2.50.1
PATCH v2 8/15 bcachefs: Optimize/fix bch2_dev_usage_remove
In reply to Nikita OfitserovMake bch2_dev_usage_remove iterate only through relevant keys instead of the whole accounting tree. Use the proper bch2_disk_accounting_mod API to ensure in-memory counters will be zeroed and cleaned up later.
--- fs/bcachefs/alloc_background.c | 2 +- fs/bcachefs/disk_accounting.c | 47 +++++++++++++++++++++++++++++++----------- fs/bcachefs/disk_accounting.h | 2 +- 3 files changed, 37 insertions(+), 14 deletions(-)
fs/bcachefs/alloc_background.c +1 -1
@@ -2397,7 +2397,7 @@ int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) BTREE_TRIGGER_norun, NULL) ?:bch2_btree_delete_range (c, BTREE_ID_alloc, start, end, BTREE_TRIGGER_norun,NULL ) ?: -bch2_dev_usage_remove (c, ca->dev_idx); +bch2_dev_usage_remove (c, ca);bch_err_msg (ca, ret,"removing dev alloc info" );return ret; }
fs/bcachefs/disk_accounting.c +35 -12
56 lines not on this page · raw message
fs/bcachefs/disk_accounting.h +1 -1
@@ -297,7 +297,7 @@ int bch2_gc_accounting_done(struct bch_fs *);int bch2_accounting_read (struct bch_fs *); -int bch2_dev_usage_remove (struct bch_fs *,unsigned ); +int bch2_dev_usage_remove (struct bch_fs *,struct bch_dev *);int bch2_dev_usage_init (struct bch_dev *,bool );void bch2_verify_accounting_clean (struct bch_fs *c);
Signature · 1 line
2.50.1
PATCH v2 9/15 bcachefs: Fix online hidden (sb+journal) data accounting
In reply to Nikita OfitserovNow the c->usage->hidden counters are kept up to date by the same trigger-based mechanism as the other ones.
--- fs/bcachefs/disk_accounting.c | 12 ++++++++---- fs/bcachefs/disk_accounting.h | 10 +++++++--- 2 files changed, 15 insertions(+), 7 deletions(-)
fs/bcachefs/disk_accounting.c +8 -4
@@ -1082,13 +1082,17 @@ void bch2_verify_accounting_clean(struct bch_fs *c)case BCH_DISK_ACCOUNTING_dev_data_type: { {guard (rcu)();/* scoped guard is a loop, and doesn't play nicely with continue */ +const enum bch_data_type data_type = acc_k.dev_data_type.data_type;struct bch_dev *ca =bch2_dev_rcu_noerror (c, acc_k.dev_data_type.dev);if (!ca)continue ; - v[0 ] =percpu_u64_get (&ca->usage->d[acc_k.dev_data_type.data_type].buckets); - v[1 ] =percpu_u64_get (&ca->usage->d[acc_k.dev_data_type.data_type].sectors); - v[2 ] =percpu_u64_get (&ca->usage->d[acc_k.dev_data_type.data_type].fragmented); + v[0 ] =percpu_u64_get (&ca->usage->d[data_type].buckets); + v[1 ] =percpu_u64_get (&ca->usage->d[data_type].sectors); + v[2 ] =percpu_u64_get (&ca->usage->d[data_type].fragmented); + +if (data_type == BCH_DATA_sb || data_type == BCH_DATA_journal) + base.hidden += a.v->d[0 ] * ca->mi.bucket_size; }if (memcmp (a.v->d, v,3 *sizeof (u64))) {
@@ -1116,7 +1120,7 @@ void bch2_verify_accounting_clean(struct bch_fs *c) mismatch = true; \ } -//check(hidden); +check (hidden);check (btree);check (data);check (cached);
fs/bcachefs/disk_accounting.h +7 -3
@@ -186,11 +186,15 @@ static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans,break ;case BCH_DISK_ACCOUNTING_dev_data_type: {guard (rcu)(); +const enum bch_data_type data_type = acc_k.dev_data_type.data_type;struct bch_dev *ca =bch2_dev_rcu_noerror (c, acc_k.dev_data_type.dev);if (ca) { -this_cpu_add (ca->usage->d[acc_k.dev_data_type.data_type].buckets, a.v->d[0 ]); -this_cpu_add (ca->usage->d[acc_k.dev_data_type.data_type].sectors, a.v->d[1 ]); -this_cpu_add (ca->usage->d[acc_k.dev_data_type.data_type].fragmented, a.v->d[2 ]); +this_cpu_add (ca->usage->d[data_type].buckets, a.v->d[0 ]); +this_cpu_add (ca->usage->d[data_type].sectors, a.v->d[1 ]); +this_cpu_add (ca->usage->d[data_type].fragmented, a.v->d[2 ]); + +if (data_type == BCH_DATA_sb || data_type == BCH_DATA_journal) + trans->fs_usage_delta.hidden += a.v->d[0 ] * ca->mi.bucket_size; }break ; }
Signature · 1 line
2.50.1
Re: [PATCH v2 09/15] bcachefs: Fix online hidden (sb+journal) data accounting
Quoted 4 lines · On Thu, Aug 28, 2025 at 05:16:11PM +0300, Nikita Ofitserov via B4 Relay wrote:
From: Nikita Ofitserov <[email protected]>
Now the
c->usage->hiddencounters are kept up to date by the same trigger-based mechanism as the other ones.
This pops warnings in accounting_verify_clean()
00133 ------------[ cut here ]------------
00133 WARNING: CPU: 8 PID: 595 at fs/bcachefs/disk_accounting.c:1144 bch2_verify_accounting_clean+0x8fc/0xac0 00133 Modules linked in:
00133 CPU: 8 UID: 0 PID: 595 Comm: mount.bcachefs Not tainted 6.17.0-rc3-ktest-g92dce30a5a04 #32156 NONE 00133 Hardware name: linux,dummy-virt (DT)
00133 pstate: 60001005 (nZCv daif -PAN -UAO -TCO -DIT +SSBS BTYPE=--)
00133 pc : bch2_verify_accounting_clean+0x8fc/0xac0
00133 lr : bch2_verify_accounting_clean+0xa60/0xac0
00133 sp : ffffffc081e0b9d0
00133 x29: ffffffc081e0bbe0 x28: ffffffc081e0ba70 x27: ffffffc080e59fc0
00133 x26: 0000000000000008 x25: 0000000000000003 x24: 0000000000000000
00133 x23: ffffff80e6220000 x22: ffffffc081e0bb08 x21: 0000000000000046
00133 x20: 0000000000000000 x19: ffffff80e4540000 x18: 00000000ffffffff
00133 x17: 696d206e65646469 x16: 682e657361625f65 x15: 676173755f736620
00133 x14: 29286e61656c635f x13: 0000000000000000 x12: ffffff80fadeffa8
00133 x11: ffffff80f97f0000 x10: ffffff80fadf0000 x9 : ffffffc0800a86f0
00133 x8 : 0000000000000001 x7 : 00000000005fffe8 x6 : 0000000000000001
00133 x5 : ffffff80fbcf9c90 x4 : 0000000000000000 x3 : 0000000000000000
00133 x2 : 0000000000000000 x1 : ffffff80c1576000 x0 : 0000000000000001
00133 Call trace:
00133 bch2_verify_accounting_clean+0x8fc/0xac0 (P) 00133 bch2_fs_read_only+0x2ec/0x358 00133 bch2_fs_reconfigure+0x98/0x128 00133 reconfigure_super+0x98/0x200 00133 path_mount+0x840/0xab8 00133 __arm64_sys_mount+0x160/0x2c0 00133 invoke_syscall.constprop.0+0x54/0xe0 00133 do_el0_svc+0x44/0xe0 00133 el0_svc+0x18/0x58 00133 el0t_64_sync_handler+0x98/0xe0 00133 el0t_64_sync+0x154/0x158
00133 ---[ end trace 0000000000000000 ]---
Quoted 69 lines
Signed-off-by Nikita Ofitserov <[email protected]>--- fs/bcachefs/disk_accounting.c | 12 ++++++++---- fs/bcachefs/disk_accounting.h | 10 +++++++--- 2 files changed, 15 insertions(+), 7 deletions(-)fs/bcachefs/disk_accounting.c +8
-4@@ -1082,13 +1082,17 @@ void bch2_verify_accounting_clean(struct bch_fs *c)case BCH_DISK_ACCOUNTING_dev_data_type: { {guard (rcu)();/* scoped guard is a loop, and doesn't play nicely with continue */ +const enum bch_data_type data_type = acc_k.dev_data_type.data_type;struct bch_dev *ca =bch2_dev_rcu_noerror (c, acc_k.dev_data_type.dev);if (!ca)continue ; - v[0 ] =percpu_u64_get (&ca->usage->d[acc_k.dev_data_type.data_type].buckets); - v[1 ] =percpu_u64_get (&ca->usage->d[acc_k.dev_data_type.data_type].sectors); - v[2 ] =percpu_u64_get (&ca->usage->d[acc_k.dev_data_type.data_type].fragmented); + v[0 ] =percpu_u64_get (&ca->usage->d[data_type].buckets); + v[1 ] =percpu_u64_get (&ca->usage->d[data_type].sectors); + v[2 ] =percpu_u64_get (&ca->usage->d[data_type].fragmented); + +if (data_type == BCH_DATA_sb || data_type == BCH_DATA_journal) + base.hidden += a.v->d[0 ] * ca->mi.bucket_size; }if (memcmp (a.v->d, v,3 *sizeof (u64))) {@@ -1116,7 +1120,7 @@ void bch2_verify_accounting_clean(struct bch_fs *c) mismatch = true; \ } -//check(hidden); +check (hidden);check (btree);check (data);check (cached);fs/bcachefs/disk_accounting.h +7
-3@@ -186,11 +186,15 @@ static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans,break ;case BCH_DISK_ACCOUNTING_dev_data_type: {guard (rcu)(); +const enum bch_data_type data_type = acc_k.dev_data_type.data_type;struct bch_dev *ca =bch2_dev_rcu_noerror (c, acc_k.dev_data_type.dev);if (ca) { -this_cpu_add (ca->usage->d[acc_k.dev_data_type.data_type].buckets, a.v->d[0 ]); -this_cpu_add (ca->usage->d[acc_k.dev_data_type.data_type].sectors, a.v->d[1 ]); -this_cpu_add (ca->usage->d[acc_k.dev_data_type.data_type].fragmented, a.v->d[2 ]); +this_cpu_add (ca->usage->d[data_type].buckets, a.v->d[0 ]); +this_cpu_add (ca->usage->d[data_type].sectors, a.v->d[1 ]); +this_cpu_add (ca->usage->d[data_type].fragmented, a.v->d[2 ]); + +if (data_type == BCH_DATA_sb || data_type == BCH_DATA_journal) + trans->fs_usage_delta.hidden += a.v->d[0 ] * ca->mi.bucket_size; }break ; }Signature · 2 lines
-- 2.50.1
PATCH v2 10/15 bcachefs: Fix outdated documentation comment
In reply to Nikita Ofitserov--- fs/bcachefs/bcachefs_format.h | 1 - 1 file changed, 1 deletion(-)
fs/bcachefs/bcachefs_format.h +0 -1
@@ -654,7 +654,6 @@ struct bch_sb_field_ext {/* * field 1: version name * field 2: BCH_VERSION(major, minor) -* field 3: recovery passess required on upgrade */ #define BCH_METADATA_VERSIONS () \ x(bkey_renumber, BCH_VERSION(0, 10)) \
Signature · 1 line
2.50.1
PATCH v2 11/15 bcachefs: Relax restrictions on the number of accounting counters
In reply to Nikita OfitserovMake adding/removing accounting counters easier when upgrading by removing their exact number from bch_accounting key validity invariants.
--- fs/bcachefs/disk_accounting.c | 11 ++++++++--- fs/bcachefs/disk_accounting.h | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-)
fs/bcachefs/disk_accounting.c +8 -3
@@ -239,10 +239,12 @@ int bch2_accounting_validate(struct bch_fs *c, struct bkey_s_c k, c, accounting_key_junk_at_end, "junk at end of accounting key"); -bkey_fsck_err_on (bch2_accounting_counters (k.k) != bch2_accounting_type_nr_counters[acc_k.type], +const unsigned nr_counters =bch2_accounting_counters (k.k); + +bkey_fsck_err_on (!nr_counters || nr_counters >BCH_ACCOUNTING_MAX_COUNTERS , c, accounting_key_nr_counters_wrong,"accounting key with %u counters, should be %u" , -bch2_accounting_counters (k.k), bch2_accounting_type_nr_counters[acc_k.type]); + nr_counters, bch2_accounting_type_nr_counters[acc_k.type]); fsck_err:return ret; }
@@ -359,10 +361,13 @@ static int __bch2_accounting_mem_insert(struct bch_fs *c, struct bkey_s_c_accoun accounting_pos_cmp, &a.k->p) < acc->k.nr)return 0 ; +struct disk_accounting_pos acc_k; +bpos_to_disk_accounting_pos (&acc_k, a.k->p); +struct accounting_mem_entry n = { .pos = a.k->p, .bversion = a.k->bversion, - .nr_counters =bch2_accounting_counters (a.k), + .nr_counters = bch2_accounting_type_nr_counters[acc_k.type], .v[0 ] =__alloc_percpu_gfp (n.nr_counters *sizeof (u64),sizeof (u64),GFP_KERNEL ), };
fs/bcachefs/disk_accounting.h +2 -2
@@ -216,9 +216,9 @@ static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans,struct accounting_mem_entry *e = &acc->k.data[idx]; -EBUG_ON (bch2_accounting_counters (a.k) != e->nr_counters); +const unsigned nr =min_t (unsigned,bch2_accounting_counters (a.k), e->nr_counters); -for (unsigned i =0 ; i <bch2_accounting_counters (a.k); i++) +for (unsigned i =0 ; i < nr; i++)this_cpu_add (e->v[gc][i], a.v->d[i]);return 0 ; }
Signature · 1 line
2.50.1
PATCH v2 12/15 bcachefs: bcachefs_metadata_version_btree_node_accounting
In reply to Nikita OfitserovIntroduce btree node number accounting for better progress reporting. This change includes a mandatory upgrade/downgrade.
Add 2 new counters for BCH_DISK_ACCOUNTING_btree: total number of btree nodes (ignoring replication) and the number of non-leaf btree nodes (likewise). Those are to be used by recovery progress reporting instead of estimating them.
--- fs/bcachefs/bcachefs_format.h | 3 ++- fs/bcachefs/buckets.c | 16 +++++++++++----- fs/bcachefs/disk_accounting_format.h | 10 +++++++++- fs/bcachefs/sb-downgrade.c | 11 +++++++++-- 4 files changed, 31 insertions(+), 9 deletions(-)
fs/bcachefs/bcachefs_format.h +2 -1
@@ -706,7 +706,8 @@ struct bch_sb_field_ext { x(fast_device_removal, BCH_VERSION(1, 27)) \ x(inode_has_case_insensitive, BCH_VERSION(1, 28)) \ x(extent_snapshot_whiteouts, BCH_VERSION(1, 29)) \ - x(31bit_dirent_offset, BCH_VERSION(1, 30)) + x(31bit_dirent_offset, BCH_VERSION(1, 30)) \ + x(btree_node_accounting, BCH_VERSION(1, 31))enum bcachefs_metadata_version { bcachefs_metadata_version_min =9 ,
fs/bcachefs/buckets.c +11 -5
@@ -749,6 +749,7 @@ static int __trigger_extent(struct btree_trans *trans, enum btree_iter_update_trigger_flags flags) {bool gc = flags & BTREE_TRIGGER_gc; +bool insert = !(flags & BTREE_TRIGGER_overwrite);struct bkey_ptrs_c ptrs =bch2_bkey_ptrs_c (k);const union bch_extent_entry *entry;struct extent_ptr_decoded p;
@@ -802,7 +803,7 @@ static int __trigger_extent(struct btree_trans *trans,if (cur_compression_type && cur_compression_type != p.crc.compression_type) { -if (flags & BTREE_TRIGGER_overwrite) +if (!insert)bch2_u64s_neg (compression_acct,ARRAY_SIZE (compression_acct)); ret = bch2_disk_accounting_mod2(trans, gc, compression_acct,
@@ -835,7 +836,7 @@ static int __trigger_extent(struct btree_trans *trans, }if (cur_compression_type) { -if (flags & BTREE_TRIGGER_overwrite) +if (!insert)bch2_u64s_neg (compression_acct,ARRAY_SIZE (compression_acct)); ret = bch2_disk_accounting_mod2(trans, gc, compression_acct,
@@ -845,12 +846,17 @@ static int __trigger_extent(struct btree_trans *trans, }if (level) { - ret =bch2_disk_accounting_mod2_nr (trans, gc, &replicas_sectors,1 , btree, btree_id); +const bool leaf_node = level ==1 ; +s64 v[3 ] = { + replicas_sectors, + insert ?1 :-1 , + !leaf_node ? (insert ?1 :-1 ) :0 , + }; + + ret =bch2_disk_accounting_mod2 (trans, gc, v, btree, btree_id);if (ret)return ret; }else { -bool insert = !(flags & BTREE_TRIGGER_overwrite); -s64 v[3 ] = { insert ?1 :-1 , insert ? k.k->size : -((s64 ) k.k->size),
fs/bcachefs/disk_accounting_format.h +9 -1
@@ -108,7 +108,7 @@ static inline bool data_type_is_hidden(enum bch_data_type type) x(dev_data_type, 3, 3) \ x(compression, 4, 3) \ x(snapshot, 5, 1) \ - x(btree, 6, 1) \ + x(btree, 6, 3) \ x(rebalance_work, 7, 1) \x (inum,8 ,3 )
@@ -174,6 +174,14 @@ struct bch_acct_snapshot {__u32 id; } __packed; +/* +* Metadata accounting per btree id: +* [ +* total btree disk usage in sectors +* total number of btree nodes +* number of non-leaf btree nodes +* ] +*/ struct bch_acct_btree {__u32 id; } __packed;
fs/bcachefs/sb-downgrade.c +9 -2
@@ -104,7 +104,10 @@x (inode_has_case_insensitive, \BIT_ULL (BCH_RECOVERY_PASS_check_inodes), \ BCH_FSCK_ERR_inode_has_case_insensitive_not_set, \ - BCH_FSCK_ERR_inode_parent_has_case_insensitive_not_set) + BCH_FSCK_ERR_inode_parent_has_case_insensitive_not_set)\ +x (btree_node_accounting, \ +BIT_ULL (BCH_RECOVERY_PASS_check_allocations), \ + BCH_FSCK_ERR_accounting_mismatch)#define DOWNGRADE_TABLE () \ x(bucket_stripe_sectors, \
@@ -152,7 +155,11 @@BIT_ULL (BCH_RECOVERY_PASS_check_allocations), \ BCH_FSCK_ERR_accounting_mismatch, \ BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0, \ - BCH_FSCK_ERR_accounting_key_junk_at_end) + BCH_FSCK_ERR_accounting_key_junk_at_end) \ + x(btree_node_accounting, \ + BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \ + BCH_FSCK_ERR_accounting_mismatch, \ + BCH_FSCK_ERR_accounting_key_nr_counters_wrong) struct upgrade_downgrade_entry { u64 recovery_passes;
Signature · 1 line
2.50.1
PATCH v2 13/15 bcachefs: Use explicit node counts in progress reporting
In reply to Nikita OfitserovAlso, consider the metadata_replicas option when better accounting is not available.
--- fs/bcachefs/progress.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)
fs/bcachefs/progress.c +21 -3
@@ -12,6 +12,10 @@ void bch2_progress_init(struct progress_indicator_state *s, s->next_print = jiffies +HZ *10 ; +/* This is only an estimation: nodes can have different replica counts */ +const u32 expected_node_disk_sectors = +READ_ONCE (c->opts.metadata_replicas) *btree_sectors (c); +for (unsigned i =0 ; i <btree_id_nr_alive (c); i++) {if (!(btree_id_mask &BIT_ULL (i)))continue ;
@@ -19,9 +23,23 @@ void bch2_progress_init(struct progress_indicator_state *s,struct disk_accounting_pos acc; disk_accounting_key_init(acc, btree, .id = i); -u64 v; -bch2_accounting_mem_read (c,disk_accounting_pos_to_bpos (&acc), &v,1 ); - s->nodes_total +=div64_ul (v,btree_sectors (c)); +struct { +u64 disk_sectors; +u64 total_nodes; +u64 inner_nodes; + } v = {0 }; +bch2_accounting_mem_read (c,disk_accounting_pos_to_bpos (&acc), + (u64 *)&v,sizeof (v) /sizeof (u64)); + +/* +* We check for zeros to degrade gracefully when run +* with un-upgraded accounting info (missing some counters). +*/ + +if (v.total_nodes !=0 ) + s->nodes_total += v.total_nodes - v.inner_nodes; +else + s->nodes_total +=div_u64 (v.disk_sectors, expected_node_disk_sectors); } }
Signature · 1 line
2.50.1
PATCH v2 14/15 bcachefs: Better progress reporting for btree iteration without leaves
In reply to Nikita Ofitserov--- fs/bcachefs/progress.c | 17 +++++++++++++---- fs/bcachefs/progress.h | 12 +++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-)
fs/bcachefs/progress.c +13 -4
@@ -4,9 +4,10 @@#include "disk_accounting.h" #include "progress.h" -void bch2_progress_init (struct progress_indicator_state *s, -struct bch_fs *c, -u64 btree_id_mask) +void bch2_progress_init_inner (struct progress_indicator_state *s, +struct bch_fs *c, +u64 leaf_btree_id_mask, +u64 inner_btree_id_mask) {memset (s,0 ,sizeof (*s));
@@ -16,6 +17,8 @@ void bch2_progress_init(struct progress_indicator_state *s,const u32 expected_node_disk_sectors =READ_ONCE (c->opts.metadata_replicas) *btree_sectors (c); +const u64 btree_id_mask = leaf_btree_id_mask | inner_btree_id_mask; +for (unsigned i =0 ; i <btree_id_nr_alive (c); i++) {if (!(btree_id_mask &BIT_ULL (i)))continue ;
@@ -31,11 +34,17 @@ void bch2_progress_init(struct progress_indicator_state *s,bch2_accounting_mem_read (c,disk_accounting_pos_to_bpos (&acc), (u64 *)&v,sizeof (v) /sizeof (u64)); +/* Better to estimate as 0 than the total node count */ +if (inner_btree_id_mask &BIT_ULL (i)) + s->nodes_total += v.inner_nodes; + +if (!(leaf_btree_id_mask &BIT_ULL (i))) +continue ; +/* * We check for zeros to degrade gracefully when run * with un-upgraded accounting info (missing some counters). */ -if (v.total_nodes !=0 ) s->nodes_total += v.total_nodes - v.inner_nodes;else
fs/bcachefs/progress.h +11 -1
@@ -20,7 +20,17 @@ struct progress_indicator_state {struct btree *last_node; }; -void bch2_progress_init (struct progress_indicator_state *,struct bch_fs *,u64 ); +void bch2_progress_init_inner (struct progress_indicator_state *s, +struct bch_fs *c, +u64 leaf_btree_id_mask, +u64 inner_btree_id_mask); + +static inline void bch2_progress_init (struct progress_indicator_state *s, +struct bch_fs *c,u64 btree_id_mask) +{ +bch2_progress_init_inner (s, c, btree_id_mask,0 ); +} + void bch2_progress_update_iter(struct btree_trans *, struct progress_indicator_state *, struct btree_iter *,
Signature · 1 line
2.50.1
PATCH v2 15/15 bcachefs: More accurate progress reporting for inner node iteration
In reply to Nikita Ofitserov--- fs/bcachefs/backpointers.c | 4 +++- fs/bcachefs/btree_gc.c | 2 +- fs/bcachefs/migrate.c | 13 +++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-)
fs/bcachefs/backpointers.c +3 -1
@@ -804,7 +804,9 @@ static int bch2_check_extents_to_backpointers_pass(struct btree_trans *trans,struct progress_indicator_state progress;int ret =0 ; -bch2_progress_init (&progress, trans->c,BIT_ULL (BTREE_ID_extents)|BIT_ULL (BTREE_ID_reflink)); +bch2_progress_init_inner (&progress, trans->c, + btree_has_data_ptrs_mask, + ~0ULL ); for (enum btree_id btree_id = 0; btree_id < btree_id_nr_alive(c);
fs/bcachefs/btree_gc.c +1 -1
@@ -780,7 +780,7 @@ static int bch2_gc_btrees(struct bch_fs *c)int ret =0 ;struct progress_indicator_state progress; -bch2_progress_init (&progress, c, ~0ULL ); +bch2_progress_init_inner (&progress, c, ~0ULL , ~0ULL );enum btree_id ids[BTREE_ID_NR ];for (unsigned i =0 ; i <BTREE_ID_NR ; i++)
fs/bcachefs/migrate.c +9 -4
@@ -266,10 +266,15 @@ int bch2_dev_data_drop_by_backpointers(struct bch_fs *c, unsigned dev_idx, unsigint bch2_dev_data_drop (struct bch_fs *c,unsigned dev_idx,unsigned flags) {struct progress_indicator_state progress; +int ret; +bch2_progress_init (&progress, c, -BIT_ULL (BTREE_ID_extents)| -BIT_ULL (BTREE_ID_reflink)); + btree_has_data_ptrs_mask & ~BIT_ULL (BTREE_ID_stripes)); + +if ((ret =bch2_dev_usrdata_drop (c, &progress, dev_idx, flags))) +return ret; + +bch2_progress_init_inner (&progress, c,0 , ~0ULL ); -return bch2_dev_usrdata_drop (c, &progress, dev_idx, flags) ?: -bch2_dev_metadata_drop (c, &progress, dev_idx, flags); +return bch2_dev_metadata_drop (c, &progress, dev_idx, flags); }
Signature · 1 line
2.50.1
Quoted 16 lines · On Thu, Aug 28, 2025 at 05:16:02PM +0300, Nikita Ofitserov via B4 Relay wrote:
This patch series introduces new per-btree accounting counters and uses them for (hopefully) accurate progress reporting in recovery passes. Also includes various assorted bugfixes.
The commit "Relax restrictions on the number of accounting counters" is optional, but will likely greatly improve the upgrade/tools version mismatch experience. Without it, all bree usage accounting will be thrown out and rebuilt on any version mismatch.
The commit "
bcachefs_metadata_version_btree_node_accounting" introduces the format change along with upgrade/downgrade table entries.The first ten commits are drive-by fixes/improvements, especially "Improve
check_allocationspass speed not in fsck", which should make future accounting upgrades much faster (including this one).
Quoted 7 lines
Signed-off-by Nikita Ofitserov <[email protected]>---
Changes in v2:
- Reordered the format-change-dependent commits to the end
- Added the new metadata version, upgrade and downgrade entries
- Fixed an issue with hidden usage calculation after device removal
- Link to v1: https://lore.kernel.org/r/[email protected]
https://evilpiepirate.org/~testdashboard/ci?user=nofitserov&branch=nofitserov
Every patch before "Relax restrictions on the number of accounting counters" looks good according to the CI; after that the test dashboard explodes:
https://evilpiepirate.org/~testdashboard/ci?user=nofitserov&branch=nofitserov
And those look like clean and simple bugfixes/refactorings, so I'm going to pull those into testing.