linux.git
18 months agobcachefs: Avoid __GFP_NOFAIL
Kent Overstreet [Sun, 28 May 2023 04:35:35 +0000 (00:35 -0400)]
bcachefs: Avoid __GFP_NOFAIL

We've been using __GFP_NOFAIL for allocating struct bch_folio, our
private per-folio state.

However, that struct is variable size - it holds state for each sector
in the folio, and folios can be quite large now, which means it's
possible for bch_folio to be larger than PAGE_SIZE now.

__GFP_NOFAIL allocations are undesirable in normal circumstances, but
particularly so at >= PAGE_SIZE, and warnings are emitted for that.

So, this patch adds proper error paths and eliminates most uses of
__GFP_NOFAIL. Also, do some more cleanup of gfp flags w.r.t. btree node
locks: we can use GFP_KERNEL, but only if we're not holding btree locks,
and if we are holding btree locks we should be using GFP_NOWAIT.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix corruption with writeable snapshots
Kent Overstreet [Sun, 28 May 2023 03:19:13 +0000 (23:19 -0400)]
bcachefs: Fix corruption with writeable snapshots

When partially overwriting an extent in an older snapshot, the existing
extent has to be split.

If the existing extent was overwritten in a different (sibling)
snapshot, we have to ensure that the split won't be visible in the
sibling snapshot.

data_update.c already has code for this,
bch2_insert_snapshot_writeouts() - we just need to move it into
btree_update_leaf.c and change bch2_trans_update_extent() to use it as
well.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Convert -ENOENT to private error codes
Kent Overstreet [Sat, 27 May 2023 23:59:59 +0000 (19:59 -0400)]
bcachefs: Convert -ENOENT to private error codes

As with previous conversions, replace -ENOENT uses with more informative
private error codes.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: trans_for_each_path_safe()
Kent Overstreet [Sat, 27 May 2023 23:55:54 +0000 (19:55 -0400)]
bcachefs: trans_for_each_path_safe()

bch2_btree_trans_to_text() is used on btree_trans objects that are owned
by different threads - when printing out deadlock cycles - so we need a
safe version of trans_for_each_path(), else we race with seeing a
btree_path that was just allocated and not fully initialized:

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a quota read bug
Kent Overstreet [Sun, 28 May 2023 00:00:13 +0000 (20:00 -0400)]
bcachefs: Fix a quota read bug

bch2_fs_quota_read() could see an inode that's been deleted
(KEY_TYPE_inode_generation) - bch2_fs_quota_read_inode() needs to check
for that instead of erroring.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix move_extent_fail counter
Kent Overstreet [Fri, 26 May 2023 22:12:55 +0000 (18:12 -0400)]
bcachefs: Fix move_extent_fail counter

fail counters need to be events, not numbers of sectors - or the
calculations the tests use for determining if we've had too many
slowpath events don't work.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Don't reuse reflink btree keyspace
Kent Overstreet [Fri, 26 May 2023 03:37:06 +0000 (23:37 -0400)]
bcachefs: Don't reuse reflink btree keyspace

We've been seeing difficult to debug "missing indirect extent" bugs,
that fsck doesn't seem to find.

One possibility is that there was a missing indirect extent, but then a
new indirect extent was created at the location of the previous indirect
extent.

This patch eliminates that possibility by always creating new indirect
extents right after the last one, at the end of the reflink btree.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agomean and variance: Add a missing include
Kent Overstreet [Sun, 4 Jun 2023 21:58:56 +0000 (17:58 -0400)]
mean and variance: Add a missing include

abs() is in math.h

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agomean and variance: More tests
Kent Overstreet [Fri, 26 May 2023 02:22:25 +0000 (22:22 -0400)]
mean and variance: More tests

Add some more tests that test conventional and weighted mean
simultaneously, and with a table of values that represents events that
we'll be using this to look for so we can verify-by-eyeball that the
output looks sane.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Disable percpu read lock mode in userspace
Kent Overstreet [Sat, 10 Jun 2023 14:57:23 +0000 (10:57 -0400)]
six locks: Disable percpu read lock mode in userspace

When running in userspace, we currently don't have a real percpu
implementation available - at least in bcachefs-tools, which is where
this code is currently used in userspace.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Use atomic_try_cmpxchg_acquire()
Kent Overstreet [Thu, 25 May 2023 18:35:06 +0000 (14:35 -0400)]
six locks: Use atomic_try_cmpxchg_acquire()

This switches to a newer cmpxchg variant which updates @old for us on
failure, simplifying the cmpxchg loops a bit and supposedly generating
better code.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Fix an unitialized var
Kent Overstreet [Thu, 25 May 2023 22:10:04 +0000 (18:10 -0400)]
six locks: Fix an unitialized var

In the conversion to atomic_t, six_lock_slowpath() ended up calling
six_lock_wakeup() in the failure path with a state variable that was
never initialized - whoops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Delete redundant comment
Kent Overstreet [Tue, 23 May 2023 04:21:22 +0000 (00:21 -0400)]
six locks: Delete redundant comment

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Tiny bit more tidying
Kent Overstreet [Mon, 22 May 2023 16:11:13 +0000 (12:11 -0400)]
six locks: Tiny bit more tidying

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Seq now only incremented on unlock
Kent Overstreet [Fri, 16 Jun 2023 19:56:42 +0000 (15:56 -0400)]
six locks: Seq now only incremented on unlock

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Split out seq, use atomic_t instead of atomic64_t
Kent Overstreet [Mon, 22 May 2023 04:17:40 +0000 (00:17 -0400)]
six locks: Split out seq, use atomic_t instead of atomic64_t

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Single instance of six_lock_vals
Kent Overstreet [Fri, 16 Jun 2023 23:21:21 +0000 (19:21 -0400)]
six locks: Single instance of six_lock_vals

Since we're not generating different versions of the lock functions for
each lock type, the constant propagation we were trying to do before is
no longer useful - this is now a small code size decrease.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix_locks: Kill test_bit()/set_bit() usage
Kent Overstreet [Mon, 22 May 2023 21:54:19 +0000 (17:54 -0400)]
six_locks: Kill test_bit()/set_bit() usage

This deletes the crazy cast-atomic-to-unsigned-long, and replaces them
with atomic_and() and atomic_or().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: lock->state.seq no longer used for write lock held
Kent Overstreet [Fri, 16 Jun 2023 22:24:05 +0000 (18:24 -0400)]
six locks: lock->state.seq no longer used for write lock held

lock->state.seq is shortly being moved out of lock->state, to kill the
depedency on atomic64; in preparation for that, we change the write
locking bit to write locked.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Simplify six_relock()
Kent Overstreet [Fri, 16 Jun 2023 19:00:48 +0000 (15:00 -0400)]
six locks: Simplify six_relock()

The next patch is going to move lock->seq out of lock->state. This
replaces six_relock() with a much simpler implementation based on
trylock.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Improve spurious wakeup handling in pcpu reader mode
Kent Overstreet [Mon, 22 May 2023 03:41:56 +0000 (23:41 -0400)]
six locks: Improve spurious wakeup handling in pcpu reader mode

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Documentation, renaming
Kent Overstreet [Sun, 21 May 2023 19:40:40 +0000 (15:40 -0400)]
six locks: Documentation, renaming

 - Expanded and revamped overview documentation in six.h, giving an
   overview of all features
 - docbook-comments for all external interfaces
 - Rename some functions for simplicity, i.e.
   six_lock_ip_type() -> six_lock_ip()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Kill six_lock_state union
Kent Overstreet [Sun, 21 May 2023 03:57:48 +0000 (23:57 -0400)]
six locks: Kill six_lock_state union

As suggested by Linus, this drops the six_lock_state union in favor of
raw bitmasks.

On the one hand, bitfields give more type-level structure to the code.
However, a significant amount of the code was working with
six_lock_state as a u64/atomic64_t, and the conversions from the
bitfields to the u64 were deemed a bit too out-there.

More significantly, because bitfield order is poorly defined (#ifdef
__LITTLE_ENDIAN_BITFIELD can be used, but is gross), incrementing the
sequence number would overflow into the rest of the bitfield if the
compiler didn't put the sequence number at the high end of the word.

The new code is a bit saner when we're on an architecture without real
atomic64_t support - all accesses to lock->state now go through
atomic64_*() operations.

On architectures with real atomic64_t support, we additionally use
atomic bit ops for setting/clearing individual bits.

Text size: 7467 bytes -> 4649 bytes - compilers still suck at
bitfields.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Simplify dispatch
Kent Overstreet [Sun, 21 May 2023 01:44:30 +0000 (21:44 -0400)]
six locks: Simplify dispatch

Originally, we used inlining/flattening to cause the compiler to
generate different versions of lock/trylock/relock/unlock for each lock
type - read, intent, and write. This made the individual functions
smaller and let the compiler eliminate table lookups: however, as the
code has gotten more complicated these optimizations have gotten less
worthwhile, and all the tricky inlining and dispatching made the code
less readable.

Text size: 11015 bytes -> 7467 bytes, and benchmarks show no loss of
performance.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Centralize setting of waiting bit
Kent Overstreet [Sun, 21 May 2023 00:37:53 +0000 (20:37 -0400)]
six locks: Centralize setting of waiting bit

Originally, the waiting bit was always set by trylock() on failure:
however, it's now set by __six_lock_type_slowpath(), with wait_lock held
- which is the more correct place to do it.

That made setting the waiting bit in trylock redundant, so this patch
deletes that.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Remove hacks for percpu mode lost wakeup
Kent Overstreet [Sun, 21 May 2023 20:38:09 +0000 (16:38 -0400)]
six locks: Remove hacks for percpu mode lost wakeup

The lost wakeup bug hasn't been observed in awhile, and we're trying to
provoke it and determine if it still exists.

This patch removes some defenses that were added to attempt to track it
down; if it still exists, this should make it easier to see it.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: Kill six_lock_pcpu_(alloc|free)
Kent Overstreet [Sun, 21 May 2023 00:57:55 +0000 (20:57 -0400)]
six locks: Kill six_lock_pcpu_(alloc|free)

six_lock_pcpu_alloc() is an unsafe interface: it's not safe to allocate
or free the percpu reader count on an existing lock that's in use, the
only safe time to allocate percpu readers is when the lock is first
being initialized.

This patch adds a flags parameter to six_lock_init(), and instead of
six_lock_pcpu_free() we now expose six_lock_exit(), which does the same
thing but is less likely to be misused.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agosix locks: six_lock_readers_add()
Kent Overstreet [Sun, 21 May 2023 00:40:08 +0000 (20:40 -0400)]
six locks: six_lock_readers_add()

This moves a helper out of the bcachefs code that shouldn't have been
there, since it touches six lock internals.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Don't call local_clock() twice in trans_begin()
Kent Overstreet [Fri, 16 Jun 2023 22:55:07 +0000 (18:55 -0400)]
bcachefs: Don't call local_clock() twice in trans_begin()

local_clock() is not as cheap as we'd like it to be, alas

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a buffer overrun in bch2_fs_usage_read()
Kent Overstreet [Mon, 22 May 2023 18:39:44 +0000 (14:39 -0400)]
bcachefs: Fix a buffer overrun in bch2_fs_usage_read()

We were copying the size of a struct bch_fs_usage_online to a struct
bch_fs_usage, which is 8 bytes smaller.

This adds some new helpers so we can do this correctly, and get rid of
some magic +1s too.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Clear btree_node_just_written() when node reused or evicted
Kent Overstreet [Mon, 22 May 2023 04:49:06 +0000 (00:49 -0400)]
bcachefs: Clear btree_node_just_written() when node reused or evicted

This fixes the following bug:

Journal reclaim attempts to flush a node, but races with the node being
evicted from the btree node cache; when we lock the node, the data
buffers have already been freed.

We don't evict a node that's dirty, so calling btree_node_write() is
fine - it's a noop - except that the btree_node_just_written bit causes
bch2_btree_post_write_cleanup() to run (resorting the node), which then
causes a null ptr deref.

00078 Unable to handle kernel NULL pointer dereference at virtual address 000000000000009e
00078 Mem abort info:
00078   ESR = 0x0000000096000005
00078   EC = 0x25: DABT (current EL), IL = 32 bits
00078   SET = 0, FnV = 0
00078   EA = 0, S1PTW = 0
00078   FSC = 0x05: level 1 translation fault
00078 Data abort info:
00078   ISV = 0, ISS = 0x00000005
00078   CM = 0, WnR = 0
00078 user pgtable: 4k pages, 39-bit VAs, pgdp=000000007ed64000
00078 [000000000000009e] pgd=0000000000000000, p4d=0000000000000000, pud=0000000000000000
00078 Internal error: Oops: 0000000096000005 [#1] SMP
00078 Modules linked in:
00078 CPU: 75 PID: 1170 Comm: stress-ng-utime Not tainted 6.3.0-ktest-g5ef5b466e77e #2078
00078 Hardware name: linux,dummy-virt (DT)
00078 pstate: 80001005 (Nzcv daif -PAN -UAO -TCO -DIT +SSBS BTYPE=--)
00078 pc : btree_node_sort+0xc4/0x568
00078 lr : bch2_btree_post_write_cleanup+0x6c/0x1c0
00078 sp : ffffff803e30b350
00078 x29: ffffff803e30b350 x28: 0000000000000001 x27: ffffff80076e52a8
00078 x26: 0000000000000002 x25: 0000000000000000 x24: ffffffc00912e000
00078 x23: ffffff80076e52a8 x22: 0000000000000000 x21: ffffff80076e52bc
00078 x20: ffffff80076e5200 x19: 0000000000000000 x18: 0000000000000000
00078 x17: fffffffff8000000 x16: 0000000008000000 x15: 0000000008000000
00078 x14: 0000000000000002 x13: 0000000000000000 x12: 00000000000000a0
00078 x11: ffffff803e30b400 x10: ffffff803e30b408 x9 : 0000000000000001
00078 x8 : 0000000000000000 x7 : ffffff803e480000 x6 : 00000000000000a0
00078 x5 : 0000000000000088 x4 : 0000000000000000 x3 : 0000000000000010
00078 x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffffff80076e52a8
00078 Call trace:
00078  btree_node_sort+0xc4/0x568
00078  bch2_btree_post_write_cleanup+0x6c/0x1c0
00078  bch2_btree_node_write+0x108/0x148
00078  __btree_node_flush+0x104/0x160
00078  bch2_btree_node_flush0+0x1c/0x30
00078  journal_flush_pins.constprop.0+0x184/0x2d0
00078  __bch2_journal_reclaim+0x4d4/0x508
00078  bch2_journal_reclaim+0x1c/0x30
00078  __bch2_journal_preres_get+0x244/0x268
00078  bch2_trans_journal_preres_get_cold+0xa4/0x180
00078  __bch2_trans_commit+0x61c/0x1bb0
00078  bch2_setattr_nonsize+0x254/0x318
00078  bch2_setattr+0x5c/0x78
00078  notify_change+0x2bc/0x408
00078  vfs_utimes+0x11c/0x218
00078  do_utimes+0x84/0x140
00078  __arm64_sys_utimensat+0x68/0xa8
00078  invoke_syscall.constprop.0+0x54/0xf0
00078  do_el0_svc+0x48/0xd8
00078  el0_svc+0x14/0x48
00078  el0t_64_sync_handler+0xb0/0xb8
00078  el0t_64_sync+0x14c/0x150
00078 Code: 8b050265 910020c6 8b060266 910060ac (79402cad)
00078 ---[ end trace 0000000000000000 ]---

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: alloc_v4_u64s() fix
Kent Overstreet [Sat, 20 May 2023 06:20:28 +0000 (02:20 -0400)]
bcachefs: alloc_v4_u64s() fix

With the recent bkey_ops.min_val_size addition, bkey values are
automatically extended to the size of the current version.

The check in bch2_alloc_v4_invalid() needs to be updated to take this
into account.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Delete an incorrect bch2_trans_unlock()
Kent Overstreet [Mon, 15 May 2023 03:01:14 +0000 (23:01 -0400)]
bcachefs: Delete an incorrect bch2_trans_unlock()

These deletes a bch2_trans_unlock() call from __bch2_move_data(). It was
redundant; bch2_move_extent() has the correct unlock call, and it was
buggy because when move_extent calls bch2_extent_drop_ptrs() we don't
want the transaction to be unlocked yet - this fixes a btree_iter.c
assertion.

Fixes https://github.com/koverstreet/bcachefs/issues/511.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Use memcpy_u64s_small() for copying keys
Kent Overstreet [Sat, 13 May 2023 21:21:55 +0000 (17:21 -0400)]
bcachefs: Use memcpy_u64s_small() for copying keys

Small performance optimization; an open coded loop is better than rep ;
movsq for small copies.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix check_overlapping_extents()
Kent Overstreet [Sat, 13 May 2023 04:11:14 +0000 (00:11 -0400)]
bcachefs: Fix check_overlapping_extents()

A error check had a flipped conditional - whoops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Replace a BUG_ON() with fatal error
Kent Overstreet [Sat, 13 May 2023 00:28:54 +0000 (20:28 -0400)]
bcachefs: Replace a BUG_ON() with fatal error

A user hit this BUG_ON() - it's unclear how it happened, so replace it
with a fatal error that will cause us to go read only, and print out
more information.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Delete some dead code in bch2_replicas_gc_end()
Kent Overstreet [Mon, 8 May 2023 18:23:08 +0000 (14:23 -0400)]
bcachefs: Delete some dead code in bch2_replicas_gc_end()

bch2_replicas_gc_(start|end) is now only used for journal replicas
entries, which don't have bucket sector counts - so this code is
entirely dead and can be deleted.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: mark journal replicas before journal write submission
Brian Foster [Thu, 4 May 2023 16:44:15 +0000 (12:44 -0400)]
bcachefs: mark journal replicas before journal write submission

The journal write submission path marks the associated replica
entries for journal data in journal_write_done(), which is just
after journal write bio submission. This creates a small window
where journal entries might have been written out, but the
associated replica is not marked such that recovery does not know
that the associated device contains journal data.

Move the replica marking a bit earlier in the write path such that
recovery is guaranteed to recognize that the device contains journal
data in the event of a crash.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improved comment for bch2_replicas_gc2()
Kent Overstreet [Tue, 2 May 2023 22:22:12 +0000 (18:22 -0400)]
bcachefs: Improved comment for bch2_replicas_gc2()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix quotas + snapshots
Kent Overstreet [Fri, 28 Apr 2023 07:50:57 +0000 (03:50 -0400)]
bcachefs: Fix quotas + snapshots

Now that we can reliably designate and find the master subvolume out of
a tree of snapshots, we can finally make quotas work with snapshots:

That is - quotas will now _ignore_ snapshot subvolumes, and only be in
effect for the master (non snapshot) subvolume.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add otime, parent to bch_subvolume
Kent Overstreet [Wed, 29 Mar 2023 15:18:59 +0000 (11:18 -0400)]
bcachefs: Add otime, parent to bch_subvolume

Add two new fields to bch_subvolume:
 - otime: creation time
 - parent: For snapshots, this is the id of the subvolume the snapshot
   was created from

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: BTREE_ID_snapshot_tree
Kent Overstreet [Wed, 29 Mar 2023 15:18:52 +0000 (11:18 -0400)]
bcachefs: BTREE_ID_snapshot_tree

This adds a new btree which gets us a persistent per-snapshot-tree
identifier.

 - BTREE_ID_snapshot_trees
 - KEY_TYPE_snapshot_tree
 - bch_snapshot now has a field that points to a snapshot_tree

This is going to be used to designate one snapshot ID/subvolume out of a
given tree of snapshots as the "main" subvolume, so that we can do quota
accounting in that subvolume and not the rest.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_bkey_get_empty_slot()
Kent Overstreet [Fri, 28 Apr 2023 03:20:18 +0000 (23:20 -0400)]
bcachefs: bch2_bkey_get_empty_slot()

Add a new helper for allocating a new slot in a btree.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_bkey_make_mut() now calls bch2_trans_update()
Kent Overstreet [Sun, 30 Apr 2023 23:21:06 +0000 (19:21 -0400)]
bcachefs: bch2_bkey_make_mut() now calls bch2_trans_update()

It's safe to call bch2_trans_update with a k/v pair where the value
hasn't been filled out, as long as the key part has been and the value
is filled out by transaction commit time.

This patch folds the bch2_trans_update() call into bch2_bkey_make_mut(),
eliminating a bit of boilerplate.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_bkey_get_mut() now calls bch2_trans_update()
Kent Overstreet [Sun, 30 Apr 2023 22:46:24 +0000 (18:46 -0400)]
bcachefs: bch2_bkey_get_mut() now calls bch2_trans_update()

It's safe to call bch2_trans_update with a k/v pair where the value
hasn't been filled out, as long as the key part has been and the value
is filled out by transaction commit time.

This patch folds the bch2_trans_update() call into bch2_bkey_get_mut(),
eliminating a bit of boilerplate.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_bkey_alloc() now calls bch2_trans_update()
Kent Overstreet [Sun, 30 Apr 2023 22:59:28 +0000 (18:59 -0400)]
bcachefs: bch2_bkey_alloc() now calls bch2_trans_update()

It's safe to call bch2_trans_update with a k/v pair where the value
hasn't been filled out, as long as the key part has been and the value
is filled out by transaction commit time.

This patch folds the bch2_trans_update() call into bch2_bkey_alloc(),
eliminating a bit of boilerplate.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_bkey_get_mut() improvements
Kent Overstreet [Fri, 28 Apr 2023 03:48:33 +0000 (23:48 -0400)]
bcachefs: bch2_bkey_get_mut() improvements

 - bch2_bkey_get_mut() now handles types increasing in size, allocating
   a buffer for the type's current size when necessary
 - bch2_bkey_make_mut_typed()
 - bch2_bkey_get_mut() now initializes the iterator, like
   bch2_bkey_get_iter()

Also, refactor so that most of the code is in functions - now macros are
only used for wrappers.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Move bch2_bkey_make_mut() to btree_update.h
Kent Overstreet [Mon, 1 May 2023 00:58:59 +0000 (20:58 -0400)]
bcachefs: Move bch2_bkey_make_mut() to btree_update.h

It's for doing updates - this is where it belongs, and next pathes will
be changing these helpers to use items from btree_update.h.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_bkey_get_iter() helpers
Kent Overstreet [Sat, 29 Apr 2023 23:33:09 +0000 (19:33 -0400)]
bcachefs: bch2_bkey_get_iter() helpers

Introduce new helpers for a common pattern:

  bch2_trans_iter_init();
  bch2_btree_iter_peek_slot();

 - bch2_bkey_get_iter_type() returns -ENOENT if it doesn't find a key of
   the correct type
 - bch2_bkey_get_val_typed() copies the val out of the btree to a
   (typically stack allocated) variable; it handles the case where the
   value in the btree is smaller than the current version of the type,
   zeroing out the remainder.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bkey_ops.min_val_size
Kent Overstreet [Sat, 29 Apr 2023 17:24:18 +0000 (13:24 -0400)]
bcachefs: bkey_ops.min_val_size

This adds a new field to bkey_ops for the minimum size of the value,
which standardizes that check and also enforces the new rule (previously
done somewhat ad-hoc) that we can extend value types by adding new
fields on to the end.

To make that work we do _not_ initialize min_val_size with sizeof,
instead we initialize it to the size of the first version of those
values.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Converting to typed bkeys is now allowed for err, null ptrs
Kent Overstreet [Sun, 30 Apr 2023 17:02:05 +0000 (13:02 -0400)]
bcachefs: Converting to typed bkeys is now allowed for err, null ptrs

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Btree iterator, update flags no longer conflict
Kent Overstreet [Sun, 30 Apr 2023 22:04:43 +0000 (18:04 -0400)]
bcachefs: Btree iterator, update flags no longer conflict

Change btree_update_flags to start after the last btree iterator flag,
so that we can pass both in the same flags argument.

This is needed for the upcoming bch2_bkey_get_mut() helper.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: remove unused key cache coherency flag
Brian Foster [Mon, 1 May 2023 13:08:26 +0000 (09:08 -0400)]
bcachefs: remove unused key cache coherency flag

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fix accounting corruption race between reclaim and dev add
Brian Foster [Mon, 1 May 2023 11:09:33 +0000 (07:09 -0400)]
bcachefs: fix accounting corruption race between reclaim and dev add

When a device is removed from a bcachefs volume, the associated
content is removed from the various btrees. The alloc tree uses the
key cache, so when keys are removed the deletes exist in cache for a
period of time until reclaim comes along and flushes outstanding
updates.

When a device is re-added to the bcachefs volume, the add process
re-adds some of these previously deleted keys. When marking device
superblock locations on device add, the keys will likely refer to
some of the same alloc keys that were just removed. The memory
triggers for these key updates are responsible for further updates,
such as bch2_mark_alloc() calling into bch2_dev_usage_update() to
update per-device usage accounting.

When a new key is added to key cache, the trans update path also
flushes the key to the backing btree for coherency reasons for tree
walks.

With all of this context, if a device is removed and re-added
quickly enough such that some key deletes from the remove are still
pending a key cache flush, the trans update path can view this as
addition of a new key because the old key in the insert entry refers
to a deleted key. However the deleted cached key has not been filled
by absence of a btree key, but rather refers to an explicit deletion
of an existing key that occurred during device removal.

The trans update path adds a new update to flush the key and tags
the original (cached) update to skip running the memory triggers.
This results in running triggers on the non-cached update instead,
which in turn will perform accounting updates based on incoherent
values. For example, bch2_dev_usage_update() subtracts the the old
alloc key dirty sector count in the non-cached btree key from the
newly initialized (i.e. zeroed) per device counters, leading to
underflow and accounting corruption.

There are at least a few ways to avoid this problem, the simplest of
which may be to run triggers against the cached update rather than
the non-cached update. If the key only needs to be flushed when the
key is not present in the tree, however, then this still performs an
unnecessary update. We could potentially use the cached key dirty
state to determine whether the delete is a dirty, cached update vs.
a clean cache fill, but this may require transmitting key cache
dirty state across layers, which adds complexity and seems to be of
limited value. Instead, update flush_new_cached_update() to handle
this by simply checking for the key in the btree and only perform
the flush when a backing key is not present.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Mark bch2_copygc() noinline
Kent Overstreet [Sat, 29 Apr 2023 20:21:51 +0000 (16:21 -0400)]
bcachefs: Mark bch2_copygc() noinline

This works around a "stack from too large" error.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Delete obsolete btree ptr check
Kent Overstreet [Thu, 27 Apr 2023 18:02:31 +0000 (14:02 -0400)]
bcachefs: Delete obsolete btree ptr check

This patch deletes a .key_invalid check for btree pointers that only
applies to _very_ old on disk format versions, and potentially
complicates the upgrade process.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Always run topology error when CONFIG_BCACHEFS_DEBUG=y
Kent Overstreet [Wed, 26 Apr 2023 21:47:09 +0000 (17:47 -0400)]
bcachefs: Always run topology error when CONFIG_BCACHEFS_DEBUG=y

Improved test coverage.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a userspace build error
Kent Overstreet [Wed, 26 Apr 2023 21:47:00 +0000 (17:47 -0400)]
bcachefs: Fix a userspace build error

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Make sure hash info gets initialized in fsck
Kent Overstreet [Tue, 25 Apr 2023 18:32:39 +0000 (14:32 -0400)]
bcachefs: Make sure hash info gets initialized in fsck

We had some bugs with setting/using first_this_inode in the inode
walker in the dirents/xattr code.

This patch changes to not clear first_this_inode until after
initializing the new hash info.

Also, we fix an error message to not print on transaction restart, and
add a comment to related fsck error code.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Kill bch2_verify_bucket_evacuated()
Kent Overstreet [Fri, 21 Apr 2023 07:42:41 +0000 (03:42 -0400)]
bcachefs: Kill bch2_verify_bucket_evacuated()

With backpointers, it's now impossible for bch2_evacuate_bucket() to be
completely reliable: it can race with an extent being partially
overwritten or split, which needs a new write buffer flush for the
backpointer to be seen.

This shouldn't be a real issue in practice; the previous patch added a
new tracepoint so we'll be able to see more easily if it is.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve move path tracepoints
Kent Overstreet [Thu, 20 Apr 2023 19:24:07 +0000 (15:24 -0400)]
bcachefs: Improve move path tracepoints

Move path tracepoints now include the key being moved. Also, add new
tracepoints for the start of move_extent, and evacuate_bucket.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Drop a redundant error message
Kent Overstreet [Fri, 21 Apr 2023 07:33:45 +0000 (03:33 -0400)]
bcachefs: Drop a redundant error message

When we're already read-only, we don't need to print out errors from
writing btree nodes.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: remove bucket_gens btree keys on device removal
Brian Foster [Wed, 19 Apr 2023 15:47:03 +0000 (11:47 -0400)]
bcachefs: remove bucket_gens btree keys on device removal

If a device has keys in the bucket_gens btree associated with its
buckets and is removed from a bcachefs volume, fsck will complain
about the presence of keys associated with an invalid device index.
A repair removes the associated keys and restores correctness.
Update bch2_dev_remove_alloc() to remove device related keys at
device removal time to avoid the problem.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fix NULL bch_dev deref when checking bucket_gens keys
Brian Foster [Tue, 18 Apr 2023 17:05:47 +0000 (13:05 -0400)]
bcachefs: fix NULL bch_dev deref when checking bucket_gens keys

fsck removes bucket_gens keys for devices that do not exist in the
volume (i.e., if the device was removed). In 'fsck -n' mode, the
associated fsck_err_on() wrapper returns false to skip the key
removal. This proceeds on to the rest of the function, which
eventually segfaults on a NULL bch_dev because the device does not
exist.

Update bch2_check_bucket_gens_key() to skip out of the rest of the
function when the associated device does not exist, regardless of
running fsck in check or repair mode.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: folio pos to bch_folio_sector index helper
Brian Foster [Mon, 3 Apr 2023 12:17:26 +0000 (08:17 -0400)]
bcachefs: folio pos to bch_folio_sector index helper

Create a small helper to translate from file offset to the
associated bch_folio_sector index in the underlying bch_folio. The
helper assumes the file offset is covered by the passed folio.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a null ptr deref in fsck check_extents()
Kent Overstreet [Mon, 17 Apr 2023 01:49:12 +0000 (21:49 -0400)]
bcachefs: Fix a null ptr deref in fsck check_extents()

It turns out, in rare situations we need to be passing in a disk
reservation, which will be used internally by the transaction commit
path when needed. Pass one in...

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix a slab-out-of-bounds
Kent Overstreet [Sun, 16 Apr 2023 11:10:46 +0000 (07:10 -0400)]
bcachefs: Fix a slab-out-of-bounds

In __bch2_alloc_to_v4_mut(), we overrun the buffer we allocate if the
alloc key had backpointers stored in it (which we no longer support).

Fix this with a max() call.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Allow answering y or n to all fsck errors of given type
Kent Overstreet [Sat, 15 Apr 2023 18:26:14 +0000 (14:26 -0400)]
bcachefs: Allow answering y or n to all fsck errors of given type

This changes the ask_yn() function used by fsck to accept Y or N,
meaning yes or no for all errors of a given type.

With this, the user can be prompted only for distinct error types -
useful when a filesystem has lots of errors.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: use u64 for folio end pos to avoid overflows
Brian Foster [Wed, 29 Mar 2023 14:43:23 +0000 (10:43 -0400)]
bcachefs: use u64 for folio end pos to avoid overflows

Some of the folio_end_*() helpers are prone to overflow of signed
64-bit types because the mapping is only limited by the max value of
loff_t and the associated helpers return the start offset of the
next folio. Therefore, a folio_end_pos() of the max allowable folio in a
mapping returns a value that overflows loff_t.

This makes it hard to rely on such values when doing folio
processing across a range of a file, as bcachefs attempts to do with
the recent folio changes. For example, generic/564 causes problems
in the buffered write path when testing writes at max boundary
conditions.

The current understanding is that the pagecache historically limited
the mapping to one less page to avoid this problem and this was
dropped with some of the folio conversions, but may be reinstated to
properly address the problem. In the meantime, update the internal
folio_end_*() helpers in bcachefs to return a u64, and all of the
associated code to use or cast to u64 to avoid overflow problems.
This allows generic/564 to pass and can be reverted back to using
loff_t if at any point the pagecache subsystem can guarantee these
boundary conditions will not overflow.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: clean up post-eof folios on -ENOSPC
Brian Foster [Wed, 29 Mar 2023 15:23:15 +0000 (11:23 -0400)]
bcachefs: clean up post-eof folios on -ENOSPC

The buffered write path batches folio creations in the file mapping
based on the requested size of the write. Under low free space
conditions, it is possible to add a bunch of folios to the mapping
and then return a short write or -ENOSPC due to lack of space. If
this occurs on an extending write, the file size is updated based on
the amount of data successfully written to the file. If folios were
added beyond the final i_size, they may hang around until reclaimed,
truncated or encountered unexpectedly by another operation.

For example, generic/083 reproduces a sequence of events where a
short write leaves around one or more post-EOF folios on an inode, a
subsequent zero range request extends beyond i_size and overlaps
with an aforementioned folio, and __bch2_truncate_folio() happens
across it and complains.

Update __bch2_buffered_write() to keep track of the start offset of
the last folio added to the mapping for a prospective write. After
i_size is updated, check whether this offset starts beyond EOF. If
so, truncate pagecache beyond the latest EOF to clean up any folios
that don't reside at least partially within EOF upon completion of
the write.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: fix truncate overflow if folio is beyond EOF
Brian Foster [Wed, 29 Mar 2023 13:49:04 +0000 (09:49 -0400)]
bcachefs: fix truncate overflow if folio is beyond EOF

generic/083 occasionally reproduces a panic caused by an overflow
when accessing the bch_folio_sector array of the folio being
processed by __bch2_truncate_folio(). The immediate cause of the
overflow is that the folio offset is beyond i_size, and therefore
the sector index calculation underflows on subtraction of the folio
offset.

One cause of this is mainly observed on nocow mounts. When nocow is
enabled, fallocate performs physical block allocation (as opposed to
block reservation in cow mode), which range_has_data() then
interprets as valid data that requires partial zeroing on truncate.
Therefore, if a post-eof zero range request lands across post-eof
preallocated blocks, __bch2_truncate_folio() may actually create a
post-eof folio in order to perform zeroing. To avoid this problem,
update range_has_data() to filter out unwritten blocks from folio
creation and partial zeroing.

Even though we should never create folios beyond EOF like this, the
mere existence of such folios is not necessarily a fatal error. Fix
up the truncate code to warn about this condition and not overflow
the sector array and possibly crash the system. The addition of this
warning without the corresponding unwritten extent fix has shown
that various other fstests are able to reproduce this problem fairly
frequently, but often in ways that doesn't necessarily result in a
kernel panic or a change in user observable behavior, and therefore
the problem goes undetected.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Enable large folios
Kent Overstreet [Sun, 19 Mar 2023 20:47:30 +0000 (16:47 -0400)]
bcachefs: Enable large folios

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Check for folios that don't have bch_folio attached
Kent Overstreet [Mon, 27 Mar 2023 20:55:27 +0000 (16:55 -0400)]
bcachefs: Check for folios that don't have bch_folio attached

With large folios, it's now incidentally possible to end up with a
clean, uptodate folio in the page cache that doesn't have a bch_folio
attached, if a folio has to be split.

This patch fixes __bch2_truncate_folio() to check for this; other code
paths appear to handle it.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_readahead() large folio conversion
Kent Overstreet [Fri, 17 Mar 2023 23:24:44 +0000 (19:24 -0400)]
bcachefs: bch2_readahead() large folio conversion

Readahead now uses the new filemap_get_contig_folios_d() helper.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: filemap_get_contig_folios_d()
Kent Overstreet [Thu, 23 Mar 2023 16:51:47 +0000 (12:51 -0400)]
bcachefs: filemap_get_contig_folios_d()

Add a new helper for getting a range of contiguous folios and returning
them in a darray.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch_folio_sector_state improvements
Kent Overstreet [Thu, 23 Mar 2023 15:08:04 +0000 (11:08 -0400)]
bcachefs: bch_folio_sector_state improvements

 - X-macro-ize the bch_folio_sector_state enum: this means we can easily
   generate strings, which is helpful for debugging.

 - Add helpers for state transitions: folio_sector_dirty(),
   folio_sector_undirty(), folio_sector_reserve()

 - Add folio_sector_set(), a single helper for changing folio sector
   state just so that we have a single place to instrument when we're
   debugging.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_truncate_page() large folio conversion
Kent Overstreet [Sun, 19 Mar 2023 22:03:22 +0000 (18:03 -0400)]
bcachefs: bch2_truncate_page() large folio conversion

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_buffered_write large folio conversion
Kent Overstreet [Sun, 19 Mar 2023 01:37:43 +0000 (21:37 -0400)]
bcachefs: bch2_buffered_write large folio conversion

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch_folio can now handle multi-order folios
Kent Overstreet [Fri, 17 Mar 2023 23:46:25 +0000 (19:46 -0400)]
bcachefs: bch_folio can now handle multi-order folios

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: More assorted large folio conversion
Kent Overstreet [Fri, 17 Mar 2023 19:37:34 +0000 (15:37 -0400)]
bcachefs: More assorted large folio conversion

Various misc small conversions in fs-io.c for large folios.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_seek_pagecache_data() folio conversion
Kent Overstreet [Sun, 19 Mar 2023 23:07:28 +0000 (19:07 -0400)]
bcachefs: bch2_seek_pagecache_data() folio conversion

This converts bch2_seek_pagecache_data() to handle large folios.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_seek_pagecache_hole() folio conversion
Kent Overstreet [Sun, 19 Mar 2023 23:06:42 +0000 (19:06 -0400)]
bcachefs: bch2_seek_pagecache_hole() folio conversion

This converts bch2_seek_pagecache_hole() to handle large folios.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bio_for_each_segment_all() -> bio_for_each_folio_all()
Kent Overstreet [Sun, 19 Mar 2023 22:59:21 +0000 (18:59 -0400)]
bcachefs: bio_for_each_segment_all() -> bio_for_each_folio_all()

This converts the writepage end_io path to folios.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Initial folio conversion
Kent Overstreet [Fri, 17 Mar 2023 18:55:53 +0000 (14:55 -0400)]
bcachefs: Initial folio conversion

This converts fs-io.c to pass folios, not pages. We're not handling
large folios yet, there's no functional changes in this patch - just a
lot of churn doing the initial type conversions.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Rename bch_page_state -> bch_folio
Kent Overstreet [Fri, 17 Mar 2023 16:53:15 +0000 (12:53 -0400)]
bcachefs: Rename bch_page_state -> bch_folio

Start of the large folio conversion.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add a bch_page_state assert
Kent Overstreet [Mon, 27 Mar 2023 19:16:24 +0000 (15:16 -0400)]
bcachefs: Add a bch_page_state assert

Seeing an odd bug with page/folio state not being properly initialized,
this is to help track it down.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add a cond_resched() call to journal_keys_sort()
Kent Overstreet [Tue, 4 Apr 2023 17:25:06 +0000 (13:25 -0400)]
bcachefs: Add a cond_resched() call to journal_keys_sort()

We're just doing cpu work here and it could take awhile, a
cond_resched() is definitely needed.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve trace_move_extent_fail()
Kent Overstreet [Fri, 10 Mar 2023 22:34:29 +0000 (17:34 -0500)]
bcachefs: Improve trace_move_extent_fail()

This greatly expands the move_extent_fail tracepoint - now it includes
all the information we have available, including exactly why the extent
wasn't updated.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Print out counters correctly
Kent Overstreet [Thu, 30 Mar 2023 22:49:02 +0000 (18:49 -0400)]
bcachefs: Print out counters correctly

Most counters aren't in units of sectors, and the ones that are should
just be switched to bytes, for simplicity.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Add missing bch2_err_class() call
Kent Overstreet [Fri, 31 Mar 2023 00:16:06 +0000 (20:16 -0400)]
bcachefs: Add missing bch2_err_class() call

We're not supposed to return our private error codes to userspace.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Rip out code for storing backpointers in alloc keys
Kent Overstreet [Fri, 31 Mar 2023 20:24:45 +0000 (16:24 -0400)]
bcachefs: Rip out code for storing backpointers in alloc keys

We don't store backpointers in alloc keys anymore, since we gained the
btree write buffer.

This patch drops support for backpointers in alloc keys, and revs the on
disk format version so that we know a fsck is required.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: use reservation for log messages during recovery
Brian Foster [Wed, 22 Mar 2023 12:27:58 +0000 (08:27 -0400)]
bcachefs: use reservation for log messages during recovery

If we block on journal reservation attempting to log journal
messages during recovery, particularly for the first message(s)
before we start doing actual work, chances are the filesystem ends
up deadlocked.

Allow logged messages to use reserved journal space to mitigate this
problem. In the worst case where no space is available whatsoever,
this at least allows the fs to recognize that the journal is stuck
and fail the mount gracefully.

Signed-off-by: Brian Foster <bfoster@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Improve trans_restart_split_race tracepoint
Kent Overstreet [Thu, 30 Mar 2023 20:04:02 +0000 (16:04 -0400)]
bcachefs: Improve trans_restart_split_race tracepoint

Seeing occasional test failures where we get stuck in a livelock that
involves this event - this will help track it down.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Data update path no longer leaves cached replicas
Kent Overstreet [Thu, 30 Mar 2023 02:47:30 +0000 (22:47 -0400)]
bcachefs: Data update path no longer leaves cached replicas

It turns out that it's currently impossible to invalidate buckets
containing only cached data if they're part of a stripe. The normal
bucket invalidate path can't do it because we have to be able to
incerement the bucket's gen, which isn't correct becasue it's still a
member of the stripe - and the bucket invalidate path makes the bucket
availabel for reuse right away, which also isn't correct for buckets in
stripes.

What would work is invalidating cached data by following backpointers,
except that cached replicas don't currently get backpointers - because
they would be awkward for the existing bucket invalidate path to delete
and they haven't been needed elsewhere.

So for the time being, to prevent running out of space in stripes,
switch the data update path to not leave cached replicas; we may revisit
this in the future.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Rhashtable based buckets_in_flight for copygc
Kent Overstreet [Sat, 11 Mar 2023 19:44:41 +0000 (14:44 -0500)]
bcachefs: Rhashtable based buckets_in_flight for copygc

Previously, copygc used a fifo for tracking buckets in flight - this had
the disadvantage of being fixed size, since we pass references to
elements into the move code.

This restructures it to be a hash table and linked list, since with
erasure coding we need to be able to pipeline across an arbitrary number
of buckets.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Use BTREE_ITER_INTENT in ec_stripe_update_extent()
Kent Overstreet [Wed, 29 Mar 2023 17:10:36 +0000 (13:10 -0400)]
bcachefs: Use BTREE_ITER_INTENT in ec_stripe_update_extent()

This adds a flags param to bch2_backpointer_get_key() so that we can
pass BTREE_ITER_INTENT, since ec_stripe_update_extent() is updating the
extent immediately.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: move snapshot_t to subvolume_types.h
Kent Overstreet [Wed, 29 Mar 2023 15:01:12 +0000 (11:01 -0400)]
bcachefs: move snapshot_t to subvolume_types.h

this doesn't need to be in bcachefs.h

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Fix bch2_get_key_or_hole()
Kent Overstreet [Tue, 28 Mar 2023 23:37:25 +0000 (19:37 -0400)]
bcachefs: Fix bch2_get_key_or_hole()

This fixes an off by one error, due to confusing closed vs. half open
intervals.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: Check return code from need_whiteout_for_snapshot()
Kent Overstreet [Tue, 28 Mar 2023 23:15:53 +0000 (19:15 -0400)]
bcachefs: Check return code from need_whiteout_for_snapshot()

This could return a transaction restart; we need to check for that.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
18 months agobcachefs: bch2_dev_freespace_init() Print out status every 10 seconds
Kent Overstreet [Thu, 23 Mar 2023 01:22:51 +0000 (21:22 -0400)]
bcachefs: bch2_dev_freespace_init() Print out status every 10 seconds

It appears freespace init can still take awhile, and we've had a report
or two of it getting stuck - let's have it print out where it's at every
10 seconds.

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