]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.c
1f266ba37386ba7763aa13d5c1dff115762861c4
[bcachefs-tools-debian] / libbcachefs / super-io.c
1
2 #include "bcachefs.h"
3 #include "checksum.h"
4 #include "error.h"
5 #include "io.h"
6 #include "super-io.h"
7 #include "super.h"
8 #include "vstructs.h"
9
10 #include <linux/backing-dev.h>
11 #include <linux/sort.h>
12
13 static int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *);
14 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *,
15                                             struct bch_replicas_cpu *);
16
17 /* superblock fields (optional/variable size sections: */
18
19 const char * const bch2_sb_fields[] = {
20 #define x(name, nr)     #name,
21         BCH_SB_FIELDS()
22 #undef x
23         NULL
24 };
25
26 #define x(f, nr)                                        \
27 static const char *bch2_sb_validate_##f(struct bch_sb *, struct bch_sb_field *);
28         BCH_SB_FIELDS()
29 #undef x
30
31 struct bch_sb_field_ops {
32         const char *    (*validate)(struct bch_sb *, struct bch_sb_field *);
33 };
34
35 static const struct bch_sb_field_ops bch2_sb_field_ops[] = {
36 #define x(f, nr)                                        \
37         [BCH_SB_FIELD_##f] = {                          \
38                 .validate = bch2_sb_validate_##f,       \
39         },
40         BCH_SB_FIELDS()
41 #undef x
42 };
43
44 static const char *bch2_sb_field_validate(struct bch_sb *sb,
45                                           struct bch_sb_field *f)
46 {
47         unsigned type = le32_to_cpu(f->type);
48
49         return type < BCH_SB_FIELD_NR
50                 ? bch2_sb_field_ops[type].validate(sb, f)
51                 : NULL;
52 }
53
54 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *sb,
55                                       enum bch_sb_field_type type)
56 {
57         struct bch_sb_field *f;
58
59         /* XXX: need locking around superblock to access optional fields */
60
61         vstruct_for_each(sb, f)
62                 if (le32_to_cpu(f->type) == type)
63                         return f;
64         return NULL;
65 }
66
67 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb *sb,
68                                                   struct bch_sb_field *f,
69                                                   unsigned u64s)
70 {
71         unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
72
73         if (!f) {
74                 f = vstruct_last(sb);
75                 memset(f, 0, sizeof(u64) * u64s);
76                 f->u64s = cpu_to_le32(u64s);
77                 f->type = 0;
78         } else {
79                 void *src, *dst;
80
81                 src = vstruct_end(f);
82                 f->u64s = cpu_to_le32(u64s);
83                 dst = vstruct_end(f);
84
85                 memmove(dst, src, vstruct_end(sb) - src);
86
87                 if (dst > src)
88                         memset(src, 0, dst - src);
89         }
90
91         le32_add_cpu(&sb->u64s, u64s - old_u64s);
92
93         return f;
94 }
95
96 /* Superblock realloc/free: */
97
98 void bch2_free_super(struct bch_sb_handle *sb)
99 {
100         if (sb->bio)
101                 bio_put(sb->bio);
102         if (!IS_ERR_OR_NULL(sb->bdev))
103                 blkdev_put(sb->bdev, sb->mode);
104
105         free_pages((unsigned long) sb->sb, sb->page_order);
106         memset(sb, 0, sizeof(*sb));
107 }
108
109 static int __bch2_super_realloc(struct bch_sb_handle *sb, unsigned order)
110 {
111         struct bch_sb *new_sb;
112         struct bio *bio;
113
114         if (sb->page_order >= order && sb->sb)
115                 return 0;
116
117         if (dynamic_fault("bcachefs:add:super_realloc"))
118                 return -ENOMEM;
119
120         bio = bio_kmalloc(GFP_KERNEL, 1 << order);
121         if (!bio)
122                 return -ENOMEM;
123
124         if (sb->bio)
125                 bio_put(sb->bio);
126         sb->bio = bio;
127
128         new_sb = (void *) __get_free_pages(GFP_KERNEL, order);
129         if (!new_sb)
130                 return -ENOMEM;
131
132         if (sb->sb)
133                 memcpy(new_sb, sb->sb, PAGE_SIZE << sb->page_order);
134
135         free_pages((unsigned long) sb->sb, sb->page_order);
136         sb->sb = new_sb;
137
138         sb->page_order = order;
139
140         return 0;
141 }
142
143 static int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
144 {
145         u64 new_bytes = __vstruct_bytes(struct bch_sb, u64s);
146         u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
147
148         if (new_bytes > max_bytes) {
149                 char buf[BDEVNAME_SIZE];
150
151                 pr_err("%s: superblock too big: want %llu but have %llu",
152                        bdevname(sb->bdev, buf), new_bytes, max_bytes);
153                 return -ENOSPC;
154         }
155
156         return __bch2_super_realloc(sb, get_order(new_bytes));
157 }
158
159 static int bch2_fs_sb_realloc(struct bch_fs *c, unsigned u64s)
160 {
161         u64 bytes = __vstruct_bytes(struct bch_sb, u64s);
162         struct bch_sb *sb;
163         unsigned order = get_order(bytes);
164
165         if (c->disk_sb && order <= c->disk_sb_order)
166                 return 0;
167
168         sb = (void *) __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
169         if (!sb)
170                 return -ENOMEM;
171
172         if (c->disk_sb)
173                 memcpy(sb, c->disk_sb, PAGE_SIZE << c->disk_sb_order);
174
175         free_pages((unsigned long) c->disk_sb, c->disk_sb_order);
176
177         c->disk_sb = sb;
178         c->disk_sb_order = order;
179         return 0;
180 }
181
182 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb,
183                                           enum bch_sb_field_type type,
184                                           unsigned u64s)
185 {
186         struct bch_sb_field *f = bch2_sb_field_get(sb->sb, type);
187         ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
188         ssize_t d = -old_u64s + u64s;
189
190         if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
191                 return NULL;
192
193         f = __bch2_sb_field_resize(sb->sb, f, u64s);
194         f->type = cpu_to_le32(type);
195         return f;
196 }
197
198 struct bch_sb_field *bch2_fs_sb_field_resize(struct bch_fs *c,
199                                             enum bch_sb_field_type type,
200                                             unsigned u64s)
201 {
202         struct bch_sb_field *f = bch2_sb_field_get(c->disk_sb, type);
203         ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
204         ssize_t d = -old_u64s + u64s;
205         struct bch_dev *ca;
206         unsigned i;
207
208         lockdep_assert_held(&c->sb_lock);
209
210         if (bch2_fs_sb_realloc(c, le32_to_cpu(c->disk_sb->u64s) + d))
211                 return NULL;
212
213         /* XXX: we're not checking that offline device have enough space */
214
215         for_each_online_member(ca, c, i) {
216                 struct bch_sb_handle *sb = &ca->disk_sb;
217
218                 if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d)) {
219                         percpu_ref_put(&ca->ref);
220                         return NULL;
221                 }
222         }
223
224         f = __bch2_sb_field_resize(c->disk_sb, f, u64s);
225         f->type = cpu_to_le32(type);
226         return f;
227 }
228
229 /* Superblock validate: */
230
231 static inline void __bch2_sb_layout_size_assert(void)
232 {
233         BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
234 }
235
236 static const char *validate_sb_layout(struct bch_sb_layout *layout)
237 {
238         u64 offset, prev_offset, max_sectors;
239         unsigned i;
240
241         if (uuid_le_cmp(layout->magic, BCACHE_MAGIC))
242                 return "Not a bcachefs superblock layout";
243
244         if (layout->layout_type != 0)
245                 return "Invalid superblock layout type";
246
247         if (!layout->nr_superblocks)
248                 return "Invalid superblock layout: no superblocks";
249
250         if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset))
251                 return "Invalid superblock layout: too many superblocks";
252
253         max_sectors = 1 << layout->sb_max_size_bits;
254
255         prev_offset = le64_to_cpu(layout->sb_offset[0]);
256
257         for (i = 1; i < layout->nr_superblocks; i++) {
258                 offset = le64_to_cpu(layout->sb_offset[i]);
259
260                 if (offset < prev_offset + max_sectors)
261                         return "Invalid superblock layout: superblocks overlap";
262                 prev_offset = offset;
263         }
264
265         return NULL;
266 }
267
268 const char *bch2_sb_validate(struct bch_sb_handle *disk_sb)
269 {
270         struct bch_sb *sb = disk_sb->sb;
271         struct bch_sb_field *f;
272         struct bch_sb_field_members *mi;
273         const char *err;
274         u16 block_size;
275
276         if (le64_to_cpu(sb->version) < BCH_SB_VERSION_MIN ||
277             le64_to_cpu(sb->version) > BCH_SB_VERSION_MAX)
278                 return"Unsupported superblock version";
279
280         if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_MAX) {
281                 SET_BCH_SB_ENCODED_EXTENT_MAX_BITS(sb, 7);
282                 SET_BCH_SB_POSIX_ACL(sb, 1);
283         }
284
285         block_size = le16_to_cpu(sb->block_size);
286
287         if (!is_power_of_2(block_size) ||
288             block_size > PAGE_SECTORS)
289                 return "Bad block size";
290
291         if (bch2_is_zero(sb->user_uuid.b, sizeof(uuid_le)))
292                 return "Bad user UUID";
293
294         if (bch2_is_zero(sb->uuid.b, sizeof(uuid_le)))
295                 return "Bad internal UUID";
296
297         if (!sb->nr_devices ||
298             sb->nr_devices <= sb->dev_idx ||
299             sb->nr_devices > BCH_SB_MEMBERS_MAX)
300                 return "Bad cache device number in set";
301
302         if (!BCH_SB_META_REPLICAS_WANT(sb) ||
303             BCH_SB_META_REPLICAS_WANT(sb) >= BCH_REPLICAS_MAX)
304                 return "Invalid number of metadata replicas";
305
306         if (!BCH_SB_META_REPLICAS_REQ(sb) ||
307             BCH_SB_META_REPLICAS_REQ(sb) >= BCH_REPLICAS_MAX)
308                 return "Invalid number of metadata replicas";
309
310         if (!BCH_SB_DATA_REPLICAS_WANT(sb) ||
311             BCH_SB_DATA_REPLICAS_WANT(sb) >= BCH_REPLICAS_MAX)
312                 return "Invalid number of data replicas";
313
314         if (!BCH_SB_DATA_REPLICAS_REQ(sb) ||
315             BCH_SB_DATA_REPLICAS_REQ(sb) >= BCH_REPLICAS_MAX)
316                 return "Invalid number of data replicas";
317
318         if (BCH_SB_META_CSUM_TYPE(sb) >= BCH_CSUM_OPT_NR)
319                 return "Invalid metadata checksum type";
320
321         if (BCH_SB_DATA_CSUM_TYPE(sb) >= BCH_CSUM_OPT_NR)
322                 return "Invalid metadata checksum type";
323
324         if (BCH_SB_COMPRESSION_TYPE(sb) >= BCH_COMPRESSION_OPT_NR)
325                 return "Invalid compression type";
326
327         if (!BCH_SB_BTREE_NODE_SIZE(sb))
328                 return "Btree node size not set";
329
330         if (!is_power_of_2(BCH_SB_BTREE_NODE_SIZE(sb)))
331                 return "Btree node size not a power of two";
332
333         if (BCH_SB_GC_RESERVE(sb) < 5)
334                 return "gc reserve percentage too small";
335
336         if (!sb->time_precision ||
337             le32_to_cpu(sb->time_precision) > NSEC_PER_SEC)
338                 return "invalid time precision";
339
340         /* validate layout */
341         err = validate_sb_layout(&sb->layout);
342         if (err)
343                 return err;
344
345         vstruct_for_each(sb, f) {
346                 if (!f->u64s)
347                         return "Invalid superblock: invalid optional field";
348
349                 if (vstruct_next(f) > vstruct_last(sb))
350                         return "Invalid superblock: invalid optional field";
351         }
352
353         /* members must be validated first: */
354         mi = bch2_sb_get_members(sb);
355         if (!mi)
356                 return "Invalid superblock: member info area missing";
357
358         err = bch2_sb_field_validate(sb, &mi->field);
359         if (err)
360                 return err;
361
362         vstruct_for_each(sb, f) {
363                 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members)
364                         continue;
365
366                 err = bch2_sb_field_validate(sb, f);
367                 if (err)
368                         return err;
369         }
370
371         if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_NONCE_V1 &&
372             bch2_sb_get_crypt(sb) &&
373             BCH_SB_INITIALIZED(sb))
374                 return "Incompatible extent nonces";
375
376         sb->version = cpu_to_le64(BCH_SB_VERSION_MAX);
377
378         return NULL;
379 }
380
381 /* device open: */
382
383 static void bch2_sb_update(struct bch_fs *c)
384 {
385         struct bch_sb *src = c->disk_sb;
386         struct bch_sb_field_members *mi = bch2_sb_get_members(src);
387         struct bch_dev *ca;
388         unsigned i;
389
390         lockdep_assert_held(&c->sb_lock);
391
392         c->sb.uuid              = src->uuid;
393         c->sb.user_uuid         = src->user_uuid;
394         c->sb.nr_devices        = src->nr_devices;
395         c->sb.clean             = BCH_SB_CLEAN(src);
396         c->sb.encryption_type   = BCH_SB_ENCRYPTION_TYPE(src);
397         c->sb.encoded_extent_max= 1 << BCH_SB_ENCODED_EXTENT_MAX_BITS(src);
398         c->sb.time_base_lo      = le64_to_cpu(src->time_base_lo);
399         c->sb.time_base_hi      = le32_to_cpu(src->time_base_hi);
400         c->sb.time_precision    = le32_to_cpu(src->time_precision);
401
402         for_each_member_device(ca, c, i)
403                 ca->mi = bch2_mi_to_cpu(mi->members + i);
404 }
405
406 /* doesn't copy member info */
407 static void __copy_super(struct bch_sb *dst, struct bch_sb *src)
408 {
409         struct bch_sb_field *src_f, *dst_f;
410
411         dst->version            = src->version;
412         dst->seq                = src->seq;
413         dst->uuid               = src->uuid;
414         dst->user_uuid          = src->user_uuid;
415         memcpy(dst->label,      src->label, sizeof(dst->label));
416
417         dst->block_size         = src->block_size;
418         dst->nr_devices         = src->nr_devices;
419
420         dst->time_base_lo       = src->time_base_lo;
421         dst->time_base_hi       = src->time_base_hi;
422         dst->time_precision     = src->time_precision;
423
424         memcpy(dst->flags,      src->flags,     sizeof(dst->flags));
425         memcpy(dst->features,   src->features,  sizeof(dst->features));
426         memcpy(dst->compat,     src->compat,    sizeof(dst->compat));
427
428         vstruct_for_each(src, src_f) {
429                 if (src_f->type == BCH_SB_FIELD_journal)
430                         continue;
431
432                 dst_f = bch2_sb_field_get(dst, le32_to_cpu(src_f->type));
433                 dst_f = __bch2_sb_field_resize(dst, dst_f,
434                                 le32_to_cpu(src_f->u64s));
435
436                 memcpy(dst_f, src_f, vstruct_bytes(src_f));
437         }
438 }
439
440 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
441 {
442         struct bch_sb_field_journal *journal_buckets =
443                 bch2_sb_get_journal(src);
444         unsigned journal_u64s = journal_buckets
445                 ? le32_to_cpu(journal_buckets->field.u64s)
446                 : 0;
447         int ret;
448
449         lockdep_assert_held(&c->sb_lock);
450
451         ret = bch2_fs_sb_realloc(c, le32_to_cpu(src->u64s) - journal_u64s);
452         if (ret)
453                 return ret;
454
455         __copy_super(c->disk_sb, src);
456
457         ret = bch2_sb_replicas_to_cpu_replicas(c);
458         if (ret)
459                 return ret;
460
461         bch2_sb_update(c);
462         return 0;
463 }
464
465 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
466 {
467         struct bch_sb *src = c->disk_sb, *dst = ca->disk_sb.sb;
468         struct bch_sb_field_journal *journal_buckets =
469                 bch2_sb_get_journal(dst);
470         unsigned journal_u64s = journal_buckets
471                 ? le32_to_cpu(journal_buckets->field.u64s)
472                 : 0;
473         unsigned u64s = le32_to_cpu(src->u64s) + journal_u64s;
474         int ret;
475
476         ret = bch2_sb_realloc(&ca->disk_sb, u64s);
477         if (ret)
478                 return ret;
479
480         __copy_super(dst, src);
481         return 0;
482 }
483
484 /* read superblock: */
485
486 static const char *read_one_super(struct bch_sb_handle *sb, u64 offset)
487 {
488         struct bch_csum csum;
489         size_t bytes;
490         unsigned order;
491 reread:
492         bio_reset(sb->bio);
493         bio_set_dev(sb->bio, sb->bdev);
494         sb->bio->bi_iter.bi_sector = offset;
495         sb->bio->bi_iter.bi_size = PAGE_SIZE << sb->page_order;
496         bio_set_op_attrs(sb->bio, REQ_OP_READ, REQ_SYNC|REQ_META);
497         bch2_bio_map(sb->bio, sb->sb);
498
499         if (submit_bio_wait(sb->bio))
500                 return "IO error";
501
502         if (uuid_le_cmp(sb->sb->magic, BCACHE_MAGIC))
503                 return "Not a bcachefs superblock";
504
505         if (le64_to_cpu(sb->sb->version) < BCH_SB_VERSION_MIN ||
506             le64_to_cpu(sb->sb->version) > BCH_SB_VERSION_MAX)
507                 return"Unsupported superblock version";
508
509         bytes = vstruct_bytes(sb->sb);
510
511         if (bytes > 512 << sb->sb->layout.sb_max_size_bits)
512                 return "Bad superblock: too big";
513
514         order = get_order(bytes);
515         if (order > sb->page_order) {
516                 if (__bch2_super_realloc(sb, order))
517                         return "cannot allocate memory";
518                 goto reread;
519         }
520
521         if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR)
522                 return "unknown csum type";
523
524         /* XXX: verify MACs */
525         csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
526                             null_nonce(), sb->sb);
527
528         if (bch2_crc_cmp(csum, sb->sb->csum))
529                 return "bad checksum reading superblock";
530
531         return NULL;
532 }
533
534 int bch2_read_super(const char *path, struct bch_opts *opts,
535                     struct bch_sb_handle *sb)
536 {
537         u64 offset = opt_get(*opts, sb);
538         struct bch_sb_layout layout;
539         const char *err;
540         __le64 *i;
541         int ret;
542
543         memset(sb, 0, sizeof(*sb));
544         sb->mode = FMODE_READ;
545
546         if (!opt_get(*opts, noexcl))
547                 sb->mode |= FMODE_EXCL;
548
549         if (!opt_get(*opts, nochanges))
550                 sb->mode |= FMODE_WRITE;
551
552         sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
553         if (IS_ERR(sb->bdev) &&
554             PTR_ERR(sb->bdev) == -EACCES &&
555             opt_get(*opts, read_only)) {
556                 sb->mode &= ~FMODE_WRITE;
557
558                 sb->bdev = blkdev_get_by_path(path, sb->mode, sb);
559                 if (!IS_ERR(sb->bdev))
560                         opt_set(*opts, nochanges, true);
561         }
562
563         if (IS_ERR(sb->bdev))
564                 return PTR_ERR(sb->bdev);
565
566         err = "cannot allocate memory";
567         ret = __bch2_super_realloc(sb, 0);
568         if (ret)
569                 goto err;
570
571         ret = -EFAULT;
572         err = "dynamic fault";
573         if (bch2_fs_init_fault("read_super"))
574                 goto err;
575
576         ret = -EINVAL;
577         err = read_one_super(sb, offset);
578         if (!err)
579                 goto got_super;
580
581         if (opt_defined(*opts, sb))
582                 goto err;
583
584         pr_err("error reading default superblock: %s", err);
585
586         /*
587          * Error reading primary superblock - read location of backup
588          * superblocks:
589          */
590         bio_reset(sb->bio);
591         bio_set_dev(sb->bio, sb->bdev);
592         sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
593         sb->bio->bi_iter.bi_size = sizeof(struct bch_sb_layout);
594         bio_set_op_attrs(sb->bio, REQ_OP_READ, REQ_SYNC|REQ_META);
595         /*
596          * use sb buffer to read layout, since sb buffer is page aligned but
597          * layout won't be:
598          */
599         bch2_bio_map(sb->bio, sb->sb);
600
601         err = "IO error";
602         if (submit_bio_wait(sb->bio))
603                 goto err;
604
605         memcpy(&layout, sb->sb, sizeof(layout));
606         err = validate_sb_layout(&layout);
607         if (err)
608                 goto err;
609
610         for (i = layout.sb_offset;
611              i < layout.sb_offset + layout.nr_superblocks; i++) {
612                 offset = le64_to_cpu(*i);
613
614                 if (offset == opt_get(*opts, sb))
615                         continue;
616
617                 err = read_one_super(sb, offset);
618                 if (!err)
619                         goto got_super;
620         }
621
622         ret = -EINVAL;
623         goto err;
624
625 got_super:
626         err = "Superblock block size smaller than device block size";
627         ret = -EINVAL;
628         if (le16_to_cpu(sb->sb->block_size) << 9 <
629             bdev_logical_block_size(sb->bdev))
630                 goto err;
631
632         if (sb->mode & FMODE_WRITE)
633                 bdev_get_queue(sb->bdev)->backing_dev_info->capabilities
634                         |= BDI_CAP_STABLE_WRITES;
635
636         return 0;
637 err:
638         bch2_free_super(sb);
639         pr_err("error reading superblock: %s", err);
640         return ret;
641 }
642
643 /* write superblock: */
644
645 static void write_super_endio(struct bio *bio)
646 {
647         struct bch_dev *ca = bio->bi_private;
648
649         /* XXX: return errors directly */
650
651         if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write"))
652                 ca->sb_write_error = 1;
653
654         closure_put(&ca->fs->sb_write);
655         percpu_ref_put(&ca->io_ref);
656 }
657
658 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
659 {
660         struct bch_sb *sb = ca->disk_sb.sb;
661         struct bio *bio = ca->disk_sb.bio;
662
663         sb->offset = sb->layout.sb_offset[idx];
664
665         SET_BCH_SB_CSUM_TYPE(sb, c->opts.metadata_checksum);
666         sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
667                                 null_nonce(), sb);
668
669         bio_reset(bio);
670         bio_set_dev(bio, ca->disk_sb.bdev);
671         bio->bi_iter.bi_sector  = le64_to_cpu(sb->offset);
672         bio->bi_iter.bi_size    =
673                 roundup(vstruct_bytes(sb),
674                         bdev_logical_block_size(ca->disk_sb.bdev));
675         bio->bi_end_io          = write_super_endio;
676         bio->bi_private         = ca;
677         bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC|REQ_META);
678         bch2_bio_map(bio, sb);
679
680         this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_SB],
681                      bio_sectors(bio));
682
683         percpu_ref_get(&ca->io_ref);
684         closure_bio_submit(bio, &c->sb_write);
685 }
686
687 void bch2_write_super(struct bch_fs *c)
688 {
689         struct closure *cl = &c->sb_write;
690         struct bch_dev *ca;
691         unsigned i, sb = 0, nr_wrote;
692         const char *err;
693         struct bch_devs_mask sb_written;
694         bool wrote, can_mount_without_written, can_mount_with_written;
695
696         lockdep_assert_held(&c->sb_lock);
697
698         closure_init_stack(cl);
699         memset(&sb_written, 0, sizeof(sb_written));
700
701         le64_add_cpu(&c->disk_sb->seq, 1);
702
703         for_each_online_member(ca, c, i)
704                 bch2_sb_from_fs(c, ca);
705
706         for_each_online_member(ca, c, i) {
707                 err = bch2_sb_validate(&ca->disk_sb);
708                 if (err) {
709                         bch2_fs_inconsistent(c, "sb invalid before write: %s", err);
710                         goto out;
711                 }
712         }
713
714         if (c->opts.nochanges ||
715             test_bit(BCH_FS_ERROR, &c->flags))
716                 goto out;
717
718         for_each_online_member(ca, c, i) {
719                 __set_bit(ca->dev_idx, sb_written.d);
720                 ca->sb_write_error = 0;
721         }
722
723         do {
724                 wrote = false;
725                 for_each_online_member(ca, c, i)
726                         if (sb < ca->disk_sb.sb->layout.nr_superblocks) {
727                                 write_one_super(c, ca, sb);
728                                 wrote = true;
729                         }
730                 closure_sync(cl);
731                 sb++;
732         } while (wrote);
733
734         for_each_online_member(ca, c, i)
735                 if (ca->sb_write_error)
736                         __clear_bit(ca->dev_idx, sb_written.d);
737
738         nr_wrote = dev_mask_nr(&sb_written);
739
740         can_mount_with_written =
741                 bch2_have_enough_devs(c,
742                         __bch2_replicas_status(c, sb_written),
743                         BCH_FORCE_IF_DEGRADED);
744
745         for (i = 0; i < ARRAY_SIZE(sb_written.d); i++)
746                 sb_written.d[i] = ~sb_written.d[i];
747
748         can_mount_without_written =
749                 bch2_have_enough_devs(c,
750                         __bch2_replicas_status(c, sb_written),
751                         BCH_FORCE_IF_DEGRADED);
752
753         /*
754          * If we would be able to mount _without_ the devices we successfully
755          * wrote superblocks to, we weren't able to write to enough devices:
756          *
757          * Exception: if we can mount without the successes because we haven't
758          * written anything (new filesystem), we continue if we'd be able to
759          * mount with the devices we did successfully write to:
760          */
761         bch2_fs_fatal_err_on(!nr_wrote ||
762                              (can_mount_without_written &&
763                               !can_mount_with_written), c,
764                 "Unable to write superblock to sufficient devices");
765 out:
766         /* Make new options visible after they're persistent: */
767         bch2_sb_update(c);
768 }
769
770 /* BCH_SB_FIELD_journal: */
771
772 static int u64_cmp(const void *_l, const void *_r)
773 {
774         u64 l = *((const u64 *) _l), r = *((const u64 *) _r);
775
776         return l < r ? -1 : l > r ? 1 : 0;
777 }
778
779 static const char *bch2_sb_validate_journal(struct bch_sb *sb,
780                                             struct bch_sb_field *f)
781 {
782         struct bch_sb_field_journal *journal = field_to_type(f, journal);
783         struct bch_member *m = bch2_sb_get_members(sb)->members + sb->dev_idx;
784         const char *err;
785         unsigned nr;
786         unsigned i;
787         u64 *b;
788
789         journal = bch2_sb_get_journal(sb);
790         if (!journal)
791                 return NULL;
792
793         nr = bch2_nr_journal_buckets(journal);
794         if (!nr)
795                 return NULL;
796
797         b = kmalloc_array(sizeof(u64), nr, GFP_KERNEL);
798         if (!b)
799                 return "cannot allocate memory";
800
801         for (i = 0; i < nr; i++)
802                 b[i] = le64_to_cpu(journal->buckets[i]);
803
804         sort(b, nr, sizeof(u64), u64_cmp, NULL);
805
806         err = "journal bucket at sector 0";
807         if (!b[0])
808                 goto err;
809
810         err = "journal bucket before first bucket";
811         if (m && b[0] < le16_to_cpu(m->first_bucket))
812                 goto err;
813
814         err = "journal bucket past end of device";
815         if (m && b[nr - 1] >= le64_to_cpu(m->nbuckets))
816                 goto err;
817
818         err = "duplicate journal buckets";
819         for (i = 0; i + 1 < nr; i++)
820                 if (b[i] == b[i + 1])
821                         goto err;
822
823         err = NULL;
824 err:
825         kfree(b);
826         return err;
827 }
828
829 /* BCH_SB_FIELD_members: */
830
831 static const char *bch2_sb_validate_members(struct bch_sb *sb,
832                                             struct bch_sb_field *f)
833 {
834         struct bch_sb_field_members *mi = field_to_type(f, members);
835         struct bch_member *m;
836
837         if ((void *) (mi->members + sb->nr_devices) >
838             vstruct_end(&mi->field))
839                 return "Invalid superblock: bad member info";
840
841         for (m = mi->members;
842              m < mi->members + sb->nr_devices;
843              m++) {
844                 if (!bch2_member_exists(m))
845                         continue;
846
847                 if (le64_to_cpu(m->nbuckets) > LONG_MAX)
848                         return "Too many buckets";
849
850                 if (le64_to_cpu(m->nbuckets) -
851                     le16_to_cpu(m->first_bucket) < 1 << 10)
852                         return "Not enough buckets";
853
854                 if (le16_to_cpu(m->bucket_size) <
855                     le16_to_cpu(sb->block_size))
856                         return "bucket size smaller than block size";
857
858                 if (le16_to_cpu(m->bucket_size) <
859                     BCH_SB_BTREE_NODE_SIZE(sb))
860                         return "bucket size smaller than btree node size";
861         }
862
863         if (le64_to_cpu(sb->version) < BCH_SB_VERSION_EXTENT_MAX)
864                 for (m = mi->members;
865                      m < mi->members + sb->nr_devices;
866                      m++)
867                         SET_BCH_MEMBER_DATA_ALLOWED(m, ~0);
868
869         return NULL;
870 }
871
872 /* BCH_SB_FIELD_crypt: */
873
874 static const char *bch2_sb_validate_crypt(struct bch_sb *sb,
875                                           struct bch_sb_field *f)
876 {
877         struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
878
879         if (vstruct_bytes(&crypt->field) != sizeof(*crypt))
880                 return "invalid field crypt: wrong size";
881
882         if (BCH_CRYPT_KDF_TYPE(crypt))
883                 return "invalid field crypt: bad kdf type";
884
885         return NULL;
886 }
887
888 /* BCH_SB_FIELD_replicas: */
889
890 /* Replicas tracking - in memory: */
891
892 #define for_each_cpu_replicas_entry(_r, _i)                             \
893         for (_i = (_r)->entries;                                        \
894              (void *) (_i) < (void *) (_r)->entries + (_r)->nr * (_r)->entry_size;\
895              _i = (void *) (_i) + (_r)->entry_size)
896
897 static inline struct bch_replicas_cpu_entry *
898 cpu_replicas_entry(struct bch_replicas_cpu *r, unsigned i)
899 {
900         return (void *) r->entries + r->entry_size * i;
901 }
902
903 static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
904 {
905         eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL);
906 }
907
908 static inline bool replicas_test_dev(struct bch_replicas_cpu_entry *e,
909                                      unsigned dev)
910 {
911         return (e->devs[dev >> 3] & (1 << (dev & 7))) != 0;
912 }
913
914 static inline void replicas_set_dev(struct bch_replicas_cpu_entry *e,
915                                     unsigned dev)
916 {
917         e->devs[dev >> 3] |= 1 << (dev & 7);
918 }
919
920 static inline unsigned replicas_dev_slots(struct bch_replicas_cpu *r)
921 {
922         return (r->entry_size -
923                 offsetof(struct bch_replicas_cpu_entry, devs)) * 8;
924 }
925
926 int bch2_cpu_replicas_to_text(struct bch_replicas_cpu *r,
927                               char *buf, size_t size)
928 {
929         char *out = buf, *end = out + size;
930         struct bch_replicas_cpu_entry *e;
931         bool first = true;
932         unsigned i;
933
934         for_each_cpu_replicas_entry(r, e) {
935                 bool first_e = true;
936
937                 if (!first)
938                         out += scnprintf(out, end - out, " ");
939                 first = false;
940
941                 out += scnprintf(out, end - out, "%u: [", e->data_type);
942
943                 for (i = 0; i < replicas_dev_slots(r); i++)
944                         if (replicas_test_dev(e, i)) {
945                                 if (!first_e)
946                                         out += scnprintf(out, end - out, " ");
947                                 first_e = false;
948                                 out += scnprintf(out, end - out, "%u", i);
949                         }
950                 out += scnprintf(out, end - out, "]");
951         }
952
953         return out - buf;
954 }
955
956 static inline unsigned bkey_to_replicas(struct bkey_s_c_extent e,
957                                         enum bch_data_type data_type,
958                                         struct bch_replicas_cpu_entry *r,
959                                         unsigned *max_dev)
960 {
961         const struct bch_extent_ptr *ptr;
962         unsigned nr = 0;
963
964         BUG_ON(!data_type ||
965                data_type == BCH_DATA_SB ||
966                data_type >= BCH_DATA_NR);
967
968         memset(r, 0, sizeof(*r));
969         r->data_type = data_type;
970
971         *max_dev = 0;
972
973         extent_for_each_ptr(e, ptr)
974                 if (!ptr->cached) {
975                         *max_dev = max_t(unsigned, *max_dev, ptr->dev);
976                         replicas_set_dev(r, ptr->dev);
977                         nr++;
978                 }
979         return nr;
980 }
981
982 static inline void devlist_to_replicas(struct bch_devs_list devs,
983                                        enum bch_data_type data_type,
984                                        struct bch_replicas_cpu_entry *r,
985                                        unsigned *max_dev)
986 {
987         unsigned i;
988
989         BUG_ON(!data_type ||
990                data_type == BCH_DATA_SB ||
991                data_type >= BCH_DATA_NR);
992
993         memset(r, 0, sizeof(*r));
994         r->data_type = data_type;
995
996         *max_dev = 0;
997
998         for (i = 0; i < devs.nr; i++) {
999                 *max_dev = max_t(unsigned, *max_dev, devs.devs[i]);
1000                 replicas_set_dev(r, devs.devs[i]);
1001         }
1002 }
1003
1004 static struct bch_replicas_cpu *
1005 cpu_replicas_add_entry(struct bch_replicas_cpu *old,
1006                        struct bch_replicas_cpu_entry new_entry,
1007                        unsigned max_dev)
1008 {
1009         struct bch_replicas_cpu *new;
1010         unsigned i, nr, entry_size;
1011
1012         entry_size = offsetof(struct bch_replicas_cpu_entry, devs) +
1013                 DIV_ROUND_UP(max_dev + 1, 8);
1014         entry_size = max(entry_size, old->entry_size);
1015         nr = old->nr + 1;
1016
1017         new = kzalloc(sizeof(struct bch_replicas_cpu) +
1018                       nr * entry_size, GFP_NOIO);
1019         if (!new)
1020                 return NULL;
1021
1022         new->nr         = nr;
1023         new->entry_size = entry_size;
1024
1025         for (i = 0; i < old->nr; i++)
1026                 memcpy(cpu_replicas_entry(new, i),
1027                        cpu_replicas_entry(old, i),
1028                        min(new->entry_size, old->entry_size));
1029
1030         memcpy(cpu_replicas_entry(new, old->nr),
1031                &new_entry,
1032                new->entry_size);
1033
1034         bch2_cpu_replicas_sort(new);
1035         return new;
1036 }
1037
1038 static bool replicas_has_entry(struct bch_replicas_cpu *r,
1039                                 struct bch_replicas_cpu_entry search,
1040                                 unsigned max_dev)
1041 {
1042         return max_dev < replicas_dev_slots(r) &&
1043                 eytzinger0_find(r->entries, r->nr,
1044                                 r->entry_size,
1045                                 memcmp, &search) < r->nr;
1046 }
1047
1048 noinline
1049 static int bch2_check_mark_super_slowpath(struct bch_fs *c,
1050                                 struct bch_replicas_cpu_entry new_entry,
1051                                 unsigned max_dev)
1052 {
1053         struct bch_replicas_cpu *old_gc, *new_gc = NULL, *old_r, *new_r = NULL;
1054         int ret = -ENOMEM;
1055
1056         mutex_lock(&c->sb_lock);
1057
1058         old_gc = rcu_dereference_protected(c->replicas_gc,
1059                                            lockdep_is_held(&c->sb_lock));
1060         if (old_gc && !replicas_has_entry(old_gc, new_entry, max_dev)) {
1061                 new_gc = cpu_replicas_add_entry(old_gc, new_entry, max_dev);
1062                 if (!new_gc)
1063                         goto err;
1064         }
1065
1066         old_r = rcu_dereference_protected(c->replicas,
1067                                           lockdep_is_held(&c->sb_lock));
1068         if (!replicas_has_entry(old_r, new_entry, max_dev)) {
1069                 new_r = cpu_replicas_add_entry(old_r, new_entry, max_dev);
1070                 if (!new_r)
1071                         goto err;
1072
1073                 ret = bch2_cpu_replicas_to_sb_replicas(c, new_r);
1074                 if (ret)
1075                         goto err;
1076         }
1077
1078         /* allocations done, now commit: */
1079
1080         if (new_r)
1081                 bch2_write_super(c);
1082
1083         /* don't update in memory replicas until changes are persistent */
1084
1085         if (new_gc) {
1086                 rcu_assign_pointer(c->replicas_gc, new_gc);
1087                 kfree_rcu(old_gc, rcu);
1088         }
1089
1090         if (new_r) {
1091                 rcu_assign_pointer(c->replicas, new_r);
1092                 kfree_rcu(old_r, rcu);
1093         }
1094
1095         mutex_unlock(&c->sb_lock);
1096         return 0;
1097 err:
1098         mutex_unlock(&c->sb_lock);
1099         if (new_gc)
1100                 kfree(new_gc);
1101         if (new_r)
1102                 kfree(new_r);
1103         return ret;
1104 }
1105
1106 int bch2_check_mark_super(struct bch_fs *c,
1107                           enum bch_data_type data_type,
1108                           struct bch_devs_list devs)
1109 {
1110         struct bch_replicas_cpu_entry search;
1111         struct bch_replicas_cpu *r, *gc_r;
1112         unsigned max_dev;
1113         bool marked;
1114
1115         if (!devs.nr)
1116                 return 0;
1117
1118         devlist_to_replicas(devs, data_type, &search, &max_dev);
1119
1120         rcu_read_lock();
1121         r = rcu_dereference(c->replicas);
1122         gc_r = rcu_dereference(c->replicas_gc);
1123         marked = replicas_has_entry(r, search, max_dev) &&
1124                 (!likely(gc_r) || replicas_has_entry(gc_r, search, max_dev));
1125         rcu_read_unlock();
1126
1127         return likely(marked) ? 0
1128                 : bch2_check_mark_super_slowpath(c, search, max_dev);
1129 }
1130
1131 int bch2_replicas_gc_end(struct bch_fs *c, int err)
1132 {
1133         struct bch_replicas_cpu *new_r, *old_r;
1134         int ret = 0;
1135
1136         lockdep_assert_held(&c->replicas_gc_lock);
1137
1138         mutex_lock(&c->sb_lock);
1139
1140         new_r = rcu_dereference_protected(c->replicas_gc,
1141                                           lockdep_is_held(&c->sb_lock));
1142
1143         if (err) {
1144                 rcu_assign_pointer(c->replicas_gc, NULL);
1145                 kfree_rcu(new_r, rcu);
1146                 goto err;
1147         }
1148
1149         if (bch2_cpu_replicas_to_sb_replicas(c, new_r)) {
1150                 ret = -ENOSPC;
1151                 goto err;
1152         }
1153
1154         old_r = rcu_dereference_protected(c->replicas,
1155                                           lockdep_is_held(&c->sb_lock));
1156
1157         rcu_assign_pointer(c->replicas, new_r);
1158         rcu_assign_pointer(c->replicas_gc, NULL);
1159         kfree_rcu(old_r, rcu);
1160
1161         bch2_write_super(c);
1162 err:
1163         mutex_unlock(&c->sb_lock);
1164         return ret;
1165 }
1166
1167 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
1168 {
1169         struct bch_replicas_cpu *dst, *src;
1170         struct bch_replicas_cpu_entry *e;
1171
1172         lockdep_assert_held(&c->replicas_gc_lock);
1173
1174         mutex_lock(&c->sb_lock);
1175         BUG_ON(c->replicas_gc);
1176
1177         src = rcu_dereference_protected(c->replicas,
1178                                         lockdep_is_held(&c->sb_lock));
1179
1180         dst = kzalloc(sizeof(struct bch_replicas_cpu) +
1181                       src->nr * src->entry_size, GFP_NOIO);
1182         if (!dst) {
1183                 mutex_unlock(&c->sb_lock);
1184                 return -ENOMEM;
1185         }
1186
1187         dst->nr         = 0;
1188         dst->entry_size = src->entry_size;
1189
1190         for_each_cpu_replicas_entry(src, e)
1191                 if (!((1 << e->data_type) & typemask))
1192                         memcpy(cpu_replicas_entry(dst, dst->nr++),
1193                                e, dst->entry_size);
1194
1195         bch2_cpu_replicas_sort(dst);
1196
1197         rcu_assign_pointer(c->replicas_gc, dst);
1198         mutex_unlock(&c->sb_lock);
1199
1200         return 0;
1201 }
1202
1203 /* Replicas tracking - superblock: */
1204
1205 static void bch2_sb_replicas_nr_entries(struct bch_sb_field_replicas *r,
1206                                         unsigned *nr,
1207                                         unsigned *bytes,
1208                                         unsigned *max_dev)
1209 {
1210         struct bch_replicas_entry *i;
1211         unsigned j;
1212
1213         *nr     = 0;
1214         *bytes  = sizeof(*r);
1215         *max_dev = 0;
1216
1217         if (!r)
1218                 return;
1219
1220         for_each_replicas_entry(r, i) {
1221                 for (j = 0; j < i->nr; j++)
1222                         *max_dev = max_t(unsigned, *max_dev, i->devs[j]);
1223                 (*nr)++;
1224         }
1225
1226         *bytes = (void *) i - (void *) r;
1227 }
1228
1229 static struct bch_replicas_cpu *
1230 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r)
1231 {
1232         struct bch_replicas_cpu *cpu_r;
1233         unsigned i, nr, bytes, max_dev, entry_size;
1234
1235         bch2_sb_replicas_nr_entries(sb_r, &nr, &bytes, &max_dev);
1236
1237         entry_size = offsetof(struct bch_replicas_cpu_entry, devs) +
1238                 DIV_ROUND_UP(max_dev + 1, 8);
1239
1240         cpu_r = kzalloc(sizeof(struct bch_replicas_cpu) +
1241                         nr * entry_size, GFP_NOIO);
1242         if (!cpu_r)
1243                 return NULL;
1244
1245         cpu_r->nr               = nr;
1246         cpu_r->entry_size       = entry_size;
1247
1248         if (nr) {
1249                 struct bch_replicas_cpu_entry *dst =
1250                         cpu_replicas_entry(cpu_r, 0);
1251                 struct bch_replicas_entry *src = sb_r->entries;
1252
1253                 while (dst < cpu_replicas_entry(cpu_r, nr)) {
1254                         dst->data_type = src->data_type;
1255                         for (i = 0; i < src->nr; i++)
1256                                 replicas_set_dev(dst, src->devs[i]);
1257
1258                         src     = replicas_entry_next(src);
1259                         dst     = (void *) dst + entry_size;
1260                 }
1261         }
1262
1263         bch2_cpu_replicas_sort(cpu_r);
1264         return cpu_r;
1265 }
1266
1267 static int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
1268 {
1269         struct bch_sb_field_replicas *sb_r;
1270         struct bch_replicas_cpu *cpu_r, *old_r;
1271
1272         sb_r    = bch2_sb_get_replicas(c->disk_sb);
1273         cpu_r   = __bch2_sb_replicas_to_cpu_replicas(sb_r);
1274         if (!cpu_r)
1275                 return -ENOMEM;
1276
1277         old_r = rcu_dereference_check(c->replicas, lockdep_is_held(&c->sb_lock));
1278         rcu_assign_pointer(c->replicas, cpu_r);
1279         if (old_r)
1280                 kfree_rcu(old_r, rcu);
1281
1282         return 0;
1283 }
1284
1285 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
1286                                             struct bch_replicas_cpu *r)
1287 {
1288         struct bch_sb_field_replicas *sb_r;
1289         struct bch_replicas_entry *sb_e;
1290         struct bch_replicas_cpu_entry *e;
1291         size_t i, bytes;
1292
1293         bytes = sizeof(struct bch_sb_field_replicas);
1294
1295         for_each_cpu_replicas_entry(r, e) {
1296                 bytes += sizeof(struct bch_replicas_entry);
1297                 for (i = 0; i < r->entry_size - 1; i++)
1298                         bytes += hweight8(e->devs[i]);
1299         }
1300
1301         sb_r = bch2_fs_sb_resize_replicas(c,
1302                         DIV_ROUND_UP(sizeof(*sb_r) + bytes, sizeof(u64)));
1303         if (!sb_r)
1304                 return -ENOSPC;
1305
1306         memset(&sb_r->entries, 0,
1307                vstruct_end(&sb_r->field) -
1308                (void *) &sb_r->entries);
1309
1310         sb_e = sb_r->entries;
1311         for_each_cpu_replicas_entry(r, e) {
1312                 sb_e->data_type = e->data_type;
1313
1314                 for (i = 0; i < replicas_dev_slots(r); i++)
1315                         if (replicas_test_dev(e, i))
1316                                 sb_e->devs[sb_e->nr++] = i;
1317
1318                 sb_e = replicas_entry_next(sb_e);
1319
1320                 BUG_ON((void *) sb_e > vstruct_end(&sb_r->field));
1321         }
1322
1323         return 0;
1324 }
1325
1326 static const char *bch2_sb_validate_replicas(struct bch_sb *sb,
1327                                              struct bch_sb_field *f)
1328 {
1329         struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
1330         struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
1331         struct bch_replicas_cpu *cpu_r = NULL;
1332         struct bch_replicas_entry *e;
1333         const char *err;
1334         unsigned i;
1335
1336         for_each_replicas_entry(sb_r, e) {
1337                 err = "invalid replicas entry: invalid data type";
1338                 if (e->data_type >= BCH_DATA_NR)
1339                         goto err;
1340
1341                 err = "invalid replicas entry: no devices";
1342                 if (!e->nr)
1343                         goto err;
1344
1345                 err = "invalid replicas entry: too many devices";
1346                 if (e->nr >= BCH_REPLICAS_MAX)
1347                         goto err;
1348
1349                 err = "invalid replicas entry: invalid device";
1350                 for (i = 0; i < e->nr; i++)
1351                         if (!bch2_dev_exists(sb, mi, e->devs[i]))
1352                                 goto err;
1353         }
1354
1355         err = "cannot allocate memory";
1356         cpu_r = __bch2_sb_replicas_to_cpu_replicas(sb_r);
1357         if (!cpu_r)
1358                 goto err;
1359
1360         sort_cmp_size(cpu_r->entries,
1361                       cpu_r->nr,
1362                       cpu_r->entry_size,
1363                       memcmp, NULL);
1364
1365         for (i = 0; i + 1 < cpu_r->nr; i++) {
1366                 struct bch_replicas_cpu_entry *l =
1367                         cpu_replicas_entry(cpu_r, i);
1368                 struct bch_replicas_cpu_entry *r =
1369                         cpu_replicas_entry(cpu_r, i + 1);
1370
1371                 BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0);
1372
1373                 err = "duplicate replicas entry";
1374                 if (!memcmp(l, r, cpu_r->entry_size))
1375                         goto err;
1376         }
1377
1378         err = NULL;
1379 err:
1380         kfree(cpu_r);
1381         return err;
1382 }
1383
1384 int bch2_sb_replicas_to_text(struct bch_sb_field_replicas *r, char *buf, size_t size)
1385 {
1386         char *out = buf, *end = out + size;
1387         struct bch_replicas_entry *e;
1388         bool first = true;
1389         unsigned i;
1390
1391         if (!r) {
1392                 out += scnprintf(out, end - out, "(no replicas section found)");
1393                 return out - buf;
1394         }
1395
1396         for_each_replicas_entry(r, e) {
1397                 if (!first)
1398                         out += scnprintf(out, end - out, " ");
1399                 first = false;
1400
1401                 out += scnprintf(out, end - out, "%u: [", e->data_type);
1402
1403                 for (i = 0; i < e->nr; i++)
1404                         out += scnprintf(out, end - out,
1405                                          i ? " %u" : "%u", e->devs[i]);
1406                 out += scnprintf(out, end - out, "]");
1407         }
1408
1409         return out - buf;
1410 }
1411
1412 /* Query replicas: */
1413
1414 bool bch2_sb_has_replicas(struct bch_fs *c,
1415                           enum bch_data_type data_type,
1416                           struct bch_devs_list devs)
1417 {
1418         struct bch_replicas_cpu_entry search;
1419         unsigned max_dev;
1420         bool ret;
1421
1422         if (!devs.nr)
1423                 return true;
1424
1425         devlist_to_replicas(devs, data_type, &search, &max_dev);
1426
1427         rcu_read_lock();
1428         ret = replicas_has_entry(rcu_dereference(c->replicas),
1429                                  search, max_dev);
1430         rcu_read_unlock();
1431
1432         return ret;
1433 }
1434
1435 struct replicas_status __bch2_replicas_status(struct bch_fs *c,
1436                                               struct bch_devs_mask online_devs)
1437 {
1438         struct bch_sb_field_members *mi;
1439         struct bch_replicas_cpu_entry *e;
1440         struct bch_replicas_cpu *r;
1441         unsigned i, dev, dev_slots, nr_online, nr_offline;
1442         struct replicas_status ret;
1443
1444         memset(&ret, 0, sizeof(ret));
1445
1446         for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
1447                 ret.replicas[i].nr_online = UINT_MAX;
1448
1449         mi = bch2_sb_get_members(c->disk_sb);
1450         rcu_read_lock();
1451
1452         r = rcu_dereference(c->replicas);
1453         dev_slots = replicas_dev_slots(r);
1454
1455         for_each_cpu_replicas_entry(r, e) {
1456                 if (e->data_type >= ARRAY_SIZE(ret.replicas))
1457                         panic("e %p data_type %u\n", e, e->data_type);
1458
1459                 nr_online = nr_offline = 0;
1460
1461                 for (dev = 0; dev < dev_slots; dev++) {
1462                         if (!replicas_test_dev(e, dev))
1463                                 continue;
1464
1465                         BUG_ON(!bch2_dev_exists(c->disk_sb, mi, dev));
1466
1467                         if (test_bit(dev, online_devs.d))
1468                                 nr_online++;
1469                         else
1470                                 nr_offline++;
1471                 }
1472
1473                 ret.replicas[e->data_type].nr_online =
1474                         min(ret.replicas[e->data_type].nr_online,
1475                             nr_online);
1476
1477                 ret.replicas[e->data_type].nr_offline =
1478                         max(ret.replicas[e->data_type].nr_offline,
1479                             nr_offline);
1480         }
1481
1482         rcu_read_unlock();
1483
1484         return ret;
1485 }
1486
1487 struct replicas_status bch2_replicas_status(struct bch_fs *c)
1488 {
1489         return __bch2_replicas_status(c, bch2_online_devs(c));
1490 }
1491
1492 bool bch2_have_enough_devs(struct bch_fs *c,
1493                            struct replicas_status s,
1494                            unsigned flags)
1495 {
1496         if ((s.replicas[BCH_DATA_JOURNAL].nr_offline ||
1497              s.replicas[BCH_DATA_BTREE].nr_offline) &&
1498             !(flags & BCH_FORCE_IF_METADATA_DEGRADED))
1499                 return false;
1500
1501         if ((!s.replicas[BCH_DATA_JOURNAL].nr_online ||
1502              !s.replicas[BCH_DATA_BTREE].nr_online) &&
1503             !(flags & BCH_FORCE_IF_METADATA_LOST))
1504                 return false;
1505
1506         if (s.replicas[BCH_DATA_USER].nr_offline &&
1507             !(flags & BCH_FORCE_IF_DATA_DEGRADED))
1508                 return false;
1509
1510         if (!s.replicas[BCH_DATA_USER].nr_online &&
1511             !(flags & BCH_FORCE_IF_DATA_LOST))
1512                 return false;
1513
1514         return true;
1515 }
1516
1517 unsigned bch2_replicas_online(struct bch_fs *c, bool meta)
1518 {
1519         struct replicas_status s = bch2_replicas_status(c);
1520
1521         return meta
1522                 ? min(s.replicas[BCH_DATA_JOURNAL].nr_online,
1523                       s.replicas[BCH_DATA_BTREE].nr_online)
1524                 : s.replicas[BCH_DATA_USER].nr_online;
1525 }
1526
1527 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
1528 {
1529         struct bch_replicas_cpu_entry *e;
1530         struct bch_replicas_cpu *r;
1531         unsigned ret = 0;
1532
1533         rcu_read_lock();
1534         r = rcu_dereference(c->replicas);
1535
1536         if (ca->dev_idx >= replicas_dev_slots(r))
1537                 goto out;
1538
1539         for_each_cpu_replicas_entry(r, e)
1540                 if (replicas_test_dev(e, ca->dev_idx))
1541                         ret |= 1 << e->data_type;
1542 out:
1543         rcu_read_unlock();
1544
1545         return ret;
1546 }
1547
1548 /* Quotas: */
1549
1550 static const char *bch2_sb_validate_quota(struct bch_sb *sb,
1551                                           struct bch_sb_field *f)
1552 {
1553         struct bch_sb_field_quota *q = field_to_type(f, quota);
1554
1555         if (vstruct_bytes(&q->field) != sizeof(*q))
1556                 return "invalid field quota: wrong size";
1557
1558         return NULL;
1559 }