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