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