]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/dirent.c
Add a subcommand for resizing the journal
[bcachefs-tools-debian] / libbcachefs / dirent.c
index 11e628876fffbb600974c9b97016a4dff19531f1..f34bfda8ab0d6be5abdcae5c972442a3d6de8f8f 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 
 #include "bcachefs.h"
 #include "bkey_methods.h"
@@ -103,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,
@@ -137,10 +138,10 @@ static struct bkey_i_dirent *dirent_create_key(struct btree_trans *trans,
        return dirent;
 }
 
-int __bch2_dirent_create(struct btree_trans *trans,
-                        u64 dir_inum, const struct bch_hash_info *hash_info,
-                        u8 type, const struct qstr *name, u64 dst_inum,
-                        int flags)
+int bch2_dirent_create(struct btree_trans *trans,
+                      u64 dir_inum, const struct bch_hash_info *hash_info,
+                      u8 type, const struct qstr *name, u64 dst_inum,
+                      int flags)
 {
        struct bkey_i_dirent *dirent;
        int ret;
@@ -154,16 +155,6 @@ int __bch2_dirent_create(struct btree_trans *trans,
                             dir_inum, &dirent->k_i, flags);
 }
 
-int bch2_dirent_create(struct bch_fs *c, u64 dir_inum,
-                      const struct bch_hash_info *hash_info,
-                      u8 type, const struct qstr *name, u64 dst_inum,
-                      u64 *journal_seq, int flags)
-{
-       return bch2_trans_do(c, journal_seq, flags,
-               __bch2_dirent_create(&trans, dir_inum, hash_info,
-                                    type, name, dst_inum, flags));
-}
-
 static void dirent_copy_target(struct bkey_i_dirent *dst,
                               struct bkey_s_c_dirent src)
 {
@@ -171,22 +162,21 @@ static void dirent_copy_target(struct bkey_i_dirent *dst,
        dst->v.d_type = src.v->d_type;
 }
 
-static struct bpos bch2_dirent_pos(struct bch_inode_info *inode,
-                                  const struct qstr *name)
-{
-       return POS(inode->v.i_ino, bch2_dirent_hash(&inode->ei_str_hash, name));
-}
-
 int bch2_dirent_rename(struct btree_trans *trans,
-               struct bch_inode_info *src_dir, const struct qstr *src_name,
-               struct bch_inode_info *dst_dir, const struct qstr *dst_name,
-               enum bch_rename_mode mode)
+                      u64 src_dir, struct bch_hash_info *src_hash,
+                      u64 dst_dir, struct bch_hash_info *dst_hash,
+                      const struct qstr *src_name, u64 *src_inum,
+                      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 = bch2_dirent_pos(dst_dir, dst_name);
-       int ret;
+       struct bpos dst_pos =
+               POS(dst_dir, bch2_dirent_hash(dst_hash, dst_name));
+       int ret = 0;
+
+       *src_inum = *dst_inum = 0;
 
        /*
         * Lookup dst:
@@ -197,29 +187,35 @@ int bch2_dirent_rename(struct btree_trans *trans,
         */
        dst_iter = mode == BCH_RENAME
                ? bch2_hash_hole(trans, bch2_dirent_hash_desc,
-                                &dst_dir->ei_str_hash,
-                                dst_dir->v.i_ino, dst_name)
+                                dst_hash, dst_dir, dst_name)
                : bch2_hash_lookup(trans, bch2_dirent_hash_desc,
-                                  &dst_dir->ei_str_hash,
-                                  dst_dir->v.i_ino, dst_name,
+                                  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)
+               *dst_inum = le64_to_cpu(bkey_s_c_to_dirent(old_dst).v->d_inum);
+
        /* Lookup src: */
        src_iter = bch2_hash_lookup(trans, bch2_dirent_hash_desc,
-                                   &src_dir->ei_str_hash,
-                                   src_dir->v.i_ino, src_name,
+                                   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;
@@ -227,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;
 
@@ -254,10 +253,9 @@ int bch2_dirent_rename(struct btree_trans *trans,
                                 * new_dst at the src position:
                                 */
                                new_dst->k.p = src_iter->pos;
-                               bch2_trans_update(trans,
-                                       BTREE_INSERT_ENTRY(src_iter,
-                                                          &new_dst->k_i));
-                               return 0;
+                               bch2_trans_update(trans, src_iter,
+                                                 &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
@@ -269,38 +267,38 @@ int bch2_dirent_rename(struct btree_trans *trans,
                } else {
                        /* Check if we need a whiteout to delete src: */
                        ret = bch2_hash_needs_whiteout(trans, bch2_dirent_hash_desc,
-                                                      &src_dir->ei_str_hash,
-                                                      src_iter);
+                                                      src_hash, src_iter);
                        if (ret < 0)
-                               return ret;
+                               goto out;
 
                        if (ret)
                                new_src->k.type = KEY_TYPE_whiteout;
                }
        }
 
-       bch2_trans_update(trans, BTREE_INSERT_ENTRY(src_iter, &new_src->k_i));
-       bch2_trans_update(trans, BTREE_INSERT_ENTRY(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(struct btree_trans *trans, u64 dir_inum,
-                        const struct bch_hash_info *hash_info,
-                        const struct qstr *name)
+int bch2_dirent_delete_at(struct btree_trans *trans,
+                         const struct bch_hash_info *hash_info,
+                         struct btree_iter *iter)
 {
-       return bch2_hash_delete(trans, bch2_dirent_hash_desc, hash_info,
-                               dir_inum, name);
+       return bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
+                                  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)
+struct btree_iter *
+__bch2_dirent_lookup_trans(struct btree_trans *trans, u64 dir_inum,
+                          const struct bch_hash_info *hash_info,
+                          const struct qstr *name, unsigned flags)
 {
-       return bch2_trans_do(c, journal_seq,
-                            BTREE_INSERT_ATOMIC|
-                            BTREE_INSERT_NOFAIL,
-               __bch2_dirent_delete(&trans, dir_inum, hash_info, name));
+       return bch2_hash_lookup(trans, bch2_dirent_hash_desc,
+                               hash_info, dir_inum, name, flags);
 }
 
 u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
@@ -314,8 +312,8 @@ u64 bch2_dirent_lookup(struct bch_fs *c, u64 dir_inum,
 
        bch2_trans_init(&trans, c, 0, 0);
 
-       iter = bch2_hash_lookup(&trans, bch2_dirent_hash_desc,
-                               hash_info, dir_inum, name, 0);
+       iter = __bch2_dirent_lookup_trans(&trans, dir_inum,
+                                         hash_info, name, 0);
        if (IS_ERR(iter)) {
                BUG_ON(PTR_ERR(iter) == -EINTR);
                goto out;
@@ -349,53 +347,37 @@ int bch2_empty_dir_trans(struct btree_trans *trans, u64 dir_inum)
        return ret;
 }
 
-int bch2_empty_dir(struct bch_fs *c, u64 dir_inum)
+int bch2_readdir(struct bch_fs *c, u64 inum, struct dir_context *ctx)
 {
-       return bch2_trans_do(c, NULL, 0,
-               bch2_empty_dir_trans(&trans, dir_inum));
-}
-
-int bch2_readdir(struct bch_fs *c, struct file *file,
-                struct dir_context *ctx)
-{
-       struct bch_inode_info *inode = file_bch_inode(file);
        struct btree_trans trans;
        struct btree_iter *iter;
        struct bkey_s_c k;
        struct bkey_s_c_dirent dirent;
-       unsigned len;
        int ret;
 
-       if (!dir_emit_dots(file, ctx))
-               return 0;
-
        bch2_trans_init(&trans, c, 0, 0);
 
        for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
-                          POS(inode->v.i_ino, ctx->pos), 0, k, ret) {
+                          POS(inum, ctx->pos), 0, k, ret) {
+               if (k.k->p.inode > inum)
+                       break;
+
                if (k.k->type != KEY_TYPE_dirent)
                        continue;
 
                dirent = bkey_s_c_to_dirent(k);
 
-               if (bkey_cmp(k.k->p, POS(inode->v.i_ino, ctx->pos)) < 0)
-                       continue;
-
-               if (k.k->p.inode > inode->v.i_ino)
-                       break;
-
-               len = bch2_dirent_name_bytes(dirent);
-
                /*
                 * XXX: dir_emit() can fault and block, while we're holding
                 * locks
                 */
-               if (!dir_emit(ctx, dirent.v->d_name, len,
+               ctx->pos = dirent.k->p.offset;
+               if (!dir_emit(ctx, dirent.v->d_name,
+                             bch2_dirent_name_bytes(dirent),
                              le64_to_cpu(dirent.v->d_inum),
                              dirent.v->d_type))
                        break;
-
-               ctx->pos = k.k->p.offset + 1;
+               ctx->pos = dirent.k->p.offset + 1;
        }
        ret = bch2_trans_exit(&trans) ?: ret;