$ btrfs filesystem defrag -v -r dir/
Recursively defragment files under dir/, print files as they are
processed. The file names will be printed in batches, similarly
the amount of data triggered by defragmentation will be
proportional to last N printed files. The system dirty memory
throttling will slow down the defragmentation but there can still
be a lot of IO load and the system may stall for a moment.
$ btrfs filesystem defrag -v -r -f dir/
Recursively defragment files under dir/, be verbose and wait
until all blocks are flushed before processing next file. You can
note slower progress of the output and lower IO load
(proportional to currently defragmented file).
$ btrfs filesystem defrag -v -r -f -clzo dir/
Recursively defragment files under dir/, be verbose, wait until
all blocks are flushed and force file compression.
$ btrfs filesystem defrag -v -r -t 64M dir/
Recursively defragment files under dir/, be verbose and try to
merge extents to be about 64MiB. As stated above, the success
rate depends on actual free space fragmentation and the final
result is not guaranteed to meet the target even if run
repeatedly.
$ btrfs filesystem resize -1G /path
$ btrfs filesystem resize 1:-1G /path
Shrink size of the filesystem's device id 1 by 1GiB. The first
syntax expects a device with id 1 to exist, otherwise fails. The
second is equivalent and more explicit. For a single-device
filesystem it's typically not necessary to specify the devid
though.
$ btrfs filesystem resize max /path
$ btrfs filesystem resize 1:max /path
Let's assume that devid 1 exists and the filesystem does not
occupy the whole block device, eg. it has been enlarged and we
want to grow the filesystem. By simply using max as size we will
achieve that.
Note
There are two ways to minimize the filesystem on a given
device. The btrfs inspect-internal min-dev-size
command, or
iteratively shrink in steps.