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_NOFAIL,
19 __BTREE_INSERT_NOCHECK_RW,
20 __BTREE_INSERT_LAZY_RW,
21 __BTREE_INSERT_USE_RESERVE,
22 __BTREE_INSERT_JOURNAL_REPLAY,
23 __BTREE_INSERT_JOURNAL_RESERVED,
24 __BTREE_INSERT_JOURNAL_RECLAIM,
25 __BTREE_INSERT_NOWAIT,
26 __BTREE_INSERT_GC_LOCK_HELD,
27 __BCH_HASH_SET_MUST_CREATE,
28 __BCH_HASH_SET_MUST_REPLACE,
31 /* Don't check for -ENOSPC: */
32 #define BTREE_INSERT_NOFAIL (1 << __BTREE_INSERT_NOFAIL)
34 #define BTREE_INSERT_NOCHECK_RW (1 << __BTREE_INSERT_NOCHECK_RW)
35 #define BTREE_INSERT_LAZY_RW (1 << __BTREE_INSERT_LAZY_RW)
37 /* for copygc, or when merging btree nodes */
38 #define BTREE_INSERT_USE_RESERVE (1 << __BTREE_INSERT_USE_RESERVE)
40 /* Insert is for journal replay - don't get journal reservations: */
41 #define BTREE_INSERT_JOURNAL_REPLAY (1 << __BTREE_INSERT_JOURNAL_REPLAY)
43 /* Indicates that we have pre-reserved space in the journal: */
44 #define BTREE_INSERT_JOURNAL_RESERVED (1 << __BTREE_INSERT_JOURNAL_RESERVED)
46 /* Insert is being called from journal reclaim path: */
47 #define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
49 /* Don't block on allocation failure (for new btree nodes: */
50 #define BTREE_INSERT_NOWAIT (1 << __BTREE_INSERT_NOWAIT)
51 #define BTREE_INSERT_GC_LOCK_HELD (1 << __BTREE_INSERT_GC_LOCK_HELD)
53 #define BCH_HASH_SET_MUST_CREATE (1 << __BCH_HASH_SET_MUST_CREATE)
54 #define BCH_HASH_SET_MUST_REPLACE (1 << __BCH_HASH_SET_MUST_REPLACE)
56 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
58 int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *);
59 int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
60 struct disk_reservation *, u64 *, int flags);
62 int bch2_btree_delete_range_trans(struct btree_trans *, enum btree_id,
63 struct bpos, struct bpos, u64 *);
64 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
65 struct bpos, struct bpos, u64 *);
67 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
69 void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *);
70 int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *,
71 struct btree *, struct bkey_i *, bool);
72 int bch2_btree_node_update_key_get_iter(struct btree_trans *,
73 struct btree *, struct bkey_i *, bool);
75 int bch2_trans_update(struct btree_trans *, struct btree_iter *,
76 struct bkey_i *, enum btree_update_flags);
77 void bch2_trans_commit_hook(struct btree_trans *,
78 struct btree_trans_commit_hook *);
79 int __bch2_trans_commit(struct btree_trans *);
82 * bch2_trans_commit - insert keys at given iterator positions
84 * This is main entry point for btree updates.
87 * -EINTR: locking changed, this function should be called again.
88 * -EROFS: filesystem read only
89 * -EIO: journal or btree node IO error
91 static inline int bch2_trans_commit(struct btree_trans *trans,
92 struct disk_reservation *disk_res,
96 trans->disk_res = disk_res;
97 trans->journal_seq = journal_seq;
100 return __bch2_trans_commit(trans);
103 #define lockrestart_do(_trans, _do) \
108 bch2_trans_begin(_trans); \
110 } while (_ret == -EINTR); \
115 #define __bch2_trans_do(_trans, _disk_res, _journal_seq, _flags, _do) \
116 lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
117 (_journal_seq), (_flags)))
119 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do) \
121 struct btree_trans trans; \
124 bch2_trans_init(&trans, (_c), 0, 0); \
125 _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
127 _ret2 = bch2_trans_exit(&trans); \
132 #define trans_for_each_update(_trans, _i) \
133 for ((_i) = (_trans)->updates; \
134 (_i) < (_trans)->updates + (_trans)->nr_updates; \
137 #endif /* _BCACHEFS_BTREE_UPDATE_H */