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)
43 u64 sb_pos = sb_start;
46 memset(l, 0, sizeof(*l));
48 l->magic = BCACHE_MAGIC;
50 l->nr_superblocks = 2;
51 l->sb_max_size_bits = ilog2(sb_size);
53 /* Create two superblocks in the allowed range: */
54 for (i = 0; i < l->nr_superblocks; i++) {
55 if (sb_pos != BCH_SB_SECTOR)
56 sb_pos = round_up(sb_pos, block_size);
58 l->sb_offset[i] = cpu_to_le64(sb_pos);
63 die("insufficient space for superblocks: start %llu end %llu > %llu size %u",
64 sb_start, sb_pos, sb_end, sb_size);
67 void bch2_pick_bucket_size(struct bch_opts opts, struct dev_opts *dev)
70 dev->size = get_size(dev->path, dev->fd) >> 9;
72 if (!dev->bucket_size) {
73 if (dev->size < min_size(opts.block_size))
74 die("cannot format %s, too small (%llu sectors, min %llu)",
75 dev->path, dev->size, min_size(opts.block_size));
77 /* Bucket size must be >= block size: */
78 dev->bucket_size = opts.block_size;
80 /* Bucket size must be >= btree node size: */
81 if (opt_defined(opts, btree_node_size))
82 dev->bucket_size = max_t(unsigned, dev->bucket_size,
83 opts.btree_node_size);
85 /* Want a bucket size of at least 128k, if possible: */
86 dev->bucket_size = max(dev->bucket_size, 256U);
88 if (dev->size >= min_size(dev->bucket_size)) {
89 unsigned scale = max(1,
90 ilog2(dev->size / min_size(dev->bucket_size)) / 4);
92 scale = rounddown_pow_of_two(scale);
94 /* max bucket size 1 mb */
95 dev->bucket_size = min(dev->bucket_size * scale, 1U << 11);
98 dev->bucket_size /= 2;
99 } while (dev->size < min_size(dev->bucket_size));
103 dev->nbuckets = dev->size / dev->bucket_size;
105 if (dev->bucket_size << 9 < opts.block_size)
106 die("Bucket size (%u) cannot be smaller than block size (%u)",
107 dev->bucket_size << 9, opts.block_size);
109 if (opt_defined(opts, btree_node_size) &&
110 dev->bucket_size << 9 < opts.btree_node_size)
111 die("Bucket size (%u) cannot be smaller than btree node size (%u)",
112 dev->bucket_size << 9, opts.btree_node_size);
114 if (dev->nbuckets < BCH_MIN_NR_NBUCKETS)
115 die("Not enough buckets: %llu, need %u (bucket size %u)",
116 dev->nbuckets, BCH_MIN_NR_NBUCKETS, dev->bucket_size);
120 static unsigned parse_target(struct bch_sb_handle *sb,
121 struct dev_opts *devs, size_t nr_devs,
130 for (i = devs; i < devs + nr_devs; i++)
131 if (!strcmp(s, i->path))
132 return dev_to_target(i - devs);
134 idx = bch2_disk_path_find(sb, s);
136 return group_to_target(idx);
138 die("Invalid target %s", s);
142 struct bch_sb *bch2_format(struct bch_opt_strs fs_opt_strs,
143 struct bch_opts fs_opts,
144 struct format_opts opts,
145 struct dev_opts *devs,
148 struct bch_sb_handle sb = { NULL };
150 struct bch_sb_field_members *mi;
151 unsigned max_dev_block_size = 0;
154 for (i = devs; i < devs + nr_devs; i++)
155 max_dev_block_size = max(max_dev_block_size,
156 get_blocksize(i->path, i->fd));
158 /* calculate block size: */
159 if (!opt_defined(fs_opts, block_size)) {
160 opt_set(fs_opts, block_size, max_dev_block_size);
161 } else if (fs_opts.block_size < max_dev_block_size)
162 die("blocksize too small: %u, must be greater than device blocksize %u",
163 fs_opts.block_size, max_dev_block_size);
165 /* calculate bucket sizes: */
166 for (i = devs; i < devs + nr_devs; i++)
167 bch2_pick_bucket_size(fs_opts, i);
169 /* calculate btree node size: */
170 if (!opt_defined(fs_opts, btree_node_size)) {
171 /* 256k default btree node size */
172 opt_set(fs_opts, btree_node_size, 256 << 10);
174 for (i = devs; i < devs + nr_devs; i++)
175 fs_opts.btree_node_size =
176 min_t(unsigned, fs_opts.btree_node_size,
177 i->bucket_size << 9);
180 if (uuid_is_null(opts.uuid.b))
181 uuid_generate(opts.uuid.b);
183 if (bch2_sb_realloc(&sb, 0))
184 die("insufficient memory");
186 sb.sb->version = le16_to_cpu(opts.version);
187 sb.sb->version_min = le16_to_cpu(opts.version);
188 sb.sb->magic = BCACHE_MAGIC;
189 sb.sb->user_uuid = opts.uuid;
190 sb.sb->nr_devices = nr_devs;
192 if (opts.version == bcachefs_metadata_version_current)
193 sb.sb->features[0] |= BCH_SB_FEATURES_ALL;
195 uuid_generate(sb.sb->uuid.b);
200 min(strlen(opts.label), sizeof(sb.sb->label)));
203 opt_id < bch2_opts_nr;
207 v = bch2_opt_defined_by_id(&fs_opts, opt_id)
208 ? bch2_opt_get_by_id(&fs_opts, opt_id)
209 : bch2_opt_get_by_id(&bch2_opts_default, opt_id);
211 __bch2_opt_set_sb(sb.sb, &bch2_opt_table[opt_id], v);
215 if (clock_gettime(CLOCK_REALTIME, &now))
216 die("error getting current time: %m");
218 sb.sb->time_base_lo = cpu_to_le64(now.tv_sec * NSEC_PER_SEC + now.tv_nsec);
219 sb.sb->time_precision = cpu_to_le32(1);
222 mi = bch2_sb_resize_members(&sb,
223 (sizeof(*mi) + sizeof(struct bch_member) *
224 nr_devs) / sizeof(u64));
226 for (i = devs; i < devs + nr_devs; i++) {
227 struct bch_member *m = mi->members + (i - devs);
229 uuid_generate(m->uuid.b);
230 m->nbuckets = cpu_to_le64(i->nbuckets);
232 m->bucket_size = cpu_to_le16(i->bucket_size);
234 SET_BCH_MEMBER_REPLACEMENT(m, BCH_CACHE_REPLACEMENT_lru);
235 SET_BCH_MEMBER_DISCARD(m, i->discard);
236 SET_BCH_MEMBER_DATA_ALLOWED(m, i->data_allowed);
237 SET_BCH_MEMBER_DURABILITY(m, i->durability + 1);
241 for (i = devs; i < devs + nr_devs; i++) {
242 struct bch_member *m = mi->members + (i - devs);
248 idx = bch2_disk_path_find_or_create(&sb, i->label);
250 die("error creating disk path: %s", idx);
252 SET_BCH_MEMBER_GROUP(m, idx + 1);
255 SET_BCH_SB_FOREGROUND_TARGET(sb.sb,
256 parse_target(&sb, devs, nr_devs, fs_opt_strs.foreground_target));
257 SET_BCH_SB_BACKGROUND_TARGET(sb.sb,
258 parse_target(&sb, devs, nr_devs, fs_opt_strs.background_target));
259 SET_BCH_SB_PROMOTE_TARGET(sb.sb,
260 parse_target(&sb, devs, nr_devs, fs_opt_strs.promote_target));
261 SET_BCH_SB_METADATA_TARGET(sb.sb,
262 parse_target(&sb, devs, nr_devs, fs_opt_strs.metadata_target));
265 if (opts.encrypted) {
266 struct bch_sb_field_crypt *crypt =
267 bch2_sb_resize_crypt(&sb, sizeof(*crypt) / sizeof(u64));
269 bch_sb_crypt_init(sb.sb, crypt, opts.passphrase);
270 SET_BCH_SB_ENCRYPTION_TYPE(sb.sb, 1);
273 for (i = devs; i < devs + nr_devs; i++) {
274 sb.sb->dev_idx = i - devs;
277 i->sb_offset = BCH_SB_SECTOR;
281 init_layout(&sb.sb->layout, fs_opts.block_size,
282 opts.superblock_size,
283 i->sb_offset, i->sb_end);
286 * Also create a backup superblock at the end of the disk:
288 * If we're not creating a superblock at the default offset, it
289 * means we're being run from the migrate tool and we could be
290 * overwriting existing data if we write to the end of the disk:
292 if (i->sb_offset == BCH_SB_SECTOR) {
293 struct bch_sb_layout *l = &sb.sb->layout;
294 u64 backup_sb = i->size - (1 << l->sb_max_size_bits);
296 backup_sb = rounddown(backup_sb, i->bucket_size);
297 l->sb_offset[l->nr_superblocks++] = cpu_to_le64(backup_sb);
300 if (i->sb_offset == BCH_SB_SECTOR) {
301 /* Zero start of disk */
302 static const char zeroes[BCH_SB_SECTOR << 9];
304 xpwrite(i->fd, zeroes, BCH_SB_SECTOR << 9, 0);
307 bch2_super_write(i->fd, sb.sb);
314 void bch2_super_write(int fd, struct bch_sb *sb)
316 struct nonce nonce = { 0 };
319 for (i = 0; i < sb->layout.nr_superblocks; i++) {
320 sb->offset = sb->layout.sb_offset[i];
322 if (sb->offset == BCH_SB_SECTOR) {
323 /* Write backup layout */
324 xpwrite(fd, &sb->layout, sizeof(sb->layout),
325 BCH_SB_LAYOUT_SECTOR << 9);
328 sb->csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb), nonce, sb);
329 xpwrite(fd, sb, vstruct_bytes(sb),
330 le64_to_cpu(sb->offset) << 9);
336 struct bch_sb *__bch2_super_read(int fd, u64 sector)
338 struct bch_sb sb, *ret;
340 xpread(fd, &sb, sizeof(sb), sector << 9);
342 if (memcmp(&sb.magic, &BCACHE_MAGIC, sizeof(sb.magic)))
343 die("not a bcachefs superblock");
345 size_t bytes = vstruct_bytes(&sb);
349 xpread(fd, ret, bytes, sector << 9);
354 static unsigned get_dev_has_data(struct bch_sb *sb, unsigned dev)
356 struct bch_sb_field_replicas *replicas;
357 struct bch_replicas_entry *r;
358 unsigned i, data_has = 0;
360 replicas = bch2_sb_get_replicas(sb);
363 for_each_replicas_entry(replicas, r)
364 for (i = 0; i < r->nr_devs; i++)
365 if (r->devs[i] == dev)
366 data_has |= 1 << r->data_type;
371 static int bch2_sb_get_target(struct bch_sb *sb, char *buf, size_t len, u64 v)
373 struct target t = target_decode(v);
378 return scnprintf(buf, len, "none");
380 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
381 struct bch_member *m = mi->members + t.dev;
383 if (bch2_dev_exists(sb, mi, t.dev)) {
386 uuid_unparse(m->uuid.b, uuid_str);
388 ret = scnprintf(buf, len, "Device %u (%s)", t.dev,
391 ret = scnprintf(buf, len, "Bad device %u", t.dev);
397 struct bch_sb_field_disk_groups *gi;
398 gi = bch2_sb_get_disk_groups(sb);
400 struct bch_disk_group *g = gi->entries + t.group;
402 if (t.group < disk_groups_nr(gi) && !BCH_GROUP_DELETED(g)) {
403 ret = scnprintf(buf, len, "Label %u (%.*s)", t.group,
404 BCH_SB_LABEL_SIZE, g->label);
406 ret = scnprintf(buf, len, "Bad label %u", t.group);
417 /* superblock printing: */
419 static void bch2_sb_print_layout(struct bch_sb *sb, enum units units)
421 struct bch_sb_layout *l = &sb->layout;
425 " superblock max size: %s\n"
426 " nr superblocks: %u\n"
429 pr_units(1 << l->sb_max_size_bits, units),
432 for (i = 0; i < l->nr_superblocks; i++) {
435 printf("%llu", le64_to_cpu(l->sb_offset[i]));
440 static void bch2_sb_print_journal(struct bch_sb *sb, struct bch_sb_field *f,
443 struct bch_sb_field_journal *journal = field_to_type(f, journal);
444 unsigned i, nr = bch2_nr_journal_buckets(journal);
446 printf(" Buckets: ");
447 for (i = 0; i < nr; i++) {
450 printf("%llu", le64_to_cpu(journal->buckets[i]));
455 static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
458 struct bch_sb_field_members *mi = field_to_type(f, members);
459 struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
462 for (i = 0; i < sb->nr_devices; i++) {
463 struct bch_member *m = mi->members + i;
464 time_t last_mount = le64_to_cpu(m->last_mount);
465 char member_uuid_str[40];
466 char data_allowed_str[100];
467 char data_has_str[100];
468 char label [BCH_SB_LABEL_SIZE+10];
471 if (!bch2_member_exists(m))
474 uuid_unparse(m->uuid.b, member_uuid_str);
476 if (BCH_MEMBER_GROUP(m)) {
477 unsigned idx = BCH_MEMBER_GROUP(m) - 1;
479 if (idx < disk_groups_nr(gi)) {
480 scnprintf(label, sizeof(label), "%.*s (%u)",
482 gi->entries[idx].label, idx);
484 strcpy(label, "(bad disk labels section)");
487 strcpy(label, "(none)");
490 bch2_flags_to_text(&PBUF(data_allowed_str),
492 BCH_MEMBER_DATA_ALLOWED(m));
493 if (!data_allowed_str[0])
494 strcpy(data_allowed_str, "(none)");
496 bch2_flags_to_text(&PBUF(data_has_str),
498 get_dev_has_data(sb, i));
499 if (!data_has_str[0])
500 strcpy(data_has_str, "(none)");
503 struct tm *tm = localtime(&last_mount);
504 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
506 strcpy(time_str, "(formatting error)");
508 strcpy(time_str, "(never)");
511 printf(" Device %u:\n"
515 " First bucket: %u\n"
520 " Data allowed: %s\n"
524 " Replacement policy: %s\n"
527 pr_units(le16_to_cpu(m->bucket_size) *
528 le64_to_cpu(m->nbuckets), units),
529 pr_units(le16_to_cpu(m->bucket_size), units),
530 le16_to_cpu(m->first_bucket),
531 le64_to_cpu(m->nbuckets),
534 BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
535 ? bch2_member_states[BCH_MEMBER_STATE(m)]
542 BCH_MEMBER_REPLACEMENT(m) < BCH_CACHE_REPLACEMENT_NR
543 ? bch2_cache_replacement_policies[BCH_MEMBER_REPLACEMENT(m)]
546 BCH_MEMBER_DISCARD(m));
550 static void bch2_sb_print_crypt(struct bch_sb *sb, struct bch_sb_field *f,
553 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
555 printf(" KFD: %llu\n"
559 BCH_CRYPT_KDF_TYPE(crypt),
560 BCH_KDF_SCRYPT_N(crypt),
561 BCH_KDF_SCRYPT_R(crypt),
562 BCH_KDF_SCRYPT_P(crypt));
565 static void bch2_sb_print_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f,
568 struct bch_sb_field_replicas_v0 *replicas = field_to_type(f, replicas_v0);
569 struct bch_replicas_entry_v0 *e;
572 for_each_replicas_entry(replicas, e) {
573 printf_pad(32, " %s:", bch2_data_types[e->data_type]);
576 for (i = 0; i < e->nr_devs; i++) {
579 printf("%u", e->devs[i]);
585 static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
588 struct bch_sb_field_replicas *replicas = field_to_type(f, replicas);
589 struct bch_replicas_entry *e;
592 for_each_replicas_entry(replicas, e) {
593 printf_pad(32, " %s: %u/%u",
594 bch2_data_types[e->data_type],
599 for (i = 0; i < e->nr_devs; i++) {
602 printf("%u", e->devs[i]);
608 static void bch2_sb_print_quota(struct bch_sb *sb, struct bch_sb_field *f,
613 static void bch2_sb_print_disk_groups(struct bch_sb *sb, struct bch_sb_field *f,
618 static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
621 struct bch_sb_field_clean *clean = field_to_type(f, clean);
624 printf(" flags: %x", le32_to_cpu(clean->flags));
625 printf(" journal seq: %llx", le64_to_cpu(clean->journal_seq));
628 static void bch2_sb_print_journal_seq_blacklist(struct bch_sb *sb, struct bch_sb_field *f,
631 struct bch_sb_field_journal_seq_blacklist *bl = field_to_type(f, journal_seq_blacklist);
632 unsigned i, nr = blacklist_nr_entries(bl);
634 for (i = 0; i < nr; i++) {
635 struct journal_seq_blacklist_entry *e =
638 printf(" %llu-%llu\n",
639 le64_to_cpu(e->start),
640 le64_to_cpu(e->end));
644 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
646 struct bch_sb_field_toolops {
647 sb_field_print_fn print;
650 static const struct bch_sb_field_toolops bch2_sb_field_ops[] = {
652 [BCH_SB_FIELD_##f] = { \
653 .print = bch2_sb_print_##f, \
659 static inline void bch2_sb_field_print(struct bch_sb *sb,
660 struct bch_sb_field *f,
663 unsigned type = le32_to_cpu(f->type);
665 if (type < BCH_SB_FIELD_NR)
666 bch2_sb_field_ops[type].print(sb, f, units);
668 printf("(unknown field %u)\n", type);
671 void bch2_sb_print(struct bch_sb *sb, bool print_layout,
672 unsigned fields, enum units units)
674 struct bch_sb_field_members *mi;
675 char user_uuid_str[40], internal_uuid_str[40];
676 char features_str[500];
677 char compat_features_str[500];
678 char fields_have_str[200];
679 char label[BCH_SB_LABEL_SIZE + 1];
681 char foreground_str[64];
682 char background_str[64];
683 char promote_str[64];
684 char metadata_str[64];
685 struct bch_sb_field *f;
687 unsigned nr_devices = 0;
688 time_t time_base = le64_to_cpu(sb->time_base_lo) / NSEC_PER_SEC;
690 memcpy(label, sb->label, BCH_SB_LABEL_SIZE);
691 label[BCH_SB_LABEL_SIZE] = '\0';
693 uuid_unparse(sb->user_uuid.b, user_uuid_str);
694 uuid_unparse(sb->uuid.b, internal_uuid_str);
697 struct tm *tm = localtime(&time_base);
698 size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
700 strcpy(time_str, "(formatting error)");
702 strcpy(time_str, "(not set)");
705 mi = bch2_sb_get_members(sb);
707 struct bch_member *m;
709 for (m = mi->members;
710 m < mi->members + sb->nr_devices;
712 nr_devices += bch2_member_exists(m);
715 bch2_sb_get_target(sb, foreground_str, sizeof(foreground_str),
716 BCH_SB_FOREGROUND_TARGET(sb));
718 bch2_sb_get_target(sb, background_str, sizeof(background_str),
719 BCH_SB_BACKGROUND_TARGET(sb));
721 bch2_sb_get_target(sb, promote_str, sizeof(promote_str),
722 BCH_SB_PROMOTE_TARGET(sb));
724 bch2_sb_get_target(sb, metadata_str, sizeof(metadata_str),
725 BCH_SB_METADATA_TARGET(sb));
727 bch2_flags_to_text(&PBUF(features_str),
729 le64_to_cpu(sb->features[0]));
731 bch2_flags_to_text(&PBUF(compat_features_str),
733 le64_to_cpu(sb->compat[0]));
735 vstruct_for_each(sb, f)
736 fields_have |= 1 << le32_to_cpu(f->type);
737 bch2_flags_to_text(&PBUF(fields_have_str),
738 bch2_sb_fields, fields_have);
740 printf("External UUID: %s\n"
741 "Internal UUID: %s\n"
745 "Oldest version on disk: %u\n"
747 "Squence number: %llu\n"
749 "Btree node size: %s\n"
753 "Compat features: %s\n"
755 "Metadata replicas: %llu\n"
756 "Data replicas: %llu\n"
758 "Metadata checksum type: %s (%llu)\n"
759 "Data checksum type: %s (%llu)\n"
760 "Compression type: %s (%llu)\n"
762 "Foreground write target: %s\n"
763 "Background write target: %s\n"
764 "Promote target: %s\n"
765 "Metadata target: %s\n"
767 "String hash type: %s (%llu)\n"
768 "32 bit inodes: %llu\n"
769 "GC reserve percentage: %llu%%\n"
770 "Root reserve percentage: %llu%%\n"
772 "Devices: %u live, %u total\n"
774 "Superblock size: %llu\n",
779 le16_to_cpu(sb->version),
780 le16_to_cpu(sb->version_min),
782 le64_to_cpu(sb->seq),
783 pr_units(le16_to_cpu(sb->block_size), units),
784 pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
786 BCH_SB_ERROR_ACTION(sb) < BCH_ON_ERROR_NR
787 ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
794 BCH_SB_META_REPLICAS_WANT(sb),
795 BCH_SB_DATA_REPLICAS_WANT(sb),
797 BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
798 ? bch2_csum_opts[BCH_SB_META_CSUM_TYPE(sb)]
800 BCH_SB_META_CSUM_TYPE(sb),
802 BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
803 ? bch2_csum_opts[BCH_SB_DATA_CSUM_TYPE(sb)]
805 BCH_SB_DATA_CSUM_TYPE(sb),
807 BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
808 ? bch2_compression_opts[BCH_SB_COMPRESSION_TYPE(sb)]
810 BCH_SB_COMPRESSION_TYPE(sb),
817 BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
818 ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
820 BCH_SB_STR_HASH_TYPE(sb),
822 BCH_SB_INODE_32BIT(sb),
823 BCH_SB_GC_RESERVE(sb),
824 BCH_SB_ROOT_RESERVE(sb),
826 nr_devices, sb->nr_devices,
833 bch2_sb_print_layout(sb, units);
836 vstruct_for_each(sb, f) {
837 unsigned type = le32_to_cpu(f->type);
840 if (!(fields & (1 << type)))
843 if (type < BCH_SB_FIELD_NR) {
844 scnprintf(name, sizeof(name), "%s", bch2_sb_fields[type]);
845 name[0] = toupper(name[0]);
847 scnprintf(name, sizeof(name), "(unknown field %u)", type);
850 printf("\n%s (size %llu):\n", name, vstruct_bytes(f));
851 if (type < BCH_SB_FIELD_NR)
852 bch2_sb_field_print(sb, f, units);
856 /* ioctl interface: */
858 /* Global control device: */
859 int bcachectl_open(void)
861 return xopen("/dev/bcachefs-ctl", O_RDWR);
864 /* Filesystem handles (ioctl, sysfs dir): */
866 #define SYSFS_BASE "/sys/fs/bcachefs/"
868 void bcache_fs_close(struct bchfs_handle fs)
874 struct bchfs_handle bcache_fs_open(const char *path)
876 struct bchfs_handle ret;
878 if (!uuid_parse(path, ret.uuid.b)) {
879 /* It's a UUID, look it up in sysfs: */
880 char *sysfs = mprintf(SYSFS_BASE "%s", path);
881 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
883 char *minor = read_file_str(ret.sysfs_fd, "minor");
884 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
885 ret.ioctl_fd = xopen(ctl, O_RDWR);
892 ret.ioctl_fd = open(path, O_RDONLY);
893 if (ret.ioctl_fd < 0)
894 die("Error opening filesystem at %s: %m", path);
896 struct bch_ioctl_query_uuid uuid;
897 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
898 die("error opening %s: not a bcachefs filesystem", path);
900 ret.uuid = uuid.uuid;
903 uuid_unparse(uuid.uuid.b, uuid_str);
905 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
906 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
914 * Given a path to a block device, open the filesystem it belongs to; also
915 * return the device's idx:
917 struct bchfs_handle bchu_fs_open_by_dev(const char *path, int *idx)
919 char buf[1024], *uuid_str;
921 struct stat stat = xstat(path);
923 if (!S_ISBLK(stat.st_mode))
924 die("%s is not a block device", path);
926 char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
929 ssize_t len = readlink(sysfs, buf, sizeof(buf));
933 char *p = strrchr(buf, '/');
934 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
935 die("error parsing sysfs");
938 p = strrchr(buf, '/');
941 struct bch_opts opts = bch2_opts_empty();
943 opt_set(opts, noexcl, true);
944 opt_set(opts, nochanges, true);
946 struct bch_sb_handle sb;
947 int ret = bch2_read_super(path, &opts, &sb);
949 die("Error opening %s: %s", path, strerror(-ret));
951 *idx = sb.sb->dev_idx;
953 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
955 bch2_free_super(&sb);
958 return bcache_fs_open(uuid_str);
961 int bchu_dev_path_to_idx(struct bchfs_handle fs, const char *dev_path)
964 struct bchfs_handle fs2 = bchu_fs_open_by_dev(dev_path, &idx);
966 if (memcmp(&fs.uuid, &fs2.uuid, sizeof(fs.uuid)))
968 bcache_fs_close(fs2);
972 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
974 int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
977 struct bch_ioctl_data_event e;
979 if (read(progress_fd, &e, sizeof(e)) != sizeof(e))
980 die("error reading from progress fd %m");
985 if (e.p.data_type == U8_MAX)
990 printf("%llu%% complete: current position %s",
992 ? e.p.sectors_done * 100 / e.p.sectors_total
994 bch2_data_types[e.p.data_type]);
996 switch (e.p.data_type) {
999 printf(" %s:%llu:%llu",
1000 bch2_btree_ids[e.p.btree_id],
1014 /* option parsing */
1016 void bch2_opt_strs_free(struct bch_opt_strs *opts)
1020 for (i = 0; i < bch2_opts_nr; i++) {
1021 free(opts->by_id[i]);
1022 opts->by_id[i] = NULL;
1026 struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
1029 struct bch_opt_strs opts;
1032 memset(&opts, 0, sizeof(opts));
1035 char *optstr = strcmp_prefix(argv[i], "--");
1036 char *valstr = NULL, *p;
1037 int optid, nr_args = 1;
1044 optstr = strdup(optstr);
1047 while (isalpha(*p) || *p == '_')
1055 optid = bch2_opt_lookup(optstr);
1057 !(bch2_opt_table[optid].flags & opt_types)) {
1063 bch2_opt_table[optid].type != BCH_OPT_BOOL) {
1065 valstr = argv[i + 1];
1071 opts.by_id[optid] = strdup(valstr);
1076 sizeof(char *) * (*argc - i));
1085 struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
1087 struct bch_opts opts = bch2_opts_empty();
1092 for (i = 0; i < bch2_opts_nr; i++) {
1093 if (!strs.by_id[i] ||
1094 bch2_opt_table[i].type == BCH_OPT_FN)
1097 ret = bch2_opt_parse(NULL, "option",
1101 die("Invalid %s: %s",
1102 bch2_opt_table[i].attr.name,
1105 bch2_opt_set_by_id(&opts, i, v);
1111 #define newline(c) \
1116 void bch2_opts_usage(unsigned opt_types)
1118 const struct bch_option *opt;
1119 unsigned i, c = 0, helpcol = 30;
1123 for (opt = bch2_opt_table;
1124 opt < bch2_opt_table + bch2_opts_nr;
1126 if (!(opt->flags & opt_types))
1129 c += printf(" --%s", opt->attr.name);
1131 switch (opt->type) {
1136 for (i = 0; opt->choices[i]; i++) {
1139 c += printf("%s", opt->choices[i]);
1144 c += printf("=%s", opt->hint);
1149 const char *l = opt->help;
1155 const char *n = strchrnul(l, '\n');
1157 while (c < helpcol) {
1161 printf("%.*s", (int) (n - l), l);
1174 dev_names bchu_fs_get_devices(struct bchfs_handle fs)
1176 DIR *dir = fdopendir(fs.sysfs_fd);
1182 while ((errno = 0), (d = readdir(dir))) {
1183 struct dev_name n = { 0, NULL, NULL };
1185 if (sscanf(d->d_name, "dev-%u", &n.idx) != 1)
1188 char *block_attr = mprintf("dev-%u/block", n.idx);
1190 char sysfs_block_buf[4096];
1191 ssize_t r = readlinkat(fs.sysfs_fd, block_attr,
1192 sysfs_block_buf, sizeof(sysfs_block_buf));
1194 sysfs_block_buf[r] = '\0';
1195 n.dev = strdup(basename(sysfs_block_buf));
1200 char *label_attr = mprintf("dev-%u/label", n.idx);
1201 n.label = read_file_str(fs.sysfs_fd, label_attr);
1204 darray_append(devs, n);