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 bch_fs *, struct btree *,
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_USE_ALLOC_RESERVE,
24 __BTREE_INSERT_JOURNAL_REPLAY,
25 __BTREE_INSERT_JOURNAL_RESERVED,
26 __BTREE_INSERT_JOURNAL_RECLAIM,
27 __BTREE_INSERT_NOWAIT,
28 __BTREE_INSERT_GC_LOCK_HELD,
29 __BCH_HASH_SET_MUST_CREATE,
30 __BCH_HASH_SET_MUST_REPLACE,
34 * Don't drop locks _after_ successfully updating btree:
36 #define BTREE_INSERT_NOUNLOCK (1 << __BTREE_INSERT_NOUNLOCK)
38 /* Don't check for -ENOSPC: */
39 #define BTREE_INSERT_NOFAIL (1 << __BTREE_INSERT_NOFAIL)
41 #define BTREE_INSERT_NOCHECK_RW (1 << __BTREE_INSERT_NOCHECK_RW)
42 #define BTREE_INSERT_LAZY_RW (1 << __BTREE_INSERT_LAZY_RW)
44 /* for copygc, or when merging btree nodes */
45 #define BTREE_INSERT_USE_RESERVE (1 << __BTREE_INSERT_USE_RESERVE)
46 #define BTREE_INSERT_USE_ALLOC_RESERVE (1 << __BTREE_INSERT_USE_ALLOC_RESERVE)
48 /* Insert is for journal replay - don't get journal reservations: */
49 #define BTREE_INSERT_JOURNAL_REPLAY (1 << __BTREE_INSERT_JOURNAL_REPLAY)
51 /* Indicates that we have pre-reserved space in the journal: */
52 #define BTREE_INSERT_JOURNAL_RESERVED (1 << __BTREE_INSERT_JOURNAL_RESERVED)
54 /* Insert is being called from journal reclaim path: */
55 #define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
57 /* Don't block on allocation failure (for new btree nodes: */
58 #define BTREE_INSERT_NOWAIT (1 << __BTREE_INSERT_NOWAIT)
59 #define BTREE_INSERT_GC_LOCK_HELD (1 << __BTREE_INSERT_GC_LOCK_HELD)
61 #define BCH_HASH_SET_MUST_CREATE (1 << __BCH_HASH_SET_MUST_CREATE)
62 #define BCH_HASH_SET_MUST_REPLACE (1 << __BCH_HASH_SET_MUST_REPLACE)
64 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
66 int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *);
67 int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
68 struct disk_reservation *, u64 *, int flags);
70 int bch2_btree_delete_at_range(struct btree_trans *, struct btree_iter *,
72 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
73 struct bpos, struct bpos, u64 *);
75 int bch2_btree_node_rewrite(struct bch_fs *c, struct btree_iter *,
77 int bch2_btree_node_update_key(struct bch_fs *, struct btree_iter *,
78 struct btree *, struct bkey_i *);
80 int bch2_trans_update(struct btree_trans *, struct btree_iter *,
81 struct bkey_i *, enum btree_trigger_flags);
82 int __bch2_trans_commit(struct btree_trans *);
85 * bch2_trans_commit - insert keys at given iterator positions
87 * This is main entry point for btree updates.
90 * -EINTR: locking changed, this function should be called again.
91 * -EROFS: filesystem read only
92 * -EIO: journal or btree node IO error
94 static inline int bch2_trans_commit(struct btree_trans *trans,
95 struct disk_reservation *disk_res,
99 trans->disk_res = disk_res;
100 trans->journal_seq = journal_seq;
101 trans->flags = flags;
103 return __bch2_trans_commit(trans);
106 #define __bch2_trans_do(_trans, _disk_res, _journal_seq, _flags, _do) \
111 _ret = (_do) ?: bch2_trans_commit(_trans, (_disk_res), \
112 (_journal_seq), (_flags)); \
113 if (_ret != -EINTR) \
115 bch2_trans_reset(_trans, 0); \
121 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do) \
123 struct btree_trans trans; \
126 bch2_trans_init(&trans, (_c), 0, 0); \
127 _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
129 _ret2 = bch2_trans_exit(&trans); \
134 #define trans_for_each_update(_trans, _i) \
135 for ((_i) = (_trans)->updates; \
136 (_i) < (_trans)->updates + (_trans)->nr_updates; \
139 #define trans_for_each_update2(_trans, _i) \
140 for ((_i) = (_trans)->updates2; \
141 (_i) < (_trans)->updates2 + (_trans)->nr_updates2; \
144 #endif /* _BCACHEFS_BTREE_UPDATE_H */