btrfs-progs: only report internal dev replace result if there's a result

If BTRFS_IOC_DEV_REPLACE ioctl failed, args.result usually won't be
updated by the ioctl.

And the arg has been initialized with 0, the result is always 0, which
is BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR, and the resulting error
message looks confusing:

ERROR: ioctl(DEV_REPLACE_START) failed on "/mnt/btrfs": No such file or directory, no error

But in case there's an internal result returned in future, don't drop
the result completely, instead print dev replace result message only
if the result is updated by a failed ioctl call.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: code optimize cmd_scan_dev() use btrfs_register_one_device()

cmd_scan_dev() has it own code to register device (calling ioctl
BTRFS_IOC_SCAN_DEV), apparently it could use btrfs_register_one_device().

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: check read extent errors when mapping

coverity barked out a warning that btrfs-map-logical was storing but
ignoring errors from read_extent_from_disk().   So don't ignore 'em.  I
made extent reading errors fatal to match the fatal errors from mapping
mirrors above.

And while we're at it have read_extent_from_disk() return -errno pread
errors instead of -EIO or -1 (-EPERM).  The only other caller who tests
errors clobbers them with -EIO.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: fix csum root copy-n-paste error

btrfs_setup_all_roots() had some copy and pasted code for trying to
setup a root and then creating a blank node if that failed.  The copy
for the csum_root created the blank node in the extent_root.

So we create a function to use a consistent root.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: Fix number of arguments check of 'btrfs fi df'

'btrfs fi df' needs exactly one arguments as mount option,
but as 3.17 we can run 'btrfs fi df' without any argument,
and it will error as "ERROR: can't access '%s'" which means
the argument number does not do what it should.

The bug is caused by manually modify the optind and use check_argc_max()
instead of the original check_argc_exact().

This patch fixes it by not modifying the optind and use check_argc_exact()
again.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
revert btrfs-progs: do a separate probe for _transient_ replacing device

There is a compatibility issue with older kernel with the progs commit id as below.

d0588bfa479409b2a0f6243f894338a01a56221a
btrfs-progs: do a separate probe for _transient_ replacing device

So as of now writing to revert the above commit id.
The brewing sysfs interface would help to fix the impending issue, which is
seed device would fail show in 'btrfs fi show' output of a sprout device.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: skip mounted fs when deal with umounted ones for fi show

Stalling problems may happen when exec balance & fi show cmds concurrently.

With the following commit:
        commit 915902c500
        btrfs-progs: fix device missing of btrfs fi show with seed devices

The fi show cmd will bother the mounted fs when only umounted fs should
be handled after @btrfs_can_kernel() has finished showing all mounted ones.

We could skip the mounted fs after @btrfs_can_kernel() is done, then tasks
keeps going on mounted fs while fi show continues on umounted ones separately.

Reported-by: Petr Janecek <janecek@ucw.cz>
Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: Fix the argument requirement for '--subvol-extents'

The following commit changed the argument requirement for
'--subvol-extents', which causes it to call arg_strtou64() on NULL,
resulting a segfault.
d34cbe76 btrfs-progs: check: do not require argument for --subvol-extents

This patch revert the patch and change the help string and man page to
make it no longer confusing.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: fix linking with libbtrfs

Reported at https://github.com/openSUSE/snapper/issues/128

Commit cdb9e22e292275237c added another rbtree file that defines
functions that libbtrfs uses.

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