summaryrefslogtreecommitdiff
path: root/fs/bcachefs/btree_iter.c
AgeCommit message (Collapse)Author
2022-04-17bcachefs: Fix bch2_btree_iter_advance()Kent Overstreet
Was popping an assertion on !BTREE_ITER_ALL_SNAPSHOTS iters when getting to the end. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix bch2_btree_iter_next_node()Kent Overstreet
We were modifying state, then return -EINTR, causing us to skip nodes - ouch. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Must check for errors from bch2_trans_cond_resched()Kent Overstreet
But we don't need to call it from outside the btree iterator code anymore, since it's called by bch2_trans_begin() and bch2_btree_path_traverse(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix restart handling in for_each_btree_key()Kent Overstreet
Code that uses for_each_btree_key often wants transaction restarts to be handled locally and not returned. Originally, we wouldn't return transaction restarts if there was a single iterator in the transaction - the reasoning being if there weren't other iterators being invalidated, and the current iterator was being advanced/retraversed, there weren't any locks or iterators we were required to preserve. But with the btree_path conversion that approach doesn't work anymore - even when we're using for_each_btree_key() with a single iterator there will still be two paths in the transaction, since we now always preserve the path at the pos the iterator was initialized at - the reason being that on restart we often restart from the same place. And it turns out there's now a lot of for_each_btree_key() uses that _do not_ want transaction restarts handled locally, and should be returning them. This patch splits out for_each_btree_key_norestart() and for_each_btree_key_continue_norestart(), and converts existing users as appropriate. for_each_btree_key(), for_each_btree_key_continue(), and for_each_btree_node() now handle transaction restarts themselves by calling bch2_trans_begin() when necessary - and the old hack to not return transaction restarts when there's a single path in the transaction has been deleted. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_trans_exit() no longer returns errorsKent Overstreet
Now that peek_node()/next_node() are converted to return errors directly, we don't need bch2_trans_exit() to return errors - it's cleaner this way and wasn't used much anymore. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: for_each_btree_node() now returns errors directlyKent Overstreet
This changes for_each_btree_node() to work like for_each_btree_key(), and to that end bch2_btree_iter_peek_node() and next_node() also return error ptrs. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Improve bch2_dump_trans_paths_updates()Kent Overstreet
Also print the key beyng overwritten for each update. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: More btree iterator fixesKent Overstreet
- check for getting to the end of the btree in bch2_path_verify_locks and __btree_path_traverse_all(), this fixes an infinite loop in __btree_path_traverse_all(). - relax requirement in bch2_btree_node_upgrade() that we must want an intent lock, this fixes bugs with paths that point to interior nodes (nonzero level). - bch2_btree_node_update_key(): fix it to upgrade the path to an intent lock, if necessary Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Ensure btree_path consistent with node iteratorsKent Overstreet
Btree node iterators want the interior btree_path to point to the same pos as the returned btree node - this fixes a regression from the introduction of btree_path, where rewriting/updating keys of btree nodes (e.g. in bch2_dev_metadata_drop()) via btree node iterators. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Require snapshot id to be setKent Overstreet
Now that all the existing code has been converted for snapshots, this patch changes the code for initializing a btree iterator to require a snapshot to be specified, and also change bkey_invalid() to allow for non U32_MAX snapshot IDs. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: BTREE_ITER_FILTER_SNAPSHOTSKent Overstreet
For snapshots, we need to implement btree lookups that return the first key that's an ancestor of the snapshot ID the lookup is being done in - and filter out keys in unrelated snapshots. This patch adds the btree iterator flag BTREE_ITER_FILTER_SNAPSHOTS which does that filtering. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17Revert "bcachefs: Add more assertions for locking btree iterators out of order"Kent Overstreet
Figured out the bug we were chasing, and it had nothing to do with locking btree iterators/paths out of order. This reverts commit ff08733dd298c969aec7c7828095458f73fd5374. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Consolidate intent lock code in btree_path_up_until_good_nodeKent Overstreet
We need to take all needed intent locks when relocking an iterator: bch2_btree_path_traverse() had a special cased, faster version of this, but it really should be in up_until_good_node() so that set_pos() can use it too. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Make trans_for_each_path_inorder() fasterKent Overstreet
Again, getting rid of data dependencies. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Inline array of sorted paths in btree_transKent Overstreet
It's small, and frequently used - this gets rid of a data dependency load. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Optimize btree lookups in write pathKent Overstreet
This patch significantly reduces the number of btree lookups required in the extent update path. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Add a missing btree_path_make_mut() callKent Overstreet
Also add another small helper, btree_path_clone(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Drop some fast path tracepointsKent Overstreet
These haven't turned out to be useful Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Add more assertions for locking btree iterators out of orderKent Overstreet
btree_path_traverse_all() traverses btree iterators in sorted order, and thus shouldn't see transaction restarts due to potential deadlocks - but sometimes we do. This patch adds some more assertions and tracks some more state to help track this down. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Tighten up btree locking invariantsKent Overstreet
New rule is: if a btree path holds any locks it should be holding precisely the locks wanted (accoringing to path->level and path->locks_want). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Extent btree iterators are no longer specialKent Overstreet
Since iter->real_pos was introduced, we no longer have to deal with extent btree iterators that have skipped past deleted keys - this is a real performance improvement on btree updates. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: btree_pathKent Overstreet
This splits btree_iter into two components: btree_iter is now the externally visible componont, and it points to a btree_path which is now reference counted. This means we no longer have to clone iterators up front if they might be mutated - btree_path can be shared by multiple iterators, and cloned if an iterator would mutate a shared btree_path. This will help us use iterators more efficiently, as well as slimming down the main long lived state in btree_trans, and significantly cleans up the logic for iterator lifetimes. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Kill BTREE_ITER_NODESKent Overstreet
We really only need to distinguish between btree iterators and btree key cache iterators - this is more prep work for btree_path. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Kill BTREE_ITER_NEED_PEEKKent Overstreet
This was used for an optimization that hasn't existing in quite awhile - iter->uptodate will probably be going away as well. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Kill bpos_diff()Kent Overstreet
This improves the btree iterator lookup code by using trans_for_each_iter_inorder(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Btree iterators are always sortedKent Overstreet
No need to actually resort, can just replace it with an debug assert. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: More renamingKent Overstreet
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Clean up/rename bch2_trans_node_* fnsKent Overstreet
These utility functions are for managing btree node state within a btree_trans - rename them for consistency, and drop some unneeded arguments. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Further reduce iter->trans usageKent Overstreet
This is prep work for splitting btree_path out from btree_iter - btree_path will not have a pointer to btree_trans. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Kill BTREE_ITER_SET_POS_AFTER_COMMITKent Overstreet
BTREE_ITER_SET_POS_AFTER_COMMIT is used internally to automagically advance extent btree iterators on sucessful commit. But with the upcomnig btree_path patch it's getting more awkward to support, and it adds overhead to core data structures that's only used in a few places, and can be easily done by the caller instead. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Reduce iter->trans usageKent Overstreet
Disfavoured, and should go away. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_dump_trans_iters_updates()Kent Overstreet
This factors out bch2_dump_trans_iters_updates() from the iter alloc overflow path, and makes some small improvements to what it prints. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Ensure iter->real_pos is consistent with key returnedKent Overstreet
iter->real_pos needs to match the key returned or bad things will happen when we go to update the key at that position. When we returned a pending update from btree_trans_peek_updates(), this wasn't necessarily the case. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Free iterator if we have duplicateKent Overstreet
This helps - but does not fully fix - the outstanding "transaction iterator overflow" bugs. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Minor btree iter refactoringKent Overstreet
This makes the flow control in bch2_btree_iter_peek() and bch2_btree_iter_peek_prev() a bit cleaner. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix btree_trans_peek_updates()Kent Overstreet
Should have been using bpos_cmp(), not bkey_cmp(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Keep a sorted list of btree iteratorsKent Overstreet
This will be used to make other operations on btree iterators within a transaction more efficient, and enable some other improvements to how we manage btree iterators. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: traverse_all() shouldn't be restarting the transactionKent Overstreet
We're only called by bch2_trans_begin() now. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: __bch2_trans_commit() no longer calls bch2_trans_reset()Kent Overstreet
It's now the caller's responsibility to call bch2_trans_begin. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: trans->restartedKent Overstreet
Start tracking when btree transactions have been restarted - and assert that we're always calling bch2_trans_begin() immediately after transaction restart. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Ensure btree_iter_traverse() obeys iter->should_be_lockedKent Overstreet
iter->should_be_locked means that if bch2_btree_iter_relock() fails, we need to restart the transaction. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_btree_iter_traverse() shouldn't normally call traverse_all()Kent Overstreet
If there's more than one iterator in the btree_trans, it's requried to call bch2_trans_begin() to handle transaction restarts. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Clean up interior update pathsKent Overstreet
Btree node merging now happens prior to transaction commit, not after, so we don't need to pay attention to BTREE_INSERT_NOUNLOCK. Also, foreground_maybe_merge shouldn't be calling bch2_btree_iter_traverse_all() - this is becoming private to the btree iterator code and should only be called by bch2_trans_begin(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Always check for transaction restartsKent Overstreet
On transaction restart iterators won't be locked anymore - make sure we're always checking for errors. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: traverse_all() is responsible for clearing should_be_lockedKent Overstreet
bch2_btree_iter_traverse_all() may loop, and it needs to clear iter->should_be_locked on every iteration. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_trans_relock() only relocks iters that should be lockedKent Overstreet
This avoids unexpected lock restarts in bch2_btree_iter_traverse_all(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Minor tracepoint improvementsKent Overstreet
Btree iterator tracepoints should print whether they're for the key cache. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_btree_iter_relock_intent()Kent Overstreet
This adds a new helper for btree_cache.c that does what we want where the iterator is still being traverse - and also eliminates some unnecessary transaction restarts. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix bch2_btree_iter_rewind()Kent Overstreet
We'd hit a BUG() when rewinding at the start of the btree on btrees with snapshots. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Tighten up btree_iter locking assertionsKent Overstreet
We weren't correctly verifying that we had interior node intent locks - this patch also fixes bugs uncovered by the new assertions. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>