7 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *,
8 struct bch_replicas_cpu *);
10 /* Replicas tracking - in memory: */
12 static inline int u8_cmp(u8 l, u8 r)
14 return (l > r) - (l < r);
17 static void verify_replicas_entry_sorted(struct bch_replicas_entry *e)
19 #ifdef CONFIG_BCACHES_DEBUG
22 for (i = 0; i + 1 < e->nr_devs; i++)
23 BUG_ON(e->devs[i] >= e->devs[i + 1]);
27 static void replicas_entry_sort(struct bch_replicas_entry *e)
29 bubble_sort(e->devs, e->nr_devs, u8_cmp);
32 static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
34 eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL);
37 void bch2_replicas_entry_to_text(struct printbuf *out,
38 struct bch_replicas_entry *e)
42 pr_buf(out, "%s: %u/%u [",
43 bch2_data_types[e->data_type],
47 for (i = 0; i < e->nr_devs; i++)
48 pr_buf(out, i ? " %u" : "%u", e->devs[i]);
52 void bch2_cpu_replicas_to_text(struct printbuf *out,
53 struct bch_replicas_cpu *r)
55 struct bch_replicas_entry *e;
58 for_each_cpu_replicas_entry(r, e) {
63 bch2_replicas_entry_to_text(out, e);
67 static void extent_to_replicas(struct bkey_s_c k,
68 struct bch_replicas_entry *r)
70 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
71 const union bch_extent_entry *entry;
72 struct extent_ptr_decoded p;
76 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
85 r->devs[r->nr_devs++] = p.ptr.dev;
89 static void stripe_to_replicas(struct bkey_s_c k,
90 struct bch_replicas_entry *r)
92 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
93 const struct bch_extent_ptr *ptr;
95 r->nr_required = s.v->nr_blocks - s.v->nr_redundant;
98 ptr < s.v->ptrs + s.v->nr_blocks;
100 r->devs[r->nr_devs++] = ptr->dev;
103 static void bkey_to_replicas(struct bch_replicas_entry *e,
109 case KEY_TYPE_btree_ptr:
110 e->data_type = BCH_DATA_BTREE;
111 extent_to_replicas(k, e);
113 case KEY_TYPE_extent:
114 e->data_type = BCH_DATA_USER;
115 extent_to_replicas(k, e);
117 case KEY_TYPE_stripe:
118 e->data_type = BCH_DATA_USER;
119 stripe_to_replicas(k, e);
123 replicas_entry_sort(e);
126 void bch2_devlist_to_replicas(struct bch_replicas_entry *e,
127 enum bch_data_type data_type,
128 struct bch_devs_list devs)
133 data_type == BCH_DATA_SB ||
134 data_type >= BCH_DATA_NR);
136 e->data_type = data_type;
140 for (i = 0; i < devs.nr; i++)
141 e->devs[e->nr_devs++] = devs.devs[i];
143 replicas_entry_sort(e);
146 static struct bch_replicas_cpu
147 cpu_replicas_add_entry(struct bch_replicas_cpu *old,
148 struct bch_replicas_entry *new_entry)
151 struct bch_replicas_cpu new = {
153 .entry_size = max_t(unsigned, old->entry_size,
154 replicas_entry_bytes(new_entry)),
157 BUG_ON(!new_entry->data_type);
158 verify_replicas_entry_sorted(new_entry);
160 new.entries = kcalloc(new.nr, new.entry_size, GFP_NOIO);
164 for (i = 0; i < old->nr; i++)
165 memcpy(cpu_replicas_entry(&new, i),
166 cpu_replicas_entry(old, i),
169 memcpy(cpu_replicas_entry(&new, old->nr),
171 replicas_entry_bytes(new_entry));
173 bch2_cpu_replicas_sort(&new);
177 static inline int __replicas_entry_idx(struct bch_replicas_cpu *r,
178 struct bch_replicas_entry *search)
180 int idx, entry_size = replicas_entry_bytes(search);
182 if (unlikely(entry_size > r->entry_size))
185 verify_replicas_entry_sorted(search);
187 #define entry_cmp(_l, _r, size) memcmp(_l, _r, entry_size)
188 idx = eytzinger0_find(r->entries, r->nr, r->entry_size,
192 return idx < r->nr ? idx : -1;
195 int bch2_replicas_entry_idx(struct bch_fs *c,
196 struct bch_replicas_entry *search)
198 replicas_entry_sort(search);
200 return __replicas_entry_idx(&c->replicas, search);
203 static bool __replicas_has_entry(struct bch_replicas_cpu *r,
204 struct bch_replicas_entry *search)
206 return __replicas_entry_idx(r, search) >= 0;
209 bool bch2_replicas_marked(struct bch_fs *c,
210 struct bch_replicas_entry *search,
211 bool check_gc_replicas)
215 if (!search->nr_devs)
218 verify_replicas_entry_sorted(search);
220 percpu_down_read_preempt_disable(&c->mark_lock);
221 marked = __replicas_has_entry(&c->replicas, search) &&
222 (!check_gc_replicas ||
223 likely((!c->replicas_gc.entries)) ||
224 __replicas_has_entry(&c->replicas_gc, search));
225 percpu_up_read_preempt_enable(&c->mark_lock);
230 static void __replicas_table_update(struct bch_fs_usage __percpu *dst_p,
231 struct bch_replicas_cpu *dst_r,
232 struct bch_fs_usage __percpu *src_p,
233 struct bch_replicas_cpu *src_r)
235 unsigned src_nr = sizeof(struct bch_fs_usage) / sizeof(u64) + src_r->nr;
236 struct bch_fs_usage *dst, *src = (void *)
237 bch2_acc_percpu_u64s((void *) src_p, src_nr);
238 int src_idx, dst_idx;
241 dst = this_cpu_ptr(dst_p);
246 for (src_idx = 0; src_idx < src_r->nr; src_idx++) {
247 if (!src->replicas[src_idx])
250 dst_idx = __replicas_entry_idx(dst_r,
251 cpu_replicas_entry(src_r, src_idx));
254 dst->replicas[dst_idx] = src->replicas[src_idx];
259 * Resize filesystem accounting:
261 static int replicas_table_update(struct bch_fs *c,
262 struct bch_replicas_cpu *new_r)
264 struct bch_fs_usage __percpu *new_usage[2] = { NULL, NULL };
265 struct bch_fs_usage __percpu *new_scratch = NULL;
266 unsigned bytes = sizeof(struct bch_fs_usage) +
267 sizeof(u64) * new_r->nr;
270 if (!(new_usage[0] = __alloc_percpu_gfp(bytes, sizeof(u64),
273 !(new_usage[1] = __alloc_percpu_gfp(bytes, sizeof(u64),
275 !(new_scratch = __alloc_percpu_gfp(bytes, sizeof(u64),
280 __replicas_table_update(new_usage[0], new_r,
281 c->usage[0], &c->replicas);
283 __replicas_table_update(new_usage[1], new_r,
284 c->usage[1], &c->replicas);
286 swap(c->usage[0], new_usage[0]);
287 swap(c->usage[1], new_usage[1]);
288 swap(c->usage_scratch, new_scratch);
289 swap(c->replicas, *new_r);
292 free_percpu(new_scratch);
293 free_percpu(new_usage[1]);
294 free_percpu(new_usage[0]);
298 static unsigned reserve_journal_replicas(struct bch_fs *c,
299 struct bch_replicas_cpu *r)
301 struct bch_replicas_entry *e;
302 unsigned journal_res_u64s = 0;
306 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
310 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
312 /* persistent_reserved: */
314 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64)) *
317 for_each_cpu_replicas_entry(r, e)
319 DIV_ROUND_UP(sizeof(struct jset_entry_data_usage) +
320 e->nr_devs, sizeof(u64));
321 return journal_res_u64s;
325 static int bch2_mark_replicas_slowpath(struct bch_fs *c,
326 struct bch_replicas_entry *new_entry)
328 struct bch_replicas_cpu new_r, new_gc;
331 memset(&new_r, 0, sizeof(new_r));
332 memset(&new_gc, 0, sizeof(new_gc));
334 mutex_lock(&c->sb_lock);
336 if (c->replicas_gc.entries &&
337 !__replicas_has_entry(&c->replicas_gc, new_entry)) {
338 new_gc = cpu_replicas_add_entry(&c->replicas_gc, new_entry);
343 if (!__replicas_has_entry(&c->replicas, new_entry)) {
344 new_r = cpu_replicas_add_entry(&c->replicas, new_entry);
348 ret = bch2_cpu_replicas_to_sb_replicas(c, &new_r);
352 bch2_journal_entry_res_resize(&c->journal,
353 &c->replicas_journal_res,
354 reserve_journal_replicas(c, &new_r));
357 if (!new_r.entries &&
361 /* allocations done, now commit: */
366 /* don't update in memory replicas until changes are persistent */
367 percpu_down_write(&c->mark_lock);
369 ret = replicas_table_update(c, &new_r);
371 swap(new_gc, c->replicas_gc);
372 percpu_up_write(&c->mark_lock);
376 mutex_unlock(&c->sb_lock);
378 kfree(new_r.entries);
379 kfree(new_gc.entries);
384 int bch2_mark_replicas(struct bch_fs *c,
385 struct bch_replicas_entry *r)
387 return likely(bch2_replicas_marked(c, r, true))
389 : bch2_mark_replicas_slowpath(c, r);
392 bool bch2_bkey_replicas_marked(struct bch_fs *c,
394 bool check_gc_replicas)
396 struct bch_replicas_padded search;
397 struct bch_devs_list cached = bch2_bkey_cached_devs(k);
400 for (i = 0; i < cached.nr; i++) {
401 bch2_replicas_entry_cached(&search.e, cached.devs[i]);
403 if (!bch2_replicas_marked(c, &search.e, check_gc_replicas))
407 bkey_to_replicas(&search.e, k);
409 return bch2_replicas_marked(c, &search.e, check_gc_replicas);
412 int bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
414 struct bch_replicas_padded search;
415 struct bch_devs_list cached = bch2_bkey_cached_devs(k);
419 for (i = 0; i < cached.nr; i++) {
420 bch2_replicas_entry_cached(&search.e, cached.devs[i]);
422 ret = bch2_mark_replicas(c, &search.e);
427 bkey_to_replicas(&search.e, k);
429 return bch2_mark_replicas(c, &search.e);
432 int bch2_replicas_gc_end(struct bch_fs *c, int ret)
436 lockdep_assert_held(&c->replicas_gc_lock);
438 mutex_lock(&c->sb_lock);
444 * this is kind of crappy; the replicas gc mechanism needs to be ripped
448 for (i = 0; i < c->replicas.nr; i++) {
449 struct bch_replicas_entry *e =
450 cpu_replicas_entry(&c->replicas, i);
451 struct bch_replicas_cpu n;
454 if (__replicas_has_entry(&c->replicas_gc, e))
457 v = percpu_u64_get(&c->usage[0]->replicas[i]);
461 n = cpu_replicas_add_entry(&c->replicas_gc, e);
467 percpu_down_write(&c->mark_lock);
468 swap(n, c->replicas_gc);
469 percpu_up_write(&c->mark_lock);
474 if (bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc)) {
481 /* don't update in memory replicas until changes are persistent */
483 percpu_down_write(&c->mark_lock);
485 ret = replicas_table_update(c, &c->replicas_gc);
487 kfree(c->replicas_gc.entries);
488 c->replicas_gc.entries = NULL;
489 percpu_up_write(&c->mark_lock);
491 mutex_unlock(&c->sb_lock);
495 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
497 struct bch_replicas_entry *e;
500 lockdep_assert_held(&c->replicas_gc_lock);
502 mutex_lock(&c->sb_lock);
503 BUG_ON(c->replicas_gc.entries);
505 c->replicas_gc.nr = 0;
506 c->replicas_gc.entry_size = 0;
508 for_each_cpu_replicas_entry(&c->replicas, e)
509 if (!((1 << e->data_type) & typemask)) {
511 c->replicas_gc.entry_size =
512 max_t(unsigned, c->replicas_gc.entry_size,
513 replicas_entry_bytes(e));
516 c->replicas_gc.entries = kcalloc(c->replicas_gc.nr,
517 c->replicas_gc.entry_size,
519 if (!c->replicas_gc.entries) {
520 mutex_unlock(&c->sb_lock);
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 int bch2_replicas_set_usage(struct bch_fs *c,
536 struct bch_replicas_entry *r,
539 int ret, idx = bch2_replicas_entry_idx(c, r);
542 struct bch_replicas_cpu n;
544 n = cpu_replicas_add_entry(&c->replicas, r);
548 ret = replicas_table_update(c, &n);
554 idx = bch2_replicas_entry_idx(c, r);
558 percpu_u64_set(&c->usage[0]->replicas[idx], sectors);
563 /* Replicas tracking - superblock: */
566 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
567 struct bch_replicas_cpu *cpu_r)
569 struct bch_replicas_entry *e, *dst;
570 unsigned nr = 0, entry_size = 0, idx = 0;
572 for_each_replicas_entry(sb_r, e) {
573 entry_size = max_t(unsigned, entry_size,
574 replicas_entry_bytes(e));
578 cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
583 cpu_r->entry_size = entry_size;
585 for_each_replicas_entry(sb_r, e) {
586 dst = cpu_replicas_entry(cpu_r, idx++);
587 memcpy(dst, e, replicas_entry_bytes(e));
588 replicas_entry_sort(dst);
595 __bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
596 struct bch_replicas_cpu *cpu_r)
598 struct bch_replicas_entry_v0 *e;
599 unsigned nr = 0, entry_size = 0, idx = 0;
601 for_each_replicas_entry(sb_r, e) {
602 entry_size = max_t(unsigned, entry_size,
603 replicas_entry_bytes(e));
607 entry_size += sizeof(struct bch_replicas_entry) -
608 sizeof(struct bch_replicas_entry_v0);
610 cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
615 cpu_r->entry_size = entry_size;
617 for_each_replicas_entry(sb_r, e) {
618 struct bch_replicas_entry *dst =
619 cpu_replicas_entry(cpu_r, idx++);
621 dst->data_type = e->data_type;
622 dst->nr_devs = e->nr_devs;
623 dst->nr_required = 1;
624 memcpy(dst->devs, e->devs, e->nr_devs);
625 replicas_entry_sort(dst);
631 int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
633 struct bch_sb_field_replicas *sb_v1;
634 struct bch_sb_field_replicas_v0 *sb_v0;
635 struct bch_replicas_cpu new_r = { 0, 0, NULL };
638 if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
639 ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
640 else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
641 ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
646 bch2_cpu_replicas_sort(&new_r);
648 percpu_down_write(&c->mark_lock);
650 ret = replicas_table_update(c, &new_r);
651 percpu_up_write(&c->mark_lock);
653 kfree(new_r.entries);
658 static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
659 struct bch_replicas_cpu *r)
661 struct bch_sb_field_replicas_v0 *sb_r;
662 struct bch_replicas_entry_v0 *dst;
663 struct bch_replicas_entry *src;
666 bytes = sizeof(struct bch_sb_field_replicas);
668 for_each_cpu_replicas_entry(r, src)
669 bytes += replicas_entry_bytes(src) - 1;
671 sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
672 DIV_ROUND_UP(bytes, sizeof(u64)));
676 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
677 sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
679 memset(&sb_r->entries, 0,
680 vstruct_end(&sb_r->field) -
681 (void *) &sb_r->entries);
684 for_each_cpu_replicas_entry(r, src) {
685 dst->data_type = src->data_type;
686 dst->nr_devs = src->nr_devs;
687 memcpy(dst->devs, src->devs, src->nr_devs);
689 dst = replicas_entry_next(dst);
691 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
697 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
698 struct bch_replicas_cpu *r)
700 struct bch_sb_field_replicas *sb_r;
701 struct bch_replicas_entry *dst, *src;
702 bool need_v1 = false;
705 bytes = sizeof(struct bch_sb_field_replicas);
707 for_each_cpu_replicas_entry(r, src) {
708 bytes += replicas_entry_bytes(src);
709 if (src->nr_required != 1)
714 return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
716 sb_r = bch2_sb_resize_replicas(&c->disk_sb,
717 DIV_ROUND_UP(bytes, sizeof(u64)));
721 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
722 sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
724 memset(&sb_r->entries, 0,
725 vstruct_end(&sb_r->field) -
726 (void *) &sb_r->entries);
729 for_each_cpu_replicas_entry(r, src) {
730 memcpy(dst, src, replicas_entry_bytes(src));
732 dst = replicas_entry_next(dst);
734 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
740 static const char *check_dup_replicas_entries(struct bch_replicas_cpu *cpu_r)
744 sort_cmp_size(cpu_r->entries,
749 for (i = 0; i + 1 < cpu_r->nr; i++) {
750 struct bch_replicas_entry *l =
751 cpu_replicas_entry(cpu_r, i);
752 struct bch_replicas_entry *r =
753 cpu_replicas_entry(cpu_r, i + 1);
755 BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0);
757 if (!memcmp(l, r, cpu_r->entry_size))
758 return "duplicate replicas entry";
764 static const char *bch2_sb_validate_replicas(struct bch_sb *sb, struct bch_sb_field *f)
766 struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
767 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
768 struct bch_replicas_cpu cpu_r = { .entries = NULL };
769 struct bch_replicas_entry *e;
773 for_each_replicas_entry(sb_r, e) {
774 err = "invalid replicas entry: invalid data type";
775 if (e->data_type >= BCH_DATA_NR)
778 err = "invalid replicas entry: no devices";
782 err = "invalid replicas entry: bad nr_required";
783 if (!e->nr_required ||
784 (e->nr_required > 1 &&
785 e->nr_required >= e->nr_devs))
788 err = "invalid replicas entry: invalid device";
789 for (i = 0; i < e->nr_devs; i++)
790 if (!bch2_dev_exists(sb, mi, e->devs[i]))
794 err = "cannot allocate memory";
795 if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
798 err = check_dup_replicas_entries(&cpu_r);
800 kfree(cpu_r.entries);
804 static void bch2_sb_replicas_to_text(struct printbuf *out,
806 struct bch_sb_field *f)
808 struct bch_sb_field_replicas *r = field_to_type(f, replicas);
809 struct bch_replicas_entry *e;
812 for_each_replicas_entry(r, e) {
817 bch2_replicas_entry_to_text(out, e);
821 const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
822 .validate = bch2_sb_validate_replicas,
823 .to_text = bch2_sb_replicas_to_text,
826 static const char *bch2_sb_validate_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f)
828 struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
829 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
830 struct bch_replicas_cpu cpu_r = { .entries = NULL };
831 struct bch_replicas_entry_v0 *e;
835 for_each_replicas_entry_v0(sb_r, e) {
836 err = "invalid replicas entry: invalid data type";
837 if (e->data_type >= BCH_DATA_NR)
840 err = "invalid replicas entry: no devices";
844 err = "invalid replicas entry: invalid device";
845 for (i = 0; i < e->nr_devs; i++)
846 if (!bch2_dev_exists(sb, mi, e->devs[i]))
850 err = "cannot allocate memory";
851 if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
854 err = check_dup_replicas_entries(&cpu_r);
856 kfree(cpu_r.entries);
860 const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
861 .validate = bch2_sb_validate_replicas_v0,
864 /* Query replicas: */
866 struct replicas_status __bch2_replicas_status(struct bch_fs *c,
867 struct bch_devs_mask online_devs)
869 struct bch_sb_field_members *mi;
870 struct bch_replicas_entry *e;
871 unsigned i, nr_online, nr_offline;
872 struct replicas_status ret;
874 memset(&ret, 0, sizeof(ret));
876 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
877 ret.replicas[i].redundancy = INT_MAX;
879 mi = bch2_sb_get_members(c->disk_sb.sb);
881 percpu_down_read_preempt_disable(&c->mark_lock);
883 for_each_cpu_replicas_entry(&c->replicas, e) {
884 if (e->data_type >= ARRAY_SIZE(ret.replicas))
885 panic("e %p data_type %u\n", e, e->data_type);
887 nr_online = nr_offline = 0;
889 for (i = 0; i < e->nr_devs; i++) {
890 BUG_ON(!bch2_dev_exists(c->disk_sb.sb, mi,
893 if (test_bit(e->devs[i], online_devs.d))
899 ret.replicas[e->data_type].redundancy =
900 min(ret.replicas[e->data_type].redundancy,
901 (int) nr_online - (int) e->nr_required);
903 ret.replicas[e->data_type].nr_offline =
904 max(ret.replicas[e->data_type].nr_offline,
908 percpu_up_read_preempt_enable(&c->mark_lock);
910 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
911 if (ret.replicas[i].redundancy == INT_MAX)
912 ret.replicas[i].redundancy = 0;
917 struct replicas_status bch2_replicas_status(struct bch_fs *c)
919 return __bch2_replicas_status(c, bch2_online_devs(c));
922 static bool have_enough_devs(struct replicas_status s,
923 enum bch_data_type type,
924 bool force_if_degraded,
927 return (!s.replicas[type].nr_offline || force_if_degraded) &&
928 (s.replicas[type].redundancy >= 0 || force_if_lost);
931 bool bch2_have_enough_devs(struct replicas_status s, unsigned flags)
933 return (have_enough_devs(s, BCH_DATA_JOURNAL,
934 flags & BCH_FORCE_IF_METADATA_DEGRADED,
935 flags & BCH_FORCE_IF_METADATA_LOST) &&
936 have_enough_devs(s, BCH_DATA_BTREE,
937 flags & BCH_FORCE_IF_METADATA_DEGRADED,
938 flags & BCH_FORCE_IF_METADATA_LOST) &&
939 have_enough_devs(s, BCH_DATA_USER,
940 flags & BCH_FORCE_IF_DATA_DEGRADED,
941 flags & BCH_FORCE_IF_DATA_LOST));
944 int bch2_replicas_online(struct bch_fs *c, bool meta)
946 struct replicas_status s = bch2_replicas_status(c);
949 ? min(s.replicas[BCH_DATA_JOURNAL].redundancy,
950 s.replicas[BCH_DATA_BTREE].redundancy)
951 : s.replicas[BCH_DATA_USER].redundancy) + 1;
954 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
956 struct bch_replicas_entry *e;
959 percpu_down_read_preempt_disable(&c->mark_lock);
961 for_each_cpu_replicas_entry(&c->replicas, e)
962 for (i = 0; i < e->nr_devs; i++)
963 if (e->devs[i] == ca->dev_idx)
964 ret |= 1 << e->data_type;
966 percpu_up_read_preempt_enable(&c->mark_lock);
971 int bch2_fs_replicas_init(struct bch_fs *c)
973 c->journal.entry_u64s_reserved +=
974 reserve_journal_replicas(c, &c->replicas);
976 return replicas_table_update(c, &c->replicas);