1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_background.h"
5 #include "btree_iter.h"
6 #include "btree_update.h"
7 #include "btree_write_buffer.h"
12 /* KEY_TYPE_lru is obsolete: */
13 int bch2_lru_invalid(struct bch_fs *c, struct bkey_s_c k,
14 enum bkey_invalid_flags flags,
19 bkey_fsck_err_on(!lru_pos_time(k.k->p), c, err,
21 "lru entry at time=0");
26 void bch2_lru_to_text(struct printbuf *out, struct bch_fs *c,
29 const struct bch_lru *lru = bkey_s_c_to_lru(k).v;
31 prt_printf(out, "idx %llu", le64_to_cpu(lru->idx));
34 void bch2_lru_pos_to_text(struct printbuf *out, struct bpos lru)
36 prt_printf(out, "%llu:%llu -> %llu:%llu",
39 u64_to_bucket(lru.offset).inode,
40 u64_to_bucket(lru.offset).offset);
43 static inline int __bch2_lru_set(struct btree_trans *trans, u16 lru_id,
44 u64 dev_bucket, u64 time, bool set)
47 ? bch2_btree_bit_mod(trans, BTREE_ID_lru,
48 lru_pos(lru_id, dev_bucket, time), set)
52 int bch2_lru_del(struct btree_trans *trans, u16 lru_id, u64 dev_bucket, u64 time)
54 return __bch2_lru_set(trans, lru_id, dev_bucket, time, false);
57 int bch2_lru_set(struct btree_trans *trans, u16 lru_id, u64 dev_bucket, u64 time)
59 return __bch2_lru_set(trans, lru_id, dev_bucket, time, true);
62 int bch2_lru_change(struct btree_trans *trans,
63 u16 lru_id, u64 dev_bucket,
64 u64 old_time, u64 new_time)
66 if (old_time == new_time)
69 return __bch2_lru_set(trans, lru_id, dev_bucket, old_time, false) ?:
70 __bch2_lru_set(trans, lru_id, dev_bucket, new_time, true);
73 static const char * const bch2_lru_types[] = {
80 /* Returns 1 if key has been deleted */
81 int bch2_check_lru_key(struct btree_trans *trans,
82 struct btree_iter *lru_iter,
83 struct bkey_s_c lru_k,
84 struct bpos *last_flushed_pos)
86 struct bch_fs *c = trans->c;
87 struct btree_iter iter;
89 struct bch_alloc_v4 a_convert;
90 const struct bch_alloc_v4 *a;
91 struct printbuf buf1 = PRINTBUF;
92 struct printbuf buf2 = PRINTBUF;
93 struct bpos alloc_pos = u64_to_bucket(lru_k.k->p.offset);
97 if (fsck_err_on(!bch2_dev_bucket_exists(c, alloc_pos), c,
98 lru_entry_to_invalid_bucket,
99 "lru key points to nonexistent device:bucket %llu:%llu",
100 alloc_pos.inode, alloc_pos.offset))
103 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc, alloc_pos, 0);
108 a = bch2_alloc_to_v4(k, &a_convert);
110 enum bch_lru_type type = lru_type(lru_k);
113 idx = alloc_lru_idx_read(*a);
115 case BCH_LRU_fragmentation:
116 idx = a->fragmentation_lru;
119 /* unknown LRU type, don't check: */
123 if (lru_k.k->type != KEY_TYPE_set ||
124 lru_pos_time(lru_k.k->p) != idx) {
125 if (!bpos_eq(*last_flushed_pos, lru_k.k->p)) {
126 ret = bch2_btree_write_buffer_flush_sync(trans);
128 *last_flushed_pos = lru_k.k->p;
129 ret = -BCH_ERR_transaction_restart_write_buffer_flush;
134 if ((c->opts.reconstruct_alloc &&
135 c->curr_recovery_pass <= BCH_RECOVERY_PASS_check_lrus) ||
136 fsck_err(c, lru_entry_bad,
137 "incorrect lru entry: lru %s time %llu\n"
141 bch2_lru_types[type],
142 lru_pos_time(lru_k.k->p),
143 (bch2_bkey_val_to_text(&buf1, c, lru_k), buf1.buf),
144 (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf)))
150 bch2_trans_iter_exit(trans, &iter);
151 printbuf_exit(&buf2);
152 printbuf_exit(&buf1);
155 ret = bch2_btree_delete_at(trans, lru_iter, 0) ?:
156 bch2_trans_commit(trans, NULL, NULL,
158 BCH_TRANS_COMMIT_lazy_rw|
159 BCH_TRANS_COMMIT_no_enospc) ?:
164 int bch2_check_lrus(struct bch_fs *c)
166 struct btree_iter iter;
168 struct bpos last_flushed_pos = POS_MIN;
170 int ret = bch2_trans_run(c,
171 for_each_btree_key2(trans, iter,
172 BTREE_ID_lru, POS_MIN, BTREE_ITER_PREFETCH, k, ({
173 int ret2 = bch2_check_lru_key(trans, &iter, k, &last_flushed_pos);