]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/replicas.c
Update bcachefs sources to 31718a2: bcachefs: Don't spin in journal reclaim
[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                 pr_buf(out, "%s", bch2_data_types[e->data_type]);
46         else
47                 pr_buf(out, "(invalid data type %u)", e->data_type);
48
49         pr_buf(out, ": %u [", e->nr_devs);
50         for (i = 0; i < e->nr_devs; i++)
51                 pr_buf(out, i ? " %u" : "%u", e->devs[i]);
52         pr_buf(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                 pr_buf(out, "%s", bch2_data_types[e->data_type]);
62         else
63                 pr_buf(out, "(invalid data type %u)", e->data_type);
64
65         pr_buf(out, ": %u/%u [", e->nr_required, e->nr_devs);
66         for (i = 0; i < e->nr_devs; i++)
67                 pr_buf(out, i ? " %u" : "%u", e->devs[i]);
68         pr_buf(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                         pr_buf(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         if (!(new_base = kzalloc(bytes, GFP_KERNEL)) ||
308             !(new_scratch  = kmalloc(scratch_bytes, GFP_KERNEL)) ||
309             (c->usage_gc &&
310              !(new_gc = __alloc_percpu_gfp(bytes, sizeof(u64), GFP_KERNEL))))
311                 goto err;
312
313         for (i = 0; i < ARRAY_SIZE(new_usage); i++)
314                 if (c->usage[i])
315                         __replicas_table_update_pcpu(new_usage[i], new_r,
316                                                      c->usage[i], &c->replicas);
317         if (c->usage_base)
318                 __replicas_table_update(new_base,               new_r,
319                                         c->usage_base,          &c->replicas);
320         if (c->usage_gc)
321                 __replicas_table_update_pcpu(new_gc,            new_r,
322                                              c->usage_gc,       &c->replicas);
323
324         for (i = 0; i < ARRAY_SIZE(new_usage); i++)
325                 swap(c->usage[i],       new_usage[i]);
326         swap(c->usage_base,     new_base);
327         swap(c->usage_scratch,  new_scratch);
328         swap(c->usage_gc,       new_gc);
329         swap(c->replicas,       *new_r);
330 out:
331         free_percpu(new_gc);
332         kfree(new_scratch);
333         for (i = 0; i < ARRAY_SIZE(new_usage); i++)
334                 free_percpu(new_usage[i]);
335         kfree(new_base);
336         return ret;
337 err:
338         bch_err(c, "error updating replicas table: memory allocation failure");
339         ret = -ENOMEM;
340         goto out;
341 }
342
343 static unsigned reserve_journal_replicas(struct bch_fs *c,
344                                      struct bch_replicas_cpu *r)
345 {
346         struct bch_replicas_entry *e;
347         unsigned journal_res_u64s = 0;
348
349         /* nr_inodes: */
350         journal_res_u64s +=
351                 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
352
353         /* key_version: */
354         journal_res_u64s +=
355                 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
356
357         /* persistent_reserved: */
358         journal_res_u64s +=
359                 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64)) *
360                 BCH_REPLICAS_MAX;
361
362         for_each_cpu_replicas_entry(r, e)
363                 journal_res_u64s +=
364                         DIV_ROUND_UP(sizeof(struct jset_entry_data_usage) +
365                                      e->nr_devs, sizeof(u64));
366         return journal_res_u64s;
367 }
368
369 noinline
370 static int bch2_mark_replicas_slowpath(struct bch_fs *c,
371                                 struct bch_replicas_entry *new_entry)
372 {
373         struct bch_replicas_cpu new_r, new_gc;
374         int ret = 0;
375
376         verify_replicas_entry(new_entry);
377
378         memset(&new_r, 0, sizeof(new_r));
379         memset(&new_gc, 0, sizeof(new_gc));
380
381         mutex_lock(&c->sb_lock);
382
383         if (c->replicas_gc.entries &&
384             !__replicas_has_entry(&c->replicas_gc, new_entry)) {
385                 new_gc = cpu_replicas_add_entry(&c->replicas_gc, new_entry);
386                 if (!new_gc.entries)
387                         goto err;
388         }
389
390         if (!__replicas_has_entry(&c->replicas, new_entry)) {
391                 new_r = cpu_replicas_add_entry(&c->replicas, new_entry);
392                 if (!new_r.entries)
393                         goto err;
394
395                 ret = bch2_cpu_replicas_to_sb_replicas(c, &new_r);
396                 if (ret)
397                         goto err;
398
399                 bch2_journal_entry_res_resize(&c->journal,
400                                 &c->replicas_journal_res,
401                                 reserve_journal_replicas(c, &new_r));
402         }
403
404         if (!new_r.entries &&
405             !new_gc.entries)
406                 goto out;
407
408         /* allocations done, now commit: */
409
410         if (new_r.entries)
411                 bch2_write_super(c);
412
413         /* don't update in memory replicas until changes are persistent */
414         percpu_down_write(&c->mark_lock);
415         if (new_r.entries)
416                 ret = replicas_table_update(c, &new_r);
417         if (new_gc.entries)
418                 swap(new_gc, c->replicas_gc);
419         percpu_up_write(&c->mark_lock);
420 out:
421         mutex_unlock(&c->sb_lock);
422
423         kfree(new_r.entries);
424         kfree(new_gc.entries);
425
426         return ret;
427 err:
428         bch_err(c, "error adding replicas entry: memory allocation failure");
429         ret = -ENOMEM;
430         goto out;
431 }
432
433 int bch2_mark_replicas(struct bch_fs *c, struct bch_replicas_entry *r)
434 {
435         return likely(bch2_replicas_marked(c, r))
436                 ? 0 : bch2_mark_replicas_slowpath(c, r);
437 }
438
439 /* replicas delta list: */
440
441 int bch2_replicas_delta_list_mark(struct bch_fs *c,
442                                   struct replicas_delta_list *r)
443 {
444         struct replicas_delta *d = r->d;
445         struct replicas_delta *top = (void *) r->d + r->used;
446         int ret = 0;
447
448         for (d = r->d; !ret && d != top; d = replicas_delta_next(d))
449                 ret = bch2_mark_replicas(c, &d->r);
450         return ret;
451 }
452
453 /*
454  * Old replicas_gc mechanism: only used for journal replicas entries now, should
455  * die at some point:
456  */
457
458 int bch2_replicas_gc_end(struct bch_fs *c, int ret)
459 {
460         unsigned i;
461
462         lockdep_assert_held(&c->replicas_gc_lock);
463
464         mutex_lock(&c->sb_lock);
465         percpu_down_write(&c->mark_lock);
466
467         /*
468          * this is kind of crappy; the replicas gc mechanism needs to be ripped
469          * out
470          */
471
472         for (i = 0; i < c->replicas.nr; i++) {
473                 struct bch_replicas_entry *e =
474                         cpu_replicas_entry(&c->replicas, i);
475                 struct bch_replicas_cpu n;
476
477                 if (!__replicas_has_entry(&c->replicas_gc, e) &&
478                     bch2_fs_usage_read_one(c, &c->usage_base->replicas[i])) {
479                         n = cpu_replicas_add_entry(&c->replicas_gc, e);
480                         if (!n.entries) {
481                                 ret = -ENOSPC;
482                                 goto err;
483                         }
484
485                         swap(n, c->replicas_gc);
486                         kfree(n.entries);
487                 }
488         }
489
490         if (bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc)) {
491                 ret = -ENOSPC;
492                 goto err;
493         }
494
495         ret = replicas_table_update(c, &c->replicas_gc);
496 err:
497         kfree(c->replicas_gc.entries);
498         c->replicas_gc.entries = NULL;
499
500         percpu_up_write(&c->mark_lock);
501
502         if (!ret)
503                 bch2_write_super(c);
504
505         mutex_unlock(&c->sb_lock);
506
507         return ret;
508 }
509
510 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
511 {
512         struct bch_replicas_entry *e;
513         unsigned i = 0;
514
515         lockdep_assert_held(&c->replicas_gc_lock);
516
517         mutex_lock(&c->sb_lock);
518         BUG_ON(c->replicas_gc.entries);
519
520         c->replicas_gc.nr               = 0;
521         c->replicas_gc.entry_size       = 0;
522
523         for_each_cpu_replicas_entry(&c->replicas, e)
524                 if (!((1 << e->data_type) & typemask)) {
525                         c->replicas_gc.nr++;
526                         c->replicas_gc.entry_size =
527                                 max_t(unsigned, c->replicas_gc.entry_size,
528                                       replicas_entry_bytes(e));
529                 }
530
531         c->replicas_gc.entries = kcalloc(c->replicas_gc.nr,
532                                          c->replicas_gc.entry_size,
533                                          GFP_KERNEL);
534         if (!c->replicas_gc.entries) {
535                 mutex_unlock(&c->sb_lock);
536                 bch_err(c, "error allocating c->replicas_gc");
537                 return -ENOMEM;
538         }
539
540         for_each_cpu_replicas_entry(&c->replicas, e)
541                 if (!((1 << e->data_type) & typemask))
542                         memcpy(cpu_replicas_entry(&c->replicas_gc, i++),
543                                e, c->replicas_gc.entry_size);
544
545         bch2_cpu_replicas_sort(&c->replicas_gc);
546         mutex_unlock(&c->sb_lock);
547
548         return 0;
549 }
550
551 /* New much simpler mechanism for clearing out unneeded replicas entries: */
552
553 int bch2_replicas_gc2(struct bch_fs *c)
554 {
555         struct bch_replicas_cpu new = { 0 };
556         unsigned i, nr;
557         int ret = 0;
558
559         bch2_journal_meta(&c->journal);
560 retry:
561         nr              = READ_ONCE(c->replicas.nr);
562         new.entry_size  = READ_ONCE(c->replicas.entry_size);
563         new.entries     = kcalloc(nr, new.entry_size, GFP_KERNEL);
564         if (!new.entries) {
565                 bch_err(c, "error allocating c->replicas_gc");
566                 return -ENOMEM;
567         }
568
569         mutex_lock(&c->sb_lock);
570         percpu_down_write(&c->mark_lock);
571
572         if (nr                  != c->replicas.nr ||
573             new.entry_size      != c->replicas.entry_size) {
574                 percpu_up_write(&c->mark_lock);
575                 mutex_unlock(&c->sb_lock);
576                 kfree(new.entries);
577                 goto retry;
578         }
579
580         for (i = 0; i < c->replicas.nr; i++) {
581                 struct bch_replicas_entry *e =
582                         cpu_replicas_entry(&c->replicas, i);
583
584                 if (e->data_type == BCH_DATA_journal ||
585                     c->usage_base->replicas[i] ||
586                     percpu_u64_get(&c->usage[0]->replicas[i]) ||
587                     percpu_u64_get(&c->usage[1]->replicas[i]) ||
588                     percpu_u64_get(&c->usage[2]->replicas[i]) ||
589                     percpu_u64_get(&c->usage[3]->replicas[i]))
590                         memcpy(cpu_replicas_entry(&new, new.nr++),
591                                e, new.entry_size);
592         }
593
594         bch2_cpu_replicas_sort(&new);
595
596         if (bch2_cpu_replicas_to_sb_replicas(c, &new)) {
597                 ret = -ENOSPC;
598                 goto err;
599         }
600
601         ret = replicas_table_update(c, &new);
602 err:
603         kfree(new.entries);
604
605         percpu_up_write(&c->mark_lock);
606
607         if (!ret)
608                 bch2_write_super(c);
609
610         mutex_unlock(&c->sb_lock);
611
612         return ret;
613 }
614
615 int bch2_replicas_set_usage(struct bch_fs *c,
616                             struct bch_replicas_entry *r,
617                             u64 sectors)
618 {
619         int ret, idx = bch2_replicas_entry_idx(c, r);
620
621         if (idx < 0) {
622                 struct bch_replicas_cpu n;
623
624                 n = cpu_replicas_add_entry(&c->replicas, r);
625                 if (!n.entries)
626                         return -ENOMEM;
627
628                 ret = replicas_table_update(c, &n);
629                 if (ret)
630                         return ret;
631
632                 kfree(n.entries);
633
634                 idx = bch2_replicas_entry_idx(c, r);
635                 BUG_ON(ret < 0);
636         }
637
638         c->usage_base->replicas[idx] = sectors;
639
640         return 0;
641 }
642
643 /* Replicas tracking - superblock: */
644
645 static int
646 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
647                                    struct bch_replicas_cpu *cpu_r)
648 {
649         struct bch_replicas_entry *e, *dst;
650         unsigned nr = 0, entry_size = 0, idx = 0;
651
652         for_each_replicas_entry(sb_r, e) {
653                 entry_size = max_t(unsigned, entry_size,
654                                    replicas_entry_bytes(e));
655                 nr++;
656         }
657
658         cpu_r->entries = kcalloc(nr, entry_size, GFP_KERNEL);
659         if (!cpu_r->entries)
660                 return -ENOMEM;
661
662         cpu_r->nr               = nr;
663         cpu_r->entry_size       = entry_size;
664
665         for_each_replicas_entry(sb_r, e) {
666                 dst = cpu_replicas_entry(cpu_r, idx++);
667                 memcpy(dst, e, replicas_entry_bytes(e));
668                 bch2_replicas_entry_sort(dst);
669         }
670
671         return 0;
672 }
673
674 static int
675 __bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
676                                       struct bch_replicas_cpu *cpu_r)
677 {
678         struct bch_replicas_entry_v0 *e;
679         unsigned nr = 0, entry_size = 0, idx = 0;
680
681         for_each_replicas_entry(sb_r, e) {
682                 entry_size = max_t(unsigned, entry_size,
683                                    replicas_entry_bytes(e));
684                 nr++;
685         }
686
687         entry_size += sizeof(struct bch_replicas_entry) -
688                 sizeof(struct bch_replicas_entry_v0);
689
690         cpu_r->entries = kcalloc(nr, entry_size, GFP_KERNEL);
691         if (!cpu_r->entries)
692                 return -ENOMEM;
693
694         cpu_r->nr               = nr;
695         cpu_r->entry_size       = entry_size;
696
697         for_each_replicas_entry(sb_r, e) {
698                 struct bch_replicas_entry *dst =
699                         cpu_replicas_entry(cpu_r, idx++);
700
701                 dst->data_type  = e->data_type;
702                 dst->nr_devs    = e->nr_devs;
703                 dst->nr_required = 1;
704                 memcpy(dst->devs, e->devs, e->nr_devs);
705                 bch2_replicas_entry_sort(dst);
706         }
707
708         return 0;
709 }
710
711 int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
712 {
713         struct bch_sb_field_replicas *sb_v1;
714         struct bch_sb_field_replicas_v0 *sb_v0;
715         struct bch_replicas_cpu new_r = { 0, 0, NULL };
716         int ret = 0;
717
718         if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
719                 ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
720         else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
721                 ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
722
723         if (ret)
724                 return -ENOMEM;
725
726         bch2_cpu_replicas_sort(&new_r);
727
728         percpu_down_write(&c->mark_lock);
729
730         ret = replicas_table_update(c, &new_r);
731         percpu_up_write(&c->mark_lock);
732
733         kfree(new_r.entries);
734
735         return 0;
736 }
737
738 static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
739                                                struct bch_replicas_cpu *r)
740 {
741         struct bch_sb_field_replicas_v0 *sb_r;
742         struct bch_replicas_entry_v0 *dst;
743         struct bch_replicas_entry *src;
744         size_t bytes;
745
746         bytes = sizeof(struct bch_sb_field_replicas);
747
748         for_each_cpu_replicas_entry(r, src)
749                 bytes += replicas_entry_bytes(src) - 1;
750
751         sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
752                         DIV_ROUND_UP(bytes, sizeof(u64)));
753         if (!sb_r)
754                 return -ENOSPC;
755
756         bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
757         sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
758
759         memset(&sb_r->entries, 0,
760                vstruct_end(&sb_r->field) -
761                (void *) &sb_r->entries);
762
763         dst = sb_r->entries;
764         for_each_cpu_replicas_entry(r, src) {
765                 dst->data_type  = src->data_type;
766                 dst->nr_devs    = src->nr_devs;
767                 memcpy(dst->devs, src->devs, src->nr_devs);
768
769                 dst = replicas_entry_next(dst);
770
771                 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
772         }
773
774         return 0;
775 }
776
777 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
778                                             struct bch_replicas_cpu *r)
779 {
780         struct bch_sb_field_replicas *sb_r;
781         struct bch_replicas_entry *dst, *src;
782         bool need_v1 = false;
783         size_t bytes;
784
785         bytes = sizeof(struct bch_sb_field_replicas);
786
787         for_each_cpu_replicas_entry(r, src) {
788                 bytes += replicas_entry_bytes(src);
789                 if (src->nr_required != 1)
790                         need_v1 = true;
791         }
792
793         if (!need_v1)
794                 return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
795
796         sb_r = bch2_sb_resize_replicas(&c->disk_sb,
797                         DIV_ROUND_UP(bytes, sizeof(u64)));
798         if (!sb_r)
799                 return -ENOSPC;
800
801         bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
802         sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
803
804         memset(&sb_r->entries, 0,
805                vstruct_end(&sb_r->field) -
806                (void *) &sb_r->entries);
807
808         dst = sb_r->entries;
809         for_each_cpu_replicas_entry(r, src) {
810                 memcpy(dst, src, replicas_entry_bytes(src));
811
812                 dst = replicas_entry_next(dst);
813
814                 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
815         }
816
817         return 0;
818 }
819
820 static int bch2_cpu_replicas_validate(struct bch_replicas_cpu *cpu_r,
821                                       struct bch_sb *sb,
822                                       struct printbuf *err)
823 {
824         struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
825         unsigned i, j;
826
827         sort_cmp_size(cpu_r->entries,
828                       cpu_r->nr,
829                       cpu_r->entry_size,
830                       memcmp, NULL);
831
832         for (i = 0; i < cpu_r->nr; i++) {
833                 struct bch_replicas_entry *e =
834                         cpu_replicas_entry(cpu_r, i);
835
836                 if (e->data_type >= BCH_DATA_NR) {
837                         pr_buf(err, "invalid data type in entry ");
838                         bch2_replicas_entry_to_text(err, e);
839                         return -EINVAL;
840                 }
841
842                 if (!e->nr_devs) {
843                         pr_buf(err, "no devices in entry ");
844                         bch2_replicas_entry_to_text(err, e);
845                         return -EINVAL;
846                 }
847
848                 if (e->nr_required > 1 &&
849                     e->nr_required >= e->nr_devs) {
850                         pr_buf(err, "bad nr_required in entry ");
851                         bch2_replicas_entry_to_text(err, e);
852                         return -EINVAL;
853                 }
854
855                 for (j = 0; j < e->nr_devs; j++)
856                         if (!bch2_dev_exists(sb, mi, e->devs[j])) {
857                                 pr_buf(err, "invalid device %u in entry ", e->devs[j]);
858                                 bch2_replicas_entry_to_text(err, e);
859                                 return -EINVAL;
860                         }
861
862                 if (i + 1 < cpu_r->nr) {
863                         struct bch_replicas_entry *n =
864                                 cpu_replicas_entry(cpu_r, i + 1);
865
866                         BUG_ON(memcmp(e, n, cpu_r->entry_size) > 0);
867
868                         if (!memcmp(e, n, cpu_r->entry_size)) {
869                                 pr_buf(err, "duplicate replicas entry ");
870                                 bch2_replicas_entry_to_text(err, e);
871                                 return -EINVAL;
872                         }
873                 }
874         }
875
876         return 0;
877 }
878
879 static int bch2_sb_replicas_validate(struct bch_sb *sb, struct bch_sb_field *f,
880                                      struct printbuf *err)
881 {
882         struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
883         struct bch_replicas_cpu cpu_r;
884         int ret;
885
886         if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
887                 return -ENOMEM;
888
889         ret = bch2_cpu_replicas_validate(&cpu_r, sb, err);
890         kfree(cpu_r.entries);
891         return ret;
892 }
893
894 static void bch2_sb_replicas_to_text(struct printbuf *out,
895                                      struct bch_sb *sb,
896                                      struct bch_sb_field *f)
897 {
898         struct bch_sb_field_replicas *r = field_to_type(f, replicas);
899         struct bch_replicas_entry *e;
900         bool first = true;
901
902         for_each_replicas_entry(r, e) {
903                 if (!first)
904                         pr_buf(out, " ");
905                 first = false;
906
907                 bch2_replicas_entry_to_text(out, e);
908         }
909         pr_newline(out);
910 }
911
912 const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
913         .validate       = bch2_sb_replicas_validate,
914         .to_text        = bch2_sb_replicas_to_text,
915 };
916
917 static int bch2_sb_replicas_v0_validate(struct bch_sb *sb, struct bch_sb_field *f,
918                                         struct printbuf *err)
919 {
920         struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
921         struct bch_replicas_cpu cpu_r;
922         int ret;
923
924         if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
925                 return -ENOMEM;
926
927         ret = bch2_cpu_replicas_validate(&cpu_r, sb, err);
928         kfree(cpu_r.entries);
929         return ret;
930 }
931
932 static void bch2_sb_replicas_v0_to_text(struct printbuf *out,
933                                         struct bch_sb *sb,
934                                         struct bch_sb_field *f)
935 {
936         struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
937         struct bch_replicas_entry_v0 *e;
938         bool first = true;
939
940         for_each_replicas_entry(sb_r, e) {
941                 if (!first)
942                         pr_buf(out, " ");
943                 first = false;
944
945                 bch2_replicas_entry_v0_to_text(out, e);
946         }
947         pr_newline(out);
948 }
949
950 const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
951         .validate       = bch2_sb_replicas_v0_validate,
952         .to_text        = bch2_sb_replicas_v0_to_text,
953 };
954
955 /* Query replicas: */
956
957 bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs,
958                            unsigned flags, bool print)
959 {
960         struct bch_replicas_entry *e;
961         bool ret = true;
962
963         percpu_down_read(&c->mark_lock);
964         for_each_cpu_replicas_entry(&c->replicas, e) {
965                 unsigned i, nr_online = 0, nr_failed = 0, dflags = 0;
966                 bool metadata = e->data_type < BCH_DATA_user;
967
968                 if (e->data_type == BCH_DATA_cached)
969                         continue;
970
971                 for (i = 0; i < e->nr_devs; i++) {
972                         struct bch_dev *ca = bch_dev_bkey_exists(c, e->devs[i]);
973
974                         nr_online += test_bit(e->devs[i], devs.d);
975                         nr_failed += ca->mi.state == BCH_MEMBER_STATE_failed;
976                 }
977
978                 if (nr_failed == e->nr_devs)
979                         continue;
980
981                 if (nr_online < e->nr_required)
982                         dflags |= metadata
983                                 ? BCH_FORCE_IF_METADATA_LOST
984                                 : BCH_FORCE_IF_DATA_LOST;
985
986                 if (nr_online < e->nr_devs)
987                         dflags |= metadata
988                                 ? BCH_FORCE_IF_METADATA_DEGRADED
989                                 : BCH_FORCE_IF_DATA_DEGRADED;
990
991                 if (dflags & ~flags) {
992                         if (print) {
993                                 struct printbuf buf = PRINTBUF;
994
995                                 bch2_replicas_entry_to_text(&buf, e);
996                                 bch_err(c, "insufficient devices online (%u) for replicas entry %s",
997                                         nr_online, buf.buf);
998                                 printbuf_exit(&buf);
999                         }
1000                         ret = false;
1001                         break;
1002                 }
1003
1004         }
1005         percpu_up_read(&c->mark_lock);
1006
1007         return ret;
1008 }
1009
1010 unsigned bch2_sb_dev_has_data(struct bch_sb *sb, unsigned dev)
1011 {
1012         struct bch_sb_field_replicas *replicas;
1013         struct bch_sb_field_replicas_v0 *replicas_v0;
1014         unsigned i, data_has = 0;
1015
1016         replicas = bch2_sb_get_replicas(sb);
1017         replicas_v0 = bch2_sb_get_replicas_v0(sb);
1018
1019         if (replicas) {
1020                 struct bch_replicas_entry *r;
1021
1022                 for_each_replicas_entry(replicas, r)
1023                         for (i = 0; i < r->nr_devs; i++)
1024                                 if (r->devs[i] == dev)
1025                                         data_has |= 1 << r->data_type;
1026         } else if (replicas_v0) {
1027                 struct bch_replicas_entry_v0 *r;
1028
1029                 for_each_replicas_entry_v0(replicas_v0, r)
1030                         for (i = 0; i < r->nr_devs; i++)
1031                                 if (r->devs[i] == dev)
1032                                         data_has |= 1 << r->data_type;
1033         }
1034
1035
1036         return data_has;
1037 }
1038
1039 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
1040 {
1041         unsigned ret;
1042
1043         mutex_lock(&c->sb_lock);
1044         ret = bch2_sb_dev_has_data(c->disk_sb.sb, ca->dev_idx);
1045         mutex_unlock(&c->sb_lock);
1046
1047         return ret;
1048 }
1049
1050 void bch2_fs_replicas_exit(struct bch_fs *c)
1051 {
1052         unsigned i;
1053
1054         kfree(c->usage_scratch);
1055         for (i = 0; i < ARRAY_SIZE(c->usage); i++)
1056                 free_percpu(c->usage[i]);
1057         kfree(c->usage_base);
1058         kfree(c->replicas.entries);
1059         kfree(c->replicas_gc.entries);
1060
1061         mempool_exit(&c->replicas_delta_pool);
1062 }
1063
1064 int bch2_fs_replicas_init(struct bch_fs *c)
1065 {
1066         bch2_journal_entry_res_resize(&c->journal,
1067                         &c->replicas_journal_res,
1068                         reserve_journal_replicas(c, &c->replicas));
1069
1070         return mempool_init_kmalloc_pool(&c->replicas_delta_pool, 1,
1071                                          REPLICAS_DELTA_LIST_MAX) ?:
1072                 replicas_table_update(c, &c->replicas);
1073 }