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