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