]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_format.c
use futex instead of pthread cond variable for schedule
[bcachefs-tools-debian] / cmd_format.c
index 73342596e3256b242610c06d5f16b44e7f34153f..42e8d1a63148f5eac9ea53d6d0d377e614c47e55 100644 (file)
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <blkid.h>
 #include <uuid/uuid.h>
 
 #include "ccan/darray/darray.h"
 
 #include "cmds.h"
-#include "libbcache.h"
+#include "libbcachefs.h"
 #include "crypto.h"
-#include "opts.h"
-#include "util.h"
-
-/* Open a block device, do magic blkid stuff: */
-static int open_for_format(const char *dev, bool force)
-{
-       blkid_probe pr;
-       const char *fs_type = NULL, *fs_label = NULL;
-       size_t fs_type_len, fs_label_len;
-
-       int fd = xopen(dev, O_RDWR|O_EXCL);
-
-       if (force)
-               return fd;
-
-       if (!(pr = blkid_new_probe()))
-               die("blkid error 1");
-       if (blkid_probe_set_device(pr, fd, 0, 0))
-               die("blkid error 2");
-       if (blkid_probe_enable_partitions(pr, true))
-               die("blkid error 3");
-       if (blkid_do_fullprobe(pr) < 0)
-               die("blkid error 4");
-
-       blkid_probe_lookup_value(pr, "TYPE", &fs_type, &fs_type_len);
-       blkid_probe_lookup_value(pr, "LABEL", &fs_label, &fs_label_len);
-
-       if (fs_type) {
-               if (fs_label)
-                       printf("%s contains a %s filesystem labelled '%s'\n",
-                              dev, 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);
-       }
-
-       blkid_free_probe(pr);
-       return fd;
-}
+#include "libbcachefs/opts.h"
+#include "libbcachefs/super-io.h"
+#include "libbcachefs/util.h"
 
 #define OPTS                                                                   \
-t("bcache format - create a new bcache filesystem on one or more devices")     \
-t("Usage: bcache format [OPTION]... <devices>")                                        \
+t("bcachefs format - create a new bcachefs filesystem on one or more devices") \
+t("Usage: bcachefs format [OPTION]... <devices>")                                      \
 t("")                                                                          \
 x('b', block_size,             "size",                 NULL)                   \
 x(0,   btree_node_size,        "size",                 "Default 256k")         \
 x(0,   metadata_checksum_type, "(none|crc32c|crc64)",  NULL)                   \
 x(0,   data_checksum_type,     "(none|crc32c|crc64)",  NULL)                   \
 x(0,   compression_type,       "(none|lz4|gzip)",      NULL)                   \
+x(0,   replicas,               "#",                    NULL)                   \
 x(0,   data_replicas,          "#",                    NULL)                   \
 x(0,   metadata_replicas,      "#",                    NULL)                   \
 x(0,   encrypted,              NULL,                   "Enable whole filesystem encryption (chacha20/poly1305)")\
 x(0,   no_passphrase,          NULL,                   "Don't encrypt master encryption key")\
 x('e', error_action,           "(continue|readonly|panic)", NULL)              \
-x(0,   max_journal_entry_size, "size",                 NULL)                   \
 x('L', label,                  "label",                NULL)                   \
 x('U', uuid,                   "uuid",                 NULL)                   \
 x('f', force,                  NULL,                   NULL)                   \
@@ -92,10 +52,12 @@ x(0,        fs_size,                "size",                 "Size of filesystem on device")\
 x(0,   bucket_size,            "size",                 "Bucket size")          \
 x('t', tier,                   "#",                    "Higher tier indicates slower devices")\
 x(0,   discard,                NULL,                   NULL)                   \
+x(0,   data_allowed,           "journal,btree,data",   "Allowed types of data on this device")\
 t("Device specific options must come before corresponding devices, e.g.")      \
-t("  bcache format --tier 0 /dev/sdb --tier 1 /dev/sdc")                       \
+t("  bcachefs format --tier 0 /dev/sdb --tier 1 /dev/sdc")                     \
 t("")                                                                          \
-x('h', help,                   NULL,                   "display this help and exit")
+x('q', quiet,                  NULL,                   "Only print errors")    \
+x('h', help,                   NULL,                   "Display this help and exit")
 
 static void usage(void)
 {
@@ -105,8 +67,8 @@ static void usage(void)
 #undef x
 #undef t
 
-       puts("bcache format - create a new bcache filesystem on one or more devices\n"
-            "Usage: bcache format [OPTION]... <devices>\n"
+       puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
+            "Usage: bcachefs format [OPTION]... <devices>\n"
             "\n"
             "Options:\n"
             "  -b, --block=size\n"
@@ -116,25 +78,26 @@ static void usage(void)
             "      --compression_type=(none|lz4|gzip)\n"
             "      --data_replicas=#       Number of data replicas\n"
             "      --metadata_replicas=#   Number of metadata replicas\n"
+            "      --replicas=#            Sets both data and metadata replicas\n"
             "      --encrypted             Enable whole filesystem encryption (chacha20/poly1305)\n"
             "      --no_passphrase         Don't encrypt master encryption key\n"
             "      --error_action=(continue|readonly|panic)\n"
             "                              Action to take on filesystem error\n"
-            "      --max_journal_entry_size=size\n"
-            "  -l, --label=label\n"
+            "  -L, --label=label\n"
             "      --uuid=uuid\n"
             "  -f, --force\n"
             "\n"
             "Device specific options:\n"
             "      --fs_size=size          Size of filesystem on device\n"
-            "      --bucket=size           bucket size\n"
+            "      --bucket=size           Bucket size\n"
             "      --discard               Enable discards\n"
-            "  -t, --tier=#                tier of subsequent devices\n"
+            "  -t, --tier=#                Higher tier (e.g. 1) indicates slower devices\n"
             "\n"
-            "  -h, --help                  display this help and exit\n"
+            "  -q, --quiet                 Only print errors\n"
+            "  -h, --help                  Display this help and exit\n"
             "\n"
             "Device specific options must come before corresponding devices, e.g.\n"
-            "  bcache format --tier 0 /dev/sdb --tier 1 /dev/sdc\n"
+            "  bcachefs format --tier 0 /dev/sdb --tier 1 /dev/sdc\n"
             "\n"
             "Report bugs to <linux-bcache@vger.kernel.org>");
 }
@@ -162,23 +125,12 @@ static const struct option format_opts[] = {
        { NULL }
 };
 
-static unsigned hatoi_validate(const char *s, const char *msg)
+u64 read_flag_list_or_die(char *opt, const char * const list[],
+                         const char *msg)
 {
-       u64 v;
-
-       if (bch_strtoull_h(s, &v))
-               die("bad %s %s", msg, s);
-
-       if (v & (v - 1))
-               die("%s must be a power of two", msg);
-
-       v /= 512;
-
-       if (v > USHRT_MAX)
-               die("%s too large\n", msg);
-
-       if (!v)
-               die("%s too small\n", msg);
+       u64 v = bch2_read_flag_list(opt, list);
+       if (v == (u64) -1)
+               die("Bad %s %s", msg, opt);
 
        return v;
 }
@@ -186,15 +138,15 @@ static unsigned hatoi_validate(const char *s, const char *msg)
 int cmd_format(int argc, char *argv[])
 {
        darray(struct dev_opts) devices;
-       struct format_opts opts = format_opts_default();
-       struct dev_opts dev_opts = { 0 }, *dev;
-       bool force = false, no_passphrase = false;
+       struct format_opts opts = format_opts_default();
+       struct dev_opts dev_opts = dev_opts_default(), *dev;
+       bool force = false, no_passphrase = false, quiet = false;
        int opt;
 
        darray_init(devices);
 
        while ((opt = getopt_long(argc, argv,
-                                 "-b:e:L:U:ft:h",
+                                 "-b:e:L:U:ft:qh",
                                  format_opts,
                                  NULL)) != -1)
                switch (opt) {
@@ -210,28 +162,34 @@ int cmd_format(int argc, char *argv[])
                case O_metadata_checksum_type:
                        opts.meta_csum_type =
                                read_string_list_or_die(optarg,
-                                               bch_csum_types, "checksum type");
+                                               bch2_csum_types, "checksum type");
                        break;
                case O_data_checksum_type:
                        opts.data_csum_type =
                                read_string_list_or_die(optarg,
-                                               bch_csum_types, "checksum type");
+                                               bch2_csum_types, "checksum type");
                        break;
                case O_compression_type:
                        opts.compression_type =
                                read_string_list_or_die(optarg,
-                                               bch_compression_types,
+                                               bch2_compression_types,
                                                "compression type");
                        break;
                case O_data_replicas:
                        if (kstrtouint(optarg, 10, &opts.data_replicas) ||
-                           dev_opts.tier >= BCH_REPLICAS_MAX)
+                           opts.data_replicas >= BCH_REPLICAS_MAX)
                                die("invalid replicas");
                        break;
                case O_metadata_replicas:
                        if (kstrtouint(optarg, 10, &opts.meta_replicas) ||
-                           dev_opts.tier >= BCH_REPLICAS_MAX)
+                           opts.meta_replicas >= BCH_REPLICAS_MAX)
+                               die("invalid replicas");
+                       break;
+               case O_replicas:
+                       if (kstrtouint(optarg, 10, &opts.data_replicas) ||
+                           opts.data_replicas >= BCH_REPLICAS_MAX)
                                die("invalid replicas");
+                       opts.meta_replicas = opts.data_replicas;
                        break;
                case O_encrypted:
                        opts.encrypted = true;
@@ -243,11 +201,7 @@ int cmd_format(int argc, char *argv[])
                case 'e':
                        opts.on_error_action =
                                read_string_list_or_die(optarg,
-                                               bch_error_actions, "error action");
-                       break;
-               case O_max_journal_entry_size:
-                       opts.max_journal_entry_size =
-                               hatoi_validate(optarg, "journal entry size");
+                                               bch2_error_actions, "error action");
                        break;
                case O_label:
                case 'L':
@@ -263,7 +217,7 @@ int cmd_format(int argc, char *argv[])
                        force = true;
                        break;
                case O_fs_size:
-                       if (bch_strtoull_h(optarg, &dev_opts.size))
+                       if (bch2_strtoull_h(optarg, &dev_opts.size))
                                die("invalid filesystem size");
 
                        dev_opts.size >>= 9;
@@ -281,11 +235,20 @@ int cmd_format(int argc, char *argv[])
                case O_discard:
                        dev_opts.discard = true;
                        break;
+               case O_data_allowed:
+                       dev_opts.data_allowed =
+                               read_flag_list_or_die(optarg,
+                                       bch2_data_types, "data type");
+                       break;
                case O_no_opt:
                        dev_opts.path = strdup(optarg);
                        darray_append(devices, dev_opts);
                        dev_opts.size = 0;
                        break;
+               case O_quiet:
+               case 'q':
+                       quiet = true;
+                       break;
                case O_help:
                case 'h':
                        usage();
@@ -296,31 +259,17 @@ int cmd_format(int argc, char *argv[])
        if (!darray_size(devices))
                die("Please supply a device");
 
-       if (opts.encrypted && !no_passphrase) {
-               opts.passphrase = read_passphrase("Enter passphrase: ");
-
-               if (isatty(STDIN_FILENO)) {
-                       char *pass2 =
-                               read_passphrase("Enter same passphrase again: ");
-
-                       if (strcmp(opts.passphrase, pass2)) {
-                               memzero_explicit(opts.passphrase,
-                                                strlen(opts.passphrase));
-                               memzero_explicit(pass2, strlen(pass2));
-                               die("Passphrases do not match");
-                       }
-
-                       memzero_explicit(pass2, strlen(pass2));
-                       free(pass2);
-               }
-       }
+       if (opts.encrypted && !no_passphrase)
+               opts.passphrase = read_passphrase_twice("Enter passphrase: ");
 
        darray_foreach(dev, devices)
                dev->fd = open_for_format(dev->path, force);
 
        struct bch_sb *sb =
-               bcache_format(opts, devices.item, darray_size(devices));
-       bcache_super_print(sb, HUMAN_READABLE);
+               bch2_format(opts, devices.item, darray_size(devices));
+
+       if (!quiet)
+               bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
        free(sb);
 
        if (opts.passphrase) {
@@ -330,3 +279,62 @@ int cmd_format(int argc, char *argv[])
 
        return 0;
 }
+
+static void show_super_usage(void)
+{
+       puts("bcachefs show-super \n"
+            "Usage: bcachefs show-super [OPTION].. device\n"
+            "\n"
+            "Options:\n"
+            "  -f, --fields=(fields)       list of sections to print\n"
+            "  -l, --layout                print superblock layout\n"
+            "  -h, --help                  display this help and exit\n"
+            "Report bugs to <linux-bcache@vger.kernel.org>");
+       exit(EXIT_SUCCESS);
+}
+
+int cmd_show_super(int argc, char *argv[])
+{
+       static const struct option longopts[] = {
+               { "fields",                     1, NULL, 'f' },
+               { "layout",                     0, NULL, 'l' },
+               { "help",                       0, NULL, 'h' },
+               { NULL }
+       };
+       unsigned fields = 1 << BCH_SB_FIELD_members;
+       bool print_layout = false;
+       int opt;
+
+       while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
+               switch (opt) {
+               case 'f':
+                       fields = !strcmp(optarg, "all")
+                               ? ~0
+                               : read_flag_list_or_die(optarg,
+                                       bch2_sb_fields, "superblock field");
+                       break;
+               case 'l':
+                       print_layout = true;
+                       break;
+               case 'h':
+                       show_super_usage();
+                       break;
+               }
+       args_shift(optind);
+
+       char *dev = arg_pop();
+       if (!dev)
+               die("please supply a device");
+       if (argc)
+               die("too many arguments");
+
+       struct bch_opts opts = bch2_opts_empty();
+       struct bch_sb_handle sb;
+       int ret = bch2_read_super(dev, &opts, &sb);
+       if (ret)
+               die("Error opening %s: %s", dev, strerror(-ret));
+
+       bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
+       bch2_free_super(&sb);
+       return 0;
+}