linux.git
18 months agobcachefs: bch2_propagate_key_to_snapshot_leaves()
Kent Overstreet [Sat, 19 Aug 2023 01:14:33 +0000 (21:14 -0400)]
bcachefs: bch2_propagate_key_to_snapshot_leaves()

If fsck finds a key that needs work done, the primary example being an
unlinked inode that needs to be deleted, and the key is in an internal
snapshot node, we have a bit of a conundrum.

The conundrum is that internal snapshot nodes are shared, and we in
general do updates in internal snapshot nodes because there may be
overwrites in some snapshots and not others, and this may affect other
keys referenced by this key (i.e. extents).

For example, we might be seeing an unlinked inode in an internal
snapshot node, but then in one child snapshot the inode might have been
reattached and might not be unlinked. Deleting the inode in the internal
snapshot node would be wrong, because then we'll delete all the extents
that the child snapshot references.

But if an unlinked inode does not have any overwrites in child
snapshots, we're fine: the inode is overwrritten in all child snapshots,
so we can do the deletion at the point of comonality in the snapshot
tree, i.e. the node where we found it.

This patch adds a new helper, bch2_propagate_key_to_snapshot_leaves(),
to handle the case where we need a to update a key that does have
overwrites in child snapshots: we copy the key to leaf snapshot nodes,
and then rewind fsck and process the needed updates there.

With this, fsck can now always correctly handle unlinked inodes found in
internal snapshot nodes.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Cleanup redundant snapshot nodes
Kent Overstreet [Fri, 18 Aug 2023 02:10:02 +0000 (22:10 -0400)]
bcachefs: Cleanup redundant snapshot nodes

After deleteing snapshots, we may be left with a snapshot tree where
some nodes only have one child, and we have a linear chain.

Interior snapshot nodes are never used directly (i.e. they never have
subvolumes that point to them), they are only referered to by child
snapshot nodes - hence, they are redundant.

The existing code talks about redundant snapshot nodes as forming and
equivalence class; i.e. nodes for which snapshot_t->equiv is equal. In a
given equivalence class, we only ever need a single key at a given
position - i.e. multiple versions with different snapshot fields are
redundant.

The existing snapshot cleanup code deletes these redundant keys, but not
redundant nodes. It turns out this is buggy, because we assume that
after snapshot deletion finishes we should only have a single key per
equivalence class, but the btree update path doesn't preserve this -
overwriting keys in old snapshots doesn't check for the equivalence
class being equal, and thus we can end up with duplicate keys in the
same equivalence class and fsck complaining about snapshot deletion not
having run correctly.

The equivalence class notion has been leaking out of the core snapshots
code and into too much other code, i.e. fsck, so this patch takes a
different approach: snapshot deletion now moves keys to the node in an
equivalence class being kept (the leafiest node) and then deletes the
redundant nodes in the equivalance class.

Some work has to be done to correctly delete interior snapshot nodes;
snapshot node depth and skiplist fields for descendent nodes have to be
fixed.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix btree write buffer with snapshots btrees
Kent Overstreet [Mon, 21 Aug 2023 23:57:34 +0000 (19:57 -0400)]
bcachefs: Fix btree write buffer with snapshots btrees

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix is_ancestor bitmap
Kent Overstreet [Thu, 13 Jul 2023 06:43:29 +0000 (02:43 -0400)]
bcachefs: Fix is_ancestor bitmap

The is_ancestor bitmap is at optimization for bch2_snapshot_is_ancestor;
once we get sufficiently close to the ancestor ID we're searching for we
test a bitmap.

But initialization of the is_ancestor bitmap was broken; we do it by
using bch2_snapshot_parent(), but we call that on nodes that haven't
been initialized yet with bch2_mark_snapshot().

Fix this by adding a separate loop in bch2_snapshots_read() for
initializing the is_ancestor bitmap, and also add some new debug asserts
for checking this sort of breakage in the future.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: move check_pos_snapshot_overwritten() to snapshot.c
Kent Overstreet [Sat, 19 Aug 2023 01:13:44 +0000 (21:13 -0400)]
bcachefs: move check_pos_snapshot_overwritten() to snapshot.c

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch2_mount error path
Kent Overstreet [Fri, 18 Aug 2023 21:44:21 +0000 (17:44 -0400)]
bcachefs: Fix bch2_mount error path

In the bch2_mount() error path, we were calling
deactivate_locked_super(), which calls ->kill_sb(), which in our case
was calling bch2_fs_free() without __bch2_fs_stop().

This changes bch2_mount() to just call bch2_fs_stop() directly.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Delete a faulty assertion
Kent Overstreet [Fri, 18 Aug 2023 04:05:35 +0000 (00:05 -0400)]
bcachefs: Delete a faulty assertion

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve btree_path_relock_fail tracepoint
Kent Overstreet [Fri, 18 Aug 2023 02:04:20 +0000 (22:04 -0400)]
bcachefs: Improve btree_path_relock_fail tracepoint

In https://github.com/koverstreet/bcachefs/issues/450, we're seeing
unexplained btree_path_relock_fail events - according to the information
currently in the tracepoint, it appears the relock should be succeeding.

This adds lock counts to the tracepoint to help track it down.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix divide by zero in rebalance_work()
Kent Overstreet [Thu, 17 Aug 2023 20:35:58 +0000 (16:35 -0400)]
bcachefs: Fix divide by zero in rebalance_work()

This fixes https://github.com/koverstreet/bcachefs-tools/issues/159

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Split out snapshot.c
Kent Overstreet [Wed, 16 Aug 2023 20:54:33 +0000 (16:54 -0400)]
bcachefs: Split out snapshot.c

subvolume.c has gotten a bit large, this splits out a separate file just
for managing snapshot trees - BTREE_ID_snapshots.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: stack_trace_save_tsk() depends on CONFIG_STACKTRACE
Kent Overstreet [Wed, 16 Aug 2023 19:05:18 +0000 (15:05 -0400)]
bcachefs: stack_trace_save_tsk() depends on CONFIG_STACKTRACE

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix swallowing of data in buffered write path
Kent Overstreet [Tue, 15 Aug 2023 02:29:41 +0000 (22:29 -0400)]
bcachefs: Fix swallowing of data in buffered write path

In __bch2_buffered_write, if we fail to write to an entire !uptodate
folio, we have to back out the write, bail out and retry.

But we were missing an iov_iter_revert() call, so the data written to
the folio was lost and the rest of the write shifted to the wrong
offset.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fix up wonky error handling in bch2_seek_pagecache_hole()
Brian Foster [Mon, 14 Aug 2023 14:49:42 +0000 (10:49 -0400)]
bcachefs: fix up wonky error handling in bch2_seek_pagecache_hole()

The folio_hole_offset() helper returns a mix of bool and int types.
The latter is to support a possible -EAGAIN error code when using
nonblocking locks. This is not only confusing, but the only caller
also essentially ignores errors outside of stopping the range
iteration. This means an -EAGAIN error can't return directly from
folio_hole_offset() and may be lost via bch2_clamp_data_hole().

Fix up the error handling and make it more readable.
__filemap_get_folio() returns -ENOENT instead of NULL when no folio
exists, so reuse the same error code in folio_hole_offset(). Fix up
bch2_seek_pagecache_hole() to return the current offset on -ENOENT,
but otherwise return unexpected error code up to the caller.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bkey format calculation
Kent Overstreet [Sun, 13 Aug 2023 23:34:02 +0000 (19:34 -0400)]
bcachefs: Fix bkey format calculation

For extents, we increase the number of bits of the size field to allow
extents to get bigger due to merging - but this code didn't check for
overflow.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch2_extent_fallocate()
Kent Overstreet [Sun, 13 Aug 2023 22:04:32 +0000 (18:04 -0400)]
bcachefs: Fix bch2_extent_fallocate()

 - There was no need for a retry loop in bch2_extent_fallocate(); if we
   have to retry we may be overwriting something different and we need
   to return an error and let the caller retry.
 - The bch2_alloc_sectors_start() error path was wrong, and wasn't
   running our cleanup at the end of the function

This also fixes a very rare open bucket leak due to the missing cleanup.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Zero btree_paths on allocation
Kent Overstreet [Sun, 13 Aug 2023 22:15:53 +0000 (18:15 -0400)]
bcachefs: Zero btree_paths on allocation

This fixes a bug in the cycle detector, bch2_check_for_deadlock() - we
have to make sure the node pointers in the btree paths array are set to
something not-garbage before another thread may see them.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix 'pointer to invalid device' check
Kent Overstreet [Sun, 13 Aug 2023 17:04:08 +0000 (13:04 -0400)]
bcachefs: Fix 'pointer to invalid device' check

This fixes the device removal tests, which have been failing at random
due to the fact that when we're running the .key_invalid checks in the
write path the key may actually no longer exist - we might be racing
with the keys being deleted.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Lower BCH_NAME_MAX to 512
Joshua Ashton [Sun, 13 Aug 2023 15:53:45 +0000 (16:53 +0100)]
bcachefs: Lower BCH_NAME_MAX to 512

To ensure we aren't shooting ourselves in the foot after merge for
potentially doing future revisions for dirent or for storing multiple
names for casefolding, limit this to 512 for now.

Previously this define was linked to the max size a d_name in
bch_dirent could be.

Signed-off-by: Joshua Ashton <joshua@froggi.es>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Optimize bch2_dirent_name_bytes
Joshua Ashton [Sat, 12 Aug 2023 21:26:30 +0000 (22:26 +0100)]
bcachefs: Optimize bch2_dirent_name_bytes

Avoids doing a full strnlen for getting the length of the name of a
dirent entry.

Given the fact that the name of dirents is stored at the end of the
bkey's value, and we know the length of that in u64s, we can find the
last u64 and figure out how many NUL bytes are at the end of the string.

On little endian systems this ends up being the leading zeros of the
last u64, whereas on big endian systems this ends up being the trailing
zeros of the last u64.
We can take that value in bits and divide it by 8 to get the number of
NUL bytes at the end.

There is no endian-fixup or other compatibility here as this is string
data interpreted as a u64.

Signed-off-by: Joshua Ashton <joshua@froggi.es>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Introduce bch2_dirent_get_name
Joshua Ashton [Sat, 12 Aug 2023 21:26:29 +0000 (22:26 +0100)]
bcachefs: Introduce bch2_dirent_get_name

A nice cleanup that avoids a bunch of open-coding name/string usage
around dirent usage.

Will be used by casefolding impl in future commits.

Signed-off-by: Joshua Ashton <joshua@froggi.es>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: six locks: Guard against wakee exiting in __six_lock_wakeup()
Kent Overstreet [Sat, 12 Aug 2023 21:10:42 +0000 (17:10 -0400)]
bcachefs: six locks: Guard against wakee exiting in __six_lock_wakeup()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Don't open code closure_nr_remaining()
Kent Overstreet [Sat, 12 Aug 2023 20:51:45 +0000 (16:51 -0400)]
bcachefs: Don't open code closure_nr_remaining()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix lifetime in bch2_write_done(), add assertion
Kent Overstreet [Sat, 12 Aug 2023 20:52:33 +0000 (16:52 -0400)]
bcachefs: Fix lifetime in bch2_write_done(), add assertion

We're hunting for an open_bucket leak, add an assertion to help track it
down: also, we can't use the bch_fs after dropping our write ref to it.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add a comment for should_drop_open_bucket()
Kent Overstreet [Sat, 12 Aug 2023 20:46:54 +0000 (16:46 -0400)]
bcachefs: Add a comment for should_drop_open_bucket()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: six locks: Fix missing barrier on wait->lock_acquired
Kent Overstreet [Sat, 12 Aug 2023 19:05:06 +0000 (15:05 -0400)]
bcachefs: six locks: Fix missing barrier on wait->lock_acquired

Six locks do lock handoff via the wakeup path: the thread doing the
wakeup also takes the lock on behalf of the waiter, which means the
waiter only has to look at its waitlist entry, and doesn't have to touch
the lock cacheline while another thread is using it.

Linus noticed that this needs a real barrier, which this patch fixes.

Also add a comment for the should_sleep_fn() error path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: linux-bcachefs@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
18 months agobcachefs: Check for directories in deleted inodes btree
Kent Overstreet [Sat, 12 Aug 2023 16:34:47 +0000 (12:34 -0400)]
bcachefs: Check for directories in deleted inodes btree

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add btree_trans* to inode_set_fn
Joshua Ashton [Sat, 12 Aug 2023 14:47:45 +0000 (15:47 +0100)]
bcachefs: Add btree_trans* to inode_set_fn

This will be used when we need to re-hash a directory tree when setting
flags.

It is not possible to have concurrent btree_trans on a thread.

Signed-off-by: Joshua Ashton <joshua@froggi.es>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve bch2_write_points_to_text()
Kent Overstreet [Sat, 12 Aug 2023 16:13:19 +0000 (12:13 -0400)]
bcachefs: Improve bch2_write_points_to_text()

Now we also print the open_buckets owned by each write_point - this is
to help with debugging a shutdown hang.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix check_version_upgrade()
Kent Overstreet [Sat, 12 Aug 2023 02:22:31 +0000 (22:22 -0400)]
bcachefs: Fix check_version_upgrade()

We were failing to upgrade to the latest compatible version - whoops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix 'journal not marked as containing replicas'
Kent Overstreet [Fri, 11 Aug 2023 23:30:38 +0000 (19:30 -0400)]
bcachefs: Fix 'journal not marked as containing replicas'

This fixes the replicas_write_errors test: the patch
  bcachefs: mark journal replicas before journal write submission

partially fixed replicas marking for the journal, but it broke the case
where one replica failed - this patch re-adds marking after the journal
write completes, when we know how many replicas succeeded.

Additionally, we do not consider it a fsck error when the very last
journal entry is not correctly marked, since there is an inherent race
there.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: btree_journal_iter.c
Kent Overstreet [Sat, 5 Aug 2023 20:08:44 +0000 (16:08 -0400)]
bcachefs: btree_journal_iter.c

Split out a new file from recovery.c for managing the list of keys we
read from the journal: before journal replay finishes the btree iterator
code needs to be able to iterate over and return keys from the journal
as well, so there's a fair bit of code here.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: sb-clean.c
Kent Overstreet [Sat, 5 Aug 2023 19:54:38 +0000 (15:54 -0400)]
bcachefs: sb-clean.c

Pull code for bch_sb_field_clean out into its own file.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Move bch_sb_field_crypt code to checksum.c
Kent Overstreet [Sat, 5 Aug 2023 19:43:00 +0000 (15:43 -0400)]
bcachefs: Move bch_sb_field_crypt code to checksum.c

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: sb-members.c
Kent Overstreet [Sat, 5 Aug 2023 19:40:21 +0000 (15:40 -0400)]
bcachefs: sb-members.c

Split out a new file for bch_sb_field_members - we'll likely want to
move more code here in the future.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Split up btree_update_leaf.c
Kent Overstreet [Sat, 5 Aug 2023 16:55:08 +0000 (12:55 -0400)]
bcachefs: Split up btree_update_leaf.c

We now have
  btree_trans_commit.c
  btree_update.c

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Split up fs-io.[ch]
Kent Overstreet [Thu, 3 Aug 2023 22:18:21 +0000 (18:18 -0400)]
bcachefs: Split up fs-io.[ch]

fs-io.c is too big - time for some reorganization
 - fs-dio.c: direct io
 - fs-pagecache.c: pagecache data structures (bch_folio), utility code

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix assorted checkpatch nits
Kent Overstreet [Mon, 7 Aug 2023 16:04:05 +0000 (12:04 -0400)]
bcachefs: Fix assorted checkpatch nits

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix for sb buffer being misaligned
Kent Overstreet [Tue, 8 Aug 2023 00:44:56 +0000 (20:44 -0400)]
bcachefs: Fix for sb buffer being misaligned

On old kernels, kmalloc() may return an allocation that's not naturally
aligned - this resulted in a bug where we allocated a bio with not
enough biovecs. Fix this by using buf_pages().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Convert journal validation to bkey_invalid_flags
Kent Overstreet [Sun, 6 Aug 2023 16:43:31 +0000 (12:43 -0400)]
bcachefs: Convert journal validation to bkey_invalid_flags

This fixes a bug where we were already passing bkey_invalid_flags
around, but treating the parameter as just read/write - so the compat
code wasn't being run correctly.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve journal_entry_err_msg()
Kent Overstreet [Sun, 6 Aug 2023 14:57:25 +0000 (10:57 -0400)]
bcachefs: Improve journal_entry_err_msg()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: BCH_COMPAT_bformat_overflow_done no longer required
Kent Overstreet [Sun, 6 Aug 2023 14:04:37 +0000 (10:04 -0400)]
bcachefs: BCH_COMPAT_bformat_overflow_done no longer required

Awhile back, we changed bkey_format generation to ensure that the packed
representation could never represent fields larger than the unpacked
representation.

This was to ensure that bkey_packed_successor() always gave a sensible
result, but in the current code bkey_packed_successor() is only used in
a debug assertion - not for anything important.

This kills the requirement that we've gotten rid of those weird bkey
formats, and instead changes the assertion to check if we're dealing
with an old weird bkey format.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: kill EBUG_ON() redefinition in bkey.c
Kent Overstreet [Sun, 6 Aug 2023 14:02:41 +0000 (10:02 -0400)]
bcachefs: kill EBUG_ON() redefinition in bkey.c

our debug mode assertions in bkey.c haven't been getting run, whoops

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add logging to bch2_inode_peek() & related
Kent Overstreet [Sun, 6 Aug 2023 14:04:05 +0000 (10:04 -0400)]
bcachefs: Add logging to bch2_inode_peek() & related

Add error messages when we fail to lookup an inode, and also add a few
missing bch2_err_class() calls.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix lock thrashing in __bchfs_fallocate()
Kent Overstreet [Thu, 3 Aug 2023 07:39:49 +0000 (03:39 -0400)]
bcachefs: Fix lock thrashing in __bchfs_fallocate()

We've observed significant lock thrashing on fstests generic/083 in
fallocate, due to dropping and retaking btree locks when checking the
pagecache for data.

This adds a nonblocking mode to bch2_clamp_data_hole(), where we only
use folio_trylock(), and can thus be used safely while btree locks are
held - thus we only have to drop btree locks as a fallback, on actual
lock contention.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix for bch2_copygc() spuriously returning -EEXIST
Kent Overstreet [Fri, 4 Aug 2023 14:51:02 +0000 (10:51 -0400)]
bcachefs: Fix for bch2_copygc() spuriously returning -EEXIST

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Convert btree_err_type to normal error codes
Kent Overstreet [Thu, 3 Aug 2023 23:36:28 +0000 (19:36 -0400)]
bcachefs: Convert btree_err_type to normal error codes

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix btree_err() macro
Kent Overstreet [Fri, 4 Aug 2023 00:32:46 +0000 (20:32 -0400)]
bcachefs: Fix btree_err() macro

Error code wasn't being propagated correctly, change it to match
fsck_err()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Ensure topology repair runs
Kent Overstreet [Fri, 4 Aug 2023 00:57:06 +0000 (20:57 -0400)]
bcachefs: Ensure topology repair runs

This fixes should_restart_for_topology_repair() - previously it was
returning false if the btree io path had already seleceted topology
repair to run, even if it hadn't run yet.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Log a message when running an explicit recovery pass
Kent Overstreet [Fri, 4 Aug 2023 00:37:32 +0000 (20:37 -0400)]
bcachefs: Log a message when running an explicit recovery pass

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Print out required recovery passes on version upgrade
Kent Overstreet [Thu, 3 Aug 2023 21:33:20 +0000 (17:33 -0400)]
bcachefs: Print out required recovery passes on version upgrade

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix shift by 64 in set_inc_field()
Kent Overstreet [Thu, 3 Aug 2023 20:38:36 +0000 (16:38 -0400)]
bcachefs: Fix shift by 64 in set_inc_field()

UBSAN was complaining about a shift by 64 in set_inc_field().

This only happened when the value being shifted was 0, so in theory
should be harmless - a shift by 64 (or register width) should logically
give a result of 0, but CPUs will in practice leave the input unchanged
when the number of bits to shift by wraps - and since our input here is
0, the output is still what we want.

But, it's still undefined behaviour and we need our UBSAN output to be
clean, so it needs to be fixed.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bkey_format helper improvements
Kent Overstreet [Thu, 3 Aug 2023 18:42:37 +0000 (14:42 -0400)]
bcachefs: bkey_format helper improvements

 - add a to_text() method for bkey_format

 - convert bch2_bkey_format_validate() to modern error message style,
   where we pass a printbuf for the error string instead of returning a
   static string

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bcachefs_metadata_version_deleted_inodes
Kent Overstreet [Mon, 17 Jul 2023 04:56:29 +0000 (00:56 -0400)]
bcachefs: bcachefs_metadata_version_deleted_inodes

Add a new bitset btree for inodes pending deletion; this means we no
longer have to scan the full inodes btree after an unclean shutdown.

Specifically, this adds:
 - a trigger to update the deleted_inodes btree based on changes to the
   inodes btree
 - a new recovery pass
 - and check_inodes is now only a fsck pass.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix folio leak in folio_hole_offset()
Kent Overstreet [Thu, 3 Aug 2023 07:29:42 +0000 (03:29 -0400)]
bcachefs: Fix folio leak in folio_hole_offset()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix overlapping extent repair
Kent Overstreet [Fri, 21 Jul 2023 02:42:26 +0000 (22:42 -0400)]
bcachefs: Fix overlapping extent repair

A number of smallish fixes for overlapping extent repair, and (part of)
a new unit test. This fixes all the issues turned up by bhzhu203, in his
filesystem image from running mongodb + snapshots.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: In debug mode, run fsck again after fixing errors
Kent Overstreet [Thu, 3 Aug 2023 00:19:58 +0000 (20:19 -0400)]
bcachefs: In debug mode, run fsck again after fixing errors

We want to ensure that fsck actually fixed all the errors it found - the
second fsck run should be clean.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: recovery_types.h
Kent Overstreet [Wed, 2 Aug 2023 23:49:24 +0000 (19:49 -0400)]
bcachefs: recovery_types.h

Move some code out of bcachefs.h, which is too much of an everything
header.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Handle weird opt string from sys_fsconfig()
Kent Overstreet [Wed, 2 Aug 2023 16:51:51 +0000 (12:51 -0400)]
bcachefs: Handle weird opt string from sys_fsconfig()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Assorted fixes for clang
Kent Overstreet [Wed, 2 Aug 2023 00:06:45 +0000 (20:06 -0400)]
bcachefs: Assorted fixes for clang

clang had a few more warnings about enum conversion, and also didn't
like the opts.c initializer.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Move fsck_inode_rm() to inode.c
Kent Overstreet [Fri, 21 Jul 2023 07:20:08 +0000 (03:20 -0400)]
bcachefs: Move fsck_inode_rm() to inode.c

Prep work for the new deleted inodes btree

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Consolidate btree id properties
Kent Overstreet [Fri, 21 Jul 2023 09:38:45 +0000 (05:38 -0400)]
bcachefs: Consolidate btree id properties

This refactoring centralizes defining per-btree properties.

bch2_key_types_allowed was also about to overflow a u32, so expand that
to a u64.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_trans_update_extent_overwrite()
Kent Overstreet [Fri, 21 Jul 2023 04:27:19 +0000 (00:27 -0400)]
bcachefs: bch2_trans_update_extent_overwrite()

Factor out a new helper, to be used when fsck has to repair overlapping
extents.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix minor memory leak on invalid bkey
Kent Overstreet [Fri, 21 Jul 2023 03:13:43 +0000 (23:13 -0400)]
bcachefs: Fix minor memory leak on invalid bkey

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Move some declarations to the correct header
Kent Overstreet [Fri, 21 Jul 2023 03:14:05 +0000 (23:14 -0400)]
bcachefs: Move some declarations to the correct header

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix btree iter leak in __bch2_insert_snapshot_whiteouts()
Kent Overstreet [Fri, 21 Jul 2023 02:47:59 +0000 (22:47 -0400)]
bcachefs: Fix btree iter leak in __bch2_insert_snapshot_whiteouts()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a null ptr deref in check_xattr()
Kent Overstreet [Thu, 20 Jul 2023 23:30:53 +0000 (19:30 -0400)]
bcachefs: Fix a null ptr deref in check_xattr()

We were attempting to initialize inode hash info when no inodes were
found.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_btree_bit_mod()
Kent Overstreet [Mon, 17 Jul 2023 04:56:07 +0000 (00:56 -0400)]
bcachefs: bch2_btree_bit_mod()

New helper for bitset btrees.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: move inode triggers to inode.c
Kent Overstreet [Mon, 17 Jul 2023 04:41:48 +0000 (00:41 -0400)]
bcachefs: move inode triggers to inode.c

bit of reorg

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fsck: delete dead code
Kent Overstreet [Mon, 17 Jul 2023 04:12:58 +0000 (00:12 -0400)]
bcachefs: fsck: delete dead code

Delete the old, now reimplemented overlapping extent check/repair.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Make topology repair a normal recovery pass
Kent Overstreet [Mon, 17 Jul 2023 03:19:49 +0000 (23:19 -0400)]
bcachefs: Make topology repair a normal recovery pass

This adds bch2_run_explicit_recovery_pass(), for rewinding recovery and
explicitly running a specific recovery pass - this is a more general
replacement for how we were running topology repair before.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_run_explicit_recovery_pass()
Kent Overstreet [Mon, 17 Jul 2023 03:21:17 +0000 (23:21 -0400)]
bcachefs: bch2_run_explicit_recovery_pass()

This introduces bch2_run_explicit_recovery_pass() and uses it for when
fsck detects that we need to re-run dead snaphots cleanup, and makes
dead snapshot cleanup more like a normal recovery pass.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Print version, options earlier in startup path
Kent Overstreet [Thu, 20 Jul 2023 22:09:26 +0000 (18:09 -0400)]
bcachefs: Print version, options earlier in startup path

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: use prejournaled key updates for write buffer flushes
Brian Foster [Wed, 19 Jul 2023 12:53:06 +0000 (08:53 -0400)]
bcachefs: use prejournaled key updates for write buffer flushes

The write buffer mechanism journals keys twice in certain
situations. A key is always journaled on write buffer insertion, and
is potentially journaled again if a write buffer flush falls into
either of the slow btree insert paths. This has shown to cause
journal recovery ordering problems in the event of an untimely
crash.

For example, consider if a key is inserted into index 0 of a write
buffer, the active write buffer switches to index 1, the key is
deleted in index 1, and then index 0 is flushed. If the original key
is rejournaled in the btree update from the index 0 flush, the (now
deleted) key is journaled in a seq buffer ahead of the latest
version of key (which was journaled when the key was deleted in
index 1). If the fs crashes while this is still observable in the
log, recovery sees the key from the btree update after the delete
key from the write buffer insert, which is the incorrect order. This
problem is occasionally reproduced by generic/388 and generally
manifests as one or more backpointer entry inconsistencies.

To avoid this problem, never rejournal write buffered key updates to
the associated btree. Instead, use prejournaled key updates to pass
the journal seq of the write buffer insert down to the btree insert,
which updates the btree leaf pin to reflect the seq of the key.

Note that tracking the seq is required instead of just using
NOJOURNAL here because otherwise we lose protection of the write
buffer pin when the buffer is flushed, which means the key can fall
off the tail of the on-disk journal before the btree leaf is flushed
and lead to similar recovery inconsistencies.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: support btree updates of prejournaled keys
Brian Foster [Wed, 19 Jul 2023 12:53:05 +0000 (08:53 -0400)]
bcachefs: support btree updates of prejournaled keys

Introduce support for prejournaled key updates. This allows a
transaction to commit an update for a key that already exists (and
is pinned) in the journal. This is required for btree write buffer
updates as the current scheme of journaling both on write buffer
insertion and write buffer (slow path) flush is unsafe in certain
crash recovery scenarios.

Create a small trans update wrapper to pass along the seq where the
key resides into the btree_insert_entry. From there, trans commit
passes the seq into the btree insert path where it is used to manage
the journal pin for the associated btree leaf.

Note that this patch only introduces the underlying mechanism and
otherwise includes no functional changes.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fold bch2_trans_update_by_path_trace() into callers
Brian Foster [Wed, 19 Jul 2023 12:53:04 +0000 (08:53 -0400)]
bcachefs: fold bch2_trans_update_by_path_trace() into callers

There is only one other caller so eliminate some boilerplate.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: remove unnecessary btree_insert_key_leaf() wrapper
Brian Foster [Wed, 19 Jul 2023 12:53:03 +0000 (08:53 -0400)]
bcachefs: remove unnecessary btree_insert_key_leaf() wrapper

This is in preparation to support prejournaled keys. We want the
ability to optionally pass a seq stored in the btree update rather
than the seq of the committing transaction.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: remove duplicate code between backpointer update paths
Brian Foster [Wed, 19 Jul 2023 12:53:02 +0000 (08:53 -0400)]
bcachefs: remove duplicate code between backpointer update paths

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agoMAINTAINERS: add Brian Foster as a reviewer for bcachefs
Brian Foster [Thu, 20 Jul 2023 13:00:33 +0000 (09:00 -0400)]
MAINTAINERS: add Brian Foster as a reviewer for bcachefs

Brian has been playing with bcachefs for several months now and has
offerred to commit time to patch review.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Suppresss various error messages in no_data_io mode
Kent Overstreet [Mon, 17 Jul 2023 02:31:19 +0000 (22:31 -0400)]
bcachefs: Suppresss various error messages in no_data_io mode

We commonly use no_data_io mode when debugging filesystem metadata
dumps, where data checksum/compression errors are expected and
unimportant - this patch suppresses these.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix lookup_inode_for_snapshot()
Kent Overstreet [Mon, 17 Jul 2023 01:56:18 +0000 (21:56 -0400)]
bcachefs: Fix lookup_inode_for_snapshot()

This fixes a use-after-free.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: need_snapshot_cleanup shouldn't be a fsck error
Kent Overstreet [Mon, 17 Jul 2023 01:09:37 +0000 (21:09 -0400)]
bcachefs: need_snapshot_cleanup shouldn't be a fsck error

We currently don't track whether snapshot cleanup still needs to finish
(aside from running a full fsck), so it shouldn't be a fsck error yet -
fsck -n after fsck has succesfully completed shouldn't error.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve key_visible_in_snapshot()
Kent Overstreet [Sun, 16 Jul 2023 22:15:01 +0000 (18:15 -0400)]
bcachefs: Improve key_visible_in_snapshot()

Delete a redundant bch2_snapshot_is_ancestor() check, and convert some
assertions to debug assertions.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Refactor overlapping extent checks
Kent Overstreet [Sun, 16 Jul 2023 19:12:25 +0000 (15:12 -0400)]
bcachefs: Refactor overlapping extent checks

Make the overlapping extent check/repair code more self contained.

This is prep work for hopefully reducing key_visible_in_snapshot() usage
here as well, and also includes a nice performance optimization to not
check ref_visible2() unless the extents potentially overlap.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: check_extent(): don't use key_visible_in_snapshot()
Kent Overstreet [Sun, 16 Jul 2023 18:55:33 +0000 (14:55 -0400)]
bcachefs: check_extent(): don't use key_visible_in_snapshot()

This changes the main part of check_extents(), that checks the extent
against the corresponding inode, to not use key_visible_in_snapshot().

key_visible_in_snapshot() has to iterate over the list of ancestor
overwrites repeatedly calling bch2_snapshot_is_ancestor(), so this is a
significant performance improvement.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: check_extent() refactoring
Kent Overstreet [Sun, 16 Jul 2023 18:45:23 +0000 (14:45 -0400)]
bcachefs: check_extent() refactoring

More prep work for reducing key_visible_in_snapshot() usage - this
rearranges how KEY_TYPE_whitout keys are handled, so that they can be
marked off in inode_warker->inode->seen_this_pos.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fsck: walk_inode() now takes is_whiteout
Kent Overstreet [Sun, 16 Jul 2023 18:19:08 +0000 (14:19 -0400)]
bcachefs: fsck: walk_inode() now takes is_whiteout

We only want to synthesize an inode for the current snapshot ID for non
whiteouts - this refactoring lets us call walk_inode() earlier and clean
up some control flow.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Simplify check_extent()
Kent Overstreet [Thu, 13 Jul 2023 05:41:02 +0000 (01:41 -0400)]
bcachefs: Simplify check_extent()

Minor refactoring/dead code deletion, prep work for reworking
check_extent() to avoid key_visible_in_snapshot().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: overlapping_extents_found()
Kent Overstreet [Thu, 13 Jul 2023 07:11:16 +0000 (03:11 -0400)]
bcachefs: overlapping_extents_found()

This improves the repair path for overlapping extents - we now verify
that we find in the btree the overlapping extents that the algorithm
detected, and fail the fsck run with a more useful error if it doesn't
match.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fsck: inode_walker: last_pos, seen_this_pos
Kent Overstreet [Sun, 16 Jul 2023 18:24:36 +0000 (14:24 -0400)]
bcachefs: fsck: inode_walker: last_pos, seen_this_pos

Prep work for changing check_extent() to avoid
key_visible_in_snapshot() - this adds the state to track whether an
inode has seen an extent at this pos.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: check_extents(): make sure to check i_sectors for last inode
Kent Overstreet [Sun, 16 Jul 2023 18:33:57 +0000 (14:33 -0400)]
bcachefs: check_extents(): make sure to check i_sectors for last inode

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Inline bch2_snapshot_is_ancestor() fast path
Kent Overstreet [Sun, 16 Jul 2023 19:59:40 +0000 (15:59 -0400)]
bcachefs: Inline bch2_snapshot_is_ancestor() fast path

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Upgrade path fixes
Kent Overstreet [Sun, 16 Jul 2023 01:03:26 +0000 (21:03 -0400)]
bcachefs: Upgrade path fixes

Some minor fixes to not print errors that are actually due to a verson
upgrade.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: is_ancestor bitmap
Kent Overstreet [Thu, 13 Jul 2023 06:43:29 +0000 (02:43 -0400)]
bcachefs: is_ancestor bitmap

Further optimization for bch2_snapshot_is_ancestor(). We add a small
inline bitmap to snapshot_t, which indicates which of the next 128
snapshot IDs are ancestors of the current id - eliminating the last few
iterations of the loop in bch2_snapshot_is_ancestor().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: mark bch_inode_info and bkey_cached as reclaimable
Mikulas Patocka [Thu, 13 Jul 2023 16:00:28 +0000 (18:00 +0200)]
bcachefs: mark bch_inode_info and bkey_cached as reclaimable

Mark these caches as reclaimable, so that available memory is correctly
reported when there is a lot of cached inodes.

Note that more work is needed - you should add __GFP_RECLAIMABLE to some
of the kmalloc calls, so that they are allocated from the "kmalloc-rcl-*"
caches.

Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Compression levels
Kent Overstreet [Thu, 13 Jul 2023 02:27:16 +0000 (22:27 -0400)]
bcachefs: Compression levels

This allows including a compression level when specifying a compression
type, e.g.
  compression=zstd:15

Values from 1 through 15 indicate compression levels, 0 or unspecified
indicates the default.

For LZ4, values 3-15 specify that the HC algorithm should be used.

Note that for compatibility, extents themselves only include the
compression type, not the compression level. This means that specifying
the same compression algorithm but different compression levels for the
compression and background_compression options will have no effect.

XXX: perhaps we could add a warning for this

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Extent sb compression type fields to 8 bits
Kent Overstreet [Thu, 13 Jul 2023 02:06:37 +0000 (22:06 -0400)]
bcachefs: Extent sb compression type fields to 8 bits

The upper 4 bits are for compression level.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bcachefs_format.h should be using __u64
Kent Overstreet [Thu, 13 Jul 2023 02:06:11 +0000 (22:06 -0400)]
bcachefs: bcachefs_format.h should be using __u64

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fix_errors option is now a proper enum
Kent Overstreet [Wed, 12 Jul 2023 03:47:29 +0000 (23:47 -0400)]
bcachefs: fix_errors option is now a proper enum

Before, it was parsed as a bool but internally it was really an enum:
this lets us pass in all the possible values.

But we special case the option parsing: no supplied value is parsed as
FSCK_FIX_yes, to match the previous behaviour.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch_opt_fn
Kent Overstreet [Thu, 13 Jul 2023 01:48:32 +0000 (21:48 -0400)]
bcachefs: bch_opt_fn

Minor refactoring to get rid of some unneeded token pasting.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Convert snapshot table to RCU array
Kent Overstreet [Wed, 12 Jul 2023 17:55:03 +0000 (13:55 -0400)]
bcachefs: Convert snapshot table to RCU array

This switches the generic radix tree for the in-memory table of snapshot
nodes to a simple rcu array. This means we have to add new locking to
deal with reallocations, but is faster than traversing the radix tree.

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