]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.c
74a38f83c41c04abc4709cb9c39792577cddee15
[bcachefs-tools-debian] / libbcachefs.c
1 #include <ctype.h>
2 #include <dirent.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <sys/sysmacros.h>
12 #include <sys/types.h>
13 #include <time.h>
14 #include <unistd.h>
15
16 #include <uuid/uuid.h>
17
18 #include "libbcachefs.h"
19 #include "crypto.h"
20 #include "libbcachefs/bcachefs_format.h"
21 #include "libbcachefs/btree_cache.h"
22 #include "libbcachefs/checksum.h"
23 #include "libbcachefs/disk_groups.h"
24 #include "libbcachefs/journal_seq_blacklist.h"
25 #include "libbcachefs/opts.h"
26 #include "libbcachefs/replicas.h"
27 #include "libbcachefs/super-io.h"
28 #include "tools-util.h"
29
30 #define NSEC_PER_SEC    1000000000L
31
32 /* minimum size filesystem we can create, given a bucket size: */
33 static u64 min_size(unsigned bucket_size)
34 {
35         return BCH_MIN_NR_NBUCKETS * bucket_size;
36 }
37
38 static void init_layout(struct bch_sb_layout *l,
39                         unsigned block_size,
40                         unsigned sb_size,
41                         u64 sb_start, u64 sb_end)
42 {
43         u64 sb_pos = sb_start;
44         unsigned i;
45
46         memset(l, 0, sizeof(*l));
47
48         l->magic                = BCACHE_MAGIC;
49         l->layout_type          = 0;
50         l->nr_superblocks       = 2;
51         l->sb_max_size_bits     = ilog2(sb_size);
52
53         /* Create two superblocks in the allowed range: */
54         for (i = 0; i < l->nr_superblocks; i++) {
55                 if (sb_pos != BCH_SB_SECTOR)
56                         sb_pos = round_up(sb_pos, block_size);
57
58                 l->sb_offset[i] = cpu_to_le64(sb_pos);
59                 sb_pos += sb_size;
60         }
61
62         if (sb_pos > sb_end)
63                 die("insufficient space for superblocks: start %llu end %llu > %llu size %u",
64                     sb_start, sb_pos, sb_end, sb_size);
65 }
66
67 void bch2_pick_bucket_size(struct bch_opts opts, struct dev_opts *dev)
68 {
69         if (!dev->size)
70                 dev->size = get_size(dev->path, dev->fd) >> 9;
71
72         if (!dev->bucket_size) {
73                 if (dev->size < min_size(opts.block_size))
74                         die("cannot format %s, too small (%llu sectors, min %llu)",
75                             dev->path, dev->size, min_size(opts.block_size));
76
77                 /* Bucket size must be >= block size: */
78                 dev->bucket_size = opts.block_size;
79
80                 /* Bucket size must be >= btree node size: */
81                 if (opt_defined(opts, btree_node_size))
82                         dev->bucket_size = max_t(unsigned, dev->bucket_size,
83                                                  opts.btree_node_size);
84
85                 /* Want a bucket size of at least 128k, if possible: */
86                 dev->bucket_size = max(dev->bucket_size, 256U);
87
88                 if (dev->size >= min_size(dev->bucket_size)) {
89                         unsigned scale = max(1,
90                                              ilog2(dev->size / min_size(dev->bucket_size)) / 4);
91
92                         scale = rounddown_pow_of_two(scale);
93
94                         /* max bucket size 1 mb */
95                         dev->bucket_size = min(dev->bucket_size * scale, 1U << 11);
96                 } else {
97                         do {
98                                 dev->bucket_size /= 2;
99                         } while (dev->size < min_size(dev->bucket_size));
100                 }
101         }
102
103         dev->nbuckets   = dev->size / dev->bucket_size;
104
105         if (dev->bucket_size < opts.block_size)
106                 die("Bucket size cannot be smaller than block size");
107
108         if (opt_defined(opts, btree_node_size) &&
109             dev->bucket_size < opts.btree_node_size)
110                 die("Bucket size cannot be smaller than btree node size");
111
112         if (dev->nbuckets < BCH_MIN_NR_NBUCKETS)
113                 die("Not enough buckets: %llu, need %u (bucket size %u)",
114                     dev->nbuckets, BCH_MIN_NR_NBUCKETS, dev->bucket_size);
115
116 }
117
118 static unsigned parse_target(struct bch_sb_handle *sb,
119                              struct dev_opts *devs, size_t nr_devs,
120                              const char *s)
121 {
122         struct dev_opts *i;
123         int idx;
124
125         if (!s)
126                 return 0;
127
128         for (i = devs; i < devs + nr_devs; i++)
129                 if (!strcmp(s, i->path))
130                         return dev_to_target(i - devs);
131
132         idx = bch2_disk_path_find(sb, s);
133         if (idx >= 0)
134                 return group_to_target(idx);
135
136         die("Invalid target %s", s);
137         return 0;
138 }
139
140 struct bch_sb *bch2_format(struct bch_opt_strs  fs_opt_strs,
141                            struct bch_opts      fs_opts,
142                            struct format_opts   opts,
143                            struct dev_opts      *devs,
144                            size_t               nr_devs)
145 {
146         struct bch_sb_handle sb = { NULL };
147         struct dev_opts *i;
148         struct bch_sb_field_members *mi;
149         unsigned max_dev_block_size = 0;
150         unsigned opt_id;
151
152         for (i = devs; i < devs + nr_devs; i++)
153                 max_dev_block_size = max(max_dev_block_size,
154                                          get_blocksize(i->path, i->fd));
155
156         /* calculate block size: */
157         if (!opt_defined(fs_opts, block_size)) {
158                 opt_set(fs_opts, block_size, max_dev_block_size);
159         } else if (fs_opts.block_size < max_dev_block_size)
160                 die("blocksize too small: %u, must be greater than device blocksize %u",
161                     fs_opts.block_size, max_dev_block_size);
162
163         /* calculate bucket sizes: */
164         for (i = devs; i < devs + nr_devs; i++)
165                 bch2_pick_bucket_size(fs_opts, i);
166
167         /* calculate btree node size: */
168         if (!opt_defined(fs_opts, btree_node_size)) {
169                 /* 256k default btree node size */
170                 opt_set(fs_opts, btree_node_size, 512);
171
172                 for (i = devs; i < devs + nr_devs; i++)
173                         fs_opts.btree_node_size =
174                                 min_t(unsigned, fs_opts.btree_node_size,
175                                       i->bucket_size);
176         }
177
178         if (!is_power_of_2(fs_opts.block_size))
179                 die("block size must be power of 2");
180
181         if (!is_power_of_2(fs_opts.btree_node_size))
182                 die("btree node size must be power of 2");
183
184         if (uuid_is_null(opts.uuid.b))
185                 uuid_generate(opts.uuid.b);
186
187         if (bch2_sb_realloc(&sb, 0))
188                 die("insufficient memory");
189
190         sb.sb->version          = le16_to_cpu(opts.version);
191         sb.sb->version_min      = le16_to_cpu(opts.version);
192         sb.sb->magic            = BCACHE_MAGIC;
193         sb.sb->block_size       = cpu_to_le16(fs_opts.block_size);
194         sb.sb->user_uuid        = opts.uuid;
195         sb.sb->nr_devices       = nr_devs;
196
197         if (opts.version == bcachefs_metadata_version_current)
198                 sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
199
200         uuid_generate(sb.sb->uuid.b);
201
202         if (opts.label)
203                 memcpy(sb.sb->label,
204                        opts.label,
205                        min(strlen(opts.label), sizeof(sb.sb->label)));
206
207         for (opt_id = 0;
208              opt_id < bch2_opts_nr;
209              opt_id++) {
210                 const struct bch_option *opt = &bch2_opt_table[opt_id];
211                 u64 v;
212
213                 if (opt->set_sb == SET_NO_SB_OPT)
214                         continue;
215
216                 v = bch2_opt_defined_by_id(&fs_opts, opt_id)
217                         ? bch2_opt_get_by_id(&fs_opts, opt_id)
218                         : bch2_opt_get_by_id(&bch2_opts_default, opt_id);
219
220                 opt->set_sb(sb.sb, v);
221         }
222
223         SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb.sb,
224                                 ilog2(opts.encoded_extent_max));
225
226         struct timespec now;
227         if (clock_gettime(CLOCK_REALTIME, &now))
228                 die("error getting current time: %m");
229
230         sb.sb->time_base_lo     = cpu_to_le64(now.tv_sec * NSEC_PER_SEC + now.tv_nsec);
231         sb.sb->time_precision   = cpu_to_le32(1);
232
233         /* Member info: */
234         mi = bch2_sb_resize_members(&sb,
235                         (sizeof(*mi) + sizeof(struct bch_member) *
236                         nr_devs) / sizeof(u64));
237
238         for (i = devs; i < devs + nr_devs; i++) {
239                 struct bch_member *m = mi->members + (i - devs);
240
241                 uuid_generate(m->uuid.b);
242                 m->nbuckets     = cpu_to_le64(i->nbuckets);
243                 m->first_bucket = 0;
244                 m->bucket_size  = cpu_to_le16(i->bucket_size);
245
246                 SET_BCH_MEMBER_REPLACEMENT(m,   BCH_CACHE_REPLACEMENT_lru);
247                 SET_BCH_MEMBER_DISCARD(m,       i->discard);
248                 SET_BCH_MEMBER_DATA_ALLOWED(m,  i->data_allowed);
249                 SET_BCH_MEMBER_DURABILITY(m,    i->durability + 1);
250         }
251
252         /* Disk labels*/
253         for (i = devs; i < devs + nr_devs; i++) {
254                 struct bch_member *m = mi->members + (i - devs);
255                 int idx;
256
257                 if (!i->label)
258                         continue;
259
260                 idx = bch2_disk_path_find_or_create(&sb, i->label);
261                 if (idx < 0)
262                         die("error creating disk path: %s", idx);
263
264                 SET_BCH_MEMBER_GROUP(m, idx + 1);
265         }
266
267         SET_BCH_SB_FOREGROUND_TARGET(sb.sb,
268                 parse_target(&sb, devs, nr_devs, fs_opt_strs.foreground_target));
269         SET_BCH_SB_BACKGROUND_TARGET(sb.sb,
270                 parse_target(&sb, devs, nr_devs, fs_opt_strs.background_target));
271         SET_BCH_SB_PROMOTE_TARGET(sb.sb,
272                 parse_target(&sb, devs, nr_devs, fs_opt_strs.promote_target));
273         SET_BCH_SB_METADATA_TARGET(sb.sb,
274                 parse_target(&sb, devs, nr_devs, fs_opt_strs.metadata_target));
275
276         /* Crypt: */
277         if (opts.encrypted) {
278                 struct bch_sb_field_crypt *crypt =
279                         bch2_sb_resize_crypt(&sb, sizeof(*crypt) / sizeof(u64));
280
281                 bch_sb_crypt_init(sb.sb, crypt, opts.passphrase);
282                 SET_BCH_SB_ENCRYPTION_TYPE(sb.sb, 1);
283         }
284
285         for (i = devs; i < devs + nr_devs; i++) {
286                 sb.sb->dev_idx = i - devs;
287
288                 if (!i->sb_offset) {
289                         i->sb_offset    = BCH_SB_SECTOR;
290                         i->sb_end       = i->size;
291                 }
292
293                 init_layout(&sb.sb->layout, fs_opts.block_size,
294                             opts.superblock_size,
295                             i->sb_offset, i->sb_end);
296
297                 /*
298                  * Also create a backup superblock at the end of the disk:
299                  *
300                  * If we're not creating a superblock at the default offset, it
301                  * means we're being run from the migrate tool and we could be
302                  * overwriting existing data if we write to the end of the disk:
303                  */
304                 if (i->sb_offset == BCH_SB_SECTOR) {
305                         struct bch_sb_layout *l = &sb.sb->layout;
306                         u64 backup_sb = i->size - (1 << l->sb_max_size_bits);
307
308                         backup_sb = rounddown(backup_sb, i->bucket_size);
309                         l->sb_offset[l->nr_superblocks++] = cpu_to_le64(backup_sb);
310                 }
311
312                 if (i->sb_offset == BCH_SB_SECTOR) {
313                         /* Zero start of disk */
314                         static const char zeroes[BCH_SB_SECTOR << 9];
315
316                         xpwrite(i->fd, zeroes, BCH_SB_SECTOR << 9, 0);
317                 }
318
319                 bch2_super_write(i->fd, sb.sb);
320                 close(i->fd);
321         }
322
323         return sb.sb;
324 }
325
326 void bch2_super_write(int fd, struct bch_sb *sb)
327 {
328         struct nonce nonce = { 0 };
329
330         unsigned i;
331         for (i = 0; i < sb->layout.nr_superblocks; i++) {
332                 sb->offset = sb->layout.sb_offset[i];
333
334                 if (sb->offset == BCH_SB_SECTOR) {
335                         /* Write backup layout */
336                         xpwrite(fd, &sb->layout, sizeof(sb->layout),
337                                 BCH_SB_LAYOUT_SECTOR << 9);
338                 }
339
340                 sb->csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb), nonce, sb);
341                 xpwrite(fd, sb, vstruct_bytes(sb),
342                         le64_to_cpu(sb->offset) << 9);
343         }
344
345         fsync(fd);
346 }
347
348 struct bch_sb *__bch2_super_read(int fd, u64 sector)
349 {
350         struct bch_sb sb, *ret;
351
352         xpread(fd, &sb, sizeof(sb), sector << 9);
353
354         if (memcmp(&sb.magic, &BCACHE_MAGIC, sizeof(sb.magic)))
355                 die("not a bcachefs superblock");
356
357         size_t bytes = vstruct_bytes(&sb);
358
359         ret = malloc(bytes);
360
361         xpread(fd, ret, bytes, sector << 9);
362
363         return ret;
364 }
365
366 static unsigned get_dev_has_data(struct bch_sb *sb, unsigned dev)
367 {
368         struct bch_sb_field_replicas *replicas;
369         struct bch_replicas_entry *r;
370         unsigned i, data_has = 0;
371
372         replicas = bch2_sb_get_replicas(sb);
373
374         if (replicas)
375                 for_each_replicas_entry(replicas, r)
376                         for (i = 0; i < r->nr_devs; i++)
377                                 if (r->devs[i] == dev)
378                                         data_has |= 1 << r->data_type;
379
380         return data_has;
381 }
382
383 static int bch2_sb_get_target(struct bch_sb *sb, char *buf, size_t len, u64 v)
384 {
385         struct target t = target_decode(v);
386         int ret;
387
388         switch (t.type) {
389         case TARGET_NULL:
390                 return scnprintf(buf, len, "none");
391         case TARGET_DEV: {
392                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
393                 struct bch_member *m = mi->members + t.dev;
394
395                 if (bch2_dev_exists(sb, mi, t.dev)) {
396                         char uuid_str[40];
397
398                         uuid_unparse(m->uuid.b, uuid_str);
399
400                         ret = scnprintf(buf, len, "Device %u (%s)", t.dev,
401                                 uuid_str);
402                 } else {
403                         ret = scnprintf(buf, len, "Bad device %u", t.dev);
404                 }
405
406                 break;
407         }
408         case TARGET_GROUP: {
409                 struct bch_sb_field_disk_groups *gi;
410                 gi = bch2_sb_get_disk_groups(sb);
411
412                 struct bch_disk_group *g = gi->entries + t.group;
413
414                 if (t.group < disk_groups_nr(gi) && !BCH_GROUP_DELETED(g)) {
415                         ret = scnprintf(buf, len, "Label %u (%.*s)", t.group,
416                                 BCH_SB_LABEL_SIZE, g->label);
417                 } else {
418                         ret = scnprintf(buf, len, "Bad label %u", t.group);
419                 }
420                 break;
421         }
422         default:
423                 BUG();
424         }
425
426         return ret;
427 }
428
429 /* superblock printing: */
430
431 static void bch2_sb_print_layout(struct bch_sb *sb, enum units units)
432 {
433         struct bch_sb_layout *l = &sb->layout;
434         unsigned i;
435
436         printf("  type:                         %u\n"
437                "  superblock max size:          %s\n"
438                "  nr superblocks:               %u\n"
439                "  Offsets:                      ",
440                l->layout_type,
441                pr_units(1 << l->sb_max_size_bits, units),
442                l->nr_superblocks);
443
444         for (i = 0; i < l->nr_superblocks; i++) {
445                 if (i)
446                         printf(", ");
447                 printf("%llu", le64_to_cpu(l->sb_offset[i]));
448         }
449         putchar('\n');
450 }
451
452 static void bch2_sb_print_journal(struct bch_sb *sb, struct bch_sb_field *f,
453                                   enum units units)
454 {
455         struct bch_sb_field_journal *journal = field_to_type(f, journal);
456         unsigned i, nr = bch2_nr_journal_buckets(journal);
457
458         printf("  Buckets:                      ");
459         for (i = 0; i < nr; i++) {
460                 if (i)
461                         putchar(' ');
462                 printf("%llu", le64_to_cpu(journal->buckets[i]));
463         }
464         putchar('\n');
465 }
466
467 static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
468                                   enum units units)
469 {
470         struct bch_sb_field_members *mi = field_to_type(f, members);
471         struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
472         unsigned i;
473
474         for (i = 0; i < sb->nr_devices; i++) {
475                 struct bch_member *m = mi->members + i;
476                 time_t last_mount = le64_to_cpu(m->last_mount);
477                 char member_uuid_str[40];
478                 char data_allowed_str[100];
479                 char data_has_str[100];
480                 char label [BCH_SB_LABEL_SIZE+10];
481                 char time_str[64];
482
483                 if (!bch2_member_exists(m))
484                         continue;
485
486                 uuid_unparse(m->uuid.b, member_uuid_str);
487
488                 if (BCH_MEMBER_GROUP(m)) {
489                         unsigned idx = BCH_MEMBER_GROUP(m) - 1;
490
491                         if (idx < disk_groups_nr(gi)) {
492                                 scnprintf(label, sizeof(label), "%.*s (%u)",
493                                         BCH_SB_LABEL_SIZE,
494                                         gi->entries[idx].label, idx);
495                         } else {
496                                 strcpy(label, "(bad disk labels section)");
497                         }
498                 } else {
499                         strcpy(label, "(none)");
500                 }
501
502                 bch2_flags_to_text(&PBUF(data_allowed_str),
503                                    bch2_data_types,
504                                    BCH_MEMBER_DATA_ALLOWED(m));
505                 if (!data_allowed_str[0])
506                         strcpy(data_allowed_str, "(none)");
507
508                 bch2_flags_to_text(&PBUF(data_has_str),
509                                    bch2_data_types,
510                                    get_dev_has_data(sb, i));
511                 if (!data_has_str[0])
512                         strcpy(data_has_str, "(none)");
513
514                 if (last_mount) {
515                         struct tm *tm = localtime(&last_mount);
516                         size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
517                         if (!err)
518                                 strcpy(time_str, "(formatting error)");
519                 } else {
520                         strcpy(time_str, "(never)");
521                 }
522
523                 printf("  Device %u:\n"
524                        "    UUID:                       %s\n"
525                        "    Size:                       %s\n"
526                        "    Bucket size:                %s\n"
527                        "    First bucket:               %u\n"
528                        "    Buckets:                    %llu\n"
529                        "    Last mount:                 %s\n"
530                        "    State:                      %s\n"
531                        "    Group:                      %s\n"
532                        "    Data allowed:               %s\n"
533
534                        "    Has data:                   %s\n"
535
536                        "    Replacement policy:         %s\n"
537                        "    Discard:                    %llu\n",
538                        i, member_uuid_str,
539                        pr_units(le16_to_cpu(m->bucket_size) *
540                                 le64_to_cpu(m->nbuckets), units),
541                        pr_units(le16_to_cpu(m->bucket_size), units),
542                        le16_to_cpu(m->first_bucket),
543                        le64_to_cpu(m->nbuckets),
544                        time_str,
545
546                        BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
547                        ? bch2_member_states[BCH_MEMBER_STATE(m)]
548                        : "unknown",
549
550                        label,
551                        data_allowed_str,
552                        data_has_str,
553
554                        BCH_MEMBER_REPLACEMENT(m) < BCH_CACHE_REPLACEMENT_NR
555                        ? bch2_cache_replacement_policies[BCH_MEMBER_REPLACEMENT(m)]
556                        : "unknown",
557
558                        BCH_MEMBER_DISCARD(m));
559         }
560 }
561
562 static void bch2_sb_print_crypt(struct bch_sb *sb, struct bch_sb_field *f,
563                                 enum units units)
564 {
565         struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
566
567         printf("  KFD:                  %llu\n"
568                "  scrypt n:             %llu\n"
569                "  scrypt r:             %llu\n"
570                "  scrypt p:             %llu\n",
571                BCH_CRYPT_KDF_TYPE(crypt),
572                BCH_KDF_SCRYPT_N(crypt),
573                BCH_KDF_SCRYPT_R(crypt),
574                BCH_KDF_SCRYPT_P(crypt));
575 }
576
577 static void bch2_sb_print_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f,
578                                       enum units units)
579 {
580         struct bch_sb_field_replicas_v0 *replicas = field_to_type(f, replicas_v0);
581         struct bch_replicas_entry_v0 *e;
582         unsigned i;
583
584         for_each_replicas_entry(replicas, e) {
585                 printf_pad(32, "  %s:", bch2_data_types[e->data_type]);
586
587                 putchar('[');
588                 for (i = 0; i < e->nr_devs; i++) {
589                         if (i)
590                                 putchar(' ');
591                         printf("%u", e->devs[i]);
592                 }
593                 printf("]\n");
594         }
595 }
596
597 static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
598                                    enum units units)
599 {
600         struct bch_sb_field_replicas *replicas = field_to_type(f, replicas);
601         struct bch_replicas_entry *e;
602         unsigned i;
603
604         for_each_replicas_entry(replicas, e) {
605                 printf_pad(32, "  %s: %u/%u",
606                            bch2_data_types[e->data_type],
607                            e->nr_required,
608                            e->nr_devs);
609
610                 putchar('[');
611                 for (i = 0; i < e->nr_devs; i++) {
612                         if (i)
613                                 putchar(' ');
614                         printf("%u", e->devs[i]);
615                 }
616                 printf("]\n");
617         }
618 }
619
620 static void bch2_sb_print_quota(struct bch_sb *sb, struct bch_sb_field *f,
621                                 enum units units)
622 {
623 }
624
625 static void bch2_sb_print_disk_groups(struct bch_sb *sb, struct bch_sb_field *f,
626                                       enum units units)
627 {
628 }
629
630 static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
631                                 enum units units)
632 {
633         struct bch_sb_field_clean *clean = field_to_type(f, clean);
634
635
636         printf("  flags:       %x", le32_to_cpu(clean->flags));
637         printf("  journal seq: %llx", le64_to_cpu(clean->journal_seq));
638 }
639
640 static void bch2_sb_print_journal_seq_blacklist(struct bch_sb *sb, struct bch_sb_field *f,
641                                                 enum units units)
642 {
643         struct bch_sb_field_journal_seq_blacklist *bl = field_to_type(f, journal_seq_blacklist);
644         unsigned i, nr = blacklist_nr_entries(bl);
645
646         for (i = 0; i < nr; i++) {
647                 struct journal_seq_blacklist_entry *e =
648                         bl->start + i;
649
650                 printf("  %llu-%llu\n",
651                        le64_to_cpu(e->start),
652                        le64_to_cpu(e->end));
653         }
654 }
655
656 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
657
658 struct bch_sb_field_toolops {
659         sb_field_print_fn       print;
660 };
661
662 static const struct bch_sb_field_toolops bch2_sb_field_ops[] = {
663 #define x(f, nr)                                        \
664         [BCH_SB_FIELD_##f] = {                          \
665                 .print  = bch2_sb_print_##f,            \
666         },
667         BCH_SB_FIELDS()
668 #undef x
669 };
670
671 static inline void bch2_sb_field_print(struct bch_sb *sb,
672                                        struct bch_sb_field *f,
673                                        enum units units)
674 {
675         unsigned type = le32_to_cpu(f->type);
676
677         if (type < BCH_SB_FIELD_NR)
678                 bch2_sb_field_ops[type].print(sb, f, units);
679         else
680                 printf("(unknown field %u)\n", type);
681 }
682
683 void bch2_sb_print(struct bch_sb *sb, bool print_layout,
684                    unsigned fields, enum units units)
685 {
686         struct bch_sb_field_members *mi;
687         char user_uuid_str[40], internal_uuid_str[40];
688         char features_str[500];
689         char compat_features_str[500];
690         char fields_have_str[200];
691         char label[BCH_SB_LABEL_SIZE + 1];
692         char time_str[64];
693         char foreground_str[64];
694         char background_str[64];
695         char promote_str[64];
696         char metadata_str[64];
697         struct bch_sb_field *f;
698         u64 fields_have = 0;
699         unsigned nr_devices = 0;
700         time_t time_base = le64_to_cpu(sb->time_base_lo) / NSEC_PER_SEC;
701
702         memcpy(label, sb->label, BCH_SB_LABEL_SIZE);
703         label[BCH_SB_LABEL_SIZE] = '\0';
704
705         uuid_unparse(sb->user_uuid.b, user_uuid_str);
706         uuid_unparse(sb->uuid.b, internal_uuid_str);
707
708         if (time_base) {
709                 struct tm *tm = localtime(&time_base);
710                 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
711                 if (!err)
712                         strcpy(time_str, "(formatting error)");
713         } else {
714                 strcpy(time_str, "(not set)");
715         }
716
717         mi = bch2_sb_get_members(sb);
718         if (mi) {
719                 struct bch_member *m;
720
721                 for (m = mi->members;
722                      m < mi->members + sb->nr_devices;
723                      m++)
724                         nr_devices += bch2_member_exists(m);
725         }
726
727         bch2_sb_get_target(sb, foreground_str, sizeof(foreground_str),
728                 BCH_SB_FOREGROUND_TARGET(sb));
729
730         bch2_sb_get_target(sb, background_str, sizeof(background_str),
731                 BCH_SB_BACKGROUND_TARGET(sb));
732
733         bch2_sb_get_target(sb, promote_str, sizeof(promote_str),
734                 BCH_SB_PROMOTE_TARGET(sb));
735
736         bch2_sb_get_target(sb, metadata_str, sizeof(metadata_str),
737                 BCH_SB_METADATA_TARGET(sb));
738
739         bch2_flags_to_text(&PBUF(features_str),
740                            bch2_sb_features,
741                            le64_to_cpu(sb->features[0]));
742
743         bch2_flags_to_text(&PBUF(compat_features_str),
744                            bch2_sb_compat,
745                            le64_to_cpu(sb->compat[0]));
746
747         vstruct_for_each(sb, f)
748                 fields_have |= 1 << le32_to_cpu(f->type);
749         bch2_flags_to_text(&PBUF(fields_have_str),
750                            bch2_sb_fields, fields_have);
751
752         printf("External UUID:                  %s\n"
753                "Internal UUID:                  %s\n"
754                "Device index:                   %u\n"
755                "Label:                          %s\n"
756                "Version:                        %u\n"
757                "Oldest version on disk:         %u\n"
758                "Created:                        %s\n"
759                "Squence number:                 %llu\n"
760                "Block_size:                     %s\n"
761                "Btree node size:                %s\n"
762                "Error action:                   %s\n"
763                "Clean:                          %llu\n"
764                "Features:                       %s\n"
765                "Compat features:                %s\n"
766
767                "Metadata replicas:              %llu\n"
768                "Data replicas:                  %llu\n"
769
770                "Metadata checksum type:         %s (%llu)\n"
771                "Data checksum type:             %s (%llu)\n"
772                "Compression type:               %s (%llu)\n"
773
774                "Foreground write target:        %s\n"
775                "Background write target:        %s\n"
776                "Promote target:                 %s\n"
777                "Metadata target:                %s\n"
778
779                "String hash type:               %s (%llu)\n"
780                "32 bit inodes:                  %llu\n"
781                "GC reserve percentage:          %llu%%\n"
782                "Root reserve percentage:        %llu%%\n"
783
784                "Devices:                        %u live, %u total\n"
785                "Sections:                       %s\n"
786                "Superblock size:                %llu\n",
787                user_uuid_str,
788                internal_uuid_str,
789                sb->dev_idx,
790                label,
791                le16_to_cpu(sb->version),
792                le16_to_cpu(sb->version_min),
793                time_str,
794                le64_to_cpu(sb->seq),
795                pr_units(le16_to_cpu(sb->block_size), units),
796                pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
797
798                BCH_SB_ERROR_ACTION(sb) < BCH_ON_ERROR_NR
799                ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
800                : "unknown",
801
802                BCH_SB_CLEAN(sb),
803                features_str,
804                compat_features_str,
805
806                BCH_SB_META_REPLICAS_WANT(sb),
807                BCH_SB_DATA_REPLICAS_WANT(sb),
808
809                BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
810                ? bch2_csum_opts[BCH_SB_META_CSUM_TYPE(sb)]
811                : "unknown",
812                BCH_SB_META_CSUM_TYPE(sb),
813
814                BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
815                ? bch2_csum_opts[BCH_SB_DATA_CSUM_TYPE(sb)]
816                : "unknown",
817                BCH_SB_DATA_CSUM_TYPE(sb),
818
819                BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
820                ? bch2_compression_opts[BCH_SB_COMPRESSION_TYPE(sb)]
821                : "unknown",
822                BCH_SB_COMPRESSION_TYPE(sb),
823
824                foreground_str,
825                background_str,
826                promote_str,
827                metadata_str,
828
829                BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
830                ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
831                : "unknown",
832                BCH_SB_STR_HASH_TYPE(sb),
833
834                BCH_SB_INODE_32BIT(sb),
835                BCH_SB_GC_RESERVE(sb),
836                BCH_SB_ROOT_RESERVE(sb),
837
838                nr_devices, sb->nr_devices,
839                fields_have_str,
840                vstruct_bytes(sb));
841
842         if (print_layout) {
843                 printf("\n"
844                        "Layout:\n");
845                 bch2_sb_print_layout(sb, units);
846         }
847
848         vstruct_for_each(sb, f) {
849                 unsigned type = le32_to_cpu(f->type);
850                 char name[60];
851
852                 if (!(fields & (1 << type)))
853                         continue;
854
855                 if (type < BCH_SB_FIELD_NR) {
856                         scnprintf(name, sizeof(name), "%s", bch2_sb_fields[type]);
857                         name[0] = toupper(name[0]);
858                 } else {
859                         scnprintf(name, sizeof(name), "(unknown field %u)", type);
860                 }
861
862                 printf("\n%s (size %llu):\n", name, vstruct_bytes(f));
863                 if (type < BCH_SB_FIELD_NR)
864                         bch2_sb_field_print(sb, f, units);
865         }
866 }
867
868 /* ioctl interface: */
869
870 /* Global control device: */
871 int bcachectl_open(void)
872 {
873         return xopen("/dev/bcachefs-ctl", O_RDWR);
874 }
875
876 /* Filesystem handles (ioctl, sysfs dir): */
877
878 #define SYSFS_BASE "/sys/fs/bcachefs/"
879
880 void bcache_fs_close(struct bchfs_handle fs)
881 {
882         close(fs.ioctl_fd);
883         close(fs.sysfs_fd);
884 }
885
886 struct bchfs_handle bcache_fs_open(const char *path)
887 {
888         struct bchfs_handle ret;
889
890         if (!uuid_parse(path, ret.uuid.b)) {
891                 /* It's a UUID, look it up in sysfs: */
892                 char *sysfs = mprintf(SYSFS_BASE "%s", path);
893                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
894
895                 char *minor = read_file_str(ret.sysfs_fd, "minor");
896                 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
897                 ret.ioctl_fd = xopen(ctl, O_RDWR);
898
899                 free(sysfs);
900                 free(minor);
901                 free(ctl);
902         } else {
903                 /* It's a path: */
904                 ret.ioctl_fd = open(path, O_RDONLY);
905                 if (ret.ioctl_fd < 0)
906                         die("Error opening filesystem at %s: %m", path);
907
908                 struct bch_ioctl_query_uuid uuid;
909                 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
910                         die("error opening %s: not a bcachefs filesystem", path);
911
912                 ret.uuid = uuid.uuid;
913
914                 char uuid_str[40];
915                 uuid_unparse(uuid.uuid.b, uuid_str);
916
917                 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
918                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
919                 free(sysfs);
920         }
921
922         return ret;
923 }
924
925 /*
926  * Given a path to a block device, open the filesystem it belongs to; also
927  * return the device's idx:
928  */
929 struct bchfs_handle bchu_fs_open_by_dev(const char *path, int *idx)
930 {
931         char buf[1024], *uuid_str;
932
933         struct stat stat = xstat(path);
934
935         if (!S_ISBLK(stat.st_mode))
936                 die("%s is not a block device", path);
937
938         char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
939                               major(stat.st_dev),
940                               minor(stat.st_dev));
941         ssize_t len = readlink(sysfs, buf, sizeof(buf));
942         free(sysfs);
943
944         if (len > 0) {
945                 char *p = strrchr(buf, '/');
946                 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
947                         die("error parsing sysfs");
948
949                 *p = '\0';
950                 p = strrchr(buf, '/');
951                 uuid_str = p + 1;
952         } else {
953                 struct bch_opts opts = bch2_opts_empty();
954
955                 opt_set(opts, noexcl,   true);
956                 opt_set(opts, nochanges, true);
957
958                 struct bch_sb_handle sb;
959                 int ret = bch2_read_super(path, &opts, &sb);
960                 if (ret)
961                         die("Error opening %s: %s", path, strerror(-ret));
962
963                 *idx = sb.sb->dev_idx;
964                 uuid_str = buf;
965                 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
966
967                 bch2_free_super(&sb);
968         }
969
970         return bcache_fs_open(uuid_str);
971 }
972
973 int bchu_dev_path_to_idx(struct bchfs_handle fs, const char *dev_path)
974 {
975         int idx;
976         struct bchfs_handle fs2 = bchu_fs_open_by_dev(dev_path, &idx);
977
978         if (memcmp(&fs.uuid, &fs2.uuid, sizeof(fs.uuid)))
979                 idx = -1;
980         bcache_fs_close(fs2);
981         return idx;
982 }
983
984 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
985 {
986         int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
987
988         while (1) {
989                 struct bch_ioctl_data_event e;
990
991                 if (read(progress_fd, &e, sizeof(e)) != sizeof(e))
992                         die("error reading from progress fd %m");
993
994                 if (e.type)
995                         continue;
996
997                 if (e.p.data_type == U8_MAX)
998                         break;
999
1000                 printf("\33[2K\r");
1001
1002                 printf("%llu%% complete: current position %s",
1003                        e.p.sectors_total
1004                        ? e.p.sectors_done * 100 / e.p.sectors_total
1005                        : 0,
1006                        bch2_data_types[e.p.data_type]);
1007
1008                 switch (e.p.data_type) {
1009                 case BCH_DATA_btree:
1010                 case BCH_DATA_user:
1011                         printf(" %s:%llu:%llu",
1012                                bch2_btree_ids[e.p.btree_id],
1013                                e.p.pos.inode,
1014                                e.p.pos.offset);
1015                 }
1016
1017                 fflush(stdout);
1018                 sleep(1);
1019         }
1020         printf("\nDone\n");
1021
1022         close(progress_fd);
1023         return 0;
1024 }
1025
1026 /* option parsing */
1027
1028 void bch2_opt_strs_free(struct bch_opt_strs *opts)
1029 {
1030         unsigned i;
1031
1032         for (i = 0; i < bch2_opts_nr; i++) {
1033                 free(opts->by_id[i]);
1034                 opts->by_id[i] = NULL;
1035         }
1036 }
1037
1038 struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
1039                                           unsigned opt_types)
1040 {
1041         struct bch_opt_strs opts;
1042         unsigned i = 1;
1043
1044         memset(&opts, 0, sizeof(opts));
1045
1046         while (i < *argc) {
1047                 char *optstr = strcmp_prefix(argv[i], "--");
1048                 char *valstr = NULL, *p;
1049                 int optid, nr_args = 1;
1050
1051                 if (!optstr) {
1052                         i++;
1053                         continue;
1054                 }
1055
1056                 optstr = strdup(optstr);
1057
1058                 p = optstr;
1059                 while (isalpha(*p) || *p == '_')
1060                         p++;
1061
1062                 if (*p == '=') {
1063                         *p = '\0';
1064                         valstr = p + 1;
1065                 }
1066
1067                 optid = bch2_opt_lookup(optstr);
1068                 if (optid < 0 ||
1069                     !(bch2_opt_table[optid].mode & opt_types)) {
1070                         i++;
1071                         goto next;
1072                 }
1073
1074                 if (!valstr &&
1075                     bch2_opt_table[optid].type != BCH_OPT_BOOL) {
1076                         nr_args = 2;
1077                         valstr = argv[i + 1];
1078                 }
1079
1080                 if (!valstr)
1081                         valstr = "1";
1082
1083                 opts.by_id[optid] = strdup(valstr);
1084
1085                 *argc -= nr_args;
1086                 memmove(&argv[i],
1087                         &argv[i + nr_args],
1088                         sizeof(char *) * (*argc - i));
1089                 argv[*argc] = NULL;
1090 next:
1091                 free(optstr);
1092         }
1093
1094         return opts;
1095 }
1096
1097 struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
1098 {
1099         struct bch_opts opts = bch2_opts_empty();
1100         unsigned i;
1101         int ret;
1102         u64 v;
1103
1104         for (i = 0; i < bch2_opts_nr; i++) {
1105                 if (!strs.by_id[i] ||
1106                     bch2_opt_table[i].type == BCH_OPT_FN)
1107                         continue;
1108
1109                 ret = bch2_opt_parse(NULL, &bch2_opt_table[i],
1110                                      strs.by_id[i], &v);
1111                 if (ret < 0)
1112                         die("Invalid %s: %s",
1113                             bch2_opt_table[i].attr.name,
1114                             strerror(-ret));
1115
1116                 bch2_opt_set_by_id(&opts, i, v);
1117         }
1118
1119         return opts;
1120 }
1121
1122 #define newline(c)              \
1123         do {                    \
1124                 printf("\n");   \
1125                 c = 0;          \
1126         } while(0)
1127 void bch2_opts_usage(unsigned opt_types)
1128 {
1129         const struct bch_option *opt;
1130         unsigned i, c = 0, helpcol = 30;
1131
1132
1133
1134         for (opt = bch2_opt_table;
1135              opt < bch2_opt_table + bch2_opts_nr;
1136              opt++) {
1137                 if (!(opt->mode & opt_types))
1138                         continue;
1139
1140                 c += printf("      --%s", opt->attr.name);
1141
1142                 switch (opt->type) {
1143                 case BCH_OPT_BOOL:
1144                         break;
1145                 case BCH_OPT_STR:
1146                         c += printf("=(");
1147                         for (i = 0; opt->choices[i]; i++) {
1148                                 if (i)
1149                                         c += printf("|");
1150                                 c += printf("%s", opt->choices[i]);
1151                         }
1152                         c += printf(")");
1153                         break;
1154                 default:
1155                         c += printf("=%s", opt->hint);
1156                         break;
1157                 }
1158
1159                 if (opt->help) {
1160                         const char *l = opt->help;
1161
1162                         if (c >= helpcol)
1163                                 newline(c);
1164
1165                         while (1) {
1166                                 const char *n = strchrnul(l, '\n');
1167
1168                                 while (c < helpcol) {
1169                                         putchar(' ');
1170                                         c++;
1171                                 }
1172                                 printf("%.*s", (int) (n - l), l);
1173                                 newline(c);
1174
1175                                 if (!*n)
1176                                         break;
1177                                 l = n + 1;
1178                         }
1179                 } else {
1180                         newline(c);
1181                 }
1182         }
1183 }
1184
1185 dev_names bchu_fs_get_devices(struct bchfs_handle fs)
1186 {
1187         DIR *dir = fdopendir(fs.sysfs_fd);
1188         struct dirent *d;
1189         dev_names devs;
1190
1191         darray_init(devs);
1192
1193         while ((errno = 0), (d = readdir(dir))) {
1194                 struct dev_name n = { 0, NULL, NULL };
1195
1196                 if (sscanf(d->d_name, "dev-%u", &n.idx) != 1)
1197                         continue;
1198
1199                 char *block_attr = mprintf("dev-%u/block", n.idx);
1200
1201                 char sysfs_block_buf[4096];
1202                 ssize_t r = readlinkat(fs.sysfs_fd, block_attr,
1203                                        sysfs_block_buf, sizeof(sysfs_block_buf));
1204                 if (r > 0) {
1205                         sysfs_block_buf[r] = '\0';
1206                         n.dev = strdup(basename(sysfs_block_buf));
1207                 }
1208
1209                 free(block_attr);
1210
1211                 char *label_attr = mprintf("dev-%u/label", n.idx);
1212                 n.label = read_file_str(fs.sysfs_fd, label_attr);
1213                 free(label_attr);
1214
1215                 darray_append(devs, n);
1216         }
1217
1218         closedir(dir);
1219
1220         return devs;
1221 }