btrfs-progs: Improve the errno string about open_path_or_dev_mnt()

open_path_or_dev_mnt() is used to on *mounted* btrfs device or mount
point, when a unmounted btrfs device is passed, errno is set to EINVAL to
info the caller.
If ignore the errno and just print "ERROR: can't access '%s'", end users
will get confused.

This patch will add check for open_path_or_dev_mnt() caller and print
more meaningful error message when a unmounted btrfs device path is
given.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: Replace the overkill assert with normal error message.

When 'btrfs replace status' encounters an unknown dev replace status, it
will cause an assert, which is somewhat overkilled and can be replaced
with a normal error message.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
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>
Signed-off-by: David Sterba <dsterba@suse.cz>
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>
btrfs-progs: Add command btrfs filesystem disk-usage

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: Add btrfs device disk-usage command

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.cz>
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>
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>
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>
btrfs-progs: read global reserve size from space infos

Kernels >= 3.15 export the global block reserve as a space info presented
by 'btrfs fi df' but would display 'unknown' instead of some meaningful
string.

Signed-off-by: David Sterba <dsterba@suse.cz>
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>
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>
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>
btrfs-progs: zero out structures before calling ioctl

Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: print B for bytes

This arguably helps parsers.

Signed-off-by: David Sterba <dsterba@suse.cz>
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>
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>
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>
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>
btrfs-progs: extend pretty printers with unit mode

The functionality of pretty unit printing was duplicated by
df_pretty_sizes, merge it with pretty_size and enhance the interface
with more suffix mode. Raw, binary or decimal.

Signed-off-by: David Sterba <dsterba@suse.cz>