xref: /linux/fs/bcachefs/btree_gc.h (revision 2697b79a469b68e3ad3640f55284359c1396278d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_GC_H
3 #define _BCACHEFS_BTREE_GC_H
4 
5 #include "bkey.h"
6 #include "btree_gc_types.h"
7 #include "btree_types.h"
8 
9 int bch2_check_topology(struct bch_fs *);
10 int bch2_check_allocations(struct bch_fs *);
11 
12 /*
13  * For concurrent mark and sweep (with other index updates), we define a total
14  * ordering of _all_ references GC walks:
15  *
16  * Note that some references will have the same GC position as others - e.g.
17  * everything within the same btree node; in those cases we're relying on
18  * whatever locking exists for where those references live, i.e. the write lock
19  * on a btree node.
20  *
21  * That locking is also required to ensure GC doesn't pass the updater in
22  * between the updater adding/removing the reference and updating the GC marks;
23  * without that, we would at best double count sometimes.
24  *
25  * That part is important - whenever calling bch2_mark_pointers(), a lock _must_
26  * be held that prevents GC from passing the position the updater is at.
27  *
28  * (What about the start of gc, when we're clearing all the marks? GC clears the
29  * mark with the gc pos seqlock held, and bch_mark_bucket checks against the gc
30  * position inside its cmpxchg loop, so crap magically works).
31  */
32 
33 /* Position of (the start of) a gc phase: */
34 static inline struct gc_pos gc_phase(enum gc_phase phase)
35 {
36 	return (struct gc_pos) { .phase	= phase, };
37 }
38 
39 static inline struct gc_pos gc_pos_btree(enum btree_id btree, unsigned level,
40 					 struct bpos pos)
41 {
42 	return (struct gc_pos) {
43 		.phase	= GC_PHASE_btree,
44 		.btree	= btree,
45 		.level	= level,
46 		.pos	= pos,
47 	};
48 }
49 
50 /*
51  * GC position of the pointers within a btree node: note, _not_ for &b->key
52  * itself, that lives in the parent node:
53  */
54 static inline struct gc_pos gc_pos_btree_node(struct btree *b)
55 {
56 	return gc_pos_btree(b->c.btree_id, b->c.level, b->key.k.p);
57 }
58 
59 static inline int gc_btree_order(enum btree_id btree)
60 {
61 	if (btree == BTREE_ID_stripes)
62 		return -1;
63 	return btree;
64 }
65 
66 static inline int gc_pos_cmp(struct gc_pos l, struct gc_pos r)
67 {
68 	return   cmp_int(l.phase, r.phase) ?:
69 		 cmp_int(gc_btree_order(l.btree),
70 			 gc_btree_order(r.btree)) ?:
71 		-cmp_int(l.level, r.level) ?:
72 		 bpos_cmp(l.pos, r.pos);
73 }
74 
75 static inline bool gc_visited(struct bch_fs *c, struct gc_pos pos)
76 {
77 	unsigned seq;
78 	bool ret;
79 
80 	do {
81 		seq = read_seqcount_begin(&c->gc_pos_lock);
82 		ret = gc_pos_cmp(pos, c->gc_pos) <= 0;
83 	} while (read_seqcount_retry(&c->gc_pos_lock, seq));
84 
85 	return ret;
86 }
87 
88 int bch2_gc_gens(struct bch_fs *);
89 void bch2_gc_gens_async(struct bch_fs *);
90 void bch2_fs_gc_init(struct bch_fs *);
91 
92 #endif /* _BCACHEFS_BTREE_GC_H */
93