10 #include <sys/sysmacros.h>
11 #include <sys/types.h>
15 #include <uuid/uuid.h>
17 #include "libbcachefs.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"
27 #define NSEC_PER_SEC 1000000000L
29 #define BCH_MIN_NR_NBUCKETS (1 << 10)
31 /* minimum size filesystem we can create, given a bucket size: */
32 static u64 min_size(unsigned bucket_size)
34 return BCH_MIN_NR_NBUCKETS * bucket_size;
37 static void init_layout(struct bch_sb_layout *l, unsigned block_size,
41 u64 backup; /* offset of 2nd sb */
43 memset(l, 0, sizeof(*l));
45 if (start != BCH_SB_SECTOR)
46 start = round_up(start, block_size);
47 end = round_down(end, block_size);
50 die("insufficient space for superblocks");
53 * Create two superblocks in the allowed range: reserve a maximum of 64k
55 sb_size = min_t(u64, 128, end - start / 2);
57 backup = start + sb_size;
58 backup = round_up(backup, block_size);
60 backup = min(backup, end);
62 sb_size = min(end - backup, backup- start);
63 sb_size = rounddown_pow_of_two(sb_size);
66 die("insufficient space for superblocks");
68 l->magic = BCACHE_MAGIC;
70 l->nr_superblocks = 2;
71 l->sb_max_size_bits = ilog2(sb_size);
72 l->sb_offset[0] = cpu_to_le64(start);
73 l->sb_offset[1] = cpu_to_le64(backup);
76 void bch2_pick_bucket_size(struct format_opts opts, struct dev_opts *dev)
78 if (!dev->sb_offset) {
79 dev->sb_offset = BCH_SB_SECTOR;
80 dev->sb_end = BCH_SB_SECTOR + 256;
84 dev->size = get_size(dev->path, dev->fd) >> 9;
86 if (!dev->bucket_size) {
87 if (dev->size < min_size(opts.block_size))
88 die("cannot format %s, too small (%llu sectors, min %llu)",
89 dev->path, dev->size, min_size(opts.block_size));
91 /* Bucket size must be >= block size: */
92 dev->bucket_size = opts.block_size;
94 /* Bucket size must be >= btree node size: */
95 dev->bucket_size = max(dev->bucket_size, opts.btree_node_size);
97 /* Want a bucket size of at least 128k, if possible: */
98 dev->bucket_size = max(dev->bucket_size, 256U);
100 if (dev->size >= min_size(dev->bucket_size)) {
101 unsigned scale = max(1,
102 ilog2(dev->size / min_size(dev->bucket_size)) / 4);
104 scale = rounddown_pow_of_two(scale);
106 /* max bucket size 1 mb */
107 dev->bucket_size = min(dev->bucket_size * scale, 1U << 11);
110 dev->bucket_size /= 2;
111 } while (dev->size < min_size(dev->bucket_size));
115 dev->nbuckets = dev->size / dev->bucket_size;
117 if (dev->bucket_size < opts.block_size)
118 die("Bucket size cannot be smaller than block size");
120 if (dev->bucket_size < opts.btree_node_size)
121 die("Bucket size cannot be smaller than btree node size");
123 if (dev->nbuckets < BCH_MIN_NR_NBUCKETS)
124 die("Not enough buckets: %llu, need %u (bucket size %u)",
125 dev->nbuckets, BCH_MIN_NR_NBUCKETS, dev->bucket_size);
129 static unsigned parse_target(struct bch_sb_handle *sb,
130 struct dev_opts *devs, size_t nr_devs,
139 for (i = devs; i < devs + nr_devs; i++)
140 if (!strcmp(s, i->path))
141 return dev_to_target(i - devs);
143 idx = bch2_disk_path_find(sb, s);
145 return group_to_target(idx);
147 die("Invalid target %s", s);
151 struct bch_sb *bch2_format(struct format_opts opts,
152 struct dev_opts *devs, size_t nr_devs)
154 struct bch_sb_handle sb = { NULL };
156 struct bch_sb_field_members *mi;
158 /* calculate block size: */
159 if (!opts.block_size)
160 for (i = devs; i < devs + nr_devs; i++)
161 opts.block_size = max(opts.block_size,
162 get_blocksize(i->path, i->fd));
164 /* calculate bucket sizes: */
165 for (i = devs; i < devs + nr_devs; i++)
166 bch2_pick_bucket_size(opts, i);
168 /* calculate btree node size: */
169 if (!opts.btree_node_size) {
170 /* 256k default btree node size */
171 opts.btree_node_size = 512;
173 for (i = devs; i < devs + nr_devs; i++)
174 opts.btree_node_size =
175 min(opts.btree_node_size, i->bucket_size);
178 if (!is_power_of_2(opts.block_size))
179 die("block size must be power of 2");
181 if (!is_power_of_2(opts.btree_node_size))
182 die("btree node size must be power of 2");
184 if (uuid_is_null(opts.uuid.b))
185 uuid_generate(opts.uuid.b);
187 if (bch2_sb_realloc(&sb, 0))
188 die("insufficient memory");
190 sb.sb->version = cpu_to_le64(BCH_SB_VERSION_MAX);
191 sb.sb->magic = BCACHE_MAGIC;
192 sb.sb->block_size = cpu_to_le16(opts.block_size);
193 sb.sb->user_uuid = opts.uuid;
194 sb.sb->nr_devices = nr_devs;
196 uuid_generate(sb.sb->uuid.b);
201 min(strlen(opts.label), sizeof(sb.sb->label)));
203 SET_BCH_SB_CSUM_TYPE(sb.sb, opts.meta_csum_type);
204 SET_BCH_SB_META_CSUM_TYPE(sb.sb, opts.meta_csum_type);
205 SET_BCH_SB_DATA_CSUM_TYPE(sb.sb, opts.data_csum_type);
206 SET_BCH_SB_COMPRESSION_TYPE(sb.sb, opts.compression_type);
207 SET_BCH_SB_BACKGROUND_COMPRESSION_TYPE(sb.sb,
208 opts.background_compression_type);
210 SET_BCH_SB_BTREE_NODE_SIZE(sb.sb, opts.btree_node_size);
211 SET_BCH_SB_GC_RESERVE(sb.sb, 8);
212 SET_BCH_SB_META_REPLICAS_WANT(sb.sb, opts.meta_replicas);
213 SET_BCH_SB_META_REPLICAS_REQ(sb.sb, opts.meta_replicas_required);
214 SET_BCH_SB_DATA_REPLICAS_WANT(sb.sb, opts.data_replicas);
215 SET_BCH_SB_DATA_REPLICAS_REQ(sb.sb, opts.data_replicas_required);
216 SET_BCH_SB_ERROR_ACTION(sb.sb, opts.on_error_action);
217 SET_BCH_SB_STR_HASH_TYPE(sb.sb, BCH_STR_HASH_SIPHASH);
218 SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb.sb,ilog2(opts.encoded_extent_max));
220 SET_BCH_SB_POSIX_ACL(sb.sb, 1);
223 if (clock_gettime(CLOCK_REALTIME, &now))
224 die("error getting current time: %m");
226 sb.sb->time_base_lo = cpu_to_le64(now.tv_sec * NSEC_PER_SEC + now.tv_nsec);
227 sb.sb->time_precision = cpu_to_le32(1);
230 mi = bch2_sb_resize_members(&sb,
231 (sizeof(*mi) + sizeof(struct bch_member) *
232 nr_devs) / sizeof(u64));
234 for (i = devs; i < devs + nr_devs; i++) {
235 struct bch_member *m = mi->members + (i - devs);
237 uuid_generate(m->uuid.b);
238 m->nbuckets = cpu_to_le64(i->nbuckets);
240 m->bucket_size = cpu_to_le16(i->bucket_size);
242 SET_BCH_MEMBER_REPLACEMENT(m, CACHE_REPLACEMENT_LRU);
243 SET_BCH_MEMBER_DISCARD(m, i->discard);
244 SET_BCH_MEMBER_DATA_ALLOWED(m, i->data_allowed);
245 SET_BCH_MEMBER_DURABILITY(m, i->durability + 1);
249 for (i = devs; i < devs + nr_devs; i++) {
250 struct bch_member *m = mi->members + (i - devs);
256 idx = bch2_disk_path_find_or_create(&sb, i->group);
258 die("error creating disk path: %s", idx);
260 SET_BCH_MEMBER_GROUP(m, idx + 1);
263 SET_BCH_SB_FOREGROUND_TARGET(sb.sb,
264 parse_target(&sb, devs, nr_devs, opts.foreground_target));
265 SET_BCH_SB_BACKGROUND_TARGET(sb.sb,
266 parse_target(&sb, devs, nr_devs, opts.background_target));
267 SET_BCH_SB_PROMOTE_TARGET(sb.sb,
268 parse_target(&sb, devs, nr_devs, opts.promote_target));
271 if (opts.encrypted) {
272 struct bch_sb_field_crypt *crypt =
273 bch2_sb_resize_crypt(&sb, sizeof(*crypt) / sizeof(u64));
275 bch_sb_crypt_init(sb.sb, crypt, opts.passphrase);
276 SET_BCH_SB_ENCRYPTION_TYPE(sb.sb, 1);
279 for (i = devs; i < devs + nr_devs; i++) {
280 sb.sb->dev_idx = i - devs;
282 init_layout(&sb.sb->layout, opts.block_size,
283 i->sb_offset, i->sb_end);
285 if (i->sb_offset == BCH_SB_SECTOR) {
286 /* Zero start of disk */
287 static const char zeroes[BCH_SB_SECTOR << 9];
289 xpwrite(i->fd, zeroes, BCH_SB_SECTOR << 9, 0);
292 bch2_super_write(i->fd, sb.sb);
299 void bch2_super_write(int fd, struct bch_sb *sb)
301 struct nonce nonce = { 0 };
304 for (i = 0; i < sb->layout.nr_superblocks; i++) {
305 sb->offset = sb->layout.sb_offset[i];
307 if (sb->offset == BCH_SB_SECTOR) {
308 /* Write backup layout */
309 xpwrite(fd, &sb->layout, sizeof(sb->layout),
310 BCH_SB_LAYOUT_SECTOR << 9);
313 sb->csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb), nonce, sb);
314 xpwrite(fd, sb, vstruct_bytes(sb),
315 le64_to_cpu(sb->offset) << 9);
321 struct bch_sb *__bch2_super_read(int fd, u64 sector)
323 struct bch_sb sb, *ret;
325 xpread(fd, &sb, sizeof(sb), sector << 9);
327 if (memcmp(&sb.magic, &BCACHE_MAGIC, sizeof(sb.magic)))
328 die("not a bcachefs superblock");
330 size_t bytes = vstruct_bytes(&sb);
334 xpread(fd, ret, bytes, sector << 9);
339 static unsigned get_dev_has_data(struct bch_sb *sb, unsigned dev)
341 struct bch_sb_field_replicas *replicas;
342 struct bch_replicas_entry *r;
343 unsigned i, data_has = 0;
345 replicas = bch2_sb_get_replicas(sb);
348 for_each_replicas_entry(replicas, r)
349 for (i = 0; i < r->nr_devs; i++)
350 if (r->devs[i] == dev)
351 data_has |= 1 << r->data_type;
356 /* superblock printing: */
358 static void bch2_sb_print_layout(struct bch_sb *sb, enum units units)
360 struct bch_sb_layout *l = &sb->layout;
364 " superblock max size: %s\n"
365 " nr superblocks: %u\n"
368 pr_units(1 << l->sb_max_size_bits, units),
371 for (i = 0; i < l->nr_superblocks; i++) {
374 printf("%llu", le64_to_cpu(l->sb_offset[i]));
379 static void bch2_sb_print_journal(struct bch_sb *sb, struct bch_sb_field *f,
382 struct bch_sb_field_journal *journal = field_to_type(f, journal);
383 unsigned i, nr = bch2_nr_journal_buckets(journal);
385 printf(" Buckets: ");
386 for (i = 0; i < nr; i++) {
389 printf("%llu", le64_to_cpu(journal->buckets[i]));
394 static void bch2_sb_print_members(struct bch_sb *sb, struct bch_sb_field *f,
397 struct bch_sb_field_members *mi = field_to_type(f, members);
398 struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
401 for (i = 0; i < sb->nr_devices; i++) {
402 struct bch_member *m = mi->members + i;
403 time_t last_mount = le64_to_cpu(m->last_mount);
404 char member_uuid_str[40];
405 char data_allowed_str[100];
406 char data_has_str[100];
409 if (!bch2_member_exists(m))
412 uuid_unparse(m->uuid.b, member_uuid_str);
414 if (BCH_MEMBER_GROUP(m)) {
415 unsigned idx = BCH_MEMBER_GROUP(m) - 1;
417 if (idx < disk_groups_nr(gi)) {
418 memcpy(group, gi->entries[idx].label,
420 group[BCH_SB_LABEL_SIZE] = '\0';
422 strcpy(group, "(bad disk groups section");
426 bch2_scnprint_flag_list(data_allowed_str,
427 sizeof(data_allowed_str),
429 BCH_MEMBER_DATA_ALLOWED(m));
430 if (!data_allowed_str[0])
431 strcpy(data_allowed_str, "(none)");
433 bch2_scnprint_flag_list(data_has_str,
434 sizeof(data_has_str),
436 get_dev_has_data(sb, i));
437 if (!data_has_str[0])
438 strcpy(data_has_str, "(none)");
440 printf(" Device %u:\n"
444 " First bucket: %u\n"
449 " Data allowed: %s\n"
453 " Replacement policy: %s\n"
456 pr_units(le16_to_cpu(m->bucket_size) *
457 le64_to_cpu(m->nbuckets), units),
458 pr_units(le16_to_cpu(m->bucket_size), units),
459 le16_to_cpu(m->first_bucket),
460 le64_to_cpu(m->nbuckets),
461 last_mount ? ctime(&last_mount) : "(never)",
463 BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
464 ? bch2_dev_state[BCH_MEMBER_STATE(m)]
471 BCH_MEMBER_REPLACEMENT(m) < CACHE_REPLACEMENT_NR
472 ? bch2_cache_replacement_policies[BCH_MEMBER_REPLACEMENT(m)]
475 BCH_MEMBER_DISCARD(m));
479 static void bch2_sb_print_crypt(struct bch_sb *sb, struct bch_sb_field *f,
482 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
484 printf(" KFD: %llu\n"
488 BCH_CRYPT_KDF_TYPE(crypt),
489 BCH_KDF_SCRYPT_N(crypt),
490 BCH_KDF_SCRYPT_R(crypt),
491 BCH_KDF_SCRYPT_P(crypt));
494 static void bch2_sb_print_replicas(struct bch_sb *sb, struct bch_sb_field *f,
497 struct bch_sb_field_replicas *replicas = field_to_type(f, replicas);
498 struct bch_replicas_entry *e;
501 for_each_replicas_entry(replicas, e) {
502 printf_pad(32, " %s:", bch2_data_types[e->data_type]);
505 for (i = 0; i < e->nr_devs; i++) {
508 printf("%u", e->devs[i]);
514 static void bch2_sb_print_quota(struct bch_sb *sb, struct bch_sb_field *f,
519 static void bch2_sb_print_disk_groups(struct bch_sb *sb, struct bch_sb_field *f,
524 static void bch2_sb_print_clean(struct bch_sb *sb, struct bch_sb_field *f,
529 typedef void (*sb_field_print_fn)(struct bch_sb *, struct bch_sb_field *, enum units);
531 struct bch_sb_field_toolops {
532 sb_field_print_fn print;
535 static const struct bch_sb_field_toolops bch2_sb_field_ops[] = {
537 [BCH_SB_FIELD_##f] = { \
538 .print = bch2_sb_print_##f, \
544 static inline void bch2_sb_field_print(struct bch_sb *sb,
545 struct bch_sb_field *f,
548 unsigned type = le32_to_cpu(f->type);
550 if (type < BCH_SB_FIELD_NR)
551 bch2_sb_field_ops[type].print(sb, f, units);
553 printf("(unknown field %u)\n", type);
556 void bch2_sb_print(struct bch_sb *sb, bool print_layout,
557 unsigned fields, enum units units)
559 struct bch_sb_field_members *mi;
560 char user_uuid_str[40], internal_uuid_str[40];
561 char fields_have_str[200];
562 char label[BCH_SB_LABEL_SIZE + 1];
563 struct bch_sb_field *f;
565 unsigned nr_devices = 0;
567 memset(label, 0, sizeof(label));
568 memcpy(label, sb->label, sizeof(sb->label));
569 uuid_unparse(sb->user_uuid.b, user_uuid_str);
570 uuid_unparse(sb->uuid.b, internal_uuid_str);
572 mi = bch2_sb_get_members(sb);
574 struct bch_member *m;
576 for (m = mi->members;
577 m < mi->members + sb->nr_devices;
579 nr_devices += bch2_member_exists(m);
582 vstruct_for_each(sb, f)
583 fields_have |= 1 << le32_to_cpu(f->type);
584 bch2_scnprint_flag_list(fields_have_str, sizeof(fields_have_str),
585 bch2_sb_fields, fields_have);
587 printf("External UUID: %s\n"
588 "Internal UUID: %s\n"
592 "Btree node size: %s\n"
596 "Metadata replicas: %llu\n"
597 "Data replicas: %llu\n"
599 "Metadata checksum type: %s (%llu)\n"
600 "Data checksum type: %s (%llu)\n"
601 "Compression type: %s (%llu)\n"
603 "Foreground write target: %llu\n"
604 "Background write target: %llu\n"
605 "Promote target: %llu\n"
607 "String hash type: %s (%llu)\n"
608 "32 bit inodes: %llu\n"
609 "GC reserve percentage: %llu%%\n"
610 "Root reserve percentage: %llu%%\n"
612 "Devices: %u live, %u total\n"
614 "Superblock size: %llu\n",
618 le64_to_cpu(sb->version),
619 pr_units(le16_to_cpu(sb->block_size), units),
620 pr_units(BCH_SB_BTREE_NODE_SIZE(sb), units),
622 BCH_SB_ERROR_ACTION(sb) < BCH_NR_ERROR_ACTIONS
623 ? bch2_error_actions[BCH_SB_ERROR_ACTION(sb)]
628 BCH_SB_META_REPLICAS_WANT(sb),
629 BCH_SB_DATA_REPLICAS_WANT(sb),
631 BCH_SB_META_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
632 ? bch2_csum_types[BCH_SB_META_CSUM_TYPE(sb)]
634 BCH_SB_META_CSUM_TYPE(sb),
636 BCH_SB_DATA_CSUM_TYPE(sb) < BCH_CSUM_OPT_NR
637 ? bch2_csum_types[BCH_SB_DATA_CSUM_TYPE(sb)]
639 BCH_SB_DATA_CSUM_TYPE(sb),
641 BCH_SB_COMPRESSION_TYPE(sb) < BCH_COMPRESSION_OPT_NR
642 ? bch2_compression_types[BCH_SB_COMPRESSION_TYPE(sb)]
644 BCH_SB_COMPRESSION_TYPE(sb),
646 BCH_SB_FOREGROUND_TARGET(sb),
647 BCH_SB_BACKGROUND_TARGET(sb),
648 BCH_SB_PROMOTE_TARGET(sb),
650 BCH_SB_STR_HASH_TYPE(sb) < BCH_STR_HASH_NR
651 ? bch2_str_hash_types[BCH_SB_STR_HASH_TYPE(sb)]
653 BCH_SB_STR_HASH_TYPE(sb),
655 BCH_SB_INODE_32BIT(sb),
656 BCH_SB_GC_RESERVE(sb),
657 BCH_SB_ROOT_RESERVE(sb),
659 nr_devices, sb->nr_devices,
666 bch2_sb_print_layout(sb, units);
669 vstruct_for_each(sb, f) {
670 unsigned type = le32_to_cpu(f->type);
673 if (!(fields & (1 << type)))
676 if (type < BCH_SB_FIELD_NR) {
677 scnprintf(name, sizeof(name), "%s", bch2_sb_fields[type]);
678 name[0] = toupper(name[0]);
680 scnprintf(name, sizeof(name), "(unknown field %u)", type);
683 printf("\n%s (size %llu):\n", name, vstruct_bytes(f));
684 if (type < BCH_SB_FIELD_NR)
685 bch2_sb_field_print(sb, f, units);
689 /* ioctl interface: */
691 /* Global control device: */
692 int bcachectl_open(void)
694 return xopen("/dev/bcachefs-ctl", O_RDWR);
697 /* Filesystem handles (ioctl, sysfs dir): */
699 #define SYSFS_BASE "/sys/fs/bcachefs/"
701 void bcache_fs_close(struct bchfs_handle fs)
707 struct bchfs_handle bcache_fs_open(const char *path)
709 struct bchfs_handle ret;
711 if (!uuid_parse(path, ret.uuid.b)) {
712 /* It's a UUID, look it up in sysfs: */
713 char *sysfs = mprintf(SYSFS_BASE "%s", path);
714 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
716 char *minor = read_file_str(ret.sysfs_fd, "minor");
717 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
718 ret.ioctl_fd = xopen(ctl, O_RDWR);
725 ret.ioctl_fd = xopen(path, O_RDONLY);
727 struct bch_ioctl_query_uuid uuid;
728 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
729 die("error opening %s: not a bcachefs filesystem", path);
731 ret.uuid = uuid.uuid;
734 uuid_unparse(uuid.uuid.b, uuid_str);
736 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
737 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
745 * Given a path to a block device, open the filesystem it belongs to; also
746 * return the device's idx:
748 struct bchfs_handle bchu_fs_open_by_dev(const char *path, unsigned *idx)
750 char buf[1024], *uuid_str;
752 struct stat stat = xstat(path);
754 if (!S_ISBLK(stat.st_mode))
755 die("%s is not a block device", path);
757 char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
760 ssize_t len = readlink(sysfs, buf, sizeof(buf));
764 char *p = strrchr(buf, '/');
765 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
766 die("error parsing sysfs");
769 p = strrchr(buf, '/');
772 struct bch_opts opts = bch2_opts_empty();
774 opt_set(opts, noexcl, true);
775 opt_set(opts, nochanges, true);
777 struct bch_sb_handle sb;
778 int ret = bch2_read_super(path, &opts, &sb);
780 die("Error opening %s: %s", path, strerror(-ret));
782 *idx = sb.sb->dev_idx;
784 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
786 bch2_free_super(&sb);
789 return bcache_fs_open(uuid_str);
792 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
794 int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
797 struct bch_ioctl_data_event e;
799 if (read(progress_fd, &e, sizeof(e)) != sizeof(e))
800 die("error reading from progress fd %m");
805 if (e.p.data_type == U8_MAX)
810 printf("%llu%% complete: current position %s",
811 e.p.sectors_done * 100 / e.p.sectors_total,
812 bch2_data_types[e.p.data_type]);
814 switch (e.p.data_type) {
817 printf(" %s:%llu:%llu",
818 bch2_btree_ids[e.p.btree_id],