Commit Graph

166 Commits (d1efe50d0a64e757fac0775858ea92e1266ce16d)

Author SHA1 Message Date
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 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
Ondrej Kozina 5d23b6a7b0 btrfs-progs: libbtrfs: remove max/min macros from API
kerncompat.h header file is part of libbtrfs API. min/max macros cause
conflict while building projects dependant on libbtrfs. Moving those
macros to btrfs-progs internal header file fixes the conflict.

Signed-off-by: Ondrej Kozina <okozina@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 13:42:47 +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 407f928cd2 btrfs-progs: add function attributes for the printf-like
Reported by gcc -Wsuggest-attribute=format .

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:02:55 +01:00
Qu Wenruo 9c572c38a1 btrfs-progs: Introduce new members for btrfs_convert_context
Introduce 3 new members for btrfs_convert_context:

1) struct cache_tree used
Records accurate byte ranges which are used by old filesystem.
This will be used to create old filesystem image.

2) struct cache_tree data_chunks
Records batched ranges which must be covered by data chunks.
The bytenr range is optimized to meet all the chunk requirement.

3) u64 total_bytenr
Records how large the filesystem is in bytenr.
Yes, we can calculate it easy, but that's for old blocks based
filesystem.
This will make it more friendly for extent based filesystem.
And later cctx->block_counts and may be removed

And 2 for mkfs_config:
1) char *chunk_uuid.
Used as temporary chunk_uuid (unparsed) string for later
make_convert_btrfs()

2) u64 super_bytenr
Records the new temporary super bytenr after make_btrfs().

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:01:03 +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 49e0f3e646 btrfs-progs: utils: rename helpinfo unit vairables
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-06 16:39:49 +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
Zhao Lei a2a7d2bb57 btrfs-progs: Introduce warning and error for common use
Current code use fprintf(stderr, "...") to output warnning and
error information.

The error message have different style, as:
 # grep fprintf *.c
 fprintf(stderr, "Open ctree failed\n");
 fprintf(stderr, "%s: open ctree failed\n", __func__);
 fprintf(stderr, "ERROR: cannot open ctree\n");
 ...

And sometimes, we forgot add tailed '\n', or use printf instead,
as in current code:
 printf("warning, device %llu is missing\n",

This patch introduce warning() and error() as common function,
to make:
1: Each warning and error information have same format
2: Easy to search/change all error message
3: Easy to modify function's internal for debug or other requirement,
   for example:
   print function/linenumber in error()
   dumpstack in error()
   add some trace for some style of message
   add support for -v, -vv, ...
   support for locales
   custom output functions
   support some special device/tty

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
[print newline after the message]
Signed-off-by: David Sterba <dsterba@suse.com>
2015-10-21 14:29: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
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
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 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 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 4647ab887a btrfs-progs: accept --help as option in the standalone utilities
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-11 00:23:30 +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
David Sterba c9b73b702b btrfs-progs: convert: allow to set filesystem features
Curretnly there are no features set for the converted filesystem and
it's not possible to request it like in mkfs.

Add new option -O|--features, use -O list-all to show all that are
supported for convert.

Note: from now on, convert without any -O option will use the same
defaults as mkfs. The original behaviour was rather confusing.

Convert is now more verbose about the parameters of the btrfs filesytem.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-04 19:14:54 +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 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 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
David Sterba a598348a32 btrfs-progs: define common geopt values for unit suffixes
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-21 18:13:18 +01:00
David Sterba 07ce7005fc btrfs-progs: unify header file inclusion protections
There are missing ifdefs or defines with very generic names.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-01-21 17:49:26 +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
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 3f22699030 btrfs-progs: move contents of math.h to utils.h and delete
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-19 15:31:00 +01:00
Satoru Takeuchi 0e4e1b1ed9 btrfs-progs: cleanup: avoid to use literal for getopt val
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-19 15:07:02 +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
Qu Wenruo 1c4d47c037 btrfs-progs: Add count_digits() function to help calculate filename len.
Add count_digits() function in utils.h to help calculate filename with
ino suffix.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-12-10 13:44:52 +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
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 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
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
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
Anand Jain 3638e10800 btrfs-progs: remove scan_for_btrfs()
With the changes as in the previous patch, now scan_for_btrfs()
is an unused function. So delete it.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-10 10:38:34 +02:00
Anand Jain 5444864e56 btrfs-progs: remove BTRFS_SCAN_PROC scan method
The libblkid scan method which was introduced later, will also
scan devices under /proc/partitions. So we don't have to do
the explicit scan of the same.

Remove the scan method BTRFS_SCAN_PROC.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-10 10:38:34 +02:00
Eric Sandeen bf17a5e43e btrfs-progs: remove BTRFS_SCAN_DEV and btrfs_scan_one_dir
After the previous 2 patches, nothing uses
whole-dev-tree scanning, so remove the code which
implemented that functionality.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-10-10 09:32:17 +02:00
Satoru Takeuchi c2d83207bf btrfs-progs: move test_isdir() to utils.c
Since test_isdir() is a utility function, it's better to
move it to utils.c. In addition, "const char *" is
more appropriate type as its "path" argument because
this argument is not changed in this function.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Cc: David Sterba <dsterba@suse.cz>
Cc: Mike Fleetwood <mike.fleetwood@googlemail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-22 15:07:04 +02:00
Satoru Takeuchi 91b3334057 btrfs-progs: introduce test_issubvolname() for simplicity
There are many duplicated codes to check if the given string is
correct subvolume name. Introduce test_issubvolname() for this
purpose for simplicity.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Cc: David Sterba <dsterba@suse.cz>
Cc: Mike Fleetwood <mike.fleetwood@googlemail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-22 15:07:04 +02:00
Anand Jain fcdd44470d btrfs-progs: define BTRFS_MKFS_SMALL_VOLUME_SIZE for small volume
mkfs cut of size '1024 * 1024 * 1024' to mark dev as small volume so to
force mixed group. Use a define for that.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-22 15:07:01 +02:00
Gui Hecheng a184abc70f btrfs-progs: move the check_argc_* functions into utils.c
To let the independent tools(e.g. btrfs-image, btrfs-convert, etc.)
share the convenience of check_argc_* functions, just move it into
utils.c.
Also add a new function "set_argv0" to set the correct tool name:
	*btrfs-image*: too few arguments

The original btrfs* tools work as before.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
[moved argv0 and check_argc to utils.*]
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-22 14:55:27 +02:00
Qu Wenruo 18e2663db3 btrfs-progs: Add minimum device size check
Btrfs has global block reservation, so even mkfs.btrfs can execute
without problem, there is still a possibility that the filesystem can't
be mounted.
For example when mkfs.btrfs on a 8M file on x86_64 platform, kernel will
refuse to mount due to ENOSPC, since system block group takes 4M and
mixed block group takes 4M, and global block reservation will takes all
the 4M from mixed block group, which makes btrfs unable to create uuid
tree.

This patch will add minimum device size check before actually mkfs.
The minimum size calculation uses a simplified one:
minimum_size_for_each_dev = 2 * (system block group + global block rsv)
and global block rsv = leafsize << 10

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-22 14:43:11 +02:00
Jeff Mahoney b276e4bc50 btrfs-progs: canonicalize pathnames for device commands
mount(8) will canonicalize pathnames before passing them to the kernel.
Links to e.g. /dev/sda will be resolved to /dev/sda. Links to /dev/dm-#
will be resolved using the name of the device mapper table to
/dev/mapper/<name>.

Btrfs will use whatever name the user passes to it, regardless of whether
it is canonical or not. That means that if a 'btrfs device ready' is
issued on any device node pointing to the original device, it will adopt
the new name instead of the name that was used during mount.

Mounting using /dev/sdb2 will result in df:
/dev/sdb2      209715200 39328 207577088   1% /mnt

lrwxrwxrwx 1 root root 4 Jun  4 13:36 /dev/whatever-i-like -> sdb2
/dev/whatever-i-like 209715200 39328 207577088   1% /mnt

Likewise, mounting with /dev/mapper/whatever and using /dev/dm-0 with a
btrfs device command results in df showing /dev/dm-0. This can happen with
multipath devices with friendly names enabled and doing something like
'partprobe' which (at least with our version) ends up issuing a 'change'
uevent on the sysfs node. That *always* uses the dm-# name, and we get
confused users.

This patch does the same canonicalization of the paths that mount does
so that we don't end up having inconsistent names reported by ->show_devices
later.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
[use PATH_MAX in canonicalize_dm_name]
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-22 14:39:34 +02:00
Eric Sandeen 85691ebeac btrfs-progs: mkfs: allow UUID specification at mkfs time
Allow the specification of the filesystem UUID at mkfs time.

Non-unique unique IDs are rejected.  This includes attempting
to re-mkfs with the same UUID; if you really want to do that,
you can mkfs with a new UUID, then re-mkfs with the one you
wanted.

(Implemented only for mkfs.btrfs, not btrfs-convert).

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
[converted help to asciidoc]
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-08-22 14:39:32 +02:00
Filipe David Borba Manana 1e6195885b Btrfs-progs: allow compression property gets for read-only subvolumes
Because the function open_file_or_dir() always opened the input file in
read/write mode (O_RDWR), we were not able to due a compression property
get against a file living in a read-only subvolume/snapshot.
Fix this by opening the file with O_RDONLY mode if we're doing a property
get.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2014-05-02 17:03:21 +02:00
Goffredo Baroncelli 4701f52390 btrfs-progs: Allow use of get_device_info()
Allow the use of get_device_info() for different units.

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
2014-03-21 06:23:22 -07:00
Wang Shilong 42a3d9b774 Btrfs-progs: new helper to parse string to u64 for btrfs
There are many places that need parse string to u64 for btrfs commands,
in fact, we do such things *too casually*, using atoi/atol/atoll..is not
right at all, and even we don't check whether it is a valid string.

Let's do everything more gracefully, we introduce a new helper
arg_strtou64() which will do all the necessary checks.If we fail to
parse string to u64, we will output message and exit directly, this is
something like what usage() is doing. It is ok to not return erro to
it's caller, because this function should be called when parsing arg
(just like usage!)

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
2014-03-21 06:23:18 -07:00
Qu Wenruo 14ef4f5695 btrfs-progs: move find_mount_root to utils.[ch]
Move find_mount_root to utils.[ch] for general use.

Signed-off-by: Qu Wenruo <quwenruo@cn.fuijitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
2014-03-21 06:23:12 -07:00
Anand Jain 1ecefced86 btrfs-progs: define BTRFS_UUID_UNPARSE_SIZE for uuid unparse buf size
we use 37 as the allocation size to hold the uuid_unparse, here
it defines BTRFS_UUID_UNPARSE_SIZE for the same.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
2014-01-31 08:22:03 -08:00
Filipe David Borba Manana 457b1286dd Btrfs-progs: let get_label return the label instead of printing it
get_label prints the label at the moment. Change this so that
the label is returned and printing is done by the caller.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
2014-01-31 08:22:02 -08:00
Josef Bacik cee267d8c8 Btrfs-progs: make pretty_sizes take u64 instead of a double
This got changed to a double but all the callers still use a u64, which causes
us to segfault sometimes because of some weird C voodoo that I had to have
explained to me.  Apparently because we're using a double the compiler will use
the floating point registers to hold our argument which ends up not being
aligned properly if you don't actually give it a double so it will cause
problems for other things, in our case it was screwing up str_bytes so it was
larger than the actual size of the str.  This patch fixes the segfault.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-24 05:57:44 -04:00
Anand Jain d33b2a4842 btrfs-progs: filesystem show of specified mounted disk should work
Originally, thinking was user will use mount point if the disk
is mounted. But thats not really true, actually user don't
(or shouldn't) care to check if disk mounted, so whether disk
is mounted/unmounted when disk path is specified it should work.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-24 05:57:44 -04:00
Anand Jain 811e993db4 btrfs-progs: make get_btrfs_mount callable
get_btrfs_mount is reusable function but it is printing
errors, this removes it. Here the parent function of
open_path_or_dev_mnt does print error msg on error.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-24 05:57:44 -04:00
Anand Jain 6c2c30ce03 btrfs-progs: use kernel for mounted disk for show
As of now btrfs filesystem show reads directly from
disks. So sometimes output can be stale, mainly when
user wants to cross verify their operation like,
label or device delete or add... etc. so this
patch will read from the kernel ioctl if it finds
that disk is mounted.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:23:13 -04:00
Wang Shilong 48d0762f35 Btrfs-progs: make pretty_size_snprintf() return len
Sometimes, we need to catch length of snprintf() in pretty_size_snprintf().

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:23:12 -04:00
Anand Jain 844034adba btrfs-progs: create helper function to use lblkid to scan for btrfs disks
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:23:11 -04:00
David Sterba 7fbcc39c30 btrfs-progs: look up the containing tree root id
Find the tree id of the containing subvolume for a given file or
directory. For subvolume return it's own id.

$ btrfs inspect-internal rootid <path>

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:23:11 -04:00
Wang Shilong 39813fb7ac Btrfs-progs: move ask_user() to utils.c
Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:22:26 -04:00
David Sterba 15ac848f8a btrfs-progs: use better name for nodiscard variable and flip the logic
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:20:43 -04:00
David Sterba 114a79f823 btrfs-progs: make btrfs-corrupt-block compile again
Reexport csum_tree_block.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:20:43 -04:00
Anand Jain cdbc107292 btrfs-progs: mkfs should check for small vol well before
This fix the regression introduced by 830427d
that it no more creates the FS if disk is small
and if no mixed option is provided.
This patch will bring it to the original design
which will force mixed profile when disk is small
and go ahead to create the FS.

Which also means that before we open the device
for the write we should also check if disk is small.

v2: fixes the checkpatch.pl warnings

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:20:03 -04:00
Anand Jain 71d6bd3c8d btrfs-progs: avoid write to the disk before sure to create fs
This patch provides fix for the following bug,

When mkfs.btrfs fails the disks shouldn't be written.
------------
btrfs fi show /dev/sdb
Label: none  uuid: 60fb76f4-3b4d-4632-a7da-6a44dea5573d
        Total devices 1 FS bytes used 24.00KiB
        devid    1 size 2.00GiB used 20.00MiB path /dev/sdb

mkfs.btrfs -dsingle -mraid1 /dev/sdb -f
::
unable to create FS with metadata profile 16 (have 1 devices)

btrfs fi show /dev/sdb
Label: none  uuid: 2da2179d-ecb1-4a4e-a44d-e7613a08c18d
        Total devices 1 FS bytes used 24.00KiB
        devid    1 size 2.00GiB used 20.00MiB path /dev/sdb
-------------

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:20:03 -04:00
Filipe David Borba Manana d608d404c3 Btrfs-progs: mkfs can now create fs with skinny extents
Before this change, passing -O skinny-metadata to mkfs.btrfs would
only set the skinny metadata incompat flag in the super block after
the filesystem was created. This change makes mkfs.btrfs directly
create a filesystem with only skinny extents for metadata.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-09-03 19:41:13 +02:00
Eric Sandeen fa25b35dcc btrfs-progs: mark static & remove unused from non-kernel code
Mark many functions as static, and remove any resulting dead code.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-09-03 19:40:54 +02:00
Eric Sandeen 4e8c4d4ba7 btrfs-progs: drop unused parameter from btrfs_release_path
Port of commit b3b4aa7 to userspace.

parameter tree root it's not used since commit
5f39d397dfbe140a14edecd4e73c34ce23c4f9ee ("Btrfs: Create extent_buffer
interface for large blocksizes")

This gets userspace a tad closer to kernelspace by removing
this unused parameter that was all over the codebase...

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-09-03 19:40:46 +02:00
Anand Jain b3a9a3c94a btrfs-progs: get_label_mounted to return label instead of print
This would help to reuse the function

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-08-09 14:32:37 +02:00
Anand Jain 5333445574 btrfs-progs: congregate dev scan
the dev scan to find btrfs is performed at two locations
all most the same way one at filesystem show and another
at device scan. They both follow the same steps. This
patch does not alter anything except that it brings these
two same logic into the function scan_for_btrfs so that
we can play tweaking it.

the patch which recommends to use /dev/mapper
will also need it

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-08-09 14:32:36 +02:00
Anand Jain 65ed88d472 btrfs-progs: btrfs_scan_for_fsid doesn't need all the arguments
btrfs_scan_for_fsid uses only one argument run_ioctl out of 3
so remove the rest two of them

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-08-09 14:32:36 +02:00
Wang Shilong c125b7cf43 Btrfs-progs: fix closing of opendir()
valgrind complains open_file_or_dir() causes a memory leak.That is because
if we open a directoy by opendir(), and then we should call closedir()
to free memory.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-08-09 14:32:36 +02:00
Zach Brown 876e3f9380 btrfs-progs: per-thread, per-call pretty buffer
We don't need callers to manage string storage for each pretty_sizes()
call.  We can use a macro to have per-thread and per-call static storage
so that pretty_sizes() can be used as many times as needed in printf()
arguments without requiring a bunch of supporting variables.

This lets us have a natural interface at the cost of requiring __thread
and TLS from gcc and a small amount of static storage.  This seems
better than the current code or doing something with illegible format
specifier macros.

Signed-off-by: Zach Brown <zab@redhat.com>
Acked-by: Wang Shilong <wangs.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-08-09 14:32:33 +02:00
Anand Jain 4b3c9136be btrfs-progs: mkfs should first check all disks before writing to a disk
In the cases where one of the disk is not suitable for
btrfs, then we would fail the mkfs, however we determine
that after we have written btrfs to the preceding disks.
At this time if user changes mind for not to use btrfs
will left with no choice.

So this patch will check if all the provided disks are
suitable for the btrfs at once before proceeding to
create btrfs on a disk.

Further this patch also removed duplicate code to check
device suitability for the btrfs.

Next, there is an existing bug about the -r mkfs option,
which this patch would carry forward most of it.
Ref:
[PATCH 2/2, RFC] btrfs-progs: overhaul mkfs.btrfs -r option

Signed-off-by: Anand Jain <anand.jain@oracle.com>

to merg prev

Signed-off-by: Anand Jain <anand.jain@oracle.com>
2013-04-23 18:56:20 +02:00
Josef Bacik d6f7e3da0d Btrfs-progs: make btrfs-image restore with a valid chunk tree V2
Previously btrfs-image would set a METADUMP flag and would make one big system
chunk to cover the entire file system in the super in order to get around the
unpleasant business of having to adjust the chunk tree.  This meant that you
could use the progs stuff on a restored file system, which is great for testing
btrfsck and other such things.  But we want to be able to run the tree log
replay on a file system that is not able to run the tree log replay.  So in
order to do this we need to fixup the super's chunk array and the chunk tree
itself.  This is pretty easy since we restore using the logical offsets of the
metadata, so we just have to set the chunk items to have 1 stripe and have the
stripes point at the primary device and then use the logical offset of the chunk
as the physical offset.  With this patch I can restore a file system image that
had a tree log and mount the file system and have the log be replayed
successfully.  This patch also gives you the -o option in case you want the old
restore way, in the case where we want to make sure the system chunks as they
were given to us are correct.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
2013-04-09 18:43:24 +02:00
Eric Sandeen 7a355379ea btrfs-progs: rework get_fs_info to remove side effects
get_fs_info() has been silently switching from a device to a mounted
path as needed; the caller's filehandle was unexpectedly closed &
reopened outside the caller's scope.  Not so great.

The callers do want "fdmnt" to be the filehandle for the mount point
in all cases, though - the various ioctls act on this (not on an fd
for the device).  But switching it in the local scope of get_fs_info
is incorrect; it just so happens that *usually* the fd number is
unchanged.

So - use the new helpers to detect when an argument is a block
device, and open the the mounted path more obviously / explicitly
for ioctl use, storing the filehandle in fdmnt.

Then, in get_fs_info, ignore the fd completely, and use the path on
the argument to determine if the caller wanted to act on just that
device, or on all devices for the filesystem.

Affects those commands which are documented to accept either
a block device or a path:

* btrfs device stats
* btrfs replace start
* btrfs scrub start
* btrfs scrub status

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
2013-03-12 17:07:40 +01:00
Eric Sandeen 10e00b0764 btrfs-progs: three new device/path helpers
Add 3 new helpers:

* is_block_device(), to test if a path is a block device.
* get_btrfs_mount(), to get the mountpoint of a device,
  if mounted.
* open_path_or_dev_mnt(path), to open either the pathname
  or, if it's a mounted btrfs dev, the mountpoint.  Useful
  for some commands which can take either type of arg.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
2013-03-12 16:44:40 +01:00
Jeff Liu efbbbc88cb 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>
2013-02-26 19:27:22 +01:00
Jeff Liu 63a44771a4 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:
  79e0e445fc
  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>
2013-02-26 19:24:14 +01:00
Tsutomu Itoh d065d63057 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>
2013-02-19 11:15:30 +01:00
Eric Sandeen bcb2b73358 btrfs-progs: simplify ioctl name copy and null termination
In the places where we copy a string into the name
member of btrfs_ioctl_vol_args or btrfs_ioctl_vol_args_v2,
we use strncopy (to not overflow the name array) and then
set the last position to the null character.

Howver, in both cases the arrays are defined with:

        char name[MAX+1];

hence the last array position is name[MAX].

In most cases, we now insert the null at name[MAX-1]
which deprives us of one useful character.

Even the above isn't consistent through the code, so
make some helper code to make it simple, i.e.
strncpy_null(dest, src) which automatically does the
right thing based on the size of dest.

Thanks to Zach Brown for the macro suggestion.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
2013-02-05 16:09:41 -08:00
Stefan Behrens 7a69dc4eec Btrfs-progs: make two utility functions globally available
Two convenient utility functions that have so far been local to scrub are
moved to utils.c.
They will be used in the device stats code in a following commit.

Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>
2013-01-30 00:40:35 +01:00
Anand Jain 46e3b8087b Btrfs-progs: move open_file_or_dir() to utils.c
The definition of the function open_file_or_dir() is moved from common.c
to utils.c in order to be able to share some common code between scrub
and the device stats in the following step. That common code uses
open_file_or_dir(). Since open_file_or_dir() makes use of the function
dirfd(3), the required XOPEN version was raised from 6 to 7.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Original-Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>
2013-01-30 00:40:35 +01:00
Goffredo Baroncelli 8f76aee6bc Move parse_size() to utils.[hc]
Move the function from cmds-filesystem.c and mkfs.c to utils.c

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
2013-01-17 18:27:54 +01:00
Josef Bacik 605e806166 Btrfs-progs: only enforce a maximum size if we specify one
My patch

04609add88

introduced a regression where if you mkfs'ed a group of disks with different
sizes it limited the disks to the size of the first one that is specified.
This was not the intent of my patch, I only want it to limit the size based
on the -b option, so I've reworked the code to pass in a max block count and
that fixes the issue.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fusionio.com>
2012-10-02 13:02:48 +02:00