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