platform/upstream/btrfs-progs.git
8 years agobtrfs-progs: move is_numerical() helper to utils and rename
Anand Jain [Sat, 10 Oct 2015 14:30:56 +0000 (22:30 +0800)]
btrfs-progs: move is_numerical() helper to utils and rename

Signed-off-by: Anand Jain <anand.jain@oracle.com>
[ moved to util.c and renamed ]
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: return -ENOMEM properly in btrfs_read_block_groups()
Eryu Guan [Mon, 19 Oct 2015 11:38:00 +0000 (19:38 +0800)]
btrfs-progs: return -ENOMEM properly in btrfs_read_block_groups()

Breaking from the while loop makes ret overwritten to zero, goto error
label directly and return -ENOMEM.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fix memory leak in cmd_qgroup_show()
Eryu Guan [Mon, 19 Oct 2015 11:37:59 +0000 (19:37 +0800)]
btrfs-progs: fix memory leak in cmd_qgroup_show()

filter_set and comparer_set should be freed on return.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: remove identical branch in record_extent()
Eryu Guan [Mon, 19 Oct 2015 11:37:58 +0000 (19:37 +0800)]
btrfs-progs: remove identical branch in record_extent()

The same code is executed when the condition "ret" is true or false,
because the code in the if-then branch and after the if statement is
identical.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fix memory leak on error path
Eryu Guan [Mon, 19 Oct 2015 11:37:57 +0000 (19:37 +0800)]
btrfs-progs: fix memory leak on error path

dev_scans and t_scans should be freed on malloc error.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: save and return error number correctly in check_chunks_and_extents
Eryu Guan [Mon, 19 Oct 2015 11:37:51 +0000 (19:37 +0800)]
btrfs-progs: save and return error number correctly in check_chunks_and_extents

The variable "err" is assigned to "ret" then "ret" gets overwritten by
check_extent_refs() before "ret" can be used. Reported by Coverity.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fix leak of "path" in btrfs_find_item() error paths
Eryu Guan [Mon, 19 Oct 2015 11:37:50 +0000 (19:37 +0800)]
btrfs-progs: fix leak of "path" in btrfs_find_item() error paths

path needs to be freed before return.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: fragments: use btrfs_open_dir for btrfs-fragments command
Zhao Lei [Mon, 12 Oct 2015 13:23:04 +0000 (21:23 +0800)]
btrfs-progs: fragments: use btrfs_open_dir for btrfs-fragments command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
deeper code, and return fuzzy error message.

Before patch:
  ./btrfs-fragments -o 123 /mnt/tmp1
  ERROR: can't perform the search

After patch:
  # ./btrfs-fragments -o 123 /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: replace: use btrfs_open_dir for btrfs replace command
Zhao Lei [Mon, 12 Oct 2015 13:23:03 +0000 (21:23 +0800)]
btrfs-progs: replace: use btrfs_open_dir for btrfs replace command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # ./btrfs replace cancel /mnt/tmp1
  ERROR: ioctl(DEV_REPLACE_CANCEL) failed on "/mnt/tmp1": Inappropriate ioctl for device
  # ./btrfs replace status /mnt/tmp1
  ERROR: ioctl(DEV_REPLACE_STATUS) failed on "/mnt/tmp1": Inappropriate ioctl for device

After patch:
  # ./btrfs replace cancel /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs replace status /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: use btrfs_open_dir in open_path_or_dev_mnt
Zhao Lei [Mon, 12 Oct 2015 13:23:02 +0000 (21:23 +0800)]
btrfs-progs: use btrfs_open_dir in open_path_or_dev_mnt

Use btrfs_open_dir() in open_path_or_dev_mnt() to make the function
return error when target is neither block device nor btrfs mount point.

Also add "verbose" argument to let function output common error
message instead of putting duplicated lines in caller.

Before patch:
  # ./btrfs device stats /mnt/tmp1
  ERROR: getting dev info for devstats failed: Inappropriate ioctl for device
  # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1
  ERROR: ioctl(DEV_REPLACE_STATUS) failed on "/mnt/tmp1": Inappropriate ioctl for device

After patch:
  # ./btrfs device stats /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: quota: use btrfs_open_dir for btrfs quota command
Zhao Lei [Mon, 12 Oct 2015 13:23:01 +0000 (21:23 +0800)]
btrfs-progs: quota: use btrfs_open_dir for btrfs quota command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # ./btrfs quota enable /mnt/tmp1
  ERROR: quota command failed: Inappropriate ioctl for device
  # ./btrfs quota disable /mnt/tmp1
  ERROR: quota command failed: Inappropriate ioctl for device
  # ./btrfs quota rescan /mnt/tmp1
  ERROR: quota rescan failed: Inappropriate ioctl for device
  #

After patch:
  # ./btrfs quota enable /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs quota disable /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs quota rescan /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  #

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: qgroup: use btrfs_open_dir for btrfs qgroup command
Zhao Lei [Mon, 12 Oct 2015 13:23:00 +0000 (21:23 +0800)]
btrfs-progs: qgroup: use btrfs_open_dir for btrfs qgroup command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # ./btrfs qgroup create 1/5  /mnt/tmp1
  ERROR: unable to create quota group: Inappropriate ioctl for device
  #
  # ./btrfs qgroup assign 1/5 2/5 /mnt/tmp1
  ERROR: unable to assign quota group: Inappropriate ioctl for device
  #
  # ./btrfs qgroup show /mnt/tmp1
  ERROR: can't perform the search - Inappropriate ioctl for device
  ERROR: can't list qgroups: Inappropriate ioctl for device
  #
  # ./btrfs qgroup limit 1G 1/5  /mnt/tmp1
  ERROR: unable to limit requested quota group: Inappropriate ioctl for device

After patch:
  # ./btrfs qgroup create 1/5 /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs qgroup assign 1/5 2/5 /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs qgroup show /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs qgroup limit 1G 1/5 /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: inspect: use btrfs_open_dir for btrfs inspect command
Zhao Lei [Mon, 12 Oct 2015 13:22:59 +0000 (21:22 +0800)]
btrfs-progs: inspect: use btrfs_open_dir for btrfs inspect command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # ./btrfs inspect-internal rootid /mnt/tmp1
  ERROR: Failed to lookup root id - Inappropriate ioctl for device
  btrfs inspect-internal rootid: rootid failed with ret=-1
  # ./btrfs inspect-internal inode-resolve 256 /mnt/tmp1
  ioctl ret=-1, error: Inappropriate ioctl for device
  # ./btrfs inspect-internal min-dev-size /mnt/tmp1
  Error invoking tree search ioctl: Inappropriate ioctl for device

After patch:
  # ./btrfs inspect-internal rootid /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs inspect-internal inode-resolve 256 /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs inspect-internal min-dev-size /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: inspect: set return value of error case
Zhao Lei [Mon, 12 Oct 2015 13:22:58 +0000 (21:22 +0800)]
btrfs-progs: inspect: set return value of error case

In case of open_file_or_dir() failed, ret is not set to right value,
and the function will return unwanted value(ret of sprintf).

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: inspect: Bypass unnecessary clean function in open_error
Zhao Lei [Mon, 12 Oct 2015 13:22:57 +0000 (21:22 +0800)]
btrfs-progs: inspect: Bypass unnecessary clean function in open_error

No need to cleanup fd in open_fail case, because it is not opened.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: balance: use btrfs_open_dir for btrfs balance command
Zhao Lei [Mon, 12 Oct 2015 13:22:56 +0000 (21:22 +0800)]
btrfs-progs: balance: use btrfs_open_dir for btrfs balance command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # btrfs balance start /mnt/tmp
  ERROR: error during balancing '/mnt/tmp' - Inappropriate ioctl for device
  There may be more info in syslog - try dmesg | tail
  #

After patch:
  # btrfs balance start /mnt/tmp
  ERROR: not btrfs filesystem: /mnt/tmp
  #

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: filesystem: use btrfs_open_dir for btrfs filesystem command
Zhao Lei [Mon, 12 Oct 2015 13:22:55 +0000 (21:22 +0800)]
btrfs-progs: filesystem: use btrfs_open_dir for btrfs filesystem command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # (/mnt/tmp is not btrfs mountpoint)
  #
  # btrfs filesystem df /mnt/tmp
  ERROR: couldn't get space info - Inappropriate ioctl for device
  ERROR: get_df failed Inappropriate ioctl for device
  #

After patch:
  # ./btrfs filesystem df /mnt/tmp
  ERROR: not btrfs filesystem: /mnt/tmp
  #

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: subvolume: use btrfs_open_dir for btrfs subvolume command
Zhao Lei [Mon, 12 Oct 2015 13:22:54 +0000 (21:22 +0800)]
btrfs-progs: subvolume: use btrfs_open_dir for btrfs subvolume command

We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # (/mnt/tmp is not btrfs mountpoint)
  #
  # btrfs subvolume create /mnt/tmp/123
  Create subvolume '/mnt/tmp/123'
  ERROR: cannot create subvolume - Inappropriate ioctl for device
  #

After patch:
  # btrfs subvolume create /mnt/tmp/123
  ERROR: not btrfs filesystem: /mnt/tmp
  #

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: print root item's last_snapshot value
Lakshmipathi.G [Wed, 7 Oct 2015 12:06:38 +0000 (17:36 +0530)]
btrfs-progs: print root item's last_snapshot value

Include last_snapshot value in print_root(). With btrfs-debug-tree, it
helps to identify whether its a snapshot-ed subvolume or not.

Signed-off-by: Lakshmipathi.G <Lakshmipathi.G@giis.co.in>
Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: docs: add html build target
David Sterba [Wed, 7 Oct 2015 12:36:34 +0000 (14:36 +0200)]
btrfs-progs: docs: add html build target

We can easily build the documentation in html format:

 $ make html

in the Documentation directory.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: build: make support for static checkers more generic
David Sterba [Wed, 7 Oct 2015 10:52:52 +0000 (12:52 +0200)]
btrfs-progs: build: make support for static checkers more generic

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: check: update help text
David Sterba [Tue, 6 Oct 2015 14:50:08 +0000 (16:50 +0200)]
btrfs-progs: check: update help text

Add some missing options and add a high-level description.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: a bit of makefile documentation
David Sterba [Tue, 6 Oct 2015 12:53:55 +0000 (14:53 +0200)]
btrfs-progs: a bit of makefile documentation

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: build: allow to build with various compiler warnings
David Sterba [Tue, 6 Oct 2015 12:40:07 +0000 (14:40 +0200)]
btrfs-progs: build: allow to build with various compiler warnings

Copied from linux kernel, 'make W=1' will build with various additional
warnings turned on. There are 3 levels, combinations are possible. The
build is quite noisy, not all warnings need to be fixed.

A specific warning can be turned on by 'make EXTRA_CFLAGS=-Wsomething'.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: tests: print commands on terminal if requested
David Sterba [Tue, 6 Oct 2015 12:16:39 +0000 (14:16 +0200)]
btrfs-progs: tests: print commands on terminal if requested

Set the variable TEST_LOG=tty (in the enviroment or as parameter to
make) to print commands executed by 'run_check' helpers to terminal (ie.
/dev/tty). This might be useful to see the test progress beside watching
the results file.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: misc tests: add 009-subvolume-sync-must-wait
David Sterba [Tue, 6 Oct 2015 11:11:09 +0000 (13:11 +0200)]
btrfs-progs: misc tests: add 009-subvolume-sync-must-wait

Test to verify that subovolume sync waits until the subvolume is
cleaned.

Signed-off-by: David Sterba <dsterba@suse.com>
8 years agobtrfs-progs: use calloc instead of malloc+memset
Silvio Fricke [Tue, 29 Sep 2015 17:10:36 +0000 (19:10 +0200)]
btrfs-progs: use calloc instead of malloc+memset

This patch is generated from a coccinelle semantic patch:

identifier t;
expression e;
statement s;
@@
-t = malloc(e);
+t = calloc(1, e);
(
if (!t) s
|
if (t == NULL) s
|
)
-memset(t, 0, e);

Signed-off-by: Silvio Fricke <silvio.fricke@gmail.com>
[squashed patches into one]
Signed-off-by: David Sterba <dsterba@suse.com>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
8 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>
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>