1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_UPDATE_H
3 #define _BCACHEFS_BTREE_UPDATE_H
5 #include "btree_iter.h"
11 void bch2_btree_node_lock_for_insert(struct btree_trans *, struct btree_iter *,
13 bool bch2_btree_bset_insert_key(struct btree_iter *, struct btree *,
14 struct btree_node_iter *, struct bkey_i *);
15 void bch2_btree_add_journal_pin(struct bch_fs *, struct btree *, u64);
17 enum btree_insert_flags {
18 __BTREE_INSERT_NOUNLOCK,
19 __BTREE_INSERT_NOFAIL,
20 __BTREE_INSERT_NOCHECK_RW,
21 __BTREE_INSERT_LAZY_RW,
22 __BTREE_INSERT_USE_RESERVE,
23 __BTREE_INSERT_JOURNAL_REPLAY,
24 __BTREE_INSERT_JOURNAL_RESERVED,
25 __BTREE_INSERT_JOURNAL_RECLAIM,
26 __BTREE_INSERT_NOWAIT,
27 __BTREE_INSERT_GC_LOCK_HELD,
28 __BCH_HASH_SET_MUST_CREATE,
29 __BCH_HASH_SET_MUST_REPLACE,
33 * Don't drop locks _after_ successfully updating btree:
35 #define BTREE_INSERT_NOUNLOCK (1 << __BTREE_INSERT_NOUNLOCK)
37 /* Don't check for -ENOSPC: */
38 #define BTREE_INSERT_NOFAIL (1 << __BTREE_INSERT_NOFAIL)
40 #define BTREE_INSERT_NOCHECK_RW (1 << __BTREE_INSERT_NOCHECK_RW)
41 #define BTREE_INSERT_LAZY_RW (1 << __BTREE_INSERT_LAZY_RW)
43 /* for copygc, or when merging btree nodes */
44 #define BTREE_INSERT_USE_RESERVE (1 << __BTREE_INSERT_USE_RESERVE)
46 /* Insert is for journal replay - don't get journal reservations: */
47 #define BTREE_INSERT_JOURNAL_REPLAY (1 << __BTREE_INSERT_JOURNAL_REPLAY)
49 /* Indicates that we have pre-reserved space in the journal: */
50 #define BTREE_INSERT_JOURNAL_RESERVED (1 << __BTREE_INSERT_JOURNAL_RESERVED)
52 /* Insert is being called from journal reclaim path: */
53 #define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
55 /* Don't block on allocation failure (for new btree nodes: */
56 #define BTREE_INSERT_NOWAIT (1 << __BTREE_INSERT_NOWAIT)
57 #define BTREE_INSERT_GC_LOCK_HELD (1 << __BTREE_INSERT_GC_LOCK_HELD)
59 #define BCH_HASH_SET_MUST_CREATE (1 << __BCH_HASH_SET_MUST_CREATE)
60 #define BCH_HASH_SET_MUST_REPLACE (1 << __BCH_HASH_SET_MUST_REPLACE)
62 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
64 int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *);
65 int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
66 struct disk_reservation *, u64 *, int flags);
68 int bch2_btree_delete_range_trans(struct btree_trans *, enum btree_id,
69 struct bpos, struct bpos, u64 *);
70 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
71 struct bpos, struct bpos, u64 *);
73 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
75 void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *);
76 int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *,
77 struct btree *, struct bkey_i *, bool);
78 int bch2_btree_node_update_key_get_iter(struct btree_trans *,
79 struct btree *, struct bkey_i *, bool);
81 int bch2_trans_update(struct btree_trans *, struct btree_iter *,
82 struct bkey_i *, enum btree_update_flags);
83 void bch2_trans_commit_hook(struct btree_trans *,
84 struct btree_trans_commit_hook *);
85 int __bch2_trans_commit(struct btree_trans *);
88 * bch2_trans_commit - insert keys at given iterator positions
90 * This is main entry point for btree updates.
93 * -EINTR: locking changed, this function should be called again.
94 * -EROFS: filesystem read only
95 * -EIO: journal or btree node IO error
97 static inline int bch2_trans_commit(struct btree_trans *trans,
98 struct disk_reservation *disk_res,
102 trans->disk_res = disk_res;
103 trans->journal_seq = journal_seq;
104 trans->flags = flags;
106 return __bch2_trans_commit(trans);
109 #define lockrestart_do(_trans, _do) \
115 if (_ret != -EINTR) \
117 bch2_trans_begin(_trans); \
123 #define __bch2_trans_do(_trans, _disk_res, _journal_seq, _flags, _do) \
124 lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
125 (_journal_seq), (_flags)))
127 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do) \
129 struct btree_trans trans; \
132 bch2_trans_init(&trans, (_c), 0, 0); \
133 _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
135 _ret2 = bch2_trans_exit(&trans); \
140 #define trans_for_each_update(_trans, _i) \
141 for ((_i) = (_trans)->updates; \
142 (_i) < (_trans)->updates + (_trans)->nr_updates; \
145 #endif /* _BCACHEFS_BTREE_UPDATE_H */