]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/error.c
Update bcachefs sources to f026e4e024
[bcachefs-tools-debian] / libbcachefs / error.c
1 #include "bcachefs.h"
2 #include "error.h"
3 #include "io.h"
4 #include "super.h"
5
6 void bch2_inconsistent_error(struct bch_fs *c)
7 {
8         set_bit(BCH_FS_ERROR, &c->flags);
9
10         switch (c->opts.errors) {
11         case BCH_ON_ERROR_CONTINUE:
12                 break;
13         case BCH_ON_ERROR_RO:
14                 if (bch2_fs_emergency_read_only(c))
15                         bch_err(c, "emergency read only");
16                 break;
17         case BCH_ON_ERROR_PANIC:
18                 panic(bch2_fmt(c, "panic after error"));
19                 break;
20         }
21 }
22
23 void bch2_fatal_error(struct bch_fs *c)
24 {
25         if (bch2_fs_emergency_read_only(c))
26                 bch_err(c, "emergency read only");
27 }
28
29 void bch2_nonfatal_io_error_work(struct work_struct *work)
30 {
31         struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
32         struct bch_fs *c = ca->fs;
33         bool dev;
34
35         mutex_lock(&c->state_lock);
36         dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_RO,
37                                     BCH_FORCE_IF_DEGRADED);
38         if (dev
39             ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_RO,
40                                   BCH_FORCE_IF_DEGRADED)
41             : bch2_fs_emergency_read_only(c))
42                 bch_err(ca,
43                         "too many IO errors, setting %s RO",
44                         dev ? "device" : "filesystem");
45         mutex_unlock(&c->state_lock);
46 }
47
48 void bch2_nonfatal_io_error(struct bch_dev *ca)
49 {
50         queue_work(system_long_wq, &ca->io_error_work);
51 }
52
53 #ifdef __KERNEL__
54 #define ask_yn()        false
55 #else
56 #include "tools-util.h"
57 #endif
58
59 enum fsck_err_ret bch2_fsck_err(struct bch_fs *c, unsigned flags,
60                                 const char *fmt, ...)
61 {
62         struct fsck_err_state *s;
63         va_list args;
64         bool fix = false, print = true, suppressing = false;
65         char _buf[sizeof(s->buf)], *buf = _buf;
66
67         mutex_lock(&c->fsck_error_lock);
68
69         if (test_bit(BCH_FS_FSCK_DONE, &c->flags))
70                 goto print;
71
72         list_for_each_entry(s, &c->fsck_errors, list)
73                 if (s->fmt == fmt)
74                         goto found;
75
76         s = kzalloc(sizeof(*s), GFP_KERNEL);
77         if (!s) {
78                 if (!c->fsck_alloc_err)
79                         bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
80                 c->fsck_alloc_err = true;
81                 buf = _buf;
82                 goto print;
83         }
84
85         INIT_LIST_HEAD(&s->list);
86         s->fmt = fmt;
87 found:
88         list_move(&s->list, &c->fsck_errors);
89         s->nr++;
90         suppressing     = s->nr == 10;
91         print           = s->nr <= 10;
92         buf             = s->buf;
93 print:
94         va_start(args, fmt);
95         vscnprintf(buf, sizeof(_buf), fmt, args);
96         va_end(args);
97
98         if (flags & FSCK_CAN_FIX) {
99                 if (c->opts.fix_errors == FSCK_ERR_ASK) {
100                         printk(KERN_ERR "%s: fix?", buf);
101                         fix = ask_yn();
102                 } else if (c->opts.fix_errors == FSCK_ERR_YES ||
103                            (c->opts.nochanges &&
104                             !(flags & FSCK_CAN_IGNORE))) {
105                         if (print)
106                                 bch_err(c, "%s, fixing", buf);
107                         fix = true;
108                 } else {
109                         if (print)
110                                 bch_err(c, "%s, not fixing", buf);
111                         fix = false;
112                 }
113         } else if (flags & FSCK_NEED_FSCK) {
114                 if (print)
115                         bch_err(c, "%s (run fsck to correct)", buf);
116         } else {
117                 if (print)
118                         bch_err(c, "%s (repair unimplemented)", buf);
119         }
120
121         if (suppressing)
122                 bch_err(c, "Ratelimiting new instances of previous error");
123
124         mutex_unlock(&c->fsck_error_lock);
125
126         if (fix)
127                 set_bit(BCH_FS_FSCK_FIXED_ERRORS, &c->flags);
128
129         return fix                              ? FSCK_ERR_FIX
130                 : flags & FSCK_CAN_IGNORE       ? FSCK_ERR_IGNORE
131                                                 : FSCK_ERR_EXIT;
132 }
133
134 void bch2_flush_fsck_errs(struct bch_fs *c)
135 {
136         struct fsck_err_state *s, *n;
137
138         mutex_lock(&c->fsck_error_lock);
139         set_bit(BCH_FS_FSCK_DONE, &c->flags);
140
141         list_for_each_entry_safe(s, n, &c->fsck_errors, list) {
142                 if (s->nr > 10)
143                         bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->buf);
144
145                 list_del(&s->list);
146                 kfree(s);
147         }
148
149         mutex_unlock(&c->fsck_error_lock);
150 }