Commit e63534a2 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Rework triggers interface

The trigger for stripe keys is shortly going to need both the old and
the new key passed to the trigger - this patch does that rework.

For now, this just changes the in memory triggers, and this doesn't
change how extent triggers work.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 697e45b2
...@@ -714,7 +714,8 @@ void bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca, ...@@ -714,7 +714,8 @@ void bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
preempt_enable(); preempt_enable();
} }
static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k, static int bch2_mark_alloc(struct bch_fs *c,
struct bkey_s_c old, struct bkey_s_c new,
struct bch_fs_usage *fs_usage, struct bch_fs_usage *fs_usage,
u64 journal_seq, unsigned flags) u64 journal_seq, unsigned flags)
{ {
...@@ -722,7 +723,11 @@ static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k, ...@@ -722,7 +723,11 @@ static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k,
struct bkey_alloc_unpacked u; struct bkey_alloc_unpacked u;
struct bch_dev *ca; struct bch_dev *ca;
struct bucket *g; struct bucket *g;
struct bucket_mark old, m; struct bucket_mark old_m, m;
/* We don't do anything for deletions - do we?: */
if (new.k->type != KEY_TYPE_alloc)
return 0;
/* /*
* alloc btree is read in by bch2_alloc_read, not gc: * alloc btree is read in by bch2_alloc_read, not gc:
...@@ -731,15 +736,15 @@ static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k, ...@@ -731,15 +736,15 @@ static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k,
!(flags & BTREE_TRIGGER_BUCKET_INVALIDATE)) !(flags & BTREE_TRIGGER_BUCKET_INVALIDATE))
return 0; return 0;
ca = bch_dev_bkey_exists(c, k.k->p.inode); ca = bch_dev_bkey_exists(c, new.k->p.inode);
if (k.k->p.offset >= ca->mi.nbuckets) if (new.k->p.offset >= ca->mi.nbuckets)
return 0; return 0;
g = __bucket(ca, k.k->p.offset, gc); g = __bucket(ca, new.k->p.offset, gc);
u = bch2_alloc_unpack(k); u = bch2_alloc_unpack(new);
old = bucket_cmpxchg(g, m, ({ old_m = bucket_cmpxchg(g, m, ({
m.gen = u.gen; m.gen = u.gen;
m.data_type = u.data_type; m.data_type = u.data_type;
m.dirty_sectors = u.dirty_sectors; m.dirty_sectors = u.dirty_sectors;
...@@ -752,7 +757,7 @@ static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k, ...@@ -752,7 +757,7 @@ static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k,
})); }));
if (!(flags & BTREE_TRIGGER_ALLOC_READ)) if (!(flags & BTREE_TRIGGER_ALLOC_READ))
bch2_dev_usage_update(c, ca, fs_usage, old, m, gc); bch2_dev_usage_update(c, ca, fs_usage, old_m, m, gc);
g->io_time[READ] = u.read_time; g->io_time[READ] = u.read_time;
g->io_time[WRITE] = u.write_time; g->io_time[WRITE] = u.write_time;
...@@ -765,11 +770,11 @@ static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k, ...@@ -765,11 +770,11 @@ static int bch2_mark_alloc(struct bch_fs *c, struct bkey_s_c k,
*/ */
if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) && if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
old.cached_sectors) { old_m.cached_sectors) {
update_cached_sectors(c, fs_usage, ca->dev_idx, update_cached_sectors(c, fs_usage, ca->dev_idx,
-old.cached_sectors); -old_m.cached_sectors);
trace_invalidate(ca, bucket_to_sector(ca, k.k->p.offset), trace_invalidate(ca, bucket_to_sector(ca, new.k->p.offset),
old.cached_sectors); old_m.cached_sectors);
} }
return 0; return 0;
...@@ -882,9 +887,9 @@ static void bucket_set_stripe(struct bch_fs *c, ...@@ -882,9 +887,9 @@ static void bucket_set_stripe(struct bch_fs *c,
const struct bch_stripe *v, const struct bch_stripe *v,
struct bch_fs_usage *fs_usage, struct bch_fs_usage *fs_usage,
u64 journal_seq, u64 journal_seq,
unsigned flags) unsigned flags,
bool enabled)
{ {
bool enabled = !(flags & BTREE_TRIGGER_OVERWRITE);
bool gc = flags & BTREE_TRIGGER_GC; bool gc = flags & BTREE_TRIGGER_GC;
unsigned i; unsigned i;
...@@ -1104,12 +1109,14 @@ static int bch2_mark_stripe_ptr(struct bch_fs *c, ...@@ -1104,12 +1109,14 @@ static int bch2_mark_stripe_ptr(struct bch_fs *c,
return 0; return 0;
} }
static int bch2_mark_extent(struct bch_fs *c, struct bkey_s_c k, static int bch2_mark_extent(struct bch_fs *c,
struct bkey_s_c old, struct bkey_s_c new,
unsigned offset, s64 sectors, unsigned offset, s64 sectors,
enum bch_data_type data_type, enum bch_data_type data_type,
struct bch_fs_usage *fs_usage, struct bch_fs_usage *fs_usage,
unsigned journal_seq, unsigned flags) unsigned journal_seq, unsigned flags)
{ {
struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
const union bch_extent_entry *entry; const union bch_extent_entry *entry;
struct extent_ptr_decoded p; struct extent_ptr_decoded p;
...@@ -1178,72 +1185,88 @@ static int bch2_mark_extent(struct bch_fs *c, struct bkey_s_c k, ...@@ -1178,72 +1185,88 @@ static int bch2_mark_extent(struct bch_fs *c, struct bkey_s_c k,
return 0; return 0;
} }
static int bch2_mark_stripe(struct bch_fs *c, struct bkey_s_c k, static int bch2_mark_stripe(struct bch_fs *c,
struct bkey_s_c old, struct bkey_s_c new,
struct bch_fs_usage *fs_usage, struct bch_fs_usage *fs_usage,
u64 journal_seq, unsigned flags) u64 journal_seq, unsigned flags)
{ {
bool gc = flags & BTREE_TRIGGER_GC; bool gc = flags & BTREE_TRIGGER_GC;
struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k); size_t idx = new.k->p.offset;
size_t idx = s.k->p.offset; const struct bch_stripe *old_s = old.k->type == KEY_TYPE_stripe
? bkey_s_c_to_stripe(old).v : NULL;
const struct bch_stripe *new_s = new.k->type == KEY_TYPE_stripe
? bkey_s_c_to_stripe(new).v : NULL;
struct stripe *m = genradix_ptr(&c->stripes[gc], idx); struct stripe *m = genradix_ptr(&c->stripes[gc], idx);
unsigned i; unsigned i;
spin_lock(&c->ec_stripes_heap_lock); if (!m || (old_s && !m->alive)) {
if (!m || ((flags & BTREE_TRIGGER_OVERWRITE) && !m->alive)) {
spin_unlock(&c->ec_stripes_heap_lock);
bch_err_ratelimited(c, "error marking nonexistent stripe %zu", bch_err_ratelimited(c, "error marking nonexistent stripe %zu",
idx); idx);
return -1; return -1;
} }
if (!(flags & BTREE_TRIGGER_OVERWRITE)) { if (!new_s) {
m->sectors = le16_to_cpu(s.v->sectors); /* Deleting: */
m->algorithm = s.v->algorithm; bucket_set_stripe(c, old_s, fs_usage,
m->nr_blocks = s.v->nr_blocks; journal_seq, flags, false);
m->nr_redundant = s.v->nr_redundant;
bch2_bkey_to_replicas(&m->r.e, k); if (!gc) {
spin_lock(&c->ec_stripes_heap_lock);
bch2_stripes_heap_del(c, m, idx);
spin_unlock(&c->ec_stripes_heap_lock);
}
/* memset(m, 0, sizeof(*m));
* XXX: account for stripes somehow here } else {
*/ BUG_ON(old_s && new_s->nr_blocks != old_s->nr_blocks);
#if 0 BUG_ON(old_s && new_s->nr_redundant != old_s->nr_redundant);
update_replicas(c, fs_usage, &m->r.e, stripe_sectors);
#endif if (!old_s)
bucket_set_stripe(c, new_s, fs_usage,
journal_seq, flags, true);
m->sectors = le16_to_cpu(new_s->sectors);
m->algorithm = new_s->algorithm;
m->nr_blocks = new_s->nr_blocks;
m->nr_redundant = new_s->nr_redundant;
bch2_bkey_to_replicas(&m->r.e, new);
/* gc recalculates these fields: */ /* gc recalculates these fields: */
if (!(flags & BTREE_TRIGGER_GC)) { if (!(flags & BTREE_TRIGGER_GC)) {
for (i = 0; i < s.v->nr_blocks; i++) { m->blocks_nonempty = 0;
for (i = 0; i < new_s->nr_blocks; i++) {
m->block_sectors[i] = m->block_sectors[i] =
stripe_blockcount_get(s.v, i); stripe_blockcount_get(new_s, i);
m->blocks_nonempty += !!m->block_sectors[i]; m->blocks_nonempty += !!m->block_sectors[i];
} }
} }
if (!gc) if (!gc) {
spin_lock(&c->ec_stripes_heap_lock);
bch2_stripes_heap_update(c, m, idx); bch2_stripes_heap_update(c, m, idx);
spin_unlock(&c->ec_stripes_heap_lock);
}
m->alive = true; m->alive = true;
} else {
if (!gc)
bch2_stripes_heap_del(c, m, idx);
memset(m, 0, sizeof(*m));
} }
spin_unlock(&c->ec_stripes_heap_lock);
bucket_set_stripe(c, s.v, fs_usage, 0, flags);
return 0; return 0;
} }
static int bch2_mark_key_locked(struct bch_fs *c, static int bch2_mark_key_locked(struct bch_fs *c,
struct bkey_s_c k, struct bkey_s_c old,
struct bkey_s_c new,
unsigned offset, s64 sectors, unsigned offset, s64 sectors,
struct bch_fs_usage *fs_usage, struct bch_fs_usage *fs_usage,
u64 journal_seq, unsigned flags) u64 journal_seq, unsigned flags)
{ {
struct bkey_s_c k = flags & BTREE_TRIGGER_INSERT ? new : old;
int ret = 0; int ret = 0;
BUG_ON(!(flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)));
preempt_disable(); preempt_disable();
if (!fs_usage || (flags & BTREE_TRIGGER_GC)) if (!fs_usage || (flags & BTREE_TRIGGER_GC))
...@@ -1252,7 +1275,7 @@ static int bch2_mark_key_locked(struct bch_fs *c, ...@@ -1252,7 +1275,7 @@ static int bch2_mark_key_locked(struct bch_fs *c,
switch (k.k->type) { switch (k.k->type) {
case KEY_TYPE_alloc: case KEY_TYPE_alloc:
ret = bch2_mark_alloc(c, k, fs_usage, journal_seq, flags); ret = bch2_mark_alloc(c, old, new, fs_usage, journal_seq, flags);
break; break;
case KEY_TYPE_btree_ptr: case KEY_TYPE_btree_ptr:
case KEY_TYPE_btree_ptr_v2: case KEY_TYPE_btree_ptr_v2:
...@@ -1260,16 +1283,16 @@ static int bch2_mark_key_locked(struct bch_fs *c, ...@@ -1260,16 +1283,16 @@ static int bch2_mark_key_locked(struct bch_fs *c,
? c->opts.btree_node_size ? c->opts.btree_node_size
: -c->opts.btree_node_size; : -c->opts.btree_node_size;
ret = bch2_mark_extent(c, k, offset, sectors, BCH_DATA_BTREE, ret = bch2_mark_extent(c, old, new, offset, sectors,
fs_usage, journal_seq, flags); BCH_DATA_BTREE, fs_usage, journal_seq, flags);
break; break;
case KEY_TYPE_extent: case KEY_TYPE_extent:
case KEY_TYPE_reflink_v: case KEY_TYPE_reflink_v:
ret = bch2_mark_extent(c, k, offset, sectors, BCH_DATA_USER, ret = bch2_mark_extent(c, old, new, offset, sectors,
fs_usage, journal_seq, flags); BCH_DATA_USER, fs_usage, journal_seq, flags);
break; break;
case KEY_TYPE_stripe: case KEY_TYPE_stripe:
ret = bch2_mark_stripe(c, k, fs_usage, journal_seq, flags); ret = bch2_mark_stripe(c, old, new, fs_usage, journal_seq, flags);
break; break;
case KEY_TYPE_inode: case KEY_TYPE_inode:
if (!(flags & BTREE_TRIGGER_OVERWRITE)) if (!(flags & BTREE_TRIGGER_OVERWRITE))
...@@ -1295,82 +1318,38 @@ static int bch2_mark_key_locked(struct bch_fs *c, ...@@ -1295,82 +1318,38 @@ static int bch2_mark_key_locked(struct bch_fs *c,
return ret; return ret;
} }
int bch2_mark_key(struct bch_fs *c, struct bkey_s_c k, int bch2_mark_key(struct bch_fs *c, struct bkey_s_c new,
unsigned offset, s64 sectors, unsigned offset, s64 sectors,
struct bch_fs_usage *fs_usage, struct bch_fs_usage *fs_usage,
u64 journal_seq, unsigned flags) u64 journal_seq, unsigned flags)
{ {
struct bkey deleted;
struct bkey_s_c old = (struct bkey_s_c) { &deleted, NULL };
int ret; int ret;
bkey_init(&deleted);
percpu_down_read(&c->mark_lock); percpu_down_read(&c->mark_lock);
ret = bch2_mark_key_locked(c, k, offset, sectors, ret = bch2_mark_key_locked(c, old, new, offset, sectors,
fs_usage, journal_seq, flags); fs_usage, journal_seq,
BTREE_TRIGGER_INSERT|flags);
percpu_up_read(&c->mark_lock); percpu_up_read(&c->mark_lock);
return ret; return ret;
} }
static int bch2_mark_overwrite(struct btree_trans *trans,
struct btree_iter *iter,
struct bkey_s_c old,
struct bkey_i *new,
struct bch_fs_usage *fs_usage,
unsigned flags,
bool is_extents)
{
struct bch_fs *c = trans->c;
unsigned offset = 0;
s64 sectors = -((s64) old.k->size);
flags |= BTREE_TRIGGER_OVERWRITE;
if (is_extents
? bkey_cmp(new->k.p, bkey_start_pos(old.k)) <= 0
: bkey_cmp(new->k.p, old.k->p))
return 0;
if (is_extents) {
switch (bch2_extent_overlap(&new->k, old.k)) {
case BCH_EXTENT_OVERLAP_ALL:
offset = 0;
sectors = -((s64) old.k->size);
break;
case BCH_EXTENT_OVERLAP_BACK:
offset = bkey_start_offset(&new->k) -
bkey_start_offset(old.k);
sectors = bkey_start_offset(&new->k) -
old.k->p.offset;
break;
case BCH_EXTENT_OVERLAP_FRONT:
offset = 0;
sectors = bkey_start_offset(old.k) -
new->k.p.offset;
break;
case BCH_EXTENT_OVERLAP_MIDDLE:
offset = bkey_start_offset(&new->k) -
bkey_start_offset(old.k);
sectors = -((s64) new->k.size);
flags |= BTREE_TRIGGER_OVERWRITE_SPLIT;
break;
}
BUG_ON(sectors >= 0);
}
return bch2_mark_key_locked(c, old, offset, sectors, fs_usage,
trans->journal_res.seq, flags) ?: 1;
}
int bch2_mark_update(struct btree_trans *trans, int bch2_mark_update(struct btree_trans *trans,
struct btree_iter *iter, struct btree_iter *iter,
struct bkey_i *insert, struct bkey_i *new,
struct bch_fs_usage *fs_usage, struct bch_fs_usage *fs_usage,
unsigned flags) unsigned flags)
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct btree *b = iter_l(iter)->b; struct btree *b = iter_l(iter)->b;
struct btree_node_iter node_iter = iter_l(iter)->iter; struct btree_node_iter node_iter = iter_l(iter)->iter;
struct bkey_packed *_k; struct bkey_packed *_old;
struct bkey_s_c old;
struct bkey unpacked;
int ret = 0; int ret = 0;
if (unlikely(flags & BTREE_TRIGGER_NORUN)) if (unlikely(flags & BTREE_TRIGGER_NORUN))
...@@ -1379,31 +1358,87 @@ int bch2_mark_update(struct btree_trans *trans, ...@@ -1379,31 +1358,87 @@ int bch2_mark_update(struct btree_trans *trans,
if (!btree_node_type_needs_gc(iter->btree_id)) if (!btree_node_type_needs_gc(iter->btree_id))
return 0; return 0;
bch2_mark_key_locked(c, bkey_i_to_s_c(insert), bkey_init(&unpacked);
0, insert->k.size, old = (struct bkey_s_c) { &unpacked, NULL };
fs_usage, trans->journal_res.seq,
BTREE_TRIGGER_INSERT|flags);
/* if (!btree_node_type_is_extents(iter->btree_id)) {
* For non extents, we only mark the new key, not the key being if (btree_iter_type(iter) != BTREE_ITER_CACHED) {
* overwritten - unless we're actually deleting: _old = bch2_btree_node_iter_peek(&node_iter, b);
*/ if (_old)
if ((iter->btree_id == BTREE_ID_ALLOC || old = bkey_disassemble(b, _old, &unpacked);
iter->btree_id == BTREE_ID_EC) && } else {
!bkey_deleted(&insert->k)) struct bkey_cached *ck = (void *) iter->l[0].b;
return 0;
while ((_k = bch2_btree_node_iter_peek(&node_iter, b))) { if (ck->valid)
struct bkey unpacked; old = bkey_i_to_s_c(ck->k);
struct bkey_s_c k = bkey_disassemble(b, _k, &unpacked); }
ret = bch2_mark_overwrite(trans, iter, k, insert, if (old.k->type == new->k.type) {
fs_usage, flags, bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
btree_node_type_is_extents(iter->btree_id)); fs_usage, trans->journal_res.seq,
if (ret <= 0) BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE|flags);
break;
bch2_btree_node_iter_advance(&node_iter, b); } else {
bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
fs_usage, trans->journal_res.seq,
BTREE_TRIGGER_INSERT|flags);
bch2_mark_key_locked(c, old, bkey_i_to_s_c(new), 0, 0,
fs_usage, trans->journal_res.seq,
BTREE_TRIGGER_OVERWRITE|flags);
}
} else {
BUG_ON(btree_iter_type(iter) == BTREE_ITER_CACHED);
bch2_mark_key_locked(c, old, bkey_i_to_s_c(new),
0, new->k.size,
fs_usage, trans->journal_res.seq,
BTREE_TRIGGER_INSERT|flags);
while ((_old = bch2_btree_node_iter_peek(&node_iter, b))) {
unsigned offset = 0;
s64 sectors;
old = bkey_disassemble(b, _old, &unpacked);
sectors = -((s64) old.k->size);
flags |= BTREE_TRIGGER_OVERWRITE;
if (bkey_cmp(new->k.p, bkey_start_pos(old.k)) <= 0)
return 0;
switch (bch2_extent_overlap(&new->k, old.k)) {
case BCH_EXTENT_OVERLAP_ALL:
offset = 0;
sectors = -((s64) old.k->size);
break;
case BCH_EXTENT_OVERLAP_BACK:
offset = bkey_start_offset(&new->k) -
bkey_start_offset(old.k);
sectors = bkey_start_offset(&new->k) -
old.k->p.offset;
break;
case BCH_EXTENT_OVERLAP_FRONT:
offset = 0;
sectors = bkey_start_offset(old.k) -
new->k.p.offset;
break;
case BCH_EXTENT_OVERLAP_MIDDLE:
offset = bkey_start_offset(&new->k) -
bkey_start_offset(old.k);
sectors = -((s64) new->k.size);
flags |= BTREE_TRIGGER_OVERWRITE_SPLIT;
break;
}
BUG_ON(sectors >= 0);
ret = bch2_mark_key_locked(c, old, bkey_i_to_s_c(new),
offset, sectors, fs_usage,
trans->journal_res.seq, flags) ?: 1;
if (ret <= 0)
break;
bch2_btree_node_iter_advance(&node_iter, b);
}
} }
return ret; return ret;
...@@ -1458,8 +1493,10 @@ void bch2_trans_fs_usage_apply(struct btree_trans *trans, ...@@ -1458,8 +1493,10 @@ void bch2_trans_fs_usage_apply(struct btree_trans *trans,
} else { } else {
struct bkey_cached *ck = (void *) i->iter->l[0].b; struct bkey_cached *ck = (void *) i->iter->l[0].b;
bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(ck->k)); if (ck->valid) {
pr_err("%s", buf); bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(ck->k));
pr_err("%s", buf);
}
} }
} }
} }
......
...@@ -259,8 +259,8 @@ void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *, ...@@ -259,8 +259,8 @@ void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
size_t, enum bch_data_type, unsigned, size_t, enum bch_data_type, unsigned,
struct gc_pos, unsigned); struct gc_pos, unsigned);
int bch2_mark_key(struct bch_fs *, struct bkey_s_c, unsigned, s64, int bch2_mark_key(struct bch_fs *, struct bkey_s_c, unsigned,
struct bch_fs_usage *, u64, unsigned); s64, struct bch_fs_usage *, u64, unsigned);
int bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage_online *, int bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage_online *,
struct disk_reservation *, unsigned); struct disk_reservation *, unsigned);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment