]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.c
New upstream snapshot
[bcachefs-tools-debian] / libbcachefs / super-io.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_update_interior.h"
5 #include "buckets.h"
6 #include "checksum.h"
7 #include "disk_groups.h"
8 #include "ec.h"
9 #include "error.h"
10 #include "io.h"
11 #include "journal.h"
12 #include "journal_io.h"
13 #include "journal_seq_blacklist.h"
14 #include "replicas.h"
15 #include "quota.h"
16 #include "super-io.h"
17 #include "super.h"
18 #include "vstructs.h"
19
20 #include <linux/backing-dev.h>
21 #include <linux/sort.h>
22
23 const char * const bch2_sb_fields[] = {
24 #define x(name, nr)     #name,
25         BCH_SB_FIELDS()
26 #undef x
27         NULL
28 };
29
30 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
31                                   struct printbuf *);
32
33 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
34                                       enum bch_sb_field_type type)
35 {
36         struct bch_sb_field *f;
37
38         /* XXX: need locking around superblock to access optional fields */
39
40         vstruct_for_each(sb, f)
41                 if (le32_to_cpu(f->type) == type)
42                         return f;
43         return NULL;
44 }
45
46 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
47                                                    struct bch_sb_field *f,
48                                                    unsigned u64s)
49 {
50         unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
51         unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
52
53         BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
54
55         if (!f && !u64s) {
56                 /* nothing to do: */
57         } else if (!f) {
58                 f = vstruct_last(sb->sb);
59                 memset(f, 0, sizeof(u64) * u64s);
60                 f->u64s = cpu_to_le32(u64s);
61                 f->type = 0;
62         } else {
63                 void *src, *dst;
64
65                 src = vstruct_end(f);
66
67                 if (u64s) {
68                         f->u64s = cpu_to_le32(u64s);
69                         dst = vstruct_end(f);
70                 } else {
71                         dst = f;
72                 }
73
74                 memmove(dst, src, vstruct_end(sb->sb) - src);
75
76                 if (dst > src)
77                         memset(src, 0, dst - src);
78         }
79
80         sb->sb->u64s = cpu_to_le32(sb_u64s);
81
82         return u64s ? f : NULL;
83 }
84
85 void bch2_sb_field_delete(struct bch_sb_handle *sb,
86                           enum bch_sb_field_type type)
87 {
88         struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
89
90         if (f)
91                 __bch2_sb_field_resize(sb, f, 0);
92 }
93
94 /* Superblock realloc/free: */
95
96 void bch2_free_super(struct bch_sb_handle *sb)
97 {
98         if (sb->bio)
99                 bio_put(sb->bio);
100         if (!IS_ERR_OR_NULL(sb->bdev))
101                 blkdev_put(sb->bdev, sb->mode);
102
103         kfree(sb->sb);
104         memset(sb, 0, sizeof(*sb));
105 }
106
107 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
108 {
109         size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
110         size_t new_buffer_size;
111         struct bch_sb *new_sb;
112         struct bio *bio;
113
114         if (sb->bdev)
115                 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
116
117         new_buffer_size = roundup_pow_of_two(new_bytes);
118
119         if (sb->sb && sb->buffer_size >= new_buffer_size)
120                 return 0;
121
122         if (sb->have_layout) {
123                 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
124
125                 if (new_bytes > max_bytes) {
126                         char buf[BDEVNAME_SIZE];
127
128                         pr_err("%s: superblock too big: want %zu but have %llu",
129                                bdevname(sb->bdev, buf), new_bytes, max_bytes);
130                         return -ENOSPC;
131                 }
132         }
133
134         if (sb->buffer_size >= new_buffer_size && sb->sb)
135                 return 0;
136
137         if (dynamic_fault("bcachefs:add:super_realloc"))
138                 return -ENOMEM;
139
140         if (sb->have_bio) {
141                 bio = bio_kmalloc(GFP_KERNEL,
142                         DIV_ROUND_UP(new_buffer_size, PAGE_SIZE));
143                 if (!bio)
144                         return -ENOMEM;
145
146                 if (sb->bio)
147                         bio_put(sb->bio);
148                 sb->bio = bio;
149         }
150
151         new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
152         if (!new_sb)
153                 return -ENOMEM;
154
155         sb->sb = new_sb;
156         sb->buffer_size = new_buffer_size;
157
158         return 0;
159 }
160
161 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb,
162                                           enum bch_sb_field_type type,
163                                           unsigned u64s)
164 {
165         struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
166         ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
167         ssize_t d = -old_u64s + u64s;
168
169         if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
170                 return NULL;
171
172         if (sb->fs_sb) {
173                 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
174                 struct bch_dev *ca;
175                 unsigned i;
176
177                 lockdep_assert_held(&c->sb_lock);
178
179                 /* XXX: we're not checking that offline device have enough space */
180
181                 for_each_online_member(ca, c, i) {
182                         struct bch_sb_handle *sb = &ca->disk_sb;
183
184                         if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) {
185                                 percpu_ref_put(&ca->ref);
186                                 return NULL;
187                         }
188                 }
189         }
190
191         f = bch2_sb_field_get(sb->sb, type);
192         f = __bch2_sb_field_resize(sb, f, u64s);
193         if (f)
194                 f->type = cpu_to_le32(type);
195         return f;
196 }
197
198 /* Superblock validate: */
199
200 static inline void __bch2_sb_layout_size_assert(void)
201 {
202         BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
203 }
204
205 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
206 {
207         u64 offset, prev_offset, max_sectors;
208         unsigned i;
209
210         if (uuid_le_cmp(layout->magic, BCACHE_MAGIC)) {
211                 pr_buf(out, "Not a bcachefs superblock layout");
212                 return -EINVAL;
213         }
214
215         if (layout->layout_type != 0) {
216                 pr_buf(out, "Invalid superblock layout type %u",
217                        layout->layout_type);
218                 return -EINVAL;
219         }
220
221         if (!layout->nr_superblocks) {
222                 pr_buf(out, "Invalid superblock layout: no superblocks");
223                 return -EINVAL;
224         }
225
226         if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
227                 pr_buf(out, "Invalid superblock layout: too many superblocks");
228                 return -EINVAL;
229         }
230
231         max_sectors = 1 << layout->sb_max_size_bits;
232
233         prev_offset = le64_to_cpu(layout->sb_offset[0]);
234
235         for (i = 1; i < layout->nr_superblocks; i++) {
236                 offset = le64_to_cpu(layout->sb_offset[i]);
237
238                 if (offset < prev_offset + max_sectors) {
239                         pr_buf(out, "Invalid superblock layout: superblocks overlap\n"
240                                "  (sb %u ends at %llu next starts at %llu",
241                                i - 1, prev_offset + max_sectors, offset);
242                         return -EINVAL;
243                 }
244                 prev_offset = offset;
245         }
246
247         return 0;
248 }
249
250 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out)
251 {
252         struct bch_sb *sb = disk_sb->sb;
253         struct bch_sb_field *f;
254         struct bch_sb_field_members *mi;
255         u32 version, version_min;
256         u16 block_size;
257         int ret;
258
259         version         = le16_to_cpu(sb->version);
260         version_min     = version >= bcachefs_metadata_version_new_versioning
261                 ? le16_to_cpu(sb->version_min)
262                 : version;
263
264         if (version    >= bcachefs_metadata_version_max) {
265                 pr_buf(out, "Unsupported superblock version %u (min %u, max %u)",
266                        version, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
267                 return -EINVAL;
268         }
269
270         if (version_min < bcachefs_metadata_version_min) {
271                 pr_buf(out, "Unsupported superblock version %u (min %u, max %u)",
272                        version_min, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
273                 return -EINVAL;
274         }
275
276         if (version_min > version) {
277                 pr_buf(out, "Bad minimum version %u, greater than version field %u",
278                        version_min, version);
279                 return -EINVAL;
280         }
281
282         if (sb->features[1] ||
283             (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) {
284                 pr_buf(out, "Filesystem has incompatible features");
285                 return -EINVAL;
286         }
287
288         block_size = le16_to_cpu(sb->block_size);
289
290         if (block_size > PAGE_SECTORS) {
291                 pr_buf(out, "Block size too big (got %u, max %u)",
292                        block_size, PAGE_SECTORS);
293                 return -EINVAL;
294         }
295
296         if (bch2_is_zero(sb->user_uuid.b, sizeof(uuid_le))) {
297                 pr_buf(out, "Bad user UUID (got zeroes)");
298                 return -EINVAL;
299         }
300
301         if (bch2_is_zero(sb->uuid.b, sizeof(uuid_le))) {
302                 pr_buf(out, "Bad intenal UUID (got zeroes)");
303                 return -EINVAL;
304         }
305
306         if (!sb->nr_devices ||
307             sb->nr_devices > BCH_SB_MEMBERS_MAX) {
308                 pr_buf(out, "Bad number of member devices %u (max %u)",
309                        sb->nr_devices, BCH_SB_MEMBERS_MAX);
310                 return -EINVAL;
311         }
312
313         if (sb->dev_idx >= sb->nr_devices) {
314                 pr_buf(out, "Bad dev_idx (got %u, nr_devices %u)",
315                        sb->dev_idx, sb->nr_devices);
316                 return -EINVAL;
317         }
318
319         if (!sb->time_precision ||
320             le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
321                 pr_buf(out, "Invalid time precision: %u (min 1, max %lu)",
322                        le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
323                 return -EINVAL;
324         }
325
326         /* validate layout */
327         ret = validate_sb_layout(&sb->layout, out);
328         if (ret)
329                 return ret;
330
331         vstruct_for_each(sb, f) {
332                 if (!f->u64s) {
333                         pr_buf(out, "Invalid superblock: optional with size 0 (type %u)",
334                                le32_to_cpu(f->type));
335                         return -EINVAL;
336                 }
337
338                 if (vstruct_next(f) > vstruct_last(sb)) {
339                         pr_buf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
340                                le32_to_cpu(f->type));
341                         return -EINVAL;
342                 }
343         }
344
345         /* members must be validated first: */
346         mi = bch2_sb_get_members(sb);
347         if (!mi) {
348                 pr_buf(out, "Invalid superblock: member info area missing");
349                 return -EINVAL;
350         }
351
352         ret = bch2_sb_field_validate(sb, &mi->field, out);
353         if (ret)
354                 return ret;
355
356         vstruct_for_each(sb, f) {
357                 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members)
358                         continue;
359
360                 ret = bch2_sb_field_validate(sb, f, out);
361                 if (ret)
362                         return ret;
363         }
364
365         return 0;
366 }
367
368 /* device open: */
369
370 static void bch2_sb_update(struct bch_fs *c)
371 {
372         struct bch_sb *src = c->disk_sb.sb;
373         struct bch_sb_field_members *mi = bch2_sb_get_members(src);
374         struct bch_dev *ca;
375         unsigned i;
376
377         lockdep_assert_held(&c->sb_lock);
378
379         c->sb.uuid              = src->uuid;
380         c->sb.user_uuid         = src->user_uuid;
381         c->sb.version           = le16_to_cpu(src->version);
382         c->sb.version_min       = le16_to_cpu(src->version_min);
383         c->sb.nr_devices        = src->nr_devices;
384         c->sb.clean             = BCH_SB_CLEAN(src);
385         c->sb.encryption_type   = BCH_SB_ENCRYPTION_TYPE(src);
386
387         c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
388         c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
389
390         /* XXX this is wrong, we need a 96 or 128 bit integer type */
391         c->sb.time_base_lo      = div_u64(le64_to_cpu(src->time_base_lo),
392                                           c->sb.nsec_per_time_unit);
393         c->sb.time_base_hi      = le32_to_cpu(src->time_base_hi);
394
395         c->sb.features          = le64_to_cpu(src->features[0]);
396         c->sb.compat            = le64_to_cpu(src->compat[0]);
397
398         for_each_member_device(ca, c, i)
399                 ca->mi = bch2_mi_to_cpu(mi->members + i);
400 }
401
402 static void __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
403 {
404         struct bch_sb_field *src_f, *dst_f;
405         struct bch_sb *dst = dst_handle->sb;
406         unsigned i;
407
408         dst->version            = src->version;
409         dst->version_min        = src->version_min;
410         dst->seq                = src->seq;
411         dst->uuid               = src->uuid;
412         dst->user_uuid          = src->user_uuid;
413         memcpy(dst->label,      src->label, sizeof(dst->label));
414
415         dst->block_size         = src->block_size;
416         dst->nr_devices         = src->nr_devices;
417
418         dst->time_base_lo       = src->time_base_lo;
419         dst->time_base_hi       = src->time_base_hi;
420         dst->time_precision     = src->time_precision;
421
422         memcpy(dst->flags,      src->flags,     sizeof(dst->flags));
423         memcpy(dst->features,   src->features,  sizeof(dst->features));
424         memcpy(dst->compat,     src->compat,    sizeof(dst->compat));
425
426         for (i = 0; i < BCH_SB_FIELD_NR; i++) {
427                 if (i == BCH_SB_FIELD_journal)
428                         continue;
429
430                 src_f = bch2_sb_field_get(src, i);
431                 dst_f = bch2_sb_field_get(dst, i);
432                 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
433                                 src_f ? le32_to_cpu(src_f->u64s) : 0);
434
435                 if (src_f)
436                         memcpy(dst_f, src_f, vstruct_bytes(src_f));
437         }
438 }
439
440 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
441 {
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)
446                 : 0;
447         int ret;
448
449         lockdep_assert_held(&c->sb_lock);
450
451         ret = bch2_sb_realloc(&c->disk_sb,
452                               le32_to_cpu(src->u64s) - journal_u64s);
453         if (ret)
454                 return ret;
455
456         __copy_super(&c->disk_sb, src);
457
458         if (BCH_SB_INITIALIZED(c->disk_sb.sb))
459                 set_bit(BCH_FS_INITIALIZED, &c->flags);
460
461         ret = bch2_sb_replicas_to_cpu_replicas(c);
462         if (ret)
463                 return ret;
464
465         ret = bch2_sb_disk_groups_to_cpu(c);
466         if (ret)
467                 return ret;
468
469         bch2_sb_update(c);
470         return 0;
471 }
472
473 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
474 {
475         struct bch_sb *src = c->disk_sb.sb, *dst = ca->disk_sb.sb;
476         struct bch_sb_field_journal *journal_buckets =
477                 bch2_sb_get_journal(dst);
478         unsigned journal_u64s = journal_buckets
479                 ? le32_to_cpu(journal_buckets->field.u64s)
480                 : 0;
481         unsigned u64s = le32_to_cpu(src->u64s) + journal_u64s;
482         int ret;
483
484         ret = bch2_sb_realloc(&ca->disk_sb, u64s);
485         if (ret)
486                 return ret;
487
488         __copy_super(&ca->disk_sb, src);
489         return 0;
490 }
491
492 /* read superblock: */
493
494 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
495 {
496         struct bch_csum csum;
497         u32 version, version_min;
498         size_t bytes;
499         int ret;
500 reread:
501         bio_reset(sb->bio);
502         bio_set_dev(sb->bio, sb->bdev);
503         sb->bio->bi_iter.bi_sector = offset;
504         bio_set_op_attrs(sb->bio, REQ_OP_READ, REQ_SYNC|REQ_META);
505         bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
506
507         ret = submit_bio_wait(sb->bio);
508         if (ret) {
509                 pr_buf(err, "IO error: %i", ret);
510                 return ret;
511         }
512
513         if (uuid_le_cmp(sb->sb->magic, BCACHE_MAGIC)) {
514                 pr_buf(err, "Not a bcachefs superblock");
515                 return -EINVAL;
516         }
517
518         version         = le16_to_cpu(sb->sb->version);
519         version_min     = version >= bcachefs_metadata_version_new_versioning
520                 ? le16_to_cpu(sb->sb->version_min)
521                 : version;
522
523         if (version    >= bcachefs_metadata_version_max) {
524                 pr_buf(err, "Unsupported superblock version %u (min %u, max %u)",
525                        version, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
526                 return -EINVAL;
527         }
528
529         if (version_min < bcachefs_metadata_version_min) {
530                 pr_buf(err, "Unsupported superblock version %u (min %u, max %u)",
531                        version_min, bcachefs_metadata_version_min, bcachefs_metadata_version_max);
532                 return -EINVAL;
533         }
534
535         bytes = vstruct_bytes(sb->sb);
536
537         if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
538                 pr_buf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
539                        bytes, 512UL << sb->sb->layout.sb_max_size_bits);
540                 return -EINVAL;
541         }
542
543         if (bytes > sb->buffer_size) {
544                 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s)))
545                         return -ENOMEM;
546                 goto reread;
547         }
548
549         if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
550                 pr_buf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
551                 return -EINVAL;
552         }
553
554         /* XXX: verify MACs */
555         csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
556                             null_nonce(), sb->sb);
557
558         if (bch2_crc_cmp(csum, sb->sb->csum)) {
559                 pr_buf(err, "bad checksum");
560                 return -EINVAL;
561         }
562
563         sb->seq = le64_to_cpu(sb->sb->seq);
564
565         return 0;
566 }
567
568 int bch2_read_super(const char *path, struct bch_opts *opts,
569                     struct bch_sb_handle *sb)
570 {
571         u64 offset = opt_get(*opts, sb);
572         struct bch_sb_layout layout;
573         char *_err;
574         struct printbuf err;
575         __le64 *i;
576         int ret;
577
578         _err = kmalloc(4096, GFP_KERNEL);
579         if (!_err)
580                 return -ENOMEM;
581         err = _PBUF(_err, 4096);
582
583         pr_verbose_init(*opts, "");
584
585         memset(sb, 0, sizeof(*sb));
586         sb->mode        = FMODE_READ;
587         sb->have_bio    = true;
588
589         if (!opt_get(*opts, noexcl))
590                 sb->mode |= FMODE_EXCL;
591
592         if (!opt_get(*opts, nochanges))
593                 sb->mode |= FMODE_WRITE;
594
595         sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
596         if (IS_ERR(sb->bdev) &&
597             PTR_ERR(sb->bdev) == -EACCES &&
598             opt_get(*opts, read_only)) {
599                 sb->mode &= ~FMODE_WRITE;
600
601                 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
602                 if (!IS_ERR(sb->bdev))
603                         opt_set(*opts, nochanges, true);
604         }
605
606         if (IS_ERR(sb->bdev)) {
607                 ret = PTR_ERR(sb->bdev);
608                 goto out;
609         }
610
611         ret = bch2_sb_realloc(sb, 0);
612         if (ret) {
613                 pr_buf(&err, "error allocating memory for superblock");
614                 goto err;
615         }
616
617         if (bch2_fs_init_fault("read_super")) {
618                 pr_buf(&err, "dynamic fault");
619                 ret = -EFAULT;
620                 goto err;
621         }
622
623         ret = read_one_super(sb, offset, &err);
624         if (!ret)
625                 goto got_super;
626
627         if (opt_defined(*opts, sb))
628                 goto err;
629
630         printk(KERN_ERR "bcachefs (%s): error reading default superblock: %s",
631                path, _err);
632         err = _PBUF(_err, 4096);
633
634         /*
635          * Error reading primary superblock - read location of backup
636          * superblocks:
637          */
638         bio_reset(sb->bio);
639         bio_set_dev(sb->bio, sb->bdev);
640         sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
641         bio_set_op_attrs(sb->bio, REQ_OP_READ, REQ_SYNC|REQ_META);
642         /*
643          * use sb buffer to read layout, since sb buffer is page aligned but
644          * layout won't be:
645          */
646         bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
647
648         ret = submit_bio_wait(sb->bio);
649         if (ret) {
650                 pr_buf(&err, "IO error: %i", ret);
651                 goto err;
652         }
653
654         memcpy(&layout, sb->sb, sizeof(layout));
655         ret = validate_sb_layout(&layout, &err);
656         if (ret)
657                 goto err;
658
659         for (i = layout.sb_offset;
660              i < layout.sb_offset + layout.nr_superblocks; i++) {
661                 offset = le64_to_cpu(*i);
662
663                 if (offset == opt_get(*opts, sb))
664                         continue;
665
666                 ret = read_one_super(sb, offset, &err);
667                 if (!ret)
668                         goto got_super;
669         }
670
671         goto err;
672
673 got_super:
674         if (le16_to_cpu(sb->sb->block_size) << 9 <
675             bdev_logical_block_size(sb->bdev)) {
676                 pr_buf(&err, "block size (%u) smaller than device block size (%u)",
677                        le16_to_cpu(sb->sb->block_size) << 9,
678                        bdev_logical_block_size(sb->bdev));
679                 ret = -EINVAL;
680                 goto err;
681         }
682
683         ret = 0;
684         sb->have_layout = true;
685
686         ret = bch2_sb_validate(sb, &err);
687         if (ret) {
688                 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s",
689                        path, _err);
690                 goto err_no_print;
691         }
692 out:
693         pr_verbose_init(*opts, "ret %i", ret);
694         kfree(_err);
695         return ret;
696 err:
697         printk(KERN_ERR "bcachefs (%s): error reading superblock: %s",
698                path, _err);
699 err_no_print:
700         bch2_free_super(sb);
701         goto out;
702 }
703
704 /* write superblock: */
705
706 static void write_super_endio(struct bio *bio)
707 {
708         struct bch_dev *ca = bio->bi_private;
709
710         /* XXX: return errors directly */
711
712         if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write error: %s",
713                                bch2_blk_status_to_str(bio->bi_status)))
714                 ca->sb_write_error = 1;
715
716         closure_put(&ca->fs->sb_write);
717         percpu_ref_put(&ca->io_ref);
718 }
719
720 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
721 {
722         struct bch_sb *sb = ca->disk_sb.sb;
723         struct bio *bio = ca->disk_sb.bio;
724
725         bio_reset(bio);
726         bio_set_dev(bio, ca->disk_sb.bdev);
727         bio->bi_iter.bi_sector  = le64_to_cpu(sb->layout.sb_offset[0]);
728         bio->bi_end_io          = write_super_endio;
729         bio->bi_private         = ca;
730         bio_set_op_attrs(bio, REQ_OP_READ, REQ_SYNC|REQ_META);
731         bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
732
733         this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
734                      bio_sectors(bio));
735
736         percpu_ref_get(&ca->io_ref);
737         closure_bio_submit(bio, &c->sb_write);
738 }
739
740 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
741 {
742         struct bch_sb *sb = ca->disk_sb.sb;
743         struct bio *bio = ca->disk_sb.bio;
744
745         sb->offset = sb->layout.sb_offset[idx];
746
747         SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
748         sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
749                                 null_nonce(), sb);
750
751         bio_reset(bio);
752         bio_set_dev(bio, ca->disk_sb.bdev);
753         bio->bi_iter.bi_sector  = le64_to_cpu(sb->offset);
754         bio->bi_end_io          = write_super_endio;
755         bio->bi_private         = ca;
756         bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC|REQ_META);
757         bch2_bio_map(bio, sb,
758                      roundup((size_t) vstruct_bytes(sb),
759                              bdev_logical_block_size(ca->disk_sb.bdev)));
760
761         this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
762                      bio_sectors(bio));
763
764         percpu_ref_get(&ca->io_ref);
765         closure_bio_submit(bio, &c->sb_write);
766 }
767
768 int bch2_write_super(struct bch_fs *c)
769 {
770         struct closure *cl = &c->sb_write;
771         struct bch_dev *ca;
772         unsigned i, sb = 0, nr_wrote;
773         struct bch_devs_mask sb_written;
774         bool wrote, can_mount_without_written, can_mount_with_written;
775         unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
776         int ret = 0;
777
778         if (c->opts.very_degraded)
779                 degraded_flags |= BCH_FORCE_IF_LOST;
780
781         lockdep_assert_held(&c->sb_lock);
782
783         closure_init_stack(cl);
784         memset(&sb_written, 0, sizeof(sb_written));
785
786         le64_add_cpu(&c->disk_sb.sb->seq, 1);
787
788         if (test_bit(BCH_FS_ERROR, &c->flags))
789                 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
790         if (test_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags))
791                 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
792
793         SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
794
795         for_each_online_member(ca, c, i)
796                 bch2_sb_from_fs(c, ca);
797
798         for_each_online_member(ca, c, i) {
799                 struct printbuf buf = { NULL, NULL };
800
801                 ret = bch2_sb_validate(&ca->disk_sb, &buf);
802                 if (ret) {
803                         char *_buf = kmalloc(4096, GFP_NOFS);
804                         if (_buf) {
805                                 buf = _PBUF(_buf, 4096);
806                                 bch2_sb_validate(&ca->disk_sb, &buf);
807                         }
808
809                         bch2_fs_inconsistent(c, "sb invalid before write: %s", _buf);
810                         kfree(_buf);
811                         percpu_ref_put(&ca->io_ref);
812                         goto out;
813                 }
814         }
815
816         if (c->opts.nochanges)
817                 goto out;
818
819         for_each_online_member(ca, c, i) {
820                 __set_bit(ca->dev_idx, sb_written.d);
821                 ca->sb_write_error = 0;
822         }
823
824         for_each_online_member(ca, c, i)
825                 read_back_super(c, ca);
826         closure_sync(cl);
827
828         for_each_online_member(ca, c, i) {
829                 if (ca->sb_write_error)
830                         continue;
831
832                 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
833                         bch2_fs_fatal_error(c,
834                                 "Superblock write was silently dropped! (seq %llu expected %llu)",
835                                 le64_to_cpu(ca->sb_read_scratch->seq),
836                                 ca->disk_sb.seq);
837                         percpu_ref_put(&ca->io_ref);
838                         ret = -EROFS;
839                         goto out;
840                 }
841
842                 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
843                         bch2_fs_fatal_error(c,
844                                 "Superblock modified by another process (seq %llu expected %llu)",
845                                 le64_to_cpu(ca->sb_read_scratch->seq),
846                                 ca->disk_sb.seq);
847                         percpu_ref_put(&ca->io_ref);
848                         ret = -EROFS;
849                         goto out;
850                 }
851         }
852
853         do {
854                 wrote = false;
855                 for_each_online_member(ca, c, i)
856                         if (!ca->sb_write_error &&
857                             sb < ca->disk_sb.sb->layout.nr_superblocks) {
858                                 write_one_super(c, ca, sb);
859                                 wrote = true;
860                         }
861                 closure_sync(cl);
862                 sb++;
863         } while (wrote);
864
865         for_each_online_member(ca, c, i) {
866                 if (ca->sb_write_error)
867                         __clear_bit(ca->dev_idx, sb_written.d);
868                 else
869                         ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
870         }
871
872         nr_wrote = dev_mask_nr(&sb_written);
873
874         can_mount_with_written =
875                 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
876
877         for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
878                 sb_written.d[i] = ~sb_written.d[i];
879
880         can_mount_without_written =
881                 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
882
883         /*
884          * If we would be able to mount _without_ the devices we successfully
885          * wrote superblocks to, we weren't able to write to enough devices:
886          *
887          * Exception: if we can mount without the successes because we haven't
888          * written anything (new filesystem), we continue if we'd be able to
889          * mount with the devices we did successfully write to:
890          */
891         if (bch2_fs_fatal_err_on(!nr_wrote ||
892                                  !can_mount_with_written ||
893                                  (can_mount_without_written &&
894                                   !can_mount_with_written), c,
895                 "Unable to write superblock to sufficient devices (from %ps)",
896                 (void *) _RET_IP_))
897                 ret = -1;
898 out:
899         /* Make new options visible after they're persistent: */
900         bch2_sb_update(c);
901         return ret;
902 }
903
904 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
905 {
906         mutex_lock(&c->sb_lock);
907         if (!(c->sb.features & (1ULL << feat))) {
908                 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
909
910                 bch2_write_super(c);
911         }
912         mutex_unlock(&c->sb_lock);
913 }
914
915 /* BCH_SB_FIELD_journal: */
916
917 static int u64_cmp(const void *_l, const void *_r)
918 {
919         u64 l = *((const u64 *) _l), r = *((const u64 *) _r);
920
921         return l < r ? -1 : l > r ? 1 : 0;
922 }
923
924 static int bch2_sb_validate_journal(struct bch_sb *sb,
925                                     struct bch_sb_field *f,
926                                     struct printbuf *err)
927 {
928         struct bch_sb_field_journal *journal = field_to_type(f, journal);
929         struct bch_member *m = bch2_sb_get_members(sb)->members + sb->dev_idx;
930         int ret = -EINVAL;
931         unsigned nr;
932         unsigned i;
933         u64 *b;
934
935         nr = bch2_nr_journal_buckets(journal);
936         if (!nr)
937                 return 0;
938
939         b = kmalloc_array(sizeof(u64), nr, GFP_KERNEL);
940         if (!b)
941                 return -ENOMEM;
942
943         for (i = 0; i < nr; i++)
944                 b[i] = le64_to_cpu(journal->buckets[i]);
945
946         sort(b, nr, sizeof(u64), u64_cmp, NULL);
947
948         if (!b[0]) {
949                 pr_buf(err, "journal bucket at sector 0");
950                 goto err;
951         }
952
953         if (b[0] < le16_to_cpu(m->first_bucket)) {
954                 pr_buf(err, "journal bucket %llu before first bucket %u",
955                        b[0], le16_to_cpu(m->first_bucket));
956                 goto err;
957         }
958
959         if (b[nr - 1] >= le64_to_cpu(m->nbuckets)) {
960                 pr_buf(err, "journal bucket %llu past end of device (nbuckets %llu)",
961                        b[nr - 1], le64_to_cpu(m->nbuckets));
962                 goto err;
963         }
964
965         for (i = 0; i + 1 < nr; i++)
966                 if (b[i] == b[i + 1]) {
967                         pr_buf(err, "duplicate journal buckets %llu", b[i]);
968                         goto err;
969                 }
970
971         ret = 0;
972 err:
973         kfree(b);
974         return ret;
975 }
976
977 static const struct bch_sb_field_ops bch_sb_field_ops_journal = {
978         .validate       = bch2_sb_validate_journal,
979 };
980
981 /* BCH_SB_FIELD_members: */
982
983 static int bch2_sb_validate_members(struct bch_sb *sb,
984                                     struct bch_sb_field *f,
985                                     struct printbuf *err)
986 {
987         struct bch_sb_field_members *mi = field_to_type(f, members);
988         unsigned i;
989
990         if ((void *) (mi->members + sb->nr_devices) >
991             vstruct_end(&mi->field)) {
992                 pr_buf(err, "too many devices for section size");
993                 return -EINVAL;
994         }
995
996         for (i = 0; i < sb->nr_devices; i++) {
997                 struct bch_member *m = mi->members + i;
998
999                 if (!bch2_member_exists(m))
1000                         continue;
1001
1002                 if (le64_to_cpu(m->nbuckets) > LONG_MAX) {
1003                         pr_buf(err, "device %u: too many buckets (got %llu, max %lu)",
1004                                i, le64_to_cpu(m->nbuckets), LONG_MAX);
1005                         return -EINVAL;
1006                 }
1007
1008                 if (le64_to_cpu(m->nbuckets) -
1009                     le16_to_cpu(m->first_bucket) < BCH_MIN_NR_NBUCKETS) {
1010                         pr_buf(err, "device %u: not enough buckets (got %llu, max %u)",
1011                                i, le64_to_cpu(m->nbuckets), BCH_MIN_NR_NBUCKETS);
1012                         return -EINVAL;
1013                 }
1014
1015                 if (le16_to_cpu(m->bucket_size) <
1016                     le16_to_cpu(sb->block_size)) {
1017                         pr_buf(err, "device %u: bucket size %u smaller than block size %u",
1018                                i, le16_to_cpu(m->bucket_size), le16_to_cpu(sb->block_size));
1019                         return -EINVAL;
1020                 }
1021
1022                 if (le16_to_cpu(m->bucket_size) <
1023                     BCH_SB_BTREE_NODE_SIZE(sb)) {
1024                         pr_buf(err, "device %u: bucket size %u smaller than btree node size %llu",
1025                                i, le16_to_cpu(m->bucket_size), BCH_SB_BTREE_NODE_SIZE(sb));
1026                         return -EINVAL;
1027                 }
1028         }
1029
1030         return 0;
1031 }
1032
1033 static const struct bch_sb_field_ops bch_sb_field_ops_members = {
1034         .validate       = bch2_sb_validate_members,
1035 };
1036
1037 /* BCH_SB_FIELD_crypt: */
1038
1039 static int bch2_sb_validate_crypt(struct bch_sb *sb,
1040                                   struct bch_sb_field *f,
1041                                   struct printbuf *err)
1042 {
1043         struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1044
1045         if (vstruct_bytes(&crypt->field) < sizeof(*crypt)) {
1046                 pr_buf(err, "wrong size (got %llu should be %zu)",
1047                        vstruct_bytes(&crypt->field), sizeof(*crypt));
1048                 return -EINVAL;
1049         }
1050
1051         if (BCH_CRYPT_KDF_TYPE(crypt)) {
1052                 pr_buf(err, "bad kdf type %llu", BCH_CRYPT_KDF_TYPE(crypt));
1053                 return -EINVAL;
1054         }
1055
1056         return 0;
1057 }
1058
1059 static const struct bch_sb_field_ops bch_sb_field_ops_crypt = {
1060         .validate       = bch2_sb_validate_crypt,
1061 };
1062
1063 /* BCH_SB_FIELD_clean: */
1064
1065 int bch2_sb_clean_validate(struct bch_fs *c, struct bch_sb_field_clean *clean, int write)
1066 {
1067         struct jset_entry *entry;
1068         int ret;
1069
1070         for (entry = clean->start;
1071              entry < (struct jset_entry *) vstruct_end(&clean->field);
1072              entry = vstruct_next(entry)) {
1073                 ret = bch2_journal_entry_validate(c, "superblock", entry,
1074                                                   le16_to_cpu(c->disk_sb.sb->version),
1075                                                   BCH_SB_BIG_ENDIAN(c->disk_sb.sb),
1076                                                   write);
1077                 if (ret)
1078                         return ret;
1079         }
1080
1081         return 0;
1082 }
1083
1084 int bch2_fs_mark_dirty(struct bch_fs *c)
1085 {
1086         int ret;
1087
1088         /*
1089          * Unconditionally write superblock, to verify it hasn't changed before
1090          * we go rw:
1091          */
1092
1093         mutex_lock(&c->sb_lock);
1094         SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1095         c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS);
1096         c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1097         ret = bch2_write_super(c);
1098         mutex_unlock(&c->sb_lock);
1099
1100         return ret;
1101 }
1102
1103 static struct jset_entry *jset_entry_init(struct jset_entry **end, size_t size)
1104 {
1105         struct jset_entry *entry = *end;
1106         unsigned u64s = DIV_ROUND_UP(size, sizeof(u64));
1107
1108         memset(entry, 0, u64s * sizeof(u64));
1109         /*
1110          * The u64s field counts from the start of data, ignoring the shared
1111          * fields.
1112          */
1113         entry->u64s = cpu_to_le16(u64s - 1);
1114
1115         *end = vstruct_next(*end);
1116         return entry;
1117 }
1118
1119 void bch2_journal_super_entries_add_common(struct bch_fs *c,
1120                                            struct jset_entry **end,
1121                                            u64 journal_seq)
1122 {
1123         struct bch_dev *ca;
1124         unsigned i, dev;
1125
1126         percpu_down_read(&c->mark_lock);
1127
1128         if (!journal_seq) {
1129                 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
1130                         bch2_fs_usage_acc_to_base(c, i);
1131         } else {
1132                 bch2_fs_usage_acc_to_base(c, journal_seq & JOURNAL_BUF_MASK);
1133         }
1134
1135         {
1136                 struct jset_entry_usage *u =
1137                         container_of(jset_entry_init(end, sizeof(*u)),
1138                                      struct jset_entry_usage, entry);
1139
1140                 u->entry.type   = BCH_JSET_ENTRY_usage;
1141                 u->entry.btree_id = BCH_FS_USAGE_inodes;
1142                 u->v            = cpu_to_le64(c->usage_base->nr_inodes);
1143         }
1144
1145         {
1146                 struct jset_entry_usage *u =
1147                         container_of(jset_entry_init(end, sizeof(*u)),
1148                                      struct jset_entry_usage, entry);
1149
1150                 u->entry.type   = BCH_JSET_ENTRY_usage;
1151                 u->entry.btree_id = BCH_FS_USAGE_key_version;
1152                 u->v            = cpu_to_le64(atomic64_read(&c->key_version));
1153         }
1154
1155         for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1156                 struct jset_entry_usage *u =
1157                         container_of(jset_entry_init(end, sizeof(*u)),
1158                                      struct jset_entry_usage, entry);
1159
1160                 u->entry.type   = BCH_JSET_ENTRY_usage;
1161                 u->entry.btree_id = BCH_FS_USAGE_reserved;
1162                 u->entry.level  = i;
1163                 u->v            = cpu_to_le64(c->usage_base->persistent_reserved[i]);
1164         }
1165
1166         for (i = 0; i < c->replicas.nr; i++) {
1167                 struct bch_replicas_entry *e =
1168                         cpu_replicas_entry(&c->replicas, i);
1169                 struct jset_entry_data_usage *u =
1170                         container_of(jset_entry_init(end, sizeof(*u) + e->nr_devs),
1171                                      struct jset_entry_data_usage, entry);
1172
1173                 u->entry.type   = BCH_JSET_ENTRY_data_usage;
1174                 u->v            = cpu_to_le64(c->usage_base->replicas[i]);
1175                 memcpy(&u->r, e, replicas_entry_bytes(e));
1176         }
1177
1178         for_each_member_device(ca, c, dev) {
1179                 unsigned b = sizeof(struct jset_entry_dev_usage) +
1180                         sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR;
1181                 struct jset_entry_dev_usage *u =
1182                         container_of(jset_entry_init(end, b),
1183                                      struct jset_entry_dev_usage, entry);
1184
1185                 u->entry.type = BCH_JSET_ENTRY_dev_usage;
1186                 u->dev = cpu_to_le32(dev);
1187                 u->buckets_ec           = cpu_to_le64(ca->usage_base->buckets_ec);
1188                 u->buckets_unavailable  = cpu_to_le64(ca->usage_base->buckets_unavailable);
1189
1190                 for (i = 0; i < BCH_DATA_NR; i++) {
1191                         u->d[i].buckets = cpu_to_le64(ca->usage_base->d[i].buckets);
1192                         u->d[i].sectors = cpu_to_le64(ca->usage_base->d[i].sectors);
1193                         u->d[i].fragmented = cpu_to_le64(ca->usage_base->d[i].fragmented);
1194                 }
1195         }
1196
1197         percpu_up_read(&c->mark_lock);
1198
1199         for (i = 0; i < 2; i++) {
1200                 struct jset_entry_clock *clock =
1201                         container_of(jset_entry_init(end, sizeof(*clock)),
1202                                      struct jset_entry_clock, entry);
1203
1204                 clock->entry.type = BCH_JSET_ENTRY_clock;
1205                 clock->rw       = i;
1206                 clock->time     = cpu_to_le64(atomic64_read(&c->io_clock[i].now));
1207         }
1208 }
1209
1210 void bch2_fs_mark_clean(struct bch_fs *c)
1211 {
1212         struct bch_sb_field_clean *sb_clean;
1213         struct jset_entry *entry;
1214         unsigned u64s;
1215         int ret;
1216
1217         mutex_lock(&c->sb_lock);
1218         if (BCH_SB_CLEAN(c->disk_sb.sb))
1219                 goto out;
1220
1221         SET_BCH_SB_CLEAN(c->disk_sb.sb, true);
1222
1223         c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
1224         c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_metadata);
1225         c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_extents_above_btree_updates));
1226         c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_btree_updates_journalled));
1227
1228         u64s = sizeof(*sb_clean) / sizeof(u64) + c->journal.entry_u64s_reserved;
1229
1230         sb_clean = bch2_sb_resize_clean(&c->disk_sb, u64s);
1231         if (!sb_clean) {
1232                 bch_err(c, "error resizing superblock while setting filesystem clean");
1233                 goto out;
1234         }
1235
1236         sb_clean->flags         = 0;
1237         sb_clean->journal_seq   = cpu_to_le64(journal_cur_seq(&c->journal) - 1);
1238
1239         /* Trying to catch outstanding bug: */
1240         BUG_ON(le64_to_cpu(sb_clean->journal_seq) > S64_MAX);
1241
1242         entry = sb_clean->start;
1243         bch2_journal_super_entries_add_common(c, &entry, 0);
1244         entry = bch2_btree_roots_to_journal_entries(c, entry, entry);
1245         BUG_ON((void *) entry > vstruct_end(&sb_clean->field));
1246
1247         memset(entry, 0,
1248                vstruct_end(&sb_clean->field) - (void *) entry);
1249
1250         /*
1251          * this should be in the write path, and we should be validating every
1252          * superblock section:
1253          */
1254         ret = bch2_sb_clean_validate(c, sb_clean, WRITE);
1255         if (ret) {
1256                 bch_err(c, "error writing marking filesystem clean: validate error");
1257                 goto out;
1258         }
1259
1260         bch2_write_super(c);
1261 out:
1262         mutex_unlock(&c->sb_lock);
1263 }
1264
1265 static int bch2_sb_validate_clean(struct bch_sb *sb,
1266                                   struct bch_sb_field *f,
1267                                   struct printbuf *err)
1268 {
1269         struct bch_sb_field_clean *clean = field_to_type(f, clean);
1270
1271         if (vstruct_bytes(&clean->field) < sizeof(*clean)) {
1272                 pr_buf(err, "wrong size (got %llu should be %zu)",
1273                        vstruct_bytes(&clean->field), sizeof(*clean));
1274                 return -EINVAL;
1275         }
1276
1277         return 0;
1278 }
1279
1280 static const struct bch_sb_field_ops bch_sb_field_ops_clean = {
1281         .validate       = bch2_sb_validate_clean,
1282 };
1283
1284 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1285 #define x(f, nr)                                        \
1286         [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1287         BCH_SB_FIELDS()
1288 #undef x
1289 };
1290
1291 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1292                                   struct printbuf *orig_err)
1293 {
1294         unsigned type = le32_to_cpu(f->type);
1295         struct printbuf err = *orig_err;
1296         int ret;
1297
1298         if (type >= BCH_SB_FIELD_NR)
1299                 return 0;
1300
1301         pr_buf(&err, "Invalid superblock section %s: ", bch2_sb_fields[type]);
1302
1303         ret = bch2_sb_field_ops[type]->validate(sb, f, &err);
1304         if (ret) {
1305                 pr_buf(&err, "\n");
1306                 bch2_sb_field_to_text(&err, sb, f);
1307                 *orig_err = err;
1308         }
1309
1310         return ret;
1311 }
1312
1313 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1314                            struct bch_sb_field *f)
1315 {
1316         unsigned type = le32_to_cpu(f->type);
1317         const struct bch_sb_field_ops *ops = type < BCH_SB_FIELD_NR
1318                 ? bch2_sb_field_ops[type] : NULL;
1319
1320         if (ops)
1321                 pr_buf(out, "%s", bch2_sb_fields[type]);
1322         else
1323                 pr_buf(out, "(unknown field %u)", type);
1324
1325         pr_buf(out, " (size %llu):", vstruct_bytes(f));
1326
1327         if (ops && ops->to_text)
1328                 bch2_sb_field_ops[type]->to_text(out, sb, f);
1329 }