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