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 void verify_replicas_entry(struct bch_replicas_entry *e)
16 #ifdef CONFIG_BCACHEFS_DEBUG
19 BUG_ON(e->data_type >= BCH_DATA_NR);
21 BUG_ON(e->nr_required > 1 &&
22 e->nr_required >= e->nr_devs);
24 for (i = 0; i + 1 < e->nr_devs; i++)
25 BUG_ON(e->devs[i] >= e->devs[i + 1]);
29 void bch2_replicas_entry_sort(struct bch_replicas_entry *e)
31 bubble_sort(e->devs, e->nr_devs, u8_cmp);
34 static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
36 eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL);
39 void bch2_replicas_entry_to_text(struct printbuf *out,
40 struct bch_replicas_entry *e)
44 if (e->data_type < BCH_DATA_NR)
45 pr_buf(out, "%s", bch2_data_types[e->data_type]);
47 pr_buf(out, "(invalid data type %u)", e->data_type);
49 pr_buf(out, ": %u/%u [", e->nr_required, e->nr_devs);
50 for (i = 0; i < e->nr_devs; i++)
51 pr_buf(out, i ? " %u" : "%u", e->devs[i]);
55 void bch2_cpu_replicas_to_text(struct printbuf *out,
56 struct bch_replicas_cpu *r)
58 struct bch_replicas_entry *e;
61 for_each_cpu_replicas_entry(r, e) {
66 bch2_replicas_entry_to_text(out, e);
70 static void extent_to_replicas(struct bkey_s_c k,
71 struct bch_replicas_entry *r)
73 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
74 const union bch_extent_entry *entry;
75 struct extent_ptr_decoded p;
79 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
84 r->devs[r->nr_devs++] = p.ptr.dev;
90 static void stripe_to_replicas(struct bkey_s_c k,
91 struct bch_replicas_entry *r)
93 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
94 const struct bch_extent_ptr *ptr;
96 r->nr_required = s.v->nr_blocks - s.v->nr_redundant;
99 ptr < s.v->ptrs + s.v->nr_blocks;
101 r->devs[r->nr_devs++] = ptr->dev;
104 void bch2_bkey_to_replicas(struct bch_replicas_entry *e,
110 case KEY_TYPE_btree_ptr:
111 case KEY_TYPE_btree_ptr_v2:
112 e->data_type = BCH_DATA_btree;
113 extent_to_replicas(k, e);
115 case KEY_TYPE_extent:
116 case KEY_TYPE_reflink_v:
117 e->data_type = BCH_DATA_user;
118 extent_to_replicas(k, e);
120 case KEY_TYPE_stripe:
121 e->data_type = BCH_DATA_parity;
122 stripe_to_replicas(k, e);
126 bch2_replicas_entry_sort(e);
129 void bch2_devlist_to_replicas(struct bch_replicas_entry *e,
130 enum bch_data_type data_type,
131 struct bch_devs_list devs)
136 data_type == BCH_DATA_sb ||
137 data_type >= BCH_DATA_NR);
139 e->data_type = data_type;
143 for (i = 0; i < devs.nr; i++)
144 e->devs[e->nr_devs++] = devs.devs[i];
146 bch2_replicas_entry_sort(e);
149 static struct bch_replicas_cpu
150 cpu_replicas_add_entry(struct bch_replicas_cpu *old,
151 struct bch_replicas_entry *new_entry)
154 struct bch_replicas_cpu new = {
156 .entry_size = max_t(unsigned, old->entry_size,
157 replicas_entry_bytes(new_entry)),
160 BUG_ON(!new_entry->data_type);
161 verify_replicas_entry(new_entry);
163 new.entries = kcalloc(new.nr, new.entry_size, GFP_KERNEL);
167 for (i = 0; i < old->nr; i++)
168 memcpy(cpu_replicas_entry(&new, i),
169 cpu_replicas_entry(old, i),
172 memcpy(cpu_replicas_entry(&new, old->nr),
174 replicas_entry_bytes(new_entry));
176 bch2_cpu_replicas_sort(&new);
180 static inline int __replicas_entry_idx(struct bch_replicas_cpu *r,
181 struct bch_replicas_entry *search)
183 int idx, entry_size = replicas_entry_bytes(search);
185 if (unlikely(entry_size > r->entry_size))
188 verify_replicas_entry(search);
190 #define entry_cmp(_l, _r, size) memcmp(_l, _r, entry_size)
191 idx = eytzinger0_find(r->entries, r->nr, r->entry_size,
195 return idx < r->nr ? idx : -1;
198 int bch2_replicas_entry_idx(struct bch_fs *c,
199 struct bch_replicas_entry *search)
201 bch2_replicas_entry_sort(search);
203 return __replicas_entry_idx(&c->replicas, search);
206 static bool __replicas_has_entry(struct bch_replicas_cpu *r,
207 struct bch_replicas_entry *search)
209 return __replicas_entry_idx(r, search) >= 0;
212 bool bch2_replicas_marked(struct bch_fs *c,
213 struct bch_replicas_entry *search)
217 if (!search->nr_devs)
220 verify_replicas_entry(search);
222 percpu_down_read(&c->mark_lock);
223 marked = __replicas_has_entry(&c->replicas, search) &&
224 (likely((!c->replicas_gc.entries)) ||
225 __replicas_has_entry(&c->replicas_gc, search));
226 percpu_up_read(&c->mark_lock);
231 static void __replicas_table_update(struct bch_fs_usage *dst,
232 struct bch_replicas_cpu *dst_r,
233 struct bch_fs_usage *src,
234 struct bch_replicas_cpu *src_r)
236 int src_idx, dst_idx;
240 for (src_idx = 0; src_idx < src_r->nr; src_idx++) {
241 if (!src->replicas[src_idx])
244 dst_idx = __replicas_entry_idx(dst_r,
245 cpu_replicas_entry(src_r, src_idx));
248 dst->replicas[dst_idx] = src->replicas[src_idx];
252 static void __replicas_table_update_pcpu(struct bch_fs_usage __percpu *dst_p,
253 struct bch_replicas_cpu *dst_r,
254 struct bch_fs_usage __percpu *src_p,
255 struct bch_replicas_cpu *src_r)
257 unsigned src_nr = sizeof(struct bch_fs_usage) / sizeof(u64) + src_r->nr;
258 struct bch_fs_usage *dst, *src = (void *)
259 bch2_acc_percpu_u64s((void *) src_p, src_nr);
262 dst = this_cpu_ptr(dst_p);
265 __replicas_table_update(dst, dst_r, src, src_r);
269 * Resize filesystem accounting:
271 static int replicas_table_update(struct bch_fs *c,
272 struct bch_replicas_cpu *new_r)
274 struct bch_fs_usage __percpu *new_usage[JOURNAL_BUF_NR];
275 struct bch_fs_usage_online *new_scratch = NULL;
276 struct bch_fs_usage __percpu *new_gc = NULL;
277 struct bch_fs_usage *new_base = NULL;
278 unsigned i, bytes = sizeof(struct bch_fs_usage) +
279 sizeof(u64) * new_r->nr;
280 unsigned scratch_bytes = sizeof(struct bch_fs_usage_online) +
281 sizeof(u64) * new_r->nr;
284 memset(new_usage, 0, sizeof(new_usage));
286 for (i = 0; i < ARRAY_SIZE(new_usage); i++)
287 if (!(new_usage[i] = __alloc_percpu_gfp(bytes,
288 sizeof(u64), GFP_KERNEL)))
291 if (!(new_base = kzalloc(bytes, GFP_KERNEL)) ||
292 !(new_scratch = kmalloc(scratch_bytes, GFP_KERNEL)) ||
294 !(new_gc = __alloc_percpu_gfp(bytes, sizeof(u64), GFP_KERNEL))))
297 for (i = 0; i < ARRAY_SIZE(new_usage); i++)
299 __replicas_table_update_pcpu(new_usage[i], new_r,
300 c->usage[i], &c->replicas);
302 __replicas_table_update(new_base, new_r,
303 c->usage_base, &c->replicas);
305 __replicas_table_update_pcpu(new_gc, new_r,
306 c->usage_gc, &c->replicas);
308 for (i = 0; i < ARRAY_SIZE(new_usage); i++)
309 swap(c->usage[i], new_usage[i]);
310 swap(c->usage_base, new_base);
311 swap(c->usage_scratch, new_scratch);
312 swap(c->usage_gc, new_gc);
313 swap(c->replicas, *new_r);
317 for (i = 0; i < ARRAY_SIZE(new_usage); i++)
318 free_percpu(new_usage[i]);
322 bch_err(c, "error updating replicas table: memory allocation failure");
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 int bch2_mark_replicas(struct bch_fs *c, struct bch_replicas_entry *r)
419 return likely(bch2_replicas_marked(c, r))
420 ? 0 : bch2_mark_replicas_slowpath(c, r);
423 /* replicas delta list: */
425 int bch2_replicas_delta_list_mark(struct bch_fs *c,
426 struct replicas_delta_list *r)
428 struct replicas_delta *d = r->d;
429 struct replicas_delta *top = (void *) r->d + r->used;
432 for (d = r->d; !ret && d != top; d = replicas_delta_next(d))
433 ret = bch2_mark_replicas(c, &d->r);
438 * Old replicas_gc mechanism: only used for journal replicas entries now, should
442 int bch2_replicas_gc_end(struct bch_fs *c, int ret)
446 lockdep_assert_held(&c->replicas_gc_lock);
448 mutex_lock(&c->sb_lock);
449 percpu_down_write(&c->mark_lock);
452 * this is kind of crappy; the replicas gc mechanism needs to be ripped
456 for (i = 0; i < c->replicas.nr; i++) {
457 struct bch_replicas_entry *e =
458 cpu_replicas_entry(&c->replicas, i);
459 struct bch_replicas_cpu n;
461 if (!__replicas_has_entry(&c->replicas_gc, e) &&
462 bch2_fs_usage_read_one(c, &c->usage_base->replicas[i])) {
463 n = cpu_replicas_add_entry(&c->replicas_gc, e);
469 swap(n, c->replicas_gc);
474 if (bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc)) {
479 ret = replicas_table_update(c, &c->replicas_gc);
481 kfree(c->replicas_gc.entries);
482 c->replicas_gc.entries = NULL;
484 percpu_up_write(&c->mark_lock);
489 mutex_unlock(&c->sb_lock);
494 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
496 struct bch_replicas_entry *e;
499 lockdep_assert_held(&c->replicas_gc_lock);
501 mutex_lock(&c->sb_lock);
502 BUG_ON(c->replicas_gc.entries);
504 c->replicas_gc.nr = 0;
505 c->replicas_gc.entry_size = 0;
507 for_each_cpu_replicas_entry(&c->replicas, e)
508 if (!((1 << e->data_type) & typemask)) {
510 c->replicas_gc.entry_size =
511 max_t(unsigned, c->replicas_gc.entry_size,
512 replicas_entry_bytes(e));
515 c->replicas_gc.entries = kcalloc(c->replicas_gc.nr,
516 c->replicas_gc.entry_size,
518 if (!c->replicas_gc.entries) {
519 mutex_unlock(&c->sb_lock);
520 bch_err(c, "error allocating c->replicas_gc");
524 for_each_cpu_replicas_entry(&c->replicas, e)
525 if (!((1 << e->data_type) & typemask))
526 memcpy(cpu_replicas_entry(&c->replicas_gc, i++),
527 e, c->replicas_gc.entry_size);
529 bch2_cpu_replicas_sort(&c->replicas_gc);
530 mutex_unlock(&c->sb_lock);
535 /* New much simpler mechanism for clearing out unneeded replicas entries: */
537 int bch2_replicas_gc2(struct bch_fs *c)
539 struct bch_replicas_cpu new = { 0 };
543 bch2_journal_meta(&c->journal);
545 nr = READ_ONCE(c->replicas.nr);
546 new.entry_size = READ_ONCE(c->replicas.entry_size);
547 new.entries = kcalloc(nr, new.entry_size, GFP_KERNEL);
549 bch_err(c, "error allocating c->replicas_gc");
553 mutex_lock(&c->sb_lock);
554 percpu_down_write(&c->mark_lock);
556 if (nr != c->replicas.nr ||
557 new.entry_size != c->replicas.entry_size) {
558 percpu_up_write(&c->mark_lock);
559 mutex_unlock(&c->sb_lock);
564 for (i = 0; i < c->replicas.nr; i++) {
565 struct bch_replicas_entry *e =
566 cpu_replicas_entry(&c->replicas, i);
568 if (e->data_type == BCH_DATA_journal ||
569 c->usage_base->replicas[i] ||
570 percpu_u64_get(&c->usage[0]->replicas[i]) ||
571 percpu_u64_get(&c->usage[1]->replicas[i]) ||
572 percpu_u64_get(&c->usage[2]->replicas[i]) ||
573 percpu_u64_get(&c->usage[3]->replicas[i]))
574 memcpy(cpu_replicas_entry(&new, new.nr++),
578 bch2_cpu_replicas_sort(&new);
580 if (bch2_cpu_replicas_to_sb_replicas(c, &new)) {
585 ret = replicas_table_update(c, &new);
589 percpu_up_write(&c->mark_lock);
594 mutex_unlock(&c->sb_lock);
599 int bch2_replicas_set_usage(struct bch_fs *c,
600 struct bch_replicas_entry *r,
603 int ret, idx = bch2_replicas_entry_idx(c, r);
606 struct bch_replicas_cpu n;
608 n = cpu_replicas_add_entry(&c->replicas, r);
612 ret = replicas_table_update(c, &n);
618 idx = bch2_replicas_entry_idx(c, r);
622 c->usage_base->replicas[idx] = sectors;
627 /* Replicas tracking - superblock: */
630 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
631 struct bch_replicas_cpu *cpu_r)
633 struct bch_replicas_entry *e, *dst;
634 unsigned nr = 0, entry_size = 0, idx = 0;
636 for_each_replicas_entry(sb_r, e) {
637 entry_size = max_t(unsigned, entry_size,
638 replicas_entry_bytes(e));
642 cpu_r->entries = kcalloc(nr, entry_size, GFP_KERNEL);
647 cpu_r->entry_size = entry_size;
649 for_each_replicas_entry(sb_r, e) {
650 dst = cpu_replicas_entry(cpu_r, idx++);
651 memcpy(dst, e, replicas_entry_bytes(e));
652 bch2_replicas_entry_sort(dst);
659 __bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
660 struct bch_replicas_cpu *cpu_r)
662 struct bch_replicas_entry_v0 *e;
663 unsigned nr = 0, entry_size = 0, idx = 0;
665 for_each_replicas_entry(sb_r, e) {
666 entry_size = max_t(unsigned, entry_size,
667 replicas_entry_bytes(e));
671 entry_size += sizeof(struct bch_replicas_entry) -
672 sizeof(struct bch_replicas_entry_v0);
674 cpu_r->entries = kcalloc(nr, entry_size, GFP_KERNEL);
679 cpu_r->entry_size = entry_size;
681 for_each_replicas_entry(sb_r, e) {
682 struct bch_replicas_entry *dst =
683 cpu_replicas_entry(cpu_r, idx++);
685 dst->data_type = e->data_type;
686 dst->nr_devs = e->nr_devs;
687 dst->nr_required = 1;
688 memcpy(dst->devs, e->devs, e->nr_devs);
689 bch2_replicas_entry_sort(dst);
695 int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
697 struct bch_sb_field_replicas *sb_v1;
698 struct bch_sb_field_replicas_v0 *sb_v0;
699 struct bch_replicas_cpu new_r = { 0, 0, NULL };
702 if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
703 ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
704 else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
705 ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
710 bch2_cpu_replicas_sort(&new_r);
712 percpu_down_write(&c->mark_lock);
714 ret = replicas_table_update(c, &new_r);
715 percpu_up_write(&c->mark_lock);
717 kfree(new_r.entries);
722 static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
723 struct bch_replicas_cpu *r)
725 struct bch_sb_field_replicas_v0 *sb_r;
726 struct bch_replicas_entry_v0 *dst;
727 struct bch_replicas_entry *src;
730 bytes = sizeof(struct bch_sb_field_replicas);
732 for_each_cpu_replicas_entry(r, src)
733 bytes += replicas_entry_bytes(src) - 1;
735 sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
736 DIV_ROUND_UP(bytes, sizeof(u64)));
740 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
741 sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
743 memset(&sb_r->entries, 0,
744 vstruct_end(&sb_r->field) -
745 (void *) &sb_r->entries);
748 for_each_cpu_replicas_entry(r, src) {
749 dst->data_type = src->data_type;
750 dst->nr_devs = src->nr_devs;
751 memcpy(dst->devs, src->devs, src->nr_devs);
753 dst = replicas_entry_next(dst);
755 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
761 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
762 struct bch_replicas_cpu *r)
764 struct bch_sb_field_replicas *sb_r;
765 struct bch_replicas_entry *dst, *src;
766 bool need_v1 = false;
769 bytes = sizeof(struct bch_sb_field_replicas);
771 for_each_cpu_replicas_entry(r, src) {
772 bytes += replicas_entry_bytes(src);
773 if (src->nr_required != 1)
778 return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
780 sb_r = bch2_sb_resize_replicas(&c->disk_sb,
781 DIV_ROUND_UP(bytes, sizeof(u64)));
785 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
786 sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
788 memset(&sb_r->entries, 0,
789 vstruct_end(&sb_r->field) -
790 (void *) &sb_r->entries);
793 for_each_cpu_replicas_entry(r, src) {
794 memcpy(dst, src, replicas_entry_bytes(src));
796 dst = replicas_entry_next(dst);
798 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
804 static int bch2_cpu_replicas_validate(struct bch_replicas_cpu *cpu_r,
806 struct printbuf *err)
808 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
811 sort_cmp_size(cpu_r->entries,
816 for (i = 0; i < cpu_r->nr; i++) {
817 struct bch_replicas_entry *e =
818 cpu_replicas_entry(cpu_r, i);
820 if (e->data_type >= BCH_DATA_NR) {
821 pr_buf(err, "invalid data type in entry ");
822 bch2_replicas_entry_to_text(err, e);
827 pr_buf(err, "no devices in entry ");
828 bch2_replicas_entry_to_text(err, e);
832 if (e->nr_required > 1 &&
833 e->nr_required >= e->nr_devs) {
834 pr_buf(err, "bad nr_required in entry ");
835 bch2_replicas_entry_to_text(err, e);
839 for (j = 0; j < e->nr_devs; j++)
840 if (!bch2_dev_exists(sb, mi, e->devs[j])) {
841 pr_buf(err, "invalid device %u in entry ", e->devs[j]);
842 bch2_replicas_entry_to_text(err, e);
846 if (i + 1 < cpu_r->nr) {
847 struct bch_replicas_entry *n =
848 cpu_replicas_entry(cpu_r, i + 1);
850 BUG_ON(memcmp(e, n, cpu_r->entry_size) > 0);
852 if (!memcmp(e, n, cpu_r->entry_size)) {
853 pr_buf(err, "duplicate replicas entry ");
854 bch2_replicas_entry_to_text(err, e);
863 static int bch2_sb_validate_replicas(struct bch_sb *sb, struct bch_sb_field *f,
864 struct printbuf *err)
866 struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
867 struct bch_replicas_cpu cpu_r;
870 if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
873 ret = bch2_cpu_replicas_validate(&cpu_r, sb, err);
874 kfree(cpu_r.entries);
878 static void bch2_sb_replicas_to_text(struct printbuf *out,
880 struct bch_sb_field *f)
882 struct bch_sb_field_replicas *r = field_to_type(f, replicas);
883 struct bch_replicas_entry *e;
886 for_each_replicas_entry(r, e) {
891 bch2_replicas_entry_to_text(out, e);
895 const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
896 .validate = bch2_sb_validate_replicas,
897 .to_text = bch2_sb_replicas_to_text,
900 static int bch2_sb_validate_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f,
901 struct printbuf *err)
903 struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
904 struct bch_replicas_cpu cpu_r;
907 if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
910 ret = bch2_cpu_replicas_validate(&cpu_r, sb, err);
911 kfree(cpu_r.entries);
915 const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
916 .validate = bch2_sb_validate_replicas_v0,
919 /* Query replicas: */
921 bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs,
922 unsigned flags, bool print)
924 struct bch_replicas_entry *e;
927 percpu_down_read(&c->mark_lock);
928 for_each_cpu_replicas_entry(&c->replicas, e) {
929 unsigned i, nr_online = 0, nr_failed = 0, dflags = 0;
930 bool metadata = e->data_type < BCH_DATA_user;
932 if (e->data_type == BCH_DATA_cached)
935 for (i = 0; i < e->nr_devs; i++) {
936 struct bch_dev *ca = bch_dev_bkey_exists(c, e->devs[i]);
938 nr_online += test_bit(e->devs[i], devs.d);
939 nr_failed += ca->mi.state == BCH_MEMBER_STATE_failed;
942 if (nr_failed == e->nr_devs)
945 if (nr_online < e->nr_required)
947 ? BCH_FORCE_IF_METADATA_LOST
948 : BCH_FORCE_IF_DATA_LOST;
950 if (nr_online < e->nr_devs)
952 ? BCH_FORCE_IF_METADATA_DEGRADED
953 : BCH_FORCE_IF_DATA_DEGRADED;
955 if (dflags & ~flags) {
959 bch2_replicas_entry_to_text(&PBUF(buf), e);
960 bch_err(c, "insufficient devices online (%u) for replicas entry %s",
968 percpu_up_read(&c->mark_lock);
973 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
975 struct bch_replicas_entry *e;
978 percpu_down_read(&c->mark_lock);
980 for_each_cpu_replicas_entry(&c->replicas, e)
981 for (i = 0; i < e->nr_devs; i++)
982 if (e->devs[i] == ca->dev_idx)
983 ret |= 1 << e->data_type;
985 percpu_up_read(&c->mark_lock);
990 void bch2_fs_replicas_exit(struct bch_fs *c)
994 kfree(c->usage_scratch);
995 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
996 free_percpu(c->usage[i]);
997 kfree(c->usage_base);
998 kfree(c->replicas.entries);
999 kfree(c->replicas_gc.entries);
1001 mempool_exit(&c->replicas_delta_pool);
1004 int bch2_fs_replicas_init(struct bch_fs *c)
1006 bch2_journal_entry_res_resize(&c->journal,
1007 &c->replicas_journal_res,
1008 reserve_journal_replicas(c, &c->replicas));
1010 return mempool_init_kmalloc_pool(&c->replicas_delta_pool, 1,
1011 REPLICAS_DELTA_LIST_MAX) ?:
1012 replicas_table_update(c, &c->replicas);