]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_format.c
fix list_journal for nochanges
[bcachefs-tools-debian] / cmd_format.c
index b955b4164accfdd24fe7e3ab4b0d4a5c9137e04c..2c470408b17251ba17864d32c3aabd75a889a49e 100644 (file)
@@ -5,6 +5,7 @@
  *
  * GPLv2
  */
+#include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
 #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 "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;
-
-       if ((fd = open(dev, O_RDWR|O_EXCL)) == -1)
-               die("Can't open dev %s: %s\n", dev, strerror(errno));
-
-       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.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                                           \
+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)
 {
-       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"
-            "      --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"
-            "      --error_action=(continue|readonly|panic)\n"
-            "                              Action to take on filesystem error\n"
-            "      --max_journal_entry_size=size\n"
-            "  -l, --label=label\n"
-            "      --uuid=uuid\n"
-            "  -f, --force\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"
+            "  -L, --fs_label=label\n"
+            "  -U, --uuid=uuid\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=#                tier of subsequent devices\n"
+            "Device specific options:");
+
+       bch2_opts_usage(OPT_DEVICE);
+
+       puts("  -l, --label=label           Disk label\n"
             "\n"
-            "  -h, --help                  display this help and exit\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"
-            "  bcache 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>");
 }
 
-#define OPTS                                                           \
-       OPT('b',        block_size,             required_argument)      \
-       OPT(0,          btree_node_size,        required_argument)      \
-       OPT(0,          metadata_checksum_type, required_argument)      \
-       OPT(0,          data_checksum_type,     required_argument)      \
-       OPT(0,          compression_type,       required_argument)      \
-       OPT('e',        error_action,           required_argument)      \
-       OPT(0,          max_journal_entry_size, required_argument)      \
-       OPT('L',        label,                  required_argument)      \
-       OPT('U',        uuid,                   required_argument)      \
-       OPT('f',        force,                  no_argument)            \
-       OPT(0,          fs_size,                required_argument)      \
-       OPT(0,          bucket_size,            required_argument)      \
-       OPT('t',        tier,                   required_argument)      \
-       OPT(0,          discard,                no_argument)            \
-       OPT('h',        help,                   no_argument)
-
 enum {
-       Opt_no_opt = 1,
-#define OPT(shortopt, longopt, has_arg)        Opt_##longopt,
+       O_no_opt = 1,
+#define x(shortopt, longopt, arg)      O_##longopt,
        OPTS
-#undef OPT
+#undef x
 };
 
+#define x(shortopt, longopt, arg) {                    \
+       .name           = #longopt,                     \
+       .has_arg        = arg,                          \
+       .flag           = NULL,                         \
+       .val            = O_##longopt,                  \
+},
 static const struct option format_opts[] = {
-#define OPT(shortopt, longopt, has_arg)        {                               \
-               #longopt,  has_arg, NULL, Opt_##longopt                 \
-       },
        OPTS
-#undef OPT
        { NULL }
 };
+#undef x
 
-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;
 }
 
 int cmd_format(int argc, char *argv[])
 {
-       darray(struct dev_opts) devices;
-       struct dev_opts *dev;
-       unsigned block_size = 0;
-       unsigned btree_node_size = 0;
-       unsigned meta_csum_type = BCH_CSUM_CRC32C;
-       unsigned data_csum_type = BCH_CSUM_CRC32C;
-       unsigned compression_type = BCH_COMPRESSION_NONE;
-       unsigned on_error_action = BCH_ON_ERROR_RO;
-       char *label = NULL;
-       uuid_le uuid;
-       bool force = false;
-
-       /* Device specific options: */
-       u64 filesystem_size = 0;
-       unsigned bucket_size = 0;
-       unsigned tier = 0;
-       bool discard = false;
-       unsigned max_journal_entry_size = 0;
-       char *passphrase = NULL;
+       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();
+       bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false;
+       bool unconsumed_dev_option = false;
+       unsigned v;
        int opt;
 
-       darray_init(devices);
-       uuid_clear(uuid.b);
+       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:h",
+                                 "-L:U:g:fqhv",
                                  format_opts,
                                  NULL)) != -1)
                switch (opt) {
-               case Opt_block_size:
-               case 'b':
-                       block_size = hatoi_validate(optarg,
-                                               "block size");
+               case O_replicas:
+                       if (kstrtouint(optarg, 10, &v) ||
+                           !v ||
+                           v > BCH_REPLICAS_MAX)
+                               die("invalid replicas");
+
+                       opt_set(fs_opts, metadata_replicas, v);
+                       opt_set(fs_opts, data_replicas, v);
                        break;
-               case Opt_btree_node_size:
-                       btree_node_size = hatoi_validate(optarg,
-                                               "btree node size");
+               case O_encrypted:
+                       opts.encrypted = true;
                        break;
-               case Opt_metadata_checksum_type:
-                       meta_csum_type = read_string_list_or_die(optarg,
-                                               bch_csum_types, "checksum type");
+               case O_no_passphrase:
+                       no_passphrase = true;
                        break;
-               case Opt_data_checksum_type:
-                       data_csum_type = read_string_list_or_die(optarg,
-                                               bch_csum_types, "checksum type");
-                       break;
-               case Opt_compression_type:
-                       compression_type = read_string_list_or_die(optarg,
-                                               bch_compression_types,
-                                               "compression type");
-                       break;
-               case Opt_error_action:
-               case 'e':
-                       on_error_action = read_string_list_or_die(optarg,
-                                               bch_error_actions, "error action");
-                       break;
-               case Opt_max_journal_entry_size:
-                       max_journal_entry_size = hatoi_validate(optarg,
-                                               "journal entry size");
-                       break;
-               case Opt_label:
+               case O_fs_label:
                case 'L':
-                       label = strdup(optarg);
+                       opts.label = optarg;
                        break;
-               case Opt_uuid:
+               case O_uuid:
                case 'U':
-                       if (uuid_parse(optarg, uuid.b))
+                       if (uuid_parse(optarg, opts.uuid.b))
                                die("Bad uuid");
                        break;
-               case Opt_force:
+               case O_force:
                case 'f':
                        force = true;
                        break;
-               case Opt_fs_size:
-                       if (bch_strtoull_h(optarg, &filesystem_size))
+               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");
 
-                       filesystem_size >>= 9;
+                       opts.superblock_size >>= 9;
+                       break;
+               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 Opt_bucket_size:
-                       bucket_size = hatoi_validate(optarg, "bucket size");
+               case O_version:
+                       if (kstrtouint(optarg, 10, &opts.version))
+                               die("invalid version");
                        break;
-               case Opt_tier:
-               case 't':
-                       if (kstrtouint(optarg, 10, &tier) ||
-                           tier >= CACHE_TIERS)
-                               die("invalid tier");
+               case O_no_initialize:
+                       initialize = false;
                        break;
-               case Opt_discard:
-                       discard = true;
+               case O_no_opt:
+                       darray_push(&device_paths, optarg);
+                       dev_opts.path = optarg;
+                       darray_push(&devices, dev_opts);
+                       dev_opts.size = 0;
+                       unconsumed_dev_option = false;
                        break;
-               case Opt_no_opt:
-                       darray_append(devices, (struct dev_opts) {
-                               .path                   = strdup(optarg),
-                               .size                   = filesystem_size,
-                               .bucket_size            = bucket_size,
-                               .tier                   = tier,
-                               .discard                = discard,
-                       });
+               case O_quiet:
+               case 'q':
+                       quiet = true;
                        break;
-               case Opt_help:
+               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 (uuid_is_null(uuid.b))
-               uuid_generate(uuid.b);
-
-       darray_foreach(dev, devices)
-               dev->fd = open_for_format(dev->path, force);
-
-       bcache_format(devices.item, darray_size(devices),
-                     block_size,
-                     btree_node_size,
-                     meta_csum_type,
-                     data_csum_type,
-                     compression_type,
-                     1,
-                     1,
-                     on_error_action,
-                     max_journal_entry_size,
-                     label,
-                     uuid);
-
-       if (passphrase) {
-               memzero_explicit(passphrase, strlen(passphrase));
-               free(passphrase);
+       if (opts.encrypted && !no_passphrase) {
+               opts.passphrase = read_passphrase_twice("Enter passphrase: ");
+               initialize = false;
+       }
+
+       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.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);
+
+               printbuf_exit(&buf);
+       }
+       free(sb);
+
+       if (opts.passphrase) {
+               memzero_explicit(opts.passphrase, strlen(opts.passphrase));
+               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;
+}
+
+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-bcachefs@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 = 0;
+       bool print_layout = false;
+       bool print_default_fields = true;
+       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");
+                       print_default_fields = false;
+                       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();
+
+       opt_set(opts, noexcl,   true);
+       opt_set(opts, nochanges, true);
+
+       struct bch_sb_handle sb;
+       int ret = bch2_read_super(dev, &opts, &sb);
+       if (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;
+       }
+
+       struct printbuf buf = PRINTBUF;
+
+       buf.human_readable_units = true;
+
+       bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
+       printf("%s", buf.buf);
+
+       bch2_free_super(&sb);
+       printbuf_exit(&buf);
        return 0;
 }