]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_trans_commit.c
Update bcachefs sources to 3ca08ab51ec9 bcachefs: six locks: Simplify optimistic...
[bcachefs-tools-debian] / libbcachefs / btree_trans_commit.c
index 32693f7c6221043d0c28b07e57f1b396bd845582..70077efae7889bb143195821b0d039d2dca64a89 100644 (file)
@@ -78,6 +78,53 @@ inline void bch2_btree_node_prep_for_write(struct btree_trans *trans,
                bch2_btree_init_next(trans, b);
 }
 
+static noinline int trans_lock_write_fail(struct btree_trans *trans, struct btree_insert_entry *i)
+{
+       while (--i >= trans->updates) {
+               if (same_leaf_as_prev(trans, i))
+                       continue;
+
+               bch2_btree_node_unlock_write(trans, i->path, insert_l(i)->b);
+       }
+
+       trace_and_count(trans->c, trans_restart_would_deadlock_write, trans);
+       return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock_write);
+}
+
+static inline int bch2_trans_lock_write(struct btree_trans *trans)
+{
+       struct btree_insert_entry *i;
+
+       EBUG_ON(trans->write_locked);
+
+       trans_for_each_update(trans, i) {
+               if (same_leaf_as_prev(trans, i))
+                       continue;
+
+               if (bch2_btree_node_lock_write(trans, i->path, &insert_l(i)->b->c))
+                       return trans_lock_write_fail(trans, i);
+
+               if (!i->cached)
+                       bch2_btree_node_prep_for_write(trans, i->path, insert_l(i)->b);
+       }
+
+       trans->write_locked = true;
+       return 0;
+}
+
+static inline void bch2_trans_unlock_write(struct btree_trans *trans)
+{
+       if (likely(trans->write_locked)) {
+               struct btree_insert_entry *i;
+
+               trans_for_each_update(trans, i)
+                       if (!same_leaf_as_prev(trans, i))
+                               bch2_btree_node_unlock_write_inlined(trans, i->path,
+                                                                    insert_l(i)->b);
+               trans->write_locked = false;
+       }
+}
+
 /* Inserting into a given leaf node (last stage of insert): */
 
 /* Handle overwrites and do insert, for non extents: */
@@ -269,23 +316,13 @@ static inline void btree_insert_entry_checks(struct btree_trans *trans,
        BUG_ON(i->level         != i->path->level);
        BUG_ON(i->btree_id      != i->path->btree_id);
        EBUG_ON(!i->level &&
+               btree_type_has_snapshots(i->btree_id) &&
                !(i->flags & BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) &&
                test_bit(JOURNAL_REPLAY_DONE, &trans->c->journal.flags) &&
                i->k->k.p.snapshot &&
                bch2_snapshot_is_internal_node(trans->c, i->k->k.p.snapshot));
 }
 
-static noinline int
-bch2_trans_journal_preres_get_cold(struct btree_trans *trans, unsigned flags,
-                                  unsigned long trace_ip)
-{
-       return drop_locks_do(trans,
-               bch2_journal_preres_get(&trans->c->journal,
-                       &trans->journal_preres,
-                       trans->journal_preres_u64s,
-                       (flags & BCH_WATERMARK_MASK)));
-}
-
 static __always_inline int bch2_trans_journal_res_get(struct btree_trans *trans,
                                                      unsigned flags)
 {
@@ -320,6 +357,45 @@ static inline int btree_key_can_insert(struct btree_trans *trans,
        return 0;
 }
 
+noinline static int
+btree_key_can_insert_cached_slowpath(struct btree_trans *trans, unsigned flags,
+                                    struct btree_path *path, unsigned new_u64s)
+{
+       struct bch_fs *c = trans->c;
+       struct btree_insert_entry *i;
+       struct bkey_cached *ck = (void *) path->l[0].b;
+       struct bkey_i *new_k;
+       int ret;
+
+       bch2_trans_unlock_write(trans);
+       bch2_trans_unlock(trans);
+
+       new_k = kmalloc(new_u64s * sizeof(u64), GFP_KERNEL);
+       if (!new_k) {
+               bch_err(c, "error allocating memory for key cache key, btree %s u64s %u",
+                       bch2_btree_id_str(path->btree_id), new_u64s);
+               return -BCH_ERR_ENOMEM_btree_key_cache_insert;
+       }
+
+       ret =   bch2_trans_relock(trans) ?:
+               bch2_trans_lock_write(trans);
+       if (unlikely(ret)) {
+               kfree(new_k);
+               return ret;
+       }
+
+       memcpy(new_k, ck->k, ck->u64s * sizeof(u64));
+
+       trans_for_each_update(trans, i)
+               if (i->old_v == &ck->k->v)
+                       i->old_v = &new_k->v;
+
+       kfree(ck->k);
+       ck->u64s        = new_u64s;
+       ck->k           = new_k;
+       return 0;
+}
+
 static int btree_key_can_insert_cached(struct btree_trans *trans, unsigned flags,
                                       struct btree_path *path, unsigned u64s)
 {
@@ -333,7 +409,7 @@ static int btree_key_can_insert_cached(struct btree_trans *trans, unsigned flags
 
        if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
            bch2_btree_key_cache_must_wait(c) &&
-           !(flags & BTREE_INSERT_JOURNAL_RECLAIM))
+           !(flags & BCH_TRANS_COMMIT_journal_reclaim))
                return -BCH_ERR_btree_insert_need_journal_reclaim;
 
        /*
@@ -346,12 +422,9 @@ static int btree_key_can_insert_cached(struct btree_trans *trans, unsigned flags
                return 0;
 
        new_u64s        = roundup_pow_of_two(u64s);
-       new_k           = krealloc(ck->k, new_u64s * sizeof(u64), GFP_NOFS);
-       if (!new_k) {
-               bch_err(c, "error allocating memory for key cache key, btree %s u64s %u",
-                       bch2_btree_id_str(path->btree_id), new_u64s);
-               return -BCH_ERR_ENOMEM_btree_key_cache_insert;
-       }
+       new_k           = krealloc(ck->k, new_u64s * sizeof(u64), GFP_NOWAIT);
+       if (unlikely(!new_k))
+               return btree_key_can_insert_cached_slowpath(trans, flags, path, new_u64s);
 
        trans_for_each_update(trans, i)
                if (i->old_v == &ck->k->v)
@@ -583,6 +656,8 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
                        *stopped_at = i;
                        return ret;
                }
+
+               i->k->k.needs_whiteout = false;
        }
 
        if (trans->nr_wb_updates &&
@@ -593,7 +668,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
         * Don't get journal reservation until after we know insert will
         * succeed:
         */
-       if (likely(!(flags & BTREE_INSERT_JOURNAL_REPLAY))) {
+       if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res))) {
                ret = bch2_trans_journal_res_get(trans,
                                (flags & BCH_WATERMARK_MASK)|
                                JOURNAL_RES_GET_NONBLOCK);
@@ -602,8 +677,6 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
 
                if (unlikely(trans->journal_transaction_names))
                        journal_transaction_name(trans);
-       } else {
-               trans->journal_res.seq = c->journal.replay_journal_seq;
        }
 
        /*
@@ -612,7 +685,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
         */
 
        if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
-           !(flags & BTREE_INSERT_JOURNAL_REPLAY)) {
+           !(flags & BCH_TRANS_COMMIT_no_journal_res)) {
                if (bch2_journal_seq_verify)
                        trans_for_each_update(trans, i)
                                i->k->k.version.lo = trans->journal_res.seq;
@@ -626,7 +699,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
                return -BCH_ERR_btree_insert_need_mark_replicas;
 
        if (trans->nr_wb_updates) {
-               EBUG_ON(flags & BTREE_INSERT_JOURNAL_REPLAY);
+               EBUG_ON(flags & BCH_TRANS_COMMIT_no_journal_res);
 
                ret = bch2_btree_insert_keys_write_buffer(trans);
                if (ret)
@@ -663,7 +736,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
                trans->journal_res.u64s         -= trans->extra_journal_entries.nr;
        }
 
-       if (likely(!(flags & BTREE_INSERT_JOURNAL_REPLAY))) {
+       if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res))) {
                struct journal *j = &c->journal;
                struct jset_entry *entry;
 
@@ -705,15 +778,8 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
        }
 
        trans_for_each_update(trans, i) {
-               i->k->k.needs_whiteout = false;
-
                if (!i->cached) {
-                       u64 seq = trans->journal_res.seq;
-
-                       if (i->flags & BTREE_UPDATE_PREJOURNAL)
-                               seq = i->seq;
-
-                       bch2_btree_insert_key_leaf(trans, i->path, i->k, seq);
+                       bch2_btree_insert_key_leaf(trans, i->path, i->k, trans->journal_res.seq);
                } else if (!i->key_cache_already_flushed)
                        bch2_btree_insert_key_cached(trans, flags, i);
                else {
@@ -731,37 +797,6 @@ revert_fs_usage:
        return ret;
 }
 
-static noinline int trans_lock_write_fail(struct btree_trans *trans, struct btree_insert_entry *i)
-{
-       while (--i >= trans->updates) {
-               if (same_leaf_as_prev(trans, i))
-                       continue;
-
-               bch2_btree_node_unlock_write(trans, i->path, insert_l(i)->b);
-       }
-
-       trace_and_count(trans->c, trans_restart_would_deadlock_write, trans);
-       return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock_write);
-}
-
-static inline int trans_lock_write(struct btree_trans *trans)
-{
-       struct btree_insert_entry *i;
-
-       trans_for_each_update(trans, i) {
-               if (same_leaf_as_prev(trans, i))
-                       continue;
-
-               if (bch2_btree_node_lock_write(trans, i->path, &insert_l(i)->b->c))
-                       return trans_lock_write_fail(trans, i);
-
-               if (!i->cached)
-                       bch2_btree_node_prep_for_write(trans, i->path, insert_l(i)->b);
-       }
-
-       return 0;
-}
-
 static noinline void bch2_drop_overwrites_from_journal(struct btree_trans *trans)
 {
        struct btree_insert_entry *i;
@@ -799,6 +834,12 @@ static noinline int bch2_trans_commit_bkey_invalid(struct btree_trans *trans,
        return -EINVAL;
 }
 
+static int bch2_trans_commit_journal_pin_flush(struct journal *j,
+                               struct journal_entry_pin *_pin, u64 seq)
+{
+       return 0;
+}
+
 /*
  * Get journal reservation, take write locks, and attempt to do btree update(s):
  */
@@ -829,15 +870,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, unsigned flags
                }
        }
 
-       ret = bch2_journal_preres_get(&c->journal,
-                       &trans->journal_preres, trans->journal_preres_u64s,
-                       (flags & BCH_WATERMARK_MASK)|JOURNAL_RES_GET_NONBLOCK);
-       if (unlikely(ret == -BCH_ERR_journal_preres_get_blocked))
-               ret = bch2_trans_journal_preres_get_cold(trans, flags, trace_ip);
-       if (unlikely(ret))
-               return ret;
-
-       ret = trans_lock_write(trans);
+       ret = bch2_trans_lock_write(trans);
        if (unlikely(ret))
                return ret;
 
@@ -846,20 +879,19 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, unsigned flags
        if (!ret && unlikely(trans->journal_replay_not_finished))
                bch2_drop_overwrites_from_journal(trans);
 
-       trans_for_each_update(trans, i)
-               if (!same_leaf_as_prev(trans, i))
-                       bch2_btree_node_unlock_write_inlined(trans, i->path,
-                                                       insert_l(i)->b);
+       bch2_trans_unlock_write(trans);
 
        if (!ret && trans->journal_pin)
                bch2_journal_pin_add(&c->journal, trans->journal_res.seq,
-                                    trans->journal_pin, NULL);
+                                    trans->journal_pin,
+                                    bch2_trans_commit_journal_pin_flush);
 
        /*
         * Drop journal reservation after dropping write locks, since dropping
         * the journal reservation may kick off a journal write:
         */
-       bch2_journal_res_put(&c->journal, &trans->journal_res);
+       if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res)))
+               bch2_journal_res_put(&c->journal, &trans->journal_res);
 
        return ret;
 }
@@ -896,7 +928,7 @@ int bch2_trans_commit_error(struct btree_trans *trans, unsigned flags,
                 * XXX: this should probably be a separate BTREE_INSERT_NONBLOCK
                 * flag
                 */
-               if ((flags & BTREE_INSERT_JOURNAL_RECLAIM) &&
+               if ((flags & BCH_TRANS_COMMIT_journal_reclaim) &&
                    (flags & BCH_WATERMARK_MASK) != BCH_WATERMARK_reclaim) {
                        ret = -BCH_ERR_journal_reclaim_would_deadlock;
                        break;
@@ -931,7 +963,7 @@ int bch2_trans_commit_error(struct btree_trans *trans, unsigned flags,
                        if (wb->state.nr > wb->size * 3 / 4) {
                                bch2_trans_begin(trans);
                                ret = __bch2_btree_write_buffer_flush(trans,
-                                               flags|BTREE_INSERT_NOCHECK_RW, true);
+                                               flags|BCH_TRANS_COMMIT_no_check_rw, true);
                                if (!ret) {
                                        trace_and_count(c, trans_restart_write_buffer_flush, trans, _THIS_IP_);
                                        ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_write_buffer_flush);
@@ -951,8 +983,7 @@ int bch2_trans_commit_error(struct btree_trans *trans, unsigned flags,
        BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart) != !!trans->restarted);
 
        bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOSPC) &&
-                               !(flags & BTREE_INSERT_NOWAIT) &&
-                               (flags & BTREE_INSERT_NOFAIL), c,
+                               (flags & BCH_TRANS_COMMIT_no_enospc), c,
                "%s: incorrectly got %s\n", __func__, bch2_err_str(ret));
 
        return ret;
@@ -964,7 +995,7 @@ bch2_trans_commit_get_rw_cold(struct btree_trans *trans, unsigned flags)
        struct bch_fs *c = trans->c;
        int ret;
 
-       if (likely(!(flags & BTREE_INSERT_LAZY_RW)) ||
+       if (likely(!(flags & BCH_TRANS_COMMIT_lazy_rw)) ||
            test_bit(BCH_FS_STARTED, &c->flags))
                return -BCH_ERR_erofs_trans_commit;
 
@@ -1002,7 +1033,6 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
        struct bch_fs *c = trans->c;
        struct btree_insert_entry *i = NULL;
        struct btree_write_buffered_key *wb;
-       unsigned u64s;
        int ret = 0;
 
        if (!trans->nr_updates &&
@@ -1010,9 +1040,6 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
            !trans->extra_journal_entries.nr)
                goto out_reset;
 
-       if (flags & BTREE_INSERT_GC_LOCK_HELD)
-               lockdep_assert_held(&c->gc_lock);
-
        ret = bch2_trans_commit_run_triggers(trans);
        if (ret)
                goto out_reset;
@@ -1021,7 +1048,7 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
                struct printbuf buf = PRINTBUF;
                enum bkey_invalid_flags invalid_flags = 0;
 
-               if (!(flags & BTREE_INSERT_JOURNAL_REPLAY))
+               if (!(flags & BCH_TRANS_COMMIT_no_journal_res))
                        invalid_flags |= BKEY_INVALID_WRITE|BKEY_INVALID_COMMIT;
 
                if (unlikely(bch2_bkey_invalid(c, bkey_i_to_s_c(i->k),
@@ -1039,7 +1066,7 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
                goto out_reset;
        }
 
-       if (!(flags & BTREE_INSERT_NOCHECK_RW) &&
+       if (!(flags & BCH_TRANS_COMMIT_no_check_rw) &&
            unlikely(!bch2_write_ref_tryget(c, BCH_WRITE_REF_trans))) {
                ret = bch2_trans_commit_get_rw_cold(trans, flags);
                if (ret)
@@ -1052,7 +1079,7 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
                bch2_trans_unlock(trans);
 
                ret = __bch2_btree_write_buffer_flush(trans,
-                                       flags|BTREE_INSERT_NOCHECK_RW, true);
+                                       flags|BCH_TRANS_COMMIT_no_check_rw, true);
                if (!ret) {
                        trace_and_count(c, trans_restart_write_buffer_flush, trans, _THIS_IP_);
                        ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_write_buffer_flush);
@@ -1062,13 +1089,8 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
 
        EBUG_ON(test_bit(BCH_FS_CLEAN_SHUTDOWN, &c->flags));
 
-       memset(&trans->journal_preres, 0, sizeof(trans->journal_preres));
-
        trans->journal_u64s             = trans->extra_journal_entries.nr;
-       trans->journal_preres_u64s      = 0;
-
        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);
 
@@ -1084,16 +1106,11 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
                if (i->key_cache_already_flushed)
                        continue;
 
-               /* we're going to journal the key being updated: */
-               u64s = jset_u64s(i->k->k.u64s);
-               if (i->cached &&
-                   likely(!(flags & BTREE_INSERT_JOURNAL_REPLAY)))
-                       trans->journal_preres_u64s += u64s;
-
                if (i->flags & BTREE_UPDATE_NOJOURNAL)
                        continue;
 
-               trans->journal_u64s += u64s;
+               /* we're going to journal the key being updated: */
+               trans->journal_u64s += jset_u64s(i->k->k.u64s);
 
                /* and we're also going to log the overwrite: */
                if (trans->journal_transaction_names)
@@ -1106,14 +1123,15 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
        if (trans->extra_journal_res) {
                ret = bch2_disk_reservation_add(c, trans->disk_res,
                                trans->extra_journal_res,
-                               (flags & BTREE_INSERT_NOFAIL)
+                               (flags & BCH_TRANS_COMMIT_no_enospc)
                                ? BCH_DISK_RESERVATION_NOFAIL : 0);
                if (ret)
                        goto err;
        }
 retry:
        bch2_trans_verify_not_in_restart(trans);
-       memset(&trans->journal_res, 0, sizeof(trans->journal_res));
+       if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res)))
+               memset(&trans->journal_res, 0, sizeof(trans->journal_res));
 
        ret = do_bch2_trans_commit(trans, flags, &i, _RET_IP_);
 
@@ -1125,9 +1143,7 @@ retry:
 
        trace_and_count(c, transaction_commit, trans, _RET_IP_);
 out:
-       bch2_journal_preres_put(&c->journal, &trans->journal_preres);
-
-       if (likely(!(flags & BTREE_INSERT_NOCHECK_RW)))
+       if (likely(!(flags & BCH_TRANS_COMMIT_no_check_rw)))
                bch2_write_ref_put(c, BCH_WRITE_REF_trans);
 out_reset:
        if (!ret)
@@ -1140,5 +1156,17 @@ err:
        if (ret)
                goto out;
 
+       /*
+        * We might have done another transaction commit in the error path -
+        * i.e. btree write buffer flush - which will have made use of
+        * trans->journal_res, but with BCH_TRANS_COMMIT_no_journal_res that is
+        * how the journal sequence number to pin is passed in - so we must
+        * restart:
+        */
+       if (flags & BCH_TRANS_COMMIT_no_journal_res) {
+               ret = -BCH_ERR_transaction_restart_nested;
+               goto out;
+       }
+
        goto retry;
 }