]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update.h
Update bcachefs sources to 1569db10e2 bcachefs: Use KEY_TYPE_deleted whitouts for...
[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_journal_key(struct btree_trans *, struct btree_iter *,
16                             struct bkey_i *);
17
18 enum {
19         __BTREE_INSERT_NOUNLOCK,
20         __BTREE_INSERT_NOFAIL,
21         __BTREE_INSERT_NOCHECK_RW,
22         __BTREE_INSERT_LAZY_RW,
23         __BTREE_INSERT_USE_RESERVE,
24         __BTREE_INSERT_USE_ALLOC_RESERVE,
25         __BTREE_INSERT_JOURNAL_REPLAY,
26         __BTREE_INSERT_JOURNAL_RESERVED,
27         __BTREE_INSERT_NOMARK_OVERWRITES,
28         __BTREE_INSERT_NOMARK,
29         __BTREE_INSERT_BUCKET_INVALIDATE,
30         __BTREE_INSERT_NOWAIT,
31         __BTREE_INSERT_GC_LOCK_HELD,
32         __BCH_HASH_SET_MUST_CREATE,
33         __BCH_HASH_SET_MUST_REPLACE,
34 };
35
36 /*
37  * Don't drop locks _after_ successfully updating btree:
38  */
39 #define BTREE_INSERT_NOUNLOCK           (1 << __BTREE_INSERT_NOUNLOCK)
40
41 /* Don't check for -ENOSPC: */
42 #define BTREE_INSERT_NOFAIL             (1 << __BTREE_INSERT_NOFAIL)
43
44 #define BTREE_INSERT_NOCHECK_RW         (1 << __BTREE_INSERT_NOCHECK_RW)
45 #define BTREE_INSERT_LAZY_RW            (1 << __BTREE_INSERT_LAZY_RW)
46
47 /* for copygc, or when merging btree nodes */
48 #define BTREE_INSERT_USE_RESERVE        (1 << __BTREE_INSERT_USE_RESERVE)
49 #define BTREE_INSERT_USE_ALLOC_RESERVE  (1 << __BTREE_INSERT_USE_ALLOC_RESERVE)
50
51 /* Insert is for journal replay - don't get journal reservations: */
52 #define BTREE_INSERT_JOURNAL_REPLAY     (1 << __BTREE_INSERT_JOURNAL_REPLAY)
53
54 #define BTREE_INSERT_JOURNAL_RESERVED   (1 << __BTREE_INSERT_JOURNAL_RESERVED)
55
56 /* Don't mark overwrites, just new key: */
57 #define BTREE_INSERT_NOMARK_OVERWRITES  (1 << __BTREE_INSERT_NOMARK_OVERWRITES)
58
59 /* Don't call mark new key at all: */
60 #define BTREE_INSERT_NOMARK             (1 << __BTREE_INSERT_NOMARK)
61
62 #define BTREE_INSERT_BUCKET_INVALIDATE  (1 << __BTREE_INSERT_BUCKET_INVALIDATE)
63
64 /* Don't block on allocation failure (for new btree nodes: */
65 #define BTREE_INSERT_NOWAIT             (1 << __BTREE_INSERT_NOWAIT)
66 #define BTREE_INSERT_GC_LOCK_HELD       (1 << __BTREE_INSERT_GC_LOCK_HELD)
67
68 #define BCH_HASH_SET_MUST_CREATE        (1 << __BCH_HASH_SET_MUST_CREATE)
69 #define BCH_HASH_SET_MUST_REPLACE       (1 << __BCH_HASH_SET_MUST_REPLACE)
70
71 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
72
73 int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
74                      struct disk_reservation *, u64 *, int flags);
75
76 int bch2_btree_delete_at_range(struct btree_trans *, struct btree_iter *,
77                                struct bpos, u64 *);
78 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
79                             struct bpos, struct bpos, u64 *);
80
81 int bch2_btree_node_rewrite(struct bch_fs *c, struct btree_iter *,
82                             __le64, unsigned);
83 int bch2_btree_node_update_key(struct bch_fs *, struct btree_iter *,
84                                struct btree *, struct bkey_i_btree_ptr *);
85
86 int __bch2_trans_commit(struct btree_trans *);
87
88 /**
89  * bch2_trans_commit - insert keys at given iterator positions
90  *
91  * This is main entry point for btree updates.
92  *
93  * Return values:
94  * -EINTR: locking changed, this function should be called again.
95  * -EROFS: filesystem read only
96  * -EIO: journal or btree node IO error
97  */
98 static inline int bch2_trans_commit(struct btree_trans *trans,
99                                     struct disk_reservation *disk_res,
100                                     u64 *journal_seq,
101                                     unsigned flags)
102 {
103         trans->disk_res         = disk_res;
104         trans->journal_seq      = journal_seq;
105         trans->flags            = flags;
106
107         return __bch2_trans_commit(trans);
108 }
109
110 static inline void bch2_trans_update(struct btree_trans *trans,
111                                      struct btree_iter *iter,
112                                      struct bkey_i *k)
113 {
114         EBUG_ON(trans->nr_updates >= trans->nr_iters + 4);
115
116         iter->flags |= BTREE_ITER_KEEP_UNTIL_COMMIT;
117
118         trans->updates[trans->nr_updates++] = (struct btree_insert_entry) {
119                 .iter = iter, .k = k
120         };
121 }
122
123 #define __bch2_trans_do(_trans, _disk_res, _journal_seq,                \
124                         _flags, _reset_flags, _do)                      \
125 ({                                                                      \
126         int _ret;                                                       \
127                                                                         \
128         do {                                                            \
129                 bch2_trans_reset(_trans, _reset_flags);                 \
130                                                                         \
131                 _ret = (_do) ?: bch2_trans_commit(_trans, (_disk_res),  \
132                                         (_journal_seq), (_flags));      \
133         } while (_ret == -EINTR);                                       \
134                                                                         \
135         _ret;                                                           \
136 })
137
138 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)         \
139 ({                                                                      \
140         struct btree_trans trans;                                       \
141         int _ret, _ret2;                                                \
142                                                                         \
143         bch2_trans_init(&trans, (_c), 0, 0);                            \
144         _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
145                                TRANS_RESET_MEM|TRANS_RESET_ITERS, _do); \
146         _ret2 = bch2_trans_exit(&trans);                                \
147                                                                         \
148         _ret ?: _ret2;                                                  \
149 })
150
151 #define trans_for_each_update(_trans, _i)                               \
152         for ((_i) = (_trans)->updates;                                  \
153              (_i) < (_trans)->updates + (_trans)->nr_updates;           \
154              (_i)++)
155
156 #endif /* _BCACHEFS_BTREE_UPDATE_H */