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