summaryrefslogtreecommitdiff
path: root/fs/bcachefs/btree_iter.c
AgeCommit message (Collapse)Author
2022-05-20bcachefs: Convert to lib/printbuf.cprintbuf_v2Kent Overstreet
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-25bcachefs: Put btree_trans_verify_sorted() behind debug_check_iteratorsKent Overstreet
This is pretty expensive, and we've tested sufficiently with it now that it doesn't need to be on by default. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-21fixup! bcachefs: bch2_btree_iter_peek_all_levels()Kent Overstreet
2022-04-17bcachefs: bch2_btree_iter_peek_all_levels()Kent Overstreet
This adds bch2_btree_iter_peek_all_levels(), which returns keys from every level of the btree - interior nodes included - in monotonically increasing order, soon to be used by the backpointers check & repair code. - BTREE_ITER_ALL_LEVELS can now be passed to for_each_btree_key() to iterate thusly, much like BTREE_ITER_SLOTS - The existing algorithm in bch2_btree_iter_advance() doesn't work with peek_all_levels(): we have to defer the actual advancing until the next time we call peek, where we have the btree path traversed and uptodate. So, we add an advanced bit to btree_iter; when BTREE_ITER_ALL_LEVELS is set bch2_btree_iter_advanced() just marks the iterator as advanced. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: btree_path_set_level_(up|down)Kent Overstreet
This adds two new helpers to btree_iter.c for changing the level of a path up or down - to be used by the new bch2_btree_iter_peek_all_levels(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_btree_iter_peek_slot() now works on interior nodesKent Overstreet
The new backpointers code will be using bch2_btree_iter_peek_slot() on interior nodes - this patch updates peek_slot() to make that work. - Pass the correct level to bch2_journal_keys_peek_slot() - We should only set BTREE_ITER_CACHED or BTREE_ITER_WITH_KEY_CACHE when using bch2_trans_iter_init(), not bch2_trans_node_iter_init() - Update assertions Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Introduce bch2_journal_keys_peek_(upto|slot)()Kent Overstreet
When many journal replay keys have been overwritten, bch2_journal_keys_peek() was taking excessively long to scan before it found a key to return. Fix this by introducing bch2_journal_keys_peek_upto() which takes a parameter for the end of the range we want, so that we can terminate the search much sooner, and replace all uses of bch2_journal_keys_peek() with peek_upto() or peek_slot(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Use darray for extra_journal_entriesKent Overstreet
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: btree_path_make_mut() clears should_be_lockedKent Overstreet
This fixes a bug where __bch2_btree_node_update_key() wasn't clearing should_be_locked, leading to bch2_btree_path_traverse() always failing - all callers of btree_path_make_mut() want should_be_locked cleared, so do it there. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Add a missing btree_path_set_dirty() callsKent Overstreet
bch2_btree_iter_next_node() was mucking with other btree_path state without setting path->update to be consistent with the fact that the path is very much no longer uptodate - oops. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_trans_updates_to_text()Kent Overstreet
This turns bch2_dump_trans_updates() into a to_text() method - this way it can be used by debug tracing. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_trans_inconsistent()Kent Overstreet
Add a new error macro that also dumps transaction updates in addition to doing an emergency shutdown - when a transaction update discovers or is causing a fs inconsistency, it's helpful to see what updates it was doing. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_btree_iter_peek_upto()Kent Overstreet
In BTREE_ITER_FILTER_SNAPHOTS mode, we skip over keys in unrelated snapshots. When we hit the end of an inode, if the next inode(s) are in a different subvolume, we could potentially have to skip past many keys before finding a key we can return to the caller, so they can terminate the iteration. This adds a peek_upto() variant to solve this problem, to be used when we know the range we're searching within. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Delay setting path->should_be_lockedKent Overstreet
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Lock ordering asserts for traverse_all()Kent Overstreet
This adds some new assertions that we always take locks in the correct order while running under traverse_all() - we've been seeing some livelocks and a bit of strange behaviour, this helps ensure that everything is working the way we expect. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix lock ordering under traverse_all()Kent Overstreet
traverse_all() traverses btree paths in sorted order, so it should never see transaction restarts due to lock ordering violations. But some code in __bch2_btree_path_upgrade(), while necessary when not running under traverse_all(), was causing some confusing lock ordering violations - disabling this code under traverse_all() will let us put in some more assertions. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix error handling in traverse_all()Kent Overstreet
In btree_path_traverse_all() we were failing to check for -EIO in the retry loop, and after btree node read error we'd go into an infinite loop. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Don't keep around btree_paths unnecessarilyKent Overstreet
When bch2_trans_begin() is called and there hasn't been a transaction restart, we presume that we're now doing something new - iterating over different keys, and we now shouldn't keep aruond paths related to the previous transaction, excepting the subvolumes btree. This should fix some of our "transaction path overflow" bugs. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix an assert in the initial GC pathKent Overstreet
With lockdep enabled, we (rarely) hit an assertion in bch2_gc_init_recurse() -> bch2_gc_mark_key() -> bch2_trans_unlock(): this is because initial GC doesn't use btree iterators for btree walking, it does its own thing for various (complicated) reasons - but we don't have to worry about deadlocks because we're only taking read locks. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix btree path sortingKent Overstreet
In btree_update_interior.c, we were changing a path's level directly - which affects path sort order - without re-sorting paths, leading to assertions when bch2_path_get() verified paths were sorted correctly. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Heap allocate printbufsKent Overstreet
This patch changes printbufs dynamically allocate and reallocate a buffer as needed. Stack usage has become a bit of a problem, and a major cause of that has been static size string buffers on the stack. The most involved part of this refactoring is that printbufs must now be exited with printbuf_exit(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Improve debug assertionKent Overstreet
We're hitting a strange bug with transaction paths not being sorted correctly - this dumps transaction paths in the order we thought was sorted, which will hopefully shed some light as to what's going on. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Always clear should_be_locked in bch2_trans_begin()Kent Overstreet
bch2_trans_begin() invalidates all iterators, until they're revalidated by calling peek() or traverse(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: run_one_trigger() now checks journal keysKent Overstreet
Previously, when doing updates and running triggers before journal replay completes, triggers would see the incorrect key for the old key being overwritten - this patch updates the trigger code to check the journal keys when necessary, needed for the upcoming allocator rewrite. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Stash a copy of key being overwritten in btree_insert_entryKent Overstreet
We currently need to call bch2_btree_path_peek_slot() multiple times in the transaction commit path - and some of those need to be updated to also check the keys from journal replay, too. Let's consolidate this and stash the key being overwritten in btree_insert_entry. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix failure to allocate btree node in cacheKent Overstreet
The error code when we fail to allocate a node in the btree node cache doesn't make it to bch2_btree_path_traverse_all(). Instead, we need to stash a flag in btree_trans so we know we have to take the cannibalize lock. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix __btree_path_traverse_allKent Overstreet
The loop that traverses paths in traverse_all() needs to be a little bit tricky, because traversing a path can cause other paths to be added (or perhaps removed) at about the same position. The old logic was buggy, replace it with simpler logic. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix __bch2_btree_node_lockKent Overstreet
__bch2_btree_node_lock() was implementing the wrong lock ordering for cached vs. non cached paths - this fixes it to match the btree path sort order as defined by __btree_path_cmp(), and also simplifies the code some. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Also show when blocked on write locksKent Overstreet
This consolidates some of the btree node lock path, so that when we're blocked taking a write lock on a node it shows up in bch2_btree_trans_to_text(), along with intent and read locks. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Check for btree locks held on transaction initKent Overstreet
Ideally we would disallow multiple btree_trans being initialized within the same process - and hopefully we will at some point, the stack usage is excessive - but for now there are a couple places where we do this: - transaction commit error path -> journal reclaim - btree key cache flush - move data path -> do_pending_writes -> write path -> bucket allocation (in the near future when bucket allocation switches to using a freespace btree) In order to avoid deadlocking the first btree_trans must have been unlocked with bch2_trans_unlock() before using the second btree_trans - this patch adds an assertion to bch2_trans_init() that verifies that this has been done when lockdep is enabled. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Kill bch2_bkey_debugcheckKent Overstreet
The old .debugcheck methods are no more and this just calls the .invalid method, which doesn't add much since we already check that when doing btree updates and when reading metadata in. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: BTREE_ITER_WITH_KEY_CACHEKent Overstreet
This is the start of cache coherency with the btree key cache - this adds a btree iterator flag that causes lookups to also check the key cache when we're iterating over the btree (not iterating over the key cache). Note that we could still race with another thread creating at item in the key cache and updating it, since we aren't holding the key cache locked if it wasn't found. The next patch for the update path will address this by causing the transaction to restart if the key cache is found to be dirty. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_btree_path_set_pos()Kent Overstreet
bch2_btree_path_set_pos() is now available outside of btree_iter.c Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: btree_id_cached()Kent Overstreet
Add a new helper that returns true if the given btree ID uses the btree key cache. This enables some new cleanups, since the helper can check the options for whether caching is enabled on a given btree. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Don't use in-memory bucket array for alloc updatesKent Overstreet
More prep work for getting rid of the in-memory bucket array: now that we have BTREE_ITER_WITH_JOURNAL, the allocator code can do ntree lookups before journal replay is finished, and there's no longer any need for it to get allocation information from the in-memory bucket array. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: iter->update_pathKent Overstreet
With BTREE_ITER_FILTER_SNAPSHOTS, we have to distinguish between the path where the key was found, and the path for inserting into the current snapshot. This adds a new field to struct btree_iter for saving a path for the current snapshot, and plumbs it through bch2_trans_update(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Refactor bch2_btree_iter()Kent Overstreet
This splits bch2_btree_iter() up into two functions: an inner function that handles BTREE_ITER_WITH_JOURNAL, BTREE_ITER_WITH_UPDATES, and iterating acrcoss leaf nodes, and an outer one that implements BTREE_ITER_FILTER_SNAPHSOTS. This is prep work for remember a btree_path at our update position in BTREE_ITER_FILTER_SNAPSHOTS mode. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Tracepoint improvementsKent Overstreet
This improves the transaction restart tracepoints - adding distinct tracepoints for all the locations and reasons a transaction might have been restarted, and ensures that there's a tracepoint for every transaction restart. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Switch to __func__for recording where btree_trans was initializedKent Overstreet
Symbol decoding, via %ps, isn't supported in userspace - this will also be faster when we're using trans->fn in the fast path, as with the new BCH_JSET_ENTRY_log journal messages. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: BTREE_ITER_WITH_JOURNALKent Overstreet
This adds a new btree iterator flag, BTREE_ITER_WITH_JOURNAL, that is automatically enabled when initializing a btree iterator before journal replay has completed - it overlays the contents of the journal with the btree. This lets us delete bch2_btree_and_journal_walk() and just use the normal btree iterator interface instead - which also lets us delete a significant amount of duplicated code. Note that BTREE_ITER_WITH_JOURNAL is still unoptimized in this patch - we're redoing the binary search over keys in the journal every time we call bch2_btree_iter_peek(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Use BTREE_ITER_NOPRESERVE in bch2_btree_iter_verify_ret()Kent Overstreet
This fixes a transaction path overflow. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: BTREE_ITER_NOPRESERVEKent Overstreet
This adds a flag to not mark the initial btree_path as preserve, for paths that we expect to be cheap to reconstitute if necessary - this solves a btree_path overflow caused by need_whiteout_for_snapshot(). Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix some shutdown path bugsKent Overstreet
This fixes some bugs when we hit an error very early in the filesystem startup path, before most things have been initialized. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Sysfs internal/btree_transactions is now always enabledKent Overstreet
This highly-useful debugging feature helps with debugging deadlocks, and it doesn't cost much, so let's have it always enabled. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Improve tracing of btree_path leaksKent Overstreet
This patch plumbs the btree_path->ip_allocated field back to where the btree_iter that owns it was first initialized - meaning it will be much easier to figure out which btree_iter wasn't exited properly. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: bch2_assert_pos_locked()Kent Overstreet
This adds a new assertion to be used by bch2_inode_update_after_write(), which updates the VFS inode based on the update to the btree inode we just did - we require that the btree inode still be locked when we do that update. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: path->should_be_locked fixesKent Overstreet
- We should only be clearing should_be_locked in btree_path_set_pos() - it's the responsiblity of the btree_path code, not the btree_iter code. - bch2_path_put() needs to pay attention to path->should_be_locked, to ensure we don't drop locks we're supposed to be keeping. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix upgrade_readers()Kent Overstreet
The bch2_btree_path_upgrade() call was failing and tripping an assert - path->level + 1 is in this case not necessarily exactly what we want, fix it by upgrading exactly the locks we want. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Fix faulty assertionKent Overstreet
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-04-17bcachefs: Convert bch2_mark_key() to take a btree_trans *Kent Overstreet
This helps to unify the interface between bch2_mark_key() and bch2_trans_mark_key() - and it also gives access to the journal reservation and journal seq in the mark_key path. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>