2021-12-05 05:31:54 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
2022-02-17 08:11:39 +00:00
|
|
|
#include "alloc_background.h"
|
2024-10-01 20:40:33 +00:00
|
|
|
#include "bkey_buf.h"
|
2021-12-05 05:31:54 +00:00
|
|
|
#include "btree_iter.h"
|
|
|
|
#include "btree_update.h"
|
2023-02-06 23:51:42 +00:00
|
|
|
#include "btree_write_buffer.h"
|
2021-12-05 05:31:54 +00:00
|
|
|
#include "error.h"
|
|
|
|
#include "lru.h"
|
2022-02-17 08:11:39 +00:00
|
|
|
#include "recovery.h"
|
2021-12-05 05:31:54 +00:00
|
|
|
|
2022-12-05 21:49:13 +00:00
|
|
|
/* KEY_TYPE_lru is obsolete: */
|
2024-08-13 01:31:25 +00:00
|
|
|
int bch2_lru_validate(struct bch_fs *c, struct bkey_s_c k,
|
|
|
|
enum bch_validate_flags flags)
|
2021-12-05 05:31:54 +00:00
|
|
|
{
|
2023-10-25 00:44:36 +00:00
|
|
|
int ret = 0;
|
2023-01-04 05:00:55 +00:00
|
|
|
|
2024-08-13 01:31:25 +00:00
|
|
|
bkey_fsck_err_on(!lru_pos_time(k.k->p),
|
|
|
|
c, lru_entry_at_time_0,
|
2023-10-25 00:44:36 +00:00
|
|
|
"lru entry at time=0");
|
|
|
|
fsck_err:
|
|
|
|
return ret;
|
2021-12-05 05:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_lru_to_text(struct printbuf *out, struct bch_fs *c,
|
|
|
|
struct bkey_s_c k)
|
|
|
|
{
|
|
|
|
const struct bch_lru *lru = bkey_s_c_to_lru(k).v;
|
|
|
|
|
2023-02-04 02:01:40 +00:00
|
|
|
prt_printf(out, "idx %llu", le64_to_cpu(lru->idx));
|
2021-12-05 05:31:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 04:54:10 +00:00
|
|
|
void bch2_lru_pos_to_text(struct printbuf *out, struct bpos lru)
|
|
|
|
{
|
|
|
|
prt_printf(out, "%llu:%llu -> %llu:%llu",
|
|
|
|
lru_pos_id(lru),
|
|
|
|
lru_pos_time(lru),
|
|
|
|
u64_to_bucket(lru.offset).inode,
|
|
|
|
u64_to_bucket(lru.offset).offset);
|
|
|
|
}
|
|
|
|
|
2022-12-05 21:49:13 +00:00
|
|
|
static int __bch2_lru_set(struct btree_trans *trans, u16 lru_id,
|
2023-07-17 04:56:07 +00:00
|
|
|
u64 dev_bucket, u64 time, bool set)
|
2021-12-05 05:31:54 +00:00
|
|
|
{
|
2023-07-17 04:56:07 +00:00
|
|
|
return time
|
2024-02-09 00:10:19 +00:00
|
|
|
? bch2_btree_bit_mod_buffered(trans, BTREE_ID_lru,
|
|
|
|
lru_pos(lru_id, dev_bucket, time), set)
|
2023-07-17 04:56:07 +00:00
|
|
|
: 0;
|
2021-12-05 05:31:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 21:49:13 +00:00
|
|
|
int bch2_lru_del(struct btree_trans *trans, u16 lru_id, u64 dev_bucket, u64 time)
|
2021-12-05 05:31:54 +00:00
|
|
|
{
|
2022-12-05 21:49:13 +00:00
|
|
|
return __bch2_lru_set(trans, lru_id, dev_bucket, time, KEY_TYPE_deleted);
|
|
|
|
}
|
2021-12-05 05:31:54 +00:00
|
|
|
|
2022-12-05 21:49:13 +00:00
|
|
|
int bch2_lru_set(struct btree_trans *trans, u16 lru_id, u64 dev_bucket, u64 time)
|
|
|
|
{
|
|
|
|
return __bch2_lru_set(trans, lru_id, dev_bucket, time, KEY_TYPE_set);
|
2021-12-05 05:31:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 21:49:13 +00:00
|
|
|
int bch2_lru_change(struct btree_trans *trans,
|
|
|
|
u16 lru_id, u64 dev_bucket,
|
|
|
|
u64 old_time, u64 new_time)
|
2021-12-05 05:31:54 +00:00
|
|
|
{
|
2022-12-05 21:49:13 +00:00
|
|
|
if (old_time == new_time)
|
2021-12-05 05:31:54 +00:00
|
|
|
return 0;
|
|
|
|
|
2022-12-05 21:49:13 +00:00
|
|
|
return bch2_lru_del(trans, lru_id, dev_bucket, old_time) ?:
|
|
|
|
bch2_lru_set(trans, lru_id, dev_bucket, new_time);
|
2021-12-05 05:31:54 +00:00
|
|
|
}
|
2022-02-17 08:11:39 +00:00
|
|
|
|
2022-12-05 15:24:19 +00:00
|
|
|
static const char * const bch2_lru_types[] = {
|
|
|
|
#define x(n) #n,
|
|
|
|
BCH_LRU_TYPES()
|
|
|
|
#undef x
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2024-06-29 22:35:18 +00:00
|
|
|
int bch2_lru_check_set(struct btree_trans *trans,
|
|
|
|
u16 lru_id, u64 time,
|
|
|
|
struct bkey_s_c referring_k,
|
|
|
|
struct bkey_buf *last_flushed)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct printbuf buf = PRINTBUF;
|
|
|
|
struct btree_iter lru_iter;
|
|
|
|
struct bkey_s_c lru_k =
|
|
|
|
bch2_bkey_get_iter(trans, &lru_iter, BTREE_ID_lru,
|
|
|
|
lru_pos(lru_id,
|
|
|
|
bucket_to_u64(referring_k.k->p),
|
|
|
|
time), 0);
|
|
|
|
int ret = bkey_err(lru_k);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (lru_k.k->type != KEY_TYPE_set) {
|
|
|
|
ret = bch2_btree_write_buffer_maybe_flush(trans, referring_k, last_flushed);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2024-02-09 02:10:32 +00:00
|
|
|
if (fsck_err(trans, alloc_key_to_missing_lru_entry,
|
2024-06-29 22:35:18 +00:00
|
|
|
"missing %s lru entry\n"
|
|
|
|
" %s",
|
|
|
|
bch2_lru_types[lru_type(lru_k)],
|
|
|
|
(bch2_bkey_val_to_text(&buf, c, referring_k), buf.buf))) {
|
|
|
|
ret = bch2_lru_set(trans, lru_id, bucket_to_u64(referring_k.k->p), time);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
fsck_err:
|
|
|
|
bch2_trans_iter_exit(trans, &lru_iter);
|
|
|
|
printbuf_exit(&buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-17 08:11:39 +00:00
|
|
|
static int bch2_check_lru_key(struct btree_trans *trans,
|
2022-07-17 04:44:19 +00:00
|
|
|
struct btree_iter *lru_iter,
|
2023-02-06 23:51:42 +00:00
|
|
|
struct bkey_s_c lru_k,
|
2024-10-01 20:40:33 +00:00
|
|
|
struct bkey_buf *last_flushed)
|
2022-02-17 08:11:39 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct btree_iter iter;
|
2022-07-17 04:44:19 +00:00
|
|
|
struct bkey_s_c k;
|
2023-01-31 01:58:43 +00:00
|
|
|
struct bch_alloc_v4 a_convert;
|
|
|
|
const struct bch_alloc_v4 *a;
|
2022-02-17 08:11:39 +00:00
|
|
|
struct printbuf buf1 = PRINTBUF;
|
|
|
|
struct printbuf buf2 = PRINTBUF;
|
2022-12-05 15:24:19 +00:00
|
|
|
enum bch_lru_type type = lru_type(lru_k);
|
2022-12-05 21:49:13 +00:00
|
|
|
struct bpos alloc_pos = u64_to_bucket(lru_k.k->p.offset);
|
2022-12-05 15:24:19 +00:00
|
|
|
u64 idx;
|
2022-02-17 08:11:39 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-10-01 23:08:37 +00:00
|
|
|
struct bch_dev *ca = bch2_dev_bucket_tryget_noerror(c, alloc_pos);
|
|
|
|
|
|
|
|
if (fsck_err_on(!ca,
|
2024-02-09 02:10:32 +00:00
|
|
|
trans, lru_entry_to_invalid_bucket,
|
2022-04-09 19:15:36 +00:00
|
|
|
"lru key points to nonexistent device:bucket %llu:%llu",
|
|
|
|
alloc_pos.inode, alloc_pos.offset))
|
2024-10-01 20:40:33 +00:00
|
|
|
return bch2_btree_bit_mod_buffered(trans, BTREE_ID_lru, lru_iter->pos, false);
|
2022-04-09 19:15:36 +00:00
|
|
|
|
2023-04-29 23:33:09 +00:00
|
|
|
k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc, alloc_pos, 0);
|
2022-02-17 08:11:39 +00:00
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2023-01-31 01:58:43 +00:00
|
|
|
a = bch2_alloc_to_v4(k, &a_convert);
|
2022-02-17 08:11:39 +00:00
|
|
|
|
2022-12-05 15:24:19 +00:00
|
|
|
switch (type) {
|
|
|
|
case BCH_LRU_read:
|
|
|
|
idx = alloc_lru_idx_read(*a);
|
|
|
|
break;
|
|
|
|
case BCH_LRU_fragmentation:
|
2024-10-01 23:08:37 +00:00
|
|
|
idx = alloc_lru_idx_fragmentation(*a, ca);
|
2022-12-05 15:24:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-06 23:51:42 +00:00
|
|
|
if (lru_k.k->type != KEY_TYPE_set ||
|
2022-12-05 15:24:19 +00:00
|
|
|
lru_pos_time(lru_k.k->p) != idx) {
|
2024-10-01 20:40:33 +00:00
|
|
|
ret = bch2_btree_write_buffer_maybe_flush(trans, lru_k, last_flushed);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
2023-02-06 23:51:42 +00:00
|
|
|
|
2024-02-09 02:10:32 +00:00
|
|
|
if (fsck_err(trans, lru_entry_bad,
|
2023-10-25 00:44:36 +00:00
|
|
|
"incorrect lru entry: lru %s time %llu\n"
|
2022-12-05 15:24:19 +00:00
|
|
|
" %s\n"
|
|
|
|
" for %s",
|
|
|
|
bch2_lru_types[type],
|
|
|
|
lru_pos_time(lru_k.k->p),
|
|
|
|
(bch2_bkey_val_to_text(&buf1, c, lru_k), buf1.buf),
|
|
|
|
(bch2_bkey_val_to_text(&buf2, c, k), buf2.buf)))
|
2024-10-01 20:40:33 +00:00
|
|
|
ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_lru, lru_iter->pos, false);
|
2022-02-17 08:11:39 +00:00
|
|
|
}
|
|
|
|
err:
|
|
|
|
fsck_err:
|
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
2024-10-01 23:08:37 +00:00
|
|
|
bch2_dev_put(ca);
|
2022-02-17 08:11:39 +00:00
|
|
|
printbuf_exit(&buf2);
|
|
|
|
printbuf_exit(&buf1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-05-14 10:58:51 +00:00
|
|
|
int bch2_check_lrus(struct bch_fs *c)
|
2022-02-17 08:11:39 +00:00
|
|
|
{
|
2024-10-01 20:40:33 +00:00
|
|
|
struct bkey_buf last_flushed;
|
|
|
|
|
|
|
|
bch2_bkey_buf_init(&last_flushed);
|
|
|
|
bkey_init(&last_flushed.k->k);
|
|
|
|
|
2023-12-17 03:30:09 +00:00
|
|
|
int ret = bch2_trans_run(c,
|
2023-09-12 21:16:02 +00:00
|
|
|
for_each_btree_key_commit(trans, iter,
|
2024-04-07 22:05:34 +00:00
|
|
|
BTREE_ID_lru, POS_MIN, BTREE_ITER_prefetch, k,
|
2023-11-11 21:31:50 +00:00
|
|
|
NULL, NULL, BCH_TRANS_COMMIT_no_enospc|BCH_TRANS_COMMIT_lazy_rw,
|
2024-10-01 20:40:33 +00:00
|
|
|
bch2_check_lru_key(trans, &iter, k, &last_flushed)));
|
|
|
|
|
|
|
|
bch2_bkey_buf_exit(&last_flushed, c);
|
2023-12-17 03:43:41 +00:00
|
|
|
bch_err_fn(c, ret);
|
2022-02-17 08:11:39 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|