bcachefs: BTREE_ITER_FILTER_SNAPSHOTS
[linux-block.git] / fs / bcachefs / btree_iter.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_ITER_H
3 #define _BCACHEFS_BTREE_ITER_H
4
5 #include "bset.h"
6 #include "btree_types.h"
7
8 static inline void __btree_path_get(struct btree_path *path, bool intent)
9 {
10         path->ref++;
11         path->intent_ref += intent;
12 }
13
14 static inline bool __btree_path_put(struct btree_path *path, bool intent)
15 {
16         EBUG_ON(!path->ref);
17         EBUG_ON(!path->intent_ref && intent);
18         path->intent_ref -= intent;
19         return --path->ref == 0;
20 }
21
22 static inline void btree_path_set_dirty(struct btree_path *path,
23                                         enum btree_path_uptodate u)
24 {
25         path->uptodate = max_t(unsigned, path->uptodate, u);
26 }
27
28 static inline struct btree *btree_path_node(struct btree_path *path,
29                                             unsigned level)
30 {
31         return level < BTREE_MAX_DEPTH ? path->l[level].b : NULL;
32 }
33
34 static inline bool btree_node_lock_seq_matches(const struct btree_path *path,
35                                         const struct btree *b, unsigned level)
36 {
37         /*
38          * We don't compare the low bits of the lock sequence numbers because
39          * @path might have taken a write lock on @b, and we don't want to skip
40          * the linked path if the sequence numbers were equal before taking that
41          * write lock. The lock sequence number is incremented by taking and
42          * releasing write locks and is even when unlocked:
43          */
44         return path->l[level].lock_seq >> 1 == b->c.lock.state.seq >> 1;
45 }
46
47 static inline struct btree *btree_node_parent(struct btree_path *path,
48                                               struct btree *b)
49 {
50         return btree_path_node(path, b->c.level + 1);
51 }
52
53 static inline int btree_iter_err(const struct btree_iter *iter)
54 {
55         return iter->flags & BTREE_ITER_ERROR ? -EIO : 0;
56 }
57
58 /* Iterate over paths within a transaction: */
59
60 void __bch2_btree_trans_sort_paths(struct btree_trans *);
61
62 static inline void btree_trans_sort_paths(struct btree_trans *trans)
63 {
64         if (!IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
65             trans->paths_sorted)
66                 return;
67         __bch2_btree_trans_sort_paths(trans);
68 }
69
70 static inline struct btree_path *
71 __trans_next_path(struct btree_trans *trans, unsigned idx)
72 {
73         u64 l;
74
75         if (idx == BTREE_ITER_MAX)
76                 return NULL;
77
78         l = trans->paths_allocated >> idx;
79         if (!l)
80                 return NULL;
81
82         idx += __ffs64(l);
83         EBUG_ON(idx >= BTREE_ITER_MAX);
84         EBUG_ON(trans->paths[idx].idx != idx);
85         return &trans->paths[idx];
86 }
87
88 #define trans_for_each_path(_trans, _path)                              \
89         for (_path = __trans_next_path((_trans), 0);                    \
90              (_path);                                                   \
91              _path = __trans_next_path((_trans), (_path)->idx + 1))
92
93 static inline struct btree_path *next_btree_path(struct btree_trans *trans, struct btree_path *path)
94 {
95         unsigned idx = path ? path->sorted_idx + 1 : 0;
96
97         EBUG_ON(idx > trans->nr_sorted);
98
99         return idx < trans->nr_sorted
100                 ? trans->paths + trans->sorted[idx]
101                 : NULL;
102 }
103
104 static inline struct btree_path *prev_btree_path(struct btree_trans *trans, struct btree_path *path)
105 {
106         unsigned idx = path ? path->sorted_idx : trans->nr_sorted;
107
108         return idx
109                 ? trans->paths + trans->sorted[idx - 1]
110                 : NULL;
111 }
112
113 #define trans_for_each_path_inorder(_trans, _path, _i)                  \
114         for (_i = 0;                                                    \
115              ((_path) = (_trans)->paths + trans->sorted[_i]), (_i) < (_trans)->nr_sorted;\
116              _i++)
117
118 #define trans_for_each_path_inorder_reverse(_trans, _path, _i)          \
119         for (_i = trans->nr_sorted - 1;                                 \
120              ((_path) = (_trans)->paths + trans->sorted[_i]), (_i) >= 0;\
121              --_i)
122
123 static inline bool __path_has_node(const struct btree_path *path,
124                                    const struct btree *b)
125 {
126         return path->l[b->c.level].b == b &&
127                 btree_node_lock_seq_matches(path, b, b->c.level);
128 }
129
130 static inline struct btree_path *
131 __trans_next_path_with_node(struct btree_trans *trans, struct btree *b,
132                             unsigned idx)
133 {
134         struct btree_path *path = __trans_next_path(trans, idx);
135
136         while (path && !__path_has_node(path, b))
137                 path = __trans_next_path(trans, path->idx + 1);
138
139         return path;
140 }
141
142 #define trans_for_each_path_with_node(_trans, _b, _path)                \
143         for (_path = __trans_next_path_with_node((_trans), (_b), 0);    \
144              (_path);                                                   \
145              _path = __trans_next_path_with_node((_trans), (_b),        \
146                                                  (_path)->idx + 1))
147
148 struct btree_path *__bch2_btree_path_make_mut(struct btree_trans *,
149                                               struct btree_path *, bool);
150
151 static inline struct btree_path * __must_check
152 bch2_btree_path_make_mut(struct btree_trans *trans,
153                          struct btree_path *path, bool intent)
154 {
155         if (path->ref > 1 || path->preserve)
156                 path = __bch2_btree_path_make_mut(trans, path, intent);
157         return path;
158 }
159
160 int __must_check bch2_btree_path_traverse(struct btree_trans *,
161                                           struct btree_path *, unsigned);
162 struct btree_path *bch2_path_get(struct btree_trans *, bool, enum btree_id,
163                                  struct bpos, unsigned, unsigned, bool);
164 inline struct bkey_s_c bch2_btree_path_peek_slot(struct btree_path *, struct bkey *);
165
166 #ifdef CONFIG_BCACHEFS_DEBUG
167 void bch2_trans_verify_paths(struct btree_trans *);
168 void bch2_trans_verify_locks(struct btree_trans *);
169 #else
170 static inline void bch2_trans_verify_paths(struct btree_trans *trans) {}
171 static inline void bch2_trans_verify_locks(struct btree_trans *trans) {}
172 #endif
173
174 void bch2_btree_path_fix_key_modified(struct btree_trans *trans,
175                                       struct btree *, struct bkey_packed *);
176 void bch2_btree_node_iter_fix(struct btree_trans *trans, struct btree_path *,
177                               struct btree *, struct btree_node_iter *,
178                               struct bkey_packed *, unsigned, unsigned);
179
180 bool bch2_btree_path_relock_intent(struct btree_trans *, struct btree_path *);
181
182 void bch2_path_put(struct btree_trans *, struct btree_path *, bool);
183
184 bool bch2_trans_relock(struct btree_trans *);
185 void bch2_trans_unlock(struct btree_trans *);
186
187 __always_inline
188 static inline int btree_trans_restart(struct btree_trans *trans)
189 {
190         trans->restarted = true;
191         bch2_trans_unlock(trans);
192         return -EINTR;
193 }
194
195 bool __bch2_btree_path_upgrade(struct btree_trans *,
196                                struct btree_path *, unsigned);
197
198 static inline bool bch2_btree_path_upgrade(struct btree_trans *trans,
199                                            struct btree_path *path,
200                                            unsigned new_locks_want)
201 {
202         new_locks_want = min(new_locks_want, BTREE_MAX_DEPTH);
203
204         return path->locks_want < new_locks_want
205                 ? __bch2_btree_path_upgrade(trans, path, new_locks_want)
206                 : path->uptodate == BTREE_ITER_UPTODATE;
207 }
208
209 void __bch2_btree_path_downgrade(struct btree_path *, unsigned);
210
211 static inline void bch2_btree_path_downgrade(struct btree_path *path)
212 {
213         unsigned new_locks_want = path->level + !!path->intent_ref;
214
215         if (path->locks_want > new_locks_want)
216                 __bch2_btree_path_downgrade(path, new_locks_want);
217 }
218
219 void bch2_trans_downgrade(struct btree_trans *);
220
221 void bch2_trans_node_add(struct btree_trans *trans, struct btree *);
222 void bch2_trans_node_reinit_iter(struct btree_trans *, struct btree *);
223
224 int __must_check __bch2_btree_iter_traverse(struct btree_iter *iter);
225 int __must_check bch2_btree_iter_traverse(struct btree_iter *);
226
227 struct btree *bch2_btree_iter_peek_node(struct btree_iter *);
228 struct btree *bch2_btree_iter_next_node(struct btree_iter *);
229
230 struct bkey_s_c bch2_btree_iter_peek(struct btree_iter *);
231 struct bkey_s_c bch2_btree_iter_next(struct btree_iter *);
232
233 struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *);
234 struct bkey_s_c bch2_btree_iter_prev(struct btree_iter *);
235
236 struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *);
237 struct bkey_s_c bch2_btree_iter_next_slot(struct btree_iter *);
238 struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *);
239
240 bool bch2_btree_iter_advance(struct btree_iter *);
241 bool bch2_btree_iter_rewind(struct btree_iter *);
242
243 static inline void bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
244 {
245         if (!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS))
246                 new_pos.snapshot = iter->snapshot;
247
248         iter->k.type = KEY_TYPE_deleted;
249         iter->k.p.inode         = iter->pos.inode       = new_pos.inode;
250         iter->k.p.offset        = iter->pos.offset      = new_pos.offset;
251         iter->k.p.snapshot      = iter->pos.snapshot    = new_pos.snapshot;
252         iter->k.size = 0;
253         if (iter->path->ref == 1)
254                 iter->path->should_be_locked = false;
255 }
256
257 static inline void bch2_btree_iter_set_pos_to_extent_start(struct btree_iter *iter)
258 {
259         BUG_ON(!(iter->flags & BTREE_ITER_IS_EXTENTS));
260         iter->pos = bkey_start_pos(&iter->k);
261 }
262
263 static inline void bch2_btree_iter_set_snapshot(struct btree_iter *iter, u32 snapshot)
264 {
265         struct bpos pos = iter->pos;
266
267         iter->snapshot = snapshot;
268         pos.snapshot = snapshot;
269         bch2_btree_iter_set_pos(iter, pos);
270 }
271
272 /*
273  * Unlocks before scheduling
274  * Note: does not revalidate iterator
275  */
276 static inline int bch2_trans_cond_resched(struct btree_trans *trans)
277 {
278         if (need_resched() || race_fault()) {
279                 bch2_trans_unlock(trans);
280                 schedule();
281                 return bch2_trans_relock(trans) ? 0 : -EINTR;
282         } else {
283                 return 0;
284         }
285 }
286
287 #define __for_each_btree_node(_trans, _iter, _btree_id, _start, \
288                               _locks_want, _depth, _flags, _b)          \
289         for (bch2_trans_node_iter_init((_trans), &(_iter), (_btree_id), \
290                                 _start, _locks_want, _depth, _flags),   \
291              _b = bch2_btree_iter_peek_node(&(_iter));                  \
292              (_b);                                                      \
293              (_b) = bch2_btree_iter_next_node(&(_iter)))
294
295 #define for_each_btree_node(_trans, _iter, _btree_id, _start,           \
296                             _flags, _b)                                 \
297         __for_each_btree_node(_trans, _iter, _btree_id, _start,         \
298                               0, 0, _flags, _b)
299
300 static inline struct bkey_s_c __bch2_btree_iter_peek(struct btree_iter *iter,
301                                                      unsigned flags)
302 {
303         return flags & BTREE_ITER_SLOTS
304                 ? bch2_btree_iter_peek_slot(iter)
305                 : bch2_btree_iter_peek(iter);
306 }
307
308 static inline struct bkey_s_c __bch2_btree_iter_next(struct btree_iter *iter,
309                                                      unsigned flags)
310 {
311         return flags & BTREE_ITER_SLOTS
312                 ? bch2_btree_iter_next_slot(iter)
313                 : bch2_btree_iter_next(iter);
314 }
315
316 static inline int bkey_err(struct bkey_s_c k)
317 {
318         return PTR_ERR_OR_ZERO(k.k);
319 }
320
321 #define for_each_btree_key(_trans, _iter, _btree_id,                    \
322                            _start, _flags, _k, _ret)                    \
323         for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id),      \
324                                   (_start), (_flags)),                  \
325              (_k) = __bch2_btree_iter_peek(&(_iter), _flags);           \
326              !((_ret) = bkey_err(_k)) && (_k).k;                        \
327              (_k) = __bch2_btree_iter_next(&(_iter), _flags))
328
329 #define for_each_btree_key_continue(_iter, _flags, _k, _ret)            \
330         for ((_k) = __bch2_btree_iter_peek(&(_iter), _flags);           \
331              !((_ret) = bkey_err(_k)) && (_k).k;                        \
332              (_k) = __bch2_btree_iter_next(&(_iter), _flags))
333
334 /* new multiple iterator interface: */
335
336 void bch2_dump_trans_paths_updates(struct btree_trans *);
337
338 void bch2_trans_iter_exit(struct btree_trans *, struct btree_iter *);
339 void bch2_trans_iter_init(struct btree_trans *, struct btree_iter *,
340                           unsigned, struct bpos, unsigned);
341 void bch2_trans_node_iter_init(struct btree_trans *, struct btree_iter *,
342                                enum btree_id, struct bpos,
343                                unsigned, unsigned, unsigned);
344 void bch2_trans_copy_iter(struct btree_iter *, struct btree_iter *);
345
346 static inline void set_btree_iter_dontneed(struct btree_iter *iter)
347 {
348         iter->path->preserve = false;
349 }
350
351 void *bch2_trans_kmalloc(struct btree_trans *, size_t);
352 void bch2_trans_begin(struct btree_trans *);
353 void bch2_trans_init(struct btree_trans *, struct bch_fs *, unsigned, size_t);
354 int bch2_trans_exit(struct btree_trans *);
355
356 void bch2_btree_trans_to_text(struct printbuf *, struct bch_fs *);
357
358 void bch2_fs_btree_iter_exit(struct bch_fs *);
359 int bch2_fs_btree_iter_init(struct bch_fs *);
360
361 #endif /* _BCACHEFS_BTREE_ITER_H */