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

bcachefs: Convert all bch2_trans_commit() users to BTREE_INSERT_ATOMIC

BTREE_INSERT_ATOMIC should really be the default mode, and there's not
that much code that doesn't need it - so this is prep work for getting
rid of the flag.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent a8abd3a7
...@@ -131,24 +131,34 @@ static inline void bch2_trans_update(struct btree_trans *trans, ...@@ -131,24 +131,34 @@ static inline void bch2_trans_update(struct btree_trans *trans,
}; };
} }
#define bch2_trans_do(_c, _journal_seq, _flags, _do) \ #define __bch2_trans_do(_trans, _disk_res, _journal_seq, \
_flags, _reset_flags, _do) \
({ \ ({ \
struct btree_trans trans; \
int _ret; \ int _ret; \
\ \
bch2_trans_init(&trans, (_c), 0, 0); \
\
do { \ do { \
bch2_trans_begin(&trans); \ bch2_trans_reset(_trans, _reset_flags); \
\ \
_ret = (_do) ?: bch2_trans_commit(&trans, NULL, \ _ret = (_do) ?: bch2_trans_commit(_trans, (_disk_res), \
(_journal_seq), (_flags)); \ (_journal_seq), (_flags)); \
} while (_ret == -EINTR); \ } while (_ret == -EINTR); \
\ \
bch2_trans_exit(&trans); \
_ret; \ _ret; \
}) })
#define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do) \
({ \
struct btree_trans trans; \
int _ret, _ret2; \
\
bch2_trans_init(&trans, (_c), 0, 0); \
_ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
TRANS_RESET_MEM|TRANS_RESET_ITERS, _do); \
_ret2 = bch2_trans_exit(&trans); \
\
_ret ?: _ret2; \
})
#define trans_for_each_update(_trans, _i) \ #define trans_for_each_update(_trans, _i) \
for ((_i) = (_trans)->updates; \ for ((_i) = (_trans)->updates; \
(_i) < (_trans)->updates + (_trans)->nr_updates; \ (_i) < (_trans)->updates + (_trans)->nr_updates; \
......
...@@ -823,6 +823,20 @@ int __bch2_trans_commit(struct btree_trans *trans) ...@@ -823,6 +823,20 @@ int __bch2_trans_commit(struct btree_trans *trans)
goto retry; goto retry;
} }
static int __bch2_btree_insert(struct btree_trans *trans,
enum btree_id id, struct bkey_i *k)
{
struct btree_iter *iter;
iter = bch2_trans_get_iter(trans, id, bkey_start_pos(&k->k),
BTREE_ITER_INTENT);
if (IS_ERR(iter))
return PTR_ERR(iter);
bch2_trans_update(trans, iter, k);
return 0;
}
/** /**
* bch2_btree_insert - insert keys into the extent btree * bch2_btree_insert - insert keys into the extent btree
* @c: pointer to struct bch_fs * @c: pointer to struct bch_fs
...@@ -831,29 +845,12 @@ int __bch2_trans_commit(struct btree_trans *trans) ...@@ -831,29 +845,12 @@ int __bch2_trans_commit(struct btree_trans *trans)
* @hook: insert callback * @hook: insert callback
*/ */
int bch2_btree_insert(struct bch_fs *c, enum btree_id id, int bch2_btree_insert(struct bch_fs *c, enum btree_id id,
struct bkey_i *k, struct bkey_i *k,
struct disk_reservation *disk_res, struct disk_reservation *disk_res,
u64 *journal_seq, int flags) u64 *journal_seq, int flags)
{ {
struct btree_trans trans; return bch2_trans_do(c, disk_res, journal_seq, flags,
struct btree_iter *iter; __bch2_btree_insert(&trans, id, k));
int ret;
bch2_trans_init(&trans, c, 0, 0);
retry:
bch2_trans_begin(&trans);
iter = bch2_trans_get_iter(&trans, id, bkey_start_pos(&k->k),
BTREE_ITER_INTENT);
bch2_trans_update(&trans, iter, k);
ret = bch2_trans_commit(&trans, disk_res, journal_seq, flags);
if (ret == -EINTR)
goto retry;
bch2_trans_exit(&trans);
return ret;
} }
int bch2_btree_delete_at_range(struct btree_trans *trans, int bch2_btree_delete_at_range(struct btree_trans *trans,
......
...@@ -281,18 +281,6 @@ int bch2_dirent_delete_at(struct btree_trans *trans, ...@@ -281,18 +281,6 @@ int bch2_dirent_delete_at(struct btree_trans *trans,
hash_info, iter); hash_info, iter);
} }
int bch2_dirent_delete(struct bch_fs *c, u64 dir_inum,
const struct bch_hash_info *hash_info,
const struct qstr *name,
u64 *journal_seq)
{
return bch2_trans_do(c, journal_seq,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOFAIL,
bch2_hash_delete(&trans, bch2_dirent_hash_desc, hash_info,
dir_inum, name));
}
struct btree_iter * struct btree_iter *
__bch2_dirent_lookup_trans(struct btree_trans *trans, u64 dir_inum, __bch2_dirent_lookup_trans(struct btree_trans *trans, u64 dir_inum,
const struct bch_hash_info *hash_info, const struct bch_hash_info *hash_info,
......
...@@ -36,8 +36,6 @@ int bch2_dirent_create(struct btree_trans *, u64, ...@@ -36,8 +36,6 @@ int bch2_dirent_create(struct btree_trans *, u64,
int bch2_dirent_delete_at(struct btree_trans *, int bch2_dirent_delete_at(struct btree_trans *,
const struct bch_hash_info *, const struct bch_hash_info *,
struct btree_iter *); struct btree_iter *);
int bch2_dirent_delete(struct bch_fs *, u64, const struct bch_hash_info *,
const struct qstr *, u64 *);
enum bch_rename_mode { enum bch_rename_mode {
BCH_RENAME, BCH_RENAME,
......
...@@ -1235,6 +1235,7 @@ static int __bch2_stripe_write_key(struct btree_trans *trans, ...@@ -1235,6 +1235,7 @@ static int __bch2_stripe_write_key(struct btree_trans *trans,
bch2_trans_update(trans, iter, &new_key->k_i); bch2_trans_update(trans, iter, &new_key->k_i);
return bch2_trans_commit(trans, NULL, NULL, return bch2_trans_commit(trans, NULL, NULL,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOFAIL|flags); BTREE_INSERT_NOFAIL|flags);
} }
...@@ -1259,8 +1260,13 @@ int bch2_stripes_write(struct bch_fs *c, unsigned flags, bool *wrote) ...@@ -1259,8 +1260,13 @@ int bch2_stripes_write(struct bch_fs *c, unsigned flags, bool *wrote)
if (!m->dirty) if (!m->dirty)
continue; continue;
ret = __bch2_stripe_write_key(&trans, iter, m, giter.pos, do {
new_key, flags); bch2_trans_reset(&trans, TRANS_RESET_MEM);
ret = __bch2_stripe_write_key(&trans, iter, m,
giter.pos, new_key, flags);
} while (ret == -EINTR);
if (ret) if (ret)
break; break;
......
...@@ -2465,9 +2465,7 @@ static long bchfs_fcollapse_finsert(struct bch_inode_info *inode, ...@@ -2465,9 +2465,7 @@ static long bchfs_fcollapse_finsert(struct bch_inode_info *inode,
struct bpos next_pos; struct bpos next_pos;
struct bpos move_pos = POS(inode->v.i_ino, offset >> 9); struct bpos move_pos = POS(inode->v.i_ino, offset >> 9);
struct bpos atomic_end; struct bpos atomic_end;
unsigned commit_flags = BTREE_INSERT_NOFAIL| unsigned commit_flags = 0;
BTREE_INSERT_ATOMIC|
BTREE_INSERT_USE_RESERVE;
k = insert k = insert
? bch2_btree_iter_peek_prev(src) ? bch2_btree_iter_peek_prev(src)
...@@ -2560,6 +2558,8 @@ static long bchfs_fcollapse_finsert(struct bch_inode_info *inode, ...@@ -2560,6 +2558,8 @@ static long bchfs_fcollapse_finsert(struct bch_inode_info *inode,
ret = bch2_trans_commit(&trans, &disk_res, ret = bch2_trans_commit(&trans, &disk_res,
&inode->ei_journal_seq, &inode->ei_journal_seq,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOFAIL|
commit_flags); commit_flags);
bch2_disk_reservation_put(c, &disk_res); bch2_disk_reservation_put(c, &disk_res);
bkey_err: bkey_err:
......
...@@ -37,8 +37,8 @@ static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum) ...@@ -37,8 +37,8 @@ static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
return ret ?: sectors; return ret ?: sectors;
} }
static int remove_dirent(struct btree_trans *trans, static int __remove_dirent(struct btree_trans *trans,
struct bkey_s_c_dirent dirent) struct bkey_s_c_dirent dirent)
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct qstr name; struct qstr name;
...@@ -49,31 +49,41 @@ static int remove_dirent(struct btree_trans *trans, ...@@ -49,31 +49,41 @@ static int remove_dirent(struct btree_trans *trans,
char *buf; char *buf;
name.len = bch2_dirent_name_bytes(dirent); name.len = bch2_dirent_name_bytes(dirent);
buf = kmalloc(name.len + 1, GFP_KERNEL); buf = bch2_trans_kmalloc(trans, name.len + 1);
if (!buf) if (IS_ERR(buf))
return -ENOMEM; return PTR_ERR(buf);
memcpy(buf, dirent.v->d_name, name.len); memcpy(buf, dirent.v->d_name, name.len);
buf[name.len] = '\0'; buf[name.len] = '\0';
name.name = buf; name.name = buf;
/* Unlock so we don't deadlock, after copying name: */ ret = bch2_inode_find_by_inum_trans(trans, dir_inum, &dir_inode);
bch2_trans_unlock(trans); if (ret && ret != -EINTR)
ret = bch2_inode_find_by_inum(c, dir_inum, &dir_inode);
if (ret) {
bch_err(c, "remove_dirent: err %i looking up directory inode", ret); bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
goto err; if (ret)
} return ret;
dir_hash_info = bch2_hash_info_init(c, &dir_inode); dir_hash_info = bch2_hash_info_init(c, &dir_inode);
ret = bch2_dirent_delete(c, dir_inum, &dir_hash_info, &name, NULL); ret = bch2_hash_delete(trans, bch2_dirent_hash_desc,
if (ret) &dir_hash_info, dir_inum, &name);
if (ret && ret != -EINTR)
bch_err(c, "remove_dirent: err %i deleting dirent", ret); bch_err(c, "remove_dirent: err %i deleting dirent", ret);
err: if (ret)
kfree(buf); return ret;
return ret;
return 0;
}
static int remove_dirent(struct btree_trans *trans,
struct bkey_s_c_dirent dirent)
{
return __bch2_trans_do(trans, NULL, NULL,
BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW,
TRANS_RESET_MEM,
__remove_dirent(trans, dirent));
} }
static int reattach_inode(struct bch_fs *c, static int reattach_inode(struct bch_fs *c,
...@@ -88,7 +98,7 @@ static int reattach_inode(struct bch_fs *c, ...@@ -88,7 +98,7 @@ static int reattach_inode(struct bch_fs *c,
snprintf(name_buf, sizeof(name_buf), "%llu", inum); snprintf(name_buf, sizeof(name_buf), "%llu", inum);
name = (struct qstr) QSTR(name_buf); name = (struct qstr) QSTR(name_buf);
ret = bch2_trans_do(c, NULL, ret = bch2_trans_do(c, NULL, NULL,
BTREE_INSERT_ATOMIC| BTREE_INSERT_ATOMIC|
BTREE_INSERT_LAZY_RW, BTREE_INSERT_LAZY_RW,
bch2_link_trans(&trans, lostfound_inode->bi_inum, bch2_link_trans(&trans, lostfound_inode->bi_inum,
...@@ -171,27 +181,27 @@ static int hash_redo_key(const struct bch_hash_desc desc, ...@@ -171,27 +181,27 @@ static int hash_redo_key(const struct bch_hash_desc desc,
struct btree_iter *k_iter, struct bkey_s_c k, struct btree_iter *k_iter, struct bkey_s_c k,
u64 hashed) u64 hashed)
{ {
struct bkey_i delete;
struct bkey_i *tmp; struct bkey_i *tmp;
int ret = 0;
tmp = kmalloc(bkey_bytes(k.k), GFP_KERNEL); bch2_trans_reset(trans, TRANS_RESET_MEM);
if (!tmp)
return -ENOMEM; tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
if (IS_ERR(tmp))
return PTR_ERR(tmp);
bkey_reassemble(tmp, k); bkey_reassemble(tmp, k);
ret = bch2_btree_delete_at(trans, k_iter, 0); bkey_init(&delete.k);
if (ret) delete.k.p = k_iter->pos;
goto err; bch2_trans_update(trans, k_iter, &delete);
bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode, return bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
tmp, BCH_HASH_SET_MUST_CREATE); tmp, BCH_HASH_SET_MUST_CREATE) ?:
ret = bch2_trans_commit(trans, NULL, NULL, bch2_trans_commit(trans, NULL, NULL,
BTREE_INSERT_NOFAIL| BTREE_INSERT_ATOMIC|
BTREE_INSERT_LAZY_RW); BTREE_INSERT_NOFAIL|
err: BTREE_INSERT_LAZY_RW);
kfree(tmp);
return ret;
} }
static int fsck_hash_delete_at(struct btree_trans *trans, static int fsck_hash_delete_at(struct btree_trans *trans,
...@@ -313,9 +323,11 @@ static int hash_check_key(struct btree_trans *trans, ...@@ -313,9 +323,11 @@ static int hash_check_key(struct btree_trans *trans,
"hashed to %llu chain starts at %llu\n%s", "hashed to %llu chain starts at %llu\n%s",
desc.btree_id, k.k->p.offset, desc.btree_id, k.k->p.offset,
hashed, h->chain->pos.offset, hashed, h->chain->pos.offset,
(bch2_bkey_val_to_text(&PBUF(buf), c, (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf))) {
k), buf))) { do {
ret = hash_redo_key(desc, trans, h, k_iter, k, hashed); ret = hash_redo_key(desc, trans, h, k_iter, k, hashed);
} while (ret == -EINTR);
if (ret) { if (ret) {
bch_err(c, "hash_redo_key err %i", ret); bch_err(c, "hash_redo_key err %i", ret);
return ret; return ret;
...@@ -376,11 +388,12 @@ static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h, ...@@ -376,11 +388,12 @@ static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)", if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
buf, strlen(buf), d->v.d_name, len)) { buf, strlen(buf), d->v.d_name, len)) {
bch2_trans_update(trans, iter, &d->k_i); ret = __bch2_trans_do(trans, NULL, NULL,
BTREE_INSERT_ATOMIC|
ret = bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL|
BTREE_INSERT_NOFAIL| BTREE_INSERT_LAZY_RW,
BTREE_INSERT_LAZY_RW); TRANS_RESET_MEM,
(bch2_trans_update(trans, iter, &d->k_i), 0));
if (ret) if (ret)
goto err; goto err;
...@@ -402,8 +415,11 @@ static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h, ...@@ -402,8 +415,11 @@ static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
k->k->p.offset, hash, h->chain->pos.offset, k->k->p.offset, hash, h->chain->pos.offset,
(bch2_bkey_val_to_text(&PBUF(buf), c, (bch2_bkey_val_to_text(&PBUF(buf), c,
*k), buf))) { *k), buf))) {
ret = hash_redo_key(bch2_dirent_hash_desc, trans, do {
h, iter, *k, hash); ret = hash_redo_key(bch2_dirent_hash_desc, trans,
h, iter, *k, hash);
} while (ret == -EINTR);
if (ret) if (ret)
bch_err(c, "hash_redo_key err %i", ret); bch_err(c, "hash_redo_key err %i", ret);
else else
...@@ -646,11 +662,12 @@ static int check_dirents(struct bch_fs *c) ...@@ -646,11 +662,12 @@ static int check_dirents(struct bch_fs *c)
bkey_reassemble(&n->k_i, d.s_c); bkey_reassemble(&n->k_i, d.s_c);
n->v.d_type = mode_to_type(target.bi_mode); n->v.d_type = mode_to_type(target.bi_mode);
bch2_trans_update(&trans, iter, &n->k_i); ret = __bch2_trans_do(&trans, NULL, NULL,
BTREE_INSERT_ATOMIC|
ret = bch2_trans_commit(&trans, NULL, NULL, BTREE_INSERT_NOFAIL|
BTREE_INSERT_NOFAIL| BTREE_INSERT_LAZY_RW,
BTREE_INSERT_LAZY_RW); TRANS_RESET_MEM,
(bch2_trans_update(&trans, iter, &n->k_i), 0));
kfree(n); kfree(n);
if (ret) if (ret)
goto err; goto err;
...@@ -790,7 +807,7 @@ static int check_lostfound(struct bch_fs *c, ...@@ -790,7 +807,7 @@ static int check_lostfound(struct bch_fs *c,
create_lostfound: create_lostfound:
bch2_inode_init_early(c, lostfound_inode); bch2_inode_init_early(c, lostfound_inode);
ret = bch2_trans_do(c, NULL, ret = bch2_trans_do(c, NULL, NULL,
BTREE_INSERT_ATOMIC| BTREE_INSERT_ATOMIC|
BTREE_INSERT_NOFAIL| BTREE_INSERT_NOFAIL|
BTREE_INSERT_LAZY_RW, BTREE_INSERT_LAZY_RW,
...@@ -1261,12 +1278,14 @@ static int check_inode(struct btree_trans *trans, ...@@ -1261,12 +1278,14 @@ static int check_inode(struct btree_trans *trans,
struct bkey_inode_buf p; struct bkey_inode_buf p;
bch2_inode_pack(&p, &u); bch2_inode_pack(&p, &u);
bch2_trans_update(trans, iter, &p.inode.k_i);
ret = bch2_trans_commit(trans, NULL, NULL, ret = __bch2_trans_do(trans, NULL, NULL,
BTREE_INSERT_NOFAIL| BTREE_INSERT_ATOMIC|
BTREE_INSERT_LAZY_RW); BTREE_INSERT_NOFAIL|
if (ret && ret != -EINTR) BTREE_INSERT_LAZY_RW,
TRANS_RESET_MEM,
(bch2_trans_update(trans, iter, &p.inode.k_i), 0));
if (ret)
bch_err(c, "error in fsck: error %i " bch_err(c, "error in fsck: error %i "
"updating inode", ret); "updating inode", ret);
} }
......
...@@ -533,7 +533,7 @@ int bch2_inode_find_by_inum_trans(struct btree_trans *trans, u64 inode_nr, ...@@ -533,7 +533,7 @@ int bch2_inode_find_by_inum_trans(struct btree_trans *trans, u64 inode_nr,
int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr, int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
struct bch_inode_unpacked *inode) struct bch_inode_unpacked *inode)
{ {
return bch2_trans_do(c, NULL, 0, return bch2_trans_do(c, NULL, NULL, 0,
bch2_inode_find_by_inum_trans(&trans, inode_nr, inode)); bch2_inode_find_by_inum_trans(&trans, inode_nr, inode));
} }
......
...@@ -1009,7 +1009,7 @@ int bch2_fs_initialize(struct bch_fs *c) ...@@ -1009,7 +1009,7 @@ int bch2_fs_initialize(struct bch_fs *c)
bch2_inode_init_early(c, &lostfound_inode); bch2_inode_init_early(c, &lostfound_inode);
err = "error creating lost+found"; err = "error creating lost+found";
ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC, ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_ATOMIC,
bch2_create_trans(&trans, BCACHEFS_ROOT_INO, bch2_create_trans(&trans, BCACHEFS_ROOT_INO,
&root_inode, &lostfound_inode, &root_inode, &lostfound_inode,
&lostfound, &lostfound,
......
...@@ -328,7 +328,8 @@ static int bch2_xattr_set_handler(const struct xattr_handler *handler, ...@@ -328,7 +328,8 @@ static int bch2_xattr_set_handler(const struct xattr_handler *handler,
struct bch_inode_info *inode = to_bch_ei(vinode); struct bch_inode_info *inode = to_bch_ei(vinode);
struct bch_fs *c = inode->v.i_sb->s_fs_info; struct bch_fs *c = inode->v.i_sb->s_fs_info;
return bch2_trans_do(c, &inode->ei_journal_seq, BTREE_INSERT_ATOMIC, return bch2_trans_do(c, NULL, &inode->ei_journal_seq,
BTREE_INSERT_ATOMIC,
bch2_xattr_set(&trans, inode->v.i_ino, bch2_xattr_set(&trans, inode->v.i_ino,
&inode->ei_str_hash, &inode->ei_str_hash,
name, value, size, name, value, size,
......
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