]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_format.c
packaging: Update RPM spec to use %_libexecdir for libexec files
[bcachefs-tools-debian] / cmd_format.c
index e57553d862ac417717e96d1044dec6138546b057..2c470408b17251ba17864d32c3aabd75a889a49e 100644 (file)
 
 #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"
@@ -46,6 +46,7 @@ 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)
@@ -73,12 +74,13 @@ static void usage(void)
             "\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 --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 {
@@ -112,23 +114,21 @@ 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(char *) device_paths;
+       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, initialize = true;
+       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);
-       darray_init(device_paths);
-
        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,
-                                 "-L:U:g:fqh",
+                                 "-L:U:g:fqhv",
                                  format_opts,
                                  NULL)) != -1)
                switch (opt) {
@@ -163,6 +163,7 @@ 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))
@@ -173,23 +174,28 @@ int cmd_format(int argc, char *argv[])
                case O_bucket_size:
                        if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
                                die("bad bucket_size %s", optarg);
+                       unconsumed_dev_option = true;
                        break;
                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))
@@ -199,15 +205,18 @@ int cmd_format(int argc, char *argv[])
                        initialize = false;
                        break;
                case O_no_opt:
-                       darray_append(device_paths, optarg);
+                       darray_push(&device_paths, optarg);
                        dev_opts.path = optarg;
-                       darray_append(devices, dev_opts);
+                       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();
@@ -218,7 +227,13 @@ int cmd_format(int argc, char *argv[])
                        break;
                }
 
-       if (darray_empty(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) {
@@ -226,24 +241,28 @@ int cmd_format(int argc, char *argv[])
                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(fs_opt_strs,
                            fs_opts,
                            opts,
-                           devices.item, darray_size(devices));
+                           devices.data, devices.nr);
        bch2_opt_strs_free(&fs_opt_strs);
 
        if (!quiet) {
-               char buf[4096];
-               struct printbuf out = PBUF(buf);
+               struct printbuf buf = PRINTBUF;
 
-               out.units = PRINTBUF_UNITS_HUMAN_READABLE;
+               buf.human_readable_units = true;
 
-               bch2_sb_to_text(&PBUF(buf), sb, false, 1 << BCH_SB_FIELD_members);
-               printf("%s", buf);
+               bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members_v2);
+               printf("%s", buf.buf);
+
+               printbuf_exit(&buf);
        }
        free(sb);
 
@@ -252,24 +271,29 @@ int cmd_format(int argc, char *argv[])
                free(opts.passphrase);
        }
 
-       darray_free(devices);
+       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.item,
-                                               darray_size(device_paths),
-                                               bch2_opts_empty());
+               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.item[0],
-                           strerror(-PTR_ERR(c)));
+                       die("error opening %s: %s", device_paths.data[0],
+                           bch2_err_str(PTR_ERR(c)));
 
                bch2_fs_stop(c);
        }
 
-       darray_free(device_paths);
+       darray_exit(&device_paths);
 
        return 0;
 }
@@ -283,7 +307,7 @@ static void show_super_usage(void)
             "  -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>");
+            "Report bugs to <linux-bcachefs@vger.kernel.org>");
        exit(EXIT_SUCCESS);
 }
 
@@ -295,8 +319,9 @@ int cmd_show_super(int argc, char *argv[])
                { "help",                       0, NULL, 'h' },
                { NULL }
        };
-       unsigned fields = 1 << BCH_SB_FIELD_members;
+       unsigned fields = 0;
        bool print_layout = false;
+       bool print_default_fields = true;
        int opt;
 
        while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
@@ -306,6 +331,7 @@ 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 'l':
                        print_layout = true;
@@ -330,16 +356,23 @@ 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 |= BCH_SB_FIELD_errors;
+       }
 
-       char buf[4096 << 2];
-       struct printbuf out = PBUF(buf);
+       struct printbuf buf = PRINTBUF;
 
-       out.units = PRINTBUF_UNITS_HUMAN_READABLE;
+       buf.human_readable_units = true;
 
-       bch2_sb_to_text(&PBUF(buf), sb.sb, print_layout, fields);
-       printf("%s", buf);
+       bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
+       printf("%s", buf.buf);
 
        bch2_free_super(&sb);
+       printbuf_exit(&buf);
        return 0;
 }