summaryrefslogtreecommitdiff
path: root/libbcachefs/btree_cache.h
blob: ca8e3195203e12cef752b63a692db5e0a8bc15c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef _BCACHE_BTREE_CACHE_H
#define _BCACHE_BTREE_CACHE_H

#include "bcachefs.h"
#include "btree_types.h"

struct btree_iter;

extern const char * const bch2_btree_ids[];

void bch2_recalc_btree_reserve(struct bch_fs *);

void bch2_btree_node_hash_remove(struct bch_fs *, struct btree *);
int bch2_btree_node_hash_insert(struct bch_fs *, struct btree *,
				unsigned, enum btree_id);

void bch2_btree_node_cannibalize_unlock(struct bch_fs *);
int bch2_btree_node_cannibalize_lock(struct bch_fs *, struct closure *);

struct btree *bch2_btree_node_mem_alloc(struct bch_fs *);

struct btree *bch2_btree_node_get(struct btree_iter *, const struct bkey_i *,
				  unsigned, enum six_lock_type);

void bch2_btree_node_prefetch(struct btree_iter *, const struct bkey_i *,
			      unsigned);

void bch2_fs_btree_exit(struct bch_fs *);
int bch2_fs_btree_init(struct bch_fs *);

#define for_each_cached_btree(_b, _c, _tbl, _iter, _pos)		\
	for ((_tbl) = rht_dereference_rcu((_c)->btree_cache_table.tbl,	\
					  &(_c)->btree_cache_table),	\
	     _iter = 0;	_iter < (_tbl)->size; _iter++)			\
		rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash)

static inline size_t btree_bytes(struct bch_fs *c)
{
	return c->sb.btree_node_size << 9;
}

static inline size_t btree_max_u64s(struct bch_fs *c)
{
	return (btree_bytes(c) - sizeof(struct btree_node)) / sizeof(u64);
}

static inline size_t btree_pages(struct bch_fs *c)
{
	return c->sb.btree_node_size >> (PAGE_SHIFT - 9);
}

static inline size_t btree_page_order(struct bch_fs *c)
{
	return ilog2(btree_pages(c));
}

static inline unsigned btree_blocks(struct bch_fs *c)
{
	return c->sb.btree_node_size >> c->block_bits;
}

#define BTREE_SPLIT_THRESHOLD(c)		(btree_blocks(c) * 3 / 4)

#define BTREE_FOREGROUND_MERGE_THRESHOLD(c)	(btree_max_u64s(c) * 1 / 3)
#define BTREE_FOREGROUND_MERGE_HYSTERESIS(c)			\
	(BTREE_FOREGROUND_MERGE_THRESHOLD(c) +			\
	 (BTREE_FOREGROUND_MERGE_THRESHOLD(c) << 2))

#define btree_node_root(_c, _b)	((_c)->btree_roots[(_b)->btree_id].b)

int bch2_print_btree_node(struct bch_fs *, struct btree *,
			 char *, size_t);

#endif /* _BCACHE_BTREE_CACHE_H */