From 9f98746bfcd5159307237f7a491fd79db02d8bf3 Mon Sep 17 00:00:00 2001 From: Hunter Shaffer Date: Tue, 29 Aug 2023 18:05:09 -0400 Subject: [PATCH] Change open_for_format to the block io api Upcoming patch will add device benchmarking at format time, which needs the bio API. Signed-off-by: Hunter Shaffer --- cmd_device.c | 8 +++++--- cmd_format.c | 7 +++++-- cmd_migrate.c | 6 +++--- libbcachefs.c | 11 +++++------ libbcachefs.h | 2 +- tools-util.c | 23 +++++++++++++---------- tools-util.h | 3 ++- 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/cmd_device.c b/cmd_device.c index 3d67e3ed..1cb31ab8 100644 --- a/cmd_device.c +++ b/cmd_device.c @@ -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; diff --git a/cmd_format.c b/cmd_format.c index 4b1dcbe2..42f3fc6c 100644 --- a/cmd_format.c +++ b/cmd_format.c @@ -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, diff --git a/cmd_migrate.c b/cmd_migrate.c index efacc50d..f82dd618 100644 --- a/cmd_migrate.c +++ b/cmd_migrate.c @@ -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); diff --git a/libbcachefs.c b/libbcachefs.c index 575cce4d..499badb1 100644 --- a/libbcachefs.c +++ b/libbcachefs.c @@ -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; diff --git a/libbcachefs.h b/libbcachefs.h index ba5d3807..6322d55e 100644 --- a/libbcachefs.h +++ b/libbcachefs.h @@ -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 */ diff --git a/tools-util.c b/tools-util.c index 9b183735..f6e9a478 100644 --- a/tools-util.c +++ b/tools-util.c @@ -17,6 +17,7 @@ #include #include +#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) diff --git a/tools-util.h b/tools-util.h index dafd7916..7a04c108 100644 --- a/tools-util.h +++ b/tools-util.h @@ -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);