10 #include <linux/backing-dev.h>
11 #include <linux/sort.h>
13 static int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *);
14 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *,
15 struct bch_replicas_cpu *);
17 /* superblock fields (optional/variable size sections: */
19 const char * const bch2_sb_fields[] = {
20 #define x(name, nr) #name,
27 static const char *bch2_sb_validate_##f(struct bch_sb *, struct bch_sb_field *);
31 struct bch_sb_field_ops {
32 const char * (*validate)(struct bch_sb *, struct bch_sb_field *);
35 static const struct bch_sb_field_ops bch2_sb_field_ops[] = {
37 [BCH_SB_FIELD_##f] = { \
38 .validate = bch2_sb_validate_##f, \
44 static const char *bch2_sb_field_validate(struct bch_sb *sb,
45 struct bch_sb_field *f)
47 unsigned type = le32_to_cpu(f->type);
49 return type < BCH_SB_FIELD_NR
50 ? bch2_sb_field_ops[type].validate(sb, f)
54 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
55 enum bch_sb_field_type type)
57 struct bch_sb_field *f;
59 /* XXX: need locking around superblock to access optional fields */
61 vstruct_for_each(sb, f)
62 if (le32_to_cpu(f->type) == type)
67 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb *sb,
68 struct bch_sb_field *f,
71 unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
75 memset(f, 0, sizeof(u64) * u64s);
76 f->u64s = cpu_to_le32(u64s);
82 f->u64s = cpu_to_le32(u64s);
85 memmove(dst, src, vstruct_end(sb) - src);
88 memset(src, 0, dst - src);
91 le32_add_cpu(&sb->u64s, u64s - old_u64s);
96 /* Superblock realloc/free: */
98 void bch2_free_super(struct bch_sb_handle *sb)
102 if (!IS_ERR_OR_NULL(sb->bdev))
103 blkdev_put(sb->bdev, sb->mode);
105 free_pages((unsigned long) sb->sb, sb->page_order);
106 memset(sb, 0, sizeof(*sb));
109 static int __bch2_super_realloc(struct bch_sb_handle *sb, unsigned order)
111 struct bch_sb *new_sb;
114 if (sb->page_order >= order && sb->sb)
117 if (dynamic_fault("bcachefs:add:super_realloc"))
120 bio = bio_kmalloc(GFP_KERNEL, 1 << order);
128 new_sb = (void *) __get_free_pages(GFP_KERNEL, order);
133 memcpy(new_sb, sb->sb, PAGE_SIZE << sb->page_order);
135 free_pages((unsigned long) sb->sb, sb->page_order);
138 sb->page_order = order;
143 static int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
145 u64 new_bytes = __vstruct_bytes(struct bch_sb, u64s);
146 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
148 if (new_bytes > max_bytes) {
149 char buf[BDEVNAME_SIZE];
151 pr_err("%s: superblock too big: want %llu but have %llu",
152 bdevname(sb->bdev, buf), new_bytes, max_bytes);
156 return __bch2_super_realloc(sb, get_order(new_bytes));
159 static int bch2_fs_sb_realloc(struct bch_fs *c, unsigned u64s)
161 u64 bytes = __vstruct_bytes(struct bch_sb, u64s);
163 unsigned order = get_order(bytes);
165 if (c->disk_sb && order <= c->disk_sb_order)
168 sb = (void *) __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
173 memcpy(sb, c->disk_sb, PAGE_SIZE << c->disk_sb_order);
175 free_pages((unsigned long) c->disk_sb, c->disk_sb_order);
178 c->disk_sb_order = order;
182 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb,
183 enum bch_sb_field_type type,
186 struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
187 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
188 ssize_t d = -old_u64s + u64s;
190 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
193 f = __bch2_sb_field_resize(sb->sb, f, u64s);
194 f->type = cpu_to_le32(type);
198 struct bch_sb_field *bch2_fs_sb_field_resize(struct bch_fs *c,
199 enum bch_sb_field_type type,
202 struct bch_sb_field *f = bch2_sb_field_get(c->disk_sb, type);
203 ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
204 ssize_t d = -old_u64s + u64s;
208 lockdep_assert_held(&c->sb_lock);
210 if (bch2_fs_sb_realloc(c, le32_to_cpu(c->disk_sb->u64s) + d))
213 /* XXX: we're not checking that offline device have enough space */
215 for_each_online_member(ca, c, i) {
216 struct bch_sb_handle *sb = &ca->disk_sb;
218 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) {
219 percpu_ref_put(&ca->ref);
224 f = __bch2_sb_field_resize(c->disk_sb, f, u64s);
225 f->type = cpu_to_le32(type);
229 /* Superblock validate: */
231 static inline void __bch2_sb_layout_size_assert(void)
233 BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
236 static const char *validate_sb_layout(struct bch_sb_layout *layout)
238 u64 offset, prev_offset, max_sectors;
241 if (uuid_le_cmp(layout->magic, BCACHE_MAGIC))
242 return "Not a bcachefs superblock layout";
244 if (layout->layout_type != 0)
245 return "Invalid superblock layout type";
247 if (!layout->nr_superblocks)
248 return "Invalid superblock layout: no superblocks";
250 if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset))
251 return "Invalid superblock layout: too many superblocks";
253 max_sectors = 1 << layout->sb_max_size_bits;
255 prev_offset = le64_to_cpu(layout->sb_offset[0]);
257 for (i = 1; i < layout->nr_superblocks; i++) {
258 offset = le64_to_cpu(layout->sb_offset[i]);
260 if (offset < prev_offset + max_sectors)
261 return "Invalid superblock layout: superblocks overlap";
262 prev_offset = offset;
268 const char *bch2_sb_validate(struct bch_sb_handle *disk_sb)
270 struct bch_sb *sb = disk_sb->sb;
271 struct bch_sb_field *f;
272 struct bch_sb_field_members *mi;
276 if (le64_to_cpu(sb->version) < BCH_SB_VERSION_MIN ||
277 le64_to_cpu(sb->version) > BCH_SB_VERSION_MAX)
278 return"Unsupported superblock version";
280 if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_MAX) {
281 SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb, 7);
282 SET_BCH_SB_POSIX_ACL(sb, 1);
285 block_size = le16_to_cpu(sb->block_size);
287 if (!is_power_of_2(block_size) ||
288 block_size > PAGE_SECTORS)
289 return "Bad block size";
291 if (bch2_is_zero(sb->user_uuid.b, sizeof(uuid_le)))
292 return "Bad user UUID";
294 if (bch2_is_zero(sb->uuid.b, sizeof(uuid_le)))
295 return "Bad internal UUID";
297 if (!sb->nr_devices ||
298 sb->nr_devices <= sb->dev_idx ||
299 sb->nr_devices > BCH_SB_MEMBERS_MAX)
300 return "Bad cache device number in set";
302 if (!BCH_SB_META_REPLICAS_WANT(sb) ||
303 BCH_SB_META_REPLICAS_WANT(sb) >= BCH_REPLICAS_MAX)
304 return "Invalid number of metadata replicas";
306 if (!BCH_SB_META_REPLICAS_REQ(sb) ||
307 BCH_SB_META_REPLICAS_REQ(sb) >= BCH_REPLICAS_MAX)
308 return "Invalid number of metadata replicas";
310 if (!BCH_SB_DATA_REPLICAS_WANT(sb) ||
311 BCH_SB_DATA_REPLICAS_WANT(sb) >= BCH_REPLICAS_MAX)
312 return "Invalid number of data replicas";
314 if (!BCH_SB_DATA_REPLICAS_REQ(sb) ||
315 BCH_SB_DATA_REPLICAS_REQ(sb) >= BCH_REPLICAS_MAX)
316 return "Invalid number of data replicas";
318 if (BCH_SB_META_CSUM_TYPE(sb) >= BCH_CSUM_OPT_NR)
319 return "Invalid metadata checksum type";
321 if (BCH_SB_DATA_CSUM_TYPE(sb) >= BCH_CSUM_OPT_NR)
322 return "Invalid metadata checksum type";
324 if (BCH_SB_COMPRESSION_TYPE(sb) >= BCH_COMPRESSION_OPT_NR)
325 return "Invalid compression type";
327 if (!BCH_SB_BTREE_NODE_SIZE(sb))
328 return "Btree node size not set";
330 if (!is_power_of_2(BCH_SB_BTREE_NODE_SIZE(sb)))
331 return "Btree node size not a power of two";
333 if (BCH_SB_GC_RESERVE(sb) < 5)
334 return "gc reserve percentage too small";
336 if (!sb->time_precision ||
337 le32_to_cpu(sb->time_precision) > NSEC_PER_SEC)
338 return "invalid time precision";
340 /* validate layout */
341 err = validate_sb_layout(&sb->layout);
345 vstruct_for_each(sb, f) {
347 return "Invalid superblock: invalid optional field";
349 if (vstruct_next(f) > vstruct_last(sb))
350 return "Invalid superblock: invalid optional field";
353 /* members must be validated first: */
354 mi = bch2_sb_get_members(sb);
356 return "Invalid superblock: member info area missing";
358 err = bch2_sb_field_validate(sb, &mi->field);
362 vstruct_for_each(sb, f) {
363 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members)
366 err = bch2_sb_field_validate(sb, f);
371 if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_NONCE_V1 &&
372 bch2_sb_get_crypt(sb) &&
373 BCH_SB_INITIALIZED(sb))
374 return "Incompatible extent nonces";
376 sb->version = cpu_to_le64(BCH_SB_VERSION_MAX);
383 static void bch2_sb_update(struct bch_fs *c)
385 struct bch_sb *src = c->disk_sb;
386 struct bch_sb_field_members *mi = bch2_sb_get_members(src);
390 lockdep_assert_held(&c->sb_lock);
392 c->sb.uuid = src->uuid;
393 c->sb.user_uuid = src->user_uuid;
394 c->sb.nr_devices = src->nr_devices;
395 c->sb.clean = BCH_SB_CLEAN(src);
396 c->sb.encryption_type = BCH_SB_ENCRYPTION_TYPE(src);
397 c->sb.encoded_extent_max= 1 << BCH_SB_ENCODED_EXTENT_MAX_BITS(src);
398 c->sb.time_base_lo = le64_to_cpu(src->time_base_lo);
399 c->sb.time_base_hi = le32_to_cpu(src->time_base_hi);
400 c->sb.time_precision = le32_to_cpu(src->time_precision);
402 for_each_member_device(ca, c, i)
403 ca->mi = bch2_mi_to_cpu(mi->members + i);
406 /* doesn't copy member info */
407 static void __copy_super(struct bch_sb *dst, struct bch_sb *src)
409 struct bch_sb_field *src_f, *dst_f;
411 dst->version = src->version;
413 dst->uuid = src->uuid;
414 dst->user_uuid = src->user_uuid;
415 memcpy(dst->label, src->label, sizeof(dst->label));
417 dst->block_size = src->block_size;
418 dst->nr_devices = src->nr_devices;
420 dst->time_base_lo = src->time_base_lo;
421 dst->time_base_hi = src->time_base_hi;
422 dst->time_precision = src->time_precision;
424 memcpy(dst->flags, src->flags, sizeof(dst->flags));
425 memcpy(dst->features, src->features, sizeof(dst->features));
426 memcpy(dst->compat, src->compat, sizeof(dst->compat));
428 vstruct_for_each(src, src_f) {
429 if (src_f->type == BCH_SB_FIELD_journal)
432 dst_f = bch2_sb_field_get(dst, le32_to_cpu(src_f->type));
433 dst_f = __bch2_sb_field_resize(dst, dst_f,
434 le32_to_cpu(src_f->u64s));
436 memcpy(dst_f, src_f, vstruct_bytes(src_f));
440 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
442 struct bch_sb_field_journal *journal_buckets =
443 bch2_sb_get_journal(src);
444 unsigned journal_u64s = journal_buckets
445 ? le32_to_cpu(journal_buckets->field.u64s)
449 lockdep_assert_held(&c->sb_lock);
451 ret = bch2_fs_sb_realloc(c, le32_to_cpu(src->u64s) - journal_u64s);
455 __copy_super(c->disk_sb, src);
457 ret = bch2_sb_replicas_to_cpu_replicas(c);
465 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
467 struct bch_sb *src = c->disk_sb, *dst = ca->disk_sb.sb;
468 struct bch_sb_field_journal *journal_buckets =
469 bch2_sb_get_journal(dst);
470 unsigned journal_u64s = journal_buckets
471 ? le32_to_cpu(journal_buckets->field.u64s)
473 unsigned u64s = le32_to_cpu(src->u64s) + journal_u64s;
476 ret = bch2_sb_realloc(&ca->disk_sb, u64s);
480 __copy_super(dst, src);
484 /* read superblock: */
486 static const char *read_one_super(struct bch_sb_handle *sb, u64 offset)
488 struct bch_csum csum;
493 bio_set_dev(sb->bio, sb->bdev);
494 sb->bio->bi_iter.bi_sector = offset;
495 sb->bio->bi_iter.bi_size = PAGE_SIZE << sb->page_order;
496 bio_set_op_attrs(sb->bio, REQ_OP_READ, REQ_SYNC|REQ_META);
497 bch2_bio_map(sb->bio, sb->sb);
499 if (submit_bio_wait(sb->bio))
502 if (uuid_le_cmp(sb->sb->magic, BCACHE_MAGIC))
503 return "Not a bcachefs superblock";
505 if (le64_to_cpu(sb->sb->version) < BCH_SB_VERSION_MIN ||
506 le64_to_cpu(sb->sb->version) > BCH_SB_VERSION_MAX)
507 return"Unsupported superblock version";
509 bytes = vstruct_bytes(sb->sb);
511 if (bytes > 512 << sb->sb->layout.sb_max_size_bits)
512 return "Bad superblock: too big";
514 order = get_order(bytes);
515 if (order > sb->page_order) {
516 if (__bch2_super_realloc(sb, order))
517 return "cannot allocate memory";
521 if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR)
522 return "unknown csum type";
524 /* XXX: verify MACs */
525 csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
526 null_nonce(), sb->sb);
528 if (bch2_crc_cmp(csum, sb->sb->csum))
529 return "bad checksum reading superblock";
534 int bch2_read_super(const char *path, struct bch_opts *opts,
535 struct bch_sb_handle *sb)
537 u64 offset = opt_get(*opts, sb);
538 struct bch_sb_layout layout;
543 memset(sb, 0, sizeof(*sb));
544 sb->mode = FMODE_READ;
546 if (!opt_get(*opts, noexcl))
547 sb->mode |= FMODE_EXCL;
549 if (!opt_get(*opts, nochanges))
550 sb->mode |= FMODE_WRITE;
552 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
553 if (IS_ERR(sb->bdev) &&
554 PTR_ERR(sb->bdev) == -EACCES &&
555 opt_get(*opts, read_only)) {
556 sb->mode &= ~FMODE_WRITE;
558 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
559 if (!IS_ERR(sb->bdev))
560 opt_set(*opts, nochanges, true);
563 if (IS_ERR(sb->bdev))
564 return PTR_ERR(sb->bdev);
566 err = "cannot allocate memory";
567 ret = __bch2_super_realloc(sb, 0);
572 err = "dynamic fault";
573 if (bch2_fs_init_fault("read_super"))
577 err = read_one_super(sb, offset);
581 if (opt_defined(*opts, sb))
584 pr_err("error reading default superblock: %s", err);
587 * Error reading primary superblock - read location of backup
591 bio_set_dev(sb->bio, sb->bdev);
592 sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
593 sb->bio->bi_iter.bi_size = sizeof(struct bch_sb_layout);
594 bio_set_op_attrs(sb->bio, REQ_OP_READ, REQ_SYNC|REQ_META);
596 * use sb buffer to read layout, since sb buffer is page aligned but
599 bch2_bio_map(sb->bio, sb->sb);
602 if (submit_bio_wait(sb->bio))
605 memcpy(&layout, sb->sb, sizeof(layout));
606 err = validate_sb_layout(&layout);
610 for (i = layout.sb_offset;
611 i < layout.sb_offset + layout.nr_superblocks; i++) {
612 offset = le64_to_cpu(*i);
614 if (offset == opt_get(*opts, sb))
617 err = read_one_super(sb, offset);
626 err = "Superblock block size smaller than device block size";
628 if (le16_to_cpu(sb->sb->block_size) << 9 <
629 bdev_logical_block_size(sb->bdev))
632 if (sb->mode & FMODE_WRITE)
633 bdev_get_queue(sb->bdev)->backing_dev_info->capabilities
634 |= BDI_CAP_STABLE_WRITES;
639 pr_err("error reading superblock: %s", err);
643 /* write superblock: */
645 static void write_super_endio(struct bio *bio)
647 struct bch_dev *ca = bio->bi_private;
649 /* XXX: return errors directly */
651 if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write"))
652 ca->sb_write_error = 1;
654 closure_put(&ca->fs->sb_write);
655 percpu_ref_put(&ca->io_ref);
658 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
660 struct bch_sb *sb = ca->disk_sb.sb;
661 struct bio *bio = ca->disk_sb.bio;
663 sb->offset = sb->layout.sb_offset[idx];
665 SET_BCH_SB_CSUM_TYPE(sb, c->opts.metadata_checksum);
666 sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
670 bio_set_dev(bio, ca->disk_sb.bdev);
671 bio->bi_iter.bi_sector = le64_to_cpu(sb->offset);
672 bio->bi_iter.bi_size =
673 roundup(vstruct_bytes(sb),
674 bdev_logical_block_size(ca->disk_sb.bdev));
675 bio->bi_end_io = write_super_endio;
676 bio->bi_private = ca;
677 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC|REQ_META);
678 bch2_bio_map(bio, sb);
680 this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_SB],
683 percpu_ref_get(&ca->io_ref);
684 closure_bio_submit(bio, &c->sb_write);
687 void bch2_write_super(struct bch_fs *c)
689 struct closure *cl = &c->sb_write;
691 unsigned i, sb = 0, nr_wrote;
693 struct bch_devs_mask sb_written;
694 bool wrote, can_mount_without_written, can_mount_with_written;
696 lockdep_assert_held(&c->sb_lock);
698 closure_init_stack(cl);
699 memset(&sb_written, 0, sizeof(sb_written));
701 le64_add_cpu(&c->disk_sb->seq, 1);
703 for_each_online_member(ca, c, i)
704 bch2_sb_from_fs(c, ca);
706 for_each_online_member(ca, c, i) {
707 err = bch2_sb_validate(&ca->disk_sb);
709 bch2_fs_inconsistent(c, "sb invalid before write: %s", err);
714 if (c->opts.nochanges ||
715 test_bit(BCH_FS_ERROR, &c->flags))
718 for_each_online_member(ca, c, i) {
719 __set_bit(ca->dev_idx, sb_written.d);
720 ca->sb_write_error = 0;
725 for_each_online_member(ca, c, i)
726 if (sb < ca->disk_sb.sb->layout.nr_superblocks) {
727 write_one_super(c, ca, sb);
734 for_each_online_member(ca, c, i)
735 if (ca->sb_write_error)
736 __clear_bit(ca->dev_idx, sb_written.d);
738 nr_wrote = dev_mask_nr(&sb_written);
740 can_mount_with_written =
741 bch2_have_enough_devs(c,
742 __bch2_replicas_status(c, sb_written),
743 BCH_FORCE_IF_DEGRADED);
745 for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
746 sb_written.d[i] = ~sb_written.d[i];
748 can_mount_without_written =
749 bch2_have_enough_devs(c,
750 __bch2_replicas_status(c, sb_written),
751 BCH_FORCE_IF_DEGRADED);
754 * If we would be able to mount _without_ the devices we successfully
755 * wrote superblocks to, we weren't able to write to enough devices:
757 * Exception: if we can mount without the successes because we haven't
758 * written anything (new filesystem), we continue if we'd be able to
759 * mount with the devices we did successfully write to:
761 bch2_fs_fatal_err_on(!nr_wrote ||
762 (can_mount_without_written &&
763 !can_mount_with_written), c,
764 "Unable to write superblock to sufficient devices");
766 /* Make new options visible after they're persistent: */
770 /* BCH_SB_FIELD_journal: */
772 static int u64_cmp(const void *_l, const void *_r)
774 u64 l = *((const u64 *) _l), r = *((const u64 *) _r);
776 return l < r ? -1 : l > r ? 1 : 0;
779 static const char *bch2_sb_validate_journal(struct bch_sb *sb,
780 struct bch_sb_field *f)
782 struct bch_sb_field_journal *journal = field_to_type(f, journal);
783 struct bch_member *m = bch2_sb_get_members(sb)->members + sb->dev_idx;
789 journal = bch2_sb_get_journal(sb);
793 nr = bch2_nr_journal_buckets(journal);
797 b = kmalloc_array(sizeof(u64), nr, GFP_KERNEL);
799 return "cannot allocate memory";
801 for (i = 0; i < nr; i++)
802 b[i] = le64_to_cpu(journal->buckets[i]);
804 sort(b, nr, sizeof(u64), u64_cmp, NULL);
806 err = "journal bucket at sector 0";
810 err = "journal bucket before first bucket";
811 if (m && b[0] < le16_to_cpu(m->first_bucket))
814 err = "journal bucket past end of device";
815 if (m && b[nr - 1] >= le64_to_cpu(m->nbuckets))
818 err = "duplicate journal buckets";
819 for (i = 0; i + 1 < nr; i++)
820 if (b[i] == b[i + 1])
829 /* BCH_SB_FIELD_members: */
831 static const char *bch2_sb_validate_members(struct bch_sb *sb,
832 struct bch_sb_field *f)
834 struct bch_sb_field_members *mi = field_to_type(f, members);
835 struct bch_member *m;
837 if ((void *) (mi->members + sb->nr_devices) >
838 vstruct_end(&mi->field))
839 return "Invalid superblock: bad member info";
841 for (m = mi->members;
842 m < mi->members + sb->nr_devices;
844 if (!bch2_member_exists(m))
847 if (le64_to_cpu(m->nbuckets) > LONG_MAX)
848 return "Too many buckets";
850 if (le64_to_cpu(m->nbuckets) -
851 le16_to_cpu(m->first_bucket) < 1 << 10)
852 return "Not enough buckets";
854 if (le16_to_cpu(m->bucket_size) <
855 le16_to_cpu(sb->block_size))
856 return "bucket size smaller than block size";
858 if (le16_to_cpu(m->bucket_size) <
859 BCH_SB_BTREE_NODE_SIZE(sb))
860 return "bucket size smaller than btree node size";
863 if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_MAX)
864 for (m = mi->members;
865 m < mi->members + sb->nr_devices;
867 SET_BCH_MEMBER_DATA_ALLOWED(m, ~0);
872 /* BCH_SB_FIELD_crypt: */
874 static const char *bch2_sb_validate_crypt(struct bch_sb *sb,
875 struct bch_sb_field *f)
877 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
879 if (vstruct_bytes(&crypt->field) != sizeof(*crypt))
880 return "invalid field crypt: wrong size";
882 if (BCH_CRYPT_KDF_TYPE(crypt))
883 return "invalid field crypt: bad kdf type";
888 /* BCH_SB_FIELD_replicas: */
890 /* Replicas tracking - in memory: */
892 #define for_each_cpu_replicas_entry(_r, _i) \
893 for (_i = (_r)->entries; \
894 (void *) (_i) < (void *) (_r)->entries + (_r)->nr * (_r)->entry_size;\
895 _i = (void *) (_i) + (_r)->entry_size)
897 static inline struct bch_replicas_cpu_entry *
898 cpu_replicas_entry(struct bch_replicas_cpu *r, unsigned i)
900 return (void *) r->entries + r->entry_size * i;
903 static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
905 eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL);
908 static inline bool replicas_test_dev(struct bch_replicas_cpu_entry *e,
911 return (e->devs[dev >> 3] & (1 << (dev & 7))) != 0;
914 static inline void replicas_set_dev(struct bch_replicas_cpu_entry *e,
917 e->devs[dev >> 3] |= 1 << (dev & 7);
920 static inline unsigned replicas_dev_slots(struct bch_replicas_cpu *r)
922 return (r->entry_size -
923 offsetof(struct bch_replicas_cpu_entry, devs)) * 8;
926 int bch2_cpu_replicas_to_text(struct bch_replicas_cpu *r,
927 char *buf, size_t size)
929 char *out = buf, *end = out + size;
930 struct bch_replicas_cpu_entry *e;
934 for_each_cpu_replicas_entry(r, e) {
938 out += scnprintf(out, end - out, " ");
941 out += scnprintf(out, end - out, "%u: [", e->data_type);
943 for (i = 0; i < replicas_dev_slots(r); i++)
944 if (replicas_test_dev(e, i)) {
946 out += scnprintf(out, end - out, " ");
948 out += scnprintf(out, end - out, "%u", i);
950 out += scnprintf(out, end - out, "]");
956 static inline unsigned bkey_to_replicas(struct bkey_s_c_extent e,
957 enum bch_data_type data_type,
958 struct bch_replicas_cpu_entry *r,
961 const struct bch_extent_ptr *ptr;
965 data_type == BCH_DATA_SB ||
966 data_type >= BCH_DATA_NR);
968 memset(r, 0, sizeof(*r));
969 r->data_type = data_type;
973 extent_for_each_ptr(e, ptr)
975 *max_dev = max_t(unsigned, *max_dev, ptr->dev);
976 replicas_set_dev(r, ptr->dev);
982 static inline void devlist_to_replicas(struct bch_devs_list devs,
983 enum bch_data_type data_type,
984 struct bch_replicas_cpu_entry *r,
990 data_type == BCH_DATA_SB ||
991 data_type >= BCH_DATA_NR);
993 memset(r, 0, sizeof(*r));
994 r->data_type = data_type;
998 for (i = 0; i < devs.nr; i++) {
999 *max_dev = max_t(unsigned, *max_dev, devs.devs[i]);
1000 replicas_set_dev(r, devs.devs[i]);
1004 static struct bch_replicas_cpu *
1005 cpu_replicas_add_entry(struct bch_replicas_cpu *old,
1006 struct bch_replicas_cpu_entry new_entry,
1009 struct bch_replicas_cpu *new;
1010 unsigned i, nr, entry_size;
1012 entry_size = offsetof(struct bch_replicas_cpu_entry, devs) +
1013 DIV_ROUND_UP(max_dev + 1, 8);
1014 entry_size = max(entry_size, old->entry_size);
1017 new = kzalloc(sizeof(struct bch_replicas_cpu) +
1018 nr * entry_size, GFP_NOIO);
1023 new->entry_size = entry_size;
1025 for (i = 0; i < old->nr; i++)
1026 memcpy(cpu_replicas_entry(new, i),
1027 cpu_replicas_entry(old, i),
1028 min(new->entry_size, old->entry_size));
1030 memcpy(cpu_replicas_entry(new, old->nr),
1034 bch2_cpu_replicas_sort(new);
1038 static bool replicas_has_entry(struct bch_replicas_cpu *r,
1039 struct bch_replicas_cpu_entry search,
1042 return max_dev < replicas_dev_slots(r) &&
1043 eytzinger0_find(r->entries, r->nr,
1045 memcmp, &search) < r->nr;
1049 static int bch2_check_mark_super_slowpath(struct bch_fs *c,
1050 struct bch_replicas_cpu_entry new_entry,
1053 struct bch_replicas_cpu *old_gc, *new_gc = NULL, *old_r, *new_r = NULL;
1056 mutex_lock(&c->sb_lock);
1058 old_gc = rcu_dereference_protected(c->replicas_gc,
1059 lockdep_is_held(&c->sb_lock));
1060 if (old_gc && !replicas_has_entry(old_gc, new_entry, max_dev)) {
1061 new_gc = cpu_replicas_add_entry(old_gc, new_entry, max_dev);
1066 old_r = rcu_dereference_protected(c->replicas,
1067 lockdep_is_held(&c->sb_lock));
1068 if (!replicas_has_entry(old_r, new_entry, max_dev)) {
1069 new_r = cpu_replicas_add_entry(old_r, new_entry, max_dev);
1073 ret = bch2_cpu_replicas_to_sb_replicas(c, new_r);
1078 /* allocations done, now commit: */
1081 bch2_write_super(c);
1083 /* don't update in memory replicas until changes are persistent */
1086 rcu_assign_pointer(c->replicas_gc, new_gc);
1087 kfree_rcu(old_gc, rcu);
1091 rcu_assign_pointer(c->replicas, new_r);
1092 kfree_rcu(old_r, rcu);
1095 mutex_unlock(&c->sb_lock);
1098 mutex_unlock(&c->sb_lock);
1106 int bch2_check_mark_super(struct bch_fs *c,
1107 enum bch_data_type data_type,
1108 struct bch_devs_list devs)
1110 struct bch_replicas_cpu_entry search;
1111 struct bch_replicas_cpu *r, *gc_r;
1118 devlist_to_replicas(devs, data_type, &search, &max_dev);
1121 r = rcu_dereference(c->replicas);
1122 gc_r = rcu_dereference(c->replicas_gc);
1123 marked = replicas_has_entry(r, search, max_dev) &&
1124 (!likely(gc_r) || replicas_has_entry(gc_r, search, max_dev));
1127 return likely(marked) ? 0
1128 : bch2_check_mark_super_slowpath(c, search, max_dev);
1131 int bch2_replicas_gc_end(struct bch_fs *c, int err)
1133 struct bch_replicas_cpu *new_r, *old_r;
1136 lockdep_assert_held(&c->replicas_gc_lock);
1138 mutex_lock(&c->sb_lock);
1140 new_r = rcu_dereference_protected(c->replicas_gc,
1141 lockdep_is_held(&c->sb_lock));
1144 rcu_assign_pointer(c->replicas_gc, NULL);
1145 kfree_rcu(new_r, rcu);
1149 if (bch2_cpu_replicas_to_sb_replicas(c, new_r)) {
1154 old_r = rcu_dereference_protected(c->replicas,
1155 lockdep_is_held(&c->sb_lock));
1157 rcu_assign_pointer(c->replicas, new_r);
1158 rcu_assign_pointer(c->replicas_gc, NULL);
1159 kfree_rcu(old_r, rcu);
1161 bch2_write_super(c);
1163 mutex_unlock(&c->sb_lock);
1167 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
1169 struct bch_replicas_cpu *dst, *src;
1170 struct bch_replicas_cpu_entry *e;
1172 lockdep_assert_held(&c->replicas_gc_lock);
1174 mutex_lock(&c->sb_lock);
1175 BUG_ON(c->replicas_gc);
1177 src = rcu_dereference_protected(c->replicas,
1178 lockdep_is_held(&c->sb_lock));
1180 dst = kzalloc(sizeof(struct bch_replicas_cpu) +
1181 src->nr * src->entry_size, GFP_NOIO);
1183 mutex_unlock(&c->sb_lock);
1188 dst->entry_size = src->entry_size;
1190 for_each_cpu_replicas_entry(src, e)
1191 if (!((1 << e->data_type) & typemask))
1192 memcpy(cpu_replicas_entry(dst, dst->nr++),
1193 e, dst->entry_size);
1195 bch2_cpu_replicas_sort(dst);
1197 rcu_assign_pointer(c->replicas_gc, dst);
1198 mutex_unlock(&c->sb_lock);
1203 /* Replicas tracking - superblock: */
1205 static void bch2_sb_replicas_nr_entries(struct bch_sb_field_replicas *r,
1210 struct bch_replicas_entry *i;
1214 *bytes = sizeof(*r);
1220 for_each_replicas_entry(r, i) {
1221 for (j = 0; j < i->nr; j++)
1222 *max_dev = max_t(unsigned, *max_dev, i->devs[j]);
1226 *bytes = (void *) i - (void *) r;
1229 static struct bch_replicas_cpu *
1230 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r)
1232 struct bch_replicas_cpu *cpu_r;
1233 unsigned i, nr, bytes, max_dev, entry_size;
1235 bch2_sb_replicas_nr_entries(sb_r, &nr, &bytes, &max_dev);
1237 entry_size = offsetof(struct bch_replicas_cpu_entry, devs) +
1238 DIV_ROUND_UP(max_dev + 1, 8);
1240 cpu_r = kzalloc(sizeof(struct bch_replicas_cpu) +
1241 nr * entry_size, GFP_NOIO);
1246 cpu_r->entry_size = entry_size;
1249 struct bch_replicas_cpu_entry *dst =
1250 cpu_replicas_entry(cpu_r, 0);
1251 struct bch_replicas_entry *src = sb_r->entries;
1253 while (dst < cpu_replicas_entry(cpu_r, nr)) {
1254 dst->data_type = src->data_type;
1255 for (i = 0; i < src->nr; i++)
1256 replicas_set_dev(dst, src->devs[i]);
1258 src = replicas_entry_next(src);
1259 dst = (void *) dst + entry_size;
1263 bch2_cpu_replicas_sort(cpu_r);
1267 static int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
1269 struct bch_sb_field_replicas *sb_r;
1270 struct bch_replicas_cpu *cpu_r, *old_r;
1272 sb_r = bch2_sb_get_replicas(c->disk_sb);
1273 cpu_r = __bch2_sb_replicas_to_cpu_replicas(sb_r);
1277 old_r = rcu_dereference_check(c->replicas, lockdep_is_held(&c->sb_lock));
1278 rcu_assign_pointer(c->replicas, cpu_r);
1280 kfree_rcu(old_r, rcu);
1285 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
1286 struct bch_replicas_cpu *r)
1288 struct bch_sb_field_replicas *sb_r;
1289 struct bch_replicas_entry *sb_e;
1290 struct bch_replicas_cpu_entry *e;
1293 bytes = sizeof(struct bch_sb_field_replicas);
1295 for_each_cpu_replicas_entry(r, e) {
1296 bytes += sizeof(struct bch_replicas_entry);
1297 for (i = 0; i < r->entry_size - 1; i++)
1298 bytes += hweight8(e->devs[i]);
1301 sb_r = bch2_fs_sb_resize_replicas(c,
1302 DIV_ROUND_UP(sizeof(*sb_r) + bytes, sizeof(u64)));
1306 memset(&sb_r->entries, 0,
1307 vstruct_end(&sb_r->field) -
1308 (void *) &sb_r->entries);
1310 sb_e = sb_r->entries;
1311 for_each_cpu_replicas_entry(r, e) {
1312 sb_e->data_type = e->data_type;
1314 for (i = 0; i < replicas_dev_slots(r); i++)
1315 if (replicas_test_dev(e, i))
1316 sb_e->devs[sb_e->nr++] = i;
1318 sb_e = replicas_entry_next(sb_e);
1320 BUG_ON((void *) sb_e > vstruct_end(&sb_r->field));
1326 static const char *bch2_sb_validate_replicas(struct bch_sb *sb,
1327 struct bch_sb_field *f)
1329 struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
1330 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
1331 struct bch_replicas_cpu *cpu_r = NULL;
1332 struct bch_replicas_entry *e;
1336 for_each_replicas_entry(sb_r, e) {
1337 err = "invalid replicas entry: invalid data type";
1338 if (e->data_type >= BCH_DATA_NR)
1341 err = "invalid replicas entry: no devices";
1345 err = "invalid replicas entry: too many devices";
1346 if (e->nr >= BCH_REPLICAS_MAX)
1349 err = "invalid replicas entry: invalid device";
1350 for (i = 0; i < e->nr; i++)
1351 if (!bch2_dev_exists(sb, mi, e->devs[i]))
1355 err = "cannot allocate memory";
1356 cpu_r = __bch2_sb_replicas_to_cpu_replicas(sb_r);
1360 sort_cmp_size(cpu_r->entries,
1365 for (i = 0; i + 1 < cpu_r->nr; i++) {
1366 struct bch_replicas_cpu_entry *l =
1367 cpu_replicas_entry(cpu_r, i);
1368 struct bch_replicas_cpu_entry *r =
1369 cpu_replicas_entry(cpu_r, i + 1);
1371 BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0);
1373 err = "duplicate replicas entry";
1374 if (!memcmp(l, r, cpu_r->entry_size))
1384 int bch2_sb_replicas_to_text(struct bch_sb_field_replicas *r, char *buf, size_t size)
1386 char *out = buf, *end = out + size;
1387 struct bch_replicas_entry *e;
1392 out += scnprintf(out, end - out, "(no replicas section found)");
1396 for_each_replicas_entry(r, e) {
1398 out += scnprintf(out, end - out, " ");
1401 out += scnprintf(out, end - out, "%u: [", e->data_type);
1403 for (i = 0; i < e->nr; i++)
1404 out += scnprintf(out, end - out,
1405 i ? " %u" : "%u", e->devs[i]);
1406 out += scnprintf(out, end - out, "]");
1412 /* Query replicas: */
1414 bool bch2_sb_has_replicas(struct bch_fs *c,
1415 enum bch_data_type data_type,
1416 struct bch_devs_list devs)
1418 struct bch_replicas_cpu_entry search;
1425 devlist_to_replicas(devs, data_type, &search, &max_dev);
1428 ret = replicas_has_entry(rcu_dereference(c->replicas),
1435 struct replicas_status __bch2_replicas_status(struct bch_fs *c,
1436 struct bch_devs_mask online_devs)
1438 struct bch_sb_field_members *mi;
1439 struct bch_replicas_cpu_entry *e;
1440 struct bch_replicas_cpu *r;
1441 unsigned i, dev, dev_slots, nr_online, nr_offline;
1442 struct replicas_status ret;
1444 memset(&ret, 0, sizeof(ret));
1446 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
1447 ret.replicas[i].nr_online = UINT_MAX;
1449 mi = bch2_sb_get_members(c->disk_sb);
1452 r = rcu_dereference(c->replicas);
1453 dev_slots = replicas_dev_slots(r);
1455 for_each_cpu_replicas_entry(r, e) {
1456 if (e->data_type >= ARRAY_SIZE(ret.replicas))
1457 panic("e %p data_type %u\n", e, e->data_type);
1459 nr_online = nr_offline = 0;
1461 for (dev = 0; dev < dev_slots; dev++) {
1462 if (!replicas_test_dev(e, dev))
1465 BUG_ON(!bch2_dev_exists(c->disk_sb, mi, dev));
1467 if (test_bit(dev, online_devs.d))
1473 ret.replicas[e->data_type].nr_online =
1474 min(ret.replicas[e->data_type].nr_online,
1477 ret.replicas[e->data_type].nr_offline =
1478 max(ret.replicas[e->data_type].nr_offline,
1487 struct replicas_status bch2_replicas_status(struct bch_fs *c)
1489 return __bch2_replicas_status(c, bch2_online_devs(c));
1492 bool bch2_have_enough_devs(struct bch_fs *c,
1493 struct replicas_status s,
1496 if ((s.replicas[BCH_DATA_JOURNAL].nr_offline ||
1497 s.replicas[BCH_DATA_BTREE].nr_offline) &&
1498 !(flags & BCH_FORCE_IF_METADATA_DEGRADED))
1501 if ((!s.replicas[BCH_DATA_JOURNAL].nr_online ||
1502 !s.replicas[BCH_DATA_BTREE].nr_online) &&
1503 !(flags & BCH_FORCE_IF_METADATA_LOST))
1506 if (s.replicas[BCH_DATA_USER].nr_offline &&
1507 !(flags & BCH_FORCE_IF_DATA_DEGRADED))
1510 if (!s.replicas[BCH_DATA_USER].nr_online &&
1511 !(flags & BCH_FORCE_IF_DATA_LOST))
1517 unsigned bch2_replicas_online(struct bch_fs *c, bool meta)
1519 struct replicas_status s = bch2_replicas_status(c);
1522 ? min(s.replicas[BCH_DATA_JOURNAL].nr_online,
1523 s.replicas[BCH_DATA_BTREE].nr_online)
1524 : s.replicas[BCH_DATA_USER].nr_online;
1527 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
1529 struct bch_replicas_cpu_entry *e;
1530 struct bch_replicas_cpu *r;
1534 r = rcu_dereference(c->replicas);
1536 if (ca->dev_idx >= replicas_dev_slots(r))
1539 for_each_cpu_replicas_entry(r, e)
1540 if (replicas_test_dev(e, ca->dev_idx))
1541 ret |= 1 << e->data_type;
1550 static const char *bch2_sb_validate_quota(struct bch_sb *sb,
1551 struct bch_sb_field *f)
1553 struct bch_sb_field_quota *q = field_to_type(f, quota);
1555 if (vstruct_bytes(&q->field) != sizeof(*q))
1556 return "invalid field quota: wrong size";