Linux 6.13-rc4
[linux.git] / fs / bcachefs / sb-errors.c
blob013a96883b4e2808b482fa5b7dc1fe366fd04b88
1 // SPDX-License-Identifier: GPL-2.0
3 #include "bcachefs.h"
4 #include "sb-errors.h"
5 #include "super-io.h"
7 const char * const bch2_sb_error_strs[] = {
8 #define x(t, n, ...) [n] = #t,
9 BCH_SB_ERRS()
10 #undef x
13 void bch2_sb_error_id_to_text(struct printbuf *out, enum bch_sb_error_id id)
15 if (id < BCH_FSCK_ERR_MAX)
16 prt_str(out, bch2_sb_error_strs[id]);
17 else
18 prt_printf(out, "(unknown error %u)", id);
21 static inline unsigned bch2_sb_field_errors_nr_entries(struct bch_sb_field_errors *e)
23 return bch2_sb_field_nr_entries(e);
26 static inline unsigned bch2_sb_field_errors_u64s(unsigned nr)
28 return (sizeof(struct bch_sb_field_errors) +
29 sizeof(struct bch_sb_field_error_entry) * nr) / sizeof(u64);
32 static int bch2_sb_errors_validate(struct bch_sb *sb, struct bch_sb_field *f,
33 enum bch_validate_flags flags, struct printbuf *err)
35 struct bch_sb_field_errors *e = field_to_type(f, errors);
36 unsigned i, nr = bch2_sb_field_errors_nr_entries(e);
38 for (i = 0; i < nr; i++) {
39 if (!BCH_SB_ERROR_ENTRY_NR(&e->entries[i])) {
40 prt_printf(err, "entry with count 0 (id ");
41 bch2_sb_error_id_to_text(err, BCH_SB_ERROR_ENTRY_ID(&e->entries[i]));
42 prt_printf(err, ")");
43 return -BCH_ERR_invalid_sb_errors;
46 if (i + 1 < nr &&
47 BCH_SB_ERROR_ENTRY_ID(&e->entries[i]) >=
48 BCH_SB_ERROR_ENTRY_ID(&e->entries[i + 1])) {
49 prt_printf(err, "entries out of order");
50 return -BCH_ERR_invalid_sb_errors;
54 return 0;
57 static void bch2_sb_errors_to_text(struct printbuf *out, struct bch_sb *sb,
58 struct bch_sb_field *f)
60 struct bch_sb_field_errors *e = field_to_type(f, errors);
61 unsigned i, nr = bch2_sb_field_errors_nr_entries(e);
63 if (out->nr_tabstops <= 1)
64 printbuf_tabstop_push(out, 16);
66 for (i = 0; i < nr; i++) {
67 bch2_sb_error_id_to_text(out, BCH_SB_ERROR_ENTRY_ID(&e->entries[i]));
68 prt_tab(out);
69 prt_u64(out, BCH_SB_ERROR_ENTRY_NR(&e->entries[i]));
70 prt_tab(out);
71 bch2_prt_datetime(out, le64_to_cpu(e->entries[i].last_error_time));
72 prt_newline(out);
76 const struct bch_sb_field_ops bch_sb_field_ops_errors = {
77 .validate = bch2_sb_errors_validate,
78 .to_text = bch2_sb_errors_to_text,
81 void bch2_sb_error_count(struct bch_fs *c, enum bch_sb_error_id err)
83 bch_sb_errors_cpu *e = &c->fsck_error_counts;
84 struct bch_sb_error_entry_cpu n = {
85 .id = err,
86 .nr = 1,
87 .last_error_time = ktime_get_real_seconds()
89 unsigned i;
91 mutex_lock(&c->fsck_error_counts_lock);
92 for (i = 0; i < e->nr; i++) {
93 if (err == e->data[i].id) {
94 e->data[i].nr++;
95 e->data[i].last_error_time = n.last_error_time;
96 goto out;
98 if (err < e->data[i].id)
99 break;
102 if (darray_make_room(e, 1))
103 goto out;
105 darray_insert_item(e, i, n);
106 out:
107 mutex_unlock(&c->fsck_error_counts_lock);
110 void bch2_sb_errors_from_cpu(struct bch_fs *c)
112 bch_sb_errors_cpu *src = &c->fsck_error_counts;
113 struct bch_sb_field_errors *dst;
114 unsigned i;
116 mutex_lock(&c->fsck_error_counts_lock);
118 dst = bch2_sb_field_resize(&c->disk_sb, errors,
119 bch2_sb_field_errors_u64s(src->nr));
121 if (!dst)
122 goto err;
124 for (i = 0; i < src->nr; i++) {
125 SET_BCH_SB_ERROR_ENTRY_ID(&dst->entries[i], src->data[i].id);
126 SET_BCH_SB_ERROR_ENTRY_NR(&dst->entries[i], src->data[i].nr);
127 dst->entries[i].last_error_time = cpu_to_le64(src->data[i].last_error_time);
130 err:
131 mutex_unlock(&c->fsck_error_counts_lock);
134 static int bch2_sb_errors_to_cpu(struct bch_fs *c)
136 struct bch_sb_field_errors *src = bch2_sb_field_get(c->disk_sb.sb, errors);
137 bch_sb_errors_cpu *dst = &c->fsck_error_counts;
138 unsigned i, nr = bch2_sb_field_errors_nr_entries(src);
139 int ret;
141 if (!nr)
142 return 0;
144 mutex_lock(&c->fsck_error_counts_lock);
145 ret = darray_make_room(dst, nr);
146 if (ret)
147 goto err;
149 dst->nr = nr;
151 for (i = 0; i < nr; i++) {
152 dst->data[i].id = BCH_SB_ERROR_ENTRY_ID(&src->entries[i]);
153 dst->data[i].nr = BCH_SB_ERROR_ENTRY_NR(&src->entries[i]);
154 dst->data[i].last_error_time = le64_to_cpu(src->entries[i].last_error_time);
156 err:
157 mutex_unlock(&c->fsck_error_counts_lock);
159 return ret;
162 void bch2_fs_sb_errors_exit(struct bch_fs *c)
164 darray_exit(&c->fsck_error_counts);
167 void bch2_fs_sb_errors_init_early(struct bch_fs *c)
169 mutex_init(&c->fsck_error_counts_lock);
170 darray_init(&c->fsck_error_counts);
173 int bch2_fs_sb_errors_init(struct bch_fs *c)
175 return bch2_sb_errors_to_cpu(c);