]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.c
Update bcachefs sources to 2cb70a82bc bcachefs: delete some debug code
[bcachefs-tools-debian] / libbcachefs / super-io.c
1
2 #include "bcachefs.h"
3 #include "checksum.h"
4 #include "disk_groups.h"
5 #include "error.h"
6 #include "io.h"
7 #include "journal.h"
8 #include "replicas.h"
9 #include "quota.h"
10 #include "super-io.h"
11 #include "super.h"
12 #include "vstructs.h"
13
14 #include <linux/backing-dev.h>
15 #include <linux/sort.h>
16
17 const char * const bch2_sb_fields[] = {
18 #define x(name, nr)     #name,
19         BCH_SB_FIELDS()
20 #undef x
21         NULL
22 };
23
24 static const char *bch2_sb_field_validate(struct bch_sb *,
25                                           struct bch_sb_field *);
26
27 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
28                                       enum bch_sb_field_type type)
29 {
30         struct bch_sb_field *f;
31
32         /* XXX: need locking around superblock to access optional fields */
33
34         vstruct_for_each(sb, f)
35                 if (le32_to_cpu(f->type) == type)
36                         return f;
37         return NULL;
38 }
39
40 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
41                                                    struct bch_sb_field *f,
42                                                    unsigned u64s)
43 {
44         unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
45         unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
46
47         BUG_ON(get_order(__vstruct_bytes(struct bch_sb, sb_u64s)) >
48                sb->page_order);
49
50         if (!f) {
51                 f = vstruct_last(sb->sb);
52                 memset(f, 0, sizeof(u64) * u64s);
53                 f->u64s = cpu_to_le32(u64s);
54                 f->type = 0;
55         } else {
56                 void *src, *dst;
57
58                 src = vstruct_end(f);
59                 f->u64s = cpu_to_le32(u64s);
60                 dst = vstruct_end(f);
61
62                 memmove(dst, src, vstruct_end(sb->sb) - src);
63
64                 if (dst > src)
65                         memset(src, 0, dst - src);
66         }
67
68         sb->sb->u64s = cpu_to_le32(sb_u64s);
69
70         return f;
71 }
72
73 /* Superblock realloc/free: */
74
75 void bch2_free_super(struct bch_sb_handle *sb)
76 {
77         if (sb->bio)
78                 bio_put(sb->bio);
79         if (!IS_ERR_OR_NULL(sb->bdev))
80                 blkdev_put(sb->bdev, sb->mode);
81
82         free_pages((unsigned long) sb->sb, sb->page_order);
83         memset(sb, 0, sizeof(*sb));
84 }
85
86 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
87 {
88         size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
89         unsigned order = get_order(new_bytes);
90         struct bch_sb *new_sb;
91         struct bio *bio;
92
93         if (sb->sb && sb->page_order >= order)
94                 return 0;
95
96         if (sb->have_layout) {
97                 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
98
99                 if (new_bytes > max_bytes) {
100                         char buf[BDEVNAME_SIZE];
101
102                         pr_err("%s: superblock too big: want %zu but have %llu",
103                                bdevname(sb->bdev, buf), new_bytes, max_bytes);
104                         return -ENOSPC;
105                 }
106         }
107
108         if (sb->page_order >= order && sb->sb)
109                 return 0;
110
111         if (dynamic_fault("bcachefs:add:super_realloc"))
112                 return -ENOMEM;
113
114         if (sb->have_bio) {
115                 bio = bio_kmalloc(GFP_KERNEL, 1 << order);
116                 if (!bio)
117                         return -ENOMEM;
118
119                 if (sb->bio)
120                         bio_put(sb->bio);
121                 sb->bio = bio;
122         }
123
124         new_sb = (void *) __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
125         if (!new_sb)
126                 return -ENOMEM;
127
128         if (sb->sb)
129                 memcpy(new_sb, sb->sb, PAGE_SIZE << sb->page_order);
130
131         free_pages((unsigned long) sb->sb, sb->page_order);
132         sb->sb = new_sb;
133
134         sb->page_order = order;
135
136         return 0;
137 }
138
139 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb,
140                                           enum bch_sb_field_type type,
141                                           unsigned u64s)
142 {
143         struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
144         ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
145         ssize_t d = -old_u64s + u64s;
146
147         if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
148                 return NULL;
149
150         if (sb->fs_sb) {
151                 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
152                 struct bch_dev *ca;
153                 unsigned i;
154
155                 lockdep_assert_held(&c->sb_lock);
156
157                 /* XXX: we're not checking that offline device have enough space */
158
159                 for_each_online_member(ca, c, i) {
160                         struct bch_sb_handle *sb = &ca->disk_sb;
161
162                         if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) {
163                                 percpu_ref_put(&ca->ref);
164                                 return NULL;
165                         }
166                 }
167         }
168
169         f = __bch2_sb_field_resize(sb, f, u64s);
170         f->type = cpu_to_le32(type);
171         return f;
172 }
173
174 /* Superblock validate: */
175
176 static inline void __bch2_sb_layout_size_assert(void)
177 {
178         BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
179 }
180
181 static const char *validate_sb_layout(struct bch_sb_layout *layout)
182 {
183         u64 offset, prev_offset, max_sectors;
184         unsigned i;
185
186         if (uuid_le_cmp(layout->magic, BCACHE_MAGIC))
187                 return "Not a bcachefs superblock layout";
188
189         if (layout->layout_type != 0)
190                 return "Invalid superblock layout type";
191
192         if (!layout->nr_superblocks)
193                 return "Invalid superblock layout: no superblocks";
194
195         if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset))
196                 return "Invalid superblock layout: too many superblocks";
197
198         max_sectors = 1 << layout->sb_max_size_bits;
199
200         prev_offset = le64_to_cpu(layout->sb_offset[0]);
201
202         for (i = 1; i < layout->nr_superblocks; i++) {
203                 offset = le64_to_cpu(layout->sb_offset[i]);
204
205                 if (offset < prev_offset + max_sectors)
206                         return "Invalid superblock layout: superblocks overlap";
207                 prev_offset = offset;
208         }
209
210         return NULL;
211 }
212
213 const char *bch2_sb_validate(struct bch_sb_handle *disk_sb)
214 {
215         struct bch_sb *sb = disk_sb->sb;
216         struct bch_sb_field *f;
217         struct bch_sb_field_members *mi;
218         const char *err;
219         u16 block_size;
220
221         if (le64_to_cpu(sb->version) < BCH_SB_VERSION_MIN ||
222             le64_to_cpu(sb->version) > BCH_SB_VERSION_MAX)
223                 return"Unsupported superblock version";
224
225         if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_MAX) {
226                 SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb, 7);
227                 SET_BCH_SB_POSIX_ACL(sb, 1);
228         }
229
230         block_size = le16_to_cpu(sb->block_size);
231
232         if (!is_power_of_2(block_size) ||
233             block_size > PAGE_SECTORS)
234                 return "Bad block size";
235
236         if (bch2_is_zero(sb->user_uuid.b, sizeof(uuid_le)))
237                 return "Bad user UUID";
238
239         if (bch2_is_zero(sb->uuid.b, sizeof(uuid_le)))
240                 return "Bad internal UUID";
241
242         if (!sb->nr_devices ||
243             sb->nr_devices <= sb->dev_idx ||
244             sb->nr_devices > BCH_SB_MEMBERS_MAX)
245                 return "Bad number of member devices";
246
247         if (!BCH_SB_META_REPLICAS_WANT(sb) ||
248             BCH_SB_META_REPLICAS_WANT(sb) >= BCH_REPLICAS_MAX)
249                 return "Invalid number of metadata replicas";
250
251         if (!BCH_SB_META_REPLICAS_REQ(sb) ||
252             BCH_SB_META_REPLICAS_REQ(sb) >= BCH_REPLICAS_MAX)
253                 return "Invalid number of metadata replicas";
254
255         if (!BCH_SB_DATA_REPLICAS_WANT(sb) ||
256             BCH_SB_DATA_REPLICAS_WANT(sb) >= BCH_REPLICAS_MAX)
257                 return "Invalid number of data replicas";
258
259         if (!BCH_SB_DATA_REPLICAS_REQ(sb) ||
260             BCH_SB_DATA_REPLICAS_REQ(sb) >= BCH_REPLICAS_MAX)
261                 return "Invalid number of data replicas";
262
263         if (BCH_SB_META_CSUM_TYPE(sb) >= BCH_CSUM_OPT_NR)
264                 return "Invalid metadata checksum type";
265
266         if (BCH_SB_DATA_CSUM_TYPE(sb) >= BCH_CSUM_OPT_NR)
267                 return "Invalid metadata checksum type";
268
269         if (BCH_SB_COMPRESSION_TYPE(sb) >= BCH_COMPRESSION_OPT_NR)
270                 return "Invalid compression type";
271
272         if (!BCH_SB_BTREE_NODE_SIZE(sb))
273                 return "Btree node size not set";
274
275         if (!is_power_of_2(BCH_SB_BTREE_NODE_SIZE(sb)))
276                 return "Btree node size not a power of two";
277
278         if (BCH_SB_GC_RESERVE(sb) < 5)
279                 return "gc reserve percentage too small";
280
281         if (!sb->time_precision ||
282             le32_to_cpu(sb->time_precision) > NSEC_PER_SEC)
283                 return "invalid time precision";
284
285         /* validate layout */
286         err = validate_sb_layout(&sb->layout);
287         if (err)
288                 return err;
289
290         vstruct_for_each(sb, f) {
291                 if (!f->u64s)
292                         return "Invalid superblock: invalid optional field";
293
294                 if (vstruct_next(f) > vstruct_last(sb))
295                         return "Invalid superblock: invalid optional field";
296         }
297
298         /* members must be validated first: */
299         mi = bch2_sb_get_members(sb);
300         if (!mi)
301                 return "Invalid superblock: member info area missing";
302
303         err = bch2_sb_field_validate(sb, &mi->field);
304         if (err)
305                 return err;
306
307         vstruct_for_each(sb, f) {
308                 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members)
309                         continue;
310
311                 err = bch2_sb_field_validate(sb, f);
312                 if (err)
313                         return err;
314         }
315
316         if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_NONCE_V1 &&
317             bch2_sb_get_crypt(sb) &&
318             BCH_SB_INITIALIZED(sb))
319                 return "Incompatible extent nonces";
320
321         sb->version = cpu_to_le64(BCH_SB_VERSION_MAX);
322
323         return NULL;
324 }
325
326 /* device open: */
327
328 static void bch2_sb_update(struct bch_fs *c)
329 {
330         struct bch_sb *src = c->disk_sb.sb;
331         struct bch_sb_field_members *mi = bch2_sb_get_members(src);
332         struct bch_dev *ca;
333         unsigned i;
334
335         lockdep_assert_held(&c->sb_lock);
336
337         c->sb.uuid              = src->uuid;
338         c->sb.user_uuid         = src->user_uuid;
339         c->sb.nr_devices        = src->nr_devices;
340         c->sb.clean             = BCH_SB_CLEAN(src);
341         c->sb.encryption_type   = BCH_SB_ENCRYPTION_TYPE(src);
342         c->sb.encoded_extent_max= 1 << BCH_SB_ENCODED_EXTENT_MAX_BITS(src);
343         c->sb.time_base_lo      = le64_to_cpu(src->time_base_lo);
344         c->sb.time_base_hi      = le32_to_cpu(src->time_base_hi);
345         c->sb.time_precision    = le32_to_cpu(src->time_precision);
346         c->sb.features          = le64_to_cpu(src->features[0]);
347
348         for_each_member_device(ca, c, i)
349                 ca->mi = bch2_mi_to_cpu(mi->members + i);
350 }
351
352 /* doesn't copy member info */
353 static void __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
354 {
355         struct bch_sb_field *src_f, *dst_f;
356         struct bch_sb *dst = dst_handle->sb;
357
358         dst->version            = src->version;
359         dst->seq                = src->seq;
360         dst->uuid               = src->uuid;
361         dst->user_uuid          = src->user_uuid;
362         memcpy(dst->label,      src->label, sizeof(dst->label));
363
364         dst->block_size         = src->block_size;
365         dst->nr_devices         = src->nr_devices;
366
367         dst->time_base_lo       = src->time_base_lo;
368         dst->time_base_hi       = src->time_base_hi;
369         dst->time_precision     = src->time_precision;
370
371         memcpy(dst->flags,      src->flags,     sizeof(dst->flags));
372         memcpy(dst->features,   src->features,  sizeof(dst->features));
373         memcpy(dst->compat,     src->compat,    sizeof(dst->compat));
374
375         vstruct_for_each(src, src_f) {
376                 if (src_f->type == BCH_SB_FIELD_journal)
377                         continue;
378
379                 dst_f = bch2_sb_field_get(dst, le32_to_cpu(src_f->type));
380                 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
381                                                le32_to_cpu(src_f->u64s));
382
383                 memcpy(dst_f, src_f, vstruct_bytes(src_f));
384         }
385 }
386
387 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
388 {
389         struct bch_sb_field_journal *journal_buckets =
390                 bch2_sb_get_journal(src);
391         unsigned journal_u64s = journal_buckets
392                 ? le32_to_cpu(journal_buckets->field.u64s)
393                 : 0;
394         int ret;
395
396         lockdep_assert_held(&c->sb_lock);
397
398         ret = bch2_sb_realloc(&c->disk_sb,
399                               le32_to_cpu(src->u64s) - journal_u64s);
400         if (ret)
401                 return ret;
402
403         __copy_super(&c->disk_sb, src);
404
405         ret = bch2_sb_replicas_to_cpu_replicas(c);
406         if (ret)
407                 return ret;
408
409         ret = bch2_sb_disk_groups_to_cpu(c);
410         if (ret)
411                 return ret;
412
413         bch2_sb_update(c);
414         return 0;
415 }
416
417 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
418 {
419         struct bch_sb *src = c->disk_sb.sb, *dst = ca->disk_sb.sb;
420         struct bch_sb_field_journal *journal_buckets =
421                 bch2_sb_get_journal(dst);
422         unsigned journal_u64s = journal_buckets
423                 ? le32_to_cpu(journal_buckets->field.u64s)
424                 : 0;
425         unsigned u64s = le32_to_cpu(src->u64s) + journal_u64s;
426         int ret;
427
428         ret = bch2_sb_realloc(&ca->disk_sb, u64s);
429         if (ret)
430                 return ret;
431
432         __copy_super(&ca->disk_sb, src);
433         return 0;
434 }
435
436 /* read superblock: */
437
438 static const char *read_one_super(struct bch_sb_handle *sb, u64 offset)
439 {
440         struct bch_csum csum;
441         size_t bytes;
442 reread:
443         bio_reset(sb->bio);
444         bio_set_dev(sb->bio, sb->bdev);
445         sb->bio->bi_iter.bi_sector = offset;
446         sb->bio->bi_iter.bi_size = PAGE_SIZE << sb->page_order;
447         bio_set_op_attrs(sb->bio, REQ_OP_READ, REQ_SYNC|REQ_META);
448         bch2_bio_map(sb->bio, sb->sb);
449
450         if (submit_bio_wait(sb->bio))
451                 return "IO error";
452
453         if (uuid_le_cmp(sb->sb->magic, BCACHE_MAGIC))
454                 return "Not a bcachefs superblock";
455
456         if (le64_to_cpu(sb->sb->version) < BCH_SB_VERSION_MIN ||
457             le64_to_cpu(sb->sb->version) > BCH_SB_VERSION_MAX)
458                 return"Unsupported superblock version";
459
460         bytes = vstruct_bytes(sb->sb);
461
462         if (bytes > 512 << sb->sb->layout.sb_max_size_bits)
463                 return "Bad superblock: too big";
464
465         if (get_order(bytes) > sb->page_order) {
466                 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s)))
467                         return "cannot allocate memory";
468                 goto reread;
469         }
470
471         if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR)
472                 return "unknown csum type";
473
474         /* XXX: verify MACs */
475         csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
476                             null_nonce(), sb->sb);
477
478         if (bch2_crc_cmp(csum, sb->sb->csum))
479                 return "bad checksum reading superblock";
480
481         return NULL;
482 }
483
484 int bch2_read_super(const char *path, struct bch_opts *opts,
485                     struct bch_sb_handle *sb)
486 {
487         u64 offset = opt_get(*opts, sb);
488         struct bch_sb_layout layout;
489         const char *err;
490         __le64 *i;
491         int ret;
492
493         pr_verbose_init(*opts, "");
494
495         memset(sb, 0, sizeof(*sb));
496         sb->mode        = FMODE_READ;
497         sb->have_bio    = true;
498
499         if (!opt_get(*opts, noexcl))
500                 sb->mode |= FMODE_EXCL;
501
502         if (!opt_get(*opts, nochanges))
503                 sb->mode |= FMODE_WRITE;
504
505         sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
506         if (IS_ERR(sb->bdev) &&
507             PTR_ERR(sb->bdev) == -EACCES &&
508             opt_get(*opts, read_only)) {
509                 sb->mode &= ~FMODE_WRITE;
510
511                 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
512                 if (!IS_ERR(sb->bdev))
513                         opt_set(*opts, nochanges, true);
514         }
515
516         if (IS_ERR(sb->bdev)) {
517                 ret = PTR_ERR(sb->bdev);
518                 goto out;
519         }
520
521         err = "cannot allocate memory";
522         ret = bch2_sb_realloc(sb, 0);
523         if (ret)
524                 goto err;
525
526         ret = -EFAULT;
527         err = "dynamic fault";
528         if (bch2_fs_init_fault("read_super"))
529                 goto err;
530
531         ret = -EINVAL;
532         err = read_one_super(sb, offset);
533         if (!err)
534                 goto got_super;
535
536         if (opt_defined(*opts, sb))
537                 goto err;
538
539         pr_err("error reading default superblock: %s", err);
540
541         /*
542          * Error reading primary superblock - read location of backup
543          * superblocks:
544          */
545         bio_reset(sb->bio);
546         bio_set_dev(sb->bio, sb->bdev);
547         sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
548         sb->bio->bi_iter.bi_size = sizeof(struct bch_sb_layout);
549         bio_set_op_attrs(sb->bio, REQ_OP_READ, REQ_SYNC|REQ_META);
550         /*
551          * use sb buffer to read layout, since sb buffer is page aligned but
552          * layout won't be:
553          */
554         bch2_bio_map(sb->bio, sb->sb);
555
556         err = "IO error";
557         if (submit_bio_wait(sb->bio))
558                 goto err;
559
560         memcpy(&layout, sb->sb, sizeof(layout));
561         err = validate_sb_layout(&layout);
562         if (err)
563                 goto err;
564
565         for (i = layout.sb_offset;
566              i < layout.sb_offset + layout.nr_superblocks; i++) {
567                 offset = le64_to_cpu(*i);
568
569                 if (offset == opt_get(*opts, sb))
570                         continue;
571
572                 err = read_one_super(sb, offset);
573                 if (!err)
574                         goto got_super;
575         }
576
577         ret = -EINVAL;
578         goto err;
579
580 got_super:
581         err = "Superblock block size smaller than device block size";
582         ret = -EINVAL;
583         if (le16_to_cpu(sb->sb->block_size) << 9 <
584             bdev_logical_block_size(sb->bdev))
585                 goto err;
586
587         if (sb->mode & FMODE_WRITE)
588                 bdev_get_queue(sb->bdev)->backing_dev_info->capabilities
589                         |= BDI_CAP_STABLE_WRITES;
590         ret = 0;
591         sb->have_layout = true;
592 out:
593         pr_verbose_init(*opts, "ret %i", ret);
594         return ret;
595 err:
596         bch2_free_super(sb);
597         pr_err("error reading superblock: %s", err);
598         goto out;
599 }
600
601 /* write superblock: */
602
603 static void write_super_endio(struct bio *bio)
604 {
605         struct bch_dev *ca = bio->bi_private;
606
607         /* XXX: return errors directly */
608
609         if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write"))
610                 ca->sb_write_error = 1;
611
612         closure_put(&ca->fs->sb_write);
613         percpu_ref_put(&ca->io_ref);
614 }
615
616 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
617 {
618         struct bch_sb *sb = ca->disk_sb.sb;
619         struct bio *bio = ca->disk_sb.bio;
620
621         sb->offset = sb->layout.sb_offset[idx];
622
623         SET_BCH_SB_CSUM_TYPE(sb, c->opts.metadata_checksum);
624         sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
625                                 null_nonce(), sb);
626
627         bio_reset(bio);
628         bio_set_dev(bio, ca->disk_sb.bdev);
629         bio->bi_iter.bi_sector  = le64_to_cpu(sb->offset);
630         bio->bi_iter.bi_size    =
631                 roundup((size_t) vstruct_bytes(sb),
632                         bdev_logical_block_size(ca->disk_sb.bdev));
633         bio->bi_end_io          = write_super_endio;
634         bio->bi_private         = ca;
635         bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC|REQ_META);
636         bch2_bio_map(bio, sb);
637
638         this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_SB],
639                      bio_sectors(bio));
640
641         percpu_ref_get(&ca->io_ref);
642         closure_bio_submit(bio, &c->sb_write);
643 }
644
645 void bch2_write_super(struct bch_fs *c)
646 {
647         struct closure *cl = &c->sb_write;
648         struct bch_dev *ca;
649         unsigned i, sb = 0, nr_wrote;
650         const char *err;
651         struct bch_devs_mask sb_written;
652         bool wrote, can_mount_without_written, can_mount_with_written;
653
654         lockdep_assert_held(&c->sb_lock);
655
656         closure_init_stack(cl);
657         memset(&sb_written, 0, sizeof(sb_written));
658
659         le64_add_cpu(&c->disk_sb.sb->seq, 1);
660
661         for_each_online_member(ca, c, i)
662                 bch2_sb_from_fs(c, ca);
663
664         for_each_online_member(ca, c, i) {
665                 err = bch2_sb_validate(&ca->disk_sb);
666                 if (err) {
667                         bch2_fs_inconsistent(c, "sb invalid before write: %s", err);
668                         goto out;
669                 }
670         }
671
672         if (c->opts.nochanges ||
673             test_bit(BCH_FS_ERROR, &c->flags))
674                 goto out;
675
676         for_each_online_member(ca, c, i) {
677                 __set_bit(ca->dev_idx, sb_written.d);
678                 ca->sb_write_error = 0;
679         }
680
681         do {
682                 wrote = false;
683                 for_each_online_member(ca, c, i)
684                         if (sb < ca->disk_sb.sb->layout.nr_superblocks) {
685                                 write_one_super(c, ca, sb);
686                                 wrote = true;
687                         }
688                 closure_sync(cl);
689                 sb++;
690         } while (wrote);
691
692         for_each_online_member(ca, c, i)
693                 if (ca->sb_write_error)
694                         __clear_bit(ca->dev_idx, sb_written.d);
695
696         nr_wrote = dev_mask_nr(&sb_written);
697
698         can_mount_with_written =
699                 bch2_have_enough_devs(__bch2_replicas_status(c, sb_written),
700                                       BCH_FORCE_IF_DEGRADED);
701
702         for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
703                 sb_written.d[i] = ~sb_written.d[i];
704
705         can_mount_without_written =
706                 bch2_have_enough_devs(__bch2_replicas_status(c, sb_written),
707                                       BCH_FORCE_IF_DEGRADED);
708
709         /*
710          * If we would be able to mount _without_ the devices we successfully
711          * wrote superblocks to, we weren't able to write to enough devices:
712          *
713          * Exception: if we can mount without the successes because we haven't
714          * written anything (new filesystem), we continue if we'd be able to
715          * mount with the devices we did successfully write to:
716          */
717         bch2_fs_fatal_err_on(!nr_wrote ||
718                              (can_mount_without_written &&
719                               !can_mount_with_written), c,
720                 "Unable to write superblock to sufficient devices");
721 out:
722         /* Make new options visible after they're persistent: */
723         bch2_sb_update(c);
724 }
725
726 /* BCH_SB_FIELD_journal: */
727
728 static int u64_cmp(const void *_l, const void *_r)
729 {
730         u64 l = *((const u64 *) _l), r = *((const u64 *) _r);
731
732         return l < r ? -1 : l > r ? 1 : 0;
733 }
734
735 static const char *bch2_sb_validate_journal(struct bch_sb *sb,
736                                             struct bch_sb_field *f)
737 {
738         struct bch_sb_field_journal *journal = field_to_type(f, journal);
739         struct bch_member *m = bch2_sb_get_members(sb)->members + sb->dev_idx;
740         const char *err;
741         unsigned nr;
742         unsigned i;
743         u64 *b;
744
745         journal = bch2_sb_get_journal(sb);
746         if (!journal)
747                 return NULL;
748
749         nr = bch2_nr_journal_buckets(journal);
750         if (!nr)
751                 return NULL;
752
753         b = kmalloc_array(sizeof(u64), nr, GFP_KERNEL);
754         if (!b)
755                 return "cannot allocate memory";
756
757         for (i = 0; i < nr; i++)
758                 b[i] = le64_to_cpu(journal->buckets[i]);
759
760         sort(b, nr, sizeof(u64), u64_cmp, NULL);
761
762         err = "journal bucket at sector 0";
763         if (!b[0])
764                 goto err;
765
766         err = "journal bucket before first bucket";
767         if (m && b[0] < le16_to_cpu(m->first_bucket))
768                 goto err;
769
770         err = "journal bucket past end of device";
771         if (m && b[nr - 1] >= le64_to_cpu(m->nbuckets))
772                 goto err;
773
774         err = "duplicate journal buckets";
775         for (i = 0; i + 1 < nr; i++)
776                 if (b[i] == b[i + 1])
777                         goto err;
778
779         err = NULL;
780 err:
781         kfree(b);
782         return err;
783 }
784
785 static const struct bch_sb_field_ops bch_sb_field_ops_journal = {
786         .validate       = bch2_sb_validate_journal,
787 };
788
789 /* BCH_SB_FIELD_members: */
790
791 static const char *bch2_sb_validate_members(struct bch_sb *sb,
792                                             struct bch_sb_field *f)
793 {
794         struct bch_sb_field_members *mi = field_to_type(f, members);
795         struct bch_member *m;
796
797         if ((void *) (mi->members + sb->nr_devices) >
798             vstruct_end(&mi->field))
799                 return "Invalid superblock: bad member info";
800
801         for (m = mi->members;
802              m < mi->members + sb->nr_devices;
803              m++) {
804                 if (!bch2_member_exists(m))
805                         continue;
806
807                 if (le64_to_cpu(m->nbuckets) > LONG_MAX)
808                         return "Too many buckets";
809
810                 if (le64_to_cpu(m->nbuckets) -
811                     le16_to_cpu(m->first_bucket) < 1 << 10)
812                         return "Not enough buckets";
813
814                 if (le16_to_cpu(m->bucket_size) <
815                     le16_to_cpu(sb->block_size))
816                         return "bucket size smaller than block size";
817
818                 if (le16_to_cpu(m->bucket_size) <
819                     BCH_SB_BTREE_NODE_SIZE(sb))
820                         return "bucket size smaller than btree node size";
821         }
822
823         if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_MAX)
824                 for (m = mi->members;
825                      m < mi->members + sb->nr_devices;
826                      m++)
827                         SET_BCH_MEMBER_DATA_ALLOWED(m, ~0);
828
829         return NULL;
830 }
831
832 static const struct bch_sb_field_ops bch_sb_field_ops_members = {
833         .validate       = bch2_sb_validate_members,
834 };
835
836 /* BCH_SB_FIELD_crypt: */
837
838 static const char *bch2_sb_validate_crypt(struct bch_sb *sb,
839                                           struct bch_sb_field *f)
840 {
841         struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
842
843         if (vstruct_bytes(&crypt->field) != sizeof(*crypt))
844                 return "invalid field crypt: wrong size";
845
846         if (BCH_CRYPT_KDF_TYPE(crypt))
847                 return "invalid field crypt: bad kdf type";
848
849         return NULL;
850 }
851
852 static const struct bch_sb_field_ops bch_sb_field_ops_crypt = {
853         .validate       = bch2_sb_validate_crypt,
854 };
855
856 /* BCH_SB_FIELD_clean: */
857
858 void bch2_fs_mark_clean(struct bch_fs *c, bool clean)
859 {
860         struct bch_sb_field_clean *sb_clean;
861         unsigned u64s = sizeof(*sb_clean) / sizeof(u64);
862         struct jset_entry *entry;
863         struct btree_root *r;
864
865         mutex_lock(&c->sb_lock);
866         if (clean == BCH_SB_CLEAN(c->disk_sb.sb))
867                 goto out;
868
869         SET_BCH_SB_CLEAN(c->disk_sb.sb, clean);
870
871         if (!clean)
872                 goto write_super;
873
874         mutex_lock(&c->btree_root_lock);
875
876         for (r = c->btree_roots;
877              r < c->btree_roots + BTREE_ID_NR;
878              r++)
879                 if (r->alive)
880                         u64s += jset_u64s(r->key.u64s);
881
882         sb_clean = bch2_sb_resize_clean(&c->disk_sb, u64s);
883         if (!sb_clean) {
884                 bch_err(c, "error resizing superblock while setting filesystem clean");
885                 goto out;
886         }
887
888         sb_clean->flags         = 0;
889         sb_clean->read_clock    = cpu_to_le16(c->bucket_clock[READ].hand);
890         sb_clean->write_clock   = cpu_to_le16(c->bucket_clock[WRITE].hand);
891         sb_clean->journal_seq   = journal_cur_seq(&c->journal) - 1;
892
893         entry = sb_clean->start;
894         memset(entry, 0,
895                vstruct_end(&sb_clean->field) - (void *) entry);
896
897         for (r = c->btree_roots;
898              r < c->btree_roots + BTREE_ID_NR;
899              r++)
900                 if (r->alive) {
901                         entry->u64s     = r->key.u64s;
902                         entry->btree_id = r - c->btree_roots;
903                         entry->level    = r->level;
904                         entry->type     = BCH_JSET_ENTRY_btree_root;
905                         bkey_copy(&entry->start[0], &r->key);
906                         entry = vstruct_next(entry);
907                         BUG_ON((void *) entry > vstruct_end(&sb_clean->field));
908                 }
909
910         BUG_ON(entry != vstruct_end(&sb_clean->field));
911
912         mutex_unlock(&c->btree_root_lock);
913 write_super:
914         bch2_write_super(c);
915 out:
916         mutex_unlock(&c->sb_lock);
917 }
918
919 static const char *bch2_sb_validate_clean(struct bch_sb *sb,
920                                           struct bch_sb_field *f)
921 {
922         struct bch_sb_field_clean *clean = field_to_type(f, clean);
923
924         if (vstruct_bytes(&clean->field) < sizeof(*clean))
925                 return "invalid field crypt: wrong size";
926
927         return NULL;
928 }
929
930 static const struct bch_sb_field_ops bch_sb_field_ops_clean = {
931         .validate       = bch2_sb_validate_clean,
932 };
933
934 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
935 #define x(f, nr)                                        \
936         [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
937         BCH_SB_FIELDS()
938 #undef x
939 };
940
941 static const char *bch2_sb_field_validate(struct bch_sb *sb,
942                                           struct bch_sb_field *f)
943 {
944         unsigned type = le32_to_cpu(f->type);
945
946         return type < BCH_SB_FIELD_NR
947                 ? bch2_sb_field_ops[type]->validate(sb, f)
948                 : NULL;
949 }
950
951 size_t bch2_sb_field_to_text(char *buf, size_t size,
952                              struct bch_sb *sb, struct bch_sb_field *f)
953 {
954         unsigned type = le32_to_cpu(f->type);
955         size_t (*to_text)(char *, size_t, struct bch_sb *,
956                                    struct bch_sb_field *) =
957                 type < BCH_SB_FIELD_NR
958                 ? bch2_sb_field_ops[type]->to_text
959                 : NULL;
960
961         if (!to_text) {
962                 if (size)
963                         buf[0] = '\0';
964                 return 0;
965         }
966
967         return to_text(buf, size, sb, f);
968 }