]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update.h
Update bcachefs sources to b964c6cba8 bcachefs: Change lockrestart_do() to always...
[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 btree_trans *, struct btree_iter *,
12                                      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);
16
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,
29 };
30
31 /* Don't check for -ENOSPC: */
32 #define BTREE_INSERT_NOFAIL             (1 << __BTREE_INSERT_NOFAIL)
33
34 #define BTREE_INSERT_NOCHECK_RW         (1 << __BTREE_INSERT_NOCHECK_RW)
35 #define BTREE_INSERT_LAZY_RW            (1 << __BTREE_INSERT_LAZY_RW)
36
37 /* for copygc, or when merging btree nodes */
38 #define BTREE_INSERT_USE_RESERVE        (1 << __BTREE_INSERT_USE_RESERVE)
39
40 /* Insert is for journal replay - don't get journal reservations: */
41 #define BTREE_INSERT_JOURNAL_REPLAY     (1 << __BTREE_INSERT_JOURNAL_REPLAY)
42
43 /* Indicates that we have pre-reserved space in the journal: */
44 #define BTREE_INSERT_JOURNAL_RESERVED   (1 << __BTREE_INSERT_JOURNAL_RESERVED)
45
46 /* Insert is being called from journal reclaim path: */
47 #define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
48
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)
52
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)
55
56 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
57
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);
61
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 *);
66
67 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
68                             __le64, unsigned);
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);
74
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 *);
80
81 /**
82  * bch2_trans_commit - insert keys at given iterator positions
83  *
84  * This is main entry point for btree updates.
85  *
86  * Return values:
87  * -EINTR: locking changed, this function should be called again.
88  * -EROFS: filesystem read only
89  * -EIO: journal or btree node IO error
90  */
91 static inline int bch2_trans_commit(struct btree_trans *trans,
92                                     struct disk_reservation *disk_res,
93                                     u64 *journal_seq,
94                                     unsigned flags)
95 {
96         trans->disk_res         = disk_res;
97         trans->journal_seq      = journal_seq;
98         trans->flags            = flags;
99
100         return __bch2_trans_commit(trans);
101 }
102
103 #define lockrestart_do(_trans, _do)                                     \
104 ({                                                                      \
105         int _ret;                                                       \
106                                                                         \
107         do {                                                            \
108                 bch2_trans_begin(_trans);                               \
109                 _ret = (_do);                                           \
110         } while (_ret == -EINTR);                                       \
111                                                                         \
112         _ret;                                                           \
113 })
114
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)))
118
119 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)         \
120 ({                                                                      \
121         struct btree_trans trans;                                       \
122         int _ret, _ret2;                                                \
123                                                                         \
124         bch2_trans_init(&trans, (_c), 0, 0);                            \
125         _ret = __bch2_trans_do(&trans, _disk_res, _journal_seq, _flags, \
126                                _do);                                    \
127         _ret2 = bch2_trans_exit(&trans);                                \
128                                                                         \
129         _ret ?: _ret2;                                                  \
130 })
131
132 #define trans_for_each_update(_trans, _i)                               \
133         for ((_i) = (_trans)->updates;                                  \
134              (_i) < (_trans)->updates + (_trans)->nr_updates;           \
135              (_i)++)
136
137 #endif /* _BCACHEFS_BTREE_UPDATE_H */