]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update.h
Update bcachefs sources to 4837f82ee1 bcachefs: Use cached iterators for alloc btree
[bcachefs-tools-debian] / libbcachefs / btree_update.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_UPDATE_H
3 #define _BCACHEFS_BTREE_UPDATE_H
4
5 #include "btree_iter.h"
6 #include "journal.h"
7
8 struct bch_fs;
9 struct btree;
10
11 void bch2_btree_node_lock_for_insert(struct bch_fs *, struct btree *,
12                                      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);
16
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,
31 };
32
33 /*
34  * Don't drop locks _after_ successfully updating btree:
35  */
36 #define BTREE_INSERT_NOUNLOCK           (1 << __BTREE_INSERT_NOUNLOCK)
37
38 /* Don't check for -ENOSPC: */
39 #define BTREE_INSERT_NOFAIL             (1 << __BTREE_INSERT_NOFAIL)
40
41 #define BTREE_INSERT_NOCHECK_RW         (1 << __BTREE_INSERT_NOCHECK_RW)
42 #define BTREE_INSERT_LAZY_RW            (1 << __BTREE_INSERT_LAZY_RW)
43
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)
47
48 /* Insert is for journal replay - don't get journal reservations: */
49 #define BTREE_INSERT_JOURNAL_REPLAY     (1 << __BTREE_INSERT_JOURNAL_REPLAY)
50
51 /* Indicates that we have pre-reserved space in the journal: */
52 #define BTREE_INSERT_JOURNAL_RESERVED   (1 << __BTREE_INSERT_JOURNAL_RESERVED)
53
54 /* Insert is being called from journal reclaim path: */
55 #define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
56
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)
60
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)
63
64 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
65
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);
69
70 int bch2_btree_delete_at_range(struct btree_trans *, struct btree_iter *,
71                                struct bpos, u64 *);
72 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
73                             struct bpos, struct bpos, u64 *);
74
75 int bch2_btree_node_rewrite(struct bch_fs *c, struct btree_iter *,
76                             __le64, unsigned);
77 int bch2_btree_node_update_key(struct bch_fs *, struct btree_iter *,
78                                struct btree *, struct bkey_i *);
79
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 *);
83
84 /**
85  * bch2_trans_commit - insert keys at given iterator positions
86  *
87  * This is main entry point for btree updates.
88  *
89  * Return values:
90  * -EINTR: locking changed, this function should be called again.
91  * -EROFS: filesystem read only
92  * -EIO: journal or btree node IO error
93  */
94 static inline int bch2_trans_commit(struct btree_trans *trans,
95                                     struct disk_reservation *disk_res,
96                                     u64 *journal_seq,
97                                     unsigned flags)
98 {
99         trans->disk_res         = disk_res;
100         trans->journal_seq      = journal_seq;
101         trans->flags            = flags;
102
103         return __bch2_trans_commit(trans);
104 }
105
106 #define __bch2_trans_do(_trans, _disk_res, _journal_seq, _flags, _do)   \
107 ({                                                                      \
108         int _ret;                                                       \
109                                                                         \
110         while (1) {                                                     \
111                 _ret = (_do) ?: bch2_trans_commit(_trans, (_disk_res),  \
112                                         (_journal_seq), (_flags));      \
113                 if (_ret != -EINTR)                                     \
114                         break;                                          \
115                 bch2_trans_reset(_trans, 0);                            \
116         }                                                               \
117                                                                         \
118         _ret;                                                           \
119 })
120
121 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)         \
122 ({                                                                      \
123         struct btree_trans trans;                                       \
124         int _ret, _ret2;                                                \
125                                                                         \
126         bch2_trans_init(&trans, (_c), 0, 0);                            \
127         _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
128                                _do);                                    \
129         _ret2 = bch2_trans_exit(&trans);                                \
130                                                                         \
131         _ret ?: _ret2;                                                  \
132 })
133
134 #define trans_for_each_update(_trans, _i)                               \
135         for ((_i) = (_trans)->updates;                                  \
136              (_i) < (_trans)->updates + (_trans)->nr_updates;           \
137              (_i)++)
138
139 #define trans_for_each_update2(_trans, _i)                              \
140         for ((_i) = (_trans)->updates2;                                 \
141              (_i) < (_trans)->updates2 + (_trans)->nr_updates2;         \
142              (_i)++)
143
144 #endif /* _BCACHEFS_BTREE_UPDATE_H */