2 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
18 #include <sys/types.h>
21 #include <uuid/uuid.h>
24 #include "libbcachefs.h"
26 #include "libbcachefs/darray.h"
27 #include "libbcachefs/errcode.h"
28 #include "libbcachefs/opts.h"
29 #include "libbcachefs/super-io.h"
30 #include "libbcachefs/util.h"
33 x(0, replicas, required_argument) \
34 x(0, encrypted, no_argument) \
35 x(0, no_passphrase, no_argument) \
36 x('L', fs_label, required_argument) \
37 x('U', uuid, required_argument) \
38 x(0, fs_size, required_argument) \
39 x(0, superblock_size, required_argument) \
40 x(0, bucket_size, required_argument) \
41 x('l', label, required_argument) \
42 x(0, discard, no_argument) \
43 x(0, data_allowed, required_argument) \
44 x(0, durability, required_argument) \
45 x(0, version, required_argument) \
46 x(0, no_initialize, no_argument) \
47 x('f', force, no_argument) \
48 x('q', quiet, no_argument) \
49 x('v', verbose, no_argument) \
50 x('h', help, no_argument)
52 static void usage(void)
54 puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
55 "Usage: bcachefs format [OPTION]... <devices>\n"
59 bch2_opts_usage(OPT_FORMAT);
62 " --replicas=# Sets both data and metadata replicas\n"
63 " --encrypted Enable whole filesystem encryption (chacha20/poly1305)\n"
64 " --no_passphrase Don't encrypt master encryption key\n"
65 " -L, --fs_label=label\n"
67 " --superblock_size=size\n"
69 "Device specific options:");
71 bch2_opts_usage(OPT_DEVICE);
73 puts(" -l, --label=label Disk label\n"
76 " -q, --quiet Only print errors\n"
77 " -v, --verbose Verbose filesystem initialization\n"
78 " -h, --help Display this help and exit\n"
80 "Device specific options must come before corresponding devices, e.g.\n"
81 " bcachefs format --label cache /dev/sdb /dev/sdc\n"
83 "Report bugs to <linux-bcachefs@vger.kernel.org>");
88 #define x(shortopt, longopt, arg) O_##longopt,
93 #define x(shortopt, longopt, arg) { \
99 static const struct option format_opts[] = {
105 u64 read_flag_list_or_die(char *opt, const char * const list[],
108 u64 v = bch2_read_flag_list(opt, list);
110 die("Bad %s %s", msg, opt);
115 int cmd_format(int argc, char *argv[])
117 DARRAY(struct dev_opts) devices = { 0 };
118 DARRAY(char *) device_paths = { 0 };
119 struct format_opts opts = format_opts_default();
120 struct dev_opts dev_opts = dev_opts_default(), *dev;
121 bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false;
122 bool unconsumed_dev_option = false;
126 struct bch_opt_strs fs_opt_strs =
127 bch2_cmdline_opts_get(&argc, argv, OPT_FORMAT);
128 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
130 while ((opt = getopt_long(argc, argv,
136 if (kstrtouint(optarg, 10, &v) ||
138 v > BCH_REPLICAS_MAX)
139 die("invalid replicas");
141 opt_set(fs_opts, metadata_replicas, v);
142 opt_set(fs_opts, data_replicas, v);
145 opts.encrypted = true;
147 case O_no_passphrase:
148 no_passphrase = true;
156 if (uuid_parse(optarg, opts.uuid.b))
164 if (bch2_strtoull_h(optarg, &dev_opts.size))
165 die("invalid filesystem size");
166 unconsumed_dev_option = true;
168 case O_superblock_size:
169 if (bch2_strtouint_h(optarg, &opts.superblock_size))
170 die("invalid filesystem size");
172 opts.superblock_size >>= 9;
175 if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
176 die("bad bucket_size %s", optarg);
177 unconsumed_dev_option = true;
181 dev_opts.label = optarg;
182 unconsumed_dev_option = true;
185 dev_opts.discard = true;
186 unconsumed_dev_option = true;
189 dev_opts.data_allowed =
190 read_flag_list_or_die(optarg,
191 bch2_data_types, "data type");
192 unconsumed_dev_option = true;
195 if (kstrtouint(optarg, 10, &dev_opts.durability) ||
196 dev_opts.durability > BCH_REPLICAS_MAX)
197 die("invalid durability");
198 unconsumed_dev_option = true;
201 if (kstrtouint(optarg, 10, &opts.version))
202 die("invalid version");
204 case O_no_initialize:
208 darray_push(&device_paths, optarg);
209 dev_opts.path = optarg;
210 darray_push(&devices, dev_opts);
212 unconsumed_dev_option = false;
230 if (unconsumed_dev_option)
231 die("Options for devices apply to subsequent devices; got a device option with no device");
233 if (opts.version != bcachefs_metadata_version_current)
237 die("Please supply a device");
239 if (opts.encrypted && !no_passphrase) {
240 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
244 darray_for_each(devices, dev) {
245 int ret = open_for_format(dev, force);
247 die("Error opening %s: %s", dev_opts.path, strerror(-ret));
251 bch2_format(fs_opt_strs,
254 devices.data, devices.nr);
255 bch2_opt_strs_free(&fs_opt_strs);
258 struct printbuf buf = PRINTBUF;
260 buf.human_readable_units = true;
262 bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members_v2);
263 printf("%s", buf.buf);
269 if (opts.passphrase) {
270 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
271 free(opts.passphrase);
274 darray_exit(&devices);
277 struct bch_opts mount_opts = bch2_opts_empty();
280 opt_set(mount_opts, verbose, verbose);
283 * Start the filesystem once, to allocate the journal and create
284 * the root directory:
286 struct bch_fs *c = bch2_fs_open(device_paths.data,
290 die("error opening %s: %s", device_paths.data[0],
291 bch2_err_str(PTR_ERR(c)));
296 darray_exit(&device_paths);
301 static void show_super_usage(void)
303 puts("bcachefs show-super \n"
304 "Usage: bcachefs show-super [OPTION].. device\n"
307 " -f, --fields=(fields) list of sections to print\n"
308 " -l, --layout print superblock layout\n"
309 " -h, --help display this help and exit\n"
310 "Report bugs to <linux-bcachefs@vger.kernel.org>");
314 int cmd_show_super(int argc, char *argv[])
316 static const struct option longopts[] = {
317 { "fields", 1, NULL, 'f' },
318 { "layout", 0, NULL, 'l' },
319 { "help", 0, NULL, 'h' },
323 bool print_layout = false;
324 bool print_default_fields = true;
327 while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
330 fields = !strcmp(optarg, "all")
332 : read_flag_list_or_die(optarg,
333 bch2_sb_fields, "superblock field");
334 print_default_fields = false;
345 char *dev = arg_pop();
347 die("please supply a device");
349 die("too many arguments");
351 struct bch_opts opts = bch2_opts_empty();
353 opt_set(opts, noexcl, true);
354 opt_set(opts, nochanges, true);
356 struct bch_sb_handle sb;
357 int ret = bch2_read_super(dev, &opts, &sb);
359 die("Error opening %s: %s", dev, bch2_err_str(ret));
361 if (print_default_fields) {
362 fields |= bch2_sb_field_get(sb.sb, members_v2)
363 ? 1 << BCH_SB_FIELD_members_v2
364 : 1 << BCH_SB_FIELD_members_v1;
365 fields |= BCH_SB_FIELD_errors;
368 struct printbuf buf = PRINTBUF;
370 buf.human_readable_units = true;
372 bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
373 printf("%s", buf.buf);
375 bch2_free_super(&sb);