]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/dirent.c
Update bcachefs sources to 0a9f0fc68a bcachefs: Don't unconditially version_upgrade...
[bcachefs-tools-debian] / libbcachefs / dirent.c
index 38017699c04af3a01d17cb1239efdaa452e1b8fd..592dd80cf963959f4b23f1ae4f463227bfa655e6 100644 (file)
@@ -64,7 +64,7 @@ static bool dirent_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
 }
 
 const struct bch_hash_desc bch2_dirent_hash_desc = {
-       .btree_id       = BTREE_ID_DIRENTS,
+       .btree_id       = BTREE_ID_dirents,
        .key_type       = KEY_TYPE_dirent,
        .hash_key       = dirent_hash_key,
        .hash_bkey      = dirent_hash_bkey,
@@ -104,7 +104,7 @@ void bch2_dirent_to_text(struct printbuf *out, struct bch_fs *c,
 
        bch_scnmemcpy(out, d.v->d_name,
                      bch2_dirent_name_bytes(d));
-       pr_buf(out, " -> %llu", d.v->d_inum);
+       pr_buf(out, " -> %llu type %u", d.v->d_inum, d.v->d_type);
 }
 
 static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
@@ -169,12 +169,12 @@ int bch2_dirent_rename(struct btree_trans *trans,
                       const struct qstr *dst_name, u64 *dst_inum,
                       enum bch_rename_mode mode)
 {
-       struct btree_iter *src_iter, *dst_iter;
+       struct btree_iter *src_iter = NULL, *dst_iter = NULL;
        struct bkey_s_c old_src, old_dst;
        struct bkey_i_dirent *new_src = NULL, *new_dst = NULL;
        struct bpos dst_pos =
                POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
-       int ret;
+       int ret = 0;
 
        *src_inum = *dst_inum = 0;
 
@@ -191,8 +191,10 @@ int bch2_dirent_rename(struct btree_trans *trans,
                : bch2_hash_lookup(trans, bch2_dirent_hash_desc,
                                   dst_hash, dst_dir, dst_name,
                                   BTREE_ITER_INTENT);
-       if (IS_ERR(dst_iter))
-               return PTR_ERR(dst_iter);
+       ret = PTR_ERR_OR_ZERO(dst_iter);
+       if (ret)
+               goto out;
+
        old_dst = bch2_btree_iter_peek_slot(dst_iter);
 
        if (mode != BCH_RENAME)
@@ -202,15 +204,18 @@ int bch2_dirent_rename(struct btree_trans *trans,
        src_iter = bch2_hash_lookup(trans, bch2_dirent_hash_desc,
                                    src_hash, src_dir, src_name,
                                    BTREE_ITER_INTENT);
-       if (IS_ERR(src_iter))
-               return PTR_ERR(src_iter);
+       ret = PTR_ERR_OR_ZERO(src_iter);
+       if (ret)
+               goto out;
+
        old_src = bch2_btree_iter_peek_slot(src_iter);
        *src_inum = le64_to_cpu(bkey_s_c_to_dirent(old_src).v->d_inum);
 
        /* Create new dst key: */
        new_dst = dirent_create_key(trans, 0, dst_name, 0);
-       if (IS_ERR(new_dst))
-               return PTR_ERR(new_dst);
+       ret = PTR_ERR_OR_ZERO(new_dst);
+       if (ret)
+               goto out;
 
        dirent_copy_target(new_dst, bkey_s_c_to_dirent(old_src));
        new_dst->k.p = dst_iter->pos;
@@ -218,15 +223,18 @@ int bch2_dirent_rename(struct btree_trans *trans,
        /* Create new src key: */
        if (mode == BCH_RENAME_EXCHANGE) {
                new_src = dirent_create_key(trans, 0, src_name, 0);
-               if (IS_ERR(new_src))
-                       return PTR_ERR(new_src);
+               ret = PTR_ERR_OR_ZERO(new_src);
+               if (ret)
+                       goto out;
 
                dirent_copy_target(new_src, bkey_s_c_to_dirent(old_dst));
                new_src->k.p = src_iter->pos;
        } else {
                new_src = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
-               if (IS_ERR(new_src))
-                       return PTR_ERR(new_src);
+               ret = PTR_ERR_OR_ZERO(new_src);
+               if (ret)
+                       goto out;
+
                bkey_init(&new_src->k);
                new_src->k.p = src_iter->pos;
 
@@ -246,31 +254,34 @@ int bch2_dirent_rename(struct btree_trans *trans,
                                 */
                                new_dst->k.p = src_iter->pos;
                                bch2_trans_update(trans, src_iter,
-                                                 &new_dst->k_i);
-                               return 0;
+                                                 &new_dst->k_i, 0);
+                               goto out;
                        } else {
                                /* If we're overwriting, we can't insert new_dst
                                 * at a different slot because it has to
                                 * overwrite old_dst - just make sure to use a
                                 * whiteout when deleting src:
                                 */
-                               new_src->k.type = KEY_TYPE_whiteout;
+                               new_src->k.type = KEY_TYPE_hash_whiteout;
                        }
                } else {
                        /* Check if we need a whiteout to delete src: */
                        ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
                                                       src_hash, src_iter);
                        if (ret < 0)
-                               return ret;
+                               goto out;
 
                        if (ret)
-                               new_src->k.type = KEY_TYPE_whiteout;
+                               new_src->k.type = KEY_TYPE_hash_whiteout;
                }
        }
 
-       bch2_trans_update(trans, src_iter, &new_src->k_i);
-       bch2_trans_update(trans, dst_iter, &new_dst->k_i);
-       return 0;
+       bch2_trans_update(trans, src_iter, &new_src->k_i, 0);
+       bch2_trans_update(trans, dst_iter, &new_dst->k_i, 0);
+out:
+       bch2_trans_iter_put(trans, src_iter);
+       bch2_trans_iter_put(trans, dst_iter);
+       return ret;
 }
 
 int bch2_dirent_delete_at(struct btree_trans *trans,
@@ -281,18 +292,6 @@ int bch2_dirent_delete_at(struct btree_trans *trans,
                                   hash_info, iter);
 }
 
-int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
-                      const struct bch_hash_info *hash_info,
-                      const struct qstr *name,
-                      u64 *journal_seq)
-{
-       return bch2_trans_do(c, journal_seq,
-                            BTREE_INSERT_ATOMIC|
-                            BTREE_INSERT_NOFAIL,
-               bch2_hash_delete(&trans, bch2_dirent_hash_desc, hash_info,
-                                dir_inum, name));
-}
-
 struct btree_iter *
 __bch2_dirent_lookup_trans(struct btree_trans *trans, u64 dir_inum,
                           const struct bch_hash_info *hash_info,
@@ -322,6 +321,7 @@ u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
 
        k = bch2_btree_iter_peek_slot(iter);
        inum = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum);
+       bch2_trans_iter_put(&trans, iter);
 out:
        bch2_trans_exit(&trans);
        return inum;
@@ -333,7 +333,7 @@ int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
        struct bkey_s_c k;
        int ret;
 
-       for_each_btree_key(trans, iter, BTREE_ID_DIRENTS,
+       for_each_btree_key(trans, iter, BTREE_ID_dirents,
                           POS(dir_inum, 0), 0, k, ret) {
                if (k.k->p.inode > dir_inum)
                        break;
@@ -358,7 +358,7 @@ int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
 
        bch2_trans_init(&trans, c, 0, 0);
 
-       for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
+       for_each_btree_key(&trans, iter, BTREE_ID_dirents,
                           POS(inum, ctx->pos), 0, k, ret) {
                if (k.k->p.inode > inum)
                        break;
@@ -380,6 +380,8 @@ int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
                        break;
                ctx->pos = dirent.k->p.offset + 1;
        }
+       bch2_trans_iter_put(&trans, iter);
+
        ret = bch2_trans_exit(&trans) ?: ret;
 
        return ret;