linux-block.git
10 months agobcachefs: Topology repair fixes
Kent Overstreet [Thu, 7 Apr 2022 21:41:02 +0000 (17:41 -0400)]
bcachefs: Topology repair fixes

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

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

This gets us better error messages.

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

.key_invalid is a better place for this assertion.

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

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

This also fixes an extra trailing newline.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Trivial cleanup.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Additionally, discards are now enabled by default.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: Run overwrite triggers before insert
Kent Overstreet [Thu, 31 Mar 2022 03:39:48 +0000 (23:39 -0400)]
bcachefs: Run overwrite triggers before insert

For backpointers, we'll need to delete old backpointers before adding
new backpointers - otherwise we'll run into spurious duplicate
backpointer errors.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Move deletion of refcount=0 indirect extents to their triggers
Kent Overstreet [Thu, 31 Mar 2022 04:03:37 +0000 (00:03 -0400)]
bcachefs: Move deletion of refcount=0 indirect extents to their triggers

For backpointers, we need to switch the order triggers are run in: we
need to run triggers for deletions/overwrites before triggers for
inserts.

To avoid breaking the reflink triggers, this patch moves deleting of
indirect extents with refcount=0 to their triggers, instead of doing it
when we update those keys.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Improve bch2_bkey_ptrs_to_text()
Kent Overstreet [Thu, 31 Mar 2022 03:40:19 +0000 (23:40 -0400)]
bcachefs: Improve bch2_bkey_ptrs_to_text()

Print bucket:offset when the filesystem is online; this makes debugging
easier when correlating with alloc updates.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: bch2_trans_log_msg()
Kent Overstreet [Wed, 30 Mar 2022 19:44:12 +0000 (15:44 -0400)]
bcachefs: bch2_trans_log_msg()

Add a new helper for logging messages to the journal - a new debugging
tool, an alternative to trace_printk().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Use darray for extra_journal_entries
Kent Overstreet [Tue, 29 Mar 2022 20:29:10 +0000 (16:29 -0400)]
bcachefs: Use darray for extra_journal_entries

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: btree_path_make_mut() clears should_be_locked
Kent Overstreet [Wed, 30 Mar 2022 17:47:07 +0000 (13:47 -0400)]
bcachefs: btree_path_make_mut() clears should_be_locked

This fixes a bug where __bch2_btree_node_update_key() wasn't clearing
should_be_locked, leading to bch2_btree_path_traverse() always failing -
all callers of btree_path_make_mut() want should_be_locked cleared, so
do it there.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Add a missing btree_path_set_dirty() calls
Kent Overstreet [Wed, 30 Mar 2022 17:10:03 +0000 (13:10 -0400)]
bcachefs: Add a missing btree_path_set_dirty() calls

bch2_btree_iter_next_node() was mucking with other btree_path state
without setting path->update to be consistent with the fact that the
path is very much no longer uptodate - oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix error path in bch2_snapshot_set_equiv()
Kent Overstreet [Mon, 28 Mar 2022 20:31:26 +0000 (16:31 -0400)]
bcachefs: Fix error path in bch2_snapshot_set_equiv()

We weren't properly catching errors from snapshot_live() - oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Work around a journal self-deadlock
Kent Overstreet [Mon, 28 Mar 2022 20:21:26 +0000 (16:21 -0400)]
bcachefs: Work around a journal self-deadlock

bch2_journal_space_available -> bch2_journal_halt() self deadlocks on
journal lock; work around this by dropping/retaking journal lock before
we call bch2_fatal_error().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Heap code fix
Kent Overstreet [Mon, 28 Mar 2022 16:31:22 +0000 (12:31 -0400)]
bcachefs: Heap code fix

When deleting an entry from a heap that was at entry h->used - 1, we'd
end up calling heap_sift() on an entry outside the heap - the entry we
just removed - which would end up re-adding it to the heap and deleting
something we didn't want to delete. Oops...

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix an unitialized var warning in userspace
Kent Overstreet [Mon, 21 Mar 2022 23:34:48 +0000 (19:34 -0400)]
bcachefs: Fix an unitialized var warning in userspace

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Add printf format attribute to bch2_pr_buf()
Kent Overstreet [Mon, 21 Mar 2022 22:05:39 +0000 (18:05 -0400)]
bcachefs: Add printf format attribute to bch2_pr_buf()

This tells the compiler to check printf format strings, and catches a
few bugs.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Reset journal flush delay to default value if zeroed
Kent Overstreet [Mon, 21 Mar 2022 07:03:03 +0000 (03:03 -0400)]
bcachefs: Reset journal flush delay to default value if zeroed

We've been seeing a very strange bug where journal flush & reclaim delay
end up getting inexplicably zeroed, in the superblock. We're now
validating all the options in bch2_validate_super(), and 0 is no longer
a valid value for those options, but we need to be careful not to
prevent people's filesystems from mounting because of the new
validation.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Change journal_io.c assertion to error message
Kent Overstreet [Mon, 21 Mar 2022 00:12:53 +0000 (20:12 -0400)]
bcachefs: Change journal_io.c assertion to error message

Something funny is going on with the new code for restoring the journal
write point, and it's hard to reproduce.

We do want to debug this because resuming writing to the journal in the
wrong spot could be something serious. For now, replace the assertion
with an error message and revert to old behaviour when it happens.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Make minimum journal_flush_delay nonzero
Kent Overstreet [Mon, 21 Mar 2022 04:27:10 +0000 (00:27 -0400)]
bcachefs: Make minimum journal_flush_delay nonzero

We're seeing a very strange bug where journal_flush_delay sometimes gets
set to 0 in the superblock. Together with the preceding patch, this
should help us track it down.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Better superblock opt validation
Kent Overstreet [Mon, 21 Mar 2022 04:15:38 +0000 (00:15 -0400)]
bcachefs: Better superblock opt validation

This moves validation of superblock options to bch2_sb_validate(), so
they'll be checked in the write path as well.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: x-macro metadata version enum
Kent Overstreet [Mon, 21 Mar 2022 03:34:11 +0000 (23:34 -0400)]
bcachefs: x-macro metadata version enum

Now we've got strings for metadata versions - this changes
bch2_sb_to_text() and our mount log message to use it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix large key cache keys
Kent Overstreet [Fri, 18 Mar 2022 01:35:51 +0000 (21:35 -0400)]
bcachefs: Fix large key cache keys

Previously, we'd go into an infinite loop when attempting to cache a
bkey in the key cache larger than 128 u64s - since we were only using a
u8 for the size field, it'd get rounded up to 256 then truncated to 0.
Oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Convert some WARN_ONs to WARN_ON_ONCE
Kent Overstreet [Thu, 17 Mar 2022 00:31:15 +0000 (20:31 -0400)]
bcachefs: Convert some WARN_ONs to WARN_ON_ONCE

These warnings are symptomatic of something else going wrong, we don't
want them spamming up the logs as that'll make it harder to find the
real issue.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Restore journal write point at startup
Kent Overstreet [Tue, 15 Mar 2022 06:41:21 +0000 (02:41 -0400)]
bcachefs: Restore journal write point at startup

This patch tweaks the journal recovery path so that we start writing
right after where we left off, instead of the next empty bucket. This is
partly prep work for supporting zoned devices, but it's also good to do
in general to avoid the journal completely filling up and getting stuck.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: darrays
Kent Overstreet [Tue, 29 Mar 2022 19:48:45 +0000 (15:48 -0400)]
bcachefs: darrays

Inspired by CCAN darray - simple, stupid resizable (dynamic) arrays.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix BTREE_TRIGGER_WANTS_OLD_AND_NEW
Kent Overstreet [Sun, 13 Mar 2022 05:30:16 +0000 (00:30 -0500)]
bcachefs: Fix BTREE_TRIGGER_WANTS_OLD_AND_NEW

BTREE_TRIGGER_WANTS_OLD_AND_NEW didn't work correctly when the old and
new key were both alloc keys, but different versions - it required old
and new key type to be identical, and this bug is a problem for the new
allocator rewrite.

This patch fixes it by checking if the old and new key have the same
trigger functions - the different versions of alloc (and inode) keys
have the same trigger functions.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Move trigger fns to bkey_ops
Kent Overstreet [Sun, 13 Mar 2022 05:26:52 +0000 (00:26 -0500)]
bcachefs: Move trigger fns to bkey_ops

This replaces the switch statements in bch2_mark_key(),
bch2_trans_mark_key() with new bkey methods - prep work for the next
patch, which fixes BTREE_TRIGGER_WANTS_OLD_AND_NEW.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Revalidate pointer to old bkey val before calling mem triggers
Kent Overstreet [Sat, 12 Mar 2022 21:14:55 +0000 (16:14 -0500)]
bcachefs: Revalidate pointer to old bkey val before calling mem triggers

We recently started stashing a copy of the key being overwritten in
btree_insert_entry: this is helpful for avoiding multiple calls to
bch2_btree_path_peek_slot() and bch2_journal_keys_peek() in the
transaction commit path.

But it turns out this has a problem - when we run mem/atomic triggers,
we've done a couple things that can invalidate the pointer to the old
key's value. This makes the optimization of stashing a pointer to the
old value questionable, but for now this patch revalidates that pointer
before running mem triggers.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: bch2_trans_updates_to_text()
Kent Overstreet [Fri, 11 Mar 2022 23:38:24 +0000 (18:38 -0500)]
bcachefs: bch2_trans_updates_to_text()

This turns bch2_dump_trans_updates() into a to_text() method - this way
it can be used by debug tracing.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: bch2_trans_inconsistent()
Kent Overstreet [Thu, 3 Mar 2022 03:18:56 +0000 (22:18 -0500)]
bcachefs: bch2_trans_inconsistent()

Add a new error macro that also dumps transaction updates in addition to
doing an emergency shutdown - when a transaction update discovers or is
causing a fs inconsistency, it's helpful to see what updates it was
doing.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Drop !did_work path from do_btree_insert_one()
Kent Overstreet [Fri, 11 Mar 2022 23:16:42 +0000 (18:16 -0500)]
bcachefs: Drop !did_work path from do_btree_insert_one()

As we've already reserved space in the journal this optimization doesn't
actually buy us anything, and when doing list_journal debugging it
deletes information we want.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: bch2_btree_iter_peek_upto()
Kent Overstreet [Fri, 11 Mar 2022 17:31:52 +0000 (12:31 -0500)]
bcachefs: bch2_btree_iter_peek_upto()

In BTREE_ITER_FILTER_SNAPHOTS mode, we skip over keys in unrelated
snapshots. When we hit the end of an inode, if the next inode(s) are in
a different subvolume, we could potentially have to skip past many keys
before finding a key we can return to the caller, so they can terminate
the iteration.

This adds a peek_upto() variant to solve this problem, to be used when
we know the range we're searching within.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Delay setting path->should_be_locked
Kent Overstreet [Fri, 11 Mar 2022 04:22:49 +0000 (23:22 -0500)]
bcachefs: Delay setting path->should_be_locked

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: Add a missing wakeup
Kent Overstreet [Thu, 10 Mar 2022 22:35:06 +0000 (17:35 -0500)]
bcachefs: Add a missing wakeup

This fixes a rare bug with bch2_btree_flush_all_writes() getting stuck.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Allocate journal buckets sequentially
Kent Overstreet [Thu, 10 Mar 2022 20:49:03 +0000 (15:49 -0500)]
bcachefs: Allocate journal buckets sequentially

This tweaks __bch2_set_nr_journal_buckets() so that we aren't reversing
their order in the jorunal anymore - nice for rotating disks.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: bch2_journal_log_msg()
Kent Overstreet [Thu, 10 Mar 2022 19:25:16 +0000 (14:25 -0500)]
bcachefs: bch2_journal_log_msg()

This adds bch2_journal_log_msg(), which just logs a message to the
journal, and uses it to mark startup and when journal replay finishes.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Change flags param to bch2_btree_delete_range to update_flags
Kent Overstreet [Sat, 5 Mar 2022 23:23:47 +0000 (18:23 -0500)]
bcachefs: Change flags param to bch2_btree_delete_range to update_flags

It wasn't used as iter_flags (excepting the unit tests, which this patch
fixes), and the next patch is going to need to pass in
BTREE_TRIGGER_NORUN.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix lock ordering under traverse_all()
Kent Overstreet [Mon, 7 Mar 2022 02:17:43 +0000 (21:17 -0500)]
bcachefs: Fix lock ordering under traverse_all()

traverse_all() traverses btree paths in sorted order, so it should never
see transaction restarts due to lock ordering violations. But some code
in __bch2_btree_path_upgrade(), while necessary when not running under
traverse_all(), was causing some confusing lock ordering violations -
disabling this code under traverse_all() will let us put in some more
assertions.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix error handling in traverse_all()
Kent Overstreet [Tue, 8 Mar 2022 03:05:49 +0000 (22:05 -0500)]
bcachefs: Fix error handling in traverse_all()

In btree_path_traverse_all() we were failing to check for -EIO in the
retry loop, and after btree node read error we'd go into an infinite
loop.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix dio write path with loopback dio mode
Kent Overstreet [Wed, 9 Mar 2022 20:37:42 +0000 (15:37 -0500)]
bcachefs: Fix dio write path with loopback dio mode

When the iov_iter is a bvec iter, it's possible the IO was submitted
from a kthread that didn't have an mm to switch to.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Use bio_iov_vecs_to_alloc()
Kent Overstreet [Tue, 8 Mar 2022 18:52:58 +0000 (13:52 -0500)]
bcachefs: Use bio_iov_vecs_to_alloc()

This fixes a bug in the DIO read path where, when using a loopback
device in DIO mode, we'd allocate a biovec that would get overwritten
and leaked in bio_iov_iter_get_pages() -> bio_iov_bvec_set().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Revert UUID format-specifier change
Kent Overstreet [Mon, 7 Mar 2022 19:13:22 +0000 (14:13 -0500)]
bcachefs: Revert UUID format-specifier change

"bcachefs: Log & error message improvements" accidentally changed the
format specifier we use for converting UUIDs to strings, which broke
mounting of encrypted filesystems - this patch reverts that change.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Skip periodic wakeup of journal reclaim when journal empty
Kent Overstreet [Sun, 6 Mar 2022 22:20:39 +0000 (17:20 -0500)]
bcachefs: Skip periodic wakeup of journal reclaim when journal empty

Less system noise.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Check for rw before setting opts via sysfs
Kent Overstreet [Sun, 6 Mar 2022 20:15:41 +0000 (15:15 -0500)]
bcachefs: Check for rw before setting opts via sysfs

This isn't a correctness issue, it just eliminates errors in the dmesg
log when we're RO.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix pr_tab_rjust()
Kent Overstreet [Sun, 6 Mar 2022 19:04:34 +0000 (14:04 -0500)]
bcachefs: Fix pr_tab_rjust()

pr_tab_rjust() was broken and leaving a null somewhere in the output
string - this patch fixes it and simplifies it a bit.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Don't keep around btree_paths unnecessarily
Kent Overstreet [Sat, 5 Mar 2022 20:21:07 +0000 (15:21 -0500)]
bcachefs: Don't keep around btree_paths unnecessarily

When bch2_trans_begin() is called and there hasn't been a transaction
restart, we presume that we're now doing something new - iterating over
different keys, and we now shouldn't keep aruond paths related to the
previous transaction, excepting the subvolumes btree.

This should fix some of our "transaction path overflow" bugs.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Don't arm journal->write_work when journal entry !open
Kent Overstreet [Sat, 5 Mar 2022 18:38:54 +0000 (13:38 -0500)]
bcachefs: Don't arm journal->write_work when journal entry !open

This fixes a shutdown race where we were rearming journal->write_work
after the journal has already shut down.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Convert bch2_sb_to_text to master option list
Kent Overstreet [Sat, 5 Mar 2022 17:01:16 +0000 (12:01 -0500)]
bcachefs: Convert bch2_sb_to_text to master option list

Options no longer have to be manually added to bch2_sb_to_text() - it
now uses the master list of options in opts.h. Also, improve some of the
formatting by converting it to tabstops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: Fix transaction path overflow in fiemap
Kent Overstreet [Sat, 5 Mar 2022 02:57:11 +0000 (21:57 -0500)]
bcachefs: Fix transaction path overflow in fiemap

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>
10 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>