]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_update.c
Update bcachefs sources to 6d44812757dd bcachefs: BCH_IOCTL_FSCK_ONLINE
[bcachefs-tools-debian] / libbcachefs / btree_update.c
index 612fba60be14ea952d32b00471026ede90207abc..254794c1d9552624ebd90a8f039d091c64911b29 100644 (file)
@@ -11,7 +11,7 @@
 #include "error.h"
 #include "extents.h"
 #include "keylist.h"
-#include "subvolume.h"
+#include "snapshot.h"
 #include "trace.h"
 
 static inline int btree_insert_entry_cmp(const struct btree_insert_entry *l,
@@ -28,51 +28,6 @@ bch2_trans_update_by_path(struct btree_trans *, struct btree_path *,
                          struct bkey_i *, enum btree_update_flags,
                          unsigned long ip);
 
-static noinline int __check_pos_snapshot_overwritten(struct btree_trans *trans,
-                                         enum btree_id id,
-                                         struct bpos pos)
-{
-       struct bch_fs *c = trans->c;
-       struct btree_iter iter;
-       struct bkey_s_c k;
-       int ret;
-
-       bch2_trans_iter_init(trans, &iter, id, pos,
-                            BTREE_ITER_NOT_EXTENTS|
-                            BTREE_ITER_ALL_SNAPSHOTS);
-       while (1) {
-               k = bch2_btree_iter_prev(&iter);
-               ret = bkey_err(k);
-               if (ret)
-                       break;
-
-               if (!k.k)
-                       break;
-
-               if (!bkey_eq(pos, k.k->p))
-                       break;
-
-               if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot)) {
-                       ret = 1;
-                       break;
-               }
-       }
-       bch2_trans_iter_exit(trans, &iter);
-
-       return ret;
-}
-
-static inline int check_pos_snapshot_overwritten(struct btree_trans *trans,
-                                         enum btree_id id,
-                                         struct bpos pos)
-{
-       if (!btree_type_has_snapshots(id) ||
-           bch2_snapshot_is_leaf(trans->c, pos.snapshot))
-               return 0;
-
-       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,
@@ -91,8 +46,8 @@ static noinline int extent_front_merge(struct btree_trans *trans,
        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);
+       ret =   bch2_key_has_snapshot_overwrites(trans, iter->btree_id, k.k->p) ?:
+               bch2_key_has_snapshot_overwrites(trans, iter->btree_id, (*insert)->k.p);
        if (ret < 0)
                return ret;
        if (ret)
@@ -114,8 +69,8 @@ static noinline int extent_back_merge(struct btree_trans *trans,
        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);
+       ret =   bch2_key_has_snapshot_overwrites(trans, iter->btree_id, insert->k.p) ?:
+               bch2_key_has_snapshot_overwrites(trans, iter->btree_id, k.k->p);
        if (ret < 0)
                return ret;
        if (ret)
@@ -169,7 +124,7 @@ int __bch2_insert_snapshot_whiteouts(struct btree_trans *trans,
        struct bkey_s_c old_k, new_k;
        snapshot_id_list s;
        struct bkey_i *update;
-       int ret;
+       int ret = 0;
 
        if (!bch2_snapshot_has_children(c, old_pos.snapshot))
                return 0;
@@ -425,21 +380,12 @@ bch2_trans_update_by_path(struct btree_trans *trans, struct btree_path *path,
 {
        struct bch_fs *c = trans->c;
        struct btree_insert_entry *i, n;
-       u64 seq = 0;
        int cmp;
 
        EBUG_ON(!path->should_be_locked);
        EBUG_ON(trans->nr_updates >= BTREE_ITER_MAX);
        EBUG_ON(!bpos_eq(k->k.p, path->pos));
 
-       /*
-        * The transaction journal res hasn't been allocated at this point.
-        * That occurs at commit time. Reuse the seq field to pass in the seq
-        * of a prejournaled key.
-        */
-       if (flags & BTREE_UPDATE_PREJOURNAL)
-               seq = trans->journal_res.seq;
-
        n = (struct btree_insert_entry) {
                .flags          = flags,
                .bkey_type      = __btree_node_type(path->level, path->btree_id),
@@ -448,7 +394,6 @@ bch2_trans_update_by_path(struct btree_trans *trans, struct btree_path *path,
                .cached         = path->cached,
                .path           = path,
                .k              = k,
-               .seq            = seq,
                .ip_allocated   = ip,
        };
 
@@ -476,7 +421,6 @@ bch2_trans_update_by_path(struct btree_trans *trans, struct btree_path *path,
                i->cached       = n.cached;
                i->k            = n.k;
                i->path         = n.path;
-               i->seq          = n.seq;
                i->ip_allocated = n.ip_allocated;
        } else {
                array_insert_item(trans->updates, trans->nr_updates,
@@ -511,11 +455,49 @@ bch2_trans_update_by_path(struct btree_trans *trans, struct btree_path *path,
        return 0;
 }
 
+static noinline int bch2_trans_update_get_key_cache(struct btree_trans *trans,
+                                                   struct btree_iter *iter,
+                                                   struct btree_path *path)
+{
+       if (!iter->key_cache_path ||
+           !iter->key_cache_path->should_be_locked ||
+           !bpos_eq(iter->key_cache_path->pos, iter->pos)) {
+               struct bkey_cached *ck;
+               int ret;
+
+               if (!iter->key_cache_path)
+                       iter->key_cache_path =
+                               bch2_path_get(trans, path->btree_id, path->pos, 1, 0,
+                                             BTREE_ITER_INTENT|
+                                             BTREE_ITER_CACHED, _THIS_IP_);
+
+               iter->key_cache_path =
+                       bch2_btree_path_set_pos(trans, iter->key_cache_path, path->pos,
+                                               iter->flags & BTREE_ITER_INTENT,
+                                               _THIS_IP_);
+
+               ret = bch2_btree_path_traverse(trans, iter->key_cache_path,
+                                              BTREE_ITER_CACHED);
+               if (unlikely(ret))
+                       return ret;
+
+               ck = (void *) iter->key_cache_path->l[0].b;
+
+               if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
+                       trace_and_count(trans->c, trans_restart_key_cache_raced, trans, _RET_IP_);
+                       return btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
+               }
+
+               btree_path_set_should_be_locked(iter->key_cache_path);
+       }
+
+       return 0;
+}
+
 int __must_check bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter,
                                   struct bkey_i *k, enum btree_update_flags flags)
 {
        struct btree_path *path = iter->update_path ?: iter->path;
-       struct bkey_cached *ck;
        int ret;
 
        if (iter->flags & BTREE_ITER_IS_EXTENTS)
@@ -539,34 +521,9 @@ int __must_check bch2_trans_update(struct btree_trans *trans, struct btree_iter
            !path->cached &&
            !path->level &&
            btree_id_cached(trans->c, path->btree_id)) {
-               if (!iter->key_cache_path ||
-                   !iter->key_cache_path->should_be_locked ||
-                   !bpos_eq(iter->key_cache_path->pos, k->k.p)) {
-                       if (!iter->key_cache_path)
-                               iter->key_cache_path =
-                                       bch2_path_get(trans, path->btree_id, path->pos, 1, 0,
-                                                     BTREE_ITER_INTENT|
-                                                     BTREE_ITER_CACHED, _THIS_IP_);
-
-                       iter->key_cache_path =
-                               bch2_btree_path_set_pos(trans, iter->key_cache_path, path->pos,
-                                                       iter->flags & BTREE_ITER_INTENT,
-                                                       _THIS_IP_);
-
-                       ret = bch2_btree_path_traverse(trans, iter->key_cache_path,
-                                                      BTREE_ITER_CACHED);
-                       if (unlikely(ret))
-                               return ret;
-
-                       ck = (void *) iter->key_cache_path->l[0].b;
-
-                       if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
-                               trace_and_count(trans->c, trans_restart_key_cache_raced, trans, _RET_IP_);
-                               return btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
-                       }
-
-                       btree_path_set_should_be_locked(iter->key_cache_path);
-               }
+               ret = bch2_trans_update_get_key_cache(trans, iter, path);
+               if (ret)
+                       return ret;
 
                path = iter->key_cache_path;
        }
@@ -574,16 +531,17 @@ int __must_check bch2_trans_update(struct btree_trans *trans, struct btree_iter
        return bch2_trans_update_by_path(trans, path, k, flags, _RET_IP_);
 }
 
-/*
- * Add a transaction update for a key that has already been journaled.
- */
-int __must_check bch2_trans_update_seq(struct btree_trans *trans, u64 seq,
-                                      struct btree_iter *iter, struct bkey_i *k,
-                                      enum btree_update_flags flags)
+static noinline int bch2_btree_insert_clone_trans(struct btree_trans *trans,
+                                                 enum btree_id btree,
+                                                 struct bkey_i *k)
 {
-       trans->journal_res.seq = seq;
-       return bch2_trans_update(trans, iter, k, flags|BTREE_UPDATE_NOJOURNAL|
-                                                BTREE_UPDATE_PREJOURNAL);
+       struct bkey_i *n = bch2_trans_kmalloc(trans, bkey_bytes(&k->k));
+       int ret = PTR_ERR_OR_ZERO(n);
+       if (ret)
+               return ret;
+
+       bkey_copy(n, k);
+       return bch2_btree_insert_trans(trans, btree, n, 0);
 }
 
 int __must_check bch2_trans_update_buffered(struct btree_trans *trans,
@@ -596,6 +554,9 @@ int __must_check bch2_trans_update_buffered(struct btree_trans *trans,
        EBUG_ON(trans->nr_wb_updates > trans->wb_updates_size);
        EBUG_ON(k->k.u64s > BTREE_WRITE_BUFERED_U64s_MAX);
 
+       if (unlikely(trans->journal_replay_not_finished))
+               return bch2_btree_insert_clone_trans(trans, btree, k);
+
        trans_for_each_wb_update(trans, i) {
                if (i->btree == btree && bpos_eq(i->k.k.p, k->k.p)) {
                        bkey_copy(&i->k, k);
@@ -685,6 +646,7 @@ int bch2_btree_insert_nonextent(struct btree_trans *trans,
        int ret;
 
        bch2_trans_iter_init(trans, &iter, btree, k->k.p,
+                            BTREE_ITER_CACHED|
                             BTREE_ITER_NOT_EXTENTS|
                             BTREE_ITER_INTENT);
        ret   = bch2_btree_iter_traverse(&iter) ?:
@@ -693,8 +655,8 @@ int bch2_btree_insert_nonextent(struct btree_trans *trans,
        return ret;
 }
 
-int __bch2_btree_insert(struct btree_trans *trans, enum btree_id id,
-                       struct bkey_i *k, enum btree_update_flags flags)
+int bch2_btree_insert_trans(struct btree_trans *trans, enum btree_id id,
+                           struct bkey_i *k, enum btree_update_flags flags)
 {
        struct btree_iter iter;
        int ret;
@@ -712,16 +674,18 @@ int __bch2_btree_insert(struct btree_trans *trans, enum btree_id id,
  * bch2_btree_insert - insert keys into the extent btree
  * @c:                 pointer to struct bch_fs
  * @id:                        btree to insert into
- * @insert_keys:       list of keys to insert
- * @hook:              insert callback
+ * @k:                 key to insert
+ * @disk_res:          must be non-NULL whenever inserting or potentially
+ *                     splitting data extents
+ * @flags:             transaction commit flags
+ *
+ * Returns:            0 on success, error code on failure
  */
-int bch2_btree_insert(struct bch_fs *c, enum btree_id id,
-                     struct bkey_i *k,
-                     struct disk_reservation *disk_res,
-                     u64 *journal_seq, int flags)
+int bch2_btree_insert(struct bch_fs *c, enum btree_id id, struct bkey_i *k,
+                     struct disk_reservation *disk_res, int flags)
 {
-       return bch2_trans_do(c, disk_res, journal_seq, flags,
-                            __bch2_btree_insert(&trans, id, k, 0));
+       return bch2_trans_do(c, disk_res, NULL, flags,
+                            bch2_btree_insert_trans(trans, id, k, 0));
 }
 
 int bch2_btree_delete_extent_at(struct btree_trans *trans, struct btree_iter *iter,
@@ -745,18 +709,21 @@ int bch2_btree_delete_at(struct btree_trans *trans,
        return bch2_btree_delete_extent_at(trans, iter, 0, update_flags);
 }
 
-int bch2_btree_delete_at_buffered(struct btree_trans *trans,
-                                 enum btree_id btree, struct bpos pos)
+int bch2_btree_delete(struct btree_trans *trans,
+                     enum btree_id btree, struct bpos pos,
+                     unsigned update_flags)
 {
-       struct bkey_i *k;
+       struct btree_iter iter;
+       int ret;
 
-       k = bch2_trans_kmalloc(trans, sizeof(*k));
-       if (IS_ERR(k))
-               return PTR_ERR(k);
+       bch2_trans_iter_init(trans, &iter, btree, pos,
+                            BTREE_ITER_CACHED|
+                            BTREE_ITER_INTENT);
+       ret   = bch2_btree_iter_traverse(&iter) ?:
+               bch2_btree_delete_at(trans, &iter, update_flags);
+       bch2_trans_iter_exit(trans, &iter);
 
-       bkey_init(&k->k);
-       k->k.p = pos;
-       return bch2_trans_update_buffered(trans, btree, k);
+       return ret;
 }
 
 int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id,
@@ -804,7 +771,7 @@ int bch2_btree_delete_range_trans(struct btree_trans *trans, enum btree_id id,
 
                ret   = bch2_trans_update(trans, &iter, &delete, update_flags) ?:
                        bch2_trans_commit(trans, &disk_res, journal_seq,
-                                         BTREE_INSERT_NOFAIL);
+                                         BCH_TRANS_COMMIT_no_enospc);
                bch2_disk_reservation_put(trans->c, &disk_res);
 err:
                /*
@@ -822,9 +789,7 @@ err:
        }
        bch2_trans_iter_exit(trans, &iter);
 
-       if (!ret && trans_was_restarted(trans, restart_count))
-               ret = -BCH_ERR_transaction_restart_nested;
-       return ret;
+       return ret ?: trans_was_restarted(trans, restart_count);
 }
 
 /*
@@ -838,7 +803,7 @@ int bch2_btree_delete_range(struct bch_fs *c, enum btree_id id,
                            u64 *journal_seq)
 {
        int ret = bch2_trans_run(c,
-                       bch2_btree_delete_range_trans(&trans, id, start, end,
+                       bch2_btree_delete_range_trans(trans, id, start, end,
                                                      update_flags, journal_seq));
        if (ret == -BCH_ERR_transaction_restart_nested)
                ret = 0;
@@ -848,21 +813,16 @@ int bch2_btree_delete_range(struct bch_fs *c, enum btree_id id,
 int bch2_btree_bit_mod(struct btree_trans *trans, enum btree_id btree,
                       struct bpos pos, bool set)
 {
-       struct bkey_i *k;
-       int ret = 0;
+       struct bkey_i k;
 
-       k = bch2_trans_kmalloc_nomemzero(trans, sizeof(*k));
-       ret = PTR_ERR_OR_ZERO(k);
-       if (unlikely(ret))
-               return ret;
-
-       bkey_init(&k->k);
-       k->k.type = set ? KEY_TYPE_set : KEY_TYPE_deleted;
-       k->k.p = pos;
+       bkey_init(&k.k);
+       k.k.type = set ? KEY_TYPE_set : KEY_TYPE_deleted;
+       k.k.p = pos;
 
-       return bch2_trans_update_buffered(trans, btree, k);
+       return bch2_trans_update_buffered(trans, btree, &k);
 }
 
+__printf(2, 0)
 static int __bch2_trans_log_msg(darray_u64 *entries, const char *fmt, va_list args)
 {
        struct printbuf buf = PRINTBUF;
@@ -899,6 +859,7 @@ err:
        return ret;
 }
 
+__printf(3, 0)
 static int
 __bch2_fs_log_msg(struct bch_fs *c, unsigned commit_flags, const char *fmt,
                  va_list args)
@@ -909,13 +870,14 @@ __bch2_fs_log_msg(struct bch_fs *c, unsigned commit_flags, const char *fmt,
                ret = __bch2_trans_log_msg(&c->journal.early_journal_entries, fmt, args);
        } else {
                ret = bch2_trans_do(c, NULL, NULL,
-                       BTREE_INSERT_LAZY_RW|commit_flags,
-                       __bch2_trans_log_msg(&trans.extra_journal_entries, fmt, args));
+                       BCH_TRANS_COMMIT_lazy_rw|commit_flags,
+                       __bch2_trans_log_msg(&trans->extra_journal_entries, fmt, args));
        }
 
        return ret;
 }
 
+__printf(2, 3)
 int bch2_fs_log_msg(struct bch_fs *c, const char *fmt, ...)
 {
        va_list args;
@@ -931,6 +893,7 @@ int bch2_fs_log_msg(struct bch_fs *c, const char *fmt, ...)
  * Use for logging messages during recovery to enable reserved space and avoid
  * blocking.
  */
+__printf(2, 3)
 int bch2_journal_log_msg(struct bch_fs *c, const char *fmt, ...)
 {
        va_list args;