1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_BTREE_UPDATE_INTERIOR_H 3 #define _BCACHEFS_BTREE_UPDATE_INTERIOR_H 4 5 #include "btree_cache.h" 6 #include "btree_locking.h" 7 #include "btree_update.h" 8 9 #define BTREE_UPDATE_NODES_MAX ((BTREE_MAX_DEPTH - 2) * 2 + GC_MERGE_NODES) 10 11 #define BTREE_UPDATE_JOURNAL_RES (BTREE_UPDATE_NODES_MAX * (BKEY_BTREE_PTR_U64s_MAX + 1)) 12 13 /* 14 * Tracks an in progress split/rewrite of a btree node and the update to the 15 * parent node: 16 * 17 * When we split/rewrite a node, we do all the updates in memory without 18 * waiting for any writes to complete - we allocate the new node(s) and update 19 * the parent node, possibly recursively up to the root. 20 * 21 * The end result is that we have one or more new nodes being written - 22 * possibly several, if there were multiple splits - and then a write (updating 23 * an interior node) which will make all these new nodes visible. 24 * 25 * Additionally, as we split/rewrite nodes we free the old nodes - but the old 26 * nodes can't be freed (their space on disk can't be reclaimed) until the 27 * update to the interior node that makes the new node visible completes - 28 * until then, the old nodes are still reachable on disk. 29 * 30 */ 31 struct btree_update { 32 struct closure cl; 33 struct bch_fs *c; 34 u64 start_time; 35 36 struct list_head list; 37 struct list_head unwritten_list; 38 39 /* What kind of update are we doing? */ 40 enum { 41 BTREE_INTERIOR_NO_UPDATE, 42 BTREE_INTERIOR_UPDATING_NODE, 43 BTREE_INTERIOR_UPDATING_ROOT, 44 BTREE_INTERIOR_UPDATING_AS, 45 } mode; 46 47 unsigned nodes_written:1; 48 unsigned took_gc_lock:1; 49 50 enum btree_id btree_id; 51 unsigned update_level; 52 53 struct disk_reservation disk_res; 54 55 /* 56 * BTREE_INTERIOR_UPDATING_NODE: 57 * The update that made the new nodes visible was a regular update to an 58 * existing interior node - @b. We can't write out the update to @b 59 * until the new nodes we created are finished writing, so we block @b 60 * from writing by putting this btree_interior update on the 61 * @b->write_blocked list with @write_blocked_list: 62 */ 63 struct btree *b; 64 struct list_head write_blocked_list; 65 66 /* 67 * We may be freeing nodes that were dirty, and thus had journal entries 68 * pinned: we need to transfer the oldest of those pins to the 69 * btree_update operation, and release it when the new node(s) 70 * are all persistent and reachable: 71 */ 72 struct journal_entry_pin journal; 73 74 /* Preallocated nodes we reserve when we start the update: */ 75 struct prealloc_nodes { 76 struct btree *b[BTREE_UPDATE_NODES_MAX]; 77 unsigned nr; 78 } prealloc_nodes[2]; 79 80 /* Nodes being freed: */ 81 struct keylist old_keys; 82 u64 _old_keys[BTREE_UPDATE_NODES_MAX * 83 BKEY_BTREE_PTR_U64s_MAX]; 84 85 /* Nodes being added: */ 86 struct keylist new_keys; 87 u64 _new_keys[BTREE_UPDATE_NODES_MAX * 88 BKEY_BTREE_PTR_U64s_MAX]; 89 90 /* New nodes, that will be made reachable by this update: */ 91 struct btree *new_nodes[BTREE_UPDATE_NODES_MAX]; 92 unsigned nr_new_nodes; 93 94 struct btree *old_nodes[BTREE_UPDATE_NODES_MAX]; 95 __le64 old_nodes_seq[BTREE_UPDATE_NODES_MAX]; 96 unsigned nr_old_nodes; 97 98 open_bucket_idx_t open_buckets[BTREE_UPDATE_NODES_MAX * 99 BCH_REPLICAS_MAX]; 100 open_bucket_idx_t nr_open_buckets; 101 102 unsigned journal_u64s; 103 u64 journal_entries[BTREE_UPDATE_JOURNAL_RES]; 104 105 /* Only here to reduce stack usage on recursive splits: */ 106 struct keylist parent_keys; 107 /* 108 * Enough room for btree_split's keys without realloc - btree node 109 * pointers never have crc/compression info, so we only need to acount 110 * for the pointers for three keys 111 */ 112 u64 inline_keys[BKEY_BTREE_PTR_U64s_MAX * 3]; 113 }; 114 115 struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *, 116 struct btree_trans *, 117 struct btree *, 118 struct bkey_format); 119 120 int bch2_btree_split_leaf(struct btree_trans *, struct btree_path *, unsigned); 121 122 int __bch2_foreground_maybe_merge(struct btree_trans *, struct btree_path *, 123 unsigned, unsigned, enum btree_node_sibling); 124 125 static inline int bch2_foreground_maybe_merge_sibling(struct btree_trans *trans, 126 struct btree_path *path, 127 unsigned level, unsigned flags, 128 enum btree_node_sibling sib) 129 { 130 struct btree *b; 131 132 EBUG_ON(!btree_node_locked(path, level)); 133 134 b = path->l[level].b; 135 if (b->sib_u64s[sib] > trans->c->btree_foreground_merge_threshold) 136 return 0; 137 138 return __bch2_foreground_maybe_merge(trans, path, level, flags, sib); 139 } 140 141 static inline int bch2_foreground_maybe_merge(struct btree_trans *trans, 142 struct btree_path *path, 143 unsigned level, 144 unsigned flags) 145 { 146 return bch2_foreground_maybe_merge_sibling(trans, path, level, flags, 147 btree_prev_sib) ?: 148 bch2_foreground_maybe_merge_sibling(trans, path, level, flags, 149 btree_next_sib); 150 } 151 152 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *, 153 struct btree *, unsigned); 154 void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *); 155 int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *, 156 struct btree *, struct bkey_i *, 157 unsigned, bool); 158 int bch2_btree_node_update_key_get_iter(struct btree_trans *, struct btree *, 159 struct bkey_i *, unsigned, bool); 160 161 void bch2_btree_set_root_for_read(struct bch_fs *, struct btree *); 162 void bch2_btree_root_alloc(struct bch_fs *, enum btree_id); 163 164 static inline unsigned btree_update_reserve_required(struct bch_fs *c, 165 struct btree *b) 166 { 167 unsigned depth = btree_node_root(c, b)->c.level + 1; 168 169 /* 170 * Number of nodes we might have to allocate in a worst case btree 171 * split operation - we split all the way up to the root, then allocate 172 * a new root, unless we're already at max depth: 173 */ 174 if (depth < BTREE_MAX_DEPTH) 175 return (depth - b->c.level) * 2 + 1; 176 else 177 return (depth - b->c.level) * 2 - 1; 178 } 179 180 static inline void btree_node_reset_sib_u64s(struct btree *b) 181 { 182 b->sib_u64s[0] = b->nr.live_u64s; 183 b->sib_u64s[1] = b->nr.live_u64s; 184 } 185 186 static inline void *btree_data_end(struct bch_fs *c, struct btree *b) 187 { 188 return (void *) b->data + btree_bytes(c); 189 } 190 191 static inline struct bkey_packed *unwritten_whiteouts_start(struct bch_fs *c, 192 struct btree *b) 193 { 194 return (void *) ((u64 *) btree_data_end(c, b) - b->whiteout_u64s); 195 } 196 197 static inline struct bkey_packed *unwritten_whiteouts_end(struct bch_fs *c, 198 struct btree *b) 199 { 200 return btree_data_end(c, b); 201 } 202 203 static inline void *write_block(struct btree *b) 204 { 205 return (void *) b->data + (b->written << 9); 206 } 207 208 static inline bool __btree_addr_written(struct btree *b, void *p) 209 { 210 return p < write_block(b); 211 } 212 213 static inline bool bset_written(struct btree *b, struct bset *i) 214 { 215 return __btree_addr_written(b, i); 216 } 217 218 static inline bool bkey_written(struct btree *b, struct bkey_packed *k) 219 { 220 return __btree_addr_written(b, k); 221 } 222 223 static inline ssize_t __bch_btree_u64s_remaining(struct bch_fs *c, 224 struct btree *b, 225 void *end) 226 { 227 ssize_t used = bset_byte_offset(b, end) / sizeof(u64) + 228 b->whiteout_u64s; 229 ssize_t total = c->opts.btree_node_size >> 3; 230 231 /* Always leave one extra u64 for bch2_varint_decode: */ 232 used++; 233 234 return total - used; 235 } 236 237 static inline size_t bch_btree_keys_u64s_remaining(struct bch_fs *c, 238 struct btree *b) 239 { 240 ssize_t remaining = __bch_btree_u64s_remaining(c, b, 241 btree_bkey_last(b, bset_tree_last(b))); 242 243 BUG_ON(remaining < 0); 244 245 if (bset_written(b, btree_bset_last(b))) 246 return 0; 247 248 return remaining; 249 } 250 251 #define BTREE_WRITE_SET_U64s_BITS 9 252 253 static inline unsigned btree_write_set_buffer(struct btree *b) 254 { 255 /* 256 * Could buffer up larger amounts of keys for btrees with larger keys, 257 * pending benchmarking: 258 */ 259 return 8 << BTREE_WRITE_SET_U64s_BITS; 260 } 261 262 static inline struct btree_node_entry *want_new_bset(struct bch_fs *c, 263 struct btree *b) 264 { 265 struct bset_tree *t = bset_tree_last(b); 266 struct btree_node_entry *bne = max(write_block(b), 267 (void *) btree_bkey_last(b, bset_tree_last(b))); 268 ssize_t remaining_space = 269 __bch_btree_u64s_remaining(c, b, bne->keys.start); 270 271 if (unlikely(bset_written(b, bset(b, t)))) { 272 if (remaining_space > (ssize_t) (block_bytes(c) >> 3)) 273 return bne; 274 } else { 275 if (unlikely(bset_u64s(t) * sizeof(u64) > btree_write_set_buffer(b)) && 276 remaining_space > (ssize_t) (btree_write_set_buffer(b) >> 3)) 277 return bne; 278 } 279 280 return NULL; 281 } 282 283 static inline void push_whiteout(struct bch_fs *c, struct btree *b, 284 struct bpos pos) 285 { 286 struct bkey_packed k; 287 288 BUG_ON(bch_btree_keys_u64s_remaining(c, b) < BKEY_U64s); 289 EBUG_ON(btree_node_just_written(b)); 290 291 if (!bkey_pack_pos(&k, pos, b)) { 292 struct bkey *u = (void *) &k; 293 294 bkey_init(u); 295 u->p = pos; 296 } 297 298 k.needs_whiteout = true; 299 300 b->whiteout_u64s += k.u64s; 301 bkey_p_copy(unwritten_whiteouts_start(c, b), &k); 302 } 303 304 /* 305 * write lock must be held on @b (else the dirty bset that we were going to 306 * insert into could be written out from under us) 307 */ 308 static inline bool bch2_btree_node_insert_fits(struct bch_fs *c, 309 struct btree *b, unsigned u64s) 310 { 311 if (unlikely(btree_node_need_rewrite(b))) 312 return false; 313 314 return u64s <= bch_btree_keys_u64s_remaining(c, b); 315 } 316 317 void bch2_btree_updates_to_text(struct printbuf *, struct bch_fs *); 318 319 bool bch2_btree_interior_updates_flush(struct bch_fs *); 320 321 void bch2_journal_entry_to_btree_root(struct bch_fs *, struct jset_entry *); 322 struct jset_entry *bch2_btree_roots_to_journal_entries(struct bch_fs *, 323 struct jset_entry *, unsigned long); 324 325 void bch2_do_pending_node_rewrites(struct bch_fs *); 326 void bch2_free_pending_node_rewrites(struct bch_fs *); 327 328 void bch2_fs_btree_interior_update_exit(struct bch_fs *); 329 void bch2_fs_btree_interior_update_init_early(struct bch_fs *); 330 int bch2_fs_btree_interior_update_init(struct bch_fs *); 331 332 #endif /* _BCACHEFS_BTREE_UPDATE_INTERIOR_H */ 333