]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_format.c
Increase default superblock size to 1MB
[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 "ccan/darray/darray.h"
24
25 #include "cmds.h"
26 #include "libbcachefs.h"
27 #include "crypto.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',  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('g',  group,                  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('h',  help,                   no_argument)
50
51 static void usage(void)
52 {
53         puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
54              "Usage: bcachefs format [OPTION]... <devices>\n"
55              "\n"
56              "Options:");
57
58         bch2_opts_usage(OPT_FORMAT);
59
60         puts(
61              "      --replicas=#            Sets both data and metadata replicas\n"
62              "      --encrypted             Enable whole filesystem encryption (chacha20/poly1305)\n"
63              "      --no_passphrase         Don't encrypt master encryption key\n"
64              "  -L, --label=label\n"
65              "  -U, --uuid=uuid\n"
66              "      --superblock_size=size\n"
67              "\n"
68              "Device specific options:");
69
70         bch2_opts_usage(OPT_DEVICE);
71
72         puts("  -g, --group=label           Disk group\n"
73              "\n"
74              "  -f, --force\n"
75              "  -q, --quiet                 Only print errors\n"
76              "  -h, --help                  Display this help and exit\n"
77              "\n"
78              "Device specific options must come before corresponding devices, e.g.\n"
79              "  bcachefs format --group cache /dev/sdb /dev/sdc\n"
80              "\n"
81              "Report bugs to <linux-bcache@vger.kernel.org>");
82 }
83
84 enum {
85         O_no_opt = 1,
86 #define x(shortopt, longopt, arg)       O_##longopt,
87         OPTS
88 #undef x
89 };
90
91 #define x(shortopt, longopt, arg) {                     \
92         .name           = #longopt,                     \
93         .has_arg        = arg,                          \
94         .flag           = NULL,                         \
95         .val            = O_##longopt,                  \
96 },
97 static const struct option format_opts[] = {
98         OPTS
99         { NULL }
100 };
101 #undef x
102
103 u64 read_flag_list_or_die(char *opt, const char * const list[],
104                           const char *msg)
105 {
106         u64 v = bch2_read_flag_list(opt, list);
107         if (v == (u64) -1)
108                 die("Bad %s %s", msg, opt);
109
110         return v;
111 }
112
113 int cmd_format(int argc, char *argv[])
114 {
115         darray(struct dev_opts) devices;
116         darray(char *) device_paths;
117         struct format_opts opts = format_opts_default();
118         struct dev_opts dev_opts = dev_opts_default(), *dev;
119         bool force = false, no_passphrase = false, quiet = false, initialize = true;
120         unsigned v;
121         int opt;
122
123         darray_init(devices);
124         darray_init(device_paths);
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:fqh",
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_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
167                         dev_opts.size >>= 9;
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                         dev_opts.bucket_size =
177                                 hatoi_validate(optarg, "bucket size");
178                         break;
179                 case O_group:
180                 case 'g':
181                         dev_opts.group = optarg;
182                         break;
183                 case O_discard:
184                         dev_opts.discard = true;
185                         break;
186                 case O_data_allowed:
187                         dev_opts.data_allowed =
188                                 read_flag_list_or_die(optarg,
189                                         bch2_data_types, "data type");
190                         break;
191                 case O_durability:
192                         if (kstrtouint(optarg, 10, &dev_opts.durability) ||
193                             dev_opts.durability > BCH_REPLICAS_MAX)
194                                 die("invalid durability");
195                         break;
196                 case O_version:
197                         if (kstrtouint(optarg, 10, &opts.version))
198                                 die("invalid version");
199                         break;
200                 case O_no_initialize:
201                         initialize = false;
202                         break;
203                 case O_no_opt:
204                         darray_append(device_paths, optarg);
205                         dev_opts.path = optarg;
206                         darray_append(devices, dev_opts);
207                         dev_opts.size = 0;
208                         break;
209                 case O_quiet:
210                 case 'q':
211                         quiet = true;
212                         break;
213                 case O_help:
214                 case 'h':
215                         usage();
216                         exit(EXIT_SUCCESS);
217                         break;
218                 case '?':
219                         exit(EXIT_FAILURE);
220                         break;
221                 }
222
223         if (darray_empty(devices))
224                 die("Please supply a device");
225
226         if (opts.encrypted && !no_passphrase) {
227                 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
228                 initialize = false;
229         }
230
231         darray_foreach(dev, devices)
232                 dev->fd = open_for_format(dev->path, force);
233
234         struct bch_sb *sb =
235                 bch2_format(fs_opt_strs,
236                             fs_opts,
237                             opts,
238                             devices.item, darray_size(devices));
239
240         if (!quiet)
241                 bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
242         free(sb);
243
244         if (opts.passphrase) {
245                 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
246                 free(opts.passphrase);
247         }
248
249         darray_free(devices);
250
251         if (initialize) {
252                 /*
253                  * Start the filesystem once, to allocate the journal and create
254                  * the root directory:
255                  */
256                 struct bch_fs *c = bch2_fs_open(device_paths.item,
257                                                 darray_size(device_paths),
258                                                 bch2_opts_empty());
259                 if (IS_ERR(c))
260                         die("error opening %s: %s", device_paths.item,
261                             strerror(-PTR_ERR(c)));
262
263                 bch2_fs_stop(c);
264         }
265
266         darray_free(device_paths);
267
268         return 0;
269 }
270
271 static void show_super_usage(void)
272 {
273         puts("bcachefs show-super \n"
274              "Usage: bcachefs show-super [OPTION].. device\n"
275              "\n"
276              "Options:\n"
277              "  -f, --fields=(fields)       list of sections to print\n"
278              "  -l, --layout                print superblock layout\n"
279              "  -h, --help                  display this help and exit\n"
280              "Report bugs to <linux-bcache@vger.kernel.org>");
281         exit(EXIT_SUCCESS);
282 }
283
284 int cmd_show_super(int argc, char *argv[])
285 {
286         static const struct option longopts[] = {
287                 { "fields",                     1, NULL, 'f' },
288                 { "layout",                     0, NULL, 'l' },
289                 { "help",                       0, NULL, 'h' },
290                 { NULL }
291         };
292         unsigned fields = 1 << BCH_SB_FIELD_members;
293         bool print_layout = false;
294         int opt;
295
296         while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
297                 switch (opt) {
298                 case 'f':
299                         fields = !strcmp(optarg, "all")
300                                 ? ~0
301                                 : read_flag_list_or_die(optarg,
302                                         bch2_sb_fields, "superblock field");
303                         break;
304                 case 'l':
305                         print_layout = true;
306                         break;
307                 case 'h':
308                         show_super_usage();
309                         break;
310                 }
311         args_shift(optind);
312
313         char *dev = arg_pop();
314         if (!dev)
315                 die("please supply a device");
316         if (argc)
317                 die("too many arguments");
318
319         struct bch_opts opts = bch2_opts_empty();
320
321         opt_set(opts, noexcl,   true);
322         opt_set(opts, nochanges, true);
323
324         struct bch_sb_handle sb;
325         int ret = bch2_read_super(dev, &opts, &sb);
326         if (ret)
327                 die("Error opening %s: %s", dev, strerror(-ret));
328
329         bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
330         bch2_free_super(&sb);
331         return 0;
332 }