btrfs-progs: misc-tests: add a test case for btrfs-restore
The new test case will:
- Create an inlined compressed file extent
- Generate checksum for that file
- Use btrfs-restore to extract that file
- Compare the extracted file with the generated checksum
The checksum should match.
This is a regression test for a behavior change in kernel, which results
older btrfs-restore unable to handle newerly created inlined compressed
file extents.
btrfs-progs: restore: handle full block compressed inline extent
Since kernel commit 3eaf5f082c4c ("btrfs: extract inlined creation into a
dedicated delalloc helper"), the creation of compressed inlined extent
has changed to compress the whole block, then truncate the ram_bytes to
isize.
This means the compressed data needs to have a full block as the output
buffer.
This is fine for kernel, as the inlined extent is alwasy decompressed to
a folio which is at least one block sized.
But for progs, if the output buffer is smaller than a block, the
decompression will fail early (not enough output buffer), causing
btrfs-restore to completely skip the content of the inlined extent.
Fix it by always allocating a full block for copy_one_inline().
As "-O mixed-bg" option didn't properly set @mixed variable, thus
regular non-mixed block groups are still created.
Fix it by setting @mixed if BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS feature
is also set.
Signed-off-by: Andrus Suvalau <andrus.suvalau@n-able.com> Reviewed-by: Qu Wenruo <wqu@suse.com>
[ Update the commit message to show the problem more clearly ] Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Andrus Suvalau <andrus.suvalau@n-able.com>
btrfs-progs: receive: fix unused variable warning without LZO
When btrfs-progs is built without LZO support, sector_shift is still
declared in decompress_and_write(), while all its uses are guarded by
COMPRESSION_LZO. This results in an unused variable warning.
Move the sector_shift calculation into the only user, which is already
under the "#if COMPRESSION_LZO" branch.
Fixes: 345731fff32d ("btrfs-progs: receive: optional build for lzo, zstd") Signed-off-by: liuh0329 <liuhuan01@kylinos.cn> Reviewed-by: Qu Wenruo <wqu@suse.com>
[ Remove the sector_shift declaration completely, as older toolchains
have older C-standard, which doesn't allow declaring a variable
inside a case branch. ] Signed-off-by: Qu Wenruo <wqu@suse.com>
btrfs-progs: check: detect leaked active zones on zoned devices
On a zoned filesystem a sequential zone can end up open (and written) on
the device while it is not referenced by any dev extent and is not in a
sb-log zone. Such a zone is unreferenced - its space is free according to
the device tree - yet it stays active on the device, permanently consuming
one of the device's limited active zones. On a device with few active
zones this can lead to premature ENOSPC and/or allocation hangs.
One way to end up in this state is a mkfs that failed to reset a
temporary chunk's zone before finishing (fixed in a preceding commit).
Existing filesystems created by such a mkfs need a way to detect and
repair the condition.
Add a zoned device specific check that cross-checks the on-disk metadata
against the physical zone state of the device. It flags any written,
sequential zone that is not backed by a dev extent, and in
--repair mode resets it. The check is a no-op on non-zoned filesystems and
runs as part of the extent/device checking rather than as a separate
numbered check phase.
Introduce check_zoned() as a entry point for future zoned device checks.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: David Sterba <dsterba@suse.com>
btrfs-progs: zoned: refresh stale cached zone state in btrfs_reset_dev_zone()
btrfs-progs reads the device zone layout once, in btrfs_get_zone_info() ->
report_zones() when the device is opened, and caches it in 'zinfo->zones[]'
without ever refreshing it afterwards. btrfs_reset_dev_zone() skips
resetting a zone whose cached condition is EMPTY, but that condition can be
stale: a zone written after open, for example a temporary chunk that mkfs
writes and then removes, can still be cached as EMPTY. Its reset is then
skipped and the zone is left open on the device, referenced by no chunk nor
dev extent, permanently consuming one of the device's limited active zones.
On a device with few active zones this results in premature ENOSPC or an
allocation hang.
Before skipping the reset of a supposedly empty zone, re-read its real
condition from the device and only skip if it is really empty.
Fixes: bfdb3ae23713 ("btrfs-progs: zoned: reset zone of freed block group") Reported-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: David Sterba <dsterba@suse.com>
Tom Hale [Wed, 26 Aug 2026 00:06:06 +0000 (07:06 +0700)]
btrfs-progs: sb-mod: use per-type digest size for superblock csum
The global csum_size was hardcoded to the CRC32C digest size (4),
while the actual checksum type is read from each superblock. For
xxhash64 (8), sha256 (32) and blake2b (32), check_csum_superblock()
compared only the first 4 bytes and update_block_csum() wrote a
truncated digest into the zeroed csum field, producing superblocks
that dump-super reports as [DON'T MATCH].
Replace both uses with btrfs_csum_type_size(csum_type) and drop the
now-unused global.
Verified against an xxhash64 filesystem: before the fix dump-super
shows csum 0x7cbc602b00000000 [DON'T MATCH], after the fix it shows
0x7cbc602bf714f3a8 [match]; the kernel accepts the repaired
superblock. See #1160.
Pull-request: #1161 Signed-off-by: Tom Hale <tom@hale.ee> Signed-off-by: David Sterba <dsterba@suse.com>
David Sterba [Tue, 8 Sep 2026 20:03:19 +0000 (22:03 +0200)]
btrfs-progs: sb-mod: add checksum type validation
Check that value stored in super block checksum type is supported
before. As we allow to set csum_type it is possible to force an invalid
value, as does the fuzz/010 test case.
Preparatory work for pull request #1161 which removes the hardcoded
CRC32C checksum type.
David Sterba [Tue, 8 Sep 2026 20:22:54 +0000 (22:22 +0200)]
btrfs-progs: tests: remove csum_type from fuzz/010 case
As the checksum is the used for primary verification, fuzzing it with
the whole set of operations will eventually hit an invalid value and
prevent updating the superblock.
The only valid case is to set it to a different supported value, this
will recalculate the checksum and set it for the whole filesystem, but
all metadata will mismatch. Related issue #1160.
Wehrwolfmann [Sat, 22 Aug 2026 07:43:54 +0000 (09:43 +0200)]
libbtrfsutil: don't leave subvolume iterator pointing at unfilled search buffer
subvolume_iterator_next_tree_search() sets nr_items to 4096 before
BTRFS_IOC_TREE_SEARCH and returns BTRFS_UTIL_ERROR_SEARCH_FAILED without
restoring it when the ioctl fails. items_pos and buf_off stay at 0, so the
next call to btrfs_util_subvolume_iterator_next() takes the 'items_pos <
nr_items' branch and parses btrfs_ioctl_search_header structures out of a
buffer the failed ioctl never filled.
Each phantom item advances buf_off by the header size plus the length read
from the uninitialized memory, so the walk quickly leaves the 3992 byte
buffer; with 4096 items it ends about 128 KB past its start. Depending on
what follows the buffer this either segfaults or spins on an unbounded
number of bogus items.
An unprivileged caller reaches this easily: use_tree_search is true whenever
top != 0, so BTRFS_UTIL_SUBVOLUME_ITERATOR_* users that pass a subvolume id
take the TREE_SEARCH path, the iterator is created successfully, and the
first ioctl then fails with EPERM.
Reset the entry on the error path so the search buffer is never parsed
before an ioctl has filled it.
Pull-request: #1159
Issue: #1158 Assisted-by: LLM Signed-off-by: David Sterba <dsterba@suse.com>
Qu Wenruo [Wed, 12 Aug 2026 00:53:17 +0000 (10:23 +0930)]
btrfs-progs: docs: add a note about RW degraded mount
When a new chunk is allocated during a RW degraded mount, btrfs can
allocate new chunks with other profiles, like SINGLE, if the current
devices can no longer support the original profile.
This can cause unexpected chunk profiles, and the end user is
recommended to balance them out if they want to maintain the current
allocation profile.
Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
Lowmem mode will detect the error like the following:
[5/8] checking fs roots
ERROR: root 5 ino 257 should not have file extent
ERROR: root 5 INODE[257] nbytes 8192 not equal to extent_size 0
ERROR: errors found in fs roots
found 172032 bytes used, error(s) found
For the original mode, it's less strict than the lowmem mode, and it
only rejects the obvious cases, without the extra verification on
inlined extent for symlinks:
[5/8] checking fs roots
root 5 inode 257 errors 40, bad file extent
ERROR: errors found in fs roots
found 172032 bytes used, error(s) found
Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
koraynilay [Thu, 20 Aug 2026 22:08:30 +0000 (00:08 +0200)]
btrfs-progs: set creation/change time (otime/ctime) of offline-created subvolumes
Update common/root-tree-utils.c's btrfs_make_subvolume() to also
generate a UUID and set otime and ctime of subvolumes created offline
(if it's a normal subvolume).
This is essentially the same change as commit ("btrfs-progs: convert:
set creation/change time (otime/ctime) of root subvol") but that applies to:
* mkfs.btrfs' --rootdir and --subvol options
* btrfs-convert's ext2_saved subvolume
koraynilay [Thu, 20 Aug 2026 17:33:44 +0000 (19:33 +0200)]
btrfs-progs: convert: set creation/change time (otime/ctime) of root subvol
Before this, btrfs-convert would leave the root subvol of the converted
fs with 0 as both otime and ctime, showing "-" when using
btrfs subvolume show /path.
This patch uses the code from mkfs.btrfs's btrfs_create_tree_root() to
set the otime/ctime to the root subvol and generate its UUID right away
(instead of having to wait for btrfs_rebuild_uuid_tree(), not
necessarily needed but cleaner).
This way the timestamp of the filesystem conversion will be available in
the "Creation time" field of btrfs subvolume show /path output, while
the original ext* "Filesystem created" timestamp is available with stat
as the mountpoint's birth time.
koraynilay [Thu, 20 Aug 2026 16:50:01 +0000 (18:50 +0200)]
btrfs-progs: subvolume show: fix "Creation time" value being shown for "Send time" too
The send time would be printed before updating tstr, which is previously used
to store the string for the subvol creation time, thus showing an incorrect
send time.
Signed-off-by: koraynilay <koray.fra@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
David Sterba [Tue, 18 Aug 2026 08:55:34 +0000 (10:55 +0200)]
btrfs-progs: docs: drop stable 5.4 heading from feature overview
The stable version headings are anchors for currently active stable
versions, 5.4 is not anymore. As the entries are short it's easy to look
up a particular version if needed.
Qu Wenruo [Mon, 3 Aug 2026 00:35:22 +0000 (10:05 +0930)]
btrfs-progs: docs: update the stability on btrfs-quota
The core design of btrfs qgroup mode is to track the ownership of every
extent accurately, but that is conflicting with snapshots, which can
change the ownership of tons of extents in a very short time, during
snapshot creation and deletion.
Although qgroup mode has several workarounds, it is not fully resolved
and still introduces extra costs, e.g. requires frequent rescan,
breaking limits etc.
So make it more clear that qgroup mode is not recommended until the end
user has a rigid subvolume layout, e.g. no more new subvolume/snapshots,
nor deletion of any existing subvolume.
[CAUSE]
The error mirror bitmap uses bit 0 for mirror 1, as documented in struct
corrupted_block, and add_corrupted_block() does that conversion when it
allocates a new entry:
set_bit(mirror - 1, last->error_mirror_bitmap);
But when an entry for the same logical bytenr already exists, the mirror
number is used as the bit number directly:
set_bit(mirror, last->error_mirror_bitmap);
The first corrupted mirror of a block always goes through the allocation
path, so only the second and any following one are affected, and their bit
ends up one position too high.
For the last mirror this means bit @num_mirrors gets set, which is beyond
the range report_corrupted_blocks() walks, so the mirror is dropped from
the report. For the other mirrors the number reported is one larger than
the corrupted one, e.g. on a RAID1C3 block with mirrors 1 and 2 corrupted
the report claims mirrors 1 and 3, marking the only intact mirror as bad.
This is more than a cosmetic problem, as the report is what the user bases
the mirror choice on in the interactive mode. Picking a mirror that is
reported as intact but is in fact corrupted updates the checksum item to
match the corrupted data, making the corruption permanent.
[FIX]
Convert the mirror number to a bit number the same way the allocation path
does.
Fixes: 0e999c9bd3e7 ("btrfs-progs: introduce "btrfs rescue fix-data-checksum"") Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Sidong Yang <sidong.yang@furiosa.ai> Signed-off-by: David Sterba <dsterba@suse.com>
Sidong Yang [Tue, 4 Aug 2026 12:36:17 +0000 (12:36 +0000)]
btrfs-progs: fix-data-checksum: fix the size of the error mirror bitmap
[BUG]
Running "btrfs rescue fix-data-checksum" on a filesystem with a data
checksum mismatch triggers a heap buffer overflow:
==3692==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000190
READ of size 8 at 0x502000000190 thread T0
#0 set_bit kernel-lib/bitops.h:32
#1 add_corrupted_block cmds/rescue-fix-data-checksum.c:99
#2 verify_one_data_block cmds/rescue-fix-data-checksum.c:143
#3 iterate_one_csum_item cmds/rescue-fix-data-checksum.c:178
#4 iterate_csum_root cmds/rescue-fix-data-checksum.c:252
#5 btrfs_recover_fix_data_checksum cmds/rescue-fix-data-checksum.c:510
0x502000000191 is located 0 bytes after 1-byte region [0x502000000190,0x502000000191)
allocated by thread T0 here:
#0 calloc
#1 add_corrupted_block cmds/rescue-fix-data-checksum.c:94
[CAUSE]
BITS_TO_LONGS() returns the number of unsigned longs needed to hold the
given amount of bits, not the number of bytes. But the value is passed as
the byte size to calloc():
For any sane number of mirrors this allocates a single byte, while
set_bit() accesses the bitmap in unsigned long units, thus reading and
writing 8 bytes out of a 1 byte allocation.
[FIX]
Use bitmap_zalloc() from kernel-lib/bitmap.h, which does the size
calculation for the caller, and pair it with bitmap_free() when the
record is released.
Fixes: 0e999c9bd3e7 ("btrfs-progs: introduce "btrfs rescue fix-data-checksum"") Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Sidong Yang <sidong.yang@furiosa.ai> Signed-off-by: David Sterba <dsterba@suse.com>
Qu Wenruo [Thu, 6 Aug 2026 09:13:09 +0000 (18:43 +0930)]
btrfs-progs: remove unnecessary forced type casting in print-tree.c
In kernel-shared/print-tree.c, we have a lot of forced type casting to
"(unsigned long long)" for the format "%llu", no matter if the original
value is really u64.
Change all those forced type casting used in printing functions, to use
the correct native type instead.
Assisted-by: LLM (coding) Signed-off-by: Qu Wenruo <wqu@suse.com>
[ reformat short lines ] Signed-off-by: David Sterba <dsterba@suse.com>
David Sterba [Thu, 23 Jul 2026 10:31:49 +0000 (12:31 +0200)]
btrfs-progs: drop type casts from set/get_label calls
The helpers set_label() and get_label() take const parameters and the
arguments are also const so it's not necessary to cast them, probably
some leftover from the past.
David Sterba [Thu, 23 Jul 2026 10:11:23 +0000 (12:11 +0200)]
btrfs-progs: receive: handle writes of 0 bytes
If pwrite() writes 0 bytes for some reason the loop will become
infinite. While this is unlikely to happen, this is a condition hard to
detect so we better handle it.
David Sterba [Thu, 23 Jul 2026 08:38:06 +0000 (10:38 +0200)]
btrfs-progs: use full size path buffer in cmd_subvolume_delete()
Commit d544db2078fd43 ("btrfs-progs: unify naming of return values in
cmd_subvolume_create()") converted the unchecked sprintf to path API but
the full_subvolpath is actually shorter than PATH_MAX. This could lead
to stack overflow for a really long path.
That option is already marked deprecated a long time ago, and is going to
be completely removed in v7.3.
Update the test case to auto-detect if the running kernel has support
for "rescue=usebackuproot", and if not skip the test case.
Furthermore, since v7.3 all "rescue=" mount options, including
"rescue=usebackuproot" will require full RO mount, also add "ro" mount
option to the test run.
btrfs-progs: docs: mark "usebackuproot" mount option as removed
Since the kernel patch is going to land in v7.3, update the man page to
reflect that change.
Also, just in case end users are using the latest progs but on a much
older kernel, keep the "usebackuproot" mount option entry, but
explicitly mark it as "removed", along with the history of deprecation.
Reviewed-by: Johannes Thumshirn <joahnnes.thumshirn@wdc.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
Qu Wenruo [Tue, 30 Jun 2026 23:50:39 +0000 (09:20 +0930)]
btrfs-progs: update the direct IO behavior notes for version 7.3
Since v7.3 will use IOMAP_DIO_BOUNCE flag to address the performance
drop of falling back to buffer IO, update the direct IO behavior notes
to avoid confusion.
Qu Wenruo [Sat, 27 Jun 2026 04:30:49 +0000 (14:00 +0930)]
btrfs-progs: sync BTRFS_FEATURE_INCOMPAT_SUPP_STABLE from the kernel
Unlike kernel, btrfs-progs was using explicit definitions for
experimental and regular builds.
This makes maintaining them much harder, since there are already 14
different features just for non-experimental builds.
Simplify the definition by pulling BTRFS_FEATURE_INCOMPAT_SUPP_STABLE
from kernel, and only add new features into experimental builds based on
that BTRFS_FEATURE_INCOMPAT_SUPP_STABLE macro.
This also addresses a small feature mismatch, the feature
raid-stripe-tree is still experimental in the kernel, and all involved
feature flags are also only defined for experimental progs builds, but
it's still defined as supported feature.
Now progs will share the same feature as kernels, for both experimental
and regular builds.
David Sterba [Mon, 20 Jul 2026 10:01:23 +0000 (12:01 +0200)]
btrfs-progs: tests: fix simple shellcheck warnings in test cases
- add quotes where missing (variables, arrays, ...)
- use $(...) instead of `....`
- use {..} ranges instead of seq or ((...))
- add command prerequisites
- minor style fixups
David Sterba [Mon, 20 Jul 2026 06:21:06 +0000 (08:21 +0200)]
btrfs-progs: tests: fix simple shellcheck warnings in test driver scripts
- use quoting for variables, not avoid confusion with commands
- use $(...) instead of `...`
- more quoting of intermediate commands
- save return values so it's not overwritten by 'if'
btrfs-progs: zoned: open host-managed devices with O_DIRECT for read-only
On host-managed zoned block devices the page cache cannot be used for
writing to the device because it does not guarantee ordering, therefore
direct I/O is used to write any on-disk data structures.
But reading in btrfs-progs is still performed using buffered I/O. This
mix of direct and buffered I/O can lead to stale page cache entries
being handed off to btrfs-progs and tools like "btrfs check" can report
spurious errors like:
checksum verify failed on <bytenr> wanted 0x00000000 found 0xb6bde3e4
even though both copies are intact on disk.
Drop the O_RDWR condition so host-managed zoned devices always use
O_DIRECT, matching btrfstune.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Reviewed-by: Qu Wenruo <wqu@suse.com>
Dongjiang Zhu [Mon, 29 Jun 2026 12:28:52 +0000 (20:28 +0800)]
btrfs-progs: check: add option to skip qgroup accounting verification
Qgroup accounting verification can be expensive because it walks extents
and backrefs to recompute referenced/exclusive numbers.
Add --skip-qgroup-accounting so btrfs check can still load qgroup
status/info/relation items from the quota tree without verifying the
accounting numbers.
Reject the option with --repair and --qgroup-report, as both depend on
full accounting verification results.
Dara Adib [Mon, 6 Jul 2026 05:14:39 +0000 (01:14 -0400)]
btrfs-progs: mkfs: dedupe hardlinks when estimating rootdir size
Previously, ftw_add_entry_size() counted every hard link to a file as
separate data, greatly overestimating the required device size for
directories with many hard links to the same inode.
Track visited inodes of non-empty regular files during the size
estimation walk, reusing the existing hardlink_root later used during
the mkfs fill stage. Hardlinks still count towards metadata usage.
Fixes: #1141 Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Dara Adib <daradib@ocf.berkeley.edu>
Sun YangKai [Fri, 26 Jun 2026 02:20:56 +0000 (10:20 +0800)]
btrfs-progs: extend OPEN_CTREE_PARTIAL to skip more non-essential trees
Extend the existing OPEN_CTREE_PARTIAL handling so that tools like
restore can open damaged filesystems where non-essential trees are
corrupted but not needed.
Previously PARTIAL only covered a few trees. Now the following
failures are also non-fatal when PARTIAL is set:
- load_global_roots_objectid: btrfs_search_slot failure on tree_root
when finding global root (extent/csum/free-space). Emits a warning
and creates synthetic empty roots.
- block group tree (BLOCK_GROUP_TREE_OBJECTID) loading failure.
Emits a warning, frees block_group_root, and sets to NULL.
maybe_load_block_groups returns false when root is NULL, so
btrfs_read_block_groups is skipped and the in-memory cache stays
empty.
- btrfs_read_block_groups non-ENOENT errors. Emits a warning and
continues without populating the block group cache.
- dev_root (BTRFS_DEV_TREE_OBJECTID) loading failure. Emits a
printk (pre-existing), frees dev_root, and sets to NULL.
Supporting changes:
- btrfs_read_fs_root returns ERR_PTR(-ENOENT) instead of a raw
NULL pointer for missing dev_root (and block_group_root, which
was already guarded).
- backup_super_roots guards dev_root dereference with a NULL check.
- Remove a stale TODO comment in disk-io.h about splitting PARTIAL
into per-tree flags.
- Unify all PARTIAL warning messages to the same format:
"<description>, skipping (partial open)".
Essential trees (tree_root, chunk_root, fs_root) remain fatal
under PARTIAL as they are required by all users.
Qu Wenruo [Sat, 27 Jun 2026 11:13:55 +0000 (20:43 +0930)]
btrfs-progs: fix the incorrectly set compression
[BUG]
The following script will lead to an incorrectly created file extent:
# mkdir rootdir
# xfs_io -f -c "pwrite -i /dev/urandom 0 32k" rootdir/foobar
# mkfs.btrfs -f -r rootdir --compress zstd test.img
# btrfs ins dump-tree -t 5 test.img
[...]
item 6 key (257 EXTENT_DATA 0) itemoff 15810 itemsize 53
generation 6 type 1 (regular)
extent data disk byte 13631488 nr 32768
extent data offset 0 nr 32768 ram 32768
extent compression 3 (zstd)
Note that file extent has the same num_bytes/ram_bytes as
disk_num_bytes.
Normally this means an uncompressed extent, but it still has the
compression value set to zstd.
This will make zstd decompression to fail, and end users will be unable
to read that file.
[CAUSE]
Inside add_file_item_extent(), if the compression failed with -E2BIG, we
set the INODE_NOCOMPRESS flag, and fallback to the uncompressed path.
But in the uncompressed path, we still check if @do_comp is set.
And if it's set, we set the file extent compression value meanwhile the
fallback path should not go through compression at all.
The file extent compression set is a left-over from the old fallback
behavior, before the proper try_compressed_write() refactor.
[FIX]
Remove the @do_comp check and compression value set completely.
Fixes: c9c8d64acd17 ("btrfs-progs: mkfs/rootdir: factor out compressed write path from add_file_item_extent()")
Issue: #1135 Signed-off-by: Qu Wenruo <wqu@suse.com>
As part of the encryption changes, encrypted inline file extents record
their actual data length in ram_bytes, like compressed inline file
extents, while the item's length records the actual size. As such,
encrypted inline extents must be treated like compressed ones for
inode length consistency checking.
David Sterba [Wed, 10 Jun 2026 17:21:41 +0000 (19:21 +0200)]
btrfs-progs: mkswapfile: new parameter to specify page size
Add support to 'btrfs fi mkswapfile' to set the page size of the
swapfile, which was hardcoded to 4K. The 16K or 64K setups are now
common. The range is 4K-64K while 'mkswap' allows to create other sizes
too.
$ btrfs fi mkswapfile --pagesize 64k swapfile
NOTE: specified page size 65536 different from system value 4096
create swapfile swapfile size 2.00GiB (2147483648) page size 65536
Issue: #1124
Pull-request: #1130 Signed-off-by: David Sterba <dsterba@suse.com>
You-Kai Zheng [Tue, 16 Jun 2026 10:43:08 +0000 (18:43 +0800)]
btrfs-progs: re-enable tree search v2 ioctl
Commit d73e69824854 ("btrfs-progs: temporarily disable usage of v2 of
search tree ioctl") turned off TREE_SEARCH_V2 detection because it led
to an infinite loop in load_chunk_info() (reachable via 'btrfs
filesystem usage', reproducible by mkfs-tests/001): from the second
item in the returned buffer the data read back as all zeros while
nr_items was non-zero, so the search key never advanced.
The root cause is the type of the variable-sized buffer buf in struct
btrfs_ioctl_search_args_v2: it is declared as __u64[], but the buffer
holds a packed byte stream addressed by byte offsets. Adding such an
offset to a __u64 pointer in btrfs_tree_search_data() scaled it by 8,
so every item after the first landed past the valid data and read
zeros, which fed a zeroed key back into the search and spun forever.
Declare buf as __u8 in all in-tree copies of the struct so the offset
arithmetic is in bytes, and restore the tree search autodetection so
callers use the v2 ioctl when supported. The struct size is unchanged:
buf stays 8-byte aligned after the __u64 buf_size.
Considering a lot of subcommands are utilizing tree-search ioctl, hide
the v2 tree search behind experimental build first, only after we have
confirmed everything is fine move it out of experimental builds.
Which will normally report a lot of csum mismatch on mirror 2:
Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: e742b7b6-2562-48e5-b411-d50975633045
[1/8] checking log skipped (none written)
[2/8] checking root items
[3/8] checking extents
[4/8] checking free space tree
[5/8] checking fs roots
[6/8] checking csums against data
mirror 2 bytenr 298909696 csum 0xd800f135 expected csum 0x7f6b1842
mirror 2 bytenr 298913792 csum 0x1f49b645 expected csum 0x7f6b1842
...
mirror 2 bytenr 1372917760 csum 0x16f78e6b expected csum 0x9462d728
mirror 2 bytenr 1372921856 csum 0xa972667e expected csum 0x3bea937f
ERROR: errors found in csum tree
[7/8] checking root refs
[8/8] checking quota groups skipped (not enabled on this FS)
found 2736128 bytes used, error(s) found
And if ASAN is enabled, it will detect a heap-over-flow:
[6/8] checking csums against data
=================================================================
==6512==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7e982fa2c800 at pc 0x7f883111e4da bp 0x7ffc69e8c9a0 sp 0x7ffc69e8c148
READ of size 65536 at 0x7e982fa2c800 thread T0
#0 0x7f883111e4d9 in memcpy /usr/src/debug/gcc/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc:115
#1 0x560b9f250c4a in read_raid56 kernel-shared/extent_io.c:413
#2 0x560b9f25100a in read_data_from_disk kernel-shared/extent_io.c:443
#3 0x560b9f3faa12 in check_extent_csums check/main.c:5964
#4 0x560b9f3fcaee in check_csum_root check/main.c:6235
#5 0x560b9f3fcef6 in check_csums check/main.c:6276
#6 0x560b9f41d168 in cmd_check check/main.c:11082
#7 0x560b9f1f04fb in cmd_execute cmds/commands.h:126
#8 0x560b9f1f1cc8 in main /home/adam/btrfs-progs/btrfs.c:474
#9 0x7f8830c27634 (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#10 0x7f8830c276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#11 0x560b9f1f03d4 in _start (/home/adam/btrfs-progs/btrfs+0x883d4) (BuildId: 2fde8cac91b9fec1b45e1b5145ee594466b76396)
0x7e982fa2c800 is located 0 bytes after 65536-byte region [0x7e982fa1c800,0x7e982fa2c800)
allocated by thread T0 here:
#0 0x7f8831120cb5 in malloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:67
#1 0x560b9f250874 in read_raid56 kernel-shared/extent_io.c:348
#2 0x560b9f25100a in read_data_from_disk kernel-shared/extent_io.c:443
#3 0x560b9f3faa12 in check_extent_csums check/main.c:5964
#4 0x560b9f3fcaee in check_csum_root check/main.c:6235
#5 0x560b9f3fcef6 in check_csums check/main.c:6276
#6 0x560b9f41d168 in cmd_check check/main.c:11082
#7 0x560b9f1f04fb in cmd_execute cmds/commands.h:126
#8 0x560b9f1f1cc8 in main /home/adam/btrfs-progs/btrfs.c:474
#9 0x7f8830c27634 (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#10 0x7f8830c276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#11 0x560b9f1f03d4 in _start (/home/adam/btrfs-progs/btrfs+0x883d4) (BuildId: 2fde8cac91b9fec1b45e1b5145ee594466b76396)
SUMMARY: AddressSanitizer: heap-buffer-overflow kernel-shared/extent_io.c:413 in read_raid56
Shadow bytes around the buggy address:
0x7e982fa2c580: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7e982fa2c600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7e982fa2c680: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7e982fa2c700: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7e982fa2c780: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x7e982fa2c800:[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x7e982fa2c880: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x7e982fa2c900: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x7e982fa2c980: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x7e982fa2ca00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x7e982fa2ca80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
[CAUSE]
During read of mirrors other than mirror 0/1 from RAID56 profiles,
btrfs-progs go through the following call chain:
read_data_from_disk()
|- btrfs_map_block()
| Note that @read_len returned by btrfs_map_block() is always
| BTRFS_STRIPE_LEN.
|
\- read_raid56()
| Which does read out all stripes, but there is no extra checks
| against if the logical range crosses stripe boundaries.
|
|- raid56_recov()
| Which rebuilds the target stripe.
|
|- memcpy(buf, pointers[failed_a] + (logical - full_stripe_start) %
BTRFS_STRIPE_LEN, len);
If @logical is not at the begining of a stripe, then @logical + @len
will go beyond the stripe buffer size, causing gargage into the target
buffer.
[FIX]
Calculate the correct read length inside the data stripe for RAID56
inside read_data_from_disk().
And add extra ASSERT()s inside read_raid56().
Pull-request: #1123 Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
Qu Wenruo [Fri, 29 May 2026 08:25:50 +0000 (17:55 +0930)]
btrfs-progs: dump-tree: fix the chunk type check for system chunks
[BUG]
Commit 408e45feb325 ("btrfs-progs: dump-tree: add extra chunk type
checks") introduced one extra check on when using "btrfs ins dump-tree
-b <bytenr>" option, to prevent end users from passing logical address
belonging to data chunks.
However that check misses that we also have metadata in system chunks,
thus will cause the following false alerts:
# mkfs.btrfs -f $dev
# btrfs ins dump-tree -t chunk $dev | grep "owner CHUNK_TREE"
leaf 22036480 items 4 free space 15781 generation 8 owner CHUNK_TREE
^^^ This means we can still print the chunk tree
# btrfs ins dump-tree -b 22036480 $dev
btrfs-progs v6.19.1
ERROR: logical 22036480 is not in a metadata chunk, found chunk 22020096 len 8388608 flags 0x22
[FIX]
Add system chunk type into the check.
Pull-request: #1125 Fixes: 408e45feb325 ("btrfs-progs: dump-tree: add extra chunk type checks") Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
Qu Wenruo [Thu, 11 Jun 2026 00:46:41 +0000 (10:16 +0930)]
btrfs-progs: fix a memory leak in __btrfs_map_block() after RST lookup failure
[BUG]
With all previous memory leaks fixed, now mkfs/030 fails with the
following memory leak:
====== RUN CHECK /home/adam/btrfs-progs/mkfs.btrfs -f -O zoned -d dup -m single /dev/nullb0 /dev/nullb1 /dev/nullb2 /dev/nullb3
WARNING: DUP is not recommended on filesystem with multiple devices
WARNING: DUP may not actually lead to 2 copies on the device, see manual page
Direct leak of 144 byte(s) in 1 object(s) allocated from:
#0 0x7fe9247205dd in calloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:74
#1 0x5587da350c9a in __btrfs_map_block kernel-shared/volumes.c:2236
#2 0x5587da34f4b9 in btrfs_map_block kernel-shared/volumes.c:2002
#3 0x5587da279470 in queue_discard_logical mkfs/main.c:1355
#4 0x5587da27a01b in discard_free_space mkfs/main.c:1453
#5 0x5587da2825a8 in main mkfs/main.c:2587
#6 0x7fe924227634 (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#7 0x7fe9242276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#8 0x5587da26ffa4 in _start (/home/adam/btrfs-progs/mkfs.btrfs+0x3cfa4) (BuildId: 67b730427c6a4739addff9bd31ecd89b1b5ae11a)
[CAUSE]
In __btrfs_map_block() if the fs has raid-stripe-tree enabled, we need
to do one extra mapping to get the physical address.
However btrfs_stripe_tree_logical_to_physical() can fail, and if that
function failed, we error out without freeing @multi we have allocated,
resulting the above memory leak.
[FIX]
Do the proper error handling before erroring out after
btrfs_stripe_tree_logical_to_physical() failure.
Pull-request: #1131 Fixes: a9fd50f85fdd ("btrfs-progs: read stripe tree when mapping blocks") Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
Direct leak of 64 byte(s) in 1 object(s) allocated from:
#0 0x7f5561b205dd in calloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:74
#1 0x5568792ba42a in bitmap_zalloc kernel-lib/bitmap.h:19
#2 0x5568792c24c0 in btrfs_load_block_group_zone_info kernel-shared/zoned.c:1227
#3 0x556879256b02 in btrfs_add_block_group kernel-shared/extent-tree.c:3025
#4 0x5568792572ba in btrfs_make_block_group kernel-shared/extent-tree.c:3094
#5 0x5568791d3d7b in create_metadata_block_groups mkfs/main.c:162
#6 0x5568791e1a52 in main mkfs/main.c:2263
#7 0x7f5561627634 (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#8 0x7f55616276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#9 0x5568791d1fa4 in _start (/home/adam/btrfs-progs/mkfs.btrfs+0x3cfa4) (BuildId: dd46ced75e977e88e3b77290546834407f113fba)
Direct leak of 64 byte(s) in 1 object(s) allocated from:
#0 0x7f5561b205dd in calloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:74
#1 0x5568792ba42a in bitmap_zalloc kernel-lib/bitmap.h:19
#2 0x5568792c24c0 in btrfs_load_block_group_zone_info kernel-shared/zoned.c:1227
#3 0x556879256b02 in btrfs_add_block_group kernel-shared/extent-tree.c:3025
#4 0x5568792572ba in btrfs_make_block_group kernel-shared/extent-tree.c:3094
#5 0x5568791d446d in create_data_block_groups mkfs/main.c:222
#6 0x5568791e1ea7 in main mkfs/main.c:2297
#7 0x7f5561627634 (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#8 0x7f55616276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#9 0x5568791d1fa4 in _start (/home/adam/btrfs-progs/mkfs.btrfs+0x3cfa4) (BuildId: dd46ced75e977e88e3b77290546834407f113fba)
Direct leak of 64 byte(s) in 1 object(s) allocated from:
#0 0x7f5561b205dd in calloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:74
#1 0x5568792ba42a in bitmap_zalloc kernel-lib/bitmap.h:19
#2 0x5568792c24c0 in btrfs_load_block_group_zone_info kernel-shared/zoned.c:1227
#3 0x556879256b02 in btrfs_add_block_group kernel-shared/extent-tree.c:3025
#4 0x5568792572ba in btrfs_make_block_group kernel-shared/extent-tree.c:3094
#5 0x5568791d446d in create_data_block_groups mkfs/main.c:222
#6 0x5568791e1ffe in main mkfs/main.c:2305
#7 0x7f5561627634 (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#8 0x7f55616276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#9 0x5568791d1fa4 in _start (/home/adam/btrfs-progs/mkfs.btrfs+0x3cfa4) (BuildId: dd46ced75e977e88e3b77290546834407f113fba)
[CAUSE]
Inside btrfs_load_block_group_zone_info() we allocated an @active bitmap
using bitmap_zalloc(), and use that bitmap for updating @zone_info.
However after everything is done we only freed @zone_info, not @active
bitmap, causing the above memory leak.
Direct leak of 20480 byte(s) in 10 object(s) allocated from:
#0 0x7ffa8d7205dd in calloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:74
#1 0x55e960f93d53 in report_zones kernel-shared/zoned.c:365
#2 0x55e960f9a9f1 in btrfs_get_zone_info kernel-shared/zoned.c:1465
#3 0x55e960fa0d37 in btrfs_prepare_device common/device-utils.c:253
#4 0x55e960eb14ee in prepare_one_device mkfs/main.c:1156
#5 0x7ffa8d65e11a in asan_thread_start /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_interceptors.cpp:239
#6 0x7ffa8d29698a (/usr/lib/libc.so.6+0x9698a) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
Direct leak of 18432 byte(s) in 9 object(s) allocated from:
#0 0x7ffa8d7205dd in calloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:74
#1 0x55e960f93d53 in report_zones kernel-shared/zoned.c:365
#2 0x55e960f9a9f1 in btrfs_get_zone_info kernel-shared/zoned.c:1465
#3 0x55e960f9a847 in btrfs_get_dev_zone_info kernel-shared/zoned.c:1431
#4 0x55e960f9a6d2 in btrfs_get_dev_zone_info_all_devices kernel-shared/zoned.c:1408
#5 0x55e960eb9b7d in main mkfs/main.c:2383
#6 0x7ffa8d227634 (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#7 0x7ffa8d2276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#8 0x55e960ea8fa4 in _start (/home/adam/btrfs-progs/mkfs.btrfs+0x3cfa4) (BuildId: 3def50571e3d23304fbe357251f52f4428140607)
Direct leak of 2048 byte(s) in 1 object(s) allocated from:
#0 0x7ffa8d7205dd in calloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:74
#1 0x55e960f93d53 in report_zones kernel-shared/zoned.c:365
#2 0x55e960f9a9f1 in btrfs_get_zone_info kernel-shared/zoned.c:1465
#3 0x55e960f9a847 in btrfs_get_dev_zone_info kernel-shared/zoned.c:1431
#4 0x55e960f9a6d2 in btrfs_get_dev_zone_info_all_devices kernel-shared/zoned.c:1408
#5 0x55e960f0d0f0 in __open_ctree_fd kernel-shared/disk-io.c:1665
#6 0x55e960f0d9d8 in open_ctree_fs_info kernel-shared/disk-io.c:1733
#7 0x55e960eb88f2 in main mkfs/main.c:2254
#8 0x7ffa8d227634 (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#9 0x7ffa8d2276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
#10 0x55e960ea8fa4 in _start (/home/adam/btrfs-progs/mkfs.btrfs+0x3cfa4) (BuildId: 3def50571e3d23304fbe357251f52f4428140607)
...
[CAUSE]
In btrfs_prepare_device(), we call btrfs_get_zone_info() which allocated
space for zinfo, then later report_zones() allocates zinfo::zones and
zinfo::active_zones.
But we only free zinfo itself, not the zones nor active_zones pointers,
leading to the above leak.
[FIX]
Introduce a helper, btrfs_free_zoned_device_info() to do the proper free
of a btrfs_zoned_device_info structure.
Pull-request: #1131 Fixes: 58ec59389263 ("btrfs-progs: zoned: support resetting zoned device") Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
David Sterba [Tue, 9 Jun 2026 19:24:09 +0000 (21:24 +0200)]
btrfs-progs: add path helper for readlink
Readlink does not add the terminating null byte and can silently
truncate the buffer if not provided enough space. Add a helper to
abstract the edge cases. The destination argument must be path, i.e.
at least PATH_MAX bytes.