platform/upstream/btrfs-progs.git
9 years agobtrfs-progs: remove dead condition for btrfs_map_block
Gui Hecheng [Wed, 26 Nov 2014 02:43:40 +0000 (10:43 +0800)]
btrfs-progs: remove dead condition for btrfs_map_block

The @search_cache_extent() only returns the next cache_extent or NULL,
it will never return the previous cache_extent.
So just remove the dead condition for previous cache_extent handle.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: cleanup duplicate assignment of variable leaf for btrfs-restore
Gui Hecheng [Thu, 21 Aug 2014 02:56:52 +0000 (10:56 +0800)]
btrfs-progs: cleanup duplicate assignment of variable leaf for btrfs-restore

The value of variable leaf in while loop don't have to be set
for every round. Just move it outside.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: remove duplicate pthread libs from build
David Sterba [Thu, 4 Dec 2014 16:17:56 +0000 (17:17 +0100)]
btrfs-progs: remove duplicate pthread libs from build

With the task-utils it's in the default LIBS flags now. We want to use
-pthread as it also sets flags for the preprocessor.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: convert: use task for progress indication of metadata creation
Silvio Fricke [Sun, 9 Nov 2014 22:16:56 +0000 (23:16 +0100)]
btrfs-progs: convert: use task for progress indication of metadata creation

Signed-off-by: Silvio Fricke <silvio.fricke@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: add task-utils
Silvio Fricke [Sun, 9 Nov 2014 22:16:55 +0000 (23:16 +0100)]
btrfs-progs: add task-utils

Support for monitoring progress of running tasks, based on timerfd and
pthreads.

Signed-off-by: Silvio Fricke <silvio.fricke@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: allow fsck to take the tree bytenr
Josef Bacik [Fri, 31 Oct 2014 18:01:21 +0000 (14:01 -0400)]
Btrfs-progs: allow fsck to take the tree bytenr

Sometimes we have a pretty corrupted fs but have an old tree bytenr that we
could use, add the ability to specify the tree root bytenr.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Tested-by: Ansgar Hockmann-Stolle <ansgar.hockmann-stolle@uni-osnabrueck.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Add simple python front end to the search ioctl
Chris Mason [Tue, 23 Sep 2014 16:39:23 +0000 (12:39 -0400)]
btrfs-progs: Add simple python front end to the search ioctl

This is a starting point for a debugfs style python interface using
the search ioctl.  For now it can only do one thing, which is to
print out all the extents in a file and calculate the compression ratio.

Over time it will grow more features, especially for the kinds of things
we might run btrfs-debug-tree to find out.  Expect the usage and output
to change dramatically over time (don't hard code to it).

Signed-off-by: Chris Mason <clm@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix wrong num_devices for btrfs fi show with seed devices
Gui Hecheng [Thu, 13 Nov 2014 01:19:29 +0000 (09:19 +0800)]
btrfs-progs: fix wrong num_devices for btrfs fi show with seed devices

The @fi_args->num_devices in @get_fs_info() does not include seed devices.
We could just correct it by searching the chunk tree and count how
many dev_items there are in total which includes seed devices.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: remove BUG_ON on num of devices for btrfs fi show
Gui Hecheng [Fri, 7 Nov 2014 02:07:42 +0000 (10:07 +0800)]
btrfs-progs: remove BUG_ON on num of devices for btrfs fi show

The following BUG_ON:
BUG_ON(ndevs >= fi_args->num_devices)
is not needed, because it always fails with seed devices present.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: skip fs with no seed when build seed/sprout mapping for fi show
Gui Hecheng [Tue, 11 Nov 2014 06:39:14 +0000 (14:39 +0800)]
btrfs-progs: skip fs with no seed when build seed/sprout mapping for fi show

There is no need to try to build seed/sprout mapping for those btrfs
without seed devices, so just skip such fs.
We could get the total number of devices from the disk super block, if it
equals the number of items in list @fs_devices->devices, then there shouldn't
be any seed devices.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: rebuild missing block group during chunk recovery if possible
Qu Wenruo [Thu, 30 Oct 2014 02:54:03 +0000 (10:54 +0800)]
btrfs-progs: rebuild missing block group during chunk recovery if possible

Before the patch, chunk will be considered bad if the corresponding
block group is missing, even the only uncertain data is the 'used'
member of the block group.

This patch will try to recalculate the 'used' value of the block group
and rebuild it.
So even only chunk item and dev extent item is found, the chunk can be
recovered.
Although if extent tree is damanged and needed extent item can't be
read, the block group's 'used' value will be the block group length, to
prevent any later write/block reserve damaging the block group.
In that case, we will prompt user and recommend them to use
'--init-extent-tree' to rebuild extent tree if possible.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: make the search target device routine more clear for fi show
Gui Hecheng [Thu, 30 Oct 2014 02:44:49 +0000 (10:44 +0800)]
btrfs-progs: make the search target device routine more clear for fi show

Extract the procedure of searching for a target device for fi show
from the @map_seed_devices() function to make it more clear.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: rework calculations of fi usage
David Sterba [Thu, 26 Jun 2014 13:42:24 +0000 (15:42 +0200)]
btrfs-progs: rework calculations of fi usage

This patch reworks the basic calculations of 'fi usage'. It does not address
all problems but should make the code more prepared to do so.

The original code tries to estimate the free space that could lead to negative
numbers for some raid profiles:

Data, RAID1: total=147.00GiB, used=141.92GiB
System, RAID1: total=32.00MiB, used=36.00KiB
Metadata, RAID1: total=2.00GiB, used=1.17GiB
GlobalReserve, single: total=404.00MiB, used=0.00B

Overall:
    Device size:                 279.46GiB
    Device allocated:            298.06GiB
    Device unallocated:           16.00EiB
    Used:                        286.18GiB
    Free (estimated):              8.00EiB      (min: 8.00EiB)
    Data ratio:                       2.00
    Metadata ratio:                   2.00
    Global reserve:              404.00MiB      (used: 0.00B)

Eg. "Device size" - "Device allocated" = negative number or a very large
positive, hence the EiB values.

There are logical and raw numbers multiplied by ratios mixed together,
so the new code makes it explicit which kind is being used. The data and
metadata ratios are calculated separately.

Output after this patch will look like:

Overall:
    Device size:                 558.92GiB
    Device allocated:            298.06GiB
    Device unallocated:          260.86GiB
    Used:                        286.18GiB
    Free (estimated):            135.51GiB      (min: 135.51GiB)
    Data ratio:                       2.00
    Metadata ratio:                   2.00
    Global reserve:              404.00MiB      (used: 0.00B)

Data,RAID1: Size:147.00GiB, Used:141.92GiB
   /dev/sdc      147.00GiB
   /dev/sdd      147.00GiB

Metadata,RAID1: Size:2.00GiB, Used:1.17GiB
   /dev/sdc        2.00GiB
   /dev/sdd        2.00GiB

System,RAID1: Size:32.00MiB, Used:36.00KiB
   /dev/sdc       32.00MiB
   /dev/sdd       32.00MiB

Unallocated:
   /dev/sdc      130.43GiB
   /dev/sdd      130.43GiB

Changes:
* Device size is now the raw size, same for the following three
* Free is the logical size
* Max/min were reduced to just min

Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc        280G  144G  141G  51% /mnt/sdc

The difference between Avail and Free is there because userspace tool does a
different guesswork than kernel.

Issues not addressed by this patch:
* RAID56 profiles are not handled
* mixed profiles are not handled

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix improper output msg for btrfs-fi-usage
Gui Hecheng [Thu, 24 Jul 2014 03:21:54 +0000 (11:21 +0800)]
btrfs-progs: fix improper output msg for btrfs-fi-usage

Even if run as root:
# su
# btrfs file usage <path> <== path exits outside the mnt point
We get the output:
WARNING: ..., run as root
WARNING: ..., run as root
ERROR:...

It is because in load_chunk_info, the errno of ioctl is not judged
but rather the ret value of ioctl is judged. And the ret value of
ioctl is -1 which happens to match -EPERM exactly.
So the outer warning is printed.

Just judge the errno of ioctl and prevent the ret value of load_chunk_info
to be -1 in other error conditions.

For load_device_info, the problem and fix is the same.
After the fix, the 'run as root' WARNINGs will not show up in this condition.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: output the correct path when fi-usage failed
Gui Hecheng [Thu, 24 Jul 2014 03:27:28 +0000 (11:27 +0800)]
btrfs-progs: output the correct path when fi-usage failed

When we exec the following cmd:
# btrfs file usage -t <path>  <-- an invalid path
output:
# ERROR: can't access '-t'
should be:
# ERROR: can't access 'path'

Just replace the static 'argv[1]' with 'argv[i]'.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix wrong data ratio for raid56 in btrfs-file-usage
Gui Hecheng [Thu, 24 Jul 2014 03:21:52 +0000 (11:21 +0800)]
btrfs-progs: fix wrong data ratio for raid56 in btrfs-file-usage

When run btrfs-file-usage on a btrfs with data profile raid5/6,
the output message for "Free" & "Data to device ratio" seems wrong
as follows:
    ...
    Device size:  100.00GiB
    Device allocated:    2.04GiB
    Device unallocated:   97.96GiB
    Used:    1.12MiB
    Free (Estimated):  197.89GiB <== Free > Device size
    Data to device ratio:      198 % <== > 100%
    Global reserve:      0.00B
    ...

It is because the function get_raid56_used() is not iterating the
chunk_info array correctly, it is just repeating adding the first
chunk_info statistics.
Just add a ptr to iterate over the array.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: provide better error message for raid profile mismatch
Hidetoshi Seto [Fri, 16 May 2014 08:20:56 +0000 (17:20 +0900)]
btrfs-progs: provide better error message for raid profile mismatch

Current error messages are like following:
  Error: unable to create FS with metadata profile 32 (have 2 devices)
  Error: unable to create FS with metadata profile 256 (have 2 devices)

Obviously it is hard for users to interpret "profile XX" to proper
meaning, such as "raidN". So use recongizable string instead of
internal numerical value. In case of "DUP", use an explicit message.

Plus this patch fix a bug that message mistake metadata profile
for data profile.

After applying this patch, messages will be like:
  Error: DUP is not allowed when FS have multiple devices
  Error: unable to create FS with metadata profile RAID6 (have 2
  devices but 3 devices are required)

Signed-off-by: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: hide FS occupied from device usage output
David Sterba [Wed, 14 May 2014 18:06:47 +0000 (20:06 +0200)]
btrfs-progs: hide FS occupied from device usage output

The term has not seen an agreement and we don't want to change it once
it's in non-development branches or even released.

Discussion under the patch:
http://thread.gmane.org/gmane.comp.file-systems.btrfs/34627

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: move global reserve to overall summary
David Sterba [Tue, 29 Apr 2014 15:32:22 +0000 (17:32 +0200)]
btrfs-progs: move global reserve to overall summary

It looks confusing among the chunks, it is not in fact a chunk type.

Sample:
Overall:
    Device size:                  35.00GiB
    Device allocated:              8.07GiB
    Device unallocated:           26.93GiB
    Used:                          1.12MiB
    Free (Estimated):             17.57GiB      (Max: 30.98GiB, min: 17.52GiB)
    Data to device ratio:             50 %
    Global reserve:               16.00MiB      (used: 0.00B)
...

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: clean up return codes and paths
David Sterba [Mon, 28 Apr 2014 16:55:05 +0000 (18:55 +0200)]
btrfs-progs: clean up return codes and paths

Use the common patterns with one return statement at the end, pass down error

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: replace df_pretty_sizes with pretty_size_mode
David Sterba [Mon, 28 Apr 2014 16:13:16 +0000 (18:13 +0200)]
btrfs-progs: replace df_pretty_sizes with pretty_size_mode

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: cleanup filesystem/device usage code
David Sterba [Fri, 25 Apr 2014 17:39:11 +0000 (19:39 +0200)]
btrfs-progs: cleanup filesystem/device usage code

The main point of this is to load the device and chunk infos at one
place and pass down to the printers. The EPERM is handled separately, in
case kernel does not give us all the information about chunks or
devices, but we want to warn and print at least something.

For non-root users, 'filesystem usage' prints only the overall stats and
warns about RAID5/6.

The sole cleanup changes affect mostly the modified code and the related
functions, should be reasonably small.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: add section of overall filesystem usage
David Sterba [Fri, 25 Apr 2014 15:24:40 +0000 (17:24 +0200)]
btrfs-progs: add section of overall filesystem usage

The 'fi usage' lacks an overall report, this used to be in the enhanced
df command. Add it back.

Sample:
Overall:
    Device size:                  35.00GiB
    Device allocated:              8.07GiB
    Device unallocated:           26.93GiB
    Used:                          1.12MiB
    Free (Estimated):             17.57GiB      (Max: 30.98GiB, min: 17.52GiB)
    Data to device ratio:             50 %
...

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: compare unallocated space against the correct value
David Sterba [Thu, 24 Apr 2014 16:57:12 +0000 (18:57 +0200)]
btrfs-progs: compare unallocated space against the correct value

The device may not be fully occupied by the filesystem, the value of
Unallocated should not be calculated against the device size but the
size provided by DEV_INFO.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Print more info about device sizes
David Sterba [Thu, 24 Apr 2014 16:32:27 +0000 (18:32 +0200)]
btrfs-progs: Print more info about device sizes

The entire device size may not be available to the filesystem, eg. if
it's modified via resize. Print this information if it can be obtained
from the DEV_INFO ioctl.

Print the device ID on the same line as the device name and move size to
the next line.

Sample:
/dev/sda7, ID: 3
   Device size:            10.00GiB
   FS occupied:             5.00GiB
   Data,RAID10:           512.00MiB
   Metadata,RAID10:       512.00MiB
   System,RAID10:           4.00MiB
   Unallocated:             9.00GiB

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: zero out structures before calling ioctl
David Sterba [Thu, 24 Apr 2014 16:37:50 +0000 (18:37 +0200)]
btrfs-progs: zero out structures before calling ioctl

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: check if we can't get info from ioctls due to permissions
David Sterba [Thu, 24 Apr 2014 13:21:16 +0000 (15:21 +0200)]
btrfs-progs: check if we can't get info from ioctls due to permissions

The TREE_SEARCH ioctl is root-only, FS_INFO will be available for
non-root users with an updated kernel, let the user know.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: move device usage to cmds-device, more cleanups
David Sterba [Wed, 23 Apr 2014 17:00:22 +0000 (19:00 +0200)]
btrfs-progs: move device usage to cmds-device, more cleanups

Move the command definitions where they belong, keep common 'usage'
functions in cmds-fi-disk_usage.c and add exports.

Rename structures containing 'disk' to 'device'.

Fix whitespace in the modified code.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: add original 'df' and rename 'disk_usage' to 'usage'
David Sterba [Wed, 23 Apr 2014 16:47:52 +0000 (18:47 +0200)]
btrfs-progs: add original 'df' and rename 'disk_usage' to 'usage'

Add back the original output of the 'btrfs fi df' command for backward
compatibility. The rich output is moved from 'disk_usage' to 'usage'.

Agreed in http://www.spinics.net/lists/linux-btrfs/msg31698.html

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: fi usage: free memory if realloc fails
Rakesh Pandit [Sat, 19 Apr 2014 11:12:03 +0000 (14:12 +0300)]
Btrfs-progs: fi usage: free memory if realloc fails

Lets not assign *info_ptr to 0 before calling free on it and lose
track of already allocated memory if realloc fails in
add_info_to_list. Lets call free first.

Signed-off-by: Rakesh Pandit <rakesh@tuxera.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Fix memleak in get_raid56_used()
Qu Wenruo [Wed, 19 Mar 2014 06:10:02 +0000 (06:10 +0000)]
btrfs-progs: Fix memleak in get_raid56_used()

Fix memleak in get_raid56_used().

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: cleanup dead return after usage() for fi-disk_usage
Gui Hecheng [Thu, 6 Mar 2014 03:36:46 +0000 (11:36 +0800)]
btrfs-progs: cleanup dead return after usage() for fi-disk_usage

The usage() calls exit() internally, so remove the return after it.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Add btrfs device disk-usage command
Goffredo Baroncelli [Thu, 13 Feb 2014 19:20:12 +0000 (20:20 +0100)]
btrfs-progs: Add btrfs device disk-usage command

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Add command btrfs filesystem disk-usage
Goffredo Baroncelli [Thu, 13 Feb 2014 19:19:50 +0000 (20:19 +0100)]
btrfs-progs: Add command btrfs filesystem disk-usage

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Add helpers functions to handle the printing of data in tabular format
Goffredo Baroncelli [Thu, 13 Feb 2014 19:19:21 +0000 (20:19 +0100)]
btrfs-progs: Add helpers functions to handle the printing of data in tabular format

This patch adds some functions to manage the printing of the data in
tabular format.

The function
struct string_table *table_create(int columns, int rows)
creates an (empty) table.

The functions
char *table_printf(struct string_table *tab, int column,
int row, char *fmt, ...)
char *table_vprintf(struct string_table *tab, int column,
int row, char *fmt, va_list ap)
populate the table with text. To align the text to the left, the text
shall be prefixed with '<', otherwise the text shall be prefixed by a
'>'. If the first character is a '=', the the text is replace by a
sequence of '=' to fill the column width.

The function
void table_free(struct string_table *)
frees all the data associated to the table.

The function
void table_dump(struct string_table *tab)
prints the table on stdout.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Enhance the command btrfs filesystem df
Goffredo Baroncelli [Thu, 13 Feb 2014 19:19:01 +0000 (20:19 +0100)]
btrfs-progs: Enhance the command btrfs filesystem df

Enhance the command "btrfs filesystem df" to show space usage information
for a mount point(s). It shows also an estimation of the space available,
on the basis of the current one used.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
[code moved under #if 0 instead of deletion]
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: move group type and profile pretty printers to utils
David Sterba [Thu, 23 Oct 2014 17:05:31 +0000 (19:05 +0200)]
btrfs-progs: move group type and profile pretty printers to utils

Move and add the btrfs_ prefix.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs progs 3.17.3
David Sterba [Thu, 4 Dec 2014 13:47:18 +0000 (14:47 +0100)]
Btrfs progs 3.17.3

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: print root dir verbose error in fsck
Qu Wenruo [Wed, 3 Dec 2014 04:18:27 +0000 (12:18 +0800)]
btrfs-progs: print root dir verbose error in fsck

Before this patch, when btrfsck found an error in root dir, it will only
output the following message "root %llu root dir %llu error" without any
detailed error.

Just add print_inode_error() to print out the whole error.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: add_refs_for_implied: Use root_id instead of root_bytenr.
Chandan Rajendra [Thu, 27 Nov 2014 04:56:45 +0000 (10:26 +0530)]
Btrfs-progs: add_refs_for_implied: Use root_id instead of root_bytenr.

resolve_one_root() returns the objectid of a tree rather than the logical
address of the root node. Hence using root_bytenr is misleading. Fix this.

Signed-off-by: Chandan Rajendra <chandan@linux.vnet.ibm.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: apply realpath for btrfs fi show when mount point is given
Gui Hecheng [Thu, 27 Nov 2014 02:01:35 +0000 (10:01 +0800)]
btrfs-progs: apply realpath for btrfs fi show when mount point is given

For now,
# btrfs fi show /mnt/btrfs
gives info correctly, while
# btrfs fi show /mnt/btrfs/
gives nothing.

This implies that the @realpath() function should be applied to
unify the behavior.

Made a more clear comment right above the call as well.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix return value problem for btrfs sub show
Gui Hecheng [Thu, 27 Nov 2014 02:01:34 +0000 (10:01 +0800)]
btrfs-progs: fix return value problem for btrfs sub show

If you exec:
# btrfs sub show <dir> <== non-subvolume dir
The cmd print error messages as expected, but returns 0.
By convetion, it should return non-zero and we should explicitly
set it before it goto out.

With other pieces adopted:
1) removed a unnecessary return value set -EINVAL
2) fixed another code branch which may return 0 upon error.
3) with 2) applied, the ret = 0 follows can be removed

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix spacing in error messages
David Sterba [Tue, 25 Nov 2014 18:50:12 +0000 (19:50 +0100)]
btrfs-progs: fix spacing in error messages

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: fix a bug of converting sparse ext2/3/4
Liu Bo [Tue, 25 Nov 2014 09:35:52 +0000 (17:35 +0800)]
Btrfs-progs: fix a bug of converting sparse ext2/3/4

When converting a sparse ext* filesystem, btrfs-convert adds checksum extents
for empty extents, whose disk_bytenr = 0, and this can end up with some weird
problems, one of them is the failure of reading free space cache inode on
mounting converted btrfs.

The fix is simple, just to skip making checksum on empty extents.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: add new dev replace result
Eryu Guan [Mon, 13 Oct 2014 04:42:13 +0000 (12:42 +0800)]
btrfs-progs: add new dev replace result

A new dev replace result was introduced by kernel commit

Btrfs: return failure if btrfs_dev_replace_finishing() failed

Make the userspace know about the new result too.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs progs v3.17.2
David Sterba [Wed, 19 Nov 2014 16:44:56 +0000 (17:44 +0100)]
Btrfs progs v3.17.2

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: use system attr instead of attr library
David Sterba [Tue, 4 Nov 2014 01:12:36 +0000 (02:12 +0100)]
btrfs-progs: use system attr instead of attr library

We use the attr version provided by system in other places already,
now we can remove dependency on the separate attr library.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix compiler warning
Christian Hesse [Tue, 3 Jun 2014 11:29:19 +0000 (13:29 +0200)]
btrfs-progs: fix compiler warning

gcc 4.9.0 gives a warning: array subscript is above array bounds

Checking for "greater or equal" instead of just "equal" fixes this.

The warning is a false positive, appears with -ftree-vrp, but we'd
rather fix it to avoid noise during build.

http://www.mail-archive.com/linux-btrfs%40vger.kernel.org/msg34338.html

Signed-off-by: Christian Hesse <mail@eworm.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: use proper size for argv0 substitution
David Sterba [Tue, 18 Nov 2014 17:05:20 +0000 (18:05 +0100)]
btrfs-progs: use proper size for argv0 substitution

Make run from a long base path will overflow the argv0 buffer during
tests. Otherwise, this would happen for all the standalone binaries that
use set_argv0.

Original report:
https://bbs.archlinux.org/viewtopic.php?id=189861

Reported-by: WorMzy Tykashi <wormzy.tykashi@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: use canonical name for device in btrfs fi show when mounted
Gui Hecheng [Thu, 13 Nov 2014 01:36:20 +0000 (09:36 +0800)]
btrfs-progs: use canonical name for device in btrfs fi show when mounted

When using lvm volumes to check fstests: btrfs/006, it fails like:
     Label: 'TestLabel.006'  uuid: <UUID>
      Total devices <EXACTNUM> FS bytes used <SIZE>
      devid <DEVID> size <SIZE> used <SIZE> path SCRATCH_DEV
    + devid <DEVID> size <SIZE> used <SIZE> path /dev/dm-4
    + devid <DEVID> size <SIZE> used <SIZE> path /dev/dm-5
    + devid <DEVID> size <SIZE> used <SIZE> path /dev/dm-6

The /dev/dm-* points to lvm volumes, use @canonicalize_path() to convert them
and we will make it through. Of course we should do the same thing for dev stat.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Check sb_bytenr with device size before scanning one device.
Qu Wenruo [Wed, 12 Nov 2014 05:52:11 +0000 (13:52 +0800)]
btrfs-progs: Check sb_bytenr with device size before scanning one device.

When using btrfs check with -s option, if using '-s 2' on a small
device which doesn't have the third superblock, "No valid Btrfs found"
will be output, but it is not appropriate.

So check sb_bytenr against device size before scanning a device and
output proper error message.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: test images for new btrfsck functionality
Josef Bacik [Fri, 31 Oct 2014 17:49:07 +0000 (13:49 -0400)]
Btrfs-progs: test images for new btrfsck functionality

These test the recreating of missing dir item/dir index pairs, fixing the no
rootdir inode item and no inode item for normal files.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
9 years agoBtrfs-progs: create missing root dirid
Josef Bacik [Fri, 31 Oct 2014 18:01:28 +0000 (14:01 -0400)]
Btrfs-progs: create missing root dirid

If we just don't have the root dirid stuff go ahead and re-create it, since it
is easily recreated.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: fix missing inode items
Josef Bacik [Fri, 31 Oct 2014 18:01:27 +0000 (14:01 -0400)]
Btrfs-progs: fix missing inode items

If we have all the other items but no inode item we can recreate it for the most
part, with the exception of the permissions and ownership.  Add this ability to
btrfsck.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: add ability to replace missing dir item/dir indexes
Josef Bacik [Fri, 31 Oct 2014 18:01:26 +0000 (14:01 -0400)]
Btrfs-progs: add ability to replace missing dir item/dir indexes

If we have everything except the dir item and dir index we can easily replace
them, so add this ability to btrfsck.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: add a message to know zero log ran successfully
Josef Bacik [Fri, 31 Oct 2014 18:01:25 +0000 (14:01 -0400)]
Btrfs-progs: add a message to know zero log ran successfully

If there are errors when opening the fs because of PARTIAL we could think that
the zero-log didn't actually work.  Add a printf so we know that it was
successfull.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: make zero-log use partial open
Josef Bacik [Fri, 31 Oct 2014 18:01:24 +0000 (14:01 -0400)]
Btrfs-progs: make zero-log use partial open

Because seriously, we only want to kill the tree log.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: spit out the broken file when ignoring errors
Josef Bacik [Fri, 31 Oct 2014 18:01:23 +0000 (14:01 -0400)]
Btrfs-progs: spit out the broken file when ignoring errors

It's nice to ignore errors on restore, but spit out the filename so the user
knows which files of his aren't going to look right.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: don't fail on log tree opening with PARTIAL
Josef Bacik [Fri, 31 Oct 2014 18:01:22 +0000 (14:01 -0400)]
Btrfs-progs: don't fail on log tree opening with PARTIAL

We were failing to fsck a volume because we couldn't open the log tree, which is
not helpful.  Make us skip erroring out if we are using OPEN_CTREE_PARTIAL since
it isn't a mandatory tree.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: add the ability to delete items
Josef Bacik [Fri, 31 Oct 2014 18:01:19 +0000 (14:01 -0400)]
Btrfs-progs: add the ability to delete items

Somtetimes you just need to delete an item, add that functionality to
btrfs-corrupt-block.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix compiler warning
Christian Hesse [Thu, 5 Jun 2014 08:13:39 +0000 (10:13 +0200)]
btrfs-progs: fix compiler warning

gcc 4.9.0 gives warnings about possibly uninitialized values when
compiling with function inlining and optimization level two enabled
(CFLAGS="-finline-functions -O2").

Initializing the values fixes the warning. Hope this is correct.

Signed-off-by: Christian Hesse <mail@eworm.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: update .gitignore
David Sterba [Fri, 7 Nov 2014 16:59:56 +0000 (17:59 +0100)]
btrfs-progs: update .gitignore

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: build, update the clean rule
David Sterba [Thu, 30 Oct 2014 18:28:02 +0000 (19:28 +0100)]
btrfs-progs: build, update the clean rule

Remove duplicate files, add extra progs, add library test binaries.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: build, gather utilities that are not built by default
David Sterba [Thu, 30 Oct 2014 18:22:32 +0000 (19:22 +0100)]
btrfs-progs: build, gather utilities that are not built by default

Add them to build test and to clean rules.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: build, add make target for a build test
David Sterba [Thu, 30 Oct 2014 18:06:31 +0000 (19:06 +0100)]
btrfs-progs: build, add make target for a build test

A quick check that everything still builds.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: build, add basic build test for library
David Sterba [Thu, 30 Oct 2014 17:59:47 +0000 (18:59 +0100)]
btrfs-progs: build, add basic build test for library

Basic test based on snapper code that uses the send stream API.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs progs v3.17.1
David Sterba [Tue, 4 Nov 2014 17:30:34 +0000 (18:30 +0100)]
Btrfs progs v3.17.1

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: use the correct SI prefixes
David Sterba [Tue, 4 Nov 2014 17:22:51 +0000 (18:22 +0100)]
btrfs-progs: use the correct SI prefixes

The SI standard defines lowercase 'k' and uppercase for the rest.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: libbtrfs, make exported headers compatible with C++
David Sterba [Mon, 3 Nov 2014 22:50:50 +0000 (23:50 +0100)]
btrfs-progs: libbtrfs, make exported headers compatible with C++

Add externs and don't use a reserved keyword.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: optimize btrfs_scan_lblkid() for multiple calls
Anand Jain [Fri, 31 Oct 2014 04:11:20 +0000 (12:11 +0800)]
btrfs-progs: optimize btrfs_scan_lblkid() for multiple calls

btrfs_scan_lblikd() is called by most the device related command functions.
And btrfs_scan_lblkid() is most expensive function and it becomes more expensive
as number of devices in the system increase. Further some threads call this
function more than once for absolutely no extra benefit and the real waste of
resources. Below list of threads and number of times btrfs_scan_lblkid()
is called in that thread.

  btrfs-find-root            1
  btrfs rescue super-recover 2
  btrfs-debug-tree           1
  btrfs-image -r             2
  btrfs check                2
  btrfs restore              2
  calc-size                  NC
  btrfs-corrupt-block        NC
  btrfs-image                NC
  btrfs-map-logical          1
  btrfs-select-super         NC
  btrfstune                  2
  btrfs-zero-log             NC
  tester                     NC
  quick-test.c               NC
  btrfs-convert              0
  mkfs                       #number of devices to be mkfs
  btrfs label set unmounted  2
  btrfs get label unmounted  2

This patch will:
  move out calling register_one_device with in btrfs_scan_lblkid()
  and so function setting the BTRFS_UPDATE_KERNEL to yes will
  call btrfs_register_all_devices() separately.

  introduce a global variable scan_done, which is set when scan is
  done succssfully per thread. So that following calls to this function
  will just return success.

  Further if any function needs to force scan after scan_done is set,
  then it can be done when there is such a requirement, but as of now there
  isn't any such requirement.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: introduce btrfs_register_all_device()
Anand Jain [Fri, 31 Oct 2014 04:11:19 +0000 (12:11 +0800)]
btrfs-progs: introduce btrfs_register_all_device()

This function is to register all devices found after scanning
the system. Before we had this functionality with in the
btrfs_scan_lblkid(), however scanning and registering are two
different distinct operation its better keep them separate.
Also we want to optimize btrfs_scan_lblkid and avoid multiple
system scans unless needed. As of now device scan uses this function.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix linking with libbtrfs
David Sterba [Thu, 30 Oct 2014 17:33:41 +0000 (18:33 +0100)]
btrfs-progs: fix linking with libbtrfs

Reported at https://github.com/openSUSE/snapper/issues/128

Commit cdb9e22e292275237c added another rbtree file that defines
functions that libbtrfs uses.

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Fix the argument requirement for '--subvol-extents'
Qu Wenruo [Thu, 30 Oct 2014 06:34:06 +0000 (14:34 +0800)]
btrfs-progs: Fix the argument requirement for '--subvol-extents'

The following commit changed the argument requirement for
'--subvol-extents', which causes it to call arg_strtou64() on NULL,
resulting a segfault.
d34cbe76 btrfs-progs: check: do not require argument for --subvol-extents

This patch revert the patch and change the help string and man page to
make it no longer confusing.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: skip mounted fs when deal with umounted ones for fi show
Gui Hecheng [Thu, 30 Oct 2014 02:44:50 +0000 (10:44 +0800)]
btrfs-progs: skip mounted fs when deal with umounted ones for fi show

Stalling problems may happen when exec balance & fi show cmds concurrently.

With the following commit:
        commit 915902c500
        btrfs-progs: fix device missing of btrfs fi show with seed devices

The fi show cmd will bother the mounted fs when only umounted fs should
be handled after @btrfs_can_kernel() has finished showing all mounted ones.

We could skip the mounted fs after @btrfs_can_kernel() is done, then tasks
keeps going on mounted fs while fi show continues on umounted ones separately.

Reported-by: Petr Janecek <janecek@ucw.cz>
Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agorevert btrfs-progs: do a separate probe for _transient_ replacing device
Anand Jain [Thu, 30 Oct 2014 04:43:03 +0000 (12:43 +0800)]
revert btrfs-progs: do a separate probe for _transient_ replacing device

There is a compatibility issue with older kernel with the progs commit id as below.

d0588bfa479409b2a0f6243f894338a01a56221a
btrfs-progs: do a separate probe for _transient_ replacing device

So as of now writing to revert the above commit id.
The brewing sysfs interface would help to fix the impending issue, which is
seed device would fail show in 'btrfs fi show' output of a sprout device.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: Fix number of arguments check of 'btrfs fi df'
Qu Wenruo [Tue, 21 Oct 2014 08:31:17 +0000 (16:31 +0800)]
btrfs-progs: Fix number of arguments check of 'btrfs fi df'

'btrfs fi df' needs exactly one arguments as mount option,
but as 3.17 we can run 'btrfs fi df' without any argument,
and it will error as "ERROR: can't access '%s'" which means
the argument number does not do what it should.

The bug is caused by manually modify the optind and use check_argc_max()
instead of the original check_argc_exact().

This patch fixes it by not modifying the optind and use check_argc_exact()
again.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix csum root copy-n-paste error
Zach Brown [Wed, 15 Oct 2014 23:14:21 +0000 (16:14 -0700)]
btrfs-progs: fix csum root copy-n-paste error

btrfs_setup_all_roots() had some copy and pasted code for trying to
setup a root and then creating a blank node if that failed.  The copy
for the csum_root created the blank node in the extent_root.

So we create a function to use a consistent root.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: check read extent errors when mapping
Zach Brown [Wed, 15 Oct 2014 23:14:19 +0000 (16:14 -0700)]
btrfs-progs: check read extent errors when mapping

coverity barked out a warning that btrfs-map-logical was storing but
ignoring errors from read_extent_from_disk().   So don't ignore 'em.  I
made extent reading errors fatal to match the fatal errors from mapping
mirrors above.

And while we're at it have read_extent_from_disk() return -errno pread
errors instead of -EIO or -1 (-EPERM).  The only other caller who tests
errors clobbers them with -EIO.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: code optimize cmd_scan_dev() use btrfs_register_one_device()
Anand Jain [Wed, 15 Oct 2014 00:50:38 +0000 (08:50 +0800)]
btrfs-progs: code optimize cmd_scan_dev() use btrfs_register_one_device()

cmd_scan_dev() has it own code to register device (calling ioctl
BTRFS_IOC_SCAN_DEV), apparently it could use btrfs_register_one_device().

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: only report internal dev replace result if there's a result
Eryu Guan [Fri, 10 Oct 2014 13:16:16 +0000 (21:16 +0800)]
btrfs-progs: only report internal dev replace result if there's a result

If BTRFS_IOC_DEV_REPLACE ioctl failed, args.result usually won't be
updated by the ioctl.

And the arg has been initialized with 0, the result is always 0, which
is BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR, and the resulting error
message looks confusing:

ERROR: ioctl(DEV_REPLACE_START) failed on "/mnt/btrfs": No such file or directory, no error

But in case there's an internal result returned in future, don't drop
the result completely, instead print dev replace result message only
if the result is updated by a failed ioctl call.

Signed-off-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs progs v3.17
David Sterba [Fri, 17 Oct 2014 16:43:33 +0000 (18:43 +0200)]
Btrfs progs v3.17

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: run fsck image tests in filename order
David Sterba [Fri, 17 Oct 2014 16:26:08 +0000 (18:26 +0200)]
btrfs-progs: run fsck image tests in filename order

Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: check, ability to detect and fix outdated snapshot root items
Filipe Manana [Fri, 17 Oct 2014 17:20:08 +0000 (18:20 +0100)]
Btrfs-progs: check, ability to detect and fix outdated snapshot root items

This change adds code to detect and fix the issue introduced in the kernel
release 3.17, where creation of read-only snapshots lead to a corrupted
filesystem if they were created at a moment when the source subvolume/snapshot
had orphan items. The issue was that the on-disk root items became incorrect,
referring to the pre orphan cleanup root node instead of the post orphan
cleanup root node.

A test filesystem can be generated with the test case recently submitted for
xfstests/fstests, which is essencially the following (bash script):

    workout()
    {
ops=$1
procs=$2
num_snapshots=$3

_scratch_mkfs >> $seqres.full 2>&1
_scratch_mount

snapshot_cmd="$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT"
snapshot_cmd="$snapshot_cmd $SCRATCH_MNT/snap_\`date +'%H_%M_%S_%N'\`"
run_check $FSSTRESS_PROG -p $procs \
    -x "$snapshot_cmd" -X $num_snapshots -d $SCRATCH_MNT -n $ops
    }

    ops=10000
    procs=4
    snapshots=500
    workout $ops $procs $snapshots

Example of btrfsck's (btrfs check) behaviour against such filesystem:

  $ btrfsck /dev/loop0
  root item for root 311, current bytenr 44630016, current gen 60, current level 1, new bytenr 44957696, new gen 61, new level 1
  root item for root 1480, current bytenr 1003569152, current gen 1271, current level 1, new bytenr 1004175360, new gen 1272, new level 1
  root item for root 1509, current bytenr 1037434880, current gen 1300, current level 1, new bytenr 1038467072, new gen 1301, new level 1
  root item for root 1562, current bytenr 33636352, current gen 1354, current level 1, new bytenr 34455552, new gen 1355, new level 1
  root item for root 3094, current bytenr 1011712000, current gen 2935, current level 1, new bytenr 1008484352, new gen 2936, new level 1
  root item for root 3716, current bytenr 80805888, current gen 3578, current level 1, new bytenr 73515008, new gen 3579, new level 1
  root item for root 4085, current bytenr 714031104, current gen 3958, current level 1, new bytenr 716816384, new gen 3959, new level 1
  Found 7 roots with an outdated root item.
  Please run a filesystem check with the option --repair to fix them.

  $ echo $?
  1

  $ btrfsck --repair /dev/loop0
  enabling repair mode
  fixing root item for root 311, current bytenr 44630016, current gen 60, current level 1, new bytenr 44957696, new gen 61, new level 1
  fixing root item for root 1480, current bytenr 1003569152, current gen 1271, current level 1, new bytenr 1004175360, new gen 1272, new level 1
  fixing root item for root 1509, current bytenr 1037434880, current gen 1300, current level 1, new bytenr 1038467072, new gen 1301, new level 1
  fixing root item for root 1562, current bytenr 33636352, current gen 1354, current level 1, new bytenr 34455552, new gen 1355, new level 1
  fixing root item for root 3094, current bytenr 1011712000, current gen 2935, current level 1, new bytenr 1008484352, new gen 2936, new level 1
  fixing root item for root 3716, current bytenr 80805888, current gen 3578, current level 1, new bytenr 73515008, new gen 3579, new level 1
  fixing root item for root 4085, current bytenr 714031104, current gen 3958, current level 1, new bytenr 716816384, new gen 3959, new level 1
  Fixed 7 roots.
  Checking filesystem on /dev/loop0
  UUID: 2186e9b9-c977-4a35-9c7b-69c6609d4620
  checking extents
  checking free space cache
  cache and super generation don't match, space cache will be invalidated
  checking fs roots
  checking csums
  checking root refs
  found 618537000 bytes used err is 0
  total csum bytes: 130824
  total tree bytes: 601620480
  total fs tree bytes: 580288512
  total extent tree bytes: 18464768
  btree space waste bytes: 136939144
  file data blocks allocated: 34150318080
   referenced 27815415808
  Btrfs v3.17-rc3-2-gbbe1dd8

  $ echo $?
  0

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix show super unknown flag output
Zach Brown [Wed, 15 Oct 2014 23:14:20 +0000 (16:14 -0700)]
btrfs-progs: fix show super unknown flag output

coverity pointed out that unknown flag printing in show super had some
dead code.  It turns out that first was reset when the first flag was
tested, not when it was output.  We only want to clear it if the first
matching bit is output.  If there are no matching bits then we'll want
to output the unknown flag first.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: check sscanf return code
Zach Brown [Wed, 15 Oct 2014 23:14:18 +0000 (16:14 -0700)]
btrfs-progs: check sscanf return code

coverity warned that the return code from sscanf() assigned to 'i'
wasn't checked before being assigned again.  Check it.

Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: open RW to register device using btrfs-control
Anand Jain [Wed, 15 Oct 2014 00:46:54 +0000 (08:46 +0800)]
btrfs-progs: open RW to register device using btrfs-control

We are passing device path to be registered with in kernel,
so we need to open with RW

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: introduce a proper structure on which cli will call register-device...
Anand Jain [Wed, 15 Oct 2014 00:46:14 +0000 (08:46 +0800)]
btrfs-progs: introduce a proper structure on which cli will call register-device ioctl

As of now commands mentioned below (with in [..]) are calling call register-device
ioctl BTRFS_IOC_SCAN_DEV for all the devices in the system.
Some issues with it:
 BTRFS_IOC_SCAN_DEV: ioctl is a write operation, we don't want command like
 btrfs-debug-tree threads to do that..
   eg:
   ----
   $ cat /proc/fs/btrfs/devlist  | egrep fsid | wc -l
   0
   $ btrfs-debug-tree /dev/sde  (num_device > 1)
   $ cat /proc/fs/btrfs/devlist  | egrep fsid | wc -l
   5
   ----

 btrfs_scan_fs_devices() ends up calling this ioctl only when num_device > 1.
 That's inconsistency with in feature/bug.

 We don't have to register _all_ the btrfs devices (again) in the system
 without user consent.

Why its inconsistent:
 function btrfs_scan_fs_devices() calls btrfs_scan_lblkid only when
 num_devices is > 1, which in turn calls BTRFS_IOC_SCAN_DEV ioctl, if
 conditions are met.

 But main issue is we have too many consumers of btrfs_scan_fs_devices()
 the names below with in [] is the cli leading to this function.

 open_ctree_broken()  [btrfs-find-root]
 recover_prepare()    [btrfs rescue super-recover]
 __open_ctree_fd
 (updates always except when flag OPEN_CTREE_RECOVER_SUPER is set and
 flag OPEN_CTREE_RECOVER_SUPER is set only by 'btrfs rescue super-
 recover' but still this thread sneaks through the open_ctree function
 to call register-device-ioctl as show below).
open_ctree_fs_info
[btrfs-debug-tree]
[btrfs-image -r]
[btrfs check]
open_fs
[btrfs restore]
open_ctree
[calc-size]
[btrfs-corrupt-block]
[btrfs-image] (create)
[btrfs-map-logical]
[btrfs-select-super]
[btrfstune]
[btrfs-zero-log]
[tester]
[mkfs]
[quick-test.c]
[btrfs label set unmounted]
[btrfs get label unmounted]
[btrfs rescue super-recover]

open_ctree_fd
[btrfs-convert]

Fix:
 In an effort to make register-device consistent, all calls to
 btrfs_scan_fs_devices() will have 5th parameter set to 0. that means
 we don't need 5th parameter at all. And with this function not calling
 the register ioctl at all, finally we will have following two cli to call
 the ioctl BTRFS_IOC_SCAN_DEV.
    btrfs dev scan and
    mkfs.btrfs
 Threads needing to update kernel about a device would have to use
 btrfs_register_one_device() separately.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: mkfs should be consistent in calling register device
Anand Jain [Wed, 15 Oct 2014 00:45:11 +0000 (08:45 +0800)]
btrfs-progs: mkfs should be consistent in calling register device

When we have one device we don't call register device.
(in fact not mandatory, but to make it consistent)
And when we have more than one we call register device.

reproducer:
Nothing in the kernel device list
cat /proc/fs/btrfs/devlist | egrep fsid | wc -l
0

mkfs.btrfs will automatically call register device when devices
is more than 1.

mkfs.btrfs -f /dev/sdb /dev/sdc
cat /proc/fs/btrfs/devlist | egrep fsid | wc -l
1

But it does not when there is only one device

mkfs.btrfs -f /dev/sdb
cat /proc/fs/btrfs/devlist | egrep fsid | wc -l
0

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: check, fix return value check of is_child_root()
Filipe Manana [Thu, 16 Oct 2014 00:33:23 +0000 (01:33 +0100)]
Btrfs-progs: check, fix return value check of is_child_root()

The following commit:

    "btrfs-progs: fsck: remove unfriendly BUG_ON() for searching tree failure"
    f495a2ac66116f0a1b15e73380c8cbca6e0a4ca0

introduced a regression, detected through xfstests/btrfs/054, where
previously a negative return value (-1) was used to mean a particular
root didn't had any parent root, and now, after that change, a negative
value is also used to mean that an error happened. That change also made
the only caller of is_child_root() interpret any negative return value
as an error and therefore incorrectly made the caller leave with an
error, instead of continuing.

This affects only the 3.17 release candidates (3.16 and older releases
don't have this issue).

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Wang Shilong <wangshilong1991@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: fix uninitialized warning in btrfs_calc_stripe_index
Anand Jain [Mon, 6 Oct 2014 22:27:16 +0000 (06:27 +0800)]
btrfs-progs: fix uninitialized warning in btrfs_calc_stripe_index

chunk-recover.c: In function btrfs_calc_stripe_index
chunk-recover.c:1481: warning: index may be used uninitialized in this function

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: lookup all roots that point to a corrupt block
Josef Bacik [Fri, 10 Oct 2014 20:57:10 +0000 (16:57 -0400)]
Btrfs-progs: lookup all roots that point to a corrupt block

If we have a corrupt block that multiple snapshots point to we will only fix the
guy who originally pointed to the block, and then simply loop forever because we
keep finding the same bad block.  So instead lookup all roots that point to this
block, and then search down to the block for each root and fix the block in all
snapshots.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: make fsck deal with bogus items
Josef Bacik [Fri, 3 Oct 2014 14:54:26 +0000 (10:54 -0400)]
btrfs-progs: make fsck deal with bogus items

We can deal with corrupt items by deleting them in a few cases.  Fsck can easily
recover from a missing extent item or a dir index item.  So if we notice a item
is completely bogus and it is of a key that we know we can repair then just
delete it and carry on.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: check blocks when checking fs roots
Josef Bacik [Fri, 3 Oct 2014 14:50:15 +0000 (10:50 -0400)]
btrfs-progs: check blocks when checking fs roots

Usually if we find a bad block during the extent tree stuff we will error out,
but if the bad block is in an fs tree and doens't have extents in it then fsck
may still pass even though the block was complete garbage.  So add the check
block logic to the fs root checking so we actually error out of fsck if there is
a bad block.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: add the ability to fix shifted item offsets
Josef Bacik [Fri, 3 Oct 2014 14:45:29 +0000 (10:45 -0400)]
btrfs-progs: add the ability to fix shifted item offsets

A user had a corrupted fs where the items had been shifted improperly.  This
patch adds the ability to fix this sort of problem within fsck.  We will simply
shift the item over to the proper offset and update the offsets to make sure
they are correct.  I tested this with a hand crafted fs that was broken in the
same way as the user, and I've included the file as a new test.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: add shift_items to btrfs-corrupt-block
Josef Bacik [Fri, 3 Oct 2014 14:38:41 +0000 (10:38 -0400)]
btrfs-progs: add shift_items to btrfs-corrupt-block

A user had a corrupted fs where his items where shifted oddly.  This adds the
functionality I needed to btrfs-corrupt-block in order to reproduce this
corruption in order to make fsck fix this sort of problem.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agobtrfs-progs: corrupt btrfs items in btrfs-corrup-block
Josef Bacik [Thu, 2 Oct 2014 19:12:20 +0000 (15:12 -0400)]
btrfs-progs: corrupt btrfs items in btrfs-corrup-block

For testing fsck against completely broken btrfs_items.

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: add ability to corrupt dir items
Josef Bacik [Fri, 10 Oct 2014 20:57:16 +0000 (16:57 -0400)]
Btrfs-progs: add ability to corrupt dir items

In order to test the dir index corruption fixing patches in fsck we need to add
functionality to btrfs-corrupt-block to corrupt dir item fields.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: deal with mismatch index between dir index and inode ref
Josef Bacik [Fri, 10 Oct 2014 20:57:15 +0000 (16:57 -0400)]
Btrfs-progs: deal with mismatch index between dir index and inode ref

Sometimes we have a dir index and an inode ref that don't agree on the index.
In this case just assume that the inode ref is the ultimate authority on the
subject and delete the dir index.  This means we have to not reset index if we
find a mismatched inode ref to make sure we delete the right dir index.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: add a dummy backref if our location is wrong
Josef Bacik [Fri, 10 Oct 2014 20:57:14 +0000 (16:57 -0400)]
Btrfs-progs: add a dummy backref if our location is wrong

If our location is bogus in our dir item we were just skipping the thing.
However in this case we want to just delete the dir index, so create a dummy
inode rec using BTRFS_MULTIPLE_OBJECTIDS and just add every backref we find to
the list so we know to straight up delete all of these items.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
9 years agoBtrfs-progs: delete bogus dir indexes
Josef Bacik [Fri, 10 Oct 2014 20:57:13 +0000 (16:57 -0400)]
Btrfs-progs: delete bogus dir indexes

We may run across dir indexes that are corrupt in such a way that it makes them
useless, such as having a bad location key or a bad name.  In this case we can
just delete dir indexes that don't show up properly and then re-create what we
need.  When we delete dir indexes however we need to restart scanning the fs
tree as we could have greated bogus inode recs if the location key was bad, so
set it up so that if we had to delete an dir index we go ahead and free up our
inode recs and return -EAGAIN to check_fs_roots so it knows to restart the loop.
Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>