1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_ITER_H
3 #define _BCACHEFS_BTREE_ITER_H
6 #include "btree_types.h"
8 static inline void __btree_path_get(struct btree_path *path, bool intent)
11 path->intent_ref += intent;
14 static inline bool __btree_path_put(struct btree_path *path, bool intent)
17 EBUG_ON(!path->intent_ref && intent);
18 path->intent_ref -= intent;
19 return --path->ref == 0;
22 static inline void btree_path_set_dirty(struct btree_path *path,
23 enum btree_path_uptodate u)
25 path->uptodate = max_t(unsigned, path->uptodate, u);
28 static inline struct btree *btree_path_node(struct btree_path *path,
31 return level < BTREE_MAX_DEPTH ? path->l[level].b : NULL;
34 static inline bool btree_node_lock_seq_matches(const struct btree_path *path,
35 const struct btree *b, unsigned level)
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:
44 return path->l[level].lock_seq >> 1 == b->c.lock.state.seq >> 1;
47 static inline struct btree *btree_node_parent(struct btree_path *path,
50 return btree_path_node(path, b->c.level + 1);
53 static inline int btree_iter_err(const struct btree_iter *iter)
55 return iter->flags & BTREE_ITER_ERROR ? -EIO : 0;
58 /* Iterate over paths within a transaction: */
60 void __bch2_btree_trans_sort_paths(struct btree_trans *);
62 static inline void btree_trans_sort_paths(struct btree_trans *trans)
64 if (!IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
67 __bch2_btree_trans_sort_paths(trans);
70 static inline struct btree_path *
71 __trans_next_path(struct btree_trans *trans, unsigned idx)
75 if (idx == BTREE_ITER_MAX)
78 l = trans->paths_allocated >> idx;
83 EBUG_ON(idx >= BTREE_ITER_MAX);
84 EBUG_ON(trans->paths[idx].idx != idx);
85 return &trans->paths[idx];
88 #define trans_for_each_path(_trans, _path) \
89 for (_path = __trans_next_path((_trans), 0); \
91 _path = __trans_next_path((_trans), (_path)->idx + 1))
93 static inline struct btree_path *next_btree_path(struct btree_trans *trans, struct btree_path *path)
95 unsigned idx = path ? path->sorted_idx + 1 : 0;
97 EBUG_ON(idx > trans->nr_sorted);
99 return idx < trans->nr_sorted
100 ? trans->paths + trans->sorted[idx]
104 static inline struct btree_path *prev_btree_path(struct btree_trans *trans, struct btree_path *path)
106 unsigned idx = path ? path->sorted_idx : trans->nr_sorted;
109 ? trans->paths + trans->sorted[idx - 1]
113 #define trans_for_each_path_inorder(_trans, _path, _i) \
115 ((_path) = (_trans)->paths + trans->sorted[_i]), (_i) < (_trans)->nr_sorted;\
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;\
123 static inline bool __path_has_node(const struct btree_path *path,
124 const struct btree *b)
126 return path->l[b->c.level].b == b &&
127 btree_node_lock_seq_matches(path, b, b->c.level);
130 static inline struct btree_path *
131 __trans_next_path_with_node(struct btree_trans *trans, struct btree *b,
134 struct btree_path *path = __trans_next_path(trans, idx);
136 while (path && !__path_has_node(path, b))
137 path = __trans_next_path(trans, path->idx + 1);
142 #define trans_for_each_path_with_node(_trans, _b, _path) \
143 for (_path = __trans_next_path_with_node((_trans), (_b), 0); \
145 _path = __trans_next_path_with_node((_trans), (_b), \
148 struct btree_path *__bch2_btree_path_make_mut(struct btree_trans *,
149 struct btree_path *, bool);
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)
155 if (path->ref > 1 || path->preserve)
156 path = __bch2_btree_path_make_mut(trans, path, intent);
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 *);
166 #ifdef CONFIG_BCACHEFS_DEBUG
167 void bch2_trans_verify_paths(struct btree_trans *);
168 void bch2_trans_verify_locks(struct btree_trans *);
170 static inline void bch2_trans_verify_paths(struct btree_trans *trans) {}
171 static inline void bch2_trans_verify_locks(struct btree_trans *trans) {}
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);
180 bool bch2_btree_path_relock_intent(struct btree_trans *, struct btree_path *);
182 void bch2_path_put(struct btree_trans *, struct btree_path *, bool);
184 bool bch2_trans_relock(struct btree_trans *);
185 void bch2_trans_unlock(struct btree_trans *);
188 static inline int btree_trans_restart(struct btree_trans *trans)
190 trans->restarted = true;
191 bch2_trans_unlock(trans);
195 bool __bch2_btree_path_upgrade(struct btree_trans *,
196 struct btree_path *, unsigned);
198 static inline bool bch2_btree_path_upgrade(struct btree_trans *trans,
199 struct btree_path *path,
200 unsigned new_locks_want)
202 new_locks_want = min(new_locks_want, BTREE_MAX_DEPTH);
204 return path->locks_want < new_locks_want
205 ? __bch2_btree_path_upgrade(trans, path, new_locks_want)
206 : path->uptodate == BTREE_ITER_UPTODATE;
209 void __bch2_btree_path_downgrade(struct btree_path *, unsigned);
211 static inline void bch2_btree_path_downgrade(struct btree_path *path)
213 unsigned new_locks_want = path->level + !!path->intent_ref;
215 if (path->locks_want > new_locks_want)
216 __bch2_btree_path_downgrade(path, new_locks_want);
219 void bch2_trans_downgrade(struct btree_trans *);
221 void bch2_trans_node_add(struct btree_trans *trans, struct btree *);
222 void bch2_trans_node_reinit_iter(struct btree_trans *, struct btree *);
224 int __must_check __bch2_btree_iter_traverse(struct btree_iter *iter);
225 int __must_check bch2_btree_iter_traverse(struct btree_iter *);
227 struct btree *bch2_btree_iter_peek_node(struct btree_iter *);
228 struct btree *bch2_btree_iter_next_node(struct btree_iter *);
230 struct bkey_s_c bch2_btree_iter_peek(struct btree_iter *);
231 struct bkey_s_c bch2_btree_iter_next(struct btree_iter *);
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 *);
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 *);
240 bool bch2_btree_iter_advance(struct btree_iter *);
241 bool bch2_btree_iter_rewind(struct btree_iter *);
243 static inline void bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
245 if (!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS))
246 new_pos.snapshot = iter->snapshot;
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;
253 if (iter->path->ref == 1)
254 iter->path->should_be_locked = false;
257 static inline void bch2_btree_iter_set_pos_to_extent_start(struct btree_iter *iter)
259 BUG_ON(!(iter->flags & BTREE_ITER_IS_EXTENTS));
260 iter->pos = bkey_start_pos(&iter->k);
263 static inline void bch2_btree_iter_set_snapshot(struct btree_iter *iter, u32 snapshot)
265 struct bpos pos = iter->pos;
267 iter->snapshot = snapshot;
268 pos.snapshot = snapshot;
269 bch2_btree_iter_set_pos(iter, pos);
273 * Unlocks before scheduling
274 * Note: does not revalidate iterator
276 static inline int bch2_trans_cond_resched(struct btree_trans *trans)
278 if (need_resched() || race_fault()) {
279 bch2_trans_unlock(trans);
281 return bch2_trans_relock(trans) ? 0 : -EINTR;
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)); \
293 (_b) = bch2_btree_iter_next_node(&(_iter)))
295 #define for_each_btree_node(_trans, _iter, _btree_id, _start, \
297 __for_each_btree_node(_trans, _iter, _btree_id, _start, \
300 static inline struct bkey_s_c __bch2_btree_iter_peek(struct btree_iter *iter,
303 return flags & BTREE_ITER_SLOTS
304 ? bch2_btree_iter_peek_slot(iter)
305 : bch2_btree_iter_peek(iter);
308 static inline struct bkey_s_c __bch2_btree_iter_next(struct btree_iter *iter,
311 return flags & BTREE_ITER_SLOTS
312 ? bch2_btree_iter_next_slot(iter)
313 : bch2_btree_iter_next(iter);
316 static inline int bkey_err(struct bkey_s_c k)
318 return PTR_ERR_OR_ZERO(k.k);
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))
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))
334 /* new multiple iterator interface: */
336 void bch2_dump_trans_paths_updates(struct btree_trans *);
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 *);
346 static inline void set_btree_iter_dontneed(struct btree_iter *iter)
348 iter->path->preserve = false;
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 *);
356 void bch2_btree_trans_to_text(struct printbuf *, struct bch_fs *);
358 void bch2_fs_btree_iter_exit(struct bch_fs *);
359 int bch2_fs_btree_iter_init(struct bch_fs *);
361 #endif /* _BCACHEFS_BTREE_ITER_H */