11 #include <sys/sysmacros.h>
12 #include <sys/types.h>
16 #include <uuid/uuid.h>
18 #include "libbcachefs.h"
20 #include "libbcachefs/bcachefs_format.h"
21 #include "libbcachefs/btree_cache.h"
22 #include "libbcachefs/checksum.h"
23 #include "libbcachefs/disk_groups.h"
24 #include "libbcachefs/journal_seq_blacklist.h"
25 #include "libbcachefs/opts.h"
26 #include "libbcachefs/replicas.h"
27 #include "libbcachefs/super-io.h"
28 #include "tools-util.h"
30 #define NSEC_PER_SEC 1000000000L
32 /* minimum size filesystem we can create, given a bucket size: */
33 static u64 min_size(unsigned bucket_size)
35 return BCH_MIN_NR_NBUCKETS * bucket_size;
38 static void init_layout(struct bch_sb_layout *l,
41 u64 sb_start, u64 sb_end)
45 memset(l, 0, sizeof(*l));
47 l->magic = BCACHE_MAGIC;
49 l->nr_superblocks = 2;
50 l->sb_max_size_bits = ilog2(sb_size);
52 /* Create two superblocks in the allowed range: */
53 for (i = 0; i < l->nr_superblocks; i++) {
54 if (sb_start != BCH_SB_SECTOR)
55 sb_start = round_up(sb_start, block_size);
57 l->sb_offset[i] = cpu_to_le64(sb_start);
61 if (sb_start >= sb_end)
62 die("insufficient space for superblocks");
65 void bch2_pick_bucket_size(struct bch_opts opts, struct dev_opts *dev)
68 dev->size = get_size(dev->path, dev->fd) >> 9;
70 if (!dev->bucket_size) {
71 if (dev->size < min_size(opts.block_size))
72 die("cannot format %s, too small (%llu sectors, min %llu)",
73 dev->path, dev->size, min_size(opts.block_size));
75 /* Bucket size must be >= block size: */
76 dev->bucket_size = opts.block_size;
78 /* Bucket size must be >= btree node size: */
79 if (opt_defined(opts, btree_node_size))
80 dev->bucket_size = max_t(unsigned, dev->bucket_size,
81 opts.btree_node_size);
83 /* Want a bucket size of at least 128k, if possible: */
84 dev->bucket_size = max(dev->bucket_size, 256U);
86 if (dev->size >= min_size(dev->bucket_size)) {
87 unsigned scale = max(1,
88 ilog2(dev->size / min_size(dev->bucket_size)) / 4);
90 scale = rounddown_pow_of_two(scale);
92 /* max bucket size 1 mb */
93 dev->bucket_size = min(dev->bucket_size * scale, 1U << 11);
96 dev->bucket_size /= 2;
97 } while (dev->size < min_size(dev->bucket_size));
101 dev->nbuckets = dev->size / dev->bucket_size;
103 if (dev->bucket_size < opts.block_size)
104 die("Bucket size cannot be smaller than block size");
106 if (opt_defined(opts, btree_node_size) &&
107 dev->bucket_size < opts.btree_node_size)
108 die("Bucket size cannot be smaller than btree node size");
110 if (dev->nbuckets < BCH_MIN_NR_NBUCKETS)
111 die("Not enough buckets: %llu, need %u (bucket size %u)",
112 dev->nbuckets, BCH_MIN_NR_NBUCKETS, dev->bucket_size);
116 static unsigned parse_target(struct bch_sb_handle *sb,
117 struct dev_opts *devs, size_t nr_devs,
126 for (i = devs; i < devs + nr_devs; i++)
127 if (!strcmp(s, i->path))
128 return dev_to_target(i - devs);
130 idx = bch2_disk_path_find(sb, s);
132 return group_to_target(idx);
134 die("Invalid target %s", s);
138 struct bch_sb *bch2_format(struct bch_opt_strs fs_opt_strs,
139 struct bch_opts fs_opts,
140 struct format_opts opts,
141 struct dev_opts *devs,
144 struct bch_sb_handle sb = { NULL };
146 struct bch_sb_field_members *mi;
147 unsigned max_dev_block_size = 0;
150 for (i = devs; i < devs + nr_devs; i++)
151 max_dev_block_size = max(max_dev_block_size,
152 get_blocksize(i->path, i->fd));
154 /* calculate block size: */
155 if (!opt_defined(fs_opts, block_size)) {
156 opt_set(fs_opts, block_size, max_dev_block_size);
157 } else if (fs_opts.block_size < max_dev_block_size)
158 die("blocksize too small: %u, must be greater than device blocksize %u",
159 fs_opts.block_size, max_dev_block_size);
161 /* calculate bucket sizes: */
162 for (i = devs; i < devs + nr_devs; i++)
163 bch2_pick_bucket_size(fs_opts, i);
165 /* calculate btree node size: */
166 if (!opt_defined(fs_opts, btree_node_size)) {
167 /* 256k default btree node size */
168 opt_set(fs_opts, btree_node_size, 512);
170 for (i = devs; i < devs + nr_devs; i++)
171 fs_opts.btree_node_size =
172 min_t(unsigned, fs_opts.btree_node_size,
176 if (!is_power_of_2(fs_opts.block_size))
177 die("block size must be power of 2");
179 if (!is_power_of_2(fs_opts.btree_node_size))
180 die("btree node size must be power of 2");
182 if (uuid_is_null(opts.uuid.b))
183 uuid_generate(opts.uuid.b);
185 if (bch2_sb_realloc(&sb, 0))
186 die("insufficient memory");
188 sb.sb->version = le16_to_cpu(opts.version);
189 sb.sb->version_min = le16_to_cpu(opts.version);
190 sb.sb->magic = BCACHE_MAGIC;
191 sb.sb->block_size = cpu_to_le16(fs_opts.block_size);
192 sb.sb->user_uuid = opts.uuid;
193 sb.sb->nr_devices = nr_devs;
195 if (opts.version == bcachefs_metadata_version_current)
196 sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
198 uuid_generate(sb.sb->uuid.b);
203 min(strlen(opts.label), sizeof(sb.sb->label)));
206 opt_id < bch2_opts_nr;
208 const struct bch_option *opt = &bch2_opt_table[opt_id];
211 if (opt->set_sb == SET_NO_SB_OPT)
214 v = bch2_opt_defined_by_id(&fs_opts, opt_id)
215 ? bch2_opt_get_by_id(&fs_opts, opt_id)
216 : bch2_opt_get_by_id(&bch2_opts_default, opt_id);
218 opt->set_sb(sb.sb, v);
221 SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb.sb,
222 ilog2(opts.encoded_extent_max));
225 if (clock_gettime(CLOCK_REALTIME, &now))
226 die("error getting current time: %m");
228 sb.sb->time_base_lo = cpu_to_le64(now.tv_sec * NSEC_PER_SEC + now.tv_nsec);
229 sb.sb->time_precision = cpu_to_le32(1);
232 mi = bch2_sb_resize_members(&sb,
233 (sizeof(*mi) + sizeof(struct bch_member) *
234 nr_devs) / sizeof(u64));
236 for (i = devs; i < devs + nr_devs; i++) {
237 struct bch_member *m = mi->members + (i - devs);
239 uuid_generate(m->uuid.b);
240 m->nbuckets = cpu_to_le64(i->nbuckets);
242 m->bucket_size = cpu_to_le16(i->bucket_size);
244 SET_BCH_MEMBER_REPLACEMENT(m, BCH_CACHE_REPLACEMENT_lru);
245 SET_BCH_MEMBER_DISCARD(m, i->discard);
246 SET_BCH_MEMBER_DATA_ALLOWED(m, i->data_allowed);
247 SET_BCH_MEMBER_DURABILITY(m, i->durability + 1);
251 for (i = devs; i < devs + nr_devs; i++) {
252 struct bch_member *m = mi->members + (i - devs);
258 idx = bch2_disk_path_find_or_create(&sb, i->group);
260 die("error creating disk path: %s", idx);
262 SET_BCH_MEMBER_GROUP(m, idx + 1);
265 SET_BCH_SB_FOREGROUND_TARGET(sb.sb,
266 parse_target(&sb, devs, nr_devs, fs_opt_strs.foreground_target));
267 SET_BCH_SB_BACKGROUND_TARGET(sb.sb,
268 parse_target(&sb, devs, nr_devs, fs_opt_strs.background_target));
269 SET_BCH_SB_PROMOTE_TARGET(sb.sb,
270 parse_target(&sb, devs, nr_devs, fs_opt_strs.promote_target));
271 SET_BCH_SB_METADATA_TARGET(sb.sb,
272 parse_target(&sb, devs, nr_devs, fs_opt_strs.metadata_target));
275 if (opts.encrypted) {
276 struct bch_sb_field_crypt *crypt =
277 bch2_sb_resize_crypt(&sb, sizeof(*crypt) / sizeof(u64));
279 bch_sb_crypt_init(sb.sb, crypt, opts.passphrase);
280 SET_BCH_SB_ENCRYPTION_TYPE(sb.sb, 1);
283 for (i = devs; i < devs + nr_devs; i++) {
284 sb.sb->dev_idx = i - devs;
287 i->sb_offset = BCH_SB_SECTOR;
291 init_layout(&sb.sb->layout, fs_opts.block_size,
292 opts.superblock_size,
293 i->sb_offset, i->sb_end);
296 * Also create a backup superblock at the end of the disk:
298 * If we're not creating a superblock at the default offset, it
299 * means we're being run from the migrate tool and we could be
300 * overwriting existing data if we write to the end of the disk:
302 if (i->sb_offset == BCH_SB_SECTOR) {
303 struct bch_sb_layout *l = &sb.sb->layout;
304 u64 backup_sb = i->size - (1 << l->sb_max_size_bits);
306 backup_sb = rounddown(backup_sb, i->bucket_size);
307 l->sb_offset[l->nr_superblocks++] = cpu_to_le64(backup_sb);
310 if (i->sb_offset == BCH_SB_SECTOR) {
311 /* Zero start of disk */
312 static const char zeroes[BCH_SB_SECTOR << 9];
314 xpwrite(i->fd, zeroes, BCH_SB_SECTOR << 9, 0);
317 bch2_super_write(i->fd, sb.sb);
324 void bch2_super_write(int fd, struct bch_sb *sb)
326 struct nonce nonce = { 0 };
329 for (i = 0; i < sb->layout.nr_superblocks; i++) {
330 sb->offset = sb->layout.sb_offset[i];
332 if (sb->offset == BCH_SB_SECTOR) {
333 /* Write backup layout */
334 xpwrite(fd, &sb->layout, sizeof(sb->layout),
335 BCH_SB_LAYOUT_SECTOR << 9);
338 sb->csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb), nonce, sb);
339 xpwrite(fd, sb, vstruct_bytes(sb),
340 le64_to_cpu(sb->offset) << 9);
346 struct bch_sb *__bch2_super_read(int fd, u64 sector)
348 struct bch_sb sb, *ret;
350 xpread(fd, &sb, sizeof(sb), sector << 9);
352 if (memcmp(&sb.magic, &BCACHE_MAGIC, sizeof(sb.magic)))
353 die("not a bcachefs superblock");
355 size_t bytes = vstruct_bytes(&sb);
359 xpread(fd, ret, bytes, sector << 9);
364 static unsigned get_dev_has_data(struct bch_sb *sb, unsigned dev)
366 struct bch_sb_field_replicas *replicas;
367 struct bch_replicas_entry *r;
368 unsigned i, data_has = 0;
370 replicas = bch2_sb_get_replicas(sb);
373 for_each_replicas_entry(replicas, r)
374 for (i = 0; i < r->nr_devs; i++)
375 if (r->devs[i] == dev)
376 data_has |= 1 << r->data_type;
381 static int bch2_sb_get_target(struct bch_sb *sb, char *buf, size_t len, u64 v)
383 struct target t = target_decode(v);
388 return scnprintf(buf, len, "none");
390 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
391 struct bch_member *m = mi->members + t.dev;
393 if (bch2_dev_exists(sb, mi, t.dev)) {
396 uuid_unparse(m->uuid.b, uuid_str);
398 ret = scnprintf(buf, len, "Device %u (%s)", t.dev,
401 ret = scnprintf(buf, len, "Bad device %u", t.dev);
407 struct bch_sb_field_disk_groups *gi;
408 gi = bch2_sb_get_disk_groups(sb);
410 struct bch_disk_group *g = gi->entries + t.group;
412 if (t.group < disk_groups_nr(gi) && !BCH_GROUP_DELETED(g)) {
413 ret = scnprintf(buf, len, "Group %u (%.*s)", t.group,
414 BCH_SB_LABEL_SIZE, g->label);
416 ret = scnprintf(buf, len, "Bad group %u", t.group);
427 /* superblock printing: */
429 static void bch2_sb_print_layout(struct bch_sb *sb, enum units units)
431 struct bch_sb_layout *l = &sb->layout;
435 " superblock max size: %s\n"
436 " nr superblocks: %u\n"
439 pr_units(1 << l->sb_max_size_bits, units),
442 for (i = 0; i < l->nr_superblocks; i++) {
445 printf("%llu", le64_to_cpu(l->sb_offset[i]));
450 static void bch2_sb_print_journal(struct bch_sb *sb, struct bch_sb_field *f,
453 struct bch_sb_field_journal *journal = field_to_type(f, journal);
454 unsigned i, nr = bch2_nr_journal_buckets(journal);
456 printf(" Buckets: ");
457 for (i = 0; i < nr; i++) {
460 printf("%llu", le64_to_cpu(journal->buckets[i]));
465 static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
468 struct bch_sb_field_members *mi = field_to_type(f, members);
469 struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
472 for (i = 0; i < sb->nr_devices; i++) {
473 struct bch_member *m = mi->members + i;
474 time_t last_mount = le64_to_cpu(m->last_mount);
475 char member_uuid_str[40];
476 char data_allowed_str[100];
477 char data_has_str[100];
478 char group[BCH_SB_LABEL_SIZE+10];
481 if (!bch2_member_exists(m))
484 uuid_unparse(m->uuid.b, member_uuid_str);
486 if (BCH_MEMBER_GROUP(m)) {
487 unsigned idx = BCH_MEMBER_GROUP(m) - 1;
489 if (idx < disk_groups_nr(gi)) {
490 snprintf(group, sizeof(group), "%.*s (%u)",
492 gi->entries[idx].label, idx);
494 strcpy(group, "(bad disk groups section)");
497 strcpy(group, "(none)");
500 bch2_flags_to_text(&PBUF(data_allowed_str),
502 BCH_MEMBER_DATA_ALLOWED(m));
503 if (!data_allowed_str[0])
504 strcpy(data_allowed_str, "(none)");
506 bch2_flags_to_text(&PBUF(data_has_str),
508 get_dev_has_data(sb, i));
509 if (!data_has_str[0])
510 strcpy(data_has_str, "(none)");
513 struct tm *tm = localtime(&last_mount);
514 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
516 strcpy(time_str, "(formatting error)");
518 strcpy(time_str, "(never)");
521 printf(" Device %u:\n"
525 " First bucket: %u\n"
530 " Data allowed: %s\n"
534 " Replacement policy: %s\n"
537 pr_units(le16_to_cpu(m->bucket_size) *
538 le64_to_cpu(m->nbuckets), units),
539 pr_units(le16_to_cpu(m->bucket_size), units),
540 le16_to_cpu(m->first_bucket),
541 le64_to_cpu(m->nbuckets),
544 BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
545 ? bch2_member_states[BCH_MEMBER_STATE(m)]
552 BCH_MEMBER_REPLACEMENT(m) < BCH_CACHE_REPLACEMENT_NR
553 ? bch2_cache_replacement_policies[BCH_MEMBER_REPLACEMENT(m)]
556 BCH_MEMBER_DISCARD(m));
560 static void bch2_sb_print_crypt(struct bch_sb *sb, struct bch_sb_field *f,
563 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
565 printf(" KFD: %llu\n"
569 BCH_CRYPT_KDF_TYPE(crypt),
570 BCH_KDF_SCRYPT_N(crypt),
571 BCH_KDF_SCRYPT_R(crypt),
572 BCH_KDF_SCRYPT_P(crypt));
575 static void bch2_sb_print_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f,
578 struct bch_sb_field_replicas_v0 *replicas = field_to_type(f, replicas_v0);
579 struct bch_replicas_entry_v0 *e;
582 for_each_replicas_entry(replicas, e) {
583 printf_pad(32, " %s:", bch2_data_types[e->data_type]);
586 for (i = 0; i < e->nr_devs; i++) {
589 printf("%u", e->devs[i]);
595 static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
598 struct bch_sb_field_replicas *replicas = field_to_type(f, replicas);
599 struct bch_replicas_entry *e;
602 for_each_replicas_entry(replicas, e) {
603 printf_pad(32, " %s: %u/%u",
604 bch2_data_types[e->data_type],
609 for (i = 0; i < e->nr_devs; i++) {
612 printf("%u", e->devs[i]);
618 static void bch2_sb_print_quota(struct bch_sb *sb, struct bch_sb_field *f,
623 static void bch2_sb_print_disk_groups(struct bch_sb *sb, struct bch_sb_field *f,
628 static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
631 struct bch_sb_field_clean *clean = field_to_type(f, clean);
634 printf(" flags: %x", le32_to_cpu(clean->flags));
635 printf(" journal seq: %llx", le64_to_cpu(clean->journal_seq));
638 static void bch2_sb_print_journal_seq_blacklist(struct bch_sb *sb, struct bch_sb_field *f,
641 struct bch_sb_field_journal_seq_blacklist *bl = field_to_type(f, journal_seq_blacklist);
642 unsigned i, nr = blacklist_nr_entries(bl);
644 for (i = 0; i < nr; i++) {
645 struct journal_seq_blacklist_entry *e =
648 printf(" %llu-%llu\n",
649 le64_to_cpu(e->start),
650 le64_to_cpu(e->end));
654 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
656 struct bch_sb_field_toolops {
657 sb_field_print_fn print;
660 static const struct bch_sb_field_toolops bch2_sb_field_ops[] = {
662 [BCH_SB_FIELD_##f] = { \
663 .print = bch2_sb_print_##f, \
669 static inline void bch2_sb_field_print(struct bch_sb *sb,
670 struct bch_sb_field *f,
673 unsigned type = le32_to_cpu(f->type);
675 if (type < BCH_SB_FIELD_NR)
676 bch2_sb_field_ops[type].print(sb, f, units);
678 printf("(unknown field %u)\n", type);
681 void bch2_sb_print(struct bch_sb *sb, bool print_layout,
682 unsigned fields, enum units units)
684 struct bch_sb_field_members *mi;
685 char user_uuid_str[40], internal_uuid_str[40];
686 char features_str[500];
687 char compat_features_str[500];
688 char fields_have_str[200];
689 char label[BCH_SB_LABEL_SIZE + 1];
691 char foreground_str[64];
692 char background_str[64];
693 char promote_str[64];
694 char metadata_str[64];
695 struct bch_sb_field *f;
697 unsigned nr_devices = 0;
698 time_t time_base = le64_to_cpu(sb->time_base_lo) / NSEC_PER_SEC;
700 memcpy(label, sb->label, BCH_SB_LABEL_SIZE);
701 label[BCH_SB_LABEL_SIZE] = '\0';
703 uuid_unparse(sb->user_uuid.b, user_uuid_str);
704 uuid_unparse(sb->uuid.b, internal_uuid_str);
707 struct tm *tm = localtime(&time_base);
708 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
710 strcpy(time_str, "(formatting error)");
712 strcpy(time_str, "(not set)");
715 mi = bch2_sb_get_members(sb);
717 struct bch_member *m;
719 for (m = mi->members;
720 m < mi->members + sb->nr_devices;
722 nr_devices += bch2_member_exists(m);
725 bch2_sb_get_target(sb, foreground_str, sizeof(foreground_str),
726 BCH_SB_FOREGROUND_TARGET(sb));
728 bch2_sb_get_target(sb, background_str, sizeof(background_str),
729 BCH_SB_BACKGROUND_TARGET(sb));
731 bch2_sb_get_target(sb, promote_str, sizeof(promote_str),
732 BCH_SB_PROMOTE_TARGET(sb));
734 bch2_sb_get_target(sb, metadata_str, sizeof(metadata_str),
735 BCH_SB_METADATA_TARGET(sb));
737 bch2_flags_to_text(&PBUF(features_str),
739 le64_to_cpu(sb->features[0]));
741 bch2_flags_to_text(&PBUF(compat_features_str),
743 le64_to_cpu(sb->compat[0]));
745 vstruct_for_each(sb, f)
746 fields_have |= 1 << le32_to_cpu(f->type);
747 bch2_flags_to_text(&PBUF(fields_have_str),
748 bch2_sb_fields, fields_have);
750 printf("External UUID: %s\n"
751 "Internal UUID: %s\n"
755 "Oldest version on disk: %u\n"
757 "Squence number: %llu\n"
759 "Btree node size: %s\n"
763 "Compat features: %s\n"
765 "Metadata replicas: %llu\n"
766 "Data replicas: %llu\n"
768 "Metadata checksum type: %s (%llu)\n"
769 "Data checksum type: %s (%llu)\n"
770 "Compression type: %s (%llu)\n"
772 "Foreground write target: %s\n"
773 "Background write target: %s\n"
774 "Promote target: %s\n"
775 "Metadata target: %s\n"
777 "String hash type: %s (%llu)\n"
778 "32 bit inodes: %llu\n"
779 "GC reserve percentage: %llu%%\n"
780 "Root reserve percentage: %llu%%\n"
782 "Devices: %u live, %u total\n"
784 "Superblock size: %llu\n",
789 le16_to_cpu(sb->version),
790 le16_to_cpu(sb->version_min),
792 le64_to_cpu(sb->seq),
793 pr_units(le16_to_cpu(sb->block_size), units),
794 pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
796 BCH_SB_ERROR_ACTION(sb) < BCH_ON_ERROR_NR
797 ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
804 BCH_SB_META_REPLICAS_WANT(sb),
805 BCH_SB_DATA_REPLICAS_WANT(sb),
807 BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
808 ? bch2_csum_opts[BCH_SB_META_CSUM_TYPE(sb)]
810 BCH_SB_META_CSUM_TYPE(sb),
812 BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
813 ? bch2_csum_opts[BCH_SB_DATA_CSUM_TYPE(sb)]
815 BCH_SB_DATA_CSUM_TYPE(sb),
817 BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
818 ? bch2_compression_opts[BCH_SB_COMPRESSION_TYPE(sb)]
820 BCH_SB_COMPRESSION_TYPE(sb),
827 BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
828 ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
830 BCH_SB_STR_HASH_TYPE(sb),
832 BCH_SB_INODE_32BIT(sb),
833 BCH_SB_GC_RESERVE(sb),
834 BCH_SB_ROOT_RESERVE(sb),
836 nr_devices, sb->nr_devices,
843 bch2_sb_print_layout(sb, units);
846 vstruct_for_each(sb, f) {
847 unsigned type = le32_to_cpu(f->type);
850 if (!(fields & (1 << type)))
853 if (type < BCH_SB_FIELD_NR) {
854 scnprintf(name, sizeof(name), "%s", bch2_sb_fields[type]);
855 name[0] = toupper(name[0]);
857 scnprintf(name, sizeof(name), "(unknown field %u)", type);
860 printf("\n%s (size %llu):\n", name, vstruct_bytes(f));
861 if (type < BCH_SB_FIELD_NR)
862 bch2_sb_field_print(sb, f, units);
866 /* ioctl interface: */
868 /* Global control device: */
869 int bcachectl_open(void)
871 return xopen("/dev/bcachefs-ctl", O_RDWR);
874 /* Filesystem handles (ioctl, sysfs dir): */
876 #define SYSFS_BASE "/sys/fs/bcachefs/"
878 void bcache_fs_close(struct bchfs_handle fs)
884 struct bchfs_handle bcache_fs_open(const char *path)
886 struct bchfs_handle ret;
888 if (!uuid_parse(path, ret.uuid.b)) {
889 /* It's a UUID, look it up in sysfs: */
890 char *sysfs = mprintf(SYSFS_BASE "%s", path);
891 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
893 char *minor = read_file_str(ret.sysfs_fd, "minor");
894 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
895 ret.ioctl_fd = xopen(ctl, O_RDWR);
902 ret.ioctl_fd = xopen(path, O_RDONLY);
904 struct bch_ioctl_query_uuid uuid;
905 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
906 die("error opening %s: not a bcachefs filesystem", path);
908 ret.uuid = uuid.uuid;
911 uuid_unparse(uuid.uuid.b, uuid_str);
913 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
914 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
922 * Given a path to a block device, open the filesystem it belongs to; also
923 * return the device's idx:
925 struct bchfs_handle bchu_fs_open_by_dev(const char *path, int *idx)
927 char buf[1024], *uuid_str;
929 struct stat stat = xstat(path);
931 if (!S_ISBLK(stat.st_mode))
932 die("%s is not a block device", path);
934 char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
937 ssize_t len = readlink(sysfs, buf, sizeof(buf));
941 char *p = strrchr(buf, '/');
942 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
943 die("error parsing sysfs");
946 p = strrchr(buf, '/');
949 struct bch_opts opts = bch2_opts_empty();
951 opt_set(opts, noexcl, true);
952 opt_set(opts, nochanges, true);
954 struct bch_sb_handle sb;
955 int ret = bch2_read_super(path, &opts, &sb);
957 die("Error opening %s: %s", path, strerror(-ret));
959 *idx = sb.sb->dev_idx;
961 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
963 bch2_free_super(&sb);
966 return bcache_fs_open(uuid_str);
969 int bchu_dev_path_to_idx(struct bchfs_handle fs, const char *dev_path)
972 struct bchfs_handle fs2 = bchu_fs_open_by_dev(dev_path, &idx);
974 if (memcmp(&fs.uuid, &fs2.uuid, sizeof(fs.uuid)))
976 bcache_fs_close(fs2);
980 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
982 int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
985 struct bch_ioctl_data_event e;
987 if (read(progress_fd, &e, sizeof(e)) != sizeof(e))
988 die("error reading from progress fd %m");
993 if (e.p.data_type == U8_MAX)
998 printf("%llu%% complete: current position %s",
1000 ? e.p.sectors_done * 100 / e.p.sectors_total
1002 bch2_data_types[e.p.data_type]);
1004 switch (e.p.data_type) {
1005 case BCH_DATA_btree:
1007 printf(" %s:%llu:%llu",
1008 bch2_btree_ids[e.p.btree_id],
1022 /* option parsing */
1024 void bch2_opt_strs_free(struct bch_opt_strs *opts)
1028 for (i = 0; i < bch2_opts_nr; i++) {
1029 free(opts->by_id[i]);
1030 opts->by_id[i] = NULL;
1034 struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
1037 struct bch_opt_strs opts;
1040 memset(&opts, 0, sizeof(opts));
1043 char *optstr = strcmp_prefix(argv[i], "--");
1044 char *valstr = NULL, *p;
1045 int optid, nr_args = 1;
1052 optstr = strdup(optstr);
1055 while (isalpha(*p) || *p == '_')
1063 optid = bch2_opt_lookup(optstr);
1065 !(bch2_opt_table[optid].mode & opt_types)) {
1071 bch2_opt_table[optid].type != BCH_OPT_BOOL) {
1073 valstr = argv[i + 1];
1079 opts.by_id[optid] = strdup(valstr);
1084 sizeof(char *) * (*argc - i));
1093 struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
1095 struct bch_opts opts = bch2_opts_empty();
1100 for (i = 0; i < bch2_opts_nr; i++) {
1101 if (!strs.by_id[i] ||
1102 bch2_opt_table[i].type == BCH_OPT_FN)
1105 ret = bch2_opt_parse(NULL, &bch2_opt_table[i],
1108 die("Invalid %s: %s",
1109 bch2_opt_table[i].attr.name,
1112 bch2_opt_set_by_id(&opts, i, v);
1118 #define newline(c) \
1123 void bch2_opts_usage(unsigned opt_types)
1125 const struct bch_option *opt;
1126 unsigned i, c = 0, helpcol = 30;
1130 for (opt = bch2_opt_table;
1131 opt < bch2_opt_table + bch2_opts_nr;
1133 if (!(opt->mode & opt_types))
1136 c += printf(" --%s", opt->attr.name);
1138 switch (opt->type) {
1143 for (i = 0; opt->choices[i]; i++) {
1146 c += printf("%s", opt->choices[i]);
1151 c += printf("=%s", opt->hint);
1156 const char *l = opt->help;
1162 const char *n = strchrnul(l, '\n');
1164 while (c < helpcol) {
1168 printf("%.*s", (int) (n - l), l);
1181 dev_names bchu_fs_get_devices(struct bchfs_handle fs)
1183 DIR *dir = fdopendir(fs.sysfs_fd);
1189 while ((errno = 0), (d = readdir(dir))) {
1190 struct dev_name n = { 0, NULL, NULL };
1192 if (sscanf(d->d_name, "dev-%u", &n.idx) != 1)
1195 char *block_attr = mprintf("dev-%u/block", n.idx);
1197 char sysfs_block_buf[4096];
1198 ssize_t r = readlinkat(fs.sysfs_fd, block_attr,
1199 sysfs_block_buf, sizeof(sysfs_block_buf));
1201 sysfs_block_buf[r] = '\0';
1202 n.dev = strdup(basename(sysfs_block_buf));
1207 char *label_attr = mprintf("dev-%u/label", n.idx);
1208 n.label = read_file_str(fs.sysfs_fd, label_attr);
1211 darray_append(devs, n);