]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_format.c
45c44e3229d0f5ff50cf5b21d3bd6d1042267f1c
[bcachefs-tools-debian] / cmd_format.c
1 /*
2  * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3  *          Gabriel de Perthuis <g2p.code@gmail.com>
4  *          Jacob Malevich <jam@datera.io>
5  *
6  * GPLv2
7  */
8 #include <ctype.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <getopt.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #include <uuid/uuid.h>
22
23 #include "cmds.h"
24 #include "libbcachefs.h"
25 #include "crypto.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"
31
32 #define OPTS                                            \
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)
51
52 static void usage(void)
53 {
54         puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
55              "Usage: bcachefs format [OPTION]... <devices>\n"
56              "\n"
57              "Options:");
58
59         bch2_opts_usage(OPT_FORMAT);
60
61         puts(
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"
66              "  -U, --uuid=uuid\n"
67              "      --superblock_size=size\n"
68              "\n"
69              "Device specific options:");
70
71         bch2_opts_usage(OPT_DEVICE);
72
73         puts("  -l, --label=label           Disk label\n"
74              "\n"
75              "  -f, --force\n"
76              "  -q, --quiet                 Only print errors\n"
77              "  -v, --verbose               Verbose filesystem initialization\n"
78              "  -h, --help                  Display this help and exit\n"
79              "\n"
80              "Device specific options must come before corresponding devices, e.g.\n"
81              "  bcachefs format --label cache /dev/sdb /dev/sdc\n"
82              "\n"
83              "Report bugs to <linux-bcachefs@vger.kernel.org>");
84 }
85
86 enum {
87         O_no_opt = 1,
88 #define x(shortopt, longopt, arg)       O_##longopt,
89         OPTS
90 #undef x
91 };
92
93 #define x(shortopt, longopt, arg) {                     \
94         .name           = #longopt,                     \
95         .has_arg        = arg,                          \
96         .flag           = NULL,                         \
97         .val            = O_##longopt,                  \
98 },
99 static const struct option format_opts[] = {
100         OPTS
101         { NULL }
102 };
103 #undef x
104
105 u64 read_flag_list_or_die(char *opt, const char * const list[],
106                           const char *msg)
107 {
108         u64 v = bch2_read_flag_list(opt, list);
109         if (v == (u64) -1)
110                 die("Bad %s %s", msg, opt);
111
112         return v;
113 }
114
115 int cmd_format(int argc, char *argv[])
116 {
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();
121         bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false;
122         bool unconsumed_dev_option = false;
123         unsigned v;
124         int opt;
125
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);
129
130         while ((opt = getopt_long(argc, argv,
131                                   "-L:U:g:fqhv",
132                                   format_opts,
133                                   NULL)) != -1)
134                 switch (opt) {
135                 case O_replicas:
136                         if (kstrtouint(optarg, 10, &v) ||
137                             !v ||
138                             v > BCH_REPLICAS_MAX)
139                                 die("invalid replicas");
140
141                         opt_set(fs_opts, metadata_replicas, v);
142                         opt_set(fs_opts, data_replicas, v);
143                         break;
144                 case O_encrypted:
145                         opts.encrypted = true;
146                         break;
147                 case O_no_passphrase:
148                         no_passphrase = true;
149                         break;
150                 case O_fs_label:
151                 case 'L':
152                         opts.label = optarg;
153                         break;
154                 case O_uuid:
155                 case 'U':
156                         if (uuid_parse(optarg, opts.uuid.b))
157                                 die("Bad uuid");
158                         break;
159                 case O_force:
160                 case 'f':
161                         force = true;
162                         break;
163                 case O_fs_size:
164                         if (bch2_strtoull_h(optarg, &dev_opts.size))
165                                 die("invalid filesystem size");
166                         unconsumed_dev_option = true;
167                         break;
168                 case O_superblock_size:
169                         if (bch2_strtouint_h(optarg, &opts.superblock_size))
170                                 die("invalid filesystem size");
171
172                         opts.superblock_size >>= 9;
173                         break;
174                 case O_bucket_size:
175                         if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
176                                 die("bad bucket_size %s", optarg);
177                         unconsumed_dev_option = true;
178                         break;
179                 case O_label:
180                 case 'l':
181                         dev_opts.label = optarg;
182                         unconsumed_dev_option = true;
183                         break;
184                 case O_discard:
185                         dev_opts.discard = true;
186                         unconsumed_dev_option = true;
187                         break;
188                 case O_data_allowed:
189                         dev_opts.data_allowed =
190                                 read_flag_list_or_die(optarg,
191                                         bch2_data_types, "data type");
192                         unconsumed_dev_option = true;
193                         break;
194                 case O_durability:
195                         if (kstrtouint(optarg, 10, &dev_opts.durability) ||
196                             dev_opts.durability > BCH_REPLICAS_MAX)
197                                 die("invalid durability");
198                         unconsumed_dev_option = true;
199                         break;
200                 case O_version:
201                         if (kstrtouint(optarg, 10, &opts.version))
202                                 die("invalid version");
203                         break;
204                 case O_no_initialize:
205                         initialize = false;
206                         break;
207                 case O_no_opt:
208                         darray_push(&device_paths, optarg);
209                         dev_opts.path = optarg;
210                         darray_push(&devices, dev_opts);
211                         dev_opts.size = 0;
212                         unconsumed_dev_option = false;
213                         break;
214                 case O_quiet:
215                 case 'q':
216                         quiet = true;
217                         break;
218                 case 'v':
219                         verbose = true;
220                 case O_help:
221                 case 'h':
222                         usage();
223                         exit(EXIT_SUCCESS);
224                         break;
225                 case '?':
226                         exit(EXIT_FAILURE);
227                         break;
228                 }
229
230         if (unconsumed_dev_option)
231                 die("Options for devices apply to subsequent devices; got a device option with no device");
232
233         if (opts.version != bcachefs_metadata_version_current)
234                 initialize = false;
235
236         if (!devices.nr)
237                 die("Please supply a device");
238
239         if (opts.encrypted && !no_passphrase) {
240                 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
241                 initialize = false;
242         }
243
244         darray_for_each(devices, dev) {
245                 int ret = open_for_format(dev, force);
246                 if (ret)
247                         die("Error opening %s: %s", dev_opts.path, strerror(-ret));
248         }
249
250         struct bch_sb *sb =
251                 bch2_format(fs_opt_strs,
252                             fs_opts,
253                             opts,
254                             devices.data, devices.nr);
255         bch2_opt_strs_free(&fs_opt_strs);
256
257         if (!quiet) {
258                 struct printbuf buf = PRINTBUF;
259
260                 buf.human_readable_units = true;
261
262                 bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members_v2);
263                 printf("%s", buf.buf);
264
265                 printbuf_exit(&buf);
266         }
267         free(sb);
268
269         if (opts.passphrase) {
270                 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
271                 free(opts.passphrase);
272         }
273
274         darray_exit(&devices);
275
276         if (initialize) {
277                 struct bch_opts mount_opts = bch2_opts_empty();
278
279
280                 opt_set(mount_opts, verbose, verbose);
281
282                 /*
283                  * Start the filesystem once, to allocate the journal and create
284                  * the root directory:
285                  */
286                 struct bch_fs *c = bch2_fs_open(device_paths.data,
287                                                 device_paths.nr,
288                                                 mount_opts);
289                 if (IS_ERR(c))
290                         die("error opening %s: %s", device_paths.data[0],
291                             bch2_err_str(PTR_ERR(c)));
292
293                 bch2_fs_stop(c);
294         }
295
296         darray_exit(&device_paths);
297
298         return 0;
299 }
300
301 static void show_super_usage(void)
302 {
303         puts("bcachefs show-super \n"
304              "Usage: bcachefs show-super [OPTION].. device\n"
305              "\n"
306              "Options:\n"
307              "  -f, --fields=(fields)       list of sections to print\n"
308              "      --field-only=fiel)      print superblock section only, no header\n"
309              "  -l, --layout                print superblock layout\n"
310              "  -h, --help                  display this help and exit\n"
311              "Report bugs to <linux-bcachefs@vger.kernel.org>");
312         exit(EXIT_SUCCESS);
313 }
314
315 int cmd_show_super(int argc, char *argv[])
316 {
317         static const struct option longopts[] = {
318                 { "fields",                     1, NULL, 'f' },
319                 { "field-only",                 1, NULL, 'F' },
320                 { "layout",                     0, NULL, 'l' },
321                 { "help",                       0, NULL, 'h' },
322                 { NULL }
323         };
324         unsigned fields = 0;
325         int field_only = -1;
326         bool print_layout = false;
327         bool print_default_fields = true;
328         int opt;
329
330         while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
331                 switch (opt) {
332                 case 'f':
333                         fields = !strcmp(optarg, "all")
334                                 ? ~0
335                                 : read_flag_list_or_die(optarg,
336                                         bch2_sb_fields, "superblock field");
337                         print_default_fields = false;
338                         break;
339                 case 'F':
340                         field_only = read_string_list_or_die(optarg,
341                                         bch2_sb_fields, "superblock field");
342                         print_default_fields = false;
343                         break;
344                 case 'l':
345                         print_layout = true;
346                         break;
347                 case 'h':
348                         show_super_usage();
349                         break;
350                 }
351         args_shift(optind);
352
353         char *dev = arg_pop();
354         if (!dev)
355                 die("please supply a device");
356         if (argc)
357                 die("too many arguments");
358
359         struct bch_opts opts = bch2_opts_empty();
360
361         opt_set(opts, noexcl,   true);
362         opt_set(opts, nochanges, true);
363
364         struct bch_sb_handle sb;
365         int ret = bch2_read_super(dev, &opts, &sb);
366         if (ret)
367                 die("Error opening %s: %s", dev, bch2_err_str(ret));
368
369         if (print_default_fields) {
370                 fields |= bch2_sb_field_get(sb.sb, members_v2)
371                         ? 1 << BCH_SB_FIELD_members_v2
372                         : 1 << BCH_SB_FIELD_members_v1;
373                 fields |= 1 << BCH_SB_FIELD_errors;
374         }
375
376         struct printbuf buf = PRINTBUF;
377
378         buf.human_readable_units = true;
379
380         if (field_only >= 0) {
381                 struct bch_sb_field *f = bch2_sb_field_get_id(sb.sb, field_only);
382
383                 if (f)
384                         __bch2_sb_field_to_text(&buf, sb.sb, f);
385         } else {
386                 bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
387         }
388         printf("%s", buf.buf);
389
390         bch2_free_super(&sb);
391         printbuf_exit(&buf);
392         return 0;
393 }