]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.c
Increase default superblock size to 1MB
[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 start, u64 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 (start != BCH_SB_SECTOR)
55                         start = round_up(start, block_size);
56
57                 l->sb_offset[i] = cpu_to_le64(start);
58                 start += sb_size;
59         }
60
61         if (start >= 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", idx);
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                 if (i->sb_offset == BCH_SB_SECTOR) {
296                         /* Zero start of disk */
297                         static const char zeroes[BCH_SB_SECTOR << 9];
298
299                         xpwrite(i->fd, zeroes, BCH_SB_SECTOR << 9, 0);
300                 }
301
302                 bch2_super_write(i->fd, sb.sb);
303                 close(i->fd);
304         }
305
306         return sb.sb;
307 }
308
309 void bch2_super_write(int fd, struct bch_sb *sb)
310 {
311         struct nonce nonce = { 0 };
312
313         unsigned i;
314         for (i = 0; i < sb->layout.nr_superblocks; i++) {
315                 sb->offset = sb->layout.sb_offset[i];
316
317                 if (sb->offset == BCH_SB_SECTOR) {
318                         /* Write backup layout */
319                         xpwrite(fd, &sb->layout, sizeof(sb->layout),
320                                 BCH_SB_LAYOUT_SECTOR << 9);
321                 }
322
323                 sb->csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb), nonce, sb);
324                 xpwrite(fd, sb, vstruct_bytes(sb),
325                         le64_to_cpu(sb->offset) << 9);
326         }
327
328         fsync(fd);
329 }
330
331 struct bch_sb *__bch2_super_read(int fd, u64 sector)
332 {
333         struct bch_sb sb, *ret;
334
335         xpread(fd, &sb, sizeof(sb), sector << 9);
336
337         if (memcmp(&sb.magic, &BCACHE_MAGIC, sizeof(sb.magic)))
338                 die("not a bcachefs superblock");
339
340         size_t bytes = vstruct_bytes(&sb);
341
342         ret = malloc(bytes);
343
344         xpread(fd, ret, bytes, sector << 9);
345
346         return ret;
347 }
348
349 static unsigned get_dev_has_data(struct bch_sb *sb, unsigned dev)
350 {
351         struct bch_sb_field_replicas *replicas;
352         struct bch_replicas_entry *r;
353         unsigned i, data_has = 0;
354
355         replicas = bch2_sb_get_replicas(sb);
356
357         if (replicas)
358                 for_each_replicas_entry(replicas, r)
359                         for (i = 0; i < r->nr_devs; i++)
360                                 if (r->devs[i] == dev)
361                                         data_has |= 1 << r->data_type;
362
363         return data_has;
364 }
365
366 static int bch2_sb_get_target(struct bch_sb *sb, char *buf, size_t len, u64 v)
367 {
368         struct target t = target_decode(v);
369         int ret;
370
371         switch (t.type) {
372         case TARGET_NULL:
373                 return scnprintf(buf, len, "none");
374         case TARGET_DEV: {
375                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
376                 struct bch_member *m = mi->members + t.dev;
377
378                 if (bch2_dev_exists(sb, mi, t.dev)) {
379                         char uuid_str[40];
380
381                         uuid_unparse(m->uuid.b, uuid_str);
382
383                         ret = scnprintf(buf, len, "Device %u (%s)", t.dev,
384                                 uuid_str);
385                 } else {
386                         ret = scnprintf(buf, len, "Bad device %u", t.dev);
387                 }
388
389                 break;
390         }
391         case TARGET_GROUP: {
392                 struct bch_sb_field_disk_groups *gi;
393                 gi = bch2_sb_get_disk_groups(sb);
394
395                 struct bch_disk_group *g = gi->entries + t.group;
396
397                 if (t.group < disk_groups_nr(gi) && !BCH_GROUP_DELETED(g)) {
398                         ret = scnprintf(buf, len, "Group %u (%.*s)", t.group,
399                                 BCH_SB_LABEL_SIZE, g->label);
400                 } else {
401                         ret = scnprintf(buf, len, "Bad group %u", t.group);
402                 }
403                 break;
404         }
405         default:
406                 BUG();
407         }
408
409         return ret;
410 }
411
412 /* superblock printing: */
413
414 static void bch2_sb_print_layout(struct bch_sb *sb, enum units units)
415 {
416         struct bch_sb_layout *l = &sb->layout;
417         unsigned i;
418
419         printf("  type:                         %u\n"
420                "  superblock max size:          %s\n"
421                "  nr superblocks:               %u\n"
422                "  Offsets:                      ",
423                l->layout_type,
424                pr_units(1 << l->sb_max_size_bits, units),
425                l->nr_superblocks);
426
427         for (i = 0; i < l->nr_superblocks; i++) {
428                 if (i)
429                         printf(", ");
430                 printf("%llu", le64_to_cpu(l->sb_offset[i]));
431         }
432         putchar('\n');
433 }
434
435 static void bch2_sb_print_journal(struct bch_sb *sb, struct bch_sb_field *f,
436                                   enum units units)
437 {
438         struct bch_sb_field_journal *journal = field_to_type(f, journal);
439         unsigned i, nr = bch2_nr_journal_buckets(journal);
440
441         printf("  Buckets:                      ");
442         for (i = 0; i < nr; i++) {
443                 if (i)
444                         putchar(' ');
445                 printf("%llu", le64_to_cpu(journal->buckets[i]));
446         }
447         putchar('\n');
448 }
449
450 static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
451                                   enum units units)
452 {
453         struct bch_sb_field_members *mi = field_to_type(f, members);
454         struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
455         unsigned i;
456
457         for (i = 0; i < sb->nr_devices; i++) {
458                 struct bch_member *m = mi->members + i;
459                 time_t last_mount = le64_to_cpu(m->last_mount);
460                 char member_uuid_str[40];
461                 char data_allowed_str[100];
462                 char data_has_str[100];
463                 char group[BCH_SB_LABEL_SIZE+10];
464                 char time_str[64];
465
466                 if (!bch2_member_exists(m))
467                         continue;
468
469                 uuid_unparse(m->uuid.b, member_uuid_str);
470
471                 if (BCH_MEMBER_GROUP(m)) {
472                         unsigned idx = BCH_MEMBER_GROUP(m) - 1;
473
474                         if (idx < disk_groups_nr(gi)) {
475                                 snprintf(group, sizeof(group), "%.*s (%u)",
476                                         BCH_SB_LABEL_SIZE,
477                                         gi->entries[idx].label, idx);
478                         } else {
479                                 strcpy(group, "(bad disk groups section)");
480                         }
481                 } else {
482                         strcpy(group, "(none)");
483                 }
484
485                 bch2_flags_to_text(&PBUF(data_allowed_str),
486                                    bch2_data_types,
487                                    BCH_MEMBER_DATA_ALLOWED(m));
488                 if (!data_allowed_str[0])
489                         strcpy(data_allowed_str, "(none)");
490
491                 bch2_flags_to_text(&PBUF(data_has_str),
492                                    bch2_data_types,
493                                    get_dev_has_data(sb, i));
494                 if (!data_has_str[0])
495                         strcpy(data_has_str, "(none)");
496
497                 if (last_mount) {
498                         struct tm *tm = localtime(&last_mount);
499                         size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
500                         if (!err)
501                                 strcpy(time_str, "(formatting error)");
502                 } else {
503                         strcpy(time_str, "(never)");
504                 }
505
506                 printf("  Device %u:\n"
507                        "    UUID:                       %s\n"
508                        "    Size:                       %s\n"
509                        "    Bucket size:                %s\n"
510                        "    First bucket:               %u\n"
511                        "    Buckets:                    %llu\n"
512                        "    Last mount:                 %s\n"
513                        "    State:                      %s\n"
514                        "    Group:                      %s\n"
515                        "    Data allowed:               %s\n"
516
517                        "    Has data:                   %s\n"
518
519                        "    Replacement policy:         %s\n"
520                        "    Discard:                    %llu\n",
521                        i, member_uuid_str,
522                        pr_units(le16_to_cpu(m->bucket_size) *
523                                 le64_to_cpu(m->nbuckets), units),
524                        pr_units(le16_to_cpu(m->bucket_size), units),
525                        le16_to_cpu(m->first_bucket),
526                        le64_to_cpu(m->nbuckets),
527                        time_str,
528
529                        BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
530                        ? bch2_member_states[BCH_MEMBER_STATE(m)]
531                        : "unknown",
532
533                        group,
534                        data_allowed_str,
535                        data_has_str,
536
537                        BCH_MEMBER_REPLACEMENT(m) < BCH_CACHE_REPLACEMENT_NR
538                        ? bch2_cache_replacement_policies[BCH_MEMBER_REPLACEMENT(m)]
539                        : "unknown",
540
541                        BCH_MEMBER_DISCARD(m));
542         }
543 }
544
545 static void bch2_sb_print_crypt(struct bch_sb *sb, struct bch_sb_field *f,
546                                 enum units units)
547 {
548         struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
549
550         printf("  KFD:                  %llu\n"
551                "  scrypt n:             %llu\n"
552                "  scrypt r:             %llu\n"
553                "  scrypt p:             %llu\n",
554                BCH_CRYPT_KDF_TYPE(crypt),
555                BCH_KDF_SCRYPT_N(crypt),
556                BCH_KDF_SCRYPT_R(crypt),
557                BCH_KDF_SCRYPT_P(crypt));
558 }
559
560 static void bch2_sb_print_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f,
561                                    enum units units)
562 {
563         struct bch_sb_field_replicas_v0 *replicas = field_to_type(f, replicas_v0);
564         struct bch_replicas_entry_v0 *e;
565         unsigned i;
566
567         for_each_replicas_entry(replicas, e) {
568                 printf_pad(32, "  %s:", bch2_data_types[e->data_type]);
569
570                 putchar('[');
571                 for (i = 0; i < e->nr_devs; i++) {
572                         if (i)
573                                 putchar(' ');
574                         printf("%u", e->devs[i]);
575                 }
576                 printf("]\n");
577         }
578 }
579
580 static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
581                                    enum units units)
582 {
583         struct bch_sb_field_replicas *replicas = field_to_type(f, replicas);
584         struct bch_replicas_entry *e;
585         unsigned i;
586
587         for_each_replicas_entry(replicas, e) {
588                 printf_pad(32, "  %s: %u/%u",
589                            bch2_data_types[e->data_type],
590                            e->nr_required,
591                            e->nr_devs);
592
593                 putchar('[');
594                 for (i = 0; i < e->nr_devs; i++) {
595                         if (i)
596                                 putchar(' ');
597                         printf("%u", e->devs[i]);
598                 }
599                 printf("]\n");
600         }
601 }
602
603 static void bch2_sb_print_quota(struct bch_sb *sb, struct bch_sb_field *f,
604                                 enum units units)
605 {
606 }
607
608 static void bch2_sb_print_disk_groups(struct bch_sb *sb, struct bch_sb_field *f,
609                                       enum units units)
610 {
611 }
612
613 static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
614                                 enum units units)
615 {
616         struct bch_sb_field_clean *clean = field_to_type(f, clean);
617
618
619         printf("  flags:       %x", le32_to_cpu(clean->flags));
620         printf("  journal seq: %llx", le64_to_cpu(clean->journal_seq));
621 }
622
623 static void bch2_sb_print_journal_seq_blacklist(struct bch_sb *sb, struct bch_sb_field *f,
624                                 enum units units)
625 {
626         struct bch_sb_field_journal_seq_blacklist *bl = field_to_type(f, journal_seq_blacklist);
627         unsigned i, nr = blacklist_nr_entries(bl);
628
629         for (i = 0; i < nr; i++) {
630                 struct journal_seq_blacklist_entry *e =
631                         bl->start + i;
632
633                 printf("  %llu-%llu\n",
634                        le64_to_cpu(e->start),
635                        le64_to_cpu(e->end));
636         }
637 }
638
639 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
640
641 struct bch_sb_field_toolops {
642         sb_field_print_fn       print;
643 };
644
645 static const struct bch_sb_field_toolops bch2_sb_field_ops[] = {
646 #define x(f, nr)                                        \
647         [BCH_SB_FIELD_##f] = {                          \
648                 .print  = bch2_sb_print_##f,            \
649         },
650         BCH_SB_FIELDS()
651 #undef x
652 };
653
654 static inline void bch2_sb_field_print(struct bch_sb *sb,
655                                        struct bch_sb_field *f,
656                                        enum units units)
657 {
658         unsigned type = le32_to_cpu(f->type);
659
660         if (type < BCH_SB_FIELD_NR)
661                 bch2_sb_field_ops[type].print(sb, f, units);
662         else
663                 printf("(unknown field %u)\n", type);
664 }
665
666 void bch2_sb_print(struct bch_sb *sb, bool print_layout,
667                    unsigned fields, enum units units)
668 {
669         struct bch_sb_field_members *mi;
670         char user_uuid_str[40], internal_uuid_str[40];
671         char features_str[500];
672         char fields_have_str[200];
673         char label[BCH_SB_LABEL_SIZE + 1];
674         char time_str[64];
675         char foreground_str[64];
676         char background_str[64];
677         char promote_str[64];
678         char metadata_str[64];
679         struct bch_sb_field *f;
680         u64 fields_have = 0;
681         unsigned nr_devices = 0;
682         time_t time_base = le64_to_cpu(sb->time_base_lo) / NSEC_PER_SEC;
683
684         memcpy(label, sb->label, BCH_SB_LABEL_SIZE);
685         label[BCH_SB_LABEL_SIZE] = '\0';
686
687         uuid_unparse(sb->user_uuid.b, user_uuid_str);
688         uuid_unparse(sb->uuid.b, internal_uuid_str);
689
690         if (time_base) {
691                 struct tm *tm = localtime(&time_base);
692                 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
693                 if (!err)
694                         strcpy(time_str, "(formatting error)");
695         } else {
696                 strcpy(time_str, "(not set)");
697         }
698
699         mi = bch2_sb_get_members(sb);
700         if (mi) {
701                 struct bch_member *m;
702
703                 for (m = mi->members;
704                      m < mi->members + sb->nr_devices;
705                      m++)
706                         nr_devices += bch2_member_exists(m);
707         }
708
709         bch2_sb_get_target(sb, foreground_str, sizeof(foreground_str),
710                 BCH_SB_FOREGROUND_TARGET(sb));
711
712         bch2_sb_get_target(sb, background_str, sizeof(background_str),
713                 BCH_SB_BACKGROUND_TARGET(sb));
714
715         bch2_sb_get_target(sb, promote_str, sizeof(promote_str),
716                 BCH_SB_PROMOTE_TARGET(sb));
717
718         bch2_sb_get_target(sb, metadata_str, sizeof(metadata_str),
719                 BCH_SB_METADATA_TARGET(sb));
720
721         bch2_flags_to_text(&PBUF(features_str),
722                            bch2_sb_features,
723                            le64_to_cpu(sb->features[0]));
724
725         vstruct_for_each(sb, f)
726                 fields_have |= 1 << le32_to_cpu(f->type);
727         bch2_flags_to_text(&PBUF(fields_have_str),
728                            bch2_sb_fields, fields_have);
729
730         printf("External UUID:                  %s\n"
731                "Internal UUID:                  %s\n"
732                "Device index:                   %u\n"
733                "Label:                          %s\n"
734                "Version:                        %u\n"
735                "Oldest version on disk:         %u\n"
736                "Created:                        %s\n"
737                "Squence number:                 %llu\n"
738                "Block_size:                     %s\n"
739                "Btree node size:                %s\n"
740                "Error action:                   %s\n"
741                "Clean:                          %llu\n"
742                "Features:                       %s\n"
743
744                "Metadata replicas:              %llu\n"
745                "Data replicas:                  %llu\n"
746
747                "Metadata checksum type:         %s (%llu)\n"
748                "Data checksum type:             %s (%llu)\n"
749                "Compression type:               %s (%llu)\n"
750
751                "Foreground write target:        %s\n"
752                "Background write target:        %s\n"
753                "Promote target:                 %s\n"
754                "Metadata target:                %s\n"
755
756                "String hash type:               %s (%llu)\n"
757                "32 bit inodes:                  %llu\n"
758                "GC reserve percentage:          %llu%%\n"
759                "Root reserve percentage:        %llu%%\n"
760
761                "Devices:                        %u live, %u total\n"
762                "Sections:                       %s\n"
763                "Superblock size:                %llu\n",
764                user_uuid_str,
765                internal_uuid_str,
766                sb->dev_idx,
767                label,
768                le16_to_cpu(sb->version),
769                le16_to_cpu(sb->version_min),
770                time_str,
771                le64_to_cpu(sb->seq),
772                pr_units(le16_to_cpu(sb->block_size), units),
773                pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
774
775                BCH_SB_ERROR_ACTION(sb) < BCH_ON_ERROR_NR
776                ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
777                : "unknown",
778
779                BCH_SB_CLEAN(sb),
780                features_str,
781
782                BCH_SB_META_REPLICAS_WANT(sb),
783                BCH_SB_DATA_REPLICAS_WANT(sb),
784
785                BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
786                ? bch2_csum_opts[BCH_SB_META_CSUM_TYPE(sb)]
787                : "unknown",
788                BCH_SB_META_CSUM_TYPE(sb),
789
790                BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
791                ? bch2_csum_opts[BCH_SB_DATA_CSUM_TYPE(sb)]
792                : "unknown",
793                BCH_SB_DATA_CSUM_TYPE(sb),
794
795                BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
796                ? bch2_compression_opts[BCH_SB_COMPRESSION_TYPE(sb)]
797                : "unknown",
798                BCH_SB_COMPRESSION_TYPE(sb),
799
800                foreground_str,
801                background_str,
802                promote_str,
803                metadata_str,
804
805                BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
806                ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
807                : "unknown",
808                BCH_SB_STR_HASH_TYPE(sb),
809
810                BCH_SB_INODE_32BIT(sb),
811                BCH_SB_GC_RESERVE(sb),
812                BCH_SB_ROOT_RESERVE(sb),
813
814                nr_devices, sb->nr_devices,
815                fields_have_str,
816                vstruct_bytes(sb));
817
818         if (print_layout) {
819                 printf("\n"
820                        "Layout:\n");
821                 bch2_sb_print_layout(sb, units);
822         }
823
824         vstruct_for_each(sb, f) {
825                 unsigned type = le32_to_cpu(f->type);
826                 char name[60];
827
828                 if (!(fields & (1 << type)))
829                         continue;
830
831                 if (type < BCH_SB_FIELD_NR) {
832                         scnprintf(name, sizeof(name), "%s", bch2_sb_fields[type]);
833                         name[0] = toupper(name[0]);
834                 } else {
835                         scnprintf(name, sizeof(name), "(unknown field %u)", type);
836                 }
837
838                 printf("\n%s (size %llu):\n", name, vstruct_bytes(f));
839                 if (type < BCH_SB_FIELD_NR)
840                         bch2_sb_field_print(sb, f, units);
841         }
842 }
843
844 /* ioctl interface: */
845
846 /* Global control device: */
847 int bcachectl_open(void)
848 {
849         return xopen("/dev/bcachefs-ctl", O_RDWR);
850 }
851
852 /* Filesystem handles (ioctl, sysfs dir): */
853
854 #define SYSFS_BASE "/sys/fs/bcachefs/"
855
856 void bcache_fs_close(struct bchfs_handle fs)
857 {
858         close(fs.ioctl_fd);
859         close(fs.sysfs_fd);
860 }
861
862 struct bchfs_handle bcache_fs_open(const char *path)
863 {
864         struct bchfs_handle ret;
865
866         if (!uuid_parse(path, ret.uuid.b)) {
867                 /* It's a UUID, look it up in sysfs: */
868                 char *sysfs = mprintf(SYSFS_BASE "%s", path);
869                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
870
871                 char *minor = read_file_str(ret.sysfs_fd, "minor");
872                 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
873                 ret.ioctl_fd = xopen(ctl, O_RDWR);
874
875                 free(sysfs);
876                 free(minor);
877                 free(ctl);
878         } else {
879                 /* It's a path: */
880                 ret.ioctl_fd = xopen(path, O_RDONLY);
881
882                 struct bch_ioctl_query_uuid uuid;
883                 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
884                         die("error opening %s: not a bcachefs filesystem", path);
885
886                 ret.uuid = uuid.uuid;
887
888                 char uuid_str[40];
889                 uuid_unparse(uuid.uuid.b, uuid_str);
890
891                 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
892                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
893                 free(sysfs);
894         }
895
896         return ret;
897 }
898
899 /*
900  * Given a path to a block device, open the filesystem it belongs to; also
901  * return the device's idx:
902  */
903 struct bchfs_handle bchu_fs_open_by_dev(const char *path, unsigned *idx)
904 {
905         char buf[1024], *uuid_str;
906
907         struct stat stat = xstat(path);
908
909         if (!S_ISBLK(stat.st_mode))
910                 die("%s is not a block device", path);
911
912         char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
913                               major(stat.st_dev),
914                               minor(stat.st_dev));
915         ssize_t len = readlink(sysfs, buf, sizeof(buf));
916         free(sysfs);
917
918         if (len > 0) {
919                 char *p = strrchr(buf, '/');
920                 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
921                         die("error parsing sysfs");
922
923                 *p = '\0';
924                 p = strrchr(buf, '/');
925                 uuid_str = p + 1;
926         } else {
927                 struct bch_opts opts = bch2_opts_empty();
928
929                 opt_set(opts, noexcl,   true);
930                 opt_set(opts, nochanges, true);
931
932                 struct bch_sb_handle sb;
933                 int ret = bch2_read_super(path, &opts, &sb);
934                 if (ret)
935                         die("Error opening %s: %s", path, strerror(-ret));
936
937                 *idx = sb.sb->dev_idx;
938                 uuid_str = buf;
939                 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
940
941                 bch2_free_super(&sb);
942         }
943
944         return bcache_fs_open(uuid_str);
945 }
946
947 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
948 {
949         int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
950
951         while (1) {
952                 struct bch_ioctl_data_event e;
953
954                 if (read(progress_fd, &e, sizeof(e)) != sizeof(e))
955                         die("error reading from progress fd %m");
956
957                 if (e.type)
958                         continue;
959
960                 if (e.p.data_type == U8_MAX)
961                         break;
962
963                 printf("\33[2K\r");
964
965                 printf("%llu%% complete: current position %s",
966                        e.p.sectors_total
967                        ? e.p.sectors_done * 100 / e.p.sectors_total
968                        : 0,
969                        bch2_data_types[e.p.data_type]);
970
971                 switch (e.p.data_type) {
972                 case BCH_DATA_btree:
973                 case BCH_DATA_user:
974                         printf(" %s:%llu:%llu",
975                                bch2_btree_ids[e.p.btree_id],
976                                e.p.pos.inode,
977                                e.p.pos.offset);
978                 }
979
980                 fflush(stdout);
981                 sleep(1);
982         }
983         printf("\nDone\n");
984
985         close(progress_fd);
986         return 0;
987 }
988
989 /* option parsing */
990
991 struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
992                                           unsigned opt_types)
993 {
994         struct bch_opt_strs opts;
995         unsigned i = 1;
996
997         memset(&opts, 0, sizeof(opts));
998
999         while (i < *argc) {
1000                 char *optstr = strcmp_prefix(argv[i], "--");
1001                 char *valstr = NULL, *p;
1002                 int optid, nr_args = 1;
1003
1004                 if (!optstr) {
1005                         i++;
1006                         continue;
1007                 }
1008
1009                 optstr = strdup(optstr);
1010
1011                 p = optstr;
1012                 while (isalpha(*p) || *p == '_')
1013                         p++;
1014
1015                 if (*p == '=') {
1016                         *p = '\0';
1017                         valstr = p + 1;
1018                 }
1019
1020                 optid = bch2_opt_lookup(optstr);
1021                 if (optid < 0 ||
1022                     !(bch2_opt_table[optid].mode & opt_types)) {
1023                         free(optstr);
1024                         i++;
1025                         continue;
1026                 }
1027
1028                 if (!valstr &&
1029                     bch2_opt_table[optid].type != BCH_OPT_BOOL) {
1030                         nr_args = 2;
1031                         valstr = argv[i + 1];
1032                 }
1033
1034                 if (!valstr)
1035                         valstr = "1";
1036
1037                 opts.by_id[optid] = valstr;
1038
1039                 *argc -= nr_args;
1040                 memmove(&argv[i],
1041                         &argv[i + nr_args],
1042                         sizeof(char *) * (*argc - i));
1043                 argv[*argc] = NULL;
1044         }
1045
1046         return opts;
1047 }
1048
1049 struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
1050 {
1051         struct bch_opts opts = bch2_opts_empty();
1052         unsigned i;
1053         int ret;
1054         u64 v;
1055
1056         for (i = 0; i < bch2_opts_nr; i++) {
1057                 if (!strs.by_id[i] ||
1058                     bch2_opt_table[i].type == BCH_OPT_FN)
1059                         continue;
1060
1061                 ret = bch2_opt_parse(NULL, &bch2_opt_table[i],
1062                                      strs.by_id[i], &v);
1063                 if (ret < 0)
1064                         die("Invalid %s: %s",
1065                             bch2_opt_table[i].attr.name,
1066                             strerror(-ret));
1067
1068                 bch2_opt_set_by_id(&opts, i, v);
1069         }
1070
1071         return opts;
1072 }
1073
1074 void bch2_opts_usage(unsigned opt_types)
1075 {
1076         const struct bch_option *opt;
1077         unsigned i, c = 0, helpcol = 30;
1078
1079         void tabalign() {
1080                 while (c < helpcol) {
1081                         putchar(' ');
1082                         c++;
1083                 }
1084         }
1085
1086         void newline() {
1087                 printf("\n");
1088                 c = 0;
1089         }
1090
1091         for (opt = bch2_opt_table;
1092              opt < bch2_opt_table + bch2_opts_nr;
1093              opt++) {
1094                 if (!(opt->mode & opt_types))
1095                         continue;
1096
1097                 c += printf("      --%s", opt->attr.name);
1098
1099                 switch (opt->type) {
1100                 case BCH_OPT_BOOL:
1101                         break;
1102                 case BCH_OPT_STR:
1103                         c += printf("=(");
1104                         for (i = 0; opt->choices[i]; i++) {
1105                                 if (i)
1106                                         c += printf("|");
1107                                 c += printf("%s", opt->choices[i]);
1108                         }
1109                         c += printf(")");
1110                         break;
1111                 default:
1112                         c += printf("=%s", opt->hint);
1113                         break;
1114                 }
1115
1116                 if (opt->help) {
1117                         const char *l = opt->help;
1118
1119                         if (c >= helpcol)
1120                                 newline();
1121
1122                         while (1) {
1123                                 const char *n = strchrnul(l, '\n');
1124
1125                                 tabalign();
1126                                 printf("%.*s", (int) (n - l), l);
1127                                 newline();
1128
1129                                 if (!*n)
1130                                         break;
1131                                 l = n + 1;
1132                         }
1133                 } else {
1134                         newline();
1135                 }
1136         }
1137 }
1138
1139 dev_names bchu_fs_get_devices(struct bchfs_handle fs)
1140 {
1141         DIR *dir = fdopendir(fs.sysfs_fd);
1142         struct dirent *d;
1143         dev_names devs;
1144
1145         darray_init(devs);
1146
1147         while ((errno = 0), (d = readdir(dir))) {
1148                 struct dev_name n = { 0, NULL, NULL };
1149
1150                 if (sscanf(d->d_name, "dev-%u", &n.idx) != 1)
1151                         continue;
1152
1153                 char *block_attr = mprintf("dev-%u/block", n.idx);
1154
1155                 char sysfs_block_buf[4096];
1156                 ssize_t r = readlinkat(fs.sysfs_fd, block_attr,
1157                                        sysfs_block_buf, sizeof(sysfs_block_buf));
1158                 if (r > 0) {
1159                         sysfs_block_buf[r] = '\0';
1160                         n.dev = strdup(basename(sysfs_block_buf));
1161                 }
1162
1163                 free(block_attr);
1164
1165                 char *label_attr = mprintf("dev-%u/label", n.idx);
1166                 n.label = read_file_str(fs.sysfs_fd, label_attr);
1167                 free(label_attr);
1168
1169                 darray_append(devs, n);
1170         }
1171
1172         closedir(dir);
1173
1174         return devs;
1175 }