Commit 9bcbc030 authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: Skip inode unpack/pack in bch2_extent_update()

This takes advantage of the new inode type to skip the expensive
pack/unpack when inode updates are required in the extent update path.
Additionally, we now skip the inode update entirely when i_sectors and
i_size aren't changing.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent b08b492e
...@@ -252,13 +252,13 @@ int bch2_extent_update(struct btree_trans *trans, ...@@ -252,13 +252,13 @@ int bch2_extent_update(struct btree_trans *trans,
s64 *i_sectors_delta_total, s64 *i_sectors_delta_total,
bool check_enospc) bool check_enospc)
{ {
/* this must live until after bch2_trans_commit(): */
struct bkey_inode_buf inode_p;
struct btree_iter inode_iter = { NULL }; struct btree_iter inode_iter = { NULL };
struct bch_inode_unpacked inode_u; struct bkey_s_c inode_k;
struct bkey_s_c_inode_v3 inode;
struct bkey_i_inode_v3 *new_inode;
struct bpos next_pos; struct bpos next_pos;
struct bkey_s_c inode;
bool usage_increasing; bool usage_increasing;
unsigned inode_update_flags = BTREE_UPDATE_NOJOURNAL;
s64 i_sectors_delta = 0, disk_sectors_delta = 0; s64 i_sectors_delta = 0, disk_sectors_delta = 0;
int ret; int ret;
...@@ -298,64 +298,66 @@ int bch2_extent_update(struct btree_trans *trans, ...@@ -298,64 +298,66 @@ int bch2_extent_update(struct btree_trans *trans,
bch2_trans_iter_init(trans, &inode_iter, BTREE_ID_inodes, bch2_trans_iter_init(trans, &inode_iter, BTREE_ID_inodes,
SPOS(0, inum.inum, iter->snapshot), SPOS(0, inum.inum, iter->snapshot),
BTREE_ITER_INTENT| BTREE_ITER_INTENT|BTREE_ITER_CACHED);
(trans->c->opts.inodes_use_key_cache inode_k = bch2_btree_iter_peek_slot(&inode_iter);
? BTREE_ITER_CACHED ret = bkey_err(inode_k);
: 0)); if (unlikely(ret))
inode = bch2_btree_iter_peek_slot(&inode_iter);
ret = bkey_err(inode);
if (ret)
goto err; goto err;
ret = bkey_is_inode(inode.k) ? 0 : -ENOENT; ret = bkey_is_inode(inode_k.k) ? 0 : -ENOENT;
if (ret) if (unlikely(ret))
goto err; goto err;
if (i_sectors_delta || new_i_size) { if (unlikely(inode_k.k->type != KEY_TYPE_inode_v3)) {
ret = bch2_inode_unpack(inode, &inode_u); inode_k = bch2_inode_to_v3(trans, inode_k);
if (ret) ret = bkey_err(inode_k);
if (unlikely(ret))
goto err; goto err;
if (!(inode_u.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
new_i_size > inode_u.bi_size)
inode_u.bi_size = new_i_size;
else
new_i_size = 0;
inode_u.bi_sectors += i_sectors_delta;
} }
if (i_sectors_delta || new_i_size) { inode = bkey_s_c_to_inode_v3(inode_k);
bch2_inode_pack(&inode_p, &inode_u);
inode_p.inode.k.p.snapshot = iter->snapshot; new_inode = bch2_trans_kmalloc(trans, bkey_bytes(inode_k.k));
ret = PTR_ERR_OR_ZERO(new_inode);
if (unlikely(ret))
goto err;
ret = bch2_trans_update(trans, &inode_iter, bkey_reassemble(&new_inode->k_i, inode.s_c);
&inode_p.inode.k_i, 0);
} else {
bkey_reassemble(&inode_p.inode.k_i, inode);
ret = bch2_trans_update(trans, &inode_iter, if (!(le64_to_cpu(inode.v->bi_flags) & BCH_INODE_I_SIZE_DIRTY) &&
&inode_p.inode.k_i, new_i_size > le64_to_cpu(inode.v->bi_size)) {
BTREE_UPDATE_NOJOURNAL); new_inode->v.bi_size = cpu_to_le64(new_i_size);
if (ret) inode_update_flags = 0;
goto err;
} }
ret = bch2_trans_update(trans, iter, k, 0) ?: if (i_sectors_delta) {
le64_add_cpu(&new_inode->v.bi_sectors, i_sectors_delta);
inode_update_flags = 0;
}
new_inode->k.p.snapshot = iter->snapshot;
/*
* Note:
* We always have to do an inode updated - even when i_size/i_sectors
* aren't changing - for fsync to work properly; fsync relies on
* inode->bi_journal_seq which is updated by the trigger code:
*/
ret = bch2_trans_update(trans, &inode_iter, &new_inode->k_i,
inode_update_flags) ?:
bch2_trans_update(trans, iter, k, 0) ?:
bch2_trans_commit(trans, disk_res, NULL, bch2_trans_commit(trans, disk_res, NULL,
BTREE_INSERT_NOCHECK_RW| BTREE_INSERT_NOCHECK_RW|
BTREE_INSERT_NOFAIL); BTREE_INSERT_NOFAIL);
err: if (unlikely(ret))
bch2_trans_iter_exit(trans, &inode_iter); goto err;
if (ret)
return ret;
if (i_sectors_delta_total) if (i_sectors_delta_total)
*i_sectors_delta_total += i_sectors_delta; *i_sectors_delta_total += i_sectors_delta;
bch2_btree_iter_set_pos(iter, next_pos); bch2_btree_iter_set_pos(iter, next_pos);
err:
return 0; bch2_trans_iter_exit(trans, &inode_iter);
return ret;
} }
/* /*
......
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