Commit Graph

141 Commits

Author SHA1 Message Date
David Sterba a67efe4362 btrfs-progs: move command definitions to commands.h
There are some trivial helpers, we can group the command declarations in
one place.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-08-24 19:08:24 +02:00
David Sterba 1c880f34f1 btrfs-progs: move help defines to own header
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:45 +01:00
Qu Wenruo a2203246ae btrfs-progs: Introduce kernel sizes to cleanup large intermediate number
Large numbers like (1024 * 1024 * 1024) may cost reader/reviewer to
waste one second to convert to 1G.

Introduce kernel include/linux/sizes.h to replace any intermediate
number larger than 4096 (not including 4096) to SZ_*.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2017-03-08 13:00:45 +01:00
David Sterba a595ade0d5 btrfs-progs: defrag: remove v1 ioctl support
Signed-off-by: David Sterba <dsterba@suse.com>
2017-01-25 09:47:46 +01:00
David Sterba 52f21fc769 btrfs-progs: defrag: force using v2 defrag ioctl and make default 32M threshold actually work
A user reported on IRC that the new 32M default for target extent size
does not work. This happens because if there are no commandline options,
the v1 ioctl is used that does not do any fine grained defrag. As the v2
ioctl has been introduced 6 years ago (2010, kernel 2.6.33) we won't
keep backward compatibility anymore.

Signed-off-by: David Sterba <dsterba@suse.com>
2017-01-25 09:47:43 +01:00
David Sterba 281e476b4d btrfs-progs: defrag: warn when deframgenting directories without -r
The current implementaion of defrag ioctl on directoreis does not do
what users expect. The -r needs to be specified, but we should also
print a warning to avoid confusion.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:36 +01:00
David Sterba cc0a80b8f2 btrfs-progs: defrag: cleanup temporary errno value
Rename a single letter variable and move the defintion to the scope of
use.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:36 +01:00
Anand Jain 8e2bd38f29 btrfs-progs: recursive defrag cleanup duplicate code
Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:36 +01:00
David Sterba 86d2e4b64b btrfs-progs: remove extra newline from messages
The common message helpers add the newline.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-12-14 15:06:34 +01:00
Karel Zak dcc3df7e6d btrfs-progs: rename btrfs_scan_lblkid() to btrfs_scan_devices()
It seems like bad idea to use a library name (lblkid) within generic
function name. The currently used scanning library is implementation
detail and this detail should be hidden for rest of the code.

Signed-off-by: Karel Zak <kzak@redhat.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2016-10-25 14:28:36 +02:00
David Sterba 85d608f7ec btrfs-progs: constify string arguments where appropriate
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 15:07:23 +02:00
David Sterba 1ed3426d3f btrfs-progs: use standard allocation functions in non-kenrel code
Signed-off-by: David Sterba <dsterba@suse.com>
2016-10-03 11:33:15 +02:00
David Sterba d75e061bcd btrfs-progs: defrag: set errno correctly in the callback
In case defrag fails, the errno is not properly reported everywhere but
rather the last value of 'e', which could be 0. Then we get confusing
error messages like:

ERROR: defrag failed on /path/to/file: Success

Reported-by: Adam Mizerski <adam@mizerski.pl>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-09-21 11:50:42 +02:00
David Sterba 059832da5f btrfs-progs: make superblock reading/scanning api more generic
We'll add more modes that affect scanning.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-08-24 14:36:58 +02:00
David Sterba 2f732723d6 btrfs-progs: fi defrag: change default extent target size to 32 MiB
The kernel default is too low, 32 MiB is recommended and should give
better results.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-07-28 14:08:30 +02:00
David Sterba f551548372 btrfs-progs: do not set optind if not necessary
In the subcommand callbacks that are called just once, we don't need to
explicitly reset optind.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-07-13 19:41:55 +02:00
Satoru Takeuchi 034643237a btrfs-progs: fi show: print error message if no valid Btrfs is specified
* Before this patch

 ===============================
 # ./btrfs fi show foo      # "foo" doesn't mean any valid Btrfs
 #                          # no error message
 # echo $?
 1
 ===============================

* After this patch

 ===============================
 # ./btrfs fi show foo
 ERROR: foo is not a valid Btrfs
 #
 # echo $?
 1
 ===============================

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-24 14:05:43 +02:00
David Sterba 62f900c445 btrfs-progs: fi resize: fix parsing of negative argument
The cleanup of option parsing caused a regression where the negative
resize argument is recognized as short options and the command fails.
Use the new helper to allow that.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=119971
Signed-off-by: David Sterba <dsterba@suse.com>
2016-06-17 17:02:12 +02:00
Yauhen Kharuzhy eba0d8925c btrfs-progs: Check if the FSID was seen by comparing full UUID
is_seen_fsid() uses simple hash to check if FS was seen before at
walking on FS list in 'filesystem show' command: hash key is first byte
of the UUID. This function doesn't check full UUID then, so, if there
are two FS with same first byte in UUIDs exist, only one will be shown:

root@test:~# btrfs fi show
Label: 'System'  uuid: 688cb918-7bac-4c8e-9b11-8d047eb14cf4
        Total devices 2 FS bytes used 1.76GiB
        devid    1 size 3.46TiB used 4.01GiB path /dev/sda2
        devid    2 size 6.91TiB used 4.01GiB path /dev/sdb2

Global spare

root@test:~# grep btrfs /proc/mounts
/dev/sda2 / btrfs rw,relatime,space_cache,subvolid=256,subvol=/root 0 0
/dev/sdc /media/688cb918-7bac-4c8e-9b11-8d047eb14cf4 btrfs rw,relatime,space_cache,subvolid=5,subvol=/ 0 0

root@test:~# btrfs fi show --all-devices
Label: 'System'  uuid: 688cb918-7bac-4c8e-9b11-8d047eb14cf4
        Total devices 2 FS bytes used 1.76GiB
        devid    1 size 3.46TiB used 4.03GiB path /dev/sda2
        devid    2 size 6.91TiB used 4.01GiB path /dev/sdb2

Label: 'test'  uuid: 683b1a80-ca7f-4c4d-b87b-7155401a4d18
        Total devices 7 FS bytes used 2.06MiB
        devid    1 size 7.28TiB used 1.57GiB path /dev/sdc
        devid    2 size 7.28TiB used 1.57GiB path /dev/sdd
        devid    3 size 7.28TiB used 1.57GiB path /dev/sde
        devid    4 size 7.28TiB used 1.57GiB path /dev/sdf
        devid    5 size 7.28TiB used 1.57GiB path /dev/sdg
        devid    6 size 7.28TiB used 1.57GiB path /dev/sdh
        devid    7 size 7.28TiB used 1.57GiB path /dev/sdi

To resolve this collision, search for full FSID in the list of seen
filesystems.

Signed-off-by: Yauhen Kharuzhy <yauhen.kharuzhy@zavadatar.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-05-02 14:42:04 +02:00
David Sterba b005ca0249 btrfs-progs: fi sync: make it silent by default
Report only errors returned by the ioctl.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-05-02 14:40:09 +02:00
David Sterba 3299da86fe btrfs-progs: fix resource leak during device scanning
The dev info is leaked each time we find a known filesystem.

Resolves-coverity-id: 1127098
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-15 10:28:14 +01:00
Satoru Takeuchi 1a521af045 btrfs-progs: Fix a regression that btrfs filesystem label doesn't work
The number of arguments which is allowed to pass became wrong
from the following commit.

commit 176aeca9a148c5e29de0 ("btrfs-progs: add getopt stubs where needed")

* actual result

  ===========================================================
  # ./btrfs prop get /btrfs label
  label=foo
  # ./btrfs fi label /btrfs
  btrfs filesystem label: too few arguments
  usage: btrfs filesystem label [<device>|<mount_point>] [<newlabel>]

      Get or change the label of a filesystem

      With one argument, get the label of filesystem on <device>.
      If <newlabel> is passed, set the filesystem label to <newlabel>.

  # ./btrfs fi label /btrfs bar
  foo
  # ./btrfs prop get /btrfs label
  label=foo
  ===========================================================

* expected result

  ===========================================================
  # ./btrfs prop get /btrfs label
  label=foo
  # ./btrfs fi label /btrfs
  foo
  # ./btrfs fi label /btrfs bar
  # ./btrfs prop get /btrfs label
  label=bar
  ===========================================================

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
Lu Fengqi 05640939ea btrfs-progs: Add new option for specify chunk root bytenr
Add new btrfsck option, '--chunk-root', to specify chunk root bytenr.
And allow open_ctree_fs_info() function accept chunk_root_bytenr to
override the bytenr in superblock. This will be mainly used when chunk
tree corruption.

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 13:42:47 +01:00
David Sterba babe94e481 btrfs-progs: add getopt stubs where needed
Commands that do not take any options do not use getopt, which means the
standard option separator "--" does not work. Update all command
handlers that need it, argv needs to be referenced using the optind that
is correctly pointed after the separator.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 13:42:47 +01:00
Mark Fasheh a2e7ff1eae btrfs-progs: filesystem: add 'du' command
'btrfs du' differs from regular du in that it will work to resolve which
blocks are shared between files in its list. This gives the user a more
accurate bytecount from which they can make decisions regarding management
of their file space.

We still print a total number of bytes counted (like regular du), but also
print the number of bytes which were found to have been shared amongst the
file set provided. From there it becomes trivial to calculate how much space
is exclusively owned.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
[ rename files to reflect the filesystem command group, add GPL v2
  file headers ]
Signed-off-by: David Sterba <dsterba@suse.com>
2016-03-14 13:42:47 +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
David Sterba 641b5d83dd btrfs-progs: cmd filesystem: switch to common error message wrapper
Message texts were adjusted.

Signed-off-by: David Sterba <dsterba@suse.com>
2016-01-12 15:02:53 +01:00
David Sterba b13197d68f btrfs-progs: use on-stack buffer for dev_to_fsid
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-13 18:24:17 +01:00
David Sterba ee07dc99d1 btrfs-progs: remove unused parameter from print_one_fs
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-06 17:05:36 +01:00
David Sterba e29ec82e4e btrfs-progs: don't print version info from embedded subcommands
The version is provided by 'btrfs --version'.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-06 17:00:36 +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
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
Anand Jain 0c90ac0872 btrfs-progs: fix uninitialized copy of btrfs_fs_devices list
Noticed that at print_one_uuid() some of the members of btrfs_fs_devices
contained some junk values. It took a while to dig this further, and found
that we make a local copy of the btrfs_fs_devices list at
search_umounted_fs_uuids() and wasn't initialized properly.

Fixed using using calloc instead of malloc.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
[ switched to calloc ]
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-02 09:35:06 +01:00
Zhao Lei d0c0f90b38 btrfs-progs: filesystem: use btrfs_open_dir for btrfs filesystem command
We can use btrfs_open_dir() to check whether target dir is
in btrfs's mount point before open, instead of checking it in
kernel space of ioctl, and return fuzzy error message.

Before patch:
  # (/mnt/tmp is not btrfs mountpoint)
  #
  # btrfs filesystem df /mnt/tmp
  ERROR: couldn't get space info - Inappropriate ioctl for device
  ERROR: get_df failed Inappropriate ioctl for device
  #

After patch:
  # ./btrfs filesystem df /mnt/tmp
  ERROR: not btrfs filesystem: /mnt/tmp
  #

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-11-02 09:35:00 +01:00
Anand Jain 8d1ed12f79 btrfs-progs: optimize not to scan repeated fsid mount points
fsid can be mounted multiple times, with different subvolid.
And we don't have to scan a mount point if we already have
that in the scanned list.

And thus nicely avoids the following warning with multiple
subvol mounts on older kernel like 2.6.32 where
BTRFS_IOC_GET_FSLABEL ioctl does not exist.

./btrfs fi show -m
Label: none  uuid: 31845933-611e-422d-ae6f-386e57ad81aa
	Total devices 2 FS bytes used 172.00KiB
	devid    1 size 3.00GiB used 642.38MiB path /dev/sdd
	devid    2 size 3.00GiB used 622.38MiB path /dev/sde

warning, device 2 is missing
warning devid 2 not found already
warning, device 2 is missing
warning devid 2 not found already

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-10-07 12:12:18 +02:00
David Sterba 1491d246c4 btrfs-progs: fix double free during scanning
If there are different devices mounted to the same directory we can run
into double free issue in the scanning code and this can lead to a
crash. The dev_info_arg buffer allocation get_fs_info might be skipped,
eg. if the FS_INFO ioctl fails due to EPERM in older kernels. Reset the
pointer before each loop starts.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-10-02 17:56:40 +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 665dc49ef6 btrfs-progs: Use common unit parser for btrfs filesystem command
Move to use get_unit_mode_from_arg() for cmds-filesystem.c,
to make "btrfs filesystem df/show/usage"'s unit argument same.

Also have cleanup effect: 19 insertions(+), 181 deletions(-)

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-09-01 14:02:48 +02:00
Zhao Lei e16cb7cb53 btrfs-progs: Accurate errormsg for resize operation on no-enouth-free-space case
btrfs progs output following error message when doing resize on
no-enouth-free-space case:
 # btrfs filesystem resize +10g /mnt/btrfs_5gb
 Resize '/mnt/btrfs_5gb' of '+10g'
 ERROR: unable to resize '/mnt/btrfs_5gb' - File too large
 #

It is not a good description for users, and this patch changed it to:
 # ./btrfs filesystem resize +10G /mnt/tmp1
 Resize '/mnt/tmp1' of '+10G'
 ERROR: unable to resize '/mnt/tmp1' - no enouth free space
 #

Reported-by: Taeha Kim <kthguru@gmail.com>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:09 +02:00
Patrik Lundquist ef433bb083 btrfs-progs: defrag: remove unused variable
A leftover from when recursive defrag was added.

Signed-off-by: Patrik Lundquist <patrik.lundquist@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:09 +02:00
Patrik Lundquist 44de34b14d btrfs-progs: defrag: fix threshold overflow again
Commit dedb1ebeee broke commit
96cfbbf0ea.

Casting thresh value greater than (u32)-1 simply truncates bits while
desired value is (u32)-1 for max defrag threshold.

I.e. "btrfs fi defrag -t 4g" is trimmed/truncated to 0
and "-t 5g" to 1073741824.

Also added a missing newline.

Signed-off-by: Patrik Lundquist <patrik.lundquist@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:09 +02:00
David Sterba c6cf9778e8 btrfs-progs: unify naming of command handlers
Use cmd_ + group + command schema.

Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:09 +02:00
David Sterba 934dd0e1f7 btrfs-progs: move min-resize implementation to inspect-internal
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:08 +02:00
Filipe Manana a57606e815 Btrfs-progs: add feature to get mininum size for resizing a fs/device
Currently there is not way for a user to know what is the minimum size a
device of a btrfs filesystem can be resized to. Sometimes the value of
total allocated space (sum of all allocated chunks/device extents), which
can be parsed from 'btrfs filesystem show' and 'btrfs filesystem usage',
works as the minimum size, but sometimes it does not, namely when device
extents have to relocated to holes (unallocated space) within the new
size of the device (the total allocated space sum).

This change adds the ability to reliably compute such minimum value and
extents 'btrfs filesystem resize' with the following syntax to get such
value:

   btrfs filesystem resize [devid:]get_min_size

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2015-08-31 19:25:03 +02:00
Omar Sandoval 12aba72aed btrfs-progs: replace struct cmd_group->hidden with flags
We're also going to want to support aliases, so rather than adding
another member, replace "hidden" with a "flags" member.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-26 16:33:38 +02:00
David Sterba b681e2536e btrfs-progs: doc: update defrag page
- update wording for -t
- add optional argument to -c

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-26 16:23:00 +02:00
David Sterba dedb1ebeee btrfs-progs: defrag, check target extent earlier
Print a warning if the target extent size (option -t) is larger than 4G.

Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-25 19:36:06 +02:00
Patrik Lundquist 96cfbbf0ea btrfs-progs: fix defrag threshold overflow
btrfs fi defrag -t 1T overflows the u32 thresh variable and default,
instead of max, threshold is used.

Signed-off-by: Patrik Lundquist <patrik.lundquist@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-25 19:32:28 +02:00
Qu Wenruo 15379fa225 btrfs-progs: Allow "filesystem show" command to handle different units
Now "filesystem show" command can handle different units now.

This is handy for higher level programs to get accurate output from "fi
show" command.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
2015-06-22 16:40:21 +02:00