Commit Graph

319 Commits (95f515f2d12fb1c1b5a01e03d9f709570b762eeb)

Author SHA1 Message Date
David Sterba 5595263cb6 btrfs-progs: fix fd leak in get_subvol_info
A typo, introduced in "btrfs-progs: create get_subvol_info()".

Resolves-coverity-id: 1357106
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:26:43 +02:00
David Sterba 91b70d4cf1 btrfs-progs: use safe copy for label buffer everywhere
There's a mix of opencoded strncpy + null termination, strncpy, memcppy
without termination etc. Unify them and use the helper.

Resolves-coverity-id: 1357105
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:26:38 +02:00
David Sterba 41f6ae4820 btrfs-progs: rename __strncpy__null to __strncpy_null
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:26:33 +02:00
David Sterba c7f72072bd btrfs-progs: mkfs: allow DUP on multidev fs, only warn
The DUP profile can work on multiple filesystems, the limitation is
rather artificial. Let the user make the decision and print a warning.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:26:27 +02:00
David Sterba 113d1ad49c btrfs-progs: cleanup block group helpers types
Use const char and remove stray prototypes.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:25:47 +02:00
David Sterba c1bb7a2d60 btrfs-progs: utils: make more arguments const
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:25:38 +02:00
Anand Jain 37288902fc btrfs-progs: rename get_subvol_name() to subvol_strip_mountpoint()
Signed-off-by: Anand Jain <anand.jain@oracle.com>
[ renamed from subvol_minus_mnt to subvol_strip_mountpoint ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:25:33 +02:00
Anand Jain e494f5790f btrfs-progs: create get_subvol_info()
get_subvol_info() is useful as we are adding more features around
subvolume. This function was inline with the function
cmd_subvol_show().

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:25:28 +02:00
Anand Jain 8cec21a7ed btrfs-progs: move get_subvol_name() to utils.c
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:25:23 +02:00
Anand Jain 0f9a5cf70c btrfs-progs: remove duplicate function __is_subvol()
The function test_issubvolume() provides the same check, and
has better logic.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:25:18 +02:00
Anand Jain a5dbd2064e btrfs-progs: move test_issubvolume() to utils.c
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:25:13 +02:00
Anand Jain 906dfb9e38 btrfs-progs: rearrange subvolume functions together
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:25:09 +02:00
Petros Angelatos ee78a83b5e btrfs-progs: utils: make sure set_label_mounted uses correct length buffers
When `btrfs filesystem label /foo bar` command is invoked, it will pass
the buffer allocated in the argv array directly to set_label_mounted()
and then to the BTRFS_IOC_SET_FSLABEL ioctl.

However, the kernel code handling the ioctl will always try to copy
BTRFS_LABEL_SIZE bytes[1] from the userland pointer. Under certain
conditions and when the label is small enough, the command will fail
with:

    [root@localhost /]# btrfs filesystem label /mnt f
    ERROR: unable to set label Bad address

Fix this by making sure we pass a BTRFS_LABEL_SIZE sized buffer to the
ioctl containing the desired label.

[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/btrfs/ioctl.c?id=refs/tags/v4.5#n5231

Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-30 16:23:21 +02:00
David Sterba 3bb71d27ac btrfs-progs: utils: switch more error messages to common helpers
Functions relatd to device changes/status/open, mount checks.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 14:38:16 +01:00
David Sterba 230bb91aa8 btrfs-progs: improve error messages after failed wiping
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 14:18:32 +01:00
Satoru Takeuchi fc4c784b65 btrfs-progs: Avoid interpreting options after "--" when getting unit mode
* actual result

  ======================================
  # ./btrfs device usage -- -m /btrfs

  /dev/sdf1, ID: 1
     Device size:          95367.41MiB
     Data,single:          2056.00MiB
     Metadata,DUP:         2048.00MiB
     System,DUP:             16.00MiB
     Unallocated:          91247.41MiB
  ======================================

* expected result

  ======================================
  # ./btrfs device usage -- -m /btrfs

  ERROR: can't access '-m': No such file or directory
  ======================================

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 13:42:47 +01:00
Yauhen Kharuzhy 3ee935e4dd btrfs-progs: Dont' stop scanning of devices at first failed device
When 'btrfs device scan' command is invoked, it scans all devices,
check them for btrfs superblock and add devices with btrfs to a list.

Next, each device from the list is passed to kernel where it is handled
in the btrfs_scan_one_device() function. This function can, for example,
return -EBUSY when device contains superblock matched to existing and
mounted filesystem (if this device was pulled out from RAID and
connected again after some time).

btrfs tool stops device scan if any device has been failed to add, so
other existing devices with (possibly) valid FS will never be reached.

Fix this by remove stopping at any failure in the btrfs_register_all_devices(),
just return error count. btrfs_scan_one_device() reports any kind of
error already.

Signed-off-by: Yauhen Kharuzhy <yauhen.kharuzhy@zavadatar.com>
[ initialize err to 0 ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 13:42:47 +01:00
Qu Wenruo af83da99d6 btrfs-progs: util: Fix a wrong unit of pretty_size
If parameter for pretty_size is smaller than default base(1024),
pretty_size() will output wrong unit.
For example, pretty_size(1008) will output '0.98B' not '1008B' or
'0.98KiB'.

The cause is, for default base and auto-detect unit, base will be 1024
but num_divs is still 0, last result will still be divided by base,
causing the bug.

Fix it by checking num_divs in default case, and if num_divs is 0,
change base to 1.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 13:42:47 +01:00
Satoru Takeuchi be09626592 btrfs-progs: write down the meaning of BTRFS_ARG_BLKDEV
Although BTRFS_ARG_BLKDEV can be returned from check_arg_type(),
it's not explained the meaning.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-02-05 16:19:52 +01:00
David Sterba a6cc8ea10a btrfs-progs: introduce helper for parsing args without options
All commands should support the "--" option separator. This is
transparently handled by getopt, but we don't use that everywhere.
Introduce a helper for commands that take no options (just the path).
The object file dependencies need to be adjusted a bit.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-14 11:07:10 +01:00
David Sterba 1267c58e58 btrfs-progs: let test_isdir return the exact error
Return any error from stat, normalize the return value in case the path
is a directory.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-13 17:45:41 +01:00
David Sterba ac4ec4d4f4 btrfs-progs: check for negative return value from ioctl
Handle only negative values returned by ioctl syscalls, with exception
of the device remove. It returns positive values that are handled later.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:02:55 +01:00
David Sterba 633dc6f80f btrfs-progs: remove unnecessary errno temp variables
We can read errno directly if it's not clobbered by any intermediate
calls.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:02:55 +01:00
Byongho Lee 83d0a1727b btrfs-progs: use NULL instead of 0
Fix the code assigning 0 to pointer instead of NULL.

Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:01:05 +01:00
David Sterba bbb0c589fc btrfs-progs: docs: mkfs, implications of DUP on devices
We offer DUP but still depend on the hardware, to do the right thing.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-04 11:28:12 +01:00
Zhao Lei c1ff75db16 btrfs-progs: mkfs: allow --data DUP for single device
Current code don't support DUP profile on single device, except it
is in mixed mode, because of following reasons:

1: Some SSD do deduplication internally, so the duplication on
   the filesystem side has no effect.
2: On a physical device, if the entire disk broken, --data DUP does not
   help.
3: Half performance compared to single profile.
4: We have a workaround: create multi-partition on a single device,
   and btffs will treat them as multi device.

Instead of refusing --data DUP, we give the user a choice and print
a wrning.

Test:
1: Tested by xfstests
   Run with modified xfstests, I add test items of -d dup in single
   device into btrfs/* and common/rc, run tests of btrfs/*,
   with all mount option, no regression diffed with v4.3.
2: Tested by btrfs-progs
   Checked following commands in "-m dup -d dup" fs with memleck
   checking, all passed:
   mkfs.btrfs -f --data dup --metadata dup /dev/sda6
   btrfs filesystem show /dev/sda6
   btrfs filesystem label /dev/sda6 btrfs_label_test
   btrfs filesystem label /dev/sda6
   btrfs device scan --all-devices
   btrfs device scan /dev/sda6
   btrfs device scan /dev/sda6
   btrfs device ready /dev/sda6
   btrfs check /dev/sda6
   btrfs check -s 1 /dev/sda6
   btrfs check --repair /dev/sda6
   btrfs check --init-csum-tree /dev/sda6
   btrfs check --init-extent-tree /dev/sda6
   btrfs check --check-data-csum /dev/sda6
   btrfs check --qgroup-report /dev/sda6
   btrfs rescue super-recover -y /dev/sda6
   btrfs rescue zero-log /dev/sda6
   btrfs restore -l /dev/sda6
   btrfs restore /dev/sda6 /
   btrfs restore -s /dev/sda6 /
   btrfs restore -x /dev/sda6 /
   btrfs restore -m /dev/sda6 /
   btrfs restore -S /dev/sda6 /
   btrfs restore -v /dev/sda6 /
   btrfs restore -i /dev/sda6 /
   btrfs restore -o /dev/sda6 /
   btrfs restore -u0 /dev/sda6 /
   btrfs restore -u1 /dev/sda6 /
   btrfs restore -D /dev/sda6 /
   btrfs property list /dev/sda6
   btrfs property get /dev/sda6 label
   btrfs property set /dev/sda6 label test
   btrfs property set /dev/sda6 label btrfs_label_test
   btrfs help
   btrfs help --full
   btrfs version
   btrfsck /dev/sda6
   btrfs-find-root /dev/sda6
   btrfs-find-root -a /dev/sda6
   btrfs-map-logical -l1 /dev/sda6
   btrfs-map-logical -l1 -c1 /dev/sda6
   btrfs-map-logical -l1 -o /tmp/btrfs-map-logic-out /dev/sda6
   btrfs-map-logical -l1 -b1 /dev/sda6
   btrfs-select-super -s 0 /dev/sda6
   btrfs-select-super -s 1 /dev/sda6
   btrfstune -S 1 /dev/sda6
   btrfstune -f -S 0 /dev/sda6
   btrfstune -r /dev/sda6
   btrfstune -x /dev/sda6
   btrfstune -n /dev/sda6
   btrfstune -f -U 00000000-0000-0000-0000-000000000000 /dev/sda6
   btrfstune -f -u /dev/sda6
   btrfs-calc-size /dev/sda6
   btrfs-calc-size -v /dev/sda6
   btrfs-calc-size -b /dev/sda6
   btrfs-debug-tree /dev/sda6
   btrfs-debug-tree -e /dev/sda6
   btrfs-debug-tree -d /dev/sda6
   btrfs-debug-tree -r /dev/sda6
   btrfs-debug-tree -R /dev/sda6
   btrfs-debug-tree -u /dev/sda6
   btrfs-debug-tree -b 0 /dev/sda6
   btrfs-debug-tree -t 0 /dev/sda6
   btrfs-debug-tree -t 2 /dev/sda6
   btrfs-show-super /dev/sda6
   btrfs-show-super -i 0 /dev/sda6
   btrfs-show-super -i 1 /dev/sda6
   btrfs-show-super -i 2 /dev/sda6
   btrfs-show-super -a /dev/sda6
   btrfs-show-super -f /dev/sda6
   btrfs-show-super -F /dev/sda6
   btrfs subvolume list /mnt/btrfs-progs-tests
   btrfs subvolume create /mnt/btrfs-progs-tests/mysubvol
   btrfs subvolume list /mnt/btrfs-progs-tests
   btrfs subvolume get-default /mnt/btrfs-progs-tests
   btrfs subvolume set-default 258 /mnt/btrfs-progs-tests
   btrfs subvolume get-default /mnt/btrfs-progs-tests
   btrfs subvolume set-default  /mnt/btrfs-progs-tests
   btrfs subvolume snapshot /mnt/btrfs-progs-tests/mysubvol /mnt/btrfs-progs-tests/mysubvol_snap
   btrfs subvolume list /mnt/btrfs-progs-tests
   btrfs subvolume find-new /mnt/btrfs-progs-tests 0
   btrfs subvolume find-new /mnt/btrfs-progs-tests 0
   btrfs subvolume find-new /mnt/btrfs-progs-tests/mysubvol 0
   btrfs subvolume find-new /mnt/btrfs-progs-tests/mysubvol 0
   btrfs subvolume show /mnt/btrfs-progs-tests
   btrfs subvolume show /mnt/btrfs-progs-tests/mysubvol
   btrfs subvolume show /mnt/btrfs-progs-tests/mysubvol_snap
   btrfs subvolume sync /mnt/btrfs-progs-tests
   btrfs subvolume delete /mnt/btrfs-progs-tests/mysubvol_snap
   btrfs subvolume delete /mnt/btrfs-progs-tests/mysubvol
   btrfs subvolume sync /mnt/btrfs-progs-tests
   btrfs filesystem df /mnt/btrfs-progs-tests
   btrfs filesystem show /mnt/btrfs-progs-tests
   btrfs filesystem sync /mnt/btrfs-progs-tests
   btrfs filesystem label /mnt/btrfs-progs-tests btrfs_label_test
   btrfs filesystem label /mnt/btrfs-progs-tests
   btrfs filesystem usage /mnt/btrfs-progs-tests
   btrfs filesystem defragment -s 1024 -l 2048 /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_0
   btrfs filesystem defragment /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_1
   btrfs filesystem defragment -f /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_2
   btrfs filesystem defragment -czlib /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_3
   btrfs filesystem defragment -clzo /mnt/btrfs-progs-tests/filesystem_test_dir/test_dir_0/test_file_4
   btrfs filesystem defragment /mnt/btrfs-progs-tests/filesystem_test_dir
   btrfs filesystem defragment -r /mnt/btrfs-progs-tests/filesystem_test_dir
   btrfs filesystem defragment /mnt/btrfs-progs-tests
   btrfs filesystem resize 1:-10M /mnt/btrfs-progs-tests
   btrfs filesystem resize 1:max /mnt/btrfs-progs-tests
   btrfs balance start /mnt/btrfs-progs-tests
   btrfs balance start -v /mnt/btrfs-progs-tests
   btrfs balance start -f /mnt/btrfs-progs-tests
   btrfs balance status -v /mnt/btrfs-progs-tests
   btrfs balance pause /mnt/btrfs-progs-tests
   btrfs balance status /mnt/btrfs-progs-tests
   btrfs balance resume /mnt/btrfs-progs-tests
   btrfs balance status -v /mnt/btrfs-progs-tests
   btrfs balance cancel /mnt/btrfs-progs-tests
   btrfs balance start -dprofiles=single /mnt/btrfs-progs-tests
   btrfs balance start -dconvert=single /mnt/btrfs-progs-tests
   btrfs balance start -ddevid=1 /mnt/btrfs-progs-tests
   btrfs balance start -f -mprofiles=single /mnt/btrfs-progs-tests
   btrfs balance start -f -mconvert=single /mnt/btrfs-progs-tests
   btrfs balance start -f -mdevid=1 /mnt/btrfs-progs-tests
   btrfs balance start -f -sprofiles=single /mnt/btrfs-progs-tests
   btrfs balance start -f -sconvert=single /mnt/btrfs-progs-tests
   btrfs balance start -f -sdevid=1 /mnt/btrfs-progs-tests
   btrfs device add -f /dev/sda10 /mnt/btrfs-progs-tests
   btrfs device del /dev/sda10 /mnt/btrfs-progs-tests
   btrfs device stats /dev/sda6
   btrfs device stats -z /dev/sda6
   btrfs device stats /mnt/btrfs-progs-tests
   btrfs device stats -z /mnt/btrfs-progs-tests
   btrfs device usage /mnt/btrfs-progs-tests
   btrfs scrub status /mnt/btrfs-progs-tests
   btrfs scrub start -B /mnt/btrfs-progs-tests
   btrfs scrub start -B -d /mnt/btrfs-progs-tests
   btrfs scrub start -B -r /mnt/btrfs-progs-tests
   btrfs scrub status /mnt/btrfs-progs-tests
   btrfs scrub start /mnt/btrfs-progs-tests
   btrfs scrub status /mnt/btrfs-progs-tests
   btrfs scrub status /mnt/btrfs-progs-tests
   btrfs scrub status -d /mnt/btrfs-progs-tests
   btrfs scrub status -R /mnt/btrfs-progs-tests
   btrfs scrub status /mnt/btrfs-progs-tests
   btrfs scrub start /dev/sda6
   btrfs scrub status /dev/sda6
   btrfs scrub status /dev/sda6
   btrfs scrub status -d /dev/sda6
   btrfs scrub status -R /dev/sda6
   btrfs scrub status /dev/sda6
   btrfs subvolume snapshot -r /mnt/btrfs-progs-tests /mnt/btrfs-progs-tests/snap1
   btrfs send -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1
   btrfs send -e -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1
   btrfs send --no-data -f /tmp/btrfs_snapshot_test /mnt/btrfs-progs-tests/snap1
   btrfs quota enable /mnt/btrfs-progs-tests
   btrfs quota rescan /mnt/btrfs-progs-tests
   btrfs quota rescan -s /mnt/btrfs-progs-tests
   btrfs quota rescan -w /mnt/btrfs-progs-tests
   btrfs quota disable /mnt/btrfs-progs-tests
   btrfs quota enable /mnt/btrfs-progs-tests
   btrfs qgroup create 1/5 /mnt/btrfs-progs-tests
   btrfs qgroup create 2/5 /mnt/btrfs-progs-tests
   btrfs qgroup assign 1/5 2/5 /mnt/btrfs-progs-tests
   btrfs qgroup limit 1G 1/5 /mnt/btrfs-progs-tests
   btrfs qgroup show /mnt/btrfs-progs-tests
   btrfs qgroup show -p -c -r -e -F -f /mnt/btrfs-progs-tests
   btrfs qgroup remove 1/5 2/5 /mnt/btrfs-progs-tests
   btrfs qgroup destroy 2/5 /mnt/btrfs-progs-tests
   btrfs qgroup destroy 1/5 /mnt/btrfs-progs-tests
   btrfs quota disable /mnt/btrfs-progs-tests
   btrfs replace start -f -B /dev/sda6 /dev/sda10 /mnt/btrfs-progs-tests
   btrfs replace status /mnt/btrfs-progs-tests
   btrfs replace start -f -B /dev/sda10 /dev/sda6 /mnt/btrfs-progs-tests
   btrfs-convert /dev/sda6
   btrfs-convert -r /dev/sda6
   btrfs-convert -d /dev/sda6
   btrfs-convert -i /dev/sda6
   btrfs-convert -n /dev/sda6
   btrfs-convert -N 4096 /dev/sda6
   btrfs-convert -l test /dev/sda6
   btrfs-convert -L /dev/sda6
   btrfs-convert --no-progress /dev/sda6
   mkfs.btrfs -f /dev/sda6
   btrfs-image /dev/sda6 /tmp/btrfs_image.img
   btrfs-image -r /tmp/btrfs_image.img /dev/sda6
   mkfs.btrfs -f /dev/sda6
   btrfs-image -c 0 /dev/sda6 /tmp/btrfs_image.img
   btrfs-image -r /tmp/btrfs_image.img /dev/sda6
   mkfs.btrfs -f /dev/sda6
   btrfs-image -c 9 /dev/sda6 /tmp/btrfs_image.img
   btrfs-image -r /tmp/btrfs_image.img /dev/sda6
   mkfs.btrfs -f /dev/sda6
   btrfs-image -t 0 /dev/sda6 /tmp/btrfs_image.img
   btrfs-image -r /tmp/btrfs_image.img /dev/sda6
   mkfs.btrfs -f /dev/sda6
   btrfs-image -t 1 /dev/sda6 /tmp/btrfs_image.img
   btrfs-image -r /tmp/btrfs_image.img /dev/sda6
   mkfs.btrfs -f /dev/sda6
   btrfs-image -t 32 /dev/sda6 /tmp/btrfs_image.img
   btrfs-image -r /tmp/btrfs_image.img /dev/sda6
   mkfs.btrfs -f /dev/sda6
   btrfs-image -w /dev/sda6 /tmp/btrfs_image.img
   btrfs-image -r /tmp/btrfs_image.img /dev/sda6
   mkfs.btrfs -f /dev/sda6
   btrfs-image -w /dev/sda6 /tmp/btrfs_image.img
   btrfs-image -r /tmp/btrfs_image.img /dev/sda6
   btrfs-image -r -t 0 /tmp/btrfs_image.img /dev/sda6
   btrfs-image -r -t 1 /tmp/btrfs_image.img /dev/sda6
   btrfs-image -r -t 32 /tmp/btrfs_image.img /dev/sda6
   btrfs-image -r -o /tmp/btrfs_image.img /dev/sda6
3: Manual check relation source by:
   grep DUP *.c
   Confirmed that all source are modified.
4: Use this raid type manually, do some operations in fs,
   no error found in command and dmesg.
5: Combination of dup conversion with fsck
   Confirmed OK with relative kernel patch titled:
   [PATCH] btrfs: Support convert to -d dup for btrfs-convert

   export TEST_DEV='/dev/vdc'
   export TEST_DIR='/var/ltf/tester/mnt'

   do_dup_test()
   {
       local m_from="$1"
       local d_from="$2"
       local m_to="$3"
       local d_to="$4"

       echo "Convert from -m $m_from -d $d_from to -m $m_to -d $d_to"

       umount "$TEST_DIR" &>/dev/null
       ./mkfs.btrfs -f -m "$m_from" -d "$d_from" "$TEST_DEV" >/dev/null || return 1
       mount "$TEST_DEV" "$TEST_DIR" || return 1

       cp -a /sbin/* "$TEST_DIR"

       [[ "$m_from" != "$m_to" ]] && {
           ./btrfs balance start -f -mconvert="$m_to" "$TEST_DIR" || return 1
       }

       [[ "$d_from" != "$d_to" ]] && {
       local opt=()
           [[ "$d_to" == single ]] && opt+=("-f")
           ./btrfs balance start "${opt[@]}" -dconvert="$d_to" "$TEST_DIR" || return 1
       }

       umount "$TEST_DIR" || return 1
       ./btrfsck "$TEST_DEV" || return 1
       echo

       return 0
   }

   test_all()
   {
       for m_from in single dup; do
       for d_from in single dup; do
       for m_to in single dup; do
       for d_to in single dup; do
       do_dup_test "$m_from" "$d_from" "$m_to" "$d_to" || return 1
       done
       done
       done
       done
   }

   test_all

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Tested-by: Austin S. Hemmelgarn <ahferroin7@gmail.com>
[ minor updates in the changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-26 15:58:38 +01:00
David Sterba bf1ac8305a btrfs-progs: mkfs: use correct size for superblock csum and writeout
If sectorsize is not BTRFS_SUPER_INFO_SIZE (4k), the superblock checksum
is wrong and mkfs fails. This has been reported on ppc64 where we pick
sectorisize from page size (64k).  This has been broken since ages
(2008) and discovered by the recently added superblock checks.

Reported-by: Dinar Valeev <dvaleev@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-19 16:00:31 +01:00
David Sterba e7c0e3f343 btrfs-progs: fix fd leak in resolve_loop_device_with_loopdev
Resolves-coverity-id: 1339300
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-16 14:23:46 +01:00
David Sterba 882f319761 btrfs-progs: utils: do not expect lo_file_name to be null terminated
The loop_info64::lo_file_name might not be null terminated. Avoid strlen
and trim the length to whatever size of the loop_info buffer.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-16 14:23:46 +01:00
Florian Margaine 0cf3b78f40 btrfs-progs: Fix partitioned loop devices resolving
When using partitions on a loop device, the device's name can be
e.g. /dev/loop0p1 or similar, and no relevant entry exists in the /sys
filesystem, so the current resolve_loop_device function fails.

Instead of using string functions to extract the device name and reading
this file, this patch uses the loop device API through ioctl to get the
correct backing file.

Signed-off-by: Florian Margaine <florian@platform.sh>
[ changed checks of error values from open and ioctl ]
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-16 14:23:46 +01:00
David Sterba f4942228d2 btrfs-progs: mkfs: handler memory allocation error in make_btrfs
Do the allocation early, no need to cleanup.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-16 14:23:44 +01:00
David Sterba 4e01c34b8b btrfs-progs: simplify empty stirngs check
We can do a strlen(str) == 0 in a simpler way.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-06 16:53:55 +01:00
Zhao Lei fa2dcf1406 btrfs-progs: mkfs: Round device size down to sectorsize
When do following command in a vm, whose disks are created by
qemu-img create -f raw 11 2.6G:
 # mkfs.btrfs -f /dev/vdd /dev/vde /dev/vdf
 # btrfs-show-super /dev/vdd /dev/vde /dev/vdf | grep dev_item.total_bytes
 dev_item.total_bytes    2791727104
 dev_item.total_bytes    2791729152
 dev_item.total_bytes    2791729152
We can see that the first device's size is little smaller.

And it fails xfstests btrfs/011.

Reason:
 First device's size is rounded down to sectorsize in make_btrfs(),
 but other devices are not.

Fix:
 Round down remain devices' size in btrfs_add_to_fsid().

Reported-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-03 13:16:05 +01:00
Zhao Lei a40b0ff048 btrfs-progs: Rename variables in btrfs_add_to_fsid
There are two total_bytes in btrfs_add_to_fsid(), local variable
of total_bytes means fs_total_bytes, and device->total_bytes means
device's total_bytes.
And device's total_bytes in argument is named block_count in current
code.

This patch rename:
 total_bytes -> fs_total_bytes
 block_count -> device_total_bytes

To make code more readable.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-03 13:16:02 +01:00
Anand Jain aa075c07c0 btrfs-progs: fix missing initialization of list head for dev_list
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-02 09:35:06 +01:00
Chandan Rajendra c11e36a29e Btrfs-progs: Do not force mixed block group creation unless '-M' option is specified
When creating small Btrfs filesystem instances (i.e. filesystem size <= 1GiB),
mkfs.btrfs fails if both sectorsize and nodesize are specified on the command
line and sectorsize != nodesize, since mixed block groups involves both data
and metadata blocks sharing the same block group. This is an incorrect behavior
when '-M' option isn't specified on the command line.

This commit makes optional the creation of mixed block groups i.e. Mixed block
groups are created only when -M option is specified on the command line.

Since we now allow small filesystem instances with sectorsize != nodesize to
be created, we can end up in the following situation,

[root@localhost ~]# mkfs.btrfs -f -n 65536 /dev/loop0
btrfs-progs v3.19-rc2-405-g976307c
See http://btrfs.wiki.kernel.org for more information.

Performing full device TRIM (512.00MiB) ...
Label:              (null)
UUID:               49fab72e-0c8b-466b-a3ca-d1bfe56475f0
Node size:          65536
Sector size:        4096
Filesystem size:    512.00MiB
Block group profiles:
  Data:             single            8.00MiB
  Metadata:         DUP              40.00MiB
  System:           DUP              12.00MiB
SSD detected:       no
Incompat features:  extref, skinny-metadata
Number of devices:  1
Devices:
   ID        SIZE  PATH
    1   512.00MiB  /dev/loop0
[root@localhost ~]# mount /dev/loop0 /mnt/
mount: mount /dev/loop0 on /mnt failed: No space left on device

The ENOSPC occurs during the creation of the UUID tree. This is because of
things like large metadata block size, DUP mode used for metadata and global
reservation consuming space. Also, large nodesize does not make sense on small
filesystems, hence this should not be an issue.

Signed-off-by: Chandan Rajendra <chandan@linux.vnet.ibm.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-02 09:35:04 +01:00
Anand Jain 3b879467b1 btrfs-progs: move is_numerical() helper to utils and rename
Signed-off-by: Anand Jain <anand.jain@oracle.com>
[ moved to util.c and renamed ]
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-02 09:35:03 +01:00
Zhao Lei 078618d822 btrfs-progs: use btrfs_open_dir in open_path_or_dev_mnt
Use btrfs_open_dir() in open_path_or_dev_mnt() to make the function
return error when target is neither block device nor btrfs mount point.

Also add "verbose" argument to let function output common error
message instead of putting duplicated lines in caller.

Before patch:
  # ./btrfs device stats /mnt/tmp1
  ERROR: getting dev info for devstats failed: Inappropriate ioctl for device
  # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1
  ERROR: ioctl(DEV_REPLACE_STATUS) failed on "/mnt/tmp1": Inappropriate ioctl for device

After patch:
  # ./btrfs device stats /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1
  # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1
  ERROR: not a btrfs filesystem: /mnt/tmp1

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-02 09:35:01 +01:00
Anand Jain 54fdddfdc1 btrfs-progs: fix is_block_device() return checks
it was highlighted to me is_block_device(), returns
 1 if the file is a block device,
 < 0 in case of an error (eg: file not found)
 0 otherwise

This patch makes proper return checks at all the places
where is_block_device() is used. Thanks to Goffredo.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Suggested-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-10-02 17:54:29 +02:00
David Sterba 5e561cef1d btrfs-progs: make sure that is_block_device will return only 0/1
The macro return nonzero, we'll test against 0/1 (or < 0).

Signed-off-by: David Sterba <dsterba@suse.com>
2015-10-02 17:54:26 +02:00
Qu Wenruo ece0e1ea03 btrfs-progs: utils: Check nodesize against features
Check nodesize against features, not only sectorsize.
In fact, one of the btrfs-convert and mkfs differs in the nodesize
check.

This patch also provides the basis for later btrfs-convert fix.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-10-02 17:54:04 +02:00
Anand Jain 94789777b9 btrfs-progs: provide fail safe for BTRFS_IOC_GET_FSLABEL ioctl
Old kernels before 3.9 do not provide ioctl BTRFS_IOC_GET_FSLABEL.
So we need to provide a fail safe logic for btrfs-progs running
on those kernel.

In this patch when get_label_mounted() fails on the old kernel
it will fail back to the old method and uses get_label_unmounted(),
where it will read from the disk directly.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-10-02 17:52:10 +02:00
David Sterba c985a2b104 btrfs-progs: cleanup, update function definitions to be ANSI conformant
Sparse reports:

 warning: non-ANSI function declaration of function

and we're using func(void) elsewhere.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-09-14 23:46:19 +02:00
David Sterba 17e1e872d4 btrfs-progs: handle errors in btrfs_wipe_existing_sb
Failure during superblock wiping is not always a hard error as we're
going to overwrite it anyway but it may catch some errors earlier. The
error message is not very descriptive though, because we don't get back
much information from blkid.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-09-02 18:56:52 +02:00
Zhao Lei 656731013a btrfs-progs: Introduce get_unit_mode_from_arg for common use
We are using separate code for parsing unit mode in current code,
better to use common function.

This patch introduces a common function to specify units as arguments
and a common help message, to make every tool in btrfs having same unit
argument.

The benefits are:
1: Unify current tool's arguments for unit
2: Make tools in future easy to implement such argument
3: Changes (enhancement) in common function have effect on all
   relative tools

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:14 +02:00
Byongho Lee 657e520a11 btrfs-progs: add memory allocation fail check in btrfs_add_to_fsid()
In btrfs_add_to_fsid(), strdup() allocates memory to device->name, but
the return value is not checked.
So add the return value check and error handling code.
And clean-up error handling code for ENOMEM case.

Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:14 +02:00
Zhao Lei 87c25626c4 btrfs-progs: Introduce btrfs_open_dir wrapper
This patch introduce open_btrfs_dir() to open a dir in btrfs
filesystem.

It can be used for several tools in btrfs-progs.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[renamed from open_btrfs_dir, adjusted error messages]
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:14 +02:00
David Sterba 9dbee1a680 btrfs-progs: utils: missing newline in error messages when checking dup
Signed-off-by: David Sterba <dsterba@suse.com>
2015-07-14 18:51:52 +02:00
David Sterba 00d07721ec btrfs-progs: drop unused parameter from make_btrfs
Signed-off-by: David Sterba <dsterba@suse.com>
2015-07-01 17:54:43 +02:00
David Sterba df4de2c089 btrfs-progs: move make_btrfs arguments to a struct
No functional change, just introduce the structure and switch current
users.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-07-01 17:49:21 +02:00
David Sterba 6bfa3cae05 btrfs-progs: add helper for copying paths
Check the source path length and do the copy.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-12 16:44:48 +02:00
David Sterba 6a039e5063 btrfs-progs: properly set up ioctl arguments
At some places we do not clear the whole ioctl structure and could
pass garbage to kernel. Zero the ioctl vol_args and use a helper for
copying the path.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-12 16:44:48 +02:00
David Sterba 2e151027d2 btrfs-progs: use PATH_MAX instead of BTRFS_PATH_NAME_MAX
The path bufferes should be PATH_MAX but BTRFS_PATH_NAME_MAX is shorter
due to embedding in 4k aligned structures.

The only reason to use BTRFS_PATH_NAME_MAX is for the respective
structures btrfs_ioctl_vol_args::name.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-12 16:44:47 +02:00
David Sterba 6d7999d5b7 btrfs-progs: use less memory for pretty_size_mode buffers
Anand reports that the static buffers used for pertty size strings cause
a stack overflow on SPARC. Zach proposed to change the printf format to
wrap the number and the suffix into a macro. This would require to
change all callsites of pretty_size* and is not very convienient to
write.

This patch replaces the per-call-site static buffers with a limited
number for slots that would be used on each invokation of pretty_size
and wrap around. The number of array slots shall be 10 for now, in
current codebase there are no more than 2 calls to pretty_size in a
single argument list.

Reported-by: Anand Jain <Anand.Jain@oracle.com>
CC: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-11 01:10:10 +02:00
David Sterba 4ceffd0927 btrfs-progs: print error within test_num_disk_vs_raid
The error string buffer passed as an argument is of a fixed size, though
we could print up to PATH_MAX + something bytes. Print the error message
directly.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-11 00:51:15 +02:00
David Sterba c848046eb9 btrfs-progs: print error within test_dev_for_mkfs
The error string buffer passed as an argument is of a fixed size, though
we could print up to PATH_MAX + something bytes. Print the error message
directly.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-11 00:46:30 +02:00
David Sterba ff36fbae4d btrfs-progs: cleanup after errors in open_file_or_dir3
We should zero the dirstream in case we close it ourselves, so the
caller does not do it again. Most current callers do not do that and
exit immediatelly.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-09 15:24:51 +02:00
David Sterba 54003d7ee6 btrfs-progs: fix argv0_buf handling
The variable argv0_buf was duplicated and the changes done in utils.c
were not propagated to help.c. So if an unknown commandline token was
found, the error message did not contain the known part:

 $ btrfs scrub test
 : unknown token 'test'

instead of

 $ btrfs scrub test
 btrfs scrub: uknown token 'test'

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-09 13:32:32 +02:00
Goffredo Baroncelli f8a0b85e38 btrfs-progs: return the fsid from make_btrfs()
The function make_btrfs() has as argument the fsid of the filesystem.
If this fsid is empty or null make_btrfs() generates a new fsid. However
If the buffer is valid (but the string is empty) the generated fsid is
copied back to the caller.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-08 17:50:46 +02:00
Goffredo Baroncelli b96345c7ff btrfs-progs: add strdup in btrfs_add_to_fsid() to track the device path
When creating a new btrfs_device, copy the path to track it.  This path
is then used by mkfs.btrfs to list all devices.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-08 17:50:46 +02:00
David Sterba 49ac5f50b5 btrfs-progs: mkfs: move info message out of btrfs_add_to_fsid
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-08 17:50:42 +02:00
David Sterba 11ddfeb261 btrfs-progs: add function to stringify filesystem features
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-04 19:10:55 +02:00
Sam Tygier fbb356632c btrfs-progs: mkfs: check metadata redundancy
Currently BTRFS allows you to make bad choices of data and
metadata levels. For example -d raid1 -m raid0 means you can
only use half your total disk space, but will lose everything
if 1 disk fails. It should give a warning in these cases.

When making a filesystem, check that metadata mode is at least
as redundant as the data mode. For example give warning when:

	-d raid1 -m raid0

Signed-off-by: Sam Tygier <samtygier@yahoo.co.uk>
[make the check more visible in mkfs output]
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-02 17:02:19 +02:00
Anand Jain e9b5ff23ea btrfs-progs: btrfs_scan_block_devices is unused function delete it
After Patch:
 remove BTRFS_SCAN_PROC scan method
There isn't any consumer for btrfs_scan_block_devices() so delete it.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-05-14 15:41:06 +02:00
David Sterba 39b3d7c77a btrfs-progs: print fs features filtered by a mask
mkfs and convert will not support the same features, -O will print only
the list according to the given mask.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-04-07 19:25:16 +02:00
David Sterba 7ea86ad282 btrfs-progs: move feature parsing from mkfs to utils
We'll use them in convert as well. Move defines and the interface
functions to utils.*.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-04-07 19:24:35 +02:00
David Sterba a297698edc btrfs-progs: mkfs, deprecate leafsize and clean up the code
Using the --leafsize will issue a warning. Replace leafsize with
nodesize in the mkfs-related code.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-04-07 19:20:17 +02:00
Qu Wenruo cc6baf5e39 btrfs-progs: Allow parse_qgroupid() to resolve subvolume path into qgroupid
Now parse_qgroupid() can resolve subvolume path into qgroupid.
This is quite handy for handling level 0 qgroupid, and user don't need
to resolve rootid by hand now.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[constify string in __is_subvol]
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-03-24 00:27:50 +01:00
Qu Wenruo a3b048b306 btrfs-progs: Move parse_qgroupid() to utils.c
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[constified strings in parse_qgroupid]
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-03-24 00:16:09 +01:00
Sebastian Thorarensen 0cabc98e51 btrfs-progs: Fix msgs in check_node_or_leaf_size
check_node_or_leaf_size in utils.c now prints 'nodesize (or leafsize)'
instead of 'leafsize (or nodesize)' in the error messages, in order to
be less confusing for the user, as leafsize in mkfs is deprecated.
'ERROR: ' is also prepended to be consistent with other error messages.

Signed-off-by: Sebastian Thorarensen <sebth@naju.se>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-03-23 16:53:53 +01:00
Sebastian Thorarensen 50580171cb btrfs-progs: mkfs: Move out some nodesize code
Move the constant DEFAULT_MKFS_LEAF_SIZE to utils.h and rename it to
BTRFS_MKFS_DEFAULT_NODE_SIZE for consistency. Move the function
check_leaf_or_node_size to utils.c and rename it to
btrfs_check_node_or_leaf_size.

Signed-off-by: Sebastian Thorarensen <sebth@naju.se>
[added btrfs_ prefix]
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-03-23 16:53:15 +01:00
Xing Gu d7c131759d btrfs-progs: don't output operational status information to stderr
Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-27 16:48:44 +01:00
Dimitri John Ledkov e69e015634 btrfs-progs: drop feature defines from C files, in favour of CFLAGS defines
glibc 2.10+ (5+ years old) enables all the desired features:
_XOPEN_SOURCE 700, __XOPEN2K8, POSIX_C_SOURCE, DEFAULT_SOURCE; with a
single _GNU_SOURCE define in the makefile alone. For portability to
other libc implementations (e.g. dietlibc) _XOPEN_SOURCE=700 is also
defined.

This also resolves Debian bug report filed by Michael Tautschnig -
"Inconsistent use of _XOPEN_SOURCE results in conflicting
declarations". Whilst I was not able to reproduce the results, the
reported fact is that _XOPEN_SOURCE set to 500 in one set of files
(e.g. cmds-filesystem.c) generates/defines different struct stat from
other files (cmds-replace.c).

This patch thus cleans up all feature defines, and sets them at a
consistent level.

Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=747969
Signed-off-by: Dimitri John Ledkov <dimitri.j.ledkov@intel.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-27 14:45:59 +01:00
Qu Wenruo dbed21e7c2 btrfs-progs: Fix a buffer overflow causing segfault in fstests/btrfs/069
The newly introduced search_chunk_tree_for_fs_info() won't count devid 0
in fi_arg->num_devices, which will cause buffer overflow since later
get_device_info() will fill di_args with devid.

This can be trigger by fstests/btrfs/069 and any operations needs to
iterate over all the devices like 'fi show' or 'dev stat' while
replacing.

The fix is do an extra probe specifically for devid 0 after
search_chunk_tree_for_fs_info() and change num_devices if needed.

Reported-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-09 18:46:16 +01:00
Qu Wenruo 73cc83d08e btrfs-progs: Allow debug-tree to be executed on regular file.
The commit 1bad43fbe0 ("btrfs-progs: refine btrfs-debug-tree error
prompt when a mount point given")
add judgement on btrfs-debug-tree to restrict only block device to be
executed on, but the command can also be used on regular file, so add
regular file support for the judgement.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-09 18:34:10 +01:00
David Sterba 9623d5549e btrfs-progs: safely copy path in btrfs_scan_lblkid
We should terminate the string, let's use the wrapper.

Resolves-coverity-id: 1127097
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-03 03:24:10 +01:00
David Sterba cff33e7ebc btrfs-progs: cleanup whitespace in is_same_blk_file
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-03 03:22:20 +01:00
David Sterba 190e916dfc btrfs-progs: use strncpy in is_same_blk_file
Although we're getting strings from the caller, let's be safe.

Resolves-coverity-id: 1199475
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-03 03:22:19 +01:00
Gui Hecheng 280434ebde btrfs-progs: move check_arg_type() to util.c
The check_arg_type() function does quite generic thing, move it to
utils.c.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-29 18:10:58 +01:00
David Sterba 8ef9ac8cda btrfs-progs: basic support for TREE_SEARCH_V2 ioctl
Add the interface and helper that checks if the v2 ioctl is supported.

Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-10 15:01:20 +01:00
Gui Hecheng 7269fa1570 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>
2014-12-04 16:48:13 +01:00
Gui Hecheng 9f9330200f 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>
2014-12-04 16:48:13 +01:00
Hidetoshi Seto 373110d84f 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>
2014-12-04 16:48:12 +01:00
Goffredo Baroncelli 9002666a2b btrfs-progs: Add command btrfs filesystem disk-usage
Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-04 16:48:09 +01:00
Goffredo Baroncelli e12e91e859 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>
2014-12-04 16:48:09 +01:00
David Sterba 3d1ed6d152 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>
2014-12-04 16:48:09 +01:00
David Sterba 5ebf59ff58 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>
2014-11-18 18:05:20 +01:00
David Sterba 0287f03a8c 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>
2014-11-04 18:22:51 +01:00
Anand Jain 2f55fd7019 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>
2014-11-03 19:17:11 +01:00
Anand Jain 53cb7fbe8d 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>
2014-11-03 19:17:08 +01:00
Anand Jain cfdb818dda 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.

d0588bfa47
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>
2014-11-03 18:35:30 +01:00
Anand Jain 9662864435 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>
2014-11-03 18:35:30 +01:00
Filipe Manana 555b7feaeb 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>
2014-10-17 18:24:54 +02:00
Zach Brown 17063426a0 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>
2014-10-16 13:38:26 +02:00
Anand Jain bdac35cea2 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>
2014-10-16 12:08:59 +02:00
David Sterba 0b90a24e11 btrfs-progs: add options to tune units for fi df output
The size unit format is a longstanding annoyance. This patch is based on
the work of Nils and Alexandre and enhances the options. It's possible
to select raw bytes, SI-based or IEC-based compact units (human
frientdly) or a fixed base from kilobytes to terabytes. The default is
compact human readable IEC-based, no change to current version.

CC: Nils Steinger <nst@voidptr.de>
CC: Alexandre Oliva <oliva@gnu.org>
Reviewed-by: Hugo Mills <hugo@carfax.org.uk>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-10 18:58:28 +02:00
Adam Buchbinder f404c1ab6c btrfs-progs: Remove extra 'const' modifiers; they don't do anything.
'const int const *x' means the same thing as 'const int *x' or
'int const *x'; the intent was probably 'const int * const x'.
However, this won't work for the 'suffix' variable, as it has
to be assigned, and making the static tables into const pointers
to const chars leads to a mismatch there.

This was found with clang's duplicate-decl-specifier warning.

Signed-off-by: Adam Buchbinder <abuchbinder@google.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-10 18:28:28 +02:00
David Sterba 6f23efdf27 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>
2014-10-10 18:26:35 +02:00
David Sterba db5d44d132 btrfs-progs: print B for bytes
This arguably helps parsers.

Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-10 18:25:31 +02:00
Anand Jain d0588bfa47 btrfs-progs: do a separate probe for transient replacing device
As mentioned in the kernel patch

btrfs: ioctl BTRFS_IOC_FS_INFO and
 BTRFS_IOC_DEV_INFO miss-matched with slots

The count as returned by BTRFS_IOC_FS_INFO is the number of slots that
btrfs-progs would allocate for the BTRFS_IOC_DEV_INFO ioctl. Since
BTRFS_IOC_DEV_INFO would loop across the seed devices, So its better
ioctl BTRFS_IOC_FS_INFO returns the total_devices instead of num_devices.

The above mentioned patch just does that. That is, it returns
total_devices instead of num_devices.

Which means we need to probe for the replacing device separately.

This patch will probe for the replacing device separately.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-10 18:11:32 +02:00