2017-03-17 06:18:50 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
2019-01-24 22:12:00 +00:00
|
|
|
#include "journal.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "replicas.h"
|
|
|
|
#include "super-io.h"
|
|
|
|
|
|
|
|
static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *,
|
|
|
|
struct bch_replicas_cpu *);
|
|
|
|
|
|
|
|
/* Replicas tracking - in memory: */
|
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
static inline int u8_cmp(u8 l, u8 r)
|
|
|
|
{
|
|
|
|
return (l > r) - (l < r);
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
static void verify_replicas_entry_sorted(struct bch_replicas_entry *e)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_BCACHES_DEBUG
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i + 1 < e->nr_devs; i++)
|
|
|
|
BUG_ON(e->devs[i] >= e->devs[i + 1]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
static void replicas_entry_sort(struct bch_replicas_entry *e)
|
|
|
|
{
|
|
|
|
bubble_sort(e->devs, e->nr_devs, u8_cmp);
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
|
|
|
|
{
|
|
|
|
eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL);
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
void bch2_replicas_entry_to_text(struct printbuf *out,
|
|
|
|
struct bch_replicas_entry *e)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2018-10-30 18:14:19 +00:00
|
|
|
unsigned i;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
pr_buf(out, "%s: %u/%u [",
|
|
|
|
bch2_data_types[e->data_type],
|
|
|
|
e->nr_required,
|
|
|
|
e->nr_devs);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
for (i = 0; i < e->nr_devs; i++)
|
2018-11-09 06:24:07 +00:00
|
|
|
pr_buf(out, i ? " %u" : "%u", e->devs[i]);
|
|
|
|
pr_buf(out, "]");
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 06:24:07 +00:00
|
|
|
void bch2_cpu_replicas_to_text(struct printbuf *out,
|
|
|
|
struct bch_replicas_cpu *r)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2018-10-30 18:14:19 +00:00
|
|
|
struct bch_replicas_entry *e;
|
2017-03-17 06:18:50 +00:00
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
for_each_cpu_replicas_entry(r, e) {
|
|
|
|
if (!first)
|
2018-11-09 06:24:07 +00:00
|
|
|
pr_buf(out, " ");
|
2017-03-17 06:18:50 +00:00
|
|
|
first = false;
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
bch2_replicas_entry_to_text(out, e);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:32:21 +00:00
|
|
|
static void extent_to_replicas(struct bkey_s_c k,
|
|
|
|
struct bch_replicas_entry *r)
|
|
|
|
{
|
2018-11-01 19:10:01 +00:00
|
|
|
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
|
|
|
|
const union bch_extent_entry *entry;
|
|
|
|
struct extent_ptr_decoded p;
|
2018-10-30 18:32:21 +00:00
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
r->nr_required = 1;
|
2018-10-30 18:32:47 +00:00
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
|
|
|
|
if (p.ptr.cached)
|
|
|
|
continue;
|
2018-11-01 19:13:19 +00:00
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
if (p.ec_nr) {
|
|
|
|
r->nr_devs = 0;
|
|
|
|
break;
|
2018-11-01 19:13:19 +00:00
|
|
|
}
|
2018-11-01 19:10:01 +00:00
|
|
|
|
|
|
|
r->devs[r->nr_devs++] = p.ptr.dev;
|
2018-11-01 19:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stripe_to_replicas(struct bkey_s_c k,
|
|
|
|
struct bch_replicas_entry *r)
|
|
|
|
{
|
2018-11-01 19:10:01 +00:00
|
|
|
struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
|
|
|
|
const struct bch_extent_ptr *ptr;
|
2018-11-01 19:13:19 +00:00
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
r->nr_required = s.v->nr_blocks - s.v->nr_redundant;
|
2018-11-01 19:13:19 +00:00
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
for (ptr = s.v->ptrs;
|
|
|
|
ptr < s.v->ptrs + s.v->nr_blocks;
|
|
|
|
ptr++)
|
|
|
|
r->devs[r->nr_devs++] = ptr->dev;
|
2018-10-30 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
static void bkey_to_replicas(struct bch_replicas_entry *e,
|
|
|
|
struct bkey_s_c k)
|
2018-10-30 18:32:21 +00:00
|
|
|
{
|
|
|
|
e->nr_devs = 0;
|
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
switch (k.k->type) {
|
|
|
|
case KEY_TYPE_btree_ptr:
|
2018-10-30 18:32:21 +00:00
|
|
|
e->data_type = BCH_DATA_BTREE;
|
|
|
|
extent_to_replicas(k, e);
|
|
|
|
break;
|
2018-11-01 19:10:01 +00:00
|
|
|
case KEY_TYPE_extent:
|
2018-10-30 18:32:21 +00:00
|
|
|
e->data_type = BCH_DATA_USER;
|
|
|
|
extent_to_replicas(k, e);
|
|
|
|
break;
|
2018-11-01 19:10:01 +00:00
|
|
|
case KEY_TYPE_stripe:
|
2018-11-01 19:13:19 +00:00
|
|
|
e->data_type = BCH_DATA_USER;
|
|
|
|
stripe_to_replicas(k, e);
|
|
|
|
break;
|
2018-10-30 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
replicas_entry_sort(e);
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
void bch2_devlist_to_replicas(struct bch_replicas_entry *e,
|
|
|
|
enum bch_data_type data_type,
|
|
|
|
struct bch_devs_list devs)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
BUG_ON(!data_type ||
|
|
|
|
data_type == BCH_DATA_SB ||
|
|
|
|
data_type >= BCH_DATA_NR);
|
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
e->data_type = data_type;
|
|
|
|
e->nr_devs = 0;
|
2018-10-30 18:32:47 +00:00
|
|
|
e->nr_required = 1;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
for (i = 0; i < devs.nr; i++)
|
|
|
|
e->devs[e->nr_devs++] = devs.devs[i];
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
replicas_entry_sort(e);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
static struct bch_replicas_cpu
|
2017-03-17 06:18:50 +00:00
|
|
|
cpu_replicas_add_entry(struct bch_replicas_cpu *old,
|
2018-10-30 18:14:19 +00:00
|
|
|
struct bch_replicas_entry *new_entry)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2018-12-01 15:32:48 +00:00
|
|
|
unsigned i;
|
|
|
|
struct bch_replicas_cpu new = {
|
|
|
|
.nr = old->nr + 1,
|
|
|
|
.entry_size = max_t(unsigned, old->entry_size,
|
|
|
|
replicas_entry_bytes(new_entry)),
|
|
|
|
};
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
BUG_ON(!new_entry->data_type);
|
|
|
|
verify_replicas_entry_sorted(new_entry);
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
new.entries = kcalloc(new.nr, new.entry_size, GFP_NOIO);
|
|
|
|
if (!new.entries)
|
|
|
|
return new;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
for (i = 0; i < old->nr; i++)
|
2018-12-01 15:32:48 +00:00
|
|
|
memcpy(cpu_replicas_entry(&new, i),
|
2017-03-17 06:18:50 +00:00
|
|
|
cpu_replicas_entry(old, i),
|
2018-10-30 18:14:19 +00:00
|
|
|
old->entry_size);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
memcpy(cpu_replicas_entry(&new, old->nr),
|
2018-10-30 18:14:19 +00:00
|
|
|
new_entry,
|
|
|
|
replicas_entry_bytes(new_entry));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
bch2_cpu_replicas_sort(&new);
|
2017-03-17 06:18:50 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
static inline int __replicas_entry_idx(struct bch_replicas_cpu *r,
|
|
|
|
struct bch_replicas_entry *search)
|
|
|
|
{
|
|
|
|
int idx, entry_size = replicas_entry_bytes(search);
|
|
|
|
|
|
|
|
if (unlikely(entry_size > r->entry_size))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
verify_replicas_entry_sorted(search);
|
|
|
|
|
|
|
|
#define entry_cmp(_l, _r, size) memcmp(_l, _r, entry_size)
|
|
|
|
idx = eytzinger0_find(r->entries, r->nr, r->entry_size,
|
|
|
|
entry_cmp, search);
|
|
|
|
#undef entry_cmp
|
|
|
|
|
|
|
|
return idx < r->nr ? idx : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_replicas_entry_idx(struct bch_fs *c,
|
|
|
|
struct bch_replicas_entry *search)
|
|
|
|
{
|
|
|
|
replicas_entry_sort(search);
|
|
|
|
|
|
|
|
return __replicas_entry_idx(&c->replicas, search);
|
|
|
|
}
|
|
|
|
|
2018-11-07 22:48:32 +00:00
|
|
|
static bool __replicas_has_entry(struct bch_replicas_cpu *r,
|
|
|
|
struct bch_replicas_entry *search)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2019-01-21 20:32:13 +00:00
|
|
|
return __replicas_entry_idx(r, search) >= 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:20:46 +00:00
|
|
|
static bool bch2_replicas_marked_locked(struct bch_fs *c,
|
2019-01-21 20:32:13 +00:00
|
|
|
struct bch_replicas_entry *search,
|
|
|
|
bool check_gc_replicas)
|
2018-11-07 22:48:32 +00:00
|
|
|
{
|
2019-01-21 20:32:13 +00:00
|
|
|
if (!search->nr_devs)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
verify_replicas_entry_sorted(search);
|
|
|
|
|
2019-03-15 22:20:46 +00:00
|
|
|
return __replicas_has_entry(&c->replicas, search) &&
|
2018-11-07 22:48:32 +00:00
|
|
|
(!check_gc_replicas ||
|
2018-12-01 15:32:48 +00:00
|
|
|
likely((!c->replicas_gc.entries)) ||
|
|
|
|
__replicas_has_entry(&c->replicas_gc, search));
|
2019-03-15 22:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bch2_replicas_marked(struct bch_fs *c,
|
|
|
|
struct bch_replicas_entry *search,
|
|
|
|
bool check_gc_replicas)
|
|
|
|
{
|
|
|
|
bool marked;
|
|
|
|
|
|
|
|
percpu_down_read(&c->mark_lock);
|
|
|
|
marked = bch2_replicas_marked_locked(c, search, check_gc_replicas);
|
2018-12-01 15:32:48 +00:00
|
|
|
percpu_up_read(&c->mark_lock);
|
2018-11-07 22:48:32 +00:00
|
|
|
|
|
|
|
return marked;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
static void __replicas_table_update(struct bch_fs_usage __percpu *dst_p,
|
|
|
|
struct bch_replicas_cpu *dst_r,
|
|
|
|
struct bch_fs_usage __percpu *src_p,
|
|
|
|
struct bch_replicas_cpu *src_r)
|
|
|
|
{
|
|
|
|
unsigned src_nr = sizeof(struct bch_fs_usage) / sizeof(u64) + src_r->nr;
|
|
|
|
struct bch_fs_usage *dst, *src = (void *)
|
|
|
|
bch2_acc_percpu_u64s((void *) src_p, src_nr);
|
|
|
|
int src_idx, dst_idx;
|
|
|
|
|
|
|
|
preempt_disable();
|
|
|
|
dst = this_cpu_ptr(dst_p);
|
|
|
|
preempt_enable();
|
|
|
|
|
|
|
|
*dst = *src;
|
|
|
|
|
|
|
|
for (src_idx = 0; src_idx < src_r->nr; src_idx++) {
|
2019-02-14 23:38:52 +00:00
|
|
|
if (!src->replicas[src_idx])
|
2019-01-21 20:32:13 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
dst_idx = __replicas_entry_idx(dst_r,
|
|
|
|
cpu_replicas_entry(src_r, src_idx));
|
|
|
|
BUG_ON(dst_idx < 0);
|
|
|
|
|
2019-02-14 23:38:52 +00:00
|
|
|
dst->replicas[dst_idx] = src->replicas[src_idx];
|
2019-01-21 20:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resize filesystem accounting:
|
|
|
|
*/
|
|
|
|
static int replicas_table_update(struct bch_fs *c,
|
|
|
|
struct bch_replicas_cpu *new_r)
|
|
|
|
{
|
2019-02-15 01:39:17 +00:00
|
|
|
struct bch_fs_usage __percpu *new_usage[2] = { NULL, NULL };
|
2019-03-15 22:20:46 +00:00
|
|
|
struct bch_fs_usage *new_scratch = NULL;
|
2019-01-21 20:32:13 +00:00
|
|
|
unsigned bytes = sizeof(struct bch_fs_usage) +
|
|
|
|
sizeof(u64) * new_r->nr;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
2019-02-15 01:39:17 +00:00
|
|
|
if (!(new_usage[0] = __alloc_percpu_gfp(bytes, sizeof(u64),
|
|
|
|
GFP_NOIO)) ||
|
|
|
|
(c->usage[1] &&
|
|
|
|
!(new_usage[1] = __alloc_percpu_gfp(bytes, sizeof(u64),
|
|
|
|
GFP_NOIO))) ||
|
2019-03-15 22:20:46 +00:00
|
|
|
!(new_scratch = kmalloc(bytes, GFP_NOIO)))
|
2019-02-15 01:39:17 +00:00
|
|
|
goto err;
|
2019-01-21 20:32:13 +00:00
|
|
|
|
2019-02-15 01:39:17 +00:00
|
|
|
if (c->usage[0])
|
|
|
|
__replicas_table_update(new_usage[0], new_r,
|
|
|
|
c->usage[0], &c->replicas);
|
|
|
|
if (c->usage[1])
|
|
|
|
__replicas_table_update(new_usage[1], new_r,
|
|
|
|
c->usage[1], &c->replicas);
|
|
|
|
|
|
|
|
swap(c->usage[0], new_usage[0]);
|
|
|
|
swap(c->usage[1], new_usage[1]);
|
|
|
|
swap(c->usage_scratch, new_scratch);
|
|
|
|
swap(c->replicas, *new_r);
|
2019-01-21 20:32:13 +00:00
|
|
|
ret = 0;
|
|
|
|
err:
|
2019-03-15 22:20:46 +00:00
|
|
|
kfree(new_scratch);
|
2019-02-15 01:39:17 +00:00
|
|
|
free_percpu(new_usage[1]);
|
|
|
|
free_percpu(new_usage[0]);
|
2019-01-21 20:32:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:12:00 +00:00
|
|
|
static unsigned reserve_journal_replicas(struct bch_fs *c,
|
|
|
|
struct bch_replicas_cpu *r)
|
|
|
|
{
|
|
|
|
struct bch_replicas_entry *e;
|
|
|
|
unsigned journal_res_u64s = 0;
|
|
|
|
|
|
|
|
/* nr_inodes: */
|
|
|
|
journal_res_u64s +=
|
|
|
|
DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
|
|
|
|
|
|
|
|
/* key_version: */
|
|
|
|
journal_res_u64s +=
|
|
|
|
DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64));
|
|
|
|
|
2019-02-10 00:20:57 +00:00
|
|
|
/* persistent_reserved: */
|
|
|
|
journal_res_u64s +=
|
|
|
|
DIV_ROUND_UP(sizeof(struct jset_entry_usage), sizeof(u64)) *
|
|
|
|
BCH_REPLICAS_MAX;
|
|
|
|
|
2019-01-24 22:12:00 +00:00
|
|
|
for_each_cpu_replicas_entry(r, e)
|
|
|
|
journal_res_u64s +=
|
2019-02-10 00:20:57 +00:00
|
|
|
DIV_ROUND_UP(sizeof(struct jset_entry_data_usage) +
|
2019-01-24 22:12:00 +00:00
|
|
|
e->nr_devs, sizeof(u64));
|
|
|
|
return journal_res_u64s;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
noinline
|
|
|
|
static int bch2_mark_replicas_slowpath(struct bch_fs *c,
|
2018-10-30 18:14:19 +00:00
|
|
|
struct bch_replicas_entry *new_entry)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2018-12-01 15:32:48 +00:00
|
|
|
struct bch_replicas_cpu new_r, new_gc;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
memset(&new_r, 0, sizeof(new_r));
|
|
|
|
memset(&new_gc, 0, sizeof(new_gc));
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
mutex_lock(&c->sb_lock);
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
if (c->replicas_gc.entries &&
|
|
|
|
!__replicas_has_entry(&c->replicas_gc, new_entry)) {
|
|
|
|
new_gc = cpu_replicas_add_entry(&c->replicas_gc, new_entry);
|
|
|
|
if (!new_gc.entries)
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
if (!__replicas_has_entry(&c->replicas, new_entry)) {
|
|
|
|
new_r = cpu_replicas_add_entry(&c->replicas, new_entry);
|
|
|
|
if (!new_r.entries)
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err;
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
ret = bch2_cpu_replicas_to_sb_replicas(c, &new_r);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
2019-01-24 22:12:00 +00:00
|
|
|
|
|
|
|
bch2_journal_entry_res_resize(&c->journal,
|
|
|
|
&c->replicas_journal_res,
|
|
|
|
reserve_journal_replicas(c, &new_r));
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
if (!new_r.entries &&
|
|
|
|
!new_gc.entries)
|
|
|
|
goto out;
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/* allocations done, now commit: */
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
if (new_r.entries)
|
2017-03-17 06:18:50 +00:00
|
|
|
bch2_write_super(c);
|
|
|
|
|
|
|
|
/* don't update in memory replicas until changes are persistent */
|
2018-12-01 15:32:48 +00:00
|
|
|
percpu_down_write(&c->mark_lock);
|
|
|
|
if (new_r.entries)
|
2019-01-21 20:32:13 +00:00
|
|
|
ret = replicas_table_update(c, &new_r);
|
2018-12-01 15:32:48 +00:00
|
|
|
if (new_gc.entries)
|
|
|
|
swap(new_gc, c->replicas_gc);
|
|
|
|
percpu_up_write(&c->mark_lock);
|
|
|
|
out:
|
|
|
|
ret = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
err:
|
|
|
|
mutex_unlock(&c->sb_lock);
|
2018-12-01 15:32:48 +00:00
|
|
|
|
|
|
|
kfree(new_r.entries);
|
|
|
|
kfree(new_gc.entries);
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
int bch2_mark_replicas(struct bch_fs *c,
|
|
|
|
struct bch_replicas_entry *r)
|
2018-10-30 18:32:21 +00:00
|
|
|
{
|
2019-01-21 20:32:13 +00:00
|
|
|
return likely(bch2_replicas_marked(c, r, true))
|
2018-11-07 22:48:32 +00:00
|
|
|
? 0
|
2019-01-21 20:32:13 +00:00
|
|
|
: bch2_mark_replicas_slowpath(c, r);
|
2018-10-30 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:20:46 +00:00
|
|
|
bool bch2_bkey_replicas_marked_locked(struct bch_fs *c,
|
|
|
|
struct bkey_s_c k,
|
|
|
|
bool check_gc_replicas)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2018-12-17 13:29:44 +00:00
|
|
|
struct bch_replicas_padded search;
|
2019-01-21 20:32:13 +00:00
|
|
|
struct bch_devs_list cached = bch2_bkey_cached_devs(k);
|
|
|
|
unsigned i;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
for (i = 0; i < cached.nr; i++) {
|
|
|
|
bch2_replicas_entry_cached(&search.e, cached.devs[i]);
|
2018-10-30 18:14:19 +00:00
|
|
|
|
2019-03-15 22:20:46 +00:00
|
|
|
if (!bch2_replicas_marked_locked(c, &search.e,
|
|
|
|
check_gc_replicas))
|
2019-01-21 20:32:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
bkey_to_replicas(&search.e, k);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-03-15 22:20:46 +00:00
|
|
|
return bch2_replicas_marked_locked(c, &search.e, check_gc_replicas);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bch2_bkey_replicas_marked(struct bch_fs *c,
|
|
|
|
struct bkey_s_c k,
|
|
|
|
bool check_gc_replicas)
|
|
|
|
{
|
|
|
|
bool marked;
|
|
|
|
|
|
|
|
percpu_down_read(&c->mark_lock);
|
|
|
|
marked = bch2_bkey_replicas_marked_locked(c, k, check_gc_replicas);
|
|
|
|
percpu_up_read(&c->mark_lock);
|
|
|
|
|
|
|
|
return marked;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
int bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2018-12-17 13:29:44 +00:00
|
|
|
struct bch_replicas_padded search;
|
2018-11-01 19:10:01 +00:00
|
|
|
struct bch_devs_list cached = bch2_bkey_cached_devs(k);
|
|
|
|
unsigned i;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
for (i = 0; i < cached.nr; i++) {
|
|
|
|
bch2_replicas_entry_cached(&search.e, cached.devs[i]);
|
2018-11-09 05:55:20 +00:00
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
ret = bch2_mark_replicas(c, &search.e);
|
|
|
|
if (ret)
|
2018-11-01 19:10:01 +00:00
|
|
|
return ret;
|
2019-01-21 20:32:13 +00:00
|
|
|
}
|
2018-10-30 18:32:21 +00:00
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
bkey_to_replicas(&search.e, k);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
return bch2_mark_replicas(c, &search.e);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_replicas_gc_end(struct bch_fs *c, int ret)
|
|
|
|
{
|
2019-01-21 20:32:13 +00:00
|
|
|
unsigned i;
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
lockdep_assert_held(&c->replicas_gc_lock);
|
|
|
|
|
|
|
|
mutex_lock(&c->sb_lock);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
/*
|
|
|
|
* this is kind of crappy; the replicas gc mechanism needs to be ripped
|
|
|
|
* out
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < c->replicas.nr; i++) {
|
|
|
|
struct bch_replicas_entry *e =
|
|
|
|
cpu_replicas_entry(&c->replicas, i);
|
|
|
|
struct bch_replicas_cpu n;
|
2019-02-06 16:42:13 +00:00
|
|
|
u64 v;
|
2019-01-21 20:32:13 +00:00
|
|
|
|
|
|
|
if (__replicas_has_entry(&c->replicas_gc, e))
|
|
|
|
continue;
|
|
|
|
|
2019-02-14 23:38:52 +00:00
|
|
|
v = percpu_u64_get(&c->usage[0]->replicas[i]);
|
2019-01-21 20:32:13 +00:00
|
|
|
if (!v)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
n = cpu_replicas_add_entry(&c->replicas_gc, e);
|
|
|
|
if (!n.entries) {
|
|
|
|
ret = -ENOSPC;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
percpu_down_write(&c->mark_lock);
|
|
|
|
swap(n, c->replicas_gc);
|
|
|
|
percpu_up_write(&c->mark_lock);
|
|
|
|
|
|
|
|
kfree(n.entries);
|
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
if (bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc)) {
|
2017-03-17 06:18:50 +00:00
|
|
|
ret = -ENOSPC;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
bch2_write_super(c);
|
|
|
|
|
|
|
|
/* don't update in memory replicas until changes are persistent */
|
2018-12-01 15:32:48 +00:00
|
|
|
err:
|
|
|
|
percpu_down_write(&c->mark_lock);
|
|
|
|
if (!ret)
|
2019-01-21 20:32:13 +00:00
|
|
|
ret = replicas_table_update(c, &c->replicas_gc);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
kfree(c->replicas_gc.entries);
|
|
|
|
c->replicas_gc.entries = NULL;
|
|
|
|
percpu_up_write(&c->mark_lock);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
mutex_unlock(&c->sb_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
|
|
|
|
{
|
2018-10-30 18:14:19 +00:00
|
|
|
struct bch_replicas_entry *e;
|
2018-12-01 15:32:48 +00:00
|
|
|
unsigned i = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
lockdep_assert_held(&c->replicas_gc_lock);
|
|
|
|
|
|
|
|
mutex_lock(&c->sb_lock);
|
2018-12-01 15:32:48 +00:00
|
|
|
BUG_ON(c->replicas_gc.entries);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
c->replicas_gc.nr = 0;
|
|
|
|
c->replicas_gc.entry_size = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
for_each_cpu_replicas_entry(&c->replicas, e)
|
|
|
|
if (!((1 << e->data_type) & typemask)) {
|
|
|
|
c->replicas_gc.nr++;
|
|
|
|
c->replicas_gc.entry_size =
|
|
|
|
max_t(unsigned, c->replicas_gc.entry_size,
|
|
|
|
replicas_entry_bytes(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
c->replicas_gc.entries = kcalloc(c->replicas_gc.nr,
|
|
|
|
c->replicas_gc.entry_size,
|
|
|
|
GFP_NOIO);
|
|
|
|
if (!c->replicas_gc.entries) {
|
2017-03-17 06:18:50 +00:00
|
|
|
mutex_unlock(&c->sb_lock);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
for_each_cpu_replicas_entry(&c->replicas, e)
|
2017-03-17 06:18:50 +00:00
|
|
|
if (!((1 << e->data_type) & typemask))
|
2018-12-01 15:32:48 +00:00
|
|
|
memcpy(cpu_replicas_entry(&c->replicas_gc, i++),
|
|
|
|
e, c->replicas_gc.entry_size);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
bch2_cpu_replicas_sort(&c->replicas_gc);
|
2017-03-17 06:18:50 +00:00
|
|
|
mutex_unlock(&c->sb_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-25 00:09:49 +00:00
|
|
|
int bch2_replicas_set_usage(struct bch_fs *c,
|
|
|
|
struct bch_replicas_entry *r,
|
|
|
|
u64 sectors)
|
|
|
|
{
|
|
|
|
int ret, idx = bch2_replicas_entry_idx(c, r);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
struct bch_replicas_cpu n;
|
|
|
|
|
|
|
|
n = cpu_replicas_add_entry(&c->replicas, r);
|
|
|
|
if (!n.entries)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = replicas_table_update(c, &n);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
kfree(n.entries);
|
|
|
|
|
|
|
|
idx = bch2_replicas_entry_idx(c, r);
|
|
|
|
BUG_ON(ret < 0);
|
|
|
|
}
|
|
|
|
|
2019-02-14 23:38:52 +00:00
|
|
|
percpu_u64_set(&c->usage[0]->replicas[idx], sectors);
|
2019-01-25 00:09:49 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/* Replicas tracking - superblock: */
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
static int
|
|
|
|
__bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
|
|
|
|
struct bch_replicas_cpu *cpu_r)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2018-10-30 18:14:19 +00:00
|
|
|
struct bch_replicas_entry *e, *dst;
|
2018-10-30 18:32:47 +00:00
|
|
|
unsigned nr = 0, entry_size = 0, idx = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
for_each_replicas_entry(sb_r, e) {
|
|
|
|
entry_size = max_t(unsigned, entry_size,
|
|
|
|
replicas_entry_bytes(e));
|
|
|
|
nr++;
|
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
|
|
|
|
if (!cpu_r->entries)
|
|
|
|
return -ENOMEM;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
cpu_r->nr = nr;
|
|
|
|
cpu_r->entry_size = entry_size;
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
for_each_replicas_entry(sb_r, e) {
|
|
|
|
dst = cpu_replicas_entry(cpu_r, idx++);
|
|
|
|
memcpy(dst, e, replicas_entry_bytes(e));
|
|
|
|
replicas_entry_sort(dst);
|
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
return 0;
|
2018-10-30 18:32:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
static int
|
|
|
|
__bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
|
|
|
|
struct bch_replicas_cpu *cpu_r)
|
2018-10-30 18:32:47 +00:00
|
|
|
{
|
|
|
|
struct bch_replicas_entry_v0 *e;
|
|
|
|
unsigned nr = 0, entry_size = 0, idx = 0;
|
|
|
|
|
|
|
|
for_each_replicas_entry(sb_r, e) {
|
|
|
|
entry_size = max_t(unsigned, entry_size,
|
|
|
|
replicas_entry_bytes(e));
|
|
|
|
nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry_size += sizeof(struct bch_replicas_entry) -
|
|
|
|
sizeof(struct bch_replicas_entry_v0);
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
cpu_r->entries = kcalloc(nr, entry_size, GFP_NOIO);
|
|
|
|
if (!cpu_r->entries)
|
|
|
|
return -ENOMEM;
|
2018-10-30 18:32:47 +00:00
|
|
|
|
|
|
|
cpu_r->nr = nr;
|
|
|
|
cpu_r->entry_size = entry_size;
|
|
|
|
|
|
|
|
for_each_replicas_entry(sb_r, e) {
|
|
|
|
struct bch_replicas_entry *dst =
|
|
|
|
cpu_replicas_entry(cpu_r, idx++);
|
|
|
|
|
|
|
|
dst->data_type = e->data_type;
|
|
|
|
dst->nr_devs = e->nr_devs;
|
|
|
|
dst->nr_required = 1;
|
|
|
|
memcpy(dst->devs, e->devs, e->nr_devs);
|
|
|
|
replicas_entry_sort(dst);
|
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
return 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
|
|
|
|
{
|
2018-10-30 18:32:47 +00:00
|
|
|
struct bch_sb_field_replicas *sb_v1;
|
|
|
|
struct bch_sb_field_replicas_v0 *sb_v0;
|
2018-12-01 15:32:48 +00:00
|
|
|
struct bch_replicas_cpu new_r = { 0, 0, NULL };
|
|
|
|
int ret = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
|
2018-12-01 15:32:48 +00:00
|
|
|
ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
|
2018-10-30 18:32:47 +00:00
|
|
|
else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
|
2018-12-01 15:32:48 +00:00
|
|
|
ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
|
2018-10-30 18:32:47 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
if (ret)
|
2017-03-17 06:18:50 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
bch2_cpu_replicas_sort(&new_r);
|
|
|
|
|
|
|
|
percpu_down_write(&c->mark_lock);
|
2019-01-24 22:12:00 +00:00
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
ret = replicas_table_update(c, &new_r);
|
2018-12-01 15:32:48 +00:00
|
|
|
percpu_up_write(&c->mark_lock);
|
2018-10-30 18:32:47 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
kfree(new_r.entries);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
|
|
|
|
struct bch_replicas_cpu *r)
|
|
|
|
{
|
|
|
|
struct bch_sb_field_replicas_v0 *sb_r;
|
|
|
|
struct bch_replicas_entry_v0 *dst;
|
|
|
|
struct bch_replicas_entry *src;
|
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
bytes = sizeof(struct bch_sb_field_replicas);
|
|
|
|
|
|
|
|
for_each_cpu_replicas_entry(r, src)
|
|
|
|
bytes += replicas_entry_bytes(src) - 1;
|
|
|
|
|
|
|
|
sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
|
|
|
|
DIV_ROUND_UP(bytes, sizeof(u64)));
|
|
|
|
if (!sb_r)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
|
|
|
|
sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
|
|
|
|
|
|
|
|
memset(&sb_r->entries, 0,
|
|
|
|
vstruct_end(&sb_r->field) -
|
|
|
|
(void *) &sb_r->entries);
|
|
|
|
|
|
|
|
dst = sb_r->entries;
|
|
|
|
for_each_cpu_replicas_entry(r, src) {
|
|
|
|
dst->data_type = src->data_type;
|
|
|
|
dst->nr_devs = src->nr_devs;
|
|
|
|
memcpy(dst->devs, src->devs, src->nr_devs);
|
|
|
|
|
|
|
|
dst = replicas_entry_next(dst);
|
|
|
|
|
|
|
|
BUG_ON((void *) dst > vstruct_end(&sb_r->field));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
|
|
|
|
struct bch_replicas_cpu *r)
|
|
|
|
{
|
|
|
|
struct bch_sb_field_replicas *sb_r;
|
2018-10-30 18:14:19 +00:00
|
|
|
struct bch_replicas_entry *dst, *src;
|
2018-10-30 18:32:47 +00:00
|
|
|
bool need_v1 = false;
|
2018-10-30 18:14:19 +00:00
|
|
|
size_t bytes;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
bytes = sizeof(struct bch_sb_field_replicas);
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
for_each_cpu_replicas_entry(r, src) {
|
2018-10-30 18:14:19 +00:00
|
|
|
bytes += replicas_entry_bytes(src);
|
2018-10-30 18:32:47 +00:00
|
|
|
if (src->nr_required != 1)
|
|
|
|
need_v1 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!need_v1)
|
|
|
|
return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
sb_r = bch2_sb_resize_replicas(&c->disk_sb,
|
2018-10-30 18:14:19 +00:00
|
|
|
DIV_ROUND_UP(bytes, sizeof(u64)));
|
2017-03-17 06:18:50 +00:00
|
|
|
if (!sb_r)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
|
|
|
|
sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
memset(&sb_r->entries, 0,
|
|
|
|
vstruct_end(&sb_r->field) -
|
|
|
|
(void *) &sb_r->entries);
|
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
dst = sb_r->entries;
|
|
|
|
for_each_cpu_replicas_entry(r, src) {
|
|
|
|
memcpy(dst, src, replicas_entry_bytes(src));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
dst = replicas_entry_next(dst);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
BUG_ON((void *) dst > vstruct_end(&sb_r->field));
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
static const char *check_dup_replicas_entries(struct bch_replicas_cpu *cpu_r)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
sort_cmp_size(cpu_r->entries,
|
|
|
|
cpu_r->nr,
|
|
|
|
cpu_r->entry_size,
|
|
|
|
memcmp, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i + 1 < cpu_r->nr; i++) {
|
|
|
|
struct bch_replicas_entry *l =
|
|
|
|
cpu_replicas_entry(cpu_r, i);
|
|
|
|
struct bch_replicas_entry *r =
|
|
|
|
cpu_replicas_entry(cpu_r, i + 1);
|
|
|
|
|
|
|
|
BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0);
|
|
|
|
|
|
|
|
if (!memcmp(l, r, cpu_r->entry_size))
|
|
|
|
return "duplicate replicas entry";
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static const char *bch2_sb_validate_replicas(struct bch_sb *sb, struct bch_sb_field *f)
|
|
|
|
{
|
|
|
|
struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
|
|
|
|
struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
|
2018-12-01 15:32:48 +00:00
|
|
|
struct bch_replicas_cpu cpu_r = { .entries = NULL };
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_replicas_entry *e;
|
|
|
|
const char *err;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for_each_replicas_entry(sb_r, e) {
|
|
|
|
err = "invalid replicas entry: invalid data type";
|
|
|
|
if (e->data_type >= BCH_DATA_NR)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
err = "invalid replicas entry: no devices";
|
2018-10-30 18:14:19 +00:00
|
|
|
if (!e->nr_devs)
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err;
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
err = "invalid replicas entry: bad nr_required";
|
|
|
|
if (!e->nr_required ||
|
|
|
|
(e->nr_required > 1 &&
|
|
|
|
e->nr_required >= e->nr_devs))
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
err = "invalid replicas entry: invalid device";
|
2018-10-30 18:14:19 +00:00
|
|
|
for (i = 0; i < e->nr_devs; i++)
|
2017-03-17 06:18:50 +00:00
|
|
|
if (!bch2_dev_exists(sb, mi, e->devs[i]))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = "cannot allocate memory";
|
2018-12-01 15:32:48 +00:00
|
|
|
if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
|
2017-03-17 06:18:50 +00:00
|
|
|
goto err;
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
err = check_dup_replicas_entries(&cpu_r);
|
2017-03-17 06:18:50 +00:00
|
|
|
err:
|
2018-12-01 15:32:48 +00:00
|
|
|
kfree(cpu_r.entries);
|
2017-03-17 06:18:50 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-11-09 06:24:07 +00:00
|
|
|
static void bch2_sb_replicas_to_text(struct printbuf *out,
|
|
|
|
struct bch_sb *sb,
|
|
|
|
struct bch_sb_field *f)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2018-11-09 06:24:07 +00:00
|
|
|
struct bch_sb_field_replicas *r = field_to_type(f, replicas);
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bch_replicas_entry *e;
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
for_each_replicas_entry(r, e) {
|
|
|
|
if (!first)
|
2018-11-09 06:24:07 +00:00
|
|
|
pr_buf(out, " ");
|
2017-03-17 06:18:50 +00:00
|
|
|
first = false;
|
|
|
|
|
2019-01-21 20:32:13 +00:00
|
|
|
bch2_replicas_entry_to_text(out, e);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 06:24:07 +00:00
|
|
|
const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
|
|
|
|
.validate = bch2_sb_validate_replicas,
|
|
|
|
.to_text = bch2_sb_replicas_to_text,
|
|
|
|
};
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
static const char *bch2_sb_validate_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f)
|
|
|
|
{
|
|
|
|
struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
|
|
|
|
struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
|
2018-12-01 15:32:48 +00:00
|
|
|
struct bch_replicas_cpu cpu_r = { .entries = NULL };
|
2018-10-30 18:32:47 +00:00
|
|
|
struct bch_replicas_entry_v0 *e;
|
|
|
|
const char *err;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for_each_replicas_entry_v0(sb_r, e) {
|
|
|
|
err = "invalid replicas entry: invalid data type";
|
|
|
|
if (e->data_type >= BCH_DATA_NR)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
err = "invalid replicas entry: no devices";
|
|
|
|
if (!e->nr_devs)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
err = "invalid replicas entry: invalid device";
|
|
|
|
for (i = 0; i < e->nr_devs; i++)
|
|
|
|
if (!bch2_dev_exists(sb, mi, e->devs[i]))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = "cannot allocate memory";
|
2018-12-01 15:32:48 +00:00
|
|
|
if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
|
2018-10-30 18:32:47 +00:00
|
|
|
goto err;
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
err = check_dup_replicas_entries(&cpu_r);
|
2018-10-30 18:32:47 +00:00
|
|
|
err:
|
2018-12-01 15:32:48 +00:00
|
|
|
kfree(cpu_r.entries);
|
2018-10-30 18:32:47 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
|
|
|
|
.validate = bch2_sb_validate_replicas_v0,
|
|
|
|
};
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/* Query replicas: */
|
|
|
|
|
|
|
|
struct replicas_status __bch2_replicas_status(struct bch_fs *c,
|
|
|
|
struct bch_devs_mask online_devs)
|
|
|
|
{
|
|
|
|
struct bch_sb_field_members *mi;
|
2018-10-30 18:14:19 +00:00
|
|
|
struct bch_replicas_entry *e;
|
|
|
|
unsigned i, nr_online, nr_offline;
|
2017-03-17 06:18:50 +00:00
|
|
|
struct replicas_status ret;
|
|
|
|
|
|
|
|
memset(&ret, 0, sizeof(ret));
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
|
2018-10-30 18:32:47 +00:00
|
|
|
ret.replicas[i].redundancy = INT_MAX;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
mi = bch2_sb_get_members(c->disk_sb.sb);
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
percpu_down_read(&c->mark_lock);
|
|
|
|
|
|
|
|
for_each_cpu_replicas_entry(&c->replicas, e) {
|
2017-03-17 06:18:50 +00:00
|
|
|
if (e->data_type >= ARRAY_SIZE(ret.replicas))
|
|
|
|
panic("e %p data_type %u\n", e, e->data_type);
|
|
|
|
|
|
|
|
nr_online = nr_offline = 0;
|
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
for (i = 0; i < e->nr_devs; i++) {
|
|
|
|
BUG_ON(!bch2_dev_exists(c->disk_sb.sb, mi,
|
|
|
|
e->devs[i]));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:14:19 +00:00
|
|
|
if (test_bit(e->devs[i], online_devs.d))
|
2017-03-17 06:18:50 +00:00
|
|
|
nr_online++;
|
|
|
|
else
|
|
|
|
nr_offline++;
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
ret.replicas[e->data_type].redundancy =
|
|
|
|
min(ret.replicas[e->data_type].redundancy,
|
|
|
|
(int) nr_online - (int) e->nr_required);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
ret.replicas[e->data_type].nr_offline =
|
|
|
|
max(ret.replicas[e->data_type].nr_offline,
|
|
|
|
nr_offline);
|
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
percpu_up_read(&c->mark_lock);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(ret.replicas); i++)
|
|
|
|
if (ret.replicas[i].redundancy == INT_MAX)
|
|
|
|
ret.replicas[i].redundancy = 0;
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct replicas_status bch2_replicas_status(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
return __bch2_replicas_status(c, bch2_online_devs(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool have_enough_devs(struct replicas_status s,
|
|
|
|
enum bch_data_type type,
|
|
|
|
bool force_if_degraded,
|
|
|
|
bool force_if_lost)
|
|
|
|
{
|
|
|
|
return (!s.replicas[type].nr_offline || force_if_degraded) &&
|
2018-10-30 18:32:47 +00:00
|
|
|
(s.replicas[type].redundancy >= 0 || force_if_lost);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bch2_have_enough_devs(struct replicas_status s, unsigned flags)
|
|
|
|
{
|
|
|
|
return (have_enough_devs(s, BCH_DATA_JOURNAL,
|
|
|
|
flags & BCH_FORCE_IF_METADATA_DEGRADED,
|
|
|
|
flags & BCH_FORCE_IF_METADATA_LOST) &&
|
|
|
|
have_enough_devs(s, BCH_DATA_BTREE,
|
|
|
|
flags & BCH_FORCE_IF_METADATA_DEGRADED,
|
|
|
|
flags & BCH_FORCE_IF_METADATA_LOST) &&
|
|
|
|
have_enough_devs(s, BCH_DATA_USER,
|
|
|
|
flags & BCH_FORCE_IF_DATA_DEGRADED,
|
|
|
|
flags & BCH_FORCE_IF_DATA_LOST));
|
|
|
|
}
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
int bch2_replicas_online(struct bch_fs *c, bool meta)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct replicas_status s = bch2_replicas_status(c);
|
|
|
|
|
2018-10-30 18:32:47 +00:00
|
|
|
return (meta
|
|
|
|
? min(s.replicas[BCH_DATA_JOURNAL].redundancy,
|
|
|
|
s.replicas[BCH_DATA_BTREE].redundancy)
|
|
|
|
: s.replicas[BCH_DATA_USER].redundancy) + 1;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
|
|
|
|
{
|
2018-10-30 18:14:19 +00:00
|
|
|
struct bch_replicas_entry *e;
|
|
|
|
unsigned i, ret = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
percpu_down_read(&c->mark_lock);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
for_each_cpu_replicas_entry(&c->replicas, e)
|
2018-10-30 18:14:19 +00:00
|
|
|
for (i = 0; i < e->nr_devs; i++)
|
|
|
|
if (e->devs[i] == ca->dev_idx)
|
|
|
|
ret |= 1 << e->data_type;
|
|
|
|
|
2018-12-01 15:32:48 +00:00
|
|
|
percpu_up_read(&c->mark_lock);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-01-24 22:12:00 +00:00
|
|
|
|
|
|
|
int bch2_fs_replicas_init(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
c->journal.entry_u64s_reserved +=
|
|
|
|
reserve_journal_replicas(c, &c->replicas);
|
2019-02-15 01:39:17 +00:00
|
|
|
|
|
|
|
return replicas_table_update(c, &c->replicas);
|
2019-01-24 22:12:00 +00:00
|
|
|
}
|