11c6fdbd8SKent Overstreet /* SPDX-License-Identifier: GPL-2.0 */ 21c6fdbd8SKent Overstreet #ifndef _BCACHEFS_BTREE_UPDATE_INTERIOR_H 31c6fdbd8SKent Overstreet #define _BCACHEFS_BTREE_UPDATE_INTERIOR_H 41c6fdbd8SKent Overstreet 51c6fdbd8SKent Overstreet #include "btree_cache.h" 61c6fdbd8SKent Overstreet #include "btree_locking.h" 71c6fdbd8SKent Overstreet #include "btree_update.h" 81c6fdbd8SKent Overstreet 900b8ccf7SKent Overstreet #define BTREE_UPDATE_NODES_MAX ((BTREE_MAX_DEPTH - 2) * 2 + GC_MERGE_NODES) 101c6fdbd8SKent Overstreet 1100b8ccf7SKent Overstreet #define BTREE_UPDATE_JOURNAL_RES (BTREE_UPDATE_NODES_MAX * (BKEY_BTREE_PTR_U64s_MAX + 1)) 120f9dda47SKent Overstreet 1379032b07SKent Overstreet int bch2_btree_node_check_topology(struct btree_trans *, struct btree *); 1479032b07SKent Overstreet 15e0319af2SKent Overstreet #define BTREE_UPDATE_MODES() \ 16e0319af2SKent Overstreet x(none) \ 17e0319af2SKent Overstreet x(node) \ 18e0319af2SKent Overstreet x(root) \ 19e0319af2SKent Overstreet x(update) 20e0319af2SKent Overstreet 21e0319af2SKent Overstreet enum btree_update_mode { 22e0319af2SKent Overstreet #define x(n) BTREE_UPDATE_##n, 23e0319af2SKent Overstreet BTREE_UPDATE_MODES() 24e0319af2SKent Overstreet #undef x 25e0319af2SKent Overstreet }; 26e0319af2SKent Overstreet 271c6fdbd8SKent Overstreet /* 281c6fdbd8SKent Overstreet * Tracks an in progress split/rewrite of a btree node and the update to the 291c6fdbd8SKent Overstreet * parent node: 301c6fdbd8SKent Overstreet * 311c6fdbd8SKent Overstreet * When we split/rewrite a node, we do all the updates in memory without 321c6fdbd8SKent Overstreet * waiting for any writes to complete - we allocate the new node(s) and update 331c6fdbd8SKent Overstreet * the parent node, possibly recursively up to the root. 341c6fdbd8SKent Overstreet * 351c6fdbd8SKent Overstreet * The end result is that we have one or more new nodes being written - 361c6fdbd8SKent Overstreet * possibly several, if there were multiple splits - and then a write (updating 371c6fdbd8SKent Overstreet * an interior node) which will make all these new nodes visible. 381c6fdbd8SKent Overstreet * 391c6fdbd8SKent Overstreet * Additionally, as we split/rewrite nodes we free the old nodes - but the old 401c6fdbd8SKent Overstreet * nodes can't be freed (their space on disk can't be reclaimed) until the 411c6fdbd8SKent Overstreet * update to the interior node that makes the new node visible completes - 421c6fdbd8SKent Overstreet * until then, the old nodes are still reachable on disk. 431c6fdbd8SKent Overstreet * 441c6fdbd8SKent Overstreet */ 451c6fdbd8SKent Overstreet struct btree_update { 461c6fdbd8SKent Overstreet struct closure cl; 471c6fdbd8SKent Overstreet struct bch_fs *c; 48991ba021SKent Overstreet u64 start_time; 493becdd48SKent Overstreet unsigned long ip_started; 501c6fdbd8SKent Overstreet 511c6fdbd8SKent Overstreet struct list_head list; 52ac7c51b2SKent Overstreet struct list_head unwritten_list; 531c6fdbd8SKent Overstreet 54e0319af2SKent Overstreet enum btree_update_mode mode; 55449ceafbSKent Overstreet enum bch_trans_commit_flags flags; 561c6fdbd8SKent Overstreet unsigned nodes_written:1; 57e264b2f6SKent Overstreet unsigned took_gc_lock:1; 581c6fdbd8SKent Overstreet 591c6fdbd8SKent Overstreet enum btree_id btree_id; 60d880a438SKent Overstreet unsigned update_level_start; 61d880a438SKent Overstreet unsigned update_level_end; 621c6fdbd8SKent Overstreet 6300b8ccf7SKent Overstreet struct disk_reservation disk_res; 641c6fdbd8SKent Overstreet 651c6fdbd8SKent Overstreet /* 66e0319af2SKent Overstreet * BTREE_UPDATE_node: 671c6fdbd8SKent Overstreet * The update that made the new nodes visible was a regular update to an 681c6fdbd8SKent Overstreet * existing interior node - @b. We can't write out the update to @b 691c6fdbd8SKent Overstreet * until the new nodes we created are finished writing, so we block @b 701c6fdbd8SKent Overstreet * from writing by putting this btree_interior update on the 711c6fdbd8SKent Overstreet * @b->write_blocked list with @write_blocked_list: 721c6fdbd8SKent Overstreet */ 731c6fdbd8SKent Overstreet struct btree *b; 741c6fdbd8SKent Overstreet struct list_head write_blocked_list; 751c6fdbd8SKent Overstreet 761c6fdbd8SKent Overstreet /* 771c6fdbd8SKent Overstreet * We may be freeing nodes that were dirty, and thus had journal entries 781c6fdbd8SKent Overstreet * pinned: we need to transfer the oldest of those pins to the 791c6fdbd8SKent Overstreet * btree_update operation, and release it when the new node(s) 801c6fdbd8SKent Overstreet * are all persistent and reachable: 811c6fdbd8SKent Overstreet */ 821c6fdbd8SKent Overstreet struct journal_entry_pin journal; 831c6fdbd8SKent Overstreet 8400b8ccf7SKent Overstreet /* Preallocated nodes we reserve when we start the update: */ 8530985537SKent Overstreet struct prealloc_nodes { 8630985537SKent Overstreet struct btree *b[BTREE_UPDATE_NODES_MAX]; 8730985537SKent Overstreet unsigned nr; 8830985537SKent Overstreet } prealloc_nodes[2]; 8900b8ccf7SKent Overstreet 9000b8ccf7SKent Overstreet /* Nodes being freed: */ 9100b8ccf7SKent Overstreet struct keylist old_keys; 9200b8ccf7SKent Overstreet u64 _old_keys[BTREE_UPDATE_NODES_MAX * 9374ef5b0dSKent Overstreet BKEY_BTREE_PTR_U64s_MAX]; 9400b8ccf7SKent Overstreet 9500b8ccf7SKent Overstreet /* Nodes being added: */ 9600b8ccf7SKent Overstreet struct keylist new_keys; 9700b8ccf7SKent Overstreet u64 _new_keys[BTREE_UPDATE_NODES_MAX * 9874ef5b0dSKent Overstreet BKEY_BTREE_PTR_U64s_MAX]; 991c6fdbd8SKent Overstreet 1001c6fdbd8SKent Overstreet /* New nodes, that will be made reachable by this update: */ 10100b8ccf7SKent Overstreet struct btree *new_nodes[BTREE_UPDATE_NODES_MAX]; 1021c6fdbd8SKent Overstreet unsigned nr_new_nodes; 1031c6fdbd8SKent Overstreet 104ee757054SKent Overstreet struct btree *old_nodes[BTREE_UPDATE_NODES_MAX]; 105ee757054SKent Overstreet __le64 old_nodes_seq[BTREE_UPDATE_NODES_MAX]; 106ee757054SKent Overstreet unsigned nr_old_nodes; 107ee757054SKent Overstreet 108374153c2SKent Overstreet open_bucket_idx_t open_buckets[BTREE_UPDATE_NODES_MAX * 10900b8ccf7SKent Overstreet BCH_REPLICAS_MAX]; 110374153c2SKent Overstreet open_bucket_idx_t nr_open_buckets; 11100b8ccf7SKent Overstreet 112501e1bdaSKent Overstreet unsigned journal_u64s; 1130f9dda47SKent Overstreet u64 journal_entries[BTREE_UPDATE_JOURNAL_RES]; 114501e1bdaSKent Overstreet 1151c6fdbd8SKent Overstreet /* Only here to reduce stack usage on recursive splits: */ 1161c6fdbd8SKent Overstreet struct keylist parent_keys; 1171c6fdbd8SKent Overstreet /* 1181c6fdbd8SKent Overstreet * Enough room for btree_split's keys without realloc - btree node 1191c6fdbd8SKent Overstreet * pointers never have crc/compression info, so we only need to acount 1201c6fdbd8SKent Overstreet * for the pointers for three keys 1211c6fdbd8SKent Overstreet */ 1221c6fdbd8SKent Overstreet u64 inline_keys[BKEY_BTREE_PTR_U64s_MAX * 3]; 1231c6fdbd8SKent Overstreet }; 1241c6fdbd8SKent Overstreet 1251c6fdbd8SKent Overstreet struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *, 126ca7d8fcaSKent Overstreet struct btree_trans *, 1271c6fdbd8SKent Overstreet struct btree *, 1281c6fdbd8SKent Overstreet struct bkey_format); 1291c6fdbd8SKent Overstreet 130ff70ad2cSKent Overstreet int bch2_btree_split_leaf(struct btree_trans *, btree_path_idx_t, unsigned); 1311c6fdbd8SKent Overstreet 132ba89083eSKent Overstreet int bch2_btree_increase_depth(struct btree_trans *, btree_path_idx_t, unsigned); 133ba89083eSKent Overstreet 134ff70ad2cSKent Overstreet int __bch2_foreground_maybe_merge(struct btree_trans *, btree_path_idx_t, 1351c6fdbd8SKent Overstreet unsigned, unsigned, enum btree_node_sibling); 1361c6fdbd8SKent Overstreet 137e3a67bdbSKent Overstreet static inline int bch2_foreground_maybe_merge_sibling(struct btree_trans *trans, 138ff70ad2cSKent Overstreet btree_path_idx_t path_idx, 1391c6fdbd8SKent Overstreet unsigned level, unsigned flags, 1401c6fdbd8SKent Overstreet enum btree_node_sibling sib) 1411c6fdbd8SKent Overstreet { 142ff70ad2cSKent Overstreet struct btree_path *path = trans->paths + path_idx; 1431c6fdbd8SKent Overstreet struct btree *b; 1441c6fdbd8SKent Overstreet 14522b383adSKent Overstreet EBUG_ON(!btree_node_locked(path, level)); 1461c6fdbd8SKent Overstreet 14755778814SKent Overstreet if (bch2_btree_node_merging_disabled) 14855778814SKent Overstreet return 0; 14955778814SKent Overstreet 15067e0dd8fSKent Overstreet b = path->l[level].b; 151e3a67bdbSKent Overstreet if (b->sib_u64s[sib] > trans->c->btree_foreground_merge_threshold) 152ecab6be7SKent Overstreet return 0; 1531c6fdbd8SKent Overstreet 154ff70ad2cSKent Overstreet return __bch2_foreground_maybe_merge(trans, path_idx, level, flags, sib); 1551c6fdbd8SKent Overstreet } 1561c6fdbd8SKent Overstreet 157e3a67bdbSKent Overstreet static inline int bch2_foreground_maybe_merge(struct btree_trans *trans, 158ff70ad2cSKent Overstreet btree_path_idx_t path, 1591c6fdbd8SKent Overstreet unsigned level, 1601c6fdbd8SKent Overstreet unsigned flags) 1611c6fdbd8SKent Overstreet { 16267e0dd8fSKent Overstreet return bch2_foreground_maybe_merge_sibling(trans, path, level, flags, 163ecab6be7SKent Overstreet btree_prev_sib) ?: 16467e0dd8fSKent Overstreet bch2_foreground_maybe_merge_sibling(trans, path, level, flags, 1651c6fdbd8SKent Overstreet btree_next_sib); 1661c6fdbd8SKent Overstreet } 1671c6fdbd8SKent Overstreet 168ac319b4fSKent Overstreet int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *, 169ac319b4fSKent Overstreet struct btree *, unsigned); 170ac319b4fSKent Overstreet void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *); 171ac319b4fSKent Overstreet int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *, 172ac319b4fSKent Overstreet struct btree *, struct bkey_i *, 173ac319b4fSKent Overstreet unsigned, bool); 174ac319b4fSKent Overstreet int bch2_btree_node_update_key_get_iter(struct btree_trans *, struct btree *, 175ac319b4fSKent Overstreet struct bkey_i *, unsigned, bool); 176ac319b4fSKent Overstreet 1771c6fdbd8SKent Overstreet void bch2_btree_set_root_for_read(struct bch_fs *, struct btree *); 178e2e568bdSKent Overstreet 179e2e568bdSKent Overstreet int bch2_btree_root_alloc_fake_trans(struct btree_trans *, enum btree_id, unsigned); 180f2f61f41SKent Overstreet void bch2_btree_root_alloc_fake(struct bch_fs *, enum btree_id, unsigned); 1811c6fdbd8SKent Overstreet 1821c6fdbd8SKent Overstreet static inline unsigned btree_update_reserve_required(struct bch_fs *c, 1831c6fdbd8SKent Overstreet struct btree *b) 1841c6fdbd8SKent Overstreet { 185c43a6ef9SKent Overstreet unsigned depth = btree_node_root(c, b)->c.level + 1; 1861c6fdbd8SKent Overstreet 1871c6fdbd8SKent Overstreet /* 1881c6fdbd8SKent Overstreet * Number of nodes we might have to allocate in a worst case btree 1891c6fdbd8SKent Overstreet * split operation - we split all the way up to the root, then allocate 1901c6fdbd8SKent Overstreet * a new root, unless we're already at max depth: 1911c6fdbd8SKent Overstreet */ 1921c6fdbd8SKent Overstreet if (depth < BTREE_MAX_DEPTH) 193c43a6ef9SKent Overstreet return (depth - b->c.level) * 2 + 1; 1941c6fdbd8SKent Overstreet else 195c43a6ef9SKent Overstreet return (depth - b->c.level) * 2 - 1; 1961c6fdbd8SKent Overstreet } 1971c6fdbd8SKent Overstreet 1981c6fdbd8SKent Overstreet static inline void btree_node_reset_sib_u64s(struct btree *b) 1991c6fdbd8SKent Overstreet { 2001c6fdbd8SKent Overstreet b->sib_u64s[0] = b->nr.live_u64s; 2011c6fdbd8SKent Overstreet b->sib_u64s[1] = b->nr.live_u64s; 2021c6fdbd8SKent Overstreet } 2031c6fdbd8SKent Overstreet 204ec4edd7bSKent Overstreet static inline void *btree_data_end(struct btree *b) 2051c6fdbd8SKent Overstreet { 206ec4edd7bSKent Overstreet return (void *) b->data + btree_buf_bytes(b); 2071c6fdbd8SKent Overstreet } 2081c6fdbd8SKent Overstreet 209ec4edd7bSKent Overstreet static inline struct bkey_packed *unwritten_whiteouts_start(struct btree *b) 2101c6fdbd8SKent Overstreet { 211ec4edd7bSKent Overstreet return (void *) ((u64 *) btree_data_end(b) - b->whiteout_u64s); 2121c6fdbd8SKent Overstreet } 2131c6fdbd8SKent Overstreet 214ec4edd7bSKent Overstreet static inline struct bkey_packed *unwritten_whiteouts_end(struct btree *b) 2151c6fdbd8SKent Overstreet { 216ec4edd7bSKent Overstreet return btree_data_end(b); 2171c6fdbd8SKent Overstreet } 2181c6fdbd8SKent Overstreet 2191c6fdbd8SKent Overstreet static inline void *write_block(struct btree *b) 2201c6fdbd8SKent Overstreet { 2211c6fdbd8SKent Overstreet return (void *) b->data + (b->written << 9); 2221c6fdbd8SKent Overstreet } 2231c6fdbd8SKent Overstreet 2241fe08f31SKent Overstreet static inline bool __btree_addr_written(struct btree *b, void *p) 2251c6fdbd8SKent Overstreet { 2261fe08f31SKent Overstreet return p < write_block(b); 2271c6fdbd8SKent Overstreet } 2281c6fdbd8SKent Overstreet 2291fe08f31SKent Overstreet static inline bool bset_written(struct btree *b, struct bset *i) 2301c6fdbd8SKent Overstreet { 2311fe08f31SKent Overstreet return __btree_addr_written(b, i); 2321fe08f31SKent Overstreet } 2331fe08f31SKent Overstreet 2341fe08f31SKent Overstreet static inline bool bkey_written(struct btree *b, struct bkey_packed *k) 2351fe08f31SKent Overstreet { 2361fe08f31SKent Overstreet return __btree_addr_written(b, k); 2371c6fdbd8SKent Overstreet } 2381c6fdbd8SKent Overstreet 239ec4edd7bSKent Overstreet static inline ssize_t __bch2_btree_u64s_remaining(struct btree *b, void *end) 2401c6fdbd8SKent Overstreet { 2411c6fdbd8SKent Overstreet ssize_t used = bset_byte_offset(b, end) / sizeof(u64) + 242c9bebae6SKent Overstreet b->whiteout_u64s; 243ec4edd7bSKent Overstreet ssize_t total = btree_buf_bytes(b) >> 3; 2441c6fdbd8SKent Overstreet 2456d9378f3SKent Overstreet /* Always leave one extra u64 for bch2_varint_decode: */ 2466d9378f3SKent Overstreet used++; 2476d9378f3SKent Overstreet 2481c6fdbd8SKent Overstreet return total - used; 2491c6fdbd8SKent Overstreet } 2501c6fdbd8SKent Overstreet 251ec4edd7bSKent Overstreet static inline size_t bch2_btree_keys_u64s_remaining(struct btree *b) 2521c6fdbd8SKent Overstreet { 253ec4edd7bSKent Overstreet ssize_t remaining = __bch2_btree_u64s_remaining(b, 2541c6fdbd8SKent Overstreet btree_bkey_last(b, bset_tree_last(b))); 2551c6fdbd8SKent Overstreet 2561c6fdbd8SKent Overstreet BUG_ON(remaining < 0); 2571c6fdbd8SKent Overstreet 2581c6fdbd8SKent Overstreet if (bset_written(b, btree_bset_last(b))) 2591c6fdbd8SKent Overstreet return 0; 2601c6fdbd8SKent Overstreet 2611c6fdbd8SKent Overstreet return remaining; 2621c6fdbd8SKent Overstreet } 2631c6fdbd8SKent Overstreet 2642177147bSKent Overstreet #define BTREE_WRITE_SET_U64s_BITS 9 2652177147bSKent Overstreet 2661c6fdbd8SKent Overstreet static inline unsigned btree_write_set_buffer(struct btree *b) 2671c6fdbd8SKent Overstreet { 2681c6fdbd8SKent Overstreet /* 2691c6fdbd8SKent Overstreet * Could buffer up larger amounts of keys for btrees with larger keys, 2701c6fdbd8SKent Overstreet * pending benchmarking: 2711c6fdbd8SKent Overstreet */ 2722177147bSKent Overstreet return 8 << BTREE_WRITE_SET_U64s_BITS; 2731c6fdbd8SKent Overstreet } 2741c6fdbd8SKent Overstreet 275ec4edd7bSKent Overstreet static inline struct btree_node_entry *want_new_bset(struct bch_fs *c, struct btree *b) 2761c6fdbd8SKent Overstreet { 277b7ba66c8SKent Overstreet struct bset_tree *t = bset_tree_last(b); 2781c6fdbd8SKent Overstreet struct btree_node_entry *bne = max(write_block(b), 2791c6fdbd8SKent Overstreet (void *) btree_bkey_last(b, bset_tree_last(b))); 2801c6fdbd8SKent Overstreet ssize_t remaining_space = 281ec4edd7bSKent Overstreet __bch2_btree_u64s_remaining(b, bne->keys.start); 2821c6fdbd8SKent Overstreet 283b7ba66c8SKent Overstreet if (unlikely(bset_written(b, bset(b, t)))) { 2841c6fdbd8SKent Overstreet if (remaining_space > (ssize_t) (block_bytes(c) >> 3)) 2851c6fdbd8SKent Overstreet return bne; 2861c6fdbd8SKent Overstreet } else { 287b7ba66c8SKent Overstreet if (unlikely(bset_u64s(t) * sizeof(u64) > btree_write_set_buffer(b)) && 2881c6fdbd8SKent Overstreet remaining_space > (ssize_t) (btree_write_set_buffer(b) >> 3)) 2891c6fdbd8SKent Overstreet return bne; 2901c6fdbd8SKent Overstreet } 2911c6fdbd8SKent Overstreet 2921c6fdbd8SKent Overstreet return NULL; 2931c6fdbd8SKent Overstreet } 2941c6fdbd8SKent Overstreet 295ec4edd7bSKent Overstreet static inline void push_whiteout(struct btree *b, struct bpos pos) 2961c6fdbd8SKent Overstreet { 297e3e464acSKent Overstreet struct bkey_packed k; 2981c6fdbd8SKent Overstreet 299ec4edd7bSKent Overstreet BUG_ON(bch2_btree_keys_u64s_remaining(b) < BKEY_U64s); 30046fee692SKent Overstreet EBUG_ON(btree_node_just_written(b)); 301c9bebae6SKent Overstreet 302e3e464acSKent Overstreet if (!bkey_pack_pos(&k, pos, b)) { 303e3e464acSKent Overstreet struct bkey *u = (void *) &k; 304e3e464acSKent Overstreet 305e3e464acSKent Overstreet bkey_init(u); 306e3e464acSKent Overstreet u->p = pos; 307e3e464acSKent Overstreet } 308e3e464acSKent Overstreet 309e3e464acSKent Overstreet k.needs_whiteout = true; 310e3e464acSKent Overstreet 311e3e464acSKent Overstreet b->whiteout_u64s += k.u64s; 312ec4edd7bSKent Overstreet bkey_p_copy(unwritten_whiteouts_start(b), &k); 3131c6fdbd8SKent Overstreet } 3141c6fdbd8SKent Overstreet 3151c6fdbd8SKent Overstreet /* 3161c6fdbd8SKent Overstreet * write lock must be held on @b (else the dirty bset that we were going to 3171c6fdbd8SKent Overstreet * insert into could be written out from under us) 3181c6fdbd8SKent Overstreet */ 319ec4edd7bSKent Overstreet static inline bool bch2_btree_node_insert_fits(struct btree *b, unsigned u64s) 3201c6fdbd8SKent Overstreet { 321f8058242SKent Overstreet if (unlikely(btree_node_need_rewrite(b))) 3221c6fdbd8SKent Overstreet return false; 3231c6fdbd8SKent Overstreet 324ec4edd7bSKent Overstreet return u64s <= bch2_btree_keys_u64s_remaining(b); 3251c6fdbd8SKent Overstreet } 3261c6fdbd8SKent Overstreet 3277807e143SKent Overstreet void bch2_btree_updates_to_text(struct printbuf *, struct bch_fs *); 3281c6fdbd8SKent Overstreet 329c0960603SKent Overstreet bool bch2_btree_interior_updates_flush(struct bch_fs *); 3301c6fdbd8SKent Overstreet 3319f6db127SKent Overstreet void bch2_journal_entry_to_btree_root(struct bch_fs *, struct jset_entry *); 33200b8ccf7SKent Overstreet struct jset_entry *bch2_btree_roots_to_journal_entries(struct bch_fs *, 333769b3600SKent Overstreet struct jset_entry *, unsigned long); 33400b8ccf7SKent Overstreet 335a1f26d70SKent Overstreet void bch2_do_pending_node_rewrites(struct bch_fs *); 336a1f26d70SKent Overstreet void bch2_free_pending_node_rewrites(struct bch_fs *); 337a1f26d70SKent Overstreet 338*cdda2126SKent Overstreet void bch2_btree_reserve_cache_to_text(struct printbuf *, struct bch_fs *); 339*cdda2126SKent Overstreet 340c823c339SKent Overstreet void bch2_fs_btree_interior_update_exit(struct bch_fs *); 34165db6049SKent Overstreet void bch2_fs_btree_interior_update_init_early(struct bch_fs *); 342c823c339SKent Overstreet int bch2_fs_btree_interior_update_init(struct bch_fs *); 343c823c339SKent Overstreet 3441c6fdbd8SKent Overstreet #endif /* _BCACHEFS_BTREE_UPDATE_INTERIOR_H */ 345