]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_update.h
Update bcachefs sources to 0342eebf85 bcachefs: Improve the backpointer to missing...
[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_prep_for_write(struct btree_trans *,
12                                     struct btree_path *, struct btree *);
13 bool bch2_btree_bset_insert_key(struct btree_trans *, struct btree_path *,
14                                 struct btree *, struct btree_node_iter *,
15                                 struct bkey_i *);
16
17 int bch2_btree_node_flush0(struct journal *, struct journal_entry_pin *, u64);
18 int bch2_btree_node_flush1(struct journal *, struct journal_entry_pin *, u64);
19 void bch2_btree_add_journal_pin(struct bch_fs *, struct btree *, u64);
20
21 void bch2_btree_insert_key_leaf(struct btree_trans *, struct btree_path *,
22                                 struct bkey_i *, u64);
23
24 enum btree_insert_flags {
25         /* First two bits for journal watermark: */
26         __BTREE_INSERT_NOFAIL = 2,
27         __BTREE_INSERT_NOCHECK_RW,
28         __BTREE_INSERT_LAZY_RW,
29         __BTREE_INSERT_USE_RESERVE,
30         __BTREE_INSERT_JOURNAL_REPLAY,
31         __BTREE_INSERT_JOURNAL_RECLAIM,
32         __BTREE_INSERT_NOWAIT,
33         __BTREE_INSERT_GC_LOCK_HELD,
34         __BCH_HASH_SET_MUST_CREATE,
35         __BCH_HASH_SET_MUST_REPLACE,
36 };
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
47 /* Insert is for journal replay - don't get journal reservations: */
48 #define BTREE_INSERT_JOURNAL_REPLAY     (1 << __BTREE_INSERT_JOURNAL_REPLAY)
49
50 /* Insert is being called from journal reclaim path: */
51 #define BTREE_INSERT_JOURNAL_RECLAIM (1 << __BTREE_INSERT_JOURNAL_RECLAIM)
52
53 /* Don't block on allocation failure (for new btree nodes: */
54 #define BTREE_INSERT_NOWAIT             (1 << __BTREE_INSERT_NOWAIT)
55 #define BTREE_INSERT_GC_LOCK_HELD       (1 << __BTREE_INSERT_GC_LOCK_HELD)
56
57 #define BCH_HASH_SET_MUST_CREATE        (1 << __BCH_HASH_SET_MUST_CREATE)
58 #define BCH_HASH_SET_MUST_REPLACE       (1 << __BCH_HASH_SET_MUST_REPLACE)
59
60 int bch2_btree_delete_extent_at(struct btree_trans *, struct btree_iter *,
61                                 unsigned, unsigned);
62 int bch2_btree_delete_at(struct btree_trans *, struct btree_iter *, unsigned);
63
64 int bch2_btree_insert_nonextent(struct btree_trans *, enum btree_id,
65                                 struct bkey_i *, enum btree_update_flags);
66
67 int __bch2_btree_insert(struct btree_trans *, enum btree_id, struct bkey_i *,
68                         enum btree_update_flags);
69 int bch2_btree_insert(struct bch_fs *, enum btree_id, struct bkey_i *,
70                      struct disk_reservation *, u64 *, int flags);
71
72 int bch2_btree_delete_range_trans(struct btree_trans *, enum btree_id,
73                                   struct bpos, struct bpos, unsigned, u64 *);
74 int bch2_btree_delete_range(struct bch_fs *, enum btree_id,
75                             struct bpos, struct bpos, unsigned, u64 *);
76
77 int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *,
78                             struct btree *, unsigned);
79 void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *);
80 int bch2_btree_node_update_key(struct btree_trans *, struct btree_iter *,
81                                struct btree *, struct bkey_i *, bool);
82 int bch2_btree_node_update_key_get_iter(struct btree_trans *,
83                                 struct btree *, struct bkey_i *, bool);
84
85 int bch2_trans_update_extent(struct btree_trans *, struct btree_iter *,
86                              struct bkey_i *, enum btree_update_flags);
87
88 int __must_check bch2_trans_update(struct btree_trans *, struct btree_iter *,
89                                    struct bkey_i *, enum btree_update_flags);
90 int __must_check bch2_trans_update_buffered(struct btree_trans *,
91                                             enum btree_id, struct bkey_i *);
92
93 void bch2_trans_commit_hook(struct btree_trans *,
94                             struct btree_trans_commit_hook *);
95 int __bch2_trans_commit(struct btree_trans *, unsigned);
96
97 int bch2_trans_log_msg(struct btree_trans *, const char *, ...);
98 int bch2_fs_log_msg(struct bch_fs *, const char *, ...);
99
100 /**
101  * bch2_trans_commit - insert keys at given iterator positions
102  *
103  * This is main entry point for btree updates.
104  *
105  * Return values:
106  * -EROFS: filesystem read only
107  * -EIO: journal or btree node IO error
108  */
109 static inline int bch2_trans_commit(struct btree_trans *trans,
110                                     struct disk_reservation *disk_res,
111                                     u64 *journal_seq,
112                                     unsigned flags)
113 {
114         trans->disk_res         = disk_res;
115         trans->journal_seq      = journal_seq;
116
117         return __bch2_trans_commit(trans, flags);
118 }
119
120 #define commit_do(_trans, _disk_res, _journal_seq, _flags, _do) \
121         lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
122                                         (_journal_seq), (_flags)))
123
124 #define nested_commit_do(_trans, _disk_res, _journal_seq, _flags, _do)  \
125         nested_lockrestart_do(_trans, _do ?: bch2_trans_commit(_trans, (_disk_res),\
126                                         (_journal_seq), (_flags)))
127
128 #define bch2_trans_do(_c, _disk_res, _journal_seq, _flags, _do)         \
129 ({                                                                      \
130         struct btree_trans trans;                                       \
131         int _ret;                                                       \
132                                                                         \
133         bch2_trans_init(&trans, (_c), 0, 0);                            \
134         _ret = commit_do(&trans, _disk_res, _journal_seq, _flags, _do); \
135         bch2_trans_exit(&trans);                                        \
136                                                                         \
137         _ret;                                                           \
138 })
139
140 #define bch2_trans_run(_c, _do)                                         \
141 ({                                                                      \
142         struct btree_trans trans;                                       \
143         int _ret;                                                       \
144                                                                         \
145         bch2_trans_init(&trans, (_c), 0, 0);                            \
146         _ret = (_do);                                                   \
147         bch2_trans_exit(&trans);                                        \
148                                                                         \
149         _ret;                                                           \
150 })
151
152 #define trans_for_each_update(_trans, _i)                               \
153         for ((_i) = (_trans)->updates;                                  \
154              (_i) < (_trans)->updates + (_trans)->nr_updates;           \
155              (_i)++)
156
157 #define trans_for_each_wb_update(_trans, _i)                            \
158         for ((_i) = (_trans)->wb_updates;                               \
159              (_i) < (_trans)->wb_updates + (_trans)->nr_wb_updates;     \
160              (_i)++)
161
162 static inline void bch2_trans_reset_updates(struct btree_trans *trans)
163 {
164         struct btree_insert_entry *i;
165
166         trans_for_each_update(trans, i)
167                 bch2_path_put(trans, i->path, true);
168
169         trans->extra_journal_res        = 0;
170         trans->nr_updates               = 0;
171         trans->nr_wb_updates            = 0;
172         trans->wb_updates               = NULL;
173         trans->hooks                    = NULL;
174         trans->extra_journal_entries.nr = 0;
175
176         if (trans->fs_usage_deltas) {
177                 trans->fs_usage_deltas->used = 0;
178                 memset((void *) trans->fs_usage_deltas +
179                        offsetof(struct replicas_delta_list, memset_start), 0,
180                        (void *) &trans->fs_usage_deltas->memset_end -
181                        (void *) &trans->fs_usage_deltas->memset_start);
182         }
183 }
184
185 #endif /* _BCACHEFS_BTREE_UPDATE_H */