]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/lru.c
Update bcachefs sources to 24bdb6fed91c bcachefs: bch2_btree_id_str()
[bcachefs-tools-debian] / libbcachefs / lru.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "alloc_background.h"
5 #include "btree_iter.h"
6 #include "btree_update.h"
7 #include "btree_write_buffer.h"
8 #include "error.h"
9 #include "lru.h"
10 #include "recovery.h"
11
12 /* KEY_TYPE_lru is obsolete: */
13 int bch2_lru_invalid(const struct bch_fs *c, struct bkey_s_c k,
14                      enum bkey_invalid_flags flags,
15                      struct printbuf *err)
16 {
17         if (!lru_pos_time(k.k->p)) {
18                 prt_printf(err, "lru entry at time=0");
19                 return -BCH_ERR_invalid_bkey;
20
21         }
22
23         return 0;
24 }
25
26 void bch2_lru_to_text(struct printbuf *out, struct bch_fs *c,
27                       struct bkey_s_c k)
28 {
29         const struct bch_lru *lru = bkey_s_c_to_lru(k).v;
30
31         prt_printf(out, "idx %llu", le64_to_cpu(lru->idx));
32 }
33
34 void bch2_lru_pos_to_text(struct printbuf *out, struct bpos lru)
35 {
36         prt_printf(out, "%llu:%llu -> %llu:%llu",
37                    lru_pos_id(lru),
38                    lru_pos_time(lru),
39                    u64_to_bucket(lru.offset).inode,
40                    u64_to_bucket(lru.offset).offset);
41 }
42
43 static int __bch2_lru_set(struct btree_trans *trans, u16 lru_id,
44                           u64 dev_bucket, u64 time, bool set)
45 {
46         return time
47                 ? bch2_btree_bit_mod(trans, BTREE_ID_lru,
48                                      lru_pos(lru_id, dev_bucket, time), set)
49                 : 0;
50 }
51
52 int bch2_lru_del(struct btree_trans *trans, u16 lru_id, u64 dev_bucket, u64 time)
53 {
54         return __bch2_lru_set(trans, lru_id, dev_bucket, time, KEY_TYPE_deleted);
55 }
56
57 int bch2_lru_set(struct btree_trans *trans, u16 lru_id, u64 dev_bucket, u64 time)
58 {
59         return __bch2_lru_set(trans, lru_id, dev_bucket, time, KEY_TYPE_set);
60 }
61
62 int bch2_lru_change(struct btree_trans *trans,
63                     u16 lru_id, u64 dev_bucket,
64                     u64 old_time, u64 new_time)
65 {
66         if (old_time == new_time)
67                 return 0;
68
69         return  bch2_lru_del(trans, lru_id, dev_bucket, old_time) ?:
70                 bch2_lru_set(trans, lru_id, dev_bucket, new_time);
71 }
72
73 static const char * const bch2_lru_types[] = {
74 #define x(n) #n,
75         BCH_LRU_TYPES()
76 #undef x
77         NULL
78 };
79
80 static int bch2_check_lru_key(struct btree_trans *trans,
81                               struct btree_iter *lru_iter,
82                               struct bkey_s_c lru_k,
83                               struct bpos *last_flushed_pos)
84 {
85         struct bch_fs *c = trans->c;
86         struct btree_iter iter;
87         struct bkey_s_c k;
88         struct bch_alloc_v4 a_convert;
89         const struct bch_alloc_v4 *a;
90         struct printbuf buf1 = PRINTBUF;
91         struct printbuf buf2 = PRINTBUF;
92         enum bch_lru_type type = lru_type(lru_k);
93         struct bpos alloc_pos = u64_to_bucket(lru_k.k->p.offset);
94         u64 idx;
95         int ret;
96
97         if (fsck_err_on(!bch2_dev_bucket_exists(c, alloc_pos), c,
98                         "lru key points to nonexistent device:bucket %llu:%llu",
99                         alloc_pos.inode, alloc_pos.offset))
100                 return bch2_btree_delete_at(trans, lru_iter, 0);
101
102         k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc, alloc_pos, 0);
103         ret = bkey_err(k);
104         if (ret)
105                 goto err;
106
107         a = bch2_alloc_to_v4(k, &a_convert);
108
109         switch (type) {
110         case BCH_LRU_read:
111                 idx = alloc_lru_idx_read(*a);
112                 break;
113         case BCH_LRU_fragmentation:
114                 idx = a->fragmentation_lru;
115                 break;
116         }
117
118         if (lru_k.k->type != KEY_TYPE_set ||
119             lru_pos_time(lru_k.k->p) != idx) {
120                 if (!bpos_eq(*last_flushed_pos, lru_k.k->p)) {
121                         *last_flushed_pos = lru_k.k->p;
122                         ret = bch2_btree_write_buffer_flush_sync(trans) ?:
123                                 -BCH_ERR_transaction_restart_write_buffer_flush;
124                         goto out;
125                 }
126
127                 if (c->opts.reconstruct_alloc ||
128                     fsck_err(c, "incorrect lru entry: lru %s time %llu\n"
129                              "  %s\n"
130                              "  for %s",
131                              bch2_lru_types[type],
132                              lru_pos_time(lru_k.k->p),
133                              (bch2_bkey_val_to_text(&buf1, c, lru_k), buf1.buf),
134                              (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf)))
135                         ret = bch2_btree_delete_at(trans, lru_iter, 0);
136         }
137 out:
138 err:
139 fsck_err:
140         bch2_trans_iter_exit(trans, &iter);
141         printbuf_exit(&buf2);
142         printbuf_exit(&buf1);
143         return ret;
144 }
145
146 int bch2_check_lrus(struct bch_fs *c)
147 {
148         struct btree_iter iter;
149         struct bkey_s_c k;
150         struct bpos last_flushed_pos = POS_MIN;
151         int ret = 0;
152
153         ret = bch2_trans_run(c,
154                 for_each_btree_key_commit(trans, iter,
155                                 BTREE_ID_lru, POS_MIN, BTREE_ITER_PREFETCH, k,
156                                 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
157                         bch2_check_lru_key(trans, &iter, k, &last_flushed_pos)));
158         if (ret)
159                 bch_err_fn(c, ret);
160         return ret;
161
162 }