Update bcachefs sources to 13bb4cdf89 bcachefs: Indirect inline data extents

This commit is contained in:
Kent Overstreet 2020-10-24 21:00:59 -04:00
parent 37270fc79c
commit 96f4a81a01
16 changed files with 203 additions and 80 deletions

View File

@ -1 +1 @@
ff83eed5f5f7083d7d111979f68239fe8efd100e
13bb4cdf89c91a1e50d2416ede10c1f9cbaf0202

View File

@ -55,7 +55,7 @@ int cmd_fsck(int argc, char *argv[])
/* force check, even if filesystem marked clean: */
break;
case 'o':
ret = bch2_parse_mount_opts(&opts, optarg);
ret = bch2_parse_mount_opts(NULL, &opts, optarg);
if (ret)
return ret;
break;

View File

@ -340,7 +340,8 @@ static inline void bkey_init(struct bkey *k)
x(reflink_p, 15) \
x(reflink_v, 16) \
x(inline_data, 17) \
x(btree_ptr_v2, 18)
x(btree_ptr_v2, 18) \
x(indirect_inline_data, 19)
enum bch_bkey_type {
#define x(name, nr) KEY_TYPE_##name = nr,
@ -886,6 +887,12 @@ struct bch_reflink_v {
__u64 _data[0];
};
struct bch_indirect_inline_data {
struct bch_val v;
__le64 refcount;
u8 data[0];
};
/* Inline data */
struct bch_inline_data {
@ -1322,7 +1329,8 @@ LE64_BITMASK(BCH_SB_ERASURE_CODE, struct bch_sb, flags[3], 0, 16);
x(incompressible, 10) \
x(btree_ptr_v2, 11) \
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 \
((1ULL << BCH_FEATURE_new_siphash)| \

View File

@ -565,6 +565,7 @@ BKEY_VAL_ACCESSORS(reflink_p);
BKEY_VAL_ACCESSORS(reflink_v);
BKEY_VAL_ACCESSORS(inline_data);
BKEY_VAL_ACCESSORS(btree_ptr_v2);
BKEY_VAL_ACCESSORS(indirect_inline_data);
/* byte order helpers */

View File

@ -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,
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) { \

View File

@ -750,7 +750,9 @@ static int validate_bset(struct bch_fs *c, struct btree *b,
btree_err_on(bkey_cmp(bn->max_key, b->key.k.p),
BTREE_ERR_MUST_RETRY, c, b, i,
"incorrect max key");
"incorrect max key %llu:%llu",
bn->max_key.inode,
bn->max_key.offset);
if (write)
compat_btree_node(b->c.level, b->c.btree_id, version,
@ -930,7 +932,8 @@ int bch2_btree_node_read_done(struct bch_fs *c, struct btree *b, bool have_retry
btree_err_on(!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i)),
BTREE_ERR_WANT_RETRY, c, b, i,
"unknown checksum type");
"unknown checksum type %llu",
BSET_CSUM_TYPE(i));
nonce = btree_nonce(i, b->written << 9);
csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, b->data);
@ -957,7 +960,8 @@ int bch2_btree_node_read_done(struct bch_fs *c, struct btree *b, bool have_retry
btree_err_on(!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i)),
BTREE_ERR_WANT_RETRY, c, b, i,
"unknown checksum type");
"unknown checksum type %llu",
BSET_CSUM_TYPE(i));
nonce = btree_nonce(i, b->written << 9);
csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, bne);

View File

@ -1812,6 +1812,18 @@ put_iter:
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,
struct bkey_s_c_reflink_p p,
u64 idx, unsigned sectors,
@ -1820,7 +1832,8 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
struct bch_fs *c = trans->c;
struct btree_iter *iter;
struct bkey_s_c k;
struct bkey_i_reflink_v *r_v;
struct bkey_i *n;
__le64 *refcount;
s64 ret;
ret = trans_get_key(trans, BTREE_ID_REFLINK,
@ -1828,14 +1841,6 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
if (ret < 0)
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) &&
(bkey_start_offset(k.k) < idx ||
k.k->p.offset > idx + sectors))
@ -1843,25 +1848,33 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
sectors = k.k->p.offset - idx;
r_v = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
ret = PTR_ERR_OR_ZERO(r_v);
n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
ret = PTR_ERR_OR_ZERO(n);
if (ret)
goto err;
bkey_reassemble(&r_v->k_i, k);
bkey_reassemble(n, k);
le64_add_cpu(&r_v->v.refcount,
!(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1);
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;
}
if (!r_v->v.refcount) {
r_v->k.type = KEY_TYPE_deleted;
set_bkey_val_u64s(&r_v->k, 0);
le64_add_cpu(refcount, !(flags & BTREE_TRIGGER_OVERWRITE) ? 1 : -1);
if (!*refcount) {
n->k.type = KEY_TYPE_deleted;
set_bkey_val_u64s(&n->k, 0);
}
bch2_btree_iter_set_pos(iter, bkey_start_pos(k.k));
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:
ret = sectors;
err:

View File

@ -1200,14 +1200,14 @@ int bch2_cut_front_s(struct bpos where, struct bkey_s k)
le64_add_cpu(&p.v->idx, sub);
break;
}
case KEY_TYPE_inline_data: {
struct bkey_s_inline_data d = bkey_s_to_inline_data(k);
case KEY_TYPE_inline_data:
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,
d.v->data + sub,
bkey_val_bytes(d.k) - sub);
memmove(p, p + sub, bytes - sub);
new_val_u64s -= sub >> 3;
break;
@ -1245,7 +1245,9 @@ int bch2_cut_back_s(struct bpos where, struct bkey_s k)
switch (k.k->type) {
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;
}

View File

@ -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)
{
return bkey_extent_is_direct_data(k) ||
k->type == KEY_TYPE_inline_data ||
return bkey_extent_is_direct_data(k) ||
bkey_extent_is_inline_data(k) ||
k->type == KEY_TYPE_reflink_p;
}
@ -463,6 +488,7 @@ static inline bool bkey_extent_is_allocation(const struct bkey *k)
case KEY_TYPE_reflink_p:
case KEY_TYPE_reflink_v:
case KEY_TYPE_inline_data:
case KEY_TYPE_indirect_inline_data:
return true;
default:
return false;

View File

@ -839,18 +839,19 @@ retry:
if (ret)
break;
bkey_on_stack_reassemble(&sk, c, k);
k = bkey_i_to_s_c(sk.k);
offset_into_extent = iter->pos.offset -
bkey_start_offset(k.k);
sectors = k.k->size - offset_into_extent;
bkey_on_stack_reassemble(&sk, c, k);
ret = bch2_read_indirect_extent(trans,
&offset_into_extent, &sk);
if (ret)
break;
k = bkey_i_to_s_c(sk.k);
sectors = min(sectors, k.k->size - offset_into_extent);
bch2_trans_unlock(trans);

View File

@ -887,20 +887,21 @@ retry:
continue;
}
bkey_on_stack_realloc(&cur, c, k.k->u64s);
bkey_on_stack_realloc(&prev, c, k.k->u64s);
bkey_reassemble(cur.k, k);
k = bkey_i_to_s_c(cur.k);
offset_into_extent = iter->pos.offset -
bkey_start_offset(k.k);
sectors = k.k->size - offset_into_extent;
bkey_on_stack_realloc(&cur, c, k.k->u64s);
bkey_on_stack_realloc(&prev, c, k.k->u64s);
bkey_reassemble(cur.k, k);
ret = bch2_read_indirect_extent(&trans,
&offset_into_extent, &cur);
if (ret)
break;
k = bkey_i_to_s_c(cur.k);
sectors = min(sectors, k.k->size - offset_into_extent);
if (offset_into_extent)
@ -1321,7 +1322,7 @@ static int bch2_remount(struct super_block *sb, int *flags, char *data)
opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
ret = bch2_parse_mount_opts(&opts, data);
ret = bch2_parse_mount_opts(c, &opts, data);
if (ret)
return ret;
@ -1462,7 +1463,7 @@ static struct dentry *bch2_mount(struct file_system_type *fs_type,
opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
ret = bch2_parse_mount_opts(&opts, data);
ret = bch2_parse_mount_opts(NULL, &opts, data);
if (ret)
return ERR_PTR(ret);
@ -1485,11 +1486,24 @@ static struct dentry *bch2_mount(struct file_system_type *fs_type,
goto got_sb;
c = bch2_fs_open(devs, nr_devs, opts);
if (!IS_ERR(c))
sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
else
if (IS_ERR(c)) {
sb = ERR_CAST(c);
goto got_sb;
}
/* Some options can't be parsed until after the fs is started: */
ret = bch2_parse_mount_opts(c, &opts, data);
if (ret) {
bch2_fs_stop(c);
sb = ERR_PTR(ret);
goto got_sb;
}
bch2_opts_apply(&c->opts, opts);
sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
if (IS_ERR(sb))
bch2_fs_stop(c);
got_sb:
kfree(devs_to_fs);
kfree(devs[0]);

View File

@ -1676,7 +1676,6 @@ retry:
unsigned bytes, sectors, offset_into_extent;
bkey_on_stack_reassemble(&sk, c, k);
k = bkey_i_to_s_c(sk.k);
offset_into_extent = iter->pos.offset -
bkey_start_offset(k.k);
@ -1687,6 +1686,8 @@ retry:
if (ret)
break;
k = bkey_i_to_s_c(sk.k);
sectors = min(sectors, k.k->size - offset_into_extent);
bch2_trans_unlock(&trans);
@ -2008,7 +2009,8 @@ int __bch2_read_indirect_extent(struct btree_trans *trans,
if (ret)
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,
"pointer to nonexistent indirect extent");
ret = -EIO;
@ -2036,13 +2038,12 @@ int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
struct bpos pos = bkey_start_pos(k.k);
int pick_ret;
if (k.k->type == KEY_TYPE_inline_data) {
struct bkey_s_c_inline_data d = bkey_s_c_to_inline_data(k);
if (bkey_extent_is_inline_data(k.k)) {
unsigned bytes = min_t(unsigned, iter.bi_size,
bkey_val_bytes(d.k));
bkey_inline_data_bytes(k.k));
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);
bio_advance_iter(&orig->bio, &iter, bytes);
zero_fill_bio_iter(&orig->bio, iter);
@ -2314,13 +2315,14 @@ retry:
sectors = k.k->size - offset_into_extent;
bkey_on_stack_reassemble(&sk, c, k);
k = bkey_i_to_s_c(sk.k);
ret = bch2_read_indirect_extent(&trans,
&offset_into_extent, &sk);
if (ret)
goto err;
k = bkey_i_to_s_c(sk.k);
/*
* With indirect extents, the amount of data to read is the min
* of the original extent and the indirect extent:

View File

@ -247,7 +247,7 @@ int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt,
break;
case BCH_OPT_FN:
if (!c)
return -EINVAL;
return 0;
return opt->parse(c, val, res);
}
@ -325,7 +325,8 @@ int bch2_opts_check_may_set(struct bch_fs *c)
return 0;
}
int bch2_parse_mount_opts(struct bch_opts *opts, char *options)
int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
char *options)
{
char *opt, *name, *val;
int ret, id;
@ -340,7 +341,7 @@ int bch2_parse_mount_opts(struct bch_opts *opts, char *options)
if (id < 0)
goto bad_opt;
ret = bch2_opt_parse(NULL, &bch2_opt_table[id], val, &v);
ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v);
if (ret < 0)
goto bad_val;
} else {

View File

@ -185,7 +185,7 @@ enum opt_type {
x(inline_data, u8, \
OPT_MOUNT|OPT_RUNTIME, \
OPT_BOOL(), \
NO_SB_OPT, false, \
NO_SB_OPT, true, \
NULL, "Enable inline data extents") \
x(acl, u8, \
OPT_FORMAT|OPT_MOUNT, \
@ -418,7 +418,7 @@ void bch2_opt_to_text(struct printbuf *, struct bch_fs *,
int bch2_opt_check_may_set(struct bch_fs *, int, u64);
int bch2_opts_check_may_set(struct bch_fs *);
int bch2_parse_mount_opts(struct bch_opts *, char *);
int bch2_parse_mount_opts(struct bch_fs *, struct bch_opts *, char *);
/* inode opts: */

View File

@ -9,6 +9,18 @@
#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 */
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,
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,
struct btree_iter *extent_iter,
struct bkey_i_extent *e)
struct bkey_i *orig)
{
struct bch_fs *c = trans->c;
struct btree_iter *reflink_iter;
struct bkey_s_c k;
struct bkey_i_reflink_v *r_v;
struct bkey_i *r_v;
struct bkey_i_reflink_p *r_p;
__le64 *refcount;
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,
POS(0, c->reflink_hint),
BTREE_ITER_INTENT|BTREE_ITER_SLOTS, k, ret) {
@ -90,7 +127,7 @@ static int bch2_make_extent_indirect(struct btree_trans *trans,
continue;
}
if (bkey_deleted(k.k) && e->k.size <= k.k->size)
if (bkey_deleted(k.k) && orig->k.size <= k.k->size)
break;
}
@ -100,29 +137,31 @@ static int bch2_make_extent_indirect(struct btree_trans *trans,
/* rewind iter to start of hole, if necessary: */
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);
if (ret)
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;
bch2_key_resize(&r_v->k, e->k.size);
r_v->k.version = e->k.version;
bch2_key_resize(&r_v->k, orig->k.size);
r_v->k.version = orig->k.version;
set_bkey_val_u64s(&r_v->k, bkey_val_u64s(&r_v->k) +
bkey_val_u64s(&e->k));
r_v->v.refcount = 0;
memcpy(r_v->v.start, e->v.start, bkey_val_bytes(&e->k));
set_bkey_val_bytes(&r_v->k, sizeof(__le64) + bkey_val_bytes(&orig->k));
bch2_trans_update(trans, reflink_iter, &r_v->k_i, 0);
refcount = (void *) &r_v->v;
*refcount = 0;
memcpy(refcount + 1, &orig->v, bkey_val_bytes(&orig->k));
bch2_trans_update(trans, reflink_iter, r_v, 0);
r_p = bch2_trans_kmalloc(trans, sizeof(*r_p));
if (IS_ERR(r_p))
return PTR_ERR(r_p);
e->k.type = KEY_TYPE_reflink_p;
r_p = bkey_i_to_reflink_p(&e->k_i);
orig->k.type = KEY_TYPE_reflink_p;
r_p = bkey_i_to_reflink_p(orig);
set_bkey_val_bytes(&r_p->k, sizeof(r_p->v));
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)
if (bkey_cmp(iter->pos, end) >= 0)
return bkey_s_c_null;
if (k.k->type == KEY_TYPE_extent ||
k.k->type == KEY_TYPE_reflink_p)
if (bkey_extent_is_data(k.k))
break;
}
@ -218,7 +256,7 @@ s64 bch2_remap_range(struct bch_fs *c,
if (!bkey_cmp(dst_iter->pos, dst_end))
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);
src_k = bkey_i_to_s_c(new_src.k);
@ -226,7 +264,7 @@ s64 bch2_remap_range(struct bch_fs *c,
bch2_cut_back(src_end, new_src.k);
ret = bch2_make_extent_indirect(&trans, src_iter,
bkey_i_to_extent(new_src.k));
new_src.k);
if (ret)
goto btree_err;

View File

@ -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 *,
struct bkey_s_c);
#define bch2_bkey_ops_reflink_v (struct bkey_ops) { \
.key_invalid = bch2_reflink_v_invalid, \
.val_to_text = bch2_reflink_v_to_text, \
.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,
u64, u64 *, u64, s64 *);