platform/upstream/btrfs-progs.git
8 years agobtrfs-progs: stat info for btrfs rescue chunk-recover
Zhao Lei [Wed, 2 Sep 2015 12:22:31 +0000 (20:22 +0800)]
btrfs-progs: stat info for btrfs rescue chunk-recover

chunk-recover need to use many many time in scan_devices(),
and no output in screen:
 # btrfs rescue chunk-recover /dev/sda6
 (no output here, but need long time)

To notice user that "the command is not hang", this patch add
dynamic updated stat information in above period:
 # btrfs rescue chunk-recover /dev/sda6
 Scanning: DONE in dev[0], 19998441472 in dev[1], DONE in dev[2]
 (until)
 Scanning: DONE in dev0, DONE in dev1, DONE in dev2
 Check chunks successfully with no orphans
 Recover the chunk tree successfully.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Use long type to get thread's return value
Zhao Lei [Wed, 2 Sep 2015 12:22:30 +0000 (20:22 +0800)]
btrfs-progs: Use long type to get thread's return value

pthread use void * to save return status, we can use this pointer to
save our return value, but we need to keep the same length.

This patch move to use long type variable to save return value
of our thread, to avoid potential invalid memory access.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: use for loop for scan_devices
Zhao Lei [Wed, 2 Sep 2015 12:22:29 +0000 (20:22 +0800)]
btrfs-progs: use for loop for scan_devices

for() is more suitable than while() in this code block.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: handle errors in btrfs_wipe_existing_sb
David Sterba [Tue, 1 Sep 2015 14:00:12 +0000 (16:00 +0200)]
btrfs-progs: handle errors in btrfs_wipe_existing_sb

Failure during superblock wiping is not always a hard error as we're
going to overwrite it anyway but it may catch some errors earlier. The
error message is not very descriptive though, because we don't get back
much information from blkid.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: use on-stack buffer for superblock
David Sterba [Tue, 1 Sep 2015 13:10:48 +0000 (15:10 +0200)]
btrfs-progs: use on-stack buffer for superblock

In update_disk_super_on_device,the buffer size is small, allocate it
onstack and get rid of one error path.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: image: better error handling in update_disk_super_on_device
David Sterba [Tue, 1 Sep 2015 13:04:29 +0000 (15:04 +0200)]
btrfs-progs: image: better error handling in update_disk_super_on_device

Return an error code instead of exitting, more verbose messages.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: Use mount_test_dev for misc-tests/007-subvolume-sync
Zhao Lei [Tue, 1 Sep 2015 10:03:28 +0000 (18:03 +0800)]
btrfs-progs: tests: Use mount_test_dev for misc-tests/007-subvolume-sync

So this test can support both block device and loop device simply.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: Fix mount fail of 013-extent-tree-rebuild
Zhao Lei [Tue, 1 Sep 2015 12:40:13 +0000 (14:40 +0200)]
btrfs-progs: tests: Fix mount fail of 013-extent-tree-rebuild

mount command in old system can not add "-o loop" option automatically
for loop device, and make following test failed:
  # ./fsck-tests.sh
  ...
    [TEST]   013-extent-tree-rebuild
  failed: mount /data/btrfsprogs/tests/test.img /data/btrfsprogs/tests/mnt
  test failed for case 013-extent-tree-rebuild

Considering that $TEST_DEV can be block or loop device, we need determine
our mount option in a condition for both case.

This patch create a wrapper function for above request, to solve current
problem in 013-extent-tree-rebuild, and support similar request in future.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: introduce test dev mount helpers
Zhao Lei [Tue, 1 Sep 2015 12:45:22 +0000 (14:45 +0200)]
btrfs-progs: tests: introduce test dev mount helpers

mount command in old system can not add "-o loop" option automatically
for a loop device, and make following test 013-extent-tree-rebuild fail.

Considering that $TEST_DEV can be block or loop device, we need to determine
our mount option in a condition for both case.

Introduce a wrapper that will add the loop options if needed.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: Use --no-same-owner option for tar
Zhao Lei [Tue, 1 Sep 2015 10:03:14 +0000 (18:03 +0800)]
btrfs-progs: tests: Use --no-same-owner option for tar

Some test failed in my nfs dir:
  ...
    [TEST]   006-bad-root-items
  tar: test.img: Cannot change ownership to uid 1000, gid 1000: Invalid argument
  tar: Exiting with failure status due to previous errors
  failed to extract default_case.tar.xz
  test failed for case 006-bad-root-items

It is because the image file's owner is:
 # tar tvf default_case.tar.xz
 -rw-r--r-- fdmanana/fdmanana 2147483648 2014-10-17 17:59 test.img

And make tar failed in chown in nfs.

It is not a big issue because we don't use nfs commonly, but extract
the image file with ownership of current user will be a better choice.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: makefile: add support for additional build flags
David Sterba [Mon, 31 Aug 2015 16:36:16 +0000 (18:36 +0200)]
btrfs-progs: makefile: add support for additional build flags

Allow fine tuned build options via EXTRA_CFLAGS and EXTRA_LDFLAGS
variables. Useful for additional debugging flags or warnings etc.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: use android compat header
David Sterba [Mon, 31 Aug 2015 16:15:27 +0000 (18:15 +0200)]
btrfs-progs: use android compat header

Applies to sources where pthreads are used.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: add compat header for android
David Sterba [Wed, 26 Aug 2015 14:32:18 +0000 (16:32 +0200)]
btrfs-progs: add compat header for android

Provide stubs and compat macros for android build.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Add Android build mk file
kenneth.kang [Wed, 5 Aug 2015 09:08:25 +0000 (18:08 +0900)]
btrfs-progs: Add Android build mk file

Add Android.mk to use btrfs on android device.

There are still outstanding problems and build support is incomplete:

1) phread_cancel was changed to pthread_kill due to android didn't
   support that.
2) This module needs lzo library on android/external folder.
3) blkid library doesn't support fully. So have to use -f option to
   work.

Signed-off-by: kenneth.kang <kenneth.kang@lge.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Simplify all-subvolumn-clean condition for wait_for_subvolume_cleaning
Zhao Lei [Wed, 26 Aug 2015 14:03:39 +0000 (22:03 +0800)]
btrfs-progs: Simplify all-subvolumn-clean condition for wait_for_subvolume_cleaning

Instead of using a dirty-subvolumn-counter in old code, this patch
turn to use a simple and direct way:
  If (not dirty-subvolumn found in current loop) {
      return all_clean;
  }

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Simplify memory allocation for enumerate_dead_subvols
Zhao Lei [Wed, 26 Aug 2015 14:03:37 +0000 (22:03 +0800)]
btrfs-progs: Simplify memory allocation for enumerate_dead_subvols

No need prepare memory for enumerate_dead_subvols() in caller, and pass
additional argument for allocated length.

Just do every thing inside enumerate_dead_subvols(), it will not
increase malloc count, but make code simple.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Fix wrong return value of wait_for_subvolume_cleaning()
Zhao Lei [Wed, 26 Aug 2015 14:03:38 +0000 (22:03 +0800)]
btrfs-progs: Fix wrong return value of wait_for_subvolume_cleaning()

Reproduce:
 # btrfs subvolume sync /mnt/btrfs
 Subvolume id 323 is gone
 # echo $?
 1
 #

Reason:
 wait_for_subvolume_cleaning() return !0 in right case, because
 value of ret is set to "is subvolume clean" state before return.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Fix infinite loop of btrfs subvolumn sync
Zhao Lei [Wed, 26 Aug 2015 14:03:36 +0000 (22:03 +0800)]
btrfs-progs: Fix infinite loop of btrfs subvolumn sync

We can trigger the bug by following operation:
  (no wait between commands 3~5)
  btrfs subvolume create /mnt/btrfs/mysubvol
  btrfs subvolume snapshot /mnt/btrfs/mysubvol /mnt/btrfs/mysubvol_snap
  btrfs subvolume delete /mnt/btrfs/mysubvol_snap
  btrfs subvolume delete /mnt/btrfs/mysubvol
  btrfs subvolume sync /mnt/btrfs
The last command will not exit.

Reason:
  List of "deleted subvolumes" are not currectly set.

  It caused by a typo of value assign, in detail:
  *ids[idx] = sh->offset;
  should be:
  (*ids)[idx] = sh->offset;
  So only first element is set to right memory address.

  If there are multiple "deleted subvolumes", program will
  keep wait.

Above typo also caused some segment fault in my test.

This patch fixed above bug.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fix memory leak in btrfs-convert main()
Byongho Lee [Thu, 27 Aug 2015 15:38:16 +0000 (00:38 +0900)]
btrfs-progs: fix memory leak in btrfs-convert main()

In btrfs-convert main(), strdup() allocates memory to fslabel but that
memory is not freed. We could fix it by adding free() calls to every
return point, but that would make the code messy because there are
several return paths.
So I fix it by changing the code using strdup() with local array and
strncpy().

And btrfs-convert main() guarantees that string length of fslabel is not
to exceed 'BTRFS_LABEL_SIZE', so it's enough to use strcpy() instead of
strncpy() to copy fslabel in do_convert().

Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: umount TEST_MNT in clean-tests.sh
Zhao Lei [Mon, 31 Aug 2015 05:04:38 +0000 (13:04 +0800)]
btrfs-progs: tests: umount TEST_MNT in clean-tests.sh

If a testcase failed, we can't run it(or other tests needs mount) again,
  # ./misc-tests.sh 007
   [TEST]   007-subvolume-sync
   failed: fail
   test failed for case 007-subvolume-sync
  # ./misc-tests.sh 007
   [TEST]   007-subvolume-sync
   failed: mount /root/btrfs-progs/tests/test.img /root/btrfs-progs/tests/mnt
   test failed for case 007-subvolume-sync

This patch add "umount $TEST_MNT" to clean-tests.sh, to let user
clean mountpoint easily.

After patch:
  # ./misc-tests.sh  007
   [TEST]   007-subvolume-sync
   failed: fail
   test failed for case 007-subvolume-sync
  #
  # clean-tests.sh
  #
  # ./misc-tests.sh  007
   [TEST]   007-subvolume-sync
   failed: fail
   test failed for case 007-subvolume-sync

Suggested-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[added sudo helper to umount]
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: Introduce init_env to initialize common env variant
Zhao Lei [Mon, 31 Aug 2015 05:04:36 +0000 (13:04 +0800)]
btrfs-progs: tests: Introduce init_env to initialize common env variant

For example, $TEST_DIR is common used in severial tests, and have
duplicated code for initialize.

These duplicated code not only benifits harddisk vendor, but have
inconsistent details, as:
  convert-tests.sh: lack of mkdir
  fsck-tests/012-leaf-corruption/test.sh: unnecessary mkdir
  fsck-tests/013-extent-tree-rebuild/test.sh: unnecessary init
  misc-tests/XXX ...
And severial error message:
  _fail "unable to create mount point on $TEST_MNT"
  _fail "failed to create mount point"
  ...

This patch move initizlizaton of $TEST_DIR to common init_env(),
to avoid above problem, and init_env() can be used to add more
things in future.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Use common unit parser for btrfs filesystem command
Zhao Lei [Tue, 1 Sep 2015 07:12:06 +0000 (15:12 +0800)]
btrfs-progs: Use common unit parser for btrfs filesystem command

Move to use get_unit_mode_from_arg() for cmds-filesystem.c,
to make "btrfs filesystem df/show/usage"'s unit argument same.

Also have cleanup effect: 19 insertions(+), 181 deletions(-)

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Use common unit parser for btrfs qgroup command
Zhao Lei [Mon, 31 Aug 2015 11:03:44 +0000 (19:03 +0800)]
btrfs-progs: Use common unit parser for btrfs qgroup command

Move to use get_unit_mode_from_arg() for btrfs qgroup command,
to make "btrfs qgroup show"'s unit argument same with other
tools.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Use common unit parser for btrfs device command
Zhao Lei [Mon, 31 Aug 2015 11:03:43 +0000 (19:03 +0800)]
btrfs-progs: Use common unit parser for btrfs device command

Move to use get_unit_mode_from_arg() for cmds-device.c,
to make "btrfs device usage"'s unit argument same with other
tools.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Introduce get_unit_mode_from_arg for common use
Zhao Lei [Mon, 31 Aug 2015 11:03:41 +0000 (19:03 +0800)]
btrfs-progs: Introduce get_unit_mode_from_arg for common use

We are using separate code for parsing unit mode in current code,
better to use common function.

This patch introduces a common function to specify units as arguments
and a common help message, to make every tool in btrfs having same unit
argument.

The benefits are:
1: Unify current tool's arguments for unit
2: Make tools in future easy to implement such argument
3: Changes (enhancement) in common function have effect on all
   relative tools

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: Introduce subvolume sync test
Zhao Lei [Fri, 28 Aug 2015 03:42:11 +0000 (11:42 +0800)]
btrfs-progs: tests: Introduce subvolume sync test

Current code have following bug for subvolume sync:
1: If there are more than 1 subvolume to sync, the program will
   infinitely loop.
2: return !0 in exit

This patch add misc-tests/007-subvolume-sync for above case.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: add memory allocation fail check in btrfs_add_to_fsid()
Byongho Lee [Thu, 27 Aug 2015 15:38:18 +0000 (00:38 +0900)]
btrfs-progs: add memory allocation fail check in btrfs_add_to_fsid()

In btrfs_add_to_fsid(), strdup() allocates memory to device->name, but
the return value is not checked.
So add the return value check and error handling code.
And clean-up error handling code for ENOMEM case.

Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fix memory leak in btrfs-map-logical main()
Byongho Lee [Thu, 27 Aug 2015 15:38:17 +0000 (00:38 +0900)]
btrfs-progs: fix memory leak in btrfs-map-logical main()

In btrfs-map-logical main(), strdup() allocates memory to output_file,
but that memory is not freed.
So add missing free() calls before return.

Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: use btrfs_open_dir for btrfs device command
Zhao Lei [Wed, 26 Aug 2015 09:04:23 +0000 (17:04 +0800)]
btrfs-progs: use btrfs_open_dir for btrfs device command

We can use btrfs_open_dir() to check whether the target dir is
under btrfs mount point before openning, instead of checking it in kernel
through ioctl, and returning a  fuzzy error message.

Before patch:
  # (/mnt/tmp is not btrfs mountpoint)
  #
  # btrfs device add -f /dev/sda13 /mnt/tmp
  ERROR: error adding the device '/dev/sda13' - Inappropriate ioctl for device
  #

After patch:
  # btrfs device add -f /dev/sda13 /mnt/tmp
  ERROR: not btrfs filesystem: /mnt/tmp
  #

Similar fix for device remove and device usage.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[renamed to btrfs_open_dir]
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Introduce btrfs_open_dir wrapper
Zhao Lei [Wed, 26 Aug 2015 09:04:22 +0000 (17:04 +0800)]
btrfs-progs: Introduce btrfs_open_dir wrapper

This patch introduce open_btrfs_dir() to open a dir in btrfs
filesystem.

It can be used for several tools in btrfs-progs.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[renamed from open_btrfs_dir, adjusted error messages]
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: remove code for setup mntid in cmd_subvol_show
Zhao Lei [Wed, 26 Aug 2015 14:16:14 +0000 (22:16 +0800)]
btrfs-progs: remove code for setup mntid in cmd_subvol_show

We don't need to use value of mntid in cmd_subvol_show(), no need
to get its value.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fix use after free in replace start
David Sterba [Wed, 26 Aug 2015 11:34:39 +0000 (13:34 +0200)]
btrfs-progs: fix use after free in replace start

Commit "btrfs-progs: Add further checks to btrfs replace start command"
accesses device size just after its memory is freed.

Resolves-coverity-id: 1320425
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: add newline to some error messages
Tsutomu Itoh [Fri, 7 Aug 2015 00:20:25 +0000 (09:20 +0900)]
btrfs-progs: add newline to some error messages

Added a missing newline to some error messages.
Also printf() was changed to fprintf(stderr) for error messages.

Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Reviewed-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: close all fs_devices before exit in some commands
Zhao Lei [Mon, 24 Aug 2015 08:45:03 +0000 (16:45 +0800)]
btrfs-progs: close all fs_devices before exit in some commands

mkfs creates more than one fs_devices in fs_uuids.
1: one is for file system being created
2: others are created in test_dev_for_mkfs in order to check mount point
   test_dev_for_mkfs()-> ... -> btrfs_scan_one_device()

Current code only closes 1, and this patch also closes in case 2.

Similar problem exist in other tools, eg.::

 cmd-check.c: the function is:
 cmd_check()->check_mounted()-> ... -> btrfs_scan_one_device()
 ...

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Introduce btrfs_close_all_devices helper
Zhao Lei [Mon, 24 Aug 2015 08:45:02 +0000 (16:45 +0800)]
btrfs-progs: Introduce btrfs_close_all_devices helper

If there is more than one fs_devices in fs_uuids list (like mkfs.btrfs
does), we need close them all before exit. Add a helper for that.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: find-root: fix a bug that will cause wrong result
Qu Wenruo [Tue, 25 Aug 2015 02:44:57 +0000 (10:44 +0800)]
btrfs-progs: find-root: fix a bug that will cause wrong result

[BUG]
btrfs-find-root may not output desire result, as due to
search_extent_cache() may return a result that doesn't cover the desired
range, generation cache can be screwed up if higher generation tree root
is found before lower generation tree root.

For example:
=======
./btrfs-find-root  /dev/sda6  -a
Superblock thinks the generation is 8
Superblock thinks the level is 0
adding bytenr: 4194304, gen: 8 <<< Debug output
adding bytenr: 24715264, gen: 7 <<< gen is 7 at read_tree_block time
Well block 4194304(gen: 8 level: 0) seems good, and it matches
superblock
Well block 24715264(gen: 8 level: 0) seems good, and it matches
superblock  <<< But its gen is wrong at result output time
=======

[Fix]
Add a new check to make sure the search_extent_cache() is returning the
desired result.

Reported-by: Marc Merlin <marc@merlins.org>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: find-root: Output matched root when searching all roots
Qu Wenruo [Tue, 25 Aug 2015 02:44:56 +0000 (10:44 +0800)]
btrfs-progs: find-root: Output matched root when searching all roots

[Bug]
When given '-a' option, btrfs-find-root will output all possible tree
roots but the exact matched one.

[Reason]
Result printing skipes the exact match one, as it will normally be shown
before the alternative ones.
But when '-a' is given, that's not the case.

[Fix]
Just show the exact match one for search all case.

Reported-by: Marc Merlin <marc@merlins.org>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: unified error handling in print_replace_status
Zhao Lei [Tue, 25 Aug 2015 14:50:08 +0000 (22:50 +0800)]
btrfs-progs: unified error handling in print_replace_status

Current code of print_replace_status() mixed stdout and stderr in error
case, output a error string to stderr without "\n", then output "\n" to
stdout to end the line.

This patch fixed above problem by using unified error handle way for 3
type of errors in print_replace_status().

Also include some small logic cleanup.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Use existing facility to replace read_data_extent function
Qu Wenruo [Fri, 21 Aug 2015 03:21:27 +0000 (11:21 +0800)]
btrfs-progs: Use existing facility to replace read_data_extent function

Function read_data_extent() in btrfs-image.c is using manual-and-read
codes.
Replace it with existing read_extent_data() to reduce duplication.

Also, now we can use other mirror to try our best to do the dump, just
like read_tree_block().

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests, verify that btrfs-image works on a missing device
David Sterba [Tue, 25 Aug 2015 16:38:36 +0000 (18:38 +0200)]
btrfs-progs: tests, verify that btrfs-image works on a missing device

Create a 2 device raid1 filesystem, fill with some data, delete one
device (the device node must not exist) and try to dump a filesystem
image.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests, add more common helpers
David Sterba [Tue, 25 Aug 2015 16:32:41 +0000 (18:32 +0200)]
btrfs-progs: tests, add more common helpers

Add support for failures of commands, log the output.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Avoid reading bad fd in case of missing device.
Qu Wenruo [Fri, 21 Aug 2015 03:21:26 +0000 (11:21 +0800)]
btrfs-progs: Avoid reading bad fd in case of missing device.

Offline btrfs tools, like btrfs-image, will infinitely loop when there
is missing device.

The reason is, for missing device, it's fd will be set to -1, but before
we reading, we only check the fd validation by checking if it's 0.
So in that case, -1 will pass the validation check, and cause pread to
return 0, and loop to read.

Just change the validation check from "== 0" to "<= 0" to avoid such
problem.

Reported-by: Timothy Normand Miller <theosib@gmail.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: add missing free operation of raid_map for raid56
Zhao Lei [Tue, 25 Aug 2015 06:11:07 +0000 (14:11 +0800)]
btrfs-progs: add missing free operation of raid_map for raid56

We forgot free raid_map for raid56's map_bio.
This patch add it.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Add initialztion of rec->crossing_stripes
Zhao Lei [Fri, 21 Aug 2015 13:07:43 +0000 (21:07 +0800)]
btrfs-progs: Add initialztion of rec->crossing_stripes

rec->crossing_stripes is not initialized in allocate place,
and have possibility causing wrong report for normal tree block.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fix memory leaks in error path
Byongho Lee [Fri, 21 Aug 2015 08:51:52 +0000 (17:51 +0900)]
btrfs-progs: fix memory leaks in error path

This patch includes below fixes in error path:
1. fix memory leaks if realloc() fails
2. add missing call free_history() before return error in scrub_read_file()

Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
Reviewed-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Add further checks to btrfs replace start command
Goffredo Baroncelli [Thu, 20 Aug 2015 05:24:23 +0000 (07:24 +0200)]
btrfs-progs: Add further checks to btrfs replace start command

Add further checks to btrfs replace start command.

The following tests where added in user space before calling
the ioctl():
1) check if the new disk is greather or equal to the old one
2) check if the source device is or a block device or a
numerical dev-id

These checks are already performed in kernel space; however
when "btrfs replace start" is ran in background is not possible
to show any error returned by the ioctl(), so in case of fail
the user had to check dmesg to understand the what happened.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: cleanup: remove unnecessary check before btrfs_free_path is called
Tsutomu Itoh [Wed, 19 Aug 2015 08:44:53 +0000 (17:44 +0900)]
btrfs-progs: cleanup: remove unnecessary check before btrfs_free_path is called

We need not check path before btrfs_free_path() is called because
path is checked in btrfs_free_path().

Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: update help for device stats
David Sterba [Tue, 25 Aug 2015 15:30:37 +0000 (17:30 +0200)]
btrfs-progs: update help for device stats

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fsck-tests: add case for inode losing all its extents
Qu Wenruo [Wed, 5 Aug 2015 08:03:14 +0000 (16:03 +0800)]
btrfs-progs: fsck-tests: add case for inode losing all its extents

Add test case for inode with no file extents, but still non-zero size.
To test whether fsck will infinite loop.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fsck: fix a infinite loop on discount file extent repair
Qu Wenruo [Wed, 5 Aug 2015 08:03:13 +0000 (16:03 +0800)]
btrfs-progs: fsck: fix a infinite loop on discount file extent repair

For a special case, discount file extent repair function will cause
infinite loop.

The case is, if the file loses all its extents, we won't have a hole
to fill, causing repair function doing nothing. Since the
I_ERR_DISCOUNT doesn't disappear, fsck will do an infinite loop.

For such case, just puch hole to fill the whole range to fix it.

Reported-by: Robert Munteanu <robert.munteanu@gmail.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fsck: print correct file hole
Qu Wenruo [Wed, 5 Aug 2015 08:03:12 +0000 (16:03 +0800)]
btrfs-progs: fsck: print correct file hole

If a file lost all its extents, fsck will unable to print out the hole.

Add an extra check to print out the hole range.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: do not install static binaries over non-static
David Sterba [Wed, 5 Aug 2015 15:44:48 +0000 (17:44 +0200)]
btrfs-progs: do not install static binaries over non-static

The builds should be able to coexist, so we don't want to forcibly
overwite the standard version.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: compilation errors when using musl libc
Brendan Heading [Wed, 29 Jul 2015 18:37:37 +0000 (19:37 +0100)]
btrfs-progs: compilation errors when using musl libc

- limits.h must be included to pick up PATH_MAX.
- remove double declaration of BTRFS_DISABLE_BACKTRACE

kerncompat.h assumed that if __GLIBC__ was not defined,
it could safely define BTRFS_DISABLE_BACKTRACE, however this can be
defined by the configure script. Added a check to ensure it is not
defined first.

Signed-off-by: Brendan Heading <brendanheading@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Add missing exit for parse_profile function
Qu Wenruo [Wed, 29 Jul 2015 02:28:17 +0000 (10:28 +0800)]
btrfs-progs: Add missing exit for parse_profile function

In parse_profile() function, in error handling route, it output error
message but forgot to exit(1), causing even profile is not valid, it
will just fallback to single.

Reported-by: James Harvey <jamespharvey20@gmail.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: doc: update qgroup docs
David Sterba [Mon, 27 Jul 2015 15:36:03 +0000 (17:36 +0200)]
btrfs-progs: doc: update qgroup docs

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: qgroup assign: add option to schedule rescan
David Sterba [Mon, 27 Jul 2015 14:27:28 +0000 (16:27 +0200)]
btrfs-progs: qgroup assign: add option to schedule rescan

Previous patch detecs inconsistency and unconditionally triggers quota
rescan. This may not be always desired as it's a heavy metadata
operation. In case of batch assignments it's better to trigger the
rescan at the end.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: qgroup assign: set path after checking argument count
David Sterba [Mon, 27 Jul 2015 14:16:52 +0000 (16:16 +0200)]
btrfs-progs: qgroup assign: set path after checking argument count

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Schedule quota rescan if qgroup assign caused inconsistence.
Qu Wenruo [Fri, 27 Feb 2015 08:26:39 +0000 (16:26 +0800)]
btrfs-progs: Schedule quota rescan if qgroup assign caused inconsistence.

NOTE: This patch needs to cooperate with kernel patches, which will fix
a kernel bug that never clear INCONSISTENT bit and return 1 if quota
assign makes qgroup data inconsistent.

Some qgroup assign will cause qgroup data inconsistent, like remove a
qgroup with shared extents from a parent qgroup. But some won't, like
assign a empty(OK, nodesize rfer and exel) to a qgroup.

Newer kernel will return 1 if qgroup data inconsistent and in that case
we should schedule a quota rescan.

This patch will do this in btrfs-progs.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Introduce a misc test for thread conflict in btrfs-convert
Zhao Lei [Mon, 27 Jul 2015 12:24:32 +0000 (20:24 +0800)]
btrfs-progs: Introduce a misc test for thread conflict in btrfs-convert

Current code of btrfs-convert have a bug of thread conflict, which caused
invalid memory accessing between threads, and make program panic.

This patch add a test item for above bug, as:
 # ./misc-tests.sh
    [TEST]   001-btrfstune-features
    [TEST]   002-uuid-rewrite
    [TEST]   003-zero-log
    [TEST]   004-convert-thread-conflict
 failed: btrfs-convert /root/btrfsprogs/tests/test.img
 test failed for case 004-convert-thread-conflict
 #
 # cat misc-tests-results.txt
 ...
 ############### btrfs-convert /root/btrfsprogs/tests/test.img
 trans 7 running 5
 ctree.c:363: btrfs_cow_block: Assertion `1` failed.
 btrfs-convert(btrfs_cow_block+0x92)[0x40acaf]
 btrfs-convert(btrfs_search_slot+0x1cb)[0x40c50f]
 btrfs-convert(btrfs_csum_file_block+0x20f)[0x41d83a]
 btrfs-convert[0x43422d]
 btrfs-convert[0x4342cd]
 btrfs-convert[0x4345ca]
 btrfs-convert[0x434767]
 btrfs-convert[0x435770]
 btrfs-convert[0x439748]
 btrfs-convert(main+0x13f8)[0x43b09d]
 /lib64/libc.so.6(__libc_start_main+0xfd)[0x335e01ecdd]
 btrfs-convert[0x407649]
 create btrfs filesystem:
         blocksize: 4096
         nodesize:  16384
         features:  extref, skinny-metadata (default)
 creating btrfs metadata.

 creating ext2fs image file.
 failed: btrfs-convert /root/btrfsprogs/tests/test.img
 test failed for case 004-convert-thread-conflict
 #

Note that this bug is not happened every time, especilly in slow
device as loop(slow cpu with fast block device is likely to trigger).
I set loop count to 20 to make bug happened in 90% tests.

Suggested-by: David Sterba <dsterba@suse.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: Move code to create loop device to common
Zhao Lei [Mon, 27 Jul 2015 12:24:31 +0000 (20:24 +0800)]
btrfs-progs: tests: Move code to create loop device to common

This code block is used several tests, move it to ./common and add a
helper.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Set info->periodic.timer_fd to 0 in init-fail
Zhao Lei [Mon, 27 Jul 2015 12:24:30 +0000 (20:24 +0800)]
btrfs-progs: Set info->periodic.timer_fd to 0 in init-fail

In current code, (info->periodic.timer_fd == 0) means it is not
valid, as:
  if (info->periodic.timer_fd) {
      close(info->periodic.timer_fd);
      ...

We need set its value from -1 to 0 in create-fail case, to make
code like above works.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: resst info->periodic.timer_fd's value after free
Zhao Lei [Mon, 27 Jul 2015 12:24:29 +0000 (20:24 +0800)]
btrfs-progs: resst info->periodic.timer_fd's value after free

task_period_stop() is used to close a timer explicitly, to avoid
the timer handle closed again by task_stop(), we should reset its
value after close.

Also add value-reset for info->id for safe.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Remove cleanup-timer code for btrfs-convert
Zhao Lei [Mon, 27 Jul 2015 12:24:28 +0000 (20:24 +0800)]
btrfs-progs: Remove cleanup-timer code for btrfs-convert

No need to close timer handle afain in subthread-close-callback,
it is closed by task_stop() automatically.

This patch also add a fflush() to make log output on time.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Move close timer handle code to line after sub process exit
Zhao Lei [Mon, 27 Jul 2015 12:24:27 +0000 (20:24 +0800)]
btrfs-progs: Move close timer handle code to line after sub process exit

The timer handle have possibility in using by sub thread,
better to close it after sub process exit.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Fix wrong address accessing by subthread in btrfs-convert
Zhao Lei [Mon, 27 Jul 2015 12:24:26 +0000 (20:24 +0800)]
btrfs-progs: Fix wrong address accessing by subthread in btrfs-convert

btrfs-convert sometimes show 'Assertion failed' in converting a nearly blank
file system, as:
  create btrfs filesystem:
          blocksize: 4096
          nodesize:  16384
          features:  extref, skinny-metadata (default)
  creating btrfs metadata.

  creating ext2fs image file.
  trans 7 running 5
  ctree.c:363: btrfs_cow_block: Assertion `1` failed.
  btrfs-convert(btrfs_cow_block+0x92)[0x40acaf]
  btrfs-convert(btrfs_search_slot+0x1cb)[0x40c50f]
  btrfs-convert(btrfs_csum_file_block+0x20f)[0x41d83a]
  btrfs-convert[0x43422d]
  btrfs-convert[0x4342cd]
  btrfs-convert[0x4345ca]
  btrfs-convert[0x434767]
  btrfs-convert[0x435770]
  btrfs-convert[0x439748]
  btrfs-convert(main+0x13f8)[0x43b09d]
  /lib64/libc.so.6(__libc_start_main+0xfd)[0x335e01ecdd]
  btrfs-convert[0x407649]

Reason is complex:
1: main thread allocated a block of memory,
   shared with sub thread
2: main thread killed sub thread, and free above memory
3: main thread malloc a new one(in same address),
   and use it
4: sub thread(which is not really quit), write into
   this address, and caused this bug.

By adding some debug lines into code, we can see following output:
  create btrfs filesystem:
          blocksize: 4096
          nodesize:  16384
          features:  extref, skinny-metadata (default)
  creating btrfs metadata.
  1:  ctx(0x7ffe1abde230)->info=0xc65b80
  2:  task_period_start: will create periodic.timer_fd
  3:  task_stop: info->periodic.timer_fd = NULL
  4:  task_stop: begin pthread_cancel info->id=-1746053376
  5:  task_stop: done pthread_cancel ret=0
  6:  task_stop: begin info->postfn
  7:  task_period_stop: periodic.timer_fd NULL
  8:  task_stop: done info->postfn
  9:  task_stop: done all
  10: creating ext2fs image file.
  trans 7 running 5
  11: task_period_start: create periodic.timer_fd done info->periodic.timer_fd(0xc65b80)=7
  12: btrfs_cow_block: root->fs_info->generation(0xc63568) = 5 trans->transid(0xc65b80)=7
  13: ctree.c:368: btrfs_cow_block: Assertion `1` failed.
  ./btrfs-convert(btrfs_cow_block+0xda)[0x40ad37]
  ./btrfs-convert(btrfs_search_slot+0x1cb)[0x40c5b4]
  ./btrfs-convert(btrfs_insert_empty_items+0xac)[0x40d9f6]
  ./btrfs-convert(btrfs_record_file_extent+0xc0)[0x4183fe]
  ./btrfs-convert[0x435796]
  ./btrfs-convert[0x439b0c]
  ./btrfs-convert(main+0x13f8)[0x43b45d]
  /lib64/libc.so.6(__libc_start_main+0xfd)[0x335e01ecdd]
  ./btrfs-convert[0x407689]
  Conclusion:
  a: subthread should exit before step 5, but it is still running
     in step 11
  b: task_stop() hadn't close periodic.timer_fd in step3,
     because periodic.timer_fd is not initialized yet.
  c. address of 0xc65b80 is overwrited by subthread in step 11,
     but this address is freed and re-malloc by main thread
     before step 10, and used for trans->transid.
  d: trans->transid which is overwrite by subthread caused error
     in step 13.

Fix:
  pthread_cancel() only send a cancellation request to the thread,
  thread will quit in next cancellation point by default.
  To make sub thread quit in time, this patch add pthread_join()
  after pthread_cancel() call.
  And to make pthread_join() works, pthread_detach() is removed.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: Add -o loop to fsck-tests/012-leaf-corruption
Zhao Lei [Mon, 27 Jul 2015 13:01:50 +0000 (21:01 +0800)]
btrfs-progs: tests: Add -o loop to fsck-tests/012-leaf-corruption

To avoid following mount error in test:
  mount: /root/btrfs/progs/tests/fsck-tests/012-leaf-corruption/test.img
  is not a block device (maybe try `-o loop'?)

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: convert: Avoid allocating metadata extent crossing stripe boundary
Qu Wenruo [Thu, 23 Jul 2015 09:18:10 +0000 (17:18 +0800)]
btrfs-progs: convert: Avoid allocating metadata extent crossing stripe boundary

As convert implement its own alloc extent, avoid such metadata problem
too.

Reported-by: Chris Murphy <lists@colorremedies.com>
Reported-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: extent-tree: avoid allocating tree block that crosses stripe boundary
Qu Wenruo [Thu, 23 Jul 2015 09:18:09 +0000 (17:18 +0800)]
btrfs-progs: extent-tree: avoid allocating tree block that crosses stripe boundary

Now find_free_extent() function won't return a metadata extent that
crosses stripe boundary.

Reported-by: Chris Murphy <lists@colorremedies.com>
Reported-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fsck: Check if a metadata tree block crossing stripe boundary
Qu Wenruo [Thu, 23 Jul 2015 09:18:08 +0000 (17:18 +0800)]
btrfs-progs: fsck: Check if a metadata tree block crossing stripe boundary

Kernel btrfs_map_block() function has a limitation that it can only
map BTRFS_STRIPE_LEN size.
That will cause scrub fails to scrub tree block which crosses strip
boundary, causing BUG_ON().

Normally, it's OK as metadata is always in metadata chunk and
BTRFS_STRIPE_LEN can always be divided by node/leaf size.
So without mixed block group, tree block won't cross stripe boundary.

But for mixed block group, especially for btrfs converted from ext4,
it's almost sure one or more tree blocks are not aligned with node size
and cross stripe boundary.
Causing bug with kernel scrub.

This patch will report the problem, although we don't have a good idea
how to fix it in user space until we add the ability to relocate tree
block in user space.

Also, kernel code should also be checked for such tree block alloc
problems.

Reported-by: Chris Murphy <lists@colorremedies.com>
Reported-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: print-tree: print stripe len of a chunk
Qu Wenruo [Thu, 23 Jul 2015 09:18:07 +0000 (17:18 +0800)]
btrfs-progs: print-tree: print stripe len of a chunk

Although it is fixed to BTRFS_STRIPE_LEN(64K) now, it's still used in a
lot of code, just output it for user who wants to trace the source of
stripe_len in btrfs_map_bio() code.

Reported-by: Chris Murphy <lists@colorremedies.com>
Reported-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: Accurate errormsg for resize operation on no-enouth-free-space case
Zhao Lei [Wed, 22 Jul 2015 07:58:06 +0000 (15:58 +0800)]
btrfs-progs: Accurate errormsg for resize operation on no-enouth-free-space case

btrfs progs output following error message when doing resize on
no-enouth-free-space case:
 # btrfs filesystem resize +10g /mnt/btrfs_5gb
 Resize '/mnt/btrfs_5gb' of '+10g'
 ERROR: unable to resize '/mnt/btrfs_5gb' - File too large
 #

It is not a good description for users, and this patch changed it to:
 # ./btrfs filesystem resize +10G /mnt/tmp1
 Resize '/mnt/tmp1' of '+10G'
 ERROR: unable to resize '/mnt/tmp1' - no enouth free space
 #

Reported-by: Taeha Kim <kthguru@gmail.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: defrag: remove unused variable
Patrik Lundquist [Fri, 24 Jul 2015 10:21:02 +0000 (12:21 +0200)]
btrfs-progs: defrag: remove unused variable

A leftover from when recursive defrag was added.

Signed-off-by: Patrik Lundquist <patrik.lundquist@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: defrag: fix threshold overflow again
Patrik Lundquist [Fri, 24 Jul 2015 08:35:02 +0000 (10:35 +0200)]
btrfs-progs: defrag: fix threshold overflow again

Commit dedb1ebeee847e3c4d71e14d0c1077887630e44a broke commit
96cfbbf0ea9fce7ecaa9e03964474f407f6e76ab.

Casting thresh value greater than (u32)-1 simply truncates bits while
desired value is (u32)-1 for max defrag threshold.

I.e. "btrfs fi defrag -t 4g" is trimmed/truncated to 0
and "-t 5g" to 1073741824.

Also added a missing newline.

Signed-off-by: Patrik Lundquist <patrik.lundquist@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: mkfs: catch errors after transaction start
David Sterba [Fri, 10 Jul 2015 22:18:21 +0000 (00:18 +0200)]
btrfs-progs: mkfs: catch errors after transaction start

Replace missing or BUG_ON in main().

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: support testname glob
David Sterba [Fri, 10 Jul 2015 22:38:07 +0000 (00:38 +0200)]
btrfs-progs: tests: support testname glob

To run a given test set the variable TEST like

 $ make test TEST=002-bad-transid
 $ make test TEST=002-*

and only tests matching the value will be run. The pattern is glob and
pased to 'find -name'.

The convert tests do not follow the fsck and misc layout and are skipped
if TEST is set.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fsck tests: move code to a function
David Sterba [Fri, 10 Jul 2015 22:31:02 +0000 (00:31 +0200)]
btrfs-progs: fsck tests: move code to a function

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: prop: use macro terminator for command list
David Sterba [Fri, 10 Jul 2015 22:07:05 +0000 (00:07 +0200)]
btrfs-progs: prop: use macro terminator for command list

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: unify naming of command handlers
David Sterba [Fri, 10 Jul 2015 22:05:05 +0000 (00:05 +0200)]
btrfs-progs: unify naming of command handlers

Use cmd_ + group + command schema.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: inspect: add command min-dev-size
David Sterba [Mon, 20 Jul 2015 15:31:43 +0000 (17:31 +0200)]
btrfs-progs: inspect: add command min-dev-size

Previously in 'filesystem resize get_min_size', now
'inspect-internal min-dev-size'. We'd like to avoid cluttering the
'resize' syntax further.

The test has been updated to exercise the new option.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: move min-resize implementation to inspect-internal
David Sterba [Mon, 20 Jul 2015 15:29:24 +0000 (17:29 +0200)]
btrfs-progs: move min-resize implementation to inspect-internal

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agoBtrfs-progs: add feature to get mininum size for resizing a fs/device
Filipe Manana [Thu, 16 Jul 2015 15:47:13 +0000 (16:47 +0100)]
Btrfs-progs: add feature to get mininum size for resizing a fs/device

Currently there is not way for a user to know what is the minimum size a
device of a btrfs filesystem can be resized to. Sometimes the value of
total allocated space (sum of all allocated chunks/device extents), which
can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage',
works as the minimum size, but sometimes it does not, namely when device
extents have to relocated to holes (unallocated space) within the new
size of the device (the total allocated space sum).

This change adds the ability to reliably compute such minimum value and
extents 'btrfs filesystem resize' with the following syntax to get such
value:

   btrfs filesystem resize [devid:]get_min_size

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: utils: missing newline in error messages when checking dup
David Sterba [Tue, 14 Jul 2015 16:51:52 +0000 (18:51 +0200)]
btrfs-progs: utils: missing newline in error messages when checking dup

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: mkfs: Cleanup temporary chunk to avoid strange balance behavior.
Qu Wenruo [Tue, 7 Jul 2015 08:15:28 +0000 (16:15 +0800)]
btrfs-progs: mkfs: Cleanup temporary chunk to avoid strange balance behavior.

[BUG]
 # mkfs.btrfs /dev/sdb /dev/sdd -m raid0 -d raid0
 # mount /dev/sdb /mnt/btrfs
 # btrfs balance start /mnt/btrfs
 # btrfs fi df /mnt/btrfs
 Data, single: total=1.00GiB, used=320.00KiB
 System, single: total=32.00MiB, used=16.00KiB
 Metadata, RAID0: total=256.00MiB, used=112.00KiB
 GlobalReserve, single: total=16.00MiB, used=0.00B

Only metadata stay RAID0. Data and system goes from RAID0 to single.

[REASON]
The problem is caused by the temporary single chunk.
In mkfs, it will always create single data/metadata/sys chunk and them
add device into the temporary btrfs.

When doing all chunk balance, for data and syschunk, they are almost
empty, so balance will move them into the single chunk and remove the
old RAID0 chunk.
For metadata, it has more data and will kick the metadata chunk pre
alloc, so new RAID0 chunk is allocated and the old metadata is move
there. Old RAID0 and single chunks are removed.

[FIX]
Now we add a new function to cleanup the temporary chunks at the end of
mkfs routine.
It will cleanup the chunks which is empty and its profile differs from
the mkfs profile.
So in balance, btrfs will always alloc a new chunk to keep the profile,
other than moving data into the single chunk.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agoDocumentation: update btrfs-replace manual to support RAID5/6
Wang Yanfeng [Tue, 7 Jul 2015 02:12:16 +0000 (10:12 +0800)]
Documentation: update btrfs-replace manual to support RAID5/6

Man manual need to be updated since RAID5/6 has been supported
by btrfs-replace.

Signed-off-by: Wang Yanfeng <wangyf-fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agoBtrfs progs v4.1.2
David Sterba [Tue, 14 Jul 2015 11:45:36 +0000 (13:45 +0200)]
Btrfs progs v4.1.2

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agoRevert "btrfs-progs: mkfs: create only desired block groups for single device"
Qu Wenruo [Tue, 14 Jul 2015 02:13:01 +0000 (10:13 +0800)]
Revert "btrfs-progs: mkfs: create only desired block groups for single device"

This reverts commit 5f8232e5c8f0b0de0ef426274911385b0e877392.

This commit causes a regression:

$ mkfs.btrfs -f /dev/sda6
$ btrfsck /dev/sda6
Checking filesystem on /dev/sda6
UUID: 2ebb483c-1986-4610-802a-c6f3e6ab4b76
checking extents
Chunk[256, 228, 0]: length(4194304), offset(0), type(2) mismatch with
block group[0, 192, 4194304]: offset(4194304), objectid(0), flags(34)
Chunk[256, 228, 4194304]: length(8388608), offset(4194304), type(4)
mismatch with block group[4194304, 192, 8388608]: offset(8388608),
objectid(4194304), flags(36)
Block group[0, 4194304] (flags = 34) didn't find the relative chunk.
Block group[4194304, 8388608] (flags = 36) didn't find the relative
chunk.
......

The commit has the following bug causing the problem.
1) Typo forgets to add meta/data_profile for alloc_chunk.
Only meta/data_profile is added to allocate a block group, but not
chunk.

2) Type for the first system chunk is impossible to modify yet.
The type for the first chunk and its stripe is hard coded into
make_btrfs() function.
So even we try to modify the type of the block group, we are unable to
change the type of the first chunk.
Causing the chunk type mismatch problem.

The 1st bug can be fixed quite easily but the second is not.
The good news is, the last patch "btrfs-progs: mkfs: Cleanup temporary
chunk to avoid strange balance behavior." from my patchset can handle it
quite well alone.

So just revert the patch.
New bug fix for btrfsck(err is 0 even chunk/extent tree is corrupted) and
new test cases for mkfs will follow soon.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agoBtrfs progs v4.1.1
David Sterba [Fri, 10 Jul 2015 13:27:49 +0000 (15:27 +0200)]
Btrfs progs v4.1.1

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: extent-tree: Introduce btrfs_free_block_group function
Qu Wenruo [Tue, 7 Jul 2015 08:15:27 +0000 (16:15 +0800)]
btrfs-progs: extent-tree: Introduce btrfs_free_block_group function

This function will be used to free a empty chunk.

This provides the basis for later temp chunk cleanup.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: extent-tree: Introduce functions to free in-memory block group cache
Qu Wenruo [Tue, 7 Jul 2015 08:15:26 +0000 (16:15 +0800)]
btrfs-progs: extent-tree: Introduce functions to free in-memory block group cache

Introduce two functions, free_space_info and free_block_group_cache.

The former will free the space of a empty block group.
The latter will free the in memory block group cache along with its
space in space_info and device space.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: extent-tree: Introduce functions to free chunk items
Qu Wenruo [Tue, 7 Jul 2015 08:15:25 +0000 (16:15 +0800)]
btrfs-progs: extent-tree: Introduce functions to free chunk items

Introduce two functions, free_chunk_item and free_system_chunk_item.
First one will free chunk item in chunk tree.
The latter one will free a system chunk in super block.

They are used for later chunk/block group free function.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: extent-tree: Introduce functions to free dev extents in a chunk
Qu Wenruo [Tue, 7 Jul 2015 08:15:24 +0000 (16:15 +0800)]
btrfs-progs: extent-tree: Introduce functions to free dev extents in a chunk

Introduce two functions, free_dev_extent_item and
free_chunk_dev_extent_items, to free dev extent items in a chunk.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: extent-tree: Introduce free_block_group_item function
Qu Wenruo [Tue, 7 Jul 2015 08:15:23 +0000 (16:15 +0800)]
btrfs-progs: extent-tree: Introduce free_block_group_item function

This function is used to free a block group item.  It must be called
with all the space in the block group pinned.  Or there is a possibility
that tree blocks be allocated into the range.

The function is used for later block group/chunk free function.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: disk-io: Support commit transaction on chunk tree
Qu Wenruo [Tue, 7 Jul 2015 08:15:22 +0000 (16:15 +0800)]
btrfs-progs: disk-io: Support commit transaction on chunk tree

As chunk tree is only stored in super block, chunk tree commit doesn't
need to go through tree root update.
Or a BUG_ON will be triggered.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: let corrupt-block kill nbytes
David Sterba [Fri, 3 Jul 2015 16:07:17 +0000 (18:07 +0200)]
btrfs-progs: let corrupt-block kill nbytes

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: tests: Add test case for I_ERR_FILE_WRONG_NBYTES repair
Qu Wenruo [Thu, 2 Jul 2015 06:36:04 +0000 (14:36 +0800)]
btrfs-progs: tests: Add test case for I_ERR_FILE_WRONG_NBYTES repair

Add a new test case for I_ERR_FILE_WRONG_NBYTES.
The new btrfs-image dump image contains a file in 12K size.
But nbytes in its inode item is a random number.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
9 years agobtrfs-progs: fsck: Add repair function for I_ERR_FILE_WRONG_NBYTES
Qu Wenruo [Fri, 19 Jun 2015 03:43:40 +0000 (11:43 +0800)]
btrfs-progs: fsck: Add repair function for I_ERR_FILE_WRONG_NBYTES

Some unknown kernel bug makes inode nbytes modification out of sync with
file extent update.

But it's quite easy to fix in btrfs-progs anyway.

So just fix it by adding a new function repair_inode_nbytes by using the
found_size in inode_record.

Reported-by: Christian <cdysthe@gmail.com>
Reported-by: Chris Murphy <lists@colorremedies.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: mkfs: create only desired block groups for single device
David Sterba [Thu, 2 Jul 2015 22:15:18 +0000 (00:15 +0200)]
btrfs-progs: mkfs: create only desired block groups for single device

The filesystem creation has to solve some chicken-egg problems and
creates some temporary objects. In our case it's an extra single/single
pair of block groups that's not used unless the user asks that
explicitly.

Example:

Data, single: total=8.00MiB, used=64.00KiB
System, DUP: total=8.00MiB, used=16.00KiB
System, single: total=4.00MiB, used=0.00B
Metadata, DUP: total=153.56MiB, used=112.00KiB
Metadata, single: total=8.00MiB, used=0.00B
GlobalReserve, single: total=16.00MiB, used=0.00B

Even with a single device filesystem and defaults, there's single
block group for metadata and system. The single device case is easy to
fix, we'll simply create the right type from the beginning.

Example:

Data, single: total=8.00MiB, used=64.00KiB
System, DUP: total=4.00MiB, used=16.00KiB
Metadata, DUP: total=136.00MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B

Filesystem on top of multiple devices still leaves the single/single
groups behind.

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: drop unused argument from create_raid_groups
David Sterba [Thu, 2 Jul 2015 17:23:27 +0000 (19:23 +0200)]
btrfs-progs: drop unused argument from create_raid_groups

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: split data block group creation out of make_root_dir
David Sterba [Wed, 1 Jul 2015 17:19:05 +0000 (19:19 +0200)]
btrfs-progs: split data block group creation out of make_root_dir

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: move transaction start/commit out of make_root_dir
David Sterba [Wed, 1 Jul 2015 17:15:35 +0000 (19:15 +0200)]
btrfs-progs: move transaction start/commit out of make_root_dir

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