]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/replicas.c
Update bcachefs sources to dab31ca168 bcachefs: Add some logging for btree node rewri...
[bcachefs-tools-debian] / libbcachefs / replicas.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "buckets.h"
5 #include "journal.h"
6 #include "replicas.h"
7 #include "super-io.h"
8
9 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *,
10                                             struct bch_replicas_cpu *);
11
12 /* Replicas tracking - in memory: */
13
14 static void verify_replicas_entry(struct bch_replicas_entry *e)
15 {
16 #ifdef CONFIG_BCACHEFS_DEBUG
17         unsigned i;
18
19         BUG_ON(e->data_type >= BCH_DATA_NR);
20         BUG_ON(!e->nr_devs);
21         BUG_ON(e->nr_required > 1 &&
22                e->nr_required >= e->nr_devs);
23
24         for (i = 0; i + 1 < e->nr_devs; i++)
25                 BUG_ON(e->devs[i] >= e->devs[i + 1]);
26 #endif
27 }
28
29 void bch2_replicas_entry_sort(struct bch_replicas_entry *e)
30 {
31         bubble_sort(e->devs, e->nr_devs, u8_cmp);
32 }
33
34 static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
35 {
36         eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL);
37 }
38
39 void bch2_replicas_entry_v0_to_text(struct printbuf *out,
40                                     struct bch_replicas_entry_v0 *e)
41 {
42         unsigned i;
43
44         if (e->data_type < BCH_DATA_NR)
45                 prt_printf(out, "%s", bch2_data_types[e->data_type]);
46         else
47                 prt_printf(out, "(invalid data type %u)", e->data_type);
48
49         prt_printf(out, ": %u [", e->nr_devs);
50         for (i = 0; i < e->nr_devs; i++)
51                 prt_printf(out, i ? " %u" : "%u", e->devs[i]);
52         prt_printf(out, "]");
53 }
54
55 void bch2_replicas_entry_to_text(struct printbuf *out,
56                                  struct bch_replicas_entry *e)
57 {
58         unsigned i;
59
60         if (e->data_type < BCH_DATA_NR)
61                 prt_printf(out, "%s", bch2_data_types[e->data_type]);
62         else
63                 prt_printf(out, "(invalid data type %u)", e->data_type);
64
65         prt_printf(out, ": %u/%u [", e->nr_required, e->nr_devs);
66         for (i = 0; i < e->nr_devs; i++)
67                 prt_printf(out, i ? " %u" : "%u", e->devs[i]);
68         prt_printf(out, "]");
69 }
70
71 void bch2_cpu_replicas_to_text(struct printbuf *out,
72                                struct bch_replicas_cpu *r)
73 {
74         struct bch_replicas_entry *e;
75         bool first = true;
76
77         for_each_cpu_replicas_entry(r, e) {
78                 if (!first)
79                         prt_printf(out, " ");
80                 first = false;
81
82                 bch2_replicas_entry_to_text(out, e);
83         }
84 }
85
86 static void extent_to_replicas(struct bkey_s_c k,
87                                struct bch_replicas_entry *r)
88 {
89         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
90         const union bch_extent_entry *entry;
91         struct extent_ptr_decoded p;
92
93         r->nr_required  = 1;
94
95         bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
96                 if (p.ptr.cached)
97                         continue;
98
99                 if (!p.has_ec)
100                         r->devs[r->nr_devs++] = p.ptr.dev;
101                 else
102                         r->nr_required = 0;
103         }
104 }
105
106 static void stripe_to_replicas(struct bkey_s_c k,
107                                struct bch_replicas_entry *r)
108 {
109         struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
110         const struct bch_extent_ptr *ptr;
111
112         r->nr_required  = s.v->nr_blocks - s.v->nr_redundant;
113
114         for (ptr = s.v->ptrs;
115              ptr < s.v->ptrs + s.v->nr_blocks;
116              ptr++)
117                 r->devs[r->nr_devs++] = ptr->dev;
118 }
119
120 void bch2_bkey_to_replicas(struct bch_replicas_entry *e,
121                            struct bkey_s_c k)
122 {
123         e->nr_devs = 0;
124
125         switch (k.k->type) {
126         case KEY_TYPE_btree_ptr:
127         case KEY_TYPE_btree_ptr_v2:
128                 e->data_type = BCH_DATA_btree;
129                 extent_to_replicas(k, e);
130                 break;
131         case KEY_TYPE_extent:
132         case KEY_TYPE_reflink_v:
133                 e->data_type = BCH_DATA_user;
134                 extent_to_replicas(k, e);
135                 break;
136         case KEY_TYPE_stripe:
137                 e->data_type = BCH_DATA_parity;
138                 stripe_to_replicas(k, e);
139                 break;
140         }
141
142         bch2_replicas_entry_sort(e);
143 }
144
145 void bch2_devlist_to_replicas(struct bch_replicas_entry *e,
146                               enum bch_data_type data_type,
147                               struct bch_devs_list devs)
148 {
149         unsigned i;
150
151         BUG_ON(!data_type ||
152                data_type == BCH_DATA_sb ||
153                data_type >= BCH_DATA_NR);
154
155         e->data_type    = data_type;
156         e->nr_devs      = 0;
157         e->nr_required  = 1;
158
159         for (i = 0; i < devs.nr; i++)
160                 e->devs[e->nr_devs++] = devs.devs[i];
161
162         bch2_replicas_entry_sort(e);
163 }
164
165 static struct bch_replicas_cpu
166 cpu_replicas_add_entry(struct bch_replicas_cpu *old,
167                        struct bch_replicas_entry *new_entry)
168 {
169         unsigned i;
170         struct bch_replicas_cpu new = {
171                 .nr             = old->nr + 1,
172                 .entry_size     = max_t(unsigned, old->entry_size,
173                                         replicas_entry_bytes(new_entry)),
174         };
175
176         BUG_ON(!new_entry->data_type);
177         verify_replicas_entry(new_entry);
178
179         new.entries = kcalloc(new.nr, new.entry_size, GFP_KERNEL);
180         if (!new.entries)
181                 return new;
182
183         for (i = 0; i < old->nr; i++)
184                 memcpy(cpu_replicas_entry(&new, i),
185                        cpu_replicas_entry(old, i),
186                        old->entry_size);
187
188         memcpy(cpu_replicas_entry(&new, old->nr),
189                new_entry,
190                replicas_entry_bytes(new_entry));
191
192         bch2_cpu_replicas_sort(&new);
193         return new;
194 }
195
196 static inline int __replicas_entry_idx(struct bch_replicas_cpu *r,
197                                        struct bch_replicas_entry *search)
198 {
199         int idx, entry_size = replicas_entry_bytes(search);
200
201         if (unlikely(entry_size > r->entry_size))
202                 return -1;
203
204         verify_replicas_entry(search);
205
206 #define entry_cmp(_l, _r, size) memcmp(_l, _r, entry_size)
207         idx = eytzinger0_find(r->entries, r->nr, r->entry_size,
208                               entry_cmp, search);
209 #undef entry_cmp
210
211         return idx < r->nr ? idx : -1;
212 }
213
214 int bch2_replicas_entry_idx(struct bch_fs *c,
215                             struct bch_replicas_entry *search)
216 {
217         bch2_replicas_entry_sort(search);
218
219         return __replicas_entry_idx(&c->replicas, search);
220 }
221
222 static bool __replicas_has_entry(struct bch_replicas_cpu *r,
223                                  struct bch_replicas_entry *search)
224 {
225         return __replicas_entry_idx(r, search) >= 0;
226 }
227
228 bool bch2_replicas_marked(struct bch_fs *c,
229                           struct bch_replicas_entry *search)
230 {
231         bool marked;
232
233         if (!search->nr_devs)
234                 return true;
235
236         verify_replicas_entry(search);
237
238         percpu_down_read(&c->mark_lock);
239         marked = __replicas_has_entry(&c->replicas, search) &&
240                 (likely((!c->replicas_gc.entries)) ||
241                  __replicas_has_entry(&c->replicas_gc, search));
242         percpu_up_read(&c->mark_lock);
243
244         return marked;
245 }
246
247 static void __replicas_table_update(struct bch_fs_usage *dst,
248                                     struct bch_replicas_cpu *dst_r,
249                                     struct bch_fs_usage *src,
250                                     struct bch_replicas_cpu *src_r)
251 {
252         int src_idx, dst_idx;
253
254         *dst = *src;
255
256         for (src_idx = 0; src_idx < src_r->nr; src_idx++) {
257                 if (!src->replicas[src_idx])
258                         continue;
259
260                 dst_idx = __replicas_entry_idx(dst_r,
261                                 cpu_replicas_entry(src_r, src_idx));
262                 BUG_ON(dst_idx < 0);
263
264                 dst->replicas[dst_idx] = src->replicas[src_idx];
265         }
266 }
267
268 static void __replicas_table_update_pcpu(struct bch_fs_usage __percpu *dst_p,
269                                     struct bch_replicas_cpu *dst_r,
270                                     struct bch_fs_usage __percpu *src_p,
271                                     struct bch_replicas_cpu *src_r)
272 {
273         unsigned src_nr = sizeof(struct bch_fs_usage) / sizeof(u64) + src_r->nr;
274         struct bch_fs_usage *dst, *src = (void *)
275                 bch2_acc_percpu_u64s((void *) src_p, src_nr);
276
277         preempt_disable();
278         dst = this_cpu_ptr(dst_p);
279         preempt_enable();
280
281         __replicas_table_update(dst, dst_r, src, src_r);
282 }
283
284 /*
285  * Resize filesystem accounting:
286  */
287 static int replicas_table_update(struct bch_fs *c,
288                                  struct bch_replicas_cpu *new_r)
289 {
290         struct bch_fs_usage __percpu *new_usage[JOURNAL_BUF_NR];
291         struct bch_fs_usage_online *new_scratch = NULL;
292         struct bch_fs_usage __percpu *new_gc = NULL;
293         struct bch_fs_usage *new_base = NULL;
294         unsigned i, bytes = sizeof(struct bch_fs_usage) +
295                 sizeof(u64) * new_r->nr;
296         unsigned scratch_bytes = sizeof(struct bch_fs_usage_online) +
297                 sizeof(u64) * new_r->nr;
298         int ret = 0;
299
300         memset(new_usage, 0, sizeof(new_usage));
301
302         for (i = 0; i < ARRAY_SIZE(new_usage); i++)
303                 if (!(new_usage[i] = __alloc_percpu_gfp(bytes,
304                                         sizeof(u64), GFP_KERNEL)))
305                         goto err;
306
307         memset(new_usage, 0, sizeof(new_usage));
308
309         for (i = 0; i < ARRAY_SIZE(new_usage); i++)
310                 if (!(new_usage[i] = __alloc_percpu_gfp(bytes,
311                                         sizeof(u64), GFP_KERNEL)))
312                         goto err;
313
314         if (!(new_base = kzalloc(bytes, GFP_KERNEL)) ||
315             !(new_scratch  = kmalloc(scratch_bytes, GFP_KERNEL)) ||
316             (c->usage_gc &&
317              !(new_gc = __alloc_percpu_gfp(bytes, sizeof(u64), GFP_KERNEL))))
318                 goto err;
319
320         for (i = 0; i < ARRAY_SIZE(new_usage); i++)
321                 if (c->usage[i])
322                         __replicas_table_update_pcpu(new_usage[i], new_r,
323                                                      c->usage[i], &c->replicas);
324         if (c->usage_base)
325                 __replicas_table_update(new_base,               new_r,
326                                         c->usage_base,          &c->replicas);
327         if (c->usage_gc)
328                 __replicas_table_update_pcpu(new_gc,            new_r,
329                                              c->usage_gc,       &c->replicas);
330
331         for (i = 0; i < ARRAY_SIZE(new_usage); i++)
332                 swap(c->usage[i],       new_usage[i]);
333         swap(c->usage_base,     new_base);
334         swap(c->usage_scratch,  new_scratch);
335         swap(c->usage_gc,       new_gc);
336         swap(c->replicas,       *new_r);
337 out:
338         free_percpu(new_gc);
339         kfree(new_scratch);
340         for (i = 0; i < ARRAY_SIZE(new_usage); i++)
341                 free_percpu(new_usage[i]);
342         kfree(new_base);
343         return ret;
344 err:
345         bch_err(c, "error updating replicas table: memory allocation failure");
346         ret = -ENOMEM;
347         goto out;
348 }
349
350 static unsigned reserve_journal_replicas(struct bch_fs *c,
351                                      struct bch_replicas_cpu *r)
352 {
353         struct bch_replicas_entry *e;
354         unsigned journal_res_u64s = 0;
355
356         /* nr_inodes: */
357         journal_res_u64s +=
358                 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
359
360         /* key_version: */
361         journal_res_u64s +=
362                 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
363
364         /* persistent_reserved: */
365         journal_res_u64s +=
366                 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64)) *
367                 BCH_REPLICAS_MAX;
368
369         for_each_cpu_replicas_entry(r, e)
370                 journal_res_u64s +=
371                         DIV_ROUND_UP(sizeof(struct jset_entry_data_usage) +
372                                      e->nr_devs, sizeof(u64));
373         return journal_res_u64s;
374 }
375
376 noinline
377 static int bch2_mark_replicas_slowpath(struct bch_fs *c,
378                                 struct bch_replicas_entry *new_entry)
379 {
380         struct bch_replicas_cpu new_r, new_gc;
381         int ret = 0;
382
383         verify_replicas_entry(new_entry);
384
385         memset(&new_r, 0, sizeof(new_r));
386         memset(&new_gc, 0, sizeof(new_gc));
387
388         mutex_lock(&c->sb_lock);
389
390         if (c->replicas_gc.entries &&
391             !__replicas_has_entry(&c->replicas_gc, new_entry)) {
392                 new_gc = cpu_replicas_add_entry(&c->replicas_gc, new_entry);
393                 if (!new_gc.entries)
394                         goto err;
395         }
396
397         if (!__replicas_has_entry(&c->replicas, new_entry)) {
398                 new_r = cpu_replicas_add_entry(&c->replicas, new_entry);
399                 if (!new_r.entries)
400                         goto err;
401
402                 ret = bch2_cpu_replicas_to_sb_replicas(c, &new_r);
403                 if (ret)
404                         goto err;
405
406                 bch2_journal_entry_res_resize(&c->journal,
407                                 &c->replicas_journal_res,
408                                 reserve_journal_replicas(c, &new_r));
409         }
410
411         if (!new_r.entries &&
412             !new_gc.entries)
413                 goto out;
414
415         /* allocations done, now commit: */
416
417         if (new_r.entries)
418                 bch2_write_super(c);
419
420         /* don't update in memory replicas until changes are persistent */
421         percpu_down_write(&c->mark_lock);
422         if (new_r.entries)
423                 ret = replicas_table_update(c, &new_r);
424         if (new_gc.entries)
425                 swap(new_gc, c->replicas_gc);
426         percpu_up_write(&c->mark_lock);
427 out:
428         mutex_unlock(&c->sb_lock);
429
430         kfree(new_r.entries);
431         kfree(new_gc.entries);
432
433         return ret;
434 err:
435         bch_err(c, "error adding replicas entry: memory allocation failure");
436         ret = -ENOMEM;
437         goto out;
438 }
439
440 int bch2_mark_replicas(struct bch_fs *c, struct bch_replicas_entry *r)
441 {
442         return likely(bch2_replicas_marked(c, r))
443                 ? 0 : bch2_mark_replicas_slowpath(c, r);
444 }
445
446 /* replicas delta list: */
447
448 int bch2_replicas_delta_list_mark(struct bch_fs *c,
449                                   struct replicas_delta_list *r)
450 {
451         struct replicas_delta *d = r->d;
452         struct replicas_delta *top = (void *) r->d + r->used;
453         int ret = 0;
454
455         for (d = r->d; !ret && d != top; d = replicas_delta_next(d))
456                 ret = bch2_mark_replicas(c, &d->r);
457         return ret;
458 }
459
460 /*
461  * Old replicas_gc mechanism: only used for journal replicas entries now, should
462  * die at some point:
463  */
464
465 int bch2_replicas_gc_end(struct bch_fs *c, int ret)
466 {
467         unsigned i;
468
469         lockdep_assert_held(&c->replicas_gc_lock);
470
471         mutex_lock(&c->sb_lock);
472         percpu_down_write(&c->mark_lock);
473
474         /*
475          * this is kind of crappy; the replicas gc mechanism needs to be ripped
476          * out
477          */
478
479         for (i = 0; i < c->replicas.nr; i++) {
480                 struct bch_replicas_entry *e =
481                         cpu_replicas_entry(&c->replicas, i);
482                 struct bch_replicas_cpu n;
483
484                 if (!__replicas_has_entry(&c->replicas_gc, e) &&
485                     bch2_fs_usage_read_one(c, &c->usage_base->replicas[i])) {
486                         n = cpu_replicas_add_entry(&c->replicas_gc, e);
487                         if (!n.entries) {
488                                 ret = -ENOMEM;
489                                 goto err;
490                         }
491
492                         swap(n, c->replicas_gc);
493                         kfree(n.entries);
494                 }
495         }
496
497         ret = bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc);
498         if (ret)
499                 goto err;
500
501         ret = replicas_table_update(c, &c->replicas_gc);
502 err:
503         kfree(c->replicas_gc.entries);
504         c->replicas_gc.entries = NULL;
505
506         percpu_up_write(&c->mark_lock);
507
508         if (!ret)
509                 bch2_write_super(c);
510
511         mutex_unlock(&c->sb_lock);
512
513         return ret;
514 }
515
516 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
517 {
518         struct bch_replicas_entry *e;
519         unsigned i = 0;
520
521         lockdep_assert_held(&c->replicas_gc_lock);
522
523         mutex_lock(&c->sb_lock);
524         BUG_ON(c->replicas_gc.entries);
525
526         c->replicas_gc.nr               = 0;
527         c->replicas_gc.entry_size       = 0;
528
529         for_each_cpu_replicas_entry(&c->replicas, e)
530                 if (!((1 << e->data_type) & typemask)) {
531                         c->replicas_gc.nr++;
532                         c->replicas_gc.entry_size =
533                                 max_t(unsigned, c->replicas_gc.entry_size,
534                                       replicas_entry_bytes(e));
535                 }
536
537         c->replicas_gc.entries = kcalloc(c->replicas_gc.nr,
538                                          c->replicas_gc.entry_size,
539                                          GFP_KERNEL);
540         if (!c->replicas_gc.entries) {
541                 mutex_unlock(&c->sb_lock);
542                 bch_err(c, "error allocating c->replicas_gc");
543                 return -ENOMEM;
544         }
545
546         for_each_cpu_replicas_entry(&c->replicas, e)
547                 if (!((1 << e->data_type) & typemask))
548                         memcpy(cpu_replicas_entry(&c->replicas_gc, i++),
549                                e, c->replicas_gc.entry_size);
550
551         bch2_cpu_replicas_sort(&c->replicas_gc);
552         mutex_unlock(&c->sb_lock);
553
554         return 0;
555 }
556
557 /* New much simpler mechanism for clearing out unneeded replicas entries: */
558
559 int bch2_replicas_gc2(struct bch_fs *c)
560 {
561         struct bch_replicas_cpu new = { 0 };
562         unsigned i, nr;
563         int ret = 0;
564
565         bch2_journal_meta(&c->journal);
566 retry:
567         nr              = READ_ONCE(c->replicas.nr);
568         new.entry_size  = READ_ONCE(c->replicas.entry_size);
569         new.entries     = kcalloc(nr, new.entry_size, GFP_KERNEL);
570         if (!new.entries) {
571                 bch_err(c, "error allocating c->replicas_gc");
572                 return -ENOMEM;
573         }
574
575         mutex_lock(&c->sb_lock);
576         percpu_down_write(&c->mark_lock);
577
578         if (nr                  != c->replicas.nr ||
579             new.entry_size      != c->replicas.entry_size) {
580                 percpu_up_write(&c->mark_lock);
581                 mutex_unlock(&c->sb_lock);
582                 kfree(new.entries);
583                 goto retry;
584         }
585
586         for (i = 0; i < c->replicas.nr; i++) {
587                 struct bch_replicas_entry *e =
588                         cpu_replicas_entry(&c->replicas, i);
589
590                 if (e->data_type == BCH_DATA_journal ||
591                     c->usage_base->replicas[i] ||
592                     percpu_u64_get(&c->usage[0]->replicas[i]) ||
593                     percpu_u64_get(&c->usage[1]->replicas[i]) ||
594                     percpu_u64_get(&c->usage[2]->replicas[i]) ||
595                     percpu_u64_get(&c->usage[3]->replicas[i]))
596                         memcpy(cpu_replicas_entry(&new, new.nr++),
597                                e, new.entry_size);
598         }
599
600         bch2_cpu_replicas_sort(&new);
601
602         ret = bch2_cpu_replicas_to_sb_replicas(c, &new);
603         if (ret)
604                 goto err;
605
606         ret = replicas_table_update(c, &new);
607 err:
608         kfree(new.entries);
609
610         percpu_up_write(&c->mark_lock);
611
612         if (!ret)
613                 bch2_write_super(c);
614
615         mutex_unlock(&c->sb_lock);
616
617         return ret;
618 }
619
620 int bch2_replicas_set_usage(struct bch_fs *c,
621                             struct bch_replicas_entry *r,
622                             u64 sectors)
623 {
624         int ret, idx = bch2_replicas_entry_idx(c, r);
625
626         if (idx < 0) {
627                 struct bch_replicas_cpu n;
628
629                 n = cpu_replicas_add_entry(&c->replicas, r);
630                 if (!n.entries)
631                         return -ENOMEM;
632
633                 ret = replicas_table_update(c, &n);
634                 if (ret)
635                         return ret;
636
637                 kfree(n.entries);
638
639                 idx = bch2_replicas_entry_idx(c, r);
640                 BUG_ON(ret < 0);
641         }
642
643         c->usage_base->replicas[idx] = sectors;
644
645         return 0;
646 }
647
648 /* Replicas tracking - superblock: */
649
650 static int
651 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
652                                    struct bch_replicas_cpu *cpu_r)
653 {
654         struct bch_replicas_entry *e, *dst;
655         unsigned nr = 0, entry_size = 0, idx = 0;
656
657         for_each_replicas_entry(sb_r, e) {
658                 entry_size = max_t(unsigned, entry_size,
659                                    replicas_entry_bytes(e));
660                 nr++;
661         }
662
663         cpu_r->entries = kcalloc(nr, entry_size, GFP_KERNEL);
664         if (!cpu_r->entries)
665                 return -ENOMEM;
666
667         cpu_r->nr               = nr;
668         cpu_r->entry_size       = entry_size;
669
670         for_each_replicas_entry(sb_r, e) {
671                 dst = cpu_replicas_entry(cpu_r, idx++);
672                 memcpy(dst, e, replicas_entry_bytes(e));
673                 bch2_replicas_entry_sort(dst);
674         }
675
676         return 0;
677 }
678
679 static int
680 __bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
681                                       struct bch_replicas_cpu *cpu_r)
682 {
683         struct bch_replicas_entry_v0 *e;
684         unsigned nr = 0, entry_size = 0, idx = 0;
685
686         for_each_replicas_entry(sb_r, e) {
687                 entry_size = max_t(unsigned, entry_size,
688                                    replicas_entry_bytes(e));
689                 nr++;
690         }
691
692         entry_size += sizeof(struct bch_replicas_entry) -
693                 sizeof(struct bch_replicas_entry_v0);
694
695         cpu_r->entries = kcalloc(nr, entry_size, GFP_KERNEL);
696         if (!cpu_r->entries)
697                 return -ENOMEM;
698
699         cpu_r->nr               = nr;
700         cpu_r->entry_size       = entry_size;
701
702         for_each_replicas_entry(sb_r, e) {
703                 struct bch_replicas_entry *dst =
704                         cpu_replicas_entry(cpu_r, idx++);
705
706                 dst->data_type  = e->data_type;
707                 dst->nr_devs    = e->nr_devs;
708                 dst->nr_required = 1;
709                 memcpy(dst->devs, e->devs, e->nr_devs);
710                 bch2_replicas_entry_sort(dst);
711         }
712
713         return 0;
714 }
715
716 int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
717 {
718         struct bch_sb_field_replicas *sb_v1;
719         struct bch_sb_field_replicas_v0 *sb_v0;
720         struct bch_replicas_cpu new_r = { 0, 0, NULL };
721         int ret = 0;
722
723         if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
724                 ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
725         else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
726                 ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
727
728         if (ret)
729                 return -ENOMEM;
730
731         bch2_cpu_replicas_sort(&new_r);
732
733         percpu_down_write(&c->mark_lock);
734
735         ret = replicas_table_update(c, &new_r);
736         percpu_up_write(&c->mark_lock);
737
738         kfree(new_r.entries);
739
740         return 0;
741 }
742
743 static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
744                                                struct bch_replicas_cpu *r)
745 {
746         struct bch_sb_field_replicas_v0 *sb_r;
747         struct bch_replicas_entry_v0 *dst;
748         struct bch_replicas_entry *src;
749         size_t bytes;
750
751         bytes = sizeof(struct bch_sb_field_replicas);
752
753         for_each_cpu_replicas_entry(r, src)
754                 bytes += replicas_entry_bytes(src) - 1;
755
756         sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
757                         DIV_ROUND_UP(bytes, sizeof(u64)));
758         if (!sb_r)
759                 return -BCH_ERR_ENOSPC_sb_replicas;
760
761         bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
762         sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
763
764         memset(&sb_r->entries, 0,
765                vstruct_end(&sb_r->field) -
766                (void *) &sb_r->entries);
767
768         dst = sb_r->entries;
769         for_each_cpu_replicas_entry(r, src) {
770                 dst->data_type  = src->data_type;
771                 dst->nr_devs    = src->nr_devs;
772                 memcpy(dst->devs, src->devs, src->nr_devs);
773
774                 dst = replicas_entry_next(dst);
775
776                 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
777         }
778
779         return 0;
780 }
781
782 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
783                                             struct bch_replicas_cpu *r)
784 {
785         struct bch_sb_field_replicas *sb_r;
786         struct bch_replicas_entry *dst, *src;
787         bool need_v1 = false;
788         size_t bytes;
789
790         bytes = sizeof(struct bch_sb_field_replicas);
791
792         for_each_cpu_replicas_entry(r, src) {
793                 bytes += replicas_entry_bytes(src);
794                 if (src->nr_required != 1)
795                         need_v1 = true;
796         }
797
798         if (!need_v1)
799                 return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
800
801         sb_r = bch2_sb_resize_replicas(&c->disk_sb,
802                         DIV_ROUND_UP(bytes, sizeof(u64)));
803         if (!sb_r)
804                 return -BCH_ERR_ENOSPC_sb_replicas;
805
806         bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
807         sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
808
809         memset(&sb_r->entries, 0,
810                vstruct_end(&sb_r->field) -
811                (void *) &sb_r->entries);
812
813         dst = sb_r->entries;
814         for_each_cpu_replicas_entry(r, src) {
815                 memcpy(dst, src, replicas_entry_bytes(src));
816
817                 dst = replicas_entry_next(dst);
818
819                 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
820         }
821
822         return 0;
823 }
824
825 static int bch2_cpu_replicas_validate(struct bch_replicas_cpu *cpu_r,
826                                       struct bch_sb *sb,
827                                       struct printbuf *err)
828 {
829         struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
830         unsigned i, j;
831
832         sort_cmp_size(cpu_r->entries,
833                       cpu_r->nr,
834                       cpu_r->entry_size,
835                       memcmp, NULL);
836
837         for (i = 0; i < cpu_r->nr; i++) {
838                 struct bch_replicas_entry *e =
839                         cpu_replicas_entry(cpu_r, i);
840
841                 if (e->data_type >= BCH_DATA_NR) {
842                         prt_printf(err, "invalid data type in entry ");
843                         bch2_replicas_entry_to_text(err, e);
844                         return -BCH_ERR_invalid_sb_replicas;
845                 }
846
847                 if (!e->nr_devs) {
848                         prt_printf(err, "no devices in entry ");
849                         bch2_replicas_entry_to_text(err, e);
850                         return -BCH_ERR_invalid_sb_replicas;
851                 }
852
853                 if (e->nr_required > 1 &&
854                     e->nr_required >= e->nr_devs) {
855                         prt_printf(err, "bad nr_required in entry ");
856                         bch2_replicas_entry_to_text(err, e);
857                         return -BCH_ERR_invalid_sb_replicas;
858                 }
859
860                 for (j = 0; j < e->nr_devs; j++)
861                         if (!bch2_dev_exists(sb, mi, e->devs[j])) {
862                                 prt_printf(err, "invalid device %u in entry ", e->devs[j]);
863                                 bch2_replicas_entry_to_text(err, e);
864                                 return -BCH_ERR_invalid_sb_replicas;
865                         }
866
867                 if (i + 1 < cpu_r->nr) {
868                         struct bch_replicas_entry *n =
869                                 cpu_replicas_entry(cpu_r, i + 1);
870
871                         BUG_ON(memcmp(e, n, cpu_r->entry_size) > 0);
872
873                         if (!memcmp(e, n, cpu_r->entry_size)) {
874                                 prt_printf(err, "duplicate replicas entry ");
875                                 bch2_replicas_entry_to_text(err, e);
876                                 return -BCH_ERR_invalid_sb_replicas;
877                         }
878                 }
879         }
880
881         return 0;
882 }
883
884 static int bch2_sb_replicas_validate(struct bch_sb *sb, struct bch_sb_field *f,
885                                      struct printbuf *err)
886 {
887         struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
888         struct bch_replicas_cpu cpu_r;
889         int ret;
890
891         if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
892                 return -ENOMEM;
893
894         ret = bch2_cpu_replicas_validate(&cpu_r, sb, err);
895         kfree(cpu_r.entries);
896         return ret;
897 }
898
899 static void bch2_sb_replicas_to_text(struct printbuf *out,
900                                      struct bch_sb *sb,
901                                      struct bch_sb_field *f)
902 {
903         struct bch_sb_field_replicas *r = field_to_type(f, replicas);
904         struct bch_replicas_entry *e;
905         bool first = true;
906
907         for_each_replicas_entry(r, e) {
908                 if (!first)
909                         prt_printf(out, " ");
910                 first = false;
911
912                 bch2_replicas_entry_to_text(out, e);
913         }
914         prt_newline(out);
915 }
916
917 const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
918         .validate       = bch2_sb_replicas_validate,
919         .to_text        = bch2_sb_replicas_to_text,
920 };
921
922 static int bch2_sb_replicas_v0_validate(struct bch_sb *sb, struct bch_sb_field *f,
923                                         struct printbuf *err)
924 {
925         struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
926         struct bch_replicas_cpu cpu_r;
927         int ret;
928
929         if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
930                 return -ENOMEM;
931
932         ret = bch2_cpu_replicas_validate(&cpu_r, sb, err);
933         kfree(cpu_r.entries);
934         return ret;
935 }
936
937 static void bch2_sb_replicas_v0_to_text(struct printbuf *out,
938                                         struct bch_sb *sb,
939                                         struct bch_sb_field *f)
940 {
941         struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
942         struct bch_replicas_entry_v0 *e;
943         bool first = true;
944
945         for_each_replicas_entry(sb_r, e) {
946                 if (!first)
947                         prt_printf(out, " ");
948                 first = false;
949
950                 bch2_replicas_entry_v0_to_text(out, e);
951         }
952         prt_newline(out);
953 }
954
955 const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
956         .validate       = bch2_sb_replicas_v0_validate,
957         .to_text        = bch2_sb_replicas_v0_to_text,
958 };
959
960 /* Query replicas: */
961
962 bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs,
963                            unsigned flags, bool print)
964 {
965         struct bch_replicas_entry *e;
966         bool ret = true;
967
968         percpu_down_read(&c->mark_lock);
969         for_each_cpu_replicas_entry(&c->replicas, e) {
970                 unsigned i, nr_online = 0, nr_failed = 0, dflags = 0;
971                 bool metadata = e->data_type < BCH_DATA_user;
972
973                 if (e->data_type == BCH_DATA_cached)
974                         continue;
975
976                 for (i = 0; i < e->nr_devs; i++) {
977                         struct bch_dev *ca = bch_dev_bkey_exists(c, e->devs[i]);
978
979                         nr_online += test_bit(e->devs[i], devs.d);
980                         nr_failed += ca->mi.state == BCH_MEMBER_STATE_failed;
981                 }
982
983                 if (nr_failed == e->nr_devs)
984                         continue;
985
986                 if (nr_online < e->nr_required)
987                         dflags |= metadata
988                                 ? BCH_FORCE_IF_METADATA_LOST
989                                 : BCH_FORCE_IF_DATA_LOST;
990
991                 if (nr_online < e->nr_devs)
992                         dflags |= metadata
993                                 ? BCH_FORCE_IF_METADATA_DEGRADED
994                                 : BCH_FORCE_IF_DATA_DEGRADED;
995
996                 if (dflags & ~flags) {
997                         if (print) {
998                                 struct printbuf buf = PRINTBUF;
999
1000                                 bch2_replicas_entry_to_text(&buf, e);
1001                                 bch_err(c, "insufficient devices online (%u) for replicas entry %s",
1002                                         nr_online, buf.buf);
1003                                 printbuf_exit(&buf);
1004                         }
1005                         ret = false;
1006                         break;
1007                 }
1008
1009         }
1010         percpu_up_read(&c->mark_lock);
1011
1012         return ret;
1013 }
1014
1015 unsigned bch2_sb_dev_has_data(struct bch_sb *sb, unsigned dev)
1016 {
1017         struct bch_sb_field_replicas *replicas;
1018         struct bch_sb_field_replicas_v0 *replicas_v0;
1019         unsigned i, data_has = 0;
1020
1021         replicas = bch2_sb_get_replicas(sb);
1022         replicas_v0 = bch2_sb_get_replicas_v0(sb);
1023
1024         if (replicas) {
1025                 struct bch_replicas_entry *r;
1026
1027                 for_each_replicas_entry(replicas, r)
1028                         for (i = 0; i < r->nr_devs; i++)
1029                                 if (r->devs[i] == dev)
1030                                         data_has |= 1 << r->data_type;
1031         } else if (replicas_v0) {
1032                 struct bch_replicas_entry_v0 *r;
1033
1034                 for_each_replicas_entry_v0(replicas_v0, r)
1035                         for (i = 0; i < r->nr_devs; i++)
1036                                 if (r->devs[i] == dev)
1037                                         data_has |= 1 << r->data_type;
1038         }
1039
1040
1041         return data_has;
1042 }
1043
1044 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
1045 {
1046         unsigned ret;
1047
1048         mutex_lock(&c->sb_lock);
1049         ret = bch2_sb_dev_has_data(c->disk_sb.sb, ca->dev_idx);
1050         mutex_unlock(&c->sb_lock);
1051
1052         return ret;
1053 }
1054
1055 void bch2_fs_replicas_exit(struct bch_fs *c)
1056 {
1057         unsigned i;
1058
1059         kfree(c->usage_scratch);
1060         for (i = 0; i < ARRAY_SIZE(c->usage); i++)
1061                 free_percpu(c->usage[i]);
1062         kfree(c->usage_base);
1063         kfree(c->replicas.entries);
1064         kfree(c->replicas_gc.entries);
1065
1066         mempool_exit(&c->replicas_delta_pool);
1067 }
1068
1069 int bch2_fs_replicas_init(struct bch_fs *c)
1070 {
1071         bch2_journal_entry_res_resize(&c->journal,
1072                         &c->replicas_journal_res,
1073                         reserve_journal_replicas(c, &c->replicas));
1074
1075         return mempool_init_kmalloc_pool(&c->replicas_delta_pool, 1,
1076                                          REPLICAS_DELTA_LIST_MAX) ?:
1077                 replicas_table_update(c, &c->replicas);
1078 }