platform/upstream/btrfs-progs.git
10 years agobtrfs-progs: call endmntent in btrfs_scan_kernel
David Sterba [Wed, 27 Nov 2013 17:14:58 +0000 (18:14 +0100)]
btrfs-progs: call endmntent in btrfs_scan_kernel

btrfs_scan_kernel() does a getmntent() but never releases the
filedescriptor it gets back from that.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=64711

Reported-by: Arjan van de Ven <arjan@linux.intel.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agoBtrfs-progs: fix the mismatch of extent buffer's space
Liu Bo [Wed, 27 Nov 2013 16:08:24 +0000 (00:08 +0800)]
Btrfs-progs: fix the mismatch of extent buffer's space

Now we set @refs to 2 on creating a new extent buffer, meanwhile we
allocate the needed free space, but we don't give enough free_extent_buffer()
to reduce the eb's references to zero so that the eb can finally be freed,
so the problem is we has decrease the referene count of backrefs to zero, which
ends up releasing the space occupied by the eb, and this space can be allocated
again for something else(another eb or disk), usually a crash(core dump) will
occur, I've hit a crash in rb_insert() because another eb re-use the space while
the original one is floating around.

We should do the same thing as the kernel code does, it's necessary to initialize
@refs to 1 instead of 2, this helps us get rid of the above problem.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agoBtrfs-progs: chunk-recover: use right size when allocating chunk root node
Wang Shilong [Wed, 27 Nov 2013 14:43:56 +0000 (22:43 +0800)]
Btrfs-progs: chunk-recover: use right size when allocating chunk root node

When allocating chunk root node, we should use nodesize rather than sectorsize,
this will casue regression when making other nodesize choice.(for example 16k size now)

Reported-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agobtrfs-progs: Make btrfs_header_chunk_tree_uuid() return unsigned long
Ross Kirk [Wed, 2 Oct 2013 12:28:27 +0000 (13:28 +0100)]
btrfs-progs: Make btrfs_header_chunk_tree_uuid() return unsigned long

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

From btrfs commit b308bc2f05a86e728bd035e21a4974acd05f4d1e

Signed-off-by: Ross Kirk <ross.kirk@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agobtrfs-progs: define BTRFS_UUID_UNPARSE_SIZE for uuid unparse buf size
Anand Jain [Mon, 4 Nov 2013 07:17:41 +0000 (15:17 +0800)]
btrfs-progs: define BTRFS_UUID_UNPARSE_SIZE for uuid unparse buf size

we use 37 as the allocation size to hold the uuid_unparse, here
it defines BTRFS_UUID_UNPARSE_SIZE for the same.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agoBtrfs-progs: let get_label return the label instead of printing it
Filipe David Borba Manana [Tue, 12 Nov 2013 13:41:42 +0000 (13:41 +0000)]
Btrfs-progs: let get_label return the label instead of printing it

get_label prints the label at the moment. Change this so that
the label is returned and printing is done by the caller.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agoBtrfs-progs: try harder to make btrfs receive successfully
Wang Shilong [Wed, 13 Nov 2013 09:25:46 +0000 (17:25 +0800)]
Btrfs-progs: try harder to make btrfs receive successfully

Steps to reproduce:
# mkfs.btrfs -f <dev>
# mount <dev> <mnt>
# mkdir <mnt>/backup
# btrfs sub create <mnt>/subv
# btrfs sub snapshot -r <mnt>/subv <mnt>/snap1
# btrfs sub snapshot -r <mnt>/subv <mnt>/snap2
# btrfs send <mnt>/snap2 -p <mnt>/snap1 -f sent_file
# btrfs receive -f sent_file <mnt>/backup

Above steps will make btrfs receive fails with "ERROR: can not find
parent subvolume", this is because we try to find parent subvolume by
RECEIVED_SUBVOL_KEY,and it will return ENOENT if parent snapshot has not
been sent or it has been deleted. Actually, we can try harder to find
whether parent subvolume exists by searching uuid key.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: Stefan Behrens <sbehrens@giantdisaster.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agobtrfs-progs: make it static if function isn't called outside
Anand Jain [Wed, 13 Nov 2013 08:19:39 +0000 (16:19 +0800)]
btrfs-progs: make it static if function isn't called outside

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agobtrfs-progs: use /proc/self/mounts
Anand Jain [Thu, 7 Nov 2013 02:53:43 +0000 (10:53 +0800)]
btrfs-progs: use /proc/self/mounts

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agobtrfs-progs: fix loop device mount checks
David Sterba [Fri, 20 Sep 2013 16:52:18 +0000 (18:52 +0200)]
btrfs-progs: fix loop device mount checks

When creating a fs on a loop device, mkfs checks whether the same file
is not already mounted, but a backing file of another loop dev does not
exist, mkfs fails. This fixes a bug during openSUSE installation.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agoBtrfs-progs: don't output baffling message when checking a fresh fs
Miao Xie [Fri, 25 Oct 2013 09:30:45 +0000 (17:30 +0800)]
Btrfs-progs: don't output baffling message when checking a fresh fs

As we know,  a new fs doesn't have space cache, so we set the cache generation
of the super block to be -1ULL, it is not equal to the fs generation. But the
check program didn't consider this case, and output the following message

  cache and super generation don't match, space cache will be invalidated

directly, it would be baffling the users. So we should avoid outputing such
message. This patch fixes this problem.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agoBtrfs-progs: allow --init-extent-tree to work when extent tree is borked
Josef Bacik [Fri, 25 Oct 2013 18:01:40 +0000 (14:01 -0400)]
Btrfs-progs: allow --init-extent-tree to work when extent tree is borked

Unfortunately you can't run --init-extent-tree if you can't actually read the
extent root.  Fix this by allowing partial starts with no extent root and then
have fsck only check to see if the extent root is uptodate _after_ the check to
see if we are init'ing the extent tree.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agobtrfs-progs: make filesystem show by label work
Anand Jain [Thu, 24 Oct 2013 14:42:56 +0000 (22:42 +0800)]
btrfs-progs: make filesystem show by label work

with design revamp around filesystem show the fsid filter
by label wasn't planned. but apparently that seemed to be
necessary. this patch will fix it.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agobtrfs-progs: remove cmds-chunk.c
David Sterba [Fri, 25 Oct 2013 10:01:44 +0000 (12:01 +0200)]
btrfs-progs: remove cmds-chunk.c

Commit "btrfs-progs: separate command and implementation of
chunk-recover code" moved contents of this file to chunk-recover.c but
failed to remove the file cmds-chunk.c

Reported-by: Mitch Harder <mitch.harder@sabayonlinux.org>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agoBtrfs-progs: fix btrfsck improper prompt on dropping snapshots
Gui Hecheng [Thu, 17 Oct 2013 06:25:29 +0000 (14:25 +0800)]
Btrfs-progs: fix btrfsck improper prompt on dropping snapshots

Exec btrfsck on btrfs with snapshots that are under a dropping
progress will cause prompt on "ref mismatch".
However we do not want this kind of prompt, since an remount
operation will continue the dropping progress.
Here the prompt is nonsense.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
10 years agoUpdate the version
Chris Mason [Mon, 25 Nov 2013 20:48:57 +0000 (15:48 -0500)]
Update the version

Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoAdd -fno-strict-aliasing
Chris Mason [Fri, 22 Nov 2013 13:41:50 +0000 (08:41 -0500)]
Add -fno-strict-aliasing

Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs filesystem show: skip duplicate fsids
Chris Mason [Mon, 18 Nov 2013 19:18:08 +0000 (14:18 -0500)]
btrfs filesystem show: skip duplicate fsids

If a given filesystem is mounted more than once, btrfs fi show will
print dups.  This adds a quick and dirty hash table of fsids it
has already printed and makes sure we don't print any fsid more than
once.

Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: for mixed group check opt before default raid profile is enforced
Anand Jain [Fri, 15 Nov 2013 11:11:09 +0000 (19:11 +0800)]
btrfs-progs: for mixed group check opt before default raid profile is enforced

This fixes the regression introduced with the patch

    btrfs-progs: avoid write to the disk before sure to create fs

what happened with this patch is it missed the check to see if the
user has the option set before pushing the defaults.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: mkfs: enable extended refs by default
David Sterba [Thu, 14 Nov 2013 14:09:53 +0000 (15:09 +0100)]
btrfs-progs: mkfs: enable extended refs by default

The feature has been introduced in kernel 3.7 and enabling it by
default is desired.

All features enabled by default are marked as such in
'mkfs.btrfs -O list-all' output.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: man: add rescue super-recover bits
David Sterba [Thu, 14 Nov 2013 13:40:43 +0000 (14:40 +0100)]
btrfs-progs: man: add rescue super-recover bits

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: mkfs: extend -O syntax to disable features
David Sterba [Thu, 14 Nov 2013 13:30:45 +0000 (14:30 +0100)]
btrfs-progs: mkfs: extend -O syntax to disable features

A way of disabling features that are on by default in case it's not
wanted, eg. due to lack of support in the used kernel.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: remove extra uuid-tree.o in objects
Emil Karlson [Mon, 11 Nov 2013 16:22:24 +0000 (18:22 +0200)]
btrfs-progs: remove extra uuid-tree.o in objects

This fixes static compile target of btrfs-progs.

Signed-off-by: Emil Karlson <jekarlson@gmail.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agomkfs: change default metadata blocksize to 16KB
Chris Mason [Fri, 8 Nov 2013 18:51:52 +0000 (13:51 -0500)]
mkfs: change default metadata blocksize to 16KB

16KB is faster and leads to less metadata fragmentation in almost all
workloads.  It does slightly increase lock contention on the root nodes
in some workloads, but that is best dealt with by adding more subvolumes
(for now).

This uses 16KB or the page size, whichever is bigger.  If you're doing a
mixed block group mkfs, it uses the sectorsize instead.

Since the kernel refuses to mount a mixed block group FS where the
metadata leaf size doesn't match the data sectorsize, this also adds a
similar check during mkfs.

Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: annotate fallthroughs in parse_limit
Eric Sandeen [Wed, 6 Nov 2013 23:15:55 +0000 (17:15 -0600)]
btrfs-progs: annotate fallthroughs in parse_limit

We intentionally fall through these case statements;
just annotate it to be clear.

Resolves-Coverity-CID: 1054884
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: annotate fallthroughs in parse_size
Eric Sandeen [Wed, 6 Nov 2013 23:15:54 +0000 (17:15 -0600)]
btrfs-progs: annotate fallthroughs in parse_size

We intentionally fall through these case statements;
just annotate it to be clear.

Resolves-Coverity-CID: 1054887
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: check for fstat failure in cmd_defrag
Eric Sandeen [Wed, 6 Nov 2013 23:15:53 +0000 (17:15 -0600)]
btrfs-progs: check for fstat failure in cmd_defrag

Resolves-Coverity-CID: 1125924
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: check btrfs_scan_one_device in btrfs_scan_lblkid()
Eric Sandeen [Wed, 6 Nov 2013 23:15:52 +0000 (17:15 -0600)]
btrfs-progs: check btrfs_scan_one_device in btrfs_scan_lblkid()

Even if it's "definitely" btrfs at this point,
btrfs_scan_one_device could fail for other reasons.

Check the return value, warn if it fails, and skip
the device register.

Resolves-Coverity-CID: 1125925
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: remove more dead code from check_extent_refs
Eric Sandeen [Wed, 6 Nov 2013 23:15:51 +0000 (17:15 -0600)]
btrfs-progs: remove more dead code from check_extent_refs

e0a04278 removed a bunch of dead code but left one little
bit; reinit is always 0, so btrfs_read_block_groups is
never called from here.

Resolves-Coverity-CID: 1125926
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: pass positive errno to strerror in cmd_df()
Eric Sandeen [Wed, 6 Nov 2013 23:15:50 +0000 (17:15 -0600)]
btrfs-progs: pass positive errno to strerror in cmd_df()

get_df returns a negative error number, but then
we pass it to strerror, which wants a positive value...

Resolves-Coverity-CID: 1125929
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: Check for open failure in btrfs_scan_lblkid()
Eric Sandeen [Wed, 6 Nov 2013 23:15:49 +0000 (17:15 -0600)]
btrfs-progs: Check for open failure in btrfs_scan_lblkid()

open can fail, of course.

Resolves-Coverity-CID: 1125925
Resolves-Coverity-CID: 1125930
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: btrfs_scan_kernel(): fd==0 is not an error
Eric Sandeen [Wed, 6 Nov 2013 23:15:48 +0000 (17:15 -0600)]
btrfs-progs: btrfs_scan_kernel(): fd==0 is not an error

The error return from open is -1, so test that, not 0,
for success/failure.

Resolves-Coverity-CID: 1125931
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: don't leak buffer on add_file_items() error
Eric Sandeen [Wed, 6 Nov 2013 23:15:46 +0000 (17:15 -0600)]
btrfs-progs: don't leak buffer on add_file_items() error

add_file_items() leaked "buffer" on this error return.
Free it first.

Resolves-Coverity-CID: 1125937
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: fix leak of "buf" in make_btrfs() error paths
Eric Sandeen [Wed, 6 Nov 2013 23:15:45 +0000 (17:15 -0600)]
btrfs-progs: fix leak of "buf" in make_btrfs() error paths

If any pwrite failed we leaked the allocated "buf" on
return from the function.  "goto out" takes care of
those paths.

Resolves-Coverity-CID: 1125938
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: don't leak fd in test_dev_for_mkfs() error paths
Eric Sandeen [Wed, 6 Nov 2013 23:15:44 +0000 (17:15 -0600)]
btrfs-progs: don't leak fd in test_dev_for_mkfs() error paths

Close fd before we return on error paths.

Resolves-Coverity-CID: 1125939
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: fix test for return of realpath in find_mount_root()
Eric Sandeen [Wed, 6 Nov 2013 23:15:43 +0000 (17:15 -0600)]
btrfs-progs: fix test for return of realpath in find_mount_root()

find_mount_root() tries to test for realpath() failure, but
tests the wrong value.  Fix it.

Resolves-Coverity-CID: 1125940
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: use strncpy in btrfs_scan_lblkid()
Eric Sandeen [Wed, 6 Nov 2013 23:15:42 +0000 (17:15 -0600)]
btrfs-progs: use strncpy in btrfs_scan_lblkid()

Use strncpy(... ,PATH_MAX) to be sure we don't overflow
the path[PATH_MAX] array.

Resolves-Coverity-CID: 1125941
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: fix error returns in get_df()
Eric Sandeen [Wed, 6 Nov 2013 23:15:41 +0000 (17:15 -0600)]
btrfs-progs: fix error returns in get_df()

get_df returns -ERRNO, or maybe (+)errno, or even 0 in
the case where we inexplicably got 0 total_spaces from
the BTRFS_IOC_SPACE_INFO.

Consistently return a negative error number, and return
-ENOENT rather than 0 for total_spaces == 0, so that the
caller will know that **sargs_ret hasn't been set up.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: fix potential double-frees in cmd_subvol_delete()
Eric Sandeen [Wed, 6 Nov 2013 23:15:40 +0000 (17:15 -0600)]
btrfs-progs: fix potential double-frees in cmd_subvol_delete()

If we "goto again" in cmd_subvol_delete(), and error out to out:
before re-allocating the dupdname and dupvname pointers, we'll
double-free them.

Set them to NULL after freeing to avoid this.

Resolves-Coverity-CID: 1125944
Resolves-Coverity-CID: 1125945
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: set return value to 0 if subvolume get-default successfully
Eryu Guan [Wed, 6 Nov 2013 10:49:12 +0000 (18:49 +0800)]
Btrfs-progs: set return value to 0 if subvolume get-default successfully

cmd_subvol_get_default() returns 1 even if finds default subvolume
successfully.

Set the correct return value.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: rework open_ctree to take flags, add a new one V2
Josef Bacik [Mon, 28 Oct 2013 18:28:43 +0000 (14:28 -0400)]
Btrfs-progs: rework open_ctree to take flags, add a new one V2

So I needed to add a flag to not try to read block groups when doing
--init-extent-tree since we could hang there, but that meant adding a whole
other 0/1 type flag to open_ctree_fs_info.  So instead I've converted it all
over to using a flags setting and added the flag that I needed.  This has been
tested with xfstests and make test.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: add -b to btrfsck to look at backup roots
Josef Bacik [Wed, 23 Oct 2013 20:24:03 +0000 (16:24 -0400)]
Btrfs-progs: add -b to btrfsck to look at backup roots

In some cases the tree root is so hosed we can't get anything useful out of it.
So add the -b option to btrfsck to make us look for the most recent backup tree
root to use for repair.  Then we can hopefully get ourselves into a working
state.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: add filter for deleted but uncleanded subvolumes
David Sterba [Wed, 23 Oct 2013 17:00:09 +0000 (19:00 +0200)]
btrfs-progs: add filter for deleted but uncleanded subvolumes

New option to subvolume list that acts as a global filter and applies
the other filters to either live subvolumes or the uncleaned ones.

The path to the deleted subvolumes is lost at the deletion time, sample
output looks like:

ID 259 gen 7 top level 0 path <FS_TREE>/DELETED

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: just return -ENOENT if we don't find the root item
Josef Bacik [Wed, 23 Oct 2013 16:18:03 +0000 (12:18 -0400)]
Btrfs-progs: just return -ENOENT if we don't find the root item

We were bug_on(slot == 0), but that's just obnoxious, return -ENOENT so we can
handle the situation properly.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: make pretty_sizes take u64 instead of a double
Josef Bacik [Tue, 22 Oct 2013 14:10:21 +0000 (10:10 -0400)]
Btrfs-progs: make pretty_sizes take u64 instead of a double

This got changed to a double but all the callers still use a u64, which causes
us to segfault sometimes because of some weird C voodoo that I had to have
explained to me.  Apparently because we're using a double the compiler will use
the floating point registers to hold our argument which ends up not being
aligned properly if you don't actually give it a double so it will cause
problems for other things, in our case it was screwing up str_bytes so it was
larger than the actual size of the str.  This patch fixes the segfault.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: filesystem show of specified mounted disk should work
Anand Jain [Tue, 22 Oct 2013 05:53:22 +0000 (13:53 +0800)]
btrfs-progs: filesystem show of specified mounted disk should work

Originally, thinking was user will use mount point if the disk
is mounted. But thats not really true, actually user don't
(or shouldn't) care to check if disk mounted, so whether disk
is mounted/unmounted when disk path is specified it should work.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: make get_btrfs_mount callable
Anand Jain [Tue, 22 Oct 2013 05:53:21 +0000 (13:53 +0800)]
btrfs-progs: make get_btrfs_mount callable

get_btrfs_mount is reusable function but it is printing
errors, this removes it. Here the parent function of
open_path_or_dev_mnt does print error msg on error.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agoBtrfs-progs: make sure we return an error when we can't add a device
Josef Bacik [Mon, 21 Oct 2013 13:58:08 +0000 (09:58 -0400)]
Btrfs-progs: make sure we return an error when we can't add a device

I noticed xfstests was failing in a weird way but it was because our device add
was failing but not actually returning an error so we were failing further down
the test.  Fix this by making sure we return an error if we fail the mkfs tests.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agomkfs: fix mkfs -r to properly allocate space
Chris Mason [Wed, 16 Oct 2013 14:36:55 +0000 (10:36 -0400)]
mkfs: fix mkfs -r to properly allocate space

mkfs -r wasn't creating chunks properly, making it very difficult to
allocate space for anything except tiny filesystems.

This changes it around to use more of the generic infrastructure, and
to do actual logical->physical block number translation.

It also allocates space to the files in smaller extents (max 1MB), which
keeps the allocator from trying to allocate an extent bigger than a
single chunk.

It doesn't quite support multi-device mkfs -r yet, but is much closer.

Signed-off-by: Chris Mason <chris.mason@fusionio.com>
10 years agobtrfs-progs: use the marco BTRFS_UPDATE_KERNEL where needed
Anand Jain [Fri, 11 Oct 2013 11:52:54 +0000 (19:52 +0800)]
btrfs-progs: use the marco BTRFS_UPDATE_KERNEL where needed

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: device scan use BTRFS_SCAN_LBLKID by default
Anand Jain [Fri, 11 Oct 2013 11:52:53 +0000 (19:52 +0800)]
btrfs-progs: device scan use BTRFS_SCAN_LBLKID by default

with this patch, BTRFS_SCAN_LBLKID (which leverages lblkid
to look for btrfs disks) would be the default scan method
to look for the btrfs disks. And thus the output as seen
in the latest btrfs fi show and btrfs fi show -m for the
mounted disks will have the consistent disks path.
(it was inconsistent (across disks) because btrfs dev scan
provided a different path from the mount command eg. below)

    devid    1 size 1.98GiB used 435.00MiB path /dev/mapper/mpatha
    devid    2 size 2.00GiB used 415.00MiB path /dev/dm-1

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: use BTRFS_SCAN_LBLKID as default scan in filesystem show
Anand Jain [Tue, 8 Oct 2013 03:41:40 +0000 (11:41 +0800)]
btrfs-progs: use BTRFS_SCAN_LBLKID as default scan in filesystem show

btrfs progs has to scan for the btrfs disks for two main reasons,
 one to register them with the btrfs kernel (under btrfs dev scan)
 2nd to report btrfs disks to the user (under btrfs fi show)
 (there few more minor reasons like check_mounted etc..).

 To facilitate the scan, in total we have the following methods
 to scan for the btrfs

 BTRFS_SCAN_PROC
  which uses the /proc/partitions to look for the disks, when
  scanning it does it twice first would look for non dm- paths
  and in the 2nd scan it would pick only dm- paths.

 BTRFS_SCAN_DEV
  which scans all the block dev under /dev as they appear during
  scanning.

 BTRFS_SCAN_LBLKID
  this uses the library functions provided  by the lblkid to get
  only disks which contains the btrfs SB.

 The better method to use would be BTRFS_SCAN_LBLKID for the obvious
 reasons we don't have to reinvent that feature with in btrfs-progs.

 For the btrfs fi show - This patch will..
   - make BTRFS_SCAN_LBLKID as the default scan option

   (BTRFS_SCAN_DEV is accessible under the option --all-devices and
   BTRFS_SCAN_PROC won't be used by btrfs fi show any more)

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: use kernel for mounted disk for show
Anand Jain [Tue, 8 Oct 2013 03:41:38 +0000 (11:41 +0800)]
btrfs-progs: use kernel for mounted disk for show

As of now btrfs filesystem show reads directly from
disks. So sometimes output can be stale, mainly when
user wants to cross verify their operation like,
label or device delete or add... etc. so this
patch will read from the kernel ioctl if it finds
that disk is mounted.

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: 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>