platform/upstream/btrfs-progs.git
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>
10 years agobtrfs-progs: Properly cast to avoid compiler warnings, fixes FTBFS on alpha and ia64
Dimitri John Ledkov [Sun, 21 Sep 2014 01:01:19 +0000 (02:01 +0100)]
btrfs-progs: Properly cast to avoid compiler warnings, fixes FTBFS on alpha and ia64

Bug-Debian: http://bugs.debian.org/539433
Bug-Debian: http://bugs.debian.org/583768
Authors:
 Luca Bruno <lucab@debian.org>
 Alexander Kurtz <kurtz.alex@googlemail.com>
 Daniel Baumann <daniel.baumann@progress-technologies.net>

Signed-off-by: Dimitri John Ledkov <xnox@debian.org>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix many typos in documents
Naohiro Aota [Fri, 19 Sep 2014 01:49:59 +0000 (10:49 +0900)]
btrfs-progs: fix many typos in documents

There are many trivial typos in Documentation/*.txt.
All of these use "exist status" to mean "exit status"
by mistake. I guess someone first made this mistake
and it has spread by copy-and-paste :-D

Signed-off-by: Naohiro Aota <naota@elisp.net>
Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs progs v3.16.1
David Sterba [Mon, 15 Sep 2014 13:18:00 +0000 (15:18 +0200)]
Btrfs progs v3.16.1

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: doc: fix invalid reference to setattr(8)
Naohiro Aota [Tue, 9 Sep 2014 02:09:00 +0000 (11:09 +0900)]
btrfs-progs: doc: fix invalid reference to setattr(8)

man 8 btrfs-property refers to `setattr(8)` which does not actually exist.
It should refer to `chattr (1)` instead.

Signed-off-by: Naohiro Aota <naota@elisp.net>
Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove wrong set_argv0 for restore
Gui Hecheng [Thu, 4 Sep 2014 03:09:24 +0000 (11:09 +0800)]
btrfs-progs: remove wrong set_argv0 for restore

Before this patch, you could see the following after exec restore
# :too few arguments

The tool name "btrfs restore" is missing.

The @set_argv0() function is introduced by:
commit a184abc70f7b1468e6036ab576f1587ee0574668
btrfs-progs: move the check_argc_* functions into utils.c
...
Also add a new function "set_argv0" to set the correct tool name:
        *btrfs-image*: too few arguments

But @set_argv0() only applies to the independent tools with
the name pattern btrfs-***.
Since restore is now is subcommand under "btrfs",
there is no need to use @set_argv0() before check_argc_* to
repair the prompt tool name before "too few arguments".

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove btrfs_release_path before btrfs_free_path
Gui Hecheng [Thu, 4 Sep 2014 03:09:23 +0000 (11:09 +0800)]
btrfs-progs: remove btrfs_release_path before btrfs_free_path

The btrfs_free_path calls btrfs_release_path internally.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix find_mount_root() to handle duplicated mount point correctly
Qu Wenruo [Thu, 4 Sep 2014 02:27:48 +0000 (10:27 +0800)]
btrfs-progs: fix find_mount_root() to handle duplicated mount point correctly

Original find_mount_root() will use the first mount point match and
return it.
It was OK until the following commit, which will also check the fstype:
de22c28ef31d9721606ba059 btrfs-progs: Check fstype in find_mount_root()

With fstype check, we should check the last match, not only the first
one.
Or the following mount will not pass the find_mount_root():
/dev/sdc on /mnt/test type ext4 (rw,relatime,data=ordered)
/dev/sdb on /mnt/test type btrfs (rw,relatime,space_cache)

This patch will use the last match to do the fstype check.

Reported-by: Remco Hosman <remco@yerf-it.nl>
Signed-off-by: Remco Hosman <remco@yerf-it.nl>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Reviewed-by: Omar Sandoval <osandov@osandov.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix next_leaf in restore as it improperly skips some slots
Gui Hecheng [Thu, 28 Aug 2014 02:25:54 +0000 (10:25 +0800)]
btrfs-progs: fix next_leaf in restore as it improperly skips some slots

When entering the next level node, the @next_leaf in restore forgets to
start at the first slot. Just reset it to the first one.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix len of read_extent_buffer for inline extent in restore
Gui Hecheng [Thu, 28 Aug 2014 02:25:53 +0000 (10:25 +0800)]
btrfs-progs: fix len of read_extent_buffer for inline extent in restore

Steps to reproduce:
# mkfs.btrfs -f <dev>
# mount -o compress-force=lzo <dev> <mnt>
# for ((i=0;i<4000;i++)); do
   echo -n 'A' >> <mnt>/inline_data
  done
# umount <mnt>
# valgrind --tool=memcheck --leak-check=full \
  btrfs restore <dev> <dest_dir>
output:
==32118== Invalid read of size 1
==32118==    at 0x4A0A4E4: memcpy@@GLIBC_2.14
==32118==    by 0x43DC91: read_extent_buffer
==32118==    by 0x421401: search_dir (cmds-restore.c:240)
==32118==    by 0x422CBB: cmd_restore (cmds-restore.c:1317)
==32118==    by 0x404709: main (btrfs.c:248)
==32118==  Address 0x4c4f4ac is not stack'd, malloc'd or...

It is because when deal with inline extent, the read_extent_buffer
is now reading a len of @ram_bytes which is the len of the uncompressed
data. But actually here we want the len of the inline item.
So in the compressed situation, use the len of the inline item.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: kill BUG_ON in readahead_tree_block()
Zach Brown [Wed, 27 Aug 2014 18:16:03 +0000 (11:16 -0700)]
btrfs-progs: kill BUG_ON in readahead_tree_block()

David sent a quick patch that removed a BUG_ON().  I took a peek and
found that the function was already leaking an eb ref and only returned
0.  So this fixes the leak and makes the function void and fixes up the
callers.

Accidentally-motivated-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: corrupt-block: fix a delete and use bug corrupting extent tree
Qu Wenruo [Fri, 22 Aug 2014 03:42:49 +0000 (11:42 +0800)]
btrfs-progs: corrupt-block: fix a delete and use bug corrupting extent tree

When corrupting extent tree, corrupt-block will iterate each child
node/leaf of a node.
However, when a node's child is leaf, btrfs_corrupt_extent_leaf() may
delete some item in the leaf, which may cause the children number of the
parent node decrease.

Before this patch, corrupt-block will read out the nritems only *ONCE*
and iterate the 'nritems' times.
When btrfs_corrupt_extent_leaf() deletes enough item, causing the
nritems of btrfs_header decreased, the last few iteration will access
non-existed node, which will cause the delete and use bug like
the following:

deleting extent record: key 40714240 168 16384
Couldn't map the block 3459802452797161472
btrfs-corrupt-block: volumes.c:1137: btrfs_num_copies: Assertion
`!(!ce)' failed.
Aborted

This patch will update the nritmes in each iteration to avoid the bug.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: init uninitialized output buf for btrfs-restore
Gui Hecheng [Thu, 21 Aug 2014 03:35:36 +0000 (11:35 +0800)]
btrfs-progs: init uninitialized output buf for btrfs-restore

A memory problem reported by valgrind as follows:
=== Syscall param pwrite64(buf) points to uninitialised byte(s)
When running:
# valgrind --leak-check=yes btrfs restore /dev/sda9 /mnt/backup

Because the output buf size is alloced with malloc, but the length of
output data is shorter than the sizeof(buf), so valgrind report
uninitialised byte(s).
We could use calloc to repalce malloc and clear this WARNING away.

Reported-by: Marc Dietrich <marvin24@gmx.de>
Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Add -R to list UUIDs of original received subvolume
Hugo Mills [Wed, 13 Aug 2014 21:18:17 +0000 (22:18 +0100)]
btrfs-progs: Add -R to list UUIDs of original received subvolume

When using send/receive, it it useful to be able to match up source
subvols on the send side (as, say, for -p or -c clone sources) with their
corresponding copies on the receive side. This patch adds a -R option to
btrfs sub list to show the received subvolume UUID on the receive side,
allowing the user to perform that matching correctly.

Signed-off-by: Hugo Mills <hugo@carfax.org.uk>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: fix typos
Holger Hoffstätte [Tue, 2 Sep 2014 11:57:20 +0000 (11:57 +0000)]
Btrfs-progs: fix typos

Fix (at least one user-visible) typos: it's its, not it's.

Signed-off-by: Holger Hoffstätte <holger.hoffstaette@googlemail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Improve static building and installation
Thomas Petazzoni [Sat, 30 Aug 2014 12:48:10 +0000 (14:48 +0200)]
btrfs-progs: Improve static building and installation

This commit improves the static-only building of btrfs-progs, and adds
support for installing the static only tools:

 - It now ensures that all programs are built statically, not only a
   small subset of them, by defining 'progs_static' from the existing
   'progs' variable.

 - It changes the order of libraries in the btrfs-%.static rule so
   that -lpthread (part of STATIC_LIBS) appears *after* the '$($(subst
   -,_,$(subst .static,,$@)-libs))' logic, which brings in
   -lcom_err. This is needed because libcom_err.a uses the semaphore
   functions, which are available in the pthread library.

 - Adds the necessary rules to generate the btrfsck.static link and
   btrfstune.static binary.

 - Adds an 'install-static' target to install the static
   binaries. Note that they are renamed to not carry a '.static'
   suffix.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Add support for DISABLE_DOCUMENTATION
Gustavo Zacarias [Sat, 30 Aug 2014 12:48:09 +0000 (14:48 +0200)]
btrfs-progs: Add support for DISABLE_DOCUMENTATION

This commit adds the support for a make variable named
"DISABLE_DOCUMENTATION", which allows to disable the build of the
documentation. This is useful in contexts where the tools needed to
build the documentation are not necessarily available.

Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: move debug info to verbose mode for restore
Gui Hecheng [Mon, 1 Sep 2014 01:47:33 +0000 (09:47 +0800)]
btrfs-progs: move debug info to verbose mode for restore

The restore tool should only print info of the restoring process
in verbose mode with -v option specified.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: library version defines
Arvin Schnell [Mon, 3 Jun 2013 12:31:41 +0000 (14:31 +0200)]
btrfs-progs: library version defines

commit 46de1a6ec3dbb0db203baa6c46cb64ba9b000ea2 changed the
parameters of btrfs_read_and_process_send_stream().  This breaks
snapper compilation. We can include version defines usable for the C
preprocessor.

Version 0.1.0: API up to and including 46de1a6ec3dbb0db2 (3.14.x)

Version 0.1.1: 909131939f750faffb9fab (changed in 3.16)

Signed-off-by: Arvin Schnell <aschnell@suse.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: read global reserve size from space infos
David Sterba [Tue, 29 Apr 2014 15:57:01 +0000 (17:57 +0200)]
btrfs-progs: read global reserve size from space infos

Kernels >= 3.15 export the global block reserve as a space info presented
by 'btrfs fi df' but would display 'unknown' instead of some meaningful
string.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove unused flags for btrfs_path
Gui Hecheng [Thu, 21 Aug 2014 02:56:53 +0000 (10:56 +0800)]
btrfs-progs: remove unused flags for btrfs_path

The three flags of @btrfs_path:
btrfs_path {
unsigned int keep_locks:1;
unsigned int skip_locking:1;
unsigned int leave_spinning:1;
}
have little meaning, because the userspace @btrfs_search_slot()
is free of locking and no other routines will decide their behavior
on these. So just remove them.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Do not free dirty extent buffer
Naohiro Aota [Mon, 25 Aug 2014 05:09:07 +0000 (14:09 +0900)]
btrfs-progs: Do not free dirty extent buffer

free_some_buffer() should not free dirty extent buffers. They are left
to be committed.

Signed-off-by: Naohiro Aota <naota@elisp.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs Progs v3.16
Chris Mason [Tue, 26 Aug 2014 13:50:36 +0000 (06:50 -0700)]
Btrfs Progs v3.16

Signed-off-by: Chris Mason <clm@fb.com>
10 years agobtrfs-progs: check: do not require argument for --subvol-extents
David Sterba [Fri, 22 Aug 2014 14:01:38 +0000 (16:01 +0200)]
btrfs-progs: check: do not require argument for --subvol-extents

$ btrfs check --subvol-extents /dev/sdx
ERROR: /dev/sdx is not a valid numeric value.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: mkfs make --uuid require an argument
David Sterba [Fri, 22 Aug 2014 13:45:58 +0000 (15:45 +0200)]
btrfs-progs: mkfs make --uuid require an argument

$ mkfs.btrfs --uuid 1234... /dev/sdx
Error: error checking 1234...  status: No such file or directory

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Fix spelling in btrfs sub list help
Hugo Mills [Tue, 12 Aug 2014 20:22:53 +0000 (21:22 +0100)]
btrfs-progs: Fix spelling in btrfs sub list help

"below", not "bellow"

Signed-off-by: Hugo Mills <hugo@carfax.org.uk>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix unaligned loads in receive
Zach Brown [Thu, 21 Aug 2014 21:24:04 +0000 (14:24 -0700)]
btrfs-progs: fix unaligned loads in receive

A user reported corruption after receiving subvolumes.  Turning up the
logging during the receive showed that the commands and string
attributes were being received correctly but the u64 attrbutes were
sometimes corrupted by having variable number of low order bytes
introduced.

It turned out they were on a platform that corrupts unaligned userspace
loads.  Loading the u64s from the unaligned pointers into the received
command stream with get_unaligned() fixed the problem.

Reported-By: Klaus Holler <kho@gmx.at>
Tested-By: Klaus Holler <kho@gmx.at>
Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add ask_user confirmation for btrfstune clear seeding flag
Gui Hecheng [Mon, 7 Jul 2014 01:54:52 +0000 (09:54 +0800)]
btrfs-progs: add ask_user confirmation for btrfstune clear seeding flag

If we do the following:
# mkfs.btrfs -f <dev>
# mount <dev> <mnt>
# dd if=/dev/urandom of=<mnt>/data bs=1M count=100
# umount <dev>
# btrfstune -S 1 <dev>  <--- make seeding device
# mount <dev> <mnt>
# btrfs dev add -f <dev2> <mnt>
# umount <dev>
# btrfstune -S 0 <dev> <--- clear seeding flag
# mount <dev2> <mnt> <=== new device not mountable

When mounting the new device, btrfs will check whether
the seeding flag is set when try to open seeding device.
If the user clears the seeding flag of the seeding device,
the new device will not be mountable. Even set the seeding
flag back will not recovery this problem, because the generation
has been changed. So clear the seeding flag has the chance to
damage the derived new fs.

So I add user confirmation check when clearing seeding flag.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: avoid to use numeric literal for the size of uuid buffer
Satoru Takeuchi [Mon, 11 Aug 2014 09:12:16 +0000 (18:12 +0900)]
btrfs-progs: avoid to use numeric literal for the size of uuid buffer

Replace a numeric literal to more descriptive macro for
the size of uuid buffer.

Signed-of-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: random fixes for usage string of btrfs scrub
Satoru Takeuchi [Mon, 11 Aug 2014 02:49:35 +0000 (11:49 +0900)]
btrfs-progs: random fixes for usage string of btrfs scrub

- Add missing description about "-R" option in the command
  usage of "btrfs scrub resume".

- Add missing comma to avoid the following misformatted command
  usage of "btrfs scrub start". See the line of "-R" option.

===
usage: btrfs scrub start [-BdqrRf] [-c ioprio_class -n ioprio_classdata] <path>|<device>

    Start a new scrub

    -B     do not background
    -d     stats per device (-B only)
    -q     be quiet
    -r     read only mode
    -R     raw print mode, print full data instead of summary-c     set ioprio class (see ionice(1) manpage)
    -n     set ioprio classdata (see ionice(1) manpage)
    -f     force to skip checking whether scrub has started/resumed in userspace
           this is useful when scrub stats record file is damaged
===

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add missing descriptions of '--max-errors' in btrfs-receive documentation
Satoru Takeuchi [Mon, 11 Aug 2014 02:49:19 +0000 (11:49 +0900)]
btrfs-progs: add missing descriptions of '--max-errors' in btrfs-receive documentation

Fix the lack of description of "--max-erros" option in
both man and command usage of btrfs receive.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove unused parameter in rollback for btrfs-convert
Gui Hecheng [Thu, 7 Aug 2014 02:35:58 +0000 (10:35 +0800)]
btrfs-progs: remove unused parameter in rollback for btrfs-convert

The @force parameter for function @do_rollback is never checked
or used, remove it.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: check option conflict for btrfs-convert
Gui Hecheng [Thu, 7 Aug 2014 02:35:57 +0000 (10:35 +0800)]
btrfs-progs: check option conflict for btrfs-convert

The -d, -i, -n options make no sense to rollback.
Check the improper usages such as:
# btrfs-convert -r -d <dev>

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: move test_isdir() to utils.c
Satoru Takeuchi [Fri, 1 Aug 2014 02:58:00 +0000 (11:58 +0900)]
btrfs-progs: move test_isdir() to utils.c

Since test_isdir() is a utility function, it's better to
move it to utils.c. In addition, "const char *" is
more appropriate type as its "path" argument because
this argument is not changed in this function.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Cc: David Sterba <dsterba@suse.cz>
Cc: Mike Fleetwood <mike.fleetwood@googlemail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: introduce test_issubvolname() for simplicity
Satoru Takeuchi [Fri, 1 Aug 2014 02:44:21 +0000 (11:44 +0900)]
btrfs-progs: introduce test_issubvolname() for simplicity

There are many duplicated codes to check if the given string is
correct subvolume name. Introduce test_issubvolname() for this
purpose for simplicity.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Cc: David Sterba <dsterba@suse.cz>
Cc: Mike Fleetwood <mike.fleetwood@googlemail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add mount status check for btrfs-image
Gui Hecheng [Mon, 7 Jul 2014 01:56:51 +0000 (09:56 +0800)]
btrfs-progs: add mount status check for btrfs-image

When btrfs-image run on a mounted filesystem,
the undergoing fs operations may change what you have imaged a while ago.
In this case, give a warning to remind the user that he may not
get a consistent image he wants.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
[tweaked the message]
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove unnecessary NULL check after get_df()
Satoru Takeuchi [Wed, 30 Jul 2014 07:26:32 +0000 (16:26 +0900)]
btrfs-progs: remove unnecessary NULL check after get_df()

If get_df() returns 0, "sargs" surely points to malloc'ed region.
So NULL check of sargs is not necessary.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix improper return value check for is_existing_blk_or_reg_file
Gui Hecheng [Thu, 31 Jul 2014 03:23:44 +0000 (11:23 +0800)]
btrfs-progs: fix improper return value check for is_existing_blk_or_reg_file

The function @is_existing_blk_or_reg_file has a return value of -errno,
which indicate the @stat call fails with non-ENOENT errors.
In this condition, we should not continue the following work.

But -errno evaluates to true and will let the following work go.
So we should judge more accurately whether the return value of
@is_existing_blk_or_reg_file is > 0 or not to decide our behavior.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove unnecessary return value check in btrfs-property
Gui Hecheng [Thu, 31 Jul 2014 03:23:43 +0000 (11:23 +0800)]
btrfs-progs: remove unnecessary return value check in btrfs-property

The function @parse_prop() returns either -1 or 0, no need to check
for other values. Just return the unnecessary check.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: init variables which are checked later in btrfs-property
Gui Hecheng [Thu, 31 Jul 2014 03:23:42 +0000 (11:23 +0800)]
btrfs-progs: init variables which are checked later in btrfs-property

The local variable @object etc. in btrfs-property get/set functions
are to be checked whether to be NULL or not, but the @parse_args()
don't guarantee to assign a value to it, so it is better to init
it to NULL.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: mkfs: remove experimental tag
David Sterba [Thu, 31 Jul 2014 12:16:50 +0000 (14:16 +0200)]
btrfs-progs: mkfs: remove experimental tag

Make it consistent with kernel status and documentation.

Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add always option to restore's looping prompt
Justin Maggard [Tue, 29 Jul 2014 01:55:09 +0000 (18:55 -0700)]
btrfs-progs: add always option to restore's looping prompt

If you are using btrfs restore to try to recover a very large or
fragmented file, you may encounter _lots_ of prompts requiring
you to press 'y' to continue because we are looping a lot.

Add the option to press 'a', to supress these prompts for the rest
of the file.

Signed-off-by: Justin Maggard <jmaggard10@gmail.com>
Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: fix some build warnings on 32bit platform
Wang Shilong [Sat, 26 Jul 2014 16:49:55 +0000 (00:49 +0800)]
Btrfs-progs: fix some build warnings on 32bit platform

Fix following build warnings on 32bit platform:

...
utils.c:1708:3: warning: left shift count >= width of
type [enabled by default]
   if (x << i & (1UL << 63))
   ^
qgroup-verify.c:393:9: warning: cast to pointer from integer
of different size [-Wint-to-pointer-cast]
  return (struct tree_block *)unode->aux;
         ^
qgroup-verify.c:407:38: warning: cast from pointer to integer
of different size [-Wpointer-to-int-cast]
   if (ulist_add(tree_blocks, bytenr, (unsigned long long)block, 0) >= 0)
                                      ^
cmds-restore.c:120:4: warning: format %lu expects argument of type
long unsigned int, but argument 3 has type size_t [-Wformat=]
    fprintf(stderr, "bad compress length %lu\n", in_len);
...

BTW, this patch also switches other castings with new helpers.

Signed-off-by: Wang Shilong <wangshilong1991@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Unify the messy error message formats
Satoru Takeuchi [Tue, 29 Jul 2014 02:18:17 +0000 (11:18 +0900)]
btrfs-progs: Unify the messy error message formats

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: remove author and copyright info from man page
Wang Shilong [Thu, 24 Jul 2014 09:30:15 +0000 (17:30 +0800)]
Btrfs-progs: remove author and copyright info from man page

>From:
http://man7.org/linux/man-pages/man7/man-pages.7.html
...
AUTHORS lists authors of the documentation or program.Use of
an AUTHORS section is strongly discouraged. Generally,
it is better not to clutter every page with a list of
(over time potentially numerous) authors; if you write
or significantly amend a page, add a copyright notice
as a comment in the source file.  If you are the author
of a device driver and want to include an address for
reporting bugs, place this under the BUGS section.
...

Suggested-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Check fstype in find_mount_root()
Qu Wenruo [Wed, 23 Jul 2014 05:47:35 +0000 (13:47 +0800)]
btrfs-progs: Check fstype in find_mount_root()

When calling find_mount_root(), caller in fact wants to find the mount
point of *BTRFS*.

So also check ent->fstype in find_mount_root() and do special error
string output in caller.

This will suppress a lot of "Inapproiate ioctl for device" error
message.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Remove fprintf() in find_mount_root().
Qu Wenruo [Wed, 23 Jul 2014 05:47:34 +0000 (13:47 +0800)]
btrfs-progs: Remove fprintf() in find_mount_root().

find_mount_root() function in utils.c should not print error string.
Caller should be responsible to print error string.

This patch will remove the only fprintf in find_mount_root() and modify
the caller a little to use strerror() to prompt users.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: fix wrong manpage of defrag command
Liu Bo [Thu, 17 Jul 2014 10:46:01 +0000 (18:46 +0800)]
Btrfs-progs: fix wrong manpage of defrag command

'btrfs filesystem defrag' has an option '-t', whose manpage says

"Any extent bigger than threshold given by -t option, will be
considered already defragged. Use 0 to take the kernel default, and
use 1 to say every single extent must be rewritten."

Here 'use 0' still works, it refers to the default value(256K), however,
'use 1' is an obvious typo, it should be -1, which means the largest value
it can be.

Right now, we use parse_size() which no more allow value '-1', so in
order to keep the manpage correct, this updates it to only keep value '0'.

If you want to make sure every single extent is rewritten, please use a fairly
large size, say 1G.

Reported-by: Sebastian Ochmann <ochmann@informatik.uni-bonn.de>
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: use check_argc_* to check arg number for all tools
Gui Hecheng [Wed, 16 Jul 2014 03:59:46 +0000 (11:59 +0800)]
btrfs-progs: use check_argc_* to check arg number for all tools

Since this patch:
btrfs-progs: move the check_argc_* functions into utils.c

All tools including the independent tools(e.g. btrfs-image, btrfs-convert)
can share the convenience of the check_argc_* functions, so this patch
adopt the argc check functions globally.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Add uninstall targets to Makefiles.
Nils Steinger [Fri, 25 Jul 2014 13:41:57 +0000 (15:41 +0200)]
btrfs-progs: Add uninstall targets to Makefiles.

Signed-off-by: Nils Steinger <git@n-st.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: replace a confusing raw number with a macro
Gui Hecheng [Thu, 17 Jul 2014 02:40:38 +0000 (10:40 +0800)]
btrfs-progs: replace a confusing raw number with a macro

The raw number 36 for the uuid string length is somewhat confusing,
use a macro to define replace it.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
[Use BTRFS_UUID_UNPARSED_SIZE]
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix wrong message about forcing the mixed group profile
Anand Jain [Tue, 15 Jul 2014 08:26:20 +0000 (16:26 +0800)]
btrfs-progs: fix wrong message about forcing the mixed group profile

When the added disk is small, the function btrfs_prepare_device() would
print message indicating that mixed group is forced, apparently thats
not true

reproducer:

mkfs.btrfs -f -draid1 -mraid1 /dev/sdd /dev/sdc
::
SMALL VOLUME: forcing mixed metadata/data groups <---- WRONG

mount /dev/sdd /btrfs

btrfs fi df /btrfs
Data, RAID1: total=126.25MiB, used=128.00KiB
Data, single: total=8.00MiB, used=0.00B
System, RAID1: total=8.00MiB, used=16.00KiB
System, single: total=4.00MiB, used=0.00B
Metadata, RAID1: total=126.25MiB, used=112.00KiB
Metadata, single: total=8.00MiB, used=0.00B
GlobalReserve, single: total=16.00MiB, used=0.00B

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix build of static target
David Sterba [Wed, 23 Jul 2014 13:13:46 +0000 (15:13 +0200)]
btrfs-progs: fix build of static target

A user repoted that static buid fails with

utils-lib.static.o: In function `arg_strtou64':
/home/dsterba/labs/btrfs-progs/utils-lib.c:17: multiple definition of `arg_strtou64'
utils-lib.static.o:/home/dsterba/labs/btrfs-progs/utils-lib.c:17: first defined here

utils-lib.o was mistakenly added to linker twice.

Signed-off-by: David Sterba <dsterba@suse.cz>
Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
10 years agobtrfs-progs: correct manpage option description for scrub
Gui Hecheng [Wed, 30 Jul 2014 07:32:05 +0000 (15:32 +0800)]
btrfs-progs: correct manpage option description for scrub

The -f option of scrub means to
    "force starting new scrub even if a scrub is already running"
*not*
    "force to check whether scrub has started or resumed in userspace"
as described originally.

So replace the orignal description in the manpage and code.

Also, add description of the potential failure as follows
    "If a scrub is already running, it fails."

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Cc: David Sterba <dsterba@suse.cz>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: remove unnecessary judgment for fd in scrub
Gui Hecheng [Thu, 17 Jul 2014 02:40:37 +0000 (10:40 +0800)]
btrfs-progs: remove unnecessary judgment for fd in scrub

The scrub_read_file function is always on a branch,
which has (fd >= 0), so there is not need to judgment
the pasted in arg.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: adjust the return values for scrub
Gui Hecheng [Thu, 17 Jul 2014 02:40:36 +0000 (10:40 +0800)]
btrfs-progs: adjust the return values for scrub

o Return 0 to indicate success,
  when detected errors were corrected during scrubbing.
  P.s. This is also to facilitate scripting when return value
       is to be checked.
o Warn the users if there are uncorrectable errors detected.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: mkfs: don't zero extend small files
Zach Brown [Wed, 16 Jul 2014 21:02:37 +0000 (14:02 -0700)]
btrfs-progs: mkfs: don't zero extend small files

mkfs can try to write outside of small devices.  The zeroing code
doesn't test the device size and runs before mkfs tests for small
devices and exits.

Testers experienced this as small regular files being extended as mkfs
failed:

 $ truncate -s 1m /tmp/some-file
 $ strace -epwrite ./mkfs.btrfs /tmp/some-file
 SMALL VOLUME: forcing mixed metadata/data groups

 WARNING! - Btrfs v3.14.2 IS EXPERIMENTAL
 WARNING! - see http://btrfs.wiki.kernel.org before using

 pwrite(3, ..., 2097152, 0) = 2097152
 pwrite(3, ..., 4096, 65536) = 4096
 pwrite(3 ..., 2097152, 18446744073708503040) = -1 EINVAL (Invalid argument)
 ERROR: failed to zero device '/tmp/some-file' - Input/output error

 $ ls -lh /tmp/some-file
 -rw-rw-r--. 1 zab zab 2.0M Jul 16 13:49 /tmp/some-file

This simple fix adds a helper that clamps a region to be zeroed to the
size of the device.  It doesn't address the larger questions of whether
to modify the device before the size test or whether or zero regions
that have been trimmed.

Finally, the error handling mess after the zeroing calls is cleaned up.
zero_blocks() and its callers only return -errno.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: define BTRFS_MKFS_SMALL_VOLUME_SIZE for small volume
Anand Jain [Tue, 15 Jul 2014 08:02:10 +0000 (16:02 +0800)]
btrfs-progs: define BTRFS_MKFS_SMALL_VOLUME_SIZE for small volume

mkfs cut of size '1024 * 1024 * 1024' to mark dev as small volume so to
force mixed group. Use a define for that.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: dont break the string
Anand Jain [Tue, 15 Jul 2014 08:02:09 +0000 (16:02 +0800)]
btrfs-progs: dont break the string

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: doc: fix incorrect subvol name
Tsutomu Itoh [Mon, 14 Jul 2014 02:27:43 +0000 (11:27 +0900)]
btrfs-progs: doc: fix incorrect subvol name

Subvolume name is wrong. Fix it.

Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: ignore orphaned qgroups by default
Mark Fasheh [Tue, 8 Jul 2014 20:41:57 +0000 (13:41 -0700)]
btrfs-progs: ignore orphaned qgroups by default

qgroup items are not deleted by btrfs when the underlying subvolume goes
away. As a result, btrfsck will print those as inconsistent. This can
clutter up the printout so we ignore them by default. They are still printed
if a full report (via --qgroup-report) is requested.

This patch and the ones it depends on (to do qgroup verification) can be
found at:

https://github.com/markfasheh/btrfs-progs-patches/tree/qgroup-verify

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: fix Segmentation fault of btrfs-convert
Liu Bo [Mon, 7 Jul 2014 14:35:53 +0000 (22:35 +0800)]
Btrfs-progs: fix Segmentation fault of btrfs-convert

Recently we merge a memory leak fix, which fails xfstests/btrfs/012,
the cause is that it only frees @fs_devices but leaves it on the global
fs_uuid list, which cause a 'Segmentation fault' over running command
btrfs-convert.  This fixes the problem.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: Allow btrfs_read_dev_super() to read all 3 super for super_recover.
Qu Wenruo [Thu, 3 Jul 2014 09:36:36 +0000 (17:36 +0800)]
btrfs-progs: Allow btrfs_read_dev_super() to read all 3 super for super_recover.

Btrfs-progs superblock checksum check is somewhat too restricted for
super-recover, since current btrfs-progs will only read the 1st
superblock and if you need super-recover the 1st superblock is
possibly already damaged.

The fix is introducing super_recover parameter for
btrfs_read_dev_super() and callers to allow scan backup superblocks if
needed.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: show extent state for a subvolume
Mark Fasheh [Wed, 2 Jul 2014 20:34:41 +0000 (13:34 -0700)]
btrfs-progs: show extent state for a subvolume

The qgroup verification code can trivially be extended to provide
extended information on the extents which a subvolume root
references. Along with qgroup-verify, I have found this tool to be
invaluable when tracking down extent references.

The patch adds a switch to the check subcommand '--subvol-extents'
which takes as args a single subvolume id. When run with the switch,
we'll print out each extent that the subvolume references. The extent
printout gives standard extent info you would expect along with
information on which other roots reference it.

Sample output follows - this is a few lines from a run on a subvolume
I've been testing qgroup changes on:

Print extent state for subvolume 281 on /dev/vdb2
UUID: 8203ca66-9858-4e3f-b447-5bbaacf79c02
Offset Len Root Refs Roots
12582912 20480 12 257 279 280 281 282 283 284 285 286 287 288 289
12603392 8192 12 257 279 280 281 282 283 284 285 286 287 288 289
12611584 12288 12 257 279 280 281 282 283 284 285 286 287 288 289
 <snip a bunch of extents to show some variety>
124583936 16384 4 281 282 283 280
125075456 16384 4 280 281 282 283
126255104 16384 11 257 280 281 282 283 284 285 286 287 288 289
4763508736 4096 3 279 280 281

In case it wasn't clear, this applies on top of my qgroup verify patch:
"btrfs-progs: add quota group verify code"

A branch with all this can be found on github:

https://github.com/markfasheh/btrfs-progs-patches/tree/qgroup-verify

Please apply,

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: limit minimal num of args for btrfs-image
Gui Hecheng [Mon, 30 Jun 2014 03:54:12 +0000 (11:54 +0800)]
btrfs-progs: limit minimal num of args for btrfs-image

The btrfs-image requires at least 2 args to run,
one for the source dev/file, the other for the target dev/file.

This patch depends on patch:
btrfs-progs: move the check_argc_* functions into utils.c

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: move the check_argc_* functions into utils.c
Gui Hecheng [Mon, 30 Jun 2014 03:54:11 +0000 (11:54 +0800)]
btrfs-progs: move the check_argc_* functions into utils.c

To let the independent tools(e.g. btrfs-image, btrfs-convert, etc.)
share the convenience of check_argc_* functions, just move it into
utils.c.
Also add a new function "set_argv0" to set the correct tool name:
*btrfs-image*: too few arguments

The original btrfs* tools work as before.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
[moved argv0 and check_argc to utils.*]
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: balance filter: add limit of processed chunks
David Sterba [Wed, 7 May 2014 15:38:00 +0000 (17:38 +0200)]
btrfs-progs: balance filter: add limit of processed chunks

Add more control to the balance behaviour.

Usage filter may not be finegrained enough and can lead to moving too
many chunks at once. Another example use is in connection with
drange+devid or vrange filters that allow to work with a specific chunk
or even with a chunk on a given device.

The limit filter applies last, the value of 0 means no limiting.

CC: Ilya Dryomov <idryomov@gmail.com>
CC: Hugo Mills <hugo@carfax.org.uk>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: add X inode flag to btrfs(5)
David Sterba [Wed, 2 Jul 2014 11:25:11 +0000 (13:25 +0200)]
btrfs-progs: add X inode flag to btrfs(5)

After the discussion in
http://thread.gmane.org/gmane.comp.file-systems.btrfs/36334

the 'X' will be mentioned in the manpage because new e2fsprogs/lsattr
will display it and represents the NOCOMPRESS bit of an inode.

Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: cleanup unnecessary free if malloc fails in btrfs-image
Gui Hecheng [Thu, 26 Jun 2014 02:53:03 +0000 (10:53 +0800)]
btrfs-progs: cleanup unnecessary free if malloc fails in btrfs-image

Don't bother free the buffer if the malloc failed.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agoBtrfs-progs: fsck: reduce memory usage for extents check
Wang Shilong [Fri, 20 Jun 2014 11:09:07 +0000 (19:09 +0800)]
Btrfs-progs: fsck: reduce memory usage for extents check

Steps to reproduce:

 # mkfs.btrfs -f /dev/sda9 -b 2g
 # mount /dev/sda9 /mnt
 # dd if=/dev/zero of=/mnt/data bs=4k oflag=direct
 # btrfs file df /mnt

    Data, single: total=1.66GiB, used=1.66GiB
    System, single: total=4.00MiB, used=16.00KiB
    Metadata, single: total=200.00MiB, used=67.88MiB

For a filesystem without snapshots, 70M metadata, extent
checking eats max memory about 110M, this is a nightmare
for some system with low memory.

It is very likely that extent record can be freed quickly
for a filesystem without snapshots, improve this by trying
if it can free memory after adding data/tree backrefs.

This patch reduces max memory cost from 110M to 40M for
extents checking for the above case.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
10 years agobtrfs-progs: fix btrfs-image old_restore fsck failure
Gui Hecheng [Thu, 26 Jun 2014 02:53:01 +0000 (10:53 +0800)]
btrfs-progs: fix btrfs-image old_restore fsck failure

Steps to reproduce:
# mkfs.btrfs -f <dev1>
# btrfs-image <dev1> <image_file>
# btrfs-image -r -o <image_file> <dev2>
# btrfs check <dev2>

btrfs check output:
: read block failed check_tree_block
: Couldn't read tree root
: Couldn't open file system

The btrfs-image should not mess with the chunk tree under the old_restore way.

The new restore way was introduced by:
commit d6f7e3da0dae7b60cb7565f8a47c3b9045c52d1d
Btrfs-progs: make btrfs-image restore with a valid chunk tree V2
...
And the following commit enhanced the new restore on the valid chunk tree
building stuff:
commit ef2a8889ef813ba77061f6a92f4954d047a78932
Btrfs-progs: make image restore with the original device offsets
...

But the second commit should not effect the old_restore way since the
old_restore way doesn't try to build a valid chunk tree at all.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>