linux-2.6-microblaze.git
11 months agobcachefs: respect superblock discard flag.
Daniel Hill [Sat, 5 Mar 2022 04:45:27 +0000 (17:45 +1300)]
bcachefs: respect superblock discard flag.

We were accidentally using default mount options and overwriting the
discard flag.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix usage of six lock's percpu mode
Kent Overstreet [Sat, 5 Mar 2022 00:16:04 +0000 (19:16 -0500)]
bcachefs: Fix usage of six lock's percpu mode

Six locks have a percpu mode, which we use for interior btree nodes, as
well as btree key cache keys for the subvolumes btree. We've been
switching locks back and forth between percpu and non percpu mode as
needed, but it turns out this is racy - when we're reusing an existing
node, other threads could be attempting to lock it while we're switching
it between modes.

This patch fixes this by never switching 'struct btree' between the two
modes, and instead segragating them between two different freed lists.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Refactor bch2_btree_node_mem_alloc()
Kent Overstreet [Sat, 5 Mar 2022 00:50:28 +0000 (19:50 -0500)]
bcachefs: Refactor bch2_btree_node_mem_alloc()

This is prep work for the next patch, which is going to fix our usage of
the percpu mode of six locks by never switching struct btree between the
two modes - which means we need separate freed lists.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Simplify parameters to bch2_btree_update_start()
Kent Overstreet [Sat, 5 Mar 2022 00:15:46 +0000 (19:15 -0500)]
bcachefs: Simplify parameters to bch2_btree_update_start()

We don't need to pass the number of nodes required to
bch2_btree_update_start, just whether we're doing a split at @level.
This is prep work for a fix to our usage of six lock's percpu mode,
which is going to require us to count up and allocate interior nodes and
leaf nodes seperately.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Make bch2_btree_cache_scan() try harder
Kent Overstreet [Thu, 3 Mar 2022 16:04:01 +0000 (11:04 -0500)]
bcachefs: Make bch2_btree_cache_scan() try harder

Previously, when bch2_btree_cache_scan() attempted to reclaim a node but
failed (because trylock failed, because it was dirty, etc.), it would
count that against the number of nodes it was scanning and attempting to
free. This patch changes that behaviour, so that now we only count nodes
that we then don't free if they have the accessed bit (which we also
clear).

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Finish writing journal after journal error
Kent Overstreet [Mon, 28 Feb 2022 23:48:33 +0000 (18:48 -0500)]
bcachefs: Finish writing journal after journal error

After emergency shutdown, all journal entries will be written as noflush
entries, meaning they will never be used - but they'll still exist for
debugging tools to examine.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: __journal_entry_close() never fails
Kent Overstreet [Tue, 1 Mar 2022 00:29:19 +0000 (19:29 -0500)]
bcachefs: __journal_entry_close() never fails

Previous patch just moved responsibility for incrementing the journal
sequence number and initializing the new journal entry from
__journal_entry_close() to journal_entry_open(); this patch makes the
analagous change for journal reservation state, incrementing the index
into array of journal_bufs at open time.

This means that __journal_entry_close() never fails to close an open
journal entry, which is important for the next patch that will change
our emergency shutdown behaviour.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Refactor journal code to not use unwritten_idx
Kent Overstreet [Tue, 1 Mar 2022 00:17:27 +0000 (19:17 -0500)]
bcachefs: Refactor journal code to not use unwritten_idx

It makes the code more readable if we work off of sequence numbers,
instead of direct indexes into the array of journal buffers.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Journal seq now incremented at entry open, not close
Kent Overstreet [Mon, 28 Feb 2022 21:35:42 +0000 (16:35 -0500)]
bcachefs: Journal seq now incremented at entry open, not close

This patch changes journal_entry_open() to initialize the new journal
entry, not __journal_entry_close().

This also means that journal_cur_seq() refers to the sequence number of
the last journal entry when we don't have an open journal entry, not the
next one.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Drop unneeded journal pin in bch2_btree_update_start()
Kent Overstreet [Tue, 1 Mar 2022 20:31:20 +0000 (15:31 -0500)]
bcachefs: Drop unneeded journal pin in bch2_btree_update_start()

When we do an interior btree update, we create new btree nodes and link
them into the btree in memory, but they don't become reachable on disk
until later, when btree_update_nodes_written_trans() runs.

Updates to the new nodes can thus happen before they're reachable on
disk, and if the updates to those new nodes are written before the nodes
become reachable, we would then drop the journal pin for those updates
before the btree has them.

This is what the journal pin in bch2_btree_update_start() was protecting
against. However, it's not actually needed because we don't allow
subsequent append writes to btree nodes until the node is reachable on
disk.

Dropping this unneeded pin also fixes a bug introduced by "bcachefs:
Journal seq now incremented at entry open, not close" - in the new code,
if the journal is completely empty a journal pin list for
journal_cur_seq() won't exist.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: bch2_journal_halt() now takes journal lock
Kent Overstreet [Mon, 28 Feb 2022 21:21:07 +0000 (16:21 -0500)]
bcachefs: bch2_journal_halt() now takes journal lock

This change is prep work for moving some work from
__journal_entry_close() to journal_entry_open(): without this change,
journal_entry_open() doesn't know if it's going to be able to open a new
journal entry until the cmpxchg loop, meaning it can't create the new
journal pin entry and update other global state because those have to be
done prior to the cmpxchg opening the new journal entry.

Fortunately, we don't call bch2_journal_halt() from interrupt context.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Kill JOURNAL_NEED_WRITE
Kent Overstreet [Fri, 25 Feb 2022 15:28:20 +0000 (10:28 -0500)]
bcachefs: Kill JOURNAL_NEED_WRITE

This replaces the journal flag JOURNAL_NEED_WRITE with per-journal buf
state - more explicit, and solving a race in the old code that would
lead to entries being opened and written unnecessarily.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Delete some dead journal code
Kent Overstreet [Mon, 28 Feb 2022 20:51:24 +0000 (15:51 -0500)]
bcachefs: Delete some dead journal code

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix a use after free
Kent Overstreet [Sun, 27 Feb 2022 16:34:21 +0000 (11:34 -0500)]
bcachefs: Fix a use after free

This fixes a regression from "bcachefs: Stash a copy of key being
overwritten in btree_insert_entry". In btree_key_can_insert_cached(), we
may reallocate the key cache key, invalidating pointers previously
returned by peek() - fix it by issuing a transaction restart.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix a memory leak
Kent Overstreet [Sun, 27 Feb 2022 16:57:42 +0000 (11:57 -0500)]
bcachefs: Fix a memory leak

This fixes a regression from "bcachefs: Heap allocate printbufs" -
bch2_sb_field_validate() was leaking an error string.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix race leading to btree node write getting stuck
Kent Overstreet [Sun, 27 Feb 2022 14:56:33 +0000 (09:56 -0500)]
bcachefs: Fix race leading to btree node write getting stuck

Checking btree_node_may_write() isn't atomic with the other btree flags,
dirty and need_write in particular. There was a rare race where we'd
unblock a node from writing while __btree_node_flush() was setting
need_write, and no thread would notice that the node was now both able
to write and needed to be written.

Fix this by adding btree node flags for will_make_reachable and
write_blocked that can be checked in the cmpxchg loop in
__bch2_btree_node_write.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Kill bch2_btree_node_write_cond()
Kent Overstreet [Sun, 27 Feb 2022 14:42:46 +0000 (09:42 -0500)]
bcachefs: Kill bch2_btree_node_write_cond()

bch2_btree_node_write_cond() was only used in one place - this inlines
it into __btree_node_flush() and makes the cmpxchg loop actually
correct.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve btree_node_write_if_need()
Kent Overstreet [Sun, 27 Feb 2022 02:46:41 +0000 (21:46 -0500)]
bcachefs: Improve btree_node_write_if_need()

btree_node_write_if_need() kicks off a btree node write only if
need_write is set; this makes the locking easier to reason about by
moving the check into the cmpxchg loop in __bch2_btree_node_write().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix locking in btree_node_write_done()
Kent Overstreet [Sun, 27 Feb 2022 02:35:16 +0000 (21:35 -0500)]
bcachefs: Fix locking in btree_node_write_done()

There was a rare recursive locking bug, in __bch2_btree_node_write()
nowrite path -> btree_node_write_done(), in the path that kicks off
another write.

This splits out an inner __btree_node_write_done() that expects to be
run with the btree node lock held.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Start moving debug info from sysfs to debugfs
Kent Overstreet [Sat, 26 Feb 2022 16:48:34 +0000 (11:48 -0500)]
bcachefs: Start moving debug info from sysfs to debugfs

In sysfs, files can only output at most PAGE_SIZE. This is a problem for
debug info that needs to list an arbitrary number of times, and because
of this limit some of our debug info has been terser and harder to read
than we'd like.

This patch moves info about journal pins and cached btree nodes to
debugfs, and greatly expands and improves the output we return.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve struct journal layout
Kent Overstreet [Wed, 28 Dec 2022 22:13:08 +0000 (17:13 -0500)]
bcachefs: Improve struct journal layout

This cacheline aligns struct journal, and puts j->reservations and
j->prereserved on their own cacheline - we may want to split them up in
a separate patch.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Use x-macros for btree node flags
Kent Overstreet [Sat, 26 Feb 2022 16:10:20 +0000 (11:10 -0500)]
bcachefs: Use x-macros for btree node flags

This is for adding an array of strings for btree node flag names.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Kill BCH_FS_HOLD_BTREE_WRITES
Kent Overstreet [Sun, 27 Feb 2022 01:25:15 +0000 (20:25 -0500)]
bcachefs: Kill BCH_FS_HOLD_BTREE_WRITES

This was just dead code.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Don't spin in journal reclaim
Kent Overstreet [Sat, 26 Feb 2022 03:45:58 +0000 (22:45 -0500)]
bcachefs: Don't spin in journal reclaim

If we're not able to flush anything, we shouldn't keep looping.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix btree path sorting
Kent Overstreet [Sat, 26 Feb 2022 03:33:01 +0000 (22:33 -0500)]
bcachefs: Fix btree path sorting

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>
11 months agobcachefs: Fix journal_flush_done()
Kent Overstreet [Sat, 26 Feb 2022 03:14:35 +0000 (22:14 -0500)]
bcachefs: Fix journal_flush_done()

journal_flush_done() was overwriting did_work, thus occasionally
returning false when it did do work and occasional assertions in the
shutdown sequence because we didn't completely flush the key cache.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Heap allocate printbufs
Kent Overstreet [Fri, 25 Feb 2022 18:18:19 +0000 (13:18 -0500)]
bcachefs: Heap allocate printbufs

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@linux.dev>
11 months agobcachefs: Convert bch2_pd_controller_print_debug() to a printbuf
Kent Overstreet [Fri, 25 Feb 2022 18:17:48 +0000 (13:17 -0500)]
bcachefs: Convert bch2_pd_controller_print_debug() to a printbuf

Fewer random on-stack char arrays.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve debug assertion
Kent Overstreet [Fri, 25 Feb 2022 00:04:11 +0000 (19:04 -0500)]
bcachefs: Improve debug assertion

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>
11 months agobcachefs: Fix bch2_journal_pins_to_text()
Kent Overstreet [Thu, 24 Feb 2022 23:19:32 +0000 (18:19 -0500)]
bcachefs: Fix bch2_journal_pins_to_text()

When key cache pins were put onto their own list, we neglected to update
bch2_journal_pins_to_text() to print them.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Always clear should_be_locked in bch2_trans_begin()
Kent Overstreet [Thu, 24 Feb 2022 18:27:31 +0000 (13:27 -0500)]
bcachefs: Always clear should_be_locked in bch2_trans_begin()

bch2_trans_begin() invalidates all iterators, until they're revalidated
by calling peek() or traverse().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Run alloc triggers last
Kent Overstreet [Thu, 6 Jan 2022 06:20:41 +0000 (01:20 -0500)]
bcachefs: Run alloc triggers last

Triggers can generate additional btree updates - we need to run alloc
triggers after all other triggers have run, because they generate
updates for the alloc btree.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Trigger code uses stashed copy of old key
Kent Overstreet [Thu, 24 Feb 2022 16:02:58 +0000 (11:02 -0500)]
bcachefs: Trigger code uses stashed copy of old key

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Consolidate trigger code a bit
Kent Overstreet [Thu, 24 Feb 2022 16:30:17 +0000 (11:30 -0500)]
bcachefs: Consolidate trigger code a bit

Upcoming patches are doing more work on the triggers code, this patch
just moves code around.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: bch2_trans_mark_key() now takes a bkey_i *
Kent Overstreet [Fri, 10 Dec 2021 22:04:26 +0000 (17:04 -0500)]
bcachefs: bch2_trans_mark_key() now takes a bkey_i *

We're now coming up with triggers that modify the update being done. A
bkey_s_c is const - bkey_i is the correct type to be using here.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix 32 bit build
Kent Overstreet [Wed, 23 Feb 2022 16:46:34 +0000 (11:46 -0500)]
bcachefs: Fix 32 bit build

vstruct_bytes() was returning a u64 - it should be a size_t, the corect
type for the size of anything that fits in memory.

Also replace a 64 bit divide with div_u64().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve some btree node read error messages
Kent Overstreet [Wed, 23 Feb 2022 15:32:43 +0000 (10:32 -0500)]
bcachefs: Improve some btree node read error messages

On btree node read error, it's helpful to see what we were trying to
read - was it all zeroes?

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Use unlikely() in err_on() macros
Kent Overstreet [Sat, 19 Feb 2022 08:56:44 +0000 (03:56 -0500)]
bcachefs: Use unlikely() in err_on() macros

Should be obviously a good thing.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve reflink repair code
Kent Overstreet [Sat, 19 Feb 2022 08:06:28 +0000 (03:06 -0500)]
bcachefs: Improve reflink repair code

When a reflink pointer points to a missing indirect extent, we replace
it with an error key. Instead of replacing the entire reflink pointer
with an error key, this patch replaces only the missing range with an
error key.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Normal update/commit path now works before going RW
Kent Overstreet [Sat, 19 Feb 2022 07:48:27 +0000 (02:48 -0500)]
bcachefs: Normal update/commit path now works before going RW

This improves __bch2_trans_commit - early in the recovery process, when
we're running btree_gc and before we want to go RW, it now uses
bch2_journal_key_insert() to add the update to the list of updates for
journal replay to do, instead of btree_gc having to use separate
interfaces depending on whether we're running at bringup or, later,
runtime.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Revert "Ensure journal doesn't get stuck in nochanges mode"
Kent Overstreet [Wed, 23 Feb 2022 11:56:35 +0000 (06:56 -0500)]
bcachefs: Revert "Ensure journal doesn't get stuck in nochanges mode"

This patch was originally to work around the journal geting stuck in
nochanges mode - but that was just a hack, we needed to fix the actual
bug. It should be fixed now, so revert it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix for journal getting stuck
Kent Overstreet [Wed, 23 Feb 2022 15:26:10 +0000 (10:26 -0500)]
bcachefs: Fix for journal getting stuck

The journal can get stuck if we need to get a journal reservation for
something we have a pre-reservation for, but aren't able to reclaim
space, or if the pin fifo is full - it's impractical to resize the pin
fifo at runtime.

Previously, we reserved 8 entries in the pin fifo for pre-reservations,
but that seems small - we're seeing the journal occasionally get stuck.
Let's reserve a quarter of it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Set BTREE_NODE_SEQ() correctly in merge path
Kent Overstreet [Tue, 22 Feb 2022 22:16:45 +0000 (17:16 -0500)]
bcachefs: Set BTREE_NODE_SEQ() correctly in merge path

BTREE_NODE_SEQ() is supposed to give us a time ordering of btree nodes
on disk, so that we can tell which btree node is newer if we ever have
to scan the entire device to find btree nodes.

The btree node merge path wasn't setting it correctly on the new node -
oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Drop journal_write_compact()
Kent Overstreet [Wed, 23 Feb 2022 12:00:34 +0000 (07:00 -0500)]
bcachefs: Drop journal_write_compact()

Long ago it was possible to get a journal reservation and not use it,
but that's no longer allowed, which means journal_write_compact() has
very little work to do, and isn't really worth the code anymore.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Btree key cache optimization
Kent Overstreet [Wed, 4 Jan 2023 09:34:16 +0000 (04:34 -0500)]
bcachefs: Btree key cache optimization

This helps with lock contention in the journalling code: instead of
updating our journal pin on every write, only get a journal pin if we
don't have one.

This means we can avoid hammering on journal locks nearly so much, at
the cost of carrying around a journal pin for an older entry than the
one we actually need. To handle that, if needed we update our journal
pin to the correct one when flushed by journal reclaim.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Add tabstops to printbufs
Kent Overstreet [Tue, 22 Feb 2022 09:53:48 +0000 (04:53 -0500)]
bcachefs: Add tabstops to printbufs

Now, when outputting to printbufs, we can set tabstops and left or right
justify text to them - this is to be used by the userspace 'bcachefs fs
usage' command.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix a use after free
Kent Overstreet [Mon, 21 Feb 2022 18:22:11 +0000 (13:22 -0500)]
bcachefs: Fix a use after free

In move_read_endio, we were checking if the next pending write has its
read completed - but this can turn after a use after free (and we were
accessing the list without a lock), so instead just better to just
unconditionally do the wakeup.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Add .to_text() methods for all superblock sections
Kent Overstreet [Sun, 20 Feb 2022 10:00:45 +0000 (05:00 -0500)]
bcachefs: Add .to_text() methods for all superblock sections

This patch improves the superblock .to_text() methods and adds methods
for all types that were missing them. It also improves printbufs by
allowing them to specfiy what units we want to be printing in, and adds
new wrapper methods for unifying our kernel and userspace environments.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Kill bch_scnmemcpy()
Kent Overstreet [Sun, 20 Feb 2022 09:52:44 +0000 (04:52 -0500)]
bcachefs: Kill bch_scnmemcpy()

bch_scnmemcpy was for printing length-limited strings that might not
have a terminating null - turns out sprintf & pr_buf can do this with
%.*s.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Don't issue discards when in nochanges mode
Kent Overstreet [Mon, 21 Feb 2022 10:05:29 +0000 (05:05 -0500)]
bcachefs: Don't issue discards when in nochanges mode

When the nochanges option is selected, we're supposed to never issue
writes. Unfortunately, it seems discards were missed when implemnting
this, leading to some painful filesystem corruption.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: opts.read_journal_only
Kent Overstreet [Sat, 19 Feb 2022 10:15:53 +0000 (05:15 -0500)]
bcachefs: opts.read_journal_only

Add an option that tells recovery to only read the journal, to be used
by the list_journal command.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Change __bch2_trans_commit() to run triggers then get RW
Kent Overstreet [Sat, 19 Feb 2022 07:40:45 +0000 (02:40 -0500)]
bcachefs: Change __bch2_trans_commit() to run triggers then get RW

This is prep work for the next patch, which is going to change
__bch2_trans_commit() to use bch2_journal_key_insert() when very early
in the recovery process, so that we have a unified interface for doing
btree updates.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Delete some flag bits that are no longer used
Kent Overstreet [Sat, 19 Feb 2022 07:39:56 +0000 (02:39 -0500)]
bcachefs: Delete some flag bits that are no longer used

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Store logical location of journal entries
Kent Overstreet [Sat, 19 Feb 2022 06:18:18 +0000 (01:18 -0500)]
bcachefs: Store logical location of journal entries

When viewing what's in the journal, it's more useful to have the logical
location - journal bucket and offset within that bucket - than just the
offset on that device.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Check for errors from crypto_skcipher_encrypt()
Kent Overstreet [Sat, 19 Feb 2022 05:42:12 +0000 (00:42 -0500)]
bcachefs: Check for errors from crypto_skcipher_encrypt()

Apparently it actually is possible for crypto_skcipher_encrypt() to
return an error - not sure why that would be - but we need to replace
our assertion with actual error handling.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix failure to allocate btree node in cache
Kent Overstreet [Fri, 18 Feb 2022 05:47:45 +0000 (00:47 -0500)]
bcachefs: Fix failure to allocate btree node in cache

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>
11 months agobcachefs: Change bch2_dev_lookup() to not use lookup_bdev()
Kent Overstreet [Wed, 16 Feb 2022 11:23:06 +0000 (06:23 -0500)]
bcachefs: Change bch2_dev_lookup() to not use lookup_bdev()

bch2_dev_lookup() is used from the extended attribute set methods, for
setting the target options, where we're already holding an inode lock -
it turns out pathname lookups also take inode locks, so that was
susceptible to deadlocks.

Fortunately we already stash the device name in ca->name. This does
change user-visible behaviour though: instead of specifying e.g.
/dev/sda1, user must now specify sda1.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Only allocate buckets_nouse when requested
Kent Overstreet [Mon, 14 Feb 2022 06:42:31 +0000 (01:42 -0500)]
bcachefs: Only allocate buckets_nouse when requested

It's only needed by the migrate tool - this patch adds an option to
enable allocating it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Stale ptr cleanup is now done by gc_gens
Kent Overstreet [Sun, 13 Feb 2022 06:58:12 +0000 (01:58 -0500)]
bcachefs: Stale ptr cleanup is now done by gc_gens

Before we had dedicated gc code for bucket->oldest_gen this was
btree_gc's responsibility, but now that we have that we can rip it out,
simplifying the already overcomplicated btree_gc.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve journal_entry_btree_keys_to_text()
Kent Overstreet [Wed, 16 Feb 2022 07:50:39 +0000 (02:50 -0500)]
bcachefs: Improve journal_entry_btree_keys_to_text()

This improves the formatting of journal_entry_btree_keys_to_text() by
putting each key on its own line.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix __btree_path_traverse_all
Kent Overstreet [Wed, 16 Feb 2022 08:13:36 +0000 (03:13 -0500)]
bcachefs: Fix __btree_path_traverse_all

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>
11 months agobcachefs: Fix slow tracepoints
Kent Overstreet [Wed, 16 Feb 2022 05:42:34 +0000 (00:42 -0500)]
bcachefs: Fix slow tracepoints

Some of our tracepoints were calling snprintf("pS") - which does symbol
table lookups - in TP_fast_assign(), which turns out to be a really bad
idea.

This was done because perf trace wasn't correctly printing tracepoints
that use %pS anymore - but it turns out trace-cmd does handle it
correctly.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Check for stale dirty pointer before reads
Kent Overstreet [Tue, 15 Feb 2022 05:06:59 +0000 (00:06 -0500)]
bcachefs: Check for stale dirty pointer before reads

Since we retry reads when we discover we read from a pointer that went
stale, if a dirty pointer is erroniously stale it would cause us to loop
retrying that read forever - unless we check before issuing the read,
while the btree is still locked, when we know that a dirty pointer
should never be stale.

This patch adds that check, along with printing some helpful debug info.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Kill verify_not_stale()
Kent Overstreet [Mon, 14 Feb 2022 09:20:39 +0000 (04:20 -0500)]
bcachefs: Kill verify_not_stale()

This is ancient code that's more effectively checked in other places
now.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix __bch2_btree_node_lock
Kent Overstreet [Wed, 16 Feb 2022 03:01:33 +0000 (22:01 -0500)]
bcachefs: Fix __bch2_btree_node_lock

__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>
11 months agobcachefs: Also show when blocked on write locks
Kent Overstreet [Wed, 16 Feb 2022 03:28:37 +0000 (22:28 -0500)]
bcachefs: Also show when blocked on write locks

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>
11 months agobcachefs: Delete redundant tracepoint
Kent Overstreet [Wed, 16 Feb 2022 02:45:04 +0000 (21:45 -0500)]
bcachefs: Delete redundant tracepoint

We were emitting two trace events on transaction restart in this code
path - delete the redundant one.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix locking in data move path
Kent Overstreet [Wed, 16 Feb 2022 04:40:30 +0000 (23:40 -0500)]
bcachefs: Fix locking in data move path

We need to ensure we don't have any btree locks held when calling
do_pending_writes() - besides issuing IOs, upcoming allocator changes
will have allocations doing btree lookups directly.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Kill bch2_bkey_debugcheck
Kent Overstreet [Mon, 14 Feb 2022 03:16:45 +0000 (22:16 -0500)]
bcachefs: Kill bch2_bkey_debugcheck

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>
11 months agobcachefs: Print a better message for mark and sweep pass
Kent Overstreet [Mon, 14 Feb 2022 01:47:05 +0000 (20:47 -0500)]
bcachefs: Print a better message for mark and sweep pass

Btree gc, aka mark and sweep, checks allocations - so let's just print
that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Small fsck fix
Kent Overstreet [Mon, 14 Feb 2022 01:42:12 +0000 (20:42 -0500)]
bcachefs: Small fsck fix

The check_dirents pass handles transaction restarts at the toplevel -
check_subdir_count() was incorrectly handling transaction restarts
without returning -EINTR, meaning that the iterator pointing to the
dirent being checked was left invalid.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix reflink repair code
Kent Overstreet [Thu, 10 Feb 2022 08:40:44 +0000 (03:40 -0500)]
bcachefs: Fix reflink repair code

The reflink repair code was incorrectly inserting a nonzero deleted key
via journal replay - this is due to bch2_journal_key_insert() being
somewhat hacky, and so this fix is also hacky for now.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: bch2_gc_gens() no longer uses bucket array
Kent Overstreet [Fri, 24 Dec 2021 09:51:10 +0000 (04:51 -0500)]
bcachefs: bch2_gc_gens() no longer uses bucket array

Like the previous patches, this converts bch2_gc_gens() to use the alloc
btree directly, and private arrays of generation numbers for its own
recalculation of oldest_gen.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Copygc no longer uses bucket array
Kent Overstreet [Sat, 25 Dec 2021 08:37:52 +0000 (03:37 -0500)]
bcachefs: Copygc no longer uses bucket array

This converts the copygc code to use the alloc btree directly to find
buckets that need to be evacuated instead of the in-memory bucket array,
which is finally going away soon.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: btree_gc no longer uses main in-memory bucket array
Kent Overstreet [Sun, 26 Dec 2021 01:39:19 +0000 (20:39 -0500)]
bcachefs: btree_gc no longer uses main in-memory bucket array

This changes the btree_gc code to only use the second bucket array, the
one dedicated to GC. On completion, it compares what's in its in memory
bucket array to the allocation information in the btree and writes it
directly, instead of updating the main in-memory bucket array and
writing that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Inode create no longer needs to probe key cache
Kent Overstreet [Mon, 9 Jan 2023 07:25:08 +0000 (02:25 -0500)]
bcachefs: Inode create no longer needs to probe key cache

Now that we have full key cache coherency, we can simplify
bch2_inode_create().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Btree key cache coherency
Kent Overstreet [Wed, 12 Jan 2022 06:14:47 +0000 (01:14 -0500)]
bcachefs: Btree key cache coherency

 - Updates to non key cache iterators will now be transparently
   redirected to the key cache for cached btrees.

 - Except when creating new keys: then the update goes to underlying
   btree

For for iterating over a cached btree to work, we need to ensure that if
a key exists in the key cache, it also exists in the btree - otherwise
the iterator code will skip past it and not check the key cache.

Otherwise, for consistency, all updates should go to the same place -
the key cache.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: BTREE_ITER_WITH_KEY_CACHE
Kent Overstreet [Mon, 7 Feb 2022 04:15:12 +0000 (23:15 -0500)]
bcachefs: BTREE_ITER_WITH_KEY_CACHE

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@linux.dev>
11 months agobcachefs: run_one_trigger() now checks journal keys
Kent Overstreet [Thu, 24 Feb 2022 13:08:53 +0000 (08:08 -0500)]
bcachefs: run_one_trigger() now checks journal keys

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>
11 months agobcachefs: Stash a copy of key being overwritten in btree_insert_entry
Kent Overstreet [Thu, 24 Feb 2022 16:02:58 +0000 (11:02 -0500)]
bcachefs: Stash a copy of key being overwritten in btree_insert_entry

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>
11 months agobcachefs: bch2_btree_path_set_pos()
Kent Overstreet [Mon, 7 Feb 2022 03:21:44 +0000 (22:21 -0500)]
bcachefs: bch2_btree_path_set_pos()

bch2_btree_path_set_pos() is now available outside of btree_iter.c

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: btree_id_cached()
Kent Overstreet [Wed, 12 Jan 2022 07:13:21 +0000 (02:13 -0500)]
bcachefs: btree_id_cached()

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>
11 months agobcachefs: Improve btree_key_cache_flush_pos()
Kent Overstreet [Wed, 12 Jan 2022 05:49:23 +0000 (00:49 -0500)]
bcachefs: Improve btree_key_cache_flush_pos()

btree_key_cache_flush_pos() uses BTREE_ITER_CACHED_NOFILL - but it
wasn't checking for !ck->valid. It does check for the entry being dirty,
so it shouldn't matter, but this refactor it a bit and adds and
assertion.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix freeing in bch2_dev_buckets_resize()
Kent Overstreet [Mon, 7 Feb 2022 00:20:36 +0000 (19:20 -0500)]
bcachefs: Fix freeing in bch2_dev_buckets_resize()

We were double-freeing old_buckets and not freeing old_buckets_gens:
also, the code was supposed to free buckets, not old_buckets;
old_buckets is only needed because we have to use rcu_assign_pointer()
instead of swap(), and won't be set if we hit the error path.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Don't keep nodes in btree_reserve locked
Kent Overstreet [Mon, 7 Feb 2022 06:19:39 +0000 (01:19 -0500)]
bcachefs: Don't keep nodes in btree_reserve locked

These nodes aren't reachable by other threads, so there's no need to
keep it locked - and this fixes a bug with the assertion in
bch2_trans_unlock() firing on transaction restart.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Log message improvements
Kent Overstreet [Wed, 12 Jan 2022 03:08:44 +0000 (22:08 -0500)]
bcachefs: Log message improvements

Change the error messages in bch2_inconsistent_error() and
bch2_fatal_error() so we can distinguish them.

Also, prefer bch2_fs_fatal_error() (which also logs an error message) to
bch2_fatal_error(), and change a call to bch2_inconsistent_error() to
bch2_fatal_error() when we can't continue.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Delete some dead code
Kent Overstreet [Tue, 11 Jan 2022 05:19:52 +0000 (00:19 -0500)]
bcachefs: Delete some dead code

__bch2_mark_replicas() is now only used in one place, so inline it into
the caller.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Ignore cached data when calculating fragmentation
Kent Overstreet [Tue, 11 Jan 2022 00:46:39 +0000 (19:46 -0500)]
bcachefs: Ignore cached data when calculating fragmentation

Previously, bucket fragmentation was considered to be bucket size -
total amount of live data, both dirty and cached.

This meant that if a bucket was full but only a small amount of data in
it was dirty - the rest cached, we'd get stuck: copygc wouldn't move the
dirty data out of the bucket and the allocator wouldn't be able to
invalidate and drop the cached data.

This changes fragmentation to exclude cached data, so that copygc will
evacuate these buckets and copygc/the allocator will always be able to
make forward progress.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Don't use in-memory bucket array for alloc updates
Kent Overstreet [Sun, 26 Dec 2021 01:36:47 +0000 (20:36 -0500)]
bcachefs: Don't use in-memory bucket array for alloc updates

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>
11 months agobcachefs: Kill allocator short-circuit invalidate
Kent Overstreet [Fri, 24 Dec 2021 05:34:48 +0000 (00:34 -0500)]
bcachefs: Kill allocator short-circuit invalidate

The allocator thread invalidates buckets (increments their generation
number) prior to discarding them and putting them on freelists. We've
had a short circuit path for some time to only update the in-memory
bucket mark when doing the invalidate if we're not invalidating cached
data, but that short-circuit path hasn't really been needed for quite
some time (likely since the btree key cache code was added).

We're deleting it now as part of deleting/converting code that uses the
in memory bucket array.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: BTREE_INSERT_LAZY_RW is only for recovery path
Kent Overstreet [Mon, 10 Jan 2022 01:55:58 +0000 (20:55 -0500)]
bcachefs: BTREE_INSERT_LAZY_RW is only for recovery path

BTREE_INSERT_LAZY_RW shouldn't do anything after the filesystem has
finished starting up - otherwise, it might interfere with going
read-only as part of shutting down.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Handle transaction restarts in __bch2_move_data()
Kent Overstreet [Mon, 10 Jan 2022 01:52:10 +0000 (20:52 -0500)]
bcachefs: Handle transaction restarts in __bch2_move_data()

We weren't checking for -EINTR in the main loop in __bch2_move_data -
this code predates modern transaction restarts.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Simplify bch2_inode_delete_keys()
Kent Overstreet [Mon, 27 Dec 2021 23:25:23 +0000 (18:25 -0500)]
bcachefs: Simplify bch2_inode_delete_keys()

Had a bug report that implies bch2_inode_delete_keys() returned -EINTR
before it completed, so this patch simplifies it and makes the flow
control a little more conventional.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: iter->update_path
Kent Overstreet [Sun, 9 Jan 2022 02:22:31 +0000 (21:22 -0500)]
bcachefs: iter->update_path

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@linux.dev>
11 months agobcachefs: Refactor bch2_btree_iter()
Kent Overstreet [Sun, 9 Jan 2022 06:07:29 +0000 (01:07 -0500)]
bcachefs: Refactor bch2_btree_iter()

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@linux.dev>
11 months agobcachefs: Tracepoint improvements
Kent Overstreet [Sun, 9 Jan 2022 03:59:58 +0000 (22:59 -0500)]
bcachefs: Tracepoint improvements

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>
11 months agobcachefs: New snapshot unit test
Kent Overstreet [Wed, 29 Dec 2021 18:50:50 +0000 (13:50 -0500)]
bcachefs: New snapshot unit test

This still needs to be expanded more, but this adds a basic test for
BTREE_ITER_FILTER_SNAPSHOTS.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix an error path in bch2_snapshot_node_create()
Kent Overstreet [Sun, 9 Jan 2022 00:07:32 +0000 (19:07 -0500)]
bcachefs: Fix an error path in bch2_snapshot_node_create()

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Use BTREE_INSERT_USE_RESERVE in btree_update_key()
Kent Overstreet [Sat, 8 Jan 2022 08:39:54 +0000 (03:39 -0500)]
bcachefs: Use BTREE_INSERT_USE_RESERVE in btree_update_key()

bch2_btree_update_key() is used in the btree node write path - before
delivering the completion we have to update the parent pointer with the
number of sectors written.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Refactor trigger code
Kent Overstreet [Thu, 6 Jan 2022 06:20:12 +0000 (01:20 -0500)]
bcachefs: Refactor trigger code

This breaks bch2_trans_commit_run_triggers() up into multiple functions,
and deletes a bit of duplication - prep work for triggers on alloc keys,
which will need to run last.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>