platform/upstream/btrfs-progs.git
9 years agobtrfs-progs: use common warning/error for cmds-scrub.c
Zhao Lei [Mon, 28 Sep 2015 13:58:14 +0000 (21:58 +0800)]
btrfs-progs: use common warning/error for cmds-scrub.c

Use common warning/error functions in cmds-scrub.c, it can make
message format unified and make code simple.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[removed ending newlines from messages]
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: Introduce warning and error for common use
Zhao Lei [Mon, 28 Sep 2015 13:58:13 +0000 (21:58 +0800)]
btrfs-progs: Introduce warning and error for common use

Current code use fprintf(stderr, "...") to output warnning and
error information.

The error message have different style, as:
 # grep fprintf *.c
 fprintf(stderr, "Open ctree failed\n");
 fprintf(stderr, "%s: open ctree failed\n", __func__);
 fprintf(stderr, "ERROR: cannot open ctree\n");
 ...

And sometimes, we forgot add tailed '\n', or use printf instead,
as in current code:
 printf("warning, device %llu is missing\n",

This patch introduce warning() and error() as common function,
to make:
1: Each warning and error information have same format
2: Easy to search/change all error message
3: Easy to modify function's internal for debug or other requirement,
   for example:
   print function/linenumber in error()
   dumpstack in error()
   add some trace for some style of message
   add support for -v, -vv, ...
   support for locales
   custom output functions
   support some special device/tty

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[print newline after the message]
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: check: add progress indicator
Silvio Fricke [Thu, 24 Sep 2015 06:13:05 +0000 (08:13 +0200)]
btrfs-progs: check: add progress indicator

Signed-off-by: Silvio Fricke <silvio.fricke@gmail.com>
[minor updates]
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: add more superblock validation checks
Qu Wenruo [Wed, 13 May 2015 09:15:35 +0000 (17:15 +0800)]
btrfs-progs: add more superblock validation checks

Now btrfs-progs will have much more strict superblock checks based on
kernel superblock checks.

This should prevent crashes or invalid memory access on crafted or
fuzzed images.

Based on kernel commit c926093ec516f5d316ecdf8c1be11f577ac71b85 .

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[added reference to kernel and comments]
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: Read the whole superblock instead of struct btrfs_super_block
Qu Wenruo [Wed, 13 May 2015 09:15:34 +0000 (17:15 +0800)]
btrfs-progs: Read the whole superblock instead of struct btrfs_super_block

Before the patch, btrfs-progs will only read sizeof(struct
btrfs_super_block) and restore it into super_copy.

This makes checksum check for superblock impossible.  Change it to read
the whole superblock.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: enhance error message with status from kernel and errno
Zhao Lei [Thu, 6 Aug 2015 03:05:55 +0000 (11:05 +0800)]
btrfs-progs: enhance error message with status from kernel and errno

Scrub output prints the following error message in my test:
  ERROR: scrubbing /var/ltf/tester/scratch_mnt failed for device id 5 (Success)

It is caused by a broken kernel and fs, but the we need to avoid
printing both "error and success" on one line as above.

This patch modified above message to:
  ERROR: scrubbing /var/ltf/tester/scratch_mnt failed for device id 5: ret=1, errno=0 (Success)

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[minor updates in formatting]
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: use switch instead of a series of ifs for output error message
Zhao Lei [Thu, 6 Aug 2015 03:05:54 +0000 (11:05 +0800)]
btrfs-progs: use switch instead of a series of ifs for output error message

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: tests: Avoid printing useless warning in fsck tests
Zhao Lei [Tue, 28 Jul 2015 02:28:13 +0000 (10:28 +0800)]
btrfs-progs: tests: Avoid printing useless warning in fsck tests

002-bad-transid outout 'transid verify failed' message in screen
which is just a warning in btrfs-image in normal condition of this
test.

This patch move above warning into $RESULTS, to:
1: Avoid trouble screen output
2: Let user known detail if other error happened in btrfs-image

Before patch:
  # ./fsck-tests.sh
    [TEST]   001-bad-file-extent-bytenr
    [TEST]   002-bad-transid
  parent transid verify failed on 29360128 wanted 9 found 755944791
  parent transid verify failed on 29360128 wanted 9 found 755944791
  Ignoring transid failure
    [TEST]   003-shift-offsets
    [TEST]   004-no-dir-index
  ...

After patch:
  # ./fsck-tests.sh
  [TEST]   001-bad-file-extent-bytenr
  [TEST]   002-bad-transid
  [TEST]   003-shift-offsets
  [TEST]   004-no-dir-index
  ...

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: fsck: Add check for extent and parent chunk type
Qu Wenruo [Thu, 10 Sep 2015 02:34:14 +0000 (10:34 +0800)]
btrfs-progs: fsck: Add check for extent and parent chunk type

There is a bug in btrfs-convert in 4.1.2, even we don't allow mixed
block group for converted image, btrfs-convert will still create image
with data and metadata inside one chunk.

And further more, the chunk type is still DATA or METADATA, not
DATA|METADATA (not mixed).

So add btrfsck check for it right now.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: tests: Introduce misc-tests/008-leaf-crossing-stripes
Zhao Lei [Wed, 23 Sep 2015 07:19:06 +0000 (15:19 +0800)]
btrfs-progs: tests: Introduce misc-tests/008-leaf-crossing-stripes

To check is btrfs-convert create bad filesystem with
leaf across stripes.

It is happened in progs version <=v4.1.2, and fixed by patch titled:
btrfs: convert: Avoid allocating metadata extent crossing stripe boundary
which was merged in v4.2.

Notice thar this testcase can not report error in old version of
btrfs-progs, because "btrfs check" can't check this type of error
in those version, but we have another testcase in fsck-tests, to
check is "btrfs check" support this check.

So, the above 2 testcase together can check leaf-crossing-stripes
bug in all versions.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[renamed and other minor changes]
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: tests: Introduce fsck-tests/018-leaf-crossing-stripes
Zhao Lei [Wed, 23 Sep 2015 07:19:05 +0000 (15:19 +0800)]
btrfs-progs: tests: Introduce fsck-tests/018-leaf-crossing-stripes

To test if fsck can check detec "leaf crossing stripes".

This function was introduced from patch titled:
btrfs-progs: fsck: Check if a metadata tree block crossing stripe boundary

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[renamed and other minor changes]
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: tests: Move extract_image out of check_all_images for common use
Zhao Lei [Wed, 23 Sep 2015 07:19:04 +0000 (15:19 +0800)]
btrfs-progs: tests: Move extract_image out of check_all_images for common use

Move code for extract image file to a function from check_all_images()
for common use, so caller can use this function to extrace single
image file.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[minor reformatting and updates]
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agoBtrfs progs v4.2.3
David Sterba [Mon, 19 Oct 2015 09:29:01 +0000 (11:29 +0200)]
Btrfs progs v4.2.3

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: restore: fix off-by-one len check
Vincent Stehlé [Thu, 8 Oct 2015 08:47:09 +0000 (10:47 +0200)]
btrfs-progs: restore: fix off-by-one len check

Fix a check of len versus PATH_MAX in function copy_symlink(), to
account for the terminating null byte.

Resolves-Coverity-CID: 1296749
Signed-off-by: Vincent Stehlé <vincent.stehle@freescale.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: subvol sync: fix reversed condition
David Sterba [Tue, 6 Oct 2015 09:32:16 +0000 (11:32 +0200)]
btrfs-progs: subvol sync: fix reversed condition

In my local change to 07cc891d1d9819d5cf0628af555e7727d289cf7b
("btrfs-progs: Simplify all-subvolumn-clean condition for
wait_for_subvolume_cleaning") that reversed the meaning of the variable
dirty -> clean, I made a mistake and broke 'subvol sync' that will not
wait as expected and ends prematurely. Zhao Lei's original patch worked.

CC: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: convert: Print different error message if convert partly failed.
Qu Wenruo [Tue, 9 Jun 2015 07:57:40 +0000 (15:57 +0800)]
btrfs-progs: convert: Print different error message if convert partly failed.

When testing under libguestfs, btrfs-convert will never succeed to fix
chunk map, and always fails.

But in that case, it's already a mountable btrfs.
So better to info user with different error message for that case.

The root cause of it is still under investigation.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: optimize not to scan repeated fsid mount points
Anand Jain [Tue, 15 Sep 2015 08:46:23 +0000 (16:46 +0800)]
btrfs-progs: optimize not to scan repeated fsid mount points

fsid can be mounted multiple times, with different subvolid.
And we don't have to scan a mount point if we already have
that in the scanned list.

And thus nicely avoids the following warning with multiple
subvol mounts on older kernel like 2.6.32 where
BTRFS_IOC_GET_FSLABEL ioctl does not exist.

./btrfs fi show -m
Label: none  uuid: 31845933-611e-422d-ae6f-386e57ad81aa
Total devices 2 FS bytes used 172.00KiB
devid    1 size 3.00GiB used 642.38MiB path /dev/sdd
devid    2 size 3.00GiB used 622.38MiB path /dev/sde

warning, device 2 is missing
warning devid 2 not found already
warning, device 2 is missing
warning devid 2 not found already

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agoBtrfs progs v4.2.2
David Sterba [Mon, 5 Oct 2015 10:58:03 +0000 (12:58 +0200)]
Btrfs progs v4.2.2

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: fix error checking in load_device_info
David Sterba [Fri, 2 Oct 2015 15:44:21 +0000 (17:44 +0200)]
btrfs-progs: fix error checking in load_device_info

load_device_info queries the FS_INFO ioctl and this may fail with EPERM
on older kernels. The check did not verify the ioctl return value and
incorrectly returned EPERM if it was previously stored in errno.

This fixes 'btrfs fi usage' that will print the overall summary for all
users (provided that the FS_INFO ioctl is already unprivileged).

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: fix double free during scanning
David Sterba [Fri, 2 Oct 2015 15:21:12 +0000 (17:21 +0200)]
btrfs-progs: fix double free during scanning

If there are different devices mounted to the same directory we can run
into double free issue in the scanning code and this can lead to a
crash. The dev_info_arg buffer allocation get_fs_info might be skipped,
eg. if the FS_INFO ioctl fails due to EPERM in older kernels. Reset the
pointer before each loop starts.

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: check: fix memset range
Silvio Fricke [Tue, 29 Sep 2015 17:10:46 +0000 (19:10 +0200)]
btrfs-progs: check: fix memset range

Signed-off-by: Silvio Fricke <silvio.fricke@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: tasks info->id is a pthread_t and should not set to -1
Silvio Fricke [Thu, 24 Sep 2015 06:13:04 +0000 (08:13 +0200)]
btrfs-progs: tasks info->id is a pthread_t and should not set to -1

Signed-off-by: Silvio Fricke <silvio.fricke@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: Show detail error message when write sb failed in write_dev_supers()
Zhao Lei [Mon, 27 Jul 2015 11:32:37 +0000 (19:32 +0800)]
btrfs-progs: Show detail error message when write sb failed in write_dev_supers()

fsck-tests.sh failed and show following message in my node:
  # ./fsck-tests.sh
     [TEST]   001-bad-file-extent-bytenr
  disk-io.c:1444: write_dev_supers: Assertion `ret != BTRFS_SUPER_INFO_SIZE` failed.
  /root/btrfsprogs/btrfs-image(write_all_supers+0x2d2)[0x41031c]
  /root/btrfsprogs/btrfs-image(write_ctree_super+0xc5)[0x41042e]
  /root/btrfsprogs/btrfs-image(btrfs_commit_transaction+0x208)[0x410976]
  /root/btrfsprogs/btrfs-image[0x438780]
  /root/btrfsprogs/btrfs-image(main+0x3d5)[0x438c5c]
  /lib64/libc.so.6(__libc_start_main+0xfd)[0x335e01ecdd]
  /root/btrfsprogs/btrfs-image[0x4074e9]
  failed to restore image /root/btrfsprogs/tests/fsck-tests/001-bad-file-extent-bytenr/default_case.img
  #

  # cat fsck-tests-results.txt
  === Entering /root/btrfsprogs/tests/fsck-tests/001-bad-file-extent-bytenr
  restoring image default_case.img
  failed to restore image /root/btrfsprogs/tests/fsck-tests/001-bad-file-extent-bytenr/default_case.img
  #

Reason:
  I run above test in a NFS mountpoint, it don't have enouth space to write
  all superblock to image file, and don't support sparse file.
  So write_dev_supers() failed in writing sb and output above message.

It takes me quite of time to know what happened, we can save these time
by output exact information in write-sb-fail case.

After patch:
  # ./fsck-tests.sh
    [TEST]   001-bad-file-extent-bytenr
  WARNING: Write sb failed: File too large
  disk-io.c:1492: write_all_supers: Assertion `ret` failed.
  ...
  #

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: Increase running state's priority in stat output
Zhao Lei [Tue, 28 Jul 2015 07:53:58 +0000 (15:53 +0800)]
btrfs-progs: Increase running state's priority in stat output

Anthony Plack <anthony@plack.net> reported a output bug in maillist:
  title: btrfs-progs SCRUB reporting aborted but still running - minor

btrfs scrub status report it was aborted but still runs to completion.
  # btrfs scrub status /mnt/data
  scrub status for f591ac13-1a69-476d-bd30-346f87a491da
scrub started at Mon Apr 27 06:48:44 2015 and was aborted after 1089 seconds
total bytes scrubbed: 1.02TiB with 0 errors
  #
  # btrfs scrub status /mnt/data
  scrub status for f591ac13-1a69-476d-bd30-346f87a491da
scrub started at Mon Apr 27 06:48:44 2015 and was aborted after 1664 seconds
total bytes scrubbed: 1.53TiB with 0 errors
  #
  ...

Reason:
  When scrub multi-device simultaneously, if some device canceled,
  and some device is still running, cancel state have higher priority to
  be outputed in global report.
  So we can see "scrub aborted" in status line, with running-time keeps
  increased.

Fix:
  We can increase running state's priority in output, if there is
  some device in scrub state, we output running state instead of
  cancelled state.

Reported-by: Anthony Plack <anthony@plack.net>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: fix is_block_device() return checks
Anand Jain [Fri, 28 Aug 2015 14:11:30 +0000 (22:11 +0800)]
btrfs-progs: fix is_block_device() return checks

it was highlighted to me is_block_device(), returns
 1 if the file is a block device,
 < 0 in case of an error (eg: file not found)
 0 otherwise

This patch makes proper return checks at all the places
where is_block_device() is used. Thanks to Goffredo.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Suggested-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: make sure that is_block_device will return only 0/1
David Sterba [Fri, 25 Sep 2015 15:59:43 +0000 (17:59 +0200)]
btrfs-progs: make sure that is_block_device will return only 0/1

The macro return nonzero, we'll test against 0/1 (or < 0).

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: utils: Check nodesize against features
Qu Wenruo [Fri, 25 Sep 2015 16:15:44 +0000 (18:15 +0200)]
btrfs-progs: utils: Check nodesize against features

Check nodesize against features, not only sectorsize.
In fact, one of the btrfs-convert and mkfs differs in the nodesize
check.

This patch also provides the basis for later btrfs-convert fix.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: provide fail safe for BTRFS_IOC_GET_FSLABEL ioctl
Anand Jain [Tue, 15 Sep 2015 08:46:22 +0000 (16:46 +0800)]
btrfs-progs: provide fail safe for BTRFS_IOC_GET_FSLABEL ioctl

Old kernels before 3.9 do not provide ioctl BTRFS_IOC_GET_FSLABEL.
So we need to provide a fail safe logic for btrfs-progs running
on those kernel.

In this patch when get_label_mounted() fails on the old kernel
it will fail back to the old method and uses get_label_unmounted(),
where it will read from the disk directly.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agoBtrfs progs v4.2.1
David Sterba [Sun, 20 Sep 2015 12:50:06 +0000 (14:50 +0200)]
Btrfs progs v4.2.1

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: tests: Add '-o loop' to mount command line in convert-tests.sh
Zhao Lei [Tue, 15 Sep 2015 09:22:17 +0000 (17:22 +0800)]
btrfs-progs: tests: Add '-o loop' to mount command line in convert-tests.sh

To fix following bug:
 # ./convert-tests.sh
     [TEST]   ext2 4k nodesize, btrfs defaults
 failed: mount /root/btrfsprogs/tests/test.img /root/btrfsprogs/tests/mnt
 # tail convert-tests-results.txt
 ...
 ############### mount /root/btrfsprogs/tests/test.img /root/btrfsprogs/tests/mnt
 mount: /root/btrfsprogs/tests/test.img is not a block device (maybe try `-o loop'?)
 failed: mount /root/btrfsprogs/tests/test.img /root/btrfsprogs/tests/mnt

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: docs: add option variants, defaults and versions to mount options
David Sterba [Mon, 14 Sep 2015 22:40:00 +0000 (00:40 +0200)]
btrfs-progs: docs: add option variants, defaults and versions to mount options

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: cleanup, update function definitions to be ANSI conformant
David Sterba [Mon, 14 Sep 2015 21:42:29 +0000 (23:42 +0200)]
btrfs-progs: cleanup, update function definitions to be ANSI conformant

Sparse reports:

 warning: non-ANSI function declaration of function

and we're using func(void) elsewhere.

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: cleanup, mark more functions static
David Sterba [Mon, 14 Sep 2015 21:39:28 +0000 (23:39 +0200)]
btrfs-progs: cleanup, mark more functions static

Reported by sparse.

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: cleanup, make usage strings static
David Sterba [Mon, 14 Sep 2015 21:27:19 +0000 (23:27 +0200)]
btrfs-progs: cleanup, make usage strings static

Reported by sparse.

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: use calloc instead of malloc+memset for tree roots
Omar Sandoval [Mon, 14 Sep 2015 06:08:22 +0000 (23:08 -0700)]
btrfs-progs: use calloc instead of malloc+memset for tree roots

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: image: close all_devices at the end
Zhao Lei [Wed, 9 Sep 2015 13:32:23 +0000 (21:32 +0800)]
btrfs-progs: image: close all_devices at the end

Devices opened by check_mounted() in main() should closed before
the exit of btrfs-image.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: image: don't create unused threads
Zhao Lei [Wed, 9 Sep 2015 13:32:22 +0000 (21:32 +0800)]
btrfs-progs: image: don't create unused threads

In case of creating an image without compression, we don't need to
create unused threads.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: image: fix infinite looping with -t 0
Zhao Lei [Wed, 9 Sep 2015 13:32:21 +0000 (21:32 +0800)]
btrfs-progs: image: fix infinite looping with -t 0

Bug:
 # btrfs-image -t0 -c9 /dev/sda6 /tmp/btrfs_image.img
 (hang)
 # btrfs-image -r -t0 /tmp/btrfs_image.img /dev/sda6
 (hang)

Reason:
 The program need to create at least 1 thread for
 compression/decompression. If the user specifies -t0, it overwrites the
 default value of 1, then the program really creates 0 thread, and
 ends up looping

Fix:
 We can add a check, to make the program not to allow -t0 argument,
 but there is another problem:

For example, on a node with 4 cpus:
 btrfs-image -c9 -t1: 4 threads (1 means use NR_CPUS)
             -c9 -t2: 2 threads
             -c9 -t3: 3 threads
             ...
 (-t1 has more threads than -t2 and -t3)

 So we change to use value of 0 as "use NR_CPUS threads", then:
 btrfs-image [no -t arg]: use NR_CPUS threads
             -t0:         use NR_CPUS threads
             -t val:      use val threads.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: fix cross stripe boundary check
David Sterba [Thu, 10 Sep 2015 14:36:04 +0000 (16:36 +0200)]
btrfs-progs: fix cross stripe boundary check

Commit 854437ca3c228d8ab3eb24d2efc1c21b5d56a635 ("btrfs-progs:
extent-tree: avoid allocating tree block that crosses stripe boundary")
does not work for 64k nodesize. Due to an off-by-one error, all queries
to check_crossing_stripes will return that all extents cross a stripe
and this will lead to a false ENOSPC. This crashes later

$ ./mkfs.btrfs -n 64k image

./mkfs.btrfs(btrfs_reserve_extent+0xb77)[0x417f38]
./mkfs.btrfs(btrfs_alloc_free_block+0x57)[0x417fe0]
./mkfs.btrfs(__btrfs_cow_block+0x163)[0x408eb7]
./mkfs.btrfs(btrfs_cow_block+0xd0)[0x4097c4]
./mkfs.btrfs(btrfs_search_slot+0x16f)[0x40be4d]
./mkfs.btrfs(btrfs_insert_empty_items+0xc0)[0x40d5f9]
./mkfs.btrfs(btrfs_insert_item+0x99)[0x40da5f]
./mkfs.btrfs(btrfs_make_block_group+0x4d)[0x41705c]
./mkfs.btrfs(main+0xeef)[0x434b56]

Holger Hoffstätte reports that this also fixes false positives in case
the nodesize is less than 64k. This happens when the node blocks end at
the stripe boundary.

Reviewed-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: close inode scan in copy_inodes
Zhao Lei [Tue, 8 Sep 2015 13:16:51 +0000 (21:16 +0800)]
btrfs-progs: close inode scan in copy_inodes

We need to call ext2fs_close_inode_scan to release resources from
ext2fs_open_inode_scan.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: Avoid uninitialized data in output of btrfs-convert
Zhao Lei [Tue, 8 Sep 2015 13:16:50 +0000 (21:16 +0800)]
btrfs-progs: Avoid uninitialized data in output of btrfs-convert

The sequence, transid and reserved fields of inode were writen to disk
with uninitizlized value, this patch fixes it.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: fix typo in set/get of btrfs_inode_item::sequence
Zhao Lei [Tue, 8 Sep 2015 13:16:49 +0000 (21:16 +0800)]
btrfs-progs: fix typo in set/get of btrfs_inode_item::sequence

s/generation/sequence/
for BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, ...)

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: tests: add crafted and fuzzed images
David Sterba [Wed, 9 Sep 2015 15:04:12 +0000 (17:04 +0200)]
btrfs-progs: tests: add crafted and fuzzed images

A collection of several images that were produced in a non-standard way
and cause various errors in check or image tools. They do not fit into
the fsck tests as we're not able to repair any of them, but the tools
should not crash or do out-of-bounds access.

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: makefile: drop u option from ar invocation
Arnd Hannemann [Fri, 4 Sep 2015 23:06:18 +0000 (01:06 +0200)]
btrfs-progs: makefile: drop u option from ar invocation

In newer distros (ubuntu 15.10, fedora rawhide) the binutils
ar uses the new D flag per default to build deterministic
binaries.
Without this patch the following warning is issued, when
building btrfs-progs:

    [AR]     libbtrfs.a
/usr/bin/ar: `u' modifier ignored since `D' is the default (see `U')

For libtrfs.a performance benefit of the u option can be neglected,
so drop the u option and silence the warning.

In the future one might want to explicitly add the D option anyway.

Signed-off-by: Arnd Hannemann <arnd@arndnet.de>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: INSTALL: enhance build instructions
David Sterba [Mon, 7 Sep 2015 09:46:32 +0000 (11:46 +0200)]
btrfs-progs: INSTALL: enhance build instructions

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: autogen: add check for pkg-config
David Sterba [Mon, 7 Sep 2015 09:09:17 +0000 (11:09 +0200)]
btrfs-progs: autogen: add check for pkg-config

Configure uses pkg-config, this should be at least checked at the
autogen time.

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agoBtrfs progs v4.2
David Sterba [Thu, 3 Sep 2015 12:29:20 +0000 (14:29 +0200)]
Btrfs progs v4.2

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: btrfstune: remove unnecessary fsid checks
David Sterba [Wed, 2 Sep 2015 17:07:51 +0000 (19:07 +0200)]
btrfs-progs: btrfstune: remove unnecessary fsid checks

The new fsid members were switched from dynamically allocated to
on-stack in e737a9d56b2cec644f1462bf540cb78f858d146d ("btrfs-progs:
btrfstune: rework change_uuid"), we don't have to do the checks in the
helpers.

Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: convert: rework checks of label size
David Sterba [Wed, 2 Sep 2015 16:02:23 +0000 (18:02 +0200)]
btrfs-progs: convert: rework checks of label size

Coverity complains that the fslabel might be longer than the superblock
buffer, down in do_convert.

The label is at most 255 bytes, terminated by zero. Use buffers of the
right size.

Resolves-coverity-id: 1320911
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: doc: document btrfs-select-super(8)
Adam Borowski [Wed, 2 Sep 2015 03:00:46 +0000 (05:00 +0200)]
btrfs-progs: doc: document btrfs-select-super(8)

Signed-off-by: Adam Borowski <kilobyte@angband.pl>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: remove unused argument from block_group_free_all_extent
Zhao Lei [Wed, 2 Sep 2015 14:57:54 +0000 (16:57 +0200)]
btrfs-progs: remove unused argument from block_group_free_all_extent

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 years agobtrfs-progs: Fix some spelling typos in chunk-recover.c
Zhao Lei [Wed, 2 Sep 2015 14:57:44 +0000 (16:57 +0200)]
btrfs-progs: Fix some spelling typos in chunk-recover.c

Only comment, not big issue.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>
9 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>