]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
Change open_for_format to the block io api
authorHunter Shaffer <huntershaffer182456@gmail.com>
Tue, 29 Aug 2023 22:05:09 +0000 (18:05 -0400)
committerHunter Shaffer <huntershaffer182456@gmail.com>
Mon, 9 Oct 2023 01:06:07 +0000 (21:06 -0400)
Upcoming patch will add device benchmarking at format time, which needs
the bio API.

Signed-off-by: Hunter Shaffer <huntershaffer182456@gmail.com>
cmd_device.c
cmd_format.c
cmd_migrate.c
libbcachefs.c
libbcachefs.h
tools-util.c
tools-util.h

index 3d67e3ed00fed9f9bb5634c7c7a697d89c1ac92e..1cb31ab858422f7f5646aeb2ee7d3d7dd38f6c35 100644 (file)
@@ -113,7 +113,9 @@ int cmd_device_add(int argc, char *argv[])
 
        struct bchfs_handle fs = bcache_fs_open(fs_path);
 
-       dev_opts.fd = open_for_format(dev_opts.path, force);
+       int ret = open_for_format(&dev_opts, force);
+       if (ret)
+               die("Error opening %s: %s", dev_opts.path, strerror(-ret));
 
        struct bch_opt_strs fs_opt_strs;
        memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
@@ -130,8 +132,8 @@ int cmd_device_add(int argc, char *argv[])
                                        format_opts,
                                        &dev_opts, 1);
        free(sb);
-       fsync(dev_opts.fd);
-       close(dev_opts.fd);
+       fsync(dev_opts.bdev->bd_buffered_fd);
+       close(dev_opts.bdev->bd_buffered_fd);
 
        bchu_disk_add(fs, dev_opts.path);
        return 0;
index 4b1dcbe26bac957773ee22b26a59e805561787d5..42f3fc6ca4ac822422906a1505a20ff7c6a571ce 100644 (file)
@@ -230,8 +230,11 @@ int cmd_format(int argc, char *argv[])
                initialize = false;
        }
 
-       darray_for_each(devices, dev)
-               dev->fd = open_for_format(dev->path, force);
+       darray_for_each(devices, dev) {
+               int ret = open_for_format(dev, force);
+               if (ret)
+                       die("Error opening %s: %s", dev_opts.path, strerror(-ret));
+       }
 
        struct bch_sb *sb =
                bch2_format(fs_opt_strs,
index efacc50d09d68c1073218579e31971fd00a3a300..f82dd6185ee51f54d67f45ee2523c6aad88cc1d8 100644 (file)
@@ -669,9 +669,9 @@ 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.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",
@@ -682,7 +682,7 @@ static int migrate_fs(const char            *fs_path,
        u64 bcachefs_inum;
        ranges extents = reserve_new_fs_space(file_path,
                                fs_opts.block_size >> 9,
-                               get_size(dev.fd) / 5,
+                               get_size(dev.bdev->bd_buffered_fd) / 5,
                                &bcachefs_inum, stat.st_dev, force);
 
        find_superblock_space(extents, format_opts, &dev);
index 575cce4dae4e8d7de1ab250ace27f2a15a0cc5f6..499badb1429c997922df27ab3849839edc0c0062 100644 (file)
@@ -67,7 +67,7 @@ static u64 min_size(unsigned bucket_size)
 void bch2_pick_bucket_size(struct bch_opts opts, struct dev_opts *dev)
 {
        if (!dev->size)
-               dev->size = get_size(dev->fd);
+               dev->size = get_size(dev->bdev->bd_buffered_fd);
 
        if (!dev->bucket_size) {
                if (dev->size < min_size(opts.block_size))
@@ -154,8 +154,7 @@ struct bch_sb *bch2_format(struct bch_opt_strs      fs_opt_strs,
        unsigned opt_id;
 
        for (i = devs; i < devs + nr_devs; i++)
-               max_dev_block_size = max(max_dev_block_size,
-                                        get_blocksize(i->fd));
+               max_dev_block_size = max(max_dev_block_size, get_blocksize(i->bdev->bd_buffered_fd));
 
        /* calculate block size: */
        if (!opt_defined(fs_opts, block_size)) {
@@ -312,12 +311,12 @@ struct bch_sb *bch2_format(struct bch_opt_strs    fs_opt_strs,
                        /* Zero start of disk */
                        static const char zeroes[BCH_SB_SECTOR << 9];
 
-                       xpwrite(i->fd, zeroes, BCH_SB_SECTOR << 9, 0,
+                       xpwrite(i->bdev->bd_buffered_fd, zeroes, BCH_SB_SECTOR << 9, 0,
                                "zeroing start of disk");
                }
 
-               bch2_super_write(i->fd, sb.sb);
-               close(i->fd);
+               bch2_super_write(i->bdev->bd_buffered_fd, sb.sb);
+               close(i->bdev->bd_buffered_fd);
        }
 
        return sb.sb;
index ba5d380768e48db25daa2438e6240ccaccc8e530..6322d55e7f8bf5f45d80d30f48ab6bf79167fbf5 100644 (file)
@@ -52,7 +52,7 @@ static inline struct format_opts format_opts_default()
 }
 
 struct dev_opts {
-       int             fd;
+       struct block_device *bdev;
        char            *path;
        u64             size;           /* bytes*/
        u64             bucket_size;    /* bytes */
index 9b183735b4dac77e01c5f4dd9446efde0364ad2b..f6e9a4789b3bc143fdb101339a8982a4417bc23a 100644 (file)
@@ -17,6 +17,7 @@
 #include <blkid.h>
 #include <uuid/uuid.h>
 
+#include "libbcachefs.h"
 #include "libbcachefs/bcachefs_ioctl.h"
 #include "linux/sort.h"
 #include "tools-util.h"
@@ -212,22 +213,24 @@ unsigned get_blocksize(int fd)
 }
 
 /* Open a block device, do magic blkid stuff to probe for existing filesystems: */
-int open_for_format(const char *dev, bool force)
+int open_for_format(struct dev_opts *dev, bool force)
 {
        blkid_probe pr;
        const char *fs_type = NULL, *fs_label = NULL;
        size_t fs_type_len, fs_label_len;
 
-       int fd = open(dev, O_RDWR|O_EXCL);
-       if (fd < 0)
-               die("Error opening device to format %s: %m", dev);
+       dev->bdev = blkdev_get_by_path(dev->path, BLK_OPEN_READ|BLK_OPEN_WRITE|BLK_OPEN_EXCL,
+                                      dev, NULL);
+       int ret = PTR_ERR_OR_ZERO(dev->bdev);
+       if (ret < 0)
+               die("Error opening device to format %s: %s", dev->path, strerror(-ret));
 
        if (force)
-               return fd;
+               return 0;
 
        if (!(pr = blkid_new_probe()))
                die("blkid error 1");
-       if (blkid_probe_set_device(pr, fd, 0, 0))
+       if (blkid_probe_set_device(pr, dev->bdev->bd_buffered_fd, 0, 0))
                die("blkid error 2");
        if (blkid_probe_enable_partitions(pr, true))
                die("blkid error 3");
@@ -240,19 +243,19 @@ int open_for_format(const char *dev, bool force)
        if (fs_type) {
                if (fs_label)
                        printf("%s contains a %s filesystem labelled '%s'\n",
-                              dev, fs_type, fs_label);
+                              dev->path, fs_type, fs_label);
                else
                        printf("%s contains a %s filesystem\n",
-                              dev, fs_type);
+                              dev->path, fs_type);
                fputs("Proceed anyway?", stdout);
                if (!ask_yn())
                        exit(EXIT_FAILURE);
                while (blkid_do_probe(pr) == 0)
-                       blkid_do_wipe(pr, 0);
+                       blkid_do_wipe(pr, 0);
        }
 
        blkid_free_probe(pr);
-       return fd;
+       return ret;
 }
 
 bool ask_yn(void)
index dafd79166ade37e9afe5cce71e0e92c007d73c0e..7a04c1080beb9ae5dc0df82ae40579ad3c20c7d4 100644 (file)
@@ -64,7 +64,8 @@ ssize_t read_string_list_or_die(const char *, const char * const[],
 
 u64 get_size(int);
 unsigned get_blocksize(int);
-int open_for_format(const char *, bool);
+struct dev_opts;
+int open_for_format(struct dev_opts *, bool);
 
 bool ask_yn(void);