1 // SPDX-License-Identifier: GPL-2.0
6 #include "btree_iter.h"
7 #include "btree_journal_iter.h"
8 #include "btree_key_cache.h"
9 #include "btree_update_interior.h"
10 #include "btree_write_buffer.h"
15 #include "journal_io.h"
16 #include "journal_reclaim.h"
20 #include <linux/prefetch.h>
22 static void verify_update_old_key(struct btree_trans *trans, struct btree_insert_entry *i)
24 #ifdef CONFIG_BCACHEFS_DEBUG
25 struct bch_fs *c = trans->c;
27 struct bkey_s_c k = bch2_btree_path_peek_slot_exact(trans->paths + i->path, &u);
29 if (unlikely(trans->journal_replay_not_finished)) {
31 bch2_journal_keys_peek_slot(c, i->btree_id, i->level, i->k->k.p);
34 k = bkey_i_to_s_c(j_k);
38 u.needs_whiteout = i->old_k.needs_whiteout;
40 BUG_ON(memcmp(&i->old_k, &u, sizeof(struct bkey)));
41 BUG_ON(i->old_v != k.v);
45 static inline struct btree_path_level *insert_l(struct btree_trans *trans, struct btree_insert_entry *i)
47 return (trans->paths + i->path)->l + i->level;
50 static inline bool same_leaf_as_prev(struct btree_trans *trans,
51 struct btree_insert_entry *i)
53 return i != trans->updates &&
54 insert_l(trans, &i[0])->b == insert_l(trans, &i[-1])->b;
57 static inline bool same_leaf_as_next(struct btree_trans *trans,
58 struct btree_insert_entry *i)
60 return i + 1 < trans->updates + trans->nr_updates &&
61 insert_l(trans, &i[0])->b == insert_l(trans, &i[1])->b;
64 inline void bch2_btree_node_prep_for_write(struct btree_trans *trans,
65 struct btree_path *path,
68 struct bch_fs *c = trans->c;
70 if (unlikely(btree_node_just_written(b)) &&
71 bch2_btree_post_write_cleanup(c, b))
72 bch2_trans_node_reinit_iter(trans, b);
75 * If the last bset has been written, or if it's gotten too big - start
76 * a new bset to insert into:
78 if (want_new_bset(c, b))
79 bch2_btree_init_next(trans, b);
82 static noinline int trans_lock_write_fail(struct btree_trans *trans, struct btree_insert_entry *i)
84 while (--i >= trans->updates) {
85 if (same_leaf_as_prev(trans, i))
88 bch2_btree_node_unlock_write(trans, trans->paths + i->path, insert_l(trans, i)->b);
91 trace_and_count(trans->c, trans_restart_would_deadlock_write, trans);
92 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock_write);
95 static inline int bch2_trans_lock_write(struct btree_trans *trans)
97 EBUG_ON(trans->write_locked);
99 trans_for_each_update(trans, i) {
100 if (same_leaf_as_prev(trans, i))
103 if (bch2_btree_node_lock_write(trans, trans->paths + i->path, &insert_l(trans, i)->b->c))
104 return trans_lock_write_fail(trans, i);
107 bch2_btree_node_prep_for_write(trans, trans->paths + i->path, insert_l(trans, i)->b);
110 trans->write_locked = true;
114 static inline void bch2_trans_unlock_write(struct btree_trans *trans)
116 if (likely(trans->write_locked)) {
117 trans_for_each_update(trans, i)
118 if (!same_leaf_as_prev(trans, i))
119 bch2_btree_node_unlock_write_inlined(trans,
120 trans->paths + i->path, insert_l(trans, i)->b);
121 trans->write_locked = false;
125 /* Inserting into a given leaf node (last stage of insert): */
127 /* Handle overwrites and do insert, for non extents: */
128 bool bch2_btree_bset_insert_key(struct btree_trans *trans,
129 struct btree_path *path,
131 struct btree_node_iter *node_iter,
132 struct bkey_i *insert)
134 struct bkey_packed *k;
135 unsigned clobber_u64s = 0, new_u64s = 0;
137 EBUG_ON(btree_node_just_written(b));
138 EBUG_ON(bset_written(b, btree_bset_last(b)));
139 EBUG_ON(bkey_deleted(&insert->k) && bkey_val_u64s(&insert->k));
140 EBUG_ON(bpos_lt(insert->k.p, b->data->min_key));
141 EBUG_ON(bpos_gt(insert->k.p, b->data->max_key));
142 EBUG_ON(insert->k.u64s >
143 bch_btree_keys_u64s_remaining(trans->c, b));
144 EBUG_ON(!b->c.level && !bpos_eq(insert->k.p, path->pos));
146 k = bch2_btree_node_iter_peek_all(node_iter, b);
147 if (k && bkey_cmp_left_packed(b, k, &insert->k.p))
150 /* @k is the key being overwritten/deleted, if any: */
151 EBUG_ON(k && bkey_deleted(k));
153 /* Deleting, but not found? nothing to do: */
154 if (bkey_deleted(&insert->k) && !k)
157 if (bkey_deleted(&insert->k)) {
159 btree_account_key_drop(b, k);
160 k->type = KEY_TYPE_deleted;
162 if (k->needs_whiteout)
163 push_whiteout(trans->c, b, insert->k.p);
164 k->needs_whiteout = false;
166 if (k >= btree_bset_last(b)->start) {
167 clobber_u64s = k->u64s;
168 bch2_bset_delete(b, k, clobber_u64s);
171 bch2_btree_path_fix_key_modified(trans, b, k);
179 btree_account_key_drop(b, k);
180 k->type = KEY_TYPE_deleted;
182 insert->k.needs_whiteout = k->needs_whiteout;
183 k->needs_whiteout = false;
185 if (k >= btree_bset_last(b)->start) {
186 clobber_u64s = k->u64s;
189 bch2_btree_path_fix_key_modified(trans, b, k);
193 k = bch2_btree_node_iter_bset_pos(node_iter, b, bset_tree_last(b));
195 bch2_bset_insert(b, node_iter, k, insert, clobber_u64s);
198 if (clobber_u64s != new_u64s)
199 bch2_btree_node_iter_fix(trans, path, b, node_iter, k,
200 clobber_u64s, new_u64s);
204 static int __btree_node_flush(struct journal *j, struct journal_entry_pin *pin,
207 struct bch_fs *c = container_of(j, struct bch_fs, journal);
208 struct btree_write *w = container_of(pin, struct btree_write, journal);
209 struct btree *b = container_of(w, struct btree, writes[i]);
210 struct btree_trans *trans = bch2_trans_get(c);
211 unsigned long old, new, v;
212 unsigned idx = w - b->writes;
214 btree_node_lock_nopath_nofail(trans, &b->c, SIX_LOCK_read);
215 v = READ_ONCE(b->flags);
220 if (!(old & (1 << BTREE_NODE_dirty)) ||
221 !!(old & (1 << BTREE_NODE_write_idx)) != idx ||
222 w->journal.seq != seq)
225 new &= ~BTREE_WRITE_TYPE_MASK;
226 new |= BTREE_WRITE_journal_reclaim;
227 new |= 1 << BTREE_NODE_need_write;
228 } while ((v = cmpxchg(&b->flags, old, new)) != old);
230 btree_node_write_if_need(c, b, SIX_LOCK_read);
231 six_unlock_read(&b->c.lock);
233 bch2_trans_put(trans);
237 int bch2_btree_node_flush0(struct journal *j, struct journal_entry_pin *pin, u64 seq)
239 return __btree_node_flush(j, pin, 0, seq);
242 int bch2_btree_node_flush1(struct journal *j, struct journal_entry_pin *pin, u64 seq)
244 return __btree_node_flush(j, pin, 1, seq);
247 inline void bch2_btree_add_journal_pin(struct bch_fs *c,
248 struct btree *b, u64 seq)
250 struct btree_write *w = btree_current_write(b);
252 bch2_journal_pin_add(&c->journal, seq, &w->journal,
253 btree_node_write_idx(b) == 0
254 ? bch2_btree_node_flush0
255 : bch2_btree_node_flush1);
259 * bch2_btree_insert_key_leaf() - insert a key one key into a leaf node
260 * @trans: btree transaction object
261 * @path: path pointing to @insert's pos
262 * @insert: key to insert
263 * @journal_seq: sequence number of journal reservation
265 inline void bch2_btree_insert_key_leaf(struct btree_trans *trans,
266 struct btree_path *path,
267 struct bkey_i *insert,
270 struct bch_fs *c = trans->c;
271 struct btree *b = path_l(path)->b;
272 struct bset_tree *t = bset_tree_last(b);
273 struct bset *i = bset(b, t);
274 int old_u64s = bset_u64s(t);
275 int old_live_u64s = b->nr.live_u64s;
276 int live_u64s_added, u64s_added;
278 if (unlikely(!bch2_btree_bset_insert_key(trans, path, b,
279 &path_l(path)->iter, insert)))
282 i->journal_seq = cpu_to_le64(max(journal_seq, le64_to_cpu(i->journal_seq)));
284 bch2_btree_add_journal_pin(c, b, journal_seq);
286 if (unlikely(!btree_node_dirty(b))) {
287 EBUG_ON(test_bit(BCH_FS_clean_shutdown, &c->flags));
288 set_btree_node_dirty_acct(c, b);
291 live_u64s_added = (int) b->nr.live_u64s - old_live_u64s;
292 u64s_added = (int) bset_u64s(t) - old_u64s;
294 if (b->sib_u64s[0] != U16_MAX && live_u64s_added < 0)
295 b->sib_u64s[0] = max(0, (int) b->sib_u64s[0] + live_u64s_added);
296 if (b->sib_u64s[1] != U16_MAX && live_u64s_added < 0)
297 b->sib_u64s[1] = max(0, (int) b->sib_u64s[1] + live_u64s_added);
299 if (u64s_added > live_u64s_added &&
300 bch2_maybe_compact_whiteouts(c, b))
301 bch2_trans_node_reinit_iter(trans, b);
304 /* Cached btree updates: */
306 /* Normal update interface: */
308 static inline void btree_insert_entry_checks(struct btree_trans *trans,
309 struct btree_insert_entry *i)
311 struct btree_path *path = trans->paths + i->path;
313 BUG_ON(!bpos_eq(i->k->k.p, path->pos));
314 BUG_ON(i->cached != path->cached);
315 BUG_ON(i->level != path->level);
316 BUG_ON(i->btree_id != path->btree_id);
318 btree_type_has_snapshots(i->btree_id) &&
319 !(i->flags & BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) &&
320 test_bit(JOURNAL_REPLAY_DONE, &trans->c->journal.flags) &&
321 i->k->k.p.snapshot &&
322 bch2_snapshot_is_internal_node(trans->c, i->k->k.p.snapshot));
325 static __always_inline int bch2_trans_journal_res_get(struct btree_trans *trans,
328 return bch2_journal_res_get(&trans->c->journal, &trans->journal_res,
329 trans->journal_u64s, flags);
332 #define JSET_ENTRY_LOG_U64s 4
334 static noinline void journal_transaction_name(struct btree_trans *trans)
336 struct bch_fs *c = trans->c;
337 struct journal *j = &c->journal;
338 struct jset_entry *entry =
339 bch2_journal_add_entry(j, &trans->journal_res,
340 BCH_JSET_ENTRY_log, 0, 0,
341 JSET_ENTRY_LOG_U64s);
342 struct jset_entry_log *l =
343 container_of(entry, struct jset_entry_log, entry);
345 strncpy(l->d, trans->fn, JSET_ENTRY_LOG_U64s * sizeof(u64));
348 static inline int btree_key_can_insert(struct btree_trans *trans,
349 struct btree *b, unsigned u64s)
351 struct bch_fs *c = trans->c;
353 if (!bch2_btree_node_insert_fits(c, b, u64s))
354 return -BCH_ERR_btree_insert_btree_node_full;
360 btree_key_can_insert_cached_slowpath(struct btree_trans *trans, unsigned flags,
361 struct btree_path *path, unsigned new_u64s)
363 struct bkey_cached *ck = (void *) path->l[0].b;
364 struct bkey_i *new_k;
367 bch2_trans_unlock_write(trans);
368 bch2_trans_unlock(trans);
370 new_k = kmalloc(new_u64s * sizeof(u64), GFP_KERNEL);
372 bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
373 bch2_btree_id_str(path->btree_id), new_u64s);
374 return -BCH_ERR_ENOMEM_btree_key_cache_insert;
377 ret = bch2_trans_relock(trans) ?:
378 bch2_trans_lock_write(trans);
384 memcpy(new_k, ck->k, ck->u64s * sizeof(u64));
386 trans_for_each_update(trans, i)
387 if (i->old_v == &ck->k->v)
388 i->old_v = &new_k->v;
396 static int btree_key_can_insert_cached(struct btree_trans *trans, unsigned flags,
397 struct btree_path *path, unsigned u64s)
399 struct bch_fs *c = trans->c;
400 struct bkey_cached *ck = (void *) path->l[0].b;
402 struct bkey_i *new_k;
404 EBUG_ON(path->level);
406 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
407 bch2_btree_key_cache_must_wait(c) &&
408 !(flags & BCH_TRANS_COMMIT_journal_reclaim))
409 return -BCH_ERR_btree_insert_need_journal_reclaim;
412 * bch2_varint_decode can read past the end of the buffer by at most 7
413 * bytes (it won't be used):
417 if (u64s <= ck->u64s)
420 new_u64s = roundup_pow_of_two(u64s);
421 new_k = krealloc(ck->k, new_u64s * sizeof(u64), GFP_NOWAIT);
422 if (unlikely(!new_k))
423 return btree_key_can_insert_cached_slowpath(trans, flags, path, new_u64s);
425 trans_for_each_update(trans, i)
426 if (i->old_v == &ck->k->v)
427 i->old_v = &new_k->v;
436 static int run_one_mem_trigger(struct btree_trans *trans,
437 struct btree_insert_entry *i,
440 struct bkey_s_c old = { &i->old_k, i->old_v };
441 struct bkey_i *new = i->k;
442 const struct bkey_ops *old_ops = bch2_bkey_type_ops(old.k->type);
443 const struct bkey_ops *new_ops = bch2_bkey_type_ops(i->k->k.type);
446 verify_update_old_key(trans, i);
448 if (unlikely(flags & BTREE_TRIGGER_NORUN))
451 if (!btree_node_type_needs_gc(__btree_node_type(i->level, i->btree_id)))
454 if (old_ops->atomic_trigger == new_ops->atomic_trigger) {
455 ret = bch2_mark_key(trans, i->btree_id, i->level,
456 old, bkey_i_to_s_c(new),
457 BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE|flags);
459 struct bkey _deleted = POS_KEY((trans->paths + i->path)->pos);
460 struct bkey_s_c deleted = (struct bkey_s_c) { &_deleted, NULL };
462 ret = bch2_mark_key(trans, i->btree_id, i->level,
463 deleted, bkey_i_to_s_c(new),
464 BTREE_TRIGGER_INSERT|flags) ?:
465 bch2_mark_key(trans, i->btree_id, i->level,
467 BTREE_TRIGGER_OVERWRITE|flags);
473 static int run_one_trans_trigger(struct btree_trans *trans, struct btree_insert_entry *i,
477 * Transactional triggers create new btree_insert_entries, so we can't
478 * pass them a pointer to a btree_insert_entry, that memory is going to
481 struct bkey old_k = i->old_k;
482 struct bkey_s_c old = { &old_k, i->old_v };
483 const struct bkey_ops *old_ops = bch2_bkey_type_ops(old.k->type);
484 const struct bkey_ops *new_ops = bch2_bkey_type_ops(i->k->k.type);
486 verify_update_old_key(trans, i);
488 if ((i->flags & BTREE_TRIGGER_NORUN) ||
489 !(BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS & (1U << i->bkey_type)))
492 if (!i->insert_trigger_run &&
493 !i->overwrite_trigger_run &&
494 old_ops->trans_trigger == new_ops->trans_trigger) {
495 i->overwrite_trigger_run = true;
496 i->insert_trigger_run = true;
497 return bch2_trans_mark_key(trans, i->btree_id, i->level, old, i->k,
498 BTREE_TRIGGER_INSERT|
499 BTREE_TRIGGER_OVERWRITE|
501 } else if (overwrite && !i->overwrite_trigger_run) {
502 i->overwrite_trigger_run = true;
503 return bch2_trans_mark_old(trans, i->btree_id, i->level, old, i->flags) ?: 1;
504 } else if (!overwrite && !i->insert_trigger_run) {
505 i->insert_trigger_run = true;
506 return bch2_trans_mark_new(trans, i->btree_id, i->level, i->k, i->flags) ?: 1;
512 static int run_btree_triggers(struct btree_trans *trans, enum btree_id btree_id,
513 struct btree_insert_entry *btree_id_start)
515 struct btree_insert_entry *i;
516 bool trans_trigger_run;
519 for (overwrite = 1; overwrite >= 0; --overwrite) {
522 * Running triggers will append more updates to the list of updates as
526 trans_trigger_run = false;
528 for (i = btree_id_start;
529 i < trans->updates + trans->nr_updates && i->btree_id <= btree_id;
531 if (i->btree_id != btree_id)
534 ret = run_one_trans_trigger(trans, i, overwrite);
538 trans_trigger_run = true;
540 } while (trans_trigger_run);
546 static int bch2_trans_commit_run_triggers(struct btree_trans *trans)
548 struct btree_insert_entry *btree_id_start = trans->updates;
549 unsigned btree_id = 0;
554 * For a given btree, this algorithm runs insert triggers before
555 * overwrite triggers: this is so that when extents are being moved
556 * (e.g. by FALLOCATE_FL_INSERT_RANGE), we don't drop references before
559 for (btree_id = 0; btree_id < BTREE_ID_NR; btree_id++) {
560 if (btree_id == BTREE_ID_alloc)
563 while (btree_id_start < trans->updates + trans->nr_updates &&
564 btree_id_start->btree_id < btree_id)
567 ret = run_btree_triggers(trans, btree_id, btree_id_start);
572 trans_for_each_update(trans, i) {
573 if (i->btree_id > BTREE_ID_alloc)
575 if (i->btree_id == BTREE_ID_alloc) {
576 ret = run_btree_triggers(trans, BTREE_ID_alloc, i);
583 #ifdef CONFIG_BCACHEFS_DEBUG
584 trans_for_each_update(trans, i)
585 BUG_ON(!(i->flags & BTREE_TRIGGER_NORUN) &&
586 (BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS & (1U << i->bkey_type)) &&
587 (!i->insert_trigger_run || !i->overwrite_trigger_run));
592 static noinline int bch2_trans_commit_run_gc_triggers(struct btree_trans *trans)
594 struct bch_fs *c = trans->c;
597 trans_for_each_update(trans, i) {
599 * XXX: synchronization of cached update triggers with gc
600 * XXX: synchronization of interior node updates with gc
602 BUG_ON(i->cached || i->level);
604 if (gc_visited(c, gc_pos_btree_node(insert_l(trans, i)->b))) {
605 ret = run_one_mem_trigger(trans, i, i->flags|BTREE_TRIGGER_GC);
615 bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
616 struct btree_insert_entry **stopped_at,
617 unsigned long trace_ip)
619 struct bch_fs *c = trans->c;
620 struct btree_trans_commit_hook *h;
625 trace_and_count(c, trans_restart_fault_inject, trans, trace_ip);
626 return btree_trans_restart_nounlock(trans, BCH_ERR_transaction_restart_fault_inject);
630 * Check if the insert will fit in the leaf node with the write lock
631 * held, otherwise another thread could write the node changing the
632 * amount of space available:
635 prefetch(&trans->c->journal.flags);
637 trans_for_each_update(trans, i) {
638 /* Multiple inserts might go to same leaf: */
639 if (!same_leaf_as_prev(trans, i))
642 u64s += i->k->k.u64s;
644 ? btree_key_can_insert(trans, insert_l(trans, i)->b, u64s)
645 : btree_key_can_insert_cached(trans, flags, trans->paths + i->path, u64s);
651 i->k->k.needs_whiteout = false;
655 * Don't get journal reservation until after we know insert will
658 if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res))) {
659 ret = bch2_trans_journal_res_get(trans,
660 (flags & BCH_WATERMARK_MASK)|
661 JOURNAL_RES_GET_NONBLOCK);
665 if (unlikely(trans->journal_transaction_names))
666 journal_transaction_name(trans);
670 * Not allowed to fail after we've gotten our journal reservation - we
674 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
675 !(flags & BCH_TRANS_COMMIT_no_journal_res)) {
676 if (bch2_journal_seq_verify)
677 trans_for_each_update(trans, i)
678 i->k->k.version.lo = trans->journal_res.seq;
679 else if (bch2_inject_invalid_keys)
680 trans_for_each_update(trans, i)
681 i->k->k.version = MAX_VERSION;
684 if (trans->fs_usage_deltas &&
685 bch2_trans_fs_usage_apply(trans, trans->fs_usage_deltas))
686 return -BCH_ERR_btree_insert_need_mark_replicas;
690 ret = h->fn(trans, h);
692 goto revert_fs_usage;
696 trans_for_each_update(trans, i)
697 if (BTREE_NODE_TYPE_HAS_MEM_TRIGGERS & (1U << i->bkey_type)) {
698 ret = run_one_mem_trigger(trans, i, i->flags);
703 if (unlikely(c->gc_pos.phase)) {
704 ret = bch2_trans_commit_run_gc_triggers(trans);
709 if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res))) {
710 struct journal *j = &c->journal;
711 struct jset_entry *entry;
713 trans_for_each_update(trans, i) {
714 if (i->key_cache_already_flushed)
717 if (i->flags & BTREE_UPDATE_NOJOURNAL)
720 verify_update_old_key(trans, i);
722 if (trans->journal_transaction_names) {
723 entry = bch2_journal_add_entry(j, &trans->journal_res,
724 BCH_JSET_ENTRY_overwrite,
725 i->btree_id, i->level,
727 bkey_reassemble((struct bkey_i *) entry->start,
728 (struct bkey_s_c) { &i->old_k, i->old_v });
731 entry = bch2_journal_add_entry(j, &trans->journal_res,
732 BCH_JSET_ENTRY_btree_keys,
733 i->btree_id, i->level,
735 bkey_copy((struct bkey_i *) entry->start, i->k);
738 memcpy_u64s_small(journal_res_entry(&c->journal, &trans->journal_res),
739 trans->journal_entries,
740 trans->journal_entries_u64s);
742 trans->journal_res.offset += trans->journal_entries_u64s;
743 trans->journal_res.u64s -= trans->journal_entries_u64s;
745 if (trans->journal_seq)
746 *trans->journal_seq = trans->journal_res.seq;
749 trans_for_each_update(trans, i) {
750 struct btree_path *path = trans->paths + i->path;
753 bch2_btree_insert_key_leaf(trans, path, i->k, trans->journal_res.seq);
754 } else if (!i->key_cache_already_flushed)
755 bch2_btree_insert_key_cached(trans, flags, i);
757 bch2_btree_key_cache_drop(trans, path);
758 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
766 if (trans->fs_usage_deltas)
767 bch2_trans_fs_usage_revert(trans, trans->fs_usage_deltas);
771 static noinline void bch2_drop_overwrites_from_journal(struct btree_trans *trans)
773 trans_for_each_update(trans, i)
774 bch2_journal_key_overwritten(trans->c, i->btree_id, i->level, i->k->k.p);
777 static noinline int bch2_trans_commit_bkey_invalid(struct btree_trans *trans,
778 enum bkey_invalid_flags flags,
779 struct btree_insert_entry *i,
780 struct printbuf *err)
782 struct bch_fs *c = trans->c;
785 prt_printf(err, "invalid bkey on insert from %s -> %ps",
786 trans->fn, (void *) i->ip_allocated);
788 printbuf_indent_add(err, 2);
790 bch2_bkey_val_to_text(err, c, bkey_i_to_s_c(i->k));
793 bch2_bkey_invalid(c, bkey_i_to_s_c(i->k), i->bkey_type, flags, err);
794 bch2_print_string_as_lines(KERN_ERR, err->buf);
796 bch2_inconsistent_error(c);
797 bch2_dump_trans_updates(trans);
802 static noinline int bch2_trans_commit_journal_entry_invalid(struct btree_trans *trans,
803 struct jset_entry *i)
805 struct bch_fs *c = trans->c;
806 struct printbuf buf = PRINTBUF;
808 prt_printf(&buf, "invalid bkey on insert from %s", trans->fn);
810 printbuf_indent_add(&buf, 2);
812 bch2_journal_entry_to_text(&buf, c, i);
815 bch2_print_string_as_lines(KERN_ERR, buf.buf);
817 bch2_inconsistent_error(c);
818 bch2_dump_trans_updates(trans);
823 static int bch2_trans_commit_journal_pin_flush(struct journal *j,
824 struct journal_entry_pin *_pin, u64 seq)
830 * Get journal reservation, take write locks, and attempt to do btree update(s):
832 static inline int do_bch2_trans_commit(struct btree_trans *trans, unsigned flags,
833 struct btree_insert_entry **stopped_at,
834 unsigned long trace_ip)
836 struct bch_fs *c = trans->c;
837 int ret = 0, u64s_delta = 0;
839 trans_for_each_update(trans, i) {
843 u64s_delta += !bkey_deleted(&i->k->k) ? i->k->k.u64s : 0;
844 u64s_delta -= i->old_btree_u64s;
846 if (!same_leaf_as_next(trans, i)) {
847 if (u64s_delta <= 0) {
848 ret = bch2_foreground_maybe_merge(trans, i->path,
858 ret = bch2_trans_lock_write(trans);
862 ret = bch2_trans_commit_write_locked(trans, flags, stopped_at, trace_ip);
864 if (!ret && unlikely(trans->journal_replay_not_finished))
865 bch2_drop_overwrites_from_journal(trans);
867 bch2_trans_unlock_write(trans);
869 if (!ret && trans->journal_pin)
870 bch2_journal_pin_add(&c->journal, trans->journal_res.seq,
872 bch2_trans_commit_journal_pin_flush);
875 * Drop journal reservation after dropping write locks, since dropping
876 * the journal reservation may kick off a journal write:
878 if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res)))
879 bch2_journal_res_put(&c->journal, &trans->journal_res);
884 static int journal_reclaim_wait_done(struct bch_fs *c)
886 int ret = bch2_journal_error(&c->journal) ?:
887 !bch2_btree_key_cache_must_wait(c);
890 journal_reclaim_kick(&c->journal);
895 int bch2_trans_commit_error(struct btree_trans *trans, unsigned flags,
896 struct btree_insert_entry *i,
897 int ret, unsigned long trace_ip)
899 struct bch_fs *c = trans->c;
902 case -BCH_ERR_btree_insert_btree_node_full:
903 ret = bch2_btree_split_leaf(trans, i->path, flags);
904 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
905 trace_and_count(c, trans_restart_btree_node_split, trans,
906 trace_ip, trans->paths + i->path);
908 case -BCH_ERR_btree_insert_need_mark_replicas:
909 ret = drop_locks_do(trans,
910 bch2_replicas_delta_list_mark(c, trans->fs_usage_deltas));
912 case -BCH_ERR_journal_res_get_blocked:
914 * XXX: this should probably be a separate BTREE_INSERT_NONBLOCK
917 if ((flags & BCH_TRANS_COMMIT_journal_reclaim) &&
918 (flags & BCH_WATERMARK_MASK) != BCH_WATERMARK_reclaim) {
919 ret = -BCH_ERR_journal_reclaim_would_deadlock;
923 ret = drop_locks_do(trans,
924 bch2_trans_journal_res_get(trans,
925 (flags & BCH_WATERMARK_MASK)|
926 JOURNAL_RES_GET_CHECK));
928 case -BCH_ERR_btree_insert_need_journal_reclaim:
929 bch2_trans_unlock(trans);
931 trace_and_count(c, trans_blocked_journal_reclaim, trans, trace_ip);
933 wait_event_freezable(c->journal.reclaim_wait,
934 (ret = journal_reclaim_wait_done(c)));
938 ret = bch2_trans_relock(trans);
945 BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart) != !!trans->restarted);
947 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOSPC) &&
948 (flags & BCH_TRANS_COMMIT_no_enospc), c,
949 "%s: incorrectly got %s\n", __func__, bch2_err_str(ret));
955 bch2_trans_commit_get_rw_cold(struct btree_trans *trans, unsigned flags)
957 struct bch_fs *c = trans->c;
960 if (likely(!(flags & BCH_TRANS_COMMIT_lazy_rw)) ||
961 test_bit(BCH_FS_started, &c->flags))
962 return -BCH_ERR_erofs_trans_commit;
964 ret = drop_locks_do(trans, bch2_fs_read_write_early(c));
968 bch2_write_ref_get(c, BCH_WRITE_REF_trans);
973 * This is for updates done in the early part of fsck - btree_gc - before we've
974 * gone RW. we only add the new key to the list of keys for journal replay to
978 do_bch2_trans_commit_to_journal_replay(struct btree_trans *trans)
980 struct bch_fs *c = trans->c;
983 trans_for_each_update(trans, i) {
984 ret = bch2_journal_key_insert(c, i->btree_id, i->level, i->k);
992 int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
994 struct btree_insert_entry *errored_at = NULL;
995 struct bch_fs *c = trans->c;
998 if (!trans->nr_updates &&
999 !trans->journal_entries_u64s)
1002 ret = bch2_trans_commit_run_triggers(trans);
1006 trans_for_each_update(trans, i) {
1007 struct printbuf buf = PRINTBUF;
1008 enum bkey_invalid_flags invalid_flags = 0;
1010 if (!(flags & BCH_TRANS_COMMIT_no_journal_res))
1011 invalid_flags |= BKEY_INVALID_WRITE|BKEY_INVALID_COMMIT;
1013 if (unlikely(bch2_bkey_invalid(c, bkey_i_to_s_c(i->k),
1014 i->bkey_type, invalid_flags, &buf)))
1015 ret = bch2_trans_commit_bkey_invalid(trans, invalid_flags, i, &buf);
1016 btree_insert_entry_checks(trans, i);
1017 printbuf_exit(&buf);
1023 for (struct jset_entry *i = trans->journal_entries;
1024 i != (void *) ((u64 *) trans->journal_entries + trans->journal_entries_u64s);
1025 i = vstruct_next(i)) {
1026 if (!jset_entry_is_key(i))
1029 enum bkey_invalid_flags invalid_flags = 0;
1031 if (!(flags & BCH_TRANS_COMMIT_no_journal_res))
1032 invalid_flags |= BKEY_INVALID_WRITE|BKEY_INVALID_COMMIT;
1034 if (unlikely(bch2_journal_entry_validate(c, NULL, i,
1035 bcachefs_metadata_version_current,
1036 CPU_BIG_ENDIAN, invalid_flags)))
1037 ret = bch2_trans_commit_journal_entry_invalid(trans, i);
1043 if (unlikely(!test_bit(BCH_FS_may_go_rw, &c->flags))) {
1044 ret = do_bch2_trans_commit_to_journal_replay(trans);
1048 if (!(flags & BCH_TRANS_COMMIT_no_check_rw) &&
1049 unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_trans))) {
1050 ret = bch2_trans_commit_get_rw_cold(trans, flags);
1055 EBUG_ON(test_bit(BCH_FS_clean_shutdown, &c->flags));
1057 trans->journal_u64s = trans->journal_entries_u64s;
1058 trans->journal_transaction_names = READ_ONCE(c->opts.journal_transaction_names);
1059 if (trans->journal_transaction_names)
1060 trans->journal_u64s += jset_u64s(JSET_ENTRY_LOG_U64s);
1062 trans_for_each_update(trans, i) {
1063 struct btree_path *path = trans->paths + i->path;
1065 EBUG_ON(!path->should_be_locked);
1067 ret = bch2_btree_path_upgrade(trans, path, i->level + 1);
1071 EBUG_ON(!btree_node_intent_locked(path, i->level));
1073 if (i->key_cache_already_flushed)
1076 if (i->flags & BTREE_UPDATE_NOJOURNAL)
1079 /* we're going to journal the key being updated: */
1080 trans->journal_u64s += jset_u64s(i->k->k.u64s);
1082 /* and we're also going to log the overwrite: */
1083 if (trans->journal_transaction_names)
1084 trans->journal_u64s += jset_u64s(i->old_k.u64s);
1087 if (trans->extra_disk_res) {
1088 ret = bch2_disk_reservation_add(c, trans->disk_res,
1089 trans->extra_disk_res,
1090 (flags & BCH_TRANS_COMMIT_no_enospc)
1091 ? BCH_DISK_RESERVATION_NOFAIL : 0);
1097 bch2_trans_verify_not_in_restart(trans);
1098 if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res)))
1099 memset(&trans->journal_res, 0, sizeof(trans->journal_res));
1101 ret = do_bch2_trans_commit(trans, flags, &errored_at, _RET_IP_);
1103 /* make sure we didn't drop or screw up locks: */
1104 bch2_trans_verify_locks(trans);
1109 trace_and_count(c, transaction_commit, trans, _RET_IP_);
1111 if (likely(!(flags & BCH_TRANS_COMMIT_no_check_rw)))
1112 bch2_write_ref_put(c, BCH_WRITE_REF_trans);
1115 bch2_trans_downgrade(trans);
1116 bch2_trans_reset_updates(trans);
1120 ret = bch2_trans_commit_error(trans, flags, errored_at, ret, _RET_IP_);
1125 * We might have done another transaction commit in the error path -
1126 * i.e. btree write buffer flush - which will have made use of
1127 * trans->journal_res, but with BCH_TRANS_COMMIT_no_journal_res that is
1128 * how the journal sequence number to pin is passed in - so we must
1131 if (flags & BCH_TRANS_COMMIT_no_journal_res) {
1132 ret = -BCH_ERR_transaction_restart_nested;