platform/upstream/btrfs-progs.git
9 years agoBtrfs-progs: add ability to replace missing dir item/dir indexes
Josef Bacik [Fri, 31 Oct 2014 18:01:26 +0000 (14:01 -0400)]
Btrfs-progs: add ability to replace missing dir item/dir indexes

If we have everything except the dir item and dir index we can easily replace
them, so add this ability to btrfsck.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: add a message to know zero log ran successfully
Josef Bacik [Fri, 31 Oct 2014 18:01:25 +0000 (14:01 -0400)]
Btrfs-progs: add a message to know zero log ran successfully

If there are errors when opening the fs because of PARTIAL we could think that
the zero-log didn't actually work.  Add a printf so we know that it was
successfull.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: make zero-log use partial open
Josef Bacik [Fri, 31 Oct 2014 18:01:24 +0000 (14:01 -0400)]
Btrfs-progs: make zero-log use partial open

Because seriously, we only want to kill the tree log.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: spit out the broken file when ignoring errors
Josef Bacik [Fri, 31 Oct 2014 18:01:23 +0000 (14:01 -0400)]
Btrfs-progs: spit out the broken file when ignoring errors

It's nice to ignore errors on restore, but spit out the filename so the user
knows which files of his aren't going to look right.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: don't fail on log tree opening with PARTIAL
Josef Bacik [Fri, 31 Oct 2014 18:01:22 +0000 (14:01 -0400)]
Btrfs-progs: don't fail on log tree opening with PARTIAL

We were failing to fsck a volume because we couldn't open the log tree, which is
not helpful.  Make us skip erroring out if we are using OPEN_CTREE_PARTIAL since
it isn't a mandatory tree.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: add the ability to delete items
Josef Bacik [Fri, 31 Oct 2014 18:01:19 +0000 (14:01 -0400)]
Btrfs-progs: add the ability to delete items

Somtetimes you just need to delete an item, add that functionality to
btrfs-corrupt-block.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix compiler warning
Christian Hesse [Thu, 5 Jun 2014 08:13:39 +0000 (10:13 +0200)]
btrfs-progs: fix compiler warning

gcc 4.9.0 gives warnings about possibly uninitialized values when
compiling with function inlining and optimization level two enabled
(CFLAGS="-finline-functions -O2").

Initializing the values fixes the warning. Hope this is correct.

Signed-off-by: Christian Hesse <mail@eworm.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: update .gitignore
David Sterba [Fri, 7 Nov 2014 16:59:56 +0000 (17:59 +0100)]
btrfs-progs: update .gitignore

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: build, update the clean rule
David Sterba [Thu, 30 Oct 2014 18:28:02 +0000 (19:28 +0100)]
btrfs-progs: build, update the clean rule

Remove duplicate files, add extra progs, add library test binaries.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: build, gather utilities that are not built by default
David Sterba [Thu, 30 Oct 2014 18:22:32 +0000 (19:22 +0100)]
btrfs-progs: build, gather utilities that are not built by default

Add them to build test and to clean rules.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: build, add make target for a build test
David Sterba [Thu, 30 Oct 2014 18:06:31 +0000 (19:06 +0100)]
btrfs-progs: build, add make target for a build test

A quick check that everything still builds.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: build, add basic build test for library
David Sterba [Thu, 30 Oct 2014 17:59:47 +0000 (18:59 +0100)]
btrfs-progs: build, add basic build test for library

Basic test based on snapper code that uses the send stream API.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs progs v3.17.1
David Sterba [Tue, 4 Nov 2014 17:30:34 +0000 (18:30 +0100)]
Btrfs progs v3.17.1

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: use the correct SI prefixes
David Sterba [Tue, 4 Nov 2014 17:22:51 +0000 (18:22 +0100)]
btrfs-progs: use the correct SI prefixes

The SI standard defines lowercase 'k' and uppercase for the rest.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: libbtrfs, make exported headers compatible with C++
David Sterba [Mon, 3 Nov 2014 22:50:50 +0000 (23:50 +0100)]
btrfs-progs: libbtrfs, make exported headers compatible with C++

Add externs and don't use a reserved keyword.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: optimize btrfs_scan_lblkid() for multiple calls
Anand Jain [Fri, 31 Oct 2014 04:11:20 +0000 (12:11 +0800)]
btrfs-progs: optimize btrfs_scan_lblkid() for multiple calls

btrfs_scan_lblikd() is called by most the device related command functions.
And btrfs_scan_lblkid() is most expensive function and it becomes more expensive
as number of devices in the system increase. Further some threads call this
function more than once for absolutely no extra benefit and the real waste of
resources. Below list of threads and number of times btrfs_scan_lblkid()
is called in that thread.

  btrfs-find-root            1
  btrfs rescue super-recover 2
  btrfs-debug-tree           1
  btrfs-image -r             2
  btrfs check                2
  btrfs restore              2
  calc-size                  NC
  btrfs-corrupt-block        NC
  btrfs-image                NC
  btrfs-map-logical          1
  btrfs-select-super         NC
  btrfstune                  2
  btrfs-zero-log             NC
  tester                     NC
  quick-test.c               NC
  btrfs-convert              0
  mkfs                       #number of devices to be mkfs
  btrfs label set unmounted  2
  btrfs get label unmounted  2

This patch will:
  move out calling register_one_device with in btrfs_scan_lblkid()
  and so function setting the BTRFS_UPDATE_KERNEL to yes will
  call btrfs_register_all_devices() separately.

  introduce a global variable scan_done, which is set when scan is
  done succssfully per thread. So that following calls to this function
  will just return success.

  Further if any function needs to force scan after scan_done is set,
  then it can be done when there is such a requirement, but as of now there
  isn't any such requirement.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: introduce btrfs_register_all_device()
Anand Jain [Fri, 31 Oct 2014 04:11:19 +0000 (12:11 +0800)]
btrfs-progs: introduce btrfs_register_all_device()

This function is to register all devices found after scanning
the system. Before we had this functionality with in the
btrfs_scan_lblkid(), however scanning and registering are two
different distinct operation its better keep them separate.
Also we want to optimize btrfs_scan_lblkid and avoid multiple
system scans unless needed. As of now device scan uses this function.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix linking with libbtrfs
David Sterba [Thu, 30 Oct 2014 17:33:41 +0000 (18:33 +0100)]
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>
10 years agobtrfs-progs: Fix the argument requirement for '--subvol-extents'
Qu Wenruo [Thu, 30 Oct 2014 06:34:06 +0000 (14:34 +0800)]
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>
10 years agobtrfs-progs: skip mounted fs when deal with umounted ones for fi show
Gui Hecheng [Thu, 30 Oct 2014 02:44:50 +0000 (10:44 +0800)]
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>
10 years agorevert btrfs-progs: do a separate probe for _transient_ replacing device
Anand Jain [Thu, 30 Oct 2014 04:43:03 +0000 (12:43 +0800)]
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>
10 years agobtrfs-progs: Fix number of arguments check of 'btrfs fi df'
Qu Wenruo [Tue, 21 Oct 2014 08:31:17 +0000 (16:31 +0800)]
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>
10 years agobtrfs-progs: fix csum root copy-n-paste error
Zach Brown [Wed, 15 Oct 2014 23:14:21 +0000 (16:14 -0700)]
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>
10 years agobtrfs-progs: check read extent errors when mapping
Zach Brown [Wed, 15 Oct 2014 23:14:19 +0000 (16:14 -0700)]
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>
10 years agobtrfs-progs: code optimize cmd_scan_dev() use btrfs_register_one_device()
Anand Jain [Wed, 15 Oct 2014 00:50:38 +0000 (08:50 +0800)]
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>
10 years agobtrfs-progs: only report internal dev replace result if there's a result
Eryu Guan [Fri, 10 Oct 2014 13:16:16 +0000 (21:16 +0800)]
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>
10 years agoBtrfs progs v3.17
David Sterba [Fri, 17 Oct 2014 16:43:33 +0000 (18:43 +0200)]
Btrfs progs v3.17

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: run fsck image tests in filename order
David Sterba [Fri, 17 Oct 2014 16:26:08 +0000 (18:26 +0200)]
btrfs-progs: run fsck image tests in filename order

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: check, ability to detect and fix outdated snapshot root items
Filipe Manana [Fri, 17 Oct 2014 17:20:08 +0000 (18:20 +0100)]
Btrfs-progs: check, ability to detect and fix outdated snapshot root items

This change adds code to detect and fix the issue introduced in the kernel
release 3.17, where creation of read-only snapshots lead to a corrupted
filesystem if they were created at a moment when the source subvolume/snapshot
had orphan items. The issue was that the on-disk root items became incorrect,
referring to the pre orphan cleanup root node instead of the post orphan
cleanup root node.

A test filesystem can be generated with the test case recently submitted for
xfstests/fstests, which is essencially the following (bash script):

    workout()
    {
ops=$1
procs=$2
num_snapshots=$3

_scratch_mkfs >> $seqres.full 2>&1
_scratch_mount

snapshot_cmd="$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT"
snapshot_cmd="$snapshot_cmd $SCRATCH_MNT/snap_\`date +'%H_%M_%S_%N'\`"
run_check $FSSTRESS_PROG -p $procs \
    -x "$snapshot_cmd" -X $num_snapshots -d $SCRATCH_MNT -n $ops
    }

    ops=10000
    procs=4
    snapshots=500
    workout $ops $procs $snapshots

Example of btrfsck's (btrfs check) behaviour against such filesystem:

  $ btrfsck /dev/loop0
  root item for root 311, current bytenr 44630016, current gen 60, current level 1, new bytenr 44957696, new gen 61, new level 1
  root item for root 1480, current bytenr 1003569152, current gen 1271, current level 1, new bytenr 1004175360, new gen 1272, new level 1
  root item for root 1509, current bytenr 1037434880, current gen 1300, current level 1, new bytenr 1038467072, new gen 1301, new level 1
  root item for root 1562, current bytenr 33636352, current gen 1354, current level 1, new bytenr 34455552, new gen 1355, new level 1
  root item for root 3094, current bytenr 1011712000, current gen 2935, current level 1, new bytenr 1008484352, new gen 2936, new level 1
  root item for root 3716, current bytenr 80805888, current gen 3578, current level 1, new bytenr 73515008, new gen 3579, new level 1
  root item for root 4085, current bytenr 714031104, current gen 3958, current level 1, new bytenr 716816384, new gen 3959, new level 1
  Found 7 roots with an outdated root item.
  Please run a filesystem check with the option --repair to fix them.

  $ echo $?
  1

  $ btrfsck --repair /dev/loop0
  enabling repair mode
  fixing root item for root 311, current bytenr 44630016, current gen 60, current level 1, new bytenr 44957696, new gen 61, new level 1
  fixing root item for root 1480, current bytenr 1003569152, current gen 1271, current level 1, new bytenr 1004175360, new gen 1272, new level 1
  fixing root item for root 1509, current bytenr 1037434880, current gen 1300, current level 1, new bytenr 1038467072, new gen 1301, new level 1
  fixing root item for root 1562, current bytenr 33636352, current gen 1354, current level 1, new bytenr 34455552, new gen 1355, new level 1
  fixing root item for root 3094, current bytenr 1011712000, current gen 2935, current level 1, new bytenr 1008484352, new gen 2936, new level 1
  fixing root item for root 3716, current bytenr 80805888, current gen 3578, current level 1, new bytenr 73515008, new gen 3579, new level 1
  fixing root item for root 4085, current bytenr 714031104, current gen 3958, current level 1, new bytenr 716816384, new gen 3959, new level 1
  Fixed 7 roots.
  Checking filesystem on /dev/loop0
  UUID: 2186e9b9-c977-4a35-9c7b-69c6609d4620
  checking extents
  checking free space cache
  cache and super generation don't match, space cache will be invalidated
  checking fs roots
  checking csums
  checking root refs
  found 618537000 bytes used err is 0
  total csum bytes: 130824
  total tree bytes: 601620480
  total fs tree bytes: 580288512
  total extent tree bytes: 18464768
  btree space waste bytes: 136939144
  file data blocks allocated: 34150318080
   referenced 27815415808
  Btrfs v3.17-rc3-2-gbbe1dd8

  $ echo $?
  0

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix show super unknown flag output
Zach Brown [Wed, 15 Oct 2014 23:14:20 +0000 (16:14 -0700)]
btrfs-progs: fix show super unknown flag output

coverity pointed out that unknown flag printing in show super had some
dead code.  It turns out that first was reset when the first flag was
tested, not when it was output.  We only want to clear it if the first
matching bit is output.  If there are no matching bits then we'll want
to output the unknown flag first.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: check sscanf return code
Zach Brown [Wed, 15 Oct 2014 23:14:18 +0000 (16:14 -0700)]
btrfs-progs: check sscanf return code

coverity warned that the return code from sscanf() assigned to 'i'
wasn't checked before being assigned again.  Check it.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: open RW to register device using btrfs-control
Anand Jain [Wed, 15 Oct 2014 00:46:54 +0000 (08:46 +0800)]
btrfs-progs: open RW to register device using btrfs-control

We are passing device path to be registered with in kernel,
so we need to open with RW

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: introduce a proper structure on which cli will call register-device...
Anand Jain [Wed, 15 Oct 2014 00:46:14 +0000 (08:46 +0800)]
btrfs-progs: introduce a proper structure on which cli will call register-device ioctl

As of now commands mentioned below (with in [..]) are calling call register-device
ioctl BTRFS_IOC_SCAN_DEV for all the devices in the system.
Some issues with it:
 BTRFS_IOC_SCAN_DEV: ioctl is a write operation, we don't want command like
 btrfs-debug-tree threads to do that..
   eg:
   ----
   $ cat /proc/fs/btrfs/devlist  | egrep fsid | wc -l
   0
   $ btrfs-debug-tree /dev/sde  (num_device > 1)
   $ cat /proc/fs/btrfs/devlist  | egrep fsid | wc -l
   5
   ----

 btrfs_scan_fs_devices() ends up calling this ioctl only when num_device > 1.
 That's inconsistency with in feature/bug.

 We don't have to register _all_ the btrfs devices (again) in the system
 without user consent.

Why its inconsistent:
 function btrfs_scan_fs_devices() calls btrfs_scan_lblkid only when
 num_devices is > 1, which in turn calls BTRFS_IOC_SCAN_DEV ioctl, if
 conditions are met.

 But main issue is we have too many consumers of btrfs_scan_fs_devices()
 the names below with in [] is the cli leading to this function.

 open_ctree_broken()  [btrfs-find-root]
 recover_prepare()    [btrfs rescue super-recover]
 __open_ctree_fd
 (updates always except when flag OPEN_CTREE_RECOVER_SUPER is set and
 flag OPEN_CTREE_RECOVER_SUPER is set only by 'btrfs rescue super-
 recover' but still this thread sneaks through the open_ctree function
 to call register-device-ioctl as show below).
open_ctree_fs_info
[btrfs-debug-tree]
[btrfs-image -r]
[btrfs check]
open_fs
[btrfs restore]
open_ctree
[calc-size]
[btrfs-corrupt-block]
[btrfs-image] (create)
[btrfs-map-logical]
[btrfs-select-super]
[btrfstune]
[btrfs-zero-log]
[tester]
[mkfs]
[quick-test.c]
[btrfs label set unmounted]
[btrfs get label unmounted]
[btrfs rescue super-recover]

open_ctree_fd
[btrfs-convert]

Fix:
 In an effort to make register-device consistent, all calls to
 btrfs_scan_fs_devices() will have 5th parameter set to 0. that means
 we don't need 5th parameter at all. And with this function not calling
 the register ioctl at all, finally we will have following two cli to call
 the ioctl BTRFS_IOC_SCAN_DEV.
    btrfs dev scan and
    mkfs.btrfs
 Threads needing to update kernel about a device would have to use
 btrfs_register_one_device() separately.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: mkfs should be consistent in calling register device
Anand Jain [Wed, 15 Oct 2014 00:45:11 +0000 (08:45 +0800)]
btrfs-progs: mkfs should be consistent in calling register device

When we have one device we don't call register device.
(in fact not mandatory, but to make it consistent)
And when we have more than one we call register device.

reproducer:
Nothing in the kernel device list
cat /proc/fs/btrfs/devlist | egrep fsid | wc -l
0

mkfs.btrfs will automatically call register device when devices
is more than 1.

mkfs.btrfs -f /dev/sdb /dev/sdc
cat /proc/fs/btrfs/devlist | egrep fsid | wc -l
1

But it does not when there is only one device

mkfs.btrfs -f /dev/sdb
cat /proc/fs/btrfs/devlist | egrep fsid | wc -l
0

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: check, fix return value check of is_child_root()
Filipe Manana [Thu, 16 Oct 2014 00:33:23 +0000 (01:33 +0100)]
Btrfs-progs: check, fix return value check of is_child_root()

The following commit:

    "btrfs-progs: fsck: remove unfriendly BUG_ON() for searching tree failure"
    f495a2ac66116f0a1b15e73380c8cbca6e0a4ca0

introduced a regression, detected through xfstests/btrfs/054, where
previously a negative return value (-1) was used to mean a particular
root didn't had any parent root, and now, after that change, a negative
value is also used to mean that an error happened. That change also made
the only caller of is_child_root() interpret any negative return value
as an error and therefore incorrectly made the caller leave with an
error, instead of continuing.

This affects only the 3.17 release candidates (3.16 and older releases
don't have this issue).

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Wang Shilong <wangshilong1991@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix uninitialized warning in btrfs_calc_stripe_index
Anand Jain [Mon, 6 Oct 2014 22:27:16 +0000 (06:27 +0800)]
btrfs-progs: fix uninitialized warning in btrfs_calc_stripe_index

chunk-recover.c: In function btrfs_calc_stripe_index
chunk-recover.c:1481: warning: index may be used uninitialized in this function

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: lookup all roots that point to a corrupt block
Josef Bacik [Fri, 10 Oct 2014 20:57:10 +0000 (16:57 -0400)]
Btrfs-progs: lookup all roots that point to a corrupt block

If we have a corrupt block that multiple snapshots point to we will only fix the
guy who originally pointed to the block, and then simply loop forever because we
keep finding the same bad block.  So instead lookup all roots that point to this
block, and then search down to the block for each root and fix the block in all
snapshots.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: make fsck deal with bogus items
Josef Bacik [Fri, 3 Oct 2014 14:54:26 +0000 (10:54 -0400)]
btrfs-progs: make fsck deal with bogus items

We can deal with corrupt items by deleting them in a few cases.  Fsck can easily
recover from a missing extent item or a dir index item.  So if we notice a item
is completely bogus and it is of a key that we know we can repair then just
delete it and carry on.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: check blocks when checking fs roots
Josef Bacik [Fri, 3 Oct 2014 14:50:15 +0000 (10:50 -0400)]
btrfs-progs: check blocks when checking fs roots

Usually if we find a bad block during the extent tree stuff we will error out,
but if the bad block is in an fs tree and doens't have extents in it then fsck
may still pass even though the block was complete garbage.  So add the check
block logic to the fs root checking so we actually error out of fsck if there is
a bad block.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add the ability to fix shifted item offsets
Josef Bacik [Fri, 3 Oct 2014 14:45:29 +0000 (10:45 -0400)]
btrfs-progs: add the ability to fix shifted item offsets

A user had a corrupted fs where the items had been shifted improperly.  This
patch adds the ability to fix this sort of problem within fsck.  We will simply
shift the item over to the proper offset and update the offsets to make sure
they are correct.  I tested this with a hand crafted fs that was broken in the
same way as the user, and I've included the file as a new test.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add shift_items to btrfs-corrupt-block
Josef Bacik [Fri, 3 Oct 2014 14:38:41 +0000 (10:38 -0400)]
btrfs-progs: add shift_items to btrfs-corrupt-block

A user had a corrupted fs where his items where shifted oddly.  This adds the
functionality I needed to btrfs-corrupt-block in order to reproduce this
corruption in order to make fsck fix this sort of problem.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: corrupt btrfs items in btrfs-corrup-block
Josef Bacik [Thu, 2 Oct 2014 19:12:20 +0000 (15:12 -0400)]
btrfs-progs: corrupt btrfs items in btrfs-corrup-block

For testing fsck against completely broken btrfs_items.

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: add ability to corrupt dir items
Josef Bacik [Fri, 10 Oct 2014 20:57:16 +0000 (16:57 -0400)]
Btrfs-progs: add ability to corrupt dir items

In order to test the dir index corruption fixing patches in fsck we need to add
functionality to btrfs-corrupt-block to corrupt dir item fields.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: deal with mismatch index between dir index and inode ref
Josef Bacik [Fri, 10 Oct 2014 20:57:15 +0000 (16:57 -0400)]
Btrfs-progs: deal with mismatch index between dir index and inode ref

Sometimes we have a dir index and an inode ref that don't agree on the index.
In this case just assume that the inode ref is the ultimate authority on the
subject and delete the dir index.  This means we have to not reset index if we
find a mismatched inode ref to make sure we delete the right dir index.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: add a dummy backref if our location is wrong
Josef Bacik [Fri, 10 Oct 2014 20:57:14 +0000 (16:57 -0400)]
Btrfs-progs: add a dummy backref if our location is wrong

If our location is bogus in our dir item we were just skipping the thing.
However in this case we want to just delete the dir index, so create a dummy
inode rec using BTRFS_MULTIPLE_OBJECTIDS and just add every backref we find to
the list so we know to straight up delete all of these items.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: delete bogus dir indexes
Josef Bacik [Fri, 10 Oct 2014 20:57:13 +0000 (16:57 -0400)]
Btrfs-progs: delete bogus dir indexes

We may run across dir indexes that are corrupt in such a way that it makes them
useless, such as having a bad location key or a bad name.  In this case we can
just delete dir indexes that don't show up properly and then re-create what we
need.  When we delete dir indexes however we need to restart scanning the fs
tree as we could have greated bogus inode recs if the location key was bad, so
set it up so that if we had to delete an dir index we go ahead and free up our
inode recs and return -EAGAIN to check_fs_roots so it knows to restart the loop.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: re-search tree root if it changes
Josef Bacik [Fri, 10 Oct 2014 20:57:12 +0000 (16:57 -0400)]
Btrfs-progs: re-search tree root if it changes

If we change something while scanning fs-roots we need to redo our search so
that we get valid root items and have valid root cache.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: reset chunk state if we restart check
Josef Bacik [Fri, 10 Oct 2014 20:57:11 +0000 (16:57 -0400)]
Btrfs-progs: reset chunk state if we restart check

If we hid a corrupt block that we fix and we restart the fsck loop you will get
lots of noise about duplicate block groups and such.  This is because we don't
clear the block group and chunk cache when we do this restart.  This patch fixes
that, which is a little tricky since the structs are linked together with
various linked lists, but this passed with a user who was hitting this problem.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: update rbtree libs
Josef Bacik [Fri, 10 Oct 2014 20:57:09 +0000 (16:57 -0400)]
Btrfs-progs: update rbtree libs

While debugging a broken fs we were seeing hangs in the rb_erase loops.  The
rbtree was simple and wasn't corrupted so it appeared to be a bug in our rbtree
library.  Updating to the kernels latest rbtree code made the infinite loop go
away, so pull it back.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: break out rbtree util functions
Josef Bacik [Fri, 10 Oct 2014 20:57:08 +0000 (16:57 -0400)]
Btrfs-progs: break out rbtree util functions

These were added to deal with duplicated functionality within btrfs-progs, but
we specifically copied rbtree.c from the kernel, so move these functions out
into their own file.  This will make it easier to keep rbtree.c in sync.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: pull back backref.c and fix it up
Josef Bacik [Fri, 10 Oct 2014 20:57:07 +0000 (16:57 -0400)]
Btrfs-progs: pull back backref.c and fix it up

This patch pulls back backref.c, adds a couple of helpers everywhere that it
needs, and cleans up backref.c to fit in btrfs-progs.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
[removed free_some_buffers after "do not reclaim extent buffer"]
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: repair missing dir index
Josef Bacik [Fri, 10 Oct 2014 20:57:06 +0000 (16:57 -0400)]
Btrfs-progs: repair missing dir index

If we have an inode backref entry then we know enough to add back a missing dir
index.  When messing with the inode backrefs we need to do all of that first
before we process the inode recs themselves as we may clear errors on the inode
recs as we fix the directory indexes.  This adds the framework for fixing
backref errors and fixes missing dir index issues.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: document the limit balance filter
David Sterba [Fri, 10 Oct 2014 17:30:22 +0000 (19:30 +0200)]
btrfs-progs: document the limit balance filter

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: convert: set label or copy from origin
David Sterba [Tue, 31 Jan 2012 13:40:22 +0000 (14:40 +0100)]
btrfs-progs: convert: set label or copy from origin

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add options to tune units for fi df output
David Sterba [Sat, 30 Aug 2014 18:27:00 +0000 (15:27 -0300)]
btrfs-progs: add options to tune units for fi df output

The size unit format is a longstanding annoyance. This patch is based on
the work of Nils and Alexandre and enhances the options. It's possible
to select raw bytes, SI-based or IEC-based compact units (human
frientdly) or a fixed base from kilobytes to terabytes. The default is
compact human readable IEC-based, no change to current version.

CC: Nils Steinger <nst@voidptr.de>
CC: Alexandre Oliva <oliva@gnu.org>
Reviewed-by: Hugo Mills <hugo@carfax.org.uk>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Remove extra 'const' modifiers; they don't do anything.
Adam Buchbinder [Fri, 13 Jun 2014 21:34:35 +0000 (14:34 -0700)]
btrfs-progs: Remove extra 'const' modifiers; they don't do anything.

'const int const *x' means the same thing as 'const int *x' or
'int const *x'; the intent was probably 'const int * const x'.
However, this won't work for the 'suffix' variable, as it has
to be assigned, and making the static tables into const pointers
to const chars leads to a mismatch there.

This was found with clang's duplicate-decl-specifier warning.

Signed-off-by: Adam Buchbinder <abuchbinder@google.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: extend pretty printers with unit mode
David Sterba [Mon, 28 Apr 2014 16:04:48 +0000 (18:04 +0200)]
btrfs-progs: extend pretty printers with unit mode

The functionality of pretty unit printing was duplicated by
df_pretty_sizes, merge it with pretty_size and enhance the interface
with more suffix mode. Raw, binary or decimal.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: print B for bytes
David Sterba [Thu, 24 Apr 2014 16:31:28 +0000 (18:31 +0200)]
btrfs-progs: print B for bytes

This arguably helps parsers.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: debug: print more info about inode
David Sterba [Wed, 2 Jul 2014 11:22:52 +0000 (13:22 +0200)]
btrfs-progs: debug: print more info about inode

Add uid, gid, rdev and flags to btrfs_print_leaf.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: check: do not dereference tree_refs as data_refs
Alexandre Oliva [Tue, 23 Sep 2014 15:51:27 +0000 (17:51 +0200)]
btrfs-progs: check: do not dereference tree_refs as data_refs

In a filesystem corrupted by a faulty memory module, btrfsck would get
very confused attempting to access backrefs that weren't data backrefs
as if they were.  Besides invoking undefined behavior for accessing
potentially-uninitialized data past the end of objects, or with
dynamic types unrelated with the static types held in the
corresponding memory, it used offsets and lengths from such fields
that did not correspond to anything in the filesystem proper.

Moving the test for full backrefs and checking that they're data
backrefs earlier avoided the crash I was running into, but that was
not enough to make the filesystem complete a successful repair.

Signed-off-by: Alexandre Oliva <oliva@gnu.org>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: repair: remove recowed entry from the to-recow list
Alexandre Oliva [Tue, 23 Sep 2014 15:49:58 +0000 (17:49 +0200)]
btrfs-progs: repair: remove recowed entry from the to-recow list

If we attempt to repair a filesystem with metadata blocks that need
recowing, we'll get into an infinite loop repeatedly recowing the
first entry in the list, without ever removing it from the list.
Oops.  Fixed.

Signed-off-by: Alexandre Oliva <oliva@gnu.org>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: fsck: deal with corrupted csum root
Wang Shilong [Thu, 29 May 2014 09:59:57 +0000 (17:59 +0800)]
Btrfs-progs: fsck: deal with corrupted csum root

If checksum root is corrupted, fsck will get segmentation. This
is because if we fail to load checksum root, root's node is NULL which
cause NULL pointer deferences later.

To fix this problem, we just did something like extent tree rebuilding.
Allocate a new one and clear uptodate flag. We will do sanity check
before fsck going on.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: fsck: only allow partial opening under repair mode
Wang Shilong [Thu, 29 May 2014 09:59:56 +0000 (17:59 +0800)]
Btrfs-progs: fsck: only allow partial opening under repair mode

The reason that we allow partial opening is that sometimes,
we may have some corrupted trees.(for example extent tree), for
fsck repair case, the broken tree may be rebuilt later.

So if users only want to do check but not repair anything, this
patch will make fsck return failure as soon as possible and
tell users that some critial roots have been corrupted.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: fsck: disallow partial opening if critical roots corrupted
Wang Shilong [Wed, 28 May 2014 11:20:39 +0000 (19:20 +0800)]
Btrfs-progs: fsck: disallow partial opening if critical roots corrupted

If btrfs tree root is corrupted, fsck will hit the following segmentation.

enabling repair mode
Check tree block failed, want=29376512, have=0
Check tree block failed, want=29376512, have=0
Check tree block failed, want=29376512, have=0
Check tree block failed, want=29376512, have=0
Check tree block failed, want=29376512, have=0
read block failed check_tree_block
Couldn't read tree root
Checking filesystem on /dev/sda9
UUID: 0e1a754d-04a5-4256-ae79-0f769751803e
Critical roots corrupted, unable to fsck the FS
Segmentation fault (core dumped)

In btrfs_setup_all_roots(), we could tolerate some trees(extent tree, csum tree)
corrupted, and we have did careful check inside that function, it will
return NULL if critial roots corrupt(for example tree root).

The problem is that we check @OPEN_CTREE_PARTIAL flag again after
calling btrfs_setup_all_roots() which will successfully return
@fs_info though critial roots corrupted.

Fix this problem by removing @OPEN_CTREE_PARTIAL flag check outsize
btrfs_setup_all_roots().

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: do a separate probe for transient replacing device
Anand Jain [Mon, 18 Aug 2014 08:38:19 +0000 (16:38 +0800)]
btrfs-progs: do a separate probe for transient replacing device

As mentioned in the kernel patch

btrfs: ioctl BTRFS_IOC_FS_INFO and
 BTRFS_IOC_DEV_INFO miss-matched with slots

The count as returned by BTRFS_IOC_FS_INFO is the number of slots that
btrfs-progs would allocate for the BTRFS_IOC_DEV_INFO ioctl. Since
BTRFS_IOC_DEV_INFO would loop across the seed devices, So its better
ioctl BTRFS_IOC_FS_INFO returns the total_devices instead of num_devices.

The above mentioned patch just does that. That is, it returns
total_devices instead of num_devices.

Which means we need to probe for the replacing device separately.

This patch will probe for the replacing device separately.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix device missing of btrfs fi show with seed devices
Gui Hecheng [Mon, 6 Oct 2014 10:16:46 +0000 (18:16 +0800)]
btrfs-progs: fix device missing of btrfs fi show with seed devices

*Note*
this handles the problem under umounted state, the similar problem
under mounted state is already fixed by Anand.

Steps to reproduce:
        # mkfs.btrfs -f /dev/sda1
        # btrfstune -S 1 /dev/sda1
        # mount /dev/sda1 /mnt
        # btrfs dev add /dev/sda2 /mnt
        # umount /mnt                   <== (umounted)
        # btrfs fi show /dev/sda2
result:
        Label: none  uuid: XXXXXXXXXXXXXXXXXX
        Total devices 2 FS bytes used 368.00KiB
        devid    2 size 9.31GiB used 1.25GiB path /dev/sda2
        *** Some devices missing
        Btrfs v3.16-67-g69f54ea-dirty

It is because @btrfs_scan_lblkid() won't establish mappinig
between the seed and sprout devices. So seeding devices are missing.
We could use @open_ctree_* to detect all seed/sprout mappings
for each fs scanned after @btrfs_scan_lblkid().

sth worthes mention:
o If there are multi-level of seeds, all devices in them will be shown
  in the ascending order of @devid
o If device replace is execed on a sprout fs with a device in a seed fs,
  the replaced device still exist in the seed fs together with
  the replacing device in the sprout fs, so we only keep the latest device
  with the newest generation

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: return error when canonicalize_path failed
Eryu Guan [Wed, 8 Oct 2014 09:42:29 +0000 (17:42 +0800)]
btrfs-progs: return error when canonicalize_path failed

Error out cmd_start_replace() if canonicalize_path() dstdev failed, add
the missing "goto leave_with_error;"

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add option to disable backtrace usage
Gustavo Zacarias [Tue, 7 Oct 2014 14:09:04 +0000 (11:09 -0300)]
btrfs-progs: add option to disable backtrace usage

This commit adds the support for a make variable named
"DISABLE_BACKTRACE" which allows to disable the support for backtrace()
usage on ASSERT(), BUG() and BUG_ON() calls.
This is useful because some alternative C libraries like uClibc have
optional support for backtrace() which is rarely built when debugging
isn't taking place.

Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix BUG_ON when all devices under seed fs are missing
Gui Hecheng [Mon, 6 Oct 2014 10:17:49 +0000 (18:17 +0800)]
btrfs-progs: fix BUG_ON when all devices under seed fs are missing

Steps to reproduce:
# mkfs.btrfs -f /dev/sda[1-2]
# btrfstune -S 1 /dev/sda1
# mount /dev/sda /mnt
# btrfs dev add /dev/sda3 /mnt
# umount /mnt
# mkfs.ext4 /dev/sda1 // kill seed dev
# mkfs.ext4 /dev/sda2 // kill seed dev
# btrfs-debug-tree /dev/sda3 <== BUG_ON
Output msg:
volumes.c:1824: btrfs_read_chunk_tree: Assertion `ret` failed.
btrfs-debug-tree[0x41cb36]
btrfs-debug-tree(btrfs_read_chunk_tree+0x3ca)
btrfs-debug-tree(btrfs_setup_chunk_tree_and_device_map
btrfs-debug-tree[0x40f695]
btrfs-debug-tree(open_ctree_fs_info+0x86)
btrfs-debug-tree(main+0x12d)
/lib64/libc.so.6(__libc_start_main+0xf5)
btrfs-debug-tree[0x4062e9]

This BUG_ON complains about a failed @read_one_dev() call when
@open_seed_devices() failed to find the seed @fs_devices object
for a dev_item in chunk tree.
In this case, just insert a "shadow" @fs_devices with the fsid in
dev_item shall make no harm since no other tools will try to
make use of the stuff that the "shadow" @fs_devices possesses
after its creation.

After apply this commit, btrfs-debug-tree will report unable
to open the device.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Check the consistence between the parent node and child node/leaf.
Qu Wenruo [Mon, 6 Oct 2014 06:31:21 +0000 (14:31 +0800)]
btrfs-progs: Check the consistence between the parent node and child node/leaf.

When btrfs-progs walk down the tree, it does not check whether the child
node/leaf is valid.
In fact, there is some corrupted image whose csum is all valid but
parent node points to a invalid leaf.

In my case, the parent node in fs tree point to a invalid leaf(gen 11),
whose generation(15) and first key(EXTENT_TREE ROOT_ITEM 0) is
completely invalid, and will cause BUG_ON in process_inode_item().

Unfortunately, we are unable to fix when it happens.
So we can only output meaningful error message and avoid the insane
node/leaf, which is still much better than the original BUG_ON().

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: skip extent rebuild test if no testdev
Josef Bacik [Fri, 3 Oct 2014 14:59:36 +0000 (10:59 -0400)]
btrfs-progs: skip extent rebuild test if no testdev

It is highly obnoxious to have to go put in a testdev when all you really want
is to run the quick image tests.  Make this part optional so if we don't have a
testdev specified we just don't run that particular test.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: scrub, detect stale information in the status file
David Sterba [Thu, 2 Oct 2014 17:15:38 +0000 (19:15 +0200)]
btrfs-progs: scrub, detect stale information in the status file

If scrub is not cancelled nor finished, the recorded status will prevent
scrub to start again though it's not running. There's a force option to
run it anyway, but this is just a bandaid and the true status of scrub
should be detected automatically. The force option should not be
necessary anymore.

The test introduced in 9681f82853360aac1ff2 checks only the status file,
not kernel status of scrub.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: do not reclaim extent buffer
Naohiro Aota [Tue, 30 Sep 2014 14:40:56 +0000 (23:40 +0900)]
btrfs-progs: do not reclaim extent buffer

We should kill free_some_buffers() to stop reclaiming extent buffers or
we will hit a problem described below.

As of commit 53ee1bccf99cd5b474fe1aa857b7dd176e3a1407, we are not
counting a reference for tree->lru anymore. However free_some_buffers()
is still left and is reclaiming extent buffers whose @refs == 1. This
cause extent buffers to be reclaimed unintentionally. Thus the following
steps could happen:

1. A buffer at address A is reclaimed by free_some_buffers()
   (address A is also free()ed)
2. Some code call alloc_extent_buffer()
3. Address A is assigned to newly allocated buffer
4. You see a buffer pointed by A suddenly changed its content

This problem is also pointed out here and it has a reproducer:
https://www.mail-archive.com/linux-btrfs@vger.kernel.org/msg36703.html

This commit drop free_some_buffers() and related variables, and also it
modify extent_io_tree_cleanup() to catch non-free'ed buffers properly.

Signed-off-by: Naohiro Aota <naota@elisp.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: rebuild the crc tree with --init-csum-tree
Josef Bacik [Wed, 1 Oct 2014 14:34:51 +0000 (10:34 -0400)]
btrfs-progs: rebuild the crc tree with --init-csum-tree

We have --init-csum-tree, which just empties the csum tree.  I'm not sure why we
would ever need this, but we definitely need to be able to rebuild the csum tree
in some cases.  This patch adds the ability to completely rebuild the crc tree
by reading all of the data and adding csum entries for them.  This patch doesn't
pay attention to NODATASUM inodes, it'll happily add csums for everything.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove scan_for_btrfs()
Anand Jain [Sat, 13 Sep 2014 01:21:22 +0000 (09:21 +0800)]
btrfs-progs: remove scan_for_btrfs()

With the changes as in the previous patch, now scan_for_btrfs()
is an unused function. So delete it.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove BTRFS_SCAN_PROC scan method
Anand Jain [Sat, 13 Sep 2014 01:21:21 +0000 (09:21 +0800)]
btrfs-progs: remove BTRFS_SCAN_PROC scan method

The libblkid scan method which was introduced later, will also
scan devices under /proc/partitions. So we don't have to do
the explicit scan of the same.

Remove the scan method BTRFS_SCAN_PROC.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: super-recover: fix double free fs_devices memory
Wang Shilong [Thu, 18 Sep 2014 09:01:12 +0000 (05:01 -0400)]
btrfs-progs: super-recover: fix double free fs_devices memory

super-recover collects btrfs devices infomation using existed
functions scan_one_devices().

Problem is fs_devices is freed twice in close_ctree() and
free_recover_superblock() for super correction path.

Fix this problem by checking whether fs_devices memory
have been freed before we free it.

Cc: Eric Sandeen <sandeen@redhat.com>
Cc: Chris Murphy <lists@colorremedies.com>
Acked-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Wang Shilong <wangshilong1991@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: force overwrite should wipe stale SB
Anand Jain [Wed, 1 Oct 2014 23:22:09 +0000 (07:22 +0800)]
btrfs-progs: force overwrite should wipe stale SB

(I am unable to reproduce the issue, tried to go back with progs versions
but still the same. So as of now this code remains untested, suggest to
wait till we have a reproducible test case).

Here is a test case which says it all..

mkfs.xfs -f $DEV
mkfs.btrfs -f $DEV
mount $DEV $MNT
mount: /dev/vdiskc: more filesystems detected. This should not happen,
       use -t <type> to explicitly specify the filesystem type or
       use wipefs(8) to clean up the device.

mount: you must specify the filesystem type

with this patch btrfs_prepare_device() also wipes old FS if any,
btrfs_prepare_device() is called after we have verified that
user has provided -f option.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: make free space checker work on non-4k sectorsize filesystems
David Sterba [Wed, 28 May 2014 09:25:24 +0000 (11:25 +0200)]
btrfs-progs: make free space checker work on non-4k sectorsize filesystems

The value of sector for space cache was hardcoded to 4k, and used to
calculate bitmap sizes.  In kernel, the BITS_PER_BITMAP is derived from
PAGE_CACHE_SIZE which is not available for userspace, that can also deal
with filesystem of varying sectorsize.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: check, fix csum check in the presence of non-inlined refs
Filipe David Borba Manana [Wed, 7 May 2014 12:09:14 +0000 (13:09 +0100)]
btrfs-progs: check, fix csum check in the presence of non-inlined refs

When we have non-inlined extent references, we were failing to find the
corresponding extent item for an existing csum item in the csum tree.

Reproducer:

   mkfs.btrfs -f /dev/sdd
   mount /dev/sdd /mnt

   xfs_io -f -c "falloc 780366 135302" /mnt/foo
   xfs_io -c "falloc 327680 151552" /mnt/foo
   xfs_io -c "pwrite -S 0xff -b 131072 0 131072" /mnt/foo
   sync

   for i in `seq 1 40`; do btrfs subvolume snapshot /mnt /mnt/snap$i ; done
   umount /mnt

   btrfs check /dev/sdd

The check command exited with status 1 and the following output:

   Checking filesystem on /dev/sdd
   UUID: 2416ab5f-9d71-457e-bb13-a27d4f6b399a
   checking extents
   checking free space cache
   checking fs roots
   checking csums
   There are no extents for csum range 12980224-12984320
   Csum exists for 12980224-12984320 but there is no extent record
   found 1388544 bytes used err is 1
   total csum bytes: 132
   total tree bytes: 704512
   total fs tree bytes: 573440
   total extent tree bytes: 16384
   btree space waste bytes: 564479
   file data blocks allocated: 19341312
    referenced 14606336
   Btrfs v3.14.1-94-g80597e7

After this change it no longer erroneously reports a missing extent for the
csum item and exits with a status of 0.

Also added missing btrfs_prev_leaf() return value checks, as we were ignoring
errors and non-existence of left siblings completely.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fsck: add ability to check reloc roots
Wang Shilong [Thu, 24 Apr 2014 10:51:10 +0000 (18:51 +0800)]
btrfs-progs: fsck: add ability to check reloc roots

When encountering system crash or balance enospc errors,
there maybe still some reloc roots left.

The way we store reloc root is different from fs root:

reloc root's root key(BTRFS_RELOC_TREE_OBJECTID, ROOT_ITEM, objectid)
fs root's root key(objectid, ROOT_ITEM, -1)
reloc data's root key(BTRFS_DATA_RELOC_TREE_OBJECTID, ROOT_ITEM, 0)

So this patch use right key to search corresponding root node, and
avoid using normal fs root cache for reloc roots.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fsck: finish transaction commit if repair error out
Wang Shilong [Thu, 29 May 2014 10:01:42 +0000 (18:01 +0800)]
btrfs-progs: fsck: finish transaction commit if repair error out

If btrfsck fail to repair, we hit something like following:

Check tree block failed, want=29442048, have=0
Check tree block failed, want=29442048, have=0
Check tree block failed, want=29442048, have=0
Check tree block failed, want=29442048, have=0
Check tree block failed, want=29442048, have=0
read block failed check_tree_block
found 98304 bytes used err is 1
total csum bytes: 0
total tree bytes: 0
total fs tree bytes: 0
total extent tree bytes: 0
btree space waste bytes: 0
file data blocks allocated: 0
referenced 0
Btrfs v3.14.2-rc2-63-g3944f15
btrfs: transaction.h:38: btrfs_start_transaction: Assertion `!(root->commit_root)' failed.
Aborted (core dumped)

This is because under repair mode, we will start a transaction, and if we error out,
we don't finish this transaction. So in close_ctree(), it will try
to start and commit transaction which causes the above segmentation.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fsck: remove unfriendly BUG_ON() for searching tree failure
Wang Shilong [Thu, 29 May 2014 10:01:43 +0000 (18:01 +0800)]
btrfs-progs: fsck: remove unfriendly BUG_ON() for searching tree failure

Now btrfsck would hit assertation failure for some searching tree failure.
It is true that filesystem may get some metadata block corrupted,
and btrfsck could not deal with these corruptings. But, Users really
don't want a BUG_ON() here, Instead, just return errors to caller.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fsck: clear out log tree in repair mode
Wang Shilong [Thu, 24 Apr 2014 03:19:16 +0000 (11:19 +0800)]
btrfs-progs: fsck: clear out log tree in repair mode

Repair mode will commit transaction which will make us
fail to load log tree anymore.

Give a warning to common users, if they really want to
coninue, we will clear out log tree.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fsck: avoid pinning same block several times
Wang Shilong [Thu, 24 Apr 2014 03:19:17 +0000 (11:19 +0800)]
btrfs-progs: fsck: avoid pinning same block several times

This can not only give some speedups but also avoid forever loop
with a really broken filesystem.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove BTRFS_SCAN_DEV and btrfs_scan_one_dir
Eric Sandeen [Wed, 20 Aug 2014 22:24:27 +0000 (17:24 -0500)]
btrfs-progs: remove BTRFS_SCAN_DEV and btrfs_scan_one_dir

After the previous 2 patches, nothing uses
whole-dev-tree scanning, so remove the code which
implemented that functionality.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: don't fall back to recursive /dev scan
Eric Sandeen [Wed, 20 Aug 2014 22:23:20 +0000 (17:23 -0500)]
btrfs-progs: don't fall back to recursive /dev scan

If we didn't find what we are looking for in /proc/partitions,
we're not going to find it by scanning every node under /dev, either.

But that's just what btrfs_scan_for_fsid() does.

Remove that fallback; at that point btrfs_scan_for_fsid() just calls
scan_for_btrfs(), so remove the wrapper & call it directly.

Side note: so, these paths always use /proc/partitions, not libblkid.
Userspace-intiated scans default to libblkid.  I presume this is
part of the design, and intentional?  Anyway, not changing it now!

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: scan /proc/partitions not all of /dev with "-d"
Eric Sandeen [Wed, 20 Aug 2014 22:22:13 +0000 (17:22 -0500)]
btrfs-progs: scan /proc/partitions not all of /dev with "-d"

We can scan for btrfs devices in a few ways.  By default
libblkid is used for "device scan" and "filesystem show";
with the -m option only mounted filesystems are scanned,
and with -d we physically read every system device.

But there's no reason for the complexity of a descent through
/dev; /proc/partitions has every device known to the kernel, so
just use that when -d is specified.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: wait until all subvolumes are cleaned
David Sterba [Wed, 23 Jul 2014 20:56:13 +0000 (22:56 +0200)]
btrfs-progs: wait until all subvolumes are cleaned

Enhance the 'subvolume' subcommand to wait until a given list of
subvolumes or all currently scheduled for deletion are cleaned
completely from the filesystem.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Fix malloc size for superblock.
Qu Wenruo [Thu, 3 Jul 2014 09:36:38 +0000 (17:36 +0800)]
btrfs-progs: Fix malloc size for superblock.

recover_prepare() in chunk-recover.c alloc memory which only contains
sizeof(struct btrfs_super_block). This will cause glibc malloc error
after superblock csum is calculated.

Use BTRFS_SUPER_INFO_SIZE to fix the bug.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs progs v3.16.2
David Sterba [Wed, 1 Oct 2014 16:07:59 +0000 (18:07 +0200)]
Btrfs progs v3.16.2

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: check all slots in leaves
Josef Bacik [Tue, 30 Sep 2014 20:56:05 +0000 (16:56 -0400)]
Btrfs-progs: check all slots in leaves

There's an off by one error in btrfs_check_leaf, we should be going to nritems -
1, not nritems - 2, we were missing problems with items in the very last slot.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Check the csum tree node before go through the csum tree
Qu Wenruo [Tue, 30 Sep 2014 08:52:10 +0000 (16:52 +0800)]
btrfs-progs: Check the csum tree node before go through the csum tree

[BUG]
Some fsfuzzed btrfs image will cause btrfsck segfault.

[REPRODUCER]
Run btrfsck on a csum tree block corrupted image.

[REASON]
check_csums() function call btrfs_search_slot() on csum_tree but doesn't
check whether the csum_tree contains a valid extent_buffer, which causes
the segfault.

[FIX]
Check the csum_root->node before any search.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add root to dirty list when fixing bad keys
Josef Bacik [Mon, 29 Sep 2014 16:39:46 +0000 (12:39 -0400)]
btrfs-progs: add root to dirty list when fixing bad keys

A user reported a WARN_ON() when trying to run btrfsck --repair on his fs with
bad key ordering.  This was because the root that was broken wasn't part of the
transaction yet.  We do this open coded thing in a few other places in fsck, so
just make it a helper function and make sure all the places that need to call it
do call it.  With this patch he was able to run repair without it dying.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: let btrfs-image actually work on a balanced fs
Josef Bacik [Fri, 26 Sep 2014 13:02:49 +0000 (09:02 -0400)]
Btrfs-progs: let btrfs-image actually work on a balanced fs

We use the read extent buffer infrastructure to read the super block when we are
creating a btrfs-image.  This works out fine most of the time except when the fs
has been balanced, then it fails to map the super block.  So we could fix
btrfs-image to read in the super in a special way, but thats more code.  So
instead just check in the eb reading code if we are reading the super and then
don't bother mapping the block, just read the actual offset.  This fixed some
poor guy who was trying to btrfs-image his fs that had been balanced.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: don't assert when we have an invalid mapping
Josef Bacik [Thu, 25 Sep 2014 20:10:48 +0000 (16:10 -0400)]
Btrfs-progs: don't assert when we have an invalid mapping

Asserting is no fun, we may be able to recover from this error in certain cases
(like btrfs-image and btrfsck).  Just do what the kernel does and spit out an
error and return that there is only 1 copy.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: make BUG*() be more verbose
Josef Bacik [Thu, 25 Sep 2014 20:07:10 +0000 (16:07 -0400)]
Btrfs-progs: make BUG*() be more verbose

Currently these macros just tie to assert(), which gives us line number and such
but no backtrace so no actual context.  This patch adds support for spitting out
a backtrace so we can see how we got to the given assert.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
[backtrace_symbols_fd]
Signed-off-by: Naohiro Aota <naota@elisp.net>
[minor fixups]
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Add human readable incompat flags output for btrfs-show-super
Qu Wenruo [Wed, 24 Sep 2014 05:31:39 +0000 (13:31 +0800)]
btrfs-progs: Add human readable incompat flags output for btrfs-show-super

Add human readable incompat flags output for btrfs-show-super,
now no longer needs to calculate the hex flags by hand.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix typo in subvol list usage
David Disseldorp [Wed, 16 Apr 2014 17:34:50 +0000 (19:34 +0200)]
btrfs-progs: fix typo in subvol list usage

Signed-off-by: David Disseldorp <ddiss@suse.de>
[moved to new asciidoc]
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: update manpage with new option -f for btrfstune
Gui Hecheng [Mon, 7 Jul 2014 01:54:53 +0000 (09:54 +0800)]
btrfs-progs: update manpage with new option -f for btrfstune

The new option -f will force to do dangerous changes.
e.g. clear the seeding flag.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
[more text from 3db4c0a3d35dd5f8 changelog]
Signed-off-by: David Sterba <dsterba@suse.cz>