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