linux-block.git
10 months agobcachefs: Specify filesystem options
Kent Overstreet [Sun, 5 Dec 2021 02:53:13 +0000 (21:53 -0500)]
bcachefs: Specify filesystem options

We've got three types of options now - filesystem, device and inode, and
a given option may belong to more than one of those types.

This patch changes the options to specify explicitly when they're a
filesystem option - in the future we'll probably be adding more device
options.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix copygc sectors_to_move calculation
Kent Overstreet [Sun, 5 Dec 2021 02:52:09 +0000 (21:52 -0500)]
bcachefs: Fix copygc sectors_to_move calculation

With erasure coding, copygc's count of sectors to move was off, which
matters for the debug statement it prints out when it's not able to move
all the data it tried to.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Convert journal sysfs params to regular options
Kent Overstreet [Sun, 5 Dec 2021 01:07:19 +0000 (20:07 -0500)]
bcachefs: Convert journal sysfs params to regular options

This converts journal_write_delay, journal_flush_disabled, and
journal_reclaim_delay to normal filesystems options, and also adds them
to the superblock.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix btree_path leaks in bch2_trans_update()
Kent Overstreet [Wed, 1 Dec 2021 08:47:54 +0000 (03:47 -0500)]
bcachefs: Fix btree_path leaks in bch2_trans_update()

bch2_trans_update() had some dodgy gets() and puts() - this fixes a few
leaks.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Erasure coding fixes
Kent Overstreet [Mon, 29 Nov 2021 21:38:27 +0000 (16:38 -0500)]
bcachefs: Erasure coding fixes

When we added the stripe and stripe_redundancy fields to alloc keys, we
neglected to add them to the functions that convert back and forth with
the in-memory types.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Handle replica marking fsck errors locally
Kent Overstreet [Sun, 28 Nov 2021 20:13:54 +0000 (15:13 -0500)]
bcachefs: Handle replica marking fsck errors locally

This simplifies the code quite a bit and eliminates an inconsistency - a
given bkey doesn't necessarily translate to a single replicas entry for
disk space accounting.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Push c->mark_lock usage down to where it is needed
Kent Overstreet [Sun, 28 Nov 2021 19:31:19 +0000 (14:31 -0500)]
bcachefs: Push c->mark_lock usage down to where it is needed

This changes the bch2_mark_key() and related paths to take mark lock
where it is needed, instead of taking it in the upper transaction commit
path - by pushing down locking we'll be able to handle fsck errors
locally instead of requiring a separate check in the btree_gc code for
replicas being marked.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Kill bch2_replicas_delta_list_marked()
Kent Overstreet [Sun, 28 Nov 2021 19:08:58 +0000 (14:08 -0500)]
bcachefs: Kill bch2_replicas_delta_list_marked()

This changes bch2_trans_fs_usage_apply() to handle failure (replicas
entry missing) by reverting the changes it made - meaning we can make
the main transaction commit path a bit slimmer, and perhaps also
simplify some locking in upcoming patches.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix reflink path for snapshots
Kent Overstreet [Mon, 29 Nov 2021 21:36:50 +0000 (16:36 -0500)]
bcachefs: Fix reflink path for snapshots

make_extent_indirect() was missing the
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE - it's updating the extent in the
original snapshot, not the curret one.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Convert bucket_alloc_ret to negative error codes
Kent Overstreet [Sun, 28 Nov 2021 18:42:05 +0000 (13:42 -0500)]
bcachefs: Convert bucket_alloc_ret to negative error codes

Start a new header, errcode.h, for bcachefs-private error codes - more
error codes will be converted later.

This patch just converts bucket_alloc_ret so that they can be mixed with
standard error codes and passed as ERR_PTR errors - the ec.c code was
doing this already, but incorrectly.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix page state after fallocate
Kent Overstreet [Tue, 23 Nov 2021 23:21:09 +0000 (18:21 -0500)]
bcachefs: Fix page state after fallocate

This tweaks the fallocate code to also update the page cache to reflect
the new on disk reservations, giving us better i_sectors consistency.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix page state when reading into !PageUptodate pages
Kent Overstreet [Tue, 23 Nov 2021 23:17:04 +0000 (18:17 -0500)]
bcachefs: Fix page state when reading into !PageUptodate pages

This patch adds code to read page state before writing to pages that
aren't uptodate, which corrects i_sectors being tempororarily too large
and means we may not need to get a disk reservation.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
# Conflicts:
# fs/bcachefs/fs-io.c

10 months agobcachefs: Kill PAGE_SECTOR_SHIFT
Kent Overstreet [Wed, 24 Nov 2021 01:00:34 +0000 (20:00 -0500)]
bcachefs: Kill PAGE_SECTOR_SHIFT

Replace it with the new, standard PAGE_SECTORS_SHIFT

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Apply workaround for too many btree iters to read path
Kent Overstreet [Wed, 24 Nov 2021 00:00:23 +0000 (19:00 -0500)]
bcachefs: Apply workaround for too many btree iters to read path

Reading from cached data, which calls bch2_bucket_io_time_reset(), is
leading to transaction iterator overflows - this standardizes the
workaround.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix quota support for snapshots
Kent Overstreet [Wed, 27 Oct 2021 17:05:56 +0000 (13:05 -0400)]
bcachefs: Fix quota support for snapshots

Quota support was disabled when snapshots were released, because of some
tricky interactions with snpashots. We're sidestepping that for now -
we're simply disabling quota accounting on snapshot subvolumes.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: SECTOR_DIRTY_RESERVED
Kent Overstreet [Tue, 23 Nov 2021 22:05:56 +0000 (17:05 -0500)]
bcachefs: SECTOR_DIRTY_RESERVED

This fixes another i_sectors accounting bug - we need to differentiate
between dirty writes that overwrite a reservation and dirty writes to
unallocated space - dirty writes to unallocated space increase
i_sectors, dirty writes over a reservation do not.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix i_sectors_leak in bch2_truncate_page
Kent Overstreet [Mon, 22 Nov 2021 17:47:20 +0000 (12:47 -0500)]
bcachefs: Fix i_sectors_leak in bch2_truncate_page

When bch2_truncate_page() discards dirty sectors in the page cache, we
need to account for that - we don't need to account for allocated
sectors because that'll be done by the bch2_fpunch() call when it
updates the btree.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix an i_sectors accounting bug
Kent Overstreet [Mon, 22 Nov 2021 03:34:26 +0000 (22:34 -0500)]
bcachefs: Fix an i_sectors accounting bug

We weren't checking for errors before calling i_sectors_acct()

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix BCH_FS_ERROR flag handling
Kent Overstreet [Sun, 21 Nov 2021 21:15:48 +0000 (16:15 -0500)]
bcachefs: Fix BCH_FS_ERROR flag handling

We were setting BCH_FS_ERROR on startup if the superblock was marked as
containing errors, which is not what we wanted - BCH_FS_ERROR indicates
whether errors have been found, so that after a successful fsck we're
able to clear the error bit in the superblock.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Add a bit of missing repair code
Kent Overstreet [Sun, 21 Nov 2021 03:59:25 +0000 (22:59 -0500)]
bcachefs: Add a bit of missing repair code

This adds repair code to drop very stale pointers.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix error reporting from bch2_journal_flush_seq
Kent Overstreet [Mon, 15 Nov 2021 22:30:11 +0000 (17:30 -0500)]
bcachefs: Fix error reporting from bch2_journal_flush_seq

- bch2_journal_halt() was unconditionally overwriting j->err_seq, the
  sequence number that we failed to write
- journal_write_done was updating seq_ondisk and flushed_seq_ondisk even
  for writes that errored, which broke the way bch2_journal_flush_seq_async()
  locklessly checked for completions.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Run insert triggers before overwrite triggers
Kent Overstreet [Wed, 27 Oct 2021 16:51:12 +0000 (12:51 -0400)]
bcachefs: Run insert triggers before overwrite triggers

Currently, btree triggers are run in natural key order, which presents a
problem for fallocate in INSERT_RANGE mode: since we're moving existing
extents to higher offsets, the trigger for deleting the old extent runs
before the trigger that adds the new extent, potentially leading to
indirect extents being deleted that shouldn't be when the delete causes
the refcount to hit 0.

This changes the order we run triggers so that for a givin btree, we run
all insert triggers before overwrite triggers, nicely sidestepping this
issue.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Disk space accounting fix on brand-new fs
Kent Overstreet [Mon, 15 Nov 2021 20:02:13 +0000 (15:02 -0500)]
bcachefs: Disk space accounting fix on brand-new fs

The filesystem initialization path first marks superblock and journal
buckets non transactionally, since the btree isn't functional yet. That
path was updating the per-journal-buf percpu counters via
bch2_dev_usage_update(), and updating the wrong set of counters so those
updates didn't get written out until journal entry 4.

The relevant code is going to get significantly rewritten in the future
as we transition away from the in memory bucket array, so this just
hacks around it for now.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Also log device name in userspace
Kent Overstreet [Mon, 15 Nov 2021 20:03:06 +0000 (15:03 -0500)]
bcachefs: Also log device name in userspace

Change log messages in userspace to be closer to what they are in kernel
space, and include the device name - it's also useful in userspace.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Update export_operations for snapshots
Kent Overstreet [Sun, 14 Nov 2021 00:49:14 +0000 (19:49 -0500)]
bcachefs: Update export_operations for snapshots

When support for snapshots was merged, export operations weren't
updated yet. This patch adds new filehandle types for bcachefs that
include the subvolume ID and updates export operations for subvolumes -
and also .get_parent, support for which was added just prior to
snapshots.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Refactor journal replay code
Kent Overstreet [Tue, 26 Oct 2021 21:35:58 +0000 (17:35 -0400)]
bcachefs: Refactor journal replay code

This consolidates duplicated code in journal replay - it's only a few
flags that are different for replaying alloc keys.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix missing field initialization
Kent Overstreet [Sat, 13 Nov 2021 22:57:52 +0000 (17:57 -0500)]
bcachefs: Fix missing field initialization

When unpacking v1 inodes, we were failing to initialize the journal_seq
field, leading to a BUG_ON() when fsync tries to flush a garbage journal
sequence number.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Convert journal BUG_ON() to a warning
Kent Overstreet [Sat, 13 Nov 2021 22:53:55 +0000 (17:53 -0500)]
bcachefs: Convert journal BUG_ON() to a warning

It's definitely indicative of a bug if we request to flush a journal
sequence number that hasn't happened yet, but it's more useful if we
warn and print out the relevant sequence numbers instead of just dying.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Improve bch2_reflink_p_to_text()
Kent Overstreet [Sat, 13 Nov 2021 22:44:13 +0000 (17:44 -0500)]
bcachefs: Improve bch2_reflink_p_to_text()

.to_text methods generally ought to print all the value fields.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: More enum strings
Kent Overstreet [Thu, 11 Nov 2021 17:11:33 +0000 (12:11 -0500)]
bcachefs: More enum strings

This patch converts more enums in the on disk format to our standard
x-macro-with-strings deal - to enable better pretty-printing.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Tweak vfs cache shrinker behaviour
Kent Overstreet [Sat, 13 Nov 2021 18:36:26 +0000 (13:36 -0500)]
bcachefs: Tweak vfs cache shrinker behaviour

In bcachefs, inodes and dentries are also cached - more compactly - by
the btree node cache, they don't require seeks to recreate.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix an exiting of uninitialized iterator
Kent Overstreet [Sat, 13 Nov 2021 17:57:00 +0000 (12:57 -0500)]
bcachefs: Fix an exiting of uninitialized iterator

bch2_dirent_lookup had an error path where we'd exit a btree_iter that
hadn't been properly initialized.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix infinite loop in bch2_btree_cache_scan()
Kent Overstreet [Thu, 11 Nov 2021 20:50:22 +0000 (15:50 -0500)]
bcachefs: Fix infinite loop in bch2_btree_cache_scan()

When attempting to free btree nodes, we might not be able to free all
the nodes that were requested. But the code was looping until it had
freed _all_ the nodes requested, when it should have only been
attempting to free nr nodes.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: Don't check for -ENOSPC in page writeback
Kent Overstreet [Thu, 11 Nov 2021 18:02:03 +0000 (13:02 -0500)]
bcachefs: Don't check for -ENOSPC in page writeback

If at all possible we'd prefer to not fail page writeback unless the
filesystem has been shutdown; allowing errors in page writeback means
things we'd like to assert about i_size consistency between the VFS and
the btree go out the window.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Inode updates should generally be BTREE_INSERT_NOFAIL
Kent Overstreet [Tue, 9 Nov 2021 22:20:06 +0000 (17:20 -0500)]
bcachefs: Inode updates should generally be BTREE_INSERT_NOFAIL

This fixes a bug where i_size may become inconsistent between the VFS
cache and the btree, when the filesystem is nearly full.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fallocate fixes
Kent Overstreet [Sat, 6 Nov 2021 17:39:42 +0000 (13:39 -0400)]
bcachefs: Fallocate fixes

- fpunch wasn't always correctly updating i_size - when we drop buffered
  writes that were extending a file, we become responsible for writing
  i_size.

- fzero was sometimes zeroing out more data that it should have -
  block_start and block_end were being rounded in the wrong directions

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Refactor bch2_fpunch_at()
Kent Overstreet [Sun, 7 Nov 2021 17:10:57 +0000 (12:10 -0500)]
bcachefs: Refactor bch2_fpunch_at()

This cleans up the error hanlding and flow control a bit.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: bch2_assert_pos_locked()
Kent Overstreet [Sat, 6 Nov 2021 04:03:40 +0000 (00:03 -0400)]
bcachefs: bch2_assert_pos_locked()

This adds a new assertion to be used by bch2_inode_update_after_write(),
which updates the VFS inode based on the update to the btree inode we
just did - we require that the btree inode still be locked when we do
that update.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: path->should_be_locked fixes
Kent Overstreet [Sun, 7 Nov 2021 15:19:37 +0000 (10:19 -0500)]
bcachefs: path->should_be_locked fixes

 - We should only be clearing should_be_locked in btree_path_set_pos() -
   it's the responsiblity of the btree_path code, not the btree_iter
   code.

 - bch2_path_put() needs to pay attention to path->should_be_locked, to
   ensure we don't drop locks we're supposed to be keeping.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Clean up error reporting in the startup path
Kent Overstreet [Sat, 6 Nov 2021 01:28:17 +0000 (21:28 -0400)]
bcachefs: Clean up error reporting in the startup path

It used to be that error reporting in the startup path was done by
returning strings describing the error, but that turned out to be a
rather silly idea - if there's something we can describe about the
error, just print it right away.

This converts a good chunk of code to returning error codes, as is more
typical style.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Return -ENOKEY/EINVAL when mount decryption fails
Chris Webb [Thu, 4 Nov 2021 21:03:16 +0000 (21:03 +0000)]
bcachefs: Return -ENOKEY/EINVAL when mount decryption fails

bch2_fs_encryption_init() correctly passes back -ENOKEY from request_key()
when no unlock key is found, or -EINVAL if superblock decryption fails
because of an invalid key. However, these get absorbed into a generic NULL
return from bch2_fs_alloc() and later returned to user space as -ENOMEM,
leading to a misleading error from mount(1):

  mount(2) system call failed: Out of memory.

Return explicit error pointers out of bch2_fs_alloc() and handle them in
both callers, so the user instead sees

  mount(2) system call failed: Required key not available.

when attempting to mount a filesystem which is still locked.

Signed-off-by: Chris Webb <chris@arachsys.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: Fix upgrade path for reflink_p fix
Kent Overstreet [Sat, 6 Nov 2021 04:05:12 +0000 (00:05 -0400)]
bcachefs: Fix upgrade path for reflink_p fix

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Switch fsync to use bi_journal_seq
Kent Overstreet [Fri, 5 Nov 2021 19:17:13 +0000 (15:17 -0400)]
bcachefs: Switch fsync to use bi_journal_seq

Now that we're recording in each inode the journal sequence number of
the most recent update, fsync becomes a lot simpler and we can delete
all the plumbing for ei_journal_seq.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Kill bucket quantiles sysfs code
Kent Overstreet [Thu, 4 Nov 2021 15:44:13 +0000 (11:44 -0400)]
bcachefs: Kill bucket quantiles sysfs code

We're getting rid of code that uses the in memory bucket array - and we
now have better mechanisms for viewing most of what the bucket quantiles
code gave us (especially internal fragmentation).

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Kill journal buf bloom filter
Kent Overstreet [Thu, 4 Nov 2021 02:33:32 +0000 (22:33 -0400)]
bcachefs: Kill journal buf bloom filter

This was used for recording which inodes have been modified by in flight
journal writes, but was broken and has been superceded.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Add journal_seq to inode & alloc keys
Kent Overstreet [Sat, 30 Oct 2021 01:14:23 +0000 (21:14 -0400)]
bcachefs: Add journal_seq to inode & alloc keys

Add fields to inode & alloc keys that record the journal sequence number
when they were most recently modified.

For alloc keys, this is needed to know what journal sequence number we
have to flush before the bucket can be reused. Currently this is tracked
in memory, but we'll be getting rid of the in memory bucket array.

For inodes, this is needed for fsync when the inode has been evicted
from the vfs cache. Currently we use a bloom filter per outstanding
journal buf - but that mechanism has been broken since we added the
ability to not issue a flush/fua for every journal write.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Update inode on every write
Kent Overstreet [Thu, 4 Nov 2021 02:35:34 +0000 (22:35 -0400)]
bcachefs: Update inode on every write

This is going to be a performance regression until we get the btree key
cache re-enabled - but it's needed for fixing fsync. Upcoming patches
will record the journal_seq an inode was updated at in the inode itself.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: BTREE_UPDATE_NOJOURNAL
Kent Overstreet [Sat, 31 Dec 2022 05:15:23 +0000 (00:15 -0500)]
bcachefs: BTREE_UPDATE_NOJOURNAL

We're going to have btree updates that don't need to be journalled; add
a flag for that.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: Fix __remove_dirent()
Kent Overstreet [Thu, 4 Nov 2021 01:22:46 +0000 (21:22 -0400)]
bcachefs: Fix __remove_dirent()

__lookup_inode() doesn't work for what __remove_dirent() wants - it just
wants the first inode at a given inode number, they all have the same
hash info.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix check_inodes()
Kent Overstreet [Thu, 4 Nov 2021 00:25:35 +0000 (20:25 -0400)]
bcachefs: Fix check_inodes()

We were starting at the wrong btree position, and thus not actually
checking any inodes - oops.

Also, make check_key_has_snapshot() a mustfix fsck error, since later
fsck code assumes that all keys have valid snapshot IDs.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Improve error message in bch2_write_super()
Kent Overstreet [Wed, 3 Nov 2021 21:23:49 +0000 (17:23 -0400)]
bcachefs: Improve error message in bch2_write_super()

It's helpful to know what the superblock write is for.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix trans_lock_write()
Kent Overstreet [Wed, 3 Nov 2021 21:23:03 +0000 (17:23 -0400)]
bcachefs: Fix trans_lock_write()

On failure to get a write lock (because we had a conflicting read lock),
we need to make sure to upgrade the read lock to an intent lock - or we
could end up spinning.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix upgrade_readers()
Kent Overstreet [Wed, 3 Nov 2021 16:08:02 +0000 (12:08 -0400)]
bcachefs: Fix upgrade_readers()

The bch2_btree_path_upgrade() call was failing and tripping an assert -
path->level + 1 is in this case not necessarily exactly what we want,
fix it by upgrading exactly the locks we want.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix faulty assertion
Kent Overstreet [Sat, 30 Oct 2021 09:28:27 +0000 (05:28 -0400)]
bcachefs: Fix faulty assertion

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: BTREE_TRIGGER_INSERT now only means insert
Kent Overstreet [Fri, 29 Oct 2021 22:58:50 +0000 (18:58 -0400)]
bcachefs: BTREE_TRIGGER_INSERT now only means insert

This allows triggers to distinguish between a key entering the btree -
i.e. being called from the trans commit path - vs. being called on a key
that already exists, i.e. by GC.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Convert bch2_mark_key() to take a btree_trans *
Kent Overstreet [Fri, 29 Oct 2021 22:43:18 +0000 (18:43 -0400)]
bcachefs: Convert bch2_mark_key() to take a btree_trans *

This helps to unify the interface between bch2_mark_key() and
bch2_trans_mark_key() - and it also gives access to the journal
reservation and journal seq in the mark_key path.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Assorted ec fixes
Kent Overstreet [Fri, 29 Oct 2021 20:29:13 +0000 (16:29 -0400)]
bcachefs: Assorted ec fixes

- The backpointer that ec_stripe_update_ptrs() uses now needs to include
  the snapshot ID, which means we have to change where we add the
  backpointer to after getting the snapshot ID for the new extents

- ec_stripe_update_ptrs() needs to be calling bch2_trans_begin()

- improve error message in bch2_mark_stripe()

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix bch2_mark_update()
Kent Overstreet [Fri, 29 Oct 2021 22:21:05 +0000 (18:21 -0400)]
bcachefs: Fix bch2_mark_update()

When the old or new key doesn't exist, we should still pass in a deleted
key with the correct pos. This fixes a bug in the ec code, when
bch2_mark_stripe() was looking up the wrong in-memory stripe.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Ensure journal doesn't get stuck in nochanges mode
Kent Overstreet [Thu, 28 Oct 2021 22:22:25 +0000 (18:22 -0400)]
bcachefs: Ensure journal doesn't get stuck in nochanges mode

This tweaks the journal code to always act as if there's space available
in nochanges mode, when we're not going to be doing any writes. This
helps in recovering filesystems that won't mount because they need
journal replay and the journal has gotten stuck.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Improve transaction restart handling in fsck code
Kent Overstreet [Thu, 28 Oct 2021 20:16:55 +0000 (16:16 -0400)]
bcachefs: Improve transaction restart handling in fsck code

The fsck code has been handling transaction restarts locally, to avoid
calling fsck_err() multiple times (and asking the user/logging the error
multiple times) on transaction restart.

However, with our improving assertions about iterator validity, this
isn't working anymore - the code wasn't entirely correct, in ways that
are fine for now but are going to matter once we start wanting online
fsck.

This code converts much of the fsck code to handle transaction restarts
in a more rigorously correct way - moving restart handling up to the top
level of check_dirent, check_xattr and others - at the cost of logging
errors multiple times on transaction restart.

Fixing the issues with logging errors multiple times is probably going
to require memoizing calls to fsck_err() - we'll leave that for future
improvements.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix bch2_btree_iter_advance()
Kent Overstreet [Thu, 28 Oct 2021 20:34:17 +0000 (16:34 -0400)]
bcachefs: Fix bch2_btree_iter_advance()

Was popping an assertion on !BTREE_ITER_ALL_SNAPSHOTS iters when getting
to the end.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Move bch2_evict_subvolume_inodes() to fs.c
Kent Overstreet [Thu, 28 Oct 2021 20:24:39 +0000 (16:24 -0400)]
bcachefs: Move bch2_evict_subvolume_inodes() to fs.c

This fixes building in userspace - code that's coupled to the kernel VFS
interface should live in fs.c

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Don't do upgrades in nochanges mode
Kent Overstreet [Wed, 27 Oct 2021 21:53:20 +0000 (17:53 -0400)]
bcachefs: Don't do upgrades in nochanges mode

nochanges mode is often used for getting data off of otherwise
nonrecoverable filesystems, which is often because of errors hit during
fsck.

Don't force version upgrade & fsck in nochanges mode, so that it's more
likely to mount.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Drop bch2_journal_meta() call when going RW
Kent Overstreet [Tue, 26 Oct 2021 20:03:28 +0000 (16:03 -0400)]
bcachefs: Drop bch2_journal_meta() call when going RW

Back when we relied on the journal sequence number blacklist machinery
for consistency between btree and the journal, we needed to ensure a new
journal entry was written before any btree writes were done. But, this
had the side effect of consuming some space in the journal prior to
doing journal replay - which could lead to a very wedged filesystem,
since we don't yet have a way to grow the journal prior to going RW.

Fortunately, the journal sequence number blacklist machinery isn't
needed anymore, as btree node pointers now record the numer of sectors
currently written to that node - that code should all be ripped out.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Add BCH_SUBVOLUME_UNLINKED
Kent Overstreet [Mon, 11 Oct 2021 16:03:19 +0000 (12:03 -0400)]
bcachefs: Add BCH_SUBVOLUME_UNLINKED

Snapshot deletion needs to become a multi step process, where we unlink,
then tear down the page cache, then delete the subvolume - the deleting
flag is equivalent to an inode with i_nlink = 0.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Improve error messages in trans_mark_reflink_p()
Kent Overstreet [Tue, 26 Oct 2021 18:07:43 +0000 (14:07 -0400)]
bcachefs: Improve error messages in trans_mark_reflink_p()

We should always print out the key we were marking.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Don't run triggers in fix_reflink_p_key()
Kent Overstreet [Mon, 25 Oct 2021 23:30:24 +0000 (19:30 -0400)]
bcachefs: Don't run triggers in fix_reflink_p_key()

It seems some users have reflink pointers which span many indirect
extents, from a short window in time when merging of reflink pointers
was allowed.

Now, we're seeing transaction path overflows in fix_reflink_p(), the
code path to clear out the reflink_p fields now used for front/back pad
- but, we don't actually need to be running triggers in that path, which
is an easy partial fix.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: More general fix for transaction paths overflow
Kent Overstreet [Mon, 25 Oct 2021 22:30:28 +0000 (18:30 -0400)]
bcachefs: More general fix for transaction paths overflow

for_each_btree_key() now calls bch2_trans_begin() as needed; that means,
we can also call it when we're in danger of overflowing transaction
paths.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix fsck path for refink pointers
Kent Overstreet [Thu, 21 Oct 2021 19:48:05 +0000 (15:48 -0400)]
bcachefs: Fix fsck path for refink pointers

The way __bch2_mark_reflink_p returns errors was clashing with returning
the number of sectors processed - we weren't returning FSCK_ERR_EXIT
correctly.

Fix this by only using the return code for errors, which actually ends
up simplifying the overall logic.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Ensure we flush btree updates in evacuate path
Kent Overstreet [Sun, 24 Oct 2021 21:00:33 +0000 (17:00 -0400)]
bcachefs: Ensure we flush btree updates in evacuate path

This fixes a possible race where we fail to remove a device because of
btree nodes still on it, that are being deleted by in flight btree
updates.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: bch2_btree_node_rewrite() now returns transaction restarts
Kent Overstreet [Sun, 24 Oct 2021 20:59:33 +0000 (16:59 -0400)]
bcachefs: bch2_btree_node_rewrite() now returns transaction restarts

We have been getting away from handling transaction restarts locally -
convert bch2_btree_node_rewrite() to the newer style.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix bch2_btree_iter_next_node()
Kent Overstreet [Sun, 24 Oct 2021 20:55:17 +0000 (16:55 -0400)]
bcachefs: Fix bch2_btree_iter_next_node()

We were modifying state, then return -EINTR, causing us to skip nodes -
ouch.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Must check for errors from bch2_trans_cond_resched()
Kent Overstreet [Sun, 24 Oct 2021 20:40:05 +0000 (16:40 -0400)]
bcachefs: Must check for errors from bch2_trans_cond_resched()

But we don't need to call it from outside the btree iterator code
anymore, since it's called by bch2_trans_begin() and
bch2_btree_path_traverse().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix dev accounting after device add
Kent Overstreet [Sun, 24 Oct 2021 15:57:47 +0000 (11:57 -0400)]
bcachefs: Fix dev accounting after device add

This is a hacky but effective fix to device usage stats for superblock
and journal being wrong on a newly added device (following the comment
that already told us how it needed to be done!)

Reported-by: Chris Webb <chris@arachsys.com>
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix a transaction path overflow
Kent Overstreet [Fri, 22 Oct 2021 21:33:38 +0000 (17:33 -0400)]
bcachefs: Fix a transaction path overflow

readdir() in a directory with many subvolumes could overflow transaction
paths - this is a simple hack around the issue.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix error handling in bch2_trans_extent_merging
Kent Overstreet [Thu, 21 Oct 2021 18:33:31 +0000 (14:33 -0400)]
bcachefs: Fix error handling in bch2_trans_extent_merging

The back merging case wasn't returning errors correctly.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Subvol dirents are now only visible in parent subvol
Kent Overstreet [Tue, 12 Oct 2021 16:06:02 +0000 (12:06 -0400)]
bcachefs: Subvol dirents are now only visible in parent subvol

This changes the on disk format for dirents that point to subvols so
that they also record the subvolid of the parent subvol, so that we can
filter them out in other subvolumes.

This also updates the dirent code to do that filtering, and in
particular tweaks the rename code - we need to ensure that there's only
ever one dirent (counting multiplicities in different snapshots) that
point to a subvolume.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix restart handling in for_each_btree_key()
Kent Overstreet [Thu, 21 Oct 2021 16:05:21 +0000 (12:05 -0400)]
bcachefs: Fix restart handling in for_each_btree_key()

Code that uses for_each_btree_key often wants transaction restarts to be
handled locally and not returned. Originally, we wouldn't return
transaction restarts if there was a single iterator in the transaction -
the reasoning being if there weren't other iterators being invalidated,
and the current iterator was being advanced/retraversed, there weren't
any locks or iterators we were required to preserve.

But with the btree_path conversion that approach doesn't work anymore -
even when we're using for_each_btree_key() with a single iterator there
will still be two paths in the transaction, since we now always preserve
the path at the pos the iterator was initialized at - the reason being
that on restart we often restart from the same place.

And it turns out there's now a lot of for_each_btree_key() uses that _do
not_ want transaction restarts handled locally, and should be returning
them.

This patch splits out for_each_btree_key_norestart() and
for_each_btree_key_continue_norestart(), and converts existing users as
appropriate. for_each_btree_key(), for_each_btree_key_continue(), and
for_each_btree_node() now handle transaction restarts themselves by
calling bch2_trans_begin() when necessary - and the old hack to not
return transaction restarts when there's a single path in the
transaction has been deleted.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: cached data shouldn't prevent fs from mounting
Kent Overstreet [Thu, 21 Oct 2021 04:38:13 +0000 (00:38 -0400)]
bcachefs: cached data shouldn't prevent fs from mounting

It's not an error if we don't have cached data - skip BCH_DATA_cached in
bch2_have_enough_devs().

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Delete dentry when deleting snapshots
Kent Overstreet [Thu, 21 Oct 2021 00:50:07 +0000 (20:50 -0400)]
bcachefs: Delete dentry when deleting snapshots

This fixes a bug where subsequently doing creates with the same name
fails.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix check_path() for snapshots
Kent Overstreet [Wed, 20 Oct 2021 21:59:38 +0000 (17:59 -0400)]
bcachefs: Fix check_path() for snapshots

check_path() wasn't checking the snapshot ID when checking for directory
structure loops - so, snapshots would cause us to detect a loop that
wasn't actually a loop.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix for leaking of reflinked extents
Kent Overstreet [Thu, 14 Oct 2021 13:54:47 +0000 (09:54 -0400)]
bcachefs: Fix for leaking of reflinked extents

When a reflink pointer points to only part of an indirect extent, and
then that indirect extent is fragmented (e.g. by copygc), if the reflink
pointer only points to one of the fragments we leak a reference.

Fix this by storing front/back pad values in reflink pointers - when
inserting reflink pointesr, we initialize them to cover the full range
of the indirect extents we reference.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: New on disk format to fix reflink_p pointers
Kent Overstreet [Tue, 19 Oct 2021 21:30:16 +0000 (17:30 -0400)]
bcachefs: New on disk format to fix reflink_p pointers

We had a bug where reflink_p pointers weren't being initialized to 0,
and when we started using the second word, things broke badly.

This patch revs the on disk format version and adds cleanup code to zero
out the second word of reflink_p pointers before we start using it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Handle transaction restarts in bch2_blacklist_entries_gc()
Kent Overstreet [Tue, 19 Oct 2021 19:11:45 +0000 (15:11 -0400)]
bcachefs: Handle transaction restarts in bch2_blacklist_entries_gc()

It shouldn't be necessary when we're only using a single iterator and
not doing updates, but that's harder to debug at the moment.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: bch2_trans_exit() no longer returns errors
Kent Overstreet [Tue, 19 Oct 2021 19:08:00 +0000 (15:08 -0400)]
bcachefs: bch2_trans_exit() no longer returns errors

Now that peek_node()/next_node() are converted to return errors
directly, we don't need bch2_trans_exit() to return errors - it's
cleaner this way and wasn't used much anymore.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: for_each_btree_node() now returns errors directly
Kent Overstreet [Tue, 19 Oct 2021 18:20:50 +0000 (14:20 -0400)]
bcachefs: for_each_btree_node() now returns errors directly

This changes for_each_btree_node() to work like for_each_btree_key(),
and to that end bch2_btree_iter_peek_node() and next_node() also return
error ptrs.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Improve reflink repair code
Kent Overstreet [Tue, 19 Oct 2021 16:27:47 +0000 (12:27 -0400)]
bcachefs: Improve reflink repair code

When a reflink pointer points to an indirect extent that doesn't exist,
we need to replace it with a KEY_TYPE_error key.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix check_path() across subvolumes
Kent Overstreet [Tue, 19 Oct 2021 05:08:05 +0000 (01:08 -0400)]
bcachefs: Fix check_path() across subvolumes

Checking of directory structure across subvolumes was broken - we need
to look up the snapshot ID of the parent subvolume when crossing subvol
boundaries.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix a cache coherency bug in bch2_subvolume_create()
Kent Overstreet [Mon, 18 Oct 2021 18:46:57 +0000 (14:46 -0400)]
bcachefs: Fix a cache coherency bug in bch2_subvolume_create()

Subvolume deletion doesn't flush & evict the btree key cache - ideally
it would, but that's tricky, so instead bch2_subvolume_create() needs to
make sure the slot doesn't exist in the key cache to avoid triggering
assertions.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix compiler warnings
Brett Holman [Sun, 17 Oct 2021 01:13:53 +0000 (19:13 -0600)]
bcachefs: Fix compiler warnings

Type size_t is architecture-specific. Fix warnings for some non-amd64
arches.

Signed-off-by: Brett Holman <bholman.devel@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: Zero out reflink_p val in bch2_make_extent_indirect()
Kent Overstreet [Mon, 18 Oct 2021 15:32:06 +0000 (11:32 -0400)]
bcachefs: Zero out reflink_p val in bch2_make_extent_indirect()

This bug was only discovered when we started using the 2nd word in the
val, which should have been zeroed out as those fields had never been
used before - ouch.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix __bch2_dirent_read_target()
Kent Overstreet [Thu, 14 Oct 2021 17:14:40 +0000 (13:14 -0400)]
bcachefs: Fix __bch2_dirent_read_target()

We were shadowing our exist status, oops

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Improve bch2_dump_trans_paths_updates()
Kent Overstreet [Thu, 14 Oct 2021 15:47:52 +0000 (11:47 -0400)]
bcachefs: Improve bch2_dump_trans_paths_updates()

Also print the key beyng overwritten for each update.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Don't allocate too-big bios
Kent Overstreet [Wed, 13 Oct 2021 17:45:46 +0000 (13:45 -0400)]
bcachefs: Don't allocate too-big bios

This fixes a null ptr deref in bio_alloc_bioset() -> biovec_slab()

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix implementation of KEY_TYPE_error
Kent Overstreet [Wed, 13 Oct 2021 17:12:26 +0000 (13:12 -0400)]
bcachefs: Fix implementation of KEY_TYPE_error

When force-removing a device, we were silently dropping extents that we
no longer had pointers for - we should have been switching them to
KEY_TYPE_error, so that reads for data that was lost return errors.

This patch adds the logic for switching a key to KEY_TYPE_error to
bch2_bkey_drop_ptr(), and improves the logic somewhat.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix deletion in __bch2_dev_usrdata_drop()
Kent Overstreet [Tue, 12 Oct 2021 18:25:13 +0000 (14:25 -0400)]
bcachefs: Fix deletion in __bch2_dev_usrdata_drop()

With snapshots, __bch2_dev_usr_data_drop() now uses an ALL_SNAPSHOTS
iterator, which isn't an extent iterator - meaning we shouldn't be
inserting whiteouts with nonzero size to delete. This fixes a bug where
we go RO because we tried to insert an invalid key in the device remove
path.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Add a valgrind memcheck hint
Brett Holman [Wed, 13 Oct 2021 03:11:25 +0000 (21:11 -0600)]
bcachefs: Add a valgrind memcheck hint

Prevent false positives in bch2_varint_decode_fast()

Signed-off-by: Brett Holman <bholman.devel@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
10 months agobcachefs: Fix rereplicate_pred()
Kent Overstreet [Tue, 12 Oct 2021 18:15:45 +0000 (14:15 -0400)]
bcachefs: Fix rereplicate_pred()

It was switching off of the key type incorrectly - this code must've
been quite old, and not rereplicating anything that wasn't a
btree_ptr_v1 or a plain old extent.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Snapshot deletion fix
Kent Overstreet [Fri, 1 Oct 2021 00:09:08 +0000 (20:09 -0400)]
bcachefs: Snapshot deletion fix

When we delete a snapshot, we unlink the inode but we don't want to run
the inode_rm path - the unlink path deletes the subvolume directly,
which does everything we need. Also allowing the inode_rm path to run
was getting us "missing subvolume" errors.

There's still another bug with snapshot deletion: we need to make
snapshot deletion a multi stage process, where we unlink the root
dentry, then tear down the page cache, then delete the snapshot.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
10 months agobcachefs: Fix a pcpu var splat
Kent Overstreet [Thu, 7 Oct 2021 22:18:01 +0000 (18:18 -0400)]
bcachefs: Fix a pcpu var splat

this_cpu_ptr() emits a warning when used without preemption disabled -
harmless in this case, as we have other locking where
bch2_acc_percpu_u64s() is used.

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