platform/upstream/btrfs-progs.git
10 years agoBtrfs-progs: check return value of realpath(3)
Eryu Guan [Sat, 12 Oct 2013 15:47:52 +0000 (23:47 +0800)]
Btrfs-progs: check return value of realpath(3)

I hit a segfault when deleting a subvolume with very long name(>4096),
it's because cmd_subvol_delete() calls strdup() and passes NULL as
argument, which is returned by realpath(3).

I used the following script to reproduce

#!/bin/bash
mnt=$1
i=1
path=$mnt/subvol_$i

# Create very deep subvolumes
while btrfs sub create $path;do
      ((i++))
      path="$path/subvol_$i"
done
last_vol=$(dirname $path)
dir=$(dirname $last_vol)
vol=$(basename $last_vol)

# Try to delete tha last one, this would get segfault
pushd $dir
btrfs sub delete $vol
popd

Fix it by checking return value of realpath(3), also fix the one in
find_mount_root().

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: check fopen failure in cmds-send
Zach Brown [Fri, 11 Oct 2013 16:21:46 +0000 (09:21 -0700)]
btrfs-progs: check fopen failure in cmds-send

Check for fopen() failure.  This shows up in static analysis as a
possible null pointer derference.

Signed-off-by: Zach Brown <zab@redhat.com>
Laughed-at-by: Stefan Behrens <sbehrens@giantdisaster.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: free leaked roots in calc-size
Zach Brown [Mon, 7 Oct 2013 21:43:05 +0000 (14:43 -0700)]
btrfs-progs: free leaked roots in calc-size

This was found by static analysis.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: remove unused variables
Zach Brown [Mon, 7 Oct 2013 21:43:04 +0000 (14:43 -0700)]
btrfs-progs: remove unused variables

Presumably people missed these warnings because btrfs-fragments isn't
built by default.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: don't overflow colors[] in fragments
Zach Brown [Mon, 7 Oct 2013 21:43:03 +0000 (14:43 -0700)]
btrfs-progs: don't overflow colors[] in fragments

Stop iteration at the number of elements in the colors[] array when
initializing the elements.  Rather than a magic number.  This was found
by static analysis.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: don't deref pipefd[-1]
Zach Brown [Mon, 7 Oct 2013 21:43:02 +0000 (14:43 -0700)]
btrfs-progs: don't deref pipefd[-1]

commit 4782e8ebdb583dfa3615f7b38dee729d34f62ec1 accidentally replaced
[0] with [-1].  Put it back.  This was found by static analysis.

Signed-off-by: Zach Brown <zab@redhat.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: don't leak path in verify_space_cache
Zach Brown [Mon, 7 Oct 2013 21:43:01 +0000 (14:43 -0700)]
btrfs-progs: don't leak path in verify_space_cache

This was found by static analysis.

Signed-off-by: Zach Brown <zab@redhat.com>
Reviewed-by: chandan <chandan@linux.vnet.ibm.com
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: free eb in fixup_chunk_tree_block()
Zach Brown [Mon, 7 Oct 2013 21:43:00 +0000 (14:43 -0700)]
btrfs-progs: free eb in fixup_chunk_tree_block()

This was found by static analysis.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: remove dead block group checking
Zach Brown [Mon, 7 Oct 2013 21:42:59 +0000 (14:42 -0700)]
btrfs-progs: remove dead block group checking

Don't carry around dead code.  If its needed again, it's only a few git
commands away.  This was found by static analysis.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: check link_subvol name base
Zach Brown [Mon, 7 Oct 2013 21:42:58 +0000 (14:42 -0700)]
btrfs-progs: check link_subvol name base

In principle, link_subvol() can be given an abitrary string as the name
of the saved subvolume.  It copies it into a fixed-size stack buffer and
then uses it as dirent names without testing its length.

This limits its length to BTRFS_NAME_LEN.  This was found by static
analsys.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: don't overflow read buffer in image
Zach Brown [Mon, 7 Oct 2013 21:42:57 +0000 (14:42 -0700)]
btrfs-progs: don't overflow read buffer in image

search_for_chunk_blocks() allocates a fixed-size buffer and then reads
arbitrary u32 sized buffers in to it.  Instead let's fail if the item is
bigger than the buffer.  This was found by static analysis.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: don't overrun name in find-collisions
Zach Brown [Mon, 7 Oct 2013 21:42:56 +0000 (14:42 -0700)]
btrfs-progs: don't overrun name in find-collisions

find_collision() allocates name_len bytes for its sub array so the index
must be less than name_len.  This was found by static analysis.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: check path alloc in corrupt block
Zach Brown [Mon, 7 Oct 2013 21:42:54 +0000 (14:42 -0700)]
btrfs-progs: check path alloc in corrupt block

btrfs-corrupt-block added some untested path allocations.  These showed
up in static analysis when they pass their path to btrfs_search_slot()
which unconditionally dereferences the path.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: make pretty_size_snprintf() return len
Wang Shilong [Mon, 7 Oct 2013 07:21:47 +0000 (15:21 +0800)]
Btrfs-progs: make pretty_size_snprintf() return len

Sometimes, we need to catch length of snprintf() in pretty_size_snprintf().

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: enhance btrfs qgroup to print the result as a table
Wang Shilong [Tue, 8 Oct 2013 16:24:02 +0000 (00:24 +0800)]
Btrfs-progs: enhance btrfs qgroup to print the result as a table

This patch enhance to print the result as a table.

You can use it like:
btrfs qgroup show <path>
However, to table the result better, we make '-p' and '-c' not present
at the same time.

For example:

btrfs qgroup show -pr <path>
The result will output as the follow format:

qgroupid rfer       excl       max_excl       parent
-------- ----       ----       --------       ------
0/265    1289752576 1289752576 0              ---
1/0      0          0          10999511627776 2/0,3/0
2/0      0          0          0              ---
3/0      0          0          0              ---

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: enhance btrfs qgroup show to sort qgroups
Wang Shilong [Mon, 7 Oct 2013 07:21:44 +0000 (15:21 +0800)]
Btrfs-progs: enhance btrfs qgroup show to sort qgroups

You might want to list qgroups in order of some items, such as 'qgroupid', 'rfer'
and so on, you can use '--sort'. Now you can sort the qgroups by 'qgroupid',
'rfer','excl','max_rfer' and 'max_excl'.

For example:
If you want to list qgroups in order of 'qgroupid'.
You can use the option like that:

btrfs qgroup show --sort=+/-qgroupid <path>
Here, '+' means the result is sorted by ascending order. '-' is by descending
order. If you don't specify either '+' nor '-', the result is sorted by
default - ascending order.

If you want to combine sort items, you do it like that:
btrfs qgroup show  --sort=-qgroupid,+rfer,max_rfer,excl <path>

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: list all qgroups impact given path(exclude ancestral qgroups)
Wang Shilong [Mon, 7 Oct 2013 07:21:43 +0000 (15:21 +0800)]
Btrfs-progs: list all qgroups impact given path(exclude ancestral qgroups)

This patch introduces '-f' option which can help you filter the qgroups
by the path name, you may use it like:

btrfs qgroup show -f <path>
For example:

                         qgroupid(2/0)
                          /     \
                         /       \
                        qgroupid(1/0)
                        /         \
                       /           \
                      /             \
                  qgroupid(0/1)   qgroupid(0/2)
                  sub1              sub2
                  /  \
                 /    \
dir1  file1

If we use the command:
btrfs qgroup show -f sub1/dir1
The result will output
0/1 -- --

'-f' option helps you list all qgroups impact given path.
(exclude ancestral qgroups)

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: list all qgroups impact given path(include ancestral qgroups)
Wang Shilong [Mon, 7 Oct 2013 07:21:42 +0000 (15:21 +0800)]
Btrfs-progs: list all qgroups impact given path(include ancestral qgroups)

This patch introduces '-F' option which can help you filter the qgroups
by the path name, you may use it like:

btrfs qgroup show -F <path>
For example:

                         qgroupid(2/0)
                          /     \
                         /       \
                        qgroupid(1/0)
                        /         \
                       /           \
                      /             \
                  qgroupid(0/1)   qgroupid(0/2)
                  sub1              sub2
                  /  \
                 /    \
dir1  file1

If we use the command:
btrfs qgroup show -F sub1/dir1
The result will output
0/1 -- --
1/0 -- --
2/0 -- --

'-F' option help you list all qgroups impact given path.
(include ancestral qgroups).

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: introduce '-e' option to print max exclusive size of qgroups
Wang Shilong [Mon, 7 Oct 2013 07:21:41 +0000 (15:21 +0800)]
Btrfs-progs: introduce '-e' option to print max exclusive size of qgroups

This patch introduce '-e' option to print max exclusive size of qgroups.
You may use it like this:
btrfs qgroup -e <path>

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: introduce '-r' option to print max referenced size of qgroups
Wang Shilong [Mon, 7 Oct 2013 07:21:40 +0000 (15:21 +0800)]
Btrfs-progs: introduce '-r' option to print max referenced size of qgroups

This patch introduces '-r' option to print max referenced size of qgroups.
You may use it like:

btrfs qgroup show -r <path>

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: introduces '-c' option to print the ID of the child qgroups
Wang Shilong [Mon, 7 Oct 2013 07:21:39 +0000 (15:21 +0800)]
Btrfs-progs: introduces '-c' option to print the ID of the child qgroups

This patch introduces '-c' option to print the ID of the child qgroups.
You may use it like:
btrfs qgroup show -c <path>

For Example:

                qgroupid(2/0)
                 /       \
                /         \
               /           \
            qgroupid(1/0) qgroupid(1/1)
               \            /
                \          /
 qgroupid(0/1)

If we use the command:

btrfs qgroup show -c <path>
The result will output
0/1 -- -- --
1/0 -- -- 0/1
1/1 -- -- 0/1
2/0 -- -- 1/0,1/1

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: introduces '-p' option to print the ID of the parent qgroups
Wang Shilong [Mon, 7 Oct 2013 07:21:38 +0000 (15:21 +0800)]
Btrfs-progs: introduces '-p' option to print the ID of the parent qgroups

This patch introduces '-p' option to print the ID of the parent qgroups.
You may use it like:

btrfs qgroup show -p <path>
For Example:
                qgroupid(2/0)
                /         \
               /           \
              /             \
        qgroupid(1/0)  qgroupid(1/1)
              \              /
               \            /
                qgroupid(0/1)

If we use the command:

btrfs qgroup show -p <path>
The result will output
0/1 -- -- 1/0,1/1
1/0 -- -- 2/0
1/1 -- -- 2/0
2/0 -- -- --

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: restructure show_qgroups
Wang Shilong [Mon, 7 Oct 2013 07:21:37 +0000 (15:21 +0800)]
Btrfs-progs: restructure show_qgroups

The current show_qgroups() just shows a little information, and it is hard to
add some functions which the users need in the future, so i restructure it, make
it easy to add new functions.

In order to improve the scalability of show_qgroups(), i add some important
structures:

struct qgroup_lookup {
struct rb_root root;
}
  /*
*store qgroup's information
*/
struct btrfs_qgroup {
struct rb_node  rb_node;
u64 qgroupid;

u64 generation;
u64 rfer;
u64 rfer_cmpr;
u64 excl_cmpr;

u64 flags;
u64 max_rfer;
u64 max_excl;
u64 rsv_rfer;
u64 rsv_excl;

struct list_head qgroups;
struct list_head members;
}
/*
*glue structure to represent the relations
*between qgroups
*/
struct btrfs_qgroup_list {
struct list_head next_qgroups;
struct list_head next_member;
struct btrfs_qgroup *qgroup;
struct btrfs_qgroup *member;
}
The above 3 structures are used to manage all the information
of qgroups.

struct {
char *name;
char *column_name;
int need_print;
} btrfs_qgroup_columns[]

We define a arrary to manage all the columns that can be
outputed, and use a member variant(->need_print) to control
the output of the relative column. Some columns are outputed
by default. But we can change it according to the requirement
of the users.

For example:
if outputing max referenced size of qgroup is needed,the function
'btrfs_qgroup_setup_column()' will be called, and the parameter 'BTRFS_QGROUP_MAX_RFER'
(extend in the future) will be passsed to the function. After the function is done,
when showing qgroups, max referenced size of qgroup will be output.

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: btrfs_list_find_updated_files: Fix memory leak.
chandan [Mon, 7 Oct 2013 06:36:11 +0000 (12:06 +0530)]
btrfs-progs: btrfs_list_find_updated_files: Fix memory leak.

The current code returns from the function when the call to ioctl
fails. This may leak cache_dir_name and cache_full_name. Fix it.

Signed-off-by: chandan <chandan@linux.vnet.ibm.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: print out human readable errors for inodes and backrefs
Josef Bacik [Fri, 4 Oct 2013 20:42:39 +0000 (16:42 -0400)]
Btrfs-progs: print out human readable errors for inodes and backrefs

We usually print out a hex value of any errors on inodes or their backrefs,
which is a huge pain for me because I have to put it into a calculator and count
the bits to figure out which errors these map to, and usually I get it wrong the
first time.  To fix this lets just print out a human readable string for each
error that way it will be easier to spot the "OH GOD THAT'S AWFUL" errors from
"oh yeah thats no big deal, repair will fix that."  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: Update manpages for btrfs defrag -r
Frank Holton [Tue, 1 Oct 2013 15:31:47 +0000 (11:31 -0400)]
btrfs-progs: Update manpages for btrfs defrag -r

Add the -r option to the manpages for btrfs defragment

Signed-off-by: Frank Holton <fholton@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: keep track of transid failures and fix them if possible
Josef Bacik [Tue, 1 Oct 2013 13:00:19 +0000 (09:00 -0400)]
Btrfs-progs: keep track of transid failures and fix them if possible

A user was reporting an issue with bad transid errors on his blocks.  The thing
is that btrfs-progs will ignore transid failures for things like restore and
fsck so we can do a best effort to fix a users file system.  So fsck can put
together a coherent view of the file system with stale blocks.  So if everything
else is ok in the mind of fsck then we can recow these blocks to fix the
generation and the user can get their file system back.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: add a test image for the transid fixer in btrfsck
Josef Bacik [Tue, 1 Oct 2013 12:58:33 +0000 (08:58 -0400)]
Btrfs-progs: add a test image for the transid fixer in btrfsck

This is a verification test for the transid recow functionality of btrfsck.
I've also adjusted the test script to spit out which image it's testing so I can
be sure the image was getting tested.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: add ability to corrupt metadata block fields
Josef Bacik [Tue, 1 Oct 2013 12:54:58 +0000 (08:54 -0400)]
Btrfs-progs: add ability to corrupt metadata block fields

This patch allows us to garble the generation field of a metadata block.  We
will search down to the block, set the bogus generation and write the block back
out.  I used this for verifying a transid fix patch for btrfsck.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: add make test framework
Josef Bacik [Mon, 9 Sep 2013 20:41:58 +0000 (16:41 -0400)]
Btrfs-progs: add make test framework

We need to start adding some sanity tests to btrfs-progs to make sure we aren't
breaking things with our patches.  The most important of these tools is btrfsck.
This patch gets things started by adding a basic btrfsck test that makes sure we
can fix a corruption problem we know we can fix.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: Add recursive defrag using -r option
Frank Holton [Mon, 23 Sep 2013 19:18:17 +0000 (15:18 -0400)]
btrfs-progs: Add recursive defrag using -r option

Add an option to defrag all files in a directory recursively.

Signed-off-by: Frank Holton <fholton@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: make the repair option a global static var
Josef Bacik [Mon, 30 Sep 2013 20:15:22 +0000 (16:15 -0400)]
Btrfs-progs: make the repair option a global static var

It's just annoying to have to pass it around everywhere.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: device add should check existing FS before adding
Anand Jain [Fri, 27 Sep 2013 17:30:05 +0000 (01:30 +0800)]
btrfs-progs: device add should check existing FS before adding

as of now, when 'btrfs device add' adds a device it doesn't
check if the given device contains an existing FS. This
patch will change that to check the same. which when true
add will fail, and ask user to use -f option to overwrite.

further, since now we have test_dev_for_mkfs() function
to check if a disk can be used, so this patch will also
use this function to test the given device before adding.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: cmd_find_new: Sync fs before searching for modified files.
chandan [Mon, 23 Sep 2013 18:17:11 +0000 (23:47 +0530)]
btrfs-progs: cmd_find_new: Sync fs before searching for modified files.

The sync makes sure that 'very recently' introduced delayed work is
accounted for in the output of 'btrfs subvolume find-new' command.

Signed-off-by: chandan <chandan@linux.vnet.ibm.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: create helper function to use lblkid to scan for btrfs disks
Anand Jain [Fri, 27 Sep 2013 15:45:00 +0000 (23:45 +0800)]
btrfs-progs: create helper function to use lblkid to scan for btrfs disks

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: move out print in cmd_df to another function
Anand Jain [Fri, 27 Sep 2013 12:24:41 +0000 (20:24 +0800)]
btrfs-progs: move out print in cmd_df to another function

This is a prepatory work for the btrfs fi show command
fixes. So that we have a function get_df to get the fs sizes

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: Make btrfs_header_fsid() return unsigned long
Ross Kirk [Tue, 1 Oct 2013 09:59:22 +0000 (11:59 +0200)]
btrfs-progs: Make btrfs_header_fsid() return unsigned long

Internally, btrfs_header_fsid() calculates an unsigned long, but casts
it to a pointer, while all callers cast it to unsigned long again.

Committed to btrfs as fba6aa75654394fccf2530041e9451414c28084f

Fix line length issues and match changes to kernelspace

Signed-off-by: Ross Kirk <ross.kirk@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: clear_extent_bits: Fix memory leak.
chandan [Fri, 20 Sep 2013 18:22:11 +0000 (23:52 +0530)]
btrfs-progs: clear_extent_bits: Fix memory leak.

The 'prealloc' extent_state structure is leaked for the case when the 'desired
range' encapsulates/covers the 'extent range'.

Signed-off-by: chandan <chandan@linux.vnet.ibm.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix the min ticks check in print_seek_histogram
Josef Bacik [Fri, 20 Sep 2013 17:51:31 +0000 (13:51 -0400)]
Btrfs-progs: fix the min ticks check in print_seek_histogram

Before I had been dividing by 5 but that gave me too much output so I changed it
to 20 without changing the min seeks test.  Fix this to avoid a divide by 0
problem.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: check if device supports trim
David Sterba [Fri, 20 Sep 2013 16:42:07 +0000 (18:42 +0200)]
btrfs-progs: check if device supports trim

The message about trim was printed unconditionally, we should check if
trim is supported at all.

Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: add a bunch of new statistics to btrfs-calc-size
Josef Bacik [Fri, 20 Sep 2013 15:32:41 +0000 (11:32 -0400)]
Btrfs-progs: add a bunch of new statistics to btrfs-calc-size

I've been wanting to get back to the allocator and make some changes to try and
fix our fragmenation woes with lots of metadata.  But in order to make these
changes I need to have something to tell me if my changes are making a real
measurable difference.  So this patch adds a bunch of new statistics to
btrfs-calc-size.  It will tell me how long it took to read in the trees, how
many seeks it had (both forward and backward).  It will tell me how far spread
out the tree is and spit out a nice histogram of the seeks.  Here is some sample
output

Calculating size of extent tree
        Total size: 60.74MB
                Inline data: 0.00
        Total seeks: 5020
                Forward seeks: 3691
                Backward seeks: 1329
                Avg seek len: 929.53MB
        Seek histogram
                      4096 -       4096:       1043 ####
                      8192 -      73728:        760 ###
                     81920 -   52527104:        753 ###
                  53518336 -  168009728:        753 ###
                 168591360 -  696045568:        753 ###
                 696238080 - 7560364032:        753 ###
                7560437760 - 8409739264:        178 |
        Total clusters: 1874
                Avg cluster size: 25.17KB
                Min cluster size: 8.00KB
                Max cluster size: 472.00KB
        Total disk spread: 7.90GB
        Total read time: 0 s 341670 us
        Levels: 4

This way we can have good numbers to back up any changes we make to the
allocator.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: look up the containing tree root id
David Sterba [Wed, 18 Sep 2013 16:19:30 +0000 (18:19 +0200)]
btrfs-progs: look up the containing tree root id

Find the tree id of the containing subvolume for a given file or
directory. For subvolume return it's own id.

$ btrfs inspect-internal rootid <path>

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: remove leftovers from hg times
David Sterba [Wed, 18 Sep 2013 15:26:33 +0000 (17:26 +0200)]
btrfs-progs: remove leftovers from hg times

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: remove unused parameter from btrfs_header_fsid
Ross Kirk [Fri, 20 Sep 2013 13:27:32 +0000 (14:27 +0100)]
btrfs-progs: remove unused parameter from btrfs_header_fsid

Remove unused parameter, 'eb'. Unused since introduction in
7777e63b425f1444d2472ea05a6b2b9cf865f35b

Signed-off-by: Ross Kirk <ross.kirk@gmail.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: drop unused parameter from btrfs_item_nr
Ross Kirk [Fri, 20 Sep 2013 09:55:26 +0000 (10:55 +0100)]
btrfs-progs: drop unused parameter from btrfs_item_nr

Remove unused eb parameter from btrfs_item_nr, unused since introduced
in 7777e63b425f1444d2472ea05a6b2b9cf865f35b

Signed-off-by: Ross Kirk <ross.kirk@gmail.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: add super-recover to recover bad supers
Wang Shilong [Sat, 21 Sep 2013 08:34:18 +0000 (16:34 +0800)]
Btrfs-progs: add super-recover to recover bad supers

Until now if one of device's first superblock is corrupt,btrfs will
fail to mount. Luckily, btrfs have at least two superblocks for
every disk.

In theory, if silent corrupting happens when we are writting superblocks
into disk, we must hold at least one good superblock.

One side effect is that user must gurantee that the disk must be
a btrfs disk. Otherwise, this tool may destroy other fs.(This is also
reason why btrfs only use first superblock in every disk to mount)

This little program will try to correct bad superblocks from
good superblocks with max generation.

There will be five kinds of return values:

0: all supers are valid, no need to recover
1: usage or syntax error
2: recover all bad superblocks successfully
3: fail to recover bad superblocks
4: abort to recover bad superblocks

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: move ask_user() to utils.c
Wang Shilong [Wed, 18 Sep 2013 08:27:35 +0000 (16:27 +0800)]
Btrfs-progs: move ask_user() to utils.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: pass flag to control whether run ioctl in btrfs_scan_for_fsid()
Wang Shilong [Wed, 18 Sep 2013 08:27:34 +0000 (16:27 +0800)]
Btrfs-progs: pass flag to control whether run ioctl in btrfs_scan_for_fsid()

If some fatal superblocks are damaged, running ioctl will return failure,
in this case, we should avoid run ioctl.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: do not run ioctls in check_mounted_where()
Wang Shilong [Wed, 18 Sep 2013 08:27:33 +0000 (16:27 +0800)]
Btrfs-progs: do not run ioctls in check_mounted_where()

We don't need to run ioctls when checking whether btrfs
has mounted somewhere.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: add list_sort and use it to sort devices by id
David Sterba [Tue, 17 Sep 2013 15:24:53 +0000 (17:24 +0200)]
btrfs-progs: add list_sort and use it to sort devices by id

The devices in 'btrfs filesystem show' are now sorted by the device id,
currently the order was undefined.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: separate command and implementation of chunk-recover code
David Sterba [Tue, 17 Sep 2013 15:21:22 +0000 (17:21 +0200)]
btrfs-progs: separate command and implementation of chunk-recover code

The command has been moved and we should rename the files accordingly,
so the entry point is now in cmds-rescue.c and the core functionality
in it's own file.

Return codes of btrfs_recover_chunk_tree have been simplified not to
require a define and another file for defintion.

CC: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: move chunk-recover to rescue group
David Sterba [Tue, 17 Sep 2013 15:21:21 +0000 (17:21 +0200)]
btrfs-progs: move chunk-recover to rescue group

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: introduce rescue command group
David Sterba [Tue, 17 Sep 2013 15:21:20 +0000 (17:21 +0200)]
btrfs-progs: introduce rescue command group

Add an empty 1st level command namespace that will collect specialized
recovery tools like chunk-recover, zero-log, select-super and similar.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: add nodiscard option to device add
David Sterba [Tue, 17 Sep 2013 14:54:01 +0000 (16:54 +0200)]
btrfs-progs: add nodiscard option to device add

Same as for mkfs.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: use better name for nodiscard variable and flip the logic
David Sterba [Tue, 17 Sep 2013 14:54:00 +0000 (16:54 +0200)]
btrfs-progs: use better name for nodiscard variable and flip the logic

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-balance.c
Wang Shilong [Tue, 10 Sep 2013 02:28:29 +0000 (10:28 +0800)]
Btrfs-progs: fix magic return value in cmds-balance.c

If there is no balance in progress, resume/pause/cancel
will return 2. Usage or syntax errors will return 1.
And 0 means operations return successfully.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: Ilya Dryomov <idryomov@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: fix -Wmissing-noreturn
Chris West (Faux) [Mon, 9 Sep 2013 22:06:38 +0000 (23:06 +0100)]
btrfs-progs: fix -Wmissing-noreturn

Signed-off-by: "Chris West (Faux)" <git@goeswhere.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: 'optarg' shadows getopt
Chris West (Faux) [Mon, 9 Sep 2013 22:06:24 +0000 (23:06 +0100)]
btrfs-progs: 'optarg' shadows getopt

Signed-off-by: "Chris West (Faux)" <git@goeswhere.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: add ability to corrupt file extent disk bytenr
Josef Bacik [Mon, 9 Sep 2013 20:41:57 +0000 (16:41 -0400)]
Btrfs-progs: add ability to corrupt file extent disk bytenr

A user had a corrupt fs where one of his file extents pointed to a completely
bogus disk bytenr.  This patch allows us to corrupt a file system in a similar
way in order to test btrfsck.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: make btrfsck fix backrefs that are broken
Josef Bacik [Mon, 9 Sep 2013 20:41:56 +0000 (16:41 -0400)]
Btrfs-progs: make btrfsck fix backrefs that are broken

If you set an file extent item's disk_bytenr to something completely wrong we
won't be able to fix this if it is the only one who has a ref on the original
disk bytenr.  Our extent records know exactly who is supposed to point at them,
so if we have an extent record that has no backrefs we can go and try to lookup
the backrefs ourselves.  If these backrefs do not point to an extent record that
was actually found then we can be pretty sure this extent record is valid and
the backref is bogus.  Then the verify_backref code can do its thing and reset
the backref to point to the right extent record and we can all carry on.  This
fixes a user reported corruption.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: allow fsck to fix directory isize errors
Josef Bacik [Mon, 9 Sep 2013 20:41:55 +0000 (16:41 -0400)]
Btrfs-progs: allow fsck to fix directory isize errors

A user reported a problem where he was unable to rmdir an empty directory.  This
is because his isize was wrong.  This patch will fix this sort of corruption and
allow him to rmdir his directory.  Thanks

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: setup framework to corrupt specific fields of an inode
Josef Bacik [Mon, 9 Sep 2013 20:41:54 +0000 (16:41 -0400)]
Btrfs-progs: setup framework to corrupt specific fields of an inode

A user reported a problem with his fs where he had a bogus isize on his
directory.  In order to make sure my patch for fsck fixes this properly I needed
to be able to corrupt an inode like this, which is what this patch is for.
Eventually I want to extend this to corrupt everything so we can integrate tests
into btrfs-progs to run btrfsck against to make sure we don't regress on fixing
things with btrfsck.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: make btrfs-corrupt-block compile again
David Sterba [Mon, 9 Sep 2013 20:40:55 +0000 (22:40 +0200)]
btrfs-progs: make btrfs-corrupt-block compile again

Reexport csum_tree_block.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix typo in btrfs_err_str()
Wang Shilong [Mon, 9 Sep 2013 06:08:12 +0000 (14:08 +0800)]
Btrfs-progs: fix typo in btrfs_err_str()

Raid5 and raid6 at least need three and foure devices respectively,
fix it.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: calculate available blocks on device properly
Hidetoshi Seto [Thu, 5 Sep 2013 06:57:19 +0000 (15:57 +0900)]
btrfs-progs: calculate available blocks on device properly

I found that mkfs.btrfs aborts when assigned multi volumes contain
a small volume:

  # parted /dev/sdf p
  Model: LSI MegaRAID SAS RMB (scsi)
  Disk /dev/sdf: 72.8GB
  Sector size (logical/physical): 512B/512B
  Partition Table: msdos

  Number  Start   End     Size    Type     File system  Flags
   1      32.3kB  72.4GB  72.4GB  primary
   2      72.4GB  72.8GB  461MB   primary

  # ./mkfs.btrfs -f /dev/sdf1 /dev/sdf2
  :
  SMALL VOLUME: forcing mixed metadata/data groups
  adding device /dev/sdf2 id 2
  mkfs.btrfs: volumes.c:852: btrfs_alloc_chunk: Assertion `!(ret)' failed.
  Aborted (core dumped)

This failure of btrfs_alloc_chunk was caused by following steps:
 1) since there is only small space in the small device, mkfs was
    going to allocate a chunk from free space as much as available.
    So mkfs called btrfs_alloc_chunk with
        size = device->total_bytes - device->used_bytes.
 2) (According to the comment in source code, to avoid overwriting
    superblock,) btrfs_alloc_chunk starts taking chunks at an offset
    of 1MB. It means that the layout of a disk will be like:
     [[1MB at beginning for sb][allocated chunks]* ... free space ... ]
    and you can see that the available free space for allocation is:
        avail = device->total_bytes - device->used_bytes - 1MB.
 3) Therefore there is only free space 1MB less than requested. damn.

>From further investigations I also found that this issue is easily
reproduced by using -A, --alloc-start option:

  # truncate --size=1G testfile
  # ./mkfs.btrfs -A900M -f testfile
   :
  mkfs.btrfs: volumes.c:852: btrfs_alloc_chunk: Assertion `!(ret)' failed.
  Aborted (core dumped)

In this case there is only 100MB for allocation but btrfs_alloc_chunk
was going to allocate more than the 100MB.

The root cause of both of above troubles is a same simple bug:
btrfs_chunk_alloc does not calculate available bytes properly even
though it researches how many devices have enough room to have a
chunk to be allocated.

So this patch introduces new function btrfs_device_avail_bytes()
which returns available bytes for allocation in specified device.

Signed-off-by: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: error if device have no space to make primary chunks
Hidetoshi Seto [Thu, 5 Sep 2013 06:55:08 +0000 (15:55 +0900)]
btrfs-progs: error if device have no space to make primary chunks

The previous patch works fine if the size of specified volume to mkfs
is less than 4MB. However usually btrfs requires more than 4MB to work,
and the minimum preferred size is depending on the raid setting etc.

This patch let mkfs print error message if it cannot allocate one of
chunks should be there at first.

 [before]
  # truncate --size=4500K testfile
  # ./mkfs.btrfs -f testfile
   :
  SMALL VOLUME: forcing mixed metadata/data groups
  mkfs.btrfs: mkfs.c:84: make_root_dir: Assertion `!(ret)' failed.
  Aborted (core dumped)

 [After]
  # truncate --size=4500K testfile
  # ./mkfs.btrfs -f testfile
   :
  SMALL VOLUME: forcing mixed metadata/data groups
  no space to alloc data/metadata chunk
  failed to setup the root directory

TBD is calculate minimum size for setting and put it in the error
message to let user know how large amount of volume is required.

Signed-off-by: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: error if device for mkfs is too small
Hidetoshi Seto [Thu, 5 Sep 2013 06:53:34 +0000 (15:53 +0900)]
btrfs-progs: error if device for mkfs is too small

Eric pointed out that mkfs abort if specified volume is too small:

  # truncate --size=2m testfile
  # ./mkfs.btrfs testfile
   :
  SMALL VOLUME: forcing mixed metadata/data groups
  mkfs.btrfs: volumes.c:852: btrfs_alloc_chunk: Assertion `!(ret)' failed.
  Aborted (core dumped)

As the first step to fix problems around there, let mkfs to report
error if the size of target volume is less than the size of the first
system block group, BTRFS_MKFS_SYSTEM_GROUP_SIZE (= 4MB).

Reported-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: btrfs_setup_chunk_tree_and_device_map: Return -EIO on error.
chandan [Thu, 5 Sep 2013 05:51:30 +0000 (11:21 +0530)]
btrfs-progs: btrfs_setup_chunk_tree_and_device_map: Return -EIO on error.

As a result of a successful call to btrfs_read_sys_array(), the 'ret'
variable is already set to 0. Hence the function would return 0 even
if the call to read_tree_block() fails.

Signed-off-by: chandan <chandan@linux.vnet.ibm.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: free strdup()s that are not freed
Gui Hecheng [Thu, 5 Sep 2013 08:00:43 +0000 (16:00 +0800)]
btrfs-progs: free strdup()s that are not freed

The strdup()s not freed are reported as memory leaks by valgrind.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: free the local list pending_list in btrfs_scan_one_dir
Gui Hecheng [Thu, 5 Sep 2013 02:38:57 +0000 (10:38 +0800)]
btrfs-progs: free the local list pending_list in btrfs_scan_one_dir

Originally the local pending_list is not guaranteed to be freed upon
fails, it should be emptyed and the elements should be freed.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: missing tree-freeing statements added
Gui Hecheng [Thu, 5 Sep 2013 02:38:56 +0000 (10:38 +0800)]
btrfs-progs: missing tree-freeing statements added

The seen cache_tree in run_next_block freed.
Originally, this "missing" causes memory leaks, reported by valgrind.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: local variable memory freed
Gui Hecheng [Thu, 5 Sep 2013 02:38:55 +0000 (10:38 +0800)]
btrfs-progs: local variable memory freed

The local probe variable in is_ssd() freed upon unsuccessful return;
The local dir_head list in make_image() freed upon unsuccessful return.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: free local variable buf upon unsuccessful returns
Gui Hecheng [Thu, 5 Sep 2013 02:38:54 +0000 (10:38 +0800)]
btrfs-progs: free local variable buf upon unsuccessful returns

The variable "buf" passed into find_collision() as parameter "name"
should be freed on unsuccessful returns.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-scrub.c
Wang Shilong [Wed, 4 Sep 2013 15:22:37 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-scrub.c

There will be four kinds of return value for command "scrub start":

0: scrub dosen't find errors and return success.
1: usage or syntax errors.
3: scrub finds errors and correct all of them.
4: scrub finds errors and some of them are not correctable.

Three kinds of return values for scrub cancel/resume:

0: cancel successfully.
1: usage or syntax errors.
2: cancel a not started or finished scrub.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-replace.c
Wang Shilong [Wed, 4 Sep 2013 15:22:36 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-replace.c

There are 3 kinds of return values in replace cancel:

0: cancel successfully.
1: usage or syntal errors
2: cancel a not started or finished replacing operations.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in random-test.c
Wang Shilong [Wed, 4 Sep 2013 15:22:34 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in random-test.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in dir-test.c
Wang Shilong [Wed, 4 Sep 2013 15:22:33 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in dir-test.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in send-test.c
Wang Shilong [Wed, 4 Sep 2013 15:22:32 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in send-test.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in btrfs-zero-log.c
Wang Shilong [Wed, 4 Sep 2013 15:22:31 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in btrfs-zero-log.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in btrfs-imgae.c
Wang Shilong [Wed, 4 Sep 2013 15:22:30 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in btrfs-imgae.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-send.c
Wang Shilong [Wed, 4 Sep 2013 15:22:29 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-send.c

If btrfs send return failure, we return 1,otherwise 0 will be returned.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-restore.c
Wang Shilong [Wed, 4 Sep 2013 15:22:28 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-restore.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-receive.c
Wang Shilong [Wed, 4 Sep 2013 15:22:27 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-receive.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-quota.c
Wang Shilong [Wed, 4 Sep 2013 15:22:26 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-quota.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-qgroup.c
Wang Shilong [Wed, 4 Sep 2013 15:22:25 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-qgroup.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-inspect.c
Wang Shilong [Wed, 4 Sep 2013 15:22:24 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-inspect.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-filesystem.c
Wang Shilong [Wed, 4 Sep 2013 15:22:23 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-filesystem.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-device.c
Wang Shilong [Wed, 4 Sep 2013 15:22:22 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-device.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-chunk.c
Wang Shilong [Wed, 4 Sep 2013 15:22:20 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-chunk.c

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix magic return value in cmds-subvolume.c
Wang Shilong [Wed, 4 Sep 2013 15:22:19 +0000 (23:22 +0800)]
Btrfs-progs: fix magic return value in cmds-subvolume.c

The patch also fixes some coding styles problems.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: return 1 rather than 129 in usage()
Wang Shilong [Wed, 4 Sep 2013 15:22:18 +0000 (23:22 +0800)]
Btrfs-progs: return 1 rather than 129 in usage()

if usage or syntax error happens, we return 1.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix compile warning in is_ssd()
Wang Shilong [Wed, 4 Sep 2013 11:43:20 +0000 (19:43 +0800)]
Btrfs-progs: fix compile warning in is_ssd()

mkfs.c: In function ‘is_ssd’:
mkfs.c:1168:26: warning: ignoring return value of ‘blkid_devno_to_wholedisk’,
declared with attribute warn_unused_result [-Wunused-result]
  blkid_devno_to_wholedisk(devno, wholedisk, sizeof(wholedisk), NULL);

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: mkfs should check for small vol well before
Anand Jain [Fri, 30 Aug 2013 08:50:37 +0000 (16:50 +0800)]
btrfs-progs: mkfs should check for small vol well before

This fix the regression introduced by 830427d
that it no more creates the FS if disk is small
and if no mixed option is provided.
This patch will bring it to the original design
which will force mixed profile when disk is small
and go ahead to create the FS.

Which also means that before we open the device
for the write we should also check if disk is small.

v2: fixes the checkpatch.pl warnings

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: avoid write to the disk before sure to create fs
Anand Jain [Wed, 7 Aug 2013 12:11:25 +0000 (20:11 +0800)]
btrfs-progs: avoid write to the disk before sure to create fs

This patch provides fix for the following bug,

When mkfs.btrfs fails the disks shouldn't be written.
------------
btrfs fi show /dev/sdb
Label: none  uuid: 60fb76f4-3b4d-4632-a7da-6a44dea5573d
        Total devices 1 FS bytes used 24.00KiB
        devid    1 size 2.00GiB used 20.00MiB path /dev/sdb

mkfs.btrfs -dsingle -mraid1 /dev/sdb -f
::
unable to create FS with metadata profile 16 (have 1 devices)

btrfs fi show /dev/sdb
Label: none  uuid: 2da2179d-ecb1-4a4e-a44d-e7613a08c18d
        Total devices 1 FS bytes used 24.00KiB
        devid    1 size 2.00GiB used 20.00MiB path /dev/sdb
-------------

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: fix restore command leaving corrupted files
Filipe David Borba Manana [Tue, 3 Sep 2013 11:19:58 +0000 (12:19 +0100)]
Btrfs-progs: fix restore command leaving corrupted files

When there are files that have parts shared with snapshots, the
restore command was incorrectly restoring them, as it was not
taking into account the offset and number of bytes fields from
the file extent item. Besides leaving the recovered file corrupt,
it was also inneficient as it read and wrote more data than needed
(with each extent copy overwriting portions of the one previously
written).

The following steps show how to reproduce this corruption issue:

$ mkfs.btrfs -f  /dev/sdb3
$ mount /dev/sdb3 /mnt/btrfs
$ perl -e '$d = "\x41" . ("\x00" x (1024*1024+349)); open($f,">","/mnt/btrfs/foobar"); print $f $d; close($f);'
$ du -b /mnt/btrfs/foobar
1048926 /mnt/btrfs/foobar
$ md5sum /mnt/btrfs/foobar
f9f778f3a7410c40e4ed104a3a63c3c4  /mnt/btrfs/foobar

$ btrfs subvolume snapshot /mnt/btrfs /mnt/btrfs/my_snap
$ perl -e 'open($f, "+<", "/mnt/btrfs/foobar"); seek($f, 4096, 0); print $f "\xff"; close($f);'
$ md5sum /mnt/btrfs/foobar
b983fcefd4622a03a78936484c40272b  /mnt/btrfs/foobar
$ umount /mnt/btrfs

$ btrfs restore /dev/sdb3 /tmp/copy
$ du -b /tmp/copy/foobar
1048926 /tmp/copy/foobar
$ md5sum /tmp/copy/foobar
88db338cbc1c44dfabae083f1ce642d5  /tmp/copy/foobar
$ od -t x1 -j 8192 -N 4 /tmp/copy/foobar
0020000 41 00 00 00
0020004
$ mount /dev/sdb3 /mnt/btrfs
$ od -t x1 -j 8192 -N 4 /mnt/btrfs/foobar
0020000 00 00 00 00
0020004
$ md5sum /mnt/btrfs/foobar
b983fcefd4622a03a78936484c40272b  /mnt/btrfs/foobar

Tested this change with zlib, lzo compression and file sizes larger
than 1GiB, and found no regression or other corruption issues (so far
at least).

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: Add Makefile infrastructure for subdirs
Eric Sandeen [Tue, 11 Jun 2013 23:15:17 +0000 (18:15 -0500)]
Btrfs-progs: Add Makefile infrastructure for subdirs

Preparatory patch to move cmd & test files into their
own subdirs.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: mkfs can now create fs with skinny extents
Filipe David Borba Manana [Sat, 17 Aug 2013 13:48:29 +0000 (14:48 +0100)]
Btrfs-progs: mkfs can now create fs with skinny extents

Before this change, passing -O skinny-metadata to mkfs.btrfs would
only set the skinny metadata incompat flag in the super block after
the filesystem was created. This change makes mkfs.btrfs directly
create a filesystem with only skinny extents for metadata.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: add restore command's -x flag to man page
Filipe David Borba Manana [Sat, 17 Aug 2013 13:16:02 +0000 (14:16 +0100)]
Btrfs-progs: add restore command's -x flag to man page

This is a recent flag added to the restore command that allows
to restore xattrs. It was missing in the man page.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: get C=1 sparse checking working again
Zach Brown [Wed, 14 Aug 2013 23:16:31 +0000 (16:16 -0700)]
btrfs-progs: get C=1 sparse checking working again

There were a few problems that were breaking sparse checking:

- We were defining CHECK_ENDIAN late in the environment, after
  linux/fs.h has been included which defines __force and __bitwise in
  confusing ways that conflict with ours.  Define it up with __CHECKER__
  so that linux/fs.h and our copy are acting on the same input.

- We had manually set a few of gcc's internal defines to give to sparse.
  It's easier to just ask gcc for all the defines it sets and hand those
  to sparse.

- We weren't passing the same *FLAGS to sparse as we were to CC.

- glibc has so many errors with FORTIFY turned on that sparse gives up
  and doesn't show us any errors from our code.  It's a questionable
  hack to always turn on FORTIFY ourselves, so we'll just not do that
  when building with sparse.

And add a nice '[SP]' quiet output line for sparse checks.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: use NULL instead of 0
Zach Brown [Wed, 14 Aug 2013 23:16:45 +0000 (16:16 -0700)]
btrfs-progs: use NULL instead of 0

These were mostly in option structs but there were a few gross string
pointer arguments given as 0.

Signed-off-by: Zach Brown <zab@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>