Btrfs-progs: add restore command to btrfs

Add 'btrfs restore' command which previously existed as a separate
utility btrfs-restore.

Signed-off-by: Ian Kumlien <pomac@demius.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: Add support for BTRFS_SEND_FLAG_NO_FILE_DATA

The flag and command are synced from kernel to user. Also, this patch adds a
callback for the BTRFS_SEND_C_UPDATE_EXTENT in struct btrfs_send_ops.
read_and_process_cmd() is updated to decode BTRFS_SEND_C_UPDATE_EXTENT and
send the values through the right callback. I did not add a callback
definition to cmds-receive.c as that code never uses
BTRFS_SEND_FLAG_NO_FILE_DATA.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
btrfs-progs: add send-test

send-test.c links against libbtrfs and uses the send functionality provided
to decode and print a send stream to the console.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
Signed-off-by: David Sterba <dsterba@suse.cz>
Btrfs-progs: make 0 a valid usage filter argument

This is a progs counterpart to a "Btrfs: allow for selecting only
completely empty chunks".  usage=0 now means "select only only
completely empty chunks and nothing else".

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
btrfs-progs: make libbtrfs usable from C++

Please find attached a patch to make the new libbtrfs usable from
C++ (at least for the parts snapper will likely need).

Signed-off-by: Arvin Schnell <aschnell@suse.de>
Signed-off-by: Mark Fasheh <mfasheh@suse.de>
Btrfs-progs: check out if the swap device

Currently, the following commands succeed.

 # cat /proc/swaps
 Filename                                Type            Size    Used    Priority
 /dev/sda3                               partition       8388604 0       -1
 /dev/sdc8                               partition       9765884 0       -2
 # mkfs.btrfs /dev/sdc8

 WARNING! - Btrfs v0.20-rc1-165-g82ac345 IS EXPERIMENTAL
 WARNING! - see http://btrfs.wiki.kernel.org before using

 fs created label (null) on /dev/sdc8
         nodesize 4096 leafsize 4096 sectorsize 4096 size 9.31GB
 Btrfs v0.20-rc1-165-g82ac345
 # btrfs fi sh /dev/sdc8
 Label: none  uuid: fc0bdbd0-7eed-460f-b4e9-131273b66df2
         Total devices 1 FS bytes used 28.00KB
         devid    1 size 9.31GB used 989.62MB path /dev/sdc8

 Btrfs v0.20-rc1-165-g82ac345
 #

But we should check out the swap device. Fixed it.

Signed-off-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Tested-by: David Sterba <dsterba@suse.cz>
btrfs-progs: Fix pointer math in __ino_to_path_fd

We are casting an array of u64 values into a char ** array so
when we dereference this array (as a char **) on a 32 bit system
we're then re-casting that back to a 32 bit value. This causes
problems when we try to print those strings.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
btrfs-progs: require mkfs -f force option to overwrite filesystem or partition table

The core of this is shamelessly stolen from xfsprogs.

Use blkid to detect an existing filesystem or partition
table on any of the target devices.  If something is found,
require the '-f' option to overwrite it, hopefully avoiding
disaster due to mistyped devicenames, etc.

# mkfs.btrfs /dev/sda1

WARNING! - Btrfs v0.20-rc1-59-gd00279c-dirty IS EXPERIMENTAL
WARNING! - see http://btrfs.wiki.kernel.org before using

/dev/sda1 appears to contain an existing filesystem (xfs).
Use the -f option to force overwrite.
#

This does introduce a requirement on libblkid.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Btrfs-progs: fix segmentation fault of 'btrfs-debug-tree -e'

Due to some historical reasons, we remove 'printing leaf' part, which'd
lead to 'Segmentation fault' of btrfs-debug-tree -e, this patch adds it
back.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
btrfsprogs: btrfstune support for extended inode refs

This patch adds an option to btrfstune, '-r' which will enable the extended
inode refs flag on the provided btrfs superblock. We don't have a disable
option at the moment as that would require far more work.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
Btrfs-progs: Change the label of a mounted file system

With this new ioctl(2), we can set/change the label for a mounted file system.
It still does normal process for an umounted file system.

Signed-off-by: Jie Liu <jeff.liu@oracle.com>
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Btrfs-progs: Fix set_label_unmounted() with label length validation

Currently, we keeping silent if the label length is exceeding BTRFS_LABEL_SIZE - 1, and just
truncating the characters beyond that.

This patch make it return error and exit in this situation.

Signed-off-by: Jie Liu <jeff.liu@oracle.com>
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Btrfs-progs: fix cmd_label_usage to reflect this change.

Fix the command usage of "btrfs filesystem label" to reflect this change. i.e. so that
we can get/set the label of a mounted filesystem against the mountpoint.

Signed-off-by: Jie Liu <jeff.liu@oracle.com>
Signed-off-by: Anand Jain <anand.jain@oracle.com>
btrfs-progs: refactor check_label()

Refactor check_label().

- Make it be static at first, this is a preparation step since we'll remove
btrfslabel.[c|h] and move those functions from there to utils.[c|h], we can
do pre-checking against the input label string with it.
- Fix the label length check up from BTRFS_LABEL_SIZE to BTRFS_LABEL_SIZE - 1.
- Kill the check of label contains an invalid character, see below commits for detail:
  79e0e445fc2365e47fc7f060d5a4445d37e184b8
  btrfs-progs: kill check for /'s in labels.

Signed-off-by: Jie Liu <jeff.liu@oracle.com>
CC: David Sterba <dsterba@suse.cz>
CC: Gene Czarcinski <gene@czarc.net>
btrfs-progs: move btrfslabel.[c|h] stuff to utils.[c|h]

Clean btrfslabel.[c|h] out of the source tree and move those related
functions to utils.[c|h].

CC: Gene Czarcinski <gene@czarc.net>
Signed-off-by: Jie Liu <jeff.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
btrfs-progs: fix btrfs_get_subvol cut/paste error

in btrfs_get_subvol(), there is a cut and paste error:

       if (ri->full_path)
               the_ri->full_path = strdup(ri->full_path);
       else
               the_ri->name = NULL;

It should be setting the_ri->full_path to NULL here.
Do it in a function instead of the cpoy & paste to avoid future
errors.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
btrfs-progs: Remove write-only var fdres in cmd_dev_stats()

fdres is initialized to -1, then later tested, but never
set.  Just remove it.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
btrfs-progs: btrfs_list_get_path_rootid error handling

btrfs_list_get_path_rootid() tries to return a negative
number on error, but it's a u64 function.  Callers which test
for a return < 0 will never see an error.

Change the function to fill in the rootid via a pointer,
and then return a simple int as error.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
btrfs-progs: avoid double-free in __btrfs_map_block

__btrfs_map_block() can possibly do the goto again: loop after
having allocated & freed the "multi" pointer.  There are then
a couple error conditions where it will attempt to again kfree
the now non-NULL multi pointer.  So before retrying, reset
multi to NULL after we free it.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
btrfs-progs: fix open error test in cmd_start_replace

open() returns a negative fd on failure, not 0.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>