Btrfs-progs: fail gracefully on error from open_ctree()

Error checking block got moved mistakenly exposing us to a potential
segmentation fault.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Btrfs-progs: change the way mkfs picks raid profiles

Currently mkfs in response to

  mkfs.btrfs -d raid10 dev1 dev2

instead of telling "you can't do that" creates a SINGLE on two devices,
and only rebalance can transform it to raid0.  Generally, it never warns
users about decisions it makes and it's not at all obvious which profile
it picks when.

Fix this by checking the number of effective devices and reporting back
if the specified profile is impossible to create.  Do not create FS in
case invalid profile was given.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Btrfs-progs: rearrange files in the repo

Separate every command group into its own file (cmds_<group>.c) and
rearrange includes.  Remove btrfs_cmds.c.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Btrfs-progs: implement new subcommand parser

This completely replaces the existing subcommand infrastructure, which
is not flexible enough to accomodate our needs.  Instead of a global
command table we now have per-level tables and command group handlers,
which allows command-group-specific handling of options and subcommands.
The new parser exports a clear interface and gets out of the way - all
control over how matching is done is passed to commands and command
group handlers.

One side effect of this is that command implementors have to check the
number of arguments themselves - patch to fix up all existing commands
follows.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Btrfs-progs: switch all existing commands to a new parser

The new infrastructure offloads checking number of arguments passed to a
command to individual command handlers.  Fix them up accordingly.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Btrfs-progs: add restriper headers

Add restriper headers and update print-tree.c

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Btrfs-progs: add 'balance' command group infrastructure

Add balance command group under both 'btrfs' and 'btrfs filesystem'.
Preserve the old 'btrfs filesystem balance <path>' behaviour.

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Btrfs-progs: add restriper commands

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Merge branch 'parser' of git://github.com/idryomov/btrfs-progs
Merge branch 'restriper' of git://github.com/idryomov/btrfs-progs
Btrfs-progs: fall back to the v1 ioctl if the new balance ioctl fails

This only falls back if the plain version of balance start is used.
Any args make us report the ioctl isn't supported.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
Merge branch 'restriper' of git://github.com/idryomov/btrfs-progs
Add open_ctree_fs_info for partial FS opens

fsck needs to be able to open a damaged FS, which means open_ctree needs
to be able to return a damaged FS.

This adds a new open_ctree_fs_info which can be used to open any and all
roots that are valid.  btrfs-debug-tree is changed to use it.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
btrfsck: print some progress
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Allow extent_buffers to use more ram

This changes free_some_buffers (called each time we allocate an extent
buffer) to allow a higher hard limit on the number of extent buffers
in use.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
btrfsck: don't BUG on corrupted extent records
btrfs-corrupt-block: add -e option to corrupt the extent record

This will zero out the extent allocation tree records for the extent.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
btrfsck: add code to rebuild extent records

This also includes a new --repair btrfsck option.  For now it can
only fix errors in the extent allocation tree.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
btrfs-corrupt-block: add -E option to randomly corrupt the extent_root

Signed-off-by: Chris Mason <chris.mason@oracle.com>
Btrfs-progs: allow multi-line command group synopsis

Signed-off-by: Ilya Dryomov <idryomov@gmail.com>