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_sorted(struct bch_replicas_entry *e)
21 #ifdef CONFIG_BCACHES_DEBUG
24 for (i = 0; i + 1 < e->nr_devs; i++)
25 BUG_ON(e->devs[i] >= e->devs[i + 1]);
29 static void 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 pr_buf(out, "%s: %u/%u [",
45 bch2_data_types[e->data_type],
49 for (i = 0; i < e->nr_devs; i++)
50 pr_buf(out, i ? " %u" : "%u", e->devs[i]);
54 void bch2_cpu_replicas_to_text(struct printbuf *out,
55 struct bch_replicas_cpu *r)
57 struct bch_replicas_entry *e;
60 for_each_cpu_replicas_entry(r, e) {
65 bch2_replicas_entry_to_text(out, e);
69 static void extent_to_replicas(struct bkey_s_c k,
70 struct bch_replicas_entry *r)
72 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
73 const union bch_extent_entry *entry;
74 struct extent_ptr_decoded p;
78 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
87 r->devs[r->nr_devs++] = p.ptr.dev;
91 static void stripe_to_replicas(struct bkey_s_c k,
92 struct bch_replicas_entry *r)
94 struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
95 const struct bch_extent_ptr *ptr;
97 r->nr_required = s.v->nr_blocks - s.v->nr_redundant;
100 ptr < s.v->ptrs + s.v->nr_blocks;
102 r->devs[r->nr_devs++] = ptr->dev;
105 void bch2_bkey_to_replicas(struct bch_replicas_entry *e,
111 case KEY_TYPE_btree_ptr:
112 e->data_type = BCH_DATA_BTREE;
113 extent_to_replicas(k, e);
115 case KEY_TYPE_extent:
116 e->data_type = BCH_DATA_USER;
117 extent_to_replicas(k, e);
119 case KEY_TYPE_stripe:
120 e->data_type = BCH_DATA_USER;
121 stripe_to_replicas(k, e);
125 replicas_entry_sort(e);
128 void bch2_devlist_to_replicas(struct bch_replicas_entry *e,
129 enum bch_data_type data_type,
130 struct bch_devs_list devs)
135 data_type == BCH_DATA_SB ||
136 data_type >= BCH_DATA_NR);
138 e->data_type = data_type;
142 for (i = 0; i < devs.nr; i++)
143 e->devs[e->nr_devs++] = devs.devs[i];
145 replicas_entry_sort(e);
148 static struct bch_replicas_cpu
149 cpu_replicas_add_entry(struct bch_replicas_cpu *old,
150 struct bch_replicas_entry *new_entry)
153 struct bch_replicas_cpu new = {
155 .entry_size = max_t(unsigned, old->entry_size,
156 replicas_entry_bytes(new_entry)),
159 BUG_ON(!new_entry->data_type);
160 verify_replicas_entry_sorted(new_entry);
162 new.entries = kcalloc(new.nr, new.entry_size, GFP_NOIO);
166 for (i = 0; i < old->nr; i++)
167 memcpy(cpu_replicas_entry(&new, i),
168 cpu_replicas_entry(old, i),
171 memcpy(cpu_replicas_entry(&new, old->nr),
173 replicas_entry_bytes(new_entry));
175 bch2_cpu_replicas_sort(&new);
179 static inline int __replicas_entry_idx(struct bch_replicas_cpu *r,
180 struct bch_replicas_entry *search)
182 int idx, entry_size = replicas_entry_bytes(search);
184 if (unlikely(entry_size > r->entry_size))
187 verify_replicas_entry_sorted(search);
189 #define entry_cmp(_l, _r, size) memcmp(_l, _r, entry_size)
190 idx = eytzinger0_find(r->entries, r->nr, r->entry_size,
194 return idx < r->nr ? idx : -1;
197 int bch2_replicas_entry_idx(struct bch_fs *c,
198 struct bch_replicas_entry *search)
200 replicas_entry_sort(search);
202 return __replicas_entry_idx(&c->replicas, search);
205 static bool __replicas_has_entry(struct bch_replicas_cpu *r,
206 struct bch_replicas_entry *search)
208 return __replicas_entry_idx(r, search) >= 0;
211 static bool bch2_replicas_marked_locked(struct bch_fs *c,
212 struct bch_replicas_entry *search,
213 bool check_gc_replicas)
215 if (!search->nr_devs)
218 verify_replicas_entry_sorted(search);
220 return __replicas_has_entry(&c->replicas, search) &&
221 (!check_gc_replicas ||
222 likely((!c->replicas_gc.entries)) ||
223 __replicas_has_entry(&c->replicas_gc, search));
226 bool bch2_replicas_marked(struct bch_fs *c,
227 struct bch_replicas_entry *search,
228 bool check_gc_replicas)
232 percpu_down_read(&c->mark_lock);
233 marked = bch2_replicas_marked_locked(c, search, check_gc_replicas);
234 percpu_up_read(&c->mark_lock);
239 static void __replicas_table_update(struct bch_fs_usage *dst,
240 struct bch_replicas_cpu *dst_r,
241 struct bch_fs_usage *src,
242 struct bch_replicas_cpu *src_r)
244 int src_idx, dst_idx;
248 for (src_idx = 0; src_idx < src_r->nr; src_idx++) {
249 if (!src->replicas[src_idx])
252 dst_idx = __replicas_entry_idx(dst_r,
253 cpu_replicas_entry(src_r, src_idx));
256 dst->replicas[dst_idx] = src->replicas[src_idx];
260 static void __replicas_table_update_pcpu(struct bch_fs_usage __percpu *dst_p,
261 struct bch_replicas_cpu *dst_r,
262 struct bch_fs_usage __percpu *src_p,
263 struct bch_replicas_cpu *src_r)
265 unsigned src_nr = sizeof(struct bch_fs_usage) / sizeof(u64) + src_r->nr;
266 struct bch_fs_usage *dst, *src = (void *)
267 bch2_acc_percpu_u64s((void *) src_p, src_nr);
270 dst = this_cpu_ptr(dst_p);
273 __replicas_table_update(dst, dst_r, src, src_r);
277 * Resize filesystem accounting:
279 static int replicas_table_update(struct bch_fs *c,
280 struct bch_replicas_cpu *new_r)
282 struct bch_fs_usage __percpu *new_usage[2] = { NULL, NULL };
283 struct bch_fs_usage *new_scratch = NULL;
284 struct bch_fs_usage __percpu *new_gc = NULL;
285 struct bch_fs_usage *new_base = NULL;
286 unsigned bytes = sizeof(struct bch_fs_usage) +
287 sizeof(u64) * new_r->nr;
290 if (!(new_base = kzalloc(bytes, GFP_NOIO)) ||
291 !(new_usage[0] = __alloc_percpu_gfp(bytes, sizeof(u64),
293 !(new_usage[1] = __alloc_percpu_gfp(bytes, sizeof(u64),
295 !(new_scratch = kmalloc(bytes, GFP_NOIO)) ||
297 !(new_gc = __alloc_percpu_gfp(bytes, sizeof(u64), GFP_NOIO))))
301 __replicas_table_update(new_base, new_r,
302 c->usage_base, &c->replicas);
304 __replicas_table_update_pcpu(new_usage[0], new_r,
305 c->usage[0], &c->replicas);
307 __replicas_table_update_pcpu(new_usage[1], new_r,
308 c->usage[1], &c->replicas);
310 __replicas_table_update_pcpu(new_gc, new_r,
311 c->usage_gc, &c->replicas);
313 swap(c->usage_base, new_base);
314 swap(c->usage[0], new_usage[0]);
315 swap(c->usage[1], new_usage[1]);
316 swap(c->usage_scratch, new_scratch);
317 swap(c->usage_gc, new_gc);
318 swap(c->replicas, *new_r);
323 free_percpu(new_usage[1]);
324 free_percpu(new_usage[0]);
329 static unsigned reserve_journal_replicas(struct bch_fs *c,
330 struct bch_replicas_cpu *r)
332 struct bch_replicas_entry *e;
333 unsigned journal_res_u64s = 0;
337 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
341 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
343 /* persistent_reserved: */
345 DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64)) *
348 for_each_cpu_replicas_entry(r, e)
350 DIV_ROUND_UP(sizeof(struct jset_entry_data_usage) +
351 e->nr_devs, sizeof(u64));
352 return journal_res_u64s;
356 static int bch2_mark_replicas_slowpath(struct bch_fs *c,
357 struct bch_replicas_entry *new_entry)
359 struct bch_replicas_cpu new_r, new_gc;
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);
407 mutex_unlock(&c->sb_lock);
409 kfree(new_r.entries);
410 kfree(new_gc.entries);
415 int bch2_mark_replicas(struct bch_fs *c,
416 struct bch_replicas_entry *r)
418 return likely(bch2_replicas_marked(c, r, true))
420 : bch2_mark_replicas_slowpath(c, r);
423 bool bch2_bkey_replicas_marked_locked(struct bch_fs *c,
425 bool check_gc_replicas)
427 struct bch_replicas_padded search;
428 struct bch_devs_list cached = bch2_bkey_cached_devs(k);
431 for (i = 0; i < cached.nr; i++) {
432 bch2_replicas_entry_cached(&search.e, cached.devs[i]);
434 if (!bch2_replicas_marked_locked(c, &search.e,
439 bch2_bkey_to_replicas(&search.e, k);
441 return bch2_replicas_marked_locked(c, &search.e, check_gc_replicas);
444 bool bch2_bkey_replicas_marked(struct bch_fs *c,
446 bool check_gc_replicas)
450 percpu_down_read(&c->mark_lock);
451 marked = bch2_bkey_replicas_marked_locked(c, k, check_gc_replicas);
452 percpu_up_read(&c->mark_lock);
457 int bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
459 struct bch_replicas_padded search;
460 struct bch_devs_list cached = bch2_bkey_cached_devs(k);
464 for (i = 0; i < cached.nr; i++) {
465 bch2_replicas_entry_cached(&search.e, cached.devs[i]);
467 ret = bch2_mark_replicas(c, &search.e);
472 bch2_bkey_to_replicas(&search.e, k);
474 return bch2_mark_replicas(c, &search.e);
477 int bch2_replicas_gc_end(struct bch_fs *c, int ret)
481 lockdep_assert_held(&c->replicas_gc_lock);
483 mutex_lock(&c->sb_lock);
484 percpu_down_write(&c->mark_lock);
487 * this is kind of crappy; the replicas gc mechanism needs to be ripped
491 for (i = 0; i < c->replicas.nr; i++) {
492 struct bch_replicas_entry *e =
493 cpu_replicas_entry(&c->replicas, i);
494 struct bch_replicas_cpu n;
496 if (!__replicas_has_entry(&c->replicas_gc, e) &&
497 (c->usage_base->replicas[i] ||
498 percpu_u64_get(&c->usage[0]->replicas[i]) ||
499 percpu_u64_get(&c->usage[1]->replicas[i]))) {
500 n = cpu_replicas_add_entry(&c->replicas_gc, e);
506 swap(n, c->replicas_gc);
511 if (bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc)) {
516 ret = replicas_table_update(c, &c->replicas_gc);
518 kfree(c->replicas_gc.entries);
519 c->replicas_gc.entries = NULL;
521 percpu_up_write(&c->mark_lock);
526 mutex_unlock(&c->sb_lock);
531 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
533 struct bch_replicas_entry *e;
536 lockdep_assert_held(&c->replicas_gc_lock);
538 mutex_lock(&c->sb_lock);
539 BUG_ON(c->replicas_gc.entries);
541 c->replicas_gc.nr = 0;
542 c->replicas_gc.entry_size = 0;
544 for_each_cpu_replicas_entry(&c->replicas, e)
545 if (!((1 << e->data_type) & typemask)) {
547 c->replicas_gc.entry_size =
548 max_t(unsigned, c->replicas_gc.entry_size,
549 replicas_entry_bytes(e));
552 c->replicas_gc.entries = kcalloc(c->replicas_gc.nr,
553 c->replicas_gc.entry_size,
555 if (!c->replicas_gc.entries) {
556 mutex_unlock(&c->sb_lock);
560 for_each_cpu_replicas_entry(&c->replicas, e)
561 if (!((1 << e->data_type) & typemask))
562 memcpy(cpu_replicas_entry(&c->replicas_gc, i++),
563 e, c->replicas_gc.entry_size);
565 bch2_cpu_replicas_sort(&c->replicas_gc);
566 mutex_unlock(&c->sb_lock);
571 int bch2_replicas_gc2(struct bch_fs *c)
573 struct bch_replicas_cpu new = { 0 };
577 bch2_journal_meta(&c->journal);
579 nr = READ_ONCE(c->replicas.nr);
580 new.entry_size = READ_ONCE(c->replicas.entry_size);
581 new.entries = kcalloc(nr, new.entry_size, GFP_KERNEL);
585 mutex_lock(&c->sb_lock);
586 percpu_down_write(&c->mark_lock);
588 if (nr != c->replicas.nr ||
589 new.entry_size != c->replicas.entry_size) {
590 percpu_up_write(&c->mark_lock);
591 mutex_unlock(&c->sb_lock);
596 for (i = 0; i < c->replicas.nr; i++) {
597 struct bch_replicas_entry *e =
598 cpu_replicas_entry(&c->replicas, i);
600 if (e->data_type == BCH_DATA_JOURNAL ||
601 c->usage_base->replicas[i] ||
602 percpu_u64_get(&c->usage[0]->replicas[i]) ||
603 percpu_u64_get(&c->usage[1]->replicas[i]))
604 memcpy(cpu_replicas_entry(&new, new.nr++),
608 bch2_cpu_replicas_sort(&new);
610 if (bch2_cpu_replicas_to_sb_replicas(c, &new)) {
615 ret = replicas_table_update(c, &new);
619 percpu_up_write(&c->mark_lock);
624 mutex_unlock(&c->sb_lock);
629 int bch2_replicas_set_usage(struct bch_fs *c,
630 struct bch_replicas_entry *r,
633 int ret, idx = bch2_replicas_entry_idx(c, r);
636 struct bch_replicas_cpu n;
638 n = cpu_replicas_add_entry(&c->replicas, r);
642 ret = replicas_table_update(c, &n);
648 idx = bch2_replicas_entry_idx(c, r);
652 c->usage_base->replicas[idx] = sectors;
657 /* Replicas tracking - superblock: */
660 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
661 struct bch_replicas_cpu *cpu_r)
663 struct bch_replicas_entry *e, *dst;
664 unsigned nr = 0, entry_size = 0, idx = 0;
666 for_each_replicas_entry(sb_r, e) {
667 entry_size = max_t(unsigned, entry_size,
668 replicas_entry_bytes(e));
672 cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
677 cpu_r->entry_size = entry_size;
679 for_each_replicas_entry(sb_r, e) {
680 dst = cpu_replicas_entry(cpu_r, idx++);
681 memcpy(dst, e, replicas_entry_bytes(e));
682 replicas_entry_sort(dst);
689 __bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
690 struct bch_replicas_cpu *cpu_r)
692 struct bch_replicas_entry_v0 *e;
693 unsigned nr = 0, entry_size = 0, idx = 0;
695 for_each_replicas_entry(sb_r, e) {
696 entry_size = max_t(unsigned, entry_size,
697 replicas_entry_bytes(e));
701 entry_size += sizeof(struct bch_replicas_entry) -
702 sizeof(struct bch_replicas_entry_v0);
704 cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
709 cpu_r->entry_size = entry_size;
711 for_each_replicas_entry(sb_r, e) {
712 struct bch_replicas_entry *dst =
713 cpu_replicas_entry(cpu_r, idx++);
715 dst->data_type = e->data_type;
716 dst->nr_devs = e->nr_devs;
717 dst->nr_required = 1;
718 memcpy(dst->devs, e->devs, e->nr_devs);
719 replicas_entry_sort(dst);
725 int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
727 struct bch_sb_field_replicas *sb_v1;
728 struct bch_sb_field_replicas_v0 *sb_v0;
729 struct bch_replicas_cpu new_r = { 0, 0, NULL };
732 if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
733 ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
734 else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
735 ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
740 bch2_cpu_replicas_sort(&new_r);
742 percpu_down_write(&c->mark_lock);
744 ret = replicas_table_update(c, &new_r);
745 percpu_up_write(&c->mark_lock);
747 kfree(new_r.entries);
752 static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
753 struct bch_replicas_cpu *r)
755 struct bch_sb_field_replicas_v0 *sb_r;
756 struct bch_replicas_entry_v0 *dst;
757 struct bch_replicas_entry *src;
760 bytes = sizeof(struct bch_sb_field_replicas);
762 for_each_cpu_replicas_entry(r, src)
763 bytes += replicas_entry_bytes(src) - 1;
765 sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
766 DIV_ROUND_UP(bytes, sizeof(u64)));
770 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
771 sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
773 memset(&sb_r->entries, 0,
774 vstruct_end(&sb_r->field) -
775 (void *) &sb_r->entries);
778 for_each_cpu_replicas_entry(r, src) {
779 dst->data_type = src->data_type;
780 dst->nr_devs = src->nr_devs;
781 memcpy(dst->devs, src->devs, src->nr_devs);
783 dst = replicas_entry_next(dst);
785 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
791 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
792 struct bch_replicas_cpu *r)
794 struct bch_sb_field_replicas *sb_r;
795 struct bch_replicas_entry *dst, *src;
796 bool need_v1 = false;
799 bytes = sizeof(struct bch_sb_field_replicas);
801 for_each_cpu_replicas_entry(r, src) {
802 bytes += replicas_entry_bytes(src);
803 if (src->nr_required != 1)
808 return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
810 sb_r = bch2_sb_resize_replicas(&c->disk_sb,
811 DIV_ROUND_UP(bytes, sizeof(u64)));
815 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
816 sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
818 memset(&sb_r->entries, 0,
819 vstruct_end(&sb_r->field) -
820 (void *) &sb_r->entries);
823 for_each_cpu_replicas_entry(r, src) {
824 memcpy(dst, src, replicas_entry_bytes(src));
826 dst = replicas_entry_next(dst);
828 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
834 static const char *check_dup_replicas_entries(struct bch_replicas_cpu *cpu_r)
838 sort_cmp_size(cpu_r->entries,
843 for (i = 0; i + 1 < cpu_r->nr; i++) {
844 struct bch_replicas_entry *l =
845 cpu_replicas_entry(cpu_r, i);
846 struct bch_replicas_entry *r =
847 cpu_replicas_entry(cpu_r, i + 1);
849 BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0);
851 if (!memcmp(l, r, cpu_r->entry_size))
852 return "duplicate replicas entry";
858 static const char *bch2_sb_validate_replicas(struct bch_sb *sb, struct bch_sb_field *f)
860 struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
861 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
862 struct bch_replicas_cpu cpu_r = { .entries = NULL };
863 struct bch_replicas_entry *e;
867 for_each_replicas_entry(sb_r, e) {
868 err = "invalid replicas entry: invalid data type";
869 if (e->data_type >= BCH_DATA_NR)
872 err = "invalid replicas entry: no devices";
876 err = "invalid replicas entry: bad nr_required";
877 if (!e->nr_required ||
878 (e->nr_required > 1 &&
879 e->nr_required >= e->nr_devs))
882 err = "invalid replicas entry: invalid device";
883 for (i = 0; i < e->nr_devs; i++)
884 if (!bch2_dev_exists(sb, mi, e->devs[i]))
888 err = "cannot allocate memory";
889 if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
892 err = check_dup_replicas_entries(&cpu_r);
894 kfree(cpu_r.entries);
898 static void bch2_sb_replicas_to_text(struct printbuf *out,
900 struct bch_sb_field *f)
902 struct bch_sb_field_replicas *r = field_to_type(f, replicas);
903 struct bch_replicas_entry *e;
906 for_each_replicas_entry(r, e) {
911 bch2_replicas_entry_to_text(out, e);
915 const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
916 .validate = bch2_sb_validate_replicas,
917 .to_text = bch2_sb_replicas_to_text,
920 static const char *bch2_sb_validate_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f)
922 struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
923 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
924 struct bch_replicas_cpu cpu_r = { .entries = NULL };
925 struct bch_replicas_entry_v0 *e;
929 for_each_replicas_entry_v0(sb_r, e) {
930 err = "invalid replicas entry: invalid data type";
931 if (e->data_type >= BCH_DATA_NR)
934 err = "invalid replicas entry: no devices";
938 err = "invalid replicas entry: invalid device";
939 for (i = 0; i < e->nr_devs; i++)
940 if (!bch2_dev_exists(sb, mi, e->devs[i]))
944 err = "cannot allocate memory";
945 if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
948 err = check_dup_replicas_entries(&cpu_r);
950 kfree(cpu_r.entries);
954 const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
955 .validate = bch2_sb_validate_replicas_v0,
958 /* Query replicas: */
960 struct replicas_status __bch2_replicas_status(struct bch_fs *c,
961 struct bch_devs_mask online_devs)
963 struct bch_sb_field_members *mi;
964 struct bch_replicas_entry *e;
965 unsigned i, nr_online, nr_offline;
966 struct replicas_status ret;
968 memset(&ret, 0, sizeof(ret));
970 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
971 ret.replicas[i].redundancy = INT_MAX;
973 mi = bch2_sb_get_members(c->disk_sb.sb);
975 percpu_down_read(&c->mark_lock);
977 for_each_cpu_replicas_entry(&c->replicas, e) {
978 if (e->data_type >= ARRAY_SIZE(ret.replicas))
979 panic("e %p data_type %u\n", e, e->data_type);
981 nr_online = nr_offline = 0;
983 for (i = 0; i < e->nr_devs; i++) {
984 BUG_ON(!bch2_dev_exists(c->disk_sb.sb, mi,
987 if (test_bit(e->devs[i], online_devs.d))
993 ret.replicas[e->data_type].redundancy =
994 min(ret.replicas[e->data_type].redundancy,
995 (int) nr_online - (int) e->nr_required);
997 ret.replicas[e->data_type].nr_offline =
998 max(ret.replicas[e->data_type].nr_offline,
1002 percpu_up_read(&c->mark_lock);
1004 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
1005 if (ret.replicas[i].redundancy == INT_MAX)
1006 ret.replicas[i].redundancy = 0;
1011 struct replicas_status bch2_replicas_status(struct bch_fs *c)
1013 return __bch2_replicas_status(c, bch2_online_devs(c));
1016 static bool have_enough_devs(struct replicas_status s,
1017 enum bch_data_type type,
1018 bool force_if_degraded,
1021 return (!s.replicas[type].nr_offline || force_if_degraded) &&
1022 (s.replicas[type].redundancy >= 0 || force_if_lost);
1025 bool bch2_have_enough_devs(struct replicas_status s, unsigned flags)
1027 return (have_enough_devs(s, BCH_DATA_JOURNAL,
1028 flags & BCH_FORCE_IF_METADATA_DEGRADED,
1029 flags & BCH_FORCE_IF_METADATA_LOST) &&
1030 have_enough_devs(s, BCH_DATA_BTREE,
1031 flags & BCH_FORCE_IF_METADATA_DEGRADED,
1032 flags & BCH_FORCE_IF_METADATA_LOST) &&
1033 have_enough_devs(s, BCH_DATA_USER,
1034 flags & BCH_FORCE_IF_DATA_DEGRADED,
1035 flags & BCH_FORCE_IF_DATA_LOST));
1038 int bch2_replicas_online(struct bch_fs *c, bool meta)
1040 struct replicas_status s = bch2_replicas_status(c);
1043 ? min(s.replicas[BCH_DATA_JOURNAL].redundancy,
1044 s.replicas[BCH_DATA_BTREE].redundancy)
1045 : s.replicas[BCH_DATA_USER].redundancy) + 1;
1048 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
1050 struct bch_replicas_entry *e;
1051 unsigned i, ret = 0;
1053 percpu_down_read(&c->mark_lock);
1055 for_each_cpu_replicas_entry(&c->replicas, e)
1056 for (i = 0; i < e->nr_devs; i++)
1057 if (e->devs[i] == ca->dev_idx)
1058 ret |= 1 << e->data_type;
1060 percpu_up_read(&c->mark_lock);
1065 int bch2_fs_replicas_init(struct bch_fs *c)
1067 c->journal.entry_u64s_reserved +=
1068 reserve_journal_replicas(c, &c->replicas);
1070 return replicas_table_update(c, &c->replicas);