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_parity;
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[JOURNAL_BUF_NR];
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 i, bytes = sizeof(struct bch_fs_usage) +
283 sizeof(u64) * new_r->nr;
286 memset(new_usage, 0, sizeof(new_usage));
288 for (i = 0; i < ARRAY_SIZE(new_usage); i++)
289 if (!(new_usage[i] = __alloc_percpu_gfp(bytes,
290 sizeof(u64), GFP_NOIO)))
293 if (!(new_base = kzalloc(bytes, GFP_NOIO)) ||
294 !(new_scratch = kmalloc(bytes, GFP_NOIO)) ||
296 !(new_gc = __alloc_percpu_gfp(bytes, sizeof(u64), GFP_NOIO))))
299 for (i = 0; i < ARRAY_SIZE(new_usage); i++)
301 __replicas_table_update_pcpu(new_usage[i], new_r,
302 c->usage[i], &c->replicas);
304 __replicas_table_update(new_base, new_r,
305 c->usage_base, &c->replicas);
307 __replicas_table_update_pcpu(new_gc, new_r,
308 c->usage_gc, &c->replicas);
310 for (i = 0; i < ARRAY_SIZE(new_usage); i++)
311 swap(c->usage[i], new_usage[i]);
312 swap(c->usage_base, new_base);
313 swap(c->usage_scratch, new_scratch);
314 swap(c->usage_gc, new_gc);
315 swap(c->replicas, *new_r);
319 free_percpu(new_usage[1]);
320 free_percpu(new_usage[0]);
324 bch_err(c, "error updating replicas table: memory allocation failure");
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 verify_replicas_entry(new_entry);
364 memset(&new_r, 0, sizeof(new_r));
365 memset(&new_gc, 0, sizeof(new_gc));
367 mutex_lock(&c->sb_lock);
369 if (c->replicas_gc.entries &&
370 !__replicas_has_entry(&c->replicas_gc, new_entry)) {
371 new_gc = cpu_replicas_add_entry(&c->replicas_gc, new_entry);
376 if (!__replicas_has_entry(&c->replicas, new_entry)) {
377 new_r = cpu_replicas_add_entry(&c->replicas, new_entry);
381 ret = bch2_cpu_replicas_to_sb_replicas(c, &new_r);
385 bch2_journal_entry_res_resize(&c->journal,
386 &c->replicas_journal_res,
387 reserve_journal_replicas(c, &new_r));
390 if (!new_r.entries &&
394 /* allocations done, now commit: */
399 /* don't update in memory replicas until changes are persistent */
400 percpu_down_write(&c->mark_lock);
402 ret = replicas_table_update(c, &new_r);
404 swap(new_gc, c->replicas_gc);
405 percpu_up_write(&c->mark_lock);
407 mutex_unlock(&c->sb_lock);
409 kfree(new_r.entries);
410 kfree(new_gc.entries);
414 bch_err(c, "error adding replicas entry: memory allocation failure");
419 static int __bch2_mark_replicas(struct bch_fs *c,
420 struct bch_replicas_entry *r,
423 return likely(bch2_replicas_marked(c, r)) ? 0
425 : bch2_mark_replicas_slowpath(c, r);
428 int bch2_mark_replicas(struct bch_fs *c, struct bch_replicas_entry *r)
430 return __bch2_mark_replicas(c, r, false);
433 static int __bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k,
436 struct bch_replicas_padded search;
437 struct bch_devs_list cached = bch2_bkey_cached_devs(k);
441 for (i = 0; i < cached.nr; i++) {
442 bch2_replicas_entry_cached(&search.e, cached.devs[i]);
444 ret = __bch2_mark_replicas(c, &search.e, check);
449 bch2_bkey_to_replicas(&search.e, k);
451 ret = __bch2_mark_replicas(c, &search.e, check);
455 if (search.e.data_type == BCH_DATA_parity) {
456 search.e.data_type = BCH_DATA_cached;
457 ret = __bch2_mark_replicas(c, &search.e, check);
461 search.e.data_type = BCH_DATA_user;
462 ret = __bch2_mark_replicas(c, &search.e, check);
470 bool bch2_bkey_replicas_marked(struct bch_fs *c,
473 return __bch2_mark_bkey_replicas(c, k, true) == 0;
476 int bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
478 return __bch2_mark_bkey_replicas(c, k, false);
481 int bch2_replicas_gc_end(struct bch_fs *c, int ret)
485 lockdep_assert_held(&c->replicas_gc_lock);
487 mutex_lock(&c->sb_lock);
488 percpu_down_write(&c->mark_lock);
491 * this is kind of crappy; the replicas gc mechanism needs to be ripped
495 for (i = 0; i < c->replicas.nr; i++) {
496 struct bch_replicas_entry *e =
497 cpu_replicas_entry(&c->replicas, i);
498 struct bch_replicas_cpu n;
500 if (!__replicas_has_entry(&c->replicas_gc, e) &&
501 bch2_fs_usage_read_one(c, &c->usage_base->replicas[i])) {
502 n = cpu_replicas_add_entry(&c->replicas_gc, e);
508 swap(n, c->replicas_gc);
513 if (bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc)) {
518 ret = replicas_table_update(c, &c->replicas_gc);
520 kfree(c->replicas_gc.entries);
521 c->replicas_gc.entries = NULL;
523 percpu_up_write(&c->mark_lock);
528 mutex_unlock(&c->sb_lock);
533 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
535 struct bch_replicas_entry *e;
538 lockdep_assert_held(&c->replicas_gc_lock);
540 mutex_lock(&c->sb_lock);
541 BUG_ON(c->replicas_gc.entries);
543 c->replicas_gc.nr = 0;
544 c->replicas_gc.entry_size = 0;
546 for_each_cpu_replicas_entry(&c->replicas, e)
547 if (!((1 << e->data_type) & typemask)) {
549 c->replicas_gc.entry_size =
550 max_t(unsigned, c->replicas_gc.entry_size,
551 replicas_entry_bytes(e));
554 c->replicas_gc.entries = kcalloc(c->replicas_gc.nr,
555 c->replicas_gc.entry_size,
557 if (!c->replicas_gc.entries) {
558 mutex_unlock(&c->sb_lock);
559 bch_err(c, "error allocating c->replicas_gc");
563 for_each_cpu_replicas_entry(&c->replicas, e)
564 if (!((1 << e->data_type) & typemask))
565 memcpy(cpu_replicas_entry(&c->replicas_gc, i++),
566 e, c->replicas_gc.entry_size);
568 bch2_cpu_replicas_sort(&c->replicas_gc);
569 mutex_unlock(&c->sb_lock);
574 int bch2_replicas_gc2(struct bch_fs *c)
576 struct bch_replicas_cpu new = { 0 };
580 bch2_journal_meta(&c->journal);
582 nr = READ_ONCE(c->replicas.nr);
583 new.entry_size = READ_ONCE(c->replicas.entry_size);
584 new.entries = kcalloc(nr, new.entry_size, GFP_KERNEL);
586 bch_err(c, "error allocating c->replicas_gc");
590 mutex_lock(&c->sb_lock);
591 percpu_down_write(&c->mark_lock);
593 if (nr != c->replicas.nr ||
594 new.entry_size != c->replicas.entry_size) {
595 percpu_up_write(&c->mark_lock);
596 mutex_unlock(&c->sb_lock);
601 for (i = 0; i < c->replicas.nr; i++) {
602 struct bch_replicas_entry *e =
603 cpu_replicas_entry(&c->replicas, i);
605 if (e->data_type == BCH_DATA_journal ||
606 bch2_fs_usage_read_one(c, &c->usage_base->replicas[i]))
607 memcpy(cpu_replicas_entry(&new, new.nr++),
611 bch2_cpu_replicas_sort(&new);
613 if (bch2_cpu_replicas_to_sb_replicas(c, &new)) {
618 ret = replicas_table_update(c, &new);
622 percpu_up_write(&c->mark_lock);
627 mutex_unlock(&c->sb_lock);
632 int bch2_replicas_set_usage(struct bch_fs *c,
633 struct bch_replicas_entry *r,
636 int ret, idx = bch2_replicas_entry_idx(c, r);
639 struct bch_replicas_cpu n;
641 n = cpu_replicas_add_entry(&c->replicas, r);
645 ret = replicas_table_update(c, &n);
651 idx = bch2_replicas_entry_idx(c, r);
655 c->usage_base->replicas[idx] = sectors;
660 /* Replicas tracking - superblock: */
663 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
664 struct bch_replicas_cpu *cpu_r)
666 struct bch_replicas_entry *e, *dst;
667 unsigned nr = 0, entry_size = 0, idx = 0;
669 for_each_replicas_entry(sb_r, e) {
670 entry_size = max_t(unsigned, entry_size,
671 replicas_entry_bytes(e));
675 cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
680 cpu_r->entry_size = entry_size;
682 for_each_replicas_entry(sb_r, e) {
683 dst = cpu_replicas_entry(cpu_r, idx++);
684 memcpy(dst, e, replicas_entry_bytes(e));
685 replicas_entry_sort(dst);
692 __bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
693 struct bch_replicas_cpu *cpu_r)
695 struct bch_replicas_entry_v0 *e;
696 unsigned nr = 0, entry_size = 0, idx = 0;
698 for_each_replicas_entry(sb_r, e) {
699 entry_size = max_t(unsigned, entry_size,
700 replicas_entry_bytes(e));
704 entry_size += sizeof(struct bch_replicas_entry) -
705 sizeof(struct bch_replicas_entry_v0);
707 cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
712 cpu_r->entry_size = entry_size;
714 for_each_replicas_entry(sb_r, e) {
715 struct bch_replicas_entry *dst =
716 cpu_replicas_entry(cpu_r, idx++);
718 dst->data_type = e->data_type;
719 dst->nr_devs = e->nr_devs;
720 dst->nr_required = 1;
721 memcpy(dst->devs, e->devs, e->nr_devs);
722 replicas_entry_sort(dst);
728 int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
730 struct bch_sb_field_replicas *sb_v1;
731 struct bch_sb_field_replicas_v0 *sb_v0;
732 struct bch_replicas_cpu new_r = { 0, 0, NULL };
735 if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
736 ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
737 else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
738 ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
743 bch2_cpu_replicas_sort(&new_r);
745 percpu_down_write(&c->mark_lock);
747 ret = replicas_table_update(c, &new_r);
748 percpu_up_write(&c->mark_lock);
750 kfree(new_r.entries);
755 static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
756 struct bch_replicas_cpu *r)
758 struct bch_sb_field_replicas_v0 *sb_r;
759 struct bch_replicas_entry_v0 *dst;
760 struct bch_replicas_entry *src;
763 bytes = sizeof(struct bch_sb_field_replicas);
765 for_each_cpu_replicas_entry(r, src)
766 bytes += replicas_entry_bytes(src) - 1;
768 sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
769 DIV_ROUND_UP(bytes, sizeof(u64)));
773 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
774 sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
776 memset(&sb_r->entries, 0,
777 vstruct_end(&sb_r->field) -
778 (void *) &sb_r->entries);
781 for_each_cpu_replicas_entry(r, src) {
782 dst->data_type = src->data_type;
783 dst->nr_devs = src->nr_devs;
784 memcpy(dst->devs, src->devs, src->nr_devs);
786 dst = replicas_entry_next(dst);
788 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
794 static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
795 struct bch_replicas_cpu *r)
797 struct bch_sb_field_replicas *sb_r;
798 struct bch_replicas_entry *dst, *src;
799 bool need_v1 = false;
802 bytes = sizeof(struct bch_sb_field_replicas);
804 for_each_cpu_replicas_entry(r, src) {
805 bytes += replicas_entry_bytes(src);
806 if (src->nr_required != 1)
811 return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
813 sb_r = bch2_sb_resize_replicas(&c->disk_sb,
814 DIV_ROUND_UP(bytes, sizeof(u64)));
818 bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
819 sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
821 memset(&sb_r->entries, 0,
822 vstruct_end(&sb_r->field) -
823 (void *) &sb_r->entries);
826 for_each_cpu_replicas_entry(r, src) {
827 memcpy(dst, src, replicas_entry_bytes(src));
829 dst = replicas_entry_next(dst);
831 BUG_ON((void *) dst > vstruct_end(&sb_r->field));
837 static const char *check_dup_replicas_entries(struct bch_replicas_cpu *cpu_r)
841 sort_cmp_size(cpu_r->entries,
846 for (i = 0; i + 1 < cpu_r->nr; i++) {
847 struct bch_replicas_entry *l =
848 cpu_replicas_entry(cpu_r, i);
849 struct bch_replicas_entry *r =
850 cpu_replicas_entry(cpu_r, i + 1);
852 BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0);
854 if (!memcmp(l, r, cpu_r->entry_size))
855 return "duplicate replicas entry";
861 static const char *bch2_sb_validate_replicas(struct bch_sb *sb, struct bch_sb_field *f)
863 struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
864 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
865 struct bch_replicas_cpu cpu_r = { .entries = NULL };
866 struct bch_replicas_entry *e;
870 for_each_replicas_entry(sb_r, e) {
871 err = "invalid replicas entry: invalid data type";
872 if (e->data_type >= BCH_DATA_NR)
875 err = "invalid replicas entry: no devices";
879 err = "invalid replicas entry: bad nr_required";
880 if (e->nr_required > 1 &&
881 e->nr_required >= e->nr_devs)
884 err = "invalid replicas entry: invalid device";
885 for (i = 0; i < e->nr_devs; i++)
886 if (!bch2_dev_exists(sb, mi, e->devs[i]))
890 err = "cannot allocate memory";
891 if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
894 err = check_dup_replicas_entries(&cpu_r);
896 kfree(cpu_r.entries);
900 static void bch2_sb_replicas_to_text(struct printbuf *out,
902 struct bch_sb_field *f)
904 struct bch_sb_field_replicas *r = field_to_type(f, replicas);
905 struct bch_replicas_entry *e;
908 for_each_replicas_entry(r, e) {
913 bch2_replicas_entry_to_text(out, e);
917 const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
918 .validate = bch2_sb_validate_replicas,
919 .to_text = bch2_sb_replicas_to_text,
922 static const char *bch2_sb_validate_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f)
924 struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
925 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
926 struct bch_replicas_cpu cpu_r = { .entries = NULL };
927 struct bch_replicas_entry_v0 *e;
931 for_each_replicas_entry_v0(sb_r, e) {
932 err = "invalid replicas entry: invalid data type";
933 if (e->data_type >= BCH_DATA_NR)
936 err = "invalid replicas entry: no devices";
940 err = "invalid replicas entry: invalid device";
941 for (i = 0; i < e->nr_devs; i++)
942 if (!bch2_dev_exists(sb, mi, e->devs[i]))
946 err = "cannot allocate memory";
947 if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
950 err = check_dup_replicas_entries(&cpu_r);
952 kfree(cpu_r.entries);
956 const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
957 .validate = bch2_sb_validate_replicas_v0,
960 /* Query replicas: */
962 struct replicas_status __bch2_replicas_status(struct bch_fs *c,
963 struct bch_devs_mask online_devs)
965 struct bch_sb_field_members *mi;
966 struct bch_replicas_entry *e;
967 unsigned i, nr_online, nr_offline;
968 struct replicas_status ret;
970 memset(&ret, 0, sizeof(ret));
972 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
973 ret.replicas[i].redundancy = INT_MAX;
975 mi = bch2_sb_get_members(c->disk_sb.sb);
977 percpu_down_read(&c->mark_lock);
979 for_each_cpu_replicas_entry(&c->replicas, e) {
980 if (e->data_type >= ARRAY_SIZE(ret.replicas))
981 panic("e %p data_type %u\n", e, e->data_type);
983 nr_online = nr_offline = 0;
985 for (i = 0; i < e->nr_devs; i++) {
986 BUG_ON(!bch2_dev_exists(c->disk_sb.sb, mi,
989 if (test_bit(e->devs[i], online_devs.d))
995 ret.replicas[e->data_type].redundancy =
996 min(ret.replicas[e->data_type].redundancy,
997 (int) nr_online - (int) e->nr_required);
999 ret.replicas[e->data_type].nr_offline =
1000 max(ret.replicas[e->data_type].nr_offline,
1004 percpu_up_read(&c->mark_lock);
1006 for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
1007 if (ret.replicas[i].redundancy == INT_MAX)
1008 ret.replicas[i].redundancy = 0;
1013 struct replicas_status bch2_replicas_status(struct bch_fs *c)
1015 return __bch2_replicas_status(c, bch2_online_devs(c));
1018 static bool have_enough_devs(struct replicas_status s,
1019 enum bch_data_type type,
1020 bool force_if_degraded,
1023 return (!s.replicas[type].nr_offline || force_if_degraded) &&
1024 (s.replicas[type].redundancy >= 0 || force_if_lost);
1027 bool bch2_have_enough_devs(struct replicas_status s, unsigned flags)
1029 return (have_enough_devs(s, BCH_DATA_journal,
1030 flags & BCH_FORCE_IF_METADATA_DEGRADED,
1031 flags & BCH_FORCE_IF_METADATA_LOST) &&
1032 have_enough_devs(s, BCH_DATA_btree,
1033 flags & BCH_FORCE_IF_METADATA_DEGRADED,
1034 flags & BCH_FORCE_IF_METADATA_LOST) &&
1035 have_enough_devs(s, BCH_DATA_user,
1036 flags & BCH_FORCE_IF_DATA_DEGRADED,
1037 flags & BCH_FORCE_IF_DATA_LOST));
1040 int bch2_replicas_online(struct bch_fs *c, bool meta)
1042 struct replicas_status s = bch2_replicas_status(c);
1045 ? min(s.replicas[BCH_DATA_journal].redundancy,
1046 s.replicas[BCH_DATA_btree].redundancy)
1047 : s.replicas[BCH_DATA_user].redundancy) + 1;
1050 unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
1052 struct bch_replicas_entry *e;
1053 unsigned i, ret = 0;
1055 percpu_down_read(&c->mark_lock);
1057 for_each_cpu_replicas_entry(&c->replicas, e)
1058 for (i = 0; i < e->nr_devs; i++)
1059 if (e->devs[i] == ca->dev_idx)
1060 ret |= 1 << e->data_type;
1062 percpu_up_read(&c->mark_lock);
1067 int bch2_fs_replicas_init(struct bch_fs *c)
1069 c->journal.entry_u64s_reserved +=
1070 reserve_journal_replicas(c, &c->replicas);
1072 return replicas_table_update(c, &c->replicas);