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