1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_update.h"
5 #include "btree_update_interior.h"
8 #include "btree_iter.h"
9 #include "btree_key_cache.h"
10 #include "btree_locking.h"
14 #include "extent_update.h"
16 #include "journal_reclaim.h"
19 #include "subvolume.h"
22 #include <linux/prefetch.h>
23 #include <linux/sort.h>
24 #include <trace/events/bcachefs.h>
26 static int __must_check
27 bch2_trans_update_by_path(struct btree_trans *, struct btree_path *,
28 struct bkey_i *, enum btree_update_flags);
30 static inline int btree_insert_entry_cmp(const struct btree_insert_entry *l,
31 const struct btree_insert_entry *r)
33 return cmp_int(l->btree_id, r->btree_id) ?:
34 -cmp_int(l->level, r->level) ?:
35 bpos_cmp(l->k->k.p, r->k->k.p);
38 static inline struct btree_path_level *insert_l(struct btree_insert_entry *i)
40 return i->path->l + i->level;
43 static inline bool same_leaf_as_prev(struct btree_trans *trans,
44 struct btree_insert_entry *i)
46 return i != trans->updates &&
47 insert_l(&i[0])->b == insert_l(&i[-1])->b;
50 static inline bool same_leaf_as_next(struct btree_trans *trans,
51 struct btree_insert_entry *i)
53 return i + 1 < trans->updates + trans->nr_updates &&
54 insert_l(&i[0])->b == insert_l(&i[1])->b;
57 static inline void bch2_btree_node_prep_for_write(struct btree_trans *trans,
58 struct btree_path *path,
61 struct bch_fs *c = trans->c;
66 if (unlikely(btree_node_just_written(b)) &&
67 bch2_btree_post_write_cleanup(c, b))
68 bch2_trans_node_reinit_iter(trans, b);
71 * If the last bset has been written, or if it's gotten too big - start
72 * a new bset to insert into:
74 if (want_new_bset(c, b))
75 bch2_btree_init_next(trans, b);
78 void bch2_btree_node_lock_for_insert(struct btree_trans *trans,
79 struct btree_path *path,
82 bch2_btree_node_lock_write(trans, path, b);
83 bch2_btree_node_prep_for_write(trans, path, b);
86 /* Inserting into a given leaf node (last stage of insert): */
88 /* Handle overwrites and do insert, for non extents: */
89 bool bch2_btree_bset_insert_key(struct btree_trans *trans,
90 struct btree_path *path,
92 struct btree_node_iter *node_iter,
93 struct bkey_i *insert)
95 struct bkey_packed *k;
96 unsigned clobber_u64s = 0, new_u64s = 0;
98 EBUG_ON(btree_node_just_written(b));
99 EBUG_ON(bset_written(b, btree_bset_last(b)));
100 EBUG_ON(bkey_deleted(&insert->k) && bkey_val_u64s(&insert->k));
101 EBUG_ON(bpos_cmp(insert->k.p, b->data->min_key) < 0);
102 EBUG_ON(bpos_cmp(insert->k.p, b->data->max_key) > 0);
103 EBUG_ON(insert->k.u64s >
104 bch_btree_keys_u64s_remaining(trans->c, b));
106 k = bch2_btree_node_iter_peek_all(node_iter, b);
107 if (k && bkey_cmp_left_packed(b, k, &insert->k.p))
110 /* @k is the key being overwritten/deleted, if any: */
111 EBUG_ON(k && bkey_deleted(k));
113 /* Deleting, but not found? nothing to do: */
114 if (bkey_deleted(&insert->k) && !k)
117 if (bkey_deleted(&insert->k)) {
119 btree_account_key_drop(b, k);
120 k->type = KEY_TYPE_deleted;
122 if (k->needs_whiteout)
123 push_whiteout(trans->c, b, insert->k.p);
124 k->needs_whiteout = false;
126 if (k >= btree_bset_last(b)->start) {
127 clobber_u64s = k->u64s;
128 bch2_bset_delete(b, k, clobber_u64s);
131 bch2_btree_path_fix_key_modified(trans, b, k);
139 btree_account_key_drop(b, k);
140 k->type = KEY_TYPE_deleted;
142 insert->k.needs_whiteout = k->needs_whiteout;
143 k->needs_whiteout = false;
145 if (k >= btree_bset_last(b)->start) {
146 clobber_u64s = k->u64s;
149 bch2_btree_path_fix_key_modified(trans, b, k);
153 k = bch2_btree_node_iter_bset_pos(node_iter, b, bset_tree_last(b));
155 bch2_bset_insert(b, node_iter, k, insert, clobber_u64s);
158 if (clobber_u64s != new_u64s)
159 bch2_btree_node_iter_fix(trans, path, b, node_iter, k,
160 clobber_u64s, new_u64s);
164 static int __btree_node_flush(struct journal *j, struct journal_entry_pin *pin,
167 struct bch_fs *c = container_of(j, struct bch_fs, journal);
168 struct btree_write *w = container_of(pin, struct btree_write, journal);
169 struct btree *b = container_of(w, struct btree, writes[i]);
170 unsigned long old, new, v;
171 unsigned idx = w - b->writes;
173 six_lock_read(&b->c.lock, NULL, NULL);
174 v = READ_ONCE(b->flags);
179 if (!(old & (1 << BTREE_NODE_dirty)) ||
180 !!(old & (1 << BTREE_NODE_write_idx)) != idx ||
181 w->journal.seq != seq)
184 new |= 1 << BTREE_NODE_need_write;
185 } while ((v = cmpxchg(&b->flags, old, new)) != old);
187 btree_node_write_if_need(c, b, SIX_LOCK_read);
188 six_unlock_read(&b->c.lock);
192 static int btree_node_flush0(struct journal *j, struct journal_entry_pin *pin, u64 seq)
194 return __btree_node_flush(j, pin, 0, seq);
197 static int btree_node_flush1(struct journal *j, struct journal_entry_pin *pin, u64 seq)
199 return __btree_node_flush(j, pin, 1, seq);
202 inline void bch2_btree_add_journal_pin(struct bch_fs *c,
203 struct btree *b, u64 seq)
205 struct btree_write *w = btree_current_write(b);
207 bch2_journal_pin_add(&c->journal, seq, &w->journal,
208 btree_node_write_idx(b) == 0
210 : btree_node_flush1);
214 * btree_insert_key - insert a key one key into a leaf node
216 static void btree_insert_key_leaf(struct btree_trans *trans,
217 struct btree_insert_entry *insert)
219 struct bch_fs *c = trans->c;
220 struct btree *b = insert_l(insert)->b;
221 struct bset_tree *t = bset_tree_last(b);
222 struct bset *i = bset(b, t);
223 int old_u64s = bset_u64s(t);
224 int old_live_u64s = b->nr.live_u64s;
225 int live_u64s_added, u64s_added;
227 if (unlikely(!bch2_btree_bset_insert_key(trans, insert->path, b,
228 &insert_l(insert)->iter, insert->k)))
231 i->journal_seq = cpu_to_le64(max(trans->journal_res.seq,
232 le64_to_cpu(i->journal_seq)));
234 bch2_btree_add_journal_pin(c, b, trans->journal_res.seq);
236 if (unlikely(!btree_node_dirty(b)))
237 set_btree_node_dirty_acct(c, b);
239 live_u64s_added = (int) b->nr.live_u64s - old_live_u64s;
240 u64s_added = (int) bset_u64s(t) - old_u64s;
242 if (b->sib_u64s[0] != U16_MAX && live_u64s_added < 0)
243 b->sib_u64s[0] = max(0, (int) b->sib_u64s[0] + live_u64s_added);
244 if (b->sib_u64s[1] != U16_MAX && live_u64s_added < 0)
245 b->sib_u64s[1] = max(0, (int) b->sib_u64s[1] + live_u64s_added);
247 if (u64s_added > live_u64s_added &&
248 bch2_maybe_compact_whiteouts(c, b))
249 bch2_trans_node_reinit_iter(trans, b);
252 /* Cached btree updates: */
254 /* Normal update interface: */
256 static inline void btree_insert_entry_checks(struct btree_trans *trans,
257 struct btree_insert_entry *i)
259 BUG_ON(bpos_cmp(i->k->k.p, i->path->pos));
260 BUG_ON(i->cached != i->path->cached);
261 BUG_ON(i->level != i->path->level);
262 BUG_ON(i->btree_id != i->path->btree_id);
264 !(i->flags & BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) &&
265 test_bit(JOURNAL_REPLAY_DONE, &trans->c->journal.flags) &&
266 i->k->k.p.snapshot &&
267 bch2_snapshot_internal_node(trans->c, i->k->k.p.snapshot));
271 bch2_trans_journal_preres_get_cold(struct btree_trans *trans, unsigned u64s,
272 unsigned long trace_ip)
274 struct bch_fs *c = trans->c;
277 bch2_trans_unlock(trans);
279 ret = bch2_journal_preres_get(&c->journal,
280 &trans->journal_preres, u64s, 0);
284 if (!bch2_trans_relock(trans)) {
285 trace_trans_restart_journal_preres_get(trans->fn, trace_ip);
292 static inline int bch2_trans_journal_res_get(struct btree_trans *trans,
295 struct bch_fs *c = trans->c;
298 ret = bch2_journal_res_get(&c->journal, &trans->journal_res,
301 (trans->flags & JOURNAL_WATERMARK_MASK));
303 return ret == -EAGAIN ? BTREE_INSERT_NEED_JOURNAL_RES : ret;
306 #define JSET_ENTRY_LOG_U64s 4
308 static noinline void journal_transaction_name(struct btree_trans *trans)
310 struct bch_fs *c = trans->c;
311 struct jset_entry *entry = journal_res_entry(&c->journal, &trans->journal_res);
312 struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
313 unsigned u64s = JSET_ENTRY_LOG_U64s - 1;
314 unsigned b, buflen = u64s * sizeof(u64);
316 l->entry.u64s = cpu_to_le16(u64s);
317 l->entry.btree_id = 0;
319 l->entry.type = BCH_JSET_ENTRY_log;
323 b = min_t(unsigned, strlen(trans->fn), buflen);
324 memcpy(l->d, trans->fn, b);
328 trans->journal_res.offset += JSET_ENTRY_LOG_U64s;
329 trans->journal_res.u64s -= JSET_ENTRY_LOG_U64s;
332 static inline enum btree_insert_ret
333 btree_key_can_insert(struct btree_trans *trans,
337 struct bch_fs *c = trans->c;
339 if (!bch2_btree_node_insert_fits(c, b, u64s))
340 return BTREE_INSERT_BTREE_NODE_FULL;
342 return BTREE_INSERT_OK;
345 static enum btree_insert_ret
346 btree_key_can_insert_cached(struct btree_trans *trans,
347 struct btree_path *path,
350 struct bch_fs *c = trans->c;
351 struct bkey_cached *ck = (void *) path->l[0].b;
352 unsigned old_u64s = ck->u64s, new_u64s;
353 struct bkey_i *new_k;
355 EBUG_ON(path->level);
357 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
358 bch2_btree_key_cache_must_wait(c) &&
359 !(trans->flags & BTREE_INSERT_JOURNAL_RECLAIM))
360 return BTREE_INSERT_NEED_JOURNAL_RECLAIM;
363 * bch2_varint_decode can read past the end of the buffer by at most 7
364 * bytes (it won't be used):
368 if (u64s <= ck->u64s)
369 return BTREE_INSERT_OK;
371 new_u64s = roundup_pow_of_two(u64s);
372 new_k = krealloc(ck->k, new_u64s * sizeof(u64), GFP_NOFS);
374 bch_err(c, "error allocating memory for key cache key, btree %s u64s %u",
375 bch2_btree_ids[path->btree_id], new_u64s);
382 * Keys returned by peek() are no longer valid pointers, so we need a
383 * transaction restart:
385 trace_trans_restart_key_cache_key_realloced(trans->fn, _RET_IP_,
386 path->btree_id, &path->pos,
389 * Not using btree_trans_restart() because we can't unlock here, we have
392 trans->restarted = true;
396 static inline void do_btree_insert_one(struct btree_trans *trans,
397 struct btree_insert_entry *i)
399 struct bch_fs *c = trans->c;
400 struct journal *j = &c->journal;
402 EBUG_ON(trans->journal_res.ref !=
403 !(trans->flags & BTREE_INSERT_JOURNAL_REPLAY));
405 i->k->k.needs_whiteout = false;
408 btree_insert_key_leaf(trans, i);
410 bch2_btree_insert_key_cached(trans, i->path, i->k);
412 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
413 bch2_journal_add_keys(j, &trans->journal_res,
418 if (trans->journal_seq)
419 *trans->journal_seq = trans->journal_res.seq;
425 static int run_one_mem_trigger(struct btree_trans *trans,
426 struct btree_insert_entry *i,
429 struct bkey_s_c old = { &i->old_k, i->old_v };
430 struct bkey_i *new = i->k;
433 if (unlikely(flags & BTREE_TRIGGER_NORUN))
436 if (!btree_node_type_needs_gc(i->btree_id))
439 if (bch2_bkey_ops[old.k->type].atomic_trigger ==
440 bch2_bkey_ops[i->k->k.type].atomic_trigger &&
441 ((1U << old.k->type) & BTREE_TRIGGER_WANTS_OLD_AND_NEW)) {
442 ret = bch2_mark_key(trans, old, bkey_i_to_s_c(new),
443 BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE|flags);
445 struct bkey _deleted = KEY(0, 0, 0);
446 struct bkey_s_c deleted = (struct bkey_s_c) { &_deleted, NULL };
448 _deleted.p = i->path->pos;
450 ret = bch2_mark_key(trans, deleted, bkey_i_to_s_c(new),
451 BTREE_TRIGGER_INSERT|flags) ?:
452 bch2_mark_key(trans, old, deleted,
453 BTREE_TRIGGER_OVERWRITE|flags);
459 static int run_one_trans_trigger(struct btree_trans *trans, struct btree_insert_entry *i,
463 * Transactional triggers create new btree_insert_entries, so we can't
464 * pass them a pointer to a btree_insert_entry, that memory is going to
467 struct bkey old_k = i->old_k;
468 struct bkey_s_c old = { &old_k, i->old_v };
470 if ((i->flags & BTREE_TRIGGER_NORUN) ||
471 !(BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS & (1U << i->bkey_type)))
474 if (!i->insert_trigger_run &&
475 !i->overwrite_trigger_run &&
476 bch2_bkey_ops[old.k->type].trans_trigger ==
477 bch2_bkey_ops[i->k->k.type].trans_trigger &&
478 ((1U << old.k->type) & BTREE_TRIGGER_WANTS_OLD_AND_NEW)) {
479 i->overwrite_trigger_run = true;
480 i->insert_trigger_run = true;
481 return bch2_trans_mark_key(trans, old, i->k,
482 BTREE_TRIGGER_INSERT|
483 BTREE_TRIGGER_OVERWRITE|
485 } else if (overwrite && !i->overwrite_trigger_run) {
486 i->overwrite_trigger_run = true;
487 return bch2_trans_mark_old(trans, old, i->flags) ?: 1;
488 } else if (!i->insert_trigger_run) {
489 i->insert_trigger_run = true;
490 return bch2_trans_mark_new(trans, i->k, i->flags) ?: 1;
496 static int run_btree_triggers(struct btree_trans *trans, enum btree_id btree_id,
497 struct btree_insert_entry *btree_id_start)
499 struct btree_insert_entry *i;
500 bool trans_trigger_run;
503 for (overwrite = 1; overwrite >= 0; --overwrite) {
506 * Running triggers will append more updates to the list of updates as
510 trans_trigger_run = false;
512 for (i = btree_id_start;
513 i < trans->updates + trans->nr_updates && i->btree_id <= btree_id;
515 if (i->btree_id != btree_id)
518 ret = run_one_trans_trigger(trans, i, overwrite);
522 trans_trigger_run = true;
524 } while (trans_trigger_run);
530 static int bch2_trans_commit_run_triggers(struct btree_trans *trans)
532 struct btree_insert_entry *i = NULL, *btree_id_start = trans->updates;
533 unsigned btree_id = 0;
538 * For a given btree, this algorithm runs insert triggers before
539 * overwrite triggers: this is so that when extents are being moved
540 * (e.g. by FALLOCATE_FL_INSERT_RANGE), we don't drop references before
543 for (btree_id = 0; btree_id < BTREE_ID_NR; btree_id++) {
544 if (btree_id == BTREE_ID_alloc)
547 while (btree_id_start < trans->updates + trans->nr_updates &&
548 btree_id_start->btree_id < btree_id)
551 ret = run_btree_triggers(trans, btree_id, btree_id_start);
556 trans_for_each_update(trans, i) {
557 if (i->btree_id > BTREE_ID_alloc)
559 if (i->btree_id == BTREE_ID_alloc) {
560 ret = run_btree_triggers(trans, BTREE_ID_alloc, i);
567 trans_for_each_update(trans, i)
568 BUG_ON(!(i->flags & BTREE_TRIGGER_NORUN) &&
569 (BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS & (1U << i->bkey_type)) &&
570 (!i->insert_trigger_run || !i->overwrite_trigger_run));
575 static noinline int bch2_trans_commit_run_gc_triggers(struct btree_trans *trans)
577 struct bch_fs *c = trans->c;
578 struct btree_insert_entry *i;
581 trans_for_each_update(trans, i) {
583 * XXX: synchronization of cached update triggers with gc
584 * XXX: synchronization of interior node updates with gc
586 BUG_ON(i->cached || i->level);
588 if (gc_visited(c, gc_pos_btree_node(insert_l(i)->b))) {
589 ret = run_one_mem_trigger(trans, i, i->flags|BTREE_TRIGGER_GC);
599 bch2_trans_commit_write_locked(struct btree_trans *trans,
600 struct btree_insert_entry **stopped_at,
601 unsigned long trace_ip)
603 struct bch_fs *c = trans->c;
604 struct btree_insert_entry *i;
605 struct btree_trans_commit_hook *h;
607 bool marking = false;
611 trace_trans_restart_fault_inject(trans->fn, trace_ip);
612 trans->restarted = true;
617 * Check if the insert will fit in the leaf node with the write lock
618 * held, otherwise another thread could write the node changing the
619 * amount of space available:
622 prefetch(&trans->c->journal.flags);
626 ret = h->fn(trans, h);
632 trans_for_each_update(trans, i) {
633 /* Multiple inserts might go to same leaf: */
634 if (!same_leaf_as_prev(trans, i))
637 u64s += i->k->k.u64s;
639 ? btree_key_can_insert(trans, insert_l(i)->b, u64s)
640 : btree_key_can_insert_cached(trans, i->path, u64s);
646 if (btree_node_type_needs_gc(i->bkey_type))
650 * Revalidate before calling mem triggers - XXX, ugly:
652 * - successful btree node splits don't cause transaction
653 * restarts and will have invalidated the pointer to the bkey
655 * - btree_node_lock_for_insert() -> btree_node_prep_for_write()
656 * when it has to resort
657 * - btree_key_can_insert_cached() when it has to reallocate
659 * Ugly because we currently have no way to tell if the
660 * pointer's been invalidated, which means it's debatabale
661 * whether we should be stashing the old key at all.
663 i->old_v = bch2_btree_path_peek_slot(i->path, &i->old_k).v;
665 if (unlikely(!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))) {
667 bch2_journal_keys_peek(c, i->btree_id, i->level, i->k->k.p);
669 if (j_k && !bpos_cmp(j_k->k.p, i->k->k.p)) {
677 * Don't get journal reservation until after we know insert will
680 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
681 ret = bch2_trans_journal_res_get(trans,
682 JOURNAL_RES_GET_NONBLOCK);
686 if (unlikely(trans->journal_transaction_names))
687 journal_transaction_name(trans);
689 trans->journal_res.seq = c->journal.replay_journal_seq;
692 if (unlikely(trans->extra_journal_entries.nr)) {
693 memcpy_u64s_small(journal_res_entry(&c->journal, &trans->journal_res),
694 trans->extra_journal_entries.data,
695 trans->extra_journal_entries.nr);
697 trans->journal_res.offset += trans->extra_journal_entries.nr;
698 trans->journal_res.u64s -= trans->extra_journal_entries.nr;
702 * Not allowed to fail after we've gotten our journal reservation - we
706 if (!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY)) {
707 if (bch2_journal_seq_verify)
708 trans_for_each_update(trans, i)
709 i->k->k.version.lo = trans->journal_res.seq;
710 else if (bch2_inject_invalid_keys)
711 trans_for_each_update(trans, i)
712 i->k->k.version = MAX_VERSION;
715 if (trans->fs_usage_deltas &&
716 bch2_trans_fs_usage_apply(trans, trans->fs_usage_deltas))
717 return BTREE_INSERT_NEED_MARK_REPLICAS;
719 trans_for_each_update(trans, i)
720 if (BTREE_NODE_TYPE_HAS_MEM_TRIGGERS & (1U << i->bkey_type)) {
721 ret = run_one_mem_trigger(trans, i, i->flags);
726 if (unlikely(c->gc_pos.phase)) {
727 ret = bch2_trans_commit_run_gc_triggers(trans);
732 trans_for_each_update(trans, i)
733 do_btree_insert_one(trans, i);
738 static inline void path_upgrade_readers(struct btree_trans *trans, struct btree_path *path)
742 for (l = 0; l < BTREE_MAX_DEPTH; l++)
743 if (btree_node_read_locked(path, l))
744 BUG_ON(!bch2_btree_node_upgrade(trans, path, l));
747 static inline void upgrade_readers(struct btree_trans *trans, struct btree_path *path)
749 struct btree *b = path_l(path)->b;
752 if (path->nodes_locked &&
753 path->nodes_locked != path->nodes_intent_locked)
754 path_upgrade_readers(trans, path);
755 } while ((path = prev_btree_path(trans, path)) &&
756 path_l(path)->b == b);
760 * Check for nodes that we have both read and intent locks on, and upgrade the
763 static inline void normalize_read_intent_locks(struct btree_trans *trans)
765 struct btree_path *path;
766 unsigned i, nr_read = 0, nr_intent = 0;
768 trans_for_each_path_inorder(trans, path, i) {
769 struct btree_path *next = i + 1 < trans->nr_sorted
770 ? trans->paths + trans->sorted[i + 1]
773 if (path->nodes_locked) {
774 if (path->nodes_intent_locked)
780 if (!next || path_l(path)->b != path_l(next)->b) {
781 if (nr_read && nr_intent)
782 upgrade_readers(trans, path);
784 nr_read = nr_intent = 0;
788 bch2_trans_verify_locks(trans);
791 static inline bool have_conflicting_read_lock(struct btree_trans *trans, struct btree_path *pos)
793 struct btree_path *path;
796 trans_for_each_path_inorder(trans, path, i) {
800 if (path->nodes_locked != path->nodes_intent_locked &&
801 !bch2_btree_path_upgrade(trans, path, path->level + 1))
808 static inline int trans_lock_write(struct btree_trans *trans)
810 struct btree_insert_entry *i;
812 trans_for_each_update(trans, i) {
813 if (same_leaf_as_prev(trans, i))
816 if (!six_trylock_write(&insert_l(i)->b->c.lock)) {
817 if (have_conflicting_read_lock(trans, i->path))
820 btree_node_lock_type(trans, i->path,
822 i->path->pos, i->level,
823 SIX_LOCK_write, NULL, NULL);
826 bch2_btree_node_prep_for_write(trans, i->path, insert_l(i)->b);
831 while (--i >= trans->updates) {
832 if (same_leaf_as_prev(trans, i))
835 bch2_btree_node_unlock_write_inlined(trans, i->path, insert_l(i)->b);
838 trace_trans_restart_would_deadlock_write(trans->fn);
839 return btree_trans_restart(trans);
842 static noinline void bch2_drop_overwrites_from_journal(struct btree_trans *trans)
844 struct btree_insert_entry *i;
846 trans_for_each_update(trans, i)
847 bch2_journal_key_overwritten(trans->c, i->btree_id, i->level, i->k->k.p);
851 * Get journal reservation, take write locks, and attempt to do btree update(s):
853 static inline int do_bch2_trans_commit(struct btree_trans *trans,
854 struct btree_insert_entry **stopped_at,
855 unsigned long trace_ip)
857 struct bch_fs *c = trans->c;
858 struct btree_insert_entry *i;
859 int ret, u64s_delta = 0;
861 trans_for_each_update(trans, i) {
862 const char *invalid = bch2_bkey_invalid(c,
863 bkey_i_to_s_c(i->k), i->bkey_type);
865 struct printbuf buf = PRINTBUF;
867 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(i->k));
868 bch2_fs_fatal_error(c, "invalid bkey %s on insert from %s -> %ps: %s\n",
869 buf.buf, trans->fn, (void *) i->ip_allocated, invalid);
873 btree_insert_entry_checks(trans, i);
876 trans_for_each_update(trans, i) {
880 u64s_delta += !bkey_deleted(&i->k->k) ? i->k->k.u64s : 0;
881 u64s_delta -= i->old_btree_u64s;
883 if (!same_leaf_as_next(trans, i)) {
884 if (u64s_delta <= 0) {
885 ret = bch2_foreground_maybe_merge(trans, i->path,
886 i->level, trans->flags);
895 ret = bch2_journal_preres_get(&c->journal,
896 &trans->journal_preres, trans->journal_preres_u64s,
897 JOURNAL_RES_GET_NONBLOCK|
898 (trans->flags & JOURNAL_WATERMARK_MASK));
899 if (unlikely(ret == -EAGAIN))
900 ret = bch2_trans_journal_preres_get_cold(trans,
901 trans->journal_preres_u64s, trace_ip);
905 normalize_read_intent_locks(trans);
907 ret = trans_lock_write(trans);
911 ret = bch2_trans_commit_write_locked(trans, stopped_at, trace_ip);
913 if (!ret && unlikely(!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags)))
914 bch2_drop_overwrites_from_journal(trans);
916 trans_for_each_update(trans, i)
917 if (!same_leaf_as_prev(trans, i))
918 bch2_btree_node_unlock_write_inlined(trans, i->path,
921 if (!ret && trans->journal_pin)
922 bch2_journal_pin_add(&c->journal, trans->journal_res.seq,
923 trans->journal_pin, NULL);
926 * Drop journal reservation after dropping write locks, since dropping
927 * the journal reservation may kick off a journal write:
929 bch2_journal_res_put(&c->journal, &trans->journal_res);
934 bch2_trans_downgrade(trans);
939 static int journal_reclaim_wait_done(struct bch_fs *c)
941 int ret = bch2_journal_error(&c->journal) ?:
942 !bch2_btree_key_cache_must_wait(c);
945 journal_reclaim_kick(&c->journal);
950 int bch2_trans_commit_error(struct btree_trans *trans,
951 struct btree_insert_entry *i,
952 int ret, unsigned long trace_ip)
954 struct bch_fs *c = trans->c;
957 case BTREE_INSERT_BTREE_NODE_FULL:
958 ret = bch2_btree_split_leaf(trans, i->path, trans->flags);
963 trace_trans_restart_btree_node_split(trans->fn, trace_ip,
964 i->btree_id, &i->path->pos);
966 case BTREE_INSERT_NEED_MARK_REPLICAS:
967 bch2_trans_unlock(trans);
969 ret = bch2_replicas_delta_list_mark(c, trans->fs_usage_deltas);
973 if (bch2_trans_relock(trans))
976 trace_trans_restart_mark_replicas(trans->fn, trace_ip);
979 case BTREE_INSERT_NEED_JOURNAL_RES:
980 bch2_trans_unlock(trans);
982 if ((trans->flags & BTREE_INSERT_JOURNAL_RECLAIM) &&
983 !(trans->flags & JOURNAL_WATERMARK_reserved)) {
984 trans->restarted = true;
989 ret = bch2_trans_journal_res_get(trans, JOURNAL_RES_GET_CHECK);
993 if (bch2_trans_relock(trans))
996 trace_trans_restart_journal_res_get(trans->fn, trace_ip);
999 case BTREE_INSERT_NEED_JOURNAL_RECLAIM:
1000 bch2_trans_unlock(trans);
1002 trace_trans_blocked_journal_reclaim(trans->fn, trace_ip);
1004 wait_event_freezable(c->journal.reclaim_wait,
1005 (ret = journal_reclaim_wait_done(c)));
1009 if (bch2_trans_relock(trans))
1012 trace_trans_restart_journal_reclaim(trans->fn, trace_ip);
1020 BUG_ON((ret == EINTR || ret == -EAGAIN) && !trans->restarted);
1021 BUG_ON(ret == -ENOSPC &&
1022 !(trans->flags & BTREE_INSERT_NOWAIT) &&
1023 (trans->flags & BTREE_INSERT_NOFAIL));
1029 bch2_trans_commit_get_rw_cold(struct btree_trans *trans)
1031 struct bch_fs *c = trans->c;
1034 if (likely(!(trans->flags & BTREE_INSERT_LAZY_RW)) ||
1035 test_bit(BCH_FS_STARTED, &c->flags))
1038 bch2_trans_unlock(trans);
1040 ret = bch2_fs_read_write_early(c);
1044 if (!bch2_trans_relock(trans))
1047 percpu_ref_get(&c->writes);
1052 * This is for updates done in the early part of fsck - btree_gc - before we've
1053 * gone RW. we only add the new key to the list of keys for journal replay to
1057 do_bch2_trans_commit_to_journal_replay(struct btree_trans *trans)
1059 struct bch_fs *c = trans->c;
1060 struct btree_insert_entry *i;
1063 trans_for_each_update(trans, i) {
1064 ret = bch2_journal_key_insert(c, i->btree_id, i->level, i->k);
1072 int __bch2_trans_commit(struct btree_trans *trans)
1074 struct bch_fs *c = trans->c;
1075 struct btree_insert_entry *i = NULL;
1079 if (!trans->nr_updates &&
1080 !trans->extra_journal_entries.nr)
1083 if (trans->flags & BTREE_INSERT_GC_LOCK_HELD)
1084 lockdep_assert_held(&c->gc_lock);
1086 ret = bch2_trans_commit_run_triggers(trans);
1090 if (unlikely(!test_bit(BCH_FS_MAY_GO_RW, &c->flags))) {
1091 ret = do_bch2_trans_commit_to_journal_replay(trans);
1095 if (!(trans->flags & BTREE_INSERT_NOCHECK_RW) &&
1096 unlikely(!percpu_ref_tryget(&c->writes))) {
1097 ret = bch2_trans_commit_get_rw_cold(trans);
1102 memset(&trans->journal_preres, 0, sizeof(trans->journal_preres));
1104 trans->journal_u64s = trans->extra_journal_entries.nr;
1105 trans->journal_preres_u64s = 0;
1107 trans->journal_transaction_names = READ_ONCE(c->opts.journal_transaction_names);
1109 if (trans->journal_transaction_names)
1110 trans->journal_u64s += JSET_ENTRY_LOG_U64s;
1112 trans_for_each_update(trans, i) {
1113 BUG_ON(!i->path->should_be_locked);
1115 if (unlikely(!bch2_btree_path_upgrade(trans, i->path, i->level + 1))) {
1116 trace_trans_restart_upgrade(trans->fn, _RET_IP_,
1117 i->btree_id, &i->path->pos);
1118 ret = btree_trans_restart(trans);
1122 BUG_ON(!btree_node_intent_locked(i->path, i->level));
1124 u64s = jset_u64s(i->k->k.u64s);
1126 likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY)))
1127 trans->journal_preres_u64s += u64s;
1128 trans->journal_u64s += u64s;
1131 if (trans->extra_journal_res) {
1132 ret = bch2_disk_reservation_add(c, trans->disk_res,
1133 trans->extra_journal_res,
1134 (trans->flags & BTREE_INSERT_NOFAIL)
1135 ? BCH_DISK_RESERVATION_NOFAIL : 0);
1140 BUG_ON(trans->restarted);
1141 memset(&trans->journal_res, 0, sizeof(trans->journal_res));
1143 ret = do_bch2_trans_commit(trans, &i, _RET_IP_);
1145 /* make sure we didn't drop or screw up locks: */
1146 bch2_trans_verify_locks(trans);
1151 bch2_journal_preres_put(&c->journal, &trans->journal_preres);
1153 if (likely(!(trans->flags & BTREE_INSERT_NOCHECK_RW)))
1154 percpu_ref_put(&c->writes);
1156 trans_for_each_update(trans, i)
1157 bch2_path_put(trans, i->path, true);
1159 trans->extra_journal_res = 0;
1160 trans->nr_updates = 0;
1161 trans->hooks = NULL;
1162 trans->extra_journal_entries.nr = 0;
1164 if (trans->fs_usage_deltas) {
1165 trans->fs_usage_deltas->used = 0;
1166 memset(&trans->fs_usage_deltas->memset_start, 0,
1167 (void *) &trans->fs_usage_deltas->memset_end -
1168 (void *) &trans->fs_usage_deltas->memset_start);
1173 ret = bch2_trans_commit_error(trans, i, ret, _RET_IP_);
1180 static int check_pos_snapshot_overwritten(struct btree_trans *trans,
1184 struct bch_fs *c = trans->c;
1185 struct btree_iter iter;
1189 if (!btree_type_has_snapshots(id))
1192 if (!snapshot_t(c, pos.snapshot)->children[0])
1195 bch2_trans_iter_init(trans, &iter, id, pos,
1196 BTREE_ITER_NOT_EXTENTS|
1197 BTREE_ITER_ALL_SNAPSHOTS);
1199 k = bch2_btree_iter_prev(&iter);
1207 if (bkey_cmp(pos, k.k->p))
1210 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot)) {
1215 bch2_trans_iter_exit(trans, &iter);
1220 int bch2_trans_update_extent(struct btree_trans *trans,
1221 struct btree_iter *orig_iter,
1222 struct bkey_i *insert,
1223 enum btree_update_flags flags)
1225 struct bch_fs *c = trans->c;
1226 struct btree_iter iter, update_iter;
1227 struct bpos start = bkey_start_pos(&insert->k);
1228 struct bkey_i *update;
1230 enum btree_id btree_id = orig_iter->btree_id;
1231 int ret = 0, compressed_sectors;
1233 bch2_trans_iter_init(trans, &iter, btree_id, start,
1235 BTREE_ITER_WITH_UPDATES|
1236 BTREE_ITER_NOT_EXTENTS);
1237 k = bch2_btree_iter_peek_upto(&iter, POS(insert->k.p.inode, U64_MAX));
1238 if ((ret = bkey_err(k)))
1243 if (bch2_bkey_maybe_mergable(k.k, &insert->k)) {
1245 * We can't merge extents if they belong to interior snapshot
1246 * tree nodes, and there's a snapshot in which one extent is
1247 * visible and the other is not - i.e. if visibility is
1250 * Instead of checking if visibilitiy of the two extents is
1251 * different, for now we just check if either has been
1254 ret = check_pos_snapshot_overwritten(trans, btree_id, insert->k.p);
1260 ret = check_pos_snapshot_overwritten(trans, btree_id, k.k->p);
1266 update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1267 if ((ret = PTR_ERR_OR_ZERO(update)))
1270 bkey_reassemble(update, k);
1272 if (bch2_bkey_merge(c, bkey_i_to_s(update), bkey_i_to_s_c(insert))) {
1273 ret = bch2_btree_delete_at(trans, &iter, flags);
1283 if (!bkey_cmp(k.k->p, start))
1286 while (bkey_cmp(insert->k.p, bkey_start_pos(k.k)) > 0) {
1287 bool front_split = bkey_cmp(bkey_start_pos(k.k), start) < 0;
1288 bool back_split = bkey_cmp(k.k->p, insert->k.p) > 0;
1291 * If we're going to be splitting a compressed extent, note it
1292 * so that __bch2_trans_commit() can increase our disk
1295 if (((front_split && back_split) ||
1296 ((front_split || back_split) && k.k->p.snapshot != insert->k.p.snapshot)) &&
1297 (compressed_sectors = bch2_bkey_sectors_compressed(k)))
1298 trans->extra_journal_res += compressed_sectors;
1301 update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1302 if ((ret = PTR_ERR_OR_ZERO(update)))
1305 bkey_reassemble(update, k);
1307 bch2_cut_back(start, update);
1309 bch2_trans_iter_init(trans, &update_iter, btree_id, update->k.p,
1310 BTREE_ITER_NOT_EXTENTS|
1311 BTREE_ITER_ALL_SNAPSHOTS|
1313 ret = bch2_btree_iter_traverse(&update_iter) ?:
1314 bch2_trans_update(trans, &update_iter, update,
1315 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
1317 bch2_trans_iter_exit(trans, &update_iter);
1323 if (k.k->p.snapshot != insert->k.p.snapshot &&
1324 (front_split || back_split)) {
1325 update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1326 if ((ret = PTR_ERR_OR_ZERO(update)))
1329 bkey_reassemble(update, k);
1331 bch2_cut_front(start, update);
1332 bch2_cut_back(insert->k.p, update);
1334 bch2_trans_iter_init(trans, &update_iter, btree_id, update->k.p,
1335 BTREE_ITER_NOT_EXTENTS|
1336 BTREE_ITER_ALL_SNAPSHOTS|
1338 ret = bch2_btree_iter_traverse(&update_iter) ?:
1339 bch2_trans_update(trans, &update_iter, update,
1340 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
1342 bch2_trans_iter_exit(trans, &update_iter);
1347 if (bkey_cmp(k.k->p, insert->k.p) <= 0) {
1348 update = bch2_trans_kmalloc(trans, sizeof(*update));
1349 if ((ret = PTR_ERR_OR_ZERO(update)))
1352 bkey_init(&update->k);
1353 update->k.p = k.k->p;
1355 if (insert->k.p.snapshot != k.k->p.snapshot) {
1356 update->k.p.snapshot = insert->k.p.snapshot;
1357 update->k.type = KEY_TYPE_whiteout;
1360 bch2_trans_iter_init(trans, &update_iter, btree_id, update->k.p,
1361 BTREE_ITER_NOT_EXTENTS|
1363 ret = bch2_btree_iter_traverse(&update_iter) ?:
1364 bch2_trans_update(trans, &update_iter, update,
1365 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
1367 bch2_trans_iter_exit(trans, &update_iter);
1374 update = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1375 if ((ret = PTR_ERR_OR_ZERO(update)))
1378 bkey_reassemble(update, k);
1379 bch2_cut_front(insert->k.p, update);
1381 ret = bch2_trans_update_by_path(trans, iter.path, update,
1382 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
1389 bch2_btree_iter_advance(&iter);
1390 k = bch2_btree_iter_peek_upto(&iter, POS(insert->k.p.inode, U64_MAX));
1391 if ((ret = bkey_err(k)))
1397 if (bch2_bkey_maybe_mergable(&insert->k, k.k)) {
1398 ret = check_pos_snapshot_overwritten(trans, btree_id, insert->k.p);
1404 ret = check_pos_snapshot_overwritten(trans, btree_id, k.k->p);
1410 bch2_bkey_merge(c, bkey_i_to_s(insert), k);
1415 if (!bkey_deleted(&insert->k)) {
1417 * Rewinding iterators is expensive: get a new one and the one
1418 * that points to the start of insert will be cloned from:
1420 bch2_trans_iter_exit(trans, &iter);
1421 bch2_trans_iter_init(trans, &iter, btree_id, insert->k.p,
1422 BTREE_ITER_NOT_EXTENTS|
1424 ret = bch2_btree_iter_traverse(&iter) ?:
1425 bch2_trans_update(trans, &iter, insert, flags);
1428 bch2_trans_iter_exit(trans, &iter);
1434 * When deleting, check if we need to emit a whiteout (because we're overwriting
1435 * something in an ancestor snapshot)
1437 static int need_whiteout_for_snapshot(struct btree_trans *trans,
1438 enum btree_id btree_id, struct bpos pos)
1440 struct btree_iter iter;
1442 u32 snapshot = pos.snapshot;
1445 if (!bch2_snapshot_parent(trans->c, pos.snapshot))
1450 for_each_btree_key_norestart(trans, iter, btree_id, pos,
1451 BTREE_ITER_ALL_SNAPSHOTS|
1452 BTREE_ITER_NOPRESERVE, k, ret) {
1453 if (bkey_cmp(k.k->p, pos))
1456 if (bch2_snapshot_is_ancestor(trans->c, snapshot,
1458 ret = !bkey_whiteout(k.k);
1462 bch2_trans_iter_exit(trans, &iter);
1467 static int __must_check
1468 bch2_trans_update_by_path(struct btree_trans *trans, struct btree_path *path,
1469 struct bkey_i *k, enum btree_update_flags flags)
1471 struct bch_fs *c = trans->c;
1472 struct btree_insert_entry *i, n;
1474 BUG_ON(!path->should_be_locked);
1476 BUG_ON(trans->nr_updates >= BTREE_ITER_MAX);
1477 BUG_ON(bpos_cmp(k->k.p, path->pos));
1479 n = (struct btree_insert_entry) {
1481 .bkey_type = __btree_node_type(path->level, path->btree_id),
1482 .btree_id = path->btree_id,
1483 .level = path->level,
1484 .cached = path->cached,
1487 .ip_allocated = _RET_IP_,
1490 #ifdef CONFIG_BCACHEFS_DEBUG
1491 trans_for_each_update(trans, i)
1492 BUG_ON(i != trans->updates &&
1493 btree_insert_entry_cmp(i - 1, i) >= 0);
1497 * Pending updates are kept sorted: first, find position of new update,
1498 * then delete/trim any updates the new update overwrites:
1500 trans_for_each_update(trans, i)
1501 if (btree_insert_entry_cmp(&n, i) <= 0)
1504 if (i < trans->updates + trans->nr_updates &&
1505 !btree_insert_entry_cmp(&n, i)) {
1506 BUG_ON(i->insert_trigger_run || i->overwrite_trigger_run);
1508 bch2_path_put(trans, i->path, true);
1510 i->cached = n.cached;
1513 i->ip_allocated = n.ip_allocated;
1515 array_insert_item(trans->updates, trans->nr_updates,
1516 i - trans->updates, n);
1518 i->old_v = bch2_btree_path_peek_slot(path, &i->old_k).v;
1519 i->old_btree_u64s = !bkey_deleted(&i->old_k) ? i->old_k.u64s : 0;
1521 if (unlikely(!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags))) {
1522 struct bkey_i *j_k =
1523 bch2_journal_keys_peek(c, n.btree_id, n.level, k->k.p);
1525 if (j_k && !bpos_cmp(j_k->k.p, i->k->k.p)) {
1532 __btree_path_get(n.path, true);
1536 int __must_check bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter,
1537 struct bkey_i *k, enum btree_update_flags flags)
1539 struct btree_path *path = iter->update_path ?: iter->path;
1540 struct bkey_cached *ck;
1543 if (iter->flags & BTREE_ITER_IS_EXTENTS)
1544 return bch2_trans_update_extent(trans, iter, k, flags);
1546 if (bkey_deleted(&k->k) &&
1547 !(flags & BTREE_UPDATE_KEY_CACHE_RECLAIM) &&
1548 (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)) {
1549 ret = need_whiteout_for_snapshot(trans, iter->btree_id, k->k.p);
1550 if (unlikely(ret < 0))
1554 k->k.type = KEY_TYPE_whiteout;
1557 if (!(flags & BTREE_UPDATE_KEY_CACHE_RECLAIM) &&
1560 btree_id_cached(trans->c, path->btree_id)) {
1561 if (!iter->key_cache_path ||
1562 !iter->key_cache_path->should_be_locked ||
1563 bpos_cmp(iter->key_cache_path->pos, k->k.p)) {
1564 if (!iter->key_cache_path)
1565 iter->key_cache_path =
1566 bch2_path_get(trans, path->btree_id, path->pos, 1, 0,
1568 BTREE_ITER_CACHED, _THIS_IP_);
1570 iter->key_cache_path =
1571 bch2_btree_path_set_pos(trans, iter->key_cache_path, path->pos,
1572 iter->flags & BTREE_ITER_INTENT,
1575 ret = bch2_btree_path_traverse(trans, iter->key_cache_path,
1580 ck = (void *) iter->key_cache_path->l[0].b;
1582 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
1583 trace_trans_restart_key_cache_raced(trans->fn, _RET_IP_);
1584 btree_trans_restart(trans);
1588 iter->key_cache_path->should_be_locked = true;
1591 path = iter->key_cache_path;
1594 return bch2_trans_update_by_path(trans, path, k, flags);
1597 void bch2_trans_commit_hook(struct btree_trans *trans,
1598 struct btree_trans_commit_hook *h)
1600 h->next = trans->hooks;
1604 int __bch2_btree_insert(struct btree_trans *trans,
1605 enum btree_id id, struct bkey_i *k)
1607 struct btree_iter iter;
1610 bch2_trans_iter_init(trans, &iter, id, bkey_start_pos(&k->k),
1612 ret = bch2_btree_iter_traverse(&iter) ?:
1613 bch2_trans_update(trans, &iter, k, 0);
1614 bch2_trans_iter_exit(trans, &iter);
1619 * bch2_btree_insert - insert keys into the extent btree
1620 * @c: pointer to struct bch_fs
1621 * @id: btree to insert into
1622 * @insert_keys: list of keys to insert
1623 * @hook: insert callback
1625 int bch2_btree_insert(struct bch_fs *c, enum btree_id id,
1627 struct disk_reservation *disk_res,
1628 u64 *journal_seq, int flags)
1630 return bch2_trans_do(c, disk_res, journal_seq, flags,
1631 __bch2_btree_insert(&trans, id, k));
1634 int bch2_btree_delete_at(struct btree_trans *trans,
1635 struct btree_iter *iter, unsigned update_flags)
1639 k = bch2_trans_kmalloc(trans, sizeof(*k));
1645 return bch2_trans_update(trans, iter, k, update_flags);
1648 int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id,
1649 struct bpos start, struct bpos end,
1650 unsigned update_flags,
1653 struct btree_iter iter;
1657 bch2_trans_iter_init(trans, &iter, id, start, BTREE_ITER_INTENT);
1659 while ((bch2_trans_begin(trans),
1660 (k = bch2_btree_iter_peek(&iter)).k) &&
1661 !(ret = bkey_err(k)) &&
1662 bkey_cmp(iter.pos, end) < 0) {
1663 struct disk_reservation disk_res =
1664 bch2_disk_reservation_init(trans->c, 0);
1665 struct bkey_i delete;
1667 bkey_init(&delete.k);
1670 * This could probably be more efficient for extents:
1674 * For extents, iter.pos won't necessarily be the same as
1675 * bkey_start_pos(k.k) (for non extents they always will be the
1676 * same). It's important that we delete starting from iter.pos
1677 * because the range we want to delete could start in the middle
1680 * (bch2_btree_iter_peek() does guarantee that iter.pos >=
1681 * bkey_start_pos(k.k)).
1683 delete.k.p = iter.pos;
1685 if (iter.flags & BTREE_ITER_IS_EXTENTS) {
1686 unsigned max_sectors =
1687 KEY_SIZE_MAX & (~0 << trans->c->block_bits);
1689 /* create the biggest key we can */
1690 bch2_key_resize(&delete.k, max_sectors);
1691 bch2_cut_back(end, &delete);
1693 ret = bch2_extent_trim_atomic(trans, &iter, &delete);
1698 ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
1699 bch2_trans_commit(trans, &disk_res, journal_seq,
1700 BTREE_INSERT_NOFAIL|
1702 bch2_disk_reservation_put(trans->c, &disk_res);
1707 if (ret == -EINTR) {
1712 bch2_trans_iter_exit(trans, &iter);
1717 * bch_btree_delete_range - delete everything within a given range
1719 * Range is a half open interval - [start, end)
1721 int bch2_btree_delete_range(struct bch_fs *c, enum btree_id id,
1722 struct bpos start, struct bpos end,
1723 unsigned update_flags,
1726 return bch2_trans_do(c, NULL, journal_seq, 0,
1727 bch2_btree_delete_range_trans(&trans, id, start, end,
1728 update_flags, journal_seq));
1731 int bch2_trans_log_msg(struct btree_trans *trans, const char *msg)
1733 unsigned len = strlen(msg);
1734 unsigned u64s = DIV_ROUND_UP(len, sizeof(u64));
1735 struct jset_entry_log *l;
1738 ret = darray_make_room(trans->extra_journal_entries, jset_u64s(u64s));
1742 l = (void *) &darray_top(trans->extra_journal_entries);
1743 l->entry.u64s = cpu_to_le16(u64s);
1744 l->entry.btree_id = 0;
1746 l->entry.type = BCH_JSET_ENTRY_log;
1747 l->entry.pad[0] = 0;
1748 l->entry.pad[1] = 0;
1749 l->entry.pad[2] = 0;
1750 memcpy(l->d, msg, len);
1754 trans->extra_journal_entries.nr += jset_u64s(u64s);