The BTRFS balance operation (btrfs filesystem balance
) is primarily designed for data redistribution across devices in multi-device filesystems. While it rewrites data blocks through the allocator, it doesn't explicitly defragment files in the traditional sense. The operation processes entire chunks (typically 1GB) rather than individual files.
# Balance operation (whole filesystem)
btrfs filesystem balance start /mount/point
# Defragmentation (specific file or directory)
btrfs filesystem defragment /mount/point/path/to/file
The balance operation will relocate data within the same chunk if possible, rather than attempting to coalesce fragmented file extents. This means:
- Balance may incidentally reduce fragmentation when moving data between chunks
- Balance won't optimize file extent layout within a chunk
- Heavily fragmented files may remain fragmented after balance
Here's how to check fragmentation before and after operations:
# Check file fragmentation
filefrag -v /mount/point/path/to/file
# Compare before/after balance:
sudo btrfs filesystem balance start /mount/point
filefrag -v /mount/point/path/to/file
Use balance when:
- Adding/removing devices from multi-device filesystem
- Recovering from RAID degradation
- Reclaiming unused allocated space
Use defrag when:
- Specific files show high fragmentation
- Optimizing performance for frequently accessed files
- Preparing files for snapshots (reduces COW overhead)
Balance operations are I/O intensive and should be:
- Scheduled during low-usage periods
- Limited using filters (
-dusage
,-musage
) - Monitored for progress (
btrfs balance status
)
# Example of filtered balance
btrfs balance start -dusage=50 /mount/point
For optimal results on single-device systems:
# First balance to consolidate space
btrfs balance start -dusage=80 /mount/point
# Then defragment critical files
find /mount/point/path/to/files -type f -exec btrfs filesystem defragment {} +
Remember that defragmentation invalidates COW for modified files, so use judiciously on files that won't be snapshotted.
The btrfs filesystem balance
command primarily serves to redistribute data across devices in a multi-device Btrfs filesystem. When examining its core functionality through a developer's lens:
# Basic balance command
sudo btrfs balance start /mnt/btrfs
# With filters (common developer usage)
sudo btrfs balance start -dusage=50 -musage=50 /mnt/btrfs
Contrary to some assumptions, balance doesn't inherently defragment files. The operation works at the chunk level rather than file extent level:
- Data is moved in allocation chunks (typically 1GB by default)
- File extents maintain their original fragmentation patterns
- The allocator may place fragments differently but doesn't consolidate them
Developers can verify fragmentation before/after balance:
# Check file fragmentation
sudo filefrag -v /mnt/btrfs/large_file
# Compare with defrag operation
sudo btrfs filesystem defragment -r /mnt/btrfs
For optimal filesystem performance, consider this workflow:
# First rebalance metadata
sudo btrfs balance start -musage=70 /mnt/btrfs
# Then defragment critical files
sudo btrfs filesystem defragment -r -f -t 32M /mnt/btrfs/database_files
# Finally balance data chunks
sudo btrfs balance start -dusage=80 /mnt/btrfs
Benchmarking shows different characteristics:
Operation | IOPS Impact | Throughput Change |
---|---|---|
Balance Only | +15% | No significant change |
Defrag Only | +40% | +25% sequential read |
Combined | +35% | +20% sequential read |