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,
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 (start != BCH_SB_SECTOR)
55 start = round_up(start, block_size);
57 l->sb_offset[i] = cpu_to_le64(start);
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);
295 if (i->sb_offset == BCH_SB_SECTOR) {
296 /* Zero start of disk */
297 static const char zeroes[BCH_SB_SECTOR << 9];
299 xpwrite(i->fd, zeroes, BCH_SB_SECTOR << 9, 0);
302 bch2_super_write(i->fd, sb.sb);
309 void bch2_super_write(int fd, struct bch_sb *sb)
311 struct nonce nonce = { 0 };
314 for (i = 0; i < sb->layout.nr_superblocks; i++) {
315 sb->offset = sb->layout.sb_offset[i];
317 if (sb->offset == BCH_SB_SECTOR) {
318 /* Write backup layout */
319 xpwrite(fd, &sb->layout, sizeof(sb->layout),
320 BCH_SB_LAYOUT_SECTOR << 9);
323 sb->csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb), nonce, sb);
324 xpwrite(fd, sb, vstruct_bytes(sb),
325 le64_to_cpu(sb->offset) << 9);
331 struct bch_sb *__bch2_super_read(int fd, u64 sector)
333 struct bch_sb sb, *ret;
335 xpread(fd, &sb, sizeof(sb), sector << 9);
337 if (memcmp(&sb.magic, &BCACHE_MAGIC, sizeof(sb.magic)))
338 die("not a bcachefs superblock");
340 size_t bytes = vstruct_bytes(&sb);
344 xpread(fd, ret, bytes, sector << 9);
349 static unsigned get_dev_has_data(struct bch_sb *sb, unsigned dev)
351 struct bch_sb_field_replicas *replicas;
352 struct bch_replicas_entry *r;
353 unsigned i, data_has = 0;
355 replicas = bch2_sb_get_replicas(sb);
358 for_each_replicas_entry(replicas, r)
359 for (i = 0; i < r->nr_devs; i++)
360 if (r->devs[i] == dev)
361 data_has |= 1 << r->data_type;
366 static int bch2_sb_get_target(struct bch_sb *sb, char *buf, size_t len, u64 v)
368 struct target t = target_decode(v);
373 return scnprintf(buf, len, "none");
375 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
376 struct bch_member *m = mi->members + t.dev;
378 if (bch2_dev_exists(sb, mi, t.dev)) {
381 uuid_unparse(m->uuid.b, uuid_str);
383 ret = scnprintf(buf, len, "Device %u (%s)", t.dev,
386 ret = scnprintf(buf, len, "Bad device %u", t.dev);
392 struct bch_sb_field_disk_groups *gi;
393 gi = bch2_sb_get_disk_groups(sb);
395 struct bch_disk_group *g = gi->entries + t.group;
397 if (t.group < disk_groups_nr(gi) && !BCH_GROUP_DELETED(g)) {
398 ret = scnprintf(buf, len, "Group %u (%.*s)", t.group,
399 BCH_SB_LABEL_SIZE, g->label);
401 ret = scnprintf(buf, len, "Bad group %u", t.group);
412 /* superblock printing: */
414 static void bch2_sb_print_layout(struct bch_sb *sb, enum units units)
416 struct bch_sb_layout *l = &sb->layout;
420 " superblock max size: %s\n"
421 " nr superblocks: %u\n"
424 pr_units(1 << l->sb_max_size_bits, units),
427 for (i = 0; i < l->nr_superblocks; i++) {
430 printf("%llu", le64_to_cpu(l->sb_offset[i]));
435 static void bch2_sb_print_journal(struct bch_sb *sb, struct bch_sb_field *f,
438 struct bch_sb_field_journal *journal = field_to_type(f, journal);
439 unsigned i, nr = bch2_nr_journal_buckets(journal);
441 printf(" Buckets: ");
442 for (i = 0; i < nr; i++) {
445 printf("%llu", le64_to_cpu(journal->buckets[i]));
450 static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
453 struct bch_sb_field_members *mi = field_to_type(f, members);
454 struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
457 for (i = 0; i < sb->nr_devices; i++) {
458 struct bch_member *m = mi->members + i;
459 time_t last_mount = le64_to_cpu(m->last_mount);
460 char member_uuid_str[40];
461 char data_allowed_str[100];
462 char data_has_str[100];
463 char group[BCH_SB_LABEL_SIZE+10];
466 if (!bch2_member_exists(m))
469 uuid_unparse(m->uuid.b, member_uuid_str);
471 if (BCH_MEMBER_GROUP(m)) {
472 unsigned idx = BCH_MEMBER_GROUP(m) - 1;
474 if (idx < disk_groups_nr(gi)) {
475 snprintf(group, sizeof(group), "%.*s (%u)",
477 gi->entries[idx].label, idx);
479 strcpy(group, "(bad disk groups section)");
482 strcpy(group, "(none)");
485 bch2_flags_to_text(&PBUF(data_allowed_str),
487 BCH_MEMBER_DATA_ALLOWED(m));
488 if (!data_allowed_str[0])
489 strcpy(data_allowed_str, "(none)");
491 bch2_flags_to_text(&PBUF(data_has_str),
493 get_dev_has_data(sb, i));
494 if (!data_has_str[0])
495 strcpy(data_has_str, "(none)");
498 struct tm *tm = localtime(&last_mount);
499 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
501 strcpy(time_str, "(formatting error)");
503 strcpy(time_str, "(never)");
506 printf(" Device %u:\n"
510 " First bucket: %u\n"
515 " Data allowed: %s\n"
519 " Replacement policy: %s\n"
522 pr_units(le16_to_cpu(m->bucket_size) *
523 le64_to_cpu(m->nbuckets), units),
524 pr_units(le16_to_cpu(m->bucket_size), units),
525 le16_to_cpu(m->first_bucket),
526 le64_to_cpu(m->nbuckets),
529 BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
530 ? bch2_member_states[BCH_MEMBER_STATE(m)]
537 BCH_MEMBER_REPLACEMENT(m) < BCH_CACHE_REPLACEMENT_NR
538 ? bch2_cache_replacement_policies[BCH_MEMBER_REPLACEMENT(m)]
541 BCH_MEMBER_DISCARD(m));
545 static void bch2_sb_print_crypt(struct bch_sb *sb, struct bch_sb_field *f,
548 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
550 printf(" KFD: %llu\n"
554 BCH_CRYPT_KDF_TYPE(crypt),
555 BCH_KDF_SCRYPT_N(crypt),
556 BCH_KDF_SCRYPT_R(crypt),
557 BCH_KDF_SCRYPT_P(crypt));
560 static void bch2_sb_print_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f,
563 struct bch_sb_field_replicas_v0 *replicas = field_to_type(f, replicas_v0);
564 struct bch_replicas_entry_v0 *e;
567 for_each_replicas_entry(replicas, e) {
568 printf_pad(32, " %s:", bch2_data_types[e->data_type]);
571 for (i = 0; i < e->nr_devs; i++) {
574 printf("%u", e->devs[i]);
580 static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
583 struct bch_sb_field_replicas *replicas = field_to_type(f, replicas);
584 struct bch_replicas_entry *e;
587 for_each_replicas_entry(replicas, e) {
588 printf_pad(32, " %s: %u/%u",
589 bch2_data_types[e->data_type],
594 for (i = 0; i < e->nr_devs; i++) {
597 printf("%u", e->devs[i]);
603 static void bch2_sb_print_quota(struct bch_sb *sb, struct bch_sb_field *f,
608 static void bch2_sb_print_disk_groups(struct bch_sb *sb, struct bch_sb_field *f,
613 static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
616 struct bch_sb_field_clean *clean = field_to_type(f, clean);
619 printf(" flags: %x", le32_to_cpu(clean->flags));
620 printf(" journal seq: %llx", le64_to_cpu(clean->journal_seq));
623 static void bch2_sb_print_journal_seq_blacklist(struct bch_sb *sb, struct bch_sb_field *f,
626 struct bch_sb_field_journal_seq_blacklist *bl = field_to_type(f, journal_seq_blacklist);
627 unsigned i, nr = blacklist_nr_entries(bl);
629 for (i = 0; i < nr; i++) {
630 struct journal_seq_blacklist_entry *e =
633 printf(" %llu-%llu\n",
634 le64_to_cpu(e->start),
635 le64_to_cpu(e->end));
639 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
641 struct bch_sb_field_toolops {
642 sb_field_print_fn print;
645 static const struct bch_sb_field_toolops bch2_sb_field_ops[] = {
647 [BCH_SB_FIELD_##f] = { \
648 .print = bch2_sb_print_##f, \
654 static inline void bch2_sb_field_print(struct bch_sb *sb,
655 struct bch_sb_field *f,
658 unsigned type = le32_to_cpu(f->type);
660 if (type < BCH_SB_FIELD_NR)
661 bch2_sb_field_ops[type].print(sb, f, units);
663 printf("(unknown field %u)\n", type);
666 void bch2_sb_print(struct bch_sb *sb, bool print_layout,
667 unsigned fields, enum units units)
669 struct bch_sb_field_members *mi;
670 char user_uuid_str[40], internal_uuid_str[40];
671 char features_str[500];
672 char compat_features_str[500];
673 char fields_have_str[200];
674 char label[BCH_SB_LABEL_SIZE + 1];
676 char foreground_str[64];
677 char background_str[64];
678 char promote_str[64];
679 char metadata_str[64];
680 struct bch_sb_field *f;
682 unsigned nr_devices = 0;
683 time_t time_base = le64_to_cpu(sb->time_base_lo) / NSEC_PER_SEC;
685 memcpy(label, sb->label, BCH_SB_LABEL_SIZE);
686 label[BCH_SB_LABEL_SIZE] = '\0';
688 uuid_unparse(sb->user_uuid.b, user_uuid_str);
689 uuid_unparse(sb->uuid.b, internal_uuid_str);
692 struct tm *tm = localtime(&time_base);
693 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
695 strcpy(time_str, "(formatting error)");
697 strcpy(time_str, "(not set)");
700 mi = bch2_sb_get_members(sb);
702 struct bch_member *m;
704 for (m = mi->members;
705 m < mi->members + sb->nr_devices;
707 nr_devices += bch2_member_exists(m);
710 bch2_sb_get_target(sb, foreground_str, sizeof(foreground_str),
711 BCH_SB_FOREGROUND_TARGET(sb));
713 bch2_sb_get_target(sb, background_str, sizeof(background_str),
714 BCH_SB_BACKGROUND_TARGET(sb));
716 bch2_sb_get_target(sb, promote_str, sizeof(promote_str),
717 BCH_SB_PROMOTE_TARGET(sb));
719 bch2_sb_get_target(sb, metadata_str, sizeof(metadata_str),
720 BCH_SB_METADATA_TARGET(sb));
722 bch2_flags_to_text(&PBUF(features_str),
724 le64_to_cpu(sb->features[0]));
726 bch2_flags_to_text(&PBUF(compat_features_str),
728 le64_to_cpu(sb->compat[0]));
730 vstruct_for_each(sb, f)
731 fields_have |= 1 << le32_to_cpu(f->type);
732 bch2_flags_to_text(&PBUF(fields_have_str),
733 bch2_sb_fields, fields_have);
735 printf("External UUID: %s\n"
736 "Internal UUID: %s\n"
740 "Oldest version on disk: %u\n"
742 "Squence number: %llu\n"
744 "Btree node size: %s\n"
748 "Compat features: %s\n"
750 "Metadata replicas: %llu\n"
751 "Data replicas: %llu\n"
753 "Metadata checksum type: %s (%llu)\n"
754 "Data checksum type: %s (%llu)\n"
755 "Compression type: %s (%llu)\n"
757 "Foreground write target: %s\n"
758 "Background write target: %s\n"
759 "Promote target: %s\n"
760 "Metadata target: %s\n"
762 "String hash type: %s (%llu)\n"
763 "32 bit inodes: %llu\n"
764 "GC reserve percentage: %llu%%\n"
765 "Root reserve percentage: %llu%%\n"
767 "Devices: %u live, %u total\n"
769 "Superblock size: %llu\n",
774 le16_to_cpu(sb->version),
775 le16_to_cpu(sb->version_min),
777 le64_to_cpu(sb->seq),
778 pr_units(le16_to_cpu(sb->block_size), units),
779 pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
781 BCH_SB_ERROR_ACTION(sb) < BCH_ON_ERROR_NR
782 ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
789 BCH_SB_META_REPLICAS_WANT(sb),
790 BCH_SB_DATA_REPLICAS_WANT(sb),
792 BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
793 ? bch2_csum_opts[BCH_SB_META_CSUM_TYPE(sb)]
795 BCH_SB_META_CSUM_TYPE(sb),
797 BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
798 ? bch2_csum_opts[BCH_SB_DATA_CSUM_TYPE(sb)]
800 BCH_SB_DATA_CSUM_TYPE(sb),
802 BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
803 ? bch2_compression_opts[BCH_SB_COMPRESSION_TYPE(sb)]
805 BCH_SB_COMPRESSION_TYPE(sb),
812 BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
813 ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
815 BCH_SB_STR_HASH_TYPE(sb),
817 BCH_SB_INODE_32BIT(sb),
818 BCH_SB_GC_RESERVE(sb),
819 BCH_SB_ROOT_RESERVE(sb),
821 nr_devices, sb->nr_devices,
828 bch2_sb_print_layout(sb, units);
831 vstruct_for_each(sb, f) {
832 unsigned type = le32_to_cpu(f->type);
835 if (!(fields & (1 << type)))
838 if (type < BCH_SB_FIELD_NR) {
839 scnprintf(name, sizeof(name), "%s", bch2_sb_fields[type]);
840 name[0] = toupper(name[0]);
842 scnprintf(name, sizeof(name), "(unknown field %u)", type);
845 printf("\n%s (size %llu):\n", name, vstruct_bytes(f));
846 if (type < BCH_SB_FIELD_NR)
847 bch2_sb_field_print(sb, f, units);
851 /* ioctl interface: */
853 /* Global control device: */
854 int bcachectl_open(void)
856 return xopen("/dev/bcachefs-ctl", O_RDWR);
859 /* Filesystem handles (ioctl, sysfs dir): */
861 #define SYSFS_BASE "/sys/fs/bcachefs/"
863 void bcache_fs_close(struct bchfs_handle fs)
869 struct bchfs_handle bcache_fs_open(const char *path)
871 struct bchfs_handle ret;
873 if (!uuid_parse(path, ret.uuid.b)) {
874 /* It's a UUID, look it up in sysfs: */
875 char *sysfs = mprintf(SYSFS_BASE "%s", path);
876 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
878 char *minor = read_file_str(ret.sysfs_fd, "minor");
879 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
880 ret.ioctl_fd = xopen(ctl, O_RDWR);
887 ret.ioctl_fd = xopen(path, O_RDONLY);
889 struct bch_ioctl_query_uuid uuid;
890 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
891 die("error opening %s: not a bcachefs filesystem", path);
893 ret.uuid = uuid.uuid;
896 uuid_unparse(uuid.uuid.b, uuid_str);
898 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
899 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
907 * Given a path to a block device, open the filesystem it belongs to; also
908 * return the device's idx:
910 struct bchfs_handle bchu_fs_open_by_dev(const char *path, int *idx)
912 char buf[1024], *uuid_str;
914 struct stat stat = xstat(path);
916 if (!S_ISBLK(stat.st_mode))
917 die("%s is not a block device", path);
919 char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
922 ssize_t len = readlink(sysfs, buf, sizeof(buf));
926 char *p = strrchr(buf, '/');
927 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
928 die("error parsing sysfs");
931 p = strrchr(buf, '/');
934 struct bch_opts opts = bch2_opts_empty();
936 opt_set(opts, noexcl, true);
937 opt_set(opts, nochanges, true);
939 struct bch_sb_handle sb;
940 int ret = bch2_read_super(path, &opts, &sb);
942 die("Error opening %s: %s", path, strerror(-ret));
944 *idx = sb.sb->dev_idx;
946 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
948 bch2_free_super(&sb);
951 return bcache_fs_open(uuid_str);
954 int bchu_dev_path_to_idx(struct bchfs_handle fs, const char *dev_path)
957 struct bchfs_handle fs2 = bchu_fs_open_by_dev(dev_path, &idx);
959 if (memcmp(&fs.uuid, &fs2.uuid, sizeof(fs.uuid)))
961 bcache_fs_close(fs2);
965 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
967 int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
970 struct bch_ioctl_data_event e;
972 if (read(progress_fd, &e, sizeof(e)) != sizeof(e))
973 die("error reading from progress fd %m");
978 if (e.p.data_type == U8_MAX)
983 printf("%llu%% complete: current position %s",
985 ? e.p.sectors_done * 100 / e.p.sectors_total
987 bch2_data_types[e.p.data_type]);
989 switch (e.p.data_type) {
992 printf(" %s:%llu:%llu",
993 bch2_btree_ids[e.p.btree_id],
1007 /* option parsing */
1009 struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
1012 struct bch_opt_strs opts;
1015 memset(&opts, 0, sizeof(opts));
1018 char *optstr = strcmp_prefix(argv[i], "--");
1019 char *valstr = NULL, *p;
1020 int optid, nr_args = 1;
1027 optstr = strdup(optstr);
1030 while (isalpha(*p) || *p == '_')
1038 optid = bch2_opt_lookup(optstr);
1040 !(bch2_opt_table[optid].mode & opt_types)) {
1047 bch2_opt_table[optid].type != BCH_OPT_BOOL) {
1049 valstr = argv[i + 1];
1055 opts.by_id[optid] = valstr;
1060 sizeof(char *) * (*argc - i));
1067 struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
1069 struct bch_opts opts = bch2_opts_empty();
1074 for (i = 0; i < bch2_opts_nr; i++) {
1075 if (!strs.by_id[i] ||
1076 bch2_opt_table[i].type == BCH_OPT_FN)
1079 ret = bch2_opt_parse(NULL, &bch2_opt_table[i],
1082 die("Invalid %s: %s",
1083 bch2_opt_table[i].attr.name,
1086 bch2_opt_set_by_id(&opts, i, v);
1092 void bch2_opts_usage(unsigned opt_types)
1094 const struct bch_option *opt;
1095 unsigned i, c = 0, helpcol = 30;
1098 while (c < helpcol) {
1109 for (opt = bch2_opt_table;
1110 opt < bch2_opt_table + bch2_opts_nr;
1112 if (!(opt->mode & opt_types))
1115 c += printf(" --%s", opt->attr.name);
1117 switch (opt->type) {
1122 for (i = 0; opt->choices[i]; i++) {
1125 c += printf("%s", opt->choices[i]);
1130 c += printf("=%s", opt->hint);
1135 const char *l = opt->help;
1141 const char *n = strchrnul(l, '\n');
1144 printf("%.*s", (int) (n - l), l);
1157 dev_names bchu_fs_get_devices(struct bchfs_handle fs)
1159 DIR *dir = fdopendir(fs.sysfs_fd);
1165 while ((errno = 0), (d = readdir(dir))) {
1166 struct dev_name n = { 0, NULL, NULL };
1168 if (sscanf(d->d_name, "dev-%u", &n.idx) != 1)
1171 char *block_attr = mprintf("dev-%u/block", n.idx);
1173 char sysfs_block_buf[4096];
1174 ssize_t r = readlinkat(fs.sysfs_fd, block_attr,
1175 sysfs_block_buf, sizeof(sysfs_block_buf));
1177 sysfs_block_buf[r] = '\0';
1178 n.dev = strdup(basename(sysfs_block_buf));
1183 char *label_attr = mprintf("dev-%u/label", n.idx);
1184 n.label = read_file_str(fs.sysfs_fd, label_attr);
1187 darray_append(devs, n);