]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_format.c
Update bcachefs sources to c801fa69f0 bcachefs: Fix bch_alloc_to_text()
[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(), *dev;
121         bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false;
122         unsigned v;
123         int opt;
124
125         struct bch_opt_strs fs_opt_strs =
126                 bch2_cmdline_opts_get(&argc, argv, OPT_FORMAT);
127         struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
128
129         while ((opt = getopt_long(argc, argv,
130                                   "-L:U:g:fqhv",
131                                   format_opts,
132                                   NULL)) != -1)
133                 switch (opt) {
134                 case O_replicas:
135                         if (kstrtouint(optarg, 10, &v) ||
136                             !v ||
137                             v > BCH_REPLICAS_MAX)
138                                 die("invalid replicas");
139
140                         opt_set(fs_opts, metadata_replicas, v);
141                         opt_set(fs_opts, data_replicas, v);
142                         break;
143                 case O_encrypted:
144                         opts.encrypted = true;
145                         break;
146                 case O_no_passphrase:
147                         no_passphrase = true;
148                         break;
149                 case O_fs_label:
150                 case 'L':
151                         opts.label = optarg;
152                         break;
153                 case O_uuid:
154                 case 'U':
155                         if (uuid_parse(optarg, opts.uuid.b))
156                                 die("Bad uuid");
157                         break;
158                 case O_force:
159                 case 'f':
160                         force = true;
161                         break;
162                 case O_fs_size:
163                         if (bch2_strtoull_h(optarg, &dev_opts.size))
164                                 die("invalid filesystem size");
165                         break;
166                 case O_superblock_size:
167                         if (bch2_strtouint_h(optarg, &opts.superblock_size))
168                                 die("invalid filesystem size");
169
170                         opts.superblock_size >>= 9;
171                         break;
172                 case O_bucket_size:
173                         if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
174                                 die("bad bucket_size %s", optarg);
175                         break;
176                 case O_label:
177                 case 'l':
178                         dev_opts.label = optarg;
179                         break;
180                 case O_discard:
181                         dev_opts.discard = true;
182                         break;
183                 case O_data_allowed:
184                         dev_opts.data_allowed =
185                                 read_flag_list_or_die(optarg,
186                                         bch2_data_types, "data type");
187                         break;
188                 case O_durability:
189                         if (kstrtouint(optarg, 10, &dev_opts.durability) ||
190                             dev_opts.durability > BCH_REPLICAS_MAX)
191                                 die("invalid durability");
192                         break;
193                 case O_version:
194                         if (kstrtouint(optarg, 10, &opts.version))
195                                 die("invalid version");
196                         break;
197                 case O_no_initialize:
198                         initialize = false;
199                         break;
200                 case O_no_opt:
201                         darray_push(&device_paths, optarg);
202                         dev_opts.path = optarg;
203                         darray_push(&devices, dev_opts);
204                         dev_opts.size = 0;
205                         break;
206                 case O_quiet:
207                 case 'q':
208                         quiet = true;
209                         break;
210                 case 'v':
211                         verbose = true;
212                 case O_help:
213                 case 'h':
214                         usage();
215                         exit(EXIT_SUCCESS);
216                         break;
217                 case '?':
218                         exit(EXIT_FAILURE);
219                         break;
220                 }
221
222         if (!devices.nr)
223                 die("Please supply a device");
224
225         if (opts.encrypted && !no_passphrase) {
226                 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
227                 initialize = false;
228         }
229
230         darray_for_each(devices, dev)
231                 dev->fd = open_for_format(dev->path, force);
232
233         struct bch_sb *sb =
234                 bch2_format(fs_opt_strs,
235                             fs_opts,
236                             opts,
237                             devices.data, devices.nr);
238         bch2_opt_strs_free(&fs_opt_strs);
239
240         if (!quiet) {
241                 struct printbuf buf = PRINTBUF;
242
243                 buf.human_readable_units = true;
244
245                 bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members);
246                 printf("%s", buf.buf);
247
248                 printbuf_exit(&buf);
249         }
250         free(sb);
251
252         if (opts.passphrase) {
253                 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
254                 free(opts.passphrase);
255         }
256
257         darray_exit(&devices);
258
259         if (initialize) {
260                 struct bch_opts mount_opts = bch2_opts_empty();
261
262
263                 opt_set(mount_opts, verbose, verbose);
264
265                 /*
266                  * Start the filesystem once, to allocate the journal and create
267                  * the root directory:
268                  */
269                 struct bch_fs *c = bch2_fs_open(device_paths.data,
270                                                 device_paths.nr,
271                                                 mount_opts);
272                 if (IS_ERR(c))
273                         die("error opening %s: %s", device_paths.data[0],
274                             bch2_err_str(PTR_ERR(c)));
275
276                 bch2_fs_stop(c);
277         }
278
279         darray_exit(&device_paths);
280
281         return 0;
282 }
283
284 static void show_super_usage(void)
285 {
286         puts("bcachefs show-super \n"
287              "Usage: bcachefs show-super [OPTION].. device\n"
288              "\n"
289              "Options:\n"
290              "  -f, --fields=(fields)       list of sections to print\n"
291              "  -l, --layout                print superblock layout\n"
292              "  -h, --help                  display this help and exit\n"
293              "Report bugs to <linux-bcachefs@vger.kernel.org>");
294         exit(EXIT_SUCCESS);
295 }
296
297 int cmd_show_super(int argc, char *argv[])
298 {
299         static const struct option longopts[] = {
300                 { "fields",                     1, NULL, 'f' },
301                 { "layout",                     0, NULL, 'l' },
302                 { "help",                       0, NULL, 'h' },
303                 { NULL }
304         };
305         unsigned fields = 1 << BCH_SB_FIELD_members;
306         bool print_layout = false;
307         int opt;
308
309         while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
310                 switch (opt) {
311                 case 'f':
312                         fields = !strcmp(optarg, "all")
313                                 ? ~0
314                                 : read_flag_list_or_die(optarg,
315                                         bch2_sb_fields, "superblock field");
316                         break;
317                 case 'l':
318                         print_layout = true;
319                         break;
320                 case 'h':
321                         show_super_usage();
322                         break;
323                 }
324         args_shift(optind);
325
326         char *dev = arg_pop();
327         if (!dev)
328                 die("please supply a device");
329         if (argc)
330                 die("too many arguments");
331
332         struct bch_opts opts = bch2_opts_empty();
333
334         opt_set(opts, noexcl,   true);
335         opt_set(opts, nochanges, true);
336
337         struct bch_sb_handle sb;
338         int ret = bch2_read_super(dev, &opts, &sb);
339         if (ret)
340                 die("Error opening %s: %s", dev, bch2_err_str(ret));
341
342         struct printbuf buf = PRINTBUF;
343
344         buf.human_readable_units = true;
345
346         bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
347         printf("%s", buf.buf);
348
349         bch2_free_super(&sb);
350         printbuf_exit(&buf);
351         return 0;
352 }