mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-02-23 00:00:02 +03:00
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 <huntershaffer182456@gmail.com>
This commit is contained in:
parent
b2ffa12074
commit
9f98746bfc
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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 */
|
||||
|
21
tools-util.c
21
tools-util.c
@ -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,10 +243,10 @@ 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);
|
||||
@ -252,7 +255,7 @@ int open_for_format(const char *dev, bool force)
|
||||
}
|
||||
|
||||
blkid_free_probe(pr);
|
||||
return fd;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ask_yn(void)
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user