openwrt/staging/blogic.git
6 years agobtrfs: drop fs_info parameter from tree_mod_log_free_eb
David Sterba [Mon, 5 Mar 2018 14:14:25 +0000 (15:14 +0100)]
btrfs: drop fs_info parameter from tree_mod_log_free_eb

It's provided by the extent_buffer.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: drop fs_info parameter from tree_mod_log_insert_key
David Sterba [Mon, 5 Mar 2018 14:09:03 +0000 (15:09 +0100)]
btrfs: drop fs_info parameter from tree_mod_log_insert_key

It's provided by the extent_buffer.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: drop fs_info parameter from tree_mod_log_insert_move
David Sterba [Mon, 5 Mar 2018 14:03:52 +0000 (15:03 +0100)]
btrfs: drop fs_info parameter from tree_mod_log_insert_move

It's provided by the extent_buffer.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: drop fs_info parameter from tree_mod_log_set_node_key
David Sterba [Mon, 5 Mar 2018 14:00:37 +0000 (15:00 +0100)]
btrfs: drop fs_info parameter from tree_mod_log_set_node_key

It's provided by the extent_buffer.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: document more parameters of submit_extent_page
David Sterba [Mon, 12 Jun 2017 17:50:41 +0000 (19:50 +0200)]
btrfs: document more parameters of submit_extent_page

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: cleanup merging conditions in submit_extent_page
David Sterba [Mon, 12 Jun 2017 18:00:43 +0000 (20:00 +0200)]
btrfs: cleanup merging conditions in submit_extent_page

The merge call was factored out to a separate helper but it's a trivial
one and arguably we can opencode it and cache the value.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: remove redundant variable in __do_readpage
David Sterba [Tue, 6 Jun 2017 17:50:13 +0000 (19:50 +0200)]
btrfs: remove redundant variable in __do_readpage

The value of page_end is only stored to end, no other use.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: assume that bio_ret is always valid in submit_extent_page
David Sterba [Tue, 6 Jun 2017 17:22:55 +0000 (19:22 +0200)]
btrfs: assume that bio_ret is always valid in submit_extent_page

All callers pass a valid pointer so we can drop the redundant checks.
The call to submit_one_bio never happend and can be removed.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: scrub: batch rebuild for raid56
Liu Bo [Wed, 7 Mar 2018 19:08:09 +0000 (12:08 -0700)]
Btrfs: scrub: batch rebuild for raid56

In case of raid56, writes and rebuilds always take BTRFS_STRIPE_LEN(64K)
as unit, however, scrub_extent() sets blocksize as unit, so rebuild
process may be triggered on every block on a same stripe.

A typical example would be that when we're replacing a disappeared disk,
all reads on the disks get -EIO, every block (size is 4K if blocksize is
4K) would go thru these,

scrub_handle_errored_block
  scrub_recheck_block # re-read pages one by one
  scrub_recheck_block # rebuild by calling raid56_parity_recover()
                        page by page

Although with raid56 stripe cache most of reads during rebuild can be
avoided, the parity recover calculation(xor or raid6 algorithms) needs to
be done $(BTRFS_STRIPE_LEN / blocksize) times.

This makes it smarter by doing raid56 scrub/replace on stripe length.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: sort and group mount option definitions
David Sterba [Fri, 9 Mar 2018 13:37:01 +0000 (14:37 +0100)]
btrfs: sort and group mount option definitions

Sort mount options by the primary name, followed by the 'no-'
counterpart if it exists. Group the deprecated and debugging options.
Enum and token defintions are synced.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Add nossd_spread mount option
Howard McLauchlan [Thu, 8 Mar 2018 18:48:48 +0000 (10:48 -0800)]
btrfs: Add nossd_spread mount option

Btrfs has two mount options for SSD optimizations: ssd and ssd_spread.
Presently there is an option to disable all SSD optimizations, but there
isn't an option to disable just ssd_spread.

This patch adds a mount option nossd_spread that disables ssd_spread
only.

Reviewed-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: Howard McLauchlan <hmclauchlan@fb.com>
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove btrfs_fs_info::open_ioctl_trans
Nikolay Borisov [Mon, 5 Feb 2018 08:41:16 +0000 (10:41 +0200)]
btrfs: Remove btrfs_fs_info::open_ioctl_trans

Since userspace transaction have been removed we no longer have use
for this field so delete it.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove code referencing unused TRANS_USERSPACE
Nikolay Borisov [Mon, 5 Feb 2018 08:41:15 +0000 (10:41 +0200)]
btrfs: Remove code referencing unused TRANS_USERSPACE

Now that the userspace transaction ioctls have been removed,
TRANS_USERSPACE is no longer used hence we can remove it.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove btrfs_file_private::trans
Nikolay Borisov [Mon, 5 Feb 2018 08:41:14 +0000 (10:41 +0200)]
btrfs: Remove btrfs_file_private::trans

Now that the userspace transaction IOCTL have been removed, this member
is no longer used so just remove it

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove userspace transaction ioctls
Nikolay Borisov [Mon, 5 Feb 2018 08:41:13 +0000 (10:41 +0200)]
btrfs: Remove userspace transaction ioctls

Commit 3558d4f88ec8 ("btrfs: Deprecate userspace transaction ioctls")
marked the beginning of the end of userspace transaction. This commit
finishes the job! There are no known users and ceph does not use the
ioctl anymore.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Acked-by: Sage Weil <sage@redhat.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: qgroup: Fix root item corruption when multiple same source snapshots are creat...
Qu Wenruo [Tue, 19 Dec 2017 07:44:54 +0000 (15:44 +0800)]
btrfs: qgroup: Fix root item corruption when multiple same source snapshots are created with quota enabled

When multiple pending snapshots referring to the same source subvolume
are executed, enabled quota will cause root item corruption, where root
items are using old bytenr (no backref in extent tree).

This can be triggered by fstests btrfs/152.

The cause is when source subvolume is still dirty, extra commit
(simplied transaction commit) of qgroup_account_snapshot() can skip
dirty roots not recorded in current transaction, making root item of
source subvolume not updated.

Fix it by forcing recording source subvolume in current transaction
before qgroup sub-transaction commit.

Reported-by: Justin Maggard <jmaggard@netgear.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Relax memory barrier in btrfs_tree_unlock
Nikolay Borisov [Wed, 14 Feb 2018 12:37:26 +0000 (14:37 +0200)]
btrfs: Relax memory barrier in btrfs_tree_unlock

When performing an unlock on an extent buffer we'd like to order the
decrement of extent_buffer::blocking_writers with waking up any
waiters. In such situations it's sufficient to use smp_mb__after_atomic
rather than the heavy smp_mb. On architectures where atomic operations
are fully ordered (such as x86 or s390) unconditionally executing
a heavyweight smp_mb instruction causes a severe hit to performance
while bringin no improvements in terms of correctness.

The better thing is to use the appropriate smp_mb__after_atomic routine
which will do the correct thing (invoke a full smp_mb or in the case
of ordered atomics insert a compiler barrier). Put another way,
an RMW atomic op + smp_load__after_atomic equals, in terms of
semantics, to a full smp_mb. This ensures that none of the problems
described in the accompanying comment of waitqueue_active occur.
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>
6 years agobtrfs: add define for oldest generation
Anand Jain [Wed, 7 Mar 2018 09:29:18 +0000 (17:29 +0800)]
btrfs: add define for oldest generation

Some functions can filter metadata by the generation. Add a define that
will annotate such arguments.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: open code trivial helper btrfs_page_exists_in_range
David Sterba [Wed, 7 Mar 2018 14:33:22 +0000 (15:33 +0100)]
btrfs: open code trivial helper btrfs_page_exists_in_range

The called function name is self explanatory.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Use filemap_range_has_page()
Matthew Wilcox [Tue, 6 Mar 2018 19:23:16 +0000 (11:23 -0800)]
btrfs: Use filemap_range_has_page()

The current implementation of btrfs_page_exists_in_range() gives the
wrong answer if the workingset code has stored a shadow entry in the
page cache.  The filemap_range_has_page() function does not have this
problem, and it's shared code, so use it instead.

eigned-off-by: Matthew Wilcox <mawilcox@microsoft.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: dev-replace: make sure target is identical to source when raid56 rebuild fails
Liu Bo [Fri, 2 Mar 2018 23:10:41 +0000 (16:10 -0700)]
Btrfs: dev-replace: make sure target is identical to source when raid56 rebuild fails

In the last step of scrub_handle_error_block, we try to combine good
copies on all possible mirrors, this works fine for raid1 and raid10,
but not for raid56 as it's doing parity rebuild.

If parity rebuild doesn't get back with correct data which matches its
checksum, in case of replace we'd rather write what is stored in the
source device than the data calculuated from parity.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: raid56: remove redundant async_missing_raid56
Liu Bo [Fri, 2 Mar 2018 23:10:39 +0000 (16:10 -0700)]
Btrfs: raid56: remove redundant async_missing_raid56

async_missing_raid56() is identical to async_read_rebuild().

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: adjust return values of btrfs_inode_by_name
Su Yue [Mon, 5 Mar 2018 09:13:37 +0000 (17:13 +0800)]
btrfs: adjust return values of btrfs_inode_by_name

Previously, btrfs_inode_by_name() returned 0 which left caller to check
objectid of location even location if the type was invalid.

Let btrfs_inode_by_name() return -EUCLEAN if a corrupted location of a
dir entry is found.  Removal of label out_err also simplifies the
function.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ drop unlikely ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: rename btrfs_close_extra_device to btrfs_free_extra_devids
Anand Jain [Tue, 27 Feb 2018 04:41:59 +0000 (12:41 +0800)]
btrfs: rename btrfs_close_extra_device to btrfs_free_extra_devids

This function btrfs_close_extra_devices() is about freeing
extra devids which once it may have belonged to this filesystem.
So rename it and add the comment. The _devid suffix is
appropriate as this function won't handle devices which are
outside of the filesytem being mounted.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove root argument from cow_file_range_inline
Nikolay Borisov [Fri, 2 Mar 2018 07:43:15 +0000 (09:43 +0200)]
btrfs: Remove root argument from cow_file_range_inline

This argument is always set to the root of the inode, which is also
passed. So let's get a reference inside the function and simplify
the arg list.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: send: fix typo in TLV_PUT
Liu Bo [Sat, 3 Mar 2018 01:05:49 +0000 (18:05 -0700)]
Btrfs: send: fix typo in TLV_PUT

According to tlv_put()'s prototype, data and attrlen needs to be
exchanged in the macro, but seems all callers are already aware of
this misorder and are therefore not affected.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove root argument from btrfs_log_dentry_safe
Nikolay Borisov [Tue, 27 Feb 2018 15:37:18 +0000 (17:37 +0200)]
btrfs: Remove root argument from btrfs_log_dentry_safe

Now that nothing uses the root arg of btrfs_log_dentry_safe it can be
safely removed. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove root arg from btrfs_log_inode_parent
Nikolay Borisov [Tue, 27 Feb 2018 15:37:17 +0000 (17:37 +0200)]
btrfs: Remove root arg from btrfs_log_inode_parent

btrfs_log_inode_parent is called from 2 places (btrfs_log_dentry_safe
and btrfs_log_new_name) both of which pass inode->root as the root
argument and the inode itself. Remove the redundant root argument and
get a reference to the root directly from the inode, also remove
redundant root != inode->root check from the same function. No
functional change.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove redundant comment from btrfs_search_forward
Nikolay Borisov [Tue, 27 Feb 2018 15:37:16 +0000 (17:37 +0200)]
btrfs: Remove redundant comment from btrfs_search_forward

This function always sets keep_locks to 1 and saves the old value of
keep_locks which is restored at the end. So there is no way it can be
called without keep_locks being set. Remove comment imposing redundant
requirement on callers.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: move btrfs_listxattr prototype to xattr.h
David Sterba [Tue, 27 Feb 2018 14:48:59 +0000 (15:48 +0100)]
btrfs: move btrfs_listxattr prototype to xattr.h

There's a proper header for xattr handlers.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: adjust return type of btrfs_getxattr
David Sterba [Tue, 27 Feb 2018 14:48:57 +0000 (15:48 +0100)]
btrfs: adjust return type of btrfs_getxattr

The xattr_handler::get prototype returns int, use it. The only ssize_t
exception is the per-inode listxattr handler.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: drop extern from function declarations
David Sterba [Tue, 27 Feb 2018 14:48:54 +0000 (15:48 +0100)]
btrfs: drop extern from function declarations

Extern for functions does not make any difference, there are only a few
so let's remove them before it's too late.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: drop underscores from exported xattr functions
David Sterba [Tue, 27 Feb 2018 14:48:52 +0000 (15:48 +0100)]
btrfs: drop underscores from exported xattr functions

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: send, do not issue unnecessary truncate operations
Filipe Manana [Tue, 6 Feb 2018 20:40:40 +0000 (20:40 +0000)]
Btrfs: send, do not issue unnecessary truncate operations

When send finishes processing an inode representing a regular file, it
always issues a truncate operation for that file, even if its size did
not change or the last write sets the file size correctly. In the most
common cases, the issued write operations set the file to correct size
(either full or incremental sends) or the file size did not change (for
incremental sends), so the only case where a truncate operation is needed
is when a file size becomes smaller in the send snapshot when compared
to the parent snapshot.

By not issuing unnecessary truncate operations we reduce the stream size
and save time in the receiver. Currently truncating a file to the same
size triggers writeback of its last page (if it's dirty) and waits for it
to complete (only if the file size is not aligned with the filesystem's
sector size). This is being fixed by another patch and is independent of
this change (that patch's title is "Btrfs: skip writeback of last page
when truncating file to same size").

The following script was used to measure time spent by a receiver without
this change applied, with this change applied, and without this change and
with the truncate fix applied (the fix to not make it start and wait for
writeback to complete).

  $ cat test_send.sh
  #!/bin/bash

  SRC_DEV=/dev/sdc
  DST_DEV=/dev/sdd
  SRC_MNT=/mnt/sdc
  DST_MNT=/mnt/sdd

  mkfs.btrfs -f $SRC_DEV >/dev/null
  mkfs.btrfs -f $DST_DEV >/dev/null
  mount $SRC_DEV $SRC_MNT
  mount $DST_DEV $DST_MNT

  echo "Creating source filesystem"
  for ((t = 0; t < 10; t++)); do
      (
          for ((i = 1; i <= 20000; i++)); do
              xfs_io -f -c "pwrite -S 0xab 0 5000" \
                  $SRC_MNT/file_$i > /dev/null
          done
      ) &
     worker_pids[$t]=$!
  done
  wait ${worker_pids[@]}

  echo "Creating and sending snapshot"
  btrfs subvolume snapshot -r $SRC_MNT $SRC_MNT/snap1 >/dev/null
  /usr/bin/time -f "send took %e seconds"    \
         btrfs send -f $SRC_MNT/send_file $SRC_MNT/snap1
  /usr/bin/time -f "receive took %e seconds" \
         btrfs receive -f $SRC_MNT/send_file $DST_MNT

  umount $SRC_MNT
  umount $DST_MNT

The results, which are averages for 5 runs for each case, were the
following:

* Without this change

average receive time was 26.49 seconds
standard deviation of 2.53 seconds

* Without this change and with the truncate fix

average receive time was 12.51 seconds
standard deviation of 0.32 seconds

* With this change and without the truncate fix

average receive time was 10.02 seconds
standard deviation of 1.11 seconds

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: skip writeback of last page when truncating file to same size
Filipe Manana [Tue, 6 Feb 2018 20:40:31 +0000 (20:40 +0000)]
Btrfs: skip writeback of last page when truncating file to same size

When we truncate a file to the same size and that size is not aligned
with the sector size, we end up triggering writeback (and wait for it to
complete) of the last page. This is unncessary as we can not have delayed
allocation beyond the inode's i_size and the goal of truncating a file
to its own size is to discard prealloc extents (allocated via the
fallocate(2) system call). Besides the unnecessary IO start and wait, it
also breaks the oppurtunity for larger contiguous extents on disk, as
before the last dirty page there might be other dirty pages.

This scenario is probably not very common in general, however it is
common for btrfs receive implementations because currently the send
stream always issues a truncate operation for each processed inode as
the last operation for that inode (this truncate operation is not
always needed and the send implementation will be addressed to avoid
them).

So improve this by not starting and waiting for writeback of the inode's
last page when we are truncating to exactly the same size.

The following script was used to quickly measure the time a receive
operation takes:

 $ cat test_send.sh
 #!/bin/bash

 SRC_DEV=/dev/sdc
 DST_DEV=/dev/sdd
 SRC_MNT=/mnt/sdc
 DST_MNT=/mnt/sdd

 mkfs.btrfs -f $SRC_DEV >/dev/null
 mkfs.btrfs -f $DST_DEV >/dev/null
 mount $SRC_DEV $SRC_MNT
 mount $DST_DEV $DST_MNT

 echo "Creating source filesystem"
 for ((t = 0; t < 10; t++)); do
     (
         for ((i = 1; i <= 20000; i++)); do
             xfs_io -f -c "pwrite -S 0xab 0 5000" \
                $SRC_MNT/file_$i > /dev/null
         done
     ) &
     worker_pids[$t]=$!
 done
 wait ${worker_pids[@]}

 echo "Creating and sending snapshot"
 btrfs subvolume snapshot -r $SRC_MNT $SRC_MNT/snap1 >/dev/null
 /usr/bin/time -f "send took %e seconds"    \
     btrfs send -f $SRC_MNT/send_file $SRC_MNT/snap1
 /usr/bin/time -f "receive took %e seconds" \
     btrfs receive -f $SRC_MNT/send_file $DST_MNT

 umount $SRC_MNT
 umount $DST_MNT

The results for 5 runs were the following:

* Without this change

average receive time was 26.49 seconds
standard deviation of 2.53 seconds

* With this change

average receive time was 12.51 seconds
standard deviation of 0.32 seconds

Reported-by: Robbie Ko <robbieko@synology.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: dev-replace: skip prealloc extents when copy nocow pages
Liu Bo [Wed, 28 Feb 2018 01:10:58 +0000 (18:10 -0700)]
Btrfs: dev-replace: skip prealloc extents when copy nocow pages

It doens't make sense to process prealloc extents as pages will be
filled with zero when reading prealloc extents.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: unify types for metadata_ratio and data_chunk_allocations
Anand Jain [Mon, 26 Feb 2018 08:46:05 +0000 (16:46 +0800)]
btrfs: unify types for metadata_ratio and data_chunk_allocations

We have btrfs_fs_info::data_chunk_allocations and
btrfs_fs_info::metadata_ratio declared as unsigned which would be
unsinged int and kernel style prefers unsigned int over bare unsigned.
So this patch changes them to u32.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove redundant memory barriers around dio_private error status
Nikolay Borisov [Wed, 14 Feb 2018 08:53:36 +0000 (10:53 +0200)]
btrfs: Remove redundant memory barriers around dio_private error status

Using any kind of memory barriers around atomic operations which have
a return value is redundant, since those operations themselves are
fully ordered. atomic_t.txt states:

    - RMW operations that have a return value are fully ordered;

    Fully ordered primitives are ordered against everything prior and
    everything subsequent. Therefore a fully ordered primitive is like
    having an smp_mb() before and an smp_mb() after the primitive.

Given this let's replace the extra memory barriers with comments.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: remove assert in btrfs_init_dev_replace_tgtdev()
Anand Jain [Tue, 20 Feb 2018 14:50:36 +0000 (22:50 +0800)]
btrfs: remove assert in btrfs_init_dev_replace_tgtdev()

In the same function we just ran btrfs_alloc_device() which means the
btrfs_device::resized_list is sure to be empty and we are protected
with the btrfs_fs_info::volume_mutex.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: add more __cold annotations
David Sterba [Mon, 19 Feb 2018 16:24:18 +0000 (17:24 +0100)]
btrfs: add more __cold annotations

The __cold functions are placed to a special section, as they're
expected to be called rarely. This could help i-cache prefetches or help
compiler to decide which branches are more/less likely to be taken
without any other annotations needed.

Though we can't add more __exit annotations, it's still possible to add
__cold (that's also added with __exit). That way the following function
categories are tagged:

- printf wrappers, error messages
- exit helpers

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: add (the only possible) __exit annotation
David Sterba [Mon, 19 Feb 2018 16:24:15 +0000 (17:24 +0100)]
btrfs: add (the only possible) __exit annotation

Recently, the __init annotations have been added. There's unfortunatelly
only one case where we can add __exit, because most of the cleanup
helpers are also called from the __init phase.

As the __exit annotated functions get discarded completely for a
built-in code, we'd miss them from the init phase.

Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: verify subvolid mount parameter
Anand Jain [Wed, 14 Feb 2018 17:11:37 +0000 (01:11 +0800)]
btrfs: verify subvolid mount parameter

We aren't verifying the parameter passed to the subvolid mount option,
so we won't report and fail the mount if a junk value is specified for
example, -o subvolid=abc.
This patch verifies the subvolid option with match_u64.

Up to now the memparse function accepts the K/M/G/ suffixes, that are
usually meant for size values and do not make sense for a subvolume it.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: fix unexpected cow in run_delalloc_nocow
Liu Bo [Thu, 1 Feb 2018 00:09:13 +0000 (17:09 -0700)]
Btrfs: fix unexpected cow in run_delalloc_nocow

Fstests generic/475 provides a way to fail metadata reads while
checking if checksum exists for the inode inside run_delalloc_nocow(),
and csum_exist_in_range() interprets error (-EIO) as inode having
checksum and makes its caller enter the cow path.

In case of free space inode, this ends up with a warning in
cow_file_range().

The same problem applies to btrfs_cross_ref_exist() since it may also
read metadata in between.

With this, run_delalloc_nocow() bails out when errors occur at the two
places.

cc: <stable@vger.kernel.org> v2.6.28+
Fixes: 17d217fe970d ("Btrfs: fix nodatasum handling in balancing code")
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove custom crc32c init code
Nikolay Borisov [Mon, 8 Jan 2018 09:45:05 +0000 (11:45 +0200)]
btrfs: Remove custom crc32c init code

The custom crc32 init code was introduced in
14a958e678cd ("Btrfs: fix btrfs boot when compiled as built-in") to
enable using btrfs as a built-in. However, later as pointed out by
60efa5eb2e88 ("Btrfs: use late_initcall instead of module_init") this
wasn't enough and finally btrfs was switched to late_initcall which
comes after the generic crc32c implementation is initiliased. The
latter commit superseeded the former. Now that we don't have to
maintain our own code let's just remove it and switch to using the
generic implementation.

Despite touching a lot of files the patch is really simple. Here is the gist of
the changes:

1. Select LIBCRC32C rather than the low-level modules.
2. s/btrfs_crc32c/crc32c/g
3. replace hash.h with linux/crc32c.h
4. Move the btrfs namehash funcs to ctree.h and change the tree accordingly.

I've tested this with btrfs being both a module and a built-in and xfstest
doesn't complain.

Does seem to fix the longstanding problem of not automatically selectiong
the crc32c module when btrfs is used. Possibly there is a workaround in
dracut.

The modinfo confirms that now all the module dependencies are there:

before:
depends:        zstd_compress,zstd_decompress,raid6_pq,xor,zlib_deflate

after:
depends:        libcrc32c,zstd_compress,zstd_decompress,raid6_pq,xor,zlib_deflate

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ add more info to changelog from mails ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agolibcrc32c: Add crc32c_impl function
Nikolay Borisov [Mon, 8 Jan 2018 09:45:04 +0000 (11:45 +0200)]
libcrc32c: Add crc32c_impl function

This function returns a string with the currently in-use implementation
of the crc32c algorithm, i.e crc32c-generic (for unoptimised, generic
implementation) or crc32c-intel for the sse optimised version. This
will be used by btrfs.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
[ use crypto_shash_driver_name as suggested by Herbert ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Refactor __get_raid_index() to btrfs_bg_flags_to_raid_index()
Qu Wenruo [Tue, 30 Jan 2018 10:20:45 +0000 (18:20 +0800)]
btrfs: Refactor __get_raid_index() to btrfs_bg_flags_to_raid_index()

Function __get_raid_index() is used to convert block group flags into
raid index, which can be used to get various info directly from
btrfs_raid_array[].

Refactor this function a little:

1) Rename to btrfs_bg_flags_to_raid_index()
   Double underline prefix is normally for internal functions, while the
   function is used by both extent-tree and volumes.

   Although the name is a little longer, but it should explain its usage
   quite well.

2) Move it to volumes.h and make it static inline
   Just several if-else branches, really no need to define it as a normal
   function.

   This also makes later code re-use between kernel and btrfs-progs
   easier.

3) Remove function get_block_group_index()
   Really no need to do such a simple thing as an exported function.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: tree-checker: Replace root parameter with fs_info
Qu Wenruo [Thu, 25 Jan 2018 06:56:18 +0000 (14:56 +0800)]
btrfs: tree-checker: Replace root parameter with fs_info

When inspecting the error message with real corruption, the "root=%llu"
always shows "1" (root tree), instead of the correct owner.

The problem is that we are getting @root from page->mapping->host, which
points the same btree inode, so we will always get the same root.

This makes the root owner output meaningless, and harder to port
tree-checker to btrfs-progs.

So get rid of the false and meaningless @root parameter and replace it
with @fs_info.
To get the owner, we can only rely on btrfs_header_owner() now.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: add tracepoint for em's EEXIST case
Liu Bo [Fri, 5 Jan 2018 19:51:16 +0000 (12:51 -0700)]
Btrfs: add tracepoint for em's EEXIST case

This is adding a tracepoint 'btrfs_handle_em_exist' to help debug the
subtle bugs around merge_extent_mapping.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Move qgroup rescan on quota enable to btrfs_quota_enable
Nikolay Borisov [Wed, 31 Jan 2018 08:52:04 +0000 (10:52 +0200)]
btrfs: Move qgroup rescan on quota enable to btrfs_quota_enable

Currently btrfs_run_qgroups is doing a bit too much. Not only is it
responsible for synchronizing in-memory state of qgroups to disk but
it also contains code to trigger the initial qgroup rescan when
quota is enabled initially. This condition is detected by checking that
BTRFS_FS_QUOTA_ENABLED is not set and BTRFS_FS_QUOTA_ENABLING is set.
Nothing really requires from the code to be structured (and scattered)
the way it is so let's streamline things. First move the quota rescan
code into btrfs_quota_enable, where its invocation is closer to the
use. This also makes the FS_QUOTA_ENABLING flag redundant so let's
remove it as well.

This has been tested with a full xfstest run with qgroups enabled on
the scratch device of every xfstest and no regressions were observed.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: use reada direction enum instead of constant value in load_free_space_tree
Gu JinXiang [Thu, 11 Jan 2018 08:12:18 +0000 (16:12 +0800)]
btrfs: use reada direction enum instead of constant value in load_free_space_tree

load_free_space_tree calls either function load_free_space_bitmaps or
load_free_space_extents. And either of those two will lead to call
btrfs_next_item.  So in function load_free_space_tree, use READA_FORWARD
to read forward ahead.

This also changes the value from READA_BACK to READA_FORWARD, since
according to the logic, it should reada_for_search forward, not
backward.

Signed-off-by: Gu JinXiang <gujx@cn.fujitsu.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
[ update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: use reada direction enum instead of constant value in populate_free_space_tree
Gu Jinxiang [Thu, 11 Jan 2018 08:12:17 +0000 (16:12 +0800)]
btrfs: use reada direction enum instead of constant value in populate_free_space_tree

populate_free_space_tree calls function btrfs_search_slot_for_read with
parameter int find_higher = 1, it means that, if no exact match is
found, then use the next higher item.  So in function
populate_free_space_tree, use READA_FORWARD to read forward ahead.

This also changes the value from READA_BACK to READA_FORWARD, since
according to the logic, it should reada_for_search forward, not
backward.

Signed-off-by: Gu JinXiang <gujx@cn.fujitsu.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
[ update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove btrfs_inode::delayed_iput_count
Nikolay Borisov [Tue, 16 Jan 2018 07:31:58 +0000 (09:31 +0200)]
btrfs: Remove btrfs_inode::delayed_iput_count

delayed_iput_count wa supposed to be used to implement, well, delayed
iput. The idea is that we keep accumulating the number of iputs we do
until eventually the inode is deleted. Turns out we never really
switched the delayed_iput_count from 0 to 1, hence all conditional
code relying on the value of that member being different than 0 was
never executed. This, as it turns out, didn't cause any problem due
to the simple fact that the generic inode's i_count member was always
used to count the number of iputs. So let's just remove the unused
member and all unused code. This patch essentially provides no
functional changes. While at it, also add proper documentation for
btrfs_add_delayed_iput

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ reformat comment ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: volumes: Cleanup stripe size calculation
Qu Wenruo [Wed, 31 Jan 2018 06:16:34 +0000 (14:16 +0800)]
btrfs: volumes: Cleanup stripe size calculation

Cleanup the following things:
1) open coded SZ_16M round up
2) use min() to replace open-coded size comparison
3) code style

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Gu Jinxiang <gujx@cn.fujitsu.com>
[ reformat comment ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Streamline btrfs_delalloc_reserve_metadata initial operations
Nikolay Borisov [Fri, 12 Jan 2018 14:21:05 +0000 (16:21 +0200)]
btrfs: Streamline btrfs_delalloc_reserve_metadata initial operations

The behavior of btrfs_delalloc_reserve_metadata depends on whether
the inode we are allocating for is the freespace inode or not. As it
stands if we are the free node we set 'flush' and 'delalloc_lock'
variable to certain values. Subsequently we check the values of those
vars and act accordingly. Instead, simplify things by having 1 if
which checks whether we are the freespace inode or not and do any
specific operation in either branches of that if. This makes the code
a bit easier to understand, as an added bonus it also shrinks the
compiled size:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-17 (-17)
Function                                     old     new   delta
btrfs_delalloc_reserve_metadata             1876    1859     -17
Total: Before=85966, After=85949, chg -0.02%

No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Edmund Nadolski <enadolski@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: insert newly opened device to the end of the list
Anand Jain [Mon, 22 Jan 2018 22:49:37 +0000 (14:49 -0800)]
btrfs: insert newly opened device to the end of the list

Add opened device to the tail of dev_alloc_list instead of head, so that
it maintains the same order as dev_list.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: keep device list sorted
Anand Jain [Mon, 22 Jan 2018 22:49:36 +0000 (14:49 -0800)]
btrfs: keep device list sorted

By maintaining the device list sorted lets us reproduce the problems
related to missing chunk in the degraded mode much more consistent. So
fix this by sorting the devices by devid within the kernel. So that we
know which device is assigned to the struct fs_info::latest_bdev when
all the devices are having and same SB generation.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
[ update changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: do not check inode's runtime flags under root->orphan_lock
Liu Bo [Thu, 25 Jan 2018 18:02:55 +0000 (11:02 -0700)]
Btrfs: do not check inode's runtime flags under root->orphan_lock

It's not necessary to hold ->orphan_lock when checking inode's runtime
flags.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Use schedule_timeout_interruptible
Nikolay Borisov [Tue, 23 Jan 2018 12:46:53 +0000 (14:46 +0200)]
btrfs: Use schedule_timeout_interruptible

Instead of manually fiddling with the state of the task
(RUNNING->INTERRUPTIBLE->RUNNING) again just use schedule_timeout_interruptible
which adjusts the task state as needed. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Josef Bacik <jbacik@fb.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Move error handling of btrfs_start_dirty_block_groups closer to call site
Nikolay Borisov [Fri, 9 Feb 2018 09:30:18 +0000 (11:30 +0200)]
btrfs: Move error handling of btrfs_start_dirty_block_groups closer to call site

Even though btrfs_start_dirty_block_groups is fairly in the beginning of
btrfs_commit_transaction outside of the critical section defined by the
transaction states it can only be run by a single comitter. In other
words it defines its own critical section thanks to the
BTRFS_TRANS_DIRTY_BG run flag and ro_block_group_mutex. However, its
error handling is outside of this critical section which is a bit
counter-intuitive. So move the error handling righ after the function is
executed and let the sole runner of dirty block groups handle the return
value. 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>
6 years agobtrfs: not a disk error if the bio_add_page fails
Anand Jain [Fri, 5 Jan 2018 02:47:07 +0000 (10:47 +0800)]
btrfs: not a disk error if the bio_add_page fails

bio_add_page() can fail for logical reasons as from the bio_add_page()
comments:

/*
 * This will only fail if either bio->bi_vcnt == bio->bi_max_vecs or
 * it's a cloned bio.
 */

Here we have just allocated the bio, so both of those failures can't
occur. So drop the check. We can also drop the error stats for write
error.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Add chunk allocation ENOSPC debug message for enospc_debug mount option
Qu Wenruo [Mon, 22 Jan 2018 05:50:54 +0000 (13:50 +0800)]
btrfs: Add chunk allocation ENOSPC debug message for enospc_debug mount option

Enospc_debug makes extent allocator print more debug messages,
however for chunk allocation, there is no debug message for enospc_debug
at all.

This patch will add message for the following parts of chunk allocator:

1) No rw device at all
   Quite rare, but at least output one message for this case.

2) Not enough space for some device
   This debug message is quite handy for unbalanced disks with stripe
   based profiles (RAID0/10/5/6).

3) Not enough free devices
   This debug message should tell us if current chunk allocator is
   working correctly under minimal device requirements.

Although in most cases, we will hit other ENOSPC before we even hit a
chunk allocator ENOSPC, but such debug info won't help.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: use ASSERT to report logical error in cow_file_range()
Anand Jain [Thu, 15 Feb 2018 10:07:59 +0000 (18:07 +0800)]
btrfs: use ASSERT to report logical error in cow_file_range()

Use ASSERT to report logical error in cow_file_range(), also move it a
bit closer to when the num_bytes is derived.

The extent start could be (u64)-1 in some cases, the assert should catch
that we do not accidentally pass it to cow_file_range.

Signed-off-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>
6 years agobtrfs: cow_file_range() num_bytes and disk_num_bytes are same
Anand Jain [Thu, 15 Feb 2018 04:29:38 +0000 (12:29 +0800)]
btrfs: cow_file_range() num_bytes and disk_num_bytes are same

This patch deletes local variable disk_num_bytes as its value
is same as num_bytes in the function cow_file_range().

Signed-off-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>
6 years agobtrfs: remove unused function btrfs_async_submit_limit()
Anand Jain [Thu, 15 Feb 2018 03:36:07 +0000 (11:36 +0800)]
btrfs: remove unused function btrfs_async_submit_limit()

Commit [1] removed the need to use btrfs_async_submit_limit(), so
delete it.

[1]
 commit 736cd52e0c720103f52ab9da47b6cc3af6b083f6
  Btrfs: remove nr_async_submits and async_submit_draining

Signed-off-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>
6 years agobtrfs: remove unused hardirq.h
Yang Shi [Fri, 17 Nov 2017 23:02:16 +0000 (07:02 +0800)]
btrfs: remove unused hardirq.h

Preempt counter APIs have been split out, currently, hardirq.h just
includes irq_enter/exit APIs which are not used by btrfs at all.

So, remove the unused hardirq.h.

Signed-off-by: Yang Shi <yang.s@alibaba-inc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Add enospc_debug printing in metadata_reserve_bytes
Nikolay Borisov [Fri, 15 Dec 2017 10:05:37 +0000 (12:05 +0200)]
btrfs: Add enospc_debug printing in metadata_reserve_bytes

Currently when enospc_debug mount option is turned on we do not print
any debug info in case metadata reservation failures happen. Fix this
by adding the necessary hook in reserve_metadata_bytes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Document consistency of transaction->io_bgs list
Nikolay Borisov [Thu, 8 Feb 2018 16:25:18 +0000 (18:25 +0200)]
btrfs: Document consistency of transaction->io_bgs list

The reason why io_bgs can be modified without holding any lock is
non-obvious. Document it and reference that documentation from the
respective call sites.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove invalid null checks from btrfs_cleanup_dirty_bgs
Nikolay Borisov [Thu, 8 Feb 2018 16:25:17 +0000 (18:25 +0200)]
btrfs: Remove invalid null checks from btrfs_cleanup_dirty_bgs

list_first_entry is essentially a wrapper over cotnainer_of. The latter
can never return null even if it's working on inconsistent list since it
will either crash or return some offset in the wrong struct.
Additionally, for the dirty_bgs list the iteration is done under
dirty_bgs_lock which ensures consistency of the list.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: log, when replace, is canceled by the user
Anand Jain [Tue, 13 Feb 2018 03:53:43 +0000 (11:53 +0800)]
btrfs: log, when replace, is canceled by the user

For debugging or administration purposes, we would want to know if and
when the user cancels the replace, to complement the existing messages
when dev-replace starts or finishes.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ update changelog, fold fix for RCU warning from Nikolay ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: fix null pointer deref when target device is missing
Anand Jain [Sat, 24 Feb 2018 11:43:56 +0000 (19:43 +0800)]
btrfs: fix null pointer deref when target device is missing

The replace target device can be missing when mounted with -o degraded,
but we wont allocate a missing btrfs_device to it. So check the device
before accessing.

BUG: unable to handle kernel NULL pointer dereference at 00000000000000b0
IP: btrfs_destroy_dev_replace_tgtdev+0x43/0xf0 [btrfs]
Call Trace:
btrfs_dev_replace_cancel+0x15f/0x180 [btrfs]
btrfs_ioctl+0x2216/0x2590 [btrfs]
do_vfs_ioctl+0x625/0x650
SyS_ioctl+0x4e/0x80
do_syscall_64+0x5d/0x160
entry_SYSCALL64_slow_path+0x25/0x25

This patch has been moved in front of patch "btrfs: log, when replace,
is canceled by the user" that could reproduce the crash if the system
reboots inside btrfs_dev_replace_start before the
btrfs_dev_replace_finishing call.

 $ mkfs /dev/sda
 $ mount /dev/sda mnt
 $ btrfs replace start /dev/sda /dev/sdb
 <insert reboot>
 $ mount po degraded /dev/sdb mnt
 <crash>

Signed-off-by: Anand Jain <anand.jain@oracle.com>
[ added reproducer description from mail ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: add a comment to mark the deprecated mount option
Anand Jain [Tue, 13 Feb 2018 09:50:47 +0000 (17:50 +0800)]
btrfs: add a comment to mark the deprecated mount option

The options alloc_start and subvolrootid are deprecated, comment them in
the tokens list. And leave them as it is. No functional changes.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: manage commit mount option as %u
Anand Jain [Tue, 13 Feb 2018 09:50:46 +0000 (17:50 +0800)]
btrfs: manage commit mount option as %u

As the commit mount option is unsigned so manage it as %u for token
verifications, instead of %d.

Signed-off-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>
6 years agobtrfs: manage check_int_print_mask mount option as %u
Anand Jain [Tue, 13 Feb 2018 09:50:45 +0000 (17:50 +0800)]
btrfs: manage check_int_print_mask mount option as %u

As check_int_print_mask mount option is unsigned so manage it as %u for
token verifications, instead of %d.

Signed-off-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>
6 years agobtrfs: manage metadata_ratio mount option as %u
Anand Jain [Tue, 13 Feb 2018 09:50:44 +0000 (17:50 +0800)]
btrfs: manage metadata_ratio mount option as %u

As metadata_ratio mount option is unsinged so manage it as %u for token
verifications, instead of %d.

Signed-off-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>
6 years agobtrfs: manage thread_pool mount option as %u
Anand Jain [Tue, 13 Feb 2018 09:50:42 +0000 (17:50 +0800)]
btrfs: manage thread_pool mount option as %u

The mount option thread_pool is always unsigned. Manage it that way all
around.

Signed-off-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>
6 years agobtrfs: extent_buffer_uptodate() make it static and inline
Anand Jain [Tue, 13 Feb 2018 04:35:44 +0000 (12:35 +0800)]
btrfs: extent_buffer_uptodate() make it static and inline

extent_buffer_uptodate() is a trivial wrapper around test_bit() and
nothing else. So make it static and inline, save on code space and call
indirection.

Before:
   text    data     bss     dec     hex filename
1131257   82898   18992 1233147  12d0fb fs/btrfs/btrfs.ko

After:
   text    data     bss     dec     hex filename
1131090   82898   18992 1232980  12d054 fs/btrfs/btrfs.ko

Signed-off-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>
6 years agobtrfs: Remove fs_info argument of btrfs_write_and_wait_transaction
Nikolay Borisov [Wed, 7 Feb 2018 15:55:50 +0000 (17:55 +0200)]
btrfs: Remove fs_info argument of btrfs_write_and_wait_transaction

We already pass btrfs_trans_handle which contains a reference to the
fs_info so use that. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove fs_info argument from btrfs_update_commit_device_bytes_used
Nikolay Borisov [Wed, 7 Feb 2018 15:55:49 +0000 (17:55 +0200)]
btrfs: Remove fs_info argument from btrfs_update_commit_device_bytes_used

We already pass the btrfs_transaction which references fs_info so no
need to pass the later as an argument. Also use the opportunity to
shorten transaction->trans. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove fs_info argument from create_pending_snapshots/create_pending_snapshot
Nikolay Borisov [Wed, 7 Feb 2018 15:55:48 +0000 (17:55 +0200)]
btrfs: Remove fs_info argument from create_pending_snapshots/create_pending_snapshot

We already pass the trans handle which has a reference to fs_info to
create_pending_snapshot so we can refer to it directly. Doing this
obviates the need to pass the fs_info to create_pending_snapshots as
well. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove fs_info argument from switch_commit_roots
Nikolay Borisov [Wed, 7 Feb 2018 15:55:47 +0000 (17:55 +0200)]
btrfs: Remove fs_info argument from switch_commit_roots

We already have the fs_info from the passed transaction so use it
directly. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove root argument of cleanup_transaction
Nikolay Borisov [Wed, 7 Feb 2018 15:55:46 +0000 (17:55 +0200)]
btrfs: Remove root argument of cleanup_transaction

The only thing the passed root is used for is:
1. get a reference to the fs_info and to
2. call trace_btrfs_transaction_commit.

We can achieve 1) by simply referring to the fs_info from passed trans
object. As far as 2) is concerned cleanup_transaction is called from
only one place and the 'root' argument passed is the one from the trans
handle. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Don't pass fs_info to commit_cowonly_roots
Nikolay Borisov [Wed, 7 Feb 2018 15:55:45 +0000 (17:55 +0200)]
btrfs: Don't pass fs_info to commit_cowonly_roots

We already pass a transaction handle which refrences the fs_info so
we can grab it from there. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Don't pass fs_info to commit_fs_roots
Nikolay Borisov [Wed, 7 Feb 2018 15:55:44 +0000 (17:55 +0200)]
btrfs: Don't pass fs_info to commit_fs_roots

We already pass the transaction handle which has a reference to the
fs_info. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Don't pass fs_info to btrfs_run_delayed_items/_nr
Nikolay Borisov [Wed, 7 Feb 2018 15:55:43 +0000 (17:55 +0200)]
btrfs: Don't pass fs_info to btrfs_run_delayed_items/_nr

We already pass the transaction which has a reference to the fs_info,
so use that. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Don't pass fs_info to __btrfs_run_delayed_items
Nikolay Borisov [Wed, 7 Feb 2018 15:55:42 +0000 (17:55 +0200)]
btrfs: Don't pass fs_info to __btrfs_run_delayed_items

We already pass the transaction handle, which contains a refrence to
the fs_info so grab it from there. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Don't pass fs_info arg to btrfs_start_dirty_block_groups
Nikolay Borisov [Wed, 7 Feb 2018 15:55:41 +0000 (17:55 +0200)]
btrfs: Don't pass fs_info arg to btrfs_start_dirty_block_groups

It can be referenced from the passed transaction so no point in passing
it as a function argument. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove fs_info argument from btrfs_create_pending_block_groups
Nikolay Borisov [Wed, 7 Feb 2018 15:55:40 +0000 (17:55 +0200)]
btrfs: Remove fs_info argument from btrfs_create_pending_block_groups

It can be referenced from the passed transaciton so no point in
passing it as function argument. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove fs_info argument from btrfs_trans_release_metadata
Nikolay Borisov [Wed, 7 Feb 2018 15:55:39 +0000 (17:55 +0200)]
btrfs: Remove fs_info argument from btrfs_trans_release_metadata

All current callers of this function just get a reference to the
trans->fs_info member and pass it as the second argument. Collapse this
into the function itself. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Open code btrfs_write_and_wait_marked_extents
Nikolay Borisov [Wed, 7 Feb 2018 15:55:38 +0000 (17:55 +0200)]
btrfs: Open code btrfs_write_and_wait_marked_extents

btrfs_write_and_wait_transaction is essentially a wrapper of
btrfs_write_and_wait_marked_extents with the addition of calling
clear_btree_io_tree. Having the code split doesn't really bring any
benefit. Open code the later into the former and add proper
documentation header.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ reformat comment ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Make btrfs_trans_release_metadata private to transaction.c
Nikolay Borisov [Wed, 7 Feb 2018 15:55:37 +0000 (17:55 +0200)]
btrfs: Make btrfs_trans_release_metadata private to transaction.c

This function is only ever used in __btrfs_end_transaction and
btrfs_commit_transaction so there is no need to export it via header.
Let's move it closer to where it's used, make it static and remove it
from the header. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: open code btrfs_init_dev_replace_tgtdev_for_resume()
Anand Jain [Mon, 12 Feb 2018 15:36:25 +0000 (23:36 +0800)]
btrfs: open code btrfs_init_dev_replace_tgtdev_for_resume()

btrfs_init_dev_replace_tgtdev_for_resume() initializes replace
target device in a few simple steps, so do it at the parent function.
Moreover, there isn't any other caller so just open code it.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: btrfs_dev_replace_cancel() can return int
Anand Jain [Mon, 12 Feb 2018 15:33:31 +0000 (23:33 +0800)]
btrfs: btrfs_dev_replace_cancel() can return int

Current u64 return from btrfs_dev_replace_cancel() was probably done
to match the btrfs_ioctl_dev_replace_args::result. However as our
actual return value fits in int, and it further gets typecast to u64,
so just return int.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: rename __btrfs_dev_replace_cancel()
Anand Jain [Mon, 12 Feb 2018 15:33:30 +0000 (23:33 +0800)]
btrfs: rename __btrfs_dev_replace_cancel()

Remove __ which is for the special functions.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: open code btrfs_dev_replace_cancel()
Anand Jain [Mon, 12 Feb 2018 15:33:29 +0000 (23:33 +0800)]
btrfs: open code btrfs_dev_replace_cancel()

btrfs_dev_replace_cancel() calls __btrfs_dev_replace_cancel() for the
actual cancel so just open code it.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Don't hardcode the csum size in btrfs_ordered_sum_size
Nikolay Borisov [Wed, 7 Feb 2018 09:19:10 +0000 (11:19 +0200)]
btrfs: Don't hardcode the csum size in btrfs_ordered_sum_size

Currently the function uses a hardcoded value for the checksum size of
a sector. This is fine, given that we currently support only a single
algorithm, whose checksum is 4 bytes == sizeof(u32). Despite not
having other algorithms, btrfs' design supports using a different
algorithm whith different space requirements. To future-proof the code
query the size of the currently used algorithm from the in-memory copy
of the super block. No functional changes.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Su Yue <suy.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agoBtrfs: extent map selftest: add missing void parameter to btrfs_test_extent_map
Colin Ian King [Mon, 8 Jan 2018 23:06:32 +0000 (23:06 +0000)]
Btrfs: extent map selftest: add missing void parameter to btrfs_test_extent_map

Add a missing void parameter to function btrfs_test_extent_map, fixes
sparse warning:

warning: non-ANSI function declaration of function 'btrfs_test_extent_map'

Signed-off-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: remove redundant check on ret and goto
Colin Ian King [Fri, 12 Jan 2018 16:52:58 +0000 (16:52 +0000)]
btrfs: remove redundant check on ret and goto

The check for a non-zero ret is redundant as the goto will jump to
the very next statement anyway.  Remove this extraneous code.

Detected by CoverityScan, CID#1463784 ("Identical code for different
branches")

Signed-off-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Remove unused btrfs_start_transaction_lflush function
Nikolay Borisov [Fri, 15 Dec 2017 10:06:18 +0000 (12:06 +0200)]
btrfs: Remove unused btrfs_start_transaction_lflush function

Commit 0e8c36a9fd81 ("Btrfs: fix lots of orphan inodes when the space
is not enough") changed the way transaction reservation is made in
btrfs_evict_node and as a result this function became unused. This has
been the status quo for 5 years in which time no one noticed, so I'd
say it's safe to assume it's unlikely it will ever be used again.

Historical note: there were more attempts to remove the function, the
reasoning was missing and only based on some static analysis tool
reports. Other reason for rejection was that there seemed to be
connection to BTRFS_RESERVE_FLUSH_LIMIT and that would need to be
removeed to. This was not correct so removing the function is all we can
do.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
[ add the note ]
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: print error if primary super block write fails
Howard McLauchlan [Fri, 2 Feb 2018 19:09:01 +0000 (11:09 -0800)]
btrfs: print error if primary super block write fails

Presently, failing a primary super block write but succeeding in at
least one super block write in general will appear to users as if
nothing important went wrong. However, upon unmounting and re-mounting,
the file system will be in a rolled back state. This was discovered
with a BCC program that uses bpf_override_return() to fail super block
writes.

This patch outputs an error clarifying that the primary super block
write has failed, so users can expect potentially erroneous behaviour.
It also forces wait_dev_supers() to return an error to its caller if
the primary super block write fails.

Signed-off-by: Howard McLauchlan <hmclauchlan@fb.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
6 years agobtrfs: Refactor parameter of BTRFS_MAX_DEVS() from root to fs_info
Qu Wenruo [Tue, 30 Jan 2018 10:20:46 +0000 (18:20 +0800)]
btrfs: Refactor parameter of BTRFS_MAX_DEVS() from root to fs_info

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>