BTRFS(5)

๐Ÿ’– BTRFS(5) โ€” the btrfs filesystem, but make it a group chat ๐Ÿ’–

okay bestie, gather round ๐Ÿซถ this is the manual page for btrfs โ€” like, topics about the BTRFS filesystem (mount options, supported file attributes and other). the tools have their own pages, this one is the big-picture lore drop.


โœจ NAME โœจ

btrfs โ€” topics about the BTRFS filesystem (mount options, supported file attributes and other). yes that's the whole name section, she's brief, she's iconic ๐Ÿ’…


๐ŸŒธ DESCRIPTION ๐ŸŒธ

So this doc is all about the BTRFS stuff that isn't tied to any specific tool. Currently it covers:

  1. mount options
  2. filesystem features
  3. swapfile support
  4. checksum algorithms
  5. compression
  6. sysfs interface
  7. filesystem exclusive operations
  8. filesystem limits
  9. bootloader support
  10. file attributes
  11. zoned mode
  12. control device
  13. filesystems with multiple block group profiles
  14. seeding device
  15. RAID56 status and recommended practices
  16. glossary
  17. storage model, hardware considerations

like, a whole curriculum fr fr ๐Ÿ“šโœจ


๐ŸŽ€ MOUNT OPTIONS ๐ŸŽ€

๐Ÿ’… BTRFS SPECIFIC MOUNT OPTIONS ๐Ÿ’…

This bit is about the mount options that are BTRFS-only, bestie. For the generic ones, go read the mount(8) manual page โ€” and also peek the section with BTRFS specifics down at btrfs-man5//#mount-options-generic. The options are sorted alphabetically (with the no prefix casually thrown away, she's not about that prefix life).

Note: Most mount options apply to the whole filesystem, and only the options in the first mounted subvolume actually take effect. This is because the implementation justโ€ฆ isn't there yet, and it may change in the future. Which means (for example) you literally cannot set per-subvolume nodatacow, nodatasum, or compress using mount options. It's supposed to get fixed eventually, but ngl it has proved to be difficult to implement correctly inside the Linux VFS framework. ๐Ÿ’€

Mount options are processed in order, and only the last occurrence of an option takes effect โ€” and it may disable other options due to constraints (see like nodatacow and compress for the drama). The output of the mount command shows which options have been applied. periodt.


  • acl, noacl โ€” (default: on)

Turn support for POSIX Access Control Lists (ACLs) on or off. The acl(5) manual page has the whole ACL story if you want it. Also important: ACL support is build-time configurable (BTRFS_FS_POSIX_ACL), so if you ask for acl and the feature wasn't compiled in, the mount straight up fails. no vibes, just error.

  • autodefrag, noautodefrag โ€” (since: 3.0, default: off)

Turns on automatic file defragmentation. When it's enabled, small random writes into files (in a range of tens of kilobytes โ€” currently it's 64KiB) get detected and queued up for the defragmentation process. Fair warning, it may not be well suited for large database workloads.

Read latency may go up because it's reading the adjacent blocks that make up the range for defragmentation, and successive writes will merge the blocks in the new location.

Warning: Defragmenting with Linux kernel versions < 3.9 or โ‰ฅ 3.14-rc2, and also with Linux stable kernel versions โ‰ฅ 3.10.31, โ‰ฅ 3.12.12 or โ‰ฅ 3.13.4, will break up the reflinks of COW data (think files copied with cp --reflink, snapshots, or de-duplicated data). This may cause a considerable increase of space usage depending on how broken up the reflinks are. ๐Ÿ˜ญ
  • barrier, nobarrier โ€” (default: on)

Makes sure all IO write operations actually make it through the device cache and are stored permanently when the filesystem is at its consistency checkpoint. Usually that means a flush command goes to the device that synchronizes all pending data and ordinary metadata blocks, then it writes the superblock and issues another flush.

The write flushes cost you a little and they also stop the IO block scheduler from reordering requests in a more effective way. Disabling barriers gets rid of that penalty but will most certainly lead to a corrupted filesystem in case of a crash or power loss. The ordinary metadata blocks could still be unwritten at the time the new superblock is stored permanently, while everyone was expecting that the block pointers to metadata were stored permanently before. ๐Ÿ™ˆ

On a device with a volatile battery-backed write-back cache, the nobarrier option won't lead to filesystem corruption, because the pending blocks are supposed to make it to the permanent storage anyway.

  • clear_cache โ€” Force clearing and rebuilding of the free space cache if something has gone wrong. This option only takes effect on the first read-write mount.

For free space cache v1, this only clears (and, unless nospace_cache is used, rebuilds) the free space cache for block groups that are modified while the filesystem is mounted with that option. To actually clear an entire free space cache v1, see btrfs check --clear-space-cache v1.

For free space cache v2, this clears the entire free space cache. To do that without needing to mount the filesystem, see btrfs check --clear-space-cache v2.

See also: space_cache.

  • commit=<seconds> โ€” (since: 3.12, default: 30)

Sets the interval of periodic transaction commit, when data get synchronized to permanent storage. Higher interval values mean a larger amount of unwritten data can accumulate in memory, which has obvious consequences when the system crashes. The upper bound is not forced, but a warning gets printed if it's more than 300 seconds (5 minutes). Use with care, bestie. ๐ŸŒธ

The periodic commit is not the only mechanism that does a transaction commit โ€” this can also happen by explicit sync, or indirectly by other commands that affect the global filesystem state, or by internal kernel mechanisms that flush based on various thresholds or policies (e.g. cgroups).

  • compress, compress=<type[:level]>, compress-force, compress-force=<type[:level]> โ€” (default: off, level support since: 5.1)

Controls BTRFS file data compression. The type may be specified as zlib, lzo, zstd or no (for no compression, used for remounting). If no type is specified, zlib is used. If compress-force is specified, then compression will always be attempted, but the data may still end up uncompressed if the compression would make them larger.

Both zlib and zstd (since version 5.1) expose the compression level as a tunable knob, with higher levels trading speed and memory (zstd) for higher compression ratios. Set it by appending a colon and the desired level. ZLIB accepts the range [1, 9] and ZSTD accepts [1, 15]. If no level is set, both currently use a default level of 3. The value 0 is an alias for the default level. โœจ

Otherwise some simple heuristics get applied to detect an incompressible file. If the first blocks written to a file are not compressible, the whole file is permanently marked to skip compression. And because that's too simple, compress-force is the workaround that will compress most of the files โ€” at the cost of some wasted CPU cycles on failed attempts. Since kernel 4.15, a set of heuristic algorithms has been improved by using frequency sampling, repeated pattern detection and Shannon entropy calculation to avoid all that.

Note: If compression is enabled, nodatacow and nodatasum are disabled. that's the tradeoff, babe.
  • datacow, nodatacow โ€” (default: on)

Enables data copy-on-write for newly created files. Nodatacow implies nodatasum, and disables compression. All files created under nodatacow also get the NOCOW file attribute set (see chattr(1)).

Note: If nodatacow or nodatasum are enabled, compression is disabled. ๐Ÿ”ฅ

Updates in-place improve performance for workloads that do frequent overwrites โ€” at the cost of potential partial writes, in case the write is interrupted (system crash, device failure). pick your fighter.

  • datasum, nodatasum โ€” (default: on)

Enables data checksumming for newly created files. Datasum implies datacow, i.e. the normal mode of operation. All files created under nodatasum inherit the "no checksums" property, however there's no corresponding file attribute for it (see chattr(1)).

Note: If nodatacow or nodatasum are enabled, compression is disabled.

There is a slight performance gain when checksums are turned off โ€” the corresponding metadata blocks holding the checksums don't need to be updated. And honestly? The cost of checksumming blocks in memory is much lower than the IO anyway, plus modern CPUs feature hardware support of the checksumming algorithm. she's efficient like that ๐Ÿ’…

  • degraded โ€” (default: off)

Allows mounts with fewer devices than the RAID profile constraints require. A read-write mount (or remount) may fail when too many devices are missing โ€” for example if a stripe member is completely missing from RAID0. ๐Ÿซ 

Since 4.14, the constraint checks have been improved and are verified on the chunk level, not at the device level. This allows degraded mounts of filesystems with mixed RAID profiles for data and metadata, even if the device number constraints wouldn't be satisfied for some of the profiles.

Example: metadata -- raid1, data -- single, devices -- /dev/sda, /dev/sdb

Suppose the data are completely stored on sda โ€” then a missing sdb won't prevent the mount, even if 1 missing device would normally prevent (any) single profile from mounting. But in case some of the data chunks are stored on sdb, then the constraint of single/data is not satisfied and the filesystem cannot be mounted. so close ๐Ÿ˜ญ

Note: During a degraded mount, if a new chunk is going to be allocated and the existing devices cannot meet the requirements for the current profile, then mount will fall back to other profiles like SINGLE.
It is recommended to check and balance chunks with unwanted profiles, after a read-write degraded mount. do your chores, bestie ๐Ÿงš
  • device=<devicepath> โ€” Specify a path to a device that will be scanned for a BTRFS filesystem during mount. This is usually done automatically by a device manager (like udev) or using the btrfs device scan command (e.g. run from the initial ramdisk). In cases where that's not possible, the device mount option can help. ๐Ÿฆ‹
Note: ๐Ÿ’€ booting something like a RAID1 system can still fail even when you've handed over every single one of the filesystem's device paths, because the actual device nodes may not have been discovered by the system at that point. Rude, honestly.
  • discard, discard=sync, discard=async, nodiscard โ€” default: async when devices support it since 6.2, async support since: 5.6

Okay so this one enables discarding of freed file blocks โœจ. Super useful for SSD/NVMe devices, thinly provisioned LUNs, or virtual machine images ๐Ÿฆ‹ โ€” BUT bestie, every single storage layer has to support discard for it to actually work. No cap.

In the synchronous mode (sync, or just no option value at all), a lack of asynchronous queued TRIM on the backing device TRIM can severely degrade performance ๐Ÿ˜ญ because a synchronous TRIM operation will be attempted instead. Queued TRIM requires SATA devices with chipsets revision newer than 3.1 and devices.

The asynchronous mode (async) gathers extents in larger chunks before sending them to the devices for TRIM. The overhead and performance impact should be negligible compared to the previous mode, and it's supposed to be the preferred mode if needed. Periodt.

If it's not necessary to immediately discard freed blocks, then the fstrim tool can be used to discard all free blocks in a batch. Scheduling a TRIM during a period of low system activity will prevent latent interference with the performance of other operations ๐Ÿ’…. Also, a device may ignore the TRIM command if the range is too small, so running a batch discard has a greater probability of actually discarding the blocks.

  • enospc_debug, noenospc_debug โ€” default: off. Enables verbose output for some ENOSPC conditions. It's safe to use, but it can be noisy if the system reaches near-full state ๐Ÿ™ˆ.
  • fatal_errors=<action> โ€” since: 3.4, default: bug. The action to take when encountering a fatal error, and there are two and they are both dramatic:
    • bug โ€” BUG() on a fatal error. The system will stay in the crashed state and may still be partially usable, but a reboot is required for full operation.
    • panic โ€” panic() on a fatal error. Depending on other system configuration, this may be followed by a reboot. Please refer to the documentation of kernel boot parameters, e.g. panic, oops or crashkernel.
  • flushoncommit, noflushoncommit โ€” default: off. This option forces any data dirtied by a write in a prior transaction to commit as part of the current commit, which is effectively a full filesystem sync ๐Ÿซถ.

This makes the committed state a fully consistent view of the file system from the application's perspective (i.e. it includes all completed file system operations). This was previously the behavior only when a snapshot was created.

When it's off, the filesystem is consistent but buffered writes may last more than one transaction commit.

  • fragment=<type> โ€” depends on compile-time option CONFIG_BTRFS_DEBUG, since: 4.4, default: off. A debugging helper to intentionally fragment a given type of block groups. The type can be data, metadata or all. This mount option should not be used outside of debugging environments, and it is not recognized if the kernel config option CONFIG_BTRFS_DEBUG is not enabled ๐Ÿ’€.
  • nologreplay โ€” default: off, even read-only. The tree-log contains pending updates to the filesystem until the full commit. The log is replayed on next mount, and this can be disabled by this option. See also treelog. Note that nologreplay is the same as norecovery.
Warning: currently, the tree log is replayed even with a read-only mount! ๐Ÿ˜ญ To disable that behaviour, mount also with nologreplay.
  • max_inline=<bytes> โ€” default: min(2048, page size). Specify the maximum amount of space that can be inlined in a metadata b-tree leaf. The value is specified in bytes, optionally with a K suffix (case insensitive). In practice, this value is limited by the filesystem block size (named sectorsize at mkfs time), and the memory page size of the system. In the case of the sectorsize limit, there's some space unavailable due to b-tree leaf headers. For example, with a 4KiB sectorsize, the maximum size of inline data is about 3900 bytes.

Inlining can be completely turned off by specifying 0. This will increase data block slack if file sizes are much smaller than block size, but it will reduce metadata consumption in return.

Note: the default value has changed to 2048 in kernel 4.6.
  • metadata_ratio=<value> โ€” default: 0, internal logic. Specifies that 1 metadata chunk should be allocated after every value data chunks. Default behaviour depends on internal logic: some percent of unused metadata space is attempted to be maintained, but that's not always possible if there's not enough space left for chunk allocation. The option could be useful to override the internal logic in favor of the metadata allocation if the expected workload is supposed to be metadata intense (snapshots, reflinks, xattrs, inlined files).
  • norecovery โ€” since: 4.5, default: off. Do not attempt any data recovery at mount time. This will disable logreplay and avoids other write operations. Note that this option is the same as nologreplay.
Note: the opposite option recovery used to have a different meaning, but it was changed for consistency with other filesystems, where norecovery is used for skipping log replay. BTRFS does the same and in general will try to avoid any write operations.
  • rescan_uuid_tree โ€” since: 3.12, default: off. Force check and rebuild procedure of the UUID tree. This should not normally be needed. Alternatively the tree can be cleared from userspace by the command btrfs rescue clear-uuid-tree, and then it will be automatically rebuilt in kernel (the mount option is not needed in that case).
  • rescue โ€” since: 5.9. Modes allowing mount with damaged filesystem structures. All of them require the filesystem to be mounted read-only and don't allow remount to read-write. This is supposed to provide unified and more fine grained tuning of errors that affect filesystem operation ๐Ÿซก.
    • usebackuproot (since 5.9) โ€” try to use backup root slots inside super block. Replaces the standalone option usebackuproot.
    • nologreplay (since 5.9) โ€” do not replay any dirty logs. Replaces the standalone option nologreplay.
    • ignorebadroots, ibadroots (since: 5.11) โ€” ignore bad tree roots, greatly improves the chance for data salvage.
    • ignoredatacsums, idatacsums (since: 5.11) โ€” ignore data checksum verification.
    • ignoremetacsums, imetacsums (since 6.12) โ€” ignore metadata checksum verification, useful for interrupted checksum conversion.
    • all (since: 5.9) โ€” enable all supported rescue options.
  • skip_balance โ€” since: 3.3, default: off. Skip automatic resume of an interrupted balance operation. The operation can later be resumed with btrfs balance resume, or the paused state can be removed with btrfs balance cancel. The default behaviour is to resume an interrupted balance immediately after the filesystem is mounted.
  • space_cache, space_cache=<version>, nospace_cache โ€” nospace_cache since: 3.2, space_cache=v1 and space_cache=v2 since 4.5, default: space_cache=v2. Options to control the free space cache ๐Ÿ“. The free space cache greatly improves performance when reading block group free space into memory. However, managing the space cache consumes some resources, including a small amount of disk space.

There are two implementations of the free space cache. The original one, referred to as v1, used to be a safe default but has been superseded by v2. The v1 space cache can be disabled at mount time with nospace_cache without clearing.

On very large filesystems (many terabytes) and certain workloads, the performance of the v1 space cache may degrade drastically ๐Ÿ˜ต. The v2 implementation, which adds a new b-tree called the free space tree, addresses this issue. Once enabled, the v2 space cache will always be used and cannot be disabled unless it is cleared. Use clear_cache,space_cache=v1 or clear_cache,nospace_cache to do so. If v2 is enabled, the v1 space cache will be cleared (at the first mount) and kernels without v2 support will only be able to mount the filesystem in read-only mode. On an unmounted filesystem the caches (both versions) can be cleared by btrfs check --clear-space-cache.

The btrfs-check(8) and mkfs.btrfs commands have full v2 free space cache support since v4.19.

If a version is not explicitly specified, the default implementation will be chosen, which is v2. Iconic ๐Ÿ‘‘.

  • ssd, ssd_spread, nossd, nossd_spread โ€” default: SSD autodetected. Options to control SSD allocation schemes. By default, BTRFS will enable or disable SSD optimizations depending on the status of a device with respect to rotational or non-rotational type. This is determined by the contents of /sys/block/DEV/queue/rotational. If it is 0, the ssd option is turned on. The option nossd will disable the autodetection.

The optimizations make use of the absence of the seek penalty that's inherent for rotational devices ๐ŸŒ€. The blocks can be typically written faster and are not offloaded to separate threads.

Note: since 4.14, the block layout optimizations have been dropped. This used to help with first generations of SSD devices ๐Ÿ’€. Their FTL (flash translation layer) was not effective and the optimization was supposed to improve the wear by better aligning blocks. This is no longer true with modern SSD devices and the optimization had no real benefit. Furthermore, it caused increased fragmentation ๐Ÿ˜ญ. The layout tuning has been kept intact for the option ssd_spread.

okay so real talk: ssd_spread tries to allocate into bigger, aligned chunks of unused space, and it may perform better on low-end SSDs. The catch is it implies ssd, so it's turning on all the other SSD heuristics too. Meanwhile nossd disables all of the SSD options, and nossd_spread only disables ssd_spread. Choose wisely bestie ๐Ÿ’…

  • subvol=path โ€” mount the subvolume at path instead of the toplevel subvolume. The path is always treated as relative to the toplevel subvolume, no exceptions. This mount option overrides the default subvolume that's set for the given filesystem. ๐Ÿ‘‘
  • subvolid=subvolid โ€” mount the subvolume specified by a subvolid number instead of the toplevel subvolume. You can peek at subvolume ID numbers with btrfs subvolume list or btrfs subvolume show. This mount option overrides the default subvolume set for the given filesystem. ๐Ÿ”
Note: if you specify both subvolid and subvol, they have to point at the same subvolume, otherwise the mount will fail. No negotiating, it's giving strict ๐Ÿ’€
  • thread_pool=number โ€” (default: min(NRCPUS + 2, 8) )

The number of worker threads to start. NRCPUS is the number of on-line CPUs detected at the time of mount. A small number leads to less parallelism in processing data and metadata, while higher numbers could lead to a performance hit โ€” because of increased locking contention, process scheduling, cache-line bouncing, or costly data transfers between local CPU memories. Balance is the moment โœจ

  • treelog, notreelog โ€” (default: on)

Enable the tree logging used for fsync and O_SYNC writes. The tree log stores changes without needing a full filesystem sync. The log operations are flushed at sync and transaction commit. If the system crashes between two such syncs, the pending tree log operations are replayed during mount. ๐Ÿซถ

Warning: currently, the tree log is replayed even with a read-only mount! To disable that behaviour, also mount with nologreplay. Not me finding out the hard way ๐Ÿ˜ญ

The tree log could contain new files/directories โ€” and those would not exist on a mounted filesystem if the log is not replayed. Keep that in mind fr fr.

  • usebackuproot โ€” (since: 4.6, removed since: 7.3, default: off)

Enable autorecovery attempts if a bad tree root is found at mount time. Currently this scans a backup list of several previous tree roots and tries to use the first readable one. This can be used with read-only mounts as well. ๐Ÿ›Ÿ

Note: this option has been deprecated in v5.9. This option is fully removed since v7.3. It's gone, babes ๐Ÿ’€
  • user_subvol_rm_allowed โ€” (default: off)

Allow subvolumes to be deleted by their respective owner. Otherwise, only the root user can do that. ๐Ÿ”

Note: historically, any user could create a snapshot even if he was not the owner of the source subvolume, and the subvolume deletion has been restricted for that reason. The subvolume creation has been restricted, but this mount option is still required. This is a usability issue. Since 4.18, the rmdir(2) syscall can delete an empty subvolume just like an ordinary directory. Whether this is possible can be detected at runtime โ€” see the rmdir_subvol feature in FILESYSTEM FEATURES.

๐Ÿ’€ DEPRECATED MOUNT OPTIONS ๐Ÿ’€

List of mount options that have been removed, kept for backward compatibility. They're the ghosts of btrfs past ๐Ÿ‘ป

  • recovery โ€” (since: 3.2, default: off, deprecated since: 4.5)
Note: this option has been replaced by usebackuproot and should not be used, but it will work on 4.5+ kernels. Don't be that girl ๐Ÿ™ˆ
  • inode_cache, noinode_cache โ€” (removed in: 5.11, since: 3.0, default: off)
Note: the functionality has been removed in 5.11. Any stale data created by previous use of the inode_cache option can be removed by btrfs rescue clear-ino-cache. Cleanup era ๐Ÿงน
  • check_int, check_int_data, check_int_print_mask=value โ€” (removed in: 6.7, since: 3.0, default: off)

These debugging options control the behavior of the integrity checking module (the BTRFS_FS_CHECK_INTEGRITY config option is required). The main goal is to verify that all blocks from a given transaction period are properly linked. ๐Ÿ”Ž

check_int enables the integrity checker module, which examines all block write requests to ensure on-disk consistency โ€” at a large memory and CPU cost. ๐Ÿ’ธ

check_int_data includes extent data in the integrity checks, and implies the check_int option.

check_int_print_mask takes a bit mask of BTRFSIC_PRINT_MASK_* values as defined in fs/btrfs/check-integrity.c, to control the integrity checker module behavior.

See the comments at the top of fs/btrfs/check-integrity.c for more information. iykyk ๐Ÿ“–


๐ŸŒˆ NOTES ON GENERIC MOUNT OPTIONS ๐ŸŒˆ

Some of the general mount options from mount(8) that affect BTRFS and are worth mentioning. Group-chat PSA ๐Ÿ’–

  • context โ€” the context refers to the SELinux contexts and policy definitions passed as mount options. This works properly since version v6.8 (because the mount option parser of BTRFS was ported to a new API that also understood the options). Glow up complete โœจ
  • noatime โ€” under read intensive work-loads, specifying noatime significantly improves performance because no new access time information needs to be written. Without this option, the default is relatime, which only reduces the number of inode atime updates in comparison to the traditional strictatime. The worst case for atime updates under relatime occurs when many files are read whose atime is older than 24 h and which are freshly snapshotted. In that case the atime is updated and COW happens โ€” for each file โ€” in bulk. See also <https://lwn.net/Articles/499293/> โ€” Atime and btrfs: a bad combination? (LWN, 2012-05-31). ๐Ÿ˜ญ
Note: noatime may break applications that rely on atime uptimes, like the venerable Mutt (unless you use maildir mailboxes). Choose your fighter ๐ŸฅŠ

๐Ÿ‘‘ MULTI-DEVICE SUPPORT ๐Ÿ‘‘

Btrfs has built-in multi-device support, and supports several profiles that require multiple devices, including RAID0, RAID1, RAID1C3, RAID1C4, RAID10, RAID5 and RAID6. She contains multitudes, literally ๐ŸŒˆ

However the multi-device support has some extra requirements/quirks:

  • Requires proper device scanning and registration ๐Ÿง Like all multi-device storage solutions, btrfs has to scan and register all involved devices to mount the filesystem.

Normally it's done by udev, but for systems without udev, the end users are responsible for proper block file creation and scanning. Bestie you're on your own there fr fr.

  • Initramfs required if the rootfs has multiple devices ๐ŸŒฑ Without an initramfs, the kernel boot sequence doesn't create /dev/ with every block file, thus it's impossible to register all devices to fulfill the mount โ€” even with the device= mount option.

It's strongly recommended to use an initramfs if btrfs is the rootfs. It's very easy and fast to add a new device to an existing btrfs, but without an initramfs the next mount will fail to mount the rootfs. Not the vibe ๐Ÿ’€

  • Device path shown in the mount output ๐Ÿ“ Btrfs maintains an internal device path for each device. The device path is recorded during the initial device scan, and normally doesn't change during the lifespan of that device.

This can lead to inconvenience if the user space tool doesn't do proper parsing. ๐Ÿ˜ค

For example, create a block file at /tmp/block_file, forget all devices, scan and mount that newly created block file, and then delete /tmp/block_file.

This will make btrfs register the device path using /tmp/block_file, even if that file is later deleted.

Tools like lsblk can still handle such cases by using device numbers, but a lot of other tools won't parse the mount point properly, as the device path no longer exists.

Normally this should not be a big deal, as udev is the first program to create those block files and scan them. Phew ๐Ÿซถ

โœจ FILESYSTEM FEATURES โœจ

okay so bestie, the base feature set of btrfs keeps growing over time ๐ŸŒฑ. The backward compatibility is maintained, and every feature is optional โ€” you literally have to explicitly ask for it, so nobody accidentally creates incompatibilities. periodt ๐Ÿ’…

There are a few classes here, and each one has its own tool situation:

  • at mkfs time only โ€” this is for the core structures, like the b-tree nodesize or the checksum algorithm. Peep mkfs.btrfs(8) for the deets ๐Ÿ“–.
  • after mkfs, on an unmounted filesystem โ€” these are the features that optimize internal structures or add new ones to support new functionality. See btrfstune(8). The command btrfs inspect-internal dump-super /dev/sdx dumps a superblock for you, and you can map the value of incompat_flags to the features listed below ๐Ÿ•ต๏ธโ€โ™€๏ธ.
  • after mkfs, on a mounted filesystem โ€” the features of a filesystem (with a given UUID) live in /sys/fs/btrfs/UUID/features/, one file per feature. The status is stored inside the file. A value of 1 means enabled and active, while 0 means the feature was enabled at mount time but got turned off afterwards ๐Ÿ˜ด.

Wanna know if some particular feature can even be turned on for a mounted filesystem? Check the directory /sys/fs/btrfs/features/, one file per feature. A value of 1 means the feature can be enabled โœ….

Here's the list of features (see also mkfs.btrfs(8) section FILESYSTEM FEATURES, iykyk):

  • big_metadata โ€” (since: 3.4, not shown since: 6.0) the filesystem uses nodesize for metadata blocks, and this can be bigger than the page size ๐ŸŒธ
  • block_group_tree โ€” (since: 6.1) block group item representation using a dedicated b-tree. This can greatly reduce mount time for large filesystems โ€” slay for big volumes โšก
  • compress_lzo โ€” (since: 2.6.38) the lzo compression has been used on the filesystem, either as a mount option or via btrfs filesystem defrag ๐Ÿ—œ๏ธ
  • compress_zstd โ€” (since: 4.14) the zstd compression has been used on the filesystem, either as a mount option or via btrfs filesystem defrag ๐Ÿ—œ๏ธ
  • default_subvol โ€” (since: 2.6.34) the default subvolume has been set on the filesystem ๐Ÿ 
  • extended_iref โ€” (since: 3.7) increased hardlink limit per file in a directory to 65536. Older kernels supported a varying number of hardlinks depending on the sum of all file name sizes that can be stored into one metadata block ๐Ÿ”—
  • free_space_tree โ€” (since: 4.5) free space representation using a dedicated b-tree, the successor of v1 space cache โœจ
  • metadata_uuid โ€” (since: 5.0) the main filesystem UUID is the metadata_uuid, which stores the new UUID only in the superblock while all metadata blocks still have the UUID set at mkfs time. See btrfstune(8) for more ๐Ÿ†”
  • mixed_backref โ€” (since: 2.6.31) the last major disk format change, improved backreferences, now default ๐Ÿ‘‘
  • mixed_groups โ€” (since: 2.6.37) mixed data and metadata block groups, i.e. the data and metadata are not separated and occupy the same block groups. This mode is suitable for small volumes, because there are no constraints on how the remaining space should be used (compared to the split mode, where empty metadata space cannot be used for data and vice versa) ๐Ÿ‘ฏ
    • on the other hand, the final layout is quite unpredictable and possibly highly fragmented, which means worse performance ๐Ÿ˜ญ
  • no_holes โ€” (since: 3.14) improved representation of file extents where holes are not explicitly stored as an extent. Saves a few percent of metadata if sparse files are used ๐Ÿ•ณ๏ธ
  • raid1c34 โ€” (since: 5.5) extended RAID1 mode with copies on 3 or 4 devices respectively ๐Ÿ›ก๏ธ
  • raid_stripe_tree โ€” (since: 6.7, CONFIG_BTRFS_EXPERIMENTAL) a separate tree for tracking file extents on RAID profiles ๐ŸŒฒ
  • RAID56 โ€” (since: 3.9) the filesystem contains or contained a RAID56 profile of block groups ๐Ÿ’พ
  • remap_tree โ€” (since: 7.0, experimental) tree with virtual-to-virtual block address mapping ๐Ÿ—บ๏ธ
  • rmdir_subvol โ€” (since: 4.18) indicate that the rmdir(2) syscall can delete an empty subvolume just like an ordinary directory. Note that this feature only depends on the kernel version ๐Ÿ‘€
  • skinny_metadata โ€” (since: 3.10) reduced-size metadata for extent references, saves a few percent of metadata ๐Ÿชถ
  • send_stream_version โ€” (since: 5.10) number of the highest supported send stream version. Experimental features depend on CONFIG_BTRFS_EXPERIMENTAL ๐Ÿ“ค
  • simple_quota โ€” (since: 6.7) simplified quota accounting ๐Ÿงฎ
  • supported_checksums โ€” (since: 5.5) list of checksum algorithms supported by the kernel module. The respective modules โ€” or built-in implementations of the algorithms โ€” need to be present to mount the filesystem. See section CHECKSUM ALGORITHMS ๐Ÿ”
  • supported_sectorsizes โ€” (since: 5.13) list of values that are accepted as sector sizes (mkfs.btrfs --sectorsize) by the running kernel ๐Ÿ“
  • supported_rescue_options โ€” (since: 5.11) list of values for the mount option rescue that are supported by the running kernel. See btrfs(5) ๐Ÿš‘
  • vefity โ€” (since: 5.15, CONFIG_FS_VERITY) the fs-verity read-only file-based authenticity protection can be enabled on filesystems ๐Ÿ”
  • zoned โ€” (since: 5.12, CONFIG_BLK_DEV_ZONED) zoned mode is allocation/write friendly to host-managed zoned devices. Allocation space is partitioned into fixed-size zones that must be updated sequentially. See section ZONED MODE ๐ŸŒ€

๐Ÿ’พ SWAPFILE SUPPORT ๐Ÿ’พ

ok so a swapfile, when it's actually active, is literally a file-backed swap area. btrfs has had this since kernel 5.0 bestie ๐Ÿ’–. Use swapon(8) to turn it on โ€” and until you do that (or again, once you've turned it back off with swapoff(8)) it's just a regular file (with NODATACOW set), so all the special active-swapfile restrictions just don't apply to it. it's giving regular file energy ๐Ÿ’…

but ngl there are some limitations, both in the btrfs implementation and in the Linux swap subsystem:

  • filesystem โ€” must be only single device
  • filesystem โ€” must have only single data profile
  • subvolume โ€” cannot be snapshotted if it contains any active swapfiles
  • swapfile โ€” must be preallocated (i.e. no holes) ๐Ÿ™ˆ
  • swapfile โ€” must be NODATACOW (i.e. also NODATASUM, no compression)

where do these come from? lowkey it's the COW-based design + the block mapping layer that makes all the fancy features (relocation, multi-device filesystems) possible. but the swap subsystem is over here expecting something simpler: no background changes to where the file's blocks live once they've been handed to swap. so the single-device + single-profile constraints? those are about the swapfile itself โ€” the extents and where they sit. technically you can create a swapfile on a multi-device filesystem as long as the extents are all on one device, but you can't control that fr fr, it depends on free space fragmentation and how much unused space is available for new chunks.

and with active swapfiles, these whole-filesystem operations will skip some block groups or just straight up fail:

  • balance โ€” block groups that have extents of any active swapfiles get skipped and reported through dmesg, the rest get processed normally
  • scrub โ€” ditto โœจ
  • resize grow โ€” unaffected, she's fine ๐Ÿ‘‘
  • resize shrink โ€” works as long as the extents of any active swapfiles are outside of the shrunk range
  • device add โ€” works if the new devices don't interfere with any already active swapfiles, though no new swapfile can be activated afterwards
  • device delete โ€” if the device was added as above, it can also be deleted
  • device replace โ€” ditto

so all this means your btrfs maintenance routines get hit HARD, specifically balance and scrub. they'll skip those block groups that have active-swapfile extents on them โ€” and those block groups might also contain extents belonging to other files too ๐Ÿ’€. so the swapfile's impact is amplified, way bigger than the swapfile itself. not the vibe.

for balance, this means data block groups may not be properly balanced, which causes unbalanced data/metadata usage, which can lead to early ENOSPC errors. ๐Ÿ˜ญ

for scrub, this means data block groups may not be verified, which degrades the integrity of data.

so like, be aware of the impact when you're running swapfiles alongside other data. and it's especially discouraged for your root filesystem. don't do that.

๐Ÿšจ also: when there are no active swapfiles and a whole-filesystem exclusive operation is running (e.g. balance, device delete, shrink), you cannot temporarily activate swapfiles. the operation has to finish first, periodt.

to create + activate a swapfile, run these:

# truncate -s 0 swapfile
# chattr +C swapfile
# fallocate -l 2G swapfile
# chmod 0600 swapfile
# mkswap swapfile
# swapon swapfile

and since version 6.1 you can do the whole swapfile creation in a single command (except the activation, that's still separate):

# btrfs filesystem mkswapfile --size 2G swapfile
# swapon swapfile

ok important note: the UUID that the mkswap utility returns identifies the swap "filesystem", and because it's stored in a file, it's not generally visible or usable as an identifier โ€” unlike if it were on a block device. so don't come for her about that ๐Ÿ’…

once activated, the file shows up in /proc/swaps:

# cat /proc/swaps
Filename          Type          Size           Used      Priority
/path/swapfile    file          2097152        0         -2

you can make the swapfile as a one-time thing, or โ€” once properly created โ€” activate it on every boot with the swapon -a command (usually started by the service manager ๐Ÿงš). just drop this entry into /etc/fstab, assuming the filesystem that provides the /path is already mounted at that point. you can set additional mount options relevant for the swapfile too (like priority โ€” not the BTRFS mount options, bestie, different thing).

/path/swapfile        none        swap        defaults      0 0

so from that point on, the subvolume with the active swapfile cannot be snapshotted until the swapfile is deactivated again with swapoff. after that the swapfile is just a regular file and you can snapshot the subvolume again ๐ŸŒธ โ€” though heads up, doing that would prevent activating any swapfile that has been snapshotted. new swapfiles (not snapshotted) can still be created and activated tho, slay.

otherwise, an inactive swapfile does not affect the containing subvolume at all. activation just creates a temporary in-memory status and prevents some file operations, but it's not stored permanently. she's chill when she's off โœจ


๐Ÿ˜ด HIBERNATION ๐Ÿ˜ด

a swapfile can be used for hibernation but it's... not straightforward. before hibernation you have to write a resume offset to the file /sys/power/resume_offset, or set the kernel command line parameter resume_offset.

the value is the physical offset on the device. and note โ€” this is not the same value that filefrag prints as physical offset! ๐Ÿ™ˆ

btrfs uses mapping between logical and physical addresses, but here the physical can still map to one or more device-specific physical block addresses. it's the device-specific physical offset that's suitable as the resume offset. iykyk.

since version 6.1 there's a command btrfs inspect-internal map-swapfile that prints the device physical offset AND the adjusted value for /sys/power/resume_offset. note that the value is divided by page size, i.e. it's not the offset itself. ๐Ÿ‘‘

# btrfs filesystem mkswapfile swapfile
# btrfs inspect-internal map-swapfile swapfile
Physical start: 811511726080
Resume offset:     198122980

for scripting and convenience, the option -r will print just the offset:

# btrfs inspect-internal map-swapfile -r swapfile
198122980

the map-swapfile command also verifies all the requirements โ€” no holes, single device, etc. she checks everything herself ๐Ÿ’…


๐Ÿฉบ TROUBLESHOOTING ๐Ÿฉบ

if your swapfile activation fails, please verify you followed all the steps above, or check the system log (e.g. dmesg or journalctl) for more info. ๐Ÿ”

notably, the swapon utility exits with a message that does NOT say what actually failed:

# swapon /path/swapfile
swapon: /path/swapfile: swapon failed: Invalid argument

the specific reason is likely printed to the system log by the btrfs module though:

# journalctl -t kernel | grep swapfile
kernel: BTRFS warning (device sda): swapfile must have single data profile

so yeah, always check the log bestie, the answer is in there ๐Ÿ’–

โœจ CHECKSUM ALGORITHMS โœจ

okay so bestie, btrfs is out here checksumming your data AND your metadata by default ๐Ÿ’– no cap. The checksum gets calculated before the write even happens, and then verified again after the blocks come back off the devices. Here's the lil structural tea:

  • the whole metadata block keeps an inline checksum, stashed right there in the b-tree node header
  • each data block gets a detached checksum, living in the checksum tree
๐Ÿซถ Note: because a data checksum is calculated literally just before submitting to the block device, btrfs has a strong requirement that the corresponding data block must not be modified until the writeback is finished. That's all fine and good for a buffered write, since btrfs has full control over its page cache. But a direct write (O_DIRECT) bypasses the page cache, and btrfs cannot control the direct IO buffer (since it can live in user space memory ๐Ÿ˜ญ). So it's literally possible for a user space program to modify its direct write buffer before the buffer is fully written back, and that can lead to a data checksum mismatch ๐Ÿ’€. To avoid this, kernels starting with version 6.14 will force a direct write to fall back to buffered, if the inode requires a data checksum. Heads up: this brings an observable performance penalty ๐Ÿ™ˆ. If you require true zero-copy direct writes, then set the NODATASUM flag for the inode and make sure the direct IO buffer is fully aligned to block size โœจ. The kernel starting with version 7.3 will address the unstable buffer by bouncing the buffer if the inode requires data checksums. Even though this is not true zero-copy, it's still better than falling back to buffered โ€” it achieves 95% of true zero-copy performance ๐Ÿ‘‘

There are several checksum algorithms supported, bestie. The default and backward compatible one is crc32c. Since kernel 5.5 there are three more, each with different characteristics and trade-offs around speed and strength. This list is here to help you pick your fighter ๐Ÿ’…:

  • CRC32C (32 bits digest) โ€” the default, best backward compatibility. Very fast, modern CPUs have instruction-level support. Not collision-resistant, but still has good error detection capabilities ๐Ÿฅ‡
  • XXHASH (64 bits digest) โ€” can be used as a CRC32C successor. Very fast, optimized for modern CPUs utilizing instruction pipelining, with good collision resistance and error detection ๐Ÿ”ฅ
  • SHA256 (256 bits digest) โ€” cryptographic-strength hash. Relatively slow, but with possible CPU instruction acceleration or specialized hardware cards. FIPS certified and in wide use ๐Ÿ‘‘
  • BLAKE2b (256 bits digest) โ€” cryptographic-strength hash. Relatively fast, with possible CPU acceleration using SIMD extensions. Not standardized, but based on BLAKE which was a SHA3 finalist, and in wide use. The algorithm used is BLAKE2b-256 that's optimized for 64-bit platforms ๐Ÿฆ‹

Now, the digest size affects the overall size of data block checksums stored in the filesystem. The metadata blocks have a fixed area up to 256 bits (32 bytes), so there's no increase there. Each data block has a separate checksum stored, though, with additional overhead of the b-tree leaves.


๐Ÿ“Š the performance tea ๐Ÿ“Š

Approximate relative performance of the algorithms, measured against CRC32C using implementations on a 11th gen 3.6GHz intel CPU:

  • CRC32C โ€” 470 Cycles/4KiB, Ratio 1.00, implementation: CPU instruction, PCL combination
  • XXHASH โ€” 870 Cycles/4KiB, Ratio 1.9, implementation: reference impl.
  • SHA256 โ€” 7600 Cycles/4KiB, Ratio 16, implementation: libgcrypt
  • SHA256 โ€” 8500 Cycles/4KiB, Ratio 18, implementation: openssl
  • SHA256 โ€” 8700 Cycles/4KiB, Ratio 18, implementation: botan
  • SHA256 โ€” 32000 Cycles/4KiB, Ratio 68, implementation: builtin, CPU instruction
  • SHA256 โ€” 37000 Cycles/4KiB, Ratio 78, implementation: libsodium
  • SHA256 โ€” 78000 Cycles/4KiB, Ratio 166, implementation: builtin, reference impl.
  • BLAKE2b โ€” 10000 Cycles/4KiB, Ratio 21, implementation: builtin/AVX2
  • BLAKE2b โ€” 10900 Cycles/4KiB, Ratio 23, implementation: libgcrypt
  • BLAKE2b โ€” 13500 Cycles/4KiB, Ratio 29, implementation: builtin/SSE41
  • BLAKE2b โ€” 13700 Cycles/4KiB, Ratio 29, implementation: libsodium
  • BLAKE2b โ€” 14100 Cycles/4KiB, Ratio 30, implementation: openssl
  • BLAKE2b โ€” 14500 Cycles/4KiB, Ratio 31, implementation: kcapi
  • BLAKE2b โ€” 14500 Cycles/4KiB, Ratio 34, implementation: builtin, reference impl.
๐ŸŒˆ Note: since kernel v6.16 the accelerated implementation is always used if available. slay.

Many kernels are configured with SHA256 as built-in and not as a module. Up to kernel v6.15 the accelerated versions are however provided by the modules and must be loaded explicitly (modprobe sha256) before mounting the filesystem to make use of them โš ๏ธ. You can check in /sys/fs/btrfs/FSID/checksum which one is used. If you see sha256-generic, then you may want to unmount and mount the filesystem again. Changing that on a mounted filesystem is not possible, periodt ๐Ÿ™…โ€โ™€๏ธ.

Check the file /proc/crypto โ€” when the implementation is built-in, you'd find:

name         : sha256
driver       : sha256-generic
module       : kernel
priority     : 100
...

While accelerated implementation is e.g.:

name         : sha256
driver       : sha256-avx2
module       : sha256_ssse3
priority     : 170
...

๐ŸŒธ COMPRESSION ๐ŸŒธ

Btrfs supports transparent file compression, bestie ๐ŸŽ€. There are three algorithms available: ZLIB, LZO and ZSTD (since v4.14), with various levels. The compression happens on the level of file extents, and the algorithm is selected by file property, mount option or by a defrag command ๐Ÿ’ซ. You can have a single btrfs mount point that has some files that are uncompressed, some that are compressed with LZO, some with ZLIB, for instance (though you may not want it that way, it is supported โ€” no cap ๐Ÿซถ).

Once the compression is set, all newly written data will be compressed, i.e. existing data are untouched โœจ. Data are split into smaller chunks (128KiB) before compression to make random rewrites possible without a high performance hit. Due to the increased number of extents the metadata consumption is higher. The chunks are compressed in parallel ๐Ÿ”ฅ.

The algorithms can be characterized as follows regarding the speed/ratio trade-offs:

ZLIB

  • slower, higher compression ratio ๐ŸŒ
  • levels: 1 to 9, mapped directly, default level is 3
  • good backward compatibility

LZO

  • faster compression and decompression than ZLIB, worse compression ratio, designed to be fast โšก
  • no levels
  • good backward compatibility

ZSTD

  • compression comparable to ZLIB with higher compression/decompression speeds and different ratio ๐Ÿ’…
  • levels: -15..15, mapped directly, default is 3
  • support since 4.14
  • levels 1..15 supported since 5.1
  • levels -15..-1 supported since 6.15

The differences depend on the actual data set and cannot be expressed by a single number or recommendation, fr fr ๐Ÿ˜ญ. Higher levels consume more CPU time and may not bring a significant improvement; lower levels are close to real time.


๐Ÿ’– HOW TO ENABLE COMPRESSION ๐Ÿ’–

okay so bestie, first thing you need to know: compression is usually turned on for the whole filesystem, set at the mount point. and here's the tea โ€” those compression mount options are shared among all mounts of the same filesystem, whether they're bind mounts or subvolume mounts. so like, no sneaking different settings per mount, the vibe is communal. go peek at the MOUNT OPTIONS section in btrfs(5) for the full deets. ๐Ÿซถ

$ mount -o compress=zstd /dev/sdx /mnt

that right there turns on the zstd algorithm at its default level โ€” which is 3, in case you were wondering. but you can set the level manually too, like zstd:3. โœจ

the tradeoff, because there's always a tradeoff: higher levels compress better but cost you time. that extra time can bump up your write latency, so low levels are the move for real-time compression โ€” on a reasonably fast CPU they won't cause any noticeable performance drops. slay. ๐Ÿ’…

$ btrfs filesystem defrag -czstd file

this one starts defragmentation of the whole file and applies the compression, regardless of the mount option. as of version 6.14 you can also pass the compression level with the --level or -L argument. important though: the compression algorithm here is not persistent โ€” it only applies to that one defragmentation command. any other writes get whatever other compression settings apply. periodt.

persistent settings on a per-file basis can be set two ways:

$ chattr +c file
$ btrfs property set file compression zstd

the first command uses the legacy file-attributes interface inherited from the ext2 filesystem, and it's not flexible โ€” by default it sets zlib compression. the other command sets a property on the file with the algorithm you give it. (note: setting the level that way is not yet implemented. someday bestie, someday. ๐Ÿ˜ญ)


๐ŸŒธ COMPRESSION LEVELS ๐ŸŒธ

level support timeline, because version history is important iykyk:

  • ZLIB level support was added in v4.14
  • LZO does not support levels at all (the kernel implementation provides only one)
  • ZSTD level support was added in v5.1
  • negative levels arrived in v6.15

ZLIB has 9 levels supported (1 to 9), mapping 1:1 from the mount option to the algorithm-defined level. the default is level 3, which gives a reasonably good compression ratio and is still reasonably fast. the difference in compression gain between levels 7, 8 and 9 is comparable, but the higher levels take longer. ๐Ÿ’€

ZSTD support includes levels -15..15 โ€” a subset of the full range of what ZSTD provides:

  • -15..-1 are real-time, with worse compression ratio
  • 1..3 are near real-time, with good compression
  • 4..8 are slower with improved compression
  • 9..15 try even harder, though the resulting size may not be significantly improved

higher levels also require more memory, and since they need more CPU, your system performance gets affected. not the system performance bestie!! โญ

Level 0 always maps to the default. and good news: the compression level does not affect compatibility. ๐ŸŽ€


๐Ÿ”ฅ EXCEPTIONS ๐Ÿ”ฅ

any file that has been touched by the fallocate system call will always be excepted from compression โ€” even if the force-compress mount option is used. yes, even then. ๐Ÿ˜ค

the reason: a successful fallocate call must guarantee that future writes to the allocated range will not fail due to lack of space. that is genuinely difficult to guarantee in a COW filesystem. to reduce the chances of it happening, btrfs preallocates space and disables compression for the file.

as a workaround, you can trigger a compressed rewrite for such a file using the btrfs defrag command. but be aware โ€” if the file gets touched again by the fallocate system call, it will be excepted again from compression for all the new data written to it. it's giving cursed, honestly. ๐Ÿ™ˆ


๐Ÿ’œ INCOMPRESSIBLE DATA ๐Ÿ’œ

files with already-compressed data, or data that just won't compress well within the CPU and memory constraints of the kernel implementations, go through a simple decision logic. if the first portion of data being compressed is not smaller than the original, compression of the whole file is disabled. ๐Ÿšซ

unless the filesystem is mounted with compress-force, in which case btrfs will try compressing every block, falling back to storing the uncompressed version for each block that ends up larger after compression. this is not optimal and is subject to optimizations and further development. (we love an honest king.)

if a file gets identified as incompressible, a flag is set โ€” NOCOMPRESS โ€” and it's sticky. on that file, compression won't be performed unless forced. the flag can also be set by chattr +m (since e2fsprogs 1.46.2), or by properties with value no or none. an empty value will reset it to the default that's currently applicable on the mounted filesystem. โœจ

there are two ways to detect incompressible data:

  • actual compression attempt โ€” data are compressed, and if the result is not smaller, it's discarded. so this depends on the algorithm and level
  • pre-compression heuristics โ€” a quick statistical evaluation on the data is performed, and based on the result either compression is performed or skipped. the NOCOMPRESS bit is not set just by the heuristic, only if the compression algorithm does not make an improvement
$ lsattr file
---------------------m file
using force compression is not recommended. the heuristics are supposed to decide that, and compression algorithms internally detect incompressible data too. let the algorithm cook. ๐Ÿ“

๐Ÿฆ‹ PRE-COMPRESSION HEURISTICS ๐Ÿฆ‹

the heuristics aim to do a few quick statistical tests on the compressed data, in order to avoid probably-costly compression that would turn out to be inefficient. compression algorithms could have internal detection of incompressible data too, but that leads to more overhead โ€” the compression is done in another thread and it has to write the data anyway. the heuristic is read-only and can utilize cached memory. ๐Ÿ’…

the tests performed are based on the following: data sampling, long repeated pattern detection, byte frequency, and Shannon entropy.


๐Ÿ‘‘ COMPATIBILITY ๐Ÿ‘‘

compression requires both data checksums and COW, so either nodatasum โ€” whether mount option or inode flag โ€” will result in no compression. that's just how it is.

direct IO reads of compressed data will always fall back to buffered reads.

direct IO write behavior depends on the inode flag and kernel version. ok so let's break it down:

  • before v7.3, for inodes with data checksums, direct IO writes always fall back to buffered writes, thus can generate compressed data if the mount option / inode flags allow that
  • for inodes without data checksums, direct IO writes will not populate page cache, and since the inode has no data checksums, no compressed data will be generated anyway
  • starting with kernel v7.3, no compression is performed, no matter if the inode has data checksums or not

the compression algorithms have been added over time, so version compatibility should also be considered โ€” together with other tools that may access the compressed data, like bootloaders. ๐Ÿ‘‘


๐ŸŒˆ SYSFS INTERFACE ๐ŸŒˆ

btrfs has a sysfs interface to provide extra knobs. yes, extra knobs. we love a knob. โœจ

the top level path is /sys/fs/btrfs/, and the main directory layout is the following:

  • features/ โ€” All supported features โ€” version 3.14
  • UUID/ โ€” Mounted fs UUID โ€” version 3.14
  • UUID/allocation/ โ€” Space allocation info โ€” version 3.14
  • UUID/bdi/ โ€” Backing device info (writeback) โ€” version 5.9
  • UUID/debug/ โ€” Debugging information (optional) โ€” version 5.6
  • UUID/devices/NODE/ โ€” Symlink to each block device sysfs โ€” version 5.6
  • UUID/devinfo/DEVID/ โ€” Btrfs specific info for each device โ€” version 5.6
  • UUID/discard/ โ€” Discard stats and tunables โ€” version 6.1
  • UUID/features/ โ€” Features of the filesystem โ€” version 3.14
  • UUID/qgroups/ โ€” Global qgroup info โ€” version 5.9
  • UUID/qgroups/LEVEL_ID/ โ€” Info for each qgroup โ€” version 5.9

for the /sys/fs/btrfs/features/ directory, each file means a supported feature of the current kernel. most files have value 0. otherwise it depends on the file โ€” value 1 typically means the feature can be turned on a mounted filesystem. some directories may depend on a feature (qgroups) or a build config option (debug and CONFIG_BTRFS_DEBUG). ๐Ÿ’–

For /sys/fs/btrfs/UUID/features/ directory, each file means an enabled feature on the mounted filesystem. ๐Ÿ’…

The features share the same name in section FILESYSTEM FEATURES. iykyk bestie, the names match up so you can cross-reference and slay.

๐Ÿฆ‹ UUID

Files in /sys/fs/btrfs/UUID/ directory are:

  • bg_reclaim_threshold โ€” (RW, since: 5.19) Used space percentage of total device space to start auto block group claim. Mostly for zoned devices. Lowkey a zoned-device thing ๐ŸŒ€
  • checksum โ€” (RO, since: 5.5) The checksum used for the mounted filesystem. This includes both the checksum type (peep section CHECKSUM ALGORITHMS) and the selected implementation (depends on kernel, it used to be type of hardware acceleration):
crc32c (crc32c-lib)
  • clone_alignment โ€” (RO, since: 3.16) The bytes alignment for clone and dedupe ioctls. No cap. ๐Ÿงฌ
  • commit_stats โ€” (RW, since: 6.0) The performance statistics for btrfs transaction commit since the first mount. For debugging or monitoring purposes. โœจ

Writing into this file will reset the maximum commit duration (max_commit_ms) to 0. The file looks like:

commits 70649
last_commit_ms 2
max_commit_ms 131
total_commit_ms 170840
  • commits โ€” number of transaction commits since the first mount
  • last_commit_ms โ€” duration in milliseconds of the last commit
  • max_commit_ms โ€” maximum time a transaction commit took since first mount or last reset
  • total_commit_ms โ€” sum of all transaction commit times
  • exclusive_operation โ€” (RO, since: 5.10) Show the running exclusive operation. Check section FILESYSTEM EXCLUSIVE OPERATIONS for details. ๐Ÿ‘€
  • generation โ€” (RO, since: 5.11) Show the generation of the mounted filesystem.
  • label โ€” (RW, since: 3.14) Show the current label of the mounted filesystem, followed by a newline (which is otherwise trimmed from the label). ๐Ÿท๏ธ
  • metadata_uuid โ€” (RO, since: 5.0) Show the metadata UUID of the mounted filesystem. Check the metadata_uuid feature for more details.
  • nodesize โ€” (RO, since: 3.14) Show the nodesize of the mounted filesystem.
  • quota_override โ€” (RW, since: 4.13) Show the current quota override status:
    • 0 โ€” no quota override.
    • 1 โ€” quota override, writes will ignore the existing limit settings
  • read_policy โ€” (RW, since: 5.11) Show the current policy for balancing reads from devices. Currently only pid (balance using the process id (pid) value) is supported. More balancing policies are available in experimental build, namely round-robin.
  • sectorsize โ€” (RO, since: 3.14) Show the sectorsize of the mounted filesystem. ๐Ÿ“
  • temp_fsid โ€” (RO, since 6.7) Indicate that this filesystem got assigned a temporary FSID at mount time, making possible to mount devices with the same FSID. ๐Ÿซถ

๐ŸŽ€ UUID/allocations

Files and directories in /sys/fs/btrfs/UUID/allocations directory are:

  • global_rsv_reserved โ€” (RO, since: 3.14) The used bytes of the global reservation.
  • global_rsv_size โ€” (RO, since: 3.14) The total size of the global reservation.
  • data/, metadata/ and system/ directories โ€” (RO, since: 5.14) Space info accounting for the 3 block group types.

๐ŸŒธ UUID/allocations/{data,metadata,system}

Files in /sys/fs/btrfs/UUID/allocations/data,metadata,system directory are:

  • bg_reclaim_threshold โ€” (RW, since: 5.19) Reclaimable space percentage of block group's size (excluding permanently unusable space) to reclaim the block group. Can be used on regular or zoned devices.
  • bytes_* โ€” (RO) Values of the corresponding data structures for the given block group type and profile that are used internally and may change rapidly depending on the load.

Complete list: bytes_may_use, bytes_pinned, bytes_readonly, bytes_reserved, bytes_used, bytes_zone_unusable ๐Ÿ’€

  • chunk_size โ€” (RW, since: 6.0) Show the chunk size. Can be changed for data and metadata (independently) and cannot be set for system block group type. Cannot be set for zoned devices as it depends on the fixed device zone size. Upper bound is 10% of the filesystem size, the value must be multiple of 256MiB and greater than 0. โš ๏ธ
  • disk_total โ€” (RO, since: 3.14) Total number of bytes on devices occupied by this block group type.
  • disk_used โ€” (RO, since: 3.14) Portion of disk_total in bytes which is used by this block group type.
  • dynamic_reclaim โ€” (RW, since: 6.11) Enable or disable dynamic block group reclaim. Use a calculated threshold instead of fixed value in bg_reclaim_threshold.
  • flags โ€” (RO, since: 6.11) Raw decimal value of block group flags.
  • force_chunk_alloc โ€” (RW, since: 6.0, CONFIG_BTRFS_DEBUG) (Privileged write.) Force allocation of a new chunk of this block group type. Note his can cause ENOSPC, users should know what they're doing. ๐Ÿ˜ญ
fr fr do not just poke at force_chunk_alloc for the plot. You can genuinely run yourself out of space and the kernel will be like "bestie I warned you."
  • periodic_reclaim โ€” (RW, since: 6.11) Enable or disable periodic block group reclaim based on bg_reclaim_threshold.
  • reclaim_bytes โ€” (RO, since: 6.11) Monotonically increasing counter of reclaimed bytes.
  • reclaim_count โ€” (RO, since: 6.11) Monotonically increasing counter of block group reclaim attempts.
  • reclaim_errors โ€” (RO, since: 6.11) Monotonically increasing counter of reclaim errors.
  • size_classes โ€” (RO, since: 6.3) Numbers of block groups of a given class based on heuristics that measure extent length, age and fragmentation. ๐Ÿงš
none 136
small 374
medium 282
large 93
  • total_bytes โ€” (RO, since: 3.14) Total bytes.

๐Ÿ’œ UUID/bdi

Symlink to the sysfs directory of the backing device info (BDI), which is related to writeback process and infrastructure. It's giving plumbing, but iconic plumbing. ๐Ÿ”ง

โญ UUID/devices

Files in /sys/fs/btrfs/UUID/devices directory are symlinks named after device nodes (e.g. sda, dm-0) and pointing to their sysfs directory.

๐ŸŒˆ UUID/devinfo

The directory contains subdirectories named after device ids (numeric values). Each subdirectory has information about the device of the given devid.

๐Ÿ“ UUID/devinfo/DEVID

Files in /sys/fs/btrfs/UUID/devinfo/DEVID directory are:

  • error_stats โ€” (RO, since: 5.14) Show device stats of this device, same as btrfs device stats (btrfs-device(8) <>). ๐Ÿ‘‘
write_errs 0
read_errs 0
flush_errs 0
corruption_errs 0
generation_errs 0
  • fsid โ€” (RO, since: 5.17) Show the fsid which the device belongs to. It can be different than the UUID if it's a seed device.
  • in_fs_metadata โ€” (RO, since: 5.6) Show whether we have found the device. Should always be 1, as if this turns to 0, the DEVID directory would get removed automatically.
  • missing โ€” (RO, since: 5.6) Show whether the device is considered missing by the kernel module. ๐Ÿ™ˆ
  • replace_target โ€” (RO, since: 5.6) Show whether the device is the replace target. If no device replace is running, this value is 0.
  • scrub_speed_max โ€” (RW, since: 5.14) Show the scrub speed limit for this device. The unit is Bytes/s. 0 means no limit. The value can be set but is not persistent and not preserved beyond mount. โณ
  • writeable โ€” (RO, since: 5.6) Show if the device is writeable.

๐Ÿฆ‹ UUID/qgroups

Files in /sys/fs/btrfs/UUID/qgroups/ directory are:

  • drop_subtree_threshold โ€” (RW, since: 6.1) Show the subtree drop threshold to automatically mark qgroup inconsistent.

When dropping large subvolumes with qgroup enabled, there would be a huge load for qgroup accounting. If we have a subtree whose level is larger than or equal to this value, we will not trigger qgroup account at all, but mark qgroup inconsistent to avoid the huge workload.

Default value is 3, which means that trees of low height will be accounted properly as this is sufficiently fast. The value was 8 until 6.13 where no subtree drop can trigger qgroup rescan making it less useful.

Lower value can reduce qgroup workload, at the cost of extra qgroup rescan to re-calculate the numbers. ๐Ÿ’…

  • enabled โ€” (RO, since: 6.1) Show if qgroup is enabled. Also, if qgroup is disabled, the qgroups directory will be removed automatically.
  • inconsistent โ€” (RO, since: 6.1) Show if the qgroup numbers are inconsistent. If 1, it's recommended to do a qgroup rescan.
  • mode โ€” (RO, since: 6.7) Show which quota mode is enabled, can be qgroup for full accounting or squota for simplified accounting.

๐ŸŒธ UUID/qgroups/LEVEL_ID

Files in each /sys/fs/btrfs/UUID/qgroups/LEVEL_ID/ directory are:

  • exclusive โ€” (RO, since: 5.9) Show the exclusively owned bytes of the qgroup.
  • limit_flags โ€” (RO, since: 5.9) Show the numeric value of the limit flags. If 0, means no limit implied.
  • max_exclusive โ€” (RO, since: 5.9) Show the limits on exclusively owned bytes.
  • max_referenced โ€” (RO, since: 5.9) Show the limits on referenced bytes.
  • referenced โ€” (RO, since: 5.9) Show the referenced bytes of the qgroup.
  • rsv_data โ€” (RO, since: 5.9) Show the reserved bytes for data.
  • rsv_meta_pertrans โ€” (RO, since: 5.9) Show the reserved bytes for per transaction metadata.
  • rsv_meta_prealloc โ€” (RO, since: 5.9) Show the reserved bytes for preallocated metadata.

๐ŸŽ€ UUID/discard

Files in /sys/fs/btrfs/UUID/discard/ directory are:

  • discardable_bytes โ€” (RO, since: 6.1) Show amount of bytes that can be discarded in the async discard and nodiscard mode. ๐Ÿ—‘๏ธ
  • discardable_extents โ€” (RO, since: 6.1) Show number of extents to be discarded in the async discard and nodiscard mode.
  • discard_bitmap_bytes โ€” (RO, since: 6.1) Show amount of discarded bytes from data tracked as bitmaps.

discard_extent_bytes

(RO, since: 6.1) โ€” shows you the amount of discarded extents from data tracked as bitmaps. ๐Ÿ’…

discard_bytes_saved

(RO, since: 6.1) โ€” shows the amount of bytes that were reallocated without being discarded.

kbps_limit

(RW, since: 6.1) โ€” a tunable limit of kilobytes per second issued as discard IO in the async discard mode.

iops_limit

(RW, since: 6.1) โ€” a tunable limit of the number of discard IO operations to be issued in the async discard mode.

max_discard_size

(RW, since: 6.1) โ€” a tunable limit for the size of one IO discard request.


๐Ÿšง FILESYSTEM EXCLUSIVE OPERATIONS ๐Ÿšง

okay so bestie, there are some operations that touch the whole filesystem, and they literally cannot run at the same time. Like, one at a time, in the group chat, no talking over each other. If you try to start one while another is already running, it will fail (see the exceptions below, because of course there are exceptions ๐Ÿซถ).

Since kernel 5.10 you can peek at whichever operation is currently running from /sys/fs/UUID/exclusive_operation, and it'll show you one of these values:

  • balance
  • balance paused (since 5.17)
  • device add
  • device delete
  • device replace
  • resize
  • swapfile activate
  • none

Enqueuing is supported for several btrfs subcommands, so you can kick a bunch of them off at once and they'll get serialized. queue girlies rise ๐Ÿ™ˆ

And there's an exception: when a paused balance is hanging around, it allows a device add operation to start, because those two don't really collide โ€” and honestly this is iconic because it means you can add more space so the balance can actually finish. ๐Ÿ’–


๐Ÿ“ FILESYSTEM LIMITS ๐Ÿ“

maximum file name length

255

This limit is imposed by Linux VFS, bestie โ€” the structures of BTRFS could totally store larger file names. it's not BTRFS's fault, it's the VFS's. ๐Ÿ’€

This one depends on the nodesize value. For 4KiB it's 3949 bytes, and for larger nodesize it's 4095 due to the system limit PATH_MAX.

Worth knowing: the symlink target may not be a valid path, i.e. the path name components can exceed the limits (NAME_MAX). There's no content validation at symlink(3) creation. no vibe check, no nothing. ๐ŸŒธ

maximum number of inodes

264 but this depends on the available metadata space, because the inodes are created dynamically. main character energy: they come into existence whenever they want.

Each subvolume is an independent namespace of inodes and thus their numbers, so the limit is per subvolume, not for the whole filesystem. โœจ

inode numbers

Minimum number: 256 (for subvolumes), regular files and directories: 257, maximum number: (264 - 256).

The inode numbers that can be assigned to user created files are from the whole 64bit space โ€” except the first 256 and the last 256 in that range, which are reserved for internal b-tree identifiers. ๐Ÿ”’

maximum file length

The inherent limit of BTRFS is 264 (16 EiB) but the practical limit of Linux VFS is 263 (8 EiB).

maximum number of subvolumes

The subvolume ids can go up to 248, but the number of actual subvolumes depends on the available metadata space. fr fr it's a "how much room do you have" situation, not a "here's your exact cap" situation.

The space consumed by all subvolume metadata โ€” including bookkeeping of shared extents โ€” can be large (MiB, GiB). And the range is not the full 64bit range, because of qgroups that use the upper 16 bits for another purposes. yes, that's the actual reason, not me being vague. ๐Ÿ’…

65536 when the extref feature is turned on during mkfs (which is the default), and roughly 100 otherwise โ€” and even that depends on the file name length that fits into one metadata node.

minimum filesystem size

The minimal size of each device depends on the mixed-bg feature. Without that (the default) it's about 109MiB, and with mixed-bg it's 16MiB. tiny king ๐Ÿงš


๐Ÿ‘‘ BOOTLOADER SUPPORT ๐Ÿ‘‘

GRUB2 (https://www.gnu.org/software/grub) has the most advanced support of booting from BTRFS with respect to features. she ate, honestly.

U-Boot (https://www.denx.de/wiki/U-Boot/) has decent support for booting, but not all BTRFS features are implemented โ€” so check the documentation, bestie, don't just assume. ๐Ÿฆ‹

In general, the first 1MiB on each device is unused, with the exception of the primary superblock that is on the offset 64KiB and spans 4KiB. The rest can be freely used by bootloaders or for other system information.

โš ๏ธ Note that booting from a filesystem on zoned device is not supported. not even a little. ๐Ÿ’€

๐ŸŽ€ FILE ATTRIBUTES ๐ŸŽ€

The btrfs filesystem supports setting file attributes or flags. And listen โ€” there are old and new interfaces with confusing names, so let's just clarify that right now before anyone spirals:

  • attributes: chattr(1) or lsattr(1) utilities (the ioctls are FS_IOC_GETFLAGS and FS_IOC_SETFLAGS). due to the ioctl names, the attributes are also called flags. iykyk.
  • xflags: to distinguish from the previous, it's extended flags, with tunable bits similar to the attributes but extensible โ€” and new bits will be added in the future (the ioctls are FS_IOC_FSGETXATTR and FS_IOC_FSSETXATTR, but they are not related to extended attributes that are also called xattrs). There's no standard tool to change the bits, but there's support in xfs_io(8) as command xfs_io -c chattr.

Attributes have constraints associated and not all combinations can be set โ€” the order of setting them also matters. Most attributes apply to files and directories but the semantics may differ. For directories the attribute may only mean to set this attribute to all new files (inheritable in the list below). Some attributes need root privileges to be set. yes queen, sometimes you gotta sudo. ๐Ÿ‘‘

Attributes

  • a (file, dir, root) โ€” append only, new writes are always written at the end of the file. no cutting the line, babe. ๐Ÿ“
  • A (file, dir) โ€” no atime updates.
  • c (file, dir, inherited) โ€” compress data, all data written after this attribute is set will be compressed. Please note that compression is also affected by the mount options or the parent directory attributes.

When set on a directory, all newly created files will inherit this attribute. This attribute cannot be set with m at the same time. pick a lane. ๐Ÿ’…

  • C (file, dir, inherited) โ€” no copy-on-write, file data modifications are done in-place.

When set on a directory, all newly created files will inherit this attribute.

โš ๏ธ Note: Due to implementation limitations, this flag can be set/unset only on empty files. empty files only, she's picky. ๐Ÿ˜ญ
  • d (file) โ€” no dump, makes sense with 3rd party tools like dump(8). On BTRFS the attribute can be set/unset but no other special handling is done. so like... she's there, she's just not doing much.
  • D (dir) โ€” synchronous directory updates, for more details search open(2) for O_SYNC and O_DSYNC.
  • i (file, dir, root) โ€” immutable, no file data and metadata changes allowed even to the root user as long as this attribute is set (obviously the exception is unsetting the attribute). ROOT PROOF. periodt. ๐Ÿ”ฅ
  • m (file, dir) โ€” no compression, permanently turn off compression on the given file. Any compression mount options will not affect this file. (chattr(1) support added in 1.46.2.)

When set on a directory, all newly created files will inherit this attribute. This attribute cannot be set with c at the same time. again: pick a lane. ๐ŸŒˆ

  • S (file) โ€” synchronous updates, for more details search open(2) for O_SYNC and O_DSYNC.
  • V (file, read-only) โ€” fs-verity enabled on the file.

No other attributes are supported. For the complete list please refer to the chattr(1) manual page. ๐Ÿ’œ

XFLAGS

There's an overlap of letters assigned to the bits with the attributes, so heads up: this list refers to what xfs_io(8) provides:

  • i โ€” immutable, same as the attribute.
  • a โ€” append only, same as the attribute.
  • s โ€” synchronous updates, same as the attribute S.
  • A โ€” no atime updates, same as the attribute.
  • d โ€” no dump, same as the attribute.

๐ŸŒˆ ZONED MODE ๐ŸŒˆ

okay bestie, since version 5.12 btrfs supports something called zoned mode. It's a special on-disk format and allocation/write strategy that's friendly to zoned devices. Short version: a device gets partitioned into fixed-size zones and each zone can be updated append-only style, or reset. Since btrfs has no fixed data structures (except, like, the super blocks obviously), zoned mode only needs block placement that follows the device constraints. You can learn about the whole architecture at <https://zonedstorage.io> . ๐Ÿฆ‹

The devices are also called SMR/ZBC/ZNS, in host-managed mode. Not me warning you: there are devices that appear non-zoned but actually are, that's drive-managed and using zoned mode won't help. ๐Ÿ˜ญ

The zone size depends on the device, typical sizes are 256MiB or 1GiB. In general it must be a power of two. Emulated zoned devices like null_blk let you set various zone sizes. โœจ

Requirements, limitations ๐ŸŽ€

  • all devices must have the same zone size
  • maximum zone size is 8GiB
  • minimum zone size is 4MiB
  • mixing zoned and non-zoned devices is possible, the zone writes are emulated, but this is namely for testing
  • the super block is handled in a special way and is at different locations than on a non-zoned filesystem:
    • primary: 0B (and the next two zones)
    • secondary: 512GiB (and the next two zones)
    • tertiary: 4TiB (4096GiB, and the next two zones)

Incompatible features ๐Ÿ’€

The main constraint of the zoned devices is lack of in-place update of the data. This is inherently incompatible with some features โ€” and babe, I mean inherently, no workaround vibes here:

  • NODATACOW โ€” overwrite in-place, cannot create such files
  • fallocate โ€” preallocating space for in-place first write
  • mixed-bg โ€” unordered writes to data and metadata, fixing that means using separate data and metadata block groups
  • booting โ€” the zone at offset 0 contains superblock, resetting the zone would destroy the bootloader data

Initial support lacks some features but they're planned:

  • only single (data, metadata) and DUP (metadata) profile is supported
  • fstrim โ€” due to dependency on free space cache v1

Super block ๐Ÿ‘‘

As said above, super block is handled in a special way. In order to be crash safe, at least one zone in a known location must contain a valid superblock. This is implemented as a ring buffer in two consecutive zones, starting from known offsets 0B, 512GiB and 4TiB.

The values are different than on non-zoned devices. Each new super block is appended to the end of the zone, once it's filled, the zone is reset and writes continue to the next one. Looking up the latest super block needs to read offsets of both zones and determine the last written version.

The amount of space reserved for super block depends on the zone size. The secondary and tertiary copies are at distant offsets as the capacity of the devices is expected to be large, tens of terabytes. Maximum zone size supported is 8GiB, which would mean that e.g. offset 0-16GiB would be reserved just for the super block on a hypothetical device of that zone size. This is wasteful but required to guarantee crash safety. ๐Ÿ™ˆ

Zone reclaim, garbage collection ๐Ÿ—‘๏ธ

As the zones are append-only, overwriting data or COW changes in metadata make parts of the zones used but not connected to the filesystem structures. This makes the space unusable and grows over time. Once the ratio hits a (configurable) threshold a background reclaim process is started and relocates the remaining blocks in use to a new zone. The old one is reset and can be used again. slay.

This process may take some time depending on other background work or amount of new data written. It is possible to hit an intermittent ENOSPC. Some devices also limit number of active zones. ๐Ÿ˜ญ

Devices ๐Ÿ”ฅ

Real hardware ๐Ÿ’พ

The WD Ultrastar series 600 advertises HM-SMR, i.e. the host-managed zoned mode. There are two more: DA (device managed, no zoned information exported to the system), HA (host aware, can be used as regular disk but zoned writes improve performance). There are not many devices available at the moment, the information about exact zoned mode is hard to find, check data sheets or community sources gathering information from real devices.

Note: zoned mode won't work with DM-SMR disks. periodt.

Emulated: null_blk ๐Ÿงš

The driver null_blk provides memory backed device and is suitable for testing. There are some quirks setting up the devices. The module must be loaded with nr_devices=0 or the numbering of device nodes will be offset. The configfs must be mounted at /sys/kernel/config and the administration of the null_blk devices is done in /sys/kernel/config/nullb. The device nodes are named like /dev/nullb0 and are numbered sequentially. NOTE: the device name may be different than the named directory in sysfs!

Setup:

modprobe configfs
modprobe null_blk nr_devices=0

Create a device mydev, assuming no other previously created devices, size is 2048MiB, zone size 256MiB. There are more tunable parameters, this is a minimal example taking defaults:

cd /sys/kernel/config/nullb/
mkdir mydev
cd mydev
echo 2048 > size
echo 1 > zoned
echo 1 > memory_backed
echo 256 > zone_size
echo 1 > power

This will create a device /dev/nullb0 and the value of file index will match the ending number of the device node.

Remove the device:

rmdir /sys/kernel/config/nullb/mydev

Then continue with mkfs.btrfs /dev/nullb0, the zoned mode is auto-detected. โœจ

For convenience, there's a script wrapping the basic null_blk management operations <https://github.com/kdave/nullb.git>, the above commands become:

nullb setup
nullb create -s 2g -z 256
mkfs.btrfs /dev/nullb0
...
nullb rm nullb0

Emulated: TCMU runner ๐ŸŒธ

TCMU is a framework to emulate SCSI devices in userspace, providing various backends for the storage, with zoned support as well. A file-backed zoned device can provide more options for larger storage and zone size. Please follow the instructions at <https://zonedstorage.io/projects/tcmu-runner/> .

Compatibility, incompatibility ๐Ÿซถ

  • the feature sets an incompat bit and requires new kernel to access the filesystem (for both read and write)
  • superblock needs to be handled in a special way, there are still 3 copies but at different offsets (0, 512GiB, 4TiB) and the 2 consecutive zones are a ring buffer of the superblocks, finding the latest one needs reading it from the write pointer or do a full scan of the zones
  • mixing zoned and non zoned devices is possible (zones are emulated) but is recommended only for testing
  • mixing zoned devices with different zone sizes is not possible
  • zone sizes must be power of two, zone sizes of real devices are e.g. 256MiB or 1GiB, larger size is expected, maximum zone size supported by btrfs is 8GiB

Status, stability, reporting bugs ๐Ÿ™‹

The zoned mode has been released in 5.12 and there are still some rough edges and corner cases one can hit during testing. Please report bugs to <https://github.com/naota/linux/issues/> .

References ๐Ÿ“š


๐Ÿ’… CONTROL DEVICE ๐Ÿ’…

okay so there's this character special device /dev/btrfs-control with major and minor numbers 10 and 234 (the device can be found under the misc category). iconic.

$ ls -l /dev/btrfs-control
crw------- 1 root root 10, 234 Jan  1 12:00 /dev/btrfs-control

The device accepts some ioctl calls that can perform following actions on the filesystem module:

  • scan devices for btrfs filesystem (i.e. to let multi-device filesystems mount automatically) and register them with the kernel module
  • similar to scan, but also wait until the device scanning process is finished for a given filesystem
  • get the supported features (can be also found under /sys/fs/btrfs/features)

The device is created when btrfs is initialized, either as a module or a built-in functionality and makes sense only in connection with that. Running e.g. mkfs without the module loaded will not register the device and will probably warn about that.

In rare cases when the module is loaded but the device is not present (most likely accidentally deleted), it's possible to recreate it by

# mknod --mode=600 /dev/btrfs-control c 10 234

or (since 5.11) by a convenience command

# btrfs rescue create-control-device

The control device is not strictly required but the device scanning will not work and a workaround would need to be used to mount a multi-device filesystem. The mount option device can trigger the device scanning during mount, see also btrfs device scan. โœจ

๐Ÿ‘ฏโ€โ™€๏ธ FILESYSTEM WITH MULTIPLE PROFILES ๐Ÿ‘ฏโ€โ™€๏ธ

ok bestie, so here's a thing that can happen and it lowkey stresses people out but it's fine: a btrfs filesystem can end up holding multiple block group profiles of the same type. ๐Ÿ˜ณ How? Usually when a profile conversion using balance filters got interrupted (peep btrfs-balance(8) for that lore). So some btrfs commands will literally vibe-check the filesystem for this exact situation and hit you with a warning like this:

WARNING: Multiple block group profiles detected, see 'man btrfs(5)'.
WARNING:   Data: single, raid1
WARNING:   Metadata: single, raid1

and the matching output of btrfs filesystem df might be giving something like:

WARNING: Multiple block group profiles detected, see 'man btrfs(5)'.
WARNING:   Data: single, raid1
WARNING:   Metadata: single, raid1
Data, RAID1: total=832.00MiB, used=0.00B
Data, single: total=1.63GiB, used=0.00B
System, single: total=4.00MiB, used=16.00KiB
Metadata, single: total=8.00MiB, used=112.00KiB
Metadata, RAID1: total=64.00MiB, used=32.00KiB
GlobalReserve, single: total=16.25MiB, used=0.00B

see how there's more than one line for type Data and Metadata, while the profiles are single and RAID1? yeah. that's the tea. ๐Ÿซ–

This state of the filesystem is OK but it most likely needs the user/administrator to take an action and finish the interrupted tasks. This cannot be easily done automatically, also the user knows the expected final profiles. ๐Ÿ’โ€โ™€๏ธ

In the example above, the filesystem started as a single device and single block group profile. Then another device was added, followed by balance with convert=raid1 but for some reason hasn't finished. Restarting the balance with convert=raid1 will continue and end up with a filesystem with all block group profiles RAID1. slay, closure. โœจ

Note: If you're familiar with balance filters, you can use convert=raid1,profiles=single,soft, which will take only the unconverted single profiles and convert them to raid1. This may speed up the conversion as it would not try to rewrite the already converted raid1 profiles. ๐Ÿ’…

Having just one profile is desired because this also clearly defines the profile of newly allocated block groups โ€” otherwise this depends on internal allocation policy. And when there are multiple profiles present, the order of selection is RAID56, RAID10, RAID1, RAID0 as long as the device number constraints are satisfied. ๐Ÿ‘‘

The commands that print that warning were chosen so they're brought to the user's attention when the filesystem state is being changed in that regard. This is: device add, device delete, balance cancel, balance pause. ๐Ÿšจ

And commands that report space usage: filesystem df, device usage. The command filesystem usage provides a line in the overall summary:

Multiple profiles:                 yes (data, metadata)

๐ŸŒฑ SEEDING DEVICE ๐ŸŒฑ

okay this one is genuinely iconic. ๐ŸŽ€ The COW mechanism plus multiple devices under one hood enables an interesting concept called a seeding device: extending a read-only filesystem on a device with another device that captures all writes. For example, imagine an immutable golden image of an operating system enhanced with another device that allows you to use the data from the golden image and do normal operation. This idea originated on CD-ROMs with a base OS and allowing them to be used for live systems, but this became obsolete. There are technologies providing similar functionality, like unionmount <https://en.wikipedia.org/wiki/Union_mount>, overlayfs <https://en.wikipedia.org/wiki/OverlayFS> or qcow2 <https://en.wikipedia.org/wiki/Qcow#qcow2> image snapshot. ๐Ÿฆ‹

The seeding device starts as a normal filesystem, and once the contents is ready, btrfstune -S 1 is used to flag it as a seeding device. Mounting such a device will not allow any writes, except adding a new device by btrfs device add. Then the filesystem can be remounted as read-write. ๐Ÿ”ฅ

Given that the filesystem on the seeding device is always recognized as read-only, it can be used to seed multiple filesystems from one device at the same time. The UUID that is normally attached to a device is automatically changed to a random UUID on each mount. โœจ

Note: Before v6.17 kernel, a seed device could have been mounted independently along with sprouted filesystems. But since 6.17 kernel, a seed device can only be mounted either through a sprouted filesystem, or the seed device itself, not both at the same time. ๐Ÿ˜ญ This is to ensure a block device to have only a single filesystem bound to it, so that runtime device missing events can be properly handled.

Once the seeding device is mounted, it needs the writable device. After adding it, unmounting and mounting with umount /path; mount /dev/writable /path or remounting read-write with remount -o remount,rw makes the filesystem at /path ready for use. ๐Ÿ’–

Note: There was a known bug with using remount to make the mount writeable: remount will leave the filesystem in a state where it is unable to clean deleted snapshots, so it will leak space until it is unmounted and mounted properly. ๐Ÿ’€ That bug has been fixed in 5.11 and newer kernels. ty king ๐Ÿ™

Furthermore, deleting the seeding device from the filesystem can turn it into a normal filesystem, provided that the writable device can also contain all the data from the seeding device.

The seeding device flag can be cleared again by btrfstune -f -S 0, e.g. allowing to update with newer data โ€” but please note that this will invalidate all existing filesystems that use this particular seeding device. This works for some use cases, not for others, and the forcing flag to the command is mandatory to avoid accidental mistakes. no cap. ๐Ÿšซ

Example how to create and use one seeding device:

# mkfs.btrfs /dev/sda
# mount /dev/sda /mnt/mnt1
... fill mnt1 with data
# umount /mnt/mnt1

# btrfstune -S 1 /dev/sda

# mount /dev/sda /mnt/mnt1
# btrfs device add /dev/sdb /mnt/mnt1
# umount /mnt/mnt1
# mount /dev/sdb /mnt/mnt1
... /mnt/mnt1 is now writable

Now /mnt/mnt1 can be used normally. The device /dev/sda can be mounted again with another writable device:

# mount /dev/sda /mnt/mnt2
# btrfs device add /dev/sdc /mnt/mnt2
# umount /mnt/mnt2
# mount /dev/sdc /mnt/mnt2
... /mnt/mnt2 is now writable

The writable device (/dev/sdb) can be decoupled from the seeding device and used independently:

# btrfs device delete /dev/sda /mnt/mnt1

As the contents originated in the seeding device, it's possible to turn /dev/sdb to a seeding device again and repeat the whole process. ๐Ÿ”

A few things to note:

  • it's recommended to use only single device for the seeding device, it works for multiple devices but the single profile must be used in order to make the seeding device deletion work
  • block group profiles single and dup support the use cases above
  • the label is copied from the seeding device and can be changed by btrfs filesystem label
  • each new mount of the seeding device gets a new random UUID
  • umount /path; mount /dev/writable /path can be replaced with mount -o remount,rw /path but it won't reclaim space of deleted subvolumes until the seeding device is mounted read-write again before making it seeding again ๐Ÿ‘€

๐Ÿงš Chained seeding devices ๐Ÿงš

Though it's not recommended and is rather an obscure and untested use case, chaining seeding devices is possible. In the first example, the writable device /dev/sdb can be turned onto another seeding device again, depending on the unchanged seeding device /dev/sda. Then using /dev/sdb as the primary seeding device it can be extended with another writable device, say /dev/sdd, and it continues as before as a simple tree structure on devices. ๐ŸŒณ

# mkfs.btrfs /dev/sda
# mount /dev/sda /mnt/mnt1
... fill mnt1 with data
# umount /mnt/mnt1

# btrfstune -S 1 /dev/sda

# mount /dev/sda /mnt/mnt1
# btrfs device add /dev/sdb /mnt/mnt1
# mount -o remount,rw /mnt/mnt1
... /mnt/mnt1 is now writable
# umount /mnt/mnt1

# btrfstune -S 1 /dev/sdb

# mount /dev/sdb /mnt/mnt1
# btrfs device add /dev/sdc /mnt
# mount -o remount,rw /mnt/mnt1
... /mnt/mnt1 is now writable
# umount /mnt/mnt1

As a result we have:

  • sda is a single seeding device, with its initial contents
  • sdb is a seeding device but requires sda, the contents are from the time when sdb is made seeding, i.e. contents of sda with any later changes
  • sdc last writable, can be made a seeding one the same way as was sdb, preserving its contents and depending on sda and sdb

As long as the seeding devices are unmodified and available, they can be used to start another branch. ๐ŸŒˆ

okay bestie, we gotta talk ๐Ÿ’€ The RAID56 feature gives you striping and parity across several devices โ€” same energy as the traditional RAID5/6, no cap. But there are some implementation and design deficiencies that make it unreliable for certain corner cases, and the feature should not be used in production โ€” only for evaluation or testing, fr fr. Also the power failure safety for metadata with RAID56 is not 100%, so likeโ€ฆ be aware ๐Ÿซ 


Metadata

๐Ÿšซ Do not use raid5 nor raid6 for metadata. Use raid1 or raid1c3 respectively. that's the rule, bestie.

The substitute profiles give you the same guarantees against loss of 1 or 2 devices, and in some respects can actually be an improvement. Why? Because recovering from one missing device only needs to access the remaining 1st or 2nd copy โ€” and that copy may generally be sitting on some other devices, because that's just how RAID1 works on btrfs. Contrast that with a striped profile (similar to raid0), which would need all devices all the time. it's giving โœจresilienceโœจ

The space allocation pattern and consumption is different too (e.g. on N devices): take raid5 as an example โ€” a 1GiB chunk is reserved on each device, while with raid1 each 1GiB chunk is stored on 2 devices. So the consumption of each 1GiB of used metadata ends up being N * 1GiB vs 2 * 1GiB. And using raid1 is also more convenient for balancing/converting to another profile, thanks to the lower requirement on the available chunk space. slay ๐Ÿ’…


Missing/incomplete support

When RAID56 is on the same filesystem alongside different raid profiles, the space reporting is inaccurate โ€” we're talking df, btrfs filesystem df or btrfs filesystem usage all lying to your face ๐Ÿ˜ญ When there's only a one profile per block group type (e.g. RAID5 for data) the reporting is accurate. so there's that.

When scrub is started on a RAID56 filesystem, it's started on all devices, which degrades the performance. The workaround is to start it on each device separately. And because of that, the device stats may not match the actual state, and some errors might get reported multiple times. messy but true ๐Ÿ™ˆ

The write hole problem ๐Ÿ˜ฑ An unclean shutdown could leave a partially written stripe in a state where some stripe ranges and the parity are from the old writes, and some are new. The information about which is which is not tracked. Write journal is not implemented. Alternatively, a full read-modify-write would make sure that a full stripe is always written, avoiding the write hole completely โ€” but performance in that case turned out to be too bad for use. iconic idea, cursed execution.

The striping happens on all available devices (at the time the chunks were allocated), so if a new device is added it may not be utilized immediately, and it would require a rebalance. A fixed configured stripe width is not implemented. noted ๐Ÿ“


๐ŸŒˆ GLOSSARY ๐ŸŒˆ

Terms in italics also appear in this glossary. iykyk, it's all connected ๐Ÿฆ‹

allocator

Usually allocator means the block allocator โ€” i.e. the logic inside the filesystem that decides where to place newly allocated blocks so it can maintain several constraints (like data locality and low fragmentation).

In btrfs, allocator may also refer to the chunk allocator, i.e. the logic behind placing chunks on devices.

balance

An operation that can be done to a btrfs filesystem, for example through btrfs balance /path. A balance passes all data in the filesystem through the allocator again. It's primarily intended to rebalance the data in the filesystem across the devices when a device is added or removed. A balance will regenerate missing copies for the redundant RAID levels, if a device has failed. As of Linux kernel 3.3, a balance operation can be made selective about which parts of the filesystem are rewritten, using filters #man-balance-filters.

barrier

An instruction to the underlying hardware to ensure that everything before the barrier is physically written to permanent storage before anything after it. Used in btrfs's copy on write approach to ensure filesystem consistency. no cutting the line ๐Ÿ™…โ€โ™€๏ธ

block

A single physically and logically contiguous piece of storage on a device, of size e.g. 4K. In some contexts this is also referred to as sector, though the term block is preferred. okay purist ๐Ÿ’…

block group

The unit of allocation of space in btrfs. A block group is laid out on the disk by the btrfs allocator, and will consist of one or more chunks, each stored on a different device. The number of chunks used in a block group will depend on its RAID level.

B-tree

The fundamental storage data structure used in btrfs. Except for the superblocks, all of btrfs metadata is stored in one of several B-trees on disk. B-trees store key/item pairs. While the same code is used to implement all of the B-trees, there are a few different categories of B-tree. The name btrfs refers to its use of B-trees. ๐Ÿ“š the origin story honestly

btrfsck, fsck, btrfs-check

Tool in btrfs-progs that checks an unmounted filesystem (offline) and reports on any errors in the filesystem structures it finds. By default the tool runs in read-only mode, because fixing errors is potentially dangerous. See also scrub.

btrfs-progs

User mode tools to manage btrfs-specific features. Maintained at http://github.com/kdave/btrfs-progs.git . The main frontend to btrfs features is the standalone tool btrfs, although other tools such as mkfs.btrfs and btrfstune are also part of btrfs-progs.

chunk

A part of a block group. Chunks are either 1 GiB in size (for data) or 256 MiB (for metadata), depending on the overall filesystem size.

chunk tree

A layer that keeps information about mapping between physical and logical block addresses. It's stored within the system group. the map, bestie ๐Ÿ—บ๏ธ

cleaner

Usually referred to in the context of deleted subvolumes. It's a background process that removes the actual data once a subvolume has been deleted. Cleaning can involve lots of IO and CPU activity depending on the fragmentation and amount of shared data with other subvolumes.

The cleaner kernel thread also processes defragmentation triggered by the autodefrag mount option, the removing of empty block groups, and some other finalization tasks. it does it all ๐Ÿงนโœจ

copy-on-write, COW

Also known as COW. The method that btrfs uses for modifying data. Instead of directly overwriting data in place, btrfs takes a copy of the data, alters it, and then writes the modified data back to a different (unused) location on the disk. It then updates the metadata to reflect the new location of the data. In order to update the metadata, the affected metadata blocks are also treated in the same way. In COW filesystems, files tend to fragment as they are modified. Copy-on-write is also used in the implementation of snapshots and reflink copies. A copy-on-write filesystem is, in theory, always consistent โ€” provided the underlying hardware supports barriers. the theory part is doing a lot of heavy lifting there ๐Ÿ˜ญ

default subvolume

The subvolume in a btrfs filesystem which is mounted when mounting the filesystem without using the subvol= mount option.

device

A Linux block device, e.g. a whole disk, partition, LVM logical volume, loopback device, or network block device. A btrfs filesystem can reside on one or more devices.

df

A standard Unix tool for reporting the amount of space used and free in a filesystem. The standard tool does not give accurate results, but the btrfs command from btrfs-progs has an implementation of df which shows space available in more detail. See the [[FAQ#Why_does_df_show_incorrect_free_space_for_my_RAID_volume.3F|FAQ]] for a more detailed explanation of btrfs free space accounting.

DUP

A form of "RAID" which stores two copies of each piece of data on the same device. This is similar to RAID1, and protects against block-level errors on the device, but does not provide any guarantees if the entire device fails. By default, btrfs uses the DUP profile for metadata on a single device filesystem. two copies, one device โ€” the vibe is very "trust me bro" ๐Ÿซถ

ENOSPC

Error code returned by the OS to a user program when the filesystem cannot allocate enough data to fulfill the user request. In most filesystems, it indicates there is no free space available in the filesystem. Due to the additional space requirements from btrfs's COW behaviour, btrfs can sometimes return ENOSPC when there is apparently (in terms of df) a large amount of space free. This is effectively a bug in btrfs, and (if it is repeatable), using the mount option enospc_debug may give a report that will help the btrfs developers. See the [[FAQ#if_your_device_is_large_.28.3E16GiB.29|FAQ entry]] on free space.

extent

Contiguous sequence of bytes on disk that holds file data. It's a compact representation that tracks the start and length of the byte range, so the logic behind allocating blocks (delayed allocation) strives for maximizing the length before writing the extents to the devices. maximizing, always maximizing ๐Ÿ“

extent buffer

An abstraction of a b-tree metadata block storing item keys and item data. The underlying related structures are a physical device block and a CPU memory page.

fallocate

Command line tool in util-linux, and a syscall, that reserves space in the filesystem for a file, without actually writing any file data to the filesystem. The first data write will turn the preallocated extents into regular ones. See fallocate(1) at https://man7.org/linux/man-pages/man1/fallocate.1.html and fallocate(2) at https://man7.org/linux/man-pages/man2/fallocate.2.html manual pages for more details.

filefrag

A tool to show the number of extents in a file, and hence the amount of fragmentation in the file. It is usually part of the e2fsprogs package on most Linux distributions. While initially developed for the ext2 filesystem, it works on Btrfs as well. It uses the FIEMAP ioctl.

free space cache

Also known as "space cache v1". A separate cache tracking free space, as btrfs only tracks the allocated space. The free space is by definition any hole between allocated ranges. Finding the free ranges can be I/O intensive, so the cache stores a condensed representation of it. It is updated every transaction commit.

The v1 free space cache has been superseded by the free space tree. rest in peace legend ๐Ÿซก

free space tree

Successor of the free space cache, also known as "space cache v2" and now default. The free space is tracked in a better way and using COW, unlike the custom mechanism of v1. glow up complete โœจ

  • fsync โ€” okay so on Unix and Unix-like systems (and Linux is the latter, bestie ๐Ÿ’…), the fsync(2) system call makes every buffered data change tied to a file descriptor get flushed down to the underlying block device. On a modern operating system, when you edit a file the changes generally do not go straight to disk โ€” they chill in memory first, for performance reasons. Calling fsync(2) is the "write that in-memory stuff to disk, right now" button. โœจ
  • generation โ€” an internal counter that bumps for every transaction. When a metadata block gets written (using copy on write), the current generation is stored inside the block, so blocks that are too new (and therefore possibly inconsistent) can be spotted. ๐Ÿ‘‘
  • key โ€” a fixed sized tuple used to identify and sort items in a B-tree. It breaks into 3 parts: objectid, type, and offset. The type field tells you how the other two fields should be used, and what you should expect to find in the item. ๐ŸŒธ
  • item โ€” a variable sized structure stored in B-tree leaves. Items hold different types of data depending on the key type.
  • log tree โ€” a b-tree that temporarily tracks ongoing metadata updates until a full transaction commit is done. It's a performance optimization of fsync. The log tracked in the tree gets replayed if the filesystem is not unmounted cleanly. ๐Ÿ˜ญ
  • metadata โ€” data about data, periodt. In btrfs this means all the internal data structures of the filesystem, including directory structures, filenames, file permissions, checksums, and the location of each file's extents. All btrfs metadata lives in B-trees. ๐ŸŒˆ
  • mkfs.btrfs โ€” the tool (from btrfs-progs) that creates a btrfs filesystem.
  • offline โ€” a filesystem that is not mounted is offline. Some tools (e.g. btrfsck) will only work on offline filesystems. Compare: online. ๐Ÿ™ˆ
  • online โ€” a filesystem that is mounted is online. Most btrfs tools will only work on online filesystems. Compare: offline.
  • orphan โ€” a file that's still in use (opened by a running process) but all of its directory entries have been removed. it's giving ghost file fr fr ๐Ÿ’€
  • RAID โ€” a whole class of methods for writing some extra redundant data across multiple devices, so that if one device fails, the missing data can be reconstructed from the remaining ones. See RAID0, RAID1, RAID5, RAID6, RAID10, DUP and single. Traditional RAID methods operate across multiple devices of equal size, whereas btrfs' RAID implementation works inside block groups. ๐Ÿงš
  • RAID0 โ€” a form of RAID that provides no guarantees of error recovery, but stripes a single copy of data across multiple devices for performance purposes. The stripe size is fixed to 64KB for now. (yes, fixed, no you can't change it, sorry bestie ๐Ÿ’…)
  • RAID1, RAID1C3, RAID1C4 โ€” a form of RAID that stores two/three/four complete copies of each piece of data. Each copy lives on a different device. btrfs requires a minimum of two devices to use RAID-1, or three/four respectively. This is the default block group profile for btrfs's metadata on more than one device. ๐Ÿ‘‘
  • RAID5 โ€” a form of RAID that stripes a single copy of data across multiple devices, including one device's worth of additional parity data. Can be used to recover from a single device failure.
  • RAID6 โ€” a form of RAID that stripes a single copy of data across multiple devices, including two device's worth of additional parity data. Can be used to recover from the failure of two devices.
  • RAID10 โ€” a form of RAID that stores two complete copies of each piece of data, and also stripes each copy across multiple devices for performance. the iconique combo tbh โœจ
  • reflink โ€” commonly used as a reference to a shallow copy of file extents that share the extents until the first change. Reflinked files (e.g. by cp) are different files but they point to the same extents โ€” any change gets detected and a new copy of the data is created, keeping the files independent. Related to that is extent range cloning, which works on a range of a file. ๐Ÿฆ‹
  • relocation โ€” the process of moving block groups within the filesystem while maintaining full filesystem integrity and consistency. This functionality underlies the balance and device removing features. ๐Ÿ’…
  • scrub โ€” an online filesystem checking tool. It reads all the data and metadata on the filesystem, verifies checksums, and eventually uses redundant copies from RAID or DUP to repair any corrupt data/metadata. self care honestly ๐ŸŽ€
  • seed device โ€” a readonly device can be used as a filesystem seed or template (e.g. a CD-ROM containing an OS image). Read/write devices can be added to store modifications (using copy on write), and changes to the writable devices are persistent across reboots. The original device remains unchanged and can be removed at any time (after Btrfs has been instructed to copy over all missing blocks). Multiple read/write file systems can be built from the same seed. ๐ŸŒฑ
  • single โ€” a block group profile storing a single copy of each piece of data.
  • snapshot โ€” a subvolume which is a copy on write copy of another subvolume. The two subvolumes share all of their common (unmodified) data, which means snapshots can be used to keep the historical state of a filesystem very cheaply. After the snapshot is made, the original subvolume and the snapshot are of equal status: the original does not "own" the snapshot, and either one can be deleted without affecting the other one. no possession, no drama, only COW ๐Ÿ’–
  • subvolume โ€” a tree of files and directories inside a btrfs that can be mounted as if it were an independent filesystem. A subvolume is created by taking a reference on the root of another subvolume. Each btrfs filesystem has at least one subvolume, the top-level subvolume, which contains everything else in the filesystem. Additional subvolumes can be created and deleted with the btrfs tool. All subvolumes share the same pool of free space in the filesystem. See also default subvolume. ๐Ÿ‘‘
  • super block โ€” a special metadata block that is a main access point of the filesystem structures. Its size is fixed, and there are fixed locations on the devices used for detecting and opening the filesystem. Updating the superblock defines one transaction. The super blocks contain filesystem identification (UUID), checksum type, block pointers to fundamental trees, features and creation parameters. โญ
  • system array โ€” a technical term for super block metadata describing how to assemble a filesystem from multiple devices, storing information about chunks and devices that are required to be scanned/registered at the time the mount happens. Scanning is done by the command btrfs device scan; alternatively all the required devices can be specified by the mount option device=/path.
  • top-level subvolume โ€” the subvolume at the very top of the filesystem. This is the only subvolume present in a newly-created btrfs filesystem, and internally it has ID 5, otherwise it could be referenced as 0 (e.g. within the set-default subcommand of btrfs). main character subvolume energy, literally ๐Ÿ˜Œ
  • transaction โ€” a consistent set of changes. To avoid generating very large amounts of disk activity, btrfs caches changes in RAM for up to 30 seconds (sometimes more often if the filesystem is running short on space or doing a lot of fsyncs), and then writes (commits) these changes out to disk in one go (using copy on write behaviour). This period of caching is called a transaction. Only one transaction is active on the filesystem at any one time. ๐Ÿซถ
  • transid โ€” an alternative term for generation.
  • writeback โ€” writeback in the context of the Linux kernel can be defined as the process of writing "dirty" memory from the page cache to the disk, when certain conditions are met (timeout, number of dirty pages over a ratio). ๐Ÿงš

๐Ÿ’พ STORAGE MODEL, HARDWARE CONSIDERATIONS ๐Ÿ’พ

Storage model

A storage model is a model that captures key physical aspects of data structure in a data store. A filesystem is the logical structure organizing data on top of the storage device. like, the device is the closet and the filesystem is how you organise the clothes in it ๐ŸŽ€

The filesystem assumes several features or limitations of the storage device and utilizes them, or applies measures to guarantee reliability. BTRFS in particular is based on a COW (copy on write) mode of writing, i.e. it does not update data in place โ€” instead it writes a new copy to a different location and then atomically switches the pointers. no editing in place bestie, we make a whole new version and swap the pointer ๐Ÿ’…

In an ideal world, the device does what it promises. But the filesystem assumes that this may not be true, so additional mechanisms are applied to either detect misbehaving hardware or get valid data by other means. The devices may (and do) apply their own detection and repair mechanisms โ€” but we won't assume any of that. ๐Ÿ™ˆ

The following assumptions about storage devices are considered (sorted by importance, numbers are for further reference):

  1. atomicity of reads and writes of blocks/sectors (the smallest unit of data the device presents to the upper layers)
  2. there's a flush command that instructs the device to forcibly order writes before and after the command; alternatively there's a barrier command that facilitates the ordering but may not flush the data
  3. data sent to write to a given device offset will be written without further changes to the data and to the offset
  4. writes can be reordered by the device, unless explicitly serialized by the flush command
  5. reads and writes can be freely reordered and interleaved

The consistency model of BTRFS builds on these assumptions. The logical data updates are grouped into a generation, written on the device, serialized by the flush command, and then the super block is written, ending the generation. All logical links among metadata comprising a consistent view of the data may not cross the generation boundary. ๐Ÿ‘‘

When things go wrong

No or partial atomicity of block reads/writes (1)

  • Problem: a partial block contents is written (torn write), e.g. due to a power glitch or other electronics failure during the read/write ๐Ÿ˜ญ
  • Detection: checksum mismatch on read ๐Ÿ”
  • Repair: use another copy or rebuild from multiple blocks using some encoding scheme ๐Ÿ› ๏ธ

The flush command does not flush (2)

okay so this one is literally the most serious problem in this whole list, and no, the filesystem can't just mitigate it โ€” not without limitations and design restrictions, and even then it's cooked. ๐Ÿ’€ what could happen in the worst case is that writes from one generation bleed into another one, while the filesystem is still out here fully convinced those generations are isolated. crash at literally any point and you're left with data on the device in an inconsistent state, with zero hint about what exactly got written, what is missing, and you end up with stale metadata link information. ๐Ÿซ 

Devices usually honor the flush command, but for performance reasons they may do internal caching, where the flushed data are not yet persistently stored. a power failure in that window could lead to a similar scenario as above, although it's less likely that later writes would be written before the cached ones. and this is fully beyond what a filesystem can take into account. ๐Ÿ˜ญ Devices or controllers are usually equipped with batteries or capacitors to write the cache contents even after power is cut โ€” that's a (Battery backed write cache) bestie, it's giving backup energy.

Data get silently changed on write (3)

Such thing should not happen frequently, but still can happen spuriously due the complex internal workings of devices or physical effects of the storage media itself. ๐ŸŒช๏ธ

  • Problem: while the data are written atomically, the contents get changed
  • Detection: checksum mismatch on read
  • Repair: use another copy or rebuild from multiple blocks using some encoding scheme

Data get silently written to another offset (3)

this would be another serious problem because the filesystem has no information when it happens โ€” so the measures have to be done ahead of time, no waiting around. this problem is also commonly called ghost write ๐Ÿ‘ป.

The metadata blocks have the checksum embedded in the blocks, so a correct atomic write would not corrupt the checksum. it's likely that after reading such block the data inside would not be consistent with the rest. to rule that out there's an embedded block number in the metadata block. it's the logical block number, because this is what the logical structure expects and verifies. periodt. ๐ŸŽฏ

The following is based on information publicly available, user feedback, community discussions or bug report analyses. It's not complete and further research is encouraged when in doubt.

๐Ÿง  Main memory

The data structures and raw data blocks are temporarily stored in computer memory before they get written to the device. It is critical that memory is reliable because even simple bit flips can have vast consequences and lead to damaged structures, not only in the filesystem but in the whole operating system. ๐Ÿ˜ตโ€๐Ÿ’ซ

Based on experience in the community, memory bit flips are more common than one would think. when it happens, it's reported by the tree-checker or by a checksum mismatch after reading blocks. there are some very obvious instances of bit flips that happen, e.g. in an ordered sequence of keys in metadata blocks. we can easily infer from the other data what values get damaged and how. however, fixing that is not straightforward and would require cross-referencing data from the entire filesystem to see the scope. ๐Ÿ˜ฎโ€๐Ÿ’จ

If available, ECC memory should lower the chances of bit flips, but this type of memory is not available in all cases. a memory test should be performed in case there's a visible bit flip pattern, though this may not detect a faulty memory module because the actual load of the system could be the factor making the problems appear. in recent years attacks on how the memory modules operate have been demonstrated (rowhammer) achieving specific bits to be flipped. while these were targeted, this shows that a series of reads or writes can affect unrelated parts of memory. ๐Ÿ™ˆ

Block group profiles with redundancy (like RAID1) will not protect against memory errors as the blocks are first stored in memory before they are written to the devices from the same source.

A filesystem mounted read-only will not affect the underlying block device in almost 100% (with highly unlikely exceptions). the exception is a tree-log that needs to be replayed during mount (and before the read-only mount takes place), working memory is needed for that and that can be affected by bit flips. there's a theoretical case where a bit flip changes the filesystem status from read-only to read-write. ๐Ÿ˜ณ

Further reading:

What to do:

  • run memtest, note that sometimes memory errors happen only when the system is under heavy load that the default memtest cannot trigger
  • memory errors may appear as filesystem going read-only due to "pre write" check, that verify meta data before they get written but fail some basic consistency checks
  • newly built systems should be tested before being put to production use, ideally start a IO/CPU load that will be run on such system later; namely systems that will utilize overclocking or special performance features

๐Ÿ”Œ Direct memory access (DMA)

Another class of errors is related to DMA (direct memory access) performed by device drivers. while this could be considered a software error, the data transfers that happen without CPU assistance may accidentally corrupt other pages. Storage devices utilize DMA for performance reasons, the filesystem structures and data pages are passed back and forth, making errors possible in case page life time is not properly tracked. ๐Ÿ‘€

There are lots of quirks (device-specific workarounds) in Linux kernel drivers (regarding not only DMA) that are added when found. the quirks may avoid specific errors or disable some features to avoid worse problems. it's giving "we patched it in the driver and moved on" energy. ๐Ÿ’…

What to do:

  • use up-to-date kernel (recent releases or maintained long term support versions)
  • as this may be caused by faulty drivers, keep the systems up-to-date

๐Ÿ’ฝ Rotational disks (HDD)

Rotational HDDs typically fail at the level of individual sectors or small clusters. read failures are caught on the levels below the filesystem and are returned to the user as EIO - Input/output error. Reading the blocks repeatedly may return the data eventually, but this is better done by specialized tools and filesystem takes the result of the lower layers. rewriting the sectors may trigger internal remapping but this inevitably leads to data loss. ๐Ÿ˜ญ

Disk firmware is technically software but from the filesystem perspective is part of the hardware. IO requests are processed, and caching or various other optimizations are performed, which may lead to bugs under high load or unexpected physical conditions or unsupported use cases. ๐Ÿซ 

Disks are connected by cables with two ends, both of which can cause problems when not attached properly. Data transfers are protected by checksums and the lower layers try hard to transfer the data correctly or not at all. the errors from badly-connecting cables may manifest as large amount of failed read or write requests, or as short error bursts depending on physical conditions. ๐Ÿชข

What to do:

  • check smartctl for potential issues

๐Ÿฆ‹ Solid state drives (SSD)

The mechanism of information storage is different from HDDs and this affects the failure mode as well. The data are stored in cells grouped in large blocks with limited number of resets and other write constraints. The firmware tries to avoid unnecessary resets and performs optimizations to maximize the storage media lifetime. the known techniques are deduplication (blocks with same fingerprint/hash are mapped to same physical block), compression or internal remapping and garbage collection of used memory cells. due to the additional processing there are measures to verify the data e.g. by ECC codes. โœจ

The observations of failing SSDs show that the whole electronic fails at once or affects a lot of data (e.g. stored on one chip). Recovering such data may need specialized equipment and reading data repeatedly does not help as it's possible with HDDs. ๐Ÿ’€

There are several technologies of the memory cells with different characteristics and price. The lifetime is directly affected by the type and frequency of data written. Writing "too much" distinct data (e.g. encrypted) may render the internal deduplication ineffective and lead to a lot of rewrites and increased wear of the memory cells. ๐Ÿ˜ฌ

There are several technologies and manufacturers so it's hard to describe them but there are some that exhibit similar behaviour:

  • expensive SSD will use more durable memory cells and is optimized for reliability and high load ๐Ÿ‘‘
  • cheap SSD is projected for a lower load ("desktop user") and is optimized for cost, it may employ the optimizations and/or extended error reporting partially or not at all

It's not possible to reliably determine the expected lifetime of an SSD due to lack of information about how it works or due to lack of reliable stats provided by the device. so like, don't ask us for a number, we don't have one. ๐Ÿ™ƒ

Metadata writes tend to be the biggest component of lifetime writes to a SSD, so there is some value in reducing them. Depending on the device class (high end/low end) the features like DUP block group profiles may affect the reliability in both ways:

  • high end are typically more reliable and using single for data and metadata could be suitable to reduce device wear
  • low end could lack ability to identify errors so an additional redundancy at the filesystem level (checksums, DUP) could help

Only users who consume 50 to 100% of the SSD's actual lifetime writes need to be concerned by the write amplification of btrfs DUP metadata. Most users will be far below 50% of the actual lifetime, or will write the drive to death and discover how many writes 100% of the actual lifetime was. SSD firmware often adds its own write multipliers that can be arbitrary and unpredictable and dependent on application behavior, and these will typically have far greater effect on SSD lifespan than DUP metadata. It's more or less impossible to predict when a SSD will run out of lifetime writes to within a factor of two, so it's hard to justify wear reduction as a benefit. ๐Ÿ“‰

Further reading:

What to do:

  • run smartctl or self-tests to look for potential issues
  • keep the firmware up-to-date

๐Ÿ’พ NVM express, non-volatile memory (NVMe)

Okay so NVMe bestie โ€” it's a type of persistent memory that's usually hooked up over a system bus (PCIe) or some similar interface, and the speeds?? Literally an order of magnitude faster than SSD. ๐Ÿ”ฅ It's also a non-rotating type of storage, and it's not typically connected by a cable โ€” so no lil wire vibes. And it's not a SCSI type device either, it's more like a whole complete specification for a logical device interface, periodt.

In a way, the way it errors out is lowkey comparable to a combo of SSD class and regular memory. Errors may show up as random bit flips or IO failures. ๐Ÿ’€ Good news: there's tools to get at the internal log (<code>nvme log</code> and <code>nvme-cli</code>) for a way more detailed analysis.

There's also separate error detection and correction steps happening, like, at the bus level, and in most cases it never even makes it up to the filesystem level. Once it does make it there, that could mean there's some systematic error going on โ€” like overheating or a bad physical connection of the device. You may want to run self-tests (using <code>smartctl</code>) at that point tbh. ๐Ÿซถ


๐Ÿ”ง Drive firmware

Firmware is technically still software, it's just embedded into the hardware. And bestie, as all software has bugs, so does firmware โ€” no exceptions, sorry. ๐Ÿ’…

Storage devices can update their firmware and fix known bugs. And in some cases it's possible to avoid certain bugs by quirks (aka device-specific workarounds) in the Linux kernel.

A faulty firmware can cause a wide range of corruptions, from small and localized all the way up to large ones affecting lots and lots of data. And self-repair capabilities may not be sufficient. ๐Ÿ˜ญ

What to do:

  • check for firmware updates in case there are known problems โ€” but ngl, note that updating firmware can be risky on itself
  • use an up-to-date kernel (recent releases or maintained long term support versions)

๐ŸŽ€ SD flash cards

There are a lot of devices with low power consumption, and so they use storage media based on low power consumption too โ€” typically flash memory stored on a chip enclosed in a detachable card package. Sneaky lil guy.

A card that's improperly inserted may get damaged by electrical spikes when the device is turned on or off. And the chips storing the data in turn may be damaged permanently. ๐Ÿ˜ญ

All types of flash memory have a limited number of rewrites, so the data gets internally translated by FTL (flash translation layer). That's implemented in firmware (technically a software btw) and it's prone to bugs that manifest as hardware errors. It's giving chaotic.

Adding redundancy like using DUP profiles for both data and metadata can help in some cases โ€” but once problems appear, a full backup might honestly be the best option, and replacing the card could be required as well. ๐Ÿซถ


๐Ÿ’€ Hardware as the main source of filesystem corruptions

If you use unreliable hardware and don't know about that, don't blame the filesystem when it tells you. That's it, that's the whole vibe. The filesystem is not the problem bestie, it's the snitch. ๐Ÿ™ˆ


๐Ÿ‘€ SEE ALSO


okay that's the whole page bestie, we made it to the end and I'm obsessed ๐Ÿ˜ญ๐Ÿ’– this was BTRFS(5) from page source version 7.3, dated Oct 24, 2026, and it's been a whole journey. go forth and mount responsibly, queen ๐Ÿ‘‘โœจ

๐Ÿ“ฆ where this page comes from