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