linux-2.6-microblaze.git
11 months agobcachefs: Flush write buffer as needed in backpointers repair
Kent Overstreet [Sat, 25 Feb 2023 10:22:37 +0000 (05:22 -0500)]
bcachefs: Flush write buffer as needed in backpointers repair

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix for shared paths in write buffer flush
Kent Overstreet [Sun, 26 Feb 2023 20:48:39 +0000 (15:48 -0500)]
bcachefs: Fix for shared paths in write buffer flush

It's possible for bch2_write_buffer_flush_one() to end up with a shared
path, if called from a context that already has a btree iterator
pointing to a key being flushed. We have to be careful when that
happens, since we can't clone a path that holds write locks.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Single open_bucket_partial list
Kent Overstreet [Sat, 25 Feb 2023 05:32:34 +0000 (00:32 -0500)]
bcachefs: Single open_bucket_partial list

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improve bch2_stripe_to_text()
Kent Overstreet [Sat, 25 Feb 2023 00:26:03 +0000 (19:26 -0500)]
bcachefs: Improve bch2_stripe_to_text()

We now print pointers as bucket:offset, the same as how we print extent
pointers.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Add option for completely disabling nocow
Kent Overstreet [Sat, 25 Feb 2023 00:07:21 +0000 (19:07 -0500)]
bcachefs: Add option for completely disabling nocow

This adds an option for completely disabling nocow mode, including the
locking in the data move path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Make bucket_alloc tracepoint more readable
Kent Overstreet [Sat, 25 Feb 2023 00:06:32 +0000 (19:06 -0500)]
bcachefs: Make bucket_alloc tracepoint more readable

Print bucket in dev:bucket notation, to be consistent with how we refer
to buckets elsewhere.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Don't call bch2_trans_update() unlocked
Kent Overstreet [Thu, 23 Feb 2023 00:39:02 +0000 (19:39 -0500)]
bcachefs: Don't call bch2_trans_update() unlocked

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: get_stripe_key_trans()
Kent Overstreet [Thu, 23 Feb 2023 00:28:58 +0000 (19:28 -0500)]
bcachefs: get_stripe_key_trans()

Another nested btree_trans fix

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix erasure coding shutdown path
Kent Overstreet [Wed, 22 Feb 2023 23:35:51 +0000 (18:35 -0500)]
bcachefs: Fix erasure coding shutdown path

It's possible when shutting down to for a stripe head to have a new
stripe that doesn't yet have any blocks allocated - we just need to free
it.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix buffer overrun in ec_stripe_update_extent()
Kent Overstreet [Wed, 22 Feb 2023 22:57:59 +0000 (17:57 -0500)]
bcachefs: Fix buffer overrun in ec_stripe_update_extent()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Check for redundant ec entries/stripe ptrs
Kent Overstreet [Wed, 22 Feb 2023 04:51:19 +0000 (23:51 -0500)]
bcachefs: Check for redundant ec entries/stripe ptrs

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Cached pointers should not be erasure coded
Kent Overstreet [Wed, 22 Feb 2023 00:22:44 +0000 (19:22 -0500)]
bcachefs: Cached pointers should not be erasure coded

There's no reason to erasure code cached pointers: we'll always have
another copy, and it'll be cheaper to read the other copy than do a
reconstruct read. And erasure coded cached pointers would add
complications that we'd rather not have to deal with, so let's make sure
to disallow them.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Kill bch2_keylist_add_in_order()
Kent Overstreet [Wed, 22 Feb 2023 05:56:41 +0000 (00:56 -0500)]
bcachefs: Kill bch2_keylist_add_in_order()

Dead code, so delete

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Add tracepoint & counter for btree split race
Kent Overstreet [Mon, 20 Feb 2023 21:41:03 +0000 (16:41 -0500)]
bcachefs: Add tracepoint & counter for btree split race

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: __bch2_btree_insert uses BTREE_INSERT_CACHED
Kent Overstreet [Mon, 20 Feb 2023 19:33:46 +0000 (14:33 -0500)]
bcachefs: __bch2_btree_insert uses BTREE_INSERT_CACHED

Cached btrees should be doing cached updates by default: this fixes a
bug in the migrate tool.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improve a verbose log message
Kent Overstreet [Mon, 20 Feb 2023 19:34:38 +0000 (14:34 -0500)]
bcachefs: Improve a verbose log message

We should be using bch2_err_str() where applicable.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: bch2_journal_entries_postprocess()
Kent Overstreet [Sun, 19 Feb 2023 05:49:51 +0000 (00:49 -0500)]
bcachefs: bch2_journal_entries_postprocess()

This brings back journal_entries_compact(), but in a more efficient form
- we need to do multiple postprocess steps, so iterate over the
journal entries being written just once to make it more efficient.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix ec repair code check
Kent Overstreet [Sun, 19 Feb 2023 05:43:10 +0000 (00:43 -0500)]
bcachefs: Fix ec repair code check

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Simplify ec stripes heap
Kent Overstreet [Sun, 19 Feb 2023 03:11:50 +0000 (22:11 -0500)]
bcachefs: Simplify ec stripes heap

Now that we have a separate data structure for tracking open stripes,
the stripes heap can track all existing stripes, which is a nice
simplification.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Erasure coding: Track open stripes
Kent Overstreet [Sun, 19 Feb 2023 02:07:25 +0000 (21:07 -0500)]
bcachefs: Erasure coding: Track open stripes

This adds a new hash table for stripes being created or updated, instead
of hackily relying on the stripes heap.

This lets us reserve the slot for the new stripe up front, at the same
time as we would pick an existing stripe - if we were updating an
existing stripe - making the overall code more consistent.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Stripe deletion now checks what it's deleting
Kent Overstreet [Sun, 19 Feb 2023 02:31:07 +0000 (21:31 -0500)]
bcachefs: Stripe deletion now checks what it's deleting

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improve c->writes refcounting for stripe create path
Kent Overstreet [Sun, 19 Feb 2023 02:10:13 +0000 (21:10 -0500)]
bcachefs: Improve c->writes refcounting for stripe create path

This makes our handling of c->writes more consistent with other
asynchronous work items.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Switch ec_stripes_heap_lock to a mutex
Kent Overstreet [Sun, 19 Feb 2023 01:49:37 +0000 (20:49 -0500)]
bcachefs: Switch ec_stripes_heap_lock to a mutex

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Split trans->last_begin_ip and trans->last_restarted_ip
Kent Overstreet [Sun, 19 Feb 2023 02:20:18 +0000 (21:20 -0500)]
bcachefs: Split trans->last_begin_ip and trans->last_restarted_ip

These are two different things - this improves our debug assert
messages.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix erasure coding locking
Kent Overstreet [Sat, 18 Feb 2023 03:43:47 +0000 (22:43 -0500)]
bcachefs: Fix erasure coding locking

This adds a new helper, bch2_trans_mutex_lock(), for locking a mutex -
dropping and retaking btree locks as needed.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Don't block on ec_stripe_head_lock with btree locks held
Kent Overstreet [Sat, 18 Feb 2023 02:04:46 +0000 (21:04 -0500)]
bcachefs: Don't block on ec_stripe_head_lock with btree locks held

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Add an assertion for using multiple btree_trans
Kent Overstreet [Sat, 18 Feb 2023 01:51:52 +0000 (20:51 -0500)]
bcachefs: Add an assertion for using multiple btree_trans

A thread should never be using more than one btree_trans - doing so is
an invitation for deadlocks.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Erasure coding now uses bch2_bucket_alloc_trans
Kent Overstreet [Sat, 18 Feb 2023 01:50:55 +0000 (20:50 -0500)]
bcachefs: Erasure coding now uses bch2_bucket_alloc_trans

This code predates plumbing btree_trans through the bucket allocation
path: switching to it fixes a deadlock due to using multiple btree_trans
at the same time, which we never want to do.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Don't invalidate open buckets
Kent Overstreet [Sat, 18 Feb 2023 01:33:12 +0000 (20:33 -0500)]
bcachefs: Don't invalidate open buckets

Like bch2_trans_mark_bucket(), we shouldn't be incrementing a bucket gen
while it's still open - erasure coding was hitting this.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fragmentation LRU
Kent Overstreet [Mon, 5 Dec 2022 15:24:19 +0000 (10:24 -0500)]
bcachefs: Fragmentation LRU

Now that we have much more efficient updates to the LRU btree, this
patch adds a new LRU that indexes buckets by fragmentation.

This means copygc no longer has to scan every bucket to find buckets
that need to be evacuated.

Changes:
 - A new field in bch_alloc_v4, fragmentation_lru - this corresponds to
   the bucket's position in the fragmentation LRU. We add a new field
   for this instead of calculating it as needed because we may make the
   fragmentation LRU optional; this field indicates whether a bucket is
   on the fragmentation LRU.

   Also, zoned devices will introduce variable bucket sizes; explicitly
   recording the LRU position will be safer for them.

 - A new copygc path for using the fragmentation LRU instead of
   scanning every bucket and building up an in-memory heap.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Use btree write buffer for LRU btree
Kent Overstreet [Mon, 6 Feb 2023 23:51:42 +0000 (18:51 -0500)]
bcachefs: Use btree write buffer for LRU btree

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix integer overflow warnings on 32 bit
Kent Overstreet [Fri, 17 Feb 2023 21:06:51 +0000 (16:06 -0500)]
bcachefs: Fix integer overflow warnings on 32 bit

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix insert_snapshot_whiteouts()
Kent Overstreet [Fri, 17 Feb 2023 04:42:09 +0000 (23:42 -0500)]
bcachefs: Fix insert_snapshot_whiteouts()

 - We were failing to set the key type on the whiteouts it was creating,
   oops.

 - Also, we need to create whiteouts when generating front splits, not
   just back splits.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: bch2_mark_snapshot() now called like other triggers
Kent Overstreet [Fri, 17 Feb 2023 05:39:12 +0000 (00:39 -0500)]
bcachefs: bch2_mark_snapshot() now called like other triggers

This fixes a bug where bch2_mark_snapshot() wasn't called for existing
snapshot nodes being updated when child nodes were added.

This led to the data update path thinking the key being updated was for
a snapshot that didn't have children, causing it to fail to insert
whiteouts when splitting existing extents.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Snapshot whiteout fix
Kent Overstreet [Fri, 17 Feb 2023 04:36:41 +0000 (23:36 -0500)]
bcachefs: Snapshot whiteout fix

When fully overwriting an existing extent, we may need to generate a
whiteout - not just if the extent being overwritten was in an older
snapshot, but also if it was overwriting an extent in an older snapshot.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Reimplement repair for overlapping extents
Daniel Hill [Sun, 8 May 2022 03:03:28 +0000 (15:03 +1200)]
bcachefs: Reimplement repair for overlapping extents

Repair now checks if overlapping extents exist in the same snapshot
and calls update_trans_update_extent to do the repair work.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Don't print out duplicate fsck errors
Kent Overstreet [Fri, 17 Feb 2023 02:02:14 +0000 (21:02 -0500)]
bcachefs: Don't print out duplicate fsck errors

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: bch2_btree_insert_nonextent()
Kent Overstreet [Fri, 17 Feb 2023 04:09:27 +0000 (23:09 -0500)]
bcachefs: bch2_btree_insert_nonextent()

This adds a new helper to delete some redundant code in
bch2_trans_update_extent().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix a 64 bit divide
Kent Overstreet [Fri, 17 Feb 2023 20:36:46 +0000 (15:36 -0500)]
bcachefs: Fix a 64 bit divide

This fixes a build failure on 32 bit

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agosix locks: Simplify six_lock_counts()
Kent Overstreet [Wed, 15 Feb 2023 23:29:16 +0000 (18:29 -0500)]
six locks: Simplify six_lock_counts()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix verify_update_old_key()
Kent Overstreet [Mon, 13 Feb 2023 23:21:40 +0000 (18:21 -0500)]
bcachefs: Fix verify_update_old_key()

This fixes a very-rare race in our assertion, with needs_whiteout being
modified in the btree key.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: New backtrace utility code
Kent Overstreet [Mon, 13 Feb 2023 04:15:53 +0000 (23:15 -0500)]
bcachefs: New backtrace utility code

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix verify_bucket_evacuated()
Kent Overstreet [Mon, 13 Feb 2023 03:42:31 +0000 (22:42 -0500)]
bcachefs: Fix verify_bucket_evacuated()

This fixes an incorrectly handled transaction restart.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Handle sb buffer resizing in __copy_super()
Kent Overstreet [Mon, 13 Feb 2023 03:08:39 +0000 (22:08 -0500)]
bcachefs: Handle sb buffer resizing in __copy_super()

This fixes a rare buffer overrun when one field is growing and another
field is shrinking - and is a nice simplification as well.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix failure to read btree roots
Kent Overstreet [Mon, 13 Feb 2023 00:24:34 +0000 (19:24 -0500)]
bcachefs: Fix failure to read btree roots

If failed to read a btree root - or if we're not using a btree root,
because of the reconstruct_alloc option - make sure we update the
corresponding info for the key/level for the root on disk.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Don't run triggers when repairing in __bch2_mark_reflink_p()
Daniel Hill [Sun, 12 Feb 2023 02:51:45 +0000 (15:51 +1300)]
bcachefs: Don't run triggers when repairing in __bch2_mark_reflink_p()

Triggers current trip-up on the faulty reflink we're trying to repair,
Disabling them lets us fix broken reflink and continue.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: let __bch2_btree_insert() pass in flags
Daniel Hill [Thu, 19 Jan 2023 12:27:30 +0000 (01:27 +1300)]
bcachefs: let __bch2_btree_insert() pass in flags

This patch is prep work for the following patch.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improve locking in __bch2_set_nr_journal_buckets()
Kent Overstreet [Sat, 11 Feb 2023 21:53:59 +0000 (16:53 -0500)]
bcachefs: Improve locking in __bch2_set_nr_journal_buckets()

This refactors to not call bch2_journal_block() with c->sb_lock held.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: More info on check_bucket_ref() error
Kent Overstreet [Sun, 12 Feb 2023 00:31:03 +0000 (19:31 -0500)]
bcachefs: More info on check_bucket_ref() error

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Add missing include
Kent Overstreet [Sun, 12 Feb 2023 00:30:41 +0000 (19:30 -0500)]
bcachefs: Add missing include

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Handle btree node rewrites before going RW
Kent Overstreet [Sat, 11 Feb 2023 17:57:04 +0000 (12:57 -0500)]
bcachefs: Handle btree node rewrites before going RW

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Nocow locking fixup
Kent Overstreet [Sat, 11 Feb 2023 17:38:28 +0000 (12:38 -0500)]
bcachefs: Nocow locking fixup

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Add some logging for btree node rewrites due to errors
Kent Overstreet [Fri, 10 Feb 2023 20:47:46 +0000 (15:47 -0500)]
bcachefs: Add some logging for btree node rewrites due to errors

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Ensure btree node cache is not more than half dirty
Kent Overstreet [Thu, 11 Nov 2021 20:50:22 +0000 (15:50 -0500)]
bcachefs: Ensure btree node cache is not more than half dirty

Tweak journal reclaim to ensure the btree node cache isn't more
than half dirty so that memory reclaim can always make progress - the
same as we do for the btree key cache.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Add max nr of IOs in flight to the move path
Kent Overstreet [Mon, 9 Jan 2023 06:45:18 +0000 (01:45 -0500)]
bcachefs: Add max nr of IOs in flight to the move path

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Add an assert to bch2_bucket_nocow_unlock()
Kent Overstreet [Thu, 26 Jan 2023 18:36:30 +0000 (13:36 -0500)]
bcachefs: Add an assert to bch2_bucket_nocow_unlock()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: don't block reads if we're promoting
Daniel Hill [Fri, 6 Jan 2023 08:11:07 +0000 (21:11 +1300)]
bcachefs: don't block reads if we're promoting

The promote path calls data_update_init() and now that we take locks here,
there's potential for promote to block our read path, just error
when we can't take the lock instead of blocking.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix promote path leak
Kent Overstreet [Thu, 5 Jan 2023 08:55:23 +0000 (03:55 -0500)]
bcachefs: Fix promote path leak

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improve invalidate_one_bucket() error messages
Kent Overstreet [Wed, 4 Jan 2023 04:54:10 +0000 (23:54 -0500)]
bcachefs: Improve invalidate_one_bucket() error messages

Make sure to check for lru entries that point to buckets that don't
exist as well as buckets in the wrong state, and improve the error
message we print out.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix move_ctxt_wait_event()
Kent Overstreet [Wed, 4 Jan 2023 04:39:42 +0000 (23:39 -0500)]
bcachefs: Fix move_ctxt_wait_event()

We shouldn't be evaluating cond again if it already returned true.

This fixes a bug when this helper is used for taking nocow locks.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix deadlock on nocow locks in data move path
Kent Overstreet [Mon, 2 Jan 2023 22:53:02 +0000 (17:53 -0500)]
bcachefs: Fix deadlock on nocow locks in data move path

The recent nocow locking rework introduced a deadlock in the data move
path: the new nocow locking scheme uses a hash table with a fixed size
array for chaining, meaning on hash collision we may have to wait for
other locks to be released before we can lock a bucket.

And since the data move path needs to submit writes from the same thread
that's taking nocow locks and submitting reads, this introduces a
deadlock.

This shouldn't happen often in practice, but since the data move path
can keep large numbers of IOs in flight simultaneously, it's something
we have to handle.

This patch makes move_ctxt_wait_event() available to
bch2_data_update_init() and uses it when appropriate, which is our
normal solution to this kind of thing.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: BKEY_INVALID_FROM_JOURNAL
Kent Overstreet [Wed, 21 Dec 2022 01:00:34 +0000 (20:00 -0500)]
bcachefs: BKEY_INVALID_FROM_JOURNAL

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Change bkey_invalid() rw param to flags
Kent Overstreet [Wed, 21 Dec 2022 00:58:16 +0000 (19:58 -0500)]
bcachefs: Change bkey_invalid() rw param to flags

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Rework lru btree
Kent Overstreet [Mon, 5 Dec 2022 21:49:13 +0000 (16:49 -0500)]
bcachefs: Rework lru btree

This patch changes how the LRU index works:

Instead of using KEY_TYPE_lru where the bucket the lru entry points to
is part of the value, this switches to KEY_TYPE_set and encoding the
bucket we refer to in the low bits of the key.

This means that we no longer have to check for collisions when inserting
LRU entries. We'll be making using of this in the next patch, which adds
a btree write buffer - a pure write buffer for btree updates, where
updates are appended to a simple array and then periodically sorted and
batch inserted.

This is a new on disk format version, and a forced upgrade.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improved nocow locking
Kent Overstreet [Thu, 15 Dec 2022 01:52:11 +0000 (20:52 -0500)]
bcachefs: Improved nocow locking

This improves the nocow lock table so that hash table entries have
multiple locks, and locks specify which bucket they're for - i.e. we can
now resolve hash collisions.

This is important because the allocator has to skip buckets that are
locked in the nocow lock table, and previously hash collisions would
cause it to spuriously skip unlocked buckets.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: handle failed data_update_init cleanup
Daniel Hill [Thu, 8 Dec 2022 23:37:56 +0000 (12:37 +1300)]
bcachefs: handle failed data_update_init cleanup

data_update_init allocates several resources, but we forget to clean
these up when it fails.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: expose nocow_lock table in sysfs
Daniel Hill [Wed, 7 Dec 2022 05:41:21 +0000 (18:41 +1300)]
bcachefs: expose nocow_lock table in sysfs

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: bucket_gens btree
Kent Overstreet [Sat, 26 Nov 2022 04:14:30 +0000 (23:14 -0500)]
bcachefs: bucket_gens btree

To improve mount times, add a btree for just bucket gens, 256 of them
per key: this means we'll have to scan drastically less metadata at
startup.

This adds
 - trigger for keeping it in sync with the all btree
 - initialization code, for filesystems from previous versions
 - new path for reading bucket gens
 - new fsck code

And a new on disk format version.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Inline bch2_two_state_(trylock|unlock)
Kent Overstreet [Wed, 23 Nov 2022 17:22:39 +0000 (12:22 -0500)]
bcachefs: Inline bch2_two_state_(trylock|unlock)

Standard inlining of fast paths - these locks are now used by our new
nocow mode.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Nocow support
Kent Overstreet [Wed, 2 Nov 2022 21:12:00 +0000 (17:12 -0400)]
bcachefs: Nocow support

This adds support for nocow mode, where we do writes in-place when
possible. Patch components:

 - New boolean filesystem and inode option, nocow: note that when nocow
   is enabled, data checksumming and compression are implicitly disabled

 - To prevent in-place writes from racing with data moves
   (data_update.c) or bucket reuse (i.e. a bucket being reused and
   re-allocated while a nocow write is in flight, we have a new locking
   mechanism.

   Buckets can be locked for either data update or data move, using a
   fixed size hash table of two_state_shared locks. We don't have any
   chaining, meaning updates and moves to different buckets that hash to
   the same lock will wait unnecessarily - we'll want to watch for this
   becoming an issue.

 - The allocator path also needs to check for in-place writes in flight
   to a given bucket before giving it out: thus we add another counter
   to bucket_alloc_state so we can track this.

 - Fsync now may need to issue cache flushes to block devices instead of
   flushing the journal. We add a device bitmask to bch_inode_info,
   ei_devs_need_flush, which tracks devices that need to have flushes
   issued - note that this will lead to unnecessary flushes when other
   codepaths have already issued flushes, we may want to replace this with
   a sequence number.

 - New nocow write path: look up extents, and if they're writable write
   to them - otherwise fall back to the normal COW write path.

XXX: switch to sequence numbers instead of bitmask for devs needing
journal flush

XXX: ei_quota_lock being a mutex means bch2_nocow_write_done() needs to
run in process context - see if we can improve this

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Data update support for unwritten extents
Kent Overstreet [Mon, 14 Nov 2022 06:31:10 +0000 (01:31 -0500)]
bcachefs: Data update support for unwritten extents

The data update path requires special support for unwritten extents - we
still need to be able to move them, but there's no need to read or write
anything.

This patch adds a new error code to tell bch2_move_extent() that we're
short circuiting the read, and adds bch2_update_unwritten_extent() to
create a reservation then call __bch2_data_update_index_update().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Unwritten extents support
Kent Overstreet [Sun, 13 Nov 2022 23:59:01 +0000 (18:59 -0500)]
bcachefs: Unwritten extents support

 - bch2_extent_merge checks unwritten bit
 - read path returns 0s for unwritten extents without actually reading
 - reflink path skips over unwritten extents
 - bch2_bkey_ptrs_invalid() checks for extents with both written and
   unwritten extents, and non-normal extents (stripes, btree ptrs) with
   unwritten ptrs
 - fiemap checks for unwritten extents and returns
   FIEMAP_EXTENT_UNWRITTEN

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: bch2_extent_update_i_size_sectors()
Kent Overstreet [Tue, 15 Nov 2022 04:41:18 +0000 (23:41 -0500)]
bcachefs: bch2_extent_update_i_size_sectors()

In the io path, when we do the extent update we also have to update the
inode - for i_size and i_sectors updates, as well as for bi_journal_seq
for fsync.

This factors that out into a new helper which will be used in the new
nocow mode, in the unwritten extent conversion path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: bch2_extent_fallocate()
Kent Overstreet [Sun, 13 Nov 2022 23:54:37 +0000 (18:54 -0500)]
bcachefs: bch2_extent_fallocate()

This factors out part of __bchfs_fallocate() in fs-io.c into an new,
lower level io.c helper, which creates a single extent reservation.

This is prep work for nocow support - the new helper will shortly gain
the ability to create unwritten extents.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Skip inode unpack/pack in bch2_extent_update()
Kent Overstreet [Fri, 21 Oct 2022 17:42:38 +0000 (13:42 -0400)]
bcachefs: Skip inode unpack/pack in bch2_extent_update()

This takes advantage of the new inode type to skip the expensive
pack/unpack when inode updates are required in the extent update path.
Additionally, we now skip the inode update entirely when i_sectors and
i_size aren't changing.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Drop old maybe_extending optimization
Kent Overstreet [Mon, 8 Nov 2021 17:30:47 +0000 (12:30 -0500)]
bcachefs: Drop old maybe_extending optimization

The extend update path had an optimization to avoid updating the inode
if we knew we were definitely not extending the file. But now that we're
updating inodes on every extent update - for fsync - that code can be
deleted.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: KEY_TYPE_inode_v3, metadata_version_inode_v3
Kent Overstreet [Fri, 21 Oct 2022 17:21:03 +0000 (13:21 -0400)]
bcachefs: KEY_TYPE_inode_v3, metadata_version_inode_v3

Move bi_size and bi_sectors into the non-varint portion of the inode, so
that the write path can update them without going through the relatively
expensive unpack/pack operations.

Other changes:
 - Add a field for the offset of the varint section, so we can add new
   non-varint fields without needing a new inode type, like alloc_v3
 - Move bi_mode into the flags field, so that the varint section can be
   u64 aligned

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Start snapshots before bch2_gc()
Kent Overstreet [Thu, 19 Jan 2023 08:37:44 +0000 (03:37 -0500)]
bcachefs: Start snapshots before bch2_gc()

bch2_gc may require snapshots to be started - the repair path when
checking the reflink btree may do updates to the extents btree.

This moves bch2_fs_initialize_subvolumes() and bch2_fs_snapshots_start()
to before bch2_gc() - since we haven't gone RW yet, the updates in
bch2_fs_initialize_subvolumes() are done via the journal replay keys
list, so it's fine to do this before bch2_gc().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improve bch2_check_alloc_info()
Kent Overstreet [Wed, 30 Nov 2022 18:25:17 +0000 (13:25 -0500)]
bcachefs: Improve bch2_check_alloc_info()

This factors out a new helper from bch2_dev_freespace_init(),
bch2_get_key_or_hole(), and uses it in bch2_check_alloc_info(): we're
now able to process holes in the alloc btree as ranges, instead of one
bucket at a time.

This will improve fsck performance on new filesystems, or filesystems
where not every bucket has been used yet.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improve bch2_dev_freespace_init()
Kent Overstreet [Sat, 26 Nov 2022 09:37:11 +0000 (04:37 -0500)]
bcachefs: Improve bch2_dev_freespace_init()

This makes bch2_dev_freespace_init() much faster: instead of processing
every bucket on the device one at a time, we handle ranges of missing
keys all at once: the freespace btree is an extents style btree, so we
only have to insert one freespace key for every range of missing keys
in the alloc btree.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agofixup bcachefs: New on disk format: Backpointers
Kent Overstreet [Sun, 12 Feb 2023 19:25:59 +0000 (14:25 -0500)]
fixup bcachefs: New on disk format: Backpointers

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Don't use key cache during fsck
Kent Overstreet [Fri, 14 Oct 2022 11:02:36 +0000 (07:02 -0400)]
bcachefs: Don't use key cache during fsck

The btree key cache mainly helps with lock contention, at the cost of
additional memory overhead. During some fsck passes the memory overhead
really matters, but fsck is single threaded so lock contention is an
issue - so skipping the key cache during fsck will help with
performance.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Run check_extents_to_backpointers() in multiple passes
Kent Overstreet [Wed, 28 Sep 2022 14:06:10 +0000 (10:06 -0400)]
bcachefs: Run check_extents_to_backpointers() in multiple passes

Similer to the previous patch for check_backpointers_to_extents(), if
the alloc + backpointers btrees do not fit in ram we need to run into
multiple passes.

The counting of btree nodes that fit in memory is different here,
because we have to walk the alloc and backpointers btrees at the same
time, since a backpointer could reside in either of them and we don't
know which without checking both.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Run bch2_check_backpointers_to_extents() in multiple passes if necessary
Kent Overstreet [Mon, 10 Oct 2022 02:18:06 +0000 (22:18 -0400)]
bcachefs: Run bch2_check_backpointers_to_extents() in multiple passes if necessary

When the extents + reflink btrees don't fit into memory this fsck pass
becomes _much_ slower, since we're doing random lookups.

This patch changes this pass to check how much of the relevant btrees
will fit into memory, and run in multiple passes if needed.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Don't stop copygc while removing devices
Kent Overstreet [Sun, 9 Oct 2022 08:26:06 +0000 (04:26 -0400)]
bcachefs: Don't stop copygc while removing devices

With the new backpointer based copygc we don't need an explicit copygc
reserve, we're always evacuating buckets one at a time - so this is no
longer needed, and in fact removing it fixes a deadlock in
bch2_dev_allocator_remove().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Delete in memory ec backpointers
Kent Overstreet [Sun, 9 Oct 2022 04:47:18 +0000 (00:47 -0400)]
bcachefs: Delete in memory ec backpointers

Post btree backpointers, these aren't needed anymore.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Erasure coding now uses backpointers
Kent Overstreet [Sun, 9 Oct 2022 04:29:51 +0000 (00:29 -0400)]
bcachefs: Erasure coding now uses backpointers

This is only a start to updating erasure coding for backpointers - it's
still not working yet. The subsequent patch will delete our old in
memory backpointers for copygc, and this fixes a spurious EPERM
bug/error message.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Copygc now uses backpointers
Kent Overstreet [Fri, 18 Mar 2022 04:42:09 +0000 (00:42 -0400)]
bcachefs: Copygc now uses backpointers

Previously, copygc needed to walk the entire extents & reflink btrees to
find extents that needed to be moved.

Now that we have backpointers, this patch implements
bch2_evacuate_bucket() in the move code, which copygc now uses for
evacuating mostly empty buckets.

Also, thanks to the new backpointers code, copygc can now move btree
nodes.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: New on disk format: Backpointers
Kent Overstreet [Fri, 18 Mar 2022 00:51:27 +0000 (20:51 -0400)]
bcachefs: New on disk format: Backpointers

This patch adds backpointers: we now have a reverse index from device
and offset on that device (specifically, offset within a bucket) back to
btree nodes and (non cached) data extents.

The first 40 backpointers within a bucket are stored in the alloc key;
after that backpointers spill over to the next backpointers btree. This
is to help avoid performance regressions from additional btree updates
on large streaming workloads.

This patch adds all the code for creating, checking and repairing
backpointers. The next patch in the series is going to use backpointers
for copygc - finally getting rid of the need to scan all extents to do
copygc.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Btree write buffer
Kent Overstreet [Wed, 4 Jan 2023 05:00:50 +0000 (00:00 -0500)]
bcachefs: Btree write buffer

This adds a new method of doing btree updates - a straight write buffer,
implemented as a flat fixed size array.

This is only useful when we don't need to read from the btree in order
to do the update, and when reading is infrequent - perfect for the LRU
btree.

This will make LRU btree updates fast enough that we'll be able to use
it for persistently indexing buckets by fragmentation, which will be a
massive boost to copygc performance.

Changes:
 - A new btree_insert_type enum, for btree_insert_entries. Specifies
   btree, btree key cache, or btree write buffer.

 - bch2_trans_update_buffered(): updates via the btree write buffer
   don't need a btree path, so we need a new update path.

 - Transaction commit path changes:
   The update to the btree write buffer both mutates global, and can
   fail if there isn't currently room. Therefore we do all write buffer
   updates in the transaction all at once, and also if it fails we have
   to revert filesystem usage counter changes.

   If there isn't room we flush the write buffer in the transaction
   commit error path and retry.

 - A new persistent option, for specifying the number of entries in the
   write buffer.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Go RW before check_alloc_info()
Kent Overstreet [Mon, 12 Dec 2022 00:14:30 +0000 (19:14 -0500)]
bcachefs: Go RW before check_alloc_info()

It's possible to do btree updates before going RW by adding them to the
list of updates for journal replay to do, but this is limited by what
fits in RAM. This patch switches the second alloc info phase to run
after going RW - btree_gc has already ensured the alloc btree itself is
correct - and tweaks the allocation path to deal with the potential
small inconsistencies.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Start copygc when first going read-write
Kent Overstreet [Mon, 17 Oct 2022 08:51:58 +0000 (04:51 -0400)]
bcachefs: Start copygc when first going read-write

In the distant past, it wasn't possible to start copygc until after
journal replay had finished. Now, the btree iterator code overlays keys
from the journal, so there's no reason not to start it earlier - and it
solves a rare deadlock.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Kill trans->flags
Kent Overstreet [Thu, 9 Feb 2023 18:22:12 +0000 (13:22 -0500)]
bcachefs: Kill trans->flags

Recursive transaction commits are occasionally necessary - in
particular, for the upcoming btree write buffer's flush path.

This avoids bugs due to trans->flags being accidentally mutated
mid-commit, which can cause c->writes refcount leaks.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: trans->notrace_relock_fail
Kent Overstreet [Thu, 9 Feb 2023 19:48:54 +0000 (14:48 -0500)]
bcachefs: trans->notrace_relock_fail

When we unlock in order to submit IO, the next relock event is likely to
fail if submit_bio() blocked - we shouldn't those events in our _fail
stats, since those are expected events and shouldn't cause test
failures.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Debug mode for c->writes references
Kent Overstreet [Thu, 9 Feb 2023 17:21:45 +0000 (12:21 -0500)]
bcachefs: Debug mode for c->writes references

This adds a debug mode where we split up the c->writes refcount into
distinct refcounts for every codepath that takes a reference, and adds
sysfs code to print the value of each ref.

This will make it easier to debug shutdown hangs due to refcount leaks.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: ec_stripe_delete_work() now takes ref on c->writes
Kent Overstreet [Thu, 9 Feb 2023 17:22:58 +0000 (12:22 -0500)]
bcachefs: ec_stripe_delete_work() now takes ref on c->writes

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix btree_node_write_blocked() not being cleared
Kent Overstreet [Fri, 10 Feb 2023 02:13:37 +0000 (21:13 -0500)]
bcachefs: Fix btree_node_write_blocked() not being cleared

The btree_node_write_blocked bit was a later addition to this code,
it only mirrors the state of the b->write_blocked list (empty or
nonempty) - unfortunately, when it was added it wasn't correctly kept in
sync - oops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Switch a BUG_ON() to a panic()
Kent Overstreet [Thu, 9 Feb 2023 20:49:25 +0000 (15:49 -0500)]
bcachefs: Switch a BUG_ON() to a panic()

This assert is popping - rarely - in the CI, this will help us track it
down from the logs.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix btree_path_alloc()
Kent Overstreet [Wed, 8 Feb 2023 23:04:22 +0000 (18:04 -0500)]
bcachefs: Fix btree_path_alloc()

We need to call bch2_trans_update_max_paths() before marking the new
path as allocated, since we're not initializing it yet.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix memleak in replicas_table_update()
Brett Holman [Fri, 10 Feb 2023 23:36:55 +0000 (16:36 -0700)]
bcachefs: Fix memleak in replicas_table_update()

Signed-off-by: Brett Holman <bholman.devel@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>