platform/kernel/linux-starfive.git
5 years agobtrfs: revert fs_devices state on error of btrfs_init_new_device
Naohiro Aota [Fri, 27 Jul 2018 00:04:55 +0000 (09:04 +0900)]
btrfs: revert fs_devices state on error of btrfs_init_new_device

When btrfs hits error after modifying fs_devices in
btrfs_init_new_device() (such as btrfs_add_dev_item() returns error), it
leaves everything as is, but frees allocated btrfs_device. As a result,
fs_devices->devices and fs_devices->alloc_list contain already freed
btrfs_device, leading to later use-after-free bug.

Error path also messes the things like ->num_devices. While they go back
to the original value by unscanning btrfs devices, it is safe to revert
them here.

Fixes: 79787eaab461 ("btrfs: replace many BUG_ONs with proper error handling")
Signed-off-by: Naohiro Aota <naota@elisp.net>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Exit gracefully when chunk map cannot be inserted to the tree
Qu Wenruo [Wed, 1 Aug 2018 02:37:20 +0000 (10:37 +0800)]
btrfs: Exit gracefully when chunk map cannot be inserted to the tree

It's entirely possible that a crafted btrfs image contains overlapping
chunks.

Although we can't detect such problem by tree-checker, it's not a
catastrophic problem, current extent map can already detect such problem
and return -EEXIST.

We just only need to exit gracefully and fail the mount.

Reported-by: Xu Wen <wen.xu@gatech.edu>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=200409
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Introduce mount time chunk <-> dev extent mapping check
Qu Wenruo [Wed, 1 Aug 2018 02:37:19 +0000 (10:37 +0800)]
btrfs: Introduce mount time chunk <-> dev extent mapping check

This patch will introduce chunk <-> dev extent mapping check, to protect
us against invalid dev extents or chunks.

Since chunk mapping is the fundamental infrastructure of btrfs, extra
check at mount time could prevent a lot of unexpected behavior (BUG_ON).

Reported-by: Xu Wen <wen.xu@gatech.edu>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=200403
Link: https://bugzilla.kernel.org/show_bug.cgi?id=200407
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Su Yue <suy.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Verify that every chunk has corresponding block group at mount time
Qu Wenruo [Wed, 1 Aug 2018 02:37:17 +0000 (10:37 +0800)]
btrfs: Verify that every chunk has corresponding block group at mount time

If a crafted image has missing block group items, it could cause
unexpected behavior and breaks the assumption of 1:1 chunk<->block group
mapping.

Although we have the block group -> chunk mapping check, we still need
chunk -> block group mapping check.

This patch will do extra check to ensure each chunk has its
corresponding block group.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=199847
Reported-by: Xu Wen <wen.xu@gatech.edu>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Check that each block group has corresponding chunk at mount time
Qu Wenruo [Wed, 1 Aug 2018 02:37:16 +0000 (10:37 +0800)]
btrfs: Check that each block group has corresponding chunk at mount time

A crafted btrfs image with incorrect chunk<->block group mapping will
trigger a lot of unexpected things as the mapping is essential.

Although the problem can be caught by block group item checker
added in "btrfs: tree-checker: Verify block_group_item", it's still not
sufficient.  A sufficiently valid block group item can pass the check
added by the mentioned patch but could fail to match the existing chunk.

This patch will add extra block group -> chunk mapping check, to ensure
we have a completely matching (start, len, flags) chunk for each block
group at mount time.

Here we reuse the original helper find_first_block_group(), which is
already doing the basic bg -> chunk checks, adding further checks of the
start/len and type flags.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=199837
Reported-by: Xu Wen <wen.xu@gatech.edu>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Su Yue <suy.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agoBtrfs: send, fix incorrect file layout after hole punching beyond eof
Filipe Manana [Mon, 30 Jul 2018 11:39:58 +0000 (12:39 +0100)]
Btrfs: send, fix incorrect file layout after hole punching beyond eof

When doing an incremental send, if we have a file in the parent snapshot
that has prealloc extents beyond EOF and in the send snapshot it got a
hole punch that partially covers the prealloc extents, the send stream,
when replayed by a receiver, can result in a file that has a size bigger
than it should and filled with zeroes past the correct EOF.

For example:

  $ mkfs.btrfs -f /dev/sdb
  $ mount /dev/sdb /mnt

  $ xfs_io -f -c "falloc -k 0 4M" /mnt/foobar
  $ xfs_io -c "pwrite -S 0xea 0 1M" /mnt/foobar

  $ btrfs subvolume snapshot -r /mnt /mnt/snap1
  $ btrfs send -f /tmp/1.send /mnt/snap1

  $ xfs_io -c "fpunch 1M 2M" /mnt/foobar

  $ btrfs subvolume snapshot -r /mnt /mnt/snap2
  $ btrfs send -f /tmp/2.send -p /mnt/snap1 /mnt/snap2

  $ stat --format %s /mnt/snap2/foobar
  1048576
  $ md5sum /mnt/snap2/foobar
  d31659e82e87798acd4669a1e0a19d4f  /mnt/snap2/foobar

  $ umount /mnt
  $ mkfs.btrfs -f /dev/sdc
  $ mount /dev/sdc /mnt

  $ btrfs receive -f /mnt/1.snap /mnt
  $ btrfs receive -f /mnt/2.snap /mnt

  $ stat --format %s /mnt/snap2/foobar
  3145728
  # --> should be 1Mb and not 3Mb (which was the end offset of hole
  #     punch operation)
  $ md5sum /mnt/snap2/foobar
  117baf295297c2a995f92da725b0b651  /mnt/snap2/foobar
  # --> should be d31659e82e87798acd4669a1e0a19d4f as in the original fs

This issue actually happens only since commit ffa7c4296e93 ("Btrfs: send,
do not issue unnecessary truncate operations"), but before that commit we
were issuing a write operation full of zeroes (to "punch" a hole) which
was extending the file size beyond the correct value and then immediately
issue a truncate operation to the correct size and undoing the previous
write operation. Since the send protocol does not support fallocate, for
extent preallocation and hole punching, fix this by not even attempting
to send a "hole" (regular write full of zeroes) if it starts at an offset
greater then or equals to the file's size. This approach, besides being
much more simple then making send issue the truncate operation, adds the
benefit of avoiding the useless pair of write of zeroes and truncate
operations, saving time and IO at the receiver and reducing the size of
the send stream.

A test case for fstests follows soon.

Fixes: ffa7c4296e93 ("Btrfs: send, do not issue unnecessary truncate operations")
CC: stable@vger.kernel.org # 4.17+
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Use wrapper macro for rcu string to remove duplicate code
Misono Tomohiro [Thu, 2 Aug 2018 07:19:07 +0000 (16:19 +0900)]
btrfs: Use wrapper macro for rcu string to remove duplicate code

Cleanup patch and no functional changes.

Signed-off-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: simplify btrfs_iget
Al Viro [Sun, 29 Jul 2018 22:04:51 +0000 (23:04 +0100)]
btrfs: simplify btrfs_iget

Don't open-code iget_failed(), don't bother with btrfs_free_path(NULL),
move handling of positive return values of btrfs_lookup_inode() from
btrfs_read_locked_inode() to btrfs_iget() and kill now obviously
pointless ASSERT() in there.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: lift make_bad_inode into btrfs_iget
Al Viro [Sun, 29 Jul 2018 22:04:50 +0000 (23:04 +0100)]
btrfs: lift make_bad_inode into btrfs_iget

We don't need to check is_bad_inode() after the call of
btrfs_read_locked_inode() - it's exactly the same as checking return
value for being non-zero.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Reviewed-by: David Sterba <dsterba@suse.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: simplify IS_ERR/PTR_ERR checks
Al Viro [Sun, 29 Jul 2018 22:04:46 +0000 (23:04 +0100)]
btrfs: simplify IS_ERR/PTR_ERR checks

IS_ERR(p) && PTR_ERR(p) == n is a weird way to spell p == ERR_PTR(n).

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Reviewed-by: David Sterba <dsterba@suse.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
[ update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: btrfs_iget never returns an is_bad_inode inode
Al Viro [Sun, 29 Jul 2018 22:04:45 +0000 (23:04 +0100)]
btrfs: btrfs_iget never returns an is_bad_inode inode

Just get rid of pointless checks.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: replace: Reset on-disk dev stats value after replace
Misono Tomohiro [Tue, 31 Jul 2018 07:20:21 +0000 (16:20 +0900)]
btrfs: replace: Reset on-disk dev stats value after replace

on-disk devs stats value is updated in btrfs_run_dev_stats(),
which is called during commit transaction, if device->dev_stats_ccnt
is not zero.

Since current replace operation does not touch dev_stats_ccnt,
on-disk dev stats value is not updated. Therefore "btrfs device stats"
may return old device's value after umount/mount
(Example: See "btrfs ins dump-t -t DEV $DEV" after btrfs/100 finish).

Fix this by just incrementing dev_stats_ccnt in
btrfs_dev_replace_finishing() when replace is succeeded and this will
update the values.

Signed-off-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: extent-tree: Remove unused __btrfs_free_block_rsv
Misono Tomohiro [Thu, 26 Jul 2018 02:40:54 +0000 (11:40 +0900)]
btrfs: extent-tree: Remove unused __btrfs_free_block_rsv

There is no user of this function anymore.

This was forgotten to be removed in commit a575ceeb1338
("Btrfs: get rid of unused orphan infrastructure").

Signed-off-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: backref: Use ERR_CAST to return error code
Misono Tomohiro [Thu, 26 Jul 2018 01:22:58 +0000 (10:22 +0900)]
btrfs: backref: Use ERR_CAST to return error code

Use ERR_CAST() instead of void * to make meaning clear.

Signed-off-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove redundant btrfs_release_path from btrfs_unlink_subvol
Lu Fengqi [Wed, 1 Aug 2018 03:32:31 +0000 (11:32 +0800)]
btrfs: Remove redundant btrfs_release_path from btrfs_unlink_subvol

Although it is safe to call this on already released paths with no locks
held or extent buffers, removing the redundant btrfs_release_path is
reasonable.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove root parameter from btrfs_unlink_subvol
Lu Fengqi [Wed, 1 Aug 2018 03:32:30 +0000 (11:32 +0800)]
btrfs: Remove root parameter from btrfs_unlink_subvol

All callers pass the root tree of dir, we can push that down to the
function itself.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_add_root_ref
Lu Fengqi [Wed, 1 Aug 2018 03:32:29 +0000 (11:32 +0800)]
btrfs: Remove fs_info from btrfs_add_root_ref

It can be referenced from the passed transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_del_root_ref
Lu Fengqi [Wed, 1 Aug 2018 03:32:28 +0000 (11:32 +0800)]
btrfs: Remove fs_info from btrfs_del_root_ref

It can be referenced from the passed transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_del_root
Lu Fengqi [Wed, 1 Aug 2018 03:32:27 +0000 (11:32 +0800)]
btrfs: Remove fs_info from btrfs_del_root

It can be referenced from the passed transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_delete_delayed_dir_index
Lu Fengqi [Wed, 1 Aug 2018 03:32:26 +0000 (11:32 +0800)]
btrfs: Remove fs_info from btrfs_delete_delayed_dir_index

It can be referenced from the passed transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_insert_delayed_dir_index
Lu Fengqi [Wed, 1 Aug 2018 03:32:25 +0000 (11:32 +0800)]
btrfs: Remove fs_info from btrfs_insert_delayed_dir_index

It can be referenced from the passed transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: extent-tree: remove unused member walk_control::for_reloc
David Sterba [Tue, 24 Jul 2018 15:19:48 +0000 (17:19 +0200)]
btrfs: extent-tree: remove unused member walk_control::for_reloc

Leftover after fix e339a6b097c5 ("Btrfs: __btrfs_mod_ref should always
use no_quota"), that removed it from the function calls but not the
structure.

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agoBtrfs: fix send failure when root has deleted files still open
Filipe Manana [Tue, 24 Jul 2018 10:54:04 +0000 (11:54 +0100)]
Btrfs: fix send failure when root has deleted files still open

The more common use case of send involves creating a RO snapshot and then
use it for a send operation. In this case it's not possible to have inodes
in the snapshot that have a link count of zero (inode with an orphan item)
since during snapshot creation we do the orphan cleanup. However, other
less common use cases for send can end up seeing inodes with a link count
of zero and in this case the send operation fails with a ENOENT error
because any attempt to generate a path for the inode, with the purpose
of creating it or updating it at the receiver, fails since there are no
inode reference items. One use case it to use a regular subvolume for
a send operation after turning it to RO mode or turning a RW snapshot
into RO mode and then using it for a send operation. In both cases, if a
file gets all its hard links deleted while there is an open file
descriptor before turning the subvolume/snapshot into RO mode, the send
operation will encounter an inode with a link count of zero and then
fail with errno ENOENT.

Example using a full send with a subvolume:

  $ mkfs.btrfs -f /dev/sdb
  $ mount /dev/sdb /mnt

  $ btrfs subvolume create /mnt/sv1
  $ touch /mnt/sv1/foo
  $ touch /mnt/sv1/bar

  # keep an open file descriptor on file bar
  $ exec 73</mnt/sv1/bar
  $ unlink /mnt/sv1/bar

  # Turn the subvolume to RO mode and use it for a full send, while
  # holding the open file descriptor.
  $ btrfs property set /mnt/sv1 ro true

  $ btrfs send -f /tmp/full.send /mnt/sv1
  At subvol /mnt/sv1
  ERROR: send ioctl failed with -2: No such file or directory

Example using an incremental send with snapshots:

  $ mkfs.btrfs -f /dev/sdb
  $ mount /dev/sdb /mnt

  $ btrfs subvolume create /mnt/sv1
  $ touch /mnt/sv1/foo
  $ touch /mnt/sv1/bar

  $ btrfs subvolume snapshot -r /mnt/sv1 /mnt/snap1

  $ echo "hello world" >> /mnt/sv1/bar

  $ btrfs subvolume snapshot -r /mnt/sv1 /mnt/snap2

  # Turn the second snapshot to RW mode and delete file foo while
  # holding an open file descriptor on it.
  $ btrfs property set /mnt/snap2 ro false
  $ exec 73</mnt/snap2/foo
  $ unlink /mnt/snap2/foo

  # Set the second snapshot back to RO mode and do an incremental send.
  $ btrfs property set /mnt/snap2 ro true

  $ btrfs send -f /tmp/inc.send -p /mnt/snap1 /mnt/snap2
  At subvol /mnt/snap2
  ERROR: send ioctl failed with -2: No such file or directory

So fix this by ignoring inodes with a link count of zero if we are either
doing a full send or if they do not exist in the parent snapshot (they
are new in the send snapshot), and unlink all paths found in the parent
snapshot when doing an incremental send (and ignoring all other inode
items, such as xattrs and extents).

A test case for fstests follows soon.

CC: stable@vger.kernel.org # 4.4+
Reported-by: Martin Wilck <martin.wilck@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agoBtrfs: fix mount failure after fsync due to hard link recreation
Filipe Manana [Fri, 20 Jul 2018 09:59:06 +0000 (10:59 +0100)]
Btrfs: fix mount failure after fsync due to hard link recreation

If we end up with logging an inode reference item which has the same name
but different index from the one we have persisted, we end up failing when
replaying the log with an errno value of -EEXIST. The error comes from
btrfs_add_link(), which is called from add_inode_ref(), when we are
replaying an inode reference item.

Example scenario where this happens:

  $ mkfs.btrfs -f /dev/sdb
  $ mount /dev/sdb /mnt

  $ touch /mnt/foo
  $ ln /mnt/foo /mnt/bar

  $ sync

  # Rename the first hard link (foo) to a new name and rename the second
  # hard link (bar) to the old name of the first hard link (foo).
  $ mv /mnt/foo /mnt/qwerty
  $ mv /mnt/bar /mnt/foo

  # Create a new file, in the same parent directory, with the old name of
  # the second hard link (bar) and fsync this new file.
  # We do this instead of calling fsync on foo/qwerty because if we did
  # that the fsync resulted in a full transaction commit, not triggering
  # the problem.
  $ touch /mnt/bar
  $ xfs_io -c "fsync" /mnt/bar

  <power fail>

  $ mount /dev/sdb /mnt
  mount: mount /dev/sdb on /mnt failed: File exists

So fix this by checking if a conflicting inode reference exists (same
name, same parent but different index), removing it (and the associated
dir index entries from the parent inode) if it exists, before attempting
to add the new reference.

A test case for fstests follows soon.

CC: stable@vger.kernel.org # 4.4+
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: don't leak ret from do_chunk_alloc
Josef Bacik [Thu, 19 Jul 2018 14:49:51 +0000 (10:49 -0400)]
btrfs: don't leak ret from do_chunk_alloc

If we're trying to make a data reservation and we have to allocate a
data chunk we could leak ret == 1, as do_chunk_alloc() will return 1 if
it allocated a chunk.  Since the end of the function is the success path
just return 0.

CC: stable@vger.kernel.org # 4.4+
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: merge free_fs_root helpers
David Sterba [Fri, 20 Jul 2018 14:30:25 +0000 (16:30 +0200)]
btrfs: merge free_fs_root helpers

The exported helper just calls the static one. There's no obvious reason
to have them separate eg. for performance reasons where the static one
could be better optimized in the same unit. There's a slight decrease in
code size and stack consumption.

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: constify strings passed to assertion helper
David Sterba [Fri, 20 Jul 2018 14:30:23 +0000 (16:30 +0200)]
btrfs: constify strings passed to assertion helper

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: dev-replace: remove unused members of btrfs_dev_replace
David Sterba [Fri, 20 Jul 2018 14:30:20 +0000 (16:30 +0200)]
btrfs: dev-replace: remove unused members of btrfs_dev_replace

Lock owner and nesting level have been unused since day 1, probably
copy&pasted from the extent_buffer locking scheme without much thinking.
The locking of device replace is simpler and does not need any lock
nesting.

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: remove unused member btrfs_root::name
David Sterba [Fri, 20 Jul 2018 14:30:18 +0000 (16:30 +0200)]
btrfs: remove unused member btrfs_root::name

Added in 58176a9604c ("Btrfs: Add per-root block accounting and sysfs
entries") in 2007, the roots had names exported in sysfs. The code
was commented out in 4df27c4d5cc1dda54ed ("Btrfs: change how subvolumes
are organized") and cleaned by 182608c8294b5fe9 ("btrfs: remove old
unused commented out code").

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: allow defrag on a file opened read-only that has rw permissions
Adam Borowski [Tue, 17 Jul 2018 22:08:59 +0000 (00:08 +0200)]
btrfs: allow defrag on a file opened read-only that has rw permissions

Requiring a read-write descriptor conflicts both ways with exec,
returning ETXTBSY whenever you try to defrag a program that's currently
being run, or causing intermittent exec failures on a live system being
defragged.

As defrag doesn't change the file's contents in any way, there's no
reason to consider it a rw operation.  Thus, let's check only whether
the file could have been opened rw.  Such access control is still needed
as currently defrag can use extra disk space, and might trigger bugs.

We return EINVAL when the request is invalid; here it's ok but merely
the user has insufficient privileges.  Thus, the EPERM return value
reflects the error better -- as discussed in the identical case for
dedupe.

According to codesearch.debian.net, no userspace program distinguishes
these values beyond strerror().

Signed-off-by: Adam Borowski <kilobyte@angband.pl>
Reviewed-by: David Sterba <dsterba@suse.com>
[ fold the EPERM patch from Adam ]
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agoBtrfs: fix btrfs_write_inode vs delayed iput deadlock
Josef Bacik [Fri, 20 Jul 2018 18:46:10 +0000 (11:46 -0700)]
Btrfs: fix btrfs_write_inode vs delayed iput deadlock

We recently ran into the following deadlock involving
btrfs_write_inode():

[  +0.005066]  __schedule+0x38e/0x8c0
[  +0.007144]  schedule+0x36/0x80
[  +0.006447]  bit_wait+0x11/0x60
[  +0.006446]  __wait_on_bit+0xbe/0x110
[  +0.007487]  ? bit_wait_io+0x60/0x60
[  +0.007319]  __inode_wait_for_writeback+0x96/0xc0
[  +0.009568]  ? autoremove_wake_function+0x40/0x40
[  +0.009565]  inode_wait_for_writeback+0x21/0x30
[  +0.009224]  evict+0xb0/0x190
[  +0.006099]  iput+0x1a8/0x210
[  +0.006103]  btrfs_run_delayed_iputs+0x73/0xc0
[  +0.009047]  btrfs_commit_transaction+0x799/0x8c0
[  +0.009567]  btrfs_write_inode+0x81/0xb0
[  +0.008008]  __writeback_single_inode+0x267/0x320
[  +0.009569]  writeback_sb_inodes+0x25b/0x4e0
[  +0.008702]  wb_writeback+0x102/0x2d0
[  +0.007487]  wb_workfn+0xa4/0x310
[  +0.006794]  ? wb_workfn+0xa4/0x310
[  +0.007143]  process_one_work+0x150/0x410
[  +0.008179]  worker_thread+0x6d/0x520
[  +0.007490]  kthread+0x12c/0x160
[  +0.006620]  ? put_pwq_unlocked+0x80/0x80
[  +0.008185]  ? kthread_park+0xa0/0xa0
[  +0.007484]  ? do_syscall_64+0x53/0x150
[  +0.007837]  ret_from_fork+0x29/0x40

Writeback calls:

btrfs_write_inode
  btrfs_commit_transaction
    btrfs_run_delayed_iputs

If iput() is called on that same inode, evict() will wait for writeback
forever.

btrfs_write_inode() was originally added way back in 4730a4bc5bf3
("btrfs_dirty_inode") to support O_SYNC writes. However, ->write_inode()
hasn't been used for O_SYNC since 148f948ba877 ("vfs: Introduce new
helpers for syncing after writing to O_SYNC file or IS_SYNC inode"), so
btrfs_write_inode() is actually unnecessary (and leads to a bunch of
unnecessary commits). Get rid of it, which also gets rid of the
deadlock.

CC: stable@vger.kernel.org # 3.2+
Signed-off-by: Josef Bacik <jbacik@fb.com>
[Omar: new commit message]
Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_finish_chunk_alloc
Nikolay Borisov [Fri, 20 Jul 2018 16:37:53 +0000 (19:37 +0300)]
btrfs: Remove fs_info from btrfs_finish_chunk_alloc

It can be referenced from the passed transaction handle.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info form btrfs_free_chunk
Nikolay Borisov [Fri, 20 Jul 2018 16:37:52 +0000 (19:37 +0300)]
btrfs: Remove fs_info form btrfs_free_chunk

It can be referenced from the passed transaction handle.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_destroy_dev_replace_tgtdev
Nikolay Borisov [Fri, 20 Jul 2018 16:37:51 +0000 (19:37 +0300)]
btrfs: Remove fs_info from btrfs_destroy_dev_replace_tgtdev

This function is always passed a well-formed tgtdevice so the fs_info
can be referenced from there.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_assign_next_active_device
Nikolay Borisov [Fri, 20 Jul 2018 16:37:50 +0000 (19:37 +0300)]
btrfs: Remove fs_info from btrfs_assign_next_active_device

It can be referenced from the passed 'device' argument which is always
a well-formed device.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: remove fs_info argument from update_dev_stat_item
Nikolay Borisov [Fri, 20 Jul 2018 16:37:49 +0000 (19:37 +0300)]
btrfs: remove fs_info argument from update_dev_stat_item

It can be referenced from the passed transaction handle.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info from btrfs_rm_dev_replace_remove_srcdev
Nikolay Borisov [Fri, 20 Jul 2018 16:37:48 +0000 (19:37 +0300)]
btrfs: Remove fs_info from btrfs_rm_dev_replace_remove_srcdev

It can be referenced from the passed srcdev argument.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove fs_info argument from btrfs_add_dev_item
Nikolay Borisov [Fri, 20 Jul 2018 16:37:47 +0000 (19:37 +0300)]
btrfs: Remove fs_info argument from btrfs_add_dev_item

It can be referenced form the passed transaction handle.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: extent-tree: Remove dead alignment check
Qu Wenruo [Mon, 23 Jul 2018 06:47:29 +0000 (14:47 +0800)]
btrfs: extent-tree: Remove dead alignment check

In find_free_extent() under checks: label, we have the following code:

search_start = ALIGN(offset, fs_info->stripesize);
/* move on to the next group */
if (search_start + num_bytes >
    block_group->key.objectid + block_group->key.offset) {
btrfs_add_free_space(block_group, offset, num_bytes);
goto loop;
}
if (offset < search_start)
btrfs_add_free_space(block_group, offset,
     search_start - offset);
BUG_ON(offset > search_start);

However ALIGN() is rounding up, thus @search_start >= @offset and that
BUG_ON() will never be triggered.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agoBtrfs: remove unused key assignment when doing a full send
Filipe Manana [Mon, 23 Jul 2018 08:10:09 +0000 (09:10 +0100)]
Btrfs: remove unused key assignment when doing a full send

At send.c:full_send_tree() we were setting the 'key' variable in the loop
while never using it later. We were also using two btrfs_key variables
to store the initial key for search and the key found in every iteration
of the loop. So remove this useless key assignment and use the same
btrfs_key variable to store the initial search key and the key found in
each iteration. This was introduced in the initial send commit but was
never used (commit 31db9f7c23fb ("Btrfs: introduce BTRFS_IOC_SEND for
btrfs send/receive").

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: drop extent_io_ops::set_range_writeback callback
David Sterba [Wed, 18 Jul 2018 18:32:52 +0000 (20:32 +0200)]
btrfs: drop extent_io_ops::set_range_writeback callback

The data and metadata callback implementation both use the same
function. We can remove the call indirection and intermediate helper
completely.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: drop extent_io_ops::merge_bio_hook callback
David Sterba [Wed, 18 Jul 2018 17:28:09 +0000 (19:28 +0200)]
btrfs: drop extent_io_ops::merge_bio_hook callback

The data and metadata callback implementation both use the same
function. We can remove the call indirection completely.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: drop extent_io_ops::tree_fs_info callback
David Sterba [Wed, 18 Jul 2018 17:23:45 +0000 (19:23 +0200)]
btrfs: drop extent_io_ops::tree_fs_info callback

All implementations of the callback are trivial and do the same and
there's only one user. Merge everything together.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: unify end_io callbacks of async_submit_bio
David Sterba [Wed, 18 Jul 2018 15:36:24 +0000 (17:36 +0200)]
btrfs: unify end_io callbacks of async_submit_bio

The end_io callbacks passed to btrfs_wq_submit_bio
(btrfs_submit_bio_done and btree_submit_bio_done) are effectively the
same code, there's no point to do the indirection. Export
btrfs_submit_bio_done and call it directly.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: remove unused member async_submit_bio::bio_flags
David Sterba [Wed, 18 Jul 2018 15:31:10 +0000 (17:31 +0200)]
btrfs: remove unused member async_submit_bio::bio_flags

After splitting the start and end hooks in a758781d4b76c3 ("btrfs:
separate types for submit_bio_start and submit_bio_done"), some of
the function arguments were dropped but not removed from the structure.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: remove unused member async_submit_bio::fs_info
David Sterba [Wed, 18 Jul 2018 15:31:19 +0000 (17:31 +0200)]
btrfs: remove unused member async_submit_bio::fs_info

Introduced by c6100a4b4e3d1 ("Btrfs: replace tree->mapping with
tree->private_data") to be used in run_one_async_done where it got
unused after 736cd52e0c720103 ("Btrfs: remove nr_async_submits and
async_submit_draining").

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: validate type when reading a chunk
Gu Jinxiang [Wed, 4 Jul 2018 10:16:39 +0000 (18:16 +0800)]
btrfs: validate type when reading a chunk

Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199839, with an
image that has an invalid chunk type but does not return an error.

Add chunk type check in btrfs_check_chunk_valid, to detect the wrong
type combinations.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=199839
Reported-by: Xu Wen <wen.xu@gatech.edu>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Rename EXTENT_BUFFER_DUMMY to EXTENT_BUFFER_UNMAPPED
Nikolay Borisov [Wed, 27 Jun 2018 13:38:24 +0000 (16:38 +0300)]
btrfs: Rename EXTENT_BUFFER_DUMMY to EXTENT_BUFFER_UNMAPPED

EXTENT_BUFFER_DUMMY is an awful name for this flag. Buffers which have
this flag set are not in any way dummy. Rather, they are private in the
sense that are not mapped and linked to the global buffer tree. This
flag has subtle implications to the way free_extent_buffer works for
example, as well as controls whether page->mapping->private_lock is held
during extent_buffer release. Pages for an unmapped buffer cannot be
under io, nor can they be written by a 3rd party so taking the lock is
unnecessary.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ EXTENT_BUFFER_UNMAPPED, update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Document locking requirement via lockdep_assert_held
Nikolay Borisov [Wed, 27 Jun 2018 13:38:23 +0000 (16:38 +0300)]
btrfs: Document locking requirement via lockdep_assert_held

Remove stale comment since there is no longer an eb->eb_lock and
document the locking expectation with a lockdep_assert_held statement.
No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: rename btrfs_release_extent_buffer_page
David Sterba [Thu, 19 Jul 2018 15:24:32 +0000 (17:24 +0200)]
btrfs: rename btrfs_release_extent_buffer_page

The function used to release one page (and always the first one), but
not anymore since a50924e3a4d7fccb0ecfbd4 ("btrfs: drop constant param
from btrfs_release_extent_buffer_page").  Update the name and comment.

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Refactor loop in btrfs_release_extent_buffer_page
Nikolay Borisov [Wed, 27 Jun 2018 13:38:22 +0000 (16:38 +0300)]
btrfs: Refactor loop in btrfs_release_extent_buffer_page

The purpose of the function is to free all the pages comprising an
extent buffer. This can be achieved with a simple for loop rather than
the slightly more involved 'do {} while' construct. So rewrite the
loop using a 'for' construct. Additionally we can never have an
extent_buffer that has 0 pages so remove the check for index == 0. No
functional changes.

The reversed order used to have a meaning in the past where the first
page served as a blocking point for several callers. See eg
4f2de97acee6532b36dd6e99 ("Btrfs: set page->private to the eb").

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Reword dodgy comments in alloc_extent_buffer
Nikolay Borisov [Wed, 4 Jul 2018 07:24:52 +0000 (10:24 +0300)]
btrfs: Reword dodgy comments in alloc_extent_buffer

Commit eb14ab8ed24a ("Btrfs: fix page->private races") fixed a genuine
race between extent buffer initialisation and btree_releasepage.
Unfortunately as the code has evolved the comments weren't changed which
made them slightly wrong and they weren't very clear in the fist place.
Fix this by (hopefully) rewording them in a more approachable manner.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Simplify page unlocking in alloc_extent_buffer
Nikolay Borisov [Wed, 4 Jul 2018 07:24:51 +0000 (10:24 +0300)]
btrfs: Simplify page unlocking in alloc_extent_buffer

Current version of the page unlocking code was added in
727011e07cbd ("Btrfs: allow metadata blocks larger than the page size")
but even in this commit that particular flag was never used per-se. In
fact, btrfs only uses PageChecked for data pages to identify pages
which have been dirtied but don't have ORDERED bit set. For more
information see 247e743cbe6e ("Btrfs: Use async helpers to deal with
pages that have been improperly dirtied").

However, this doesn't apply to extent buffer pages. The important bit
here is that the pages are unlocked AFTER the extent buffer has been
properly recorded in the radix tree to avoid races with
btree_releasepage. Let's exploit this fact and simplify the page
unlocking sequence by unlocking the pages in-order and removing the
redundant PageChecked flag setting/clearing.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: scrub: cleanup the remaining nodatasum fixup code
Qu Wenruo [Wed, 11 Jul 2018 05:41:22 +0000 (13:41 +0800)]
btrfs: scrub: cleanup the remaining nodatasum fixup code

Remove the remaining code that misused the page cache pages during
device replace and could cause data corruption for compressed nodatasum
extents. Such files do not normally exist but there's a bug that allows
this combination and the corruption was exposed by device replace fixup
code.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: refactor block group replication factor calculation to a helper
David Sterba [Fri, 13 Jul 2018 18:46:30 +0000 (20:46 +0200)]
btrfs: refactor block group replication factor calculation to a helper

There are many places that open code the duplicity factor of the block
group profiles, create a common helper. This can be easily extended for
more copies.

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: use the assigned fs_devices instead of the dereference
Anand Jain [Mon, 16 Jul 2018 14:58:09 +0000 (22:58 +0800)]
btrfs: use the assigned fs_devices instead of the dereference

We have assigned the %fs_info->fs_devices in %fs_devices as its not
modified just use it for the mutex_lock().

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from qgroup_rescan_leaf
Lu Fengqi [Wed, 18 Jul 2018 06:45:42 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from qgroup_rescan_leaf

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_qgroup_inherit
Lu Fengqi [Wed, 18 Jul 2018 06:45:41 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_qgroup_inherit

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_run_qgroups
Lu Fengqi [Wed, 18 Jul 2018 06:45:40 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_run_qgroups

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_qgroup_account_extent
Lu Fengqi [Wed, 18 Jul 2018 06:45:39 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_qgroup_account_extent

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop root parameter from btrfs_qgroup_trace_subtree
Lu Fengqi [Wed, 18 Jul 2018 06:45:38 +0000 (14:45 +0800)]
btrfs: qgroup: Drop root parameter from btrfs_qgroup_trace_subtree

The fs_info can be fetched from the transaction handle directly.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_qgroup_trace_leaf_items
Lu Fengqi [Wed, 18 Jul 2018 06:45:37 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_qgroup_trace_leaf_items

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_qgroup_trace_extent
Lu Fengqi [Wed, 18 Jul 2018 08:28:03 +0000 (16:28 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_qgroup_trace_extent

It can be fetched from the transaction handle. In addition, remove the
WARN_ON(trans == NULL) because it's not possible to hit this condition.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_limit_qgroup
Lu Fengqi [Wed, 18 Jul 2018 06:45:35 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_limit_qgroup

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_remove_qgroup
Lu Fengqi [Wed, 18 Jul 2018 06:45:34 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_remove_qgroup

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_create_qgroup
Lu Fengqi [Wed, 18 Jul 2018 06:45:33 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_create_qgroup

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_del_qgroup_relation
Lu Fengqi [Wed, 18 Jul 2018 06:45:32 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_del_qgroup_relation

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from __del_qgroup_relation
Lu Fengqi [Wed, 18 Jul 2018 06:45:31 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from __del_qgroup_relation

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop fs_info parameter from btrfs_add_qgroup_relation
Lu Fengqi [Wed, 18 Jul 2018 06:45:30 +0000 (14:45 +0800)]
btrfs: qgroup: Drop fs_info parameter from btrfs_add_qgroup_relation

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop quota_root and fs_info parameters from update_qgroup_status_item
Lu Fengqi [Wed, 18 Jul 2018 06:45:29 +0000 (14:45 +0800)]
btrfs: qgroup: Drop quota_root and fs_info parameters from update_qgroup_status_item

They can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop root parameter from update_qgroup_info_item
Lu Fengqi [Wed, 18 Jul 2018 06:45:28 +0000 (14:45 +0800)]
btrfs: qgroup: Drop root parameter from update_qgroup_info_item

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop root parameter from update_qgroup_limit_item
Lu Fengqi [Wed, 18 Jul 2018 06:45:27 +0000 (14:45 +0800)]
btrfs: qgroup: Drop root parameter from update_qgroup_limit_item

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop quota_root parameter from del_qgroup_item
Lu Fengqi [Wed, 18 Jul 2018 06:45:26 +0000 (14:45 +0800)]
btrfs: qgroup: Drop quota_root parameter from del_qgroup_item

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop quota_root parameter from del_qgroup_relation_item
Lu Fengqi [Wed, 18 Jul 2018 06:45:25 +0000 (14:45 +0800)]
btrfs: qgroup: Drop quota_root parameter from del_qgroup_relation_item

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: Drop quota_root parameter from add_qgroup_relation_item
Lu Fengqi [Wed, 18 Jul 2018 06:45:24 +0000 (14:45 +0800)]
btrfs: qgroup: Drop quota_root parameter from add_qgroup_relation_item

It can be fetched from the transaction handle.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: rename btrfs_parse_early_options
Anand Jain [Mon, 16 Jul 2018 14:18:07 +0000 (22:18 +0800)]
btrfs: rename btrfs_parse_early_options

Rename btrfs_parse_early_options() to btrfs_parse_device_options(). As
btrfs_parse_early_options() parses the -o device options and scan the
device provided. So this rename specifies its action. Also the function
name is in line with btrfs_parse_subvol_options().
No functional changes.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroup: cleanup the unused srcroot from btrfs_qgroup_inherit
Lu Fengqi [Tue, 17 Jul 2018 08:58:22 +0000 (16:58 +0800)]
btrfs: qgroup: cleanup the unused srcroot from btrfs_qgroup_inherit

Since commit 0b246afa62b0 ("btrfs: root->fs_info cleanup, add fs_info
convenience variables"), the srcroot is no longer used to get
fs_info::nodesize.  In fact, it can be dropped after commit 707e8a071528
("btrfs: use nodesize everywhere, kill leafsize").

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Use btrfs_mark_bg_unused to replace open code
Qu Wenruo [Tue, 22 May 2018 08:43:47 +0000 (16:43 +0800)]
btrfs: Use btrfs_mark_bg_unused to replace open code

Introduce a small helper, btrfs_mark_bg_unused(), to acquire locks and
add a block group to unused_bgs list.

No functional modification, and only 3 callers are involved.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Rewrite retry logic in do_chunk_alloc
Nikolay Borisov [Wed, 18 Apr 2018 07:27:57 +0000 (10:27 +0300)]
btrfs: Rewrite retry logic in do_chunk_alloc

do_chunk_alloc implements logic to detect whether there is currently
pending chunk allocation (by means of space_info->chunk_alloc being
set) and if so it loops around to the 'again' label. Additionally,
based on the state of the space_info (e.g. whether it's full or not)
and the return value of should_alloc_chunk() it decides whether this
is a "hard" error (ENOSPC) or we can just return 0.

This patch refactors all of this:

1. Put order to the scattered ifs handling the various cases in an
easy-to-read if {} else if{} branches. This makes clear the various
cases we are interested in handling.

2. Call should_alloc_chunk only once and use the result in the
if/else if constructs. All of this is done under space_info->lock, so
even before multiple calls of should_alloc_chunk were unnecessary.

3. Rewrite the "do {} while()" loop currently implemented via label
into an explicit loop construct.

4. Move the mutex locking for the case where the caller is the one doing
the allocation. For the case where the caller needs to wait a concurrent
allocation, introduce a pair of mutex_lock/mutex_unlock to act as a
barrier and reword the comment.

5. Switch local vars to bool type where pertinent.

All in all this shouldn't introduce any functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: use customized batch size for total_bytes_pinned
Ethan Lien [Fri, 13 Jul 2018 08:50:42 +0000 (16:50 +0800)]
btrfs: use customized batch size for total_bytes_pinned

In commit b150a4f10d878 ("Btrfs: use a percpu to keep track of possibly
pinned bytes") we use total_bytes_pinned to track how many bytes we are
going to free in this transaction. When we are close to ENOSPC, we check it
and know if we can make the allocation by commit the current transaction.
For every data/metadata extent we are going to free, we add
total_bytes_pinned in btrfs_free_extent() and btrfs_free_tree_block(), and
release it in unpin_extent_range() when we finish the transaction. So this
is a variable we frequently update but rarely read - just the suitable
use of percpu_counter. But in previous commit we update total_bytes_pinned
by default 32 batch size, making every update essentially a spin lock
protected update. Since every spin lock/unlock operation involves syncing
a globally used variable and some kind of barrier in a SMP system, this is
more expensive than using total_bytes_pinned as a simple atomic64_t.

So fix this by using a customized batch size. Since we only read
total_bytes_pinned when we are close to ENOSPC and fail to allocate new
chunk, we can use a really large batch size and have nearly no penalty
in most cases.

[Test]
We tested the patch on a 4-cores x86 machine:

1. fallocate a 16GiB size test file
2. take snapshot (so all following writes will be COW)
3. run a 180 sec, 4 jobs, 4K random write fio on test file

We also added a temporary lockdep class on percpu_counter's spin lock
used by total_bytes_pinned to track it by lock_stat.

[Results]
unpatched:
lock_stat version 0.4
-----------------------------------------------------------------------
                              class name    con-bounces    contentions
waittime-min   waittime-max waittime-total   waittime-avg    acq-bounces
acquisitions   holdtime-min   holdtime-max holdtime-total   holdtime-avg

               total_bytes_pinned_percpu:            82             82
        0.21           0.61          29.46           0.36         298340
      635973           0.09          11.01      173476.25           0.27

patched:
lock_stat version 0.4
-----------------------------------------------------------------------
                              class name    con-bounces    contentions
waittime-min   waittime-max waittime-total   waittime-avg    acq-bounces
acquisitions   holdtime-min   holdtime-max holdtime-total   holdtime-avg

               total_bytes_pinned_percpu:             1              1
        0.62           0.62           0.62           0.62          13601
       31542           0.14           9.61       11016.90           0.35

[Analysis]
Since the spin lock only protects a single in-memory variable, the
contentions (number of lock acquisitions that had to wait) in both
unpatched and patched version are low. But when we see acquisitions and
acq-bounces, we get much lower counts in patched version. Here the most
important metric is acq-bounces. It means how many times the lock gets
transferred between different cpus, so the patch can really reduce
cacheline bouncing of spin lock (also the global counter of percpu_counter)
in a SMP system.

Fixes: b150a4f10d878 ("Btrfs: use a percpu to keep track of possibly pinned bytes")
Signed-off-by: Ethan Lien <ethanlien@synology.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: use correct compare function of dirty_metadata_bytes
Ethan Lien [Mon, 2 Jul 2018 07:44:58 +0000 (15:44 +0800)]
btrfs: use correct compare function of dirty_metadata_bytes

We use customized, nodesize batch value to update dirty_metadata_bytes.
We should also use batch version of compare function or we will easily
goto fast path and get false result from percpu_counter_compare().

Fixes: e2d845211eda ("Btrfs: use percpu counter for dirty metadata count")
CC: stable@vger.kernel.org # 4.4+
Signed-off-by: Ethan Lien <ethanlien@synology.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: return device pointer from btrfs_scan_one_device
Gu Jinxiang [Thu, 12 Jul 2018 06:23:16 +0000 (14:23 +0800)]
btrfs: return device pointer from btrfs_scan_one_device

Return device pointer (with the IS_ERR semantics) from
btrfs_scan_one_device so we don't have to return in through pointer.

And since btrfs_fs_devices can be obtained from btrfs_device, return that.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ fixed conflics after recent changes to btrfs_scan_one_device ]
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: make fs_devices a local variable in btrfs_parse_early_options
Gu Jinxiang [Thu, 12 Jul 2018 06:23:15 +0000 (14:23 +0800)]
btrfs: make fs_devices a local variable in btrfs_parse_early_options

fs_devices is always passed to btrfs_scan_one_device which overrides it.
In the call stack below fs_devices is passed to btrfs_scan_one_device
from btrfs_mount_root.  In btrfs_mount_root the output fs_devices of
this call stack is not used.

btrfs_mount_root
  btrfs_parse_early_options
    btrfs_scan_one_device

So, it is not necessary to pass fs_devices from btrfs_mount_root, using
a local variable in btrfs_parse_early_options is enough.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: Anand Jain <Anand.Jain@oracle.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: fix mount and ioctl device scan ioctl race
David Sterba [Tue, 19 Jun 2018 16:04:07 +0000 (18:04 +0200)]
btrfs: fix mount and ioctl device scan ioctl race

Technically this extends the critical section covered by uuid_mutex to:

- parse early mount options -- here we can call device scan on paths
  that can be passed as 'device=/dev/...'

- scan the device passed to mount

- open the devices related to the fs_devices -- this increases
  fs_devices::opened

The race can happen when mount calls one of the scans and there's
another one called eg. by mkfs or 'btrfs dev scan':

Mount                                  Scan
-----                                  ----
scan_one_device (dev1, fsid1)
                                       scan_one_device (dev2, fsid1)
           add the device
   free stale devices
       fsid1 fs_devices::opened == 0
           find fsid1:dev1
           free fsid1:dev1
           if it's the last one,
            free fs_devices of fsid1
    too

open_devices (dev1, fsid1)
   dev1 not found

When fixed, the uuid mutex will make sure that mount will increase
fs_devices::opened and this will not be touched by the racing scan
ioctl.

Reported-and-tested-by: syzbot+909a5177749d7990ffa4@syzkaller.appspotmail.com
Reported-and-tested-by: syzbot+ceb2606025ec1cc3479c@syzkaller.appspotmail.com
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: reorder initialization before the mount locks uuid_mutex
David Sterba [Tue, 19 Jun 2018 16:01:24 +0000 (18:01 +0200)]
btrfs: reorder initialization before the mount locks uuid_mutex

In preparation to take a big lock, move resource initialization before
the critical section. It's not obvious from the diff, the desired order
is:

- initialize mount security options
- allocate temporary fs_info
- allocate superblock buffers

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: lift uuid_mutex to callers of btrfs_parse_early_options
David Sterba [Tue, 19 Jun 2018 15:50:25 +0000 (17:50 +0200)]
btrfs: lift uuid_mutex to callers of btrfs_parse_early_options

Prepartory work to fix race between mount and device scan.

btrfs_parse_early_options calls the device scan from mount and we'll
need to let mount completely manage the critical section.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: lift uuid_mutex to callers of btrfs_open_devices
David Sterba [Tue, 19 Jun 2018 15:09:47 +0000 (17:09 +0200)]
btrfs: lift uuid_mutex to callers of btrfs_open_devices

Prepartory work to fix race between mount and device scan.

The callers will have to manage the critical section, eg. mount wants to
scan and then call btrfs_open_devices without the ioctl scan walking in
and modifying the fs devices in the meantime.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: lift uuid_mutex to callers of btrfs_scan_one_device
David Sterba [Tue, 19 Jun 2018 14:37:36 +0000 (16:37 +0200)]
btrfs: lift uuid_mutex to callers of btrfs_scan_one_device

Prepartory work to fix race between mount and device scan.

The callers will have to manage the critical section, eg. mount wants to
scan and then call btrfs_open_devices without the ioctl scan walking in
and modifying the fs devices in the meantime.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: use device_list_mutex when removing stale devices
Anand Jain [Tue, 29 May 2018 09:23:20 +0000 (17:23 +0800)]
btrfs: use device_list_mutex when removing stale devices

btrfs_free_stale_devices() finds a stale (not opened) device matching
path in the fs_uuid list. We are already under uuid_mutex so when we
check for each fs_devices, hold the device_list_mutex too.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: rename local devices for fs_devices in btrfs_free_stale_devices(
Anand Jain [Tue, 29 May 2018 07:33:08 +0000 (15:33 +0800)]
btrfs: rename local devices for fs_devices in btrfs_free_stale_devices(

Over the years we named %fs_devices and %devices to represent the
struct btrfs_fs_devices and the struct btrfs_device. So follow the same
scheme here too. No functional changes.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: extend locked section when adding a new device in device_list_add
Anand Jain [Tue, 29 May 2018 06:10:20 +0000 (14:10 +0800)]
btrfs: extend locked section when adding a new device in device_list_add

Make sure the device_list_lock is held the whole time:

* when the device is being looked up
* new device is initialized and put to the list
* the list counters are updated (fs_devices::opened, fs_devices::total_devices)

Signed-off-by: Anand Jain <anand.jain@oracle.com>
[ update changelog ]
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: do btrfs_free_stale_devices outside of device_list_add
Anand Jain [Tue, 29 May 2018 04:28:37 +0000 (12:28 +0800)]
btrfs: do btrfs_free_stale_devices outside of device_list_add

btrfs_free_stale_devices() looks for device path reused for another
filesystem, and deletes the older fs_devices::device entry.

In preparation to handle locking in device_list_add, move
btrfs_free_stale_devices outside as these two functions serve a
different purpose.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: close devices without offloading to a temporary list
Nikolay Borisov [Fri, 29 Jun 2018 05:26:05 +0000 (08:26 +0300)]
btrfs: close devices without offloading to a temporary list

Since commit 88c14590cdd6 ("btrfs: use RCU in btrfs_show_devname for
device list traversal") btrfs_show_devname no longer takes
device_list_mutex. As such the deadlock that 0ccd05285e7f ("btrfs: fix a
possible umount deadlock") aimed to fix no longer exists, we can free
the devices immediatelly and remove the code that does the pending work.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
[ update changelog ]
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: Remove unused function btrfs_account_dev_extents_size
Qu Wenruo [Mon, 9 Jul 2018 06:52:54 +0000 (14:52 +0800)]
btrfs: Remove unused function btrfs_account_dev_extents_size

This function is not used since the alloc_start parameter has been
obsoleted in commit 0d0c71b317207082856 ("btrfs: obsolete and remove
mount option alloc_start").

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: remove unused parameter from btrfs_parse_subvol_options
Gu Jinxiang [Mon, 9 Jul 2018 06:39:15 +0000 (14:39 +0800)]
btrfs: remove unused parameter from btrfs_parse_subvol_options

Since parameter flags is no more used since commit d7407606564c ("btrfs:
split parse_early_options() in two"), remove it.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: fix in-memory value of total_devices after seed device deletion
Anand Jain [Tue, 3 Jul 2018 09:07:23 +0000 (17:07 +0800)]
btrfs: fix in-memory value of total_devices after seed device deletion

In case of deleting the seed device the %cur_devices (seed) and the
%fs_devices (parent) are different. Now, as the parent
fs_devices::total_devices also maintains the total number of devices
including the seed device, so decrement its in-memory value for the
successful seed delete. We are already updating its corresponding
on-disk btrfs_super_block::number_devices value.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: qgroups: Move transaction management inside btrfs_quota_enable/disable
Nikolay Borisov [Thu, 5 Jul 2018 11:50:48 +0000 (14:50 +0300)]
btrfs: qgroups: Move transaction management inside btrfs_quota_enable/disable

Commit 5d23515be669 ("btrfs: Move qgroup rescan on quota enable to
btrfs_quota_enable") not only resulted in an easier to follow code but
it also introduced a subtle bug. It changed the timing when the initial
transaction rescan was happening:

- before the commit: it would happen after transaction commit had occured
- after the commit: it might happen before the transaction was committed

This results in failure to correctly rescan the quota since there could
be data which is still not committed on disk.

This patch aims to fix this by moving the transaction creation/commit
inside btrfs_quota_enable, which allows to schedule the quota commit
after the transaction has been committed.

Fixes: 5d23515be669 ("btrfs: Move qgroup rescan on quota enable to btrfs_quota_enable")
Reported-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
Link: https://marc.info/?l=linux-btrfs&m=152999289017582
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: raid56: catch errors from full_stripe_write
David Sterba [Fri, 29 Jun 2018 08:57:10 +0000 (10:57 +0200)]
btrfs: raid56: catch errors from full_stripe_write

Add fall-back code to catch failure of full_stripe_write. Proper error
handling from inside run_plug would need more code restructuring as it's
called at arbitrary points by io scheduler.

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: raid56: merge rbio_is_full helpers
David Sterba [Fri, 29 Jun 2018 08:57:05 +0000 (10:57 +0200)]
btrfs: raid56: merge rbio_is_full helpers

There's only one call site of the unlocked helper so it can be folded
into the caller.

Signed-off-by: David Sterba <dsterba@suse.com>
5 years agobtrfs: raid56: use new helper for async_scrub_parity
David Sterba [Fri, 29 Jun 2018 08:57:03 +0000 (10:57 +0200)]
btrfs: raid56: use new helper for async_scrub_parity

Signed-off-by: David Sterba <dsterba@suse.com>