Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include "bcachefs.h" |
| 3 | #include "super-io.h" |
Kent Overstreet | 3a58dfb | 2024-01-20 23:46:35 -0500 | [diff] [blame] | 4 | #include "sb-counters.h" |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 5 | |
| 6 | /* BCH_SB_FIELD_counters */ |
| 7 | |
Kent Overstreet | 73bd774 | 2023-07-06 22:47:42 -0400 | [diff] [blame] | 8 | static const char * const bch2_counter_names[] = { |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 9 | #define x(t, n, ...) (#t), |
| 10 | BCH_PERSISTENT_COUNTERS() |
| 11 | #undef x |
| 12 | NULL |
| 13 | }; |
| 14 | |
| 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 | |
Kent Overstreet | a5c3e26 | 2024-05-08 18:49:14 -0400 | [diff] [blame] | 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) |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 25 | { |
| 26 | return 0; |
| 27 | }; |
| 28 | |
Kent Overstreet | 73bd774 | 2023-07-06 22:47:42 -0400 | [diff] [blame] | 29 | static void bch2_sb_counters_to_text(struct printbuf *out, struct bch_sb *sb, |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 30 | struct bch_sb_field *f) |
| 31 | { |
| 32 | struct bch_sb_field_counters *ctrs = field_to_type(f, counters); |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 33 | unsigned int nr = bch2_sb_counter_nr_entries(ctrs); |
| 34 | |
Kent Overstreet | 7423330 | 2024-04-10 16:08:24 -0400 | [diff] [blame] | 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])); |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | int bch2_sb_counters_to_cpu(struct bch_fs *c) |
| 42 | { |
Kent Overstreet | 4637429 | 2023-09-26 17:49:34 -0400 | [diff] [blame] | 43 | struct bch_sb_field_counters *ctrs = bch2_sb_field_get(c->disk_sb.sb, counters); |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 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 | |
| 59 | int bch2_sb_counters_from_cpu(struct bch_fs *c) |
| 60 | { |
Kent Overstreet | 4637429 | 2023-09-26 17:49:34 -0400 | [diff] [blame] | 61 | struct bch_sb_field_counters *ctrs = bch2_sb_field_get(c->disk_sb.sb, counters); |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 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) { |
Kent Overstreet | 4637429 | 2023-09-26 17:49:34 -0400 | [diff] [blame] | 67 | ret = bch2_sb_field_resize(&c->disk_sb, counters, |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 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 | |
| 82 | void bch2_fs_counters_exit(struct bch_fs *c) |
| 83 | { |
| 84 | free_percpu(c->counters); |
| 85 | } |
| 86 | |
| 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) |
Kent Overstreet | 65d48e3 | 2023-03-14 15:35:57 -0400 | [diff] [blame] | 91 | return -BCH_ERR_ENOMEM_fs_counters_init; |
Daniel Hill | 104c697 | 2022-03-15 21:36:33 +1300 | [diff] [blame] | 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 | }; |