xref: /openbmc/linux/drivers/md/bcache/btree.h (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2cafe5635SKent Overstreet #ifndef _BCACHE_BTREE_H
3cafe5635SKent Overstreet #define _BCACHE_BTREE_H
4cafe5635SKent Overstreet 
5cafe5635SKent Overstreet /*
6cafe5635SKent Overstreet  * THE BTREE:
7cafe5635SKent Overstreet  *
8cafe5635SKent Overstreet  * At a high level, bcache's btree is relatively standard b+ tree. All keys and
9cafe5635SKent Overstreet  * pointers are in the leaves; interior nodes only have pointers to the child
10cafe5635SKent Overstreet  * nodes.
11cafe5635SKent Overstreet  *
12cafe5635SKent Overstreet  * In the interior nodes, a struct bkey always points to a child btree node, and
13cafe5635SKent Overstreet  * the key is the highest key in the child node - except that the highest key in
14cafe5635SKent Overstreet  * an interior node is always MAX_KEY. The size field refers to the size on disk
15cafe5635SKent Overstreet  * of the child node - this would allow us to have variable sized btree nodes
16cafe5635SKent Overstreet  * (handy for keeping the depth of the btree 1 by expanding just the root).
17cafe5635SKent Overstreet  *
18cafe5635SKent Overstreet  * Btree nodes are themselves log structured, but this is hidden fairly
19cafe5635SKent Overstreet  * thoroughly. Btree nodes on disk will in practice have extents that overlap
20cafe5635SKent Overstreet  * (because they were written at different times), but in memory we never have
21cafe5635SKent Overstreet  * overlapping extents - when we read in a btree node from disk, the first thing
22cafe5635SKent Overstreet  * we do is resort all the sets of keys with a mergesort, and in the same pass
23cafe5635SKent Overstreet  * we check for overlapping extents and adjust them appropriately.
24cafe5635SKent Overstreet  *
25cafe5635SKent Overstreet  * struct btree_op is a central interface to the btree code. It's used for
26cafe5635SKent Overstreet  * specifying read vs. write locking, and the embedded closure is used for
27cafe5635SKent Overstreet  * waiting on IO or reserve memory.
28cafe5635SKent Overstreet  *
29cafe5635SKent Overstreet  * BTREE CACHE:
30cafe5635SKent Overstreet  *
31cafe5635SKent Overstreet  * Btree nodes are cached in memory; traversing the btree might require reading
32cafe5635SKent Overstreet  * in btree nodes which is handled mostly transparently.
33cafe5635SKent Overstreet  *
34cafe5635SKent Overstreet  * bch_btree_node_get() looks up a btree node in the cache and reads it in from
35cafe5635SKent Overstreet  * disk if necessary. This function is almost never called directly though - the
36cafe5635SKent Overstreet  * btree() macro is used to get a btree node, call some function on it, and
37cafe5635SKent Overstreet  * unlock the node after the function returns.
38cafe5635SKent Overstreet  *
39cafe5635SKent Overstreet  * The root is special cased - it's taken out of the cache's lru (thus pinning
40cafe5635SKent Overstreet  * it in memory), so we can find the root of the btree by just dereferencing a
41cafe5635SKent Overstreet  * pointer instead of looking it up in the cache. This makes locking a bit
42cafe5635SKent Overstreet  * tricky, since the root pointer is protected by the lock in the btree node it
43cafe5635SKent Overstreet  * points to - the btree_root() macro handles this.
44cafe5635SKent Overstreet  *
45cafe5635SKent Overstreet  * In various places we must be able to allocate memory for multiple btree nodes
46cafe5635SKent Overstreet  * in order to make forward progress. To do this we use the btree cache itself
47cafe5635SKent Overstreet  * as a reserve; if __get_free_pages() fails, we'll find a node in the btree
48cafe5635SKent Overstreet  * cache we can reuse. We can't allow more than one thread to be doing this at a
49cafe5635SKent Overstreet  * time, so there's a lock, implemented by a pointer to the btree_op closure -
50cafe5635SKent Overstreet  * this allows the btree_root() macro to implicitly release this lock.
51cafe5635SKent Overstreet  *
52cafe5635SKent Overstreet  * BTREE IO:
53cafe5635SKent Overstreet  *
54cafe5635SKent Overstreet  * Btree nodes never have to be explicitly read in; bch_btree_node_get() handles
55cafe5635SKent Overstreet  * this.
56cafe5635SKent Overstreet  *
57cafe5635SKent Overstreet  * For writing, we have two btree_write structs embeddded in struct btree - one
58cafe5635SKent Overstreet  * write in flight, and one being set up, and we toggle between them.
59cafe5635SKent Overstreet  *
60cafe5635SKent Overstreet  * Writing is done with a single function -  bch_btree_write() really serves two
61cafe5635SKent Overstreet  * different purposes and should be broken up into two different functions. When
62cafe5635SKent Overstreet  * passing now = false, it merely indicates that the node is now dirty - calling
63cafe5635SKent Overstreet  * it ensures that the dirty keys will be written at some point in the future.
64cafe5635SKent Overstreet  *
65cafe5635SKent Overstreet  * When passing now = true, bch_btree_write() causes a write to happen
66cafe5635SKent Overstreet  * "immediately" (if there was already a write in flight, it'll cause the write
67cafe5635SKent Overstreet  * to happen as soon as the previous write completes). It returns immediately
68cafe5635SKent Overstreet  * though - but it takes a refcount on the closure in struct btree_op you passed
69cafe5635SKent Overstreet  * to it, so a closure_sync() later can be used to wait for the write to
70cafe5635SKent Overstreet  * complete.
71cafe5635SKent Overstreet  *
72cafe5635SKent Overstreet  * This is handy because btree_split() and garbage collection can issue writes
73cafe5635SKent Overstreet  * in parallel, reducing the amount of time they have to hold write locks.
74cafe5635SKent Overstreet  *
75cafe5635SKent Overstreet  * LOCKING:
76cafe5635SKent Overstreet  *
77cafe5635SKent Overstreet  * When traversing the btree, we may need write locks starting at some level -
78cafe5635SKent Overstreet  * inserting a key into the btree will typically only require a write lock on
79cafe5635SKent Overstreet  * the leaf node.
80cafe5635SKent Overstreet  *
81cafe5635SKent Overstreet  * This is specified with the lock field in struct btree_op; lock = 0 means we
82cafe5635SKent Overstreet  * take write locks at level <= 0, i.e. only leaf nodes. bch_btree_node_get()
83cafe5635SKent Overstreet  * checks this field and returns the node with the appropriate lock held.
84cafe5635SKent Overstreet  *
85cafe5635SKent Overstreet  * If, after traversing the btree, the insertion code discovers it has to split
86cafe5635SKent Overstreet  * then it must restart from the root and take new locks - to do this it changes
87cafe5635SKent Overstreet  * the lock field and returns -EINTR, which causes the btree_root() macro to
88cafe5635SKent Overstreet  * loop.
89cafe5635SKent Overstreet  *
90cafe5635SKent Overstreet  * Handling cache misses require a different mechanism for upgrading to a write
91cafe5635SKent Overstreet  * lock. We do cache lookups with only a read lock held, but if we get a cache
92cafe5635SKent Overstreet  * miss and we wish to insert this data into the cache, we have to insert a
93cafe5635SKent Overstreet  * placeholder key to detect races - otherwise, we could race with a write and
94cafe5635SKent Overstreet  * overwrite the data that was just written to the cache with stale data from
95cafe5635SKent Overstreet  * the backing device.
96cafe5635SKent Overstreet  *
97cafe5635SKent Overstreet  * For this we use a sequence number that write locks and unlocks increment - to
98cafe5635SKent Overstreet  * insert the check key it unlocks the btree node and then takes a write lock,
99cafe5635SKent Overstreet  * and fails if the sequence number doesn't match.
100cafe5635SKent Overstreet  */
101cafe5635SKent Overstreet 
102cafe5635SKent Overstreet #include "bset.h"
103cafe5635SKent Overstreet #include "debug.h"
104cafe5635SKent Overstreet 
105cafe5635SKent Overstreet struct btree_write {
106cafe5635SKent Overstreet 	atomic_t		*journal;
107cafe5635SKent Overstreet 
108cafe5635SKent Overstreet 	/* If btree_split() frees a btree node, it writes a new pointer to that
109cafe5635SKent Overstreet 	 * btree node indicating it was freed; it takes a refcount on
110cafe5635SKent Overstreet 	 * c->prio_blocked because we can't write the gens until the new
111cafe5635SKent Overstreet 	 * pointer is on disk. This allows btree_write_endio() to release the
112cafe5635SKent Overstreet 	 * refcount that btree_split() took.
113cafe5635SKent Overstreet 	 */
114cafe5635SKent Overstreet 	int			prio_blocked;
115cafe5635SKent Overstreet };
116cafe5635SKent Overstreet 
117cafe5635SKent Overstreet struct btree {
118cafe5635SKent Overstreet 	/* Hottest entries first */
119cafe5635SKent Overstreet 	struct hlist_node	hash;
120cafe5635SKent Overstreet 
121cafe5635SKent Overstreet 	/* Key/pointer for this btree node */
122cafe5635SKent Overstreet 	BKEY_PADDED(key);
123cafe5635SKent Overstreet 
124cafe5635SKent Overstreet 	unsigned long		seq;
125cafe5635SKent Overstreet 	struct rw_semaphore	lock;
126cafe5635SKent Overstreet 	struct cache_set	*c;
127d6fd3b11SKent Overstreet 	struct btree		*parent;
128cafe5635SKent Overstreet 
1292a285686SKent Overstreet 	struct mutex		write_lock;
1302a285686SKent Overstreet 
131cafe5635SKent Overstreet 	unsigned long		flags;
132cafe5635SKent Overstreet 	uint16_t		written;	/* would be nice to kill */
133cafe5635SKent Overstreet 	uint8_t			level;
134cafe5635SKent Overstreet 
135a85e968eSKent Overstreet 	struct btree_keys	keys;
136cafe5635SKent Overstreet 
13757943511SKent Overstreet 	/* For outstanding btree writes, used as a lock - protects write_idx */
138cb7a583eSKent Overstreet 	struct closure		io;
139cb7a583eSKent Overstreet 	struct semaphore	io_mutex;
140cafe5635SKent Overstreet 
141cafe5635SKent Overstreet 	struct list_head	list;
142cafe5635SKent Overstreet 	struct delayed_work	work;
143cafe5635SKent Overstreet 
144cafe5635SKent Overstreet 	struct btree_write	writes[2];
145cafe5635SKent Overstreet 	struct bio		*bio;
146cafe5635SKent Overstreet };
147cafe5635SKent Overstreet 
1488e710227SColy Li 
1498e710227SColy Li 
1508e710227SColy Li 
151cafe5635SKent Overstreet #define BTREE_FLAG(flag)						\
152cafe5635SKent Overstreet static inline bool btree_node_ ## flag(struct btree *b)			\
153cafe5635SKent Overstreet {	return test_bit(BTREE_NODE_ ## flag, &b->flags); }		\
154cafe5635SKent Overstreet 									\
155cafe5635SKent Overstreet static inline void set_btree_node_ ## flag(struct btree *b)		\
156cbb751c0SShenghui Wang {	set_bit(BTREE_NODE_ ## flag, &b->flags); }
157cafe5635SKent Overstreet 
158cafe5635SKent Overstreet enum btree_flags {
159cafe5635SKent Overstreet 	BTREE_NODE_io_error,
160cafe5635SKent Overstreet 	BTREE_NODE_dirty,
161cafe5635SKent Overstreet 	BTREE_NODE_write_idx,
16250a260e8SColy Li 	BTREE_NODE_journal_flush,
163cafe5635SKent Overstreet };
164cafe5635SKent Overstreet 
165cafe5635SKent Overstreet BTREE_FLAG(io_error);
166cafe5635SKent Overstreet BTREE_FLAG(dirty);
167cafe5635SKent Overstreet BTREE_FLAG(write_idx);
16850a260e8SColy Li BTREE_FLAG(journal_flush);
169cafe5635SKent Overstreet 
btree_current_write(struct btree * b)170cafe5635SKent Overstreet static inline struct btree_write *btree_current_write(struct btree *b)
171cafe5635SKent Overstreet {
172cafe5635SKent Overstreet 	return b->writes + btree_node_write_idx(b);
173cafe5635SKent Overstreet }
174cafe5635SKent Overstreet 
btree_prev_write(struct btree * b)175cafe5635SKent Overstreet static inline struct btree_write *btree_prev_write(struct btree *b)
176cafe5635SKent Overstreet {
177cafe5635SKent Overstreet 	return b->writes + (btree_node_write_idx(b) ^ 1);
178cafe5635SKent Overstreet }
179cafe5635SKent Overstreet 
btree_bset_first(struct btree * b)18088b9f8c4SKent Overstreet static inline struct bset *btree_bset_first(struct btree *b)
18188b9f8c4SKent Overstreet {
182a85e968eSKent Overstreet 	return b->keys.set->data;
18388b9f8c4SKent Overstreet }
18488b9f8c4SKent Overstreet 
btree_bset_last(struct btree * b)185ee811287SKent Overstreet static inline struct bset *btree_bset_last(struct btree *b)
186ee811287SKent Overstreet {
187a85e968eSKent Overstreet 	return bset_tree_last(&b->keys)->data;
18888b9f8c4SKent Overstreet }
18988b9f8c4SKent Overstreet 
bset_block_offset(struct btree * b,struct bset * i)1906f10f7d1SColy Li static inline unsigned int bset_block_offset(struct btree *b, struct bset *i)
19188b9f8c4SKent Overstreet {
192a85e968eSKent Overstreet 	return bset_sector_offset(&b->keys, i) >> b->c->block_bits;
193cafe5635SKent Overstreet }
194cafe5635SKent Overstreet 
set_gc_sectors(struct cache_set * c)195cafe5635SKent Overstreet static inline void set_gc_sectors(struct cache_set *c)
196cafe5635SKent Overstreet {
1974a784266SColy Li 	atomic_set(&c->sectors_to_gc, c->cache->sb.bucket_size * c->nbuckets / 16);
198cafe5635SKent Overstreet }
199cafe5635SKent Overstreet 
2003a3b6a4eSKent Overstreet void bkey_put(struct cache_set *c, struct bkey *k);
201e7c590ebSKent Overstreet 
202cafe5635SKent Overstreet /* Looping macros */
203cafe5635SKent Overstreet 
204cafe5635SKent Overstreet #define for_each_cached_btree(b, c, iter)				\
205cafe5635SKent Overstreet 	for (iter = 0;							\
206cafe5635SKent Overstreet 	     iter < ARRAY_SIZE((c)->bucket_hash);			\
207cafe5635SKent Overstreet 	     iter++)							\
208cafe5635SKent Overstreet 		hlist_for_each_entry_rcu((b), (c)->bucket_hash + iter, hash)
209cafe5635SKent Overstreet 
210cafe5635SKent Overstreet /* Recursing down the btree */
211cafe5635SKent Overstreet 
212cafe5635SKent Overstreet struct btree_op {
21378365411SKent Overstreet 	/* for waiting on btree reserve in btree_split() */
214ac6424b9SIngo Molnar 	wait_queue_entry_t		wait;
21578365411SKent Overstreet 
216cafe5635SKent Overstreet 	/* Btree level at which we start taking write locks */
217cafe5635SKent Overstreet 	short			lock;
218cafe5635SKent Overstreet 
2196f10f7d1SColy Li 	unsigned int		insert_collision:1;
220cafe5635SKent Overstreet };
221cafe5635SKent Overstreet 
2228e710227SColy Li struct btree_check_state;
2238e710227SColy Li struct btree_check_info {
2248e710227SColy Li 	struct btree_check_state	*state;
2258e710227SColy Li 	struct task_struct		*thread;
2268e710227SColy Li 	int				result;
2278e710227SColy Li };
2288e710227SColy Li 
22962253644SColy Li #define BCH_BTR_CHKTHREAD_MAX	12
2308e710227SColy Li struct btree_check_state {
2318e710227SColy Li 	struct cache_set		*c;
2328e710227SColy Li 	int				total_threads;
2338e710227SColy Li 	int				key_idx;
2348e710227SColy Li 	spinlock_t			idx_lock;
2358e710227SColy Li 	atomic_t			started;
2368e710227SColy Li 	atomic_t			enough;
2378e710227SColy Li 	wait_queue_head_t		wait;
2388e710227SColy Li 	struct btree_check_info		infos[BCH_BTR_CHKTHREAD_MAX];
2398e710227SColy Li };
2408e710227SColy Li 
bch_btree_op_init(struct btree_op * op,int write_lock_level)241b54d6934SKent Overstreet static inline void bch_btree_op_init(struct btree_op *op, int write_lock_level)
242b54d6934SKent Overstreet {
243b54d6934SKent Overstreet 	memset(op, 0, sizeof(struct btree_op));
24478365411SKent Overstreet 	init_wait(&op->wait);
245b54d6934SKent Overstreet 	op->lock = write_lock_level;
246b54d6934SKent Overstreet }
247cafe5635SKent Overstreet 
rw_lock(bool w,struct btree * b,int level)248cafe5635SKent Overstreet static inline void rw_lock(bool w, struct btree *b, int level)
249cafe5635SKent Overstreet {
2504c8a4924SKent Overstreet 	w ? down_write(&b->lock)
2514c8a4924SKent Overstreet 	  : down_read(&b->lock);
252cafe5635SKent Overstreet 	if (w)
253cafe5635SKent Overstreet 		b->seq++;
254cafe5635SKent Overstreet }
255cafe5635SKent Overstreet 
rw_unlock(bool w,struct btree * b)256cafe5635SKent Overstreet static inline void rw_unlock(bool w, struct btree *b)
257cafe5635SKent Overstreet {
258cafe5635SKent Overstreet 	if (w)
259cafe5635SKent Overstreet 		b->seq++;
260cafe5635SKent Overstreet 	(w ? up_write : up_read)(&b->lock);
261cafe5635SKent Overstreet }
262cafe5635SKent Overstreet 
263fc2d5988SColy Li void bch_btree_node_read_done(struct btree *b);
264fc2d5988SColy Li void __bch_btree_node_write(struct btree *b, struct closure *parent);
265fc2d5988SColy Li void bch_btree_node_write(struct btree *b, struct closure *parent);
266cafe5635SKent Overstreet 
267fc2d5988SColy Li void bch_btree_set_root(struct btree *b);
268fc2d5988SColy Li struct btree *__bch_btree_node_alloc(struct cache_set *c, struct btree_op *op,
269fc2d5988SColy Li 				     int level, bool wait,
270fc2d5988SColy Li 				     struct btree *parent);
271fc2d5988SColy Li struct btree *bch_btree_node_get(struct cache_set *c, struct btree_op *op,
272fc2d5988SColy Li 				 struct bkey *k, int level, bool write,
273fc2d5988SColy Li 				 struct btree *parent);
274cafe5635SKent Overstreet 
275fc2d5988SColy Li int bch_btree_insert_check_key(struct btree *b, struct btree_op *op,
276fc2d5988SColy Li 			       struct bkey *check_key);
277fc2d5988SColy Li int bch_btree_insert(struct cache_set *c, struct keylist *keys,
278fc2d5988SColy Li 		     atomic_t *journal_ref, struct bkey *replace_key);
279cafe5635SKent Overstreet 
280fc2d5988SColy Li int bch_gc_thread_start(struct cache_set *c);
281fc2d5988SColy Li void bch_initial_gc_finish(struct cache_set *c);
282fc2d5988SColy Li void bch_moving_gc(struct cache_set *c);
283fc2d5988SColy Li int bch_btree_check(struct cache_set *c);
284fc2d5988SColy Li void bch_initial_mark_key(struct cache_set *c, int level, struct bkey *k);
285*f0854489SMingzhe Zou void bch_cannibalize_unlock(struct cache_set *c);
286cafe5635SKent Overstreet 
wake_up_gc(struct cache_set * c)28772a44517SKent Overstreet static inline void wake_up_gc(struct cache_set *c)
28872a44517SKent Overstreet {
289be628be0SKent Overstreet 	wake_up(&c->gc_wait);
29072a44517SKent Overstreet }
29172a44517SKent Overstreet 
force_wake_up_gc(struct cache_set * c)292cb07ad63SColy Li static inline void force_wake_up_gc(struct cache_set *c)
293cb07ad63SColy Li {
294cb07ad63SColy Li 	/*
295cb07ad63SColy Li 	 * Garbage collection thread only works when sectors_to_gc < 0,
296cb07ad63SColy Li 	 * calling wake_up_gc() won't start gc thread if sectors_to_gc is
297cb07ad63SColy Li 	 * not a nagetive value.
298cb07ad63SColy Li 	 * Therefore sectors_to_gc is set to -1 here, before waking up
299cb07ad63SColy Li 	 * gc thread by calling wake_up_gc(). Then gc_should_run() will
300cb07ad63SColy Li 	 * give a chance to permit gc thread to run. "Give a chance" means
301cb07ad63SColy Li 	 * before going into gc_should_run(), there is still possibility
302cb07ad63SColy Li 	 * that c->sectors_to_gc being set to other positive value. So
303cb07ad63SColy Li 	 * this routine won't 100% make sure gc thread will be woken up
304cb07ad63SColy Li 	 * to run.
305cb07ad63SColy Li 	 */
306cb07ad63SColy Li 	atomic_set(&c->sectors_to_gc, -1);
307cb07ad63SColy Li 	wake_up_gc(c);
308cb07ad63SColy Li }
309cb07ad63SColy Li 
310253a99d9SColy Li /*
311253a99d9SColy Li  * These macros are for recursing down the btree - they handle the details of
312253a99d9SColy Li  * locking and looking up nodes in the cache for you. They're best treated as
313253a99d9SColy Li  * mere syntax when reading code that uses them.
314253a99d9SColy Li  *
315253a99d9SColy Li  * op->lock determines whether we take a read or a write lock at a given depth.
316253a99d9SColy Li  * If you've got a read lock and find that you need a write lock (i.e. you're
317253a99d9SColy Li  * going to have to split), set op->lock and return -EINTR; btree_root() will
318253a99d9SColy Li  * call you again and you'll have the correct lock.
319253a99d9SColy Li  */
320253a99d9SColy Li 
321253a99d9SColy Li /**
322253a99d9SColy Li  * btree - recurse down the btree on a specified key
323253a99d9SColy Li  * @fn:		function to call, which will be passed the child node
324253a99d9SColy Li  * @key:	key to recurse on
325253a99d9SColy Li  * @b:		parent btree node
326253a99d9SColy Li  * @op:		pointer to struct btree_op
327253a99d9SColy Li  */
328feac1a70SColy Li #define bcache_btree(fn, key, b, op, ...)				\
329253a99d9SColy Li ({									\
330253a99d9SColy Li 	int _r, l = (b)->level - 1;					\
331253a99d9SColy Li 	bool _w = l <= (op)->lock;					\
332253a99d9SColy Li 	struct btree *_child = bch_btree_node_get((b)->c, op, key, l,	\
333253a99d9SColy Li 						  _w, b);		\
334253a99d9SColy Li 	if (!IS_ERR(_child)) {						\
335253a99d9SColy Li 		_r = bch_btree_ ## fn(_child, op, ##__VA_ARGS__);	\
336253a99d9SColy Li 		rw_unlock(_w, _child);					\
337253a99d9SColy Li 	} else								\
338253a99d9SColy Li 		_r = PTR_ERR(_child);					\
339253a99d9SColy Li 	_r;								\
340253a99d9SColy Li })
341253a99d9SColy Li 
342253a99d9SColy Li /**
343253a99d9SColy Li  * btree_root - call a function on the root of the btree
344253a99d9SColy Li  * @fn:		function to call, which will be passed the child node
345253a99d9SColy Li  * @c:		cache set
346253a99d9SColy Li  * @op:		pointer to struct btree_op
347253a99d9SColy Li  */
348feac1a70SColy Li #define bcache_btree_root(fn, c, op, ...)				\
349253a99d9SColy Li ({									\
350253a99d9SColy Li 	int _r = -EINTR;						\
351253a99d9SColy Li 	do {								\
352253a99d9SColy Li 		struct btree *_b = (c)->root;				\
353253a99d9SColy Li 		bool _w = insert_lock(op, _b);				\
354253a99d9SColy Li 		rw_lock(_w, _b, _b->level);				\
355253a99d9SColy Li 		if (_b == (c)->root &&					\
356253a99d9SColy Li 		    _w == insert_lock(op, _b)) {			\
357253a99d9SColy Li 			_r = bch_btree_ ## fn(_b, op, ##__VA_ARGS__);	\
358253a99d9SColy Li 		}							\
359253a99d9SColy Li 		rw_unlock(_w, _b);					\
360253a99d9SColy Li 		bch_cannibalize_unlock(c);                              \
361253a99d9SColy Li 		if (_r == -EINTR)                                       \
362253a99d9SColy Li 			schedule();                                     \
363253a99d9SColy Li 	} while (_r == -EINTR);                                         \
364253a99d9SColy Li 									\
365253a99d9SColy Li 	finish_wait(&(c)->btree_cache_wait, &(op)->wait);               \
366253a99d9SColy Li 	_r;                                                             \
367253a99d9SColy Li })
368253a99d9SColy Li 
36948dad8baSKent Overstreet #define MAP_DONE	0
37048dad8baSKent Overstreet #define MAP_CONTINUE	1
37148dad8baSKent Overstreet 
37248dad8baSKent Overstreet #define MAP_ALL_NODES	0
37348dad8baSKent Overstreet #define MAP_LEAF_NODES	1
37448dad8baSKent Overstreet 
37548dad8baSKent Overstreet #define MAP_END_KEY	1
37648dad8baSKent Overstreet 
377fc2d5988SColy Li typedef int (btree_map_nodes_fn)(struct btree_op *b_op, struct btree *b);
378fc2d5988SColy Li int __bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
379fc2d5988SColy Li 			  struct bkey *from, btree_map_nodes_fn *fn, int flags);
38048dad8baSKent Overstreet 
bch_btree_map_nodes(struct btree_op * op,struct cache_set * c,struct bkey * from,btree_map_nodes_fn * fn)38148dad8baSKent Overstreet static inline int bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
38248dad8baSKent Overstreet 				      struct bkey *from, btree_map_nodes_fn *fn)
38348dad8baSKent Overstreet {
38448dad8baSKent Overstreet 	return __bch_btree_map_nodes(op, c, from, fn, MAP_ALL_NODES);
38548dad8baSKent Overstreet }
38648dad8baSKent Overstreet 
bch_btree_map_leaf_nodes(struct btree_op * op,struct cache_set * c,struct bkey * from,btree_map_nodes_fn * fn)38748dad8baSKent Overstreet static inline int bch_btree_map_leaf_nodes(struct btree_op *op,
38848dad8baSKent Overstreet 					   struct cache_set *c,
38948dad8baSKent Overstreet 					   struct bkey *from,
39048dad8baSKent Overstreet 					   btree_map_nodes_fn *fn)
39148dad8baSKent Overstreet {
39248dad8baSKent Overstreet 	return __bch_btree_map_nodes(op, c, from, fn, MAP_LEAF_NODES);
39348dad8baSKent Overstreet }
39448dad8baSKent Overstreet 
395fc2d5988SColy Li typedef int (btree_map_keys_fn)(struct btree_op *op, struct btree *b,
396fc2d5988SColy Li 				struct bkey *k);
397fc2d5988SColy Li int bch_btree_map_keys(struct btree_op *op, struct cache_set *c,
398fc2d5988SColy Li 		       struct bkey *from, btree_map_keys_fn *fn, int flags);
3995ae3a2c0SColy Li int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op,
4005ae3a2c0SColy Li 			       struct bkey *from, btree_map_keys_fn *fn,
4015ae3a2c0SColy Li 			       int flags);
40248dad8baSKent Overstreet 
403fc2d5988SColy Li typedef bool (keybuf_pred_fn)(struct keybuf *buf, struct bkey *k);
40448dad8baSKent Overstreet 
405fc2d5988SColy Li void bch_keybuf_init(struct keybuf *buf);
406fc2d5988SColy Li void bch_refill_keybuf(struct cache_set *c, struct keybuf *buf,
407fc2d5988SColy Li 		       struct bkey *end, keybuf_pred_fn *pred);
408fc2d5988SColy Li bool bch_keybuf_check_overlapping(struct keybuf *buf, struct bkey *start,
409fc2d5988SColy Li 				  struct bkey *end);
410fc2d5988SColy Li void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w);
411fc2d5988SColy Li struct keybuf_key *bch_keybuf_next(struct keybuf *buf);
412b0d30981SColy Li struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *c,
413b0d30981SColy Li 					  struct keybuf *buf,
414b0d30981SColy Li 					  struct bkey *end,
415b0d30981SColy Li 					  keybuf_pred_fn *pred);
416d44c2f9eSTang Junhui void bch_update_bucket_in_use(struct cache_set *c, struct gc_stat *stats);
417cafe5635SKent Overstreet #endif
418