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