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));
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;
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,
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",
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);
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))
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)) {
/* 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;
}
struct dev_opts {
- int fd;
+ struct block_device *bdev;
char *path;
u64 size; /* bytes*/
u64 bucket_size; /* bytes */
#include <blkid.h>
#include <uuid/uuid.h>
+#include "libbcachefs.h"
#include "libbcachefs/bcachefs_ioctl.h"
#include "linux/sort.h"
#include "tools-util.h"
}
/* 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");
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)
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);