Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
3 | #include "bcachefs.h" | |
4 | #include "bkey_methods.h" | |
07a1006a | 5 | #include "bkey_buf.h" |
1c6fdbd8 KO |
6 | #include "btree_cache.h" |
7 | #include "btree_iter.h" | |
401585fe | 8 | #include "btree_journal_iter.h" |
2ca88e5a | 9 | #include "btree_key_cache.h" |
1c6fdbd8 | 10 | #include "btree_locking.h" |
57b0b3db | 11 | #include "btree_update.h" |
1c6fdbd8 | 12 | #include "debug.h" |
50dc0f69 | 13 | #include "error.h" |
1c6fdbd8 | 14 | #include "extents.h" |
2ca88e5a | 15 | #include "journal.h" |
67997234 | 16 | #include "journal_io.h" |
35d5aff2 | 17 | #include "replicas.h" |
8e877caa | 18 | #include "snapshot.h" |
1c6fdbd8 KO |
19 | #include "trace.h" |
20 | ||
a0cb8d78 | 21 | #include <linux/random.h> |
1c6fdbd8 KO |
22 | #include <linux/prefetch.h> |
23 | ||
67e0dd8f KO |
24 | static inline void btree_path_list_remove(struct btree_trans *, struct btree_path *); |
25 | static inline void btree_path_list_add(struct btree_trans *, struct btree_path *, | |
26 | struct btree_path *); | |
27 | ||
ee2c6ea7 KO |
28 | static inline unsigned long btree_iter_ip_allocated(struct btree_iter *iter) |
29 | { | |
94c69faf | 30 | #ifdef TRACK_PATH_ALLOCATED |
ee2c6ea7 KO |
31 | return iter->ip_allocated; |
32 | #else | |
33 | return 0; | |
34 | #endif | |
35 | } | |
36 | ||
67e0dd8f KO |
37 | static struct btree_path *btree_path_alloc(struct btree_trans *, struct btree_path *); |
38 | ||
39 | static inline int __btree_path_cmp(const struct btree_path *l, | |
40 | enum btree_id r_btree_id, | |
41 | bool r_cached, | |
42 | struct bpos r_pos, | |
43 | unsigned r_level) | |
0423fb71 | 44 | { |
7abda8c1 KO |
45 | /* |
46 | * Must match lock ordering as defined by __bch2_btree_node_lock: | |
47 | */ | |
67e0dd8f | 48 | return cmp_int(l->btree_id, r_btree_id) ?: |
32b26e8c | 49 | cmp_int((int) l->cached, (int) r_cached) ?: |
67e0dd8f KO |
50 | bpos_cmp(l->pos, r_pos) ?: |
51 | -cmp_int(l->level, r_level); | |
52 | } | |
53 | ||
54 | static inline int btree_path_cmp(const struct btree_path *l, | |
55 | const struct btree_path *r) | |
56 | { | |
57 | return __btree_path_cmp(l, r->btree_id, r->cached, r->pos, r->level); | |
0423fb71 KO |
58 | } |
59 | ||
e751c01a KO |
60 | static inline struct bpos bkey_successor(struct btree_iter *iter, struct bpos p) |
61 | { | |
e751c01a KO |
62 | /* Are we iterating over keys in all snapshots? */ |
63 | if (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) { | |
64 | p = bpos_successor(p); | |
65 | } else { | |
66 | p = bpos_nosnap_successor(p); | |
67 | p.snapshot = iter->snapshot; | |
68 | } | |
69 | ||
70 | return p; | |
71 | } | |
72 | ||
73 | static inline struct bpos bkey_predecessor(struct btree_iter *iter, struct bpos p) | |
74 | { | |
e751c01a KO |
75 | /* Are we iterating over keys in all snapshots? */ |
76 | if (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) { | |
77 | p = bpos_predecessor(p); | |
78 | } else { | |
79 | p = bpos_nosnap_predecessor(p); | |
80 | p.snapshot = iter->snapshot; | |
81 | } | |
82 | ||
83 | return p; | |
84 | } | |
85 | ||
9626aeb1 | 86 | static inline struct bpos btree_iter_search_key(struct btree_iter *iter) |
a00fd8c5 | 87 | { |
9626aeb1 | 88 | struct bpos pos = iter->pos; |
cbdf24ce | 89 | |
9626aeb1 | 90 | if ((iter->flags & BTREE_ITER_IS_EXTENTS) && |
e88a75eb | 91 | !bkey_eq(pos, POS_MAX)) |
e751c01a | 92 | pos = bkey_successor(iter, pos); |
9626aeb1 | 93 | return pos; |
a00fd8c5 KO |
94 | } |
95 | ||
67e0dd8f | 96 | static inline bool btree_path_pos_before_node(struct btree_path *path, |
e3ecf4f5 KO |
97 | struct btree *b) |
98 | { | |
e88a75eb | 99 | return bpos_lt(path->pos, b->data->min_key); |
e3ecf4f5 KO |
100 | } |
101 | ||
67e0dd8f | 102 | static inline bool btree_path_pos_after_node(struct btree_path *path, |
e3ecf4f5 KO |
103 | struct btree *b) |
104 | { | |
e88a75eb | 105 | return bpos_gt(path->pos, b->key.k.p); |
e3ecf4f5 KO |
106 | } |
107 | ||
67e0dd8f | 108 | static inline bool btree_path_pos_in_node(struct btree_path *path, |
e3ecf4f5 KO |
109 | struct btree *b) |
110 | { | |
67e0dd8f KO |
111 | return path->btree_id == b->c.btree_id && |
112 | !btree_path_pos_before_node(path, b) && | |
113 | !btree_path_pos_after_node(path, b); | |
e3ecf4f5 KO |
114 | } |
115 | ||
1c6fdbd8 KO |
116 | /* Btree iterator: */ |
117 | ||
118 | #ifdef CONFIG_BCACHEFS_DEBUG | |
119 | ||
67e0dd8f KO |
120 | static void bch2_btree_path_verify_cached(struct btree_trans *trans, |
121 | struct btree_path *path) | |
d211b408 KO |
122 | { |
123 | struct bkey_cached *ck; | |
67e0dd8f | 124 | bool locked = btree_node_locked(path, 0); |
d211b408 | 125 | |
67e0dd8f | 126 | if (!bch2_btree_node_relock(trans, path, 0)) |
d211b408 KO |
127 | return; |
128 | ||
67e0dd8f KO |
129 | ck = (void *) path->l[0].b; |
130 | BUG_ON(ck->key.btree_id != path->btree_id || | |
e88a75eb | 131 | !bkey_eq(ck->key.pos, path->pos)); |
d211b408 KO |
132 | |
133 | if (!locked) | |
8bfe14e8 | 134 | btree_node_unlock(trans, path, 0); |
d211b408 KO |
135 | } |
136 | ||
67e0dd8f KO |
137 | static void bch2_btree_path_verify_level(struct btree_trans *trans, |
138 | struct btree_path *path, unsigned level) | |
1c6fdbd8 | 139 | { |
67e0dd8f | 140 | struct btree_path_level *l; |
4ce41957 KO |
141 | struct btree_node_iter tmp; |
142 | bool locked; | |
2dac0eae | 143 | struct bkey_packed *p, *k; |
fa8e94fa KO |
144 | struct printbuf buf1 = PRINTBUF; |
145 | struct printbuf buf2 = PRINTBUF; | |
146 | struct printbuf buf3 = PRINTBUF; | |
2dac0eae | 147 | const char *msg; |
1c6fdbd8 | 148 | |
29364f34 | 149 | if (!bch2_debug_check_iterators) |
f13f5a8c KO |
150 | return; |
151 | ||
67e0dd8f | 152 | l = &path->l[level]; |
4ce41957 | 153 | tmp = l->iter; |
67e0dd8f | 154 | locked = btree_node_locked(path, level); |
4ce41957 | 155 | |
67e0dd8f | 156 | if (path->cached) { |
d211b408 | 157 | if (!level) |
67e0dd8f | 158 | bch2_btree_path_verify_cached(trans, path); |
d211b408 KO |
159 | return; |
160 | } | |
161 | ||
67e0dd8f | 162 | if (!btree_path_node(path, level)) |
2dac0eae KO |
163 | return; |
164 | ||
e9174370 | 165 | if (!bch2_btree_node_relock_notrace(trans, path, level)) |
271a3d3a KO |
166 | return; |
167 | ||
67e0dd8f | 168 | BUG_ON(!btree_path_pos_in_node(path, l->b)); |
2dac0eae | 169 | |
2dac0eae | 170 | bch2_btree_node_iter_verify(&l->iter, l->b); |
1c6fdbd8 KO |
171 | |
172 | /* | |
1ae29c1f | 173 | * For interior nodes, the iterator will have skipped past deleted keys: |
1c6fdbd8 | 174 | */ |
1ae29c1f | 175 | p = level |
c052cf82 | 176 | ? bch2_btree_node_iter_prev(&tmp, l->b) |
2dac0eae KO |
177 | : bch2_btree_node_iter_prev_all(&tmp, l->b); |
178 | k = bch2_btree_node_iter_peek_all(&l->iter, l->b); | |
1c6fdbd8 | 179 | |
67e0dd8f | 180 | if (p && bkey_iter_pos_cmp(l->b, p, &path->pos) >= 0) { |
2dac0eae KO |
181 | msg = "before"; |
182 | goto err; | |
1c6fdbd8 KO |
183 | } |
184 | ||
67e0dd8f | 185 | if (k && bkey_iter_pos_cmp(l->b, k, &path->pos) < 0) { |
2dac0eae KO |
186 | msg = "after"; |
187 | goto err; | |
188 | } | |
f21566f1 | 189 | |
2dac0eae | 190 | if (!locked) |
8bfe14e8 | 191 | btree_node_unlock(trans, path, level); |
2dac0eae KO |
192 | return; |
193 | err: | |
fa8e94fa | 194 | bch2_bpos_to_text(&buf1, path->pos); |
2dac0eae KO |
195 | |
196 | if (p) { | |
197 | struct bkey uk = bkey_unpack_key(l->b, p); | |
a1019576 | 198 | |
fa8e94fa KO |
199 | bch2_bkey_to_text(&buf2, &uk); |
200 | } else { | |
401ec4db | 201 | prt_printf(&buf2, "(none)"); |
2dac0eae | 202 | } |
1c6fdbd8 | 203 | |
2dac0eae KO |
204 | if (k) { |
205 | struct bkey uk = bkey_unpack_key(l->b, k); | |
a1019576 | 206 | |
fa8e94fa KO |
207 | bch2_bkey_to_text(&buf3, &uk); |
208 | } else { | |
401ec4db | 209 | prt_printf(&buf3, "(none)"); |
1c6fdbd8 | 210 | } |
2dac0eae | 211 | |
67e0dd8f KO |
212 | panic("path should be %s key at level %u:\n" |
213 | "path pos %s\n" | |
2dac0eae KO |
214 | "prev key %s\n" |
215 | "cur key %s\n", | |
fa8e94fa | 216 | msg, level, buf1.buf, buf2.buf, buf3.buf); |
1c6fdbd8 KO |
217 | } |
218 | ||
67e0dd8f KO |
219 | static void bch2_btree_path_verify(struct btree_trans *trans, |
220 | struct btree_path *path) | |
1c6fdbd8 | 221 | { |
5aab6635 | 222 | struct bch_fs *c = trans->c; |
2dac0eae | 223 | unsigned i; |
1c6fdbd8 | 224 | |
67e0dd8f KO |
225 | EBUG_ON(path->btree_id >= BTREE_ID_NR); |
226 | ||
227 | for (i = 0; i < (!path->cached ? BTREE_MAX_DEPTH : 1); i++) { | |
228 | if (!path->l[i].b) { | |
d7407292 | 229 | BUG_ON(!path->cached && |
faa6cb6c | 230 | bch2_btree_id_root(c, path->btree_id)->b->c.level > i); |
67e0dd8f KO |
231 | break; |
232 | } | |
233 | ||
234 | bch2_btree_path_verify_level(trans, path, i); | |
235 | } | |
236 | ||
237 | bch2_btree_path_verify_locks(path); | |
238 | } | |
239 | ||
240 | void bch2_trans_verify_paths(struct btree_trans *trans) | |
241 | { | |
242 | struct btree_path *path; | |
243 | ||
67e0dd8f KO |
244 | trans_for_each_path(trans, path) |
245 | bch2_btree_path_verify(trans, path); | |
246 | } | |
247 | ||
248 | static void bch2_btree_iter_verify(struct btree_iter *iter) | |
249 | { | |
250 | struct btree_trans *trans = iter->trans; | |
251 | ||
252 | BUG_ON(iter->btree_id >= BTREE_ID_NR); | |
253 | ||
254 | BUG_ON(!!(iter->flags & BTREE_ITER_CACHED) != iter->path->cached); | |
7e1a3aa9 | 255 | |
e751c01a KO |
256 | BUG_ON((iter->flags & BTREE_ITER_IS_EXTENTS) && |
257 | (iter->flags & BTREE_ITER_ALL_SNAPSHOTS)); | |
258 | ||
f21566f1 | 259 | BUG_ON(!(iter->flags & __BTREE_ITER_ALL_SNAPSHOTS) && |
e751c01a | 260 | (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) && |
9fcdd23b | 261 | !btree_type_has_snapshot_field(iter->btree_id)); |
e751c01a | 262 | |
1f2d9192 KO |
263 | if (iter->update_path) |
264 | bch2_btree_path_verify(trans, iter->update_path); | |
67e0dd8f | 265 | bch2_btree_path_verify(trans, iter->path); |
2dac0eae KO |
266 | } |
267 | ||
7e1a3aa9 KO |
268 | static void bch2_btree_iter_verify_entry_exit(struct btree_iter *iter) |
269 | { | |
a861c722 KO |
270 | BUG_ON((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) && |
271 | !iter->pos.snapshot); | |
272 | ||
e751c01a KO |
273 | BUG_ON(!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS) && |
274 | iter->pos.snapshot != iter->snapshot); | |
275 | ||
e88a75eb KO |
276 | BUG_ON(bkey_lt(iter->pos, bkey_start_pos(&iter->k)) || |
277 | bkey_gt(iter->pos, iter->k.p)); | |
7e1a3aa9 KO |
278 | } |
279 | ||
c075ff70 KO |
280 | static int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k) |
281 | { | |
282 | struct btree_trans *trans = iter->trans; | |
283 | struct btree_iter copy; | |
284 | struct bkey_s_c prev; | |
285 | int ret = 0; | |
286 | ||
287 | if (!bch2_debug_check_iterators) | |
288 | return 0; | |
289 | ||
290 | if (!(iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)) | |
291 | return 0; | |
292 | ||
293 | if (bkey_err(k) || !k.k) | |
294 | return 0; | |
295 | ||
296 | BUG_ON(!bch2_snapshot_is_ancestor(trans->c, | |
297 | iter->snapshot, | |
298 | k.k->p.snapshot)); | |
299 | ||
300 | bch2_trans_iter_init(trans, ©, iter->btree_id, iter->pos, | |
ffa7d262 | 301 | BTREE_ITER_NOPRESERVE| |
c075ff70 KO |
302 | BTREE_ITER_ALL_SNAPSHOTS); |
303 | prev = bch2_btree_iter_prev(©); | |
304 | if (!prev.k) | |
305 | goto out; | |
306 | ||
307 | ret = bkey_err(prev); | |
308 | if (ret) | |
309 | goto out; | |
310 | ||
e88a75eb | 311 | if (bkey_eq(prev.k->p, k.k->p) && |
c075ff70 KO |
312 | bch2_snapshot_is_ancestor(trans->c, iter->snapshot, |
313 | prev.k->p.snapshot) > 0) { | |
fa8e94fa | 314 | struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF; |
c075ff70 | 315 | |
fa8e94fa KO |
316 | bch2_bkey_to_text(&buf1, k.k); |
317 | bch2_bkey_to_text(&buf2, prev.k); | |
c075ff70 KO |
318 | |
319 | panic("iter snap %u\n" | |
320 | "k %s\n" | |
321 | "prev %s\n", | |
322 | iter->snapshot, | |
fa8e94fa | 323 | buf1.buf, buf2.buf); |
c075ff70 KO |
324 | } |
325 | out: | |
326 | bch2_trans_iter_exit(trans, ©); | |
327 | return ret; | |
328 | } | |
329 | ||
32b26e8c KO |
330 | void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id, |
331 | struct bpos pos, bool key_cache) | |
332 | { | |
333 | struct btree_path *path; | |
334 | unsigned idx; | |
fa8e94fa | 335 | struct printbuf buf = PRINTBUF; |
32b26e8c | 336 | |
5c0bb66a KO |
337 | btree_trans_sort_paths(trans); |
338 | ||
32b26e8c KO |
339 | trans_for_each_path_inorder(trans, path, idx) { |
340 | int cmp = cmp_int(path->btree_id, id) ?: | |
341 | cmp_int(path->cached, key_cache); | |
342 | ||
343 | if (cmp > 0) | |
344 | break; | |
345 | if (cmp < 0) | |
346 | continue; | |
347 | ||
d4263e56 | 348 | if (!btree_node_locked(path, 0) || |
32b26e8c KO |
349 | !path->should_be_locked) |
350 | continue; | |
351 | ||
352 | if (!key_cache) { | |
e88a75eb KO |
353 | if (bkey_ge(pos, path->l[0].b->data->min_key) && |
354 | bkey_le(pos, path->l[0].b->key.k.p)) | |
32b26e8c KO |
355 | return; |
356 | } else { | |
e88a75eb | 357 | if (bkey_eq(pos, path->pos)) |
32b26e8c KO |
358 | return; |
359 | } | |
360 | } | |
361 | ||
362 | bch2_dump_trans_paths_updates(trans); | |
fa8e94fa KO |
363 | bch2_bpos_to_text(&buf, pos); |
364 | ||
32b26e8c | 365 | panic("not locked: %s %s%s\n", |
88dfe193 | 366 | bch2_btree_id_str(id), buf.buf, |
32b26e8c KO |
367 | key_cache ? " cached" : ""); |
368 | } | |
369 | ||
271a3d3a KO |
370 | #else |
371 | ||
67e0dd8f KO |
372 | static inline void bch2_btree_path_verify_level(struct btree_trans *trans, |
373 | struct btree_path *path, unsigned l) {} | |
374 | static inline void bch2_btree_path_verify(struct btree_trans *trans, | |
375 | struct btree_path *path) {} | |
7d6f9b64 | 376 | static inline void bch2_btree_iter_verify(struct btree_iter *iter) {} |
7e1a3aa9 | 377 | static inline void bch2_btree_iter_verify_entry_exit(struct btree_iter *iter) {} |
c075ff70 | 378 | static inline int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k) { return 0; } |
271a3d3a | 379 | |
1c6fdbd8 KO |
380 | #endif |
381 | ||
67e0dd8f KO |
382 | /* Btree path: fixups after btree updates */ |
383 | ||
23bbd2bb KO |
384 | static void btree_node_iter_set_set_pos(struct btree_node_iter *iter, |
385 | struct btree *b, | |
386 | struct bset_tree *t, | |
387 | struct bkey_packed *k) | |
388 | { | |
389 | struct btree_node_iter_set *set; | |
390 | ||
391 | btree_node_iter_for_each(iter, set) | |
392 | if (set->end == t->end_offset) { | |
393 | set->k = __btree_node_key_to_offset(b, k); | |
394 | bch2_btree_node_iter_sort(iter, b); | |
395 | return; | |
396 | } | |
397 | ||
398 | bch2_btree_node_iter_push(iter, b, k, btree_bkey_last(b, t)); | |
399 | } | |
400 | ||
67e0dd8f | 401 | static void __bch2_btree_path_fix_key_modified(struct btree_path *path, |
9626aeb1 KO |
402 | struct btree *b, |
403 | struct bkey_packed *where) | |
887c2a4e | 404 | { |
67e0dd8f | 405 | struct btree_path_level *l = &path->l[b->c.level]; |
887c2a4e | 406 | |
9626aeb1 KO |
407 | if (where != bch2_btree_node_iter_peek_all(&l->iter, l->b)) |
408 | return; | |
409 | ||
67e0dd8f | 410 | if (bkey_iter_pos_cmp(l->b, where, &path->pos) < 0) |
9626aeb1 | 411 | bch2_btree_node_iter_advance(&l->iter, l->b); |
887c2a4e KO |
412 | } |
413 | ||
67e0dd8f | 414 | void bch2_btree_path_fix_key_modified(struct btree_trans *trans, |
887c2a4e KO |
415 | struct btree *b, |
416 | struct bkey_packed *where) | |
417 | { | |
67e0dd8f | 418 | struct btree_path *path; |
887c2a4e | 419 | |
67e0dd8f KO |
420 | trans_for_each_path_with_node(trans, b, path) { |
421 | __bch2_btree_path_fix_key_modified(path, b, where); | |
422 | bch2_btree_path_verify_level(trans, path, b->c.level); | |
887c2a4e KO |
423 | } |
424 | } | |
425 | ||
67e0dd8f KO |
426 | static void __bch2_btree_node_iter_fix(struct btree_path *path, |
427 | struct btree *b, | |
428 | struct btree_node_iter *node_iter, | |
429 | struct bset_tree *t, | |
430 | struct bkey_packed *where, | |
431 | unsigned clobber_u64s, | |
432 | unsigned new_u64s) | |
1c6fdbd8 KO |
433 | { |
434 | const struct bkey_packed *end = btree_bkey_last(b, t); | |
435 | struct btree_node_iter_set *set; | |
436 | unsigned offset = __btree_node_key_to_offset(b, where); | |
437 | int shift = new_u64s - clobber_u64s; | |
271a3d3a | 438 | unsigned old_end = t->end_offset - shift; |
c0fc30da KO |
439 | unsigned orig_iter_pos = node_iter->data[0].k; |
440 | bool iter_current_key_modified = | |
441 | orig_iter_pos >= offset && | |
442 | orig_iter_pos <= offset + clobber_u64s; | |
1c6fdbd8 KO |
443 | |
444 | btree_node_iter_for_each(node_iter, set) | |
445 | if (set->end == old_end) | |
446 | goto found; | |
447 | ||
448 | /* didn't find the bset in the iterator - might have to readd it: */ | |
449 | if (new_u64s && | |
67e0dd8f | 450 | bkey_iter_pos_cmp(b, where, &path->pos) >= 0) { |
1c6fdbd8 | 451 | bch2_btree_node_iter_push(node_iter, b, where, end); |
c0fc30da KO |
452 | goto fixup_done; |
453 | } else { | |
454 | /* Iterator is after key that changed */ | |
059e4134 | 455 | return; |
1c6fdbd8 | 456 | } |
1c6fdbd8 | 457 | found: |
271a3d3a | 458 | set->end = t->end_offset; |
1c6fdbd8 KO |
459 | |
460 | /* Iterator hasn't gotten to the key that changed yet: */ | |
461 | if (set->k < offset) | |
059e4134 | 462 | return; |
1c6fdbd8 KO |
463 | |
464 | if (new_u64s && | |
67e0dd8f | 465 | bkey_iter_pos_cmp(b, where, &path->pos) >= 0) { |
1c6fdbd8 KO |
466 | set->k = offset; |
467 | } else if (set->k < offset + clobber_u64s) { | |
468 | set->k = offset + new_u64s; | |
469 | if (set->k == set->end) | |
470 | bch2_btree_node_iter_set_drop(node_iter, set); | |
471 | } else { | |
c0fc30da | 472 | /* Iterator is after key that changed */ |
1c6fdbd8 | 473 | set->k = (int) set->k + shift; |
059e4134 | 474 | return; |
1c6fdbd8 KO |
475 | } |
476 | ||
1c6fdbd8 | 477 | bch2_btree_node_iter_sort(node_iter, b); |
c0fc30da KO |
478 | fixup_done: |
479 | if (node_iter->data[0].k != orig_iter_pos) | |
480 | iter_current_key_modified = true; | |
56e0e7c7 | 481 | |
1c6fdbd8 | 482 | /* |
23bbd2bb KO |
483 | * When a new key is added, and the node iterator now points to that |
484 | * key, the iterator might have skipped past deleted keys that should | |
485 | * come after the key the iterator now points to. We have to rewind to | |
c0fc30da KO |
486 | * before those deleted keys - otherwise |
487 | * bch2_btree_node_iter_prev_all() breaks: | |
1c6fdbd8 | 488 | */ |
23bbd2bb | 489 | if (!bch2_btree_node_iter_end(node_iter) && |
c0fc30da | 490 | iter_current_key_modified && |
1ae29c1f | 491 | b->c.level) { |
23bbd2bb KO |
492 | struct bkey_packed *k, *k2, *p; |
493 | ||
494 | k = bch2_btree_node_iter_peek_all(node_iter, b); | |
1c6fdbd8 KO |
495 | |
496 | for_each_bset(b, t) { | |
23bbd2bb KO |
497 | bool set_pos = false; |
498 | ||
499 | if (node_iter->data[0].end == t->end_offset) | |
1c6fdbd8 KO |
500 | continue; |
501 | ||
23bbd2bb KO |
502 | k2 = bch2_btree_node_iter_bset_pos(node_iter, b, t); |
503 | ||
504 | while ((p = bch2_bkey_prev_all(b, t, k2)) && | |
505 | bkey_iter_cmp(b, k, p) < 0) { | |
506 | k2 = p; | |
507 | set_pos = true; | |
1c6fdbd8 | 508 | } |
23bbd2bb KO |
509 | |
510 | if (set_pos) | |
511 | btree_node_iter_set_set_pos(node_iter, | |
512 | b, t, k2); | |
1c6fdbd8 KO |
513 | } |
514 | } | |
515 | } | |
516 | ||
9f6bd307 | 517 | void bch2_btree_node_iter_fix(struct btree_trans *trans, |
67e0dd8f | 518 | struct btree_path *path, |
216c9fac KO |
519 | struct btree *b, |
520 | struct btree_node_iter *node_iter, | |
521 | struct bkey_packed *where, | |
522 | unsigned clobber_u64s, | |
523 | unsigned new_u64s) | |
1c6fdbd8 | 524 | { |
216c9fac | 525 | struct bset_tree *t = bch2_bkey_to_bset_inlined(b, where); |
67e0dd8f | 526 | struct btree_path *linked; |
1c6fdbd8 | 527 | |
67e0dd8f KO |
528 | if (node_iter != &path->l[b->c.level].iter) { |
529 | __bch2_btree_node_iter_fix(path, b, node_iter, t, | |
059e4134 | 530 | where, clobber_u64s, new_u64s); |
2dac0eae | 531 | |
29364f34 | 532 | if (bch2_debug_check_iterators) |
2dac0eae | 533 | bch2_btree_node_iter_verify(node_iter, b); |
059e4134 | 534 | } |
1c6fdbd8 | 535 | |
67e0dd8f | 536 | trans_for_each_path_with_node(trans, b, linked) { |
1c6fdbd8 | 537 | __bch2_btree_node_iter_fix(linked, b, |
059e4134 KO |
538 | &linked->l[b->c.level].iter, t, |
539 | where, clobber_u64s, new_u64s); | |
67e0dd8f | 540 | bch2_btree_path_verify_level(trans, linked, b->c.level); |
059e4134 | 541 | } |
1c6fdbd8 KO |
542 | } |
543 | ||
67e0dd8f KO |
544 | /* Btree path level: pointer to a particular btree node and node iter */ |
545 | ||
546 | static inline struct bkey_s_c __btree_iter_unpack(struct bch_fs *c, | |
547 | struct btree_path_level *l, | |
1c6fdbd8 KO |
548 | struct bkey *u, |
549 | struct bkey_packed *k) | |
550 | { | |
1c6fdbd8 KO |
551 | if (unlikely(!k)) { |
552 | /* | |
553 | * signal to bch2_btree_iter_peek_slot() that we're currently at | |
554 | * a hole | |
555 | */ | |
26609b61 | 556 | u->type = KEY_TYPE_deleted; |
1c6fdbd8 KO |
557 | return bkey_s_c_null; |
558 | } | |
559 | ||
2ce8fbd9 | 560 | return bkey_disassemble(l->b, k, u); |
1c6fdbd8 KO |
561 | } |
562 | ||
67e0dd8f KO |
563 | static inline struct bkey_s_c btree_path_level_peek_all(struct bch_fs *c, |
564 | struct btree_path_level *l, | |
565 | struct bkey *u) | |
1c6fdbd8 | 566 | { |
67e0dd8f | 567 | return __btree_iter_unpack(c, l, u, |
1c6fdbd8 KO |
568 | bch2_btree_node_iter_peek_all(&l->iter, l->b)); |
569 | } | |
570 | ||
67e0dd8f KO |
571 | static inline struct bkey_s_c btree_path_level_peek(struct btree_trans *trans, |
572 | struct btree_path *path, | |
573 | struct btree_path_level *l, | |
574 | struct bkey *u) | |
1c6fdbd8 | 575 | { |
67e0dd8f | 576 | struct bkey_s_c k = __btree_iter_unpack(trans->c, l, u, |
1c6fdbd8 | 577 | bch2_btree_node_iter_peek(&l->iter, l->b)); |
ca58cbd4 | 578 | |
67e0dd8f KO |
579 | path->pos = k.k ? k.k->p : l->b->key.k.p; |
580 | trans->paths_sorted = false; | |
01eed771 | 581 | bch2_btree_path_verify_level(trans, path, l - path->l); |
ca58cbd4 | 582 | return k; |
1c6fdbd8 KO |
583 | } |
584 | ||
67e0dd8f KO |
585 | static inline struct bkey_s_c btree_path_level_prev(struct btree_trans *trans, |
586 | struct btree_path *path, | |
587 | struct btree_path_level *l, | |
588 | struct bkey *u) | |
ccf5a109 | 589 | { |
67e0dd8f | 590 | struct bkey_s_c k = __btree_iter_unpack(trans->c, l, u, |
ccf5a109 | 591 | bch2_btree_node_iter_prev(&l->iter, l->b)); |
ca58cbd4 | 592 | |
67e0dd8f KO |
593 | path->pos = k.k ? k.k->p : l->b->data->min_key; |
594 | trans->paths_sorted = false; | |
01eed771 | 595 | bch2_btree_path_verify_level(trans, path, l - path->l); |
ca58cbd4 | 596 | return k; |
ccf5a109 KO |
597 | } |
598 | ||
67e0dd8f KO |
599 | static inline bool btree_path_advance_to_pos(struct btree_path *path, |
600 | struct btree_path_level *l, | |
a00fd8c5 | 601 | int max_advance) |
1c6fdbd8 | 602 | { |
a00fd8c5 KO |
603 | struct bkey_packed *k; |
604 | int nr_advanced = 0; | |
605 | ||
606 | while ((k = bch2_btree_node_iter_peek_all(&l->iter, l->b)) && | |
67e0dd8f | 607 | bkey_iter_pos_cmp(l->b, k, &path->pos) < 0) { |
a00fd8c5 KO |
608 | if (max_advance > 0 && nr_advanced >= max_advance) |
609 | return false; | |
610 | ||
611 | bch2_btree_node_iter_advance(&l->iter, l->b); | |
612 | nr_advanced++; | |
613 | } | |
614 | ||
615 | return true; | |
1c6fdbd8 KO |
616 | } |
617 | ||
67e0dd8f | 618 | static inline void __btree_path_level_init(struct btree_path *path, |
78cf784e | 619 | unsigned level) |
1c6fdbd8 | 620 | { |
67e0dd8f | 621 | struct btree_path_level *l = &path->l[level]; |
a00fd8c5 | 622 | |
67e0dd8f | 623 | bch2_btree_node_iter_init(&l->iter, l->b, &path->pos); |
1c6fdbd8 | 624 | |
537c49d6 KO |
625 | /* |
626 | * Iterators to interior nodes should always be pointed at the first non | |
627 | * whiteout: | |
628 | */ | |
629 | if (level) | |
630 | bch2_btree_node_iter_peek(&l->iter, l->b); | |
1c6fdbd8 KO |
631 | } |
632 | ||
b2d1d56b KO |
633 | void bch2_btree_path_level_init(struct btree_trans *trans, |
634 | struct btree_path *path, | |
635 | struct btree *b) | |
1c6fdbd8 | 636 | { |
67e0dd8f | 637 | BUG_ON(path->cached); |
2ca88e5a | 638 | |
67e0dd8f | 639 | EBUG_ON(!btree_path_pos_in_node(path, b)); |
1c6fdbd8 | 640 | |
1fb4fe63 | 641 | path->l[b->c.level].lock_seq = six_lock_seq(&b->c.lock); |
67e0dd8f KO |
642 | path->l[b->c.level].b = b; |
643 | __btree_path_level_init(path, b->c.level); | |
1c6fdbd8 KO |
644 | } |
645 | ||
67e0dd8f KO |
646 | /* Btree path: fixups after btree node updates: */ |
647 | ||
08f78031 KO |
648 | static void bch2_trans_revalidate_updates_in_node(struct btree_trans *trans, struct btree *b) |
649 | { | |
650 | struct bch_fs *c = trans->c; | |
651 | struct btree_insert_entry *i; | |
652 | ||
653 | trans_for_each_update(trans, i) | |
654 | if (!i->cached && | |
655 | i->level == b->c.level && | |
656 | i->btree_id == b->c.btree_id && | |
657 | bpos_cmp(i->k->k.p, b->data->min_key) >= 0 && | |
658 | bpos_cmp(i->k->k.p, b->data->max_key) <= 0) { | |
659 | i->old_v = bch2_btree_path_peek_slot(i->path, &i->old_k).v; | |
660 | ||
661 | if (unlikely(trans->journal_replay_not_finished)) { | |
662 | struct bkey_i *j_k = | |
663 | bch2_journal_keys_peek_slot(c, i->btree_id, i->level, | |
664 | i->k->k.p); | |
665 | ||
666 | if (j_k) { | |
667 | i->old_k = j_k->k; | |
668 | i->old_v = &j_k->v; | |
669 | } | |
670 | } | |
671 | } | |
672 | } | |
673 | ||
1c6fdbd8 KO |
674 | /* |
675 | * A btree node is being replaced - update the iterator to point to the new | |
676 | * node: | |
677 | */ | |
f3360005 KO |
678 | void bch2_trans_node_add(struct btree_trans *trans, |
679 | struct btree_path *path, | |
680 | struct btree *b) | |
1c6fdbd8 | 681 | { |
f3360005 | 682 | struct btree_path *prev; |
1c6fdbd8 | 683 | |
f3360005 KO |
684 | BUG_ON(!btree_path_pos_in_node(path, b)); |
685 | ||
686 | while ((prev = prev_btree_path(trans, path)) && | |
687 | btree_path_pos_in_node(prev, b)) | |
688 | path = prev; | |
689 | ||
690 | for (; | |
691 | path && btree_path_pos_in_node(path, b); | |
692 | path = next_btree_path(trans, path)) | |
693 | if (path->uptodate == BTREE_ITER_UPTODATE && !path->cached) { | |
1d3ecd7e KO |
694 | enum btree_node_locked_type t = |
695 | btree_lock_want(path, b->c.level); | |
1c6fdbd8 | 696 | |
38474c26 | 697 | if (t != BTREE_NODE_UNLOCKED) { |
8bfe14e8 | 698 | btree_node_unlock(trans, path, b->c.level); |
c43a6ef9 | 699 | six_lock_increment(&b->c.lock, (enum six_lock_type) t); |
5b7fbdcd | 700 | mark_btree_node_locked(trans, path, b->c.level, t); |
1c6fdbd8 KO |
701 | } |
702 | ||
e4215d0f | 703 | bch2_btree_path_level_init(trans, path, b); |
1c6fdbd8 | 704 | } |
08f78031 KO |
705 | |
706 | bch2_trans_revalidate_updates_in_node(trans, b); | |
1c6fdbd8 KO |
707 | } |
708 | ||
1c6fdbd8 KO |
709 | /* |
710 | * A btree node has been modified in such a way as to invalidate iterators - fix | |
711 | * them: | |
712 | */ | |
f7a966a3 | 713 | void bch2_trans_node_reinit_iter(struct btree_trans *trans, struct btree *b) |
1c6fdbd8 | 714 | { |
67e0dd8f | 715 | struct btree_path *path; |
1c6fdbd8 | 716 | |
67e0dd8f KO |
717 | trans_for_each_path_with_node(trans, b, path) |
718 | __btree_path_level_init(path, b->c.level); | |
08f78031 KO |
719 | |
720 | bch2_trans_revalidate_updates_in_node(trans, b); | |
1c6fdbd8 KO |
721 | } |
722 | ||
67e0dd8f KO |
723 | /* Btree path: traverse, set_pos: */ |
724 | ||
67e0dd8f KO |
725 | static inline int btree_path_lock_root(struct btree_trans *trans, |
726 | struct btree_path *path, | |
a301dc38 KO |
727 | unsigned depth_want, |
728 | unsigned long trace_ip) | |
1c6fdbd8 | 729 | { |
e5af273f | 730 | struct bch_fs *c = trans->c; |
faa6cb6c | 731 | struct btree *b, **rootp = &bch2_btree_id_root(c, path->btree_id)->b; |
1c6fdbd8 KO |
732 | enum six_lock_type lock_type; |
733 | unsigned i; | |
549d173c | 734 | int ret; |
1c6fdbd8 | 735 | |
67e0dd8f | 736 | EBUG_ON(path->nodes_locked); |
1c6fdbd8 KO |
737 | |
738 | while (1) { | |
bd2bb273 | 739 | b = READ_ONCE(*rootp); |
67e0dd8f | 740 | path->level = READ_ONCE(b->c.level); |
1c6fdbd8 | 741 | |
67e0dd8f | 742 | if (unlikely(path->level < depth_want)) { |
1c6fdbd8 KO |
743 | /* |
744 | * the root is at a lower depth than the depth we want: | |
745 | * got to the end of the btree, or we're walking nodes | |
746 | * greater than some depth and there are no nodes >= | |
747 | * that depth | |
748 | */ | |
67e0dd8f KO |
749 | path->level = depth_want; |
750 | for (i = path->level; i < BTREE_MAX_DEPTH; i++) | |
751 | path->l[i].b = NULL; | |
8812600c | 752 | return 1; |
1c6fdbd8 KO |
753 | } |
754 | ||
67e0dd8f | 755 | lock_type = __btree_lock_want(path, path->level); |
0d7009d7 KO |
756 | ret = btree_node_lock(trans, path, &b->c, |
757 | path->level, lock_type, trace_ip); | |
549d173c KO |
758 | if (unlikely(ret)) { |
759 | if (bch2_err_matches(ret, BCH_ERR_lock_fail_root_changed)) | |
760 | continue; | |
761 | if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) | |
762 | return ret; | |
763 | BUG(); | |
e5af273f | 764 | } |
1c6fdbd8 | 765 | |
bd2bb273 | 766 | if (likely(b == READ_ONCE(*rootp) && |
67e0dd8f | 767 | b->c.level == path->level && |
1c6fdbd8 | 768 | !race_fault())) { |
67e0dd8f | 769 | for (i = 0; i < path->level; i++) |
315c9ba6 | 770 | path->l[i].b = ERR_PTR(-BCH_ERR_no_btree_node_lock_root); |
67e0dd8f KO |
771 | path->l[path->level].b = b; |
772 | for (i = path->level + 1; i < BTREE_MAX_DEPTH; i++) | |
773 | path->l[i].b = NULL; | |
774 | ||
5b7fbdcd KO |
775 | mark_btree_node_locked(trans, path, path->level, |
776 | (enum btree_node_locked_type) lock_type); | |
e4215d0f | 777 | bch2_btree_path_level_init(trans, path, b); |
1c6fdbd8 | 778 | return 0; |
1c6fdbd8 KO |
779 | } |
780 | ||
c43a6ef9 | 781 | six_unlock_type(&b->c.lock, lock_type); |
1c6fdbd8 KO |
782 | } |
783 | } | |
784 | ||
785 | noinline | |
67e0dd8f | 786 | static int btree_path_prefetch(struct btree_trans *trans, struct btree_path *path) |
1c6fdbd8 | 787 | { |
9f6bd307 | 788 | struct bch_fs *c = trans->c; |
67e0dd8f | 789 | struct btree_path_level *l = path_l(path); |
1c6fdbd8 KO |
790 | struct btree_node_iter node_iter = l->iter; |
791 | struct bkey_packed *k; | |
07a1006a | 792 | struct bkey_buf tmp; |
3c471b65 | 793 | unsigned nr = test_bit(BCH_FS_started, &c->flags) |
67e0dd8f KO |
794 | ? (path->level > 1 ? 0 : 2) |
795 | : (path->level > 1 ? 1 : 16); | |
796 | bool was_locked = btree_node_locked(path, path->level); | |
8b3e9bd6 | 797 | int ret = 0; |
1c6fdbd8 | 798 | |
07a1006a KO |
799 | bch2_bkey_buf_init(&tmp); |
800 | ||
dccedaaa | 801 | while (nr-- && !ret) { |
67e0dd8f | 802 | if (!bch2_btree_node_relock(trans, path, path->level)) |
07a1006a | 803 | break; |
1c6fdbd8 KO |
804 | |
805 | bch2_btree_node_iter_advance(&node_iter, l->b); | |
806 | k = bch2_btree_node_iter_peek(&node_iter, l->b); | |
807 | if (!k) | |
808 | break; | |
809 | ||
07a1006a | 810 | bch2_bkey_buf_unpack(&tmp, c, l->b, k); |
1306f87d | 811 | ret = bch2_btree_node_prefetch(trans, path, tmp.k, path->btree_id, |
67e0dd8f | 812 | path->level - 1); |
1c6fdbd8 KO |
813 | } |
814 | ||
815 | if (!was_locked) | |
8bfe14e8 | 816 | btree_node_unlock(trans, path, path->level); |
07a1006a KO |
817 | |
818 | bch2_bkey_buf_exit(&tmp, c); | |
8b3e9bd6 | 819 | return ret; |
1c6fdbd8 KO |
820 | } |
821 | ||
5222a460 KO |
822 | static int btree_path_prefetch_j(struct btree_trans *trans, struct btree_path *path, |
823 | struct btree_and_journal_iter *jiter) | |
824 | { | |
825 | struct bch_fs *c = trans->c; | |
826 | struct bkey_s_c k; | |
827 | struct bkey_buf tmp; | |
3c471b65 | 828 | unsigned nr = test_bit(BCH_FS_started, &c->flags) |
5222a460 KO |
829 | ? (path->level > 1 ? 0 : 2) |
830 | : (path->level > 1 ? 1 : 16); | |
831 | bool was_locked = btree_node_locked(path, path->level); | |
832 | int ret = 0; | |
833 | ||
834 | bch2_bkey_buf_init(&tmp); | |
835 | ||
dccedaaa | 836 | while (nr-- && !ret) { |
5222a460 KO |
837 | if (!bch2_btree_node_relock(trans, path, path->level)) |
838 | break; | |
839 | ||
840 | bch2_btree_and_journal_iter_advance(jiter); | |
841 | k = bch2_btree_and_journal_iter_peek(jiter); | |
842 | if (!k.k) | |
843 | break; | |
844 | ||
845 | bch2_bkey_buf_reassemble(&tmp, c, k); | |
1306f87d | 846 | ret = bch2_btree_node_prefetch(trans, path, tmp.k, path->btree_id, |
5222a460 KO |
847 | path->level - 1); |
848 | } | |
849 | ||
850 | if (!was_locked) | |
8bfe14e8 | 851 | btree_node_unlock(trans, path, path->level); |
5222a460 KO |
852 | |
853 | bch2_bkey_buf_exit(&tmp, c); | |
854 | return ret; | |
855 | } | |
856 | ||
78cf784e | 857 | static noinline void btree_node_mem_ptr_set(struct btree_trans *trans, |
67e0dd8f | 858 | struct btree_path *path, |
72141e1f KO |
859 | unsigned plevel, struct btree *b) |
860 | { | |
67e0dd8f KO |
861 | struct btree_path_level *l = &path->l[plevel]; |
862 | bool locked = btree_node_locked(path, plevel); | |
72141e1f KO |
863 | struct bkey_packed *k; |
864 | struct bch_btree_ptr_v2 *bp; | |
865 | ||
67e0dd8f | 866 | if (!bch2_btree_node_relock(trans, path, plevel)) |
72141e1f KO |
867 | return; |
868 | ||
869 | k = bch2_btree_node_iter_peek_all(&l->iter, l->b); | |
870 | BUG_ON(k->type != KEY_TYPE_btree_ptr_v2); | |
871 | ||
872 | bp = (void *) bkeyp_val(&l->b->format, k); | |
873 | bp->mem_ptr = (unsigned long)b; | |
874 | ||
875 | if (!locked) | |
8bfe14e8 | 876 | btree_node_unlock(trans, path, plevel); |
72141e1f KO |
877 | } |
878 | ||
5222a460 KO |
879 | static noinline int btree_node_iter_and_journal_peek(struct btree_trans *trans, |
880 | struct btree_path *path, | |
881 | unsigned flags, | |
882 | struct bkey_buf *out) | |
883 | { | |
884 | struct bch_fs *c = trans->c; | |
885 | struct btree_path_level *l = path_l(path); | |
886 | struct btree_and_journal_iter jiter; | |
887 | struct bkey_s_c k; | |
888 | int ret = 0; | |
889 | ||
890 | __bch2_btree_and_journal_iter_init_node_iter(&jiter, c, l->b, l->iter, path->pos); | |
891 | ||
892 | k = bch2_btree_and_journal_iter_peek(&jiter); | |
893 | ||
894 | bch2_bkey_buf_reassemble(out, c, k); | |
895 | ||
896 | if (flags & BTREE_ITER_PREFETCH) | |
897 | ret = btree_path_prefetch_j(trans, path, &jiter); | |
898 | ||
899 | bch2_btree_and_journal_iter_exit(&jiter); | |
900 | return ret; | |
901 | } | |
902 | ||
67e0dd8f KO |
903 | static __always_inline int btree_path_down(struct btree_trans *trans, |
904 | struct btree_path *path, | |
905 | unsigned flags, | |
a301dc38 | 906 | unsigned long trace_ip) |
1c6fdbd8 | 907 | { |
6e075b54 | 908 | struct bch_fs *c = trans->c; |
67e0dd8f | 909 | struct btree_path_level *l = path_l(path); |
1c6fdbd8 | 910 | struct btree *b; |
67e0dd8f KO |
911 | unsigned level = path->level - 1; |
912 | enum six_lock_type lock_type = __btree_lock_want(path, level); | |
07a1006a KO |
913 | struct bkey_buf tmp; |
914 | int ret; | |
1c6fdbd8 | 915 | |
67e0dd8f | 916 | EBUG_ON(!btree_node_locked(path, path->level)); |
1c6fdbd8 | 917 | |
07a1006a | 918 | bch2_bkey_buf_init(&tmp); |
5222a460 KO |
919 | |
920 | if (unlikely(trans->journal_replay_not_finished)) { | |
921 | ret = btree_node_iter_and_journal_peek(trans, path, flags, &tmp); | |
922 | if (ret) | |
923 | goto err; | |
924 | } else { | |
925 | bch2_bkey_buf_unpack(&tmp, c, l->b, | |
926 | bch2_btree_node_iter_peek(&l->iter, l->b)); | |
927 | ||
928 | if (flags & BTREE_ITER_PREFETCH) { | |
929 | ret = btree_path_prefetch(trans, path); | |
930 | if (ret) | |
931 | goto err; | |
932 | } | |
933 | } | |
1c6fdbd8 | 934 | |
67e0dd8f | 935 | b = bch2_btree_node_get(trans, path, tmp.k, level, lock_type, trace_ip); |
07a1006a KO |
936 | ret = PTR_ERR_OR_ZERO(b); |
937 | if (unlikely(ret)) | |
938 | goto err; | |
1c6fdbd8 | 939 | |
5222a460 KO |
940 | if (likely(!trans->journal_replay_not_finished && |
941 | tmp.k->k.type == KEY_TYPE_btree_ptr_v2) && | |
07a1006a | 942 | unlikely(b != btree_node_mem_ptr(tmp.k))) |
67e0dd8f | 943 | btree_node_mem_ptr_set(trans, path, level + 1, b); |
72141e1f | 944 | |
67e0dd8f | 945 | if (btree_node_read_locked(path, level + 1)) |
8bfe14e8 | 946 | btree_node_unlock(trans, path, level + 1); |
223b560e | 947 | |
5b7fbdcd KO |
948 | mark_btree_node_locked(trans, path, level, |
949 | (enum btree_node_locked_type) lock_type); | |
67e0dd8f | 950 | path->level = level; |
e4215d0f | 951 | bch2_btree_path_level_init(trans, path, b); |
c205321b | 952 | |
67e0dd8f | 953 | bch2_btree_path_verify_locks(path); |
07a1006a KO |
954 | err: |
955 | bch2_bkey_buf_exit(&tmp, c); | |
956 | return ret; | |
1c6fdbd8 KO |
957 | } |
958 | ||
1c6fdbd8 | 959 | |
8f9ad91a | 960 | static int bch2_btree_path_traverse_all(struct btree_trans *trans) |
1c6fdbd8 | 961 | { |
e542029e | 962 | struct bch_fs *c = trans->c; |
0d7009d7 | 963 | struct btree_path *path; |
8f9ad91a KO |
964 | unsigned long trace_ip = _RET_IP_; |
965 | int i, ret = 0; | |
e542029e | 966 | |
2ca88e5a | 967 | if (trans->in_traverse_all) |
549d173c | 968 | return -BCH_ERR_transaction_restart_in_traverse_all; |
2ca88e5a KO |
969 | |
970 | trans->in_traverse_all = true; | |
971 | retry_all: | |
549d173c | 972 | trans->restarted = 0; |
5e2d8be8 | 973 | trans->last_restarted_ip = 0; |
e5af273f | 974 | |
67e0dd8f KO |
975 | trans_for_each_path(trans, path) |
976 | path->should_be_locked = false; | |
e542029e | 977 | |
67e0dd8f | 978 | btree_trans_sort_paths(trans); |
2527dd91 | 979 | |
58fbf808 | 980 | bch2_trans_unlock(trans); |
a301dc38 | 981 | cond_resched(); |
1c6fdbd8 | 982 | |
8f9ad91a | 983 | if (unlikely(trans->memory_allocation_failure)) { |
1c6fdbd8 KO |
984 | struct closure cl; |
985 | ||
986 | closure_init_stack(&cl); | |
987 | ||
988 | do { | |
a564c9fa | 989 | ret = bch2_btree_cache_cannibalize_lock(trans, &cl); |
1c6fdbd8 KO |
990 | closure_sync(&cl); |
991 | } while (ret); | |
992 | } | |
993 | ||
1c6fdbd8 | 994 | /* Now, redo traversals in correct order: */ |
0423fb71 KO |
995 | i = 0; |
996 | while (i < trans->nr_sorted) { | |
67e0dd8f | 997 | path = trans->paths + trans->sorted[i]; |
2ca88e5a | 998 | |
33aa419d KO |
999 | /* |
1000 | * Traversing a path can cause another path to be added at about | |
1001 | * the same position: | |
1002 | */ | |
1003 | if (path->uptodate) { | |
c82ed304 | 1004 | __btree_path_get(path, false); |
4e3d1899 | 1005 | ret = bch2_btree_path_traverse_one(trans, path, 0, _THIS_IP_); |
c82ed304 KO |
1006 | __btree_path_put(path, false); |
1007 | ||
549d173c | 1008 | if (bch2_err_matches(ret, BCH_ERR_transaction_restart) || |
65d48e35 | 1009 | bch2_err_matches(ret, ENOMEM)) |
33aa419d | 1010 | goto retry_all; |
a897ef68 KO |
1011 | if (ret) |
1012 | goto err; | |
33aa419d | 1013 | } else { |
0423fb71 | 1014 | i++; |
33aa419d | 1015 | } |
e542029e | 1016 | } |
0423fb71 KO |
1017 | |
1018 | /* | |
ee94c413 KO |
1019 | * We used to assert that all paths had been traversed here |
1020 | * (path->uptodate < BTREE_ITER_NEED_TRAVERSE); however, since | |
1e81f89b | 1021 | * path->should_be_locked is not set yet, we might have unlocked and |
ee94c413 | 1022 | * then failed to relock a path - that's fine. |
0423fb71 | 1023 | */ |
a897ef68 | 1024 | err: |
a564c9fa | 1025 | bch2_btree_cache_cannibalize_unlock(trans); |
2ca88e5a | 1026 | |
669f87a5 KO |
1027 | trans->in_traverse_all = false; |
1028 | ||
674cfc26 | 1029 | trace_and_count(c, trans_traverse_all, trans, trace_ip); |
1c6fdbd8 | 1030 | return ret; |
bf7b87a4 | 1031 | } |
1c6fdbd8 | 1032 | |
29aa78f1 KO |
1033 | static inline bool btree_path_check_pos_in_node(struct btree_path *path, |
1034 | unsigned l, int check_pos) | |
f4b61341 | 1035 | { |
67e0dd8f | 1036 | if (check_pos < 0 && btree_path_pos_before_node(path, path->l[l].b)) |
f4b61341 | 1037 | return false; |
67e0dd8f | 1038 | if (check_pos > 0 && btree_path_pos_after_node(path, path->l[l].b)) |
f4b61341 KO |
1039 | return false; |
1040 | return true; | |
1041 | } | |
1042 | ||
29aa78f1 KO |
1043 | static inline bool btree_path_good_node(struct btree_trans *trans, |
1044 | struct btree_path *path, | |
1045 | unsigned l, int check_pos) | |
1046 | { | |
1047 | return is_btree_node(path, l) && | |
1048 | bch2_btree_node_relock(trans, path, l) && | |
1049 | btree_path_check_pos_in_node(path, l, check_pos); | |
1050 | } | |
1051 | ||
c4bce586 KO |
1052 | static void btree_path_set_level_down(struct btree_trans *trans, |
1053 | struct btree_path *path, | |
1054 | unsigned new_level) | |
1055 | { | |
1056 | unsigned l; | |
1057 | ||
1058 | path->level = new_level; | |
1059 | ||
1060 | for (l = path->level + 1; l < BTREE_MAX_DEPTH; l++) | |
1061 | if (btree_lock_want(path, l) == BTREE_NODE_UNLOCKED) | |
8bfe14e8 | 1062 | btree_node_unlock(trans, path, l); |
c4bce586 KO |
1063 | |
1064 | btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE); | |
1065 | bch2_btree_path_verify(trans, path); | |
1066 | } | |
1067 | ||
29aa78f1 KO |
1068 | static noinline unsigned __btree_path_up_until_good_node(struct btree_trans *trans, |
1069 | struct btree_path *path, | |
1070 | int check_pos) | |
1c6fdbd8 | 1071 | { |
8ee0134e | 1072 | unsigned i, l = path->level; |
29cea6f4 | 1073 | again: |
67e0dd8f | 1074 | while (btree_path_node(path, l) && |
315c9ba6 KO |
1075 | !btree_path_good_node(trans, path, l, check_pos)) |
1076 | __btree_path_set_level_up(trans, path, l++); | |
1c6fdbd8 | 1077 | |
8ee0134e KO |
1078 | /* If we need intent locks, take them too: */ |
1079 | for (i = l + 1; | |
1080 | i < path->locks_want && btree_path_node(path, i); | |
1081 | i++) | |
29cea6f4 | 1082 | if (!bch2_btree_node_relock(trans, path, i)) { |
315c9ba6 KO |
1083 | while (l <= i) |
1084 | __btree_path_set_level_up(trans, path, l++); | |
29cea6f4 KO |
1085 | goto again; |
1086 | } | |
8ee0134e | 1087 | |
1c6fdbd8 KO |
1088 | return l; |
1089 | } | |
1090 | ||
29aa78f1 KO |
1091 | static inline unsigned btree_path_up_until_good_node(struct btree_trans *trans, |
1092 | struct btree_path *path, | |
1093 | int check_pos) | |
1094 | { | |
1095 | return likely(btree_node_locked(path, path->level) && | |
1096 | btree_path_check_pos_in_node(path, path->level, check_pos)) | |
1097 | ? path->level | |
1098 | : __btree_path_up_until_good_node(trans, path, check_pos); | |
1099 | } | |
1100 | ||
1c6fdbd8 KO |
1101 | /* |
1102 | * This is the main state machine for walking down the btree - walks down to a | |
1103 | * specified depth | |
1104 | * | |
1105 | * Returns 0 on success, -EIO on error (error reading in a btree node). | |
1106 | * | |
1107 | * On error, caller (peek_node()/peek_key()) must return NULL; the error is | |
b7607ce9 | 1108 | * stashed in the iterator and returned from bch2_trans_exit(). |
1c6fdbd8 | 1109 | */ |
4e3d1899 KO |
1110 | int bch2_btree_path_traverse_one(struct btree_trans *trans, |
1111 | struct btree_path *path, | |
1112 | unsigned flags, | |
1113 | unsigned long trace_ip) | |
1c6fdbd8 | 1114 | { |
8ee0134e | 1115 | unsigned depth_want = path->level; |
0f35e086 | 1116 | int ret = -((int) trans->restarted); |
1c6fdbd8 | 1117 | |
549d173c | 1118 | if (unlikely(ret)) |
979735df | 1119 | goto out; |
979735df | 1120 | |
c4accde4 KO |
1121 | if (unlikely(!trans->srcu_held)) |
1122 | bch2_trans_srcu_lock(trans); | |
1123 | ||
e829b717 | 1124 | /* |
67e0dd8f KO |
1125 | * Ensure we obey path->should_be_locked: if it's set, we can't unlock |
1126 | * and re-traverse the path without a transaction restart: | |
e829b717 | 1127 | */ |
67e0dd8f | 1128 | if (path->should_be_locked) { |
549d173c | 1129 | ret = bch2_btree_path_relock(trans, path, trace_ip); |
e829b717 KO |
1130 | goto out; |
1131 | } | |
1132 | ||
67e0dd8f KO |
1133 | if (path->cached) { |
1134 | ret = bch2_btree_path_traverse_cached(trans, path, flags); | |
531a0095 KO |
1135 | goto out; |
1136 | } | |
2ca88e5a | 1137 | |
67e0dd8f | 1138 | if (unlikely(path->level >= BTREE_MAX_DEPTH)) |
531a0095 | 1139 | goto out; |
2ca88e5a | 1140 | |
67e0dd8f | 1141 | path->level = btree_path_up_until_good_node(trans, path, 0); |
1c6fdbd8 | 1142 | |
845cffed KO |
1143 | EBUG_ON(btree_path_node(path, path->level) && |
1144 | !btree_node_locked(path, path->level)); | |
1145 | ||
1c6fdbd8 | 1146 | /* |
67e0dd8f | 1147 | * Note: path->nodes[path->level] may be temporarily NULL here - that |
1c6fdbd8 KO |
1148 | * would indicate to other code that we got to the end of the btree, |
1149 | * here it indicates that relocking the root failed - it's critical that | |
67e0dd8f | 1150 | * btree_path_lock_root() comes next and that it can't fail |
1c6fdbd8 | 1151 | */ |
67e0dd8f KO |
1152 | while (path->level > depth_want) { |
1153 | ret = btree_path_node(path, path->level) | |
1154 | ? btree_path_down(trans, path, flags, trace_ip) | |
1155 | : btree_path_lock_root(trans, path, depth_want, trace_ip); | |
1c6fdbd8 | 1156 | if (unlikely(ret)) { |
531a0095 KO |
1157 | if (ret == 1) { |
1158 | /* | |
f21566f1 KO |
1159 | * No nodes at this level - got to the end of |
1160 | * the btree: | |
531a0095 KO |
1161 | */ |
1162 | ret = 0; | |
1163 | goto out; | |
1164 | } | |
8812600c | 1165 | |
8bfe14e8 | 1166 | __bch2_btree_path_unlock(trans, path); |
67e0dd8f | 1167 | path->level = depth_want; |
315c9ba6 | 1168 | path->l[path->level].b = ERR_PTR(ret); |
531a0095 | 1169 | goto out; |
1c6fdbd8 KO |
1170 | } |
1171 | } | |
1172 | ||
67e0dd8f | 1173 | path->uptodate = BTREE_ITER_UPTODATE; |
531a0095 | 1174 | out: |
434b1c75 KO |
1175 | if (bch2_err_matches(ret, BCH_ERR_transaction_restart) != !!trans->restarted) |
1176 | panic("ret %s (%i) trans->restarted %s (%i)\n", | |
1177 | bch2_err_str(ret), ret, | |
1178 | bch2_err_str(trans->restarted), trans->restarted); | |
67e0dd8f | 1179 | bch2_btree_path_verify(trans, path); |
531a0095 | 1180 | return ret; |
1c6fdbd8 KO |
1181 | } |
1182 | ||
0cc455b3 | 1183 | static inline void btree_path_copy(struct btree_trans *trans, struct btree_path *dst, |
67e0dd8f KO |
1184 | struct btree_path *src) |
1185 | { | |
1186 | unsigned i, offset = offsetof(struct btree_path, pos); | |
1187 | ||
1188 | memcpy((void *) dst + offset, | |
1189 | (void *) src + offset, | |
1190 | sizeof(struct btree_path) - offset); | |
1191 | ||
c81f5836 KO |
1192 | for (i = 0; i < BTREE_MAX_DEPTH; i++) { |
1193 | unsigned t = btree_node_locked_type(dst, i); | |
1194 | ||
1195 | if (t != BTREE_NODE_UNLOCKED) | |
1196 | six_lock_increment(&dst->l[i].b->c.lock, t); | |
1197 | } | |
67e0dd8f KO |
1198 | } |
1199 | ||
c404f203 KO |
1200 | static struct btree_path *btree_path_clone(struct btree_trans *trans, struct btree_path *src, |
1201 | bool intent) | |
67e0dd8f | 1202 | { |
c404f203 | 1203 | struct btree_path *new = btree_path_alloc(trans, src); |
67e0dd8f | 1204 | |
c404f203 | 1205 | btree_path_copy(trans, new, src); |
67e0dd8f | 1206 | __btree_path_get(new, intent); |
c404f203 KO |
1207 | return new; |
1208 | } | |
1209 | ||
0cc455b3 | 1210 | __flatten |
d8648425 | 1211 | struct btree_path *__bch2_btree_path_make_mut(struct btree_trans *trans, |
ee2c6ea7 KO |
1212 | struct btree_path *path, bool intent, |
1213 | unsigned long ip) | |
c404f203 | 1214 | { |
67e0dd8f | 1215 | __btree_path_put(path, intent); |
c404f203 | 1216 | path = btree_path_clone(trans, path, intent); |
67e0dd8f | 1217 | path->preserve = false; |
67e0dd8f KO |
1218 | return path; |
1219 | } | |
1220 | ||
ce91abd6 | 1221 | struct btree_path * __must_check |
67e0dd8f | 1222 | __bch2_btree_path_set_pos(struct btree_trans *trans, |
ee2c6ea7 KO |
1223 | struct btree_path *path, struct bpos new_pos, |
1224 | bool intent, unsigned long ip, int cmp) | |
67e0dd8f | 1225 | { |
12344c7c | 1226 | bch2_trans_verify_not_in_restart(trans); |
67e0dd8f KO |
1227 | EBUG_ON(!path->ref); |
1228 | ||
ee2c6ea7 | 1229 | path = bch2_btree_path_make_mut(trans, path, intent, ip); |
67e0dd8f KO |
1230 | |
1231 | path->pos = new_pos; | |
67e0dd8f KO |
1232 | trans->paths_sorted = false; |
1233 | ||
1234 | if (unlikely(path->cached)) { | |
8bfe14e8 | 1235 | btree_node_unlock(trans, path, 0); |
315c9ba6 | 1236 | path->l[0].b = ERR_PTR(-BCH_ERR_no_btree_node_up); |
67e0dd8f KO |
1237 | btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE); |
1238 | goto out; | |
1239 | } | |
1240 | ||
cd5bd162 | 1241 | unsigned level = btree_path_up_until_good_node(trans, path, cmp); |
67e0dd8f | 1242 | |
5c792e1b KO |
1243 | if (btree_path_node(path, level)) { |
1244 | struct btree_path_level *l = &path->l[level]; | |
1245 | ||
1246 | BUG_ON(!btree_node_locked(path, level)); | |
67e0dd8f KO |
1247 | /* |
1248 | * We might have to skip over many keys, or just a few: try | |
1249 | * advancing the node iterator, and if we have to skip over too | |
1250 | * many keys just reinit it (or if we're rewinding, since that | |
1251 | * is expensive). | |
1252 | */ | |
1253 | if (cmp < 0 || | |
5c792e1b KO |
1254 | !btree_path_advance_to_pos(path, l, 8)) |
1255 | bch2_btree_node_iter_init(&l->iter, l->b, &path->pos); | |
1256 | ||
1257 | /* | |
1258 | * Iterators to interior nodes should always be pointed at the first non | |
1259 | * whiteout: | |
1260 | */ | |
1261 | if (unlikely(level)) | |
1262 | bch2_btree_node_iter_peek(&l->iter, l->b); | |
67e0dd8f KO |
1263 | } |
1264 | ||
5c792e1b | 1265 | if (unlikely(level != path->level)) { |
67e0dd8f | 1266 | btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE); |
8bfe14e8 | 1267 | __bch2_btree_path_unlock(trans, path); |
1d3ecd7e | 1268 | } |
67e0dd8f KO |
1269 | out: |
1270 | bch2_btree_path_verify(trans, path); | |
67e0dd8f KO |
1271 | return path; |
1272 | } | |
1273 | ||
67e0dd8f KO |
1274 | /* Btree path: main interface: */ |
1275 | ||
1276 | static struct btree_path *have_path_at_pos(struct btree_trans *trans, struct btree_path *path) | |
1277 | { | |
a300261a | 1278 | struct btree_path *sib; |
67e0dd8f | 1279 | |
a300261a KO |
1280 | sib = prev_btree_path(trans, path); |
1281 | if (sib && !btree_path_cmp(sib, path)) | |
1282 | return sib; | |
67e0dd8f | 1283 | |
a300261a KO |
1284 | sib = next_btree_path(trans, path); |
1285 | if (sib && !btree_path_cmp(sib, path)) | |
1286 | return sib; | |
67e0dd8f KO |
1287 | |
1288 | return NULL; | |
1289 | } | |
1290 | ||
9a74f63c | 1291 | static struct btree_path *have_node_at_pos(struct btree_trans *trans, struct btree_path *path) |
67e0dd8f | 1292 | { |
a300261a | 1293 | struct btree_path *sib; |
67e0dd8f | 1294 | |
a300261a KO |
1295 | sib = prev_btree_path(trans, path); |
1296 | if (sib && sib->level == path->level && path_l(sib)->b == path_l(path)->b) | |
1297 | return sib; | |
67e0dd8f | 1298 | |
a300261a KO |
1299 | sib = next_btree_path(trans, path); |
1300 | if (sib && sib->level == path->level && path_l(sib)->b == path_l(path)->b) | |
1301 | return sib; | |
67e0dd8f | 1302 | |
9a74f63c | 1303 | return NULL; |
67e0dd8f KO |
1304 | } |
1305 | ||
1306 | static inline void __bch2_path_free(struct btree_trans *trans, struct btree_path *path) | |
08070cba | 1307 | { |
8bfe14e8 | 1308 | __bch2_btree_path_unlock(trans, path); |
67e0dd8f | 1309 | btree_path_list_remove(trans, path); |
6e92d155 | 1310 | __clear_bit(path->idx, trans->paths_allocated); |
08070cba KO |
1311 | } |
1312 | ||
67e0dd8f KO |
1313 | void bch2_path_put(struct btree_trans *trans, struct btree_path *path, bool intent) |
1314 | { | |
1315 | struct btree_path *dup; | |
1316 | ||
1317 | EBUG_ON(trans->paths + path->idx != path); | |
1318 | EBUG_ON(!path->ref); | |
1319 | ||
1320 | if (!__btree_path_put(path, intent)) | |
1321 | return; | |
1322 | ||
a300261a KO |
1323 | dup = path->preserve |
1324 | ? have_path_at_pos(trans, path) | |
1325 | : have_node_at_pos(trans, path); | |
1326 | ||
1327 | if (!dup && !(!path->preserve && !is_btree_node(path, path->level))) | |
1328 | return; | |
67e0dd8f | 1329 | |
9a74f63c | 1330 | if (path->should_be_locked && |
a300261a KO |
1331 | !trans->restarted && |
1332 | (!dup || !bch2_btree_path_relock_norestart(trans, dup, _THIS_IP_))) | |
9a74f63c KO |
1333 | return; |
1334 | ||
a300261a KO |
1335 | if (dup) { |
1336 | dup->preserve |= path->preserve; | |
1337 | dup->should_be_locked |= path->should_be_locked; | |
1338 | } | |
1339 | ||
9a74f63c | 1340 | __bch2_path_free(trans, path); |
67e0dd8f KO |
1341 | } |
1342 | ||
7dcbdbd8 KO |
1343 | static void bch2_path_put_nokeep(struct btree_trans *trans, struct btree_path *path, |
1344 | bool intent) | |
1345 | { | |
1346 | EBUG_ON(trans->paths + path->idx != path); | |
1347 | EBUG_ON(!path->ref); | |
1348 | ||
1349 | if (!__btree_path_put(path, intent)) | |
1350 | return; | |
1351 | ||
1352 | __bch2_path_free(trans, path); | |
1353 | } | |
1354 | ||
12344c7c KO |
1355 | void __noreturn bch2_trans_restart_error(struct btree_trans *trans, u32 restart_count) |
1356 | { | |
1357 | panic("trans->restart_count %u, should be %u, last restarted by %pS\n", | |
1358 | trans->restart_count, restart_count, | |
5e2d8be8 | 1359 | (void *) trans->last_begin_ip); |
12344c7c KO |
1360 | } |
1361 | ||
1362 | void __noreturn bch2_trans_in_restart_error(struct btree_trans *trans) | |
1363 | { | |
1364 | panic("in transaction restart: %s, last restarted by %pS\n", | |
1365 | bch2_err_str(trans->restarted), | |
1366 | (void *) trans->last_restarted_ip); | |
1367 | } | |
1368 | ||
5c0bb66a | 1369 | noinline __cold |
8570d775 | 1370 | void bch2_trans_updates_to_text(struct printbuf *buf, struct btree_trans *trans) |
67e0dd8f | 1371 | { |
67e0dd8f | 1372 | struct btree_insert_entry *i; |
2158fe46 | 1373 | |
401ec4db | 1374 | prt_printf(buf, "transaction updates for %s journal seq %llu", |
8570d775 | 1375 | trans->fn, trans->journal_res.seq); |
401ec4db KO |
1376 | prt_newline(buf); |
1377 | printbuf_indent_add(buf, 2); | |
2158fe46 KO |
1378 | |
1379 | trans_for_each_update(trans, i) { | |
1380 | struct bkey_s_c old = { &i->old_k, i->old_v }; | |
1381 | ||
401ec4db | 1382 | prt_printf(buf, "update: btree=%s cached=%u %pS", |
88dfe193 | 1383 | bch2_btree_id_str(i->btree_id), |
8570d775 KO |
1384 | i->cached, |
1385 | (void *) i->ip_allocated); | |
401ec4db | 1386 | prt_newline(buf); |
8570d775 | 1387 | |
401ec4db | 1388 | prt_printf(buf, " old "); |
8570d775 | 1389 | bch2_bkey_val_to_text(buf, trans->c, old); |
401ec4db | 1390 | prt_newline(buf); |
8570d775 | 1391 | |
401ec4db | 1392 | prt_printf(buf, " new "); |
8570d775 | 1393 | bch2_bkey_val_to_text(buf, trans->c, bkey_i_to_s_c(i->k)); |
401ec4db | 1394 | prt_newline(buf); |
2158fe46 KO |
1395 | } |
1396 | ||
67997234 KO |
1397 | for (struct jset_entry *e = trans->journal_entries; |
1398 | e != btree_trans_journal_entries_top(trans); | |
1399 | e = vstruct_next(e)) | |
1400 | bch2_journal_entry_to_text(buf, trans->c, e); | |
920e69bc | 1401 | |
401ec4db | 1402 | printbuf_indent_sub(buf, 2); |
8570d775 KO |
1403 | } |
1404 | ||
1405 | noinline __cold | |
1406 | void bch2_dump_trans_updates(struct btree_trans *trans) | |
1407 | { | |
1408 | struct printbuf buf = PRINTBUF; | |
1409 | ||
1410 | bch2_trans_updates_to_text(&buf, trans); | |
a8f35428 | 1411 | bch2_print_string_as_lines(KERN_ERR, buf.buf); |
8570d775 | 1412 | printbuf_exit(&buf); |
2158fe46 KO |
1413 | } |
1414 | ||
1415 | noinline __cold | |
5c0bb66a KO |
1416 | void bch2_btree_path_to_text(struct printbuf *out, struct btree_path *path) |
1417 | { | |
1418 | prt_printf(out, "path: idx %2u ref %u:%u %c %c btree=%s l=%u pos ", | |
1419 | path->idx, path->ref, path->intent_ref, | |
1420 | path->preserve ? 'P' : ' ', | |
1421 | path->should_be_locked ? 'S' : ' ', | |
88dfe193 | 1422 | bch2_btree_id_str(path->btree_id), |
5c0bb66a KO |
1423 | path->level); |
1424 | bch2_bpos_to_text(out, path->pos); | |
1425 | ||
1426 | prt_printf(out, " locks %u", path->nodes_locked); | |
94c69faf | 1427 | #ifdef TRACK_PATH_ALLOCATED |
5c0bb66a KO |
1428 | prt_printf(out, " %pS", (void *) path->ip_allocated); |
1429 | #endif | |
1430 | prt_newline(out); | |
1431 | } | |
1432 | ||
73bd774d | 1433 | static noinline __cold |
5c0bb66a KO |
1434 | void __bch2_trans_paths_to_text(struct printbuf *out, struct btree_trans *trans, |
1435 | bool nosort) | |
2158fe46 KO |
1436 | { |
1437 | struct btree_path *path; | |
67e0dd8f | 1438 | unsigned idx; |
67e0dd8f | 1439 | |
5c0bb66a KO |
1440 | if (!nosort) |
1441 | btree_trans_sort_paths(trans); | |
67e0dd8f | 1442 | |
5c0bb66a KO |
1443 | trans_for_each_path_inorder(trans, path, idx) |
1444 | bch2_btree_path_to_text(out, path); | |
1445 | } | |
fa8e94fa | 1446 | |
5c0bb66a KO |
1447 | noinline __cold |
1448 | void bch2_trans_paths_to_text(struct printbuf *out, struct btree_trans *trans) | |
1449 | { | |
1450 | __bch2_trans_paths_to_text(out, trans, false); | |
1451 | } | |
fa8e94fa | 1452 | |
73bd774d | 1453 | static noinline __cold |
5c0bb66a KO |
1454 | void __bch2_dump_trans_paths_updates(struct btree_trans *trans, bool nosort) |
1455 | { | |
1456 | struct printbuf buf = PRINTBUF; | |
1457 | ||
1458 | __bch2_trans_paths_to_text(&buf, trans, nosort); | |
a8f35428 | 1459 | bch2_trans_updates_to_text(&buf, trans); |
67e0dd8f | 1460 | |
a8f35428 | 1461 | bch2_print_string_as_lines(KERN_ERR, buf.buf); |
2158fe46 | 1462 | printbuf_exit(&buf); |
67e0dd8f KO |
1463 | } |
1464 | ||
5c0bb66a KO |
1465 | noinline __cold |
1466 | void bch2_dump_trans_paths_updates(struct btree_trans *trans) | |
1467 | { | |
1468 | __bch2_dump_trans_paths_updates(trans, false); | |
1469 | } | |
1470 | ||
1471 | noinline __cold | |
1472 | static void bch2_trans_update_max_paths(struct btree_trans *trans) | |
1473 | { | |
1474 | struct btree_transaction_stats *s = btree_trans_stats(trans); | |
1475 | struct printbuf buf = PRINTBUF; | |
6e92d155 | 1476 | size_t nr = bitmap_weight(trans->paths_allocated, BTREE_ITER_MAX); |
5c0bb66a KO |
1477 | |
1478 | if (!s) | |
1479 | return; | |
1480 | ||
1481 | bch2_trans_paths_to_text(&buf, trans); | |
1482 | ||
1483 | if (!buf.allocation_failure) { | |
1484 | mutex_lock(&s->lock); | |
6e92d155 KO |
1485 | if (nr > s->nr_max_paths) { |
1486 | s->nr_max_paths = nr; | |
5c0bb66a KO |
1487 | swap(s->max_paths_text, buf.buf); |
1488 | } | |
1489 | mutex_unlock(&s->lock); | |
1490 | } | |
1491 | ||
1492 | printbuf_exit(&buf); | |
1493 | ||
6e92d155 | 1494 | trans->nr_max_paths = nr; |
5c0bb66a KO |
1495 | } |
1496 | ||
e153a0d7 KO |
1497 | noinline __cold |
1498 | int __bch2_btree_trans_too_many_iters(struct btree_trans *trans) | |
1499 | { | |
1500 | if (trace_trans_restart_too_many_iters_enabled()) { | |
1501 | struct printbuf buf = PRINTBUF; | |
1502 | ||
1503 | bch2_trans_paths_to_text(&buf, trans); | |
1504 | trace_trans_restart_too_many_iters(trans, _THIS_IP_, buf.buf); | |
1505 | printbuf_exit(&buf); | |
1506 | } | |
1507 | ||
1508 | count_event(trans->c, trans_restart_too_many_iters); | |
1509 | ||
1510 | return btree_trans_restart(trans, BCH_ERR_transaction_restart_too_many_iters); | |
1511 | } | |
1512 | ||
3f3bc66e KO |
1513 | static noinline void btree_path_overflow(struct btree_trans *trans) |
1514 | { | |
1515 | bch2_dump_trans_paths_updates(trans); | |
6bf3766b | 1516 | panic("trans path overflow\n"); |
3f3bc66e KO |
1517 | } |
1518 | ||
1519 | static inline struct btree_path *btree_path_alloc(struct btree_trans *trans, | |
1520 | struct btree_path *pos) | |
67e0dd8f KO |
1521 | { |
1522 | struct btree_path *path; | |
6e92d155 | 1523 | size_t idx = find_first_zero_bit(trans->paths_allocated, BTREE_ITER_MAX); |
67e0dd8f | 1524 | |
6e92d155 | 1525 | if (unlikely(idx == BTREE_ITER_MAX)) |
3f3bc66e | 1526 | btree_path_overflow(trans); |
67e0dd8f | 1527 | |
6e92d155 | 1528 | BUG_ON(idx > BTREE_ITER_MAX); |
992fa4e6 KO |
1529 | |
1530 | /* | |
1531 | * Do this before marking the new path as allocated, since it won't be | |
1532 | * initialized yet: | |
1533 | */ | |
1534 | if (unlikely(idx > trans->nr_max_paths)) | |
1535 | bch2_trans_update_max_paths(trans); | |
1536 | ||
6e92d155 | 1537 | __set_bit(idx, trans->paths_allocated); |
67e0dd8f KO |
1538 | |
1539 | path = &trans->paths[idx]; | |
67e0dd8f KO |
1540 | path->idx = idx; |
1541 | path->ref = 0; | |
1542 | path->intent_ref = 0; | |
1543 | path->nodes_locked = 0; | |
67e0dd8f KO |
1544 | |
1545 | btree_path_list_add(trans, pos, path); | |
5c0bb66a | 1546 | trans->paths_sorted = false; |
67e0dd8f KO |
1547 | return path; |
1548 | } | |
1549 | ||
f3e1f444 | 1550 | struct btree_path *bch2_path_get(struct btree_trans *trans, |
67e0dd8f KO |
1551 | enum btree_id btree_id, struct bpos pos, |
1552 | unsigned locks_want, unsigned level, | |
ee2c6ea7 | 1553 | unsigned flags, unsigned long ip) |
67e0dd8f | 1554 | { |
807dda8c | 1555 | struct btree_path *path, *path_pos = NULL; |
f3e1f444 KO |
1556 | bool cached = flags & BTREE_ITER_CACHED; |
1557 | bool intent = flags & BTREE_ITER_INTENT; | |
67e0dd8f KO |
1558 | int i; |
1559 | ||
12344c7c | 1560 | bch2_trans_verify_not_in_restart(trans); |
7071878b KO |
1561 | bch2_trans_verify_locks(trans); |
1562 | ||
eb7bd15f | 1563 | btree_trans_sort_paths(trans); |
67e0dd8f | 1564 | |
807dda8c KO |
1565 | trans_for_each_path_inorder(trans, path, i) { |
1566 | if (__btree_path_cmp(path, | |
1567 | btree_id, | |
1568 | cached, | |
1569 | pos, | |
1570 | level) > 0) | |
1571 | break; | |
67e0dd8f | 1572 | |
807dda8c | 1573 | path_pos = path; |
67e0dd8f KO |
1574 | } |
1575 | ||
807dda8c KO |
1576 | if (path_pos && |
1577 | path_pos->cached == cached && | |
1578 | path_pos->btree_id == btree_id && | |
1579 | path_pos->level == level) { | |
1580 | __btree_path_get(path_pos, intent); | |
ee2c6ea7 | 1581 | path = bch2_btree_path_set_pos(trans, path_pos, pos, intent, ip); |
67e0dd8f | 1582 | } else { |
807dda8c KO |
1583 | path = btree_path_alloc(trans, path_pos); |
1584 | path_pos = NULL; | |
67e0dd8f KO |
1585 | |
1586 | __btree_path_get(path, intent); | |
1587 | path->pos = pos; | |
1588 | path->btree_id = btree_id; | |
1589 | path->cached = cached; | |
67e0dd8f KO |
1590 | path->uptodate = BTREE_ITER_NEED_TRAVERSE; |
1591 | path->should_be_locked = false; | |
1592 | path->level = level; | |
1593 | path->locks_want = locks_want; | |
1594 | path->nodes_locked = 0; | |
67e0dd8f | 1595 | for (i = 0; i < ARRAY_SIZE(path->l); i++) |
315c9ba6 | 1596 | path->l[i].b = ERR_PTR(-BCH_ERR_no_btree_node_init); |
94c69faf | 1597 | #ifdef TRACK_PATH_ALLOCATED |
ee2c6ea7 | 1598 | path->ip_allocated = ip; |
67e0dd8f KO |
1599 | #endif |
1600 | trans->paths_sorted = false; | |
1601 | } | |
1602 | ||
f3e1f444 KO |
1603 | if (!(flags & BTREE_ITER_NOPRESERVE)) |
1604 | path->preserve = true; | |
1605 | ||
67e0dd8f KO |
1606 | if (path->intent_ref) |
1607 | locks_want = max(locks_want, level + 1); | |
1608 | ||
1609 | /* | |
1610 | * If the path has locks_want greater than requested, we don't downgrade | |
1611 | * it here - on transaction restart because btree node split needs to | |
1612 | * upgrade locks, we might be putting/getting the iterator again. | |
1613 | * Downgrading iterators only happens via bch2_trans_downgrade(), after | |
1614 | * a successful transaction commit. | |
1615 | */ | |
1616 | ||
1617 | locks_want = min(locks_want, BTREE_MAX_DEPTH); | |
cd5afabe | 1618 | if (locks_want > path->locks_want) |
be9e782d | 1619 | bch2_btree_path_upgrade_noupgrade_sibs(trans, path, locks_want, NULL); |
67e0dd8f | 1620 | |
67e0dd8f KO |
1621 | return path; |
1622 | } | |
1623 | ||
b2d1d56b | 1624 | struct bkey_s_c bch2_btree_path_peek_slot(struct btree_path *path, struct bkey *u) |
67e0dd8f KO |
1625 | { |
1626 | ||
f42238b5 KO |
1627 | struct btree_path_level *l = path_l(path); |
1628 | struct bkey_packed *_k; | |
67e0dd8f KO |
1629 | struct bkey_s_c k; |
1630 | ||
f42238b5 KO |
1631 | if (unlikely(!l->b)) |
1632 | return bkey_s_c_null; | |
f7b6ca23 | 1633 | |
f42238b5 KO |
1634 | EBUG_ON(path->uptodate != BTREE_ITER_UPTODATE); |
1635 | EBUG_ON(!btree_node_locked(path, path->level)); | |
67e0dd8f | 1636 | |
f42238b5 | 1637 | if (!path->cached) { |
f7b6ca23 | 1638 | _k = bch2_btree_node_iter_peek_all(&l->iter, l->b); |
67e0dd8f KO |
1639 | k = _k ? bkey_disassemble(l->b, _k, u) : bkey_s_c_null; |
1640 | ||
e88a75eb | 1641 | EBUG_ON(k.k && bkey_deleted(k.k) && bpos_eq(k.k->p, path->pos)); |
67e0dd8f | 1642 | |
e88a75eb | 1643 | if (!k.k || !bpos_eq(path->pos, k.k->p)) |
67e0dd8f KO |
1644 | goto hole; |
1645 | } else { | |
1646 | struct bkey_cached *ck = (void *) path->l[0].b; | |
1647 | ||
f7b6ca23 KO |
1648 | EBUG_ON(ck && |
1649 | (path->btree_id != ck->key.btree_id || | |
e88a75eb | 1650 | !bkey_eq(path->pos, ck->key.pos))); |
087e53c2 KO |
1651 | if (!ck || !ck->valid) |
1652 | return bkey_s_c_null; | |
67e0dd8f | 1653 | |
2e63e180 | 1654 | *u = ck->k->k; |
67e0dd8f KO |
1655 | k = bkey_i_to_s_c(ck->k); |
1656 | } | |
1657 | ||
1658 | return k; | |
1659 | hole: | |
1660 | bkey_init(u); | |
1661 | u->p = path->pos; | |
1662 | return (struct bkey_s_c) { u, NULL }; | |
1663 | } | |
1664 | ||
1665 | /* Btree iterators: */ | |
1666 | ||
db92f2ea KO |
1667 | int __must_check |
1668 | __bch2_btree_iter_traverse(struct btree_iter *iter) | |
1669 | { | |
1670 | return bch2_btree_path_traverse(iter->trans, iter->path, iter->flags); | |
1671 | } | |
1672 | ||
08070cba KO |
1673 | int __must_check |
1674 | bch2_btree_iter_traverse(struct btree_iter *iter) | |
1675 | { | |
66a0a497 KO |
1676 | int ret; |
1677 | ||
ce91abd6 | 1678 | iter->path = bch2_btree_path_set_pos(iter->trans, iter->path, |
67e0dd8f | 1679 | btree_iter_search_key(iter), |
ee2c6ea7 KO |
1680 | iter->flags & BTREE_ITER_INTENT, |
1681 | btree_iter_ip_allocated(iter)); | |
08070cba | 1682 | |
67e0dd8f | 1683 | ret = bch2_btree_path_traverse(iter->trans, iter->path, iter->flags); |
66a0a497 KO |
1684 | if (ret) |
1685 | return ret; | |
1686 | ||
fd211bc7 | 1687 | btree_path_set_should_be_locked(iter->path); |
66a0a497 | 1688 | return 0; |
08070cba KO |
1689 | } |
1690 | ||
1c6fdbd8 KO |
1691 | /* Iterate across nodes (leaf and interior nodes) */ |
1692 | ||
1693 | struct btree *bch2_btree_iter_peek_node(struct btree_iter *iter) | |
1694 | { | |
502027a8 | 1695 | struct btree_trans *trans = iter->trans; |
f21566f1 | 1696 | struct btree *b = NULL; |
1c6fdbd8 KO |
1697 | int ret; |
1698 | ||
67e0dd8f | 1699 | EBUG_ON(iter->path->cached); |
7e1a3aa9 | 1700 | bch2_btree_iter_verify(iter); |
1c6fdbd8 | 1701 | |
502027a8 | 1702 | ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); |
1c6fdbd8 | 1703 | if (ret) |
d355c6f4 | 1704 | goto err; |
1c6fdbd8 | 1705 | |
67e0dd8f | 1706 | b = btree_path_node(iter->path, iter->path->level); |
1c6fdbd8 | 1707 | if (!b) |
f21566f1 | 1708 | goto out; |
1c6fdbd8 | 1709 | |
e88a75eb | 1710 | BUG_ON(bpos_lt(b->key.k.p, iter->pos)); |
1c6fdbd8 | 1711 | |
f21566f1 | 1712 | bkey_init(&iter->k); |
67e0dd8f | 1713 | iter->k.p = iter->pos = b->key.k.p; |
502027a8 | 1714 | |
ce91abd6 | 1715 | iter->path = bch2_btree_path_set_pos(trans, iter->path, b->key.k.p, |
ee2c6ea7 KO |
1716 | iter->flags & BTREE_ITER_INTENT, |
1717 | btree_iter_ip_allocated(iter)); | |
fd211bc7 | 1718 | btree_path_set_should_be_locked(iter->path); |
f21566f1 KO |
1719 | out: |
1720 | bch2_btree_iter_verify_entry_exit(iter); | |
1721 | bch2_btree_iter_verify(iter); | |
2dac0eae | 1722 | |
1c6fdbd8 | 1723 | return b; |
d355c6f4 KO |
1724 | err: |
1725 | b = ERR_PTR(ret); | |
1726 | goto out; | |
1c6fdbd8 KO |
1727 | } |
1728 | ||
511b629a KO |
1729 | struct btree *bch2_btree_iter_peek_node_and_restart(struct btree_iter *iter) |
1730 | { | |
1731 | struct btree *b; | |
1732 | ||
1733 | while (b = bch2_btree_iter_peek_node(iter), | |
1734 | bch2_err_matches(PTR_ERR_OR_ZERO(b), BCH_ERR_transaction_restart)) | |
1735 | bch2_trans_begin(iter->trans); | |
1736 | ||
1737 | return b; | |
1738 | } | |
1739 | ||
2dac0eae | 1740 | struct btree *bch2_btree_iter_next_node(struct btree_iter *iter) |
1c6fdbd8 | 1741 | { |
67e0dd8f KO |
1742 | struct btree_trans *trans = iter->trans; |
1743 | struct btree_path *path = iter->path; | |
f21566f1 | 1744 | struct btree *b = NULL; |
1c6fdbd8 KO |
1745 | int ret; |
1746 | ||
12344c7c | 1747 | bch2_trans_verify_not_in_restart(trans); |
67e0dd8f | 1748 | EBUG_ON(iter->path->cached); |
7e1a3aa9 | 1749 | bch2_btree_iter_verify(iter); |
1c6fdbd8 | 1750 | |
979735df | 1751 | /* already at end? */ |
67e0dd8f | 1752 | if (!btree_path_node(path, path->level)) |
979735df | 1753 | return NULL; |
1c6fdbd8 | 1754 | |
979735df KO |
1755 | /* got to end? */ |
1756 | if (!btree_path_node(path, path->level + 1)) { | |
8bfe14e8 | 1757 | btree_path_set_level_up(trans, path); |
979735df KO |
1758 | return NULL; |
1759 | } | |
1c6fdbd8 | 1760 | |
979735df | 1761 | if (!bch2_btree_node_relock(trans, path, path->level + 1)) { |
8bfe14e8 | 1762 | __bch2_btree_path_unlock(trans, path); |
315c9ba6 KO |
1763 | path->l[path->level].b = ERR_PTR(-BCH_ERR_no_btree_node_relock); |
1764 | path->l[path->level + 1].b = ERR_PTR(-BCH_ERR_no_btree_node_relock); | |
7071878b | 1765 | btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE); |
674cfc26 | 1766 | trace_and_count(trans->c, trans_restart_relock_next_node, trans, _THIS_IP_, path); |
549d173c | 1767 | ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_relock); |
d355c6f4 | 1768 | goto err; |
979735df | 1769 | } |
1c6fdbd8 | 1770 | |
979735df | 1771 | b = btree_path_node(path, path->level + 1); |
1c6fdbd8 | 1772 | |
e88a75eb | 1773 | if (bpos_eq(iter->pos, b->key.k.p)) { |
315c9ba6 | 1774 | __btree_path_set_level_up(trans, path, path->level++); |
979735df | 1775 | } else { |
1c6fdbd8 KO |
1776 | /* |
1777 | * Haven't gotten to the end of the parent node: go back down to | |
1778 | * the next child node | |
1779 | */ | |
67e0dd8f | 1780 | path = iter->path = |
ce91abd6 | 1781 | bch2_btree_path_set_pos(trans, path, bpos_successor(iter->pos), |
ee2c6ea7 KO |
1782 | iter->flags & BTREE_ITER_INTENT, |
1783 | btree_iter_ip_allocated(iter)); | |
1c6fdbd8 | 1784 | |
c4bce586 | 1785 | btree_path_set_level_down(trans, path, iter->min_depth); |
345ca825 | 1786 | |
67e0dd8f | 1787 | ret = bch2_btree_path_traverse(trans, path, iter->flags); |
d355c6f4 KO |
1788 | if (ret) |
1789 | goto err; | |
1c6fdbd8 | 1790 | |
67e0dd8f | 1791 | b = path->l[path->level].b; |
1c6fdbd8 KO |
1792 | } |
1793 | ||
f21566f1 | 1794 | bkey_init(&iter->k); |
67e0dd8f | 1795 | iter->k.p = iter->pos = b->key.k.p; |
502027a8 | 1796 | |
ce91abd6 | 1797 | iter->path = bch2_btree_path_set_pos(trans, iter->path, b->key.k.p, |
ee2c6ea7 KO |
1798 | iter->flags & BTREE_ITER_INTENT, |
1799 | btree_iter_ip_allocated(iter)); | |
fd211bc7 | 1800 | btree_path_set_should_be_locked(iter->path); |
502027a8 | 1801 | BUG_ON(iter->path->uptodate); |
f21566f1 KO |
1802 | out: |
1803 | bch2_btree_iter_verify_entry_exit(iter); | |
1804 | bch2_btree_iter_verify(iter); | |
2dac0eae | 1805 | |
1c6fdbd8 | 1806 | return b; |
d355c6f4 KO |
1807 | err: |
1808 | b = ERR_PTR(ret); | |
1809 | goto out; | |
1c6fdbd8 KO |
1810 | } |
1811 | ||
1812 | /* Iterate across keys (in leaf nodes only) */ | |
1813 | ||
e0ba3b64 | 1814 | inline bool bch2_btree_iter_advance(struct btree_iter *iter) |
434094be | 1815 | { |
e56978c8 KO |
1816 | struct bpos pos = iter->k.p; |
1817 | bool ret = !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS | |
1818 | ? bpos_eq(pos, SPOS_MAX) | |
1819 | : bkey_eq(pos, SPOS_MAX)); | |
3d495595 | 1820 | |
e56978c8 KO |
1821 | if (ret && !(iter->flags & BTREE_ITER_IS_EXTENTS)) |
1822 | pos = bkey_successor(iter, pos); | |
1823 | bch2_btree_iter_set_pos(iter, pos); | |
1824 | return ret; | |
3d495595 KO |
1825 | } |
1826 | ||
e0ba3b64 | 1827 | inline bool bch2_btree_iter_rewind(struct btree_iter *iter) |
3d495595 KO |
1828 | { |
1829 | struct bpos pos = bkey_start_pos(&iter->k); | |
e88a75eb KO |
1830 | bool ret = !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS |
1831 | ? bpos_eq(pos, POS_MIN) | |
1832 | : bkey_eq(pos, POS_MIN)); | |
3d495595 | 1833 | |
7e1a3aa9 | 1834 | if (ret && !(iter->flags & BTREE_ITER_IS_EXTENTS)) |
e751c01a | 1835 | pos = bkey_predecessor(iter, pos); |
3d495595 | 1836 | bch2_btree_iter_set_pos(iter, pos); |
7e1a3aa9 | 1837 | return ret; |
434094be KO |
1838 | } |
1839 | ||
3763cb95 | 1840 | static noinline |
67e0dd8f | 1841 | struct bkey_i *__bch2_btree_trans_peek_updates(struct btree_iter *iter) |
818664f5 KO |
1842 | { |
1843 | struct btree_insert_entry *i; | |
5288e66a | 1844 | struct bkey_i *ret = NULL; |
818664f5 | 1845 | |
8e6bbc41 | 1846 | trans_for_each_update(iter->trans, i) { |
5288e66a KO |
1847 | if (i->btree_id < iter->btree_id) |
1848 | continue; | |
1849 | if (i->btree_id > iter->btree_id) | |
818664f5 | 1850 | break; |
e88a75eb | 1851 | if (bpos_lt(i->k->k.p, iter->path->pos)) |
5288e66a | 1852 | continue; |
12ce5b7d KO |
1853 | if (i->key_cache_already_flushed) |
1854 | continue; | |
e88a75eb | 1855 | if (!ret || bpos_lt(i->k->k.p, ret->k.p)) |
5288e66a KO |
1856 | ret = i->k; |
1857 | } | |
818664f5 | 1858 | |
5288e66a KO |
1859 | return ret; |
1860 | } | |
1861 | ||
3763cb95 KO |
1862 | static inline struct bkey_i *btree_trans_peek_updates(struct btree_iter *iter) |
1863 | { | |
1864 | return iter->flags & BTREE_ITER_WITH_UPDATES | |
1865 | ? __bch2_btree_trans_peek_updates(iter) | |
1866 | : NULL; | |
1867 | } | |
1868 | ||
73bd774d KO |
1869 | static struct bkey_i *bch2_btree_journal_peek(struct btree_trans *trans, |
1870 | struct btree_iter *iter, | |
1871 | struct bpos end_pos) | |
30525f68 | 1872 | { |
ad9c7992 KO |
1873 | return bch2_journal_keys_peek_upto(trans->c, iter->btree_id, |
1874 | iter->path->level, | |
1875 | iter->path->pos, | |
1876 | end_pos, | |
1877 | &iter->journal_idx); | |
30525f68 KO |
1878 | } |
1879 | ||
5222a460 KO |
1880 | static noinline |
1881 | struct bkey_s_c btree_trans_peek_slot_journal(struct btree_trans *trans, | |
1882 | struct btree_iter *iter) | |
1883 | { | |
30525f68 | 1884 | struct bkey_i *k = bch2_btree_journal_peek(trans, iter, iter->path->pos); |
5222a460 | 1885 | |
5650bb46 | 1886 | if (k) { |
5222a460 KO |
1887 | iter->k = k->k; |
1888 | return bkey_i_to_s_c(k); | |
1889 | } else { | |
1890 | return bkey_s_c_null; | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | static noinline | |
1895 | struct bkey_s_c btree_trans_peek_journal(struct btree_trans *trans, | |
1896 | struct btree_iter *iter, | |
1897 | struct bkey_s_c k) | |
1898 | { | |
1899 | struct bkey_i *next_journal = | |
30525f68 | 1900 | bch2_btree_journal_peek(trans, iter, |
c167f9e5 | 1901 | k.k ? k.k->p : path_l(iter->path)->b->key.k.p); |
5222a460 | 1902 | |
5650bb46 | 1903 | if (next_journal) { |
5222a460 KO |
1904 | iter->k = next_journal->k; |
1905 | k = bkey_i_to_s_c(next_journal); | |
1906 | } | |
1907 | ||
1908 | return k; | |
1909 | } | |
1910 | ||
f7b6ca23 KO |
1911 | /* |
1912 | * Checks btree key cache for key at iter->pos and returns it if present, or | |
1913 | * bkey_s_c_null: | |
1914 | */ | |
1915 | static noinline | |
dcced069 | 1916 | struct bkey_s_c btree_trans_peek_key_cache(struct btree_iter *iter, struct bpos pos) |
f7b6ca23 KO |
1917 | { |
1918 | struct btree_trans *trans = iter->trans; | |
1919 | struct bch_fs *c = trans->c; | |
1920 | struct bkey u; | |
52bf51b9 | 1921 | struct bkey_s_c k; |
f7b6ca23 KO |
1922 | int ret; |
1923 | ||
1617d56d KO |
1924 | if ((iter->flags & BTREE_ITER_KEY_CACHE_FILL) && |
1925 | bpos_eq(iter->pos, pos)) | |
1926 | return bkey_s_c_null; | |
1927 | ||
f7b6ca23 KO |
1928 | if (!bch2_btree_key_cache_find(c, iter->btree_id, pos)) |
1929 | return bkey_s_c_null; | |
1930 | ||
1931 | if (!iter->key_cache_path) | |
1932 | iter->key_cache_path = bch2_path_get(trans, iter->btree_id, pos, | |
1933 | iter->flags & BTREE_ITER_INTENT, 0, | |
087e53c2 | 1934 | iter->flags|BTREE_ITER_CACHED| |
ee2c6ea7 KO |
1935 | BTREE_ITER_CACHED_NOFILL, |
1936 | _THIS_IP_); | |
f7b6ca23 KO |
1937 | |
1938 | iter->key_cache_path = bch2_btree_path_set_pos(trans, iter->key_cache_path, pos, | |
ee2c6ea7 KO |
1939 | iter->flags & BTREE_ITER_INTENT, |
1940 | btree_iter_ip_allocated(iter)); | |
f7b6ca23 | 1941 | |
dcced069 KO |
1942 | ret = bch2_btree_path_traverse(trans, iter->key_cache_path, |
1943 | iter->flags|BTREE_ITER_CACHED) ?: | |
1944 | bch2_btree_path_relock(trans, iter->path, _THIS_IP_); | |
f7b6ca23 KO |
1945 | if (unlikely(ret)) |
1946 | return bkey_s_c_err(ret); | |
1947 | ||
fd211bc7 | 1948 | btree_path_set_should_be_locked(iter->key_cache_path); |
f7b6ca23 | 1949 | |
52bf51b9 KO |
1950 | k = bch2_btree_path_peek_slot(iter->key_cache_path, &u); |
1951 | if (k.k && !bkey_err(k)) { | |
1952 | iter->k = u; | |
1953 | k.k = &iter->k; | |
1954 | } | |
1955 | return k; | |
f7b6ca23 KO |
1956 | } |
1957 | ||
a1e82d35 | 1958 | static struct bkey_s_c __bch2_btree_iter_peek(struct btree_iter *iter, struct bpos search_key) |
1c6fdbd8 | 1959 | { |
67e0dd8f | 1960 | struct btree_trans *trans = iter->trans; |
360746bf | 1961 | struct bkey_i *next_update; |
f7b6ca23 | 1962 | struct bkey_s_c k, k2; |
8e432d98 | 1963 | int ret; |
1c6fdbd8 | 1964 | |
969576ec | 1965 | EBUG_ON(iter->path->cached); |
7e1a3aa9 | 1966 | bch2_btree_iter_verify(iter); |
1c6fdbd8 | 1967 | |
1c6fdbd8 | 1968 | while (1) { |
969576ec KO |
1969 | struct btree_path_level *l; |
1970 | ||
ce91abd6 | 1971 | iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key, |
ee2c6ea7 KO |
1972 | iter->flags & BTREE_ITER_INTENT, |
1973 | btree_iter_ip_allocated(iter)); | |
c8476a4e | 1974 | |
67e0dd8f | 1975 | ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); |
e6e024e9 KO |
1976 | if (unlikely(ret)) { |
1977 | /* ensure that iter->k is consistent with iter->pos: */ | |
1978 | bch2_btree_iter_set_pos(iter, iter->pos); | |
1979 | k = bkey_s_c_err(ret); | |
1980 | goto out; | |
1981 | } | |
1c6fdbd8 | 1982 | |
969576ec KO |
1983 | l = path_l(iter->path); |
1984 | ||
1985 | if (unlikely(!l->b)) { | |
1986 | /* No btree nodes at requested level: */ | |
1987 | bch2_btree_iter_set_pos(iter, SPOS_MAX); | |
1988 | k = bkey_s_c_null; | |
1989 | goto out; | |
1990 | } | |
1991 | ||
fd211bc7 | 1992 | btree_path_set_should_be_locked(iter->path); |
f7b6ca23 | 1993 | |
969576ec | 1994 | k = btree_path_level_peek_all(trans->c, l, &iter->k); |
e6e024e9 | 1995 | |
f7b6ca23 KO |
1996 | if (unlikely(iter->flags & BTREE_ITER_WITH_KEY_CACHE) && |
1997 | k.k && | |
1998 | (k2 = btree_trans_peek_key_cache(iter, k.k->p)).k) { | |
8192f8a5 KO |
1999 | k = k2; |
2000 | ret = bkey_err(k); | |
f7b6ca23 | 2001 | if (ret) { |
f7b6ca23 KO |
2002 | bch2_btree_iter_set_pos(iter, iter->pos); |
2003 | goto out; | |
2004 | } | |
f7b6ca23 KO |
2005 | } |
2006 | ||
5222a460 KO |
2007 | if (unlikely(iter->flags & BTREE_ITER_WITH_JOURNAL)) |
2008 | k = btree_trans_peek_journal(trans, iter, k); | |
2009 | ||
2010 | next_update = btree_trans_peek_updates(iter); | |
e6e024e9 | 2011 | |
818664f5 | 2012 | if (next_update && |
e88a75eb KO |
2013 | bpos_le(next_update->k.p, |
2014 | k.k ? k.k->p : l->b->key.k.p)) { | |
5288e66a | 2015 | iter->k = next_update->k; |
818664f5 | 2016 | k = bkey_i_to_s_c(next_update); |
5288e66a | 2017 | } |
818664f5 | 2018 | |
5222a460 KO |
2019 | if (k.k && bkey_deleted(k.k)) { |
2020 | /* | |
2021 | * If we've got a whiteout, and it's after the search | |
2022 | * key, advance the search key to the whiteout instead | |
2023 | * of just after the whiteout - it might be a btree | |
2024 | * whiteout, with a real key at the same position, since | |
2025 | * in the btree deleted keys sort before non deleted. | |
2026 | */ | |
e88a75eb | 2027 | search_key = !bpos_eq(search_key, k.k->p) |
5222a460 KO |
2028 | ? k.k->p |
2029 | : bpos_successor(k.k->p); | |
2030 | continue; | |
2031 | } | |
2032 | ||
818664f5 | 2033 | if (likely(k.k)) { |
c075ff70 | 2034 | break; |
e88a75eb | 2035 | } else if (likely(!bpos_eq(l->b->key.k.p, SPOS_MAX))) { |
e6e024e9 | 2036 | /* Advance to next leaf node: */ |
969576ec | 2037 | search_key = bpos_successor(l->b->key.k.p); |
e6e024e9 KO |
2038 | } else { |
2039 | /* End of btree: */ | |
c8476a4e KO |
2040 | bch2_btree_iter_set_pos(iter, SPOS_MAX); |
2041 | k = bkey_s_c_null; | |
2042 | goto out; | |
2043 | } | |
1c6fdbd8 | 2044 | } |
a1e82d35 | 2045 | out: |
a1e82d35 KO |
2046 | bch2_btree_iter_verify(iter); |
2047 | ||
2048 | return k; | |
2049 | } | |
2050 | ||
2051 | /** | |
96dea3d5 KO |
2052 | * bch2_btree_iter_peek_upto() - returns first key greater than or equal to |
2053 | * iterator's current position | |
2054 | * @iter: iterator to peek from | |
2055 | * @end: search limit: returns keys less than or equal to @end | |
2056 | * | |
2057 | * Returns: key if found, or an error extractable with bkey_err(). | |
a1e82d35 | 2058 | */ |
85d8cf16 | 2059 | struct bkey_s_c bch2_btree_iter_peek_upto(struct btree_iter *iter, struct bpos end) |
a1e82d35 KO |
2060 | { |
2061 | struct btree_trans *trans = iter->trans; | |
2062 | struct bpos search_key = btree_iter_search_key(iter); | |
2063 | struct bkey_s_c k; | |
85d8cf16 | 2064 | struct bpos iter_pos; |
a1e82d35 KO |
2065 | int ret; |
2066 | ||
c72f687a | 2067 | EBUG_ON((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) && bkey_eq(end, POS_MAX)); |
b0babf2a | 2068 | |
1f2d9192 | 2069 | if (iter->update_path) { |
7dcbdbd8 KO |
2070 | bch2_path_put_nokeep(trans, iter->update_path, |
2071 | iter->flags & BTREE_ITER_INTENT); | |
1f2d9192 KO |
2072 | iter->update_path = NULL; |
2073 | } | |
2074 | ||
a1e82d35 KO |
2075 | bch2_btree_iter_verify_entry_exit(iter); |
2076 | ||
2077 | while (1) { | |
2078 | k = __bch2_btree_iter_peek(iter, search_key); | |
c72f687a KO |
2079 | if (unlikely(!k.k)) |
2080 | goto end; | |
2081 | if (unlikely(bkey_err(k))) | |
fd211bc7 | 2082 | goto out_no_locked; |
a1e82d35 | 2083 | |
85d8cf16 | 2084 | /* |
fa014953 KO |
2085 | * We need to check against @end before FILTER_SNAPSHOTS because |
2086 | * if we get to a different inode that requested we might be | |
2087 | * seeing keys for a different snapshot tree that will all be | |
2088 | * filtered out. | |
2089 | * | |
2090 | * But we can't do the full check here, because bkey_start_pos() | |
2091 | * isn't monotonically increasing before FILTER_SNAPSHOTS, and | |
2092 | * that's what we check against in extents mode: | |
85d8cf16 | 2093 | */ |
fa014953 | 2094 | if (k.k->p.inode > end.inode) |
c72f687a | 2095 | goto end; |
85d8cf16 | 2096 | |
1f2d9192 | 2097 | if (iter->update_path && |
e88a75eb | 2098 | !bkey_eq(iter->update_path->pos, k.k->p)) { |
7dcbdbd8 KO |
2099 | bch2_path_put_nokeep(trans, iter->update_path, |
2100 | iter->flags & BTREE_ITER_INTENT); | |
1f2d9192 KO |
2101 | iter->update_path = NULL; |
2102 | } | |
2103 | ||
2104 | if ((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) && | |
2105 | (iter->flags & BTREE_ITER_INTENT) && | |
2106 | !(iter->flags & BTREE_ITER_IS_EXTENTS) && | |
2107 | !iter->update_path) { | |
2108 | struct bpos pos = k.k->p; | |
2109 | ||
2110 | if (pos.snapshot < iter->snapshot) { | |
2111 | search_key = bpos_successor(k.k->p); | |
2112 | continue; | |
2113 | } | |
2114 | ||
2115 | pos.snapshot = iter->snapshot; | |
2116 | ||
2117 | /* | |
2118 | * advance, same as on exit for iter->path, but only up | |
2119 | * to snapshot | |
2120 | */ | |
2121 | __btree_path_get(iter->path, iter->flags & BTREE_ITER_INTENT); | |
2122 | iter->update_path = iter->path; | |
2123 | ||
ce91abd6 | 2124 | iter->update_path = bch2_btree_path_set_pos(trans, |
1f2d9192 | 2125 | iter->update_path, pos, |
ee2c6ea7 KO |
2126 | iter->flags & BTREE_ITER_INTENT, |
2127 | _THIS_IP_); | |
ef073286 KO |
2128 | ret = bch2_btree_path_traverse(trans, iter->update_path, iter->flags); |
2129 | if (unlikely(ret)) { | |
2130 | k = bkey_s_c_err(ret); | |
2131 | goto out_no_locked; | |
2132 | } | |
1f2d9192 KO |
2133 | } |
2134 | ||
a1e82d35 KO |
2135 | /* |
2136 | * We can never have a key in a leaf node at POS_MAX, so | |
2137 | * we don't have to check these successor() calls: | |
2138 | */ | |
2139 | if ((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) && | |
2140 | !bch2_snapshot_is_ancestor(trans->c, | |
2141 | iter->snapshot, | |
2142 | k.k->p.snapshot)) { | |
2143 | search_key = bpos_successor(k.k->p); | |
2144 | continue; | |
2145 | } | |
2146 | ||
2147 | if (bkey_whiteout(k.k) && | |
2148 | !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) { | |
2149 | search_key = bkey_successor(iter, k.k->p); | |
2150 | continue; | |
2151 | } | |
2152 | ||
fa014953 KO |
2153 | /* |
2154 | * iter->pos should be mononotically increasing, and always be | |
2155 | * equal to the key we just returned - except extents can | |
2156 | * straddle iter->pos: | |
2157 | */ | |
2158 | if (!(iter->flags & BTREE_ITER_IS_EXTENTS)) | |
2159 | iter_pos = k.k->p; | |
2160 | else | |
2161 | iter_pos = bkey_max(iter->pos, bkey_start_pos(k.k)); | |
2162 | ||
2163 | if (unlikely(!(iter->flags & BTREE_ITER_IS_EXTENTS) | |
2164 | ? bkey_gt(iter_pos, end) | |
2165 | : bkey_ge(iter_pos, end))) | |
2166 | goto end; | |
2167 | ||
a1e82d35 KO |
2168 | break; |
2169 | } | |
2170 | ||
85d8cf16 | 2171 | iter->pos = iter_pos; |
1f2d9192 | 2172 | |
ce91abd6 | 2173 | iter->path = bch2_btree_path_set_pos(trans, iter->path, k.k->p, |
ee2c6ea7 KO |
2174 | iter->flags & BTREE_ITER_INTENT, |
2175 | btree_iter_ip_allocated(iter)); | |
fd211bc7 KO |
2176 | |
2177 | btree_path_set_should_be_locked(iter->path); | |
2178 | out_no_locked: | |
1f2d9192 | 2179 | if (iter->update_path) { |
549d173c | 2180 | ret = bch2_btree_path_relock(trans, iter->update_path, _THIS_IP_); |
fd211bc7 | 2181 | if (unlikely(ret)) |
549d173c | 2182 | k = bkey_s_c_err(ret); |
fd211bc7 KO |
2183 | else |
2184 | btree_path_set_should_be_locked(iter->update_path); | |
1f2d9192 | 2185 | } |
1f2d9192 | 2186 | |
a1e82d35 | 2187 | if (!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) |
c075ff70 KO |
2188 | iter->pos.snapshot = iter->snapshot; |
2189 | ||
a1e82d35 KO |
2190 | ret = bch2_btree_iter_verify_ret(iter, k); |
2191 | if (unlikely(ret)) { | |
2192 | bch2_btree_iter_set_pos(iter, iter->pos); | |
2193 | k = bkey_s_c_err(ret); | |
2194 | } | |
67e0dd8f | 2195 | |
7e1a3aa9 | 2196 | bch2_btree_iter_verify_entry_exit(iter); |
c075ff70 | 2197 | |
1c6fdbd8 | 2198 | return k; |
c72f687a KO |
2199 | end: |
2200 | bch2_btree_iter_set_pos(iter, end); | |
2201 | k = bkey_s_c_null; | |
2202 | goto out_no_locked; | |
1c6fdbd8 KO |
2203 | } |
2204 | ||
f4b61341 | 2205 | /** |
96dea3d5 | 2206 | * bch2_btree_iter_next() - returns first key greater than iterator's current |
f4b61341 | 2207 | * position |
96dea3d5 KO |
2208 | * @iter: iterator to peek from |
2209 | * | |
2210 | * Returns: key if found, or an error extractable with bkey_err(). | |
f4b61341 | 2211 | */ |
1c6fdbd8 KO |
2212 | struct bkey_s_c bch2_btree_iter_next(struct btree_iter *iter) |
2213 | { | |
e0ba3b64 | 2214 | if (!bch2_btree_iter_advance(iter)) |
2e70ce56 | 2215 | return bkey_s_c_null; |
f4b61341 | 2216 | |
2dac0eae | 2217 | return bch2_btree_iter_peek(iter); |
1c6fdbd8 KO |
2218 | } |
2219 | ||
ccf5a109 | 2220 | /** |
96dea3d5 | 2221 | * bch2_btree_iter_peek_prev() - returns first key less than or equal to |
ccf5a109 | 2222 | * iterator's current position |
96dea3d5 KO |
2223 | * @iter: iterator to peek from |
2224 | * | |
2225 | * Returns: key if found, or an error extractable with bkey_err(). | |
ccf5a109 KO |
2226 | */ |
2227 | struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *iter) | |
1c6fdbd8 | 2228 | { |
67e0dd8f | 2229 | struct btree_trans *trans = iter->trans; |
c8476a4e | 2230 | struct bpos search_key = iter->pos; |
c075ff70 | 2231 | struct btree_path *saved_path = NULL; |
1c6fdbd8 | 2232 | struct bkey_s_c k; |
c075ff70 KO |
2233 | struct bkey saved_k; |
2234 | const struct bch_val *saved_v; | |
1c6fdbd8 KO |
2235 | int ret; |
2236 | ||
67e0dd8f | 2237 | EBUG_ON(iter->path->cached || iter->path->level); |
5288e66a | 2238 | EBUG_ON(iter->flags & BTREE_ITER_WITH_UPDATES); |
5222a460 KO |
2239 | |
2240 | if (iter->flags & BTREE_ITER_WITH_JOURNAL) | |
2241 | return bkey_s_c_err(-EIO); | |
2242 | ||
7e1a3aa9 KO |
2243 | bch2_btree_iter_verify(iter); |
2244 | bch2_btree_iter_verify_entry_exit(iter); | |
2245 | ||
c075ff70 KO |
2246 | if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) |
2247 | search_key.snapshot = U32_MAX; | |
2248 | ||
1c6fdbd8 | 2249 | while (1) { |
ce91abd6 | 2250 | iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key, |
ee2c6ea7 KO |
2251 | iter->flags & BTREE_ITER_INTENT, |
2252 | btree_iter_ip_allocated(iter)); | |
c8476a4e | 2253 | |
67e0dd8f | 2254 | ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); |
7e1a3aa9 | 2255 | if (unlikely(ret)) { |
e6e024e9 KO |
2256 | /* ensure that iter->k is consistent with iter->pos: */ |
2257 | bch2_btree_iter_set_pos(iter, iter->pos); | |
7e1a3aa9 | 2258 | k = bkey_s_c_err(ret); |
fd211bc7 | 2259 | goto out_no_locked; |
7e1a3aa9 | 2260 | } |
1c6fdbd8 | 2261 | |
67e0dd8f KO |
2262 | k = btree_path_level_peek(trans, iter->path, |
2263 | &iter->path->l[0], &iter->k); | |
3d495595 KO |
2264 | if (!k.k || |
2265 | ((iter->flags & BTREE_ITER_IS_EXTENTS) | |
e88a75eb KO |
2266 | ? bpos_ge(bkey_start_pos(k.k), search_key) |
2267 | : bpos_gt(k.k->p, search_key))) | |
67e0dd8f KO |
2268 | k = btree_path_level_prev(trans, iter->path, |
2269 | &iter->path->l[0], &iter->k); | |
ccf5a109 | 2270 | |
e6e024e9 | 2271 | if (likely(k.k)) { |
c075ff70 KO |
2272 | if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) { |
2273 | if (k.k->p.snapshot == iter->snapshot) | |
2274 | goto got_key; | |
2275 | ||
2276 | /* | |
2277 | * If we have a saved candidate, and we're no | |
2278 | * longer at the same _key_ (not pos), return | |
2279 | * that candidate | |
2280 | */ | |
e88a75eb | 2281 | if (saved_path && !bkey_eq(k.k->p, saved_k.p)) { |
7dcbdbd8 | 2282 | bch2_path_put_nokeep(trans, iter->path, |
c075ff70 KO |
2283 | iter->flags & BTREE_ITER_INTENT); |
2284 | iter->path = saved_path; | |
2285 | saved_path = NULL; | |
2286 | iter->k = saved_k; | |
2287 | k.v = saved_v; | |
2288 | goto got_key; | |
2289 | } | |
2290 | ||
2291 | if (bch2_snapshot_is_ancestor(iter->trans->c, | |
2292 | iter->snapshot, | |
2293 | k.k->p.snapshot)) { | |
2294 | if (saved_path) | |
7dcbdbd8 | 2295 | bch2_path_put_nokeep(trans, saved_path, |
c075ff70 KO |
2296 | iter->flags & BTREE_ITER_INTENT); |
2297 | saved_path = btree_path_clone(trans, iter->path, | |
2298 | iter->flags & BTREE_ITER_INTENT); | |
2299 | saved_k = *k.k; | |
2300 | saved_v = k.v; | |
2301 | } | |
2302 | ||
2303 | search_key = bpos_predecessor(k.k->p); | |
2304 | continue; | |
2305 | } | |
2306 | got_key: | |
2307 | if (bkey_whiteout(k.k) && | |
2308 | !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) { | |
2309 | search_key = bkey_predecessor(iter, k.k->p); | |
2310 | if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) | |
2311 | search_key.snapshot = U32_MAX; | |
2312 | continue; | |
2313 | } | |
2314 | ||
1c6fdbd8 | 2315 | break; |
e88a75eb | 2316 | } else if (likely(!bpos_eq(iter->path->l[0].b->data->min_key, POS_MIN))) { |
e6e024e9 | 2317 | /* Advance to previous leaf node: */ |
67e0dd8f | 2318 | search_key = bpos_predecessor(iter->path->l[0].b->data->min_key); |
e6e024e9 KO |
2319 | } else { |
2320 | /* Start of btree: */ | |
c8476a4e | 2321 | bch2_btree_iter_set_pos(iter, POS_MIN); |
7e1a3aa9 | 2322 | k = bkey_s_c_null; |
fd211bc7 | 2323 | goto out_no_locked; |
7e1a3aa9 | 2324 | } |
ccf5a109 | 2325 | } |
1c6fdbd8 | 2326 | |
e88a75eb | 2327 | EBUG_ON(bkey_gt(bkey_start_pos(k.k), iter->pos)); |
3d495595 KO |
2328 | |
2329 | /* Extents can straddle iter->pos: */ | |
e88a75eb | 2330 | if (bkey_lt(k.k->p, iter->pos)) |
3d495595 | 2331 | iter->pos = k.k->p; |
c075ff70 KO |
2332 | |
2333 | if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) | |
2334 | iter->pos.snapshot = iter->snapshot; | |
fd211bc7 KO |
2335 | |
2336 | btree_path_set_should_be_locked(iter->path); | |
2337 | out_no_locked: | |
c075ff70 | 2338 | if (saved_path) |
7dcbdbd8 | 2339 | bch2_path_put_nokeep(trans, saved_path, iter->flags & BTREE_ITER_INTENT); |
67e0dd8f | 2340 | |
7e1a3aa9 KO |
2341 | bch2_btree_iter_verify_entry_exit(iter); |
2342 | bch2_btree_iter_verify(iter); | |
67e0dd8f | 2343 | |
1c6fdbd8 KO |
2344 | return k; |
2345 | } | |
2346 | ||
ccf5a109 | 2347 | /** |
96dea3d5 | 2348 | * bch2_btree_iter_prev() - returns first key less than iterator's current |
ccf5a109 | 2349 | * position |
96dea3d5 KO |
2350 | * @iter: iterator to peek from |
2351 | * | |
2352 | * Returns: key if found, or an error extractable with bkey_err(). | |
ccf5a109 KO |
2353 | */ |
2354 | struct bkey_s_c bch2_btree_iter_prev(struct btree_iter *iter) | |
2355 | { | |
e0ba3b64 | 2356 | if (!bch2_btree_iter_rewind(iter)) |
2e70ce56 | 2357 | return bkey_s_c_null; |
ccf5a109 | 2358 | |
2e70ce56 | 2359 | return bch2_btree_iter_peek_prev(iter); |
ccf5a109 KO |
2360 | } |
2361 | ||
c3801239 | 2362 | struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *iter) |
271a3d3a | 2363 | { |
9f6bd307 | 2364 | struct btree_trans *trans = iter->trans; |
953ee28a | 2365 | struct bpos search_key; |
271a3d3a | 2366 | struct bkey_s_c k; |
c3801239 KO |
2367 | int ret; |
2368 | ||
7e1a3aa9 KO |
2369 | bch2_btree_iter_verify(iter); |
2370 | bch2_btree_iter_verify_entry_exit(iter); | |
2ae4573e | 2371 | EBUG_ON(iter->path->level && (iter->flags & BTREE_ITER_WITH_KEY_CACHE)); |
7e1a3aa9 | 2372 | |
1d214eb1 KO |
2373 | /* extents can't span inode numbers: */ |
2374 | if ((iter->flags & BTREE_ITER_IS_EXTENTS) && | |
953ee28a | 2375 | unlikely(iter->pos.offset == KEY_OFFSET_MAX)) { |
1d214eb1 KO |
2376 | if (iter->pos.inode == KEY_INODE_MAX) |
2377 | return bkey_s_c_null; | |
c3801239 | 2378 | |
1d214eb1 KO |
2379 | bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos)); |
2380 | } | |
8042b5b7 | 2381 | |
953ee28a | 2382 | search_key = btree_iter_search_key(iter); |
ce91abd6 | 2383 | iter->path = bch2_btree_path_set_pos(trans, iter->path, search_key, |
ee2c6ea7 KO |
2384 | iter->flags & BTREE_ITER_INTENT, |
2385 | btree_iter_ip_allocated(iter)); | |
953ee28a | 2386 | |
67e0dd8f | 2387 | ret = bch2_btree_path_traverse(trans, iter->path, iter->flags); |
23dfb3a2 KO |
2388 | if (unlikely(ret)) { |
2389 | k = bkey_s_c_err(ret); | |
2390 | goto out_no_locked; | |
2391 | } | |
1c6fdbd8 | 2392 | |
c075ff70 KO |
2393 | if ((iter->flags & BTREE_ITER_CACHED) || |
2394 | !(iter->flags & (BTREE_ITER_IS_EXTENTS|BTREE_ITER_FILTER_SNAPSHOTS))) { | |
953ee28a | 2395 | struct bkey_i *next_update; |
1c6fdbd8 | 2396 | |
5222a460 | 2397 | if ((next_update = btree_trans_peek_updates(iter)) && |
e88a75eb | 2398 | bpos_eq(next_update->k.p, iter->pos)) { |
1d214eb1 KO |
2399 | iter->k = next_update->k; |
2400 | k = bkey_i_to_s_c(next_update); | |
5222a460 | 2401 | goto out; |
1d214eb1 KO |
2402 | } |
2403 | ||
5222a460 KO |
2404 | if (unlikely(iter->flags & BTREE_ITER_WITH_JOURNAL) && |
2405 | (k = btree_trans_peek_slot_journal(trans, iter)).k) | |
2406 | goto out; | |
2407 | ||
f7b6ca23 | 2408 | if (unlikely(iter->flags & BTREE_ITER_WITH_KEY_CACHE) && |
dcced069 | 2409 | (k = btree_trans_peek_key_cache(iter, iter->pos)).k) { |
8192f8a5 | 2410 | if (!bkey_err(k)) |
f7b6ca23 | 2411 | iter->k = *k.k; |
8192f8a5 KO |
2412 | /* We're not returning a key from iter->path: */ |
2413 | goto out_no_locked; | |
f7b6ca23 KO |
2414 | } |
2415 | ||
5222a460 | 2416 | k = bch2_btree_path_peek_slot(iter->path, &iter->k); |
f42238b5 KO |
2417 | if (unlikely(!k.k)) |
2418 | goto out_no_locked; | |
1d214eb1 KO |
2419 | } else { |
2420 | struct bpos next; | |
c72f687a KO |
2421 | struct bpos end = iter->pos; |
2422 | ||
2423 | if (iter->flags & BTREE_ITER_IS_EXTENTS) | |
2424 | end.offset = U64_MAX; | |
1d214eb1 | 2425 | |
2ae4573e KO |
2426 | EBUG_ON(iter->path->level); |
2427 | ||
b1d87f52 | 2428 | if (iter->flags & BTREE_ITER_INTENT) { |
67e0dd8f | 2429 | struct btree_iter iter2; |
b1d87f52 | 2430 | |
67e0dd8f | 2431 | bch2_trans_copy_iter(&iter2, iter); |
85d8cf16 | 2432 | k = bch2_btree_iter_peek_upto(&iter2, end); |
b1d87f52 | 2433 | |
67e0dd8f KO |
2434 | if (k.k && !bkey_err(k)) { |
2435 | iter->k = iter2.k; | |
2436 | k.k = &iter->k; | |
2437 | } | |
2438 | bch2_trans_iter_exit(trans, &iter2); | |
b1d87f52 KO |
2439 | } else { |
2440 | struct bpos pos = iter->pos; | |
2441 | ||
c72f687a | 2442 | k = bch2_btree_iter_peek_upto(iter, end); |
23dfb3a2 KO |
2443 | if (unlikely(bkey_err(k))) |
2444 | bch2_btree_iter_set_pos(iter, pos); | |
2445 | else | |
2446 | iter->pos = pos; | |
b1d87f52 | 2447 | } |
1d214eb1 KO |
2448 | |
2449 | if (unlikely(bkey_err(k))) | |
f42238b5 | 2450 | goto out_no_locked; |
1d214eb1 KO |
2451 | |
2452 | next = k.k ? bkey_start_pos(k.k) : POS_MAX; | |
2453 | ||
e88a75eb | 2454 | if (bkey_lt(iter->pos, next)) { |
1d214eb1 KO |
2455 | bkey_init(&iter->k); |
2456 | iter->k.p = iter->pos; | |
c075ff70 KO |
2457 | |
2458 | if (iter->flags & BTREE_ITER_IS_EXTENTS) { | |
2459 | bch2_key_resize(&iter->k, | |
2460 | min_t(u64, KEY_SIZE_MAX, | |
2461 | (next.inode == iter->pos.inode | |
2462 | ? next.offset | |
2463 | : KEY_OFFSET_MAX) - | |
2464 | iter->pos.offset)); | |
2465 | EBUG_ON(!iter->k.size); | |
2466 | } | |
1d214eb1 KO |
2467 | |
2468 | k = (struct bkey_s_c) { &iter->k, NULL }; | |
1d214eb1 | 2469 | } |
271a3d3a | 2470 | } |
5222a460 | 2471 | out: |
fd211bc7 KO |
2472 | btree_path_set_should_be_locked(iter->path); |
2473 | out_no_locked: | |
7e1a3aa9 KO |
2474 | bch2_btree_iter_verify_entry_exit(iter); |
2475 | bch2_btree_iter_verify(iter); | |
c075ff70 KO |
2476 | ret = bch2_btree_iter_verify_ret(iter, k); |
2477 | if (unlikely(ret)) | |
2478 | return bkey_s_c_err(ret); | |
66a0a497 | 2479 | |
63f1a598 | 2480 | return k; |
1c6fdbd8 KO |
2481 | } |
2482 | ||
1c6fdbd8 KO |
2483 | struct bkey_s_c bch2_btree_iter_next_slot(struct btree_iter *iter) |
2484 | { | |
e0ba3b64 | 2485 | if (!bch2_btree_iter_advance(iter)) |
2e70ce56 | 2486 | return bkey_s_c_null; |
1c6fdbd8 | 2487 | |
2dac0eae | 2488 | return bch2_btree_iter_peek_slot(iter); |
1c6fdbd8 KO |
2489 | } |
2490 | ||
18fc6ae5 KO |
2491 | struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *iter) |
2492 | { | |
e0ba3b64 | 2493 | if (!bch2_btree_iter_rewind(iter)) |
18fc6ae5 KO |
2494 | return bkey_s_c_null; |
2495 | ||
2496 | return bch2_btree_iter_peek_slot(iter); | |
2497 | } | |
2498 | ||
0f2ea655 KO |
2499 | struct bkey_s_c bch2_btree_iter_peek_and_restart_outlined(struct btree_iter *iter) |
2500 | { | |
2501 | struct bkey_s_c k; | |
2502 | ||
2503 | while (btree_trans_too_many_iters(iter->trans) || | |
2504 | (k = bch2_btree_iter_peek_type(iter, iter->flags), | |
2505 | bch2_err_matches(bkey_err(k), BCH_ERR_transaction_restart))) | |
2506 | bch2_trans_begin(iter->trans); | |
2507 | ||
2508 | return k; | |
2509 | } | |
2510 | ||
1c6fdbd8 KO |
2511 | /* new transactional stuff: */ |
2512 | ||
0423fb71 KO |
2513 | #ifdef CONFIG_BCACHEFS_DEBUG |
2514 | static void btree_trans_verify_sorted_refs(struct btree_trans *trans) | |
2515 | { | |
67e0dd8f | 2516 | struct btree_path *path; |
0423fb71 KO |
2517 | unsigned i; |
2518 | ||
f8fd5871 | 2519 | BUG_ON(trans->nr_sorted != bitmap_weight(trans->paths_allocated, BTREE_ITER_MAX) - 1); |
0423fb71 | 2520 | |
67e0dd8f KO |
2521 | trans_for_each_path(trans, path) { |
2522 | BUG_ON(path->sorted_idx >= trans->nr_sorted); | |
2523 | BUG_ON(trans->sorted[path->sorted_idx] != path->idx); | |
0423fb71 KO |
2524 | } |
2525 | ||
2526 | for (i = 0; i < trans->nr_sorted; i++) { | |
2527 | unsigned idx = trans->sorted[i]; | |
2528 | ||
6e92d155 | 2529 | BUG_ON(!test_bit(idx, trans->paths_allocated)); |
67e0dd8f | 2530 | BUG_ON(trans->paths[idx].sorted_idx != i); |
0423fb71 KO |
2531 | } |
2532 | } | |
0423fb71 KO |
2533 | |
2534 | static void btree_trans_verify_sorted(struct btree_trans *trans) | |
2535 | { | |
67e0dd8f | 2536 | struct btree_path *path, *prev = NULL; |
0423fb71 KO |
2537 | unsigned i; |
2538 | ||
ee4d17d0 KO |
2539 | if (!bch2_debug_check_iterators) |
2540 | return; | |
2541 | ||
67e0dd8f | 2542 | trans_for_each_path_inorder(trans, path, i) { |
eb7bd15f | 2543 | if (prev && btree_path_cmp(prev, path) > 0) { |
5c0bb66a | 2544 | __bch2_dump_trans_paths_updates(trans, true); |
eb7bd15f KO |
2545 | panic("trans paths out of order!\n"); |
2546 | } | |
67e0dd8f | 2547 | prev = path; |
0423fb71 | 2548 | } |
0423fb71 | 2549 | } |
068bcaa5 KO |
2550 | #else |
2551 | static inline void btree_trans_verify_sorted_refs(struct btree_trans *trans) {} | |
2552 | static inline void btree_trans_verify_sorted(struct btree_trans *trans) {} | |
2553 | #endif | |
0423fb71 | 2554 | |
068bcaa5 | 2555 | void __bch2_btree_trans_sort_paths(struct btree_trans *trans) |
0423fb71 KO |
2556 | { |
2557 | int i, l = 0, r = trans->nr_sorted, inc = 1; | |
2558 | bool swapped; | |
2559 | ||
068bcaa5 KO |
2560 | btree_trans_verify_sorted_refs(trans); |
2561 | ||
2562 | if (trans->paths_sorted) | |
2563 | goto out; | |
2564 | ||
0423fb71 KO |
2565 | /* |
2566 | * Cocktail shaker sort: this is efficient because iterators will be | |
5c0bb66a | 2567 | * mostly sorted. |
0423fb71 KO |
2568 | */ |
2569 | do { | |
2570 | swapped = false; | |
2571 | ||
2572 | for (i = inc > 0 ? l : r - 2; | |
2573 | i + 1 < r && i >= l; | |
2574 | i += inc) { | |
67e0dd8f KO |
2575 | if (btree_path_cmp(trans->paths + trans->sorted[i], |
2576 | trans->paths + trans->sorted[i + 1]) > 0) { | |
0423fb71 | 2577 | swap(trans->sorted[i], trans->sorted[i + 1]); |
67e0dd8f KO |
2578 | trans->paths[trans->sorted[i]].sorted_idx = i; |
2579 | trans->paths[trans->sorted[i + 1]].sorted_idx = i + 1; | |
0423fb71 KO |
2580 | swapped = true; |
2581 | } | |
2582 | } | |
2583 | ||
2584 | if (inc > 0) | |
2585 | --r; | |
2586 | else | |
2587 | l++; | |
2588 | inc = -inc; | |
2589 | } while (swapped); | |
2590 | ||
67e0dd8f | 2591 | trans->paths_sorted = true; |
068bcaa5 | 2592 | out: |
0423fb71 KO |
2593 | btree_trans_verify_sorted(trans); |
2594 | } | |
2595 | ||
67e0dd8f KO |
2596 | static inline void btree_path_list_remove(struct btree_trans *trans, |
2597 | struct btree_path *path) | |
0423fb71 KO |
2598 | { |
2599 | unsigned i; | |
2600 | ||
67e0dd8f | 2601 | EBUG_ON(path->sorted_idx >= trans->nr_sorted); |
0423fb71 KO |
2602 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
2603 | trans->nr_sorted--; | |
67e0dd8f KO |
2604 | memmove_u64s_down_small(trans->sorted + path->sorted_idx, |
2605 | trans->sorted + path->sorted_idx + 1, | |
2606 | DIV_ROUND_UP(trans->nr_sorted - path->sorted_idx, 8)); | |
0423fb71 | 2607 | #else |
67e0dd8f | 2608 | array_remove_item(trans->sorted, trans->nr_sorted, path->sorted_idx); |
0423fb71 | 2609 | #endif |
67e0dd8f KO |
2610 | for (i = path->sorted_idx; i < trans->nr_sorted; i++) |
2611 | trans->paths[trans->sorted[i]].sorted_idx = i; | |
0423fb71 | 2612 | |
67e0dd8f | 2613 | path->sorted_idx = U8_MAX; |
0423fb71 KO |
2614 | } |
2615 | ||
67e0dd8f KO |
2616 | static inline void btree_path_list_add(struct btree_trans *trans, |
2617 | struct btree_path *pos, | |
2618 | struct btree_path *path) | |
0423fb71 KO |
2619 | { |
2620 | unsigned i; | |
2621 | ||
068bcaa5 | 2622 | path->sorted_idx = pos ? pos->sorted_idx + 1 : trans->nr_sorted; |
0423fb71 KO |
2623 | |
2624 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS | |
67e0dd8f KO |
2625 | memmove_u64s_up_small(trans->sorted + path->sorted_idx + 1, |
2626 | trans->sorted + path->sorted_idx, | |
2627 | DIV_ROUND_UP(trans->nr_sorted - path->sorted_idx, 8)); | |
0423fb71 | 2628 | trans->nr_sorted++; |
67e0dd8f | 2629 | trans->sorted[path->sorted_idx] = path->idx; |
0423fb71 | 2630 | #else |
67e0dd8f | 2631 | array_insert_item(trans->sorted, trans->nr_sorted, path->sorted_idx, path->idx); |
0423fb71 KO |
2632 | #endif |
2633 | ||
67e0dd8f KO |
2634 | for (i = path->sorted_idx; i < trans->nr_sorted; i++) |
2635 | trans->paths[trans->sorted[i]].sorted_idx = i; | |
64bc0011 | 2636 | |
0423fb71 | 2637 | btree_trans_verify_sorted_refs(trans); |
7c26ecae KO |
2638 | } |
2639 | ||
67e0dd8f | 2640 | void bch2_trans_iter_exit(struct btree_trans *trans, struct btree_iter *iter) |
64bc0011 | 2641 | { |
1f2d9192 | 2642 | if (iter->update_path) |
7dcbdbd8 | 2643 | bch2_path_put_nokeep(trans, iter->update_path, |
1f2d9192 | 2644 | iter->flags & BTREE_ITER_INTENT); |
11f11737 KO |
2645 | if (iter->path) |
2646 | bch2_path_put(trans, iter->path, | |
2647 | iter->flags & BTREE_ITER_INTENT); | |
f7b6ca23 KO |
2648 | if (iter->key_cache_path) |
2649 | bch2_path_put(trans, iter->key_cache_path, | |
2650 | iter->flags & BTREE_ITER_INTENT); | |
67e0dd8f | 2651 | iter->path = NULL; |
1f2d9192 | 2652 | iter->update_path = NULL; |
f7b6ca23 | 2653 | iter->key_cache_path = NULL; |
64bc0011 KO |
2654 | } |
2655 | ||
c96f108b KO |
2656 | void bch2_trans_iter_init_outlined(struct btree_trans *trans, |
2657 | struct btree_iter *iter, | |
2658 | enum btree_id btree_id, struct bpos pos, | |
2659 | unsigned flags) | |
2660 | { | |
2661 | bch2_trans_iter_init_common(trans, iter, btree_id, pos, 0, 0, | |
ee2c6ea7 KO |
2662 | bch2_btree_iter_flags(trans, btree_id, flags), |
2663 | _RET_IP_); | |
424eb881 KO |
2664 | } |
2665 | ||
67e0dd8f KO |
2666 | void bch2_trans_node_iter_init(struct btree_trans *trans, |
2667 | struct btree_iter *iter, | |
2668 | enum btree_id btree_id, | |
2669 | struct bpos pos, | |
2670 | unsigned locks_want, | |
2671 | unsigned depth, | |
2672 | unsigned flags) | |
1c6fdbd8 | 2673 | { |
1e81f89b KO |
2674 | flags |= BTREE_ITER_NOT_EXTENTS; |
2675 | flags |= __BTREE_ITER_ALL_SNAPSHOTS; | |
2676 | flags |= BTREE_ITER_ALL_SNAPSHOTS; | |
c96f108b KO |
2677 | |
2678 | bch2_trans_iter_init_common(trans, iter, btree_id, pos, locks_want, depth, | |
ee2c6ea7 KO |
2679 | __bch2_btree_iter_flags(trans, btree_id, flags), |
2680 | _RET_IP_); | |
c96f108b KO |
2681 | |
2682 | iter->min_depth = depth; | |
2683 | ||
67e0dd8f KO |
2684 | BUG_ON(iter->path->locks_want < min(locks_want, BTREE_MAX_DEPTH)); |
2685 | BUG_ON(iter->path->level != depth); | |
2686 | BUG_ON(iter->min_depth != depth); | |
2687 | } | |
7c26ecae | 2688 | |
67e0dd8f KO |
2689 | void bch2_trans_copy_iter(struct btree_iter *dst, struct btree_iter *src) |
2690 | { | |
2691 | *dst = *src; | |
2692 | if (src->path) | |
2693 | __btree_path_get(src->path, src->flags & BTREE_ITER_INTENT); | |
1f2d9192 KO |
2694 | if (src->update_path) |
2695 | __btree_path_get(src->update_path, src->flags & BTREE_ITER_INTENT); | |
f7b6ca23 | 2696 | dst->key_cache_path = NULL; |
1c6fdbd8 KO |
2697 | } |
2698 | ||
14d8f26a | 2699 | void *__bch2_trans_kmalloc(struct btree_trans *trans, size_t size) |
1c6fdbd8 | 2700 | { |
616928c3 | 2701 | unsigned new_top = trans->mem_top + size; |
14d8f26a KO |
2702 | size_t old_bytes = trans->mem_bytes; |
2703 | size_t new_bytes = roundup_pow_of_two(new_top); | |
78367aaa | 2704 | int ret; |
14d8f26a | 2705 | void *new_mem; |
73a117d2 KO |
2706 | void *p; |
2707 | ||
616928c3 KO |
2708 | trans->mem_max = max(trans->mem_max, new_top); |
2709 | ||
14d8f26a | 2710 | WARN_ON_ONCE(new_bytes > BTREE_TRANS_MEM_MAX); |
e131b6aa | 2711 | |
78367aaa KO |
2712 | new_mem = krealloc(trans->mem, new_bytes, GFP_NOWAIT|__GFP_NOWARN); |
2713 | if (unlikely(!new_mem)) { | |
2714 | bch2_trans_unlock(trans); | |
2715 | ||
2716 | new_mem = krealloc(trans->mem, new_bytes, GFP_KERNEL); | |
2717 | if (!new_mem && new_bytes <= BTREE_TRANS_MEM_MAX) { | |
2718 | new_mem = mempool_alloc(&trans->c->btree_trans_mem_pool, GFP_KERNEL); | |
2719 | new_bytes = BTREE_TRANS_MEM_MAX; | |
2720 | kfree(trans->mem); | |
2721 | } | |
1c6fdbd8 | 2722 | |
78367aaa KO |
2723 | if (!new_mem) |
2724 | return ERR_PTR(-BCH_ERR_ENOMEM_trans_kmalloc); | |
2725 | ||
2726 | trans->mem = new_mem; | |
2727 | trans->mem_bytes = new_bytes; | |
2728 | ||
2729 | ret = bch2_trans_relock(trans); | |
2730 | if (ret) | |
2731 | return ERR_PTR(ret); | |
2732 | } | |
1c6fdbd8 | 2733 | |
14d8f26a KO |
2734 | trans->mem = new_mem; |
2735 | trans->mem_bytes = new_bytes; | |
1c6fdbd8 | 2736 | |
14d8f26a KO |
2737 | if (old_bytes) { |
2738 | trace_and_count(trans->c, trans_restart_mem_realloced, trans, _RET_IP_, new_bytes); | |
2739 | return ERR_PTR(btree_trans_restart(trans, BCH_ERR_transaction_restart_mem_realloced)); | |
1c6fdbd8 KO |
2740 | } |
2741 | ||
20bceecb | 2742 | p = trans->mem + trans->mem_top; |
1c6fdbd8 | 2743 | trans->mem_top += size; |
7ed158f2 | 2744 | memset(p, 0, size); |
20bceecb | 2745 | return p; |
1c6fdbd8 KO |
2746 | } |
2747 | ||
f82755e4 KO |
2748 | static inline void check_srcu_held_too_long(struct btree_trans *trans) |
2749 | { | |
2750 | WARN(trans->srcu_held && time_after(jiffies, trans->srcu_lock_time + HZ * 10), | |
2751 | "btree trans held srcu lock (delaying memory reclaim) for %lu seconds", | |
2752 | (jiffies - trans->srcu_lock_time) / HZ); | |
2753 | } | |
2754 | ||
c4accde4 | 2755 | void bch2_trans_srcu_unlock(struct btree_trans *trans) |
e242b92a | 2756 | { |
c4accde4 KO |
2757 | if (trans->srcu_held) { |
2758 | struct bch_fs *c = trans->c; | |
2759 | struct btree_path *path; | |
e242b92a | 2760 | |
c4accde4 KO |
2761 | trans_for_each_path(trans, path) |
2762 | if (path->cached && !btree_node_locked(path, 0)) | |
2763 | path->l[0].b = ERR_PTR(-BCH_ERR_no_btree_node_srcu_reset); | |
e242b92a | 2764 | |
f82755e4 | 2765 | check_srcu_held_too_long(trans); |
c4accde4 KO |
2766 | srcu_read_unlock(&c->btree_trans_barrier, trans->srcu_idx); |
2767 | trans->srcu_held = false; | |
2768 | } | |
2769 | } | |
2770 | ||
2771 | void bch2_trans_srcu_lock(struct btree_trans *trans) | |
2772 | { | |
2773 | if (!trans->srcu_held) { | |
2774 | trans->srcu_idx = srcu_read_lock(&trans->c->btree_trans_barrier); | |
2775 | trans->srcu_lock_time = jiffies; | |
2776 | trans->srcu_held = true; | |
2777 | } | |
e242b92a KO |
2778 | } |
2779 | ||
d38494c4 | 2780 | /** |
955af634 | 2781 | * bch2_trans_begin() - reset a transaction after a interrupted attempt |
d38494c4 | 2782 | * @trans: transaction to reset |
d38494c4 | 2783 | * |
96dea3d5 KO |
2784 | * Returns: current restart counter, to be used with trans_was_restarted() |
2785 | * | |
549d173c KO |
2786 | * While iterating over nodes or updating nodes a attempt to lock a btree node |
2787 | * may return BCH_ERR_transaction_restart when the trylock fails. When this | |
2788 | * occurs bch2_trans_begin() should be called and the transaction retried. | |
d38494c4 | 2789 | */ |
e941ae7d | 2790 | u32 bch2_trans_begin(struct btree_trans *trans) |
1c6fdbd8 | 2791 | { |
67e0dd8f | 2792 | struct btree_path *path; |
f375d6ca | 2793 | u64 now; |
a8e00bd4 | 2794 | |
0fbf71f8 | 2795 | bch2_trans_reset_updates(trans); |
1c6fdbd8 | 2796 | |
c497df8b | 2797 | trans->restart_count++; |
163e885a | 2798 | trans->mem_top = 0; |
24de63da | 2799 | trans->journal_entries = NULL; |
bf7b87a4 | 2800 | |
67e0dd8f | 2801 | trans_for_each_path(trans, path) { |
25a77231 KO |
2802 | path->should_be_locked = false; |
2803 | ||
e1f7fa06 KO |
2804 | /* |
2805 | * If the transaction wasn't restarted, we're presuming to be | |
2806 | * doing something new: dont keep iterators excpt the ones that | |
2807 | * are in use - except for the subvolumes btree: | |
2808 | */ | |
2809 | if (!trans->restarted && path->btree_id != BTREE_ID_subvolumes) | |
2810 | path->preserve = false; | |
2811 | ||
67e0dd8f KO |
2812 | /* |
2813 | * XXX: we probably shouldn't be doing this if the transaction | |
2814 | * was restarted, but currently we still overflow transaction | |
2815 | * iterators if we do that | |
2816 | */ | |
2817 | if (!path->ref && !path->preserve) | |
2818 | __bch2_path_free(trans, path); | |
2819 | else | |
25a77231 | 2820 | path->preserve = false; |
67e0dd8f KO |
2821 | } |
2822 | ||
f375d6ca | 2823 | now = local_clock(); |
43de721a KO |
2824 | if (!trans->restarted && |
2825 | (need_resched() || | |
f375d6ca | 2826 | now - trans->last_begin_time > BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS)) { |
49c7cd9d | 2827 | drop_locks_do(trans, (cond_resched(), 0)); |
f375d6ca | 2828 | now = local_clock(); |
43de721a | 2829 | } |
f375d6ca | 2830 | trans->last_begin_time = now; |
50dc0f69 | 2831 | |
c4accde4 KO |
2832 | if (unlikely(trans->srcu_held && |
2833 | time_after(jiffies, trans->srcu_lock_time + msecs_to_jiffies(10)))) | |
2834 | bch2_trans_srcu_unlock(trans); | |
e242b92a | 2835 | |
5e2d8be8 | 2836 | trans->last_begin_ip = _RET_IP_; |
60b55388 | 2837 | if (trans->restarted) { |
67e0dd8f | 2838 | bch2_btree_path_traverse_all(trans); |
60b55388 KO |
2839 | trans->notrace_relock_fail = false; |
2840 | } | |
e5af273f | 2841 | |
e941ae7d KO |
2842 | return trans->restart_count; |
2843 | } | |
2844 | ||
6bd68ec2 | 2845 | static struct btree_trans *bch2_trans_alloc(struct bch_fs *c) |
1a21bf98 | 2846 | { |
6bd68ec2 | 2847 | struct btree_trans *trans; |
1a21bf98 | 2848 | |
6bd68ec2 KO |
2849 | if (IS_ENABLED(__KERNEL__)) { |
2850 | trans = this_cpu_xchg(c->btree_trans_bufs->trans, NULL); | |
2851 | if (trans) | |
2852 | return trans; | |
ff5b741c | 2853 | } |
1a21bf98 | 2854 | |
6bd68ec2 KO |
2855 | trans = mempool_alloc(&c->btree_trans_pool, GFP_NOFS); |
2856 | /* | |
2857 | * paths need to be zeroed, bch2_check_for_deadlock looks at | |
2858 | * paths in other threads | |
2859 | */ | |
2860 | memset(&trans->paths, 0, sizeof(trans->paths)); | |
2861 | return trans; | |
1a21bf98 KO |
2862 | } |
2863 | ||
307e3c13 KO |
2864 | const char *bch2_btree_transaction_fns[BCH_TRANSACTIONS_NR]; |
2865 | ||
2866 | unsigned bch2_trans_get_fn_idx(const char *fn) | |
4aba7d45 KO |
2867 | { |
2868 | unsigned i; | |
2869 | ||
307e3c13 KO |
2870 | for (i = 0; i < ARRAY_SIZE(bch2_btree_transaction_fns); i++) |
2871 | if (!bch2_btree_transaction_fns[i] || | |
2872 | bch2_btree_transaction_fns[i] == fn) { | |
2873 | bch2_btree_transaction_fns[i] = fn; | |
4aba7d45 KO |
2874 | return i; |
2875 | } | |
2876 | ||
2877 | pr_warn_once("BCH_TRANSACTIONS_NR not big enough!"); | |
2878 | return i; | |
2879 | } | |
2880 | ||
6bd68ec2 | 2881 | struct btree_trans *__bch2_trans_get(struct bch_fs *c, unsigned fn_idx) |
c0ebe3e4 | 2882 | __acquires(&c->btree_trans_barrier) |
1c6fdbd8 | 2883 | { |
6bd68ec2 | 2884 | struct btree_trans *trans; |
5c0bb66a KO |
2885 | struct btree_transaction_stats *s; |
2886 | ||
6bd68ec2 KO |
2887 | trans = bch2_trans_alloc(c); |
2888 | ||
ae1ede58 | 2889 | memset(trans, 0, sizeof(*trans)); |
1c6fdbd8 | 2890 | trans->c = c; |
307e3c13 KO |
2891 | trans->fn = fn_idx < ARRAY_SIZE(bch2_btree_transaction_fns) |
2892 | ? bch2_btree_transaction_fns[fn_idx] : NULL; | |
d7e4e513 | 2893 | trans->last_begin_time = local_clock(); |
307e3c13 | 2894 | trans->fn_idx = fn_idx; |
33bd5d06 | 2895 | trans->locking_wait.task = current; |
5222a460 | 2896 | trans->journal_replay_not_finished = |
8a443d3e KO |
2897 | unlikely(!test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags)) && |
2898 | atomic_inc_not_zero(&c->journal_keys.ref); | |
33bd5d06 | 2899 | closure_init_stack(&trans->ref); |
20bceecb | 2900 | |
f8fd5871 KO |
2901 | trans->paths_allocated[0] = 1; |
2902 | ||
616928c3 | 2903 | s = btree_trans_stats(trans); |
1f69368c | 2904 | if (s && s->max_mem) { |
616928c3 KO |
2905 | unsigned expected_mem_bytes = roundup_pow_of_two(s->max_mem); |
2906 | ||
0b5c9f59 | 2907 | trans->mem = kmalloc(expected_mem_bytes, GFP_KERNEL); |
e131b6aa KO |
2908 | |
2909 | if (!unlikely(trans->mem)) { | |
2910 | trans->mem = mempool_alloc(&c->btree_trans_mem_pool, GFP_KERNEL); | |
2911 | trans->mem_bytes = BTREE_TRANS_MEM_MAX; | |
2912 | } else { | |
0b5c9f59 | 2913 | trans->mem_bytes = expected_mem_bytes; |
e131b6aa | 2914 | } |
616928c3 | 2915 | } |
920e69bc KO |
2916 | |
2917 | if (s) { | |
1f69368c | 2918 | trans->nr_max_paths = s->nr_max_paths; |
24de63da | 2919 | trans->journal_entries_size = s->journal_entries_size; |
920e69bc | 2920 | } |
5c0bb66a | 2921 | |
c4accde4 | 2922 | trans->srcu_idx = srcu_read_lock(&c->btree_trans_barrier); |
e242b92a | 2923 | trans->srcu_lock_time = jiffies; |
c4accde4 | 2924 | trans->srcu_held = true; |
876c7af3 | 2925 | |
b84d42c3 | 2926 | if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG_TRANSACTIONS)) { |
50b13bee KO |
2927 | struct btree_trans *pos; |
2928 | ||
a5b696ee | 2929 | seqmutex_lock(&c->btree_trans_lock); |
50b13bee | 2930 | list_for_each_entry(pos, &c->btree_trans_list, list) { |
6623c0fc KO |
2931 | /* |
2932 | * We'd much prefer to be stricter here and completely | |
2933 | * disallow multiple btree_trans in the same thread - | |
2934 | * but the data move path calls bch2_write when we | |
2935 | * already have a btree_trans initialized. | |
2936 | */ | |
2937 | BUG_ON(trans->locking_wait.task->pid == pos->locking_wait.task->pid && | |
2938 | bch2_trans_locked(pos)); | |
2939 | ||
33bd5d06 | 2940 | if (trans->locking_wait.task->pid < pos->locking_wait.task->pid) { |
50b13bee KO |
2941 | list_add_tail(&trans->list, &pos->list); |
2942 | goto list_add_done; | |
2943 | } | |
2944 | } | |
2945 | list_add_tail(&trans->list, &c->btree_trans_list); | |
2946 | list_add_done: | |
a5b696ee | 2947 | seqmutex_unlock(&c->btree_trans_lock); |
b84d42c3 | 2948 | } |
6bd68ec2 KO |
2949 | |
2950 | return trans; | |
1c6fdbd8 KO |
2951 | } |
2952 | ||
67e0dd8f KO |
2953 | static void check_btree_paths_leaked(struct btree_trans *trans) |
2954 | { | |
2955 | #ifdef CONFIG_BCACHEFS_DEBUG | |
2956 | struct bch_fs *c = trans->c; | |
2957 | struct btree_path *path; | |
2958 | ||
2959 | trans_for_each_path(trans, path) | |
2960 | if (path->ref) | |
2961 | goto leaked; | |
2962 | return; | |
2963 | leaked: | |
669f87a5 | 2964 | bch_err(c, "btree paths leaked from %s!", trans->fn); |
67e0dd8f KO |
2965 | trans_for_each_path(trans, path) |
2966 | if (path->ref) | |
2967 | printk(KERN_ERR " btree %s %pS\n", | |
88dfe193 | 2968 | bch2_btree_id_str(path->btree_id), |
67e0dd8f KO |
2969 | (void *) path->ip_allocated); |
2970 | /* Be noisy about this: */ | |
2971 | bch2_fatal_error(c); | |
2972 | #endif | |
2973 | } | |
2974 | ||
6bd68ec2 | 2975 | void bch2_trans_put(struct btree_trans *trans) |
c0ebe3e4 | 2976 | __releases(&c->btree_trans_barrier) |
1c6fdbd8 | 2977 | { |
67e0dd8f | 2978 | struct btree_insert_entry *i; |
1a21bf98 | 2979 | struct bch_fs *c = trans->c; |
616928c3 | 2980 | struct btree_transaction_stats *s = btree_trans_stats(trans); |
1a21bf98 | 2981 | |
ece254b2 | 2982 | bch2_trans_unlock(trans); |
1c6fdbd8 | 2983 | |
a5b696ee KO |
2984 | if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG_TRANSACTIONS)) { |
2985 | seqmutex_lock(&c->btree_trans_lock); | |
2986 | list_del(&trans->list); | |
2987 | seqmutex_unlock(&c->btree_trans_lock); | |
2988 | } | |
2989 | ||
33bd5d06 KO |
2990 | closure_sync(&trans->ref); |
2991 | ||
616928c3 KO |
2992 | if (s) |
2993 | s->max_mem = max(s->max_mem, trans->mem_max); | |
2994 | ||
67e0dd8f KO |
2995 | trans_for_each_update(trans, i) |
2996 | __btree_path_put(i->path, true); | |
2997 | trans->nr_updates = 0; | |
509d3e0a | 2998 | |
67e0dd8f | 2999 | check_btree_paths_leaked(trans); |
50dc0f69 | 3000 | |
f82755e4 KO |
3001 | if (trans->srcu_held) { |
3002 | check_srcu_held_too_long(trans); | |
c4accde4 | 3003 | srcu_read_unlock(&c->btree_trans_barrier, trans->srcu_idx); |
f82755e4 | 3004 | } |
876c7af3 | 3005 | |
9620c3ec KO |
3006 | if (trans->fs_usage_deltas) { |
3007 | if (trans->fs_usage_deltas->size + sizeof(trans->fs_usage_deltas) == | |
3008 | REPLICAS_DELTA_LIST_MAX) | |
3009 | mempool_free(trans->fs_usage_deltas, | |
67e0dd8f | 3010 | &c->replicas_delta_pool); |
9620c3ec KO |
3011 | else |
3012 | kfree(trans->fs_usage_deltas); | |
3013 | } | |
e131b6aa | 3014 | |
8a443d3e KO |
3015 | if (unlikely(trans->journal_replay_not_finished)) |
3016 | bch2_journal_keys_put(c); | |
3017 | ||
e131b6aa | 3018 | if (trans->mem_bytes == BTREE_TRANS_MEM_MAX) |
67e0dd8f | 3019 | mempool_free(trans->mem, &c->btree_trans_mem_pool); |
e131b6aa KO |
3020 | else |
3021 | kfree(trans->mem); | |
1a21bf98 | 3022 | |
6bd68ec2 KO |
3023 | /* Userspace doesn't have a real percpu implementation: */ |
3024 | if (IS_ENABLED(__KERNEL__)) | |
3025 | trans = this_cpu_xchg(c->btree_trans_bufs->trans, trans); | |
3026 | if (trans) | |
3027 | mempool_free(trans, &c->btree_trans_pool); | |
1c6fdbd8 | 3028 | } |
36e9d698 | 3029 | |
b3d1e6ca | 3030 | static void __maybe_unused |
afbc7194 KO |
3031 | bch2_btree_bkey_cached_common_to_text(struct printbuf *out, |
3032 | struct btree_bkey_cached_common *b) | |
d211b408 | 3033 | { |
c240c3a9 KO |
3034 | struct six_lock_count c = six_lock_counts(&b->lock); |
3035 | struct task_struct *owner; | |
3036 | pid_t pid; | |
3037 | ||
3038 | rcu_read_lock(); | |
3039 | owner = READ_ONCE(b->lock.owner); | |
3e3e02e6 | 3040 | pid = owner ? owner->pid : 0; |
c240c3a9 KO |
3041 | rcu_read_unlock(); |
3042 | ||
afbc7194 KO |
3043 | prt_tab(out); |
3044 | prt_printf(out, "%px %c l=%u %s:", b, b->cached ? 'c' : 'b', | |
88dfe193 | 3045 | b->level, bch2_btree_id_str(b->btree_id)); |
4e6defd1 | 3046 | bch2_bpos_to_text(out, btree_node_pos(b)); |
c240c3a9 | 3047 | |
afbc7194 KO |
3048 | prt_tab(out); |
3049 | prt_printf(out, " locks %u:%u:%u held by pid %u", | |
c240c3a9 | 3050 | c.n[0], c.n[1], c.n[2], pid); |
d211b408 KO |
3051 | } |
3052 | ||
50b13bee | 3053 | void bch2_btree_trans_to_text(struct printbuf *out, struct btree_trans *trans) |
5c1d808a | 3054 | { |
67e0dd8f | 3055 | struct btree_path *path; |
86b74451 | 3056 | struct btree_bkey_cached_common *b; |
c7ce2732 | 3057 | static char lock_types[] = { 'r', 'i', 'w' }; |
33981244 | 3058 | struct task_struct *task = READ_ONCE(trans->locking_wait.task); |
f154c3eb | 3059 | unsigned l, idx; |
495aabed | 3060 | |
afbc7194 KO |
3061 | if (!out->nr_tabstops) { |
3062 | printbuf_tabstop_push(out, 16); | |
3063 | printbuf_tabstop_push(out, 32); | |
3064 | } | |
3065 | ||
33981244 | 3066 | prt_printf(out, "%i %s\n", task ? task->pid : 0, trans->fn); |
495aabed | 3067 | |
f154c3eb | 3068 | trans_for_each_path_safe(trans, path, idx) { |
50b13bee KO |
3069 | if (!path->nodes_locked) |
3070 | continue; | |
495aabed | 3071 | |
50b13bee KO |
3072 | prt_printf(out, " path %u %c l=%u %s:", |
3073 | path->idx, | |
3074 | path->cached ? 'c' : 'b', | |
3075 | path->level, | |
88dfe193 | 3076 | bch2_btree_id_str(path->btree_id)); |
50b13bee | 3077 | bch2_bpos_to_text(out, path->pos); |
afbc7194 | 3078 | prt_newline(out); |
50b13bee KO |
3079 | |
3080 | for (l = 0; l < BTREE_MAX_DEPTH; l++) { | |
86b74451 | 3081 | if (btree_node_locked(path, l) && |
315c9ba6 | 3082 | !IS_ERR_OR_NULL(b = (void *) READ_ONCE(path->l[l].b))) { |
d4263e56 KO |
3083 | prt_printf(out, " %c l=%u ", |
3084 | lock_types[btree_node_locked_type(path, l)], l); | |
afbc7194 KO |
3085 | bch2_btree_bkey_cached_common_to_text(out, b); |
3086 | prt_newline(out); | |
495aabed KO |
3087 | } |
3088 | } | |
50b13bee | 3089 | } |
495aabed | 3090 | |
50b13bee KO |
3091 | b = READ_ONCE(trans->locking); |
3092 | if (b) { | |
9d7f2a41 KO |
3093 | prt_printf(out, " blocked for %lluus on", |
3094 | div_u64(local_clock() - trans->locking_wait.start_time, | |
3095 | 1000)); | |
afbc7194 KO |
3096 | prt_newline(out); |
3097 | prt_printf(out, " %c", lock_types[trans->locking_wait.lock_want]); | |
3098 | bch2_btree_bkey_cached_common_to_text(out, b); | |
3099 | prt_newline(out); | |
495aabed | 3100 | } |
495aabed KO |
3101 | } |
3102 | ||
36e9d698 KO |
3103 | void bch2_fs_btree_iter_exit(struct bch_fs *c) |
3104 | { | |
4aba7d45 | 3105 | struct btree_transaction_stats *s; |
6bd68ec2 KO |
3106 | struct btree_trans *trans; |
3107 | int cpu; | |
3108 | ||
3109 | trans = list_first_entry_or_null(&c->btree_trans_list, struct btree_trans, list); | |
3110 | if (trans) | |
3111 | panic("%s leaked btree_trans\n", trans->fn); | |
3112 | ||
3113 | if (c->btree_trans_bufs) | |
3114 | for_each_possible_cpu(cpu) | |
3115 | kfree(per_cpu_ptr(c->btree_trans_bufs, cpu)->trans); | |
3116 | free_percpu(c->btree_trans_bufs); | |
4aba7d45 KO |
3117 | |
3118 | for (s = c->btree_transaction_stats; | |
3119 | s < c->btree_transaction_stats + ARRAY_SIZE(c->btree_transaction_stats); | |
5c0bb66a KO |
3120 | s++) { |
3121 | kfree(s->max_paths_text); | |
4aba7d45 | 3122 | bch2_time_stats_exit(&s->lock_hold_times); |
5c0bb66a | 3123 | } |
4aba7d45 | 3124 | |
99fafb04 KO |
3125 | if (c->btree_trans_barrier_initialized) |
3126 | cleanup_srcu_struct(&c->btree_trans_barrier); | |
e131b6aa | 3127 | mempool_exit(&c->btree_trans_mem_pool); |
6bd68ec2 | 3128 | mempool_exit(&c->btree_trans_pool); |
36e9d698 KO |
3129 | } |
3130 | ||
50a8a732 | 3131 | void bch2_fs_btree_iter_init_early(struct bch_fs *c) |
36e9d698 | 3132 | { |
4aba7d45 | 3133 | struct btree_transaction_stats *s; |
36e9d698 | 3134 | |
4aba7d45 KO |
3135 | for (s = c->btree_transaction_stats; |
3136 | s < c->btree_transaction_stats + ARRAY_SIZE(c->btree_transaction_stats); | |
5c0bb66a | 3137 | s++) { |
4aba7d45 | 3138 | bch2_time_stats_init(&s->lock_hold_times); |
5c0bb66a KO |
3139 | mutex_init(&s->lock); |
3140 | } | |
4aba7d45 | 3141 | |
495aabed | 3142 | INIT_LIST_HEAD(&c->btree_trans_list); |
a5b696ee | 3143 | seqmutex_init(&c->btree_trans_lock); |
50a8a732 TB |
3144 | } |
3145 | ||
3146 | int bch2_fs_btree_iter_init(struct bch_fs *c) | |
3147 | { | |
3148 | int ret; | |
495aabed | 3149 | |
6bd68ec2 KO |
3150 | c->btree_trans_bufs = alloc_percpu(struct btree_trans_buf); |
3151 | if (!c->btree_trans_bufs) | |
3152 | return -ENOMEM; | |
3153 | ||
3154 | ret = mempool_init_kmalloc_pool(&c->btree_trans_pool, 1, | |
3155 | sizeof(struct btree_trans)) ?: | |
e131b6aa | 3156 | mempool_init_kmalloc_pool(&c->btree_trans_mem_pool, 1, |
99fafb04 KO |
3157 | BTREE_TRANS_MEM_MAX) ?: |
3158 | init_srcu_struct(&c->btree_trans_barrier); | |
3159 | if (!ret) | |
3160 | c->btree_trans_barrier_initialized = true; | |
3161 | return ret; | |
36e9d698 | 3162 | } |