1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "bcachefs.h" 4 #include "alloc_background.h" 5 #include "bkey_buf.h" 6 #include "btree_iter.h" 7 #include "btree_update.h" 8 #include "btree_write_buffer.h" 9 #include "error.h" 10 #include "lru.h" 11 #include "recovery.h" 12 13 /* KEY_TYPE_lru is obsolete: */ 14 int bch2_lru_validate(struct bch_fs *c, struct bkey_s_c k, 15 enum bch_validate_flags flags) 16 { 17 int ret = 0; 18 19 bkey_fsck_err_on(!lru_pos_time(k.k->p), 20 c, lru_entry_at_time_0, 21 "lru entry at time=0"); 22 fsck_err: 23 return ret; 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_buffered(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 int bch2_lru_check_set(struct btree_trans *trans, 81 u16 lru_id, u64 time, 82 struct bkey_s_c referring_k, 83 struct bkey_buf *last_flushed) 84 { 85 struct bch_fs *c = trans->c; 86 struct printbuf buf = PRINTBUF; 87 struct btree_iter lru_iter; 88 struct bkey_s_c lru_k = 89 bch2_bkey_get_iter(trans, &lru_iter, BTREE_ID_lru, 90 lru_pos(lru_id, 91 bucket_to_u64(referring_k.k->p), 92 time), 0); 93 int ret = bkey_err(lru_k); 94 if (ret) 95 return ret; 96 97 if (lru_k.k->type != KEY_TYPE_set) { 98 ret = bch2_btree_write_buffer_maybe_flush(trans, referring_k, last_flushed); 99 if (ret) 100 goto err; 101 102 if (fsck_err(trans, alloc_key_to_missing_lru_entry, 103 "missing %s lru entry\n" 104 " %s", 105 bch2_lru_types[lru_type(lru_k)], 106 (bch2_bkey_val_to_text(&buf, c, referring_k), buf.buf))) { 107 ret = bch2_lru_set(trans, lru_id, bucket_to_u64(referring_k.k->p), time); 108 if (ret) 109 goto err; 110 } 111 } 112 err: 113 fsck_err: 114 bch2_trans_iter_exit(trans, &lru_iter); 115 printbuf_exit(&buf); 116 return ret; 117 } 118 119 static int bch2_check_lru_key(struct btree_trans *trans, 120 struct btree_iter *lru_iter, 121 struct bkey_s_c lru_k, 122 struct bkey_buf *last_flushed) 123 { 124 struct bch_fs *c = trans->c; 125 struct btree_iter iter; 126 struct bkey_s_c k; 127 struct bch_alloc_v4 a_convert; 128 const struct bch_alloc_v4 *a; 129 struct printbuf buf1 = PRINTBUF; 130 struct printbuf buf2 = PRINTBUF; 131 enum bch_lru_type type = lru_type(lru_k); 132 struct bpos alloc_pos = u64_to_bucket(lru_k.k->p.offset); 133 u64 idx; 134 int ret; 135 136 struct bch_dev *ca = bch2_dev_bucket_tryget_noerror(c, alloc_pos); 137 138 if (fsck_err_on(!ca, 139 trans, lru_entry_to_invalid_bucket, 140 "lru key points to nonexistent device:bucket %llu:%llu", 141 alloc_pos.inode, alloc_pos.offset)) 142 return bch2_btree_bit_mod_buffered(trans, BTREE_ID_lru, lru_iter->pos, false); 143 144 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc, alloc_pos, 0); 145 ret = bkey_err(k); 146 if (ret) 147 goto err; 148 149 a = bch2_alloc_to_v4(k, &a_convert); 150 151 switch (type) { 152 case BCH_LRU_read: 153 idx = alloc_lru_idx_read(*a); 154 break; 155 case BCH_LRU_fragmentation: 156 idx = alloc_lru_idx_fragmentation(*a, ca); 157 break; 158 } 159 160 if (lru_k.k->type != KEY_TYPE_set || 161 lru_pos_time(lru_k.k->p) != idx) { 162 ret = bch2_btree_write_buffer_maybe_flush(trans, lru_k, last_flushed); 163 if (ret) 164 goto err; 165 166 if (fsck_err(trans, lru_entry_bad, 167 "incorrect lru entry: lru %s time %llu\n" 168 " %s\n" 169 " for %s", 170 bch2_lru_types[type], 171 lru_pos_time(lru_k.k->p), 172 (bch2_bkey_val_to_text(&buf1, c, lru_k), buf1.buf), 173 (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf))) 174 ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_lru, lru_iter->pos, false); 175 } 176 err: 177 fsck_err: 178 bch2_trans_iter_exit(trans, &iter); 179 bch2_dev_put(ca); 180 printbuf_exit(&buf2); 181 printbuf_exit(&buf1); 182 return ret; 183 } 184 185 int bch2_check_lrus(struct bch_fs *c) 186 { 187 struct bkey_buf last_flushed; 188 189 bch2_bkey_buf_init(&last_flushed); 190 bkey_init(&last_flushed.k->k); 191 192 int ret = bch2_trans_run(c, 193 for_each_btree_key_commit(trans, iter, 194 BTREE_ID_lru, POS_MIN, BTREE_ITER_prefetch, k, 195 NULL, NULL, BCH_TRANS_COMMIT_no_enospc|BCH_TRANS_COMMIT_lazy_rw, 196 bch2_check_lru_key(trans, &iter, k, &last_flushed))); 197 198 bch2_bkey_buf_exit(&last_flushed, c); 199 bch_err_fn(c, ret); 200 return ret; 201 202 } 203