linux-2.6-microblaze.git
11 months agobcachefs: Add distinct error code for key_cache_upgrade
Kent Overstreet [Thu, 4 Aug 2022 16:46:37 +0000 (12:46 -0400)]
bcachefs: Add distinct error code for key_cache_upgrade

This aids in debugging.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix not punting to worqueue when promoting
Kent Overstreet [Tue, 26 Jul 2022 04:50:25 +0000 (00:50 -0400)]
bcachefs: Fix not punting to worqueue when promoting

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: fsck: Fix nested transaction handling
Kent Overstreet [Fri, 22 Jul 2022 10:57:05 +0000 (06:57 -0400)]
bcachefs: fsck: Fix nested transaction handling

This uses the new trans->restart count to make sure we always correctly
return -BCH_ERR_transaction_restart_nested when we restart a nested
transaction - eliminating some other hacks and preparing for new
assertions.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Add an O_DIRECT option (for userspace)
Kent Overstreet [Thu, 21 Jul 2022 19:41:29 +0000 (15:41 -0400)]
bcachefs: Add an O_DIRECT option (for userspace)

Sometimes we see IO errors due to O_DIRECT alignment issues - having an
option to use buffered IO will be helpful.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Tighten up btree_path assertions
Kent Overstreet [Thu, 21 Jul 2022 13:53:28 +0000 (09:53 -0400)]
bcachefs: Tighten up btree_path assertions

Currently seeing a very rare and difficult to explain btree_path
inconsistency - this patch adds assertions to the only place that seems
to be missing them.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: bch2_bucket_alloc_trans_early -> for_each_btree_key_norestart
Kent Overstreet [Sun, 17 Jul 2022 06:46:46 +0000 (02:46 -0400)]
bcachefs: bch2_bucket_alloc_trans_early -> for_each_btree_key_norestart

Nested btree transactions require special care, and an upcoming patch is
going to add assertions to that effect. We don't want to be using them
unnecessarily, so this patch switches bch2_bucket_trans_early() to not
handle transaction restarts.

This patch also adds a cursor so that on transaction restart we can
continue scanning from where the previous search for an empty bucket
left off.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix check_i_sectors()
Kent Overstreet [Wed, 20 Jul 2022 21:35:57 +0000 (17:35 -0400)]
bcachefs: Fix check_i_sectors()

bch2_count_inode_sectors() uses for_each_btree_key() internally, which
handles lock restarts - the lockrestart_do() in check_i_sectors() is
redundant, and buggy here since the count that
bch2_count_inode_sectors() returns was interpreted as an error by
lockrestart_do().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert debugfs code to for_each_btree_key2()
Kent Overstreet [Wed, 20 Jul 2022 20:50:26 +0000 (16:50 -0400)]
bcachefs: Convert debugfs code to for_each_btree_key2()

This fixes a bug where we were leaking a transaction restart error to
userspace.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Unit test updates
Kent Overstreet [Wed, 20 Jul 2022 20:25:00 +0000 (16:25 -0400)]
bcachefs: Unit test updates

 - Convert to for_each_btree_key2(), for_each_btree_key_commit(),
   for_each_btree_key_reverse()
 - No more bare bch2_btree_iter_peek(); we're now fault-injection lock
   restarts, so we always need a lockrestart_do() or equivalent.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: for_each_btree_key_reverse()
Kent Overstreet [Wed, 20 Jul 2022 20:13:27 +0000 (16:13 -0400)]
bcachefs: for_each_btree_key_reverse()

This adds a new macro, like for_each_btree_key2(), but for iterating in
reverse order.

Also, change for_each_btree_key2() to properly check the return value of
bch2_btree_iter_advance().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert fsck errors to errcode.h
Kent Overstreet [Tue, 19 Jul 2022 21:20:18 +0000 (17:20 -0400)]
bcachefs: Convert fsck errors to errcode.h

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Inject transaction restarts in debug mode
Kent Overstreet [Mon, 18 Jul 2022 00:22:30 +0000 (20:22 -0400)]
bcachefs: Inject transaction restarts in debug mode

In CONFIG_BCACHEFS_DEBUG mode, we'll now randomly issue transaction
restarts - with a decaying probability based on the number of restarts
we've already had, to ensure that transactions eventually make forward
progress. This should help shake out some bugs.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: EINTR -> BCH_ERR_transaction_restart
Kent Overstreet [Mon, 18 Jul 2022 03:06:38 +0000 (23:06 -0400)]
bcachefs: EINTR -> BCH_ERR_transaction_restart

Now that we have error codes, with subtypes, we can switch to our own
error code for transaction restarts - and even better, a distinct error
code for each transaction restart reason: clearer code and better
debugging.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: btree_trans_too_many_iters() is now a transaction restart
Kent Overstreet [Tue, 5 Jul 2022 21:27:44 +0000 (17:27 -0400)]
bcachefs: btree_trans_too_many_iters() is now a transaction restart

All transaction restarts need a tracepoint - this is essential for
debugging

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Prevent a btree iter overflow in alloc path
Kent Overstreet [Tue, 19 Jul 2022 18:51:52 +0000 (14:51 -0400)]
bcachefs: Prevent a btree iter overflow in alloc path

In bch2_bucket_alloc_trans(), we're iterating over buckets - but not
directly with an iterator, since we're iterating over the freespace
btree.

This means that we need to clear iter->path->preserve, otherwise we'll
end up retaining a btree_path for every alloc key we touched - which is
not what we want here.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Use bch2_err_str() in error messages
Kent Overstreet [Mon, 18 Jul 2022 23:42:58 +0000 (19:42 -0400)]
bcachefs: Use bch2_err_str() in error messages

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improved errcodes
Kent Overstreet [Mon, 18 Jul 2022 02:31:21 +0000 (22:31 -0400)]
bcachefs: Improved errcodes

Instead of overloading standard error codes (EINTR/EAGAIN), and defining
short lists of error codes in multiple places that potentially end up
overlapping & conflicting, we're now going to have one master list of
error codes.

Error codes are defined with an x-macro: thus we also have
bch2_err_str() now.

Also, error codes have a class field. Now, instead of checking for
errors with ==, code should use bch2_err_matches(), which returns true
if the error is equal to or a sub-error of the error class.

This means we can define unique errors for every source location where
an error is generated, which will help improve our error messages.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: We can handle missing btree roots for all alloc btrees
Kent Overstreet [Thu, 23 Jun 2022 03:06:16 +0000 (23:06 -0400)]
bcachefs: We can handle missing btree roots for all alloc btrees

We can rebuild alloc info if these btree roots are missing - no need to
bail out and say the filesystem is unrecoverable

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix should_invalidate_buckets()
Kent Overstreet [Mon, 18 Jul 2022 02:59:01 +0000 (22:59 -0400)]
bcachefs: Fix should_invalidate_buckets()

Like bch2_copygc_wait_amount, should_invalidate_buckets() needs to try
to ensure that there are always more buckets free than the largest
reserve.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: ec_stripe_bkey_insert() -> for_each_btree_key_norestart()
Kent Overstreet [Mon, 18 Jul 2022 00:08:37 +0000 (20:08 -0400)]
bcachefs: ec_stripe_bkey_insert() -> for_each_btree_key_norestart()

With the upcoming patches to add assertions for incorrect nested
transaction restart handling, this code is now bogus. Switch it to
for_each_btree_key_norestart() so that transaction restarts are only
handled in one place.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert erasure coding to for_each_btree_key_commit()
Kent Overstreet [Sun, 17 Jul 2022 04:44:19 +0000 (00:44 -0400)]
bcachefs: Convert erasure coding to for_each_btree_key_commit()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Add a counter for btree_trans restarts
Kent Overstreet [Sun, 17 Jul 2022 23:35:38 +0000 (19:35 -0400)]
bcachefs: Add a counter for btree_trans restarts

This will help us improve nested transactions - we need to add
assertions that whenever an inner transaction handles a restart, it
still returns -EINTR to the outer transaction.

This also adds nested_lockrestart_do() and nested_commit_do() which use
the new counters to correctly return -EINTR when the transaction was
restarted.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert alloc code to for_each_btree_key_commit()
Kent Overstreet [Sun, 17 Jul 2022 04:44:19 +0000 (00:44 -0400)]
bcachefs: Convert alloc code to for_each_btree_key_commit()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert subvol code to for_each_btree_key_commit()
Kent Overstreet [Sun, 17 Jul 2022 04:44:19 +0000 (00:44 -0400)]
bcachefs: Convert subvol code to for_each_btree_key_commit()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert bch2_dev_usrdata_drop() to for_each_btree_key2()
Kent Overstreet [Sun, 17 Jul 2022 04:31:40 +0000 (00:31 -0400)]
bcachefs: Convert bch2_dev_usrdata_drop() to for_each_btree_key2()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Convert bch2_do_invalidates_work() to for_each_btree_key2()
Kent Overstreet [Sun, 17 Jul 2022 04:31:40 +0000 (00:31 -0400)]
bcachefs: Convert bch2_do_invalidates_work() to for_each_btree_key2()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: bch2_trans_run()
Kent Overstreet [Thu, 14 Jul 2022 06:08:58 +0000 (02:08 -0400)]
bcachefs: bch2_trans_run()

This adds a new helper, bch2_trans_run(), that runs a function with a
btree_transaction context but without handling transaction restarts.
We're adding checks for nested transaction restart handling: when an
inner transaction handles a transaction restart it will still have to
return it to the outer transaction, or else assertions will be popped in
the outer transaction.

But some places don't need restart handling at the outer scope, so this
helper does what they need.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert bch2_gc_done() for_each_btree_key2()
Kent Overstreet [Sun, 17 Jul 2022 04:44:19 +0000 (00:44 -0400)]
bcachefs: Convert bch2_gc_done() for_each_btree_key2()

This converts bch2_gc_stripes_done() and bch2_gc_reflink_done() to the
new for_each_btree_key_commit() macro.

The new for_each_btree_key2() and for_each_btree_key_commit() macros
handles transaction retries, allowing us to avoid nested transactions -
which we want to avoid since they're tricky to do completely correctly
and upcoming assertions are going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert more fsck code to for_each_btree_key2()
Kent Overstreet [Sun, 17 Jul 2022 04:44:19 +0000 (00:44 -0400)]
bcachefs: Convert more fsck code to for_each_btree_key2()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert more quota code to for_each_btree_key2()
Kent Overstreet [Sun, 17 Jul 2022 04:44:19 +0000 (00:44 -0400)]
bcachefs: Convert more quota code to for_each_btree_key2()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert bch2_check_lrus() to for_each_btree_key_commit()
Kent Overstreet [Sun, 17 Jul 2022 04:44:19 +0000 (00:44 -0400)]
bcachefs: Convert bch2_check_lrus() to for_each_btree_key_commit()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert bch2_dev_freespace_init() to for_each_btree_key_commit()
Kent Overstreet [Sun, 17 Jul 2022 04:44:19 +0000 (00:44 -0400)]
bcachefs: Convert bch2_dev_freespace_init() to for_each_btree_key_commit()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Convert bch2_do_discards_work() to for_each_btree_key2()
Kent Overstreet [Sun, 17 Jul 2022 04:31:40 +0000 (00:31 -0400)]
bcachefs: Convert bch2_do_discards_work() to for_each_btree_key2()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Improve bucket_alloc_fail tracepoint
Kent Overstreet [Mon, 18 Jul 2022 01:40:39 +0000 (21:40 -0400)]
bcachefs: Improve bucket_alloc_fail tracepoint

We should be printing the number of free buckets, not just the number of
available buckets.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: bch2_mark_alloc(): Do wakeups after updating usage
Kent Overstreet [Mon, 18 Jul 2022 01:33:00 +0000 (21:33 -0400)]
bcachefs: bch2_mark_alloc(): Do wakeups after updating usage

We have an obvious wake up race if we do the wakeup _before_ updating
the counters the thing doing the waiting is reading.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: added lock held time stats
Daniel Hill [Thu, 14 Jul 2022 08:33:09 +0000 (20:33 +1200)]
bcachefs: added lock held time stats

We now record the length of time btree locks are held and expose this in debugfs.

Enabled via CONFIG_BCACHEFS_LOCK_TIME_STATS.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: bch2_time_stats_to_text now indents properly
Daniel Hill [Thu, 14 Jul 2022 08:31:36 +0000 (20:31 +1200)]
bcachefs: bch2_time_stats_to_text now indents properly

Printbufs indentation feature doesn't yet work with '\n' and '\t'. So we've
replaced all instances of '\n' with prt_newline.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: lock time stats prep work.
Daniel Hill [Thu, 14 Jul 2022 06:58:23 +0000 (18:58 +1200)]
bcachefs: lock time stats prep work.

We need the caller name and a place to store our results, btree_trans provides this.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Unlock in bch2_trans_begin() if we've held locks more than 10us
Kent Overstreet [Wed, 13 Jul 2022 10:03:21 +0000 (06:03 -0400)]
bcachefs: Unlock in bch2_trans_begin() if we've held locks more than 10us

We try to ensure we never hold btree locks for too long - bcachefs tries
to be soft realtime. This adds a check when restarting a transaction,
where a transaction restart is cheap - if we've been holding locks for
too long, drop and retake them.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: for_each_btree_key2()
Kent Overstreet [Sat, 16 Jul 2022 00:51:09 +0000 (20:51 -0400)]
bcachefs: for_each_btree_key2()

This introduces two new macros for iterating through the btree, with
transaction restart handling
 - for_each_btree_key2()
 - for_each_btree_key_commit()

Every iteration is now in an implicit transaction, and - as with
lockrestart_do() and commit_do() - returning -EINTR will cause the
transaction to be restarted, at the same key.

This patch converts a bunch of code that was open coding this to these
new macros, saving a substantial amount of code.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix repair for extent past end of inode
Kent Overstreet [Sun, 17 Jul 2022 03:31:28 +0000 (23:31 -0400)]
bcachefs: Fix repair for extent past end of inode

When we find an extent past an inode's i_size, we need to do the
deletion in the inode's snapshot (which will emit a whiteout if
necessary); and we also need to note that we now have an a key at that
position and snapshot, so that we don't go into an infinite loop.

Also, switch to walking inodes in reverse older, oldest snapshot to
newest, so that we emit the fewest whiteouts possible.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: When fsck finds redundant snapshot keys, trigger snapshots cleanup
Kent Overstreet [Sun, 17 Jul 2022 03:21:15 +0000 (23:21 -0400)]
bcachefs: When fsck finds redundant snapshot keys, trigger snapshots cleanup

Fsck now checks for keys in different snapshot IDs that are now
redundant due to other snapshots being deleted - it needs to for its own
algorithms to not get confused.

When it detects this it should re-run the post snapshot deletion cleanup
- this patch does that.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve fsck for subvols/snapshots
Kent Overstreet [Thu, 14 Jul 2022 09:44:10 +0000 (05:44 -0400)]
bcachefs: Improve fsck for subvols/snapshots

 - Bunch of refactoring, and move some code out of
   bch2_snapshots_start() and into bch2_snapshots_check(), for constency
   with the rest of fsck

 - Interior snapshot nodes no longer point to a subvolume; this is so we
   don't end up with dangling subvol references when deleting or require
   scanning the full snapshots btree.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve snapshots_seen
Kent Overstreet [Thu, 14 Jul 2022 06:47:36 +0000 (02:47 -0400)]
bcachefs: Improve snapshots_seen

This makes the snapshots_seen data structure fsck private and improves
it; we now also track the equivalence class for each snapshot id we've
seen, which means we can detect when snapshot deletion hasn't finished
or run correctly (which will otherwise confuse fsck).

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix subvol/snapshot deleting in recovery
Kent Overstreet [Thu, 14 Jul 2022 05:10:24 +0000 (01:10 -0400)]
bcachefs: Fix subvol/snapshot deleting in recovery

fsck doesn't want to run while we're cleaning up deleted snapshots - if
that work needs to be done, we want it to have finished before fsck
runs, otherwise fsck will get confused when it finds multiple keys in
the same snapshot ID equivalence class (i.e. the mechanism that
snapshot deletion uses for cleaning up redundant keys).

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: fsck_inode_rm() shouldn't delete subvols
Kent Overstreet [Thu, 14 Jul 2022 04:44:09 +0000 (00:44 -0400)]
bcachefs: fsck_inode_rm() shouldn't delete subvols

We should never see an inode marked as unlinked that's a subvolume root
(or a directory) in fsck, but even if we do it's not correct for fsck to
delete the subvolume: subvolumes are owned by dirents, and if we find a
dangling subvolume (not marked as unlinked) we want fsck to reattach it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Switch data_update path to snapshot_id_list
Kent Overstreet [Thu, 14 Jul 2022 06:34:48 +0000 (02:34 -0400)]
bcachefs: Switch data_update path to snapshot_id_list

snapshots_seen is becoming private to fsck, and snapshot_id_list is
actually what the data update path needs.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix snapshot deletion
Kent Overstreet [Tue, 12 Jul 2022 13:11:52 +0000 (09:11 -0400)]
bcachefs: Fix snapshot deletion

Snapshots being deleted won't in general have a corresponding subvolume:
this fixes a spurious fsck error where we'd complain about a snapshot
pointing to a missing subvolume - but the subvolume had been deleted,
and the snapshot was pending deletion as well.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Rename __bch2_trans_do() -> commit_do()
Kent Overstreet [Wed, 13 Jul 2022 09:25:29 +0000 (05:25 -0400)]
bcachefs: Rename __bch2_trans_do() -> commit_do()

Better/more descriptive naming, and prep for adding
nested_lockrestart_do() and nested_commit_do().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Silence some fsck errors when reconstructing alloc info
Kent Overstreet [Tue, 12 Jul 2022 01:06:52 +0000 (21:06 -0400)]
bcachefs: Silence some fsck errors when reconstructing alloc info

There's no need to print fsck errors for errors that are expected, and
the user has already opted to repair.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Put some repair messages behind opts->verbose
Kent Overstreet [Tue, 12 Jul 2022 02:32:04 +0000 (22:32 -0400)]
bcachefs: Put some repair messages behind opts->verbose

These messages log the updates we're doing in bch2_check_fix_ptrs(),
which is useful when debugging but not usually needed.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Silence unimportant tracepoints
Kent Overstreet [Tue, 5 Jul 2022 20:46:40 +0000 (16:46 -0400)]
bcachefs: Silence unimportant tracepoints

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix move path when move_stats == NULL
Kent Overstreet [Wed, 29 Jun 2022 21:14:06 +0000 (17:14 -0400)]
bcachefs: Fix move path when move_stats == NULL

This isn't done very often, but it is legitimate

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Get ref on c->writes in move.c
Kent Overstreet [Mon, 20 Jun 2022 23:43:35 +0000 (19:43 -0400)]
bcachefs: Get ref on c->writes in move.c

There's no point reading an extent in order to move it if the write is
going to fail because we're shutting down. This patch changes the move
path so that moving_io now owns a ref on c->writes - as a bonus,
rebalance and copygc will now notice that we're shutting down and exit
quicker.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: move.c refactoring
Kent Overstreet [Mon, 20 Jun 2022 19:40:26 +0000 (15:40 -0400)]
bcachefs: move.c refactoring

 - add bch2_moving_ctxt_(init|exit)
 - split out __bch2_evacutae_bucket() which takes an existing
   moving_ctxt, this will be used for improving copygc performance by
   pipelining across multiple buckets

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: data jobs, including rebalance wait for copygc.
Daniel Hill [Wed, 15 Jun 2022 14:06:43 +0000 (02:06 +1200)]
bcachefs: data jobs, including rebalance wait for copygc.

move_ratelimit() now has a bool that specifies whether we want to
wait for copygc to finish.

When copygc is running, we're probably low on free buckets instead
of consuming the remaining buckets, we want to wait for copygc to
finish.

This should help with performance, and run away bucket fragmentation.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Redo data_update interface
Kent Overstreet [Mon, 13 Jun 2022 23:17:45 +0000 (19:17 -0400)]
bcachefs: Redo data_update interface

This patch significantly cleans up and simplifies the data_update
interface. Instead of only being able to specify a single pointer by
device to rewrite, we're now able to specify any or all of the pointers
in the original extent to be rewrited, as a bitmask.

data_cmd is no more: the various pred functions now just return true if
the extent should be moved/updated. All the data_update path does is
rewrite existing replicas, or add new ones.

This fixes a bug where with background compression on replicated
filesystems, where rebalance -> data_update would incorrectly drop the
wrong old replica, and keep trying to recompress an extent pointer and
each time failing to drop the right replica. Oops.

Now, the data update path doesn't look at the io options to decide which
pointers to keep and which to drop - it only goes off of the
data_update_options passed to it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix bch2_check_alloc_key()
Kent Overstreet [Mon, 27 Jun 2022 00:34:34 +0000 (20:34 -0400)]
bcachefs: Fix bch2_check_alloc_key()

bch2_check_alloc_key() was failing to check buckets that didn't have
alloc keys yet (because they'd never been used) - they still need to be
added to the freespace btree.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve bch2_check_alloc_info
Kent Overstreet [Mon, 20 Jun 2022 02:43:00 +0000 (22:43 -0400)]
bcachefs: Improve bch2_check_alloc_info

 - In check_alloc_key(), previously we were re-initializing iterators
   for the need_discard and freespace btrees for every alloc key we
   checked. But this was causing us to redo lookups into the journal
   keys every time, since those lookups are cached in struct btree_iter.
   This initializes the iterators in bch2_check_alloc_info and passes
   them into check_alloc_key().

 - Make the looping more consistent/efficient in bch2_check_alloc_info()

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Use BTREE_INSERT_LAZY_RW in bch2_check_alloc_info()
Kent Overstreet [Sun, 26 Jun 2022 22:31:51 +0000 (18:31 -0400)]
bcachefs: Use BTREE_INSERT_LAZY_RW in bch2_check_alloc_info()

This runs before we go rw for journal replay, but after we're allowed to
go rw. It might be time to consider killing BTREE_INSERT_LAZY_RW,
though.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Bucket invalidate path improvements
Kent Overstreet [Tue, 21 Jun 2022 02:26:41 +0000 (22:26 -0400)]
bcachefs: Bucket invalidate path improvements

 - invalidate_one_bucket() now returns 1 when we don't have any buckets
   on this device to invalidate, ensuring we don't spin
 - the tracepoint invocation is moved to after the transaction commit,
   and we now include the number of cached sectors in the tracepoint

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Don't BUG_ON() inode link count underflow
Kent Overstreet [Thu, 23 Jun 2022 22:26:01 +0000 (18:26 -0400)]
bcachefs: Don't BUG_ON() inode link count underflow

This switches that assertion to a bch2_trans_inconsistent() call, as it
should be.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Always descend to leaf nodes it btree_gc
Kent Overstreet [Thu, 23 Jun 2022 02:53:43 +0000 (22:53 -0400)]
bcachefs: Always descend to leaf nodes it btree_gc

If a btree node is unreadable, it's the topology repair that fixes that
and it's kicked off by btree_gc, so btree_gc needs to touch every node
and very that they can be read.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: fix __dev_available().
Daniel Hill [Wed, 22 Jun 2022 22:28:30 +0000 (10:28 +1200)]
bcachefs: fix __dev_available().

__dev_available() now calculates available buckets correctly. Previously
it would almost always return 0 when we have cached data.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix assertion in topology repair
Kent Overstreet [Thu, 23 Jun 2022 01:33:43 +0000 (21:33 -0400)]
bcachefs: Fix assertion in topology repair

If we were at the end of the node, when breaking out of the loop we'd
pop the assertion on line 446 when cur wasn't NULL.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Make verbose option settable at runtime
Kent Overstreet [Wed, 22 Jun 2022 21:56:25 +0000 (17:56 -0400)]
bcachefs: Make verbose option settable at runtime

-o verbose is very useful, and we're starting to use it more for runtime
debug statements - making it possible to enable at runtime is a no
brainer.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve "copygc requested to run" error message
Kent Overstreet [Fri, 17 Jun 2022 22:30:17 +0000 (18:30 -0400)]
bcachefs: Improve "copygc requested to run" error message

This improves the "copygc requested to run but no buckets found" to show
the device that requires copygc to be run on - we'll definitely need to
improve this more.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Pull out data_update.c
Kent Overstreet [Mon, 13 Jun 2022 23:07:19 +0000 (19:07 -0400)]
bcachefs: Pull out data_update.c

This is the start of reorganizing the data IO paths. The plan is to also
break apart io.c into data_read.c and data_write.c, and migrate_write
will be renamed to the data_update path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Split out dev_buckets_free()
Kent Overstreet [Tue, 14 Jun 2022 21:51:20 +0000 (17:51 -0400)]
bcachefs: Split out dev_buckets_free()

Previously, dev_buckets_available() only counted buckets that are
eligible to be allocated right now - i.e. buckets that don't have cached
data, or need discard, or need gc gens, etc.

But most users of this function want to know how many buckets are
eligible to be allocated from without moving data around - copygc,
allocator striping, which means we should be including cached data
buckets etc.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: btree key cache pcpu freedlist
Kent Overstreet [Fri, 17 Jun 2022 05:07:54 +0000 (01:07 -0400)]
bcachefs: btree key cache pcpu freedlist

Originally, the btree key cache code would always allocate new entries
by reusing from the recently-freed list, if that list wasn't empty. But
that behaviour was dropped, for lock contention reasons.

But it seems that entries stranded on the freed list have been
contributing to some of our oom issues, because long running btree
transactions will prevent them from being freed.

This patch re-adds allocating from the freed list, but it also adds
percpu buffers to solve the lock contention issues - and the new percpu
freed lists will improve the evict paths, too.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Make IO in flight by copygc/rebalance configurable
Kent Overstreet [Mon, 20 Jun 2022 03:03:07 +0000 (23:03 -0400)]
bcachefs: Make IO in flight by copygc/rebalance configurable

This adds a new option, move_bytes_in_flight, for configuring the amount
of IO in flight by copygc/rebalance - users with many devices in their
filesystem will want to increase this.

In the future we should be smarter about this, but this is an easy
improvement.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Check for extents with too many ptrs
Kent Overstreet [Fri, 17 Jun 2022 02:38:10 +0000 (22:38 -0400)]
bcachefs: Check for extents with too many ptrs

We have a hardcoded maximum on number of pointers in an extent that's
used by some other data structures - notably bch_devs_list - but we
weren't actually checking for it. Oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix refcount leak in bch2_do_invalidates()
Kent Overstreet [Mon, 20 Jun 2022 23:48:16 +0000 (19:48 -0400)]
bcachefs: Fix refcount leak in bch2_do_invalidates()

If we fail to queue the work item because it's already in process, we
need to drop the ref we just took.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Always use percpu_ref_tryget_live() on c->writes
Kent Overstreet [Sat, 18 Jun 2022 23:55:32 +0000 (19:55 -0400)]
bcachefs: Always use percpu_ref_tryget_live() on c->writes

If we're trying to get a ref and the refcount has been killed, it means
we're doing an emergency shutdown - we always want tryget_live().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve checksum error messages
Kent Overstreet [Sat, 18 Jun 2022 23:03:25 +0000 (19:03 -0400)]
bcachefs: Improve checksum error messages

We're seeing checksum errors in the bch2_rechecksum_bio() path - give it
a better error message to help track this down.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improve an error message
Kent Overstreet [Sat, 18 Jun 2022 00:12:02 +0000 (20:12 -0400)]
bcachefs: Improve an error message

When inserting a key type that's not valid for a given btree, we should
print out which btree we were inserting into.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix assertion in bch2_dev_list_add_dev()
Kent Overstreet [Fri, 17 Jun 2022 23:15:26 +0000 (19:15 -0400)]
bcachefs: Fix assertion in bch2_dev_list_add_dev()

We were only allowing 4 devices in a dev_list, not 16.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Increase max size for btree_trans bump allocator
Kent Overstreet [Thu, 19 May 2022 19:29:50 +0000 (15:29 -0400)]
bcachefs: Increase max size for btree_trans bump allocator

With backpointers, alloc keys have gotten bigger, so we're needing more
memory here.

We're probably going to need to go with something more sophisticated
than a bump allocator, but - let's see if we can avoid doing that just
yet.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Add a persistent counter for bucket discards
Kent Overstreet [Tue, 14 Jun 2022 03:32:09 +0000 (23:32 -0400)]
bcachefs: Add a persistent counter for bucket discards

Like the previous patch for bucket invalidates, add another counter for
a core allocator path.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix btree node read retries
Kent Overstreet [Tue, 14 Jun 2022 05:37:16 +0000 (01:37 -0400)]
bcachefs: Fix btree node read retries

b->written wasn't being reset to 0 in the btree node read retry path,
causing decrypting & validation of previously read bsets to not be
re-run - ouch.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Add a persistent counter for bucket invalidation
Kent Overstreet [Mon, 13 Jun 2022 23:45:07 +0000 (19:45 -0400)]
bcachefs: Add a persistent counter for bucket invalidation

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Call bch2_do_invalidates() when going read write
Kent Overstreet [Mon, 13 Jun 2022 23:34:17 +0000 (19:34 -0400)]
bcachefs: Call bch2_do_invalidates() when going read write

Like bch2_do_discards(), we should check if this needs to be done when
going rw.

Also, add some sysfs code for debugging bucket invalidation.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Improved human readable integer parsing
Kent Overstreet [Fri, 10 Jun 2022 23:45:19 +0000 (19:45 -0400)]
bcachefs: Improved human readable integer parsing

Printbufs recently switched to using string_get_size() for printing
integers in human readable units. This updates __bch2_strtoh() to parse
numbers printed by string_get_size() - we now have to handle floating
point numbers, and new unit suffixes.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix freespace initialization
Kent Overstreet [Fri, 10 Jun 2022 23:39:00 +0000 (19:39 -0400)]
bcachefs: Fix freespace initialization

bch2_dev_freespace_init() was using __bch2_trans_do() incorrectly, and
calling bch2_bucket_do_index() with a stale alloc key.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Printbuf rework
Kent Overstreet [Sat, 4 Feb 2023 02:01:40 +0000 (21:01 -0500)]
bcachefs: Printbuf rework

This converts bcachefs to the modern printbuf interface/implementation,
synced with the version to be submitted upstream.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix btree node read error path
Kent Overstreet [Tue, 7 Jun 2022 02:09:11 +0000 (22:09 -0400)]
bcachefs: Fix btree node read error path

We were forgetting to clear the read_in_flight flag - oops. This also
fixes it to not call bch2_fatal_error() before topology repair has had a
chance to do its thing.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix btree_and_journal_iter
Kent Overstreet [Tue, 7 Jun 2022 01:59:34 +0000 (21:59 -0400)]
bcachefs: Fix btree_and_journal_iter

We had a bug where btree_and_journal_iter would return the same key
twice - after deleting it (perhaps because it was present in both the
btree and the journal?)

This reworks btree_and_journal_iter to track the current position, much
like btree_paths, which makes the logic considerably simpler and more
robust.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix for cmd_list_journal
Kent Overstreet [Tue, 7 Jun 2022 03:04:33 +0000 (23:04 -0400)]
bcachefs: Fix for cmd_list_journal

cmd_list_journal wasn't correctly listing the most recent journal
entries as blacklisted - because in the recovery path when just reading
the journal, we were failing to add those to the blacklist table.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Also log overwrites in journal
Kent Overstreet [Sun, 5 Jun 2022 19:32:57 +0000 (15:32 -0400)]
bcachefs: Also log overwrites in journal

Lately we've been doing a lot of debugging by looking at the journal to
see what was changed, and by what code path. This patch adds a new
journal entry type for recording overwrites, so that we don't have to
search backwards through the journal to see what was being overwritten
in order to work out what the triggers were supposed to be doing.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Refactor journal entry adding
Kent Overstreet [Sun, 5 Jun 2022 19:29:00 +0000 (15:29 -0400)]
bcachefs: Refactor journal entry adding

This takes copying the payload out of bch2_journal_add_entry(), which
means we can use it for journal_transaction_name() - also prep work for
journalling overwrites.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Add some missing error messages
Kent Overstreet [Sat, 4 Jun 2022 18:49:02 +0000 (14:49 -0400)]
bcachefs: Add some missing error messages

bch2_opt_parse() was failing to generate error messages in error path.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix memory corruption in encryption path
Kent Overstreet [Fri, 3 Jun 2022 06:34:14 +0000 (02:34 -0400)]
bcachefs: Fix memory corruption in encryption path

When do_encrypt() was passed a vmalloc address and the buffer spanned
more than a single page, we were encrypting/decrypting completely
different pages than the ones intended.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: bch2_trans_reset_updates()
Kent Overstreet [Sun, 29 May 2022 15:38:48 +0000 (11:38 -0400)]
bcachefs: bch2_trans_reset_updates()

Factor out a new helper.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix error checking in bch2_fs_alloc()
Kent Overstreet [Sat, 28 May 2022 20:21:01 +0000 (16:21 -0400)]
bcachefs: Fix error checking in bch2_fs_alloc()

One of the init calls had a ; instead of a ?:, and errors after that got
dropped - oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Print message on btree node read retry success
Kent Overstreet [Thu, 26 May 2022 19:19:20 +0000 (15:19 -0400)]
bcachefs: Print message on btree node read retry success

Right now, we print an error message on btree node read error, and we
print that we're retrying, but we don't explicitly say if the retry
succeeded - this makes things a little clearer.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Fix journal_keys_search() overhead
Kent Overstreet [Sat, 21 May 2022 17:10:39 +0000 (13:10 -0400)]
bcachefs: Fix journal_keys_search() overhead

Previously, on every btree_iter_peek() operation we were searching the
journal keys, doing a full binary search - which was slow.

This patch fixes that by saving our position in the journal keys, so
that we only do a full binary search when moving our position backwards
or a large jump forwards.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Always print when doing journal replay in fsck
Kent Overstreet [Thu, 26 May 2022 01:34:11 +0000 (21:34 -0400)]
bcachefs: Always print when doing journal replay in fsck

This logging improvement helps see when the previous fsck pass has
completed.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Rename group to label for remaining strings.
Daniel Hill [Wed, 25 May 2022 02:57:39 +0000 (14:57 +1200)]
bcachefs: Rename group to label for remaining strings.

Signed-off-by: Daniel Hill <daniel@gluo.nz>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
11 months agobcachefs: Fix encryption path on arm
Kent Overstreet [Tue, 24 May 2022 02:37:01 +0000 (22:37 -0400)]
bcachefs: Fix encryption path on arm

flush_dcache_page() is not a noop on arm, but we were using
virt_to_page() instead of vmalloc_to_page() for an address on the kernel
stack - vmalloc memory, leading to an oops in flush_dcache_page().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
11 months agobcachefs: Switch to key_type_user, not logon
Kent Overstreet [Sat, 14 May 2022 11:00:22 +0000 (07:00 -0400)]
bcachefs: Switch to key_type_user, not logon

The only difference key_type_logon and key_type_user is that
key_type_logon keys can't be read by userspace.

However, userspace has actually been adding keys to both the logon and
user keychains, because userspace fsck requires the keychain interface -
so we might as well just use user and drop the logon keychain.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>