Skip to content

Commit 1e0272e

Browse files
author
Kent Overstreet
committed
bcachefs: bch_accounting_mode
Minor refactoring - replace multiple bool arguments with an enum; prep work for fixing a bug in accounting read. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 3672bda commit 1e0272e

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

fs/bcachefs/btree_trans_commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
712712
a->k.version = journal_pos_to_bversion(&trans->journal_res,
713713
(u64 *) entry - (u64 *) trans->journal_entries);
714714
BUG_ON(bversion_zero(a->k.version));
715-
ret = bch2_accounting_mem_mod_locked(trans, accounting_i_to_s_c(a), false, false);
715+
ret = bch2_accounting_mem_mod_locked(trans, accounting_i_to_s_c(a), BCH_ACCOUNTING_normal);
716716
if (ret)
717717
goto revert_fs_usage;
718718
}
@@ -798,7 +798,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
798798
struct bkey_s_accounting a = bkey_i_to_s_accounting(entry2->start);
799799

800800
bch2_accounting_neg(a);
801-
bch2_accounting_mem_mod_locked(trans, a.c, false, false);
801+
bch2_accounting_mem_mod_locked(trans, a.c, BCH_ACCOUNTING_normal);
802802
bch2_accounting_neg(a);
803803
}
804804
percpu_up_read(&c->mark_lock);

fs/bcachefs/disk_accounting.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ static int __bch2_accounting_mem_insert(struct bch_fs *c, struct bkey_s_c_accoun
319319
return -BCH_ERR_ENOMEM_disk_accounting;
320320
}
321321

322-
int bch2_accounting_mem_insert(struct bch_fs *c, struct bkey_s_c_accounting a, bool gc)
322+
int bch2_accounting_mem_insert(struct bch_fs *c, struct bkey_s_c_accounting a,
323+
enum bch_accounting_mode mode)
323324
{
324325
struct bch_replicas_padded r;
325326

@@ -566,7 +567,9 @@ int bch2_gc_accounting_done(struct bch_fs *c)
566567
struct { __BKEY_PADDED(k, BCH_ACCOUNTING_MAX_COUNTERS); } k_i;
567568

568569
accounting_key_init(&k_i.k, &acc_k, src_v, nr);
569-
bch2_accounting_mem_mod_locked(trans, bkey_i_to_s_c_accounting(&k_i.k), false, false);
570+
bch2_accounting_mem_mod_locked(trans,
571+
bkey_i_to_s_c_accounting(&k_i.k),
572+
BCH_ACCOUNTING_normal);
570573

571574
preempt_disable();
572575
struct bch_fs_usage_base *dst = this_cpu_ptr(c->usage);
@@ -595,7 +598,8 @@ static int accounting_read_key(struct btree_trans *trans, struct bkey_s_c k)
595598
return 0;
596599

597600
percpu_down_read(&c->mark_lock);
598-
int ret = bch2_accounting_mem_mod_locked(trans, bkey_s_c_to_accounting(k), false, true);
601+
int ret = bch2_accounting_mem_mod_locked(trans, bkey_s_c_to_accounting(k),
602+
BCH_ACCOUNTING_read);
599603
percpu_up_read(&c->mark_lock);
600604

601605
if (bch2_accounting_key_is_zero(bkey_s_c_to_accounting(k)) &&

fs/bcachefs/disk_accounting.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,35 @@ static inline int accounting_pos_cmp(const void *_l, const void *_r)
103103
return bpos_cmp(*l, *r);
104104
}
105105

106-
int bch2_accounting_mem_insert(struct bch_fs *, struct bkey_s_c_accounting, bool);
106+
enum bch_accounting_mode {
107+
BCH_ACCOUNTING_normal,
108+
BCH_ACCOUNTING_gc,
109+
BCH_ACCOUNTING_read,
110+
};
111+
112+
int bch2_accounting_mem_insert(struct bch_fs *, struct bkey_s_c_accounting, enum bch_accounting_mode);
107113
void bch2_accounting_mem_gc(struct bch_fs *);
108114

109115
/*
110116
* Update in memory counters so they match the btree update we're doing; called
111117
* from transaction commit path
112118
*/
113-
static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans, struct bkey_s_c_accounting a, bool gc, bool read)
119+
static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans,
120+
struct bkey_s_c_accounting a,
121+
enum bch_accounting_mode mode)
114122
{
115123
struct bch_fs *c = trans->c;
124+
struct bch_accounting_mem *acc = &c->accounting;
116125
struct disk_accounting_pos acc_k;
117126
bpos_to_disk_accounting_pos(&acc_k, a.k->p);
127+
bool gc = mode == BCH_ACCOUNTING_gc;
128+
129+
EBUG_ON(gc && !acc->gc_running);
118130

119131
if (acc_k.type == BCH_DISK_ACCOUNTING_inum)
120132
return 0;
121133

122-
if (!gc && !read) {
134+
if (mode == BCH_ACCOUNTING_normal) {
123135
switch (acc_k.type) {
124136
case BCH_DISK_ACCOUNTING_persistent_reserved:
125137
trans->fs_usage_delta.reserved += acc_k.persistent_reserved.nr_replicas * a.v->d[0];
@@ -140,14 +152,11 @@ static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans, stru
140152
}
141153
}
142154

143-
struct bch_accounting_mem *acc = &c->accounting;
144155
unsigned idx;
145156

146-
EBUG_ON(gc && !acc->gc_running);
147-
148157
while ((idx = eytzinger0_find(acc->k.data, acc->k.nr, sizeof(acc->k.data[0]),
149158
accounting_pos_cmp, &a.k->p)) >= acc->k.nr) {
150-
int ret = bch2_accounting_mem_insert(c, a, gc);
159+
int ret = bch2_accounting_mem_insert(c, a, mode);
151160
if (ret)
152161
return ret;
153162
}
@@ -164,7 +173,7 @@ static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans, stru
164173
static inline int bch2_accounting_mem_add(struct btree_trans *trans, struct bkey_s_c_accounting a, bool gc)
165174
{
166175
percpu_down_read(&trans->c->mark_lock);
167-
int ret = bch2_accounting_mem_mod_locked(trans, a, gc, false);
176+
int ret = bch2_accounting_mem_mod_locked(trans, a, gc ? BCH_ACCOUNTING_gc : BCH_ACCOUNTING_normal);
168177
percpu_up_read(&trans->c->mark_lock);
169178
return ret;
170179
}

0 commit comments

Comments
 (0)