Btrfs: Modern Copy-on-Write Filesystem

10 min

Explore Btrfs (B-tree filesystem) - the modern Linux filesystem with built-in snapshots, RAID, compression, and advanced features for data integrity and flexibility.

Best viewed on desktop for optimal interactive experience

Btrfs: Where Your Data Gets Superpowers

Imagine a filesystem that could travel back in time. One that never loses data, even when you accidentally delete something. A filesystem that can detect and fix corruption before you even know it's there. Welcome to Btrfs—where science fiction meets your storage!

Btrfs (B-tree filesystem, pronounced "Butter FS" or "Better FS") isn't just another filesystem—it's a complete rethinking of how we store data. Born at Oracle in 2007 and now community-driven, Btrfs brings enterprise-grade features to everyone.

Think of Btrfs as Linux's Swiss Army knife for storage. While ext4 is your reliable daily driver, Btrfs is the transformer that can morph into whatever you need: a snapshot machine, a RAID array, a compression engine, or all of the above simultaneously!

Key Features

1. Copy-on-Write: The Magic Behind Everything

The Revolution: Traditional filesystems are like writing with a pen—once you overwrite something, it's gone forever. Btrfs is like having an infinite stack of transparent sheets. Every change creates a new layer, and you can always peek back at previous versions.

Watch Copy-on-Write in action—see how Btrfs creates snapshots instantly and preserves your data:

Copy-on-Write (CoW) in Action

1
2
3
4
5

Original File State

File contains blocks A, B, C, D at their original locations

Storage Blocks

A
Addr: 0x1000
Refs: 1
B
Addr: 0x2000
Refs: 1
C
Addr: 0x3000
Refs: 1
D
Addr: 0x4000
Refs: 1

Metadata Trees

Current File
1000
2000
3000
4000

Why Copy-on-Write is Brilliant

Instant Snapshots
Creating snapshots takes milliseconds, not minutes
Space Efficient
Only changed blocks use additional space
Data Protection
Original data never overwritten, always consistent
Time Travel
Access any previous version instantly

Why CoW Changes Everything

Copy-on-Write isn't just a feature—it's the foundation that enables:

  • Instant snapshots (no data copying needed)
  • Atomic transactions (all or nothing writes)
  • Data integrity (old data never corrupted)
  • Time travel (rollback to any snapshot)

2. Subvolumes

Subvolumes are like lightweight filesystems within Btrfs:

# Create subvolume sudo btrfs subvolume create /mnt/data/projects # List subvolumes sudo btrfs subvolume list /mnt # Subvolumes can be mounted independently sudo mount -o subvol=projects /dev/sda1 /mnt/projects # Each subvolume can have different mount options sudo mount -o subvol=databases,nodatasum /dev/sda1 /mnt/db

3. Snapshots

Instant, space-efficient copies of subvolumes:

# Create snapshot sudo btrfs subvolume snapshot /mnt/data /mnt/snapshots/data-$(date +%Y%m%d) # Create read-only snapshot sudo btrfs subvolume snapshot -r /mnt/data /mnt/snapshots/data-backup # List snapshots sudo btrfs subvolume list -s /mnt # Rollback to snapshot sudo btrfs subvolume delete /mnt/data sudo btrfs subvolume snapshot /mnt/snapshots/data-backup /mnt/data

Architecture

Everything stored in B-trees (Root Tree → Extent/Chunk/Filesystem Trees).

Key objects: Subvolumes (independent trees), Snapshots (CoW copies), Extents (data blocks), Chunks (device allocations)

Creating and Managing Btrfs

Creating Btrfs Filesystem

# Single device sudo mkfs.btrfs /dev/sdb1 # Multiple devices (RAID) sudo mkfs.btrfs -d raid1 -m raid1 /dev/sdb1 /dev/sdc1 # With label and options sudo mkfs.btrfs -L "MyData" \ -O compress-force \ # Force compression -O no-holes \ # Efficient sparse files /dev/sdb1 # For SSD with optimizations sudo mkfs.btrfs -O ssd \ -O discard=async \ /dev/sdb1

Mount Options

# Basic mount sudo mount /dev/sdb1 /mnt/btrfs # With compression sudo mount -o compress=zstd:3 /dev/sdb1 /mnt/btrfs # SSD optimizations sudo mount -o ssd,discard=async,noatime /dev/sdb1 /mnt/btrfs # Space cache for performance sudo mount -o space_cache=v2 /dev/sdb1 /mnt/btrfs # Autodefrag for desktop use sudo mount -o autodefrag /dev/sdb1 /mnt/btrfs

Advanced Features

1. Compression

Transparent compression: zstd (recommended), zlib (best), lzo (fast)

sudo mount -o compress=zstd:3 /dev/sdb1 /mnt sudo compsize /mnt/ # Check ratio

2. Built-in RAID

RAID 0/1/10 supported. RAID 5/6 not production-ready.

sudo mkfs.btrfs -d raid1 -m raid1 /dev/sdb /dev/sdc sudo btrfs device add /dev/sdd /mnt sudo btrfs balance start /mnt

3. Deduplication

Offline only via duperemove (high memory usage).

4. Scrubbing

sudo btrfs scrub start /mnt sudo btrfs scrub status /mnt

5. Send/Receive

Efficient incremental backups.

sudo btrfs send /mnt/snapshot | sudo btrfs receive /backup/ # Or over network: | ssh remote "sudo btrfs receive /mnt/"

Performance

SSD: mount -o ssd,discard=async,compress=zstd,noatime HDD: mount -o compress=zstd,autodefrag,noatime Databases: Disable CoW with chattr +C on empty directories

Maintenance

# Check usage sudo btrfs filesystem usage /mnt # Balance (redistribute data) sudo btrfs balance start /mnt # Defragment (breaks CoW links!) sudo btrfs filesystem defragment -czstd -r /mnt/data

Troubleshooting

No space left: Balance metadata sudo btrfs balance start -m /mnt Read-only: Try sudo btrfs rescue zero-log /dev/sdb1 Corrupted: sudo btrfs check --repair /dev/sdb1 (use caution!)

# Recovery tools sudo btrfs rescue super-recover /dev/sdb1 sudo btrfs restore /dev/sdb1 /recovery/

Btrfs vs Other Filesystems

Feature Comparison

FeatureBtrfsext4XFSZFS
CoW
Snapshots
Compression
Checksums
Built-in RAID
Deduplication
Subvolumes
Linux mainline

Performance: Btrfs ~80% of XFS for sequential, ~75% for random I/O. Excellent snapshot performance.

Best Practices

  1. Automate snapshots with systemd timers
  2. Use send/receive for incremental backups
  3. Monitor with btrfs device stats and btrfs filesystem usage
  4. Run scrubs regularly (weekly/monthly)
  5. Keep 10-20% free space

When to Use Btrfs

✅ Perfect for:

  • Desktop/laptop systems: Snapshots for easy recovery
  • Development environments: Quick rollback capability
  • Container hosts: Efficient storage with deduplication
  • Home NAS: Built-in RAID and data integrity
  • Backup servers: Send/receive for efficient replication

❌ Avoid for:

  • Databases in production: CoW overhead impacts performance
  • RAID 5/6 requirements: Not stable yet
  • Maximum stability needs: Use ext4 or XFS
  • Very large filesystems: ZFS handles scale better

Limitations

  • RAID 5/6 unstable
  • Deduplication offline only, high RAM usage
  • fsck less capable than ext4
  • Needs 1GB+ RAM, CPU for compression/checksums

Migration from ext4

# In-place (backup first!) sudo btrfs-convert /dev/sdb1 # Or fresh: rsync to new Btrfs filesystem sudo rsync -avxHAX /mnt/old/ /mnt/new/

Btrfs brings enterprise features (snapshots, compression, checksums) to Linux. While not matching ext4's maturity or XFS's speed, it's ideal for desktops, developers, and home servers where data integrity and flexibility matter.

If you found this explanation helpful, consider sharing it with others.

Mastodon