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; 55e0319af2SKent Overstreet enum bch_watermark watermark; 561c6fdbd8SKent Overstreet unsigned nodes_written:1; 57e264b2f6SKent Overstreet unsigned took_gc_lock:1; 581c6fdbd8SKent Overstreet 591c6fdbd8SKent Overstreet enum btree_id btree_id; 601f0f731fSKent Overstreet unsigned update_level; 611c6fdbd8SKent Overstreet 6200b8ccf7SKent Overstreet struct disk_reservation disk_res; 631c6fdbd8SKent Overstreet 641c6fdbd8SKent Overstreet /* 65e0319af2SKent Overstreet * BTREE_UPDATE_node: 661c6fdbd8SKent Overstreet * The update that made the new nodes visible was a regular update to an 671c6fdbd8SKent Overstreet * existing interior node - @b. We can't write out the update to @b 681c6fdbd8SKent Overstreet * until the new nodes we created are finished writing, so we block @b 691c6fdbd8SKent Overstreet * from writing by putting this btree_interior update on the 701c6fdbd8SKent Overstreet * @b->write_blocked list with @write_blocked_list: 711c6fdbd8SKent Overstreet */ 721c6fdbd8SKent Overstreet struct btree *b; 731c6fdbd8SKent Overstreet struct list_head write_blocked_list; 741c6fdbd8SKent Overstreet 751c6fdbd8SKent Overstreet /* 761c6fdbd8SKent Overstreet * We may be freeing nodes that were dirty, and thus had journal entries 771c6fdbd8SKent Overstreet * pinned: we need to transfer the oldest of those pins to the 781c6fdbd8SKent Overstreet * btree_update operation, and release it when the new node(s) 791c6fdbd8SKent Overstreet * are all persistent and reachable: 801c6fdbd8SKent Overstreet */ 811c6fdbd8SKent Overstreet struct journal_entry_pin journal; 821c6fdbd8SKent Overstreet 8300b8ccf7SKent Overstreet /* Preallocated nodes we reserve when we start the update: */ 8430985537SKent Overstreet struct prealloc_nodes { 8530985537SKent Overstreet struct btree *b[BTREE_UPDATE_NODES_MAX]; 8630985537SKent Overstreet unsigned nr; 8730985537SKent Overstreet } prealloc_nodes[2]; 8800b8ccf7SKent Overstreet 8900b8ccf7SKent Overstreet /* Nodes being freed: */ 9000b8ccf7SKent Overstreet struct keylist old_keys; 9100b8ccf7SKent Overstreet u64 _old_keys[BTREE_UPDATE_NODES_MAX * 9274ef5b0dSKent Overstreet BKEY_BTREE_PTR_U64s_MAX]; 9300b8ccf7SKent Overstreet 9400b8ccf7SKent Overstreet /* Nodes being added: */ 9500b8ccf7SKent Overstreet struct keylist new_keys; 9600b8ccf7SKent Overstreet u64 _new_keys[BTREE_UPDATE_NODES_MAX * 9774ef5b0dSKent Overstreet BKEY_BTREE_PTR_U64s_MAX]; 981c6fdbd8SKent Overstreet 991c6fdbd8SKent Overstreet /* New nodes, that will be made reachable by this update: */ 10000b8ccf7SKent Overstreet struct btree *new_nodes[BTREE_UPDATE_NODES_MAX]; 1011c6fdbd8SKent Overstreet unsigned nr_new_nodes; 1021c6fdbd8SKent Overstreet 103ee757054SKent Overstreet struct btree *old_nodes[BTREE_UPDATE_NODES_MAX]; 104ee757054SKent Overstreet __le64 old_nodes_seq[BTREE_UPDATE_NODES_MAX]; 105ee757054SKent Overstreet unsigned nr_old_nodes; 106ee757054SKent Overstreet 107374153c2SKent Overstreet open_bucket_idx_t open_buckets[BTREE_UPDATE_NODES_MAX * 10800b8ccf7SKent Overstreet BCH_REPLICAS_MAX]; 109374153c2SKent Overstreet open_bucket_idx_t nr_open_buckets; 11000b8ccf7SKent Overstreet 111501e1bdaSKent Overstreet unsigned journal_u64s; 1120f9dda47SKent Overstreet u64 journal_entries[BTREE_UPDATE_JOURNAL_RES]; 113501e1bdaSKent Overstreet 1141c6fdbd8SKent Overstreet /* Only here to reduce stack usage on recursive splits: */ 1151c6fdbd8SKent Overstreet struct keylist parent_keys; 1161c6fdbd8SKent Overstreet /* 1171c6fdbd8SKent Overstreet * Enough room for btree_split's keys without realloc - btree node 1181c6fdbd8SKent Overstreet * pointers never have crc/compression info, so we only need to acount 1191c6fdbd8SKent Overstreet * for the pointers for three keys 1201c6fdbd8SKent Overstreet */ 1211c6fdbd8SKent Overstreet u64 inline_keys[BKEY_BTREE_PTR_U64s_MAX * 3]; 1221c6fdbd8SKent Overstreet }; 1231c6fdbd8SKent Overstreet 1241c6fdbd8SKent Overstreet struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *, 125ca7d8fcaSKent Overstreet struct btree_trans *, 1261c6fdbd8SKent Overstreet struct btree *, 1271c6fdbd8SKent Overstreet struct bkey_format); 1281c6fdbd8SKent Overstreet 129ff70ad2cSKent Overstreet int bch2_btree_split_leaf(struct btree_trans *, btree_path_idx_t, unsigned); 1301c6fdbd8SKent Overstreet 131ba89083eSKent Overstreet int bch2_btree_increase_depth(struct btree_trans *, btree_path_idx_t, unsigned); 132ba89083eSKent Overstreet 133ff70ad2cSKent Overstreet int __bch2_foreground_maybe_merge(struct btree_trans *, btree_path_idx_t, 1341c6fdbd8SKent Overstreet unsigned, unsigned, enum btree_node_sibling); 1351c6fdbd8SKent Overstreet 136e3a67bdbSKent Overstreet static inline int bch2_foreground_maybe_merge_sibling(struct btree_trans *trans, 137ff70ad2cSKent Overstreet btree_path_idx_t path_idx, 1381c6fdbd8SKent Overstreet unsigned level, unsigned flags, 1391c6fdbd8SKent Overstreet enum btree_node_sibling sib) 1401c6fdbd8SKent Overstreet { 141ff70ad2cSKent Overstreet struct btree_path *path = trans->paths + path_idx; 1421c6fdbd8SKent Overstreet struct btree *b; 1431c6fdbd8SKent Overstreet 14422b383adSKent Overstreet EBUG_ON(!btree_node_locked(path, level)); 1451c6fdbd8SKent Overstreet 14667e0dd8fSKent Overstreet b = path->l[level].b; 147e3a67bdbSKent Overstreet if (b->sib_u64s[sib] > trans->c->btree_foreground_merge_threshold) 148ecab6be7SKent Overstreet return 0; 1491c6fdbd8SKent Overstreet 150ff70ad2cSKent Overstreet return __bch2_foreground_maybe_merge(trans, path_idx, level, flags, sib); 1511c6fdbd8SKent Overstreet } 1521c6fdbd8SKent Overstreet 153e3a67bdbSKent Overstreet static inline int bch2_foreground_maybe_merge(struct btree_trans *trans, 154ff70ad2cSKent Overstreet btree_path_idx_t path, 1551c6fdbd8SKent Overstreet unsigned level, 1561c6fdbd8SKent Overstreet unsigned flags) 1571c6fdbd8SKent Overstreet { 15867e0dd8fSKent Overstreet return bch2_foreground_maybe_merge_sibling(trans, path, level, flags, 159ecab6be7SKent Overstreet btree_prev_sib) ?: 16067e0dd8fSKent Overstreet bch2_foreground_maybe_merge_sibling(trans, path, level, flags, 1611c6fdbd8SKent Overstreet btree_next_sib); 1621c6fdbd8SKent Overstreet } 1631c6fdbd8SKent Overstreet 164ac319b4fSKent Overstreet int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *, 165ac319b4fSKent Overstreet struct btree *, unsigned); 166ac319b4fSKent Overstreet void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *); 167ac319b4fSKent Overstreet int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *, 168ac319b4fSKent Overstreet struct btree *, struct bkey_i *, 169ac319b4fSKent Overstreet unsigned, bool); 170ac319b4fSKent Overstreet int bch2_btree_node_update_key_get_iter(struct btree_trans *, struct btree *, 171ac319b4fSKent Overstreet struct bkey_i *, unsigned, bool); 172ac319b4fSKent Overstreet 1731c6fdbd8SKent Overstreet void bch2_btree_set_root_for_read(struct bch_fs *, struct btree *); 174*f2f61f41SKent Overstreet void bch2_btree_root_alloc_fake(struct bch_fs *, enum btree_id, unsigned); 1751c6fdbd8SKent Overstreet 1761c6fdbd8SKent Overstreet static inline unsigned btree_update_reserve_required(struct bch_fs *c, 1771c6fdbd8SKent Overstreet struct btree *b) 1781c6fdbd8SKent Overstreet { 179c43a6ef9SKent Overstreet unsigned depth = btree_node_root(c, b)->c.level + 1; 1801c6fdbd8SKent Overstreet 1811c6fdbd8SKent Overstreet /* 1821c6fdbd8SKent Overstreet * Number of nodes we might have to allocate in a worst case btree 1831c6fdbd8SKent Overstreet * split operation - we split all the way up to the root, then allocate 1841c6fdbd8SKent Overstreet * a new root, unless we're already at max depth: 1851c6fdbd8SKent Overstreet */ 1861c6fdbd8SKent Overstreet if (depth < BTREE_MAX_DEPTH) 187c43a6ef9SKent Overstreet return (depth - b->c.level) * 2 + 1; 1881c6fdbd8SKent Overstreet else 189c43a6ef9SKent Overstreet return (depth - b->c.level) * 2 - 1; 1901c6fdbd8SKent Overstreet } 1911c6fdbd8SKent Overstreet 1921c6fdbd8SKent Overstreet static inline void btree_node_reset_sib_u64s(struct btree *b) 1931c6fdbd8SKent Overstreet { 1941c6fdbd8SKent Overstreet b->sib_u64s[0] = b->nr.live_u64s; 1951c6fdbd8SKent Overstreet b->sib_u64s[1] = b->nr.live_u64s; 1961c6fdbd8SKent Overstreet } 1971c6fdbd8SKent Overstreet 198ec4edd7bSKent Overstreet static inline void *btree_data_end(struct btree *b) 1991c6fdbd8SKent Overstreet { 200ec4edd7bSKent Overstreet return (void *) b->data + btree_buf_bytes(b); 2011c6fdbd8SKent Overstreet } 2021c6fdbd8SKent Overstreet 203ec4edd7bSKent Overstreet static inline struct bkey_packed *unwritten_whiteouts_start(struct btree *b) 2041c6fdbd8SKent Overstreet { 205ec4edd7bSKent Overstreet return (void *) ((u64 *) btree_data_end(b) - b->whiteout_u64s); 2061c6fdbd8SKent Overstreet } 2071c6fdbd8SKent Overstreet 208ec4edd7bSKent Overstreet static inline struct bkey_packed *unwritten_whiteouts_end(struct btree *b) 2091c6fdbd8SKent Overstreet { 210ec4edd7bSKent Overstreet return btree_data_end(b); 2111c6fdbd8SKent Overstreet } 2121c6fdbd8SKent Overstreet 2131c6fdbd8SKent Overstreet static inline void *write_block(struct btree *b) 2141c6fdbd8SKent Overstreet { 2151c6fdbd8SKent Overstreet return (void *) b->data + (b->written << 9); 2161c6fdbd8SKent Overstreet } 2171c6fdbd8SKent Overstreet 2181fe08f31SKent Overstreet static inline bool __btree_addr_written(struct btree *b, void *p) 2191c6fdbd8SKent Overstreet { 2201fe08f31SKent Overstreet return p < write_block(b); 2211c6fdbd8SKent Overstreet } 2221c6fdbd8SKent Overstreet 2231fe08f31SKent Overstreet static inline bool bset_written(struct btree *b, struct bset *i) 2241c6fdbd8SKent Overstreet { 2251fe08f31SKent Overstreet return __btree_addr_written(b, i); 2261fe08f31SKent Overstreet } 2271fe08f31SKent Overstreet 2281fe08f31SKent Overstreet static inline bool bkey_written(struct btree *b, struct bkey_packed *k) 2291fe08f31SKent Overstreet { 2301fe08f31SKent Overstreet return __btree_addr_written(b, k); 2311c6fdbd8SKent Overstreet } 2321c6fdbd8SKent Overstreet 233ec4edd7bSKent Overstreet static inline ssize_t __bch2_btree_u64s_remaining(struct btree *b, void *end) 2341c6fdbd8SKent Overstreet { 2351c6fdbd8SKent Overstreet ssize_t used = bset_byte_offset(b, end) / sizeof(u64) + 236c9bebae6SKent Overstreet b->whiteout_u64s; 237ec4edd7bSKent Overstreet ssize_t total = btree_buf_bytes(b) >> 3; 2381c6fdbd8SKent Overstreet 2396d9378f3SKent Overstreet /* Always leave one extra u64 for bch2_varint_decode: */ 2406d9378f3SKent Overstreet used++; 2416d9378f3SKent Overstreet 2421c6fdbd8SKent Overstreet return total - used; 2431c6fdbd8SKent Overstreet } 2441c6fdbd8SKent Overstreet 245ec4edd7bSKent Overstreet static inline size_t bch2_btree_keys_u64s_remaining(struct btree *b) 2461c6fdbd8SKent Overstreet { 247ec4edd7bSKent Overstreet ssize_t remaining = __bch2_btree_u64s_remaining(b, 2481c6fdbd8SKent Overstreet btree_bkey_last(b, bset_tree_last(b))); 2491c6fdbd8SKent Overstreet 2501c6fdbd8SKent Overstreet BUG_ON(remaining < 0); 2511c6fdbd8SKent Overstreet 2521c6fdbd8SKent Overstreet if (bset_written(b, btree_bset_last(b))) 2531c6fdbd8SKent Overstreet return 0; 2541c6fdbd8SKent Overstreet 2551c6fdbd8SKent Overstreet return remaining; 2561c6fdbd8SKent Overstreet } 2571c6fdbd8SKent Overstreet 2582177147bSKent Overstreet #define BTREE_WRITE_SET_U64s_BITS 9 2592177147bSKent Overstreet 2601c6fdbd8SKent Overstreet static inline unsigned btree_write_set_buffer(struct btree *b) 2611c6fdbd8SKent Overstreet { 2621c6fdbd8SKent Overstreet /* 2631c6fdbd8SKent Overstreet * Could buffer up larger amounts of keys for btrees with larger keys, 2641c6fdbd8SKent Overstreet * pending benchmarking: 2651c6fdbd8SKent Overstreet */ 2662177147bSKent Overstreet return 8 << BTREE_WRITE_SET_U64s_BITS; 2671c6fdbd8SKent Overstreet } 2681c6fdbd8SKent Overstreet 269ec4edd7bSKent Overstreet static inline struct btree_node_entry *want_new_bset(struct bch_fs *c, struct btree *b) 2701c6fdbd8SKent Overstreet { 271b7ba66c8SKent Overstreet struct bset_tree *t = bset_tree_last(b); 2721c6fdbd8SKent Overstreet struct btree_node_entry *bne = max(write_block(b), 2731c6fdbd8SKent Overstreet (void *) btree_bkey_last(b, bset_tree_last(b))); 2741c6fdbd8SKent Overstreet ssize_t remaining_space = 275ec4edd7bSKent Overstreet __bch2_btree_u64s_remaining(b, bne->keys.start); 2761c6fdbd8SKent Overstreet 277b7ba66c8SKent Overstreet if (unlikely(bset_written(b, bset(b, t)))) { 2781c6fdbd8SKent Overstreet if (remaining_space > (ssize_t) (block_bytes(c) >> 3)) 2791c6fdbd8SKent Overstreet return bne; 2801c6fdbd8SKent Overstreet } else { 281b7ba66c8SKent Overstreet if (unlikely(bset_u64s(t) * sizeof(u64) > btree_write_set_buffer(b)) && 2821c6fdbd8SKent Overstreet remaining_space > (ssize_t) (btree_write_set_buffer(b) >> 3)) 2831c6fdbd8SKent Overstreet return bne; 2841c6fdbd8SKent Overstreet } 2851c6fdbd8SKent Overstreet 2861c6fdbd8SKent Overstreet return NULL; 2871c6fdbd8SKent Overstreet } 2881c6fdbd8SKent Overstreet 289ec4edd7bSKent Overstreet static inline void push_whiteout(struct btree *b, struct bpos pos) 2901c6fdbd8SKent Overstreet { 291e3e464acSKent Overstreet struct bkey_packed k; 2921c6fdbd8SKent Overstreet 293ec4edd7bSKent Overstreet BUG_ON(bch2_btree_keys_u64s_remaining(b) < BKEY_U64s); 29446fee692SKent Overstreet EBUG_ON(btree_node_just_written(b)); 295c9bebae6SKent Overstreet 296e3e464acSKent Overstreet if (!bkey_pack_pos(&k, pos, b)) { 297e3e464acSKent Overstreet struct bkey *u = (void *) &k; 298e3e464acSKent Overstreet 299e3e464acSKent Overstreet bkey_init(u); 300e3e464acSKent Overstreet u->p = pos; 301e3e464acSKent Overstreet } 302e3e464acSKent Overstreet 303e3e464acSKent Overstreet k.needs_whiteout = true; 304e3e464acSKent Overstreet 305e3e464acSKent Overstreet b->whiteout_u64s += k.u64s; 306ec4edd7bSKent Overstreet bkey_p_copy(unwritten_whiteouts_start(b), &k); 3071c6fdbd8SKent Overstreet } 3081c6fdbd8SKent Overstreet 3091c6fdbd8SKent Overstreet /* 3101c6fdbd8SKent Overstreet * write lock must be held on @b (else the dirty bset that we were going to 3111c6fdbd8SKent Overstreet * insert into could be written out from under us) 3121c6fdbd8SKent Overstreet */ 313ec4edd7bSKent Overstreet static inline bool bch2_btree_node_insert_fits(struct btree *b, unsigned u64s) 3141c6fdbd8SKent Overstreet { 315f8058242SKent Overstreet if (unlikely(btree_node_need_rewrite(b))) 3161c6fdbd8SKent Overstreet return false; 3171c6fdbd8SKent Overstreet 318ec4edd7bSKent Overstreet return u64s <= bch2_btree_keys_u64s_remaining(b); 3191c6fdbd8SKent Overstreet } 3201c6fdbd8SKent Overstreet 3217807e143SKent Overstreet void bch2_btree_updates_to_text(struct printbuf *, struct bch_fs *); 3221c6fdbd8SKent Overstreet 323c0960603SKent Overstreet bool bch2_btree_interior_updates_flush(struct bch_fs *); 3241c6fdbd8SKent Overstreet 3259f6db127SKent Overstreet void bch2_journal_entry_to_btree_root(struct bch_fs *, struct jset_entry *); 32600b8ccf7SKent Overstreet struct jset_entry *bch2_btree_roots_to_journal_entries(struct bch_fs *, 327769b3600SKent Overstreet struct jset_entry *, unsigned long); 32800b8ccf7SKent Overstreet 329a1f26d70SKent Overstreet void bch2_do_pending_node_rewrites(struct bch_fs *); 330a1f26d70SKent Overstreet void bch2_free_pending_node_rewrites(struct bch_fs *); 331a1f26d70SKent Overstreet 332c823c339SKent Overstreet void bch2_fs_btree_interior_update_exit(struct bch_fs *); 33365db6049SKent Overstreet void bch2_fs_btree_interior_update_init_early(struct bch_fs *); 334c823c339SKent Overstreet int bch2_fs_btree_interior_update_init(struct bch_fs *); 335c823c339SKent Overstreet 3361c6fdbd8SKent Overstreet #endif /* _BCACHEFS_BTREE_UPDATE_INTERIOR_H */ 337