Commit 801a3de6 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Indirect inline data extents

When inline data extents were added, reflink was forgotten about - we
need indirect inline data extents for reflink + inline data to work
correctly.

This patch adds them, and a new feature bit that's flipped when they're
used.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 13dcd4ab
...@@ -344,7 +344,8 @@ static inline void bkey_init(struct bkey *k) ...@@ -344,7 +344,8 @@ static inline void bkey_init(struct bkey *k)
x(reflink_p, 15) \ x(reflink_p, 15) \
x(reflink_v, 16) \ x(reflink_v, 16) \
x(inline_data, 17) \ x(inline_data, 17) \
x(btree_ptr_v2, 18) x(btree_ptr_v2, 18) \
x(indirect_inline_data, 19)
enum bch_bkey_type { enum bch_bkey_type {
#define x(name, nr) KEY_TYPE_##name = nr, #define x(name, nr) KEY_TYPE_##name = nr,
...@@ -890,6 +891,12 @@ struct bch_reflink_v { ...@@ -890,6 +891,12 @@ struct bch_reflink_v {
__u64 _data[0]; __u64 _data[0];
}; };
struct bch_indirect_inline_data {
struct bch_val v;
__le64 refcount;
u8 data[0];
};
/* Inline data */ /* Inline data */
struct bch_inline_data { struct bch_inline_data {
...@@ -1326,7 +1333,8 @@ LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16); ...@@ -1326,7 +1333,8 @@ LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16);
x(incompressible, 10) \ x(incompressible, 10) \
x(btree_ptr_v2, 11) \ x(btree_ptr_v2, 11) \
x(extents_above_btree_updates, 12) \ x(extents_above_btree_updates, 12) \
x(btree_updates_journalled, 13) x(btree_updates_journalled, 13) \
x(reflink_inline_data, 14)
#define BCH_SB_FEATURES_ALL \ #define BCH_SB_FEATURES_ALL \
((1ULL << BCH_FEATURE_new_siphash)| \ ((1ULL << BCH_FEATURE_new_siphash)| \
......
...@@ -573,6 +573,7 @@ BKEY_VAL_ACCESSORS(reflink_p); ...@@ -573,6 +573,7 @@ BKEY_VAL_ACCESSORS(reflink_p);
BKEY_VAL_ACCESSORS(reflink_v); BKEY_VAL_ACCESSORS(reflink_v);
BKEY_VAL_ACCESSORS(inline_data); BKEY_VAL_ACCESSORS(inline_data);
BKEY_VAL_ACCESSORS(btree_ptr_v2); BKEY_VAL_ACCESSORS(btree_ptr_v2);
BKEY_VAL_ACCESSORS(indirect_inline_data);
/* byte order helpers */ /* byte order helpers */
......
...@@ -72,7 +72,11 @@ static const char *key_type_inline_data_invalid(const struct bch_fs *c, ...@@ -72,7 +72,11 @@ static const char *key_type_inline_data_invalid(const struct bch_fs *c,
static void key_type_inline_data_to_text(struct printbuf *out, struct bch_fs *c, static void key_type_inline_data_to_text(struct printbuf *out, struct bch_fs *c,
struct bkey_s_c k) struct bkey_s_c k)
{ {
pr_buf(out, "(%zu bytes)", bkey_val_bytes(k.k)); struct bkey_s_c_inline_data d = bkey_s_c_to_inline_data(k);
unsigned datalen = bkey_inline_data_bytes(k.k);
pr_buf(out, "datalen %u: %*phN",
datalen, min(datalen, 32U), d.v->data);
} }
#define bch2_bkey_ops_inline_data (struct bkey_ops) { \ #define bch2_bkey_ops_inline_data (struct bkey_ops) { \
......
...@@ -1811,6 +1811,18 @@ static int bch2_trans_mark_stripe(struct btree_trans *trans, ...@@ -1811,6 +1811,18 @@ static int bch2_trans_mark_stripe(struct btree_trans *trans,
return ret; return ret;
} }
static __le64 *bkey_refcount(struct bkey_i *k)
{
switch (k->k.type) {
case KEY_TYPE_reflink_v:
return &bkey_i_to_reflink_v(k)->v.refcount;
case KEY_TYPE_indirect_inline_data:
return &bkey_i_to_indirect_inline_data(k)->v.refcount;
default:
return NULL;
}
}
static int __bch2_trans_mark_reflink_p(struct btree_trans *trans, static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
struct bkey_s_c_reflink_p p, struct bkey_s_c_reflink_p p,
u64 idx, unsigned sectors, u64 idx, unsigned sectors,
...@@ -1819,7 +1831,8 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans, ...@@ -1819,7 +1831,8 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct btree_iter *iter; struct btree_iter *iter;
struct bkey_s_c k; struct bkey_s_c k;
struct bkey_i_reflink_v *r_v; struct bkey_i *n;
__le64 *refcount;
s64 ret; s64 ret;
ret = trans_get_key(trans, BTREE_ID_REFLINK, ret = trans_get_key(trans, BTREE_ID_REFLINK,
...@@ -1827,14 +1840,6 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans, ...@@ -1827,14 +1840,6 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
if (ret < 0) if (ret < 0)
return ret; return ret;
if (k.k->type != KEY_TYPE_reflink_v) {
bch2_fs_inconsistent(c,
"%llu:%llu len %u points to nonexistent indirect extent %llu",
p.k->p.inode, p.k->p.offset, p.k->size, idx);
ret = -EIO;
goto err;
}
if ((flags & BTREE_TRIGGER_OVERWRITE) && if ((flags & BTREE_TRIGGER_OVERWRITE) &&
(bkey_start_offset(k.k) < idx || (bkey_start_offset(k.k) < idx ||
k.k->p.offset > idx + sectors)) k.k->p.offset > idx + sectors))
...@@ -1842,25 +1847,33 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans, ...@@ -1842,25 +1847,33 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
sectors = k.k->p.offset - idx; sectors = k.k->p.offset - idx;
r_v = bch2_trans_kmalloc(trans, bkey_bytes(k.k)); n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
ret = PTR_ERR_OR_ZERO(r_v); ret = PTR_ERR_OR_ZERO(n);
if (ret) if (ret)
goto err; goto err;
bkey_reassemble(&r_v->k_i, k); bkey_reassemble(n, k);
refcount = bkey_refcount(n);
if (!refcount) {
bch2_fs_inconsistent(c,
"%llu:%llu len %u points to nonexistent indirect extent %llu",
p.k->p.inode, p.k->p.offset, p.k->size, idx);
ret = -EIO;
goto err;
}
le64_add_cpu(&r_v->v.refcount, le64_add_cpu(refcount, !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1);
!(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1);
if (!r_v->v.refcount) { if (!*refcount) {
r_v->k.type = KEY_TYPE_deleted; n->k.type = KEY_TYPE_deleted;
set_bkey_val_u64s(&r_v->k, 0); set_bkey_val_u64s(&n->k, 0);
} }
bch2_btree_iter_set_pos(iter, bkey_start_pos(k.k)); bch2_btree_iter_set_pos(iter, bkey_start_pos(k.k));
BUG_ON(iter->uptodate > BTREE_ITER_NEED_PEEK); BUG_ON(iter->uptodate > BTREE_ITER_NEED_PEEK);
bch2_trans_update(trans, iter, &r_v->k_i, 0); bch2_trans_update(trans, iter, n, 0);
out: out:
ret = sectors; ret = sectors;
err: err:
......
...@@ -1199,14 +1199,14 @@ int bch2_cut_front_s(struct bpos where, struct bkey_s k) ...@@ -1199,14 +1199,14 @@ int bch2_cut_front_s(struct bpos where, struct bkey_s k)
le64_add_cpu(&p.v->idx, sub); le64_add_cpu(&p.v->idx, sub);
break; break;
} }
case KEY_TYPE_inline_data: { case KEY_TYPE_inline_data:
struct bkey_s_inline_data d = bkey_s_to_inline_data(k); case KEY_TYPE_indirect_inline_data: {
void *p = bkey_inline_data_p(k);
unsigned bytes = bkey_inline_data_bytes(k.k);
sub = min_t(u64, sub << 9, bkey_val_bytes(d.k)); sub = min_t(u64, sub << 9, bytes);
memmove(d.v->data, memmove(p, p + sub, bytes - sub);
d.v->data + sub,
bkey_val_bytes(d.k) - sub);
new_val_u64s -= sub >> 3; new_val_u64s -= sub >> 3;
break; break;
...@@ -1244,7 +1244,9 @@ int bch2_cut_back_s(struct bpos where, struct bkey_s k) ...@@ -1244,7 +1244,9 @@ int bch2_cut_back_s(struct bpos where, struct bkey_s k)
switch (k.k->type) { switch (k.k->type) {
case KEY_TYPE_inline_data: case KEY_TYPE_inline_data:
new_val_u64s = min(new_val_u64s, k.k->size << 6); case KEY_TYPE_indirect_inline_data:
new_val_u64s = (bkey_inline_data_offset(k.k) +
min(bkey_inline_data_bytes(k.k), k.k->size << 9)) >> 3;
break; break;
} }
......
...@@ -445,10 +445,35 @@ static inline bool bkey_extent_is_direct_data(const struct bkey *k) ...@@ -445,10 +445,35 @@ static inline bool bkey_extent_is_direct_data(const struct bkey *k)
} }
} }
static inline bool bkey_extent_is_inline_data(const struct bkey *k)
{
return k->type == KEY_TYPE_inline_data ||
k->type == KEY_TYPE_indirect_inline_data;
}
static inline unsigned bkey_inline_data_offset(const struct bkey *k)
{
switch (k->type) {
case KEY_TYPE_inline_data:
return sizeof(struct bch_inline_data);
case KEY_TYPE_indirect_inline_data:
return sizeof(struct bch_indirect_inline_data);
default:
BUG();
}
}
static inline unsigned bkey_inline_data_bytes(const struct bkey *k)
{
return bkey_val_bytes(k) - bkey_inline_data_offset(k);
}
#define bkey_inline_data_p(_k) (((void *) (_k).v) + bkey_inline_data_offset((_k).k))
static inline bool bkey_extent_is_data(const struct bkey *k) static inline bool bkey_extent_is_data(const struct bkey *k)
{ {
return bkey_extent_is_direct_data(k) || return bkey_extent_is_direct_data(k) ||
k->type == KEY_TYPE_inline_data || bkey_extent_is_inline_data(k) ||
k->type == KEY_TYPE_reflink_p; k->type == KEY_TYPE_reflink_p;
} }
...@@ -463,6 +488,7 @@ static inline bool bkey_extent_is_allocation(const struct bkey *k) ...@@ -463,6 +488,7 @@ static inline bool bkey_extent_is_allocation(const struct bkey *k)
case KEY_TYPE_reflink_p: case KEY_TYPE_reflink_p:
case KEY_TYPE_reflink_v: case KEY_TYPE_reflink_v:
case KEY_TYPE_inline_data: case KEY_TYPE_inline_data:
case KEY_TYPE_indirect_inline_data:
return true; return true;
default: default:
return false; return false;
......
...@@ -2000,7 +2000,8 @@ int __bch2_read_indirect_extent(struct btree_trans *trans, ...@@ -2000,7 +2000,8 @@ int __bch2_read_indirect_extent(struct btree_trans *trans,
if (ret) if (ret)
goto err; goto err;
if (k.k->type != KEY_TYPE_reflink_v) { if (k.k->type != KEY_TYPE_reflink_v &&
k.k->type != KEY_TYPE_indirect_inline_data) {
__bcache_io_error(trans->c, __bcache_io_error(trans->c,
"pointer to nonexistent indirect extent"); "pointer to nonexistent indirect extent");
ret = -EIO; ret = -EIO;
...@@ -2027,13 +2028,12 @@ int __bch2_read_extent(struct bch_fs *c, struct bch_read_bio *orig, ...@@ -2027,13 +2028,12 @@ int __bch2_read_extent(struct bch_fs *c, struct bch_read_bio *orig,
struct bpos pos = bkey_start_pos(k.k); struct bpos pos = bkey_start_pos(k.k);
int pick_ret; int pick_ret;
if (k.k->type == KEY_TYPE_inline_data) { if (bkey_extent_is_inline_data(k.k)) {
struct bkey_s_c_inline_data d = bkey_s_c_to_inline_data(k);
unsigned bytes = min_t(unsigned, iter.bi_size, unsigned bytes = min_t(unsigned, iter.bi_size,
bkey_val_bytes(d.k)); bkey_inline_data_bytes(k.k));
swap(iter.bi_size, bytes); swap(iter.bi_size, bytes);
memcpy_to_bio(&orig->bio, iter, d.v->data); memcpy_to_bio(&orig->bio, iter, bkey_inline_data_p(k));
swap(iter.bi_size, bytes); swap(iter.bi_size, bytes);
bio_advance_iter(&orig->bio, &iter, bytes); bio_advance_iter(&orig->bio, &iter, bytes);
zero_fill_bio_iter(&orig->bio, iter); zero_fill_bio_iter(&orig->bio, iter);
......
...@@ -185,7 +185,7 @@ enum opt_type { ...@@ -185,7 +185,7 @@ enum opt_type {
x(inline_data, u8, \ x(inline_data, u8, \
OPT_MOUNT|OPT_RUNTIME, \ OPT_MOUNT|OPT_RUNTIME, \
OPT_BOOL(), \ OPT_BOOL(), \
NO_SB_OPT, false, \ NO_SB_OPT, true, \
NULL, "Enable inline data extents") \ NULL, "Enable inline data extents") \
x(acl, u8, \ x(acl, u8, \
OPT_FORMAT|OPT_MOUNT, \ OPT_FORMAT|OPT_MOUNT, \
......
...@@ -9,6 +9,18 @@ ...@@ -9,6 +9,18 @@
#include <linux/sched/signal.h> #include <linux/sched/signal.h>
static inline unsigned bkey_type_to_indirect(const struct bkey *k)
{
switch (k->type) {
case KEY_TYPE_extent:
return KEY_TYPE_reflink_v;
case KEY_TYPE_inline_data:
return KEY_TYPE_indirect_inline_data;
default:
return 0;
}
}
/* reflink pointers */ /* reflink pointers */
const char *bch2_reflink_p_invalid(const struct bch_fs *c, struct bkey_s_c k) const char *bch2_reflink_p_invalid(const struct bch_fs *c, struct bkey_s_c k)
...@@ -71,17 +83,42 @@ void bch2_reflink_v_to_text(struct printbuf *out, struct bch_fs *c, ...@@ -71,17 +83,42 @@ void bch2_reflink_v_to_text(struct printbuf *out, struct bch_fs *c,
bch2_bkey_ptrs_to_text(out, c, k); bch2_bkey_ptrs_to_text(out, c, k);
} }
/* indirect inline data */
const char *bch2_indirect_inline_data_invalid(const struct bch_fs *c,
struct bkey_s_c k)
{
if (bkey_val_bytes(k.k) < sizeof(struct bch_indirect_inline_data))
return "incorrect value size";
return NULL;
}
void bch2_indirect_inline_data_to_text(struct printbuf *out,
struct bch_fs *c, struct bkey_s_c k)
{
struct bkey_s_c_indirect_inline_data d = bkey_s_c_to_indirect_inline_data(k);
unsigned datalen = bkey_inline_data_bytes(k.k);
pr_buf(out, "refcount %llu datalen %u: %*phN",
le64_to_cpu(d.v->refcount), datalen,
min(datalen, 32U), d.v->data);
}
static int bch2_make_extent_indirect(struct btree_trans *trans, static int bch2_make_extent_indirect(struct btree_trans *trans,
struct btree_iter *extent_iter, struct btree_iter *extent_iter,
struct bkey_i_extent *e) struct bkey_i *orig)
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct btree_iter *reflink_iter; struct btree_iter *reflink_iter;
struct bkey_s_c k; struct bkey_s_c k;
struct bkey_i_reflink_v *r_v; struct bkey_i *r_v;
struct bkey_i_reflink_p *r_p; struct bkey_i_reflink_p *r_p;
__le64 *refcount;
int ret; int ret;
if (orig->k.type == KEY_TYPE_inline_data)
bch2_check_set_feature(c, BCH_FEATURE_reflink_inline_data);
for_each_btree_key(trans, reflink_iter, BTREE_ID_REFLINK, for_each_btree_key(trans, reflink_iter, BTREE_ID_REFLINK,
POS(0, c->reflink_hint), POS(0, c->reflink_hint),
BTREE_ITER_INTENT|BTREE_ITER_SLOTS, k, ret) { BTREE_ITER_INTENT|BTREE_ITER_SLOTS, k, ret) {
...@@ -90,7 +127,7 @@ static int bch2_make_extent_indirect(struct btree_trans *trans, ...@@ -90,7 +127,7 @@ static int bch2_make_extent_indirect(struct btree_trans *trans,
continue; continue;
} }
if (bkey_deleted(k.k) && e->k.size <= k.k->size) if (bkey_deleted(k.k) && orig->k.size <= k.k->size)
break; break;
} }
...@@ -100,29 +137,31 @@ static int bch2_make_extent_indirect(struct btree_trans *trans, ...@@ -100,29 +137,31 @@ static int bch2_make_extent_indirect(struct btree_trans *trans,
/* rewind iter to start of hole, if necessary: */ /* rewind iter to start of hole, if necessary: */
bch2_btree_iter_set_pos(reflink_iter, bkey_start_pos(k.k)); bch2_btree_iter_set_pos(reflink_iter, bkey_start_pos(k.k));
r_v = bch2_trans_kmalloc(trans, sizeof(*r_v) + bkey_val_bytes(&e->k)); r_v = bch2_trans_kmalloc(trans, sizeof(__le64) + bkey_val_bytes(&orig->k));
ret = PTR_ERR_OR_ZERO(r_v); ret = PTR_ERR_OR_ZERO(r_v);
if (ret) if (ret)
goto err; goto err;
bkey_reflink_v_init(&r_v->k_i); bkey_init(&r_v->k);
r_v->k.type = bkey_type_to_indirect(&orig->k);
r_v->k.p = reflink_iter->pos; r_v->k.p = reflink_iter->pos;
bch2_key_resize(&r_v->k, e->k.size); bch2_key_resize(&r_v->k, orig->k.size);
r_v->k.version = e->k.version; r_v->k.version = orig->k.version;
set_bkey_val_bytes(&r_v->k, sizeof(__le64) + bkey_val_bytes(&orig->k));
set_bkey_val_u64s(&r_v->k, bkey_val_u64s(&r_v->k) + refcount = (void *) &r_v->v;
bkey_val_u64s(&e->k)); *refcount = 0;
r_v->v.refcount = 0; memcpy(refcount + 1, &orig->v, bkey_val_bytes(&orig->k));
memcpy(r_v->v.start, e->v.start, bkey_val_bytes(&e->k));
bch2_trans_update(trans, reflink_iter, &r_v->k_i, 0); bch2_trans_update(trans, reflink_iter, r_v, 0);
r_p = bch2_trans_kmalloc(trans, sizeof(*r_p)); r_p = bch2_trans_kmalloc(trans, sizeof(*r_p));
if (IS_ERR(r_p)) if (IS_ERR(r_p))
return PTR_ERR(r_p); return PTR_ERR(r_p);
e->k.type = KEY_TYPE_reflink_p; orig->k.type = KEY_TYPE_reflink_p;
r_p = bkey_i_to_reflink_p(&e->k_i); r_p = bkey_i_to_reflink_p(orig);
set_bkey_val_bytes(&r_p->k, sizeof(r_p->v)); set_bkey_val_bytes(&r_p->k, sizeof(r_p->v));
r_p->v.idx = cpu_to_le64(bkey_start_offset(&r_v->k)); r_p->v.idx = cpu_to_le64(bkey_start_offset(&r_v->k));
...@@ -144,8 +183,7 @@ static struct bkey_s_c get_next_src(struct btree_iter *iter, struct bpos end) ...@@ -144,8 +183,7 @@ static struct bkey_s_c get_next_src(struct btree_iter *iter, struct bpos end)
if (bkey_cmp(iter->pos, end) >= 0) if (bkey_cmp(iter->pos, end) >= 0)
return bkey_s_c_null; return bkey_s_c_null;
if (k.k->type == KEY_TYPE_extent || if (bkey_extent_is_data(k.k))
k.k->type == KEY_TYPE_reflink_p)
break; break;
} }
...@@ -218,7 +256,7 @@ s64 bch2_remap_range(struct bch_fs *c, ...@@ -218,7 +256,7 @@ s64 bch2_remap_range(struct bch_fs *c,
if (!bkey_cmp(dst_iter->pos, dst_end)) if (!bkey_cmp(dst_iter->pos, dst_end))
break; break;
if (src_k.k->type == KEY_TYPE_extent) { if (src_k.k->type != KEY_TYPE_reflink_p) {
bkey_on_stack_reassemble(&new_src, c, src_k); bkey_on_stack_reassemble(&new_src, c, src_k);
src_k = bkey_i_to_s_c(new_src.k); src_k = bkey_i_to_s_c(new_src.k);
...@@ -226,7 +264,7 @@ s64 bch2_remap_range(struct bch_fs *c, ...@@ -226,7 +264,7 @@ s64 bch2_remap_range(struct bch_fs *c,
bch2_cut_back(src_end, new_src.k); bch2_cut_back(src_end, new_src.k);
ret = bch2_make_extent_indirect(&trans, src_iter, ret = bch2_make_extent_indirect(&trans, src_iter,
bkey_i_to_extent(new_src.k)); new_src.k);
if (ret) if (ret)
goto btree_err; goto btree_err;
......
...@@ -18,13 +18,22 @@ const char *bch2_reflink_v_invalid(const struct bch_fs *, struct bkey_s_c); ...@@ -18,13 +18,22 @@ const char *bch2_reflink_v_invalid(const struct bch_fs *, struct bkey_s_c);
void bch2_reflink_v_to_text(struct printbuf *, struct bch_fs *, void bch2_reflink_v_to_text(struct printbuf *, struct bch_fs *,
struct bkey_s_c); struct bkey_s_c);
#define bch2_bkey_ops_reflink_v (struct bkey_ops) { \ #define bch2_bkey_ops_reflink_v (struct bkey_ops) { \
.key_invalid = bch2_reflink_v_invalid, \ .key_invalid = bch2_reflink_v_invalid, \
.val_to_text = bch2_reflink_v_to_text, \ .val_to_text = bch2_reflink_v_to_text, \
.swab = bch2_ptr_swab, \ .swab = bch2_ptr_swab, \
} }
const char *bch2_indirect_inline_data_invalid(const struct bch_fs *,
struct bkey_s_c);
void bch2_indirect_inline_data_to_text(struct printbuf *,
struct bch_fs *, struct bkey_s_c);
#define bch2_bkey_ops_indirect_inline_data (struct bkey_ops) { \
.key_invalid = bch2_indirect_inline_data_invalid, \
.val_to_text = bch2_indirect_inline_data_to_text, \
}
s64 bch2_remap_range(struct bch_fs *, struct bpos, struct bpos, s64 bch2_remap_range(struct bch_fs *, struct bpos, struct bpos,
u64, u64 *, u64, s64 *); u64, u64 *, u64, s64 *);
......
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