// SPDX-License-Identifier: GPL-2.0

#include "bcachefs.h"

#include "alloc/background.h"
#include "alloc/lru.h"

#include "btree/bkey_buf.h"
#include "btree/cache.h"
#include "btree/iter.h"
#include "btree/update.h"
#include "btree/write_buffer.h"

#include "data/ec/trigger.h"

#include "init/error.h"
#include "init/progress.h"
#include "init/recovery.h"

/* KEY_TYPE_lru is obsolete: */
int bch2_lru_validate(struct bch_fs *c, struct bkey_s_c k,
		      const struct bkey_validate_context *from)
{
	int ret = 0;

	bkey_fsck_err_on(!lru_pos_time(k.k->p),
			 c, lru_entry_at_time_0,
			 "lru entry at time=0");
fsck_err:
	return ret;
}

__cold 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;

	prt_printf(out, "idx %llu", le64_to_cpu(lru->idx));
}

__cold 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);
}

static int __bch2_lru_set(struct btree_trans *trans, u16 lru_id,
			  u64 dev_bucket, u64 time, bool set)
{
	return time
		? bch2_btree_bit_mod_buffered(trans, BTREE_ID_lru,
					      lru_pos(lru_id, dev_bucket, time), set)
		: 0;
}

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, true);
}

int __bch2_lru_change(struct btree_trans *trans,
		      u16 lru_id, u64 dev_bucket,
		      u64 old_time, u64 new_time)
{
	return  __bch2_lru_set(trans, lru_id, dev_bucket, old_time, false) ?:
		__bch2_lru_set(trans, lru_id, dev_bucket, new_time, true);
}

static const char * const bch2_lru_types[] = {
#define x(n) #n,
	BCH_LRU_TYPES()
#undef x
	NULL
};

int bch2_lru_check_set(struct btree_trans *trans,
		       u16 lru_id,
		       u64 dev_bucket,
		       u64 time,
		       struct bkey_s_c referring_k,
		       struct wb_maybe_flush *last_flushed)
{
	struct bch_fs *c = trans->c;

	CLASS(btree_iter, lru_iter)(trans, BTREE_ID_lru, lru_pos(lru_id, dev_bucket, time), 0);
	struct bkey_s_c lru_k = bkey_try(bch2_btree_iter_peek_slot(&lru_iter));

	if (lru_k.k->type != KEY_TYPE_set) {
		try(bch2_btree_write_buffer_maybe_flush(trans, referring_k, last_flushed));

		CLASS(printbuf, buf)();
		prt_printf(&buf, "missing %s lru entry at pos ", bch2_lru_types[lru_type(lru_k)]);
		bch2_bpos_to_text(&buf, lru_iter.pos);
		prt_newline(&buf);
		bch2_bkey_val_to_text(&buf, c, referring_k);

		if (ret_fsck_err(trans, alloc_key_to_missing_lru_entry, "%s", buf.buf))
			try(bch2_lru_set(trans, lru_id, dev_bucket, time));
	}

	return 0;
}

static struct bbpos lru_pos_to_bp(struct bkey_s_c lru_k)
{
	enum bch_lru_type type = lru_type(lru_k);

	switch (type) {
	case BCH_LRU_read:
	case BCH_LRU_fragmentation:
		return BBPOS(BTREE_ID_alloc, u64_to_bucket(lru_k.k->p.offset));
	case BCH_LRU_stripes:
		return BBPOS(BTREE_ID_stripes, POS(0, lru_k.k->p.offset));
	default:
		BUG();
	}
}

static int bch2_dev_remove_lrus_scan(struct bch_fs *c, struct bch_dev *ca)
{
	CLASS(btree_trans, trans)(c);
	return for_each_btree_key(trans, iter,
				  BTREE_ID_lru, POS_MIN, BTREE_ITER_prefetch, k, ({
		struct bbpos bp = lru_pos_to_bp(k);

		bp.btree == BTREE_ID_alloc && bp.pos.inode == ca->dev_idx
		? (bch2_btree_delete_at(trans, &iter, 0) ?:
		   bch2_trans_commit(trans, NULL, NULL, 0))
		: 0;
	}));
}

static int bch2_dev_remove_lru_range(struct bch_fs *c, u16 lru_id)
{
	/*
	 * lru_end(), not lru_start(lru_id + 1): bch2_btree_delete_range()'s
	 * bound is inclusive - bch2_btree_iter_peek_max() returns keys <= end -
	 * so the exclusive-bound spelling reaches one key into the next LRU.
	 * (lru_id + 1, time 0, dev_bucket 0) is a real position.
	 */
	return bch2_btree_delete_range(c, BTREE_ID_lru,
				       lru_start(lru_id),
				       lru_end(lru_id), 0);
}

int bch2_dev_remove_lrus(struct bch_fs *c, struct bch_dev *ca)
{
	int ret;

	{
		CLASS(btree_trans, trans)(c);
		ret = bch2_btree_write_buffer_flush_sync(trans);
	}
	if (ret)
		goto err;

	if (ca->dev_idx < BCH_LRU_READ_MAX) {
		ret = bch2_dev_remove_lru_range(c, ca->dev_idx) ?:
		      bch2_dev_remove_lru_range(c, bucket_fragmentation_lru(ca->dev_idx));
	} else {
		ret = bch2_dev_remove_lrus_scan(c, ca);
	}
	if (ret)
		goto err;

	/*
	 * Old fs-wide fragmentation LRU entries are not grouped by device; only
	 * pre-upgrade filesystems need the slower full scan to clean them out.
	 */
	if (c->sb.version_upgrade_complete < bcachefs_metadata_version_per_dev_fragmentation_lru)
		ret = bch2_dev_remove_lrus_scan(c, ca);
err:
	bch_err_fn(c, ret);
	return ret;
}

static u64 bkey_lru_type_idx(struct bch_fs *c,
			     enum bch_lru_type type,
			     struct bkey_s_c k)
{
	struct bch_alloc_v4 a_convert;
	const struct bch_alloc_v4 *a;

	switch (type) {
	case BCH_LRU_read:
		a = bch2_alloc_to_v4(k, &a_convert);
		return alloc_lru_idx_read(*a);
	case BCH_LRU_fragmentation: {
		a = bch2_alloc_to_v4(k, &a_convert);

		guard(rcu)();
		struct bch_dev *ca = bch2_dev_rcu_noerror(c, k.k->p.inode);
		return ca
			? alloc_lru_idx_fragmentation(*a, ca)
			: 0;
	}
	case BCH_LRU_stripes:
		return k.k->type == KEY_TYPE_stripe
			? stripe_lru_pos(bkey_s_c_to_stripe(k).v)
			: 0;
	default:
		BUG();
	}
}

static int bch2_check_lru_key(struct btree_trans *trans,
			      struct btree_iter *lru_iter,
			      struct bkey_s_c lru_k,
			      struct wb_maybe_flush *last_flushed)
{
	struct bch_fs *c = trans->c;
	CLASS(printbuf, buf1)();
	CLASS(printbuf, buf2)();

	/*
	 * per_dev_fragmentation_lru upgrade: delete entries in the old
	 * fs-wide fragmentation lru unconditionally - no need to verify
	 * against the backing alloc key, check_alloc_to_lru_refs is
	 * recreating the per-device entries from the alloc btree:
	 */
	if (lru_pos_id(lru_k.k->p) == BCH_LRU_BUCKET_FRAGMENTATION_OLD &&
	    c->sb.version_upgrade_complete < bcachefs_metadata_version_per_dev_fragmentation_lru)
		return bch2_btree_bit_mod_buffered(trans, BTREE_ID_lru, lru_iter->pos, false);

	struct bbpos bp = lru_pos_to_bp(lru_k);

	CLASS(btree_iter, iter)(trans, bp.btree, bp.pos, 0);
	struct bkey_s_c k = bkey_try(bch2_btree_iter_peek_slot(&iter));

	enum bch_lru_type type = lru_type(lru_k);
	u64 idx = bkey_lru_type_idx(c, type, k);

	/*
	 * Read and bucket fragmentation lrus are per-device: check the entry is
	 * in the lru its bucket's device says it belongs to. This is also what
	 * deletes stragglers in the old fs-wide fragmentation lru
	 * (BCH_LRU_BUCKET_FRAGMENTATION_OLD) found after the
	 * per_dev_fragmentation_lru upgrade completed:
	 */
	u16 lru_id = lru_pos_id(lru_k.k->p);
	u16 want_id = lru_id;

	switch (type) {
	case BCH_LRU_read:
		want_id = u64_to_bucket(lru_k.k->p.offset).inode;
		break;
	case BCH_LRU_fragmentation:
		want_id = bucket_fragmentation_lru(u64_to_bucket(lru_k.k->p.offset).inode);
		break;
	default:
		break;
	}

	if (lru_id != want_id ||
	    lru_pos_time(lru_k.k->p) != idx) {
		try(bch2_btree_write_buffer_maybe_flush(trans, lru_k, last_flushed));

		if (ret_fsck_err(trans, lru_entry_bad,
			     "incorrect lru entry: lru %s, expected id %u time %llu\n"
			     "%s\n"
			     "for %s",
			     bch2_lru_types[type],
			     want_id, idx,
			     (bch2_bkey_val_to_text(&buf1, c, lru_k), buf1.buf),
			     (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf)))
			return bch2_btree_bit_mod_buffered(trans, BTREE_ID_lru, lru_iter->pos, false);
	}

	return 0;
}

/*
 * Pin the backing keyspace for one lru: a per-device lru's backing keys are
 * that device's contiguous slice of the alloc btree, the stripe lru's are the
 * whole stripes btree. Everything else - the obsolete fs-wide fragmentation
 * lru, and ids nothing uses - gets no pin: those entries are scattered or
 * bogus, and either way they're stragglers headed for deletion.
 *
 * See lru_id_to_dev() on why this range can be derived from the id at all,
 * and why a misplaced entry falls outside it.
 */
static int check_lru_id_pin(struct btree_trans *trans, u16 lru_id)
{
	unsigned dev;

	if (lru_id_to_dev(lru_id, &dev))
		return bch2_btree_cache_pin_range(trans, BTREE_ID_alloc,
						  POS(dev, 0),
						  SPOS(dev, U64_MAX, U32_MAX));

	if (lru_id == BCH_LRU_STRIPE_FRAGMENTATION)
		return bch2_btree_cache_pin_range(trans, BTREE_ID_stripes,
						  POS_MIN, SPOS_MAX);

	bch2_btree_cache_unpin(trans->c);
	return 0;
}

static int lru_peek_id(struct btree_trans *trans, struct bpos pos, int *lru_id)
{
	CLASS(btree_iter, iter)(trans, BTREE_ID_lru, pos, 0);
	struct bkey_s_c k = bkey_try(bch2_btree_iter_peek(&iter));

	*lru_id = k.k ? (int) lru_pos_id(k.k->p) : -1;
	return 0;
}

int bch2_check_lrus(struct bch_fs *c)
{
	struct wb_maybe_flush last_flushed __cleanup(wb_maybe_flush_exit);
	wb_maybe_flush_init(&last_flushed);

	struct progress_indicator progress;
	bch2_progress_init(&progress, __func__, c, BIT_ULL(BTREE_ID_lru), 0);

	CLASS(btree_trans, trans)(c);
	struct bpos pos = POS_MIN;
	int ret = 0;

	/*
	 * Scan one lru id at a time, with the backing keyspace prefetched and
	 * pinned: the backing lookups are random-order, so on a cold cache
	 * this turns per-key synchronous reads into cache hits.
	 */
	while (1) {
		int lru_id;

		ret = lockrestart_do(trans, lru_peek_id(trans, pos, &lru_id));
		if (ret || lru_id < 0)
			break;

		ret = check_lru_id_pin(trans, lru_id);
		if (ret)
			break;

		ret = for_each_btree_key_max_commit(trans, iter, BTREE_ID_lru,
					lru_start(lru_id), lru_end(lru_id),
					BTREE_ITER_prefetch, k,
					NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
			bch2_progress_update_iter(trans, &progress, &iter) ?:
			wb_maybe_flush_inc(&last_flushed) ?:
			bch2_check_lru_key(trans, &iter, k, &last_flushed);
		}));
		if (ret || lru_id == U16_MAX)
			break;

		pos = lru_start(lru_id + 1);
	}

	bch2_btree_cache_unpin(c);
	return ret;
}
