1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_BTREE_CACHE_H 3 #define _BCACHEFS_BTREE_CACHE_H 4 5 #include "bcachefs.h" 6 #include "btree_types.h" 7 #include "bkey_methods.h" 8 9 extern const char * const bch2_btree_node_flags[]; 10 11 struct btree_iter; 12 13 void bch2_recalc_btree_reserve(struct bch_fs *); 14 15 void bch2_btree_node_hash_remove(struct btree_cache *, struct btree *); 16 int __bch2_btree_node_hash_insert(struct btree_cache *, struct btree *); 17 int bch2_btree_node_hash_insert(struct btree_cache *, struct btree *, 18 unsigned, enum btree_id); 19 20 void bch2_btree_node_update_key_early(struct btree_trans *, enum btree_id, unsigned, 21 struct bkey_s_c, struct bkey_i *); 22 23 void bch2_btree_cache_cannibalize_unlock(struct btree_trans *); 24 int bch2_btree_cache_cannibalize_lock(struct btree_trans *, struct closure *); 25 26 struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *); 27 struct btree *bch2_btree_node_mem_alloc(struct btree_trans *, bool); 28 29 struct btree *bch2_btree_node_get(struct btree_trans *, struct btree_path *, 30 const struct bkey_i *, unsigned, 31 enum six_lock_type, unsigned long); 32 33 struct btree *bch2_btree_node_get_noiter(struct btree_trans *, const struct bkey_i *, 34 enum btree_id, unsigned, bool); 35 36 int bch2_btree_node_prefetch(struct btree_trans *, struct btree_path *, 37 const struct bkey_i *, enum btree_id, unsigned); 38 39 void bch2_btree_node_evict(struct btree_trans *, const struct bkey_i *); 40 41 void bch2_fs_btree_cache_exit(struct bch_fs *); 42 int bch2_fs_btree_cache_init(struct bch_fs *); 43 void bch2_fs_btree_cache_init_early(struct btree_cache *); 44 45 static inline u64 btree_ptr_hash_val(const struct bkey_i *k) 46 { 47 switch (k->k.type) { 48 case KEY_TYPE_btree_ptr: 49 return *((u64 *) bkey_i_to_btree_ptr_c(k)->v.start); 50 case KEY_TYPE_btree_ptr_v2: 51 /* 52 * The cast/deref is only necessary to avoid sparse endianness 53 * warnings: 54 */ 55 return *((u64 *) &bkey_i_to_btree_ptr_v2_c(k)->v.seq); 56 default: 57 return 0; 58 } 59 } 60 61 static inline struct btree *btree_node_mem_ptr(const struct bkey_i *k) 62 { 63 return k->k.type == KEY_TYPE_btree_ptr_v2 64 ? (void *)(unsigned long)bkey_i_to_btree_ptr_v2_c(k)->v.mem_ptr 65 : NULL; 66 } 67 68 /* is btree node in hash table? */ 69 static inline bool btree_node_hashed(struct btree *b) 70 { 71 return b->hash_val != 0; 72 } 73 74 #define for_each_cached_btree(_b, _c, _tbl, _iter, _pos) \ 75 for ((_tbl) = rht_dereference_rcu((_c)->btree_cache.table.tbl, \ 76 &(_c)->btree_cache.table), \ 77 _iter = 0; _iter < (_tbl)->size; _iter++) \ 78 rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash) 79 80 static inline size_t btree_buf_bytes(const struct btree *b) 81 { 82 return 1UL << b->byte_order; 83 } 84 85 static inline size_t btree_buf_max_u64s(const struct btree *b) 86 { 87 return (btree_buf_bytes(b) - sizeof(struct btree_node)) / sizeof(u64); 88 } 89 90 static inline size_t btree_max_u64s(const struct bch_fs *c) 91 { 92 return (c->opts.btree_node_size - sizeof(struct btree_node)) / sizeof(u64); 93 } 94 95 static inline size_t btree_sectors(const struct bch_fs *c) 96 { 97 return c->opts.btree_node_size >> SECTOR_SHIFT; 98 } 99 100 static inline unsigned btree_blocks(const struct bch_fs *c) 101 { 102 return btree_sectors(c) >> c->block_bits; 103 } 104 105 #define BTREE_SPLIT_THRESHOLD(c) (btree_max_u64s(c) * 2 / 3) 106 107 #define BTREE_FOREGROUND_MERGE_THRESHOLD(c) (btree_max_u64s(c) * 1 / 3) 108 #define BTREE_FOREGROUND_MERGE_HYSTERESIS(c) \ 109 (BTREE_FOREGROUND_MERGE_THRESHOLD(c) + \ 110 (BTREE_FOREGROUND_MERGE_THRESHOLD(c) >> 2)) 111 112 static inline unsigned btree_id_nr_alive(struct bch_fs *c) 113 { 114 return BTREE_ID_NR + c->btree_roots_extra.nr; 115 } 116 117 static inline struct btree_root *bch2_btree_id_root(struct bch_fs *c, unsigned id) 118 { 119 if (likely(id < BTREE_ID_NR)) { 120 return &c->btree_roots_known[id]; 121 } else { 122 unsigned idx = id - BTREE_ID_NR; 123 124 EBUG_ON(idx >= c->btree_roots_extra.nr); 125 return &c->btree_roots_extra.data[idx]; 126 } 127 } 128 129 static inline struct btree *btree_node_root(struct bch_fs *c, struct btree *b) 130 { 131 return bch2_btree_id_root(c, b->c.btree_id)->b; 132 } 133 134 const char *bch2_btree_id_str(enum btree_id); 135 void bch2_btree_id_to_text(struct printbuf *, enum btree_id); 136 137 void bch2_btree_pos_to_text(struct printbuf *, struct bch_fs *, const struct btree *); 138 void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *, const struct btree *); 139 void bch2_btree_cache_to_text(struct printbuf *, const struct btree_cache *); 140 141 #endif /* _BCACHEFS_BTREE_CACHE_H */ 142