linux.git
18 months agobcachefs: Use for_each_btree_key_upto() more consistently
Kent Overstreet [Tue, 11 Oct 2022 08:32:41 +0000 (04:32 -0400)]
bcachefs: Use for_each_btree_key_upto() more consistently

It's important that in BTREE_ITER_FILTER_SNAPSHOTS mode we always use
peek_upto() and provide an end for the interval we're searching for -
otherwise, when we hit the end of the inode the next inode be in a
different subvolume and not have any keys in the current snapshot, and
we'd iterate over arbitrarily many keys before returning one.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Don't call bch2_journal_pin_drop() under key cache lock
Kent Overstreet [Fri, 3 Mar 2023 04:51:47 +0000 (23:51 -0500)]
bcachefs: Don't call bch2_journal_pin_drop() under key cache lock

This fixes a (harmless) lockdep splat, due to a lock order violation in
the key cache exit path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Improved optimistic spinning
Kent Overstreet [Sun, 5 Feb 2023 19:09:30 +0000 (14:09 -0500)]
six locks: Improved optimistic spinning

This adds a threshold for the maximum spin time, similar to the rwsem
code, and a flag to the lock itself indicating when we've spun too long
so other threads also refrain from spinning.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Use six_lock_ip()
Kent Overstreet [Sun, 5 Feb 2023 00:39:59 +0000 (19:39 -0500)]
bcachefs: Use six_lock_ip()

This uses the new _ip() interface to six locks and hooks it up to
btree_path->ip_allocated, when available.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Expose tracepoint IP
Kent Overstreet [Sun, 5 Feb 2023 00:38:43 +0000 (19:38 -0500)]
six locks: Expose tracepoint IP

This adds _ip variations of the various lock functions that allow an IP
to be passed in, which is used by lockstat.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_trans_in_restart_error()
Kent Overstreet [Wed, 1 Feb 2023 21:15:51 +0000 (16:15 -0500)]
bcachefs: bch2_trans_in_restart_error()

This replaces various BUG_ON() assertions with panics that tell us where
the restart was done and the restart type.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve btree node read error path
Kent Overstreet [Wed, 1 Feb 2023 20:45:45 +0000 (15:45 -0500)]
bcachefs: Improve btree node read error path

This ensures that failure to read a btree node error is treated as a
topology error, and returns the correct error so that the topology
repair pass will be run.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch2_trans_reset_updates()
Kent Overstreet [Sun, 5 Feb 2023 19:07:34 +0000 (14:07 -0500)]
bcachefs: Fix bch2_trans_reset_updates()

This should have been resetting trans->fs_usage_deltas as well.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Inline bch2_btree_path_traverse() fastpath
Kent Overstreet [Sun, 5 Feb 2023 01:40:29 +0000 (20:40 -0500)]
bcachefs: Inline bch2_btree_path_traverse() fastpath

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix hash_check_key()
Kent Overstreet [Wed, 1 Feb 2023 21:46:42 +0000 (16:46 -0500)]
bcachefs: Fix hash_check_key()

On hash collision when we have to check for duplicates or incorrect
hash value, we weren't specifying a snapshot ID to iterate with.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Don't emit tracepoints for expected events
Kent Overstreet [Wed, 25 Jan 2023 17:16:23 +0000 (12:16 -0500)]
bcachefs: Don't emit tracepoints for expected events

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Use trylock in bch2_prt_backtrace()
Kent Overstreet [Wed, 25 Jan 2023 15:18:32 +0000 (10:18 -0500)]
bcachefs: Use trylock in bch2_prt_backtrace()

Easy workaround for a lockdep splat - and since bch2_prt_backtrace() is
only used in debug code this is fine.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_inode_opts_get()
Kent Overstreet [Thu, 24 Nov 2022 01:14:55 +0000 (20:14 -0500)]
bcachefs: bch2_inode_opts_get()

This improves io_opts() and makes it a non-inline function - it's big
enough that it probably shouldn't be.

Also, bch_io_opts no longer needs fields for whether options are
defined, so we can slim it down a bit.

We'd like to stop passing around the full bch_io_opts, but that'll be
tricky because of bch2_rebalance_add_key().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch_alloc_to_text()
Kent Overstreet [Mon, 19 Dec 2022 20:55:38 +0000 (15:55 -0500)]
bcachefs: Fix bch_alloc_to_text()

We weren't guarding against the alloc key having an invalid data type.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Better inlining in core write path
Kent Overstreet [Thu, 24 Nov 2022 23:03:55 +0000 (18:03 -0500)]
bcachefs: Better inlining in core write path

Provide inline versions of some allocation functions
 - bch2_alloc_sectors_done_inlined()
 - bch2_alloc_sectors_append_ptrs_inlined()

and use them in the core IO path.

Also, inline bch2_extent_update_i_size_sectors() and
bch2_bkey_append_ptr().

In the core write path, function call overhead matters - every function
call is a jump to a new location and a potential cache miss.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Better inlining for bch2_alloc_to_v4_mut
Kent Overstreet [Tue, 31 Jan 2023 01:58:43 +0000 (20:58 -0500)]
bcachefs: Better inlining for bch2_alloc_to_v4_mut

This separates out the slowpath into a separate function, and inlines
bch2_alloc_v4_mut into bch2_trans_start_alloc_update(), the main place
it's called.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve btree_reserve_get_fail tracepoint
Kent Overstreet [Thu, 2 Feb 2023 03:51:51 +0000 (22:51 -0500)]
bcachefs: Improve btree_reserve_get_fail tracepoint

Now we include the return code.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch2_bucket_alloc_early()
Kent Overstreet [Tue, 24 Jan 2023 01:28:59 +0000 (20:28 -0500)]
bcachefs: Fix bch2_bucket_alloc_early()

We were incorrectly retrying after a transaction restart.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Check for lru entries with time=0
Kent Overstreet [Wed, 4 Jan 2023 05:00:55 +0000 (00:00 -0500)]
bcachefs: Check for lru entries with time=0

These are invalid.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix rereplicate when we already have a cached pointer
Kent Overstreet [Sun, 8 Jan 2023 03:55:42 +0000 (22:55 -0500)]
bcachefs: Fix rereplicate when we already have a cached pointer

When we need to add more replicas to an extent, it might be the case
that we already have a replica on every device, but some of them are
cached.

This patch fixes a bug where we'd spin on that extent because the write
path fails to find a device we can allocate from: we allow allocating
from devices that already have cached replicas on them, and change
bch2_data_update_index_update() to drop the cached replica if needed.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix repair path in bch2_mark_reflink_p()
Kent Overstreet [Fri, 20 Jan 2023 22:02:56 +0000 (17:02 -0500)]
bcachefs: Fix repair path in bch2_mark_reflink_p()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Dump transaction updates before panicing
Kent Overstreet [Wed, 25 Jan 2023 15:08:50 +0000 (10:08 -0500)]
bcachefs: Dump transaction updates before panicing

When errors=panic, we need to dump transaction updates before calling
bch2_inconsistent_error().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill fs_usage_apply_warn()
Kent Overstreet [Wed, 25 Jan 2023 15:07:52 +0000 (10:07 -0500)]
bcachefs: Kill fs_usage_apply_warn()

We now have bch2_trans_inconsistent() which generically does the same
thing - dumps pending btree transaction updates.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: debug: Fix some locking bugs
Kent Overstreet [Wed, 25 Jan 2023 00:42:04 +0000 (19:42 -0500)]
bcachefs: debug: Fix some locking bugs

This fixes a few error paths in debug code that lead to locks failing to
be dropped.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Convert EAGAIN errors to private error codes
Kent Overstreet [Tue, 13 Dec 2022 20:17:40 +0000 (15:17 -0500)]
bcachefs: Convert EAGAIN errors to private error codes

More error code cleanup, for better error messages and debugability.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Convert EROFS errors to private error codes
Kent Overstreet [Mon, 12 Dec 2022 01:37:11 +0000 (20:37 -0500)]
bcachefs: Convert EROFS errors to private error codes

More error code improvements - this gets us more useful error messages.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix compat path for old inode formats
Kent Overstreet [Sat, 3 Dec 2022 20:44:54 +0000 (15:44 -0500)]
bcachefs: Fix compat path for old inode formats

Old inode formats don't have all the fields of the current inode format:
when unpacking inodes in the current format we can thus skip zeroing out
the destination buffer, but that doesn't work on for the old formats.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_trans_relock_notrace()
Kent Overstreet [Tue, 24 Jan 2023 05:26:48 +0000 (00:26 -0500)]
bcachefs: bch2_trans_relock_notrace()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: btree_iter->ip_allocated
Kent Overstreet [Mon, 9 Jan 2023 06:11:18 +0000 (01:11 -0500)]
bcachefs: btree_iter->ip_allocated

In debug mode, we now track where btree iterators and paths are
initialized/allocated - helpful in tracking down btree path overflows.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: key cache: Don't hold btree locks while using GFP_RECLAIM
Kent Overstreet [Sat, 7 Jan 2023 10:46:52 +0000 (05:46 -0500)]
bcachefs: key cache: Don't hold btree locks while using GFP_RECLAIM

This is something we need to do more widely: instead of bothering with
GFP_NOIO/GFP_NOFS, if we need to allocate memory while holding locks:

 - first attempt the allocation with GFP_NOWAIT
 - if that fails, drop btree locks with bch2_trans_unlock(), then
   retry with GFP_KERNEL.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve bkey_cached_lock_for_evict()
Kent Overstreet [Sun, 8 Jan 2023 05:05:30 +0000 (00:05 -0500)]
bcachefs: Improve bkey_cached_lock_for_evict()

We don't need a write lock to check if a key is dirty.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch2_btree_path_traverse_all()
Kent Overstreet [Sun, 8 Jan 2023 05:04:30 +0000 (00:04 -0500)]
bcachefs: Fix bch2_btree_path_traverse_all()

We need to take a ref on a path while we're traversing it: this fixes a
bug with paths getting reused while being traversed, in the key cache
fill code.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Delete a faulty assertion
Kent Overstreet [Sat, 7 Jan 2023 03:58:19 +0000 (22:58 -0500)]
bcachefs: Delete a faulty assertion

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill bch2_extent_trim_atomic() usage
Kent Overstreet [Fri, 6 Jan 2023 11:29:04 +0000 (06:29 -0500)]
bcachefs: Kill bch2_extent_trim_atomic() usage

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Plumb saw_error through to btree_err()
Kent Overstreet [Tue, 3 Jan 2023 22:32:16 +0000 (17:32 -0500)]
bcachefs: Plumb saw_error through to btree_err()

The btree node read path has the ability to kick off an asynchronous
btree node rewrite if we saw and corrected an error. Previously this was
only used for errors that caused one of the replicas to be unusable -
this patch plumbs it through to all error paths, so that normal fsck
errors can be corrected.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Convert btree_err() to a function
Kent Overstreet [Tue, 3 Jan 2023 22:14:07 +0000 (17:14 -0500)]
bcachefs: Convert btree_err() to a function

This makes the code more readable, and reduces text size by 8 kb.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fix fsck error
Kent Overstreet [Sun, 25 Dec 2022 20:31:27 +0000 (15:31 -0500)]
bcachefs: fix fsck error

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Make log message at startup a bit cleaner
Kent Overstreet [Wed, 21 Dec 2022 00:27:02 +0000 (19:27 -0500)]
bcachefs: Make log message at startup a bit cleaner

Don't print out opts= if no options have been specified.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Log more messages in the journal
Kent Overstreet [Wed, 14 Dec 2022 15:39:04 +0000 (10:39 -0500)]
bcachefs: Log more messages in the journal

This patch

 - Adds a mechanism for queuing up journal entries prior to the journal
   being started, which will be used for early journal log messages

 - Adds bch2_fs_log_msg() and improves bch2_trans_log_msg(), which now
   take format strings. bch2_fs_log_msg() can be used before or after
   the journal has been started, and will use the appropriate mechanism.

 - Deletes the now obsolete bch2_journal_log_msg()

 - And adds more log messages to the recovery path - messages for
   journal/filesystem started, journal entries being blacklisted, and
   journal replay starting/finishing.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_btree_trans_to_text(): print blocked time
Kent Overstreet [Thu, 5 Jan 2023 02:34:41 +0000 (21:34 -0500)]
bcachefs: bch2_btree_trans_to_text(): print blocked time

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix some memcpy() warnings
Kent Overstreet [Wed, 28 Dec 2022 20:17:07 +0000 (15:17 -0500)]
bcachefs: Fix some memcpy() warnings

With CONFIG_FORTIFY_SOURCE, the compiler attempts to warn about mempcys
that extend past struct field boundaries. This results in some spurious
warnings where we use embedded variable length structs, this patch
switches to unsafe_mecpy() to fix the warnings.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Be less restrictive when validating journal overwrite entries
Kent Overstreet [Wed, 21 Dec 2022 00:43:41 +0000 (19:43 -0500)]
bcachefs: Be less restrictive when validating journal overwrite entries

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch2_journal_flush_device_pins()
Kent Overstreet [Mon, 7 Mar 2022 06:35:55 +0000 (01:35 -0500)]
bcachefs: Fix bch2_journal_flush_device_pins()

It's now legal for the pin fifo to be empty, which means this code needs
to be updated in order to not hit an assert.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
18 months agobcachefs: Fix for long running btree transactions & key cache
Kent Overstreet [Fri, 16 Dec 2022 02:44:32 +0000 (21:44 -0500)]
bcachefs: Fix for long running btree transactions & key cache

While a btree transaction is running, we hold a SRCU read lock on the
btree key cache that prevents btree key cache keys from being freed -
this is so that relock() operations won't access freed memory.

The downside of this is that long running btree transactions prevent
memory from being freed from the key cache. This adds a check in
bch2_trans_begin() - if the transaction has been running longer than 1
second, drop and retake the SRCU read lock and zero out pointers to
unlock key cache paths.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add a missing bch2_err_str() call
Kent Overstreet [Fri, 23 Dec 2022 01:51:02 +0000 (20:51 -0500)]
bcachefs: Add a missing bch2_err_str() call

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add some unlikely() annotations
Kent Overstreet [Thu, 24 Nov 2022 01:11:46 +0000 (20:11 -0500)]
bcachefs: Add some unlikely() annotations

Add a few easy unlikely() optimizations. These are mainly worthwhile
because the compiler will (usually) put the branch-not-taken path at the
end of the function, meaning better icache utilization.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: New btree helpers
Kent Overstreet [Thu, 24 Nov 2022 03:13:19 +0000 (22:13 -0500)]
bcachefs: New btree helpers

This introduces some new conveniences, to help cut down on boilerplate:

 - bch2_trans_kmalloc_nomemzero() - performance optimiation
 - bch2_bkey_make_mut()
 - bch2_bkey_get_mut()
 - bch2_bkey_get_mut_typed()
 - bch2_bkey_alloc()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Allow for more btrees
Kent Overstreet [Sat, 3 Dec 2022 01:36:06 +0000 (20:36 -0500)]
bcachefs: Allow for more btrees

Expand some bitfields so we can keep adding more btrees.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Recover from blacklisted journal entries
Kent Overstreet [Wed, 14 Dec 2022 19:47:42 +0000 (14:47 -0500)]
bcachefs: Recover from blacklisted journal entries

If it so happens that we crash while dirty, meaning we don't have the
superblock clean section, and we erroneously mark a journal entry we
wrote as blacklisted, we won't be able to recover.

This patch fixes this by adding a fallback: if we've got no superblock
clean section, and no non-ignored journal entries, we try the most
recent ignored journal entry.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix btree_gc when multiple passes required
Kent Overstreet [Tue, 13 Dec 2022 19:43:03 +0000 (14:43 -0500)]
bcachefs: Fix btree_gc when multiple passes required

We weren't resetting filesystem & device usage when restarting gc, which
was spotted when free bucket counters overflowed - whoops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix error path in bch2_trans_commit_write_locked()
Kent Overstreet [Tue, 13 Dec 2022 23:19:30 +0000 (18:19 -0500)]
bcachefs: Fix error path in bch2_trans_commit_write_locked()

Previously, we were journalling extra_journal_entries (which is used for
new btree roots, and irreversably mutates system state) before calling
bch2_trans_fs_usage_apply(), which can fail - whoops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_trans_revalidate_updates_in_node()
Kent Overstreet [Wed, 23 Nov 2022 23:46:03 +0000 (18:46 -0500)]
bcachefs: bch2_trans_revalidate_updates_in_node()

When we started stashing the key being overwritten in
btree_insert_entry, this introduced a typical iterator invalidation
problem, triggered by btree node splits or resorts.

Previously, dealt with this by unconditionally re-validating those
stashed pointers in the transaction commit path. This patch gets rid of
that by doing it only when needed, in bch2_trans_node_add() or
bch2_trans_node_reinit_iter().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bkey_min(), bkey_max()
Kent Overstreet [Fri, 25 Nov 2022 20:01:36 +0000 (15:01 -0500)]
bcachefs: bkey_min(), bkey_max()

Parallel to bpos_min(), bpos_max() - trivial refactoring.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: More errcode cleanup
Kent Overstreet [Sun, 20 Nov 2022 03:39:08 +0000 (22:39 -0500)]
bcachefs: More errcode cleanup

We shouldn't be overloading standard error codes now that we have
provisions for bcachefs-specific errorcodes: this patch converts super.c
and super-io.c to per error site errcodes, with a bit of cleanup.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Suppress -EROFS messages when shutting down
Kent Overstreet [Wed, 12 Oct 2022 20:11:31 +0000 (16:11 -0400)]
bcachefs: Suppress -EROFS messages when shutting down

This isn't actually an error condition, this just indicates a normal
shutdown - no reason for these to be in the log.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add a missing bch2_btree_path_traverse() call
Kent Overstreet [Fri, 9 Dec 2022 21:22:36 +0000 (16:22 -0500)]
bcachefs: Add a missing bch2_btree_path_traverse() call

bch2_btree_iter_peek_upto() in snapshots mode may need to keep a
btree_path for the insert position, not just the position of the key
we're returning. The code was incorrectly assuming this would be in the
same btree node - we were missing a bch2_btree_path_traverse() call.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch2_journal_keys_peek_upto()
Kent Overstreet [Fri, 9 Dec 2022 19:09:14 +0000 (14:09 -0500)]
bcachefs: Fix bch2_journal_keys_peek_upto()

bch2_journal_keys_peek_upto() was comparing against btree_id & level
incorrectly - fix this by using __journal_key_cmp().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill btree_insert_ret enum
Kent Overstreet [Wed, 7 Dec 2022 16:39:34 +0000 (11:39 -0500)]
bcachefs: Kill btree_insert_ret enum

Replace with standard bcachefs-private error codes.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a btree iter assertion pop
Kent Overstreet [Fri, 2 Dec 2022 17:45:37 +0000 (12:45 -0500)]
bcachefs: Fix a btree iter assertion pop

This fixes a (harmless) broken invariant in __bch2_btree_path_set_pos():
iterators to interior nodes should point to the first non whiteout.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Simplify journal read path
Kent Overstreet [Fri, 2 Dec 2022 02:59:25 +0000 (21:59 -0500)]
bcachefs: Simplify journal read path

This just cleans up and simplifies the code that decides where to resume
writing in the journal - when the code was originally written we weren't
saving the precise location of every journal write found.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a "no journal entries found" bug
Kent Overstreet [Fri, 2 Dec 2022 16:45:58 +0000 (11:45 -0500)]
bcachefs: Fix a "no journal entries found" bug

On startup, we need to ensure the first journal entry written is a flush
write: after a clean shutdown we generally don't read the journal, which
means we might be overwriting whatever was there previously, and there
must always be at least one flush entry in the journal or recovery will
fail.

Found by fstests generic/388.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Don't error out when just reading the journal
Kent Overstreet [Thu, 1 Dec 2022 16:17:18 +0000 (11:17 -0500)]
bcachefs: Don't error out when just reading the journal

This tweaks the recovery and journal paths so that we don't error out
before we need to: the list_journal command should work, even if we
wouldn't be able to replay successfully.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a livelock in key cache fill path
Kent Overstreet [Wed, 25 Jan 2023 15:15:39 +0000 (10:15 -0500)]
bcachefs: Fix a livelock in key cache fill path

We weren't setting path->uptodate before calling
bch2_btree_key_cache_fill() - which causes __bch2_btree_path_upgrade()
to fail.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Key cache now works for snapshots btrees
Kent Overstreet [Wed, 23 Nov 2022 01:15:33 +0000 (20:15 -0500)]
bcachefs: Key cache now works for snapshots btrees

This switches btree_key_cache_fill() to use a btree iterator, not a
btree path, so that it can search for keys in previous snapshots.

We also add another iterator flag, BTREE_ITER_KEY_CACHE_FILL, to avoid
recursion back into the key cache.

This will allow us to re-enable the key cache for inodes in the next
patch.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Bring back BTREE_ITER_CACHED_NOFILL
Kent Overstreet [Tue, 20 Dec 2022 16:26:57 +0000 (11:26 -0500)]
bcachefs: Bring back BTREE_ITER_CACHED_NOFILL

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill __btree_trans_peek_key_cache()
Kent Overstreet [Tue, 20 Dec 2022 21:02:09 +0000 (16:02 -0500)]
bcachefs: Kill __btree_trans_peek_key_cache()

There was no reason for this to be a separate helper - we always want
the relock call that btree_trans_peek_key_cache() did.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_btree_path_peek_slot_exact()
Kent Overstreet [Tue, 20 Dec 2022 16:13:19 +0000 (11:13 -0500)]
bcachefs: bch2_btree_path_peek_slot_exact()

When we start using the key cache for inodes again, it'll be possible
for bch2_btree_path_peek_slot() to return a key in a different snapshot
with a key cache path.

This isn't what we want when triggers are checking what they're
overwriting, so introduce a new helper for the commit path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix __btree_trans_peek_key_cache()
Kent Overstreet [Tue, 20 Dec 2022 15:51:20 +0000 (10:51 -0500)]
bcachefs: Fix __btree_trans_peek_key_cache()

We were returning a pointer to a variable on the stack - oops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: New bpos_cmp(), bkey_cmp() replacements
Kent Overstreet [Thu, 24 Nov 2022 08:12:22 +0000 (03:12 -0500)]
bcachefs: New bpos_cmp(), bkey_cmp() replacements

This patch introduces
 - bpos_eq()
 - bpos_lt()
 - bpos_le()
 - bpos_gt()
 - bpos_ge()

and equivalent replacements for bkey_cmp().

Looking at the generated assembly these could probably be improved
further, but we already see a significant code size improvement with
this patch.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: New magic number
Kent Overstreet [Sat, 3 Dec 2022 00:46:49 +0000 (19:46 -0500)]
bcachefs: New magic number

Add a new bcachefs-specific magic number for the superblock, instead of
continuing to use the old bcache magic number3

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: extents no longer require special handling for packing
Kent Overstreet [Sat, 26 Nov 2022 09:36:56 +0000 (04:36 -0500)]
bcachefs: extents no longer require special handling for packing

Extent overwrite used to  be handled differently, underneath the
journaling layer and within the core btree code. This imposed
restrictions on bkey packing/packed formats, which no longer apply.

This patch deletes those restrictions.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix BCH_IOCTL_DISK_SET_STATE
Kent Overstreet [Fri, 25 Nov 2022 23:29:36 +0000 (18:29 -0500)]
bcachefs: Fix BCH_IOCTL_DISK_SET_STATE

 - Ensure we print an error message if necessary.

   Ideally we'd return the precise error code to userspace and leave
   printing the error message to the userspace tool, but we haven't
   decided to make our private error codes ABI-stable yet.

 - Return standard error code to userspace

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Don't set accessed bit on btree node fill
Kent Overstreet [Fri, 25 Nov 2022 21:04:42 +0000 (16:04 -0500)]
bcachefs: Don't set accessed bit on btree node fill

Btree nodes shouldn't have their accessed bit set when entering the
btree cache by being read in from disk - this fixes linear scans
thrashing the cache.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix an include
Kent Overstreet [Fri, 25 Nov 2022 21:15:14 +0000 (16:15 -0500)]
bcachefs: Fix an include

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill BCH_FEATURE_incompressible
Kent Overstreet [Fri, 25 Nov 2022 17:56:49 +0000 (12:56 -0500)]
bcachefs: Kill BCH_FEATURE_incompressible

This isn't needed anymore, we only support metadata versions that have
this.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Better inlining in bch2_time_stats_update()
Kent Overstreet [Wed, 23 Nov 2022 23:23:48 +0000 (18:23 -0500)]
bcachefs: Better inlining in bch2_time_stats_update()

Move the actual slowpath off into a new function -
bch2_time_stats_clear_buffer() - and inline
bch2_time_stats_update_one().

Alo, use the new inlined update functions from mean_and_variance.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Optimize bch2_trans_iter_init()
Kent Overstreet [Fri, 25 Nov 2022 05:40:27 +0000 (00:40 -0500)]
bcachefs: Optimize bch2_trans_iter_init()

When flags & btree_id are constants, we can constant fold the entire
calculation of the actual iterator flags - and the whole thing becomes
small enough to inline.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: More dio inlining
Kent Overstreet [Fri, 25 Nov 2022 04:52:28 +0000 (23:52 -0500)]
bcachefs: More dio inlining

Eliminate another function call in the O_DIRECT write path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill some unneeded references to c->flags
Kent Overstreet [Wed, 23 Nov 2022 23:51:27 +0000 (18:51 -0500)]
bcachefs: Kill some unneeded references to c->flags

This drops some unneeded references to JOURNAL_REPLAY_DONE in c->flags:
we're already mirroring it in btree_trans, we just weren't using it
consistently.

We may want to do this with more flags:
  btree_iter.c:   unsigned nr = test_bit(BCH_FS_STARTED, &c->flags)
  btree_update_leaf.c:    if (unlikely(!test_bit(BCH_FS_MAY_GO_RW, &c->flags))) {

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve bch2_inode_opts_to_opts()
Kent Overstreet [Thu, 24 Nov 2022 01:28:15 +0000 (20:28 -0500)]
bcachefs: Improve bch2_inode_opts_to_opts()

It turns out the *_defined entries of bch_io_opts are only used in one
place - in the xattr get path - and there we immediately convert to a
bch_opts struct, which also has the *_defined entries.

This patch changes bch2_inode_opts_to_opts() to go directly from
bch_inode_unpacked to bch_opts, which is a minor simplification and will
also let us slim down struct bch_io_opts in another patch.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Better inlining in bch2_subvolume_get_snapshot()
Kent Overstreet [Wed, 23 Nov 2022 23:22:59 +0000 (18:22 -0500)]
bcachefs: Better inlining in bch2_subvolume_get_snapshot()

This provides an inlined version of bch2_subvolume_get() and uses it in
bch2_subvolume_get_snapshot(), since this is the version that's used all
over the place and in fast paths (e.g. IO paths).

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Inline bch2_bkey_format_add_key()
Kent Overstreet [Thu, 24 Nov 2022 08:38:31 +0000 (03:38 -0500)]
bcachefs: Inline bch2_bkey_format_add_key()

This is only called in two places, and when it's used we use it in a
tight loop - it's definitely worth inlining.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Tiny bch2_trans_update_by_path_trace() optimization
Kent Overstreet [Wed, 23 Nov 2022 04:47:22 +0000 (23:47 -0500)]
bcachefs: Tiny bch2_trans_update_by_path_trace() optimization

This just removes a redundant comparison - there's more work we could do
here to remove some redundant copying.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Move some asserts behind CONFIG_BCACHEFS_DEBUG
Kent Overstreet [Wed, 23 Nov 2022 03:06:04 +0000 (22:06 -0500)]
bcachefs: Move some asserts behind CONFIG_BCACHEFS_DEBUG

Convert some non-critical asserts in long-stable code to debug asserts.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Split out __bch2_btree_node_get()
Kent Overstreet [Wed, 23 Nov 2022 03:05:45 +0000 (22:05 -0500)]
bcachefs: Split out __bch2_btree_node_get()

Standard splitting out of the slow path from the fast path of a
function. We may follow this up in another patch with inlining the fast
path into btree_iter.c.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Handle last journal write being torn
Kent Overstreet [Sun, 20 Nov 2022 02:40:35 +0000 (21:40 -0500)]
bcachefs: Handle last journal write being torn

If the last journal write didn't complete sucessfully due to a torn
write, we'll detect it as a checksum error. In that case, we should just
pretend that journal entry was never written.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve journal_read() logging
Kent Overstreet [Sun, 20 Nov 2022 02:20:58 +0000 (21:20 -0500)]
bcachefs: Improve journal_read() logging

Print out the journal entries we read and will replay as soon as
possible - if we get an error walidating keys it's helpful to know where
it was in the journal.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a transaction path overflow
Kent Overstreet [Fri, 18 Nov 2022 00:07:40 +0000 (19:07 -0500)]
bcachefs: Fix a transaction path overflow

It turns out we need bch2_extent_trim_atomi() even when we're deleting
extents one at a time because it's possible for one reflink_p to
reference arbitrarily many reflink_v extents. This doesn't normally
happen, but the data move path can fragment existing extents in the
background.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a race with b->write_type
Kent Overstreet [Thu, 17 Nov 2022 21:03:15 +0000 (16:03 -0500)]
bcachefs: Fix a race with b->write_type

b->write_type needs to be set atomically with setting the
btree_node_need_write flag, so move it into b->flags.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Error message improvement
Kent Overstreet [Wed, 16 Nov 2022 01:25:08 +0000 (20:25 -0500)]
bcachefs: Error message improvement

 - Centralize format strings in bcachefs.h
 - Add bch2_fmt_inum_offset() and related helpers
 - Switch error messages for inodes to also print out the offset, in
   bytes

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve a few warnings
Kent Overstreet [Tue, 15 Nov 2022 20:57:07 +0000 (15:57 -0500)]
bcachefs: Improve a few warnings

Warnings ought to always have a format string/log message - makes them
considerably more useful.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix for_each_btree_key2()
Kent Overstreet [Wed, 16 Nov 2022 04:17:55 +0000 (23:17 -0500)]
bcachefs: Fix for_each_btree_key2()

Previously, when we exited from the loop body with a break statement
_ret wouldn't have been assigned to yet, and we could spuriously return
a transaction restart error.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Btree split improvement
Kent Overstreet [Wed, 16 Nov 2022 02:52:12 +0000 (21:52 -0500)]
bcachefs: Btree split improvement

This improves the bkey_format calculation when splitting btree nodes.
Previously, we'd use a format calculated for the original node for the
lower of the two new nodes.

This was particularly bad on sequential insertions, where we iteratively
split the last btree node, whos format has to include KEY_MAX.

Now, we calculate formats precisely for the keys the two new nodes will
contain. This also should make splitting a bit more efficient, since
we're only copying keys once (from the original node to the new node,
instead of new node, replacement node, then upper split).

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix return code from btree_path_traverse_one()
Kent Overstreet [Wed, 16 Nov 2022 03:48:03 +0000 (22:48 -0500)]
bcachefs: Fix return code from btree_path_traverse_one()

trans->restarted is a positive error code, not the usual negative

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Minor dio write path improvements
Kent Overstreet [Mon, 14 Nov 2022 03:43:37 +0000 (22:43 -0500)]
bcachefs: Minor dio write path improvements

This switches where we take quota reservations to be per bch_wirte_op
instead of per dio_write, so we can drop the quota reservation in the
same place as we call i_sectors_acct(), and only take/release
ei_quota_lock once.

In the future we'd like ei_quota_lock to not be a mutex, so that we can
avoid punting to process context before deliving write completions in
nocow mode.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Quota: Don't allocate memory under lock
Kent Overstreet [Mon, 14 Nov 2022 03:35:55 +0000 (22:35 -0500)]
bcachefs: Quota: Don't allocate memory under lock

The genradix code can handle multiple threads trying to allocate at the
same time - we don't need the genradix_ptr_alloc() call to happen under
a lock.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a use after free
Kent Overstreet [Mon, 14 Nov 2022 07:22:30 +0000 (02:22 -0500)]
bcachefs: Fix a use after free

This fixes a regression from percpu freedlists in the btree key cache
code: in a rare error path, we were immediately freeing a bkey_cached
that had been used before and should've waited for an SRCU barrier.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Delete atomic_inc_bug()
Kent Overstreet [Mon, 14 Nov 2022 01:53:24 +0000 (20:53 -0500)]
bcachefs: Delete atomic_inc_bug()

These were wrappers around atomic operations that verified that the
counter wasn't negative, but they're dead code - delete.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fixes for building in userspace
Kent Overstreet [Mon, 14 Nov 2022 01:01:42 +0000 (20:01 -0500)]
bcachefs: Fixes for building in userspace

 - Marking a non-static function as inline doesn't actually work and is
   now causing problems - drop that

 - Introduce BCACHEFS_LOG_PREFIX for when we want to prefix log messages
   with bcachefs (filesystem name)

 - Userspace doesn't have real percpu variables (maybe we can get this
   fixed someday), put an #ifdef around bch2_disk_reservation_add()
   fastpath

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Factor out two_state_shared_lock
Kent Overstreet [Fri, 4 Nov 2022 17:25:57 +0000 (13:25 -0400)]
bcachefs: Factor out two_state_shared_lock

We have a unique lock used for controlling adding to the pagecache: the
lock has two states, where both states are shared - the lock may be held
multiple times for either state - but not both states at the same time.

This is exactly what we need for nocow mode locking, so this patch pulls
it out of fs.c into its own file.

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