]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - tools-util.c
Merge remote-tracking branch 'amoz/devel'
[bcachefs-tools-debian] / tools-util.c
index 9b183735b4dac77e01c5f4dd9446efde0364ad2b..a1e67dd51ad12fc38f64c1fc5254ac3640bdb85a 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,24 +213,26 @@ 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);
-
-       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 +243,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)
@@ -670,7 +677,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);