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