]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.c
Update bcachefs sources to 2cb70a82bc bcachefs: delete some debug code
[bcachefs-tools-debian] / libbcachefs.c
1 #include <ctype.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/stat.h>
10 #include <sys/sysmacros.h>
11 #include <sys/types.h>
12 #include <time.h>
13 #include <unistd.h>
14
15 #include <uuid/uuid.h>
16
17 #include "libbcachefs.h"
18 #include "crypto.h"
19 #include "libbcachefs/bcachefs_format.h"
20 #include "libbcachefs/btree_cache.h"
21 #include "libbcachefs/checksum.h"
22 #include "libbcachefs/disk_groups.h"
23 #include "libbcachefs/opts.h"
24 #include "libbcachefs/replicas.h"
25 #include "libbcachefs/super-io.h"
26
27 #define NSEC_PER_SEC    1000000000L
28
29 #define BCH_MIN_NR_NBUCKETS     (1 << 10)
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 format_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                 dev->bucket_size = max(dev->bucket_size, opts.btree_node_size);
96
97                 /* Want a bucket size of at least 128k, if possible: */
98                 dev->bucket_size = max(dev->bucket_size, 256U);
99
100                 if (dev->size >= min_size(dev->bucket_size)) {
101                         unsigned scale = max(1,
102                                              ilog2(dev->size / min_size(dev->bucket_size)) / 4);
103
104                         scale = rounddown_pow_of_two(scale);
105
106                         /* max bucket size 1 mb */
107                         dev->bucket_size = min(dev->bucket_size * scale, 1U << 11);
108                 } else {
109                         do {
110                                 dev->bucket_size /= 2;
111                         } while (dev->size < min_size(dev->bucket_size));
112                 }
113         }
114
115         dev->nbuckets   = dev->size / dev->bucket_size;
116
117         if (dev->bucket_size < opts.block_size)
118                 die("Bucket size cannot be smaller than block size");
119
120         if (dev->bucket_size < opts.btree_node_size)
121                 die("Bucket size cannot be smaller than btree node size");
122
123         if (dev->nbuckets < BCH_MIN_NR_NBUCKETS)
124                 die("Not enough buckets: %llu, need %u (bucket size %u)",
125                     dev->nbuckets, BCH_MIN_NR_NBUCKETS, dev->bucket_size);
126
127 }
128
129 static unsigned parse_target(struct bch_sb_handle *sb,
130                              struct dev_opts *devs, size_t nr_devs,
131                              const char *s)
132 {
133         struct dev_opts *i;
134         int idx;
135
136         if (!s)
137                 return 0;
138
139         for (i = devs; i < devs + nr_devs; i++)
140                 if (!strcmp(s, i->path))
141                         return dev_to_target(i - devs);
142
143         idx = bch2_disk_path_find(sb, s);
144         if (idx >= 0)
145                 return group_to_target(idx);
146
147         die("Invalid target %s", s);
148         return 0;
149 }
150
151 struct bch_sb *bch2_format(struct format_opts opts,
152                            struct dev_opts *devs, size_t nr_devs)
153 {
154         struct bch_sb_handle sb = { NULL };
155         struct dev_opts *i;
156         struct bch_sb_field_members *mi;
157
158         /* calculate block size: */
159         if (!opts.block_size)
160                 for (i = devs; i < devs + nr_devs; i++)
161                         opts.block_size = max(opts.block_size,
162                                               get_blocksize(i->path, i->fd));
163
164         /* calculate bucket sizes: */
165         for (i = devs; i < devs + nr_devs; i++)
166                 bch2_pick_bucket_size(opts, i);
167
168         /* calculate btree node size: */
169         if (!opts.btree_node_size) {
170                 /* 256k default btree node size */
171                 opts.btree_node_size = 512;
172
173                 for (i = devs; i < devs + nr_devs; i++)
174                         opts.btree_node_size =
175                                 min(opts.btree_node_size, i->bucket_size);
176         }
177
178         if (!is_power_of_2(opts.block_size))
179                 die("block size must be power of 2");
180
181         if (!is_power_of_2(opts.btree_node_size))
182                 die("btree node size must be power of 2");
183
184         if (uuid_is_null(opts.uuid.b))
185                 uuid_generate(opts.uuid.b);
186
187         if (bch2_sb_realloc(&sb, 0))
188                 die("insufficient memory");
189
190         sb.sb->version          = cpu_to_le64(BCH_SB_VERSION_MAX);
191         sb.sb->magic            = BCACHE_MAGIC;
192         sb.sb->block_size       = cpu_to_le16(opts.block_size);
193         sb.sb->user_uuid        = opts.uuid;
194         sb.sb->nr_devices       = nr_devs;
195
196         uuid_generate(sb.sb->uuid.b);
197
198         if (opts.label)
199                 strncpy((char *) sb.sb->label, opts.label, sizeof(sb.sb->label));
200
201         SET_BCH_SB_CSUM_TYPE(sb.sb,             opts.meta_csum_type);
202         SET_BCH_SB_META_CSUM_TYPE(sb.sb,        opts.meta_csum_type);
203         SET_BCH_SB_DATA_CSUM_TYPE(sb.sb,        opts.data_csum_type);
204         SET_BCH_SB_COMPRESSION_TYPE(sb.sb,      opts.compression_type);
205         SET_BCH_SB_BACKGROUND_COMPRESSION_TYPE(sb.sb,
206                                                 opts.background_compression_type);
207
208         SET_BCH_SB_BTREE_NODE_SIZE(sb.sb,       opts.btree_node_size);
209         SET_BCH_SB_GC_RESERVE(sb.sb,            8);
210         SET_BCH_SB_META_REPLICAS_WANT(sb.sb,    opts.meta_replicas);
211         SET_BCH_SB_META_REPLICAS_REQ(sb.sb,     opts.meta_replicas_required);
212         SET_BCH_SB_DATA_REPLICAS_WANT(sb.sb,    opts.data_replicas);
213         SET_BCH_SB_DATA_REPLICAS_REQ(sb.sb,     opts.data_replicas_required);
214         SET_BCH_SB_ERROR_ACTION(sb.sb,          opts.on_error_action);
215         SET_BCH_SB_STR_HASH_TYPE(sb.sb,         BCH_STR_HASH_SIPHASH);
216         SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb.sb,ilog2(opts.encoded_extent_max));
217
218         SET_BCH_SB_POSIX_ACL(sb.sb,             1);
219
220         struct timespec now;
221         if (clock_gettime(CLOCK_REALTIME, &now))
222                 die("error getting current time: %m");
223
224         sb.sb->time_base_lo     = cpu_to_le64(now.tv_sec * NSEC_PER_SEC + now.tv_nsec);
225         sb.sb->time_precision   = cpu_to_le32(1);
226
227         /* Member info: */
228         mi = bch2_sb_resize_members(&sb,
229                         (sizeof(*mi) + sizeof(struct bch_member) *
230                          nr_devs) / sizeof(u64));
231
232         for (i = devs; i < devs + nr_devs; i++) {
233                 struct bch_member *m = mi->members + (i - devs);
234
235                 uuid_generate(m->uuid.b);
236                 m->nbuckets     = cpu_to_le64(i->nbuckets);
237                 m->first_bucket = 0;
238                 m->bucket_size  = cpu_to_le16(i->bucket_size);
239
240                 SET_BCH_MEMBER_REPLACEMENT(m,   CACHE_REPLACEMENT_LRU);
241                 SET_BCH_MEMBER_DISCARD(m,       i->discard);
242                 SET_BCH_MEMBER_DATA_ALLOWED(m,  i->data_allowed);
243                 SET_BCH_MEMBER_DURABILITY(m,    i->durability + 1);
244         }
245
246         /* Disk groups */
247         for (i = devs; i < devs + nr_devs; i++) {
248                 struct bch_member *m = mi->members + (i - devs);
249                 int idx;
250
251                 if (!i->group)
252                         continue;
253
254                 idx = bch2_disk_path_find_or_create(&sb, i->group);
255                 if (idx < 0)
256                         die("error creating disk path: %s", idx);
257
258                 SET_BCH_MEMBER_GROUP(m, idx + 1);
259         }
260
261         SET_BCH_SB_FOREGROUND_TARGET(sb.sb,
262                 parse_target(&sb, devs, nr_devs, opts.foreground_target));
263         SET_BCH_SB_BACKGROUND_TARGET(sb.sb,
264                 parse_target(&sb, devs, nr_devs, opts.background_target));
265         SET_BCH_SB_PROMOTE_TARGET(sb.sb,
266                 parse_target(&sb, devs, nr_devs, opts.promote_target));
267
268         /* Crypt: */
269         if (opts.encrypted) {
270                 struct bch_sb_field_crypt *crypt =
271                         bch2_sb_resize_crypt(&sb, sizeof(*crypt) / sizeof(u64));
272
273                 bch_sb_crypt_init(sb.sb, crypt, opts.passphrase);
274                 SET_BCH_SB_ENCRYPTION_TYPE(sb.sb, 1);
275         }
276
277         for (i = devs; i < devs + nr_devs; i++) {
278                 sb.sb->dev_idx = i - devs;
279
280                 init_layout(&sb.sb->layout, opts.block_size,
281                             i->sb_offset, i->sb_end);
282
283                 if (i->sb_offset == BCH_SB_SECTOR) {
284                         /* Zero start of disk */
285                         static const char zeroes[BCH_SB_SECTOR << 9];
286
287                         xpwrite(i->fd, zeroes, BCH_SB_SECTOR << 9, 0);
288                 }
289
290                 bch2_super_write(i->fd, sb.sb);
291                 close(i->fd);
292         }
293
294         return sb.sb;
295 }
296
297 void bch2_super_write(int fd, struct bch_sb *sb)
298 {
299         struct nonce nonce = { 0 };
300
301         unsigned i;
302         for (i = 0; i < sb->layout.nr_superblocks; i++) {
303                 sb->offset = sb->layout.sb_offset[i];
304
305                 if (sb->offset == BCH_SB_SECTOR) {
306                         /* Write backup layout */
307                         xpwrite(fd, &sb->layout, sizeof(sb->layout),
308                                 BCH_SB_LAYOUT_SECTOR << 9);
309                 }
310
311                 sb->csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb), nonce, sb);
312                 xpwrite(fd, sb, vstruct_bytes(sb),
313                         le64_to_cpu(sb->offset) << 9);
314         }
315
316         fsync(fd);
317 }
318
319 struct bch_sb *__bch2_super_read(int fd, u64 sector)
320 {
321         struct bch_sb sb, *ret;
322
323         xpread(fd, &sb, sizeof(sb), sector << 9);
324
325         if (memcmp(&sb.magic, &BCACHE_MAGIC, sizeof(sb.magic)))
326                 die("not a bcachefs superblock");
327
328         size_t bytes = vstruct_bytes(&sb);
329
330         ret = malloc(bytes);
331
332         xpread(fd, ret, bytes, sector << 9);
333
334         return ret;
335 }
336
337 static unsigned get_dev_has_data(struct bch_sb *sb, unsigned dev)
338 {
339         struct bch_sb_field_replicas *replicas;
340         struct bch_replicas_entry *r;
341         unsigned i, data_has = 0;
342
343         replicas = bch2_sb_get_replicas(sb);
344
345         if (replicas)
346                 for_each_replicas_entry(replicas, r)
347                         for (i = 0; i < r->nr; i++)
348                                 if (r->devs[i] == dev)
349                                         data_has |= 1 << r->data_type;
350
351         return data_has;
352 }
353
354 /* superblock printing: */
355
356 static void bch2_sb_print_layout(struct bch_sb *sb, enum units units)
357 {
358         struct bch_sb_layout *l = &sb->layout;
359         unsigned i;
360
361         printf("  type:                         %u\n"
362                "  superblock max size:          %s\n"
363                "  nr superblocks:               %u\n"
364                "  Offsets:                      ",
365                l->layout_type,
366                pr_units(1 << l->sb_max_size_bits, units),
367                l->nr_superblocks);
368
369         for (i = 0; i < l->nr_superblocks; i++) {
370                 if (i)
371                         printf(", ");
372                 printf("%llu", le64_to_cpu(l->sb_offset[i]));
373         }
374         putchar('\n');
375 }
376
377 static void bch2_sb_print_journal(struct bch_sb *sb, struct bch_sb_field *f,
378                                   enum units units)
379 {
380         struct bch_sb_field_journal *journal = field_to_type(f, journal);
381         unsigned i, nr = bch2_nr_journal_buckets(journal);
382
383         printf("  Buckets:                      ");
384         for (i = 0; i < nr; i++) {
385                 if (i)
386                         putchar(' ');
387                 printf("%llu", le64_to_cpu(journal->buckets[i]));
388         }
389         putchar('\n');
390 }
391
392 static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
393                                   enum units units)
394 {
395         struct bch_sb_field_members *mi = field_to_type(f, members);
396         struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
397         unsigned i;
398
399         for (i = 0; i < sb->nr_devices; i++) {
400                 struct bch_member *m = mi->members + i;
401                 time_t last_mount = le64_to_cpu(m->last_mount);
402                 char member_uuid_str[40];
403                 char data_allowed_str[100];
404                 char data_has_str[100];
405                 char group[64];
406
407                 if (!bch2_member_exists(m))
408                         continue;
409
410                 uuid_unparse(m->uuid.b, member_uuid_str);
411
412                 if (BCH_MEMBER_GROUP(m)) {
413                         unsigned idx = BCH_MEMBER_GROUP(m) - 1;
414
415                         if (idx < disk_groups_nr(gi)) {
416                                 memcpy(group, gi->entries[idx].label,
417                                        BCH_SB_LABEL_SIZE);
418                                 group[BCH_SB_LABEL_SIZE] = '\0';
419                         } else {
420                                 strcpy(group, "(bad disk groups section");
421                         }
422                 }
423
424                 bch2_scnprint_flag_list(data_allowed_str,
425                                         sizeof(data_allowed_str),
426                                         bch2_data_types,
427                                         BCH_MEMBER_DATA_ALLOWED(m));
428                 if (!data_allowed_str[0])
429                         strcpy(data_allowed_str, "(none)");
430
431                 bch2_scnprint_flag_list(data_has_str,
432                                         sizeof(data_has_str),
433                                         bch2_data_types,
434                                         get_dev_has_data(sb, i));
435                 if (!data_has_str[0])
436                         strcpy(data_has_str, "(none)");
437
438                 printf("  Device %u:\n"
439                        "    UUID:                       %s\n"
440                        "    Size:                       %s\n"
441                        "    Bucket size:                %s\n"
442                        "    First bucket:               %u\n"
443                        "    Buckets:                    %llu\n"
444                        "    Last mount:                 %s\n"
445                        "    State:                      %s\n"
446                        "    Group:                      %s\n"
447                        "    Data allowed:               %s\n"
448
449                        "    Has data:                   %s\n"
450
451                        "    Replacement policy:         %s\n"
452                        "    Discard:                    %llu\n",
453                        i, member_uuid_str,
454                        pr_units(le16_to_cpu(m->bucket_size) *
455                                 le64_to_cpu(m->nbuckets), units),
456                        pr_units(le16_to_cpu(m->bucket_size), units),
457                        le16_to_cpu(m->first_bucket),
458                        le64_to_cpu(m->nbuckets),
459                        last_mount ? ctime(&last_mount) : "(never)",
460
461                        BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
462                        ? bch2_dev_state[BCH_MEMBER_STATE(m)]
463                        : "unknown",
464
465                        group,
466                        data_allowed_str,
467                        data_has_str,
468
469                        BCH_MEMBER_REPLACEMENT(m) < CACHE_REPLACEMENT_NR
470                        ? bch2_cache_replacement_policies[BCH_MEMBER_REPLACEMENT(m)]
471                        : "unknown",
472
473                        BCH_MEMBER_DISCARD(m));
474         }
475 }
476
477 static void bch2_sb_print_crypt(struct bch_sb *sb, struct bch_sb_field *f,
478                                 enum units units)
479 {
480         struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
481
482         printf("  KFD:                  %llu\n"
483                "  scrypt n:             %llu\n"
484                "  scrypt r:             %llu\n"
485                "  scrypt p:             %llu\n",
486                BCH_CRYPT_KDF_TYPE(crypt),
487                BCH_KDF_SCRYPT_N(crypt),
488                BCH_KDF_SCRYPT_R(crypt),
489                BCH_KDF_SCRYPT_P(crypt));
490 }
491
492 static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
493                                    enum units units)
494 {
495         struct bch_sb_field_replicas *replicas = field_to_type(f, replicas);
496         struct bch_replicas_entry *e;
497         unsigned i;
498
499         for_each_replicas_entry(replicas, e) {
500                 printf_pad(32, "  %s:", bch2_data_types[e->data_type]);
501
502                 putchar('[');
503                 for (i = 0; i < e->nr; i++) {
504                         if (i)
505                                 putchar(' ');
506                         printf("%u", e->devs[i]);
507                 }
508                 printf("]\n");
509         }
510 }
511
512 static void bch2_sb_print_quota(struct bch_sb *sb, struct bch_sb_field *f,
513                                 enum units units)
514 {
515 }
516
517 static void bch2_sb_print_disk_groups(struct bch_sb *sb, struct bch_sb_field *f,
518                                       enum units units)
519 {
520 }
521
522 static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
523                                 enum units units)
524 {
525 }
526
527 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
528
529 struct bch_sb_field_toolops {
530         sb_field_print_fn       print;
531 };
532
533 static const struct bch_sb_field_toolops bch2_sb_field_ops[] = {
534 #define x(f, nr)                                        \
535         [BCH_SB_FIELD_##f] = {                          \
536                 .print  = bch2_sb_print_##f,            \
537         },
538         BCH_SB_FIELDS()
539 #undef x
540 };
541
542 static inline void bch2_sb_field_print(struct bch_sb *sb,
543                                        struct bch_sb_field *f,
544                                        enum units units)
545 {
546         unsigned type = le32_to_cpu(f->type);
547
548         if (type < BCH_SB_FIELD_NR)
549                 bch2_sb_field_ops[type].print(sb, f, units);
550         else
551                 printf("(unknown field %u)\n", type);
552 }
553
554 void bch2_sb_print(struct bch_sb *sb, bool print_layout,
555                    unsigned fields, enum units units)
556 {
557         struct bch_sb_field_members *mi;
558         char user_uuid_str[40], internal_uuid_str[40];
559         char fields_have_str[200];
560         char label[BCH_SB_LABEL_SIZE + 1];
561         struct bch_sb_field *f;
562         u64 fields_have = 0;
563         unsigned nr_devices = 0;
564
565         memset(label, 0, sizeof(label));
566         memcpy(label, sb->label, sizeof(sb->label));
567         uuid_unparse(sb->user_uuid.b, user_uuid_str);
568         uuid_unparse(sb->uuid.b, internal_uuid_str);
569
570         mi = bch2_sb_get_members(sb);
571         if (mi) {
572                 struct bch_member *m;
573
574                 for (m = mi->members;
575                      m < mi->members + sb->nr_devices;
576                      m++)
577                         nr_devices += bch2_member_exists(m);
578         }
579
580         vstruct_for_each(sb, f)
581                 fields_have |= 1 << le32_to_cpu(f->type);
582         bch2_scnprint_flag_list(fields_have_str, sizeof(fields_have_str),
583                                 bch2_sb_fields, fields_have);
584
585         printf("External UUID:                  %s\n"
586                "Internal UUID:                  %s\n"
587                "Label:                          %s\n"
588                "Version:                        %llu\n"
589                "Block_size:                     %s\n"
590                "Btree node size:                %s\n"
591                "Error action:                   %s\n"
592                "Clean:                          %llu\n"
593
594                "Metadata replicas:              %llu\n"
595                "Data replicas:                  %llu\n"
596
597                "Metadata checksum type:         %s (%llu)\n"
598                "Data checksum type:             %s (%llu)\n"
599                "Compression type:               %s (%llu)\n"
600
601                "Foreground write target:        %llu\n"
602                "Background write target:        %llu\n"
603                "Promote target:                 %llu\n"
604
605                "String hash type:               %s (%llu)\n"
606                "32 bit inodes:                  %llu\n"
607                "GC reserve percentage:          %llu%%\n"
608                "Root reserve percentage:        %llu%%\n"
609
610                "Devices:                        %u live, %u total\n"
611                "Sections:                       %s\n"
612                "Superblock size:                %llu\n",
613                user_uuid_str,
614                internal_uuid_str,
615                label,
616                le64_to_cpu(sb->version),
617                pr_units(le16_to_cpu(sb->block_size), units),
618                pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
619
620                BCH_SB_ERROR_ACTION(sb) < BCH_NR_ERROR_ACTIONS
621                ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
622                : "unknown",
623
624                BCH_SB_CLEAN(sb),
625
626                BCH_SB_META_REPLICAS_WANT(sb),
627                BCH_SB_DATA_REPLICAS_WANT(sb),
628
629                BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
630                ? bch2_csum_types[BCH_SB_META_CSUM_TYPE(sb)]
631                : "unknown",
632                BCH_SB_META_CSUM_TYPE(sb),
633
634                BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
635                ? bch2_csum_types[BCH_SB_DATA_CSUM_TYPE(sb)]
636                : "unknown",
637                BCH_SB_DATA_CSUM_TYPE(sb),
638
639                BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
640                ? bch2_compression_types[BCH_SB_COMPRESSION_TYPE(sb)]
641                : "unknown",
642                BCH_SB_COMPRESSION_TYPE(sb),
643
644                BCH_SB_FOREGROUND_TARGET(sb),
645                BCH_SB_BACKGROUND_TARGET(sb),
646                BCH_SB_PROMOTE_TARGET(sb),
647
648                BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
649                ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
650                : "unknown",
651                BCH_SB_STR_HASH_TYPE(sb),
652
653                BCH_SB_INODE_32BIT(sb),
654                BCH_SB_GC_RESERVE(sb),
655                BCH_SB_ROOT_RESERVE(sb),
656
657                nr_devices, sb->nr_devices,
658                fields_have_str,
659                vstruct_bytes(sb));
660
661         if (print_layout) {
662                 printf("\n"
663                        "Layout:\n");
664                 bch2_sb_print_layout(sb, units);
665         }
666
667         vstruct_for_each(sb, f) {
668                 unsigned type = le32_to_cpu(f->type);
669                 char name[60];
670
671                 if (!(fields & (1 << type)))
672                         continue;
673
674                 if (type < BCH_SB_FIELD_NR) {
675                         scnprintf(name, sizeof(name), "%s", bch2_sb_fields[type]);
676                         name[0] = toupper(name[0]);
677                 } else {
678                         scnprintf(name, sizeof(name), "(unknown field %u)", type);
679                 }
680
681                 printf("\n%s (size %llu):\n", name, vstruct_bytes(f));
682                 if (type < BCH_SB_FIELD_NR)
683                         bch2_sb_field_print(sb, f, units);
684         }
685 }
686
687 /* ioctl interface: */
688
689 /* Global control device: */
690 int bcachectl_open(void)
691 {
692         return xopen("/dev/bcachefs-ctl", O_RDWR);
693 }
694
695 /* Filesystem handles (ioctl, sysfs dir): */
696
697 #define SYSFS_BASE "/sys/fs/bcachefs/"
698
699 void bcache_fs_close(struct bchfs_handle fs)
700 {
701         close(fs.ioctl_fd);
702         close(fs.sysfs_fd);
703 }
704
705 struct bchfs_handle bcache_fs_open(const char *path)
706 {
707         struct bchfs_handle ret;
708
709         if (!uuid_parse(path, ret.uuid.b)) {
710                 /* It's a UUID, look it up in sysfs: */
711                 char *sysfs = mprintf(SYSFS_BASE "%s", path);
712                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
713
714                 char *minor = read_file_str(ret.sysfs_fd, "minor");
715                 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
716                 ret.ioctl_fd = xopen(ctl, O_RDWR);
717
718                 free(sysfs);
719                 free(minor);
720                 free(ctl);
721         } else {
722                 /* It's a path: */
723                 ret.ioctl_fd = xopen(path, O_RDONLY);
724
725                 struct bch_ioctl_query_uuid uuid;
726                 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
727                         die("error opening %s: not a bcachefs filesystem", path);
728
729                 ret.uuid = uuid.uuid;
730
731                 char uuid_str[40];
732                 uuid_unparse(uuid.uuid.b, uuid_str);
733
734                 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
735                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
736                 free(sysfs);
737         }
738
739         return ret;
740 }
741
742 /*
743  * Given a path to a block device, open the filesystem it belongs to; also
744  * return the device's idx:
745  */
746 struct bchfs_handle bchu_fs_open_by_dev(const char *path, unsigned *idx)
747 {
748         char buf[1024], *uuid_str;
749
750         struct stat stat = xstat(path);
751
752         if (!S_ISBLK(stat.st_mode))
753                 die("%s is not a block device", path);
754
755         char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
756                               major(stat.st_dev),
757                               minor(stat.st_dev));
758         ssize_t len = readlink(sysfs, buf, sizeof(buf));
759         free(sysfs);
760
761         if (len > 0) {
762                 char *p = strrchr(buf, '/');
763                 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
764                         die("error parsing sysfs");
765
766                 *p = '\0';
767                 p = strrchr(buf, '/');
768                 uuid_str = p + 1;
769         } else {
770                 struct bch_opts opts = bch2_opts_empty();
771
772                 opt_set(opts, noexcl,   true);
773                 opt_set(opts, nochanges, true);
774
775                 struct bch_sb_handle sb;
776                 int ret = bch2_read_super(path, &opts, &sb);
777                 if (ret)
778                         die("Error opening %s: %s", path, strerror(-ret));
779
780                 *idx = sb.sb->dev_idx;
781                 uuid_str = buf;
782                 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
783
784                 bch2_free_super(&sb);
785         }
786
787         return bcache_fs_open(uuid_str);
788 }
789
790 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
791 {
792         int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
793
794         while (1) {
795                 struct bch_ioctl_data_progress p;
796
797                 if (read(progress_fd, &p, sizeof(p)) != sizeof(p))
798                         die("error reading from progress fd");
799
800                 if (p.data_type == U8_MAX)
801                         break;
802
803                 printf("\33[2K\r");
804
805                 printf("%llu%% complete: current position %s",
806                        p.sectors_done * 100 / p.sectors_total,
807                        bch2_data_types[p.data_type]);
808
809                 switch (p.data_type) {
810                 case BCH_DATA_BTREE:
811                 case BCH_DATA_USER:
812                         printf(" %s:%llu:%llu",
813                                bch2_btree_ids[p.btree_id],
814                                p.pos.inode,
815                                p.pos.offset);
816                 }
817
818                 sleep(1);
819         }
820         printf("\nDone\n");
821
822         close(progress_fd);
823         return 0;
824 }