]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/fs-common.c
Update bcachefs sources to 9922afc8b6 bcachefs: Add repair code for out of order...
[bcachefs-tools-debian] / libbcachefs / fs-common.c
index 878419d409927c7d33902993d0f37a4b5357e362..281a6135e59977f7d057c8b7958a85ec87435dfd 100644 (file)
@@ -20,8 +20,10 @@ int bch2_create_trans(struct btree_trans *trans, u64 dir_inum,
 {
        struct bch_fs *c = trans->c;
        struct btree_iter *dir_iter = NULL;
+       struct btree_iter *inode_iter = NULL;
        struct bch_hash_info hash = bch2_hash_info_init(c, new_inode);
-       u64 now = bch2_current_time(trans->c);
+       u64 now = bch2_current_time(c);
+       u64 dir_offset = 0;
        int ret;
 
        dir_iter = bch2_inode_peek(trans, dir_u, dir_inum, BTREE_ITER_INTENT);
@@ -34,9 +36,8 @@ int bch2_create_trans(struct btree_trans *trans, u64 dir_inum,
        if (!name)
                new_inode->bi_flags |= BCH_INODE_UNLINKED;
 
-       ret = bch2_inode_create(trans, new_inode,
-                               BLOCKDEV_INODE_MAX, 0,
-                               &c->unused_inode_hint);
+       inode_iter = bch2_inode_create(trans, new_inode, U32_MAX);
+       ret = PTR_ERR_OR_ZERO(inode_iter);
        if (ret)
                goto err;
 
@@ -68,11 +69,24 @@ int bch2_create_trans(struct btree_trans *trans, u64 dir_inum,
                ret = bch2_dirent_create(trans, dir_inum, &dir_hash,
                                         mode_to_type(new_inode->bi_mode),
                                         name, new_inode->bi_inum,
+                                        &dir_offset,
                                         BCH_HASH_SET_MUST_CREATE);
                if (ret)
                        goto err;
        }
+
+       if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
+               new_inode->bi_dir               = dir_u->bi_inum;
+               new_inode->bi_dir_offset        = dir_offset;
+       }
+
+       /* XXX use bch2_btree_iter_set_snapshot() */
+       inode_iter->snapshot = U32_MAX;
+       bch2_btree_iter_set_pos(inode_iter, SPOS(0, new_inode->bi_inum, U32_MAX));
+
+       ret = bch2_inode_write(trans, inode_iter, new_inode);
 err:
+       bch2_trans_iter_put(trans, inode_iter);
        bch2_trans_iter_put(trans, dir_iter);
        return ret;
 }
@@ -81,9 +95,11 @@ int bch2_link_trans(struct btree_trans *trans, u64 dir_inum,
                    u64 inum, struct bch_inode_unpacked *dir_u,
                    struct bch_inode_unpacked *inode_u, const struct qstr *name)
 {
+       struct bch_fs *c = trans->c;
        struct btree_iter *dir_iter = NULL, *inode_iter = NULL;
        struct bch_hash_info dir_hash;
-       u64 now = bch2_current_time(trans->c);
+       u64 now = bch2_current_time(c);
+       u64 dir_offset = 0;
        int ret;
 
        inode_iter = bch2_inode_peek(trans, inode_u, inum, BTREE_ITER_INTENT);
@@ -94,6 +110,8 @@ int bch2_link_trans(struct btree_trans *trans, u64 dir_inum,
        inode_u->bi_ctime = now;
        bch2_inode_nlink_inc(inode_u);
 
+       inode_u->bi_flags |= BCH_INODE_BACKPTR_UNTRUSTED;
+
        dir_iter = bch2_inode_peek(trans, dir_u, dir_inum, 0);
        ret = PTR_ERR_OR_ZERO(dir_iter);
        if (ret)
@@ -101,12 +119,21 @@ int bch2_link_trans(struct btree_trans *trans, u64 dir_inum,
 
        dir_u->bi_mtime = dir_u->bi_ctime = now;
 
-       dir_hash = bch2_hash_info_init(trans->c, dir_u);
+       dir_hash = bch2_hash_info_init(c, dir_u);
 
-       ret =   bch2_dirent_create(trans, dir_inum, &dir_hash,
-                                 mode_to_type(inode_u->bi_mode),
-                                 name, inum, BCH_HASH_SET_MUST_CREATE) ?:
-               bch2_inode_write(trans, dir_iter, dir_u) ?:
+       ret = bch2_dirent_create(trans, dir_inum, &dir_hash,
+                                mode_to_type(inode_u->bi_mode),
+                                name, inum, &dir_offset,
+                                BCH_HASH_SET_MUST_CREATE);
+       if (ret)
+               goto err;
+
+       if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
+               inode_u->bi_dir         = dir_inum;
+               inode_u->bi_dir_offset  = dir_offset;
+       }
+
+       ret =   bch2_inode_write(trans, dir_iter, dir_u) ?:
                bch2_inode_write(trans, inode_iter, inode_u);
 err:
        bch2_trans_iter_put(trans, dir_iter);
@@ -119,10 +146,11 @@ int bch2_unlink_trans(struct btree_trans *trans,
                      struct bch_inode_unpacked *inode_u,
                      const struct qstr *name)
 {
+       struct bch_fs *c = trans->c;
        struct btree_iter *dir_iter = NULL, *dirent_iter = NULL,
                          *inode_iter = NULL;
        struct bch_hash_info dir_hash;
-       u64 inum, now = bch2_current_time(trans->c);
+       u64 inum, now = bch2_current_time(c);
        struct bkey_s_c k;
        int ret;
 
@@ -131,7 +159,7 @@ int bch2_unlink_trans(struct btree_trans *trans,
        if (ret)
                goto err;
 
-       dir_hash = bch2_hash_info_init(trans->c, dir_u);
+       dir_hash = bch2_hash_info_init(c, dir_u);
 
        dirent_iter = __bch2_dirent_lookup_trans(trans, dir_inum, &dir_hash,
                                                 name, BTREE_ITER_INTENT);
@@ -197,10 +225,12 @@ int bch2_rename_trans(struct btree_trans *trans,
                      const struct qstr *dst_name,
                      enum bch_rename_mode mode)
 {
+       struct bch_fs *c = trans->c;
        struct btree_iter *src_dir_iter = NULL, *dst_dir_iter = NULL;
        struct btree_iter *src_inode_iter = NULL, *dst_inode_iter = NULL;
        struct bch_hash_info src_hash, dst_hash;
-       u64 src_inode, dst_inode, now = bch2_current_time(trans->c);
+       u64 src_inode, src_offset, dst_inode, dst_offset;
+       u64 now = bch2_current_time(c);
        int ret;
 
        src_dir_iter = bch2_inode_peek(trans, src_dir_u, src_dir,
@@ -209,7 +239,7 @@ int bch2_rename_trans(struct btree_trans *trans,
        if (ret)
                goto err;
 
-       src_hash = bch2_hash_info_init(trans->c, src_dir_u);
+       src_hash = bch2_hash_info_init(c, src_dir_u);
 
        if (dst_dir != src_dir) {
                dst_dir_iter = bch2_inode_peek(trans, dst_dir_u, dst_dir,
@@ -218,7 +248,7 @@ int bch2_rename_trans(struct btree_trans *trans,
                if (ret)
                        goto err;
 
-               dst_hash = bch2_hash_info_init(trans->c, dst_dir_u);
+               dst_hash = bch2_hash_info_init(c, dst_dir_u);
        } else {
                dst_dir_u = src_dir_u;
                dst_hash = src_hash;
@@ -227,8 +257,8 @@ int bch2_rename_trans(struct btree_trans *trans,
        ret = bch2_dirent_rename(trans,
                                 src_dir, &src_hash,
                                 dst_dir, &dst_hash,
-                                src_name, &src_inode,
-                                dst_name, &dst_inode,
+                                src_name, &src_inode, &src_offset,
+                                dst_name, &dst_inode, &dst_offset,
                                 mode);
        if (ret)
                goto err;
@@ -247,6 +277,16 @@ int bch2_rename_trans(struct btree_trans *trans,
                        goto err;
        }
 
+       if (c->sb.version >= bcachefs_metadata_version_inode_backpointers) {
+               src_inode_u->bi_dir             = dst_dir_u->bi_inum;
+               src_inode_u->bi_dir_offset      = dst_offset;
+
+               if (mode == BCH_RENAME_EXCHANGE) {
+                       dst_inode_u->bi_dir             = src_dir_u->bi_inum;
+                       dst_inode_u->bi_dir_offset      = src_offset;
+               }
+       }
+
        if (mode == BCH_RENAME_OVERWRITE) {
                if (S_ISDIR(src_inode_u->bi_mode) !=
                    S_ISDIR(dst_inode_u->bi_mode)) {