]> git.hungrycats.org Git - btrfs-progs/log
btrfs-progs
7 weeks agobtrfs-progs: misc-tests: add a test case for btrfs-restore devel
Qu Wenruo [Wed, 16 Sep 2026 03:51:33 +0000 (13:21 +0930)]
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.

Signed-off-by: Qu Wenruo <wqu@suse.com>
7 weeks agobtrfs-progs: restore: handle inlined extent larger than 4K
Qu Wenruo [Wed, 16 Sep 2026 04:00:50 +0000 (13:30 +0930)]
btrfs-progs: restore: handle inlined extent larger than 4K

Most btrfs are already using 4K sectorsize, but for systems with 64K
page size, it's possible to create large inlined extent that is larger
than 4K:

item 6 key (257 EXTENT_DATA 0) itemoff 56802 itemsize 8213
generation 9 type 0 (inline)
inline extent data size 8192 ram_bytes 8192 compression 0 (none)

In that case, btrfs-restore will crash as its input buffer is only 4K
sized.

Enhance btrfs-restore to handle larger block size correctly, by allocate
a block-sized buffer at input buffer.

Signed-off-by: Qu Wenruo <wqu@suse.com>
7 weeks agobtrfs-progs: restore: handle full block compressed inline extent
Qu Wenruo [Wed, 16 Sep 2026 03:36:02 +0000 (13:06 +0930)]
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().

Issue: #1171
Signed-off-by: Qu Wenruo <wqu@suse.com>
7 weeks agobtrfs-progs: mkfs: make -O mixed-bg behave identically to --mixed
Andrus Suvalau [Mon, 28 Sep 2026 15:10:04 +0000 (17:10 +0200)]
btrfs-progs: mkfs: make -O mixed-bg behave identically to --mixed

Mixed block groups can be requested either with the -M/--mixed flag or
with the -O mixed-bg filesystem feature.

However using "mkfs.btrfs -O mixed-bg" results non-mixed block groups:

 Label:              (null)
 UUID:               8b6b2eaf-d399-4190-8b64-fcbed24f20fe
 Node size:          4096
 Sector size:        4096        (CPU page size: 4096)
 Filesystem size:    2.00GiB
 Block group profiles:
   Data:             single            8.00MiB <<<
   Metadata:         DUP             102.38MiB <<<
   Data+Metadata:    single            8.00MiB
   System:           DUP               8.00MiB

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>
7 weeks agobtrfs-progs: receive: fix unused variable warning without LZO
liuh0329 [Wed, 30 Sep 2026 06:43:28 +0000 (14:43 +0800)]
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>
2 months agobtrfs-progs: check: detect leaked active zones on zoned devices
Johannes Thumshirn [Fri, 4 Sep 2026 06:24:44 +0000 (08:24 +0200)]
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>
2 months agobtrfs-progs: zoned: refresh stale cached zone state in btrfs_reset_dev_zone()
Johannes Thumshirn [Fri, 4 Sep 2026 06:24:43 +0000 (08:24 +0200)]
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>
2 months agobtrfs-progs: sb-mod: use per-type digest size for superblock csum
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>
2 months agobtrfs-progs: sb-mod: add checksum type validation
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.

Signed-off-by: David Sterba <dsterba@suse.com>
2 months agobtrfs-progs: tests: remove csum_type from fuzz/010 case
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.

Signed-off-by: David Sterba <dsterba@suse.com>
2 months agolibbtrfsutil: don't leave subvolume iterator pointing at unfilled search buffer
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>
2 months agobtrfs-progs: docs: add a note about RW degraded mount
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>
2 months agobtrfs-progs: tests: add case for unexpected file extent items
Qu Wenruo [Tue, 18 Aug 2026 06:04:07 +0000 (15:34 +0930)]
btrfs-progs: tests: add case for unexpected file extent items

The image is created using btrfs-corrupt-block, the fs tree leaf looks
like this:

item 0 key (256 INODE_ITEM 0) itemoff 16123 itemsize 160
generation 3 transid 9 size 12 nbytes 16384
block group 0 mode 40755 links 1 uid 0 gid 0 rdev 0
sequence 1 flags 0x0(none)
item 1 key (256 INODE_REF 256) itemoff 16111 itemsize 12
index 0 namelen 2 name: ..
item 2 key (256 DIR_ITEM 496027801) itemoff 16075 itemsize 36
location key (257 INODE_ITEM 0) type BLKDEV <<<
transid 9 data_len 0 name_len 6
name: foobar
item 3 key (256 DIR_INDEX 2) itemoff 16039 itemsize 36
location key (257 INODE_ITEM 0) type BLKDEV <<<
transid 9 data_len 0 name_len 6
name: foobar
item 4 key (257 INODE_ITEM 0) itemoff 15879 itemsize 160
generation 9 transid 9 size 8192 nbytes 8192
block group 0 mode 60660 links 1 uid 0 gid 0 rdev 0 <<<
sequence 2 flags 0x0(none)
item 5 key (257 INODE_REF 256) itemoff 15863 itemsize 16
index 2 namelen 6 name: foobar
item 6 key (257 EXTENT_DATA 0) itemoff 15810 itemsize 53
generation 9 type 1 (regular)
extent data disk byte 13631488 nr 8192
extent data offset 0 nr 8192 ram 8192
extent compression 0 (none)

The image is generated by the following steps:

  # mkfs.btrfs -f $dev
  # mount $dev $mnt
  # xfs_io -f -c "pwrite 0 4k" $mnt/foobar
  # umount $mnt

  ## Change the inode's mode from REG to BLK
  # btrfs-corrupt-block -i 257 -f mode --value 25008 $dev

  ## Change the dir item's flag from REG to BLK
  # btrfs-corrupt-block -D 256,84,496027801 -f flags --value 4 $dev

  ## Change the dir index's flag from REG to BLK
  # btrfs-corrupt-block -D 256,96,2 -f flags --value 4 $dev

The inode's mode and dir flags all match, but there is one unexpected
file extent item there.

Both lowmem and original mode should detect such problem.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2 months agobtrfs-progs: check: detect invalid file extent items
Qu Wenruo [Tue, 18 Aug 2026 05:49:05 +0000 (15:19 +0930)]
btrfs-progs: check: detect invalid file extent items

File extent items should only exist for regular and symlink inodes.
And for symlink inodes, the file extent must be inlined.

Add such check for lowmem mode to detect those invalid file extents.

Now for a corrupted image with the following fs tree layout:

item 0 key (256 INODE_ITEM 0) itemoff 16123 itemsize 160
generation 3 transid 9 size 12 nbytes 16384
block group 0 mode 40755 links 1 uid 0 gid 0 rdev 0
sequence 1 flags 0x0(none)
item 1 key (256 INODE_REF 256) itemoff 16111 itemsize 12
index 0 namelen 2 name: ..
item 2 key (256 DIR_ITEM 496027801) itemoff 16075 itemsize 36
location key (257 INODE_ITEM 0) type BLKDEV
transid 9 data_len 0 name_len 6
name: foobar
item 3 key (256 DIR_INDEX 2) itemoff 16039 itemsize 36
location key (257 INODE_ITEM 0) type BLKDEV
transid 9 data_len 0 name_len 6
name: foobar
item 4 key (257 INODE_ITEM 0) itemoff 15879 itemsize 160
generation 9 transid 9 size 8192 nbytes 8192
block group 0 mode 60660 links 1 uid 0 gid 0 rdev 0
sequence 2 flags 0x0(none)
item 5 key (257 INODE_REF 256) itemoff 15863 itemsize 16
index 2 namelen 6 name: foobar
item 6 key (257 EXTENT_DATA 0) itemoff 15810 itemsize 53
generation 9 type 1 (regular)
extent data disk byte 13631488 nr 8192
extent data offset 0 nr 8192 ram 8192
extent compression 0 (none)

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>
2 months agobtrfs-progs: corrupt-block: allow corrupting dir item flags
Qu Wenruo [Tue, 18 Aug 2026 05:35:10 +0000 (15:05 +0930)]
btrfs-progs: corrupt-block: allow corrupting dir item flags

With this feature and the previous inode corruption, we can change the
type in inode item, dir item, dir index to be consistent with each
other.

Assisted-by: LLM (coding)
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2 months agobtrfs-progs: corrupt-block: allow dir item corruption to use --value option
Qu Wenruo [Tue, 18 Aug 2026 05:30:15 +0000 (15:00 +0930)]
btrfs-progs: corrupt-block: allow dir item corruption to use --value option

For now this only affects DIR_ITEM location objectid corruption, but
later this will be utilized for more dir item member corruption.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2 months agobtrfs-progs: corrupt-block: allow inode corruption to use --value option
Qu Wenruo [Tue, 18 Aug 2026 05:21:56 +0000 (14:51 +0930)]
btrfs-progs: corrupt-block: allow inode corruption to use --value option

This allows us to do more accurate inode item corruption, not only to
inject random number, but more targeted values.

This feature will be later utilized to change inode's mode from REG to
BLK.

Assisted-by: LLM (coding)
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2 months agobtrfs-progs: set creation/change time (otime/ctime) of offline-created subvolumes
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

Pull-request: #1157
Assisted-by: Gemini:3.1-pro antigravity-cli-1.1.7
Signed-off-by: koraynilay <koray.fra@gmail.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
[ Remove the duplicated <string.h> header inclusion ]
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2 months agobtrfs-progs: convert: set creation/change time (otime/ctime) of root subvol
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.

Pull-request: #1157
Assisted-by: Claude:opus-4.6 antigravity-cli-1.1.7
Assisted-by: Gemini:3.1-pro antigravity-cli-1.1.7
Signed-off-by: koraynilay <koray.fra@gmail.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
[ Remove the duplicated <time.h> header inclusion ]
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2 months agobtrfs-progs: subvolume show: fix "Creation time" value being shown for "Send 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>
3 months agobtrfs-progs: docs: drop stable 5.4 heading from feature overview
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.

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: ci: update image administration steps
David Sterba [Tue, 18 Aug 2026 08:43:27 +0000 (10:43 +0200)]
btrfs-progs: ci: update image administration steps

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: docs: clarify Deduplication
David Sterba [Tue, 18 Aug 2026 08:15:15 +0000 (10:15 +0200)]
btrfs-progs: docs: clarify Deduplication

Based on question and answer in the issue, udpate the initial paragraph.
Mention that we now have access to raw checksums too.

[ci skip]

Issue: #1137
Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: docs: add heading for 7.2
David Sterba [Mon, 15 Jul 2024 13:00:15 +0000 (15:00 +0200)]
btrfs-progs: docs: add heading for 7.2

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: docs: update list of experimental features
David Sterba [Mon, 17 Aug 2026 17:46:51 +0000 (19:46 +0200)]
btrfs-progs: docs: update list of experimental features

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: docs: update kernel features for 7.2
David Sterba [Mon, 9 Feb 2026 19:25:54 +0000 (20:25 +0100)]
btrfs-progs: docs: update kernel features for 7.2

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: docs: update Status page
David Sterba [Tue, 16 Jun 2026 23:17:09 +0000 (01:17 +0200)]
btrfs-progs: docs: update Status page

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: docs: update 7.2 contribution graphs
David Sterba [Thu, 20 Mar 2025 23:59:22 +0000 (00:59 +0100)]
btrfs-progs: docs: update 7.2 contribution graphs

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: docs: add 7.2 kernel development statistics
David Sterba [Tue, 16 Jun 2026 23:24:08 +0000 (01:24 +0200)]
btrfs-progs: docs: add 7.2 kernel development statistics

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: fix typos or spelling
David Sterba [Mon, 17 Aug 2026 13:29:06 +0000 (15:29 +0200)]
btrfs-progs: fix typos or spelling

Manual pass over all sources to fix things not reported by codespell.
Raw spellcheck used.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: docs: update the stability on btrfs-quota
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.

Signed-off-by: Qu Wenruo <wqu@suse.com>
3 months agobtrfs-progs: fix-data-checksum: fix an off-by-one error in the mirror bitmap
Sidong Yang [Tue, 4 Aug 2026 12:39:15 +0000 (12:39 +0000)]
btrfs-progs: fix-data-checksum: fix an off-by-one error in the mirror bitmap

[BUG]
With both mirrors of a DUP data block corrupted, only the first one shows
up in the report:

  # btrfs rescue fix-data-checksum dup.img

  logical=84082688 corrtuped mirrors=1 affected files:
    (subvolume 5)/file2

While both mirrors have a checksum mismatch, so the expected output is:

  logical=84082688 corrtuped mirrors=1,2 affected files:
    (subvolume 5)/file2

[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>
3 months agobtrfs-progs: fix-data-checksum: fix the size of the error mirror bitmap
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():

  last->error_mirror_bitmap = calloc(1, BITS_TO_LONGS(num_mirrors));

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>
3 months agobtrfs-progs: remove unnecessary forced type casting in print-tree.c
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>
3 months agobtrfs-progs: tests: update bash style guide
David Sterba [Mon, 3 Aug 2026 11:04:11 +0000 (13:04 +0200)]
btrfs-progs: tests: update bash style guide

[ ci skip ]

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: tests: cleanup bash style in ./common
David Sterba [Fri, 31 Jul 2026 16:42:39 +0000 (18:42 +0200)]
btrfs-progs: tests: cleanup bash style in ./common

- use [[ ]]
- use $* or [*] inside string
- add missing quotes
- visual style fixups
- arithmetic expression simplifications

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: tests: cleanup bash style in common scripts
David Sterba [Fri, 31 Jul 2026 16:00:10 +0000 (18:00 +0200)]
btrfs-progs: tests: cleanup bash style in common scripts

- use [[ ]]
- use $* inside string
- visual style fixups

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: tests: cleanup bash style in driver scripts
David Sterba [Fri, 31 Jul 2026 15:32:57 +0000 (17:32 +0200)]
btrfs-progs: tests: cleanup bash style in driver scripts

- use [[ ]]
- use $* inside string
- use "=" for simple comparisons
- add missing quotes
- change && || to if-else

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: drop type casts from set/get_label calls
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.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: receive: use correct type for pwrite in process_write()
David Sterba [Thu, 23 Jul 2026 10:14:08 +0000 (12:14 +0200)]
btrfs-progs: receive: use correct type for pwrite in process_write()

pwrite() return value will fit 'int' most of the time, but we should use
the correct type for consistency.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: receive: handle writes of 0 bytes
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.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: fix assigning negative errno
David Sterba [Thu, 23 Jul 2026 09:52:47 +0000 (11:52 +0200)]
btrfs-progs: fix assigning negative errno

As we use negative errno in many places, there were two conversion that
assigned a negative value there, which is wrong.

Note that in many other places we do 'errno = ret' but that's for
pthread_* or posix_memalign() functions that return errno directly.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: use full size path buffer in cmd_subvolume_delete()
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.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: use path API to build subvolume paths
David Sterba [Wed, 22 Jul 2026 16:32:53 +0000 (18:32 +0200)]
btrfs-progs: use path API to build subvolume paths

There's open coded path concatenation using raw str* functions, we have
the path API for that.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: use parse_u64() in cmd_subvolume_sync()
David Sterba [Wed, 22 Jul 2026 11:44:52 +0000 (13:44 +0200)]
btrfs-progs: use parse_u64() in cmd_subvolume_sync()

Use our helper to avoid intricacies of strtoull().

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: reset errno before strtoull() when parsing numbers
David Sterba [Wed, 22 Jul 2026 11:37:43 +0000 (13:37 +0200)]
btrfs-progs: reset errno before strtoull() when parsing numbers

On success strtoull() does not change the errno, so we have to reset it
not to read any stale value.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: don't reuse filesystem fd for sysfs files
David Sterba [Wed, 22 Jul 2026 11:32:01 +0000 (13:32 +0200)]
btrfs-progs: don't reuse filesystem fd for sysfs files

Sysfs helpers reuse the input file descriptor of the filesystem for the
sysfs files, as this is one time read. For clarity use two variables for
that.

Signed-off-by: David Sterba <dsterba@suse.com>
3 months agobtrfs-progs: handle strdup() failures
David Sterba [Wed, 22 Jul 2026 11:13:02 +0000 (13:13 +0200)]
btrfs-progs: handle strdup() failures

There are many unhandled calls to strdup(), while this almost never
fails we still want to handle all runtime errors.

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: misc-tests: replace deprecated standalone "usebackuproot" option
Qu Wenruo [Wed, 8 Jul 2026 06:26:04 +0000 (15:56 +0930)]
btrfs-progs: misc-tests: replace deprecated standalone "usebackuproot" option

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.

Signed-off-by: Qu Wenruo <wqu@suse.com>
4 months agobtrfs-progs: docs: mark "usebackuproot" mount option as removed
Qu Wenruo [Sat, 4 Jul 2026 09:05:30 +0000 (18:35 +0930)]
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>
4 months agobtrfs-progs: update the direct IO behavior notes for version 7.3
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.

Signed-off-by: Qu Wenruo <wqu@suse.com>
4 months agobtrfs-progs: sync BTRFS_FEATURE_INCOMPAT_SUPP_STABLE from the kernel
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.

Signed-off-by: Qu Wenruo <wqu@suse.com>
4 months agobtrfs-progs: remove out-of-data comments on free space tree
Qu Wenruo [Sat, 27 Jun 2026 04:21:53 +0000 (13:51 +0930)]
btrfs-progs: remove out-of-data comments on free space tree

Btrfs-progs has read-write support for free-space-tree since commit
f46b08f3ab69 ("btrfs-progs: Add freespace tree as compat_ro supported
feature").

The out-of-date comment no longer makes any sense, just remove it.

Signed-off-by: Qu Wenruo <wqu@suse.com>
4 months agobtrfs-progs: tests: don't use eval in mkfs/014 for file filler
David Sterba [Mon, 20 Jul 2026 10:21:10 +0000 (12:21 +0200)]
btrfs-progs: tests: don't use eval in mkfs/014 for file filler

While the 'eval printf' is safe here, let's avoid it and use a different
way based on zero prefix of decimal values.

The other use of 'eval' is in the interactive test console which is not
ran during the testsuite and only meant for local prototyping.

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: tests: fix simple shellcheck warnings in test cases
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

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: tests: fix ctime check in convert/019
David Sterba [Mon, 20 Jul 2026 08:29:04 +0000 (10:29 +0200)]
btrfs-progs: tests: fix ctime check in convert/019

This seems to be a copy&paste error, stat format %z is ctime and this is
also reported in the failure. Use the correct variable.

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: tests: remove unused variables from test cases
David Sterba [Mon, 20 Jul 2026 08:28:23 +0000 (10:28 +0200)]
btrfs-progs: tests: remove unused variables from test cases

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: tests: assign default size value directly in prepare_test_dev()
David Sterba [Mon, 20 Jul 2026 08:25:26 +0000 (10:25 +0200)]
btrfs-progs: tests: assign default size value directly in prepare_test_dev()

Shellcheck is slightly confused by the way the size is assigned the
default value, in a separate statement. Do it directly in the local
declaration.

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: tests: fix simple shellcheck warnings in test driver scripts
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'

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: tests: run dmesg with sudo
David Sterba [Fri, 17 Jul 2026 19:59:28 +0000 (21:59 +0200)]
btrfs-progs: tests: run dmesg with sudo

On many systems 'dmesg' is not available for regular users and this
fails some of our tests. Use the sudo helper.

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: zoned: open host-managed devices with O_DIRECT for read-only
Johannes Thumshirn [Thu, 16 Jul 2026 11:28:19 +0000 (13:28 +0200)]
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>
4 months agoBtrfs progs v7.1
David Sterba [Tue, 14 Jul 2026 22:41:48 +0000 (00:41 +0200)]
Btrfs progs v7.1

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: update CHANGES for 7.1
David Sterba [Fri, 7 Nov 2025 09:03:34 +0000 (10:03 +0100)]
btrfs-progs: update CHANGES for 7.1

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: check: add option to skip qgroup accounting verification
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.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Dongjiang Zhu <zhudongjiang@fnnas.com>
4 months agobtrfs-progs: test: add mkfs test case for hardlink deduplication
Dara Adib [Thu, 9 Jul 2026 00:11:59 +0000 (20:11 -0400)]
btrfs-progs: test: add mkfs test case for hardlink deduplication

Add a test that creates 100 hard links to a 10 MB file and verifies that
mkfs.btrfs does not grow the shrunk image.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Dara Adib <daradib@ocf.berkeley.edu>
4 months agobtrfs-progs: mkfs: dedupe hardlinks when estimating rootdir size
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>
4 months agobtrfs-progs: extend OPEN_CTREE_PARTIAL to skip more non-essential trees
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.

Reviewed-by: Qu Wenruo <wqu@suse.com>
4 months agobtrfs-progs: tests: add a new mkfs case for incompressible data
Qu Wenruo [Sat, 27 Jun 2026 11:30:39 +0000 (21:00 +0930)]
btrfs-progs: tests: add a new mkfs case for incompressible data

The new test case will verify that mkfs.btrfs --compress option will not
create a uncompressed file extent but with compression set.

The idea is to create an incompressible file using /dev/urandom, then
try all compress algorithm, verify the contents and data checksum.

For unpatched progs, reading the file will fail.

Signed-off-by: Qu Wenruo <wqu@suse.com>
4 months agobtrfs-progs: fix the incorrectly set compression
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>
4 months agobtrfs-progs: recognize ENCRYPT inode item flag
Daniel Vacek [Tue, 7 Jul 2026 14:27:36 +0000 (16:27 +0200)]
btrfs-progs: recognize ENCRYPT inode item flag

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Daniel Vacek <neelx@suse.com>
4 months agobtrfs-progs: check: update inline extent length checking
Sweet Tea Dorminy [Tue, 7 Jul 2026 14:27:35 +0000 (16:27 +0200)]
btrfs-progs: check: update inline extent length checking

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.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Signed-off-by: Daniel Vacek <neelx@suse.com>
4 months agobtrfs-progs: handle fscrypt context items
Sweet Tea Dorminy [Tue, 7 Jul 2026 14:27:34 +0000 (16:27 +0200)]
btrfs-progs: handle fscrypt context items

Encrypted inodes have a new associated item, the fscrypt context, which
can be printed as a pure hex string in dump-tree.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Signed-off-by: Daniel Vacek <neelx@suse.com>
4 months agobtrfs-progs: print encryptin type field of file extents
Sweet Tea Dorminy [Tue, 7 Jul 2026 14:27:33 +0000 (16:27 +0200)]
btrfs-progs: print encryptin type field of file extents

Encrypted file extents now have the 'encryption' field set to an
encryption type.  Let's print it.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Signed-off-by: Daniel Vacek <neelx@suse.com>
4 months agobtrfs-progs: add inode encryption contexts
Sweet Tea Dorminy [Tue, 7 Jul 2026 14:27:32 +0000 (16:27 +0200)]
btrfs-progs: add inode encryption contexts

Recapitulates relevant parts of kernel change 'btrfs: add inode
encryption contexts'.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Signed-off-by: Daniel Vacek <neelx@suse.com>
4 months agobtrfs-progs: start tracking extent encryption context info
Sweet Tea Dorminy [Tue, 7 Jul 2026 14:27:31 +0000 (16:27 +0200)]
btrfs-progs: start tracking extent encryption context info

This recapitulates the kernel change named 'btrfs: start tracking extent
encryption context info".

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Signed-off-by: Daniel Vacek <neelx@suse.com>
4 months agobtrfs-progs: add new FEATURE_INCOMPAT_ENCRYPT flag
Sweet Tea Dorminy [Tue, 7 Jul 2026 14:27:30 +0000 (16:27 +0200)]
btrfs-progs: add new FEATURE_INCOMPAT_ENCRYPT flag

Matches kernel change by the same name.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Sweet Tea Dorminy <sweettea-kernel@dorminy.me>
Signed-off-by: Daniel Vacek <neelx@suse.com>
4 months agobtrfs-progs: tests: add test case for mkswapfile
David Sterba [Wed, 10 Jun 2026 18:07:03 +0000 (20:07 +0200)]
btrfs-progs: tests: add test case for mkswapfile

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: mkswapfile: new parameter to specify page size
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>
4 months agobtrfs-progs: re-enable tree search v2 ioctl
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.

Suggested-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: You-Kai Zheng <ykzheng@synology.com>
[ Hide the v2 tree search behind experimental builds ]
Signed-off-by: Qu Wenruo <wqu@suse.com>
4 months agobtrfs-progs: docs: add new ioctls to list
David Sterba [Wed, 17 Jun 2026 00:06:23 +0000 (02:06 +0200)]
btrfs-progs: docs: add new ioctls to list

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: docs: add kernel changelog for 7.1
David Sterba [Tue, 16 Jun 2026 23:58:20 +0000 (01:58 +0200)]
btrfs-progs: docs: add kernel changelog for 7.1

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: docs: update 7.0 contribution graphs
David Sterba [Tue, 16 Jun 2026 23:41:53 +0000 (01:41 +0200)]
btrfs-progs: docs: update 7.0 contribution graphs

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: docs: add 7.1 kernel development statistics
David Sterba [Tue, 16 Jun 2026 23:24:08 +0000 (01:24 +0200)]
btrfs-progs: docs: add 7.1 kernel development statistics

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: docs: update Status page
David Sterba [Tue, 16 Jun 2026 23:17:09 +0000 (01:17 +0200)]
btrfs-progs: docs: update Status page

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: docs: formatting adjustments
David Sterba [Tue, 16 Jun 2026 23:01:18 +0000 (01:01 +0200)]
btrfs-progs: docs: formatting adjustments

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: docs: update sysfs file documentation
David Sterba [Tue, 16 Jun 2026 22:46:06 +0000 (00:46 +0200)]
btrfs-progs: docs: update sysfs file documentation

Unify style, fix typos, add missing entries.

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: docs: update kernel features for 7.1
David Sterba [Mon, 9 Feb 2026 19:25:54 +0000 (20:25 +0100)]
btrfs-progs: docs: update kernel features for 7.1

Plus some minor typo fixes.

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: update TODO
David Sterba [Tue, 16 Jun 2026 21:06:05 +0000 (23:06 +0200)]
btrfs-progs: update TODO

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: tests: add new check case for the raid56 read fix
Qu Wenruo [Fri, 22 May 2026 09:35:05 +0000 (19:05 +0930)]
btrfs-progs: tests: add new check case for the raid56 read fix

The new test case is a regression test, which does:

- Create a btrfs with RAID5 data
- Fsstress it with 256 write operations
- Unmount
- Make sure "btrfs check --check-data-csum" reports no error

Pull-request: #1123
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: fix raid56 reconstruction read
Qu Wenruo [Fri, 22 May 2026 08:52:18 +0000 (18:22 +0930)]
btrfs-progs: fix raid56 reconstruction read

[BUG]
The following script can easily lead to false alerts from
"btrfs check --check-data-csum":

mkfs.btrfs -f -d raid5 $dev1 $dev2 > /dev/null
mount $dev1 $mnt
$fsstress -w -n 64 -d $mnt
umount $mnt
btrfs check --check-data-csum $dev1

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>
4 months agobtrfs-progs: tests: add a regression test case for dump tree
Qu Wenruo [Fri, 29 May 2026 08:57:49 +0000 (18:27 +0930)]
btrfs-progs: tests: add a regression test case for dump tree

The new test case will:

- Create an empty btrfs
- Grab the chunk tree root bytenr
- Pass that bytenr into "btrfs ins dump-tree -b <bytenr>"
  That must success.

This is for the regression introduced by commit 408e45feb325
("btrfs-progs: dump-tree: add extra chunk type checks").

Pull-request: #1125
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: dump-tree: fix the chunk type check for system chunks
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>
4 months agobtrfs-progs: scrub: update the error message to reduce confusion
Qu Wenruo [Sat, 30 May 2026 01:07:52 +0000 (10:37 +0930)]
btrfs-progs: scrub: update the error message to reduce confusion

[BUG]
There is a bug report that scrub is reporting confusing and conflicting
messages:

  $ sudo btrfs scrub start -B /mnt
  Starting scrub on devid 1
  scrub done for 44cb181f-faf6-48c2-a462-904cfe493576
  Scrub started:    Fri May 29 23:54:53 2026
  Status:           finished
  Duration:         0:10:23
  Total to scrub:   911.45GiB
  Rate:             1.46GiB/s
  Error summary:    csum=18
    Corrected:      0
    Uncorrectable:  18
    Unverified:     0
  ERROR: there are 1 uncorrectable errors

There are 18 uncorrectable errors, but the final line only shows one.

[CAUSE]
The "1" is for the number of devices with uncorrectable errors, not the
total errors found in the device.

[FIX]
Change the error message to "there are 1 device(s) with uncorrectable
errors" instead.

Pull-request: #1127
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: fix a memory leak in __btrfs_map_block() after RST lookup failure
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

=================================================================
==48914==ERROR: LeakSanitizer: detected memory leaks

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>
4 months agobtrfs-progs: fix a memory leak in btrfs_load_block_group_zone_info()
Qu Wenruo [Thu, 11 Jun 2026 00:27:59 +0000 (09:57 +0930)]
btrfs-progs: fix a memory leak in btrfs_load_block_group_zone_info()

[BUG]
With the previous btrfs_zoned_device_info memory leak fixed, the test
case mkfs/025 still fails with extra memory leaks:

====== RUN CHECK /home/adam/btrfs-progs/mkfs.btrfs -d single -m single /dev/nullb0 /dev/nullb1 /dev/nullb2 /dev/nullb3 /dev/nullb4 /dev/nullb5 /dev/nullb6 /dev/nullb7 /dev/nullb8 /dev/nullb9

=================================================================
==4392==ERROR: LeakSanitizer: detected memory leaks

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.

[FIX]
Free @active bitmap inside btrfs_load_block_group_zone_info().

Pull-request: #1131
Fixes: 6936ffa265c7 ("btrfs-progs: zoned: activate block group on loading")
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: fix a memory leak in btrfs_prepare_device() for zoned devices
Qu Wenruo [Wed, 10 Jun 2026 23:09:03 +0000 (08:39 +0930)]
btrfs-progs: fix a memory leak in btrfs_prepare_device() for zoned devices

[BUG]
With D=asan compiling option enabled, the test case mkfs/025 fails with
the following asan report triggered:

====== RUN CHECK root_helper /home/adam/btrfs-progs/mkfs.btrfs -d single -m single /dev/nullb0 /dev/nullb1 /dev/nullb2 /dev/nullb3 /dev/nullb4 /dev/nullb5 /dev/nullb6 /dev/nullb7 /dev/nullb8 /dev/nullb9

=================================================================
==185235==ERROR: LeakSanitizer: detected memory leaks

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>
4 months agobtrfs-progs: use path_readlink in add_symbolic_link
David Sterba [Tue, 9 Jun 2026 19:28:48 +0000 (21:28 +0200)]
btrfs-progs: use path_readlink in add_symbolic_link

Use the path helper that handles all the corner cases of readlink().

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: add path helper for readlink
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.

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: ci: fixup Leap 16 build name reference
David Sterba [Tue, 9 Jun 2026 18:28:41 +0000 (20:28 +0200)]
btrfs-progs: ci: fixup Leap 16 build name reference

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>
4 months agobtrfs-progs: ci: mention Alpine in musl builds
David Sterba [Tue, 9 Jun 2026 18:22:48 +0000 (20:22 +0200)]
btrfs-progs: ci: mention Alpine in musl builds

Use full name of distro that has been providing us Musl based CI image
for years.

[ci skip]

Signed-off-by: David Sterba <dsterba@suse.com>