linux.git
18 months agobcachefs: Improved human readable integer parsing
Kent Overstreet [Fri, 10 Jun 2022 23:45:19 +0000 (19:45 -0400)]
bcachefs: Improved human readable integer parsing

Printbufs recently switched to using string_get_size() for printing
integers in human readable units. This updates __bch2_strtoh() to parse
numbers printed by string_get_size() - we now have to handle floating
point numbers, and new unit suffixes.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix freespace initialization
Kent Overstreet [Fri, 10 Jun 2022 23:39:00 +0000 (19:39 -0400)]
bcachefs: Fix freespace initialization

bch2_dev_freespace_init() was using __bch2_trans_do() incorrectly, and
calling bch2_bucket_do_index() with a stale alloc key.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Printbuf rework
Kent Overstreet [Sat, 4 Feb 2023 02:01:40 +0000 (21:01 -0500)]
bcachefs: Printbuf rework

This converts bcachefs to the modern printbuf interface/implementation,
synced with the version to be submitted upstream.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix btree node read error path
Kent Overstreet [Tue, 7 Jun 2022 02:09:11 +0000 (22:09 -0400)]
bcachefs: Fix btree node read error path

We were forgetting to clear the read_in_flight flag - oops. This also
fixes it to not call bch2_fatal_error() before topology repair has had a
chance to do its thing.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix btree_and_journal_iter
Kent Overstreet [Tue, 7 Jun 2022 01:59:34 +0000 (21:59 -0400)]
bcachefs: Fix btree_and_journal_iter

We had a bug where btree_and_journal_iter would return the same key
twice - after deleting it (perhaps because it was present in both the
btree and the journal?)

This reworks btree_and_journal_iter to track the current position, much
like btree_paths, which makes the logic considerably simpler and more
robust.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix for cmd_list_journal
Kent Overstreet [Tue, 7 Jun 2022 03:04:33 +0000 (23:04 -0400)]
bcachefs: Fix for cmd_list_journal

cmd_list_journal wasn't correctly listing the most recent journal
entries as blacklisted - because in the recovery path when just reading
the journal, we were failing to add those to the blacklist table.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Also log overwrites in journal
Kent Overstreet [Sun, 5 Jun 2022 19:32:57 +0000 (15:32 -0400)]
bcachefs: Also log overwrites in journal

Lately we've been doing a lot of debugging by looking at the journal to
see what was changed, and by what code path. This patch adds a new
journal entry type for recording overwrites, so that we don't have to
search backwards through the journal to see what was being overwritten
in order to work out what the triggers were supposed to be doing.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Refactor journal entry adding
Kent Overstreet [Sun, 5 Jun 2022 19:29:00 +0000 (15:29 -0400)]
bcachefs: Refactor journal entry adding

This takes copying the payload out of bch2_journal_add_entry(), which
means we can use it for journal_transaction_name() - also prep work for
journalling overwrites.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Add some missing error messages
Kent Overstreet [Sat, 4 Jun 2022 18:49:02 +0000 (14:49 -0400)]
bcachefs: Add some missing error messages

bch2_opt_parse() was failing to generate error messages in error path.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix memory corruption in encryption path
Kent Overstreet [Fri, 3 Jun 2022 06:34:14 +0000 (02:34 -0400)]
bcachefs: Fix memory corruption in encryption path

When do_encrypt() was passed a vmalloc address and the buffer spanned
more than a single page, we were encrypting/decrypting completely
different pages than the ones intended.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: bch2_trans_reset_updates()
Kent Overstreet [Sun, 29 May 2022 15:38:48 +0000 (11:38 -0400)]
bcachefs: bch2_trans_reset_updates()

Factor out a new helper.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix error checking in bch2_fs_alloc()
Kent Overstreet [Sat, 28 May 2022 20:21:01 +0000 (16:21 -0400)]
bcachefs: Fix error checking in bch2_fs_alloc()

One of the init calls had a ; instead of a ?:, and errors after that got
dropped - oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Print message on btree node read retry success
Kent Overstreet [Thu, 26 May 2022 19:19:20 +0000 (15:19 -0400)]
bcachefs: Print message on btree node read retry success

Right now, we print an error message on btree node read error, and we
print that we're retrying, but we don't explicitly say if the retry
succeeded - this makes things a little clearer.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix journal_keys_search() overhead
Kent Overstreet [Sat, 21 May 2022 17:10:39 +0000 (13:10 -0400)]
bcachefs: Fix journal_keys_search() overhead

Previously, on every btree_iter_peek() operation we were searching the
journal keys, doing a full binary search - which was slow.

This patch fixes that by saving our position in the journal keys, so
that we only do a full binary search when moving our position backwards
or a large jump forwards.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Always print when doing journal replay in fsck
Kent Overstreet [Thu, 26 May 2022 01:34:11 +0000 (21:34 -0400)]
bcachefs: Always print when doing journal replay in fsck

This logging improvement helps see when the previous fsck pass has
completed.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Rename group to label for remaining strings.
Daniel Hill [Wed, 25 May 2022 02:57:39 +0000 (14:57 +1200)]
bcachefs: Rename group to label for remaining strings.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix encryption path on arm
Kent Overstreet [Tue, 24 May 2022 02:37:01 +0000 (22:37 -0400)]
bcachefs: Fix encryption path on arm

flush_dcache_page() is not a noop on arm, but we were using
virt_to_page() instead of vmalloc_to_page() for an address on the kernel
stack - vmalloc memory, leading to an oops in flush_dcache_page().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Switch to key_type_user, not logon
Kent Overstreet [Sat, 14 May 2022 11:00:22 +0000 (07:00 -0400)]
bcachefs: Switch to key_type_user, not logon

The only difference key_type_logon and key_type_user is that
key_type_logon keys can't be read by userspace.

However, userspace has actually been adding keys to both the logon and
user keychains, because userspace fsck requires the keychain interface -
so we might as well just use user and drop the logon keychain.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: LRU repair tweaks
Kent Overstreet [Sat, 14 May 2022 10:58:51 +0000 (06:58 -0400)]
bcachefs: LRU repair tweaks

 - Drop old unneeded parameter for whether we're in initial GC - which
   was from when btree updates had to be done differently before we
   went RW.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Delete bch_writepage
Kent Overstreet [Thu, 5 May 2022 21:20:41 +0000 (17:20 -0400)]
bcachefs: Delete bch_writepage

Per Dave Chinner and the xfs folks, .writepage is no longer needed, and
it's better not to define it if .writepages is the intended path.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Make bch_option compatible with Rust ffi
Brett Holman [Tue, 3 May 2022 22:50:57 +0000 (16:50 -0600)]
bcachefs: Make bch_option compatible with Rust ffi

Rust FFI lacks support for unnamed structs and unions. The space
saved in bch_option is not enough to be significant.

Signed-off-by: Brett Holman <bholman.devel@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Put btree_trans_verify_sorted() behind debug_check_iterators
Kent Overstreet [Mon, 25 Apr 2022 06:12:03 +0000 (02:12 -0400)]
bcachefs: Put btree_trans_verify_sorted() behind debug_check_iterators

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>
18 months agobcachefs: Fix extent merging
Kent Overstreet [Mon, 25 Apr 2022 03:03:02 +0000 (23:03 -0400)]
bcachefs: Fix extent merging

When merging extents, we have to check that we won't overflow size
fields in any CRC entries - but the check for this was wrong, because in
the loop it was in we weren't keeping a pointer to the (packed, encoded)
CRC field.

Fix this by moving it to its own loop.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Improve invalid bkey error message
Kent Overstreet [Tue, 19 Apr 2022 03:43:08 +0000 (23:43 -0400)]
bcachefs: Improve invalid bkey error message

Bkeys have gotten a lot bigger since this code was written and now are
often formatted across multiple lines - while the reason a bkey is
invalid will still be short and fit on a single line. This patch prints
the error bfore the bkey, making it a bit more readable.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix journal_iters_fix()
Kent Overstreet [Thu, 21 Apr 2022 04:34:58 +0000 (00:34 -0400)]
bcachefs: Fix journal_iters_fix()

journal_iters_fix() was incorrectly rewinding iterators past keys they
had already returned, leading to those keys being double counted in the
bch2_gc() path - oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Go RW before bch2_check_lrus()
Kent Overstreet [Thu, 21 Apr 2022 17:13:57 +0000 (13:13 -0400)]
bcachefs: Go RW before bch2_check_lrus()

btree updates before going RW are expensive if they're in random order,
since they use the list of keys for journal replay to insert, which is
just a gap buffer.

This patch improves the bucket invalidate path so that if
bch2_check_lrus() hasn't finished it only prints warnings instead of
doing an emergency shutdown, which means we can now set BCH_FS_MAY_GO_RW
before bch2_check_lrus().

Also, the filesystem state bits are reorganized a bit.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Add persistent counters
Daniel Hill [Tue, 15 Mar 2022 08:36:33 +0000 (21:36 +1300)]
bcachefs: Add persistent counters

This adds a new superblock field for persisting counters
and adds a sysfs interface in counters/ exposing these counters.

The superblock field is ignored by older versions letting us avoid
an on disk version bump.

Each sysfs file outputs a counter that tracks since filesystem
creation and a counter for the current mount session.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Tracepoint improvements
Kent Overstreet [Sun, 17 Apr 2022 22:06:31 +0000 (18:06 -0400)]
bcachefs: Tracepoint improvements

Delete some obsolete tracepoints, organize alloc tracepoints better,
make a few tracepoints more consistent.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Don't kick journal reclaim unless low on space
Kent Overstreet [Tue, 19 Apr 2022 02:03:17 +0000 (22:03 -0400)]
bcachefs: Don't kick journal reclaim unless low on space

We shouldn't kick journal reclaim unnecessarily, it's got its own timer
for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Lock ordering fix
Kent Overstreet [Tue, 19 Apr 2022 01:50:19 +0000 (21:50 -0400)]
bcachefs: Lock ordering fix

Can't take btree node locks while holding btree_reserve_cache_lock - it
would be nice if we could check this with lockdep.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Shutdown path improvements
Kent Overstreet [Sun, 17 Apr 2022 21:30:49 +0000 (17:30 -0400)]
bcachefs: Shutdown path improvements

We're seeing occasional firings of the assertion in the key cache
shutdown code that nr_dirty == 0, which means we must sometimes be doing
transaction commits after we've gone read only.

Cleanups & changes:
 - BCH_FS_ALLOC_CLEAN renamed to BCH_FS_CLEAN_SHUTDOWN
 - new helper bch2_btree_interior_updates_flush(), which returns true if
   it had to wait
 - bch2_btree_flush_writes() now also returns true if there were btree
   writes in flight
 - __bch2_fs_read_only now checks if btree writes were in flight in the
   shutdown loop: btree write completion does a transaction update, to
   update the pointer in the parent node
 - assert that !BCH_FS_CLEAN_SHUTDOWN in __bch2_trans_commit

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix hash_check_key()
Kent Overstreet [Sun, 17 Apr 2022 23:02:04 +0000 (19:02 -0400)]
bcachefs: Fix hash_check_key()

hash_check_key() was incorrectly handling transaction restarts - switch
it to for_each_btree_key_norestart().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Allocate some extra room in btree_key_cache_fill()
Kent Overstreet [Sun, 17 Apr 2022 21:50:47 +0000 (17:50 -0400)]
bcachefs: Allocate some extra room in btree_key_cache_fill()

If we allocate a buffer that's a bit bigger than necessary the
transaction commit path will be much less likely to have to reallocate -
which requires a transaction restart.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: bch2_btree_iter_peek_all_levels()
Kent Overstreet [Tue, 12 Apr 2022 22:04:08 +0000 (18:04 -0400)]
bcachefs: bch2_btree_iter_peek_all_levels()

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@linux.dev>
18 months agobcachefs: btree_path_set_level_(up|down)
Kent Overstreet [Thu, 14 Apr 2022 19:43:37 +0000 (15:43 -0400)]
bcachefs: btree_path_set_level_(up|down)

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>
18 months agobcachefs: bch2_btree_iter_peek_slot() now works on interior nodes
Kent Overstreet [Thu, 14 Apr 2022 19:45:00 +0000 (15:45 -0400)]
bcachefs: bch2_btree_iter_peek_slot() now works on interior nodes

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>
18 months agobcachefs: btree_update_interior.c prep for backpointers
Kent Overstreet [Thu, 14 Apr 2022 19:37:16 +0000 (15:37 -0400)]
bcachefs: btree_update_interior.c prep for backpointers

Previously, btree_update_interior.c passed keys to bch2_trans_mark_*
that hadn't been fully initialized - they didn't have the key field
filled out, just the value.

With backpointers, we need to make sure keys are fully initialized
before marking them - because the backpointer points back to the
original key.

This patch tweaks the interior update paths to fix this.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Plumb btree_id & level to trans_mark
Kent Overstreet [Fri, 1 Apr 2022 01:44:55 +0000 (21:44 -0400)]
bcachefs: Plumb btree_id & level to trans_mark

For backpointers, we'll need the full key location - that means btree_id
and btree level. This patch plumbs it through.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Improve some fsck error messages
Kent Overstreet [Sat, 16 Apr 2022 22:59:58 +0000 (18:59 -0400)]
bcachefs: Improve some fsck error messages

We have string names for d_type; use it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Go emergency RO when i_blocks underflows
Kent Overstreet [Sat, 16 Apr 2022 20:06:59 +0000 (16:06 -0400)]
bcachefs: Go emergency RO when i_blocks underflows

This improves some of our warnings and assertions - they imply possible
filesystem inconsistencies, so they should be calling
bch2_fs_inconsistent().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Ensure sysfs show fns print a newline
Kent Overstreet [Fri, 15 Apr 2022 00:30:30 +0000 (20:30 -0400)]
bcachefs: Ensure sysfs show fns print a newline

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Kill old rebuild_replicas option
Kent Overstreet [Wed, 13 Apr 2022 00:03:19 +0000 (20:03 -0400)]
bcachefs: Kill old rebuild_replicas option

This option was useful when the replicas mechism was new and still being
debugged, but hasn't been used in ages - let's delete it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: In fsck, pass BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE when deleting dirents
Kent Overstreet [Tue, 12 Apr 2022 17:09:09 +0000 (13:09 -0400)]
bcachefs: In fsck, pass BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE when deleting dirents

A user reported an error where we hit an assertion due to deleting a key
in an internal snapshot node, when deleting a dirent that points to a
nonexisting inode.

We try to avoid doing updates to keys for internal snapshot nodes, but
upon inspection of the places where we remove dirents in fsck it appears
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE is correct for all of them: either
the target dirent doesn't exist, or it's a directory with multiple
dirents pointing to it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix for getting stuck in journal replay
Kent Overstreet [Tue, 12 Apr 2022 05:31:33 +0000 (01:31 -0400)]
bcachefs: Fix for getting stuck in journal replay

In journal replay, we weren't immediately dropping journal pins when we
start doing updates that ewern't from journal replay - leading to
journal reclaim getting stuck.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Improve error logging in fsck.c
Kent Overstreet [Tue, 12 Apr 2022 02:36:53 +0000 (22:36 -0400)]
bcachefs: Improve error logging in fsck.c

This adds error logging to a bunch of functions in fsck.c - in fsck,
reduntant error messages is probably better than not enough.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix inode_backpointer_exists()
Kent Overstreet [Tue, 12 Apr 2022 02:36:13 +0000 (22:36 -0400)]
bcachefs: Fix inode_backpointer_exists()

If the dirent an inode points to doesn't exist, we shouldn't be
returning an error - just 0/false.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Improve bch2_lru_delete() error messages
Kent Overstreet [Tue, 12 Apr 2022 00:57:01 +0000 (20:57 -0400)]
bcachefs: Improve bch2_lru_delete() error messages

When we detect a filesystem inconsistency, we should include the
relevent keys in the error message. This patch adds a parameter to pass
the key with the lru entry to bch2_lru_delete(), so that it can be
printed.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Introduce bch2_journal_keys_peek_(upto|slot)()
Kent Overstreet [Tue, 12 Apr 2022 00:28:13 +0000 (20:28 -0400)]
bcachefs: Introduce bch2_journal_keys_peek_(upto|slot)()

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>
18 months agobcachefs: Improve error message when alloc key doesn't match lru entry
Kent Overstreet [Mon, 11 Apr 2022 21:23:39 +0000 (17:23 -0400)]
bcachefs: Improve error message when alloc key doesn't match lru entry

Error messages should always print out the full key when available -
this gives us a starting point when looking through the journal to debug
what went wrong.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Ensure buckets have io_time[READ] set
Kent Overstreet [Sun, 10 Apr 2022 23:59:26 +0000 (19:59 -0400)]
bcachefs: Ensure buckets have io_time[READ] set

It's an error if a bucket is in state BCH_DATA_cached but not on the LRU
btree - i.e io_time[READ] == 0 - so, make sure it's set before adding
it.

Also, make some of the LRU code a bit clearer and more direct.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Use bch2_trans_inconsistent_on() in more places
Kent Overstreet [Sun, 10 Apr 2022 22:12:04 +0000 (18:12 -0400)]
bcachefs: Use bch2_trans_inconsistent_on() in more places

This gets us better error messages.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Improve bch2_open_buckets_to_text()
Kent Overstreet [Sun, 10 Apr 2022 22:04:24 +0000 (18:04 -0400)]
bcachefs: Improve bch2_open_buckets_to_text()

This patch updates bch2_open_buckets_to_text() to include the device and
bucket the open_bucket owns.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix CPU usage in journal read path
Kent Overstreet [Sun, 10 Apr 2022 20:26:34 +0000 (16:26 -0400)]
bcachefs: Fix CPU usage in journal read path

In journal_entry_add(), we were repeatedly scanning the journal entries
radix tree to scan for old entries that can be freed, with O(n^2)
behaviour. This patch tweaks things to remember the previous last_seq,
so we don't have to scan for entries to free from the start.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix a null ptr deref
Kent Overstreet [Sun, 10 Apr 2022 18:36:10 +0000 (14:36 -0400)]
bcachefs: Fix a null ptr deref

We start doing allocations before the GC thread is created, which means
we need to check for that to avoid a null ptr deref.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Don't trigger extra assertions in journal replay
Kent Overstreet [Sun, 10 Apr 2022 04:48:36 +0000 (00:48 -0400)]
bcachefs: Don't trigger extra assertions in journal replay

We now pass a rw argument to .key_invalid methods so they can trigger
assertions for updates but not on existing keys. We shouldn't trigger
these extra assertions in journal replay - this patch changes the
transaction commit path accordingly.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Minor device removal fixes
Kent Overstreet [Sat, 9 Apr 2022 19:15:36 +0000 (15:15 -0400)]
bcachefs: Minor device removal fixes

 - We weren't clearing the LRU btree
 - bch2_alloc_read() runs before bch2_check_alloc_key() deletes alloc
   keys for devices/buckets that don't exists, so it needs to check for
   that
 - bch2_check_lrus() needs to check that buckets exists
 - improve some error messages

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix a few warnings on 32 bit
Kent Overstreet [Sun, 10 Apr 2022 00:29:26 +0000 (20:29 -0400)]
bcachefs: Fix a few warnings on 32 bit

These showed up when building for mips.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: bch2_btree_delete_extent_at()
Kent Overstreet [Sat, 9 Apr 2022 19:07:11 +0000 (15:07 -0400)]
bcachefs: bch2_btree_delete_extent_at()

New helper, for deleting extents.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Don't skip triggers in fcollapse()
Kent Overstreet [Sat, 9 Apr 2022 03:54:14 +0000 (23:54 -0400)]
bcachefs: Don't skip triggers in fcollapse()

With backpointers this doesn't work anymore - backpointers always need
to be updated to point to the new extent position.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Initialize ec work structs early
Kent Overstreet [Sat, 9 Apr 2022 05:23:50 +0000 (01:23 -0400)]
bcachefs: Initialize ec work structs early

We need to ensure that work structs in bch_fs always get initialized -
otherwise an error in filesystem initialization can pop a warning in the
workqueue code when we try to cancel a work struct that wasn't
initialized.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Use a genradix for reading journal entries
Kent Overstreet [Mon, 21 Mar 2022 04:15:53 +0000 (00:15 -0400)]
bcachefs: Use a genradix for reading journal entries

Previously, the journal read path used a linked list for storing the
journal entries we read from disk. But there's been a bug that's been
causing journal_flush_delay to incorrectly be set to 0, leading to far
more journal entries than is normal being written out, which then means
filesystems are no longer able to start due to the O(n^2) behaviour of
inserting into/searching that linked list.

Fix this by switching to a radix tree.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Refactor journal_keys_sort() to return an error code
Kent Overstreet [Mon, 21 Mar 2022 04:15:53 +0000 (00:15 -0400)]
bcachefs: Refactor journal_keys_sort() to return an error code

When there weren't any keys in the journal there's no need to allocate
the buffer - but doing that causes a spurious -ENOMEM.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fold bucket_state in to BCH_DATA_TYPES()
Kent Overstreet [Fri, 1 Apr 2022 05:29:59 +0000 (01:29 -0400)]
bcachefs: Fold bucket_state in to BCH_DATA_TYPES()

Previously, we were missing accounting for buckets in need_gc_gens and
need_discard states. This matters because buckets in those states need
other btree operations done before they can be used, so they can't be
conuted when checking current number of free buckets against the
allocation watermark.

Also, we weren't directly counting free buckets at all. Now, data type 0
== BCH_DATA_free, and free buckets are counted; this means we can get
rid of the separate (poorly defined) count of unavailable buckets.

This is a new on disk format version, with upgrade and fsck required for
the accounting changes.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Add a sysfs attr for triggering discards
Kent Overstreet [Thu, 7 Apr 2022 22:38:16 +0000 (18:38 -0400)]
bcachefs: Add a sysfs attr for triggering discards

We're currently debugging an issue with discards not getting run; this
patch adds a manual trigger so we can then watch the tracepoint while it
runs.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Topology repair fixes
Kent Overstreet [Thu, 7 Apr 2022 21:41:02 +0000 (17:41 -0400)]
bcachefs: Topology repair fixes

 - We were failing to start topology repair, because we hadn't set the
   superblock flag indicating it needed to run
 - set_node_min() forget to update the btree node's key
 - bch2_gc_alloc_reset() didn't reset data type, leading to inserting an
   invalid key that was empty but had nonzero data type

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Use bch2_trans_inconsistent() more
Kent Overstreet [Thu, 7 Apr 2022 21:34:57 +0000 (17:34 -0400)]
bcachefs: Use bch2_trans_inconsistent() more

This gets us better error messages.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Move alloc assertion to .key_invalid()
Kent Overstreet [Thu, 7 Apr 2022 21:32:57 +0000 (17:32 -0400)]
bcachefs: Move alloc assertion to .key_invalid()

.key_invalid is a better place for this assertion.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Improve btree_bad_header()
Kent Overstreet [Thu, 7 Apr 2022 21:28:09 +0000 (17:28 -0400)]
bcachefs: Improve btree_bad_header()

In the future printbufs will be mempool-ified, so we shouldn't be using
more than one at a time if we don't have to.

This also fixes an extra trailing newline.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Check for read_time == 0 in bch2_alloc_v4_invalid()
Kent Overstreet [Wed, 6 Apr 2022 21:22:47 +0000 (17:22 -0400)]
bcachefs: Check for read_time == 0 in bch2_alloc_v4_invalid()

We've been seeing this error in fsck and we weren't able to track down
where it came from - but now that .key_invalid methods take a rw
argument, we can safely check for this.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: fsck: Work around transaction restarts
Kent Overstreet [Wed, 6 Apr 2022 18:35:10 +0000 (14:35 -0400)]
bcachefs: fsck: Work around transaction restarts

In check_extents() and check_dirents(), we're working towards only
handling transaction restarts in one place, at the top level - but we're
not there yet. check_i_sectors() and check_subdir_count() handle
transaction restarts locally, which means the iterator for the
dirent/extent is left unlocked (should_be_locked == 0), leading to
asserts popping when we go to do updates.

This patch hacks around this for now, until we can delete the offending
code.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Add rw to .key_invalid()
Kent Overstreet [Mon, 4 Apr 2022 01:50:25 +0000 (21:50 -0400)]
bcachefs: Add rw to .key_invalid()

This adds a new parameter to .key_invalid() methods for whether the key
is being read or written; the idea being that methods can do more
aggressive checks when a key is newly created and being written, when we
wouldn't want to delete the key because of those checks.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: More improvements for alloc info checks
Kent Overstreet [Tue, 5 Apr 2022 17:44:18 +0000 (13:44 -0400)]
bcachefs: More improvements for alloc info checks

 - Move checks for whether the device & bucket are valid from the
   .key_invalid method to bch2_check_alloc_key(). This is because
   .key_invalid() is called on keys that may no longer exist (post
   journal replay), which is a problem when removing/resizing devices.

 - We weren't checking the need_discard btree to ensure that every set
   bucket has a corresponding alloc key. This refactors the code for
   checking the freespace btree, so that it now checks both.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Silence spurious copygc err when shutting down
Kent Overstreet [Tue, 5 Apr 2022 03:36:56 +0000 (23:36 -0400)]
bcachefs: Silence spurious copygc err when shutting down

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Convert .key_invalid methods to printbufs
Kent Overstreet [Sun, 3 Apr 2022 21:50:01 +0000 (17:50 -0400)]
bcachefs: Convert .key_invalid methods to printbufs

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Gap buffer for journal keys
Kent Overstreet [Mon, 4 Apr 2022 05:09:26 +0000 (01:09 -0400)]
bcachefs: Gap buffer for journal keys

Btree updates before we go RW work by inserting into the array of keys
that journal replay will insert - but inserting into a flat array is
O(n), meaning if btree_gc needs to update many alloc keys, we're O(n^2).

Fortunately, the updates btree_gc does happens in sequential order,
which means a gap buffer works nicely here - this patch implements a gap
buffer for journal keys.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Don't normalize to pages in btree cache shrinker
Kent Overstreet [Mon, 4 Apr 2022 00:36:32 +0000 (20:36 -0400)]
bcachefs: Don't normalize to pages in btree cache shrinker

This behavior dates from the early, early days of bcache, and upon
further delving appears to not make any sense. The shrinker only works
in terms of 'objects' of unknown size; normalizing to pages only had the
effect of changing the batch size, which we could do directly - if we
wanted; we probably don't. Normalizing to pages meant our batch size was
very small, which seems to have been keeping us from doing as much
shrinking as we should be under heavy memory pressure; this patch
appears to alleviate some OOMs we've been seeing.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Add a tracepoint for superblock writes
Kent Overstreet [Sun, 3 Apr 2022 19:13:20 +0000 (15:13 -0400)]
bcachefs: Add a tracepoint for superblock writes

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: gc mark fn fixes, cleanups
Kent Overstreet [Sat, 2 Apr 2022 22:00:04 +0000 (18:00 -0400)]
bcachefs: gc mark fn fixes, cleanups

mark_stripe_bucket() was busted; it was using @new unitialized.

Also, clean up all the gc mark functions, and convert them to the same
style.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Don't write partially-initialized superblocks
Kent Overstreet [Sat, 2 Apr 2022 21:24:25 +0000 (17:24 -0400)]
bcachefs: Don't write partially-initialized superblocks

This neatly avoids bugs where we fail partway through initializing a new
filesystem, if we just don't write out partly-initialized state.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Improve read_from_stale_dirty_pointer() message
Kent Overstreet [Sat, 2 Apr 2022 20:57:29 +0000 (16:57 -0400)]
bcachefs: Improve read_from_stale_dirty_pointer() message

With printbufs, it's now easy to build up multi-line log messages and
emit them with one call, which is good because it prevents multiple
multi-line log messages from getting Interspersed in the log buffer;
this patch also improves the formatting and converts it to latest style.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Use crc_is_compressed()
Kent Overstreet [Fri, 1 Apr 2022 02:05:33 +0000 (22:05 -0400)]
bcachefs: Use crc_is_compressed()

Trivial cleanup.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix pr_buf() calls
Kent Overstreet [Sat, 2 Apr 2022 20:30:37 +0000 (16:30 -0400)]
bcachefs: Fix pr_buf() calls

In a few places we were passing a variable to pr_buf() for the format
string - oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Kill struct bucket_mark
Kent Overstreet [Mon, 14 Feb 2022 05:07:38 +0000 (00:07 -0500)]
bcachefs: Kill struct bucket_mark

This switches struct bucket to using a lock, instead of cmpxchg. And now
that the protected members no longer need to fit into a u64, we can
expand the sector counts to 32 bits.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill main in-memory bucket array
Kent Overstreet [Fri, 11 Feb 2022 00:26:55 +0000 (19:26 -0500)]
bcachefs: Kill main in-memory bucket array

All code using the in-memory bucket array, excluding GC, has now been
converted to use the alloc btree directly - so we can finally delete it.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_dev_usage_update() no longer depends on bucket_mark
Kent Overstreet [Fri, 11 Feb 2022 00:09:40 +0000 (19:09 -0500)]
bcachefs: bch2_dev_usage_update() no longer depends on bucket_mark

This is one of the last steps in getting rid of the main in-memory
bucket array.

This changes bch2_dev_usage_update() to take bkey_alloc_unpacked instead
of bucket_mark, and for the places where we are in fact working with
bucket_mark and don't have bkey_alloc_unpacked, we add a wrapper that
takes bucket_mark and converts to bkey_alloc_unpacked.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fsck for need_discard & freespace btrees
Kent Overstreet [Thu, 17 Feb 2022 08:11:39 +0000 (03:11 -0500)]
bcachefs: Fsck for need_discard & freespace btrees

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: New bucket invalidate path
Kent Overstreet [Thu, 10 Feb 2022 23:18:41 +0000 (18:18 -0500)]
bcachefs: New bucket invalidate path

In the old allocator code, preparing an existing empty bucket was part
of the same code path that invalidated buckets containing cached data.
In the new allocator code this is no longer the case: the main allocator
path finds empty buckets (via the new freespace btree), and can't
allocate buckets that contain cached data.

We now need a separate code path to invalidate buckets containing cached
data when we're low on empty buckets, which this patch implements. When
the number of free buckets decreases that triggers the new invalidate
path to run, which uses the LRU btree to pick cached data buckets to
invalidate until we're above our watermark.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: New discard implementation
Kent Overstreet [Thu, 10 Feb 2022 09:32:19 +0000 (04:32 -0500)]
bcachefs: New discard implementation

In the old allocator code, buckets would be discarded just prior to
being used - this made sense in bcache where we were discarding buckets
just after invalidating the cached data they contain, but in a
filesystem where we typically have more free space we want to be
discarding buckets when they become empty.

This patch implements the new behaviour - it checks the need_discard
btree for buckets awaiting discards, and then clears the appropriate
bit in the alloc btree, which moves the buckets to the freespace btree.

Additionally, discards are now enabled by default.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill allocator threads & freelists
Kent Overstreet [Mon, 10 Jan 2022 01:48:31 +0000 (20:48 -0500)]
bcachefs: Kill allocator threads & freelists

Now that we have new persistent data structures for the allocator, this
patch converts the allocator to use them.

Now, foreground bucket allocation uses the freespace btree to find
buckets to allocate, instead of popping buckets off the freelist.

The background allocator threads are no longer needed and are deleted,
as well as the allocator freelists. Now we only need background tasks
for invalidating buckets containing cached data (when we are low on
empty buckets), and for issuing discards.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Freespace, need_discard btrees
Kent Overstreet [Sat, 11 Dec 2021 22:13:09 +0000 (17:13 -0500)]
bcachefs: Freespace, need_discard btrees

This adds two new btrees for the upcoming allocator rewrite: an extents
btree of free buckets, and a btree for buckets awaiting discards.

We also add a new trigger for alloc keys to keep the new btrees up to
date, and a compatibility path to initialize them on existing
filesystems.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: KEY_TYPE_alloc_v4
Kent Overstreet [Sat, 1 Jan 2022 01:03:29 +0000 (20:03 -0500)]
bcachefs: KEY_TYPE_alloc_v4

This introduces a new alloc key which doesn't use varints. Soon we'll be
adding backpointers and storing them in alloc keys, which means our
pack/unpack workflow for alloc keys won't really work - we'll need to be
mutating alloc keys in place.

Instead of bch2_alloc_unpack(), we now have bch2_alloc_to_v4() that
converts older types of alloc keys to v4 if needed.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: LRU btree
Kent Overstreet [Sun, 5 Dec 2021 05:31:54 +0000 (00:31 -0500)]
bcachefs: LRU btree

This implements new persistent LRUs, to be used for buckets containing
cached data, as well as stripes ordered by time when a block became
empty.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: KEY_TYPE_set
Kent Overstreet [Thu, 6 Jan 2022 03:13:13 +0000 (22:13 -0500)]
bcachefs: KEY_TYPE_set

A new empty key type, to be used when using a btree as a set.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: bch_sb_field_journal_v2
Kent Overstreet [Thu, 10 Mar 2022 21:43:52 +0000 (16:43 -0500)]
bcachefs: bch_sb_field_journal_v2

Add a new superblock field which represents journal buckets as ranges:
also move code for the superblock journal fields to journal_sb.c.

This also reworks the code for resizing the journal to write the new
superblock before using the new journal buckets, and thus be a bit
safer.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Run btree updates after write out of write_point
Kent Overstreet [Mon, 31 Oct 2022 20:13:05 +0000 (16:13 -0400)]
bcachefs: Run btree updates after write out of write_point

In the write path, after the write to the block device(s) complete we
have to punt to process context to do the btree update.

Instead of using the work item embedded in op->cl, this patch switches
to a per write-point work item. This helps with two different issues:

 - lock contention: btree updates to the same writepoint will (usually)
   be updating the same alloc keys
 - context switch overhead: when we're bottlenecked on btree updates,
   having a thread (running out of a work item) checking the write point
   for completed ops is cheaper than queueing up a new work item and
   waking up a kworker.

In an arbitrary benchmark, 4k random writes with fio running inside a
VM, this patch resulted in a 10% improvement in total iops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_btree_update_start() refactoring
Kent Overstreet [Wed, 12 Jan 2022 04:24:43 +0000 (23:24 -0500)]
bcachefs: bch2_btree_update_start() refactoring

This simplifies the logic in bch2_btree_update_start() a bit, handling
the unlock/block logic more locally.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Introduce a separate journal watermark for copygc
Kent Overstreet [Tue, 15 Mar 2022 01:48:42 +0000 (21:48 -0400)]
bcachefs: Introduce a separate journal watermark for copygc

Since journal reclaim -> btree key cache flushing may require the
allocation of new btree nodes, it has an implicit dependency on copygc
in order to make forward progress - so we should avoid blocking copygc
unless the journal is really close to full.

This introduces watermarks to replace our single MAY_GET_UNRESERVED bit
in the journal, and adds a watermark for copygc and plumbs it through.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Copygc allocations shouldn't be nowait
Kent Overstreet [Tue, 15 Mar 2022 20:40:55 +0000 (16:40 -0400)]
bcachefs: Copygc allocations shouldn't be nowait

We don't actually want copygc allocations to be nowait - an allocation
for copygc might fail and then later succeed due to a bucket needing to
wait on journal commit, or to be discarded.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix bch2_journal_pin_set()
Kent Overstreet [Thu, 5 Jan 2023 15:13:37 +0000 (10:13 -0500)]
bcachefs: Fix bch2_journal_pin_set()

When bch2_journal_pin_set() is updating an existing pin, we shouldn't
call bch2_journal_reclaim_fast() after dropping the old pin and before
dropping the new pin - that could reclaim the entry we're trying to pin.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: x-macroize alloc_reserve enum
Kent Overstreet [Sun, 13 Mar 2022 23:27:55 +0000 (19:27 -0400)]
bcachefs: x-macroize alloc_reserve enum

This makes an array of strings available, like our other enums.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>