1 // SPDX-License-Identifier: GPL-2.0
6 /* BCH_SB_FIELD_counters */
8 static const char * const bch2_counter_names[] = {
9 #define x(t, n, ...) (#t),
10 BCH_PERSISTENT_COUNTERS()
15 static size_t bch2_sb_counter_nr_entries(struct bch_sb_field_counters *ctrs)
20 return (__le64 *) vstruct_end(&ctrs->field) - &ctrs->d[0];
23 static int bch2_sb_counters_validate(struct bch_sb *sb,
24 struct bch_sb_field *f,
30 static void bch2_sb_counters_to_text(struct printbuf *out, struct bch_sb *sb,
31 struct bch_sb_field *f)
33 struct bch_sb_field_counters *ctrs = field_to_type(f, counters);
35 unsigned int nr = bch2_sb_counter_nr_entries(ctrs);
37 for (i = 0; i < nr; i++) {
38 if (i < BCH_COUNTER_NR)
39 prt_printf(out, "%s ", bch2_counter_names[i]);
41 prt_printf(out, "(unknown)");
44 prt_printf(out, "%llu", le64_to_cpu(ctrs->d[i]));
49 int bch2_sb_counters_to_cpu(struct bch_fs *c)
51 struct bch_sb_field_counters *ctrs = bch2_sb_field_get(c->disk_sb.sb, counters);
53 unsigned int nr = bch2_sb_counter_nr_entries(ctrs);
56 for (i = 0; i < BCH_COUNTER_NR; i++)
57 c->counters_on_mount[i] = 0;
59 for (i = 0; i < min_t(unsigned int, nr, BCH_COUNTER_NR); i++) {
60 val = le64_to_cpu(ctrs->d[i]);
61 percpu_u64_set(&c->counters[i], val);
62 c->counters_on_mount[i] = val;
67 int bch2_sb_counters_from_cpu(struct bch_fs *c)
69 struct bch_sb_field_counters *ctrs = bch2_sb_field_get(c->disk_sb.sb, counters);
70 struct bch_sb_field_counters *ret;
72 unsigned int nr = bch2_sb_counter_nr_entries(ctrs);
74 if (nr < BCH_COUNTER_NR) {
75 ret = bch2_sb_field_resize(&c->disk_sb, counters,
76 sizeof(*ctrs) / sizeof(u64) + BCH_COUNTER_NR);
80 nr = bch2_sb_counter_nr_entries(ctrs);
85 for (i = 0; i < min_t(unsigned int, nr, BCH_COUNTER_NR); i++)
86 ctrs->d[i] = cpu_to_le64(percpu_u64_get(&c->counters[i]));
90 void bch2_fs_counters_exit(struct bch_fs *c)
92 free_percpu(c->counters);
95 int bch2_fs_counters_init(struct bch_fs *c)
97 c->counters = __alloc_percpu(sizeof(u64) * BCH_COUNTER_NR, sizeof(u64));
99 return -BCH_ERR_ENOMEM_fs_counters_init;
101 return bch2_sb_counters_to_cpu(c);
104 const struct bch_sb_field_ops bch_sb_field_ops_counters = {
105 .validate = bch2_sb_counters_validate,
106 .to_text = bch2_sb_counters_to_text,