18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Uses a block device as cache for other block devices; optimized for SSDs.
68c2ecf20Sopenharmony_ci * All allocation is done in buckets, which should match the erase block size
78c2ecf20Sopenharmony_ci * of the device.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Buckets containing cached data are kept on a heap sorted by priority;
108c2ecf20Sopenharmony_ci * bucket priority is increased on cache hit, and periodically all the buckets
118c2ecf20Sopenharmony_ci * on the heap have their priority scaled down. This currently is just used as
128c2ecf20Sopenharmony_ci * an LRU but in the future should allow for more intelligent heuristics.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Buckets have an 8 bit counter; freeing is accomplished by incrementing the
158c2ecf20Sopenharmony_ci * counter. Garbage collection is used to remove stale pointers.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * Indexing is done via a btree; nodes are not necessarily fully sorted, rather
188c2ecf20Sopenharmony_ci * as keys are inserted we only sort the pages that have not yet been written.
198c2ecf20Sopenharmony_ci * When garbage collection is run, we resort the entire node.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * All configuration is done via sysfs; see Documentation/admin-guide/bcache.rst.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "bcache.h"
258c2ecf20Sopenharmony_ci#include "btree.h"
268c2ecf20Sopenharmony_ci#include "debug.h"
278c2ecf20Sopenharmony_ci#include "extents.h"
288c2ecf20Sopenharmony_ci#include "writeback.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic void sort_key_next(struct btree_iter *iter,
318c2ecf20Sopenharmony_ci			  struct btree_iter_set *i)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	i->k = bkey_next(i->k);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	if (i->k == i->end)
368c2ecf20Sopenharmony_ci		*i = iter->data[--iter->used];
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic bool bch_key_sort_cmp(struct btree_iter_set l,
408c2ecf20Sopenharmony_ci			     struct btree_iter_set r)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	int64_t c = bkey_cmp(l.k, r.k);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return c ? c > 0 : l.k < r.k;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic bool __ptr_invalid(struct cache_set *c, const struct bkey *k)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	unsigned int i;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	for (i = 0; i < KEY_PTRS(k); i++)
528c2ecf20Sopenharmony_ci		if (ptr_available(c, k, i)) {
538c2ecf20Sopenharmony_ci			struct cache *ca = PTR_CACHE(c, k, i);
548c2ecf20Sopenharmony_ci			size_t bucket = PTR_BUCKET_NR(c, k, i);
558c2ecf20Sopenharmony_ci			size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci			if (KEY_SIZE(k) + r > c->cache->sb.bucket_size ||
588c2ecf20Sopenharmony_ci			    bucket <  ca->sb.first_bucket ||
598c2ecf20Sopenharmony_ci			    bucket >= ca->sb.nbuckets)
608c2ecf20Sopenharmony_ci				return true;
618c2ecf20Sopenharmony_ci		}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return false;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* Common among btree and extent ptrs */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic const char *bch_ptr_status(struct cache_set *c, const struct bkey *k)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	unsigned int i;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	for (i = 0; i < KEY_PTRS(k); i++)
738c2ecf20Sopenharmony_ci		if (ptr_available(c, k, i)) {
748c2ecf20Sopenharmony_ci			struct cache *ca = PTR_CACHE(c, k, i);
758c2ecf20Sopenharmony_ci			size_t bucket = PTR_BUCKET_NR(c, k, i);
768c2ecf20Sopenharmony_ci			size_t r = bucket_remainder(c, PTR_OFFSET(k, i));
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci			if (KEY_SIZE(k) + r > c->cache->sb.bucket_size)
798c2ecf20Sopenharmony_ci				return "bad, length too big";
808c2ecf20Sopenharmony_ci			if (bucket <  ca->sb.first_bucket)
818c2ecf20Sopenharmony_ci				return "bad, short offset";
828c2ecf20Sopenharmony_ci			if (bucket >= ca->sb.nbuckets)
838c2ecf20Sopenharmony_ci				return "bad, offset past end of device";
848c2ecf20Sopenharmony_ci			if (ptr_stale(c, k, i))
858c2ecf20Sopenharmony_ci				return "stale";
868c2ecf20Sopenharmony_ci		}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (!bkey_cmp(k, &ZERO_KEY))
898c2ecf20Sopenharmony_ci		return "bad, null key";
908c2ecf20Sopenharmony_ci	if (!KEY_PTRS(k))
918c2ecf20Sopenharmony_ci		return "bad, no pointers";
928c2ecf20Sopenharmony_ci	if (!KEY_SIZE(k))
938c2ecf20Sopenharmony_ci		return "zeroed key";
948c2ecf20Sopenharmony_ci	return "";
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_civoid bch_extent_to_text(char *buf, size_t size, const struct bkey *k)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	unsigned int i = 0;
1008c2ecf20Sopenharmony_ci	char *out = buf, *end = buf + size;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define p(...)	(out += scnprintf(out, end - out, __VA_ARGS__))
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	p("%llu:%llu len %llu -> [", KEY_INODE(k), KEY_START(k), KEY_SIZE(k));
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	for (i = 0; i < KEY_PTRS(k); i++) {
1078c2ecf20Sopenharmony_ci		if (i)
1088c2ecf20Sopenharmony_ci			p(", ");
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		if (PTR_DEV(k, i) == PTR_CHECK_DEV)
1118c2ecf20Sopenharmony_ci			p("check dev");
1128c2ecf20Sopenharmony_ci		else
1138c2ecf20Sopenharmony_ci			p("%llu:%llu gen %llu", PTR_DEV(k, i),
1148c2ecf20Sopenharmony_ci			  PTR_OFFSET(k, i), PTR_GEN(k, i));
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	p("]");
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (KEY_DIRTY(k))
1208c2ecf20Sopenharmony_ci		p(" dirty");
1218c2ecf20Sopenharmony_ci	if (KEY_CSUM(k))
1228c2ecf20Sopenharmony_ci		p(" cs%llu %llx", KEY_CSUM(k), k->ptr[1]);
1238c2ecf20Sopenharmony_ci#undef p
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic void bch_bkey_dump(struct btree_keys *keys, const struct bkey *k)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct btree *b = container_of(keys, struct btree, keys);
1298c2ecf20Sopenharmony_ci	unsigned int j;
1308c2ecf20Sopenharmony_ci	char buf[80];
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	bch_extent_to_text(buf, sizeof(buf), k);
1338c2ecf20Sopenharmony_ci	pr_cont(" %s", buf);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	for (j = 0; j < KEY_PTRS(k); j++) {
1368c2ecf20Sopenharmony_ci		size_t n = PTR_BUCKET_NR(b->c, k, j);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		pr_cont(" bucket %zu", n);
1398c2ecf20Sopenharmony_ci		if (n >= b->c->cache->sb.first_bucket && n < b->c->cache->sb.nbuckets)
1408c2ecf20Sopenharmony_ci			pr_cont(" prio %i",
1418c2ecf20Sopenharmony_ci				PTR_BUCKET(b->c, k, j)->prio);
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	pr_cont(" %s\n", bch_ptr_status(b->c, k));
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci/* Btree ptrs */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cibool __bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	char buf[80];
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k))
1548c2ecf20Sopenharmony_ci		goto bad;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (__ptr_invalid(c, k))
1578c2ecf20Sopenharmony_ci		goto bad;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return false;
1608c2ecf20Sopenharmony_cibad:
1618c2ecf20Sopenharmony_ci	bch_extent_to_text(buf, sizeof(buf), k);
1628c2ecf20Sopenharmony_ci	cache_bug(c, "spotted btree ptr %s: %s", buf, bch_ptr_status(c, k));
1638c2ecf20Sopenharmony_ci	return true;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic bool bch_btree_ptr_invalid(struct btree_keys *bk, const struct bkey *k)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct btree *b = container_of(bk, struct btree, keys);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return __bch_btree_ptr_invalid(b->c, k);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic bool btree_ptr_bad_expensive(struct btree *b, const struct bkey *k)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	unsigned int i;
1768c2ecf20Sopenharmony_ci	char buf[80];
1778c2ecf20Sopenharmony_ci	struct bucket *g;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (mutex_trylock(&b->c->bucket_lock)) {
1808c2ecf20Sopenharmony_ci		for (i = 0; i < KEY_PTRS(k); i++)
1818c2ecf20Sopenharmony_ci			if (ptr_available(b->c, k, i)) {
1828c2ecf20Sopenharmony_ci				g = PTR_BUCKET(b->c, k, i);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci				if (KEY_DIRTY(k) ||
1858c2ecf20Sopenharmony_ci				    g->prio != BTREE_PRIO ||
1868c2ecf20Sopenharmony_ci				    (b->c->gc_mark_valid &&
1878c2ecf20Sopenharmony_ci				     GC_MARK(g) != GC_MARK_METADATA))
1888c2ecf20Sopenharmony_ci					goto err;
1898c2ecf20Sopenharmony_ci			}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci		mutex_unlock(&b->c->bucket_lock);
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return false;
1958c2ecf20Sopenharmony_cierr:
1968c2ecf20Sopenharmony_ci	mutex_unlock(&b->c->bucket_lock);
1978c2ecf20Sopenharmony_ci	bch_extent_to_text(buf, sizeof(buf), k);
1988c2ecf20Sopenharmony_ci	btree_bug(b,
1998c2ecf20Sopenharmony_ci"inconsistent btree pointer %s: bucket %zi pin %i prio %i gen %i last_gc %i mark %llu",
2008c2ecf20Sopenharmony_ci		  buf, PTR_BUCKET_NR(b->c, k, i), atomic_read(&g->pin),
2018c2ecf20Sopenharmony_ci		  g->prio, g->gen, g->last_gc, GC_MARK(g));
2028c2ecf20Sopenharmony_ci	return true;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic bool bch_btree_ptr_bad(struct btree_keys *bk, const struct bkey *k)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	struct btree *b = container_of(bk, struct btree, keys);
2088c2ecf20Sopenharmony_ci	unsigned int i;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (!bkey_cmp(k, &ZERO_KEY) ||
2118c2ecf20Sopenharmony_ci	    !KEY_PTRS(k) ||
2128c2ecf20Sopenharmony_ci	    bch_ptr_invalid(bk, k))
2138c2ecf20Sopenharmony_ci		return true;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	for (i = 0; i < KEY_PTRS(k); i++)
2168c2ecf20Sopenharmony_ci		if (!ptr_available(b->c, k, i) ||
2178c2ecf20Sopenharmony_ci		    ptr_stale(b->c, k, i))
2188c2ecf20Sopenharmony_ci			return true;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	if (expensive_debug_checks(b->c) &&
2218c2ecf20Sopenharmony_ci	    btree_ptr_bad_expensive(b, k))
2228c2ecf20Sopenharmony_ci		return true;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return false;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic bool bch_btree_ptr_insert_fixup(struct btree_keys *bk,
2288c2ecf20Sopenharmony_ci				       struct bkey *insert,
2298c2ecf20Sopenharmony_ci				       struct btree_iter *iter,
2308c2ecf20Sopenharmony_ci				       struct bkey *replace_key)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	struct btree *b = container_of(bk, struct btree, keys);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (!KEY_OFFSET(insert))
2358c2ecf20Sopenharmony_ci		btree_current_write(b)->prio_blocked++;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	return false;
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ciconst struct btree_keys_ops bch_btree_keys_ops = {
2418c2ecf20Sopenharmony_ci	.sort_cmp	= bch_key_sort_cmp,
2428c2ecf20Sopenharmony_ci	.insert_fixup	= bch_btree_ptr_insert_fixup,
2438c2ecf20Sopenharmony_ci	.key_invalid	= bch_btree_ptr_invalid,
2448c2ecf20Sopenharmony_ci	.key_bad	= bch_btree_ptr_bad,
2458c2ecf20Sopenharmony_ci	.key_to_text	= bch_extent_to_text,
2468c2ecf20Sopenharmony_ci	.key_dump	= bch_bkey_dump,
2478c2ecf20Sopenharmony_ci};
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci/* Extents */
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/*
2528c2ecf20Sopenharmony_ci * Returns true if l > r - unless l == r, in which case returns true if l is
2538c2ecf20Sopenharmony_ci * older than r.
2548c2ecf20Sopenharmony_ci *
2558c2ecf20Sopenharmony_ci * Necessary for btree_sort_fixup() - if there are multiple keys that compare
2568c2ecf20Sopenharmony_ci * equal in different sets, we have to process them newest to oldest.
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_cistatic bool bch_extent_sort_cmp(struct btree_iter_set l,
2598c2ecf20Sopenharmony_ci				struct btree_iter_set r)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	int64_t c = bkey_cmp(&START_KEY(l.k), &START_KEY(r.k));
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return c ? c > 0 : l.k < r.k;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic struct bkey *bch_extent_sort_fixup(struct btree_iter *iter,
2678c2ecf20Sopenharmony_ci					  struct bkey *tmp)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	while (iter->used > 1) {
2708c2ecf20Sopenharmony_ci		struct btree_iter_set *top = iter->data, *i = top + 1;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		if (iter->used > 2 &&
2738c2ecf20Sopenharmony_ci		    bch_extent_sort_cmp(i[0], i[1]))
2748c2ecf20Sopenharmony_ci			i++;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci		if (bkey_cmp(top->k, &START_KEY(i->k)) <= 0)
2778c2ecf20Sopenharmony_ci			break;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci		if (!KEY_SIZE(i->k)) {
2808c2ecf20Sopenharmony_ci			sort_key_next(iter, i);
2818c2ecf20Sopenharmony_ci			heap_sift(iter, i - top, bch_extent_sort_cmp);
2828c2ecf20Sopenharmony_ci			continue;
2838c2ecf20Sopenharmony_ci		}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci		if (top->k > i->k) {
2868c2ecf20Sopenharmony_ci			if (bkey_cmp(top->k, i->k) >= 0)
2878c2ecf20Sopenharmony_ci				sort_key_next(iter, i);
2888c2ecf20Sopenharmony_ci			else
2898c2ecf20Sopenharmony_ci				bch_cut_front(top->k, i->k);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci			heap_sift(iter, i - top, bch_extent_sort_cmp);
2928c2ecf20Sopenharmony_ci		} else {
2938c2ecf20Sopenharmony_ci			/* can't happen because of comparison func */
2948c2ecf20Sopenharmony_ci			BUG_ON(!bkey_cmp(&START_KEY(top->k), &START_KEY(i->k)));
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci			if (bkey_cmp(i->k, top->k) < 0) {
2978c2ecf20Sopenharmony_ci				bkey_copy(tmp, top->k);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci				bch_cut_back(&START_KEY(i->k), tmp);
3008c2ecf20Sopenharmony_ci				bch_cut_front(i->k, top->k);
3018c2ecf20Sopenharmony_ci				heap_sift(iter, 0, bch_extent_sort_cmp);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci				return tmp;
3048c2ecf20Sopenharmony_ci			} else {
3058c2ecf20Sopenharmony_ci				bch_cut_back(&START_KEY(i->k), top->k);
3068c2ecf20Sopenharmony_ci			}
3078c2ecf20Sopenharmony_ci		}
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return NULL;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic void bch_subtract_dirty(struct bkey *k,
3148c2ecf20Sopenharmony_ci			   struct cache_set *c,
3158c2ecf20Sopenharmony_ci			   uint64_t offset,
3168c2ecf20Sopenharmony_ci			   int sectors)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	if (KEY_DIRTY(k))
3198c2ecf20Sopenharmony_ci		bcache_dev_sectors_dirty_add(c, KEY_INODE(k),
3208c2ecf20Sopenharmony_ci					     offset, -sectors);
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic bool bch_extent_insert_fixup(struct btree_keys *b,
3248c2ecf20Sopenharmony_ci				    struct bkey *insert,
3258c2ecf20Sopenharmony_ci				    struct btree_iter *iter,
3268c2ecf20Sopenharmony_ci				    struct bkey *replace_key)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	struct cache_set *c = container_of(b, struct btree, keys)->c;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	uint64_t old_offset;
3318c2ecf20Sopenharmony_ci	unsigned int old_size, sectors_found = 0;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	BUG_ON(!KEY_OFFSET(insert));
3348c2ecf20Sopenharmony_ci	BUG_ON(!KEY_SIZE(insert));
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	while (1) {
3378c2ecf20Sopenharmony_ci		struct bkey *k = bch_btree_iter_next(iter);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci		if (!k)
3408c2ecf20Sopenharmony_ci			break;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci		if (bkey_cmp(&START_KEY(k), insert) >= 0) {
3438c2ecf20Sopenharmony_ci			if (KEY_SIZE(k))
3448c2ecf20Sopenharmony_ci				break;
3458c2ecf20Sopenharmony_ci			else
3468c2ecf20Sopenharmony_ci				continue;
3478c2ecf20Sopenharmony_ci		}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci		if (bkey_cmp(k, &START_KEY(insert)) <= 0)
3508c2ecf20Sopenharmony_ci			continue;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci		old_offset = KEY_START(k);
3538c2ecf20Sopenharmony_ci		old_size = KEY_SIZE(k);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci		/*
3568c2ecf20Sopenharmony_ci		 * We might overlap with 0 size extents; we can't skip these
3578c2ecf20Sopenharmony_ci		 * because if they're in the set we're inserting to we have to
3588c2ecf20Sopenharmony_ci		 * adjust them so they don't overlap with the key we're
3598c2ecf20Sopenharmony_ci		 * inserting. But we don't want to check them for replace
3608c2ecf20Sopenharmony_ci		 * operations.
3618c2ecf20Sopenharmony_ci		 */
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		if (replace_key && KEY_SIZE(k)) {
3648c2ecf20Sopenharmony_ci			/*
3658c2ecf20Sopenharmony_ci			 * k might have been split since we inserted/found the
3668c2ecf20Sopenharmony_ci			 * key we're replacing
3678c2ecf20Sopenharmony_ci			 */
3688c2ecf20Sopenharmony_ci			unsigned int i;
3698c2ecf20Sopenharmony_ci			uint64_t offset = KEY_START(k) -
3708c2ecf20Sopenharmony_ci				KEY_START(replace_key);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci			/* But it must be a subset of the replace key */
3738c2ecf20Sopenharmony_ci			if (KEY_START(k) < KEY_START(replace_key) ||
3748c2ecf20Sopenharmony_ci			    KEY_OFFSET(k) > KEY_OFFSET(replace_key))
3758c2ecf20Sopenharmony_ci				goto check_failed;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci			/* We didn't find a key that we were supposed to */
3788c2ecf20Sopenharmony_ci			if (KEY_START(k) > KEY_START(insert) + sectors_found)
3798c2ecf20Sopenharmony_ci				goto check_failed;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci			if (!bch_bkey_equal_header(k, replace_key))
3828c2ecf20Sopenharmony_ci				goto check_failed;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci			/* skip past gen */
3858c2ecf20Sopenharmony_ci			offset <<= 8;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci			BUG_ON(!KEY_PTRS(replace_key));
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci			for (i = 0; i < KEY_PTRS(replace_key); i++)
3908c2ecf20Sopenharmony_ci				if (k->ptr[i] != replace_key->ptr[i] + offset)
3918c2ecf20Sopenharmony_ci					goto check_failed;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci			sectors_found = KEY_OFFSET(k) - KEY_START(insert);
3948c2ecf20Sopenharmony_ci		}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci		if (bkey_cmp(insert, k) < 0 &&
3978c2ecf20Sopenharmony_ci		    bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0) {
3988c2ecf20Sopenharmony_ci			/*
3998c2ecf20Sopenharmony_ci			 * We overlapped in the middle of an existing key: that
4008c2ecf20Sopenharmony_ci			 * means we have to split the old key. But we have to do
4018c2ecf20Sopenharmony_ci			 * slightly different things depending on whether the
4028c2ecf20Sopenharmony_ci			 * old key has been written out yet.
4038c2ecf20Sopenharmony_ci			 */
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci			struct bkey *top;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci			bch_subtract_dirty(k, c, KEY_START(insert),
4088c2ecf20Sopenharmony_ci				       KEY_SIZE(insert));
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci			if (bkey_written(b, k)) {
4118c2ecf20Sopenharmony_ci				/*
4128c2ecf20Sopenharmony_ci				 * We insert a new key to cover the top of the
4138c2ecf20Sopenharmony_ci				 * old key, and the old key is modified in place
4148c2ecf20Sopenharmony_ci				 * to represent the bottom split.
4158c2ecf20Sopenharmony_ci				 *
4168c2ecf20Sopenharmony_ci				 * It's completely arbitrary whether the new key
4178c2ecf20Sopenharmony_ci				 * is the top or the bottom, but it has to match
4188c2ecf20Sopenharmony_ci				 * up with what btree_sort_fixup() does - it
4198c2ecf20Sopenharmony_ci				 * doesn't check for this kind of overlap, it
4208c2ecf20Sopenharmony_ci				 * depends on us inserting a new key for the top
4218c2ecf20Sopenharmony_ci				 * here.
4228c2ecf20Sopenharmony_ci				 */
4238c2ecf20Sopenharmony_ci				top = bch_bset_search(b, bset_tree_last(b),
4248c2ecf20Sopenharmony_ci						      insert);
4258c2ecf20Sopenharmony_ci				bch_bset_insert(b, top, k);
4268c2ecf20Sopenharmony_ci			} else {
4278c2ecf20Sopenharmony_ci				BKEY_PADDED(key) temp;
4288c2ecf20Sopenharmony_ci				bkey_copy(&temp.key, k);
4298c2ecf20Sopenharmony_ci				bch_bset_insert(b, k, &temp.key);
4308c2ecf20Sopenharmony_ci				top = bkey_next(k);
4318c2ecf20Sopenharmony_ci			}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci			bch_cut_front(insert, top);
4348c2ecf20Sopenharmony_ci			bch_cut_back(&START_KEY(insert), k);
4358c2ecf20Sopenharmony_ci			bch_bset_fix_invalidated_key(b, k);
4368c2ecf20Sopenharmony_ci			goto out;
4378c2ecf20Sopenharmony_ci		}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		if (bkey_cmp(insert, k) < 0) {
4408c2ecf20Sopenharmony_ci			bch_cut_front(insert, k);
4418c2ecf20Sopenharmony_ci		} else {
4428c2ecf20Sopenharmony_ci			if (bkey_cmp(&START_KEY(insert), &START_KEY(k)) > 0)
4438c2ecf20Sopenharmony_ci				old_offset = KEY_START(insert);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci			if (bkey_written(b, k) &&
4468c2ecf20Sopenharmony_ci			    bkey_cmp(&START_KEY(insert), &START_KEY(k)) <= 0) {
4478c2ecf20Sopenharmony_ci				/*
4488c2ecf20Sopenharmony_ci				 * Completely overwrote, so we don't have to
4498c2ecf20Sopenharmony_ci				 * invalidate the binary search tree
4508c2ecf20Sopenharmony_ci				 */
4518c2ecf20Sopenharmony_ci				bch_cut_front(k, k);
4528c2ecf20Sopenharmony_ci			} else {
4538c2ecf20Sopenharmony_ci				__bch_cut_back(&START_KEY(insert), k);
4548c2ecf20Sopenharmony_ci				bch_bset_fix_invalidated_key(b, k);
4558c2ecf20Sopenharmony_ci			}
4568c2ecf20Sopenharmony_ci		}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci		bch_subtract_dirty(k, c, old_offset, old_size - KEY_SIZE(k));
4598c2ecf20Sopenharmony_ci	}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cicheck_failed:
4628c2ecf20Sopenharmony_ci	if (replace_key) {
4638c2ecf20Sopenharmony_ci		if (!sectors_found) {
4648c2ecf20Sopenharmony_ci			return true;
4658c2ecf20Sopenharmony_ci		} else if (sectors_found < KEY_SIZE(insert)) {
4668c2ecf20Sopenharmony_ci			SET_KEY_OFFSET(insert, KEY_OFFSET(insert) -
4678c2ecf20Sopenharmony_ci				       (KEY_SIZE(insert) - sectors_found));
4688c2ecf20Sopenharmony_ci			SET_KEY_SIZE(insert, sectors_found);
4698c2ecf20Sopenharmony_ci		}
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ciout:
4728c2ecf20Sopenharmony_ci	if (KEY_DIRTY(insert))
4738c2ecf20Sopenharmony_ci		bcache_dev_sectors_dirty_add(c, KEY_INODE(insert),
4748c2ecf20Sopenharmony_ci					     KEY_START(insert),
4758c2ecf20Sopenharmony_ci					     KEY_SIZE(insert));
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	return false;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_cibool __bch_extent_invalid(struct cache_set *c, const struct bkey *k)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	char buf[80];
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	if (!KEY_SIZE(k))
4858c2ecf20Sopenharmony_ci		return true;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	if (KEY_SIZE(k) > KEY_OFFSET(k))
4888c2ecf20Sopenharmony_ci		goto bad;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (__ptr_invalid(c, k))
4918c2ecf20Sopenharmony_ci		goto bad;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	return false;
4948c2ecf20Sopenharmony_cibad:
4958c2ecf20Sopenharmony_ci	bch_extent_to_text(buf, sizeof(buf), k);
4968c2ecf20Sopenharmony_ci	cache_bug(c, "spotted extent %s: %s", buf, bch_ptr_status(c, k));
4978c2ecf20Sopenharmony_ci	return true;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic bool bch_extent_invalid(struct btree_keys *bk, const struct bkey *k)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	struct btree *b = container_of(bk, struct btree, keys);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	return __bch_extent_invalid(b->c, k);
5058c2ecf20Sopenharmony_ci}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_cistatic bool bch_extent_bad_expensive(struct btree *b, const struct bkey *k,
5088c2ecf20Sopenharmony_ci				     unsigned int ptr)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	struct bucket *g = PTR_BUCKET(b->c, k, ptr);
5118c2ecf20Sopenharmony_ci	char buf[80];
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	if (mutex_trylock(&b->c->bucket_lock)) {
5148c2ecf20Sopenharmony_ci		if (b->c->gc_mark_valid &&
5158c2ecf20Sopenharmony_ci		    (!GC_MARK(g) ||
5168c2ecf20Sopenharmony_ci		     GC_MARK(g) == GC_MARK_METADATA ||
5178c2ecf20Sopenharmony_ci		     (GC_MARK(g) != GC_MARK_DIRTY && KEY_DIRTY(k))))
5188c2ecf20Sopenharmony_ci			goto err;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci		if (g->prio == BTREE_PRIO)
5218c2ecf20Sopenharmony_ci			goto err;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci		mutex_unlock(&b->c->bucket_lock);
5248c2ecf20Sopenharmony_ci	}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	return false;
5278c2ecf20Sopenharmony_cierr:
5288c2ecf20Sopenharmony_ci	mutex_unlock(&b->c->bucket_lock);
5298c2ecf20Sopenharmony_ci	bch_extent_to_text(buf, sizeof(buf), k);
5308c2ecf20Sopenharmony_ci	btree_bug(b,
5318c2ecf20Sopenharmony_ci"inconsistent extent pointer %s:\nbucket %zu pin %i prio %i gen %i last_gc %i mark %llu",
5328c2ecf20Sopenharmony_ci		  buf, PTR_BUCKET_NR(b->c, k, ptr), atomic_read(&g->pin),
5338c2ecf20Sopenharmony_ci		  g->prio, g->gen, g->last_gc, GC_MARK(g));
5348c2ecf20Sopenharmony_ci	return true;
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_cistatic bool bch_extent_bad(struct btree_keys *bk, const struct bkey *k)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	struct btree *b = container_of(bk, struct btree, keys);
5408c2ecf20Sopenharmony_ci	unsigned int i, stale;
5418c2ecf20Sopenharmony_ci	char buf[80];
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (!KEY_PTRS(k) ||
5448c2ecf20Sopenharmony_ci	    bch_extent_invalid(bk, k))
5458c2ecf20Sopenharmony_ci		return true;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	for (i = 0; i < KEY_PTRS(k); i++)
5488c2ecf20Sopenharmony_ci		if (!ptr_available(b->c, k, i))
5498c2ecf20Sopenharmony_ci			return true;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	for (i = 0; i < KEY_PTRS(k); i++) {
5528c2ecf20Sopenharmony_ci		stale = ptr_stale(b->c, k, i);
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		if (stale && KEY_DIRTY(k)) {
5558c2ecf20Sopenharmony_ci			bch_extent_to_text(buf, sizeof(buf), k);
5568c2ecf20Sopenharmony_ci			pr_info("stale dirty pointer, stale %u, key: %s\n",
5578c2ecf20Sopenharmony_ci				stale, buf);
5588c2ecf20Sopenharmony_ci		}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci		btree_bug_on(stale > BUCKET_GC_GEN_MAX, b,
5618c2ecf20Sopenharmony_ci			     "key too stale: %i, need_gc %u",
5628c2ecf20Sopenharmony_ci			     stale, b->c->need_gc);
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci		if (stale)
5658c2ecf20Sopenharmony_ci			return true;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci		if (expensive_debug_checks(b->c) &&
5688c2ecf20Sopenharmony_ci		    bch_extent_bad_expensive(b, k, i))
5698c2ecf20Sopenharmony_ci			return true;
5708c2ecf20Sopenharmony_ci	}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	return false;
5738c2ecf20Sopenharmony_ci}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_cistatic uint64_t merge_chksums(struct bkey *l, struct bkey *r)
5768c2ecf20Sopenharmony_ci{
5778c2ecf20Sopenharmony_ci	return (l->ptr[KEY_PTRS(l)] + r->ptr[KEY_PTRS(r)]) &
5788c2ecf20Sopenharmony_ci		~((uint64_t)1 << 63);
5798c2ecf20Sopenharmony_ci}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_cistatic bool bch_extent_merge(struct btree_keys *bk,
5828c2ecf20Sopenharmony_ci			     struct bkey *l,
5838c2ecf20Sopenharmony_ci			     struct bkey *r)
5848c2ecf20Sopenharmony_ci{
5858c2ecf20Sopenharmony_ci	struct btree *b = container_of(bk, struct btree, keys);
5868c2ecf20Sopenharmony_ci	unsigned int i;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	if (key_merging_disabled(b->c))
5898c2ecf20Sopenharmony_ci		return false;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	for (i = 0; i < KEY_PTRS(l); i++)
5928c2ecf20Sopenharmony_ci		if (l->ptr[i] + MAKE_PTR(0, KEY_SIZE(l), 0) != r->ptr[i] ||
5938c2ecf20Sopenharmony_ci		    PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i))
5948c2ecf20Sopenharmony_ci			return false;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/* Keys with no pointers aren't restricted to one bucket and could
5978c2ecf20Sopenharmony_ci	 * overflow KEY_SIZE
5988c2ecf20Sopenharmony_ci	 */
5998c2ecf20Sopenharmony_ci	if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) {
6008c2ecf20Sopenharmony_ci		SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l));
6018c2ecf20Sopenharmony_ci		SET_KEY_SIZE(l, USHRT_MAX);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		bch_cut_front(l, r);
6048c2ecf20Sopenharmony_ci		return false;
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	if (KEY_CSUM(l)) {
6088c2ecf20Sopenharmony_ci		if (KEY_CSUM(r))
6098c2ecf20Sopenharmony_ci			l->ptr[KEY_PTRS(l)] = merge_chksums(l, r);
6108c2ecf20Sopenharmony_ci		else
6118c2ecf20Sopenharmony_ci			SET_KEY_CSUM(l, 0);
6128c2ecf20Sopenharmony_ci	}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r));
6158c2ecf20Sopenharmony_ci	SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r));
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	return true;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ciconst struct btree_keys_ops bch_extent_keys_ops = {
6218c2ecf20Sopenharmony_ci	.sort_cmp	= bch_extent_sort_cmp,
6228c2ecf20Sopenharmony_ci	.sort_fixup	= bch_extent_sort_fixup,
6238c2ecf20Sopenharmony_ci	.insert_fixup	= bch_extent_insert_fixup,
6248c2ecf20Sopenharmony_ci	.key_invalid	= bch_extent_invalid,
6258c2ecf20Sopenharmony_ci	.key_bad	= bch_extent_bad,
6268c2ecf20Sopenharmony_ci	.key_merge	= bch_extent_merge,
6278c2ecf20Sopenharmony_ci	.key_to_text	= bch_extent_to_text,
6288c2ecf20Sopenharmony_ci	.key_dump	= bch_bkey_dump,
6298c2ecf20Sopenharmony_ci	.is_extents	= true,
6308c2ecf20Sopenharmony_ci};
631