From a104f0407b7f5de54972389ef10e11dd8c525a96 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Thu, 9 Feb 2023 18:34:08 -0500 Subject: Update bcachefs sources to ea93c26e98 fixup! bcachefs: We can handle missing btree roots for all alloc btrees Signed-off-by: Kent Overstreet --- libbcachefs/btree_update_leaf.c | 276 +++++++++++++++++++++------------------- 1 file changed, 148 insertions(+), 128 deletions(-) (limited to 'libbcachefs/btree_update_leaf.c') diff --git a/libbcachefs/btree_update_leaf.c b/libbcachefs/btree_update_leaf.c index a2b37dd4..f01a2e90 100644 --- a/libbcachefs/btree_update_leaf.c +++ b/libbcachefs/btree_update_leaf.c @@ -24,12 +24,28 @@ #include #include +/* + * bch2_btree_path_peek_slot() for a cached iterator might return a key in a + * different snapshot: + */ +struct bkey_s_c bch2_btree_path_peek_slot_exact(struct btree_path *path, struct bkey *u) +{ + struct bkey_s_c k = bch2_btree_path_peek_slot(path, u); + + if (k.k && bpos_eq(path->pos, k.k->p)) + return k; + + bkey_init(u); + u->p = path->pos; + return (struct bkey_s_c) { u, NULL }; +} + static void verify_update_old_key(struct btree_trans *trans, struct btree_insert_entry *i) { #ifdef CONFIG_BCACHEFS_DEBUG struct bch_fs *c = trans->c; struct bkey u; - struct bkey_s_c k = bch2_btree_path_peek_slot(i->path, &u); + struct bkey_s_c k = bch2_btree_path_peek_slot_exact(i->path, &u); if (unlikely(trans->journal_replay_not_finished)) { struct bkey_i *j_k = @@ -314,17 +330,15 @@ bch2_trans_journal_preres_get_cold(struct btree_trans *trans, unsigned u64s, } static __always_inline int bch2_trans_journal_res_get(struct btree_trans *trans, - unsigned flags) + unsigned flags) { return bch2_journal_res_get(&trans->c->journal, &trans->journal_res, - trans->journal_u64s, - flags| - (trans->flags & JOURNAL_WATERMARK_MASK)); + trans->journal_u64s, flags); } #define JSET_ENTRY_LOG_U64s 4 -static void journal_transaction_name(struct btree_trans *trans) +static noinline void journal_transaction_name(struct btree_trans *trans) { struct bch_fs *c = trans->c; struct journal *j = &c->journal; @@ -349,9 +363,8 @@ static inline int btree_key_can_insert(struct btree_trans *trans, return 0; } -static int btree_key_can_insert_cached(struct btree_trans *trans, - struct btree_path *path, - unsigned u64s) +static int btree_key_can_insert_cached(struct btree_trans *trans, unsigned flags, + struct btree_path *path, unsigned u64s) { struct bch_fs *c = trans->c; struct bkey_cached *ck = (void *) path->l[0].b; @@ -363,7 +376,7 @@ static int btree_key_can_insert_cached(struct btree_trans *trans, if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) && bch2_btree_key_cache_must_wait(c) && - !(trans->flags & BTREE_INSERT_JOURNAL_RECLAIM)) + !(flags & BTREE_INSERT_JOURNAL_RECLAIM)) return -BCH_ERR_btree_insert_need_journal_reclaim; /* @@ -573,7 +586,7 @@ static noinline int bch2_trans_commit_run_gc_triggers(struct btree_trans *trans) } static inline int -bch2_trans_commit_write_locked(struct btree_trans *trans, +bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags, struct btree_insert_entry **stopped_at, unsigned long trace_ip) { @@ -613,7 +626,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, u64s += i->k->k.u64s; ret = !i->cached ? btree_key_can_insert(trans, insert_l(i)->b, u64s) - : btree_key_can_insert_cached(trans, i->path, u64s); + : btree_key_can_insert_cached(trans, flags, i->path, u64s); if (ret) { *stopped_at = i; return ret; @@ -627,13 +640,15 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, * Don't get journal reservation until after we know insert will * succeed: */ - if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) { + if (likely(!(flags & BTREE_INSERT_JOURNAL_REPLAY))) { ret = bch2_trans_journal_res_get(trans, + (flags & JOURNAL_WATERMARK_MASK)| JOURNAL_RES_GET_NONBLOCK); if (ret) return ret; - journal_transaction_name(trans); + if (unlikely(trans->journal_transaction_names)) + journal_transaction_name(trans); } else { trans->journal_res.seq = c->journal.replay_journal_seq; } @@ -644,7 +659,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, */ if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) && - !(trans->flags & BTREE_INSERT_JOURNAL_REPLAY)) { + !(flags & BTREE_INSERT_JOURNAL_REPLAY)) { if (bch2_journal_seq_verify) trans_for_each_update(trans, i) i->k->k.version.lo = trans->journal_res.seq; @@ -679,7 +694,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, trans->journal_res.u64s -= trans->extra_journal_entries.nr; } - if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) { + if (likely(!(flags & BTREE_INSERT_JOURNAL_REPLAY))) { trans_for_each_update(trans, i) { struct journal *j = &c->journal; struct jset_entry *entry; @@ -687,14 +702,19 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, if (i->key_cache_already_flushed) continue; + if (i->flags & BTREE_UPDATE_NOJOURNAL) + continue; + verify_update_old_key(trans, i); - entry = bch2_journal_add_entry(j, &trans->journal_res, - BCH_JSET_ENTRY_overwrite, - i->btree_id, i->level, - i->old_k.u64s); - bkey_reassemble(&entry->start[0], - (struct bkey_s_c) { &i->old_k, i->old_v }); + if (trans->journal_transaction_names) { + entry = bch2_journal_add_entry(j, &trans->journal_res, + BCH_JSET_ENTRY_overwrite, + i->btree_id, i->level, + i->old_k.u64s); + bkey_reassemble(&entry->start[0], + (struct bkey_s_c) { &i->old_k, i->old_v }); + } entry = bch2_journal_add_entry(j, &trans->journal_res, BCH_JSET_ENTRY_btree_keys, @@ -713,7 +733,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, if (!i->cached) btree_insert_key_leaf(trans, i); else if (!i->key_cache_already_flushed) - bch2_btree_insert_key_cached(trans, i->path, i->k); + bch2_btree_insert_key_cached(trans, flags, i->path, i->k); else { bch2_btree_key_cache_drop(trans, i->path); btree_path_set_dirty(i->path, BTREE_ITER_NEED_TRAVERSE); @@ -762,12 +782,12 @@ static noinline void bch2_drop_overwrites_from_journal(struct btree_trans *trans } #ifdef CONFIG_BCACHEFS_DEBUG -static noinline int bch2_trans_commit_bkey_invalid(struct btree_trans *trans, +static noinline int bch2_trans_commit_bkey_invalid(struct btree_trans *trans, unsigned flags, struct btree_insert_entry *i, struct printbuf *err) { struct bch_fs *c = trans->c; - int rw = (trans->flags & BTREE_INSERT_JOURNAL_REPLAY) ? READ : WRITE; + int rw = (flags & BTREE_INSERT_JOURNAL_REPLAY) ? READ : WRITE; printbuf_reset(err); prt_printf(err, "invalid bkey on insert from %s -> %ps", @@ -793,7 +813,7 @@ static noinline int bch2_trans_commit_bkey_invalid(struct btree_trans *trans, /* * Get journal reservation, take write locks, and attempt to do btree update(s): */ -static inline int do_bch2_trans_commit(struct btree_trans *trans, +static inline int do_bch2_trans_commit(struct btree_trans *trans, unsigned flags, struct btree_insert_entry **stopped_at, unsigned long trace_ip) { @@ -804,11 +824,11 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, #ifdef CONFIG_BCACHEFS_DEBUG trans_for_each_update(trans, i) { - int rw = (trans->flags & BTREE_INSERT_JOURNAL_REPLAY) ? READ : WRITE; + int rw = (flags & BTREE_INSERT_JOURNAL_REPLAY) ? READ : WRITE; if (unlikely(bch2_bkey_invalid(c, bkey_i_to_s_c(i->k), i->bkey_type, rw, &buf))) - return bch2_trans_commit_bkey_invalid(trans, i, &buf); + return bch2_trans_commit_bkey_invalid(trans, flags, i, &buf); btree_insert_entry_checks(trans, i); } #endif @@ -824,7 +844,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, if (!same_leaf_as_next(trans, i)) { if (u64s_delta <= 0) { ret = bch2_foreground_maybe_merge(trans, i->path, - i->level, trans->flags); + i->level, flags); if (unlikely(ret)) return ret; } @@ -835,8 +855,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, ret = bch2_journal_preres_get(&c->journal, &trans->journal_preres, trans->journal_preres_u64s, - JOURNAL_RES_GET_NONBLOCK| - (trans->flags & JOURNAL_WATERMARK_MASK)); + (flags & JOURNAL_WATERMARK_MASK)|JOURNAL_RES_GET_NONBLOCK); if (unlikely(ret == -BCH_ERR_journal_preres_get_blocked)) ret = bch2_trans_journal_preres_get_cold(trans, trans->journal_preres_u64s, trace_ip); @@ -847,7 +866,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, if (unlikely(ret)) return ret; - ret = bch2_trans_commit_write_locked(trans, stopped_at, trace_ip); + ret = bch2_trans_commit_write_locked(trans, flags, stopped_at, trace_ip); if (!ret && unlikely(trans->journal_replay_not_finished)) bch2_drop_overwrites_from_journal(trans); @@ -886,7 +905,7 @@ static int journal_reclaim_wait_done(struct bch_fs *c) } static noinline -int bch2_trans_commit_error(struct btree_trans *trans, +int bch2_trans_commit_error(struct btree_trans *trans, unsigned flags, struct btree_insert_entry *i, int ret, unsigned long trace_ip) { @@ -894,7 +913,7 @@ int bch2_trans_commit_error(struct btree_trans *trans, switch (ret) { case -BCH_ERR_btree_insert_btree_node_full: - ret = bch2_btree_split_leaf(trans, i->path, trans->flags); + ret = bch2_btree_split_leaf(trans, i->path, flags); if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) trace_and_count(c, trans_restart_btree_node_split, trans, trace_ip, i->path); break; @@ -912,8 +931,8 @@ int bch2_trans_commit_error(struct btree_trans *trans, case -BCH_ERR_journal_res_get_blocked: bch2_trans_unlock(trans); - if ((trans->flags & BTREE_INSERT_JOURNAL_RECLAIM) && - !(trans->flags & JOURNAL_WATERMARK_reserved)) { + if ((flags & BTREE_INSERT_JOURNAL_RECLAIM) && + !(flags & JOURNAL_WATERMARK_reserved)) { ret = -BCH_ERR_journal_reclaim_would_deadlock; break; } @@ -948,20 +967,20 @@ int bch2_trans_commit_error(struct btree_trans *trans, BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart) != !!trans->restarted); bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOSPC) && - !(trans->flags & BTREE_INSERT_NOWAIT) && - (trans->flags & BTREE_INSERT_NOFAIL), c, + !(flags & BTREE_INSERT_NOWAIT) && + (flags & BTREE_INSERT_NOFAIL), c, "%s: incorrectly got %s\n", __func__, bch2_err_str(ret)); return ret; } static noinline int -bch2_trans_commit_get_rw_cold(struct btree_trans *trans) +bch2_trans_commit_get_rw_cold(struct btree_trans *trans, unsigned flags) { struct bch_fs *c = trans->c; int ret; - if (likely(!(trans->flags & BTREE_INSERT_LAZY_RW)) || + if (likely(!(flags & BTREE_INSERT_LAZY_RW)) || test_bit(BCH_FS_STARTED, &c->flags)) return -BCH_ERR_erofs_trans_commit; @@ -972,7 +991,7 @@ bch2_trans_commit_get_rw_cold(struct btree_trans *trans) if (ret) return ret; - percpu_ref_get(&c->writes); + bch2_write_ref_get(c, BCH_WRITE_REF_trans); return 0; } @@ -997,7 +1016,7 @@ do_bch2_trans_commit_to_journal_replay(struct btree_trans *trans) return ret; } -int __bch2_trans_commit(struct btree_trans *trans) +int __bch2_trans_commit(struct btree_trans *trans, unsigned flags) { struct bch_fs *c = trans->c; struct btree_insert_entry *i = NULL; @@ -1008,7 +1027,7 @@ int __bch2_trans_commit(struct btree_trans *trans) !trans->extra_journal_entries.nr) goto out_reset; - if (trans->flags & BTREE_INSERT_GC_LOCK_HELD) + if (flags & BTREE_INSERT_GC_LOCK_HELD) lockdep_assert_held(&c->gc_lock); ret = bch2_trans_commit_run_triggers(trans); @@ -1020,9 +1039,9 @@ int __bch2_trans_commit(struct btree_trans *trans) goto out_reset; } - if (!(trans->flags & BTREE_INSERT_NOCHECK_RW) && - unlikely(!percpu_ref_tryget_live(&c->writes))) { - ret = bch2_trans_commit_get_rw_cold(trans); + if (!(flags & BTREE_INSERT_NOCHECK_RW) && + unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_trans))) { + ret = bch2_trans_commit_get_rw_cold(trans, flags); if (ret) goto out_reset; } @@ -1034,8 +1053,10 @@ int __bch2_trans_commit(struct btree_trans *trans) trans->journal_u64s = trans->extra_journal_entries.nr; trans->journal_preres_u64s = 0; - /* For journalling transaction name: */ - trans->journal_u64s += jset_u64s(JSET_ENTRY_LOG_U64s); + trans->journal_transaction_names = READ_ONCE(c->opts.journal_transaction_names); + + if (trans->journal_transaction_names) + trans->journal_u64s += jset_u64s(JSET_ENTRY_LOG_U64s); trans_for_each_update(trans, i) { EBUG_ON(!i->path->should_be_locked); @@ -1052,27 +1073,32 @@ int __bch2_trans_commit(struct btree_trans *trans) /* we're going to journal the key being updated: */ u64s = jset_u64s(i->k->k.u64s); if (i->cached && - likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) + likely(!(flags & BTREE_INSERT_JOURNAL_REPLAY))) trans->journal_preres_u64s += u64s; + + if (i->flags & BTREE_UPDATE_NOJOURNAL) + continue; + trans->journal_u64s += u64s; /* and we're also going to log the overwrite: */ - trans->journal_u64s += jset_u64s(i->old_k.u64s); + if (trans->journal_transaction_names) + trans->journal_u64s += jset_u64s(i->old_k.u64s); } if (trans->extra_journal_res) { ret = bch2_disk_reservation_add(c, trans->disk_res, trans->extra_journal_res, - (trans->flags & BTREE_INSERT_NOFAIL) + (flags & BTREE_INSERT_NOFAIL) ? BCH_DISK_RESERVATION_NOFAIL : 0); if (ret) goto err; } retry: - EBUG_ON(trans->restarted); + bch2_trans_verify_not_in_restart(trans); memset(&trans->journal_res, 0, sizeof(trans->journal_res)); - ret = do_bch2_trans_commit(trans, &i, _RET_IP_); + ret = do_bch2_trans_commit(trans, flags, &i, _RET_IP_); /* make sure we didn't drop or screw up locks: */ bch2_trans_verify_locks(trans); @@ -1084,22 +1110,14 @@ retry: out: bch2_journal_preres_put(&c->journal, &trans->journal_preres); - if (likely(!(trans->flags & BTREE_INSERT_NOCHECK_RW))) - percpu_ref_put(&c->writes); + if (likely(!(flags & BTREE_INSERT_NOCHECK_RW))) + bch2_write_ref_put(c, BCH_WRITE_REF_trans); out_reset: bch2_trans_reset_updates(trans); - if (trans->fs_usage_deltas) { - trans->fs_usage_deltas->used = 0; - memset((void *) trans->fs_usage_deltas + - offsetof(struct replicas_delta_list, memset_start), 0, - (void *) &trans->fs_usage_deltas->memset_end - - (void *) &trans->fs_usage_deltas->memset_start); - } - return ret; err: - ret = bch2_trans_commit_error(trans, i, ret, _RET_IP_); + ret = bch2_trans_commit_error(trans, flags, i, ret, _RET_IP_); if (ret) goto out; @@ -1152,12 +1170,63 @@ static inline int check_pos_snapshot_overwritten(struct btree_trans *trans, return __check_pos_snapshot_overwritten(trans, id, pos); } +static noinline int extent_front_merge(struct btree_trans *trans, + struct btree_iter *iter, + struct bkey_s_c k, + struct bkey_i **insert, + enum btree_update_flags flags) +{ + struct bch_fs *c = trans->c; + struct bkey_i *update; + int ret; + + update = bch2_bkey_make_mut(trans, k); + ret = PTR_ERR_OR_ZERO(update); + if (ret) + return ret; + + if (!bch2_bkey_merge(c, bkey_i_to_s(update), bkey_i_to_s_c(*insert))) + return 0; + + ret = check_pos_snapshot_overwritten(trans, iter->btree_id, k.k->p) ?: + check_pos_snapshot_overwritten(trans, iter->btree_id, (*insert)->k.p); + if (ret < 0) + return ret; + if (ret) + return 0; + + ret = bch2_btree_delete_at(trans, iter, flags); + if (ret) + return ret; + + *insert = update; + return 0; +} + +static noinline int extent_back_merge(struct btree_trans *trans, + struct btree_iter *iter, + struct bkey_i *insert, + struct bkey_s_c k) +{ + struct bch_fs *c = trans->c; + int ret; + + ret = check_pos_snapshot_overwritten(trans, iter->btree_id, insert->k.p) ?: + check_pos_snapshot_overwritten(trans, iter->btree_id, k.k->p); + if (ret < 0) + return ret; + if (ret) + return 0; + + bch2_bkey_merge(c, bkey_i_to_s(insert), k); + return 0; +} + int bch2_trans_update_extent(struct btree_trans *trans, struct btree_iter *orig_iter, struct bkey_i *insert, enum btree_update_flags flags) { - struct bch_fs *c = trans->c; struct btree_iter iter, update_iter; struct bpos start = bkey_start_pos(&insert->k); struct bkey_i *update; @@ -1175,46 +1244,15 @@ int bch2_trans_update_extent(struct btree_trans *trans, if (!k.k) goto out; - if (bch2_bkey_maybe_mergable(k.k, &insert->k)) { - /* - * We can't merge extents if they belong to interior snapshot - * tree nodes, and there's a snapshot in which one extent is - * visible and the other is not - i.e. if visibility is - * different. - * - * Instead of checking if visibilitiy of the two extents is - * different, for now we just check if either has been - * overwritten: - */ - ret = check_pos_snapshot_overwritten(trans, btree_id, insert->k.p); - if (ret < 0) - goto err; - if (ret) - goto nomerge1; - - ret = check_pos_snapshot_overwritten(trans, btree_id, k.k->p); - if (ret < 0) - goto err; - if (ret) - goto nomerge1; - - update = bch2_bkey_make_mut(trans, k); - if ((ret = PTR_ERR_OR_ZERO(update))) - goto err; - - if (bch2_bkey_merge(c, bkey_i_to_s(update), bkey_i_to_s_c(insert))) { - ret = bch2_btree_delete_at(trans, &iter, flags); + if (bkey_eq(k.k->p, bkey_start_pos(&insert->k))) { + if (bch2_bkey_maybe_mergable(k.k, &insert->k)) { + ret = extent_front_merge(trans, &iter, k, &insert, flags); if (ret) goto err; - - insert = update; - goto next; } - } -nomerge1: - ret = 0; - if (bkey_eq(k.k->p, start)) + goto next; + } while (bkey_gt(insert->k.p, bkey_start_pos(k.k))) { bool front_split = bkey_lt(bkey_start_pos(k.k), start); @@ -1323,22 +1361,10 @@ next: } if (bch2_bkey_maybe_mergable(&insert->k, k.k)) { - ret = check_pos_snapshot_overwritten(trans, btree_id, insert->k.p); - if (ret < 0) - goto err; + ret = extent_back_merge(trans, &iter, insert, k); if (ret) - goto nomerge2; - - ret = check_pos_snapshot_overwritten(trans, btree_id, k.k->p); - if (ret < 0) goto err; - if (ret) - goto nomerge2; - - bch2_bkey_merge(c, bkey_i_to_s(insert), k); } -nomerge2: - ret = 0; out: if (!bkey_deleted(&insert->k)) { /* @@ -1476,7 +1502,7 @@ bch2_trans_update_by_path_trace(struct btree_trans *trans, struct btree_path *pa array_insert_item(trans->updates, trans->nr_updates, i - trans->updates, n); - i->old_v = bch2_btree_path_peek_slot(path, &i->old_k).v; + i->old_v = bch2_btree_path_peek_slot_exact(path, &i->old_k).v; i->old_btree_u64s = !bkey_deleted(&i->old_k) ? i->old_k.u64s : 0; if (unlikely(trans->journal_replay_not_finished)) { @@ -1499,7 +1525,9 @@ bch2_trans_update_by_path_trace(struct btree_trans *trans, struct btree_path *pa * the key cache - but the key has to exist in the btree for that to * work: */ - if (unlikely(path->cached && bkey_deleted(&i->old_k))) + if (path->cached && + bkey_deleted(&i->old_k) && + !(flags & BTREE_UPDATE_NO_KEY_CACHE_COHERENCY)) return flush_new_cached_update(trans, path, i, flags, ip); return 0; @@ -1671,18 +1699,10 @@ int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id, */ delete.k.p = iter.pos; - if (iter.flags & BTREE_ITER_IS_EXTENTS) { - unsigned max_sectors = - KEY_SIZE_MAX & (~0 << trans->c->block_bits); - - /* create the biggest key we can */ - bch2_key_resize(&delete.k, max_sectors); - bch2_cut_back(end, &delete); - - ret = bch2_extent_trim_atomic(trans, &iter, &delete); - if (ret) - goto err; - } + if (iter.flags & BTREE_ITER_IS_EXTENTS) + bch2_key_resize(&delete.k, + bpos_min(end, k.k->p).offset - + iter.pos.offset); ret = bch2_trans_update(trans, &iter, &delete, update_flags) ?: bch2_trans_commit(trans, &disk_res, journal_seq, -- cgit v1.2.3