1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "super-io.h"
4 #include "sb-counters.h"
5 
6 /* BCH_SB_FIELD_counters */
7 
8 static const char * const bch2_counter_names[] = {
9 #define x(t, n, ...) (#t),
10 	BCH_PERSISTENT_COUNTERS()
11 #undef x
12 	NULL
13 };
14 
bch2_sb_counter_nr_entries(struct bch_sb_field_counters * ctrs)15 static size_t bch2_sb_counter_nr_entries(struct bch_sb_field_counters *ctrs)
16 {
17 	if (!ctrs)
18 		return 0;
19 
20 	return (__le64 *) vstruct_end(&ctrs->field) - &ctrs->d[0];
21 };
22 
bch2_sb_counters_validate(struct bch_sb * sb,struct bch_sb_field * f,enum bch_validate_flags flags,struct printbuf * err)23 static int bch2_sb_counters_validate(struct bch_sb *sb, struct bch_sb_field *f,
24 				enum bch_validate_flags flags, struct printbuf *err)
25 {
26 	return 0;
27 };
28 
bch2_sb_counters_to_text(struct printbuf * out,struct bch_sb * sb,struct bch_sb_field * f)29 static void bch2_sb_counters_to_text(struct printbuf *out, struct bch_sb *sb,
30 			      struct bch_sb_field *f)
31 {
32 	struct bch_sb_field_counters *ctrs = field_to_type(f, counters);
33 	unsigned int nr = bch2_sb_counter_nr_entries(ctrs);
34 
35 	for (unsigned i = 0; i < nr; i++)
36 		prt_printf(out, "%s \t%llu\n",
37 			   i < BCH_COUNTER_NR ? bch2_counter_names[i] : "(unknown)",
38 			   le64_to_cpu(ctrs->d[i]));
39 };
40 
bch2_sb_counters_to_cpu(struct bch_fs * c)41 int bch2_sb_counters_to_cpu(struct bch_fs *c)
42 {
43 	struct bch_sb_field_counters *ctrs = bch2_sb_field_get(c->disk_sb.sb, counters);
44 	unsigned int i;
45 	unsigned int nr = bch2_sb_counter_nr_entries(ctrs);
46 	u64 val = 0;
47 
48 	for (i = 0; i < BCH_COUNTER_NR; i++)
49 		c->counters_on_mount[i] = 0;
50 
51 	for (i = 0; i < min_t(unsigned int, nr, BCH_COUNTER_NR); i++) {
52 		val = le64_to_cpu(ctrs->d[i]);
53 		percpu_u64_set(&c->counters[i], val);
54 		c->counters_on_mount[i] = val;
55 	}
56 	return 0;
57 };
58 
bch2_sb_counters_from_cpu(struct bch_fs * c)59 int bch2_sb_counters_from_cpu(struct bch_fs *c)
60 {
61 	struct bch_sb_field_counters *ctrs = bch2_sb_field_get(c->disk_sb.sb, counters);
62 	struct bch_sb_field_counters *ret;
63 	unsigned int i;
64 	unsigned int nr = bch2_sb_counter_nr_entries(ctrs);
65 
66 	if (nr < BCH_COUNTER_NR) {
67 		ret = bch2_sb_field_resize(&c->disk_sb, counters,
68 					       sizeof(*ctrs) / sizeof(u64) + BCH_COUNTER_NR);
69 
70 		if (ret) {
71 			ctrs = ret;
72 			nr = bch2_sb_counter_nr_entries(ctrs);
73 		}
74 	}
75 
76 
77 	for (i = 0; i < min_t(unsigned int, nr, BCH_COUNTER_NR); i++)
78 		ctrs->d[i] = cpu_to_le64(percpu_u64_get(&c->counters[i]));
79 	return 0;
80 }
81 
bch2_fs_counters_exit(struct bch_fs * c)82 void bch2_fs_counters_exit(struct bch_fs *c)
83 {
84 	free_percpu(c->counters);
85 }
86 
bch2_fs_counters_init(struct bch_fs * c)87 int bch2_fs_counters_init(struct bch_fs *c)
88 {
89 	c->counters = __alloc_percpu(sizeof(u64) * BCH_COUNTER_NR, sizeof(u64));
90 	if (!c->counters)
91 		return -BCH_ERR_ENOMEM_fs_counters_init;
92 
93 	return bch2_sb_counters_to_cpu(c);
94 }
95 
96 const struct bch_sb_field_ops bch_sb_field_ops_counters = {
97 	.validate	= bch2_sb_counters_validate,
98 	.to_text	= bch2_sb_counters_to_text,
99 };
100