]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_format.c
convert main() from C to Rust
[bcachefs-tools-debian] / cmd_format.c
index b6889ac1360b18001e03537d9ed87c91948a5f1a..45c44e3229d0f5ff50cf5b21d3bd6d1042267f1c 100644 (file)
@@ -5,6 +5,7 @@
  *
  * GPLv2
  */
+#include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
 
 #include <uuid/uuid.h>
 
-#include "ccan/darray/darray.h"
-
 #include "cmds.h"
 #include "libbcachefs.h"
 #include "crypto.h"
+#include "libbcachefs/darray.h"
+#include "libbcachefs/errcode.h"
 #include "libbcachefs/opts.h"
 #include "libbcachefs/super-io.h"
 #include "libbcachefs/util.h"
 
-#define OPTS                                                                   \
-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|remount-ro|panic)", NULL)            \
-x('L', label,                  "label",                NULL)                   \
-x('U', uuid,                   "uuid",                 NULL)                   \
-x('f', force,                  NULL,                   NULL)                   \
-t("")                                                                          \
-t("Device specific options:")                                                  \
-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("  bcachefs format --tier 0 /dev/sdb --tier 1 /dev/sdc")                     \
-t("")                                                                          \
-x('q', quiet,                  NULL,                   "Only print errors")    \
-x('h', help,                   NULL,                   "Display this help and exit")
+#define OPTS                                           \
+x(0,   replicas,               required_argument)      \
+x(0,   encrypted,              no_argument)            \
+x(0,   no_passphrase,          no_argument)            \
+x('L', fs_label,               required_argument)      \
+x('U', uuid,                   required_argument)      \
+x(0,   fs_size,                required_argument)      \
+x(0,   superblock_size,        required_argument)      \
+x(0,   bucket_size,            required_argument)      \
+x('l', label,                  required_argument)      \
+x(0,   discard,                no_argument)            \
+x(0,   data_allowed,           required_argument)      \
+x(0,   durability,             required_argument)      \
+x(0,   version,                required_argument)      \
+x(0,   no_initialize,          no_argument)            \
+x('f', force,                  no_argument)            \
+x('q', quiet,                  no_argument)            \
+x('v', verbose,                no_argument)            \
+x('h', help,                   no_argument)
 
 static void usage(void)
 {
-#define t(text)                                puts(text "\n")
-#define x(shortopt, longopt, arg, help) do {                           \
-       OPTS
-#undef x
-#undef t
-
        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"
-            "      --btree_node=size       Btree node size, default 256k\n"
-            "      --metadata_checksum_type=(none|crc32c|crc64)\n"
-            "      --data_checksum_type=(none|crc32c|crc64)\n"
-            "      --compression_type=(none|lz4|gzip)\n"
-            "      --data_replicas=#       Number of data replicas\n"
-            "      --metadata_replicas=#   Number of metadata replicas\n"
+            "Options:");
+
+       bch2_opts_usage(OPT_FORMAT);
+
+       puts(
             "      --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|remount-ro|panic)\n"
-            "                              Action to take on filesystem error\n"
-            "  -L, --label=label\n"
+            "  -L, --fs_label=label\n"
             "  -U, --uuid=uuid\n"
-            "  -f, --force\n"
+            "      --superblock_size=size\n"
             "\n"
-            "Device specific options:\n"
-            "      --fs_size=size          Size of filesystem on device\n"
-            "      --bucket=size           Bucket size\n"
-            "      --discard               Enable discards\n"
-            "  -t, --tier=#                Higher tier (e.g. 1) indicates slower devices\n"
+            "Device specific options:");
+
+       bch2_opts_usage(OPT_DEVICE);
+
+       puts("  -l, --label=label           Disk label\n"
             "\n"
+            "  -f, --force\n"
             "  -q, --quiet                 Only print errors\n"
+            "  -v, --verbose               Verbose filesystem initialization\n"
             "  -h, --help                  Display this help and exit\n"
             "\n"
             "Device specific options must come before corresponding devices, e.g.\n"
-            "  bcachefs format --tier 0 /dev/sdb --tier 1 /dev/sdc\n"
+            "  bcachefs format --label cache /dev/sdb /dev/sdc\n"
             "\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
 }
 
 enum {
        O_no_opt = 1,
-#define t(text)
-#define x(shortopt, longopt, arg, help)        O_##longopt,
+#define x(shortopt, longopt, arg)      O_##longopt,
        OPTS
 #undef x
-#undef t
 };
 
-static const struct option format_opts[] = {
-#define t(text)
-#define x(shortopt, longopt, arg, help)        {                               \
-       .name           = #longopt,                                     \
-       .has_arg        = arg ? required_argument : no_argument,        \
-       .flag           = NULL,                                         \
-       .val            = O_##longopt,                                  \
+#define x(shortopt, longopt, arg) {                    \
+       .name           = #longopt,                     \
+       .has_arg        = arg,                          \
+       .flag           = NULL,                         \
+       .val            = O_##longopt,                  \
 },
+static const struct option format_opts[] = {
        OPTS
-#undef x
-#undef t
        { NULL }
 };
+#undef x
 
 u64 read_flag_list_or_die(char *opt, const char * const list[],
                          const char *msg)
@@ -137,59 +114,32 @@ u64 read_flag_list_or_die(char *opt, const char * const list[],
 
 int cmd_format(int argc, char *argv[])
 {
-       darray(struct dev_opts) devices;
+       DARRAY(struct dev_opts) devices = { 0 };
+       DARRAY(char *) device_paths = { 0 };
        struct format_opts opts = format_opts_default();
-       struct dev_opts dev_opts = dev_opts_default(), *dev;
-       bool force = false, no_passphrase = false, quiet = false;
+       struct dev_opts dev_opts = dev_opts_default();
+       bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false;
+       bool unconsumed_dev_option = false;
+       unsigned v;
        int opt;
 
-       darray_init(devices);
+       struct bch_opt_strs fs_opt_strs =
+               bch2_cmdline_opts_get(&argc, argv, OPT_FORMAT);
+       struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
 
        while ((opt = getopt_long(argc, argv,
-                                 "-b:e:L:U:ft:qh",
+                                 "-L:U:g:fqhv",
                                  format_opts,
                                  NULL)) != -1)
                switch (opt) {
-               case O_block_size:
-               case 'b':
-                       opts.block_size =
-                               hatoi_validate(optarg, "block size");
-                       break;
-               case O_btree_node_size:
-                       opts.btree_node_size =
-                               hatoi_validate(optarg, "btree node size");
-                       break;
-               case O_metadata_checksum_type:
-                       opts.meta_csum_type =
-                               read_string_list_or_die(optarg,
-                                               bch2_csum_types, "checksum type");
-                       break;
-               case O_data_checksum_type:
-                       opts.data_csum_type =
-                               read_string_list_or_die(optarg,
-                                               bch2_csum_types, "checksum type");
-                       break;
-               case O_compression_type:
-                       opts.compression_type =
-                               read_string_list_or_die(optarg,
-                                               bch2_compression_types,
-                                               "compression type");
-                       break;
-               case O_data_replicas:
-                       if (kstrtouint(optarg, 10, &opts.data_replicas) ||
-                           opts.data_replicas >= BCH_REPLICAS_MAX)
-                               die("invalid replicas");
-                       break;
-               case O_metadata_replicas:
-                       if (kstrtouint(optarg, 10, &opts.meta_replicas) ||
-                           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)
+                       if (kstrtouint(optarg, 10, &v) ||
+                           !v ||
+                           v > BCH_REPLICAS_MAX)
                                die("invalid replicas");
-                       opts.meta_replicas = opts.data_replicas;
+
+                       opt_set(fs_opts, metadata_replicas, v);
+                       opt_set(fs_opts, data_replicas, v);
                        break;
                case O_encrypted:
                        opts.encrypted = true;
@@ -197,15 +147,9 @@ int cmd_format(int argc, char *argv[])
                case O_no_passphrase:
                        no_passphrase = true;
                        break;
-               case O_error_action:
-               case 'e':
-                       opts.on_error_action =
-                               read_string_list_or_die(optarg,
-                                               bch2_error_actions, "error action");
-                       break;
-               case O_label:
+               case O_fs_label:
                case 'L':
-                       opts.label = strdup(optarg);
+                       opts.label = optarg;
                        break;
                case O_uuid:
                case 'U':
@@ -219,57 +163,107 @@ int cmd_format(int argc, char *argv[])
                case O_fs_size:
                        if (bch2_strtoull_h(optarg, &dev_opts.size))
                                die("invalid filesystem size");
+                       unconsumed_dev_option = true;
+                       break;
+               case O_superblock_size:
+                       if (bch2_strtouint_h(optarg, &opts.superblock_size))
+                               die("invalid filesystem size");
 
-                       dev_opts.size >>= 9;
+                       opts.superblock_size >>= 9;
                        break;
                case O_bucket_size:
-                       dev_opts.bucket_size =
-                               hatoi_validate(optarg, "bucket size");
+                       if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
+                               die("bad bucket_size %s", optarg);
+                       unconsumed_dev_option = true;
                        break;
-               case O_tier:
-               case 't':
-                       if (kstrtouint(optarg, 10, &dev_opts.tier) ||
-                           dev_opts.tier >= BCH_TIER_MAX)
-                               die("invalid tier");
+               case O_label:
+               case 'l':
+                       dev_opts.label = optarg;
+                       unconsumed_dev_option = true;
                        break;
                case O_discard:
                        dev_opts.discard = true;
+                       unconsumed_dev_option = true;
                        break;
                case O_data_allowed:
                        dev_opts.data_allowed =
                                read_flag_list_or_die(optarg,
                                        bch2_data_types, "data type");
+                       unconsumed_dev_option = true;
+                       break;
+               case O_durability:
+                       if (kstrtouint(optarg, 10, &dev_opts.durability) ||
+                           dev_opts.durability > BCH_REPLICAS_MAX)
+                               die("invalid durability");
+                       unconsumed_dev_option = true;
+                       break;
+               case O_version:
+                       if (kstrtouint(optarg, 10, &opts.version))
+                               die("invalid version");
+                       break;
+               case O_no_initialize:
+                       initialize = false;
                        break;
                case O_no_opt:
-                       dev_opts.path = strdup(optarg);
-                       darray_append(devices, dev_opts);
+                       darray_push(&device_paths, optarg);
+                       dev_opts.path = optarg;
+                       darray_push(&devices, dev_opts);
                        dev_opts.size = 0;
+                       unconsumed_dev_option = false;
                        break;
                case O_quiet:
                case 'q':
                        quiet = true;
                        break;
+               case 'v':
+                       verbose = true;
                case O_help:
                case 'h':
                        usage();
                        exit(EXIT_SUCCESS);
                        break;
+               case '?':
+                       exit(EXIT_FAILURE);
+                       break;
                }
 
-       if (!darray_size(devices))
+       if (unconsumed_dev_option)
+               die("Options for devices apply to subsequent devices; got a device option with no device");
+
+       if (opts.version != bcachefs_metadata_version_current)
+               initialize = false;
+
+       if (!devices.nr)
                die("Please supply a device");
 
-       if (opts.encrypted && !no_passphrase)
+       if (opts.encrypted && !no_passphrase) {
                opts.passphrase = read_passphrase_twice("Enter passphrase: ");
+               initialize = false;
+       }
 
-       darray_foreach(dev, devices)
-               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(opts, devices.item, darray_size(devices));
+               bch2_format(fs_opt_strs,
+                           fs_opts,
+                           opts,
+                           devices.data, devices.nr);
+       bch2_opt_strs_free(&fs_opt_strs);
+
+       if (!quiet) {
+               struct printbuf buf = PRINTBUF;
+
+               buf.human_readable_units = true;
+
+               bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members_v2);
+               printf("%s", buf.buf);
 
-       if (!quiet)
-               bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
+               printbuf_exit(&buf);
+       }
        free(sb);
 
        if (opts.passphrase) {
@@ -277,6 +271,30 @@ int cmd_format(int argc, char *argv[])
                free(opts.passphrase);
        }
 
+       darray_exit(&devices);
+
+       if (initialize) {
+               struct bch_opts mount_opts = bch2_opts_empty();
+
+
+               opt_set(mount_opts, verbose, verbose);
+
+               /*
+                * Start the filesystem once, to allocate the journal and create
+                * the root directory:
+                */
+               struct bch_fs *c = bch2_fs_open(device_paths.data,
+                                               device_paths.nr,
+                                               mount_opts);
+               if (IS_ERR(c))
+                       die("error opening %s: %s", device_paths.data[0],
+                           bch2_err_str(PTR_ERR(c)));
+
+               bch2_fs_stop(c);
+       }
+
+       darray_exit(&device_paths);
+
        return 0;
 }
 
@@ -287,9 +305,10 @@ static void show_super_usage(void)
             "\n"
             "Options:\n"
             "  -f, --fields=(fields)       list of sections to print\n"
+            "      --field-only=fiel)      print superblock section only, no header\n"
             "  -l, --layout                print superblock layout\n"
             "  -h, --help                  display this help and exit\n"
-            "Report bugs to <linux-bcache@vger.kernel.org>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
        exit(EXIT_SUCCESS);
 }
 
@@ -297,12 +316,15 @@ int cmd_show_super(int argc, char *argv[])
 {
        static const struct option longopts[] = {
                { "fields",                     1, NULL, 'f' },
+               { "field-only",                 1, NULL, 'F' },
                { "layout",                     0, NULL, 'l' },
                { "help",                       0, NULL, 'h' },
                { NULL }
        };
-       unsigned fields = 1 << BCH_SB_FIELD_members;
+       unsigned fields = 0;
+       int field_only = -1;
        bool print_layout = false;
+       bool print_default_fields = true;
        int opt;
 
        while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
@@ -312,6 +334,12 @@ int cmd_show_super(int argc, char *argv[])
                                ? ~0
                                : read_flag_list_or_die(optarg,
                                        bch2_sb_fields, "superblock field");
+                       print_default_fields = false;
+                       break;
+               case 'F':
+                       field_only = read_string_list_or_die(optarg,
+                                       bch2_sb_fields, "superblock field");
+                       print_default_fields = false;
                        break;
                case 'l':
                        print_layout = true;
@@ -336,9 +364,30 @@ int cmd_show_super(int argc, char *argv[])
        struct bch_sb_handle sb;
        int ret = bch2_read_super(dev, &opts, &sb);
        if (ret)
-               die("Error opening %s: %s", dev, strerror(-ret));
+               die("Error opening %s: %s", dev, bch2_err_str(ret));
+
+       if (print_default_fields) {
+               fields |= bch2_sb_field_get(sb.sb, members_v2)
+                       ? 1 << BCH_SB_FIELD_members_v2
+                       : 1 << BCH_SB_FIELD_members_v1;
+               fields |= 1 << BCH_SB_FIELD_errors;
+       }
+
+       struct printbuf buf = PRINTBUF;
+
+       buf.human_readable_units = true;
+
+       if (field_only >= 0) {
+               struct bch_sb_field *f = bch2_sb_field_get_id(sb.sb, field_only);
+
+               if (f)
+                       __bch2_sb_field_to_text(&buf, sb.sb, f);
+       } else {
+               bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
+       }
+       printf("%s", buf.buf);
 
-       bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
        bch2_free_super(&sb);
+       printbuf_exit(&buf);
        return 0;
 }