]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.c
Update bcachefs sources to 1e3ca87f7b bcachefs: bcachefs_metadata_version_major_minor
[bcachefs-tools-debian] / libbcachefs / super-io.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_update_interior.h"
5 #include "buckets.h"
6 #include "checksum.h"
7 #include "disk_groups.h"
8 #include "ec.h"
9 #include "error.h"
10 #include "io.h"
11 #include "journal.h"
12 #include "journal_io.h"
13 #include "journal_sb.h"
14 #include "journal_seq_blacklist.h"
15 #include "replicas.h"
16 #include "quota.h"
17 #include "super-io.h"
18 #include "super.h"
19 #include "trace.h"
20 #include "vstructs.h"
21 #include "counters.h"
22
23 #include <linux/backing-dev.h>
24 #include <linux/sort.h>
25
26 struct bch2_metadata_version_str {
27         u16             version;
28         const char      *name;
29 };
30
31 static const struct bch2_metadata_version_str bch2_metadata_versions[] = {
32 #define x(n, v) { .version = v, .name = #n },
33         BCH_METADATA_VERSIONS()
34 #undef x
35 };
36
37 void bch2_version_to_text(struct printbuf *out, unsigned v)
38 {
39         const char *str = "(unknown version)";
40
41         for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
42                 if (bch2_metadata_versions[i].version == v) {
43                         str = bch2_metadata_versions[i].name;
44                         break;
45                 }
46
47         prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str);
48 }
49
50 unsigned bch2_latest_compatible_version(unsigned v)
51 {
52         if (!BCH_VERSION_MAJOR(v))
53                 return v;
54
55         for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
56                 if (bch2_metadata_versions[i].version > v &&
57                     BCH_VERSION_MAJOR(bch2_metadata_versions[i].version) ==
58                     BCH_VERSION_MAJOR(v))
59                         v = bch2_metadata_versions[i].version;
60
61         return v;
62 }
63
64 const char * const bch2_sb_fields[] = {
65 #define x(name, nr)     #name,
66         BCH_SB_FIELDS()
67 #undef x
68         NULL
69 };
70
71 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
72                                   struct printbuf *);
73
74 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
75                                       enum bch_sb_field_type type)
76 {
77         struct bch_sb_field *f;
78
79         /* XXX: need locking around superblock to access optional fields */
80
81         vstruct_for_each(sb, f)
82                 if (le32_to_cpu(f->type) == type)
83                         return f;
84         return NULL;
85 }
86
87 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
88                                                    struct bch_sb_field *f,
89                                                    unsigned u64s)
90 {
91         unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
92         unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
93
94         BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
95
96         if (!f && !u64s) {
97                 /* nothing to do: */
98         } else if (!f) {
99                 f = vstruct_last(sb->sb);
100                 memset(f, 0, sizeof(u64) * u64s);
101                 f->u64s = cpu_to_le32(u64s);
102                 f->type = 0;
103         } else {
104                 void *src, *dst;
105
106                 src = vstruct_end(f);
107
108                 if (u64s) {
109                         f->u64s = cpu_to_le32(u64s);
110                         dst = vstruct_end(f);
111                 } else {
112                         dst = f;
113                 }
114
115                 memmove(dst, src, vstruct_end(sb->sb) - src);
116
117                 if (dst > src)
118                         memset(src, 0, dst - src);
119         }
120
121         sb->sb->u64s = cpu_to_le32(sb_u64s);
122
123         return u64s ? f : NULL;
124 }
125
126 void bch2_sb_field_delete(struct bch_sb_handle *sb,
127                           enum bch_sb_field_type type)
128 {
129         struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
130
131         if (f)
132                 __bch2_sb_field_resize(sb, f, 0);
133 }
134
135 /* Superblock realloc/free: */
136
137 void bch2_free_super(struct bch_sb_handle *sb)
138 {
139         kfree(sb->bio);
140         if (!IS_ERR_OR_NULL(sb->bdev))
141                 blkdev_put(sb->bdev, sb->mode);
142
143         kfree(sb->sb);
144         memset(sb, 0, sizeof(*sb));
145 }
146
147 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
148 {
149         size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
150         size_t new_buffer_size;
151         struct bch_sb *new_sb;
152         struct bio *bio;
153
154         if (sb->bdev)
155                 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
156
157         new_buffer_size = roundup_pow_of_two(new_bytes);
158
159         if (sb->sb && sb->buffer_size >= new_buffer_size)
160                 return 0;
161
162         if (sb->have_layout) {
163                 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
164
165                 if (new_bytes > max_bytes) {
166                         pr_err("%pg: superblock too big: want %zu but have %llu",
167                                sb->bdev, new_bytes, max_bytes);
168                         return -BCH_ERR_ENOSPC_sb;
169                 }
170         }
171
172         if (sb->buffer_size >= new_buffer_size && sb->sb)
173                 return 0;
174
175         if (dynamic_fault("bcachefs:add:super_realloc"))
176                 return -BCH_ERR_ENOMEM_sb_realloc_injected;
177
178         if (sb->have_bio) {
179                 unsigned nr_bvecs = DIV_ROUND_UP(new_buffer_size, PAGE_SIZE);
180
181                 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
182                 if (!bio)
183                         return -BCH_ERR_ENOMEM_sb_bio_realloc;
184
185                 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0);
186
187                 kfree(sb->bio);
188                 sb->bio = bio;
189         }
190
191         new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
192         if (!new_sb)
193                 return -BCH_ERR_ENOMEM_sb_buf_realloc;
194
195         sb->sb = new_sb;
196         sb->buffer_size = new_buffer_size;
197
198         return 0;
199 }
200
201 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb,
202                                           enum bch_sb_field_type type,
203                                           unsigned u64s)
204 {
205         struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
206         ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
207         ssize_t d = -old_u64s + u64s;
208
209         if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
210                 return NULL;
211
212         if (sb->fs_sb) {
213                 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
214                 struct bch_dev *ca;
215                 unsigned i;
216
217                 lockdep_assert_held(&c->sb_lock);
218
219                 /* XXX: we're not checking that offline device have enough space */
220
221                 for_each_online_member(ca, c, i) {
222                         struct bch_sb_handle *sb = &ca->disk_sb;
223
224                         if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) {
225                                 percpu_ref_put(&ca->ref);
226                                 return NULL;
227                         }
228                 }
229         }
230
231         f = bch2_sb_field_get(sb->sb, type);
232         f = __bch2_sb_field_resize(sb, f, u64s);
233         if (f)
234                 f->type = cpu_to_le32(type);
235         return f;
236 }
237
238 /* Superblock validate: */
239
240 static inline void __bch2_sb_layout_size_assert(void)
241 {
242         BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
243 }
244
245 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
246 {
247         u64 offset, prev_offset, max_sectors;
248         unsigned i;
249
250         if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) &&
251             !uuid_equal(&layout->magic, &BCHFS_MAGIC)) {
252                 prt_printf(out, "Not a bcachefs superblock layout");
253                 return -BCH_ERR_invalid_sb_layout;
254         }
255
256         if (layout->layout_type != 0) {
257                 prt_printf(out, "Invalid superblock layout type %u",
258                        layout->layout_type);
259                 return -BCH_ERR_invalid_sb_layout_type;
260         }
261
262         if (!layout->nr_superblocks) {
263                 prt_printf(out, "Invalid superblock layout: no superblocks");
264                 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
265         }
266
267         if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
268                 prt_printf(out, "Invalid superblock layout: too many superblocks");
269                 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
270         }
271
272         max_sectors = 1 << layout->sb_max_size_bits;
273
274         prev_offset = le64_to_cpu(layout->sb_offset[0]);
275
276         for (i = 1; i < layout->nr_superblocks; i++) {
277                 offset = le64_to_cpu(layout->sb_offset[i]);
278
279                 if (offset < prev_offset + max_sectors) {
280                         prt_printf(out, "Invalid superblock layout: superblocks overlap\n"
281                                "  (sb %u ends at %llu next starts at %llu",
282                                i - 1, prev_offset + max_sectors, offset);
283                         return -BCH_ERR_invalid_sb_layout_superblocks_overlap;
284                 }
285                 prev_offset = offset;
286         }
287
288         return 0;
289 }
290
291 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out)
292 {
293         u16 version             = le16_to_cpu(sb->version);
294         u16 version_min         = le16_to_cpu(sb->version_min);
295
296         if (!bch2_version_compatible(version)) {
297                 prt_str(out, "Unsupported superblock version ");
298                 bch2_version_to_text(out, version);
299                 prt_str(out, " (min ");
300                 bch2_version_to_text(out, bcachefs_metadata_version_min);
301                 prt_str(out, ", max ");
302                 bch2_version_to_text(out, bcachefs_metadata_version_current);
303                 prt_str(out, ")");
304                 return -BCH_ERR_invalid_sb_version;
305         }
306
307         if (!bch2_version_compatible(version_min)) {
308                 prt_str(out, "Unsupported superblock version_min ");
309                 bch2_version_to_text(out, version_min);
310                 prt_str(out, " (min ");
311                 bch2_version_to_text(out, bcachefs_metadata_version_min);
312                 prt_str(out, ", max ");
313                 bch2_version_to_text(out, bcachefs_metadata_version_current);
314                 prt_str(out, ")");
315                 return -BCH_ERR_invalid_sb_version;
316         }
317
318         if (version_min > version) {
319                 prt_str(out, "Bad minimum version ");
320                 bch2_version_to_text(out, version_min);
321                 prt_str(out, ", greater than version field ");
322                 bch2_version_to_text(out, version);
323                 return -BCH_ERR_invalid_sb_version;
324         }
325
326         return 0;
327 }
328
329 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
330                             int rw)
331 {
332         struct bch_sb *sb = disk_sb->sb;
333         struct bch_sb_field *f;
334         struct bch_sb_field_members *mi;
335         enum bch_opt_id opt_id;
336         u16 block_size;
337         int ret;
338
339         ret = bch2_sb_compatible(sb, out);
340         if (ret)
341                 return ret;
342
343         if (sb->features[1] ||
344             (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) {
345                 prt_printf(out, "Filesystem has incompatible features");
346                 return -BCH_ERR_invalid_sb_features;
347         }
348
349         block_size = le16_to_cpu(sb->block_size);
350
351         if (block_size > PAGE_SECTORS) {
352                 prt_printf(out, "Block size too big (got %u, max %u)",
353                        block_size, PAGE_SECTORS);
354                 return -BCH_ERR_invalid_sb_block_size;
355         }
356
357         if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) {
358                 prt_printf(out, "Bad user UUID (got zeroes)");
359                 return -BCH_ERR_invalid_sb_uuid;
360         }
361
362         if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) {
363                 prt_printf(out, "Bad intenal UUID (got zeroes)");
364                 return -BCH_ERR_invalid_sb_uuid;
365         }
366
367         if (!sb->nr_devices ||
368             sb->nr_devices > BCH_SB_MEMBERS_MAX) {
369                 prt_printf(out, "Bad number of member devices %u (max %u)",
370                        sb->nr_devices, BCH_SB_MEMBERS_MAX);
371                 return -BCH_ERR_invalid_sb_too_many_members;
372         }
373
374         if (sb->dev_idx >= sb->nr_devices) {
375                 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)",
376                        sb->dev_idx, sb->nr_devices);
377                 return -BCH_ERR_invalid_sb_dev_idx;
378         }
379
380         if (!sb->time_precision ||
381             le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
382                 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)",
383                        le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
384                 return -BCH_ERR_invalid_sb_time_precision;
385         }
386
387         if (rw == READ) {
388                 /*
389                  * Been seeing a bug where these are getting inexplicably
390                  * zeroed, so we're now validating them, but we have to be
391                  * careful not to preven people's filesystems from mounting:
392                  */
393                 if (!BCH_SB_JOURNAL_FLUSH_DELAY(sb))
394                         SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
395                 if (!BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
396                         SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 1000);
397         }
398
399         for (opt_id = 0; opt_id < bch2_opts_nr; opt_id++) {
400                 const struct bch_option *opt = bch2_opt_table + opt_id;
401
402                 if (opt->get_sb != BCH2_NO_SB_OPT) {
403                         u64 v = bch2_opt_from_sb(sb, opt_id);
404
405                         prt_printf(out, "Invalid option ");
406                         ret = bch2_opt_validate(opt, v, out);
407                         if (ret)
408                                 return ret;
409
410                         printbuf_reset(out);
411                 }
412         }
413
414         /* validate layout */
415         ret = validate_sb_layout(&sb->layout, out);
416         if (ret)
417                 return ret;
418
419         vstruct_for_each(sb, f) {
420                 if (!f->u64s) {
421                         prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)",
422                                le32_to_cpu(f->type));
423                         return -BCH_ERR_invalid_sb_field_size;
424                 }
425
426                 if (vstruct_next(f) > vstruct_last(sb)) {
427                         prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
428                                le32_to_cpu(f->type));
429                         return -BCH_ERR_invalid_sb_field_size;
430                 }
431         }
432
433         /* members must be validated first: */
434         mi = bch2_sb_get_members(sb);
435         if (!mi) {
436                 prt_printf(out, "Invalid superblock: member info area missing");
437                 return -BCH_ERR_invalid_sb_members_missing;
438         }
439
440         ret = bch2_sb_field_validate(sb, &mi->field, out);
441         if (ret)
442                 return ret;
443
444         vstruct_for_each(sb, f) {
445                 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members)
446                         continue;
447
448                 ret = bch2_sb_field_validate(sb, f, out);
449                 if (ret)
450                         return ret;
451         }
452
453         return 0;
454 }
455
456 /* device open: */
457
458 static void bch2_sb_update(struct bch_fs *c)
459 {
460         struct bch_sb *src = c->disk_sb.sb;
461         struct bch_sb_field_members *mi = bch2_sb_get_members(src);
462         struct bch_dev *ca;
463         unsigned i;
464
465         lockdep_assert_held(&c->sb_lock);
466
467         c->sb.uuid              = src->uuid;
468         c->sb.user_uuid         = src->user_uuid;
469         c->sb.version           = le16_to_cpu(src->version);
470         c->sb.version_min       = le16_to_cpu(src->version_min);
471         c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src) ?: c->sb.version;
472         c->sb.nr_devices        = src->nr_devices;
473         c->sb.clean             = BCH_SB_CLEAN(src);
474         c->sb.encryption_type   = BCH_SB_ENCRYPTION_TYPE(src);
475
476         c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
477         c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
478
479         /* XXX this is wrong, we need a 96 or 128 bit integer type */
480         c->sb.time_base_lo      = div_u64(le64_to_cpu(src->time_base_lo),
481                                           c->sb.nsec_per_time_unit);
482         c->sb.time_base_hi      = le32_to_cpu(src->time_base_hi);
483
484         c->sb.features          = le64_to_cpu(src->features[0]);
485         c->sb.compat            = le64_to_cpu(src->compat[0]);
486
487         for_each_member_device(ca, c, i)
488                 ca->mi = bch2_mi_to_cpu(mi->members + i);
489 }
490
491 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
492 {
493         struct bch_sb_field *src_f, *dst_f;
494         struct bch_sb *dst = dst_handle->sb;
495         unsigned i;
496
497         dst->version            = src->version;
498         dst->version_min        = src->version_min;
499         dst->seq                = src->seq;
500         dst->uuid               = src->uuid;
501         dst->user_uuid          = src->user_uuid;
502         memcpy(dst->label,      src->label, sizeof(dst->label));
503
504         dst->block_size         = src->block_size;
505         dst->nr_devices         = src->nr_devices;
506
507         dst->time_base_lo       = src->time_base_lo;
508         dst->time_base_hi       = src->time_base_hi;
509         dst->time_precision     = src->time_precision;
510
511         memcpy(dst->flags,      src->flags,     sizeof(dst->flags));
512         memcpy(dst->features,   src->features,  sizeof(dst->features));
513         memcpy(dst->compat,     src->compat,    sizeof(dst->compat));
514
515         for (i = 0; i < BCH_SB_FIELD_NR; i++) {
516                 int d;
517
518                 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
519                         continue;
520
521                 src_f = bch2_sb_field_get(src, i);
522                 dst_f = bch2_sb_field_get(dst, i);
523
524                 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) -
525                     (dst_f ? le32_to_cpu(dst_f->u64s) : 0);
526                 if (d > 0) {
527                         int ret = bch2_sb_realloc(dst_handle, le32_to_cpu(dst_handle->sb->u64s) + d);
528                         if (ret)
529                                 return ret;
530
531                         dst = dst_handle->sb;
532                         dst_f = bch2_sb_field_get(dst, i);
533                 }
534
535                 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
536                                 src_f ? le32_to_cpu(src_f->u64s) : 0);
537
538                 if (src_f)
539                         memcpy(dst_f, src_f, vstruct_bytes(src_f));
540         }
541
542         return 0;
543 }
544
545 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
546 {
547         int ret;
548
549         lockdep_assert_held(&c->sb_lock);
550
551         ret =   bch2_sb_realloc(&c->disk_sb, 0) ?:
552                 __copy_super(&c->disk_sb, src) ?:
553                 bch2_sb_replicas_to_cpu_replicas(c) ?:
554                 bch2_sb_disk_groups_to_cpu(c);
555         if (ret)
556                 return ret;
557
558         bch2_sb_update(c);
559         return 0;
560 }
561
562 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
563 {
564         return __copy_super(&ca->disk_sb, c->disk_sb.sb);
565 }
566
567 /* read superblock: */
568
569 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
570 {
571         struct bch_csum csum;
572         size_t bytes;
573         int ret;
574 reread:
575         bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
576         sb->bio->bi_iter.bi_sector = offset;
577         bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
578
579         ret = submit_bio_wait(sb->bio);
580         if (ret) {
581                 prt_printf(err, "IO error: %i", ret);
582                 return ret;
583         }
584
585         if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
586             !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
587                 prt_printf(err, "Not a bcachefs superblock");
588                 return -BCH_ERR_invalid_sb_magic;
589         }
590
591         ret = bch2_sb_compatible(sb->sb, err);
592         if (ret)
593                 return ret;
594
595         bytes = vstruct_bytes(sb->sb);
596
597         if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
598                 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
599                        bytes, 512UL << sb->sb->layout.sb_max_size_bits);
600                 return -BCH_ERR_invalid_sb_too_big;
601         }
602
603         if (bytes > sb->buffer_size) {
604                 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
605                 if (ret)
606                         return ret;
607                 goto reread;
608         }
609
610         if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
611                 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
612                 return -BCH_ERR_invalid_sb_csum_type;
613         }
614
615         /* XXX: verify MACs */
616         csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
617                             null_nonce(), sb->sb);
618
619         if (bch2_crc_cmp(csum, sb->sb->csum)) {
620                 prt_printf(err, "bad checksum");
621                 return -BCH_ERR_invalid_sb_csum;
622         }
623
624         sb->seq = le64_to_cpu(sb->sb->seq);
625
626         return 0;
627 }
628
629 int bch2_read_super(const char *path, struct bch_opts *opts,
630                     struct bch_sb_handle *sb)
631 {
632         u64 offset = opt_get(*opts, sb);
633         struct bch_sb_layout layout;
634         struct printbuf err = PRINTBUF;
635         __le64 *i;
636         int ret;
637
638         memset(sb, 0, sizeof(*sb));
639         sb->mode        = FMODE_READ;
640         sb->have_bio    = true;
641
642         if (!opt_get(*opts, noexcl))
643                 sb->mode |= FMODE_EXCL;
644
645         if (!opt_get(*opts, nochanges))
646                 sb->mode |= FMODE_WRITE;
647
648         sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
649         if (IS_ERR(sb->bdev) &&
650             PTR_ERR(sb->bdev) == -EACCES &&
651             opt_get(*opts, read_only)) {
652                 sb->mode &= ~FMODE_WRITE;
653
654                 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
655                 if (!IS_ERR(sb->bdev))
656                         opt_set(*opts, nochanges, true);
657         }
658
659         if (IS_ERR(sb->bdev)) {
660                 ret = PTR_ERR(sb->bdev);
661                 goto out;
662         }
663
664         ret = bch2_sb_realloc(sb, 0);
665         if (ret) {
666                 prt_printf(&err, "error allocating memory for superblock");
667                 goto err;
668         }
669
670         if (bch2_fs_init_fault("read_super")) {
671                 prt_printf(&err, "dynamic fault");
672                 ret = -EFAULT;
673                 goto err;
674         }
675
676         ret = read_one_super(sb, offset, &err);
677         if (!ret)
678                 goto got_super;
679
680         if (opt_defined(*opts, sb))
681                 goto err;
682
683         printk(KERN_ERR "bcachefs (%s): error reading default superblock: %s",
684                path, err.buf);
685         printbuf_reset(&err);
686
687         /*
688          * Error reading primary superblock - read location of backup
689          * superblocks:
690          */
691         bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
692         sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
693         /*
694          * use sb buffer to read layout, since sb buffer is page aligned but
695          * layout won't be:
696          */
697         bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
698
699         ret = submit_bio_wait(sb->bio);
700         if (ret) {
701                 prt_printf(&err, "IO error: %i", ret);
702                 goto err;
703         }
704
705         memcpy(&layout, sb->sb, sizeof(layout));
706         ret = validate_sb_layout(&layout, &err);
707         if (ret)
708                 goto err;
709
710         for (i = layout.sb_offset;
711              i < layout.sb_offset + layout.nr_superblocks; i++) {
712                 offset = le64_to_cpu(*i);
713
714                 if (offset == opt_get(*opts, sb))
715                         continue;
716
717                 ret = read_one_super(sb, offset, &err);
718                 if (!ret)
719                         goto got_super;
720         }
721
722         goto err;
723
724 got_super:
725         if (le16_to_cpu(sb->sb->block_size) << 9 <
726             bdev_logical_block_size(sb->bdev)) {
727                 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
728                        le16_to_cpu(sb->sb->block_size) << 9,
729                        bdev_logical_block_size(sb->bdev));
730                 ret = -BCH_ERR_block_size_too_small;
731                 goto err;
732         }
733
734         ret = 0;
735         sb->have_layout = true;
736
737         ret = bch2_sb_validate(sb, &err, READ);
738         if (ret) {
739                 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s",
740                        path, err.buf);
741                 goto err_no_print;
742         }
743 out:
744         printbuf_exit(&err);
745         return ret;
746 err:
747         printk(KERN_ERR "bcachefs (%s): error reading superblock: %s",
748                path, err.buf);
749 err_no_print:
750         bch2_free_super(sb);
751         goto out;
752 }
753
754 /* write superblock: */
755
756 static void write_super_endio(struct bio *bio)
757 {
758         struct bch_dev *ca = bio->bi_private;
759
760         /* XXX: return errors directly */
761
762         if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write error: %s",
763                                bch2_blk_status_to_str(bio->bi_status)))
764                 ca->sb_write_error = 1;
765
766         closure_put(&ca->fs->sb_write);
767         percpu_ref_put(&ca->io_ref);
768 }
769
770 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
771 {
772         struct bch_sb *sb = ca->disk_sb.sb;
773         struct bio *bio = ca->disk_sb.bio;
774
775         bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
776         bio->bi_iter.bi_sector  = le64_to_cpu(sb->layout.sb_offset[0]);
777         bio->bi_end_io          = write_super_endio;
778         bio->bi_private         = ca;
779         bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
780
781         this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
782                      bio_sectors(bio));
783
784         percpu_ref_get(&ca->io_ref);
785         closure_bio_submit(bio, &c->sb_write);
786 }
787
788 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
789 {
790         struct bch_sb *sb = ca->disk_sb.sb;
791         struct bio *bio = ca->disk_sb.bio;
792
793         sb->offset = sb->layout.sb_offset[idx];
794
795         SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
796         sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
797                                 null_nonce(), sb);
798
799         bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
800         bio->bi_iter.bi_sector  = le64_to_cpu(sb->offset);
801         bio->bi_end_io          = write_super_endio;
802         bio->bi_private         = ca;
803         bch2_bio_map(bio, sb,
804                      roundup((size_t) vstruct_bytes(sb),
805                              bdev_logical_block_size(ca->disk_sb.bdev)));
806
807         this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
808                      bio_sectors(bio));
809
810         percpu_ref_get(&ca->io_ref);
811         closure_bio_submit(bio, &c->sb_write);
812 }
813
814 int bch2_write_super(struct bch_fs *c)
815 {
816         struct closure *cl = &c->sb_write;
817         struct bch_dev *ca;
818         struct printbuf err = PRINTBUF;
819         unsigned i, sb = 0, nr_wrote;
820         struct bch_devs_mask sb_written;
821         bool wrote, can_mount_without_written, can_mount_with_written;
822         unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
823         int ret = 0;
824
825         trace_and_count(c, write_super, c, _RET_IP_);
826
827         if (c->opts.very_degraded)
828                 degraded_flags |= BCH_FORCE_IF_LOST;
829
830         lockdep_assert_held(&c->sb_lock);
831
832         closure_init_stack(cl);
833         memset(&sb_written, 0, sizeof(sb_written));
834
835         /* Make sure we're using the new magic numbers: */
836         c->disk_sb.sb->magic = BCHFS_MAGIC;
837         c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
838
839         le64_add_cpu(&c->disk_sb.sb->seq, 1);
840
841         if (test_bit(BCH_FS_ERROR, &c->flags))
842                 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
843         if (test_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags))
844                 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
845
846         SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
847
848         bch2_sb_counters_from_cpu(c);
849
850         for_each_online_member(ca, c, i)
851                 bch2_sb_from_fs(c, ca);
852
853         for_each_online_member(ca, c, i) {
854                 printbuf_reset(&err);
855
856                 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
857                 if (ret) {
858                         bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
859                         percpu_ref_put(&ca->io_ref);
860                         goto out;
861                 }
862         }
863
864         if (c->opts.nochanges)
865                 goto out;
866
867         /*
868          * Defer writing the superblock until filesystem initialization is
869          * complete - don't write out a partly initialized superblock:
870          */
871         if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
872                 goto out;
873
874         for_each_online_member(ca, c, i) {
875                 __set_bit(ca->dev_idx, sb_written.d);
876                 ca->sb_write_error = 0;
877         }
878
879         for_each_online_member(ca, c, i)
880                 read_back_super(c, ca);
881         closure_sync(cl);
882
883         for_each_online_member(ca, c, i) {
884                 if (ca->sb_write_error)
885                         continue;
886
887                 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
888                         bch2_fs_fatal_error(c,
889                                 "Superblock write was silently dropped! (seq %llu expected %llu)",
890                                 le64_to_cpu(ca->sb_read_scratch->seq),
891                                 ca->disk_sb.seq);
892                         percpu_ref_put(&ca->io_ref);
893                         ret = -BCH_ERR_erofs_sb_err;
894                         goto out;
895                 }
896
897                 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
898                         bch2_fs_fatal_error(c,
899                                 "Superblock modified by another process (seq %llu expected %llu)",
900                                 le64_to_cpu(ca->sb_read_scratch->seq),
901                                 ca->disk_sb.seq);
902                         percpu_ref_put(&ca->io_ref);
903                         ret = -BCH_ERR_erofs_sb_err;
904                         goto out;
905                 }
906         }
907
908         do {
909                 wrote = false;
910                 for_each_online_member(ca, c, i)
911                         if (!ca->sb_write_error &&
912                             sb < ca->disk_sb.sb->layout.nr_superblocks) {
913                                 write_one_super(c, ca, sb);
914                                 wrote = true;
915                         }
916                 closure_sync(cl);
917                 sb++;
918         } while (wrote);
919
920         for_each_online_member(ca, c, i) {
921                 if (ca->sb_write_error)
922                         __clear_bit(ca->dev_idx, sb_written.d);
923                 else
924                         ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
925         }
926
927         nr_wrote = dev_mask_nr(&sb_written);
928
929         can_mount_with_written =
930                 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
931
932         for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
933                 sb_written.d[i] = ~sb_written.d[i];
934
935         can_mount_without_written =
936                 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
937
938         /*
939          * If we would be able to mount _without_ the devices we successfully
940          * wrote superblocks to, we weren't able to write to enough devices:
941          *
942          * Exception: if we can mount without the successes because we haven't
943          * written anything (new filesystem), we continue if we'd be able to
944          * mount with the devices we did successfully write to:
945          */
946         if (bch2_fs_fatal_err_on(!nr_wrote ||
947                                  !can_mount_with_written ||
948                                  (can_mount_without_written &&
949                                   !can_mount_with_written), c,
950                 "Unable to write superblock to sufficient devices (from %ps)",
951                 (void *) _RET_IP_))
952                 ret = -1;
953 out:
954         /* Make new options visible after they're persistent: */
955         bch2_sb_update(c);
956         printbuf_exit(&err);
957         return ret;
958 }
959
960 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
961 {
962         mutex_lock(&c->sb_lock);
963         if (!(c->sb.features & (1ULL << feat))) {
964                 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
965
966                 bch2_write_super(c);
967         }
968         mutex_unlock(&c->sb_lock);
969 }
970
971 /* BCH_SB_FIELD_members: */
972
973 static int bch2_sb_members_validate(struct bch_sb *sb,
974                                     struct bch_sb_field *f,
975                                     struct printbuf *err)
976 {
977         struct bch_sb_field_members *mi = field_to_type(f, members);
978         unsigned i;
979
980         if ((void *) (mi->members + sb->nr_devices) >
981             vstruct_end(&mi->field)) {
982                 prt_printf(err, "too many devices for section size");
983                 return -BCH_ERR_invalid_sb_members;
984         }
985
986         for (i = 0; i < sb->nr_devices; i++) {
987                 struct bch_member *m = mi->members + i;
988
989                 if (!bch2_member_exists(m))
990                         continue;
991
992                 if (le64_to_cpu(m->nbuckets) > LONG_MAX) {
993                         prt_printf(err, "device %u: too many buckets (got %llu, max %lu)",
994                                i, le64_to_cpu(m->nbuckets), LONG_MAX);
995                         return -BCH_ERR_invalid_sb_members;
996                 }
997
998                 if (le64_to_cpu(m->nbuckets) -
999                     le16_to_cpu(m->first_bucket) < BCH_MIN_NR_NBUCKETS) {
1000                         prt_printf(err, "device %u: not enough buckets (got %llu, max %u)",
1001                                i, le64_to_cpu(m->nbuckets), BCH_MIN_NR_NBUCKETS);
1002                         return -BCH_ERR_invalid_sb_members;
1003                 }
1004
1005                 if (le16_to_cpu(m->bucket_size) <
1006                     le16_to_cpu(sb->block_size)) {
1007                         prt_printf(err, "device %u: bucket size %u smaller than block size %u",
1008                                i, le16_to_cpu(m->bucket_size), le16_to_cpu(sb->block_size));
1009                         return -BCH_ERR_invalid_sb_members;
1010                 }
1011
1012                 if (le16_to_cpu(m->bucket_size) <
1013                     BCH_SB_BTREE_NODE_SIZE(sb)) {
1014                         prt_printf(err, "device %u: bucket size %u smaller than btree node size %llu",
1015                                i, le16_to_cpu(m->bucket_size), BCH_SB_BTREE_NODE_SIZE(sb));
1016                         return -BCH_ERR_invalid_sb_members;
1017                 }
1018         }
1019
1020         return 0;
1021 }
1022
1023 static void bch2_sb_members_to_text(struct printbuf *out, struct bch_sb *sb,
1024                                     struct bch_sb_field *f)
1025 {
1026         struct bch_sb_field_members *mi = field_to_type(f, members);
1027         struct bch_sb_field_disk_groups *gi = bch2_sb_get_disk_groups(sb);
1028         unsigned i;
1029
1030         for (i = 0; i < sb->nr_devices; i++) {
1031                 struct bch_member *m = mi->members + i;
1032                 unsigned data_have = bch2_sb_dev_has_data(sb, i);
1033                 u64 bucket_size = le16_to_cpu(m->bucket_size);
1034                 u64 device_size = le64_to_cpu(m->nbuckets) * bucket_size;
1035
1036                 if (!bch2_member_exists(m))
1037                         continue;
1038
1039                 prt_printf(out, "Device:");
1040                 prt_tab(out);
1041                 prt_printf(out, "%u", i);
1042                 prt_newline(out);
1043
1044                 printbuf_indent_add(out, 2);
1045
1046                 prt_printf(out, "UUID:");
1047                 prt_tab(out);
1048                 pr_uuid(out, m->uuid.b);
1049                 prt_newline(out);
1050
1051                 prt_printf(out, "Size:");
1052                 prt_tab(out);
1053                 prt_units_u64(out, device_size << 9);
1054                 prt_newline(out);
1055
1056                 prt_printf(out, "Bucket size:");
1057                 prt_tab(out);
1058                 prt_units_u64(out, bucket_size << 9);
1059                 prt_newline(out);
1060
1061                 prt_printf(out, "First bucket:");
1062                 prt_tab(out);
1063                 prt_printf(out, "%u", le16_to_cpu(m->first_bucket));
1064                 prt_newline(out);
1065
1066                 prt_printf(out, "Buckets:");
1067                 prt_tab(out);
1068                 prt_printf(out, "%llu", le64_to_cpu(m->nbuckets));
1069                 prt_newline(out);
1070
1071                 prt_printf(out, "Last mount:");
1072                 prt_tab(out);
1073                 if (m->last_mount)
1074                         pr_time(out, le64_to_cpu(m->last_mount));
1075                 else
1076                         prt_printf(out, "(never)");
1077                 prt_newline(out);
1078
1079                 prt_printf(out, "State:");
1080                 prt_tab(out);
1081                 prt_printf(out, "%s",
1082                        BCH_MEMBER_STATE(m) < BCH_MEMBER_STATE_NR
1083                        ? bch2_member_states[BCH_MEMBER_STATE(m)]
1084                        : "unknown");
1085                 prt_newline(out);
1086
1087                 prt_printf(out, "Label:");
1088                 prt_tab(out);
1089                 if (BCH_MEMBER_GROUP(m)) {
1090                         unsigned idx = BCH_MEMBER_GROUP(m) - 1;
1091
1092                         if (idx < disk_groups_nr(gi))
1093                                 prt_printf(out, "%s (%u)",
1094                                        gi->entries[idx].label, idx);
1095                         else
1096                                 prt_printf(out, "(bad disk labels section)");
1097                 } else {
1098                         prt_printf(out, "(none)");
1099                 }
1100                 prt_newline(out);
1101
1102                 prt_printf(out, "Data allowed:");
1103                 prt_tab(out);
1104                 if (BCH_MEMBER_DATA_ALLOWED(m))
1105                         prt_bitflags(out, bch2_data_types, BCH_MEMBER_DATA_ALLOWED(m));
1106                 else
1107                         prt_printf(out, "(none)");
1108                 prt_newline(out);
1109
1110                 prt_printf(out, "Has data:");
1111                 prt_tab(out);
1112                 if (data_have)
1113                         prt_bitflags(out, bch2_data_types, data_have);
1114                 else
1115                         prt_printf(out, "(none)");
1116                 prt_newline(out);
1117
1118                 prt_printf(out, "Discard:");
1119                 prt_tab(out);
1120                 prt_printf(out, "%llu", BCH_MEMBER_DISCARD(m));
1121                 prt_newline(out);
1122
1123                 prt_printf(out, "Freespace initialized:");
1124                 prt_tab(out);
1125                 prt_printf(out, "%llu", BCH_MEMBER_FREESPACE_INITIALIZED(m));
1126                 prt_newline(out);
1127
1128                 printbuf_indent_sub(out, 2);
1129         }
1130 }
1131
1132 static const struct bch_sb_field_ops bch_sb_field_ops_members = {
1133         .validate       = bch2_sb_members_validate,
1134         .to_text        = bch2_sb_members_to_text,
1135 };
1136
1137 /* BCH_SB_FIELD_crypt: */
1138
1139 static int bch2_sb_crypt_validate(struct bch_sb *sb,
1140                                   struct bch_sb_field *f,
1141                                   struct printbuf *err)
1142 {
1143         struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1144
1145         if (vstruct_bytes(&crypt->field) < sizeof(*crypt)) {
1146                 prt_printf(err, "wrong size (got %zu should be %zu)",
1147                        vstruct_bytes(&crypt->field), sizeof(*crypt));
1148                 return -BCH_ERR_invalid_sb_crypt;
1149         }
1150
1151         if (BCH_CRYPT_KDF_TYPE(crypt)) {
1152                 prt_printf(err, "bad kdf type %llu", BCH_CRYPT_KDF_TYPE(crypt));
1153                 return -BCH_ERR_invalid_sb_crypt;
1154         }
1155
1156         return 0;
1157 }
1158
1159 static void bch2_sb_crypt_to_text(struct printbuf *out, struct bch_sb *sb,
1160                                   struct bch_sb_field *f)
1161 {
1162         struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
1163
1164         prt_printf(out, "KFD:               %llu", BCH_CRYPT_KDF_TYPE(crypt));
1165         prt_newline(out);
1166         prt_printf(out, "scrypt n:          %llu", BCH_KDF_SCRYPT_N(crypt));
1167         prt_newline(out);
1168         prt_printf(out, "scrypt r:          %llu", BCH_KDF_SCRYPT_R(crypt));
1169         prt_newline(out);
1170         prt_printf(out, "scrypt p:          %llu", BCH_KDF_SCRYPT_P(crypt));
1171         prt_newline(out);
1172 }
1173
1174 static const struct bch_sb_field_ops bch_sb_field_ops_crypt = {
1175         .validate       = bch2_sb_crypt_validate,
1176         .to_text        = bch2_sb_crypt_to_text,
1177 };
1178
1179 /* BCH_SB_FIELD_clean: */
1180
1181 int bch2_sb_clean_validate_late(struct bch_fs *c, struct bch_sb_field_clean *clean, int write)
1182 {
1183         struct jset_entry *entry;
1184         int ret;
1185
1186         for (entry = clean->start;
1187              entry < (struct jset_entry *) vstruct_end(&clean->field);
1188              entry = vstruct_next(entry)) {
1189                 ret = bch2_journal_entry_validate(c, NULL, entry,
1190                                                   le16_to_cpu(c->disk_sb.sb->version),
1191                                                   BCH_SB_BIG_ENDIAN(c->disk_sb.sb),
1192                                                   write);
1193                 if (ret)
1194                         return ret;
1195         }
1196
1197         return 0;
1198 }
1199
1200 int bch2_fs_mark_dirty(struct bch_fs *c)
1201 {
1202         int ret;
1203
1204         /*
1205          * Unconditionally write superblock, to verify it hasn't changed before
1206          * we go rw:
1207          */
1208
1209         mutex_lock(&c->sb_lock);
1210         SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1211
1212         /*
1213          * Downgrade, if superblock is at a higher version than currently
1214          * supported:
1215          */
1216         if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1217                 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1218         if (c->sb.version > bcachefs_metadata_version_current)
1219                 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1220         if (c->sb.version_min > bcachefs_metadata_version_current)
1221                 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1222         c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1223
1224         c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS);
1225         ret = bch2_write_super(c);
1226         mutex_unlock(&c->sb_lock);
1227
1228         return ret;
1229 }
1230
1231 static struct jset_entry *jset_entry_init(struct jset_entry **end, size_t size)
1232 {
1233         struct jset_entry *entry = *end;
1234         unsigned u64s = DIV_ROUND_UP(size, sizeof(u64));
1235
1236         memset(entry, 0, u64s * sizeof(u64));
1237         /*
1238          * The u64s field counts from the start of data, ignoring the shared
1239          * fields.
1240          */
1241         entry->u64s = cpu_to_le16(u64s - 1);
1242
1243         *end = vstruct_next(*end);
1244         return entry;
1245 }
1246
1247 void bch2_journal_super_entries_add_common(struct bch_fs *c,
1248                                            struct jset_entry **end,
1249                                            u64 journal_seq)
1250 {
1251         struct bch_dev *ca;
1252         unsigned i, dev;
1253
1254         percpu_down_read(&c->mark_lock);
1255
1256         if (!journal_seq) {
1257                 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
1258                         bch2_fs_usage_acc_to_base(c, i);
1259         } else {
1260                 bch2_fs_usage_acc_to_base(c, journal_seq & JOURNAL_BUF_MASK);
1261         }
1262
1263         {
1264                 struct jset_entry_usage *u =
1265                         container_of(jset_entry_init(end, sizeof(*u)),
1266                                      struct jset_entry_usage, entry);
1267
1268                 u->entry.type   = BCH_JSET_ENTRY_usage;
1269                 u->entry.btree_id = BCH_FS_USAGE_inodes;
1270                 u->v            = cpu_to_le64(c->usage_base->nr_inodes);
1271         }
1272
1273         {
1274                 struct jset_entry_usage *u =
1275                         container_of(jset_entry_init(end, sizeof(*u)),
1276                                      struct jset_entry_usage, entry);
1277
1278                 u->entry.type   = BCH_JSET_ENTRY_usage;
1279                 u->entry.btree_id = BCH_FS_USAGE_key_version;
1280                 u->v            = cpu_to_le64(atomic64_read(&c->key_version));
1281         }
1282
1283         for (i = 0; i < BCH_REPLICAS_MAX; i++) {
1284                 struct jset_entry_usage *u =
1285                         container_of(jset_entry_init(end, sizeof(*u)),
1286                                      struct jset_entry_usage, entry);
1287
1288                 u->entry.type   = BCH_JSET_ENTRY_usage;
1289                 u->entry.btree_id = BCH_FS_USAGE_reserved;
1290                 u->entry.level  = i;
1291                 u->v            = cpu_to_le64(c->usage_base->persistent_reserved[i]);
1292         }
1293
1294         for (i = 0; i < c->replicas.nr; i++) {
1295                 struct bch_replicas_entry *e =
1296                         cpu_replicas_entry(&c->replicas, i);
1297                 struct jset_entry_data_usage *u =
1298                         container_of(jset_entry_init(end, sizeof(*u) + e->nr_devs),
1299                                      struct jset_entry_data_usage, entry);
1300
1301                 u->entry.type   = BCH_JSET_ENTRY_data_usage;
1302                 u->v            = cpu_to_le64(c->usage_base->replicas[i]);
1303                 unsafe_memcpy(&u->r, e, replicas_entry_bytes(e),
1304                               "embedded variable length struct");
1305         }
1306
1307         for_each_member_device(ca, c, dev) {
1308                 unsigned b = sizeof(struct jset_entry_dev_usage) +
1309                         sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR;
1310                 struct jset_entry_dev_usage *u =
1311                         container_of(jset_entry_init(end, b),
1312                                      struct jset_entry_dev_usage, entry);
1313
1314                 u->entry.type = BCH_JSET_ENTRY_dev_usage;
1315                 u->dev = cpu_to_le32(dev);
1316                 u->buckets_ec           = cpu_to_le64(ca->usage_base->buckets_ec);
1317
1318                 for (i = 0; i < BCH_DATA_NR; i++) {
1319                         u->d[i].buckets = cpu_to_le64(ca->usage_base->d[i].buckets);
1320                         u->d[i].sectors = cpu_to_le64(ca->usage_base->d[i].sectors);
1321                         u->d[i].fragmented = cpu_to_le64(ca->usage_base->d[i].fragmented);
1322                 }
1323         }
1324
1325         percpu_up_read(&c->mark_lock);
1326
1327         for (i = 0; i < 2; i++) {
1328                 struct jset_entry_clock *clock =
1329                         container_of(jset_entry_init(end, sizeof(*clock)),
1330                                      struct jset_entry_clock, entry);
1331
1332                 clock->entry.type = BCH_JSET_ENTRY_clock;
1333                 clock->rw       = i;
1334                 clock->time     = cpu_to_le64(atomic64_read(&c->io_clock[i].now));
1335         }
1336 }
1337
1338 void bch2_fs_mark_clean(struct bch_fs *c)
1339 {
1340         struct bch_sb_field_clean *sb_clean;
1341         struct jset_entry *entry;
1342         unsigned u64s;
1343         int ret;
1344
1345         mutex_lock(&c->sb_lock);
1346         if (BCH_SB_CLEAN(c->disk_sb.sb))
1347                 goto out;
1348
1349         SET_BCH_SB_CLEAN(c->disk_sb.sb, true);
1350
1351         c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
1352         c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_metadata);
1353         c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_extents_above_btree_updates));
1354         c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_btree_updates_journalled));
1355
1356         u64s = sizeof(*sb_clean) / sizeof(u64) + c->journal.entry_u64s_reserved;
1357
1358         sb_clean = bch2_sb_resize_clean(&c->disk_sb, u64s);
1359         if (!sb_clean) {
1360                 bch_err(c, "error resizing superblock while setting filesystem clean");
1361                 goto out;
1362         }
1363
1364         sb_clean->flags         = 0;
1365         sb_clean->journal_seq   = cpu_to_le64(atomic64_read(&c->journal.seq));
1366
1367         /* Trying to catch outstanding bug: */
1368         BUG_ON(le64_to_cpu(sb_clean->journal_seq) > S64_MAX);
1369
1370         entry = sb_clean->start;
1371         bch2_journal_super_entries_add_common(c, &entry, 0);
1372         entry = bch2_btree_roots_to_journal_entries(c, entry, entry);
1373         BUG_ON((void *) entry > vstruct_end(&sb_clean->field));
1374
1375         memset(entry, 0,
1376                vstruct_end(&sb_clean->field) - (void *) entry);
1377
1378         /*
1379          * this should be in the write path, and we should be validating every
1380          * superblock section:
1381          */
1382         ret = bch2_sb_clean_validate_late(c, sb_clean, WRITE);
1383         if (ret) {
1384                 bch_err(c, "error writing marking filesystem clean: validate error");
1385                 goto out;
1386         }
1387
1388         bch2_write_super(c);
1389 out:
1390         mutex_unlock(&c->sb_lock);
1391 }
1392
1393 static int bch2_sb_clean_validate(struct bch_sb *sb,
1394                                   struct bch_sb_field *f,
1395                                   struct printbuf *err)
1396 {
1397         struct bch_sb_field_clean *clean = field_to_type(f, clean);
1398
1399         if (vstruct_bytes(&clean->field) < sizeof(*clean)) {
1400                 prt_printf(err, "wrong size (got %zu should be %zu)",
1401                        vstruct_bytes(&clean->field), sizeof(*clean));
1402                 return -BCH_ERR_invalid_sb_clean;
1403         }
1404
1405         return 0;
1406 }
1407
1408 static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb,
1409                                   struct bch_sb_field *f)
1410 {
1411         struct bch_sb_field_clean *clean = field_to_type(f, clean);
1412         struct jset_entry *entry;
1413
1414         prt_printf(out, "flags:          %x",   le32_to_cpu(clean->flags));
1415         prt_newline(out);
1416         prt_printf(out, "journal_seq:    %llu", le64_to_cpu(clean->journal_seq));
1417         prt_newline(out);
1418
1419         for (entry = clean->start;
1420              entry != vstruct_end(&clean->field);
1421              entry = vstruct_next(entry)) {
1422                 if (entry->type == BCH_JSET_ENTRY_btree_keys &&
1423                     !entry->u64s)
1424                         continue;
1425
1426                 bch2_journal_entry_to_text(out, NULL, entry);
1427                 prt_newline(out);
1428         }
1429 }
1430
1431 static const struct bch_sb_field_ops bch_sb_field_ops_clean = {
1432         .validate       = bch2_sb_clean_validate,
1433         .to_text        = bch2_sb_clean_to_text,
1434 };
1435
1436 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1437 #define x(f, nr)                                        \
1438         [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1439         BCH_SB_FIELDS()
1440 #undef x
1441 };
1442
1443 static const struct bch_sb_field_ops bch2_sb_field_null_ops = {
1444         NULL
1445 };
1446
1447 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1448 {
1449         return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1450                 ? bch2_sb_field_ops[type]
1451                 : &bch2_sb_field_null_ops;
1452 }
1453
1454 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1455                                   struct printbuf *err)
1456 {
1457         unsigned type = le32_to_cpu(f->type);
1458         struct printbuf field_err = PRINTBUF;
1459         const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1460         int ret;
1461
1462         ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1463         if (ret) {
1464                 prt_printf(err, "Invalid superblock section %s: %s",
1465                            bch2_sb_fields[type], field_err.buf);
1466                 prt_newline(err);
1467                 bch2_sb_field_to_text(err, sb, f);
1468         }
1469
1470         printbuf_exit(&field_err);
1471         return ret;
1472 }
1473
1474 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1475                            struct bch_sb_field *f)
1476 {
1477         unsigned type = le32_to_cpu(f->type);
1478         const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1479
1480         if (!out->nr_tabstops)
1481                 printbuf_tabstop_push(out, 32);
1482
1483         if (type < BCH_SB_FIELD_NR)
1484                 prt_printf(out, "%s", bch2_sb_fields[type]);
1485         else
1486                 prt_printf(out, "(unknown field %u)", type);
1487
1488         prt_printf(out, " (size %zu):", vstruct_bytes(f));
1489         prt_newline(out);
1490
1491         if (ops->to_text) {
1492                 printbuf_indent_add(out, 2);
1493                 ops->to_text(out, sb, f);
1494                 printbuf_indent_sub(out, 2);
1495         }
1496 }
1497
1498 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1499 {
1500         unsigned i;
1501
1502         prt_printf(out, "Type:                    %u", l->layout_type);
1503         prt_newline(out);
1504
1505         prt_str(out, "Superblock max size:     ");
1506         prt_units_u64(out, 512 << l->sb_max_size_bits);
1507         prt_newline(out);
1508
1509         prt_printf(out, "Nr superblocks:          %u", l->nr_superblocks);
1510         prt_newline(out);
1511
1512         prt_str(out, "Offsets:                 ");
1513         for (i = 0; i < l->nr_superblocks; i++) {
1514                 if (i)
1515                         prt_str(out, ", ");
1516                 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1517         }
1518         prt_newline(out);
1519 }
1520
1521 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1522                      bool print_layout, unsigned fields)
1523 {
1524         struct bch_sb_field_members *mi;
1525         struct bch_sb_field *f;
1526         u64 fields_have = 0;
1527         unsigned nr_devices = 0;
1528
1529         if (!out->nr_tabstops)
1530                 printbuf_tabstop_push(out, 44);
1531
1532         mi = bch2_sb_get_members(sb);
1533         if (mi) {
1534                 struct bch_member *m;
1535
1536                 for (m = mi->members;
1537                      m < mi->members + sb->nr_devices;
1538                      m++)
1539                         nr_devices += bch2_member_exists(m);
1540         }
1541
1542         prt_printf(out, "External UUID:");
1543         prt_tab(out);
1544         pr_uuid(out, sb->user_uuid.b);
1545         prt_newline(out);
1546
1547         prt_printf(out, "Internal UUID:");
1548         prt_tab(out);
1549         pr_uuid(out, sb->uuid.b);
1550         prt_newline(out);
1551
1552         prt_str(out, "Device index:");
1553         prt_tab(out);
1554         prt_printf(out, "%u", sb->dev_idx);
1555         prt_newline(out);
1556
1557         prt_str(out, "Label:");
1558         prt_tab(out);
1559         prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1560         prt_newline(out);
1561
1562         prt_str(out, "Version:");
1563         prt_tab(out);
1564         bch2_version_to_text(out, le16_to_cpu(sb->version));
1565         prt_newline(out);
1566
1567         prt_str(out, "Version upgrade complete:");
1568         prt_tab(out);
1569         bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1570         prt_newline(out);
1571
1572         prt_printf(out, "Oldest version on disk:");
1573         prt_tab(out);
1574         bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1575         prt_newline(out);
1576
1577         prt_printf(out, "Created:");
1578         prt_tab(out);
1579         if (sb->time_base_lo)
1580                 pr_time(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1581         else
1582                 prt_printf(out, "(not set)");
1583         prt_newline(out);
1584
1585         prt_printf(out, "Sequence number:");
1586         prt_tab(out);
1587         prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1588         prt_newline(out);
1589
1590         prt_printf(out, "Superblock size:");
1591         prt_tab(out);
1592         prt_printf(out, "%zu", vstruct_bytes(sb));
1593         prt_newline(out);
1594
1595         prt_printf(out, "Clean:");
1596         prt_tab(out);
1597         prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1598         prt_newline(out);
1599
1600         prt_printf(out, "Devices:");
1601         prt_tab(out);
1602         prt_printf(out, "%u", nr_devices);
1603         prt_newline(out);
1604
1605         prt_printf(out, "Sections:");
1606         vstruct_for_each(sb, f)
1607                 fields_have |= 1 << le32_to_cpu(f->type);
1608         prt_tab(out);
1609         prt_bitflags(out, bch2_sb_fields, fields_have);
1610         prt_newline(out);
1611
1612         prt_printf(out, "Features:");
1613         prt_tab(out);
1614         prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1615         prt_newline(out);
1616
1617         prt_printf(out, "Compat features:");
1618         prt_tab(out);
1619         prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1620         prt_newline(out);
1621
1622         prt_newline(out);
1623         prt_printf(out, "Options:");
1624         prt_newline(out);
1625         printbuf_indent_add(out, 2);
1626         {
1627                 enum bch_opt_id id;
1628
1629                 for (id = 0; id < bch2_opts_nr; id++) {
1630                         const struct bch_option *opt = bch2_opt_table + id;
1631
1632                         if (opt->get_sb != BCH2_NO_SB_OPT) {
1633                                 u64 v = bch2_opt_from_sb(sb, id);
1634
1635                                 prt_printf(out, "%s:", opt->attr.name);
1636                                 prt_tab(out);
1637                                 bch2_opt_to_text(out, NULL, sb, opt, v,
1638                                                  OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1639                                 prt_newline(out);
1640                         }
1641                 }
1642         }
1643
1644         printbuf_indent_sub(out, 2);
1645
1646         if (print_layout) {
1647                 prt_newline(out);
1648                 prt_printf(out, "layout:");
1649                 prt_newline(out);
1650                 printbuf_indent_add(out, 2);
1651                 bch2_sb_layout_to_text(out, &sb->layout);
1652                 printbuf_indent_sub(out, 2);
1653         }
1654
1655         vstruct_for_each(sb, f)
1656                 if (fields & (1 << le32_to_cpu(f->type))) {
1657                         prt_newline(out);
1658                         bch2_sb_field_to_text(out, sb, f);
1659                 }
1660 }