1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_UPDATE_INTERIOR_H
3 #define _BCACHEFS_BTREE_UPDATE_INTERIOR_H
5 #include "btree_cache.h"
6 #include "btree_locking.h"
7 #include "btree_update.h"
9 void __bch2_btree_calc_format(struct bkey_format_state *, struct btree *);
10 bool bch2_btree_node_format_fits(struct bch_fs *c, struct btree *,
11 struct bkey_format *);
13 #define BTREE_UPDATE_NODES_MAX ((BTREE_MAX_DEPTH - 2) * 2 + GC_MERGE_NODES)
15 #define BTREE_UPDATE_JOURNAL_RES (BTREE_UPDATE_NODES_MAX * (BKEY_BTREE_PTR_U64s_MAX + 1))
18 * Tracks an in progress split/rewrite of a btree node and the update to the
21 * When we split/rewrite a node, we do all the updates in memory without
22 * waiting for any writes to complete - we allocate the new node(s) and update
23 * the parent node, possibly recursively up to the root.
25 * The end result is that we have one or more new nodes being written -
26 * possibly several, if there were multiple splits - and then a write (updating
27 * an interior node) which will make all these new nodes visible.
29 * Additionally, as we split/rewrite nodes we free the old nodes - but the old
30 * nodes can't be freed (their space on disk can't be reclaimed) until the
31 * update to the interior node that makes the new node visible completes -
32 * until then, the old nodes are still reachable on disk.
40 struct list_head list;
41 struct list_head unwritten_list;
43 /* What kind of update are we doing? */
45 BTREE_INTERIOR_NO_UPDATE,
46 BTREE_INTERIOR_UPDATING_NODE,
47 BTREE_INTERIOR_UPDATING_ROOT,
48 BTREE_INTERIOR_UPDATING_AS,
51 unsigned nodes_written:1;
52 unsigned took_gc_lock:1;
54 enum btree_id btree_id;
56 struct disk_reservation disk_res;
57 struct journal_preres journal_preres;
60 * BTREE_INTERIOR_UPDATING_NODE:
61 * The update that made the new nodes visible was a regular update to an
62 * existing interior node - @b. We can't write out the update to @b
63 * until the new nodes we created are finished writing, so we block @b
64 * from writing by putting this btree_interior update on the
65 * @b->write_blocked list with @write_blocked_list:
68 struct list_head write_blocked_list;
71 * We may be freeing nodes that were dirty, and thus had journal entries
72 * pinned: we need to transfer the oldest of those pins to the
73 * btree_update operation, and release it when the new node(s)
74 * are all persistent and reachable:
76 struct journal_entry_pin journal;
78 /* Preallocated nodes we reserve when we start the update: */
79 struct btree *prealloc_nodes[BTREE_UPDATE_NODES_MAX];
80 unsigned nr_prealloc_nodes;
82 /* Nodes being freed: */
83 struct keylist old_keys;
84 u64 _old_keys[BTREE_UPDATE_NODES_MAX *
85 BKEY_BTREE_PTR_U64s_MAX];
87 /* Nodes being added: */
88 struct keylist new_keys;
89 u64 _new_keys[BTREE_UPDATE_NODES_MAX *
90 BKEY_BTREE_PTR_U64s_MAX];
92 /* New nodes, that will be made reachable by this update: */
93 struct btree *new_nodes[BTREE_UPDATE_NODES_MAX];
94 unsigned nr_new_nodes;
96 struct btree *old_nodes[BTREE_UPDATE_NODES_MAX];
97 __le64 old_nodes_seq[BTREE_UPDATE_NODES_MAX];
98 unsigned nr_old_nodes;
100 open_bucket_idx_t open_buckets[BTREE_UPDATE_NODES_MAX *
102 open_bucket_idx_t nr_open_buckets;
104 unsigned journal_u64s;
105 u64 journal_entries[BTREE_UPDATE_JOURNAL_RES];
107 /* Only here to reduce stack usage on recursive splits: */
108 struct keylist parent_keys;
110 * Enough room for btree_split's keys without realloc - btree node
111 * pointers never have crc/compression info, so we only need to acount
112 * for the pointers for three keys
114 u64 inline_keys[BKEY_BTREE_PTR_U64s_MAX * 3];
117 struct btree *__bch2_btree_node_alloc_replacement(struct btree_update *,
121 int bch2_btree_split_leaf(struct btree_trans *, struct btree_path *, unsigned);
123 int __bch2_foreground_maybe_merge(struct btree_trans *, struct btree_path *,
124 unsigned, unsigned, enum btree_node_sibling);
126 static inline int bch2_foreground_maybe_merge_sibling(struct btree_trans *trans,
127 struct btree_path *path,
128 unsigned level, unsigned flags,
129 enum btree_node_sibling sib)
133 EBUG_ON(!btree_node_locked(path, level));
135 b = path->l[level].b;
136 if (b->sib_u64s[sib] > trans->c->btree_foreground_merge_threshold)
139 return __bch2_foreground_maybe_merge(trans, path, level, flags, sib);
142 static inline int bch2_foreground_maybe_merge(struct btree_trans *trans,
143 struct btree_path *path,
147 return bch2_foreground_maybe_merge_sibling(trans, path, level, flags,
149 bch2_foreground_maybe_merge_sibling(trans, path, level, flags,
153 void bch2_btree_set_root_for_read(struct bch_fs *, struct btree *);
154 void bch2_btree_root_alloc(struct bch_fs *, enum btree_id);
156 static inline unsigned btree_update_reserve_required(struct bch_fs *c,
159 unsigned depth = btree_node_root(c, b)->c.level + 1;
162 * Number of nodes we might have to allocate in a worst case btree
163 * split operation - we split all the way up to the root, then allocate
164 * a new root, unless we're already at max depth:
166 if (depth < BTREE_MAX_DEPTH)
167 return (depth - b->c.level) * 2 + 1;
169 return (depth - b->c.level) * 2 - 1;
172 static inline void btree_node_reset_sib_u64s(struct btree *b)
174 b->sib_u64s[0] = b->nr.live_u64s;
175 b->sib_u64s[1] = b->nr.live_u64s;
178 static inline void *btree_data_end(struct bch_fs *c, struct btree *b)
180 return (void *) b->data + btree_bytes(c);
183 static inline struct bkey_packed *unwritten_whiteouts_start(struct bch_fs *c,
186 return (void *) ((u64 *) btree_data_end(c, b) - b->whiteout_u64s);
189 static inline struct bkey_packed *unwritten_whiteouts_end(struct bch_fs *c,
192 return btree_data_end(c, b);
195 static inline void *write_block(struct btree *b)
197 return (void *) b->data + (b->written << 9);
200 static inline bool __btree_addr_written(struct btree *b, void *p)
202 return p < write_block(b);
205 static inline bool bset_written(struct btree *b, struct bset *i)
207 return __btree_addr_written(b, i);
210 static inline bool bkey_written(struct btree *b, struct bkey_packed *k)
212 return __btree_addr_written(b, k);
215 static inline ssize_t __bch_btree_u64s_remaining(struct bch_fs *c,
219 ssize_t used = bset_byte_offset(b, end) / sizeof(u64) +
221 ssize_t total = c->opts.btree_node_size >> 3;
223 /* Always leave one extra u64 for bch2_varint_decode: */
229 static inline size_t bch_btree_keys_u64s_remaining(struct bch_fs *c,
232 ssize_t remaining = __bch_btree_u64s_remaining(c, b,
233 btree_bkey_last(b, bset_tree_last(b)));
235 BUG_ON(remaining < 0);
237 if (bset_written(b, btree_bset_last(b)))
243 #define BTREE_WRITE_SET_U64s_BITS 9
245 static inline unsigned btree_write_set_buffer(struct btree *b)
248 * Could buffer up larger amounts of keys for btrees with larger keys,
249 * pending benchmarking:
251 return 8 << BTREE_WRITE_SET_U64s_BITS;
254 static inline struct btree_node_entry *want_new_bset(struct bch_fs *c,
257 struct bset_tree *t = bset_tree_last(b);
258 struct btree_node_entry *bne = max(write_block(b),
259 (void *) btree_bkey_last(b, bset_tree_last(b)));
260 ssize_t remaining_space =
261 __bch_btree_u64s_remaining(c, b, &bne->keys.start[0]);
263 if (unlikely(bset_written(b, bset(b, t)))) {
264 if (remaining_space > (ssize_t) (block_bytes(c) >> 3))
267 if (unlikely(bset_u64s(t) * sizeof(u64) > btree_write_set_buffer(b)) &&
268 remaining_space > (ssize_t) (btree_write_set_buffer(b) >> 3))
275 static inline void push_whiteout(struct bch_fs *c, struct btree *b,
278 struct bkey_packed k;
280 BUG_ON(bch_btree_keys_u64s_remaining(c, b) < BKEY_U64s);
282 if (!bkey_pack_pos(&k, pos, b)) {
283 struct bkey *u = (void *) &k;
289 k.needs_whiteout = true;
291 b->whiteout_u64s += k.u64s;
292 bkey_copy(unwritten_whiteouts_start(c, b), &k);
296 * write lock must be held on @b (else the dirty bset that we were going to
297 * insert into could be written out from under us)
299 static inline bool bch2_btree_node_insert_fits(struct bch_fs *c,
300 struct btree *b, unsigned u64s)
302 if (unlikely(btree_node_need_rewrite(b)))
305 return u64s <= bch_btree_keys_u64s_remaining(c, b);
308 void bch2_btree_updates_to_text(struct printbuf *, struct bch_fs *);
310 size_t bch2_btree_interior_updates_nr_pending(struct bch_fs *);
312 void bch2_journal_entries_to_btree_roots(struct bch_fs *, struct jset *);
313 struct jset_entry *bch2_btree_roots_to_journal_entries(struct bch_fs *,
314 struct jset_entry *, struct jset_entry *);
316 void bch2_fs_btree_interior_update_exit(struct bch_fs *);
317 int bch2_fs_btree_interior_update_init(struct bch_fs *);
319 #endif /* _BCACHEFS_BTREE_UPDATE_INTERIOR_H */