Accounting for accurate progress reporting
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 first 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 second commit has the format change but does not contain the upgrade/downgrade table entries. It is intended to be integrated together with other accounting changes in a single version upgrade.
The last four commits are drive-by fixes/improvements, especially "Improve check_allocations pass speed not in fsck", which should make the future accounting upgrades much faster.
---
Nikita Ofitserov (12):
bcachefs: Relax restrictions on the number of accounting counters
bcachefs: Introduce btree node number accounting
bcachefs: Use explicit node counts in progress reporting
bcachefs: Introduce btree_leaf_has_triggers_mask
bcachefs: Better progress reporting for btree iteration without leaves
bcachefs: Refactor/rename btree_type_has_ptrs
bcachefs: More accurate progress reporting for inner node iteration
bcachefs: Fix progress reporting for unknown btrees
bcachefs: Partially fix old device removal with unknown btrees
bcachefs: Improve check_allocations pass speed not in fsck
bcachefs: Fix missing c->usage updates from early recovery
bcachefs: Fix online hidden (sb+journal) data accounting
fs/bcachefs/backpointers.c | 6 ++++-- 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 | 23 ++++++++++++++------ fs/bcachefs/disk_accounting.h | 14 +++++++----- 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 ++++++++++- 12 files changed, 136 insertions(+), 48 deletions(-)
---
base-commit: 8bf9cabd6109efc82bdccf6c450102d0be3ed3da
change-id: 20250826-better-progress-835d946a3c38
Best regards,
Signature · 1 line
Nikita Ofitserov
PATCH 1/12 bcachefs: Relax restrictions on the number of accounting counters
Make 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
@@ -212,9 +212,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 2/12 bcachefs: Introduce btree node number accounting
In reply to Nikita OfitserovAdd 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.
This commit is missing the required upgrade/downgrade entries and should be included only together with other accounting updates!
--- fs/bcachefs/buckets.c | 16 +++++++++++----- fs/bcachefs/disk_accounting_format.h | 10 +++++++++- 2 files changed, 20 insertions(+), 6 deletions(-)
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;
Signature · 1 line
2.50.1
PATCH 3/12 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 ; 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 4/12 bcachefs: Introduce btree_leaf_has_triggers_mask
In reply to Nikita Ofitserov--- 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 5/12 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 ; 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 6/12 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
@@ -1228,7 +1228,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
@@ -671,7 +671,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 7/12 bcachefs: More accurate progress reporting for inner node iteration
In reply to Nikita Ofitserov--- fs/bcachefs/backpointers.c | 4 +++- fs/bcachefs/btree_gc.c | 13 +++++-------- fs/bcachefs/migrate.c | 13 +++++++++---- 3 files changed, 17 insertions(+), 13 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 +5 -8
@@ -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;
@@ -784,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++)
@@ -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);
fs/bcachefs/migrate.c +9 -4
@@ -265,10 +265,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
PATCH 8/12 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
@@ -19,7 +19,7 @@ void bch2_progress_init_inner(struct progress_indicator_state *s,const u64 btree_id_mask = leaf_btree_id_mask | inner_btree_id_mask; -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 9/12 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 10/12 bcachefs: Improve check_allocations pass speed 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 11/12 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
PATCH 12/12 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
@@ -1064,13 +1064,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))) {
@@ -1098,7 +1102,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
Quoted 9 lines · On Wed, Aug 27, 2025 at 01:49:06AM +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 first 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.
Quoted 7 lines
The second commit has the format change but does not contain the upgrade/downgrade table entries. It is intended to be integrated together with other accounting changes in a single version upgrade.
The last four commits are drive-by fixes/improvements, especially "Improve
check_allocationspass speed not in fsck", which should make the future accounting upgrades much faster.
Series looks good, except for a couple notes:
- I prefer bugfixes/refactorings to come at the start of a series, so we can get them in right away; I try to push on disk format changes as far back in the series as I can
- The nr_counters check you deleted in bkey_validate has upgrade/downgrade considerations, old versions will blow away the btree counters on downgrade. That means we have to add a downgrade table entry and run check_allocations, on both upgrade and downgrade
- As noted on IRC, by default all on disk format changes with only very simple exceptions should get a new on disk format version; it helps document what changed and it's good hygiene - version numbers are cheap
- We should probably think more about upgrade/downgrade testing: I have upgrade/downgrade tests, but they're not automated since they require disk images that the CI doesn't know about