]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/subvolume.c
Update bcachefs sources to 25de2b00dc bcachefs: Change check for invalid key types
[bcachefs-tools-debian] / libbcachefs / subvolume.c
index 320e1a008764b38a44bdc47790c5994fca5d5dd0..f26397aa2b31546e68e3da0e629fcb211c46092e 100644 (file)
@@ -23,7 +23,8 @@ void bch2_snapshot_tree_to_text(struct printbuf *out, struct bch_fs *c,
 }
 
 int bch2_snapshot_tree_invalid(const struct bch_fs *c, struct bkey_s_c k,
-                              unsigned flags, struct printbuf *err)
+                              enum bkey_invalid_flags flags,
+                              struct printbuf *err)
 {
        if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
            bkey_lt(k.k->p, POS(0, 1))) {
@@ -37,8 +38,12 @@ int bch2_snapshot_tree_invalid(const struct bch_fs *c, struct bkey_s_c k,
 int bch2_snapshot_tree_lookup(struct btree_trans *trans, u32 id,
                              struct bch_snapshot_tree *s)
 {
-       return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
-                                      BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
+       int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
+                                         BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
+
+       if (bch2_err_matches(ret, ENOENT))
+               ret = -BCH_ERR_ENOENT_snapshot_tree;
+       return ret;
 }
 
 static struct bkey_i_snapshot_tree *
@@ -82,17 +87,19 @@ void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
 {
        struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
 
-       prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u",
+       prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u tree %u",
               BCH_SNAPSHOT_SUBVOL(s.v),
               BCH_SNAPSHOT_DELETED(s.v),
               le32_to_cpu(s.v->parent),
               le32_to_cpu(s.v->children[0]),
               le32_to_cpu(s.v->children[1]),
-              le32_to_cpu(s.v->subvol));
+              le32_to_cpu(s.v->subvol),
+              le32_to_cpu(s.v->tree));
 }
 
 int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
-                         unsigned flags, struct printbuf *err)
+                         enum bkey_invalid_flags flags,
+                         struct printbuf *err)
 {
        struct bkey_s_c_snapshot s;
        u32 i, id;
@@ -185,7 +192,7 @@ static int snapshot_live(struct btree_trans *trans, u32 id)
                return 0;
 
        ret = snapshot_lookup(trans, id, &v);
-       if (ret == -ENOENT)
+       if (bch2_err_matches(ret, ENOENT))
                bch_err(trans->c, "snapshot node %u not found", id);
        if (ret)
                return ret;
@@ -284,6 +291,7 @@ static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
        struct btree_iter iter;
        struct bkey_s_c k;
        struct bkey_s_c_subvolume s;
+       bool found = false;
        int ret;
 
        for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
@@ -296,14 +304,14 @@ static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
                        continue;
                if (!BCH_SUBVOLUME_SNAP(s.v)) {
                        *subvol_id = s.k->p.offset;
-                       goto found;
+                       found = true;
+                       break;
                }
        }
-       ret = ret ?: -ENOENT;
-found:
+
        bch2_trans_iter_exit(trans, &iter);
 
-       if (ret == -ENOENT) {
+       if (!ret && !found) {
                struct bkey_i_subvolume *s;
 
                *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
@@ -379,7 +387,7 @@ static int check_snapshot_tree(struct btree_trans *trans,
                if (ret)
                        goto err;
 
-               u = bch2_bkey_make_mut_typed(trans, iter, k, 0, snapshot_tree);
+               u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot_tree);
                ret = PTR_ERR_OR_ZERO(u);
                if (ret)
                        goto err;
@@ -428,6 +436,8 @@ static int snapshot_tree_ptr_good(struct btree_trans *trans,
        struct bch_snapshot_tree s_t;
        int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
 
+       if (bch2_err_matches(ret, ENOENT))
+               return 0;
        if (ret)
                return ret;
 
@@ -461,11 +471,11 @@ static int snapshot_tree_ptr_repair(struct btree_trans *trans,
        tree_id = le32_to_cpu(root.v->tree);
 
        ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
-       if (ret)
+       if (ret && !bch2_err_matches(ret, ENOENT))
                return ret;
 
-       if (le32_to_cpu(s_t.root_snapshot) != root_id) {
-               u = bch2_bkey_make_mut_typed(trans, &root_iter, root.s_c, 0, snapshot);
+       if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
+               u = bch2_bkey_make_mut_typed(trans, &root_iter, &root.s_c, 0, snapshot);
                ret =   PTR_ERR_OR_ZERO(u) ?:
                        snapshot_tree_create(trans, root_id,
                                bch2_snapshot_tree_oldest_subvol(c, root_id),
@@ -479,7 +489,7 @@ static int snapshot_tree_ptr_repair(struct btree_trans *trans,
        }
 
        if (s->k->p.snapshot != root_id) {
-               u = bch2_bkey_make_mut_typed(trans, iter, s->s_c, 0, snapshot);
+               u = bch2_bkey_make_mut_typed(trans, iter, &s->s_c, 0, snapshot);
                ret = PTR_ERR_OR_ZERO(u);
                if (ret)
                        goto err;
@@ -512,7 +522,7 @@ static int check_snapshot(struct btree_trans *trans,
        id = le32_to_cpu(s.v->parent);
        if (id) {
                ret = snapshot_lookup(trans, id, &v);
-               if (ret == -ENOENT)
+               if (bch2_err_matches(ret, ENOENT))
                        bch_err(c, "snapshot with nonexistent parent:\n  %s",
                                (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
                if (ret)
@@ -531,7 +541,7 @@ static int check_snapshot(struct btree_trans *trans,
                id = le32_to_cpu(s.v->children[i]);
 
                ret = snapshot_lookup(trans, id, &v);
-               if (ret == -ENOENT)
+               if (bch2_err_matches(ret, ENOENT))
                        bch_err(c, "snapshot node %llu has nonexistent child %u",
                                s.k->p.offset, id);
                if (ret)
@@ -551,7 +561,7 @@ static int check_snapshot(struct btree_trans *trans,
        if (should_have_subvol) {
                id = le32_to_cpu(s.v->subvol);
                ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
-               if (ret == -ENOENT)
+               if (bch2_err_matches(ret, ENOENT))
                        bch_err(c, "snapshot points to nonexistent subvolume:\n  %s",
                                (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
                if (ret)
@@ -614,9 +624,8 @@ int bch2_fs_check_snapshots(struct bch_fs *c)
                        BTREE_ITER_PREFETCH, k,
                        NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
                check_snapshot(&trans, &iter, k)));
-
        if (ret)
-               bch_err(c, "error %i checking snapshots", ret);
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -637,7 +646,7 @@ static int check_subvol(struct btree_trans *trans,
        snapid = le32_to_cpu(subvol.v->snapshot);
        ret = snapshot_lookup(trans, snapid, &snapshot);
 
-       if (ret == -ENOENT)
+       if (bch2_err_matches(ret, ENOENT))
                bch_err(c, "subvolume %llu points to nonexistent snapshot %u",
                        k.k->p.offset, snapid);
        if (ret)
@@ -655,9 +664,14 @@ static int check_subvol(struct btree_trans *trans,
 
        if (!BCH_SUBVOLUME_SNAP(subvol.v)) {
                u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot));
+               u32 snapshot_tree = snapshot_t(c, snapshot_root)->tree;
                struct bch_snapshot_tree st;
 
-               ret = bch2_snapshot_tree_lookup(trans, snapshot_root, &st);
+               ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st);
+
+               bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
+                               "%s: snapshot tree %u not found", __func__, snapshot_tree);
+
                if (ret)
                        return ret;
 
@@ -665,7 +679,7 @@ static int check_subvol(struct btree_trans *trans,
                                "subvolume %llu is not set as snapshot but is not master subvolume",
                                k.k->p.offset)) {
                        struct bkey_i_subvolume *s =
-                               bch2_bkey_make_mut_typed(trans, iter, subvol.s_c, 0, subvolume);
+                               bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);
                        ret = PTR_ERR_OR_ZERO(s);
                        if (ret)
                                return ret;
@@ -680,20 +694,17 @@ fsck_err:
 
 int bch2_fs_check_subvols(struct bch_fs *c)
 {
-       struct btree_trans trans;
        struct btree_iter iter;
        struct bkey_s_c k;
        int ret;
 
-       bch2_trans_init(&trans, c, 0, 0);
-
-       ret = for_each_btree_key_commit(&trans, iter,
+       ret = bch2_trans_run(c,
+               for_each_btree_key_commit(&trans, iter,
                        BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
                        NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
-               check_subvol(&trans, &iter, k));
-
-       bch2_trans_exit(&trans);
-
+               check_subvol(&trans, &iter, k)));
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -704,22 +715,17 @@ void bch2_fs_snapshots_exit(struct bch_fs *c)
 
 int bch2_fs_snapshots_start(struct bch_fs *c)
 {
-       struct btree_trans trans;
        struct btree_iter iter;
        struct bkey_s_c k;
        int ret = 0;
 
-       bch2_trans_init(&trans, c, 0, 0);
-
-       for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
+       ret = bch2_trans_run(c,
+               for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
                           POS_MIN, 0, k,
-               bch2_mark_snapshot(&trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
-               bch2_snapshot_set_equiv(&trans, k));
-
-       bch2_trans_exit(&trans);
-
+                       bch2_mark_snapshot(&trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
+                       bch2_snapshot_set_equiv(&trans, k)));
        if (ret)
-               bch_err(c, "error starting snapshots: %s", bch2_err_str(ret));
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -737,7 +743,8 @@ static int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
                                    0, snapshot);
        ret = PTR_ERR_OR_ZERO(s);
        if (unlikely(ret)) {
-               bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing snapshot %u", id);
+               bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
+                                       trans->c, "missing snapshot %u", id);
                return ret;
        }
 
@@ -766,7 +773,8 @@ static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
        s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
                                     BTREE_ITER_INTENT, snapshot);
        ret = bkey_err(s);
-       bch2_fs_inconsistent_on(ret == -ENOENT, c, "missing snapshot %u", id);
+       bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
+                               "missing snapshot %u", id);
 
        if (ret)
                goto err;
@@ -782,7 +790,8 @@ static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
                                     0, snapshot);
                ret = PTR_ERR_OR_ZERO(parent);
                if (unlikely(ret)) {
-                       bch2_fs_inconsistent_on(ret == -ENOENT, c, "missing snapshot %u", parent_id);
+                       bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
+                                               "missing snapshot %u", parent_id);
                        goto err;
                }
 
@@ -818,7 +827,7 @@ static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
                        goto err;
 
                if (s.v->children[0]) {
-                       s_t->v.root_snapshot = cpu_to_le32(s.v->children[0]);
+                       s_t->v.root_snapshot = s.v->children[0];
                } else {
                        s_t->k.type = KEY_TYPE_deleted;
                        set_bkey_val_u64s(&s_t->k, 0);
@@ -902,7 +911,7 @@ static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 par
                        0, snapshot);
        ret = PTR_ERR_OR_ZERO(n_parent);
        if (unlikely(ret)) {
-               if (ret == -ENOENT)
+               if (bch2_err_matches(ret, ENOENT))
                        bch_err(trans->c, "snapshot %u not found", parent);
                return ret;
        }
@@ -1115,6 +1124,8 @@ int bch2_delete_dead_snapshots(struct bch_fs *c)
 err:
        darray_exit(&deleted);
        bch2_trans_exit(&trans);
+       if (ret)
+               bch_err_fn(c, ret);
        return ret;
 }
 
@@ -1182,7 +1193,8 @@ bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
 {
        int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
                                          iter_flags, subvolume, s);
-       bch2_fs_inconsistent_on(ret == -ENOENT && inconsistent_if_not_found,
+       bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) &&
+                               inconsistent_if_not_found,
                                trans->c, "missing subvolume %u", subvol);
        return ret;
 }
@@ -1214,11 +1226,11 @@ int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol,
        k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_subvolumes, POS(0, subvol),
                               BTREE_ITER_CACHED|
                               BTREE_ITER_WITH_UPDATES);
-       ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -ENOENT;
+       ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -BCH_ERR_ENOENT_subvolume;
 
        if (likely(!ret))
                *snapid = le32_to_cpu(bkey_s_c_to_subvolume(k).v->snapshot);
-       else if (ret == -ENOENT)
+       else if (bch2_err_matches(ret, ENOENT))
                bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvol);
        bch2_trans_iter_exit(trans, &iter);
        return ret;
@@ -1239,7 +1251,7 @@ static int bch2_subvolume_reparent(struct btree_trans *trans,
            le32_to_cpu(bkey_s_c_to_subvolume(k).v->parent) != old_parent)
                return 0;
 
-       s = bch2_bkey_make_mut_typed(trans, iter, k, 0, subvolume);
+       s = bch2_bkey_make_mut_typed(trans, iter, &k, 0, subvolume);
        ret = PTR_ERR_OR_ZERO(s);
        if (ret)
                return ret;
@@ -1284,7 +1296,8 @@ static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
                                BTREE_ITER_CACHED|BTREE_ITER_INTENT,
                                subvolume);
        ret = bkey_err(subvol);
-       bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing subvolume %u", subvolid);
+       bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
+                               "missing subvolume %u", subvolid);
        if (ret)
                return ret;
 
@@ -1317,7 +1330,7 @@ static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
                          __bch2_subvolume_delete(trans, subvolid));
 }
 
-void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
+static void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
 {
        struct bch_fs *c = container_of(work, struct bch_fs,
                                snapshot_wait_for_pagecache_and_delete_work);
@@ -1355,7 +1368,7 @@ struct subvolume_unlink_hook {
        u32                             subvol;
 };
 
-int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
+static int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
                                                      struct btree_trans_commit_hook *_h)
 {
        struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
@@ -1399,7 +1412,8 @@ int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
                        BTREE_ITER_CACHED, subvolume);
        ret = PTR_ERR_OR_ZERO(n);
        if (unlikely(ret)) {
-               bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing subvolume %u", subvolid);
+               bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
+                                       "missing subvolume %u", subvolid);
                return ret;
        }
 
@@ -1439,7 +1453,7 @@ int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
                                BTREE_ITER_CACHED, subvolume);
                ret = PTR_ERR_OR_ZERO(src_subvol);
                if (unlikely(ret)) {
-                       bch2_fs_inconsistent_on(ret == -ENOENT, c,
+                       bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
                                                "subvolume %u not found", src_subvolid);
                        goto err;
                }