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