1 // SPDX-License-Identifier: GPL-2.0
9 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *,
10 struct bch_replicas_cpu *);
12 /* Replicas tracking - in memory: */
14 static inline int u8_cmp(u8 l, u8 r)
19 static void verify_replicas_entry(struct bch_replicas_entry *e)
21 #ifdef CONFIG_BCACHEFS_DEBUG
24 BUG_ON(e->data_type >= BCH_DATA_NR);
26 BUG_ON(e->nr_required > 1 &&
27 e->nr_required >= e->nr_devs);
29 for (i = 0; i + 1 < e->nr_devs; i++)
30 BUG_ON(e->devs[i] >= e->devs[i + 1]);
34 static void replicas_entry_sort(struct bch_replicas_entry *e)
36 bubble_sort(e->devs, e->nr_devs, u8_cmp);
39 static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
41 eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL);
44 void bch2_replicas_entry_to_text(struct printbuf *out,
45 struct bch_replicas_entry *e)
49 pr_buf(out, "%s: %u/%u [",
50 bch2_data_types[e->data_type],
54 for (i = 0; i < e->nr_devs; i++)
55 pr_buf(out, i ? " %u" : "%u", e->devs[i]);
59 void bch2_cpu_replicas_to_text(struct printbuf *out,
60 struct bch_replicas_cpu *r)
62 struct bch_replicas_entry *e;
65 for_each_cpu_replicas_entry(r, e) {
70 bch2_replicas_entry_to_text(out, e);
74 static void extent_to_replicas(struct bkey_s_c k,
75 struct bch_replicas_entry *r)
77 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
78 const union bch_extent_entry *entry;
79 struct extent_ptr_decoded p;
83 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
88 r->devs[r->nr_devs++] = p.ptr.dev;
94 static void stripe_to_replicas(struct bkey_s_c k,
95 struct bch_replicas_entry *r)
97 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
98 const struct bch_extent_ptr *ptr;
100 r->nr_required = s.v->nr_blocks - s.v->nr_redundant;
102 for (ptr = s.v->ptrs;
103 ptr < s.v->ptrs + s.v->nr_blocks;
105 r->devs[r->nr_devs++] = ptr->dev;
108 void bch2_bkey_to_replicas(struct bch_replicas_entry *e,
114 case KEY_TYPE_btree_ptr:
115 case KEY_TYPE_btree_ptr_v2:
116 e->data_type = BCH_DATA_btree;
117 extent_to_replicas(k, e);
119 case KEY_TYPE_extent:
120 case KEY_TYPE_reflink_v:
121 e->data_type = BCH_DATA_user;
122 extent_to_replicas(k, e);
124 case KEY_TYPE_stripe:
125 e->data_type = BCH_DATA_user;
126 stripe_to_replicas(k, e);
130 replicas_entry_sort(e);
133 void bch2_devlist_to_replicas(struct bch_replicas_entry *e,
134 enum bch_data_type data_type,
135 struct bch_devs_list devs)
140 data_type == BCH_DATA_sb ||
141 data_type >= BCH_DATA_NR);
143 e->data_type = data_type;
147 for (i = 0; i < devs.nr; i++)
148 e->devs[e->nr_devs++] = devs.devs[i];
150 replicas_entry_sort(e);
153 static struct bch_replicas_cpu
154 cpu_replicas_add_entry(struct bch_replicas_cpu *old,
155 struct bch_replicas_entry *new_entry)
158 struct bch_replicas_cpu new = {
160 .entry_size = max_t(unsigned, old->entry_size,
161 replicas_entry_bytes(new_entry)),
164 BUG_ON(!new_entry->data_type);
165 verify_replicas_entry(new_entry);
167 new.entries = kcalloc(new.nr, new.entry_size, GFP_NOIO);
171 for (i = 0; i < old->nr; i++)
172 memcpy(cpu_replicas_entry(&new, i),
173 cpu_replicas_entry(old, i),
176 memcpy(cpu_replicas_entry(&new, old->nr),
178 replicas_entry_bytes(new_entry));
180 bch2_cpu_replicas_sort(&new);
184 static inline int __replicas_entry_idx(struct bch_replicas_cpu *r,
185 struct bch_replicas_entry *search)
187 int idx, entry_size = replicas_entry_bytes(search);
189 if (unlikely(entry_size > r->entry_size))
192 verify_replicas_entry(search);
194 #define entry_cmp(_l, _r, size) memcmp(_l, _r, entry_size)
195 idx = eytzinger0_find(r->entries, r->nr, r->entry_size,
199 return idx < r->nr ? idx : -1;
202 int bch2_replicas_entry_idx(struct bch_fs *c,
203 struct bch_replicas_entry *search)
205 replicas_entry_sort(search);
207 return __replicas_entry_idx(&c->replicas, search);
210 static bool __replicas_has_entry(struct bch_replicas_cpu *r,
211 struct bch_replicas_entry *search)
213 return __replicas_entry_idx(r, search) >= 0;
216 bool bch2_replicas_marked(struct bch_fs *c,
217 struct bch_replicas_entry *search)
221 if (!search->nr_devs)
224 verify_replicas_entry(search);
226 percpu_down_read(&c->mark_lock);
227 marked = __replicas_has_entry(&c->replicas, search) &&
228 (likely((!c->replicas_gc.entries)) ||
229 __replicas_has_entry(&c->replicas_gc, search));
230 percpu_up_read(&c->mark_lock);
235 static void __replicas_table_update(struct bch_fs_usage *dst,
236 struct bch_replicas_cpu *dst_r,
237 struct bch_fs_usage *src,
238 struct bch_replicas_cpu *src_r)
240 int src_idx, dst_idx;
244 for (src_idx = 0; src_idx < src_r->nr; src_idx++) {
245 if (!src->replicas[src_idx])
248 dst_idx = __replicas_entry_idx(dst_r,
249 cpu_replicas_entry(src_r, src_idx));
252 dst->replicas[dst_idx] = src->replicas[src_idx];
256 static void __replicas_table_update_pcpu(struct bch_fs_usage __percpu *dst_p,
257 struct bch_replicas_cpu *dst_r,
258 struct bch_fs_usage __percpu *src_p,
259 struct bch_replicas_cpu *src_r)
261 unsigned src_nr = sizeof(struct bch_fs_usage) / sizeof(u64) + src_r->nr;
262 struct bch_fs_usage *dst, *src = (void *)
263 bch2_acc_percpu_u64s((void *) src_p, src_nr);
266 dst = this_cpu_ptr(dst_p);
269 __replicas_table_update(dst, dst_r, src, src_r);
273 * Resize filesystem accounting:
275 static int replicas_table_update(struct bch_fs *c,
276 struct bch_replicas_cpu *new_r)
278 struct bch_fs_usage __percpu *new_usage[2] = { NULL, NULL };
279 struct bch_fs_usage *new_scratch = NULL;
280 struct bch_fs_usage __percpu *new_gc = NULL;
281 struct bch_fs_usage *new_base = NULL;
282 unsigned bytes = sizeof(struct bch_fs_usage) +
283 sizeof(u64) * new_r->nr;
286 if (!(new_base = kzalloc(bytes, GFP_NOIO)) ||
287 !(new_usage[0] = __alloc_percpu_gfp(bytes, sizeof(u64),
289 !(new_usage[1] = __alloc_percpu_gfp(bytes, sizeof(u64),
291 !(new_scratch = kmalloc(bytes, GFP_NOIO)) ||
293 !(new_gc = __alloc_percpu_gfp(bytes, sizeof(u64), GFP_NOIO)))) {
294 bch_err(c, "error updating replicas table: memory allocation failure");
299 __replicas_table_update(new_base, new_r,
300 c->usage_base, &c->replicas);
302 __replicas_table_update_pcpu(new_usage[0], new_r,
303 c->usage[0], &c->replicas);
305 __replicas_table_update_pcpu(new_usage[1], new_r,
306 c->usage[1], &c->replicas);
308 __replicas_table_update_pcpu(new_gc, new_r,
309 c->usage_gc, &c->replicas);
311 swap(c->usage_base, new_base);
312 swap(c->usage[0], new_usage[0]);
313 swap(c->usage[1], new_usage[1]);
314 swap(c->usage_scratch, new_scratch);
315 swap(c->usage_gc, new_gc);
316 swap(c->replicas, *new_r);
321 free_percpu(new_usage[1]);
322 free_percpu(new_usage[0]);
327 static unsigned reserve_journal_replicas(struct bch_fs *c,
328 struct bch_replicas_cpu *r)
330 struct bch_replicas_entry *e;
331 unsigned journal_res_u64s = 0;
335 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
339 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
341 /* persistent_reserved: */
343 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64)) *
346 for_each_cpu_replicas_entry(r, e)
348 DIV_ROUND_UP(sizeof(struct jset_entry_data_usage) +
349 e->nr_devs, sizeof(u64));
350 return journal_res_u64s;
354 static int bch2_mark_replicas_slowpath(struct bch_fs *c,
355 struct bch_replicas_entry *new_entry)
357 struct bch_replicas_cpu new_r, new_gc;
360 verify_replicas_entry(new_entry);
362 memset(&new_r, 0, sizeof(new_r));
363 memset(&new_gc, 0, sizeof(new_gc));
365 mutex_lock(&c->sb_lock);
367 if (c->replicas_gc.entries &&
368 !__replicas_has_entry(&c->replicas_gc, new_entry)) {
369 new_gc = cpu_replicas_add_entry(&c->replicas_gc, new_entry);
374 if (!__replicas_has_entry(&c->replicas, new_entry)) {
375 new_r = cpu_replicas_add_entry(&c->replicas, new_entry);
379 ret = bch2_cpu_replicas_to_sb_replicas(c, &new_r);
383 bch2_journal_entry_res_resize(&c->journal,
384 &c->replicas_journal_res,
385 reserve_journal_replicas(c, &new_r));
388 if (!new_r.entries &&
392 /* allocations done, now commit: */
397 /* don't update in memory replicas until changes are persistent */
398 percpu_down_write(&c->mark_lock);
400 ret = replicas_table_update(c, &new_r);
402 swap(new_gc, c->replicas_gc);
403 percpu_up_write(&c->mark_lock);
405 mutex_unlock(&c->sb_lock);
407 kfree(new_r.entries);
408 kfree(new_gc.entries);
412 bch_err(c, "error adding replicas entry: memory allocation failure");
417 static int __bch2_mark_replicas(struct bch_fs *c,
418 struct bch_replicas_entry *r,
421 return likely(bch2_replicas_marked(c, r)) ? 0
423 : bch2_mark_replicas_slowpath(c, r);
426 int bch2_mark_replicas(struct bch_fs *c, struct bch_replicas_entry *r)
428 return __bch2_mark_replicas(c, r, false);
431 static int __bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k,
434 struct bch_replicas_padded search;
435 struct bch_devs_list cached = bch2_bkey_cached_devs(k);
439 for (i = 0; i < cached.nr; i++) {
440 bch2_replicas_entry_cached(&search.e, cached.devs[i]);
442 ret = __bch2_mark_replicas(c, &search.e, check);
447 bch2_bkey_to_replicas(&search.e, k);
449 return __bch2_mark_replicas(c, &search.e, check);
452 bool bch2_bkey_replicas_marked(struct bch_fs *c,
455 return __bch2_mark_bkey_replicas(c, k, true) == 0;
458 int bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
460 return __bch2_mark_bkey_replicas(c, k, false);
463 int bch2_replicas_gc_end(struct bch_fs *c, int ret)
467 lockdep_assert_held(&c->replicas_gc_lock);
469 mutex_lock(&c->sb_lock);
470 percpu_down_write(&c->mark_lock);
473 * this is kind of crappy; the replicas gc mechanism needs to be ripped
477 for (i = 0; i < c->replicas.nr; i++) {
478 struct bch_replicas_entry *e =
479 cpu_replicas_entry(&c->replicas, i);
480 struct bch_replicas_cpu n;
482 if (!__replicas_has_entry(&c->replicas_gc, e) &&
483 (c->usage_base->replicas[i] ||
484 percpu_u64_get(&c->usage[0]->replicas[i]) ||
485 percpu_u64_get(&c->usage[1]->replicas[i]))) {
486 n = cpu_replicas_add_entry(&c->replicas_gc, e);
492 swap(n, c->replicas_gc);
497 if (bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc)) {
502 ret = replicas_table_update(c, &c->replicas_gc);
504 kfree(c->replicas_gc.entries);
505 c->replicas_gc.entries = NULL;
507 percpu_up_write(&c->mark_lock);
512 mutex_unlock(&c->sb_lock);
517 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
519 struct bch_replicas_entry *e;
522 lockdep_assert_held(&c->replicas_gc_lock);
524 mutex_lock(&c->sb_lock);
525 BUG_ON(c->replicas_gc.entries);
527 c->replicas_gc.nr = 0;
528 c->replicas_gc.entry_size = 0;
530 for_each_cpu_replicas_entry(&c->replicas, e)
531 if (!((1 << e->data_type) & typemask)) {
533 c->replicas_gc.entry_size =
534 max_t(unsigned, c->replicas_gc.entry_size,
535 replicas_entry_bytes(e));
538 c->replicas_gc.entries = kcalloc(c->replicas_gc.nr,
539 c->replicas_gc.entry_size,
541 if (!c->replicas_gc.entries) {
542 mutex_unlock(&c->sb_lock);
543 bch_err(c, "error allocating c->replicas_gc");
547 for_each_cpu_replicas_entry(&c->replicas, e)
548 if (!((1 << e->data_type) & typemask))
549 memcpy(cpu_replicas_entry(&c->replicas_gc, i++),
550 e, c->replicas_gc.entry_size);
552 bch2_cpu_replicas_sort(&c->replicas_gc);
553 mutex_unlock(&c->sb_lock);
558 int bch2_replicas_gc2(struct bch_fs *c)
560 struct bch_replicas_cpu new = { 0 };
564 bch2_journal_meta(&c->journal);
566 nr = READ_ONCE(c->replicas.nr);
567 new.entry_size = READ_ONCE(c->replicas.entry_size);
568 new.entries = kcalloc(nr, new.entry_size, GFP_KERNEL);
570 bch_err(c, "error allocating c->replicas_gc");
574 mutex_lock(&c->sb_lock);
575 percpu_down_write(&c->mark_lock);
577 if (nr != c->replicas.nr ||
578 new.entry_size != c->replicas.entry_size) {
579 percpu_up_write(&c->mark_lock);
580 mutex_unlock(&c->sb_lock);
585 for (i = 0; i < c->replicas.nr; i++) {
586 struct bch_replicas_entry *e =
587 cpu_replicas_entry(&c->replicas, i);
589 if (e->data_type == BCH_DATA_journal ||
590 c->usage_base->replicas[i] ||
591 percpu_u64_get(&c->usage[0]->replicas[i]) ||
592 percpu_u64_get(&c->usage[1]->replicas[i]))
593 memcpy(cpu_replicas_entry(&new, new.nr++),
597 bch2_cpu_replicas_sort(&new);
599 if (bch2_cpu_replicas_to_sb_replicas(c, &new)) {
604 ret = replicas_table_update(c, &new);
608 percpu_up_write(&c->mark_lock);
613 mutex_unlock(&c->sb_lock);
618 int bch2_replicas_set_usage(struct bch_fs *c,
619 struct bch_replicas_entry *r,
622 int ret, idx = bch2_replicas_entry_idx(c, r);
625 struct bch_replicas_cpu n;
627 n = cpu_replicas_add_entry(&c->replicas, r);
631 ret = replicas_table_update(c, &n);
637 idx = bch2_replicas_entry_idx(c, r);
641 c->usage_base->replicas[idx] = sectors;
646 /* Replicas tracking - superblock: */
649 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
650 struct bch_replicas_cpu *cpu_r)
652 struct bch_replicas_entry *e, *dst;
653 unsigned nr = 0, entry_size = 0, idx = 0;
655 for_each_replicas_entry(sb_r, e) {
656 entry_size = max_t(unsigned, entry_size,
657 replicas_entry_bytes(e));
661 cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
666 cpu_r->entry_size = entry_size;
668 for_each_replicas_entry(sb_r, e) {
669 dst = cpu_replicas_entry(cpu_r, idx++);
670 memcpy(dst, e, replicas_entry_bytes(e));
671 replicas_entry_sort(dst);
678 __bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
679 struct bch_replicas_cpu *cpu_r)
681 struct bch_replicas_entry_v0 *e;
682 unsigned nr = 0, entry_size = 0, idx = 0;
684 for_each_replicas_entry(sb_r, e) {
685 entry_size = max_t(unsigned, entry_size,
686 replicas_entry_bytes(e));
690 entry_size += sizeof(struct bch_replicas_entry) -
691 sizeof(struct bch_replicas_entry_v0);
693 cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
698 cpu_r->entry_size = entry_size;
700 for_each_replicas_entry(sb_r, e) {
701 struct bch_replicas_entry *dst =
702 cpu_replicas_entry(cpu_r, idx++);
704 dst->data_type = e->data_type;
705 dst->nr_devs = e->nr_devs;
706 dst->nr_required = 1;
707 memcpy(dst->devs, e->devs, e->nr_devs);
708 replicas_entry_sort(dst);
714 int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
716 struct bch_sb_field_replicas *sb_v1;
717 struct bch_sb_field_replicas_v0 *sb_v0;
718 struct bch_replicas_cpu new_r = { 0, 0, NULL };
721 if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
722 ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
723 else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
724 ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
729 bch2_cpu_replicas_sort(&new_r);
731 percpu_down_write(&c->mark_lock);
733 ret = replicas_table_update(c, &new_r);
734 percpu_up_write(&c->mark_lock);
736 kfree(new_r.entries);
741 static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
742 struct bch_replicas_cpu *r)
744 struct bch_sb_field_replicas_v0 *sb_r;
745 struct bch_replicas_entry_v0 *dst;
746 struct bch_replicas_entry *src;
749 bytes = sizeof(struct bch_sb_field_replicas);
751 for_each_cpu_replicas_entry(r, src)
752 bytes += replicas_entry_bytes(src) - 1;
754 sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
755 DIV_ROUND_UP(bytes, sizeof(u64)));
759 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
760 sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
762 memset(&sb_r->entries, 0,
763 vstruct_end(&sb_r->field) -
764 (void *) &sb_r->entries);
767 for_each_cpu_replicas_entry(r, src) {
768 dst->data_type = src->data_type;
769 dst->nr_devs = src->nr_devs;
770 memcpy(dst->devs, src->devs, src->nr_devs);
772 dst = replicas_entry_next(dst);
774 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
780 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
781 struct bch_replicas_cpu *r)
783 struct bch_sb_field_replicas *sb_r;
784 struct bch_replicas_entry *dst, *src;
785 bool need_v1 = false;
788 bytes = sizeof(struct bch_sb_field_replicas);
790 for_each_cpu_replicas_entry(r, src) {
791 bytes += replicas_entry_bytes(src);
792 if (src->nr_required != 1)
797 return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
799 sb_r = bch2_sb_resize_replicas(&c->disk_sb,
800 DIV_ROUND_UP(bytes, sizeof(u64)));
804 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
805 sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
807 memset(&sb_r->entries, 0,
808 vstruct_end(&sb_r->field) -
809 (void *) &sb_r->entries);
812 for_each_cpu_replicas_entry(r, src) {
813 memcpy(dst, src, replicas_entry_bytes(src));
815 dst = replicas_entry_next(dst);
817 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
823 static const char *check_dup_replicas_entries(struct bch_replicas_cpu *cpu_r)
827 sort_cmp_size(cpu_r->entries,
832 for (i = 0; i + 1 < cpu_r->nr; i++) {
833 struct bch_replicas_entry *l =
834 cpu_replicas_entry(cpu_r, i);
835 struct bch_replicas_entry *r =
836 cpu_replicas_entry(cpu_r, i + 1);
838 BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0);
840 if (!memcmp(l, r, cpu_r->entry_size))
841 return "duplicate replicas entry";
847 static const char *bch2_sb_validate_replicas(struct bch_sb *sb, struct bch_sb_field *f)
849 struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
850 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
851 struct bch_replicas_cpu cpu_r = { .entries = NULL };
852 struct bch_replicas_entry *e;
856 for_each_replicas_entry(sb_r, e) {
857 err = "invalid replicas entry: invalid data type";
858 if (e->data_type >= BCH_DATA_NR)
861 err = "invalid replicas entry: no devices";
865 err = "invalid replicas entry: bad nr_required";
866 if (e->nr_required > 1 &&
867 e->nr_required >= e->nr_devs)
870 err = "invalid replicas entry: invalid device";
871 for (i = 0; i < e->nr_devs; i++)
872 if (!bch2_dev_exists(sb, mi, e->devs[i]))
876 err = "cannot allocate memory";
877 if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
880 err = check_dup_replicas_entries(&cpu_r);
882 kfree(cpu_r.entries);
886 static void bch2_sb_replicas_to_text(struct printbuf *out,
888 struct bch_sb_field *f)
890 struct bch_sb_field_replicas *r = field_to_type(f, replicas);
891 struct bch_replicas_entry *e;
894 for_each_replicas_entry(r, e) {
899 bch2_replicas_entry_to_text(out, e);
903 const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
904 .validate = bch2_sb_validate_replicas,
905 .to_text = bch2_sb_replicas_to_text,
908 static const char *bch2_sb_validate_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f)
910 struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
911 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
912 struct bch_replicas_cpu cpu_r = { .entries = NULL };
913 struct bch_replicas_entry_v0 *e;
917 for_each_replicas_entry_v0(sb_r, e) {
918 err = "invalid replicas entry: invalid data type";
919 if (e->data_type >= BCH_DATA_NR)
922 err = "invalid replicas entry: no devices";
926 err = "invalid replicas entry: invalid device";
927 for (i = 0; i < e->nr_devs; i++)
928 if (!bch2_dev_exists(sb, mi, e->devs[i]))
932 err = "cannot allocate memory";
933 if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
936 err = check_dup_replicas_entries(&cpu_r);
938 kfree(cpu_r.entries);
942 const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
943 .validate = bch2_sb_validate_replicas_v0,
946 /* Query replicas: */
948 struct replicas_status __bch2_replicas_status(struct bch_fs *c,
949 struct bch_devs_mask online_devs)
951 struct bch_sb_field_members *mi;
952 struct bch_replicas_entry *e;
953 unsigned i, nr_online, nr_offline;
954 struct replicas_status ret;
956 memset(&ret, 0, sizeof(ret));
958 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
959 ret.replicas[i].redundancy = INT_MAX;
961 mi = bch2_sb_get_members(c->disk_sb.sb);
963 percpu_down_read(&c->mark_lock);
965 for_each_cpu_replicas_entry(&c->replicas, e) {
966 if (e->data_type >= ARRAY_SIZE(ret.replicas))
967 panic("e %p data_type %u\n", e, e->data_type);
969 nr_online = nr_offline = 0;
971 for (i = 0; i < e->nr_devs; i++) {
972 BUG_ON(!bch2_dev_exists(c->disk_sb.sb, mi,
975 if (test_bit(e->devs[i], online_devs.d))
981 ret.replicas[e->data_type].redundancy =
982 min(ret.replicas[e->data_type].redundancy,
983 (int) nr_online - (int) e->nr_required);
985 ret.replicas[e->data_type].nr_offline =
986 max(ret.replicas[e->data_type].nr_offline,
990 percpu_up_read(&c->mark_lock);
992 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
993 if (ret.replicas[i].redundancy == INT_MAX)
994 ret.replicas[i].redundancy = 0;
999 struct replicas_status bch2_replicas_status(struct bch_fs *c)
1001 return __bch2_replicas_status(c, bch2_online_devs(c));
1004 static bool have_enough_devs(struct replicas_status s,
1005 enum bch_data_type type,
1006 bool force_if_degraded,
1009 return (!s.replicas[type].nr_offline || force_if_degraded) &&
1010 (s.replicas[type].redundancy >= 0 || force_if_lost);
1013 bool bch2_have_enough_devs(struct replicas_status s, unsigned flags)
1015 return (have_enough_devs(s, BCH_DATA_journal,
1016 flags & BCH_FORCE_IF_METADATA_DEGRADED,
1017 flags & BCH_FORCE_IF_METADATA_LOST) &&
1018 have_enough_devs(s, BCH_DATA_btree,
1019 flags & BCH_FORCE_IF_METADATA_DEGRADED,
1020 flags & BCH_FORCE_IF_METADATA_LOST) &&
1021 have_enough_devs(s, BCH_DATA_user,
1022 flags & BCH_FORCE_IF_DATA_DEGRADED,
1023 flags & BCH_FORCE_IF_DATA_LOST));
1026 int bch2_replicas_online(struct bch_fs *c, bool meta)
1028 struct replicas_status s = bch2_replicas_status(c);
1031 ? min(s.replicas[BCH_DATA_journal].redundancy,
1032 s.replicas[BCH_DATA_btree].redundancy)
1033 : s.replicas[BCH_DATA_user].redundancy) + 1;
1036 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
1038 struct bch_replicas_entry *e;
1039 unsigned i, ret = 0;
1041 percpu_down_read(&c->mark_lock);
1043 for_each_cpu_replicas_entry(&c->replicas, e)
1044 for (i = 0; i < e->nr_devs; i++)
1045 if (e->devs[i] == ca->dev_idx)
1046 ret |= 1 << e->data_type;
1048 percpu_up_read(&c->mark_lock);
1053 int bch2_fs_replicas_init(struct bch_fs *c)
1055 c->journal.entry_u64s_reserved +=
1056 reserve_journal_replicas(c, &c->replicas);
1058 return replicas_table_update(c, &c->replicas);