/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _BCACHEFS_BTREE_GC_H2#define _BCACHEFS_BTREE_GC_H34#include "bkey.h"5#include "btree_gc_types.h"6#include "btree_types.h"78int bch2_check_topology(struct bch_fs *);9int bch2_check_allocations(struct bch_fs *);1011/*12* For concurrent mark and sweep (with other index updates), we define a total13* ordering of _all_ references GC walks:14*15* Note that some references will have the same GC position as others - e.g.16* everything within the same btree node; in those cases we're relying on17* whatever locking exists for where those references live, i.e. the write lock18* on a btree node.19*20* That locking is also required to ensure GC doesn't pass the updater in21* between the updater adding/removing the reference and updating the GC marks;22* without that, we would at best double count sometimes.23*24* That part is important - whenever calling bch2_mark_pointers(), a lock _must_25* be held that prevents GC from passing the position the updater is at.26*27* (What about the start of gc, when we're clearing all the marks? GC clears the28* mark with the gc pos seqlock held, and bch_mark_bucket checks against the gc29* position inside its cmpxchg loop, so crap magically works).30*/3132/* Position of (the start of) a gc phase: */33static inline struct gc_pos gc_phase(enum gc_phase phase)34{35return (struct gc_pos) { .phase = phase, };36}3738static inline struct gc_pos gc_pos_btree(enum btree_id btree, unsigned level,39struct bpos pos)40{41return (struct gc_pos) {42.phase = GC_PHASE_btree,43.btree = btree,44.level = level,45.pos = pos,46};47}4849static inline int gc_btree_order(enum btree_id btree)50{51if (btree == BTREE_ID_alloc)52return -2;53if (btree == BTREE_ID_stripes)54return -1;55return btree;56}5758static inline int gc_pos_cmp(struct gc_pos l, struct gc_pos r)59{60return cmp_int(l.phase, r.phase) ?:61cmp_int(gc_btree_order(l.btree),62gc_btree_order(r.btree)) ?:63cmp_int(l.level, r.level) ?:64bpos_cmp(l.pos, r.pos);65}6667static inline bool gc_visited(struct bch_fs *c, struct gc_pos pos)68{69unsigned seq;70bool ret;7172do {73seq = read_seqcount_begin(&c->gc_pos_lock);74ret = gc_pos_cmp(pos, c->gc_pos) <= 0;75} while (read_seqcount_retry(&c->gc_pos_lock, seq));7677return ret;78}7980void bch2_gc_pos_to_text(struct printbuf *, struct gc_pos *);8182int bch2_gc_gens(struct bch_fs *);83void bch2_gc_gens_async(struct bch_fs *);8485void bch2_fs_btree_gc_init_early(struct bch_fs *);8687#endif /* _BCACHEFS_BTREE_GC_H */888990