blob: 6992e7469112cf143b6a8fd5c7303e6f1d502235 [file] [log] [blame]
Daniel Hill104c6972022-03-15 21:36:33 +13001// SPDX-License-Identifier: GPL-2.0
2#include "bcachefs.h"
3#include "super-io.h"
Kent Overstreet3a58dfb2024-01-20 23:46:35 -05004#include "sb-counters.h"
Daniel Hill104c6972022-03-15 21:36:33 +13005
6/* BCH_SB_FIELD_counters */
7
Kent Overstreet73bd7742023-07-06 22:47:42 -04008static const char * const bch2_counter_names[] = {
Daniel Hill104c6972022-03-15 21:36:33 +13009#define x(t, n, ...) (#t),
10 BCH_PERSISTENT_COUNTERS()
11#undef x
12 NULL
13};
14
15static 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
Kent Overstreeta5c3e262024-05-08 18:49:14 -040023static int bch2_sb_counters_validate(struct bch_sb *sb, struct bch_sb_field *f,
24 enum bch_validate_flags flags, struct printbuf *err)
Daniel Hill104c6972022-03-15 21:36:33 +130025{
26 return 0;
27};
28
Kent Overstreet73bd7742023-07-06 22:47:42 -040029static void bch2_sb_counters_to_text(struct printbuf *out, struct bch_sb *sb,
Daniel Hill104c6972022-03-15 21:36:33 +130030 struct bch_sb_field *f)
31{
32 struct bch_sb_field_counters *ctrs = field_to_type(f, counters);
Daniel Hill104c6972022-03-15 21:36:33 +130033 unsigned int nr = bch2_sb_counter_nr_entries(ctrs);
34
Kent Overstreet74233302024-04-10 16:08:24 -040035 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]));
Daniel Hill104c6972022-03-15 21:36:33 +130039};
40
41int bch2_sb_counters_to_cpu(struct bch_fs *c)
42{
Kent Overstreet46374292023-09-26 17:49:34 -040043 struct bch_sb_field_counters *ctrs = bch2_sb_field_get(c->disk_sb.sb, counters);
Daniel Hill104c6972022-03-15 21:36:33 +130044 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
59int bch2_sb_counters_from_cpu(struct bch_fs *c)
60{
Kent Overstreet46374292023-09-26 17:49:34 -040061 struct bch_sb_field_counters *ctrs = bch2_sb_field_get(c->disk_sb.sb, counters);
Daniel Hill104c6972022-03-15 21:36:33 +130062 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) {
Kent Overstreet46374292023-09-26 17:49:34 -040067 ret = bch2_sb_field_resize(&c->disk_sb, counters,
Daniel Hill104c6972022-03-15 21:36:33 +130068 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
82void bch2_fs_counters_exit(struct bch_fs *c)
83{
84 free_percpu(c->counters);
85}
86
87int 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)
Kent Overstreet65d48e32023-03-14 15:35:57 -040091 return -BCH_ERR_ENOMEM_fs_counters_init;
Daniel Hill104c6972022-03-15 21:36:33 +130092
93 return bch2_sb_counters_to_cpu(c);
94}
95
96const 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};