]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_migrate.c
Update bcachefs sources to 1a739db0b256 bcachefs; guard against overflow in btree...
[bcachefs-tools-debian] / cmd_migrate.c
index a553319be2a7d500f3f132ce5003d65fd7c77187..8b2841ab895beecb4b5d89df47890660faf52224 100644 (file)
 #include "libbcachefs/btree_update.h"
 #include "libbcachefs/buckets.h"
 #include "libbcachefs/dirent.h"
+#include "libbcachefs/errcode.h"
 #include "libbcachefs/fs-common.h"
 #include "libbcachefs/inode.h"
-#include "libbcachefs/io.h"
+#include "libbcachefs/io_write.h"
 #include "libbcachefs/replicas.h"
 #include "libbcachefs/str_hash.h"
 #include "libbcachefs/super.h"
@@ -122,12 +123,12 @@ static void update_inode(struct bch_fs *c,
        struct bkey_inode_buf packed;
        int ret;
 
-       bch2_inode_pack(c, &packed, inode);
+       bch2_inode_pack(&packed, inode);
        packed.inode.k.p.snapshot = U32_MAX;
        ret = bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
-                               NULL, NULL, 0);
+                               NULL, 0);
        if (ret)
-               die("error updating inode: %s", strerror(-ret));
+               die("error updating inode: %s", bch2_err_str(ret));
 }
 
 static void create_link(struct bch_fs *c,
@@ -139,11 +140,11 @@ static void create_link(struct bch_fs *c,
        struct bch_inode_unpacked inode;
 
        int ret = bch2_trans_do(c, NULL, NULL, 0,
-               bch2_link_trans(&trans,
+               bch2_link_trans(trans,
                                (subvol_inum) { 1, parent->bi_inum }, &parent_u,
                                (subvol_inum) { 1, inum }, &inode, &qstr));
        if (ret)
-               die("error creating hardlink: %s", strerror(-ret));
+               die("error creating hardlink: %s", bch2_err_str(ret));
 }
 
 static struct bch_inode_unpacked create_file(struct bch_fs *c,
@@ -158,13 +159,13 @@ static struct bch_inode_unpacked create_file(struct bch_fs *c,
        bch2_inode_init_early(c, &new_inode);
 
        int ret = bch2_trans_do(c, NULL, NULL, 0,
-               bch2_create_trans(&trans,
+               bch2_create_trans(trans,
                                  (subvol_inum) { 1, parent->bi_inum }, parent,
                                  &new_inode, &qstr,
                                  uid, gid, mode, rdev, NULL, NULL,
                                  (subvol_inum) {}, 0));
        if (ret)
-               die("error creating %s: %s", name, strerror(-ret));
+               die("error creating %s: %s", name, bch2_err_str(ret));
 
        return new_inode;
 }
@@ -228,14 +229,15 @@ static void copy_xattrs(struct bch_fs *c, struct bch_inode_unpacked *dst,
                        die("error getting xattr val: %m");
 
                const struct xattr_handler *h = xattr_resolve_name(&attr);
+               struct bch_inode_unpacked inode_u;
 
                int ret = bch2_trans_do(c, NULL, NULL, 0,
-                               bch2_xattr_set(&trans,
+                               bch2_xattr_set(trans,
                                               (subvol_inum) { 1, dst->bi_inum },
-                                              &hash_info, attr,
+                                              &inode_u, &hash_info, attr,
                                               val, val_size, h->flags, 0));
                if (ret < 0)
-                       die("error creating xattr: %s", strerror(-ret));
+                       die("error creating xattr: %s", bch2_err_str(ret));
        }
 }
 
@@ -257,7 +259,7 @@ static void write_data(struct bch_fs *c,
 
        closure_init_stack(&cl);
 
-       bio_init(&op.wbio.bio, bv, ARRAY_SIZE(bv));
+       bio_init(&op.wbio.bio, NULL, bv, ARRAY_SIZE(bv), 0);
        bch2_bio_map(&op.wbio.bio, buf, len);
 
        bch2_write_op_init(&op, c, bch2_opts_to_inode_opts(c->opts));
@@ -265,14 +267,14 @@ static void write_data(struct bch_fs *c,
        op.nr_replicas  = 1;
        op.subvol       = 1;
        op.pos          = SPOS(dst_inode->bi_inum, dst_offset >> 9, U32_MAX);
+       op.flags |= BCH_WRITE_SYNC;
 
        int ret = bch2_disk_reservation_get(c, &op.res, len >> 9,
                                            c->opts.data_replicas, 0);
        if (ret)
-               die("error reserving space in new filesystem: %s", strerror(-ret));
+               die("error reserving space in new filesystem: %s", bch2_err_str(ret));
 
        closure_call(&op.cl, bch2_write, NULL, &cl);
-       closure_sync(&cl);
 
        dst_inode->bi_sectors += len >> 9;
 }
@@ -310,7 +312,7 @@ static void link_data(struct bch_fs *c, struct bch_inode_unpacked *dst,
 
        while (length) {
                struct bkey_i_extent *e;
-               __BKEY_PADDED(k, BKEY_EXTENT_VAL_U64s_MAX) k;
+               BKEY_PADDED_ONSTACK(k, BKEY_EXTENT_VAL_U64s_MAX) k;
                u64 b = sector_to_bucket(ca, physical);
                struct disk_reservation res;
                unsigned sectors;
@@ -335,12 +337,11 @@ static void link_data(struct bch_fs *c, struct bch_inode_unpacked *dst,
                                                BCH_DISK_RESERVATION_NOFAIL);
                if (ret)
                        die("error reserving space in new filesystem: %s",
-                           strerror(-ret));
+                           bch2_err_str(ret));
 
-               ret = bch2_btree_insert(c, BTREE_ID_extents, &e->k_i,
-                                       &res, NULL, 0);
+               ret = bch2_btree_insert(c, BTREE_ID_extents, &e->k_i, &res, 0);
                if (ret)
-                       die("btree insert error %s", strerror(-ret));
+                       die("btree insert error %s", bch2_err_str(ret));
 
                bch2_disk_reservation_put(c, &res);
 
@@ -581,7 +582,7 @@ static void copy_fs(struct bch_fs *c, int src_fd, const char *src_path,
        int ret = bch2_inode_find_by_inum(c, (subvol_inum) { 1, BCACHEFS_ROOT_INO },
                                          &root_inode);
        if (ret)
-               die("error looking up root directory: %s", strerror(-ret));
+               die("error looking up root directory: %s", bch2_err_str(ret));
 
        if (fchdir(src_fd))
                die("chdir error: %m");
@@ -611,8 +612,6 @@ static void find_superblock_space(ranges extents,
                                  struct format_opts opts,
                                  struct dev_opts *dev)
 {
-       struct range *i;
-
        darray_for_each(extents, i) {
                u64 start = round_up(max(256ULL << 10, i->start),
                                     dev->bucket_size << 9);
@@ -641,7 +640,7 @@ static void migrate_usage(void)
             "      --no_passphrase    Don't encrypt master encryption key\n"
             "  -F                     Force, even if metadata file already exists\n"
             "  -h                     Display this help and exit\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
 }
 
 static const struct option migrate_opts[] = {
@@ -668,20 +667,24 @@ static int migrate_fs(const char          *fs_path,
        struct dev_opts dev = dev_opts_default();
 
        dev.path = dev_t_to_path(stat.st_dev);
-       dev.fd = xopen(dev.path, O_RDWR);
+       dev.bdev = blkdev_get_by_path(dev.path, BLK_OPEN_READ|BLK_OPEN_WRITE, &dev, NULL);
 
-       opt_set(fs_opts, block_size, get_blocksize(dev.path, dev.fd));
+       opt_set(fs_opts, block_size, get_blocksize(dev.bdev->bd_buffered_fd));
 
        char *file_path = mprintf("%s/bcachefs", fs_path);
        printf("Creating new filesystem on %s in space reserved at %s\n",
               dev.path, file_path);
 
-       bch2_pick_bucket_size(fs_opts, &dev);
+       dev.size        = get_size(dev.bdev->bd_buffered_fd);
+       dev.bucket_size = bch2_pick_bucket_size(fs_opts, &dev);
+       dev.nbuckets    = dev.size / dev.bucket_size;
+
+       bch2_check_bucket_size(fs_opts, &dev);
 
        u64 bcachefs_inum;
        ranges extents = reserve_new_fs_space(file_path,
                                fs_opts.block_size >> 9,
-                               get_size(dev.path, dev.fd) / 5,
+                               get_size(dev.bdev->bd_buffered_fd) / 5,
                                &bcachefs_inum, stat.st_dev, force);
 
        find_superblock_space(extents, format_opts, &dev);
@@ -706,13 +709,13 @@ static int migrate_fs(const char          *fs_path,
 
        c = bch2_fs_open(path, 1, opts);
        if (IS_ERR(c))
-               die("Error opening new filesystem: %s", strerror(-PTR_ERR(c)));
+               die("Error opening new filesystem: %s", bch2_err_str(PTR_ERR(c)));
 
        mark_unreserved_space(c, extents);
 
        int ret = bch2_fs_start(c);
        if (ret)
-               die("Error starting new filesystem: %s", strerror(-ret));
+               die("Error starting new filesystem: %s", bch2_err_str(ret));
 
        copy_fs(c, fs_fd, fs_path, bcachefs_inum, &extents);
 
@@ -724,7 +727,7 @@ static int migrate_fs(const char            *fs_path,
 
        c = bch2_fs_open(path, 1, opts);
        if (IS_ERR(c))
-               die("Error opening new filesystem: %s", strerror(-PTR_ERR(c)));
+               die("Error opening new filesystem: %s", bch2_err_str(PTR_ERR(c)));
 
        bch2_fs_stop(c);
        printf("fsck complete\n");
@@ -799,7 +802,7 @@ static void migrate_superblock_usage(void)
             "  -d device     Device to create superblock for\n"
             "  -o offset     Offset of existing superblock\n"
             "  -h            Display this help and exit\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
 }
 
 int cmd_migrate_superblock(int argc, char *argv[])