X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=tools-util.c;h=5f0b9ba4fd5f7c279763c3f7df9a9da1d0ca0f59;hb=d101ad4a61ce48c498936b28eedcf0e01a568d49;hp=81ce63ae95ba431e161b6bea53227d2f672a27d9;hpb=63747ac6534e3092f703527ba809eb93034bd57b;p=bcachefs-tools-debian diff --git a/tools-util.c b/tools-util.c index 81ce63a..5f0b9ba 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" @@ -50,36 +51,6 @@ char *mprintf(const char *fmt, ...) return str; } -void *xcalloc(size_t count, size_t size) -{ - void *p = calloc(count, size); - - if (!p) - die("insufficient memory"); - - return p; -} - -void *xmalloc(size_t size) -{ - void *p = malloc(size); - - if (!p) - die("insufficient memory"); - - memset(p, 0, size); - return p; -} - -void *xrealloc(void *p, size_t size) -{ - p = realloc(p, size); - if (!p) - die("insufficient memory"); - - return p; -} - void xpread(int fd, void *buf, size_t count, off_t offset) { while (count) { @@ -186,7 +157,7 @@ ssize_t read_string_list_or_die(const char *opt, const char * const list[], } /* Returns size of file or block device: */ -u64 get_size(const char *path, int fd) +u64 get_size(int fd) { struct stat statbuf = xfstat(fd); @@ -199,7 +170,7 @@ u64 get_size(const char *path, int fd) } /* Returns blocksize, in bytes: */ -unsigned get_blocksize(const char *path, int fd) +unsigned get_blocksize(int fd) { struct stat statbuf = xfstat(fd); @@ -212,24 +183,26 @@ unsigned get_blocksize(const char *path, 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); - - if (force) - return fd; + 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 (!(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)) + if (blkid_probe_enable_partitions(pr, true) || + blkid_probe_enable_superblocks(pr, true) || + blkid_probe_set_superblocks_flags(pr, + BLKID_SUBLKS_LABEL|BLKID_SUBLKS_TYPE|BLKID_SUBLKS_MAGIC)) die("blkid error 3"); if (blkid_do_fullprobe(pr) < 0) die("blkid error 4"); @@ -240,19 +213,23 @@ 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); - fputs("Proceed anyway?", stdout); - if (!ask_yn()) - exit(EXIT_FAILURE); - while (blkid_do_probe(pr) == 0) - blkid_do_wipe(pr, 0); + dev->path, fs_type); + if (!force) { + fputs("Proceed anyway?", stdout); + if (!ask_yn()) + exit(EXIT_FAILURE); + } + while (blkid_do_probe(pr) == 0) { + if (blkid_do_wipe(pr, 0)) + die("Failed to wipe preexisting metadata."); + } } blkid_free_probe(pr); - return fd; + return ret; } bool ask_yn(void) @@ -286,14 +263,13 @@ static int range_cmp(const void *_l, const void *_r) void ranges_sort_merge(ranges *r) { - struct range *t, *i; ranges tmp = { 0 }; sort(r->data, r->nr, sizeof(r->data[0]), range_cmp, NULL); /* Merge contiguous ranges: */ darray_for_each(*r, i) { - t = tmp.nr ? &tmp.data[tmp.nr - 1] : NULL; + struct range *t = tmp.nr ? &tmp.data[tmp.nr - 1] : NULL; if (t && t->end >= i->start) t->end = max(t->end, i->end); @@ -307,8 +283,6 @@ void ranges_sort_merge(ranges *r) void ranges_roundup(ranges *r, unsigned block_size) { - struct range *i; - darray_for_each(*r, i) { i->start = round_down(i->start, block_size); i->end = round_up(i->end, block_size); @@ -317,8 +291,6 @@ void ranges_roundup(ranges *r, unsigned block_size) void ranges_rounddown(ranges *r, unsigned block_size) { - struct range *i; - darray_for_each(*r, i) { i->start = round_up(i->start, block_size); i->end = round_down(i->end, block_size); @@ -670,7 +642,7 @@ struct bbpos bbpos_parse(char *buf) if (!(field = strsep(&s, ":"))) die("invalid bbpos %s", buf); - ret.btree = read_string_list_or_die(field, bch2_btree_ids, "btree id"); + ret.btree = read_string_list_or_die(field, __bch2_btree_ids, "btree id"); if (!s) die("invalid bbpos %s", buf); @@ -678,3 +650,17 @@ struct bbpos bbpos_parse(char *buf) ret.pos = bpos_parse(s); return ret; } + +darray_str get_or_split_cmdline_devs(int argc, char *argv[]) +{ + darray_str ret = {}; + + if (argc == 1) { + bch2_split_devs(argv[0], &ret); + } else { + for (unsigned i = 0; i < argc; i++) + darray_push(&ret, strdup(argv[i])); + } + + return ret; +}