]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.c
minor fixes for clang support
[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 compat_features_str[500];
673         char fields_have_str[200];
674         char label[BCH_SB_LABEL_SIZE + 1];
675         char time_str[64];
676         char foreground_str[64];
677         char background_str[64];
678         char promote_str[64];
679         char metadata_str[64];
680         struct bch_sb_field *f;
681         u64 fields_have = 0;
682         unsigned nr_devices = 0;
683         time_t time_base = le64_to_cpu(sb->time_base_lo) / NSEC_PER_SEC;
684
685         memcpy(label, sb->label, BCH_SB_LABEL_SIZE);
686         label[BCH_SB_LABEL_SIZE] = '\0';
687
688         uuid_unparse(sb->user_uuid.b, user_uuid_str);
689         uuid_unparse(sb->uuid.b, internal_uuid_str);
690
691         if (time_base) {
692                 struct tm *tm = localtime(&time_base);
693                 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
694                 if (!err)
695                         strcpy(time_str, "(formatting error)");
696         } else {
697                 strcpy(time_str, "(not set)");
698         }
699
700         mi = bch2_sb_get_members(sb);
701         if (mi) {
702                 struct bch_member *m;
703
704                 for (m = mi->members;
705                      m < mi->members + sb->nr_devices;
706                      m++)
707                         nr_devices += bch2_member_exists(m);
708         }
709
710         bch2_sb_get_target(sb, foreground_str, sizeof(foreground_str),
711                 BCH_SB_FOREGROUND_TARGET(sb));
712
713         bch2_sb_get_target(sb, background_str, sizeof(background_str),
714                 BCH_SB_BACKGROUND_TARGET(sb));
715
716         bch2_sb_get_target(sb, promote_str, sizeof(promote_str),
717                 BCH_SB_PROMOTE_TARGET(sb));
718
719         bch2_sb_get_target(sb, metadata_str, sizeof(metadata_str),
720                 BCH_SB_METADATA_TARGET(sb));
721
722         bch2_flags_to_text(&PBUF(features_str),
723                            bch2_sb_features,
724                            le64_to_cpu(sb->features[0]));
725
726         bch2_flags_to_text(&PBUF(compat_features_str),
727                            bch2_sb_compat,
728                            le64_to_cpu(sb->compat[0]));
729
730         vstruct_for_each(sb, f)
731                 fields_have |= 1 << le32_to_cpu(f->type);
732         bch2_flags_to_text(&PBUF(fields_have_str),
733                            bch2_sb_fields, fields_have);
734
735         printf("External UUID:                  %s\n"
736                "Internal UUID:                  %s\n"
737                "Device index:                   %u\n"
738                "Label:                          %s\n"
739                "Version:                        %u\n"
740                "Oldest version on disk:         %u\n"
741                "Created:                        %s\n"
742                "Squence number:                 %llu\n"
743                "Block_size:                     %s\n"
744                "Btree node size:                %s\n"
745                "Error action:                   %s\n"
746                "Clean:                          %llu\n"
747                "Features:                       %s\n"
748                "Compat features:                %s\n"
749
750                "Metadata replicas:              %llu\n"
751                "Data replicas:                  %llu\n"
752
753                "Metadata checksum type:         %s (%llu)\n"
754                "Data checksum type:             %s (%llu)\n"
755                "Compression type:               %s (%llu)\n"
756
757                "Foreground write target:        %s\n"
758                "Background write target:        %s\n"
759                "Promote target:                 %s\n"
760                "Metadata target:                %s\n"
761
762                "String hash type:               %s (%llu)\n"
763                "32 bit inodes:                  %llu\n"
764                "GC reserve percentage:          %llu%%\n"
765                "Root reserve percentage:        %llu%%\n"
766
767                "Devices:                        %u live, %u total\n"
768                "Sections:                       %s\n"
769                "Superblock size:                %llu\n",
770                user_uuid_str,
771                internal_uuid_str,
772                sb->dev_idx,
773                label,
774                le16_to_cpu(sb->version),
775                le16_to_cpu(sb->version_min),
776                time_str,
777                le64_to_cpu(sb->seq),
778                pr_units(le16_to_cpu(sb->block_size), units),
779                pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
780
781                BCH_SB_ERROR_ACTION(sb) < BCH_ON_ERROR_NR
782                ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
783                : "unknown",
784
785                BCH_SB_CLEAN(sb),
786                features_str,
787                compat_features_str,
788
789                BCH_SB_META_REPLICAS_WANT(sb),
790                BCH_SB_DATA_REPLICAS_WANT(sb),
791
792                BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
793                ? bch2_csum_opts[BCH_SB_META_CSUM_TYPE(sb)]
794                : "unknown",
795                BCH_SB_META_CSUM_TYPE(sb),
796
797                BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
798                ? bch2_csum_opts[BCH_SB_DATA_CSUM_TYPE(sb)]
799                : "unknown",
800                BCH_SB_DATA_CSUM_TYPE(sb),
801
802                BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
803                ? bch2_compression_opts[BCH_SB_COMPRESSION_TYPE(sb)]
804                : "unknown",
805                BCH_SB_COMPRESSION_TYPE(sb),
806
807                foreground_str,
808                background_str,
809                promote_str,
810                metadata_str,
811
812                BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
813                ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
814                : "unknown",
815                BCH_SB_STR_HASH_TYPE(sb),
816
817                BCH_SB_INODE_32BIT(sb),
818                BCH_SB_GC_RESERVE(sb),
819                BCH_SB_ROOT_RESERVE(sb),
820
821                nr_devices, sb->nr_devices,
822                fields_have_str,
823                vstruct_bytes(sb));
824
825         if (print_layout) {
826                 printf("\n"
827                        "Layout:\n");
828                 bch2_sb_print_layout(sb, units);
829         }
830
831         vstruct_for_each(sb, f) {
832                 unsigned type = le32_to_cpu(f->type);
833                 char name[60];
834
835                 if (!(fields & (1 << type)))
836                         continue;
837
838                 if (type < BCH_SB_FIELD_NR) {
839                         scnprintf(name, sizeof(name), "%s", bch2_sb_fields[type]);
840                         name[0] = toupper(name[0]);
841                 } else {
842                         scnprintf(name, sizeof(name), "(unknown field %u)", type);
843                 }
844
845                 printf("\n%s (size %llu):\n", name, vstruct_bytes(f));
846                 if (type < BCH_SB_FIELD_NR)
847                         bch2_sb_field_print(sb, f, units);
848         }
849 }
850
851 /* ioctl interface: */
852
853 /* Global control device: */
854 int bcachectl_open(void)
855 {
856         return xopen("/dev/bcachefs-ctl", O_RDWR);
857 }
858
859 /* Filesystem handles (ioctl, sysfs dir): */
860
861 #define SYSFS_BASE "/sys/fs/bcachefs/"
862
863 void bcache_fs_close(struct bchfs_handle fs)
864 {
865         close(fs.ioctl_fd);
866         close(fs.sysfs_fd);
867 }
868
869 struct bchfs_handle bcache_fs_open(const char *path)
870 {
871         struct bchfs_handle ret;
872
873         if (!uuid_parse(path, ret.uuid.b)) {
874                 /* It's a UUID, look it up in sysfs: */
875                 char *sysfs = mprintf(SYSFS_BASE "%s", path);
876                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
877
878                 char *minor = read_file_str(ret.sysfs_fd, "minor");
879                 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
880                 ret.ioctl_fd = xopen(ctl, O_RDWR);
881
882                 free(sysfs);
883                 free(minor);
884                 free(ctl);
885         } else {
886                 /* It's a path: */
887                 ret.ioctl_fd = xopen(path, O_RDONLY);
888
889                 struct bch_ioctl_query_uuid uuid;
890                 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
891                         die("error opening %s: not a bcachefs filesystem", path);
892
893                 ret.uuid = uuid.uuid;
894
895                 char uuid_str[40];
896                 uuid_unparse(uuid.uuid.b, uuid_str);
897
898                 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
899                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
900                 free(sysfs);
901         }
902
903         return ret;
904 }
905
906 /*
907  * Given a path to a block device, open the filesystem it belongs to; also
908  * return the device's idx:
909  */
910 struct bchfs_handle bchu_fs_open_by_dev(const char *path, int *idx)
911 {
912         char buf[1024], *uuid_str;
913
914         struct stat stat = xstat(path);
915
916         if (!S_ISBLK(stat.st_mode))
917                 die("%s is not a block device", path);
918
919         char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
920                               major(stat.st_dev),
921                               minor(stat.st_dev));
922         ssize_t len = readlink(sysfs, buf, sizeof(buf));
923         free(sysfs);
924
925         if (len > 0) {
926                 char *p = strrchr(buf, '/');
927                 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
928                         die("error parsing sysfs");
929
930                 *p = '\0';
931                 p = strrchr(buf, '/');
932                 uuid_str = p + 1;
933         } else {
934                 struct bch_opts opts = bch2_opts_empty();
935
936                 opt_set(opts, noexcl,   true);
937                 opt_set(opts, nochanges, true);
938
939                 struct bch_sb_handle sb;
940                 int ret = bch2_read_super(path, &opts, &sb);
941                 if (ret)
942                         die("Error opening %s: %s", path, strerror(-ret));
943
944                 *idx = sb.sb->dev_idx;
945                 uuid_str = buf;
946                 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
947
948                 bch2_free_super(&sb);
949         }
950
951         return bcache_fs_open(uuid_str);
952 }
953
954 int bchu_dev_path_to_idx(struct bchfs_handle fs, const char *dev_path)
955 {
956         int idx;
957         struct bchfs_handle fs2 = bchu_fs_open_by_dev(dev_path, &idx);
958
959         if (memcmp(&fs.uuid, &fs2.uuid, sizeof(fs.uuid)))
960                 idx = -1;
961         bcache_fs_close(fs2);
962         return idx;
963 }
964
965 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
966 {
967         int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
968
969         while (1) {
970                 struct bch_ioctl_data_event e;
971
972                 if (read(progress_fd, &e, sizeof(e)) != sizeof(e))
973                         die("error reading from progress fd %m");
974
975                 if (e.type)
976                         continue;
977
978                 if (e.p.data_type == U8_MAX)
979                         break;
980
981                 printf("\33[2K\r");
982
983                 printf("%llu%% complete: current position %s",
984                        e.p.sectors_total
985                        ? e.p.sectors_done * 100 / e.p.sectors_total
986                        : 0,
987                        bch2_data_types[e.p.data_type]);
988
989                 switch (e.p.data_type) {
990                 case BCH_DATA_btree:
991                 case BCH_DATA_user:
992                         printf(" %s:%llu:%llu",
993                                bch2_btree_ids[e.p.btree_id],
994                                e.p.pos.inode,
995                                e.p.pos.offset);
996                 }
997
998                 fflush(stdout);
999                 sleep(1);
1000         }
1001         printf("\nDone\n");
1002
1003         close(progress_fd);
1004         return 0;
1005 }
1006
1007 /* option parsing */
1008
1009 void bch2_opt_strs_free(struct bch_opt_strs *opts)
1010 {
1011         unsigned i;
1012
1013         for (i = 0; i < bch2_opts_nr; i++) {
1014                 free(opts->by_id[i]);
1015                 opts->by_id[i] = NULL;
1016         }
1017 }
1018
1019 struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
1020                                           unsigned opt_types)
1021 {
1022         struct bch_opt_strs opts;
1023         unsigned i = 1;
1024
1025         memset(&opts, 0, sizeof(opts));
1026
1027         while (i < *argc) {
1028                 char *optstr = strcmp_prefix(argv[i], "--");
1029                 char *valstr = NULL, *p;
1030                 int optid, nr_args = 1;
1031
1032                 if (!optstr) {
1033                         i++;
1034                         continue;
1035                 }
1036
1037                 optstr = strdup(optstr);
1038
1039                 p = optstr;
1040                 while (isalpha(*p) || *p == '_')
1041                         p++;
1042
1043                 if (*p == '=') {
1044                         *p = '\0';
1045                         valstr = p + 1;
1046                 }
1047
1048                 optid = bch2_opt_lookup(optstr);
1049                 if (optid < 0 ||
1050                     !(bch2_opt_table[optid].mode & opt_types)) {
1051                         i++;
1052                         goto next;
1053                 }
1054
1055                 if (!valstr &&
1056                     bch2_opt_table[optid].type != BCH_OPT_BOOL) {
1057                         nr_args = 2;
1058                         valstr = argv[i + 1];
1059                 }
1060
1061                 if (!valstr)
1062                         valstr = "1";
1063
1064                 opts.by_id[optid] = strdup(valstr);
1065
1066                 *argc -= nr_args;
1067                 memmove(&argv[i],
1068                         &argv[i + nr_args],
1069                         sizeof(char *) * (*argc - i));
1070                 argv[*argc] = NULL;
1071 next:
1072                 free(optstr);
1073         }
1074
1075         return opts;
1076 }
1077
1078 struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
1079 {
1080         struct bch_opts opts = bch2_opts_empty();
1081         unsigned i;
1082         int ret;
1083         u64 v;
1084
1085         for (i = 0; i < bch2_opts_nr; i++) {
1086                 if (!strs.by_id[i] ||
1087                     bch2_opt_table[i].type == BCH_OPT_FN)
1088                         continue;
1089
1090                 ret = bch2_opt_parse(NULL, &bch2_opt_table[i],
1091                                      strs.by_id[i], &v);
1092                 if (ret < 0)
1093                         die("Invalid %s: %s",
1094                             bch2_opt_table[i].attr.name,
1095                             strerror(-ret));
1096
1097                 bch2_opt_set_by_id(&opts, i, v);
1098         }
1099
1100         return opts;
1101 }
1102
1103 #define newline(c)              \
1104         do {                    \
1105                 printf("\n");   \
1106                 c = 0;          \
1107         } while(0)
1108 void bch2_opts_usage(unsigned opt_types)
1109 {
1110         const struct bch_option *opt;
1111         unsigned i, c = 0, helpcol = 30;
1112
1113
1114
1115         for (opt = bch2_opt_table;
1116              opt < bch2_opt_table + bch2_opts_nr;
1117              opt++) {
1118                 if (!(opt->mode & opt_types))
1119                         continue;
1120
1121                 c += printf("      --%s", opt->attr.name);
1122
1123                 switch (opt->type) {
1124                 case BCH_OPT_BOOL:
1125                         break;
1126                 case BCH_OPT_STR:
1127                         c += printf("=(");
1128                         for (i = 0; opt->choices[i]; i++) {
1129                                 if (i)
1130                                         c += printf("|");
1131                                 c += printf("%s", opt->choices[i]);
1132                         }
1133                         c += printf(")");
1134                         break;
1135                 default:
1136                         c += printf("=%s", opt->hint);
1137                         break;
1138                 }
1139
1140                 if (opt->help) {
1141                         const char *l = opt->help;
1142
1143                         if (c >= helpcol)
1144                                 newline(c);
1145
1146                         while (1) {
1147                                 const char *n = strchrnul(l, '\n');
1148
1149                                 while (c < helpcol) {
1150                                         putchar(' ');
1151                                         c++;
1152                                 }
1153                                 printf("%.*s", (int) (n - l), l);
1154                                 newline(c);
1155
1156                                 if (!*n)
1157                                         break;
1158                                 l = n + 1;
1159                         }
1160                 } else {
1161                         newline(c);
1162                 }
1163         }
1164 }
1165
1166 dev_names bchu_fs_get_devices(struct bchfs_handle fs)
1167 {
1168         DIR *dir = fdopendir(fs.sysfs_fd);
1169         struct dirent *d;
1170         dev_names devs;
1171
1172         darray_init(devs);
1173
1174         while ((errno = 0), (d = readdir(dir))) {
1175                 struct dev_name n = { 0, NULL, NULL };
1176
1177                 if (sscanf(d->d_name, "dev-%u", &n.idx) != 1)
1178                         continue;
1179
1180                 char *block_attr = mprintf("dev-%u/block", n.idx);
1181
1182                 char sysfs_block_buf[4096];
1183                 ssize_t r = readlinkat(fs.sysfs_fd, block_attr,
1184                                        sysfs_block_buf, sizeof(sysfs_block_buf));
1185                 if (r > 0) {
1186                         sysfs_block_buf[r] = '\0';
1187                         n.dev = strdup(basename(sysfs_block_buf));
1188                 }
1189
1190                 free(block_attr);
1191
1192                 char *label_attr = mprintf("dev-%u/label", n.idx);
1193                 n.label = read_file_str(fs.sysfs_fd, label_attr);
1194                 free(label_attr);
1195
1196                 darray_append(devs, n);
1197         }
1198
1199         closedir(dir);
1200
1201         return devs;
1202 }