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 131c6fdbd8SKent Overstreet /* 141c6fdbd8SKent Overstreet * Tracks an in progress split/rewrite of a btree node and the update to the 151c6fdbd8SKent Overstreet * parent node: 161c6fdbd8SKent Overstreet * 171c6fdbd8SKent Overstreet * When we split/rewrite a node, we do all the updates in memory without 181c6fdbd8SKent Overstreet * waiting for any writes to complete - we allocate the new node(s) and update 191c6fdbd8SKent Overstreet * the parent node, possibly recursively up to the root. 201c6fdbd8SKent Overstreet * 211c6fdbd8SKent Overstreet * The end result is that we have one or more new nodes being written - 221c6fdbd8SKent Overstreet * possibly several, if there were multiple splits - and then a write (updating 231c6fdbd8SKent Overstreet * an interior node) which will make all these new nodes visible. 241c6fdbd8SKent Overstreet * 251c6fdbd8SKent Overstreet * Additionally, as we split/rewrite nodes we free the old nodes - but the old 261c6fdbd8SKent Overstreet * nodes can't be freed (their space on disk can't be reclaimed) until the 271c6fdbd8SKent Overstreet * update to the interior node that makes the new node visible completes - 281c6fdbd8SKent Overstreet * until then, the old nodes are still reachable on disk. 291c6fdbd8SKent Overstreet * 301c6fdbd8SKent Overstreet */ 311c6fdbd8SKent Overstreet struct btree_update { 321c6fdbd8SKent Overstreet struct closure cl; 331c6fdbd8SKent Overstreet struct bch_fs *c; 34991ba021SKent Overstreet u64 start_time; 351c6fdbd8SKent Overstreet 361c6fdbd8SKent Overstreet struct list_head list; 37ac7c51b2SKent Overstreet struct list_head unwritten_list; 381c6fdbd8SKent Overstreet 391c6fdbd8SKent Overstreet /* What kind of update are we doing? */ 401c6fdbd8SKent Overstreet enum { 411c6fdbd8SKent Overstreet BTREE_INTERIOR_NO_UPDATE, 421c6fdbd8SKent Overstreet BTREE_INTERIOR_UPDATING_NODE, 431c6fdbd8SKent Overstreet BTREE_INTERIOR_UPDATING_ROOT, 441c6fdbd8SKent Overstreet BTREE_INTERIOR_UPDATING_AS, 451c6fdbd8SKent Overstreet } mode; 461c6fdbd8SKent Overstreet 471c6fdbd8SKent Overstreet unsigned nodes_written:1; 48e264b2f6SKent Overstreet unsigned took_gc_lock:1; 491c6fdbd8SKent Overstreet 501c6fdbd8SKent Overstreet enum btree_id btree_id; 511f0f731fSKent Overstreet unsigned update_level; 521c6fdbd8SKent Overstreet 5300b8ccf7SKent Overstreet struct disk_reservation disk_res; 541c6fdbd8SKent Overstreet 551c6fdbd8SKent Overstreet /* 561c6fdbd8SKent Overstreet * BTREE_INTERIOR_UPDATING_NODE: 571c6fdbd8SKent Overstreet * The update that made the new nodes visible was a regular update to an 581c6fdbd8SKent Overstreet * existing interior node - @b. We can't write out the update to @b 591c6fdbd8SKent Overstreet * until the new nodes we created are finished writing, so we block @b 601c6fdbd8SKent Overstreet * from writing by putting this btree_interior update on the 611c6fdbd8SKent Overstreet * @b->write_blocked list with @write_blocked_list: 621c6fdbd8SKent Overstreet */ 631c6fdbd8SKent Overstreet struct btree *b; 641c6fdbd8SKent Overstreet struct list_head write_blocked_list; 651c6fdbd8SKent Overstreet 661c6fdbd8SKent Overstreet /* 671c6fdbd8SKent Overstreet * We may be freeing nodes that were dirty, and thus had journal entries 681c6fdbd8SKent Overstreet * pinned: we need to transfer the oldest of those pins to the 691c6fdbd8SKent Overstreet * btree_update operation, and release it when the new node(s) 701c6fdbd8SKent Overstreet * are all persistent and reachable: 711c6fdbd8SKent Overstreet */ 721c6fdbd8SKent Overstreet struct journal_entry_pin journal; 731c6fdbd8SKent Overstreet 7400b8ccf7SKent Overstreet /* Preallocated nodes we reserve when we start the update: */ 7530985537SKent Overstreet struct prealloc_nodes { 7630985537SKent Overstreet struct btree *b[BTREE_UPDATE_NODES_MAX]; 7730985537SKent Overstreet unsigned nr; 7830985537SKent Overstreet } prealloc_nodes[2]; 7900b8ccf7SKent Overstreet 8000b8ccf7SKent Overstreet /* Nodes being freed: */ 8100b8ccf7SKent Overstreet struct keylist old_keys; 8200b8ccf7SKent Overstreet u64 _old_keys[BTREE_UPDATE_NODES_MAX * 8374ef5b0dSKent Overstreet BKEY_BTREE_PTR_U64s_MAX]; 8400b8ccf7SKent Overstreet 8500b8ccf7SKent Overstreet /* Nodes being added: */ 8600b8ccf7SKent Overstreet struct keylist new_keys; 8700b8ccf7SKent Overstreet u64 _new_keys[BTREE_UPDATE_NODES_MAX * 8874ef5b0dSKent Overstreet BKEY_BTREE_PTR_U64s_MAX]; 891c6fdbd8SKent Overstreet 901c6fdbd8SKent Overstreet /* New nodes, that will be made reachable by this update: */ 9100b8ccf7SKent Overstreet struct btree *new_nodes[BTREE_UPDATE_NODES_MAX]; 921c6fdbd8SKent Overstreet unsigned nr_new_nodes; 931c6fdbd8SKent Overstreet 94ee757054SKent Overstreet struct btree *old_nodes[BTREE_UPDATE_NODES_MAX]; 95ee757054SKent Overstreet __le64 old_nodes_seq[BTREE_UPDATE_NODES_MAX]; 96ee757054SKent Overstreet unsigned nr_old_nodes; 97ee757054SKent Overstreet 98374153c2SKent Overstreet open_bucket_idx_t open_buckets[BTREE_UPDATE_NODES_MAX * 9900b8ccf7SKent Overstreet BCH_REPLICAS_MAX]; 100374153c2SKent Overstreet open_bucket_idx_t nr_open_buckets; 10100b8ccf7SKent Overstreet 102501e1bdaSKent Overstreet unsigned journal_u64s; 1030f9dda47SKent Overstreet u64 journal_entries[BTREE_UPDATE_JOURNAL_RES]; 104501e1bdaSKent Overstreet 1051c6fdbd8SKent Overstreet /* Only here to reduce stack usage on recursive splits: */ 1061c6fdbd8SKent Overstreet struct keylist parent_keys; 1071c6fdbd8SKent Overstreet /* 1081c6fdbd8SKent Overstreet * Enough room for btree_split's keys without realloc - btree node 1091c6fdbd8SKent Overstreet * pointers never have crc/compression info, so we only need to acount 1101c6fdbd8SKent Overstreet * for the pointers for three keys 1111c6fdbd8SKent Overstreet */ 1121c6fdbd8SKent Overstreet u64 inline_keys[BKEY_BTREE_PTR_U64s_MAX * 3]; 1131c6fdbd8SKent Overstreet }; 1141c6fdbd8SKent Overstreet 1151c6fdbd8SKent Overstreet struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *, 116ca7d8fcaSKent Overstreet struct btree_trans *, 1171c6fdbd8SKent Overstreet struct btree *, 1181c6fdbd8SKent Overstreet struct bkey_format); 1191c6fdbd8SKent Overstreet 120*ff70ad2cSKent Overstreet int bch2_btree_split_leaf(struct btree_trans *, btree_path_idx_t, unsigned); 1211c6fdbd8SKent Overstreet 122*ff70ad2cSKent Overstreet int __bch2_foreground_maybe_merge(struct btree_trans *, btree_path_idx_t, 1231c6fdbd8SKent Overstreet unsigned, unsigned, enum btree_node_sibling); 1241c6fdbd8SKent Overstreet 125e3a67bdbSKent Overstreet static inline int bch2_foreground_maybe_merge_sibling(struct btree_trans *trans, 126*ff70ad2cSKent Overstreet btree_path_idx_t path_idx, 1271c6fdbd8SKent Overstreet unsigned level, unsigned flags, 1281c6fdbd8SKent Overstreet enum btree_node_sibling sib) 1291c6fdbd8SKent Overstreet { 130*ff70ad2cSKent Overstreet struct btree_path *path = trans->paths + path_idx; 1311c6fdbd8SKent Overstreet struct btree *b; 1321c6fdbd8SKent Overstreet 13322b383adSKent Overstreet EBUG_ON(!btree_node_locked(path, level)); 1341c6fdbd8SKent Overstreet 13567e0dd8fSKent Overstreet b = path->l[level].b; 136e3a67bdbSKent Overstreet if (b->sib_u64s[sib] > trans->c->btree_foreground_merge_threshold) 137ecab6be7SKent Overstreet return 0; 1381c6fdbd8SKent Overstreet 139*ff70ad2cSKent Overstreet return __bch2_foreground_maybe_merge(trans, path_idx, level, flags, sib); 1401c6fdbd8SKent Overstreet } 1411c6fdbd8SKent Overstreet 142e3a67bdbSKent Overstreet static inline int bch2_foreground_maybe_merge(struct btree_trans *trans, 143*ff70ad2cSKent Overstreet btree_path_idx_t path, 1441c6fdbd8SKent Overstreet unsigned level, 1451c6fdbd8SKent Overstreet unsigned flags) 1461c6fdbd8SKent Overstreet { 14767e0dd8fSKent Overstreet return bch2_foreground_maybe_merge_sibling(trans, path, level, flags, 148ecab6be7SKent Overstreet btree_prev_sib) ?: 14967e0dd8fSKent Overstreet bch2_foreground_maybe_merge_sibling(trans, path, level, flags, 1501c6fdbd8SKent Overstreet btree_next_sib); 1511c6fdbd8SKent Overstreet } 1521c6fdbd8SKent Overstreet 153ac319b4fSKent Overstreet int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *, 154ac319b4fSKent Overstreet struct btree *, unsigned); 155ac319b4fSKent Overstreet void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *); 156ac319b4fSKent Overstreet int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *, 157ac319b4fSKent Overstreet struct btree *, struct bkey_i *, 158ac319b4fSKent Overstreet unsigned, bool); 159ac319b4fSKent Overstreet int bch2_btree_node_update_key_get_iter(struct btree_trans *, struct btree *, 160ac319b4fSKent Overstreet struct bkey_i *, unsigned, bool); 161ac319b4fSKent Overstreet 1621c6fdbd8SKent Overstreet void bch2_btree_set_root_for_read(struct bch_fs *, struct btree *); 1631c6fdbd8SKent Overstreet void bch2_btree_root_alloc(struct bch_fs *, enum btree_id); 1641c6fdbd8SKent Overstreet 1651c6fdbd8SKent Overstreet static inline unsigned btree_update_reserve_required(struct bch_fs *c, 1661c6fdbd8SKent Overstreet struct btree *b) 1671c6fdbd8SKent Overstreet { 168c43a6ef9SKent Overstreet unsigned depth = btree_node_root(c, b)->c.level + 1; 1691c6fdbd8SKent Overstreet 1701c6fdbd8SKent Overstreet /* 1711c6fdbd8SKent Overstreet * Number of nodes we might have to allocate in a worst case btree 1721c6fdbd8SKent Overstreet * split operation - we split all the way up to the root, then allocate 1731c6fdbd8SKent Overstreet * a new root, unless we're already at max depth: 1741c6fdbd8SKent Overstreet */ 1751c6fdbd8SKent Overstreet if (depth < BTREE_MAX_DEPTH) 176c43a6ef9SKent Overstreet return (depth - b->c.level) * 2 + 1; 1771c6fdbd8SKent Overstreet else 178c43a6ef9SKent Overstreet return (depth - b->c.level) * 2 - 1; 1791c6fdbd8SKent Overstreet } 1801c6fdbd8SKent Overstreet 1811c6fdbd8SKent Overstreet static inline void btree_node_reset_sib_u64s(struct btree *b) 1821c6fdbd8SKent Overstreet { 1831c6fdbd8SKent Overstreet b->sib_u64s[0] = b->nr.live_u64s; 1841c6fdbd8SKent Overstreet b->sib_u64s[1] = b->nr.live_u64s; 1851c6fdbd8SKent Overstreet } 1861c6fdbd8SKent Overstreet 1871c6fdbd8SKent Overstreet static inline void *btree_data_end(struct bch_fs *c, struct btree *b) 1881c6fdbd8SKent Overstreet { 1891c6fdbd8SKent Overstreet return (void *) b->data + btree_bytes(c); 1901c6fdbd8SKent Overstreet } 1911c6fdbd8SKent Overstreet 1921c6fdbd8SKent Overstreet static inline struct bkey_packed *unwritten_whiteouts_start(struct bch_fs *c, 1931c6fdbd8SKent Overstreet struct btree *b) 1941c6fdbd8SKent Overstreet { 1951c6fdbd8SKent Overstreet return (void *) ((u64 *) btree_data_end(c, b) - b->whiteout_u64s); 1961c6fdbd8SKent Overstreet } 1971c6fdbd8SKent Overstreet 1981c6fdbd8SKent Overstreet static inline struct bkey_packed *unwritten_whiteouts_end(struct bch_fs *c, 1991c6fdbd8SKent Overstreet struct btree *b) 2001c6fdbd8SKent Overstreet { 2011c6fdbd8SKent Overstreet return btree_data_end(c, b); 2021c6fdbd8SKent Overstreet } 2031c6fdbd8SKent Overstreet 2041c6fdbd8SKent Overstreet static inline void *write_block(struct btree *b) 2051c6fdbd8SKent Overstreet { 2061c6fdbd8SKent Overstreet return (void *) b->data + (b->written << 9); 2071c6fdbd8SKent Overstreet } 2081c6fdbd8SKent Overstreet 2091fe08f31SKent Overstreet static inline bool __btree_addr_written(struct btree *b, void *p) 2101c6fdbd8SKent Overstreet { 2111fe08f31SKent Overstreet return p < write_block(b); 2121c6fdbd8SKent Overstreet } 2131c6fdbd8SKent Overstreet 2141fe08f31SKent Overstreet static inline bool bset_written(struct btree *b, struct bset *i) 2151c6fdbd8SKent Overstreet { 2161fe08f31SKent Overstreet return __btree_addr_written(b, i); 2171fe08f31SKent Overstreet } 2181fe08f31SKent Overstreet 2191fe08f31SKent Overstreet static inline bool bkey_written(struct btree *b, struct bkey_packed *k) 2201fe08f31SKent Overstreet { 2211fe08f31SKent Overstreet return __btree_addr_written(b, k); 2221c6fdbd8SKent Overstreet } 2231c6fdbd8SKent Overstreet 2241c6fdbd8SKent Overstreet static inline ssize_t __bch_btree_u64s_remaining(struct bch_fs *c, 2251c6fdbd8SKent Overstreet struct btree *b, 2261c6fdbd8SKent Overstreet void *end) 2271c6fdbd8SKent Overstreet { 2281c6fdbd8SKent Overstreet ssize_t used = bset_byte_offset(b, end) / sizeof(u64) + 229c9bebae6SKent Overstreet b->whiteout_u64s; 2308244f320SKent Overstreet ssize_t total = c->opts.btree_node_size >> 3; 2311c6fdbd8SKent Overstreet 2326d9378f3SKent Overstreet /* Always leave one extra u64 for bch2_varint_decode: */ 2336d9378f3SKent Overstreet used++; 2346d9378f3SKent Overstreet 2351c6fdbd8SKent Overstreet return total - used; 2361c6fdbd8SKent Overstreet } 2371c6fdbd8SKent Overstreet 2381c6fdbd8SKent Overstreet static inline size_t bch_btree_keys_u64s_remaining(struct bch_fs *c, 2391c6fdbd8SKent Overstreet struct btree *b) 2401c6fdbd8SKent Overstreet { 2411c6fdbd8SKent Overstreet ssize_t remaining = __bch_btree_u64s_remaining(c, b, 2421c6fdbd8SKent Overstreet btree_bkey_last(b, bset_tree_last(b))); 2431c6fdbd8SKent Overstreet 2441c6fdbd8SKent Overstreet BUG_ON(remaining < 0); 2451c6fdbd8SKent Overstreet 2461c6fdbd8SKent Overstreet if (bset_written(b, btree_bset_last(b))) 2471c6fdbd8SKent Overstreet return 0; 2481c6fdbd8SKent Overstreet 2491c6fdbd8SKent Overstreet return remaining; 2501c6fdbd8SKent Overstreet } 2511c6fdbd8SKent Overstreet 2522177147bSKent Overstreet #define BTREE_WRITE_SET_U64s_BITS 9 2532177147bSKent Overstreet 2541c6fdbd8SKent Overstreet static inline unsigned btree_write_set_buffer(struct btree *b) 2551c6fdbd8SKent Overstreet { 2561c6fdbd8SKent Overstreet /* 2571c6fdbd8SKent Overstreet * Could buffer up larger amounts of keys for btrees with larger keys, 2581c6fdbd8SKent Overstreet * pending benchmarking: 2591c6fdbd8SKent Overstreet */ 2602177147bSKent Overstreet return 8 << BTREE_WRITE_SET_U64s_BITS; 2611c6fdbd8SKent Overstreet } 2621c6fdbd8SKent Overstreet 2631c6fdbd8SKent Overstreet static inline struct btree_node_entry *want_new_bset(struct bch_fs *c, 2641c6fdbd8SKent Overstreet struct btree *b) 2651c6fdbd8SKent Overstreet { 266b7ba66c8SKent Overstreet struct bset_tree *t = bset_tree_last(b); 2671c6fdbd8SKent Overstreet struct btree_node_entry *bne = max(write_block(b), 2681c6fdbd8SKent Overstreet (void *) btree_bkey_last(b, bset_tree_last(b))); 2691c6fdbd8SKent Overstreet ssize_t remaining_space = 2706dfa10abSKent Overstreet __bch_btree_u64s_remaining(c, b, bne->keys.start); 2711c6fdbd8SKent Overstreet 272b7ba66c8SKent Overstreet if (unlikely(bset_written(b, bset(b, t)))) { 2731c6fdbd8SKent Overstreet if (remaining_space > (ssize_t) (block_bytes(c) >> 3)) 2741c6fdbd8SKent Overstreet return bne; 2751c6fdbd8SKent Overstreet } else { 276b7ba66c8SKent Overstreet if (unlikely(bset_u64s(t) * sizeof(u64) > btree_write_set_buffer(b)) && 2771c6fdbd8SKent Overstreet remaining_space > (ssize_t) (btree_write_set_buffer(b) >> 3)) 2781c6fdbd8SKent Overstreet return bne; 2791c6fdbd8SKent Overstreet } 2801c6fdbd8SKent Overstreet 2811c6fdbd8SKent Overstreet return NULL; 2821c6fdbd8SKent Overstreet } 2831c6fdbd8SKent Overstreet 284c9bebae6SKent Overstreet static inline void push_whiteout(struct bch_fs *c, struct btree *b, 285e3e464acSKent Overstreet struct bpos pos) 2861c6fdbd8SKent Overstreet { 287e3e464acSKent Overstreet struct bkey_packed k; 2881c6fdbd8SKent Overstreet 289e3e464acSKent Overstreet BUG_ON(bch_btree_keys_u64s_remaining(c, b) < BKEY_U64s); 29046fee692SKent Overstreet EBUG_ON(btree_node_just_written(b)); 291c9bebae6SKent Overstreet 292e3e464acSKent Overstreet if (!bkey_pack_pos(&k, pos, b)) { 293e3e464acSKent Overstreet struct bkey *u = (void *) &k; 294e3e464acSKent Overstreet 295e3e464acSKent Overstreet bkey_init(u); 296e3e464acSKent Overstreet u->p = pos; 297e3e464acSKent Overstreet } 298e3e464acSKent Overstreet 299e3e464acSKent Overstreet k.needs_whiteout = true; 300e3e464acSKent Overstreet 301e3e464acSKent Overstreet b->whiteout_u64s += k.u64s; 302a8958a1aSKent Overstreet bkey_p_copy(unwritten_whiteouts_start(c, b), &k); 3031c6fdbd8SKent Overstreet } 3041c6fdbd8SKent Overstreet 3051c6fdbd8SKent Overstreet /* 3061c6fdbd8SKent Overstreet * write lock must be held on @b (else the dirty bset that we were going to 3071c6fdbd8SKent Overstreet * insert into could be written out from under us) 3081c6fdbd8SKent Overstreet */ 3091c6fdbd8SKent Overstreet static inline bool bch2_btree_node_insert_fits(struct bch_fs *c, 3101c6fdbd8SKent Overstreet struct btree *b, unsigned u64s) 3111c6fdbd8SKent Overstreet { 312f8058242SKent Overstreet if (unlikely(btree_node_need_rewrite(b))) 3131c6fdbd8SKent Overstreet return false; 3141c6fdbd8SKent Overstreet 3151c6fdbd8SKent Overstreet return u64s <= bch_btree_keys_u64s_remaining(c, b); 3161c6fdbd8SKent Overstreet } 3171c6fdbd8SKent Overstreet 3187807e143SKent Overstreet void bch2_btree_updates_to_text(struct printbuf *, struct bch_fs *); 3191c6fdbd8SKent Overstreet 320c0960603SKent Overstreet bool bch2_btree_interior_updates_flush(struct bch_fs *); 3211c6fdbd8SKent Overstreet 3229f6db127SKent Overstreet void bch2_journal_entry_to_btree_root(struct bch_fs *, struct jset_entry *); 32300b8ccf7SKent Overstreet struct jset_entry *bch2_btree_roots_to_journal_entries(struct bch_fs *, 324769b3600SKent Overstreet struct jset_entry *, unsigned long); 32500b8ccf7SKent Overstreet 326a1f26d70SKent Overstreet void bch2_do_pending_node_rewrites(struct bch_fs *); 327a1f26d70SKent Overstreet void bch2_free_pending_node_rewrites(struct bch_fs *); 328a1f26d70SKent Overstreet 329c823c339SKent Overstreet void bch2_fs_btree_interior_update_exit(struct bch_fs *); 33065db6049SKent Overstreet void bch2_fs_btree_interior_update_init_early(struct bch_fs *); 331c823c339SKent Overstreet int bch2_fs_btree_interior_update_init(struct bch_fs *); 332c823c339SKent Overstreet 3331c6fdbd8SKent Overstreet #endif /* _BCACHEFS_BTREE_UPDATE_INTERIOR_H */ 334