Zygo Blaxell [Sat, 31 Oct 2026 00:26:49 +0000 (20:26 -0400)]
btrfs-progs: inspect: add -z to logical-resolve and inode-resolve
The paths that logical-resolve and inode-resolve print are file names,
which may contain newlines, so a script cannot split the output into
paths reliably. Add -z (--zero): end each path with a NUL character
instead of a newline. With logical-resolve -P each inode line ends
with NUL instead. Errors still go to stderr.
No btrfs-progs command has such an option yet. The name follows the
coreutils commands that print file names, such as realpath, readlink,
dirname and basename (-z, --zero). -0 is used by commands that read
NUL-separated input (xargs -0) or for a few outputs (du -0, env -0),
and -z is free in both commands.
Add -z cases to misc-tests 042 (a path with a space, tab, newline and
backslash, and -P) and 074 (hard links, one of them with a newline and a
space in its name).
Zygo Blaxell [Fri, 30 Oct 2026 05:04:01 +0000 (01:04 -0400)]
btrfs-progs: scrub: keep the progress socket of a background scrub
A background scrub start creates and binds the progress socket, then
forks. The child scrubs and answers on the socket, but the parent
leaves through the common exit path, which closes the socket and
removes it from the file system. Nothing can connect to the child
after that: "btrfs scrub status" falls back to the status file, which
is written every 5 seconds, and the check for a running scrub in
another "btrfs scrub start -f" does not see the background scrub.
Close the parent's copy of the socket without removing it. The child
removes it when the scrub is finished.
Zygo Blaxell [Fri, 30 Oct 2026 05:04:01 +0000 (01:04 -0400)]
btrfs-progs: scrub: survive a progress client that closes early
scrub start answers each connection to its progress socket by writing
the current progress to it. A client that closes the connection without
reading makes that write raise SIGPIPE, which kills the scrub process
and with it the scrub. scrub start itself is such a client: when the
socket is in use, it connects to see whether a scrub is running and
closes the connection at once. So "btrfs scrub start -f" while another
"btrfs scrub start -B" runs on the same filesystem kills the first one,
and a scrub started with --limit then leaves the --limit value on the
devices. A write error to a client also ended the progress thread, so
the progress of the rest of the scrub was not recorded.
Block SIGPIPE in the progress thread, so the write fails with EPIPE
instead, and do not treat a failed write to a client as an error of the
scrub: close the connection and go on.
Zygo Blaxell [Fri, 30 Oct 2026 03:34:39 +0000 (23:34 -0400)]
btrfs-progs: inspect logical-resolve: fail on an unresolvable reference
When the subvolume of a reference cannot be opened, logical-resolve
prints "inode N subvol S could not be accessed: not mounted" to stdout
and exits with status 0, so a script that reads the paths sees fewer
paths, or none at all, and no error. The same goes for a path of an
inode that no mount reaches ("inode N subvol S path P could not be
accessed: not mounted"), in logical-resolve and inode-resolve.
Report them as errors on stderr and exit with status 1 once all the
other paths have been printed.
This changes the output: scripts that parsed the message from stdout
will no longer see it there.
Update misc-tests/042, which read the message from stdout and relied on
the exit status 0: accept exit status 1 only together with at least one
such error, and require the error whenever the status is 1. Also add
cases where bind mounts of directories are the only way in: a directory
of a subvolume with a file in it and a subvolume nested below it, and a
directory of that nested subvolume, each resolved with the bind mount
point, a directory in it, "." and an absolute path as the argument, and
checked to print the expected path relative to the argument, and a
file reached through the bind mount of its directory when the argument
is elsewhere, and a directory and mount points whose names have a space,
a tab, a newline and a backslash (escaped as octal in mountinfo) through
the argument, a bind mount and a subvolume mount. Update misc-tests/074
for the stderr message and exit status.
Zygo Blaxell [Fri, 30 Oct 2026 21:18:00 +0000 (17:18 -0400)]
btrfs-progs: inspect inode-resolve: print paths relative to the argument
INO_PATHS returns the paths of the inode relative to the root of the
subvolume of the file descriptor, and inode-resolve prints each of them
after the path argument. That is only right when the argument is the
root of the subvolume. For a directory inside the subvolume, a bind
mount of one, or a relative path from such a directory, the printed paths
repeat the part between the subvolume root and the argument, and do not
exist:
When the argument is a directory inside its subvolume, resolve the
paths with the routes logical-resolve uses for its references: print the
paths below the argument relative to it, and the others through the
mount of the subvolume or of an ancestor, or through a bind mount of a
directory they are below. A path that no mount reaches is reported as
not mounted, like in logical-resolve. When the argument is the
subvolume root nothing changes.
Add misc-tests/074-inspect-internal-inode-resolve: the subvolume root,
a directory in it, ".", a bind mount of a directory as the argument,
then hard links outside the argument, resolved through the mount of the
subvolume, through a bind mount of their directory, or not at all.
Link: https://github.com/kdave/btrfs-progs/issues/400 Fixes: 6055e736045d ("Btrfs-progs: added ioctls and commands to resolve inodes and logical addrs") Assisted-by: Claude:claude-opus-5-5
Zygo Blaxell [Fri, 30 Oct 2026 04:03:11 +0000 (00:03 -0400)]
btrfs-progs: scrub: restore the device limits on every exit
With --limit, scrub start writes the new limit to each device's
scrub_speed_max and writes the old value back once the device threads
have been joined. Every exit between the two skips the restore and
leaves the --limit value on the devices:
- "nothing to resume" from scrub resume;
- "scrub already running" from the progress socket check, reachable with
-f while another scrub runs, which also leaves the running scrub with
the new limit;
- a failed pthread_mutex_init() in the setup loop;
- a failed fork() of the background scrub;
- a failed pthread_create() of a device or the progress thread.
Record per device whether its limit was changed, and write the old value
back from the common exit path for those devices only, so a start that
fails partway leaves the devices it did not reach or could not change
alone. The parent of a background scrub forgets its changes, because
the child owns them and restores them when the scrub is finished.
Fixes: 7c1c88e41668 ("btrfs-progs: scrub: add the new --limit option to set the throughput limit at runtime") Assisted-by: Claude:claude-opus-5-5
Zygo Blaxell [Fri, 30 Oct 2026 03:34:26 +0000 (23:34 -0400)]
btrfs-progs: inspect logical-resolve: find subvolumes under any mount
Since v5.10, logical-resolve looks up a mount point whose mounted path
inside the filesystem is exactly the subvolume of each reference, by
comparing the subvol path and subvolid against /proc/self/mountinfo.
Subvolumes are usually not mounted individually but reached as
directories of the top level or of another subvolume, so nested
subvolumes and snapshots are reported as
inode 257 subvol sv1 could not be accessed: not mounted
even when the top level subvolume is mounted and is the path argument,
and the command still exits with status 0. Before v5.10 the same command
printed the file names, relative to the path argument. The lookup also
has two older problems:
- It accepts a mount of any btrfs filesystem that has a subvolume with
the same path and id (e.g. "/@home", 256, which is common), and calls
INO_PATHS there, which prints the names of unrelated files from the
wrong filesystem, or fails with ENOENT.
- For references in the top level subvolume (id 5) it uses the path
argument without checking which subvolume that is, so when the
argument is a subvol= mount or a subvolume directory INO_PATHS
resolves the inode number in the wrong subvolume, and prints a wrong
name or fails with "ino paths ioctl: No such file or directory".
Resolve the paths of each reference through the first of these routes
that reaches them:
1. Below the path argument: if the subvolume's path in the filesystem
is below the path of the argument, open it relative to the argument,
and print paths relative to the argument as given, like before v5.10
(so "." gives "./subvol/file").
2. Inside the path argument: if the argument is a directory of the
subvolume itself rather than its root (for example a bind mount of a
directory, or a directory in one), print the paths of the inode that
are below the argument, relative to it. The argument must be in the
subvolume (same root id; same filesystem by definition).
3. The mount of its deepest ancestor (or of itself): among btrfs mounts
whose mounted path is the subvolume's path or a prefix of it at a
path component boundary, the longest one that leads to the
subvolume, the first in mountinfo order among equals.
4. The mount of a directory inside the subvolume (a bind mount of a
directory), for the paths below that directory: the mount whose
directory is closest to the subvolume root, the first in mountinfo
order among equals. The mount must be in the subvolume (root id) on
the same filesystem (fsid), and the directory's place in the
subvolume is found from its inode with INO_LOOKUP, not from the
mountinfo text, so a renamed directory is found where it is now. A
deleted directory (link count 0) is skipped: until the transaction
commits INO_LOOKUP still returns its old path, which can be another
directory's by then.
Routes 1 and 3 reach every path of the inode; routes 2 and 4 only the
paths below their directory, so for those the routes apply to each path
(a hard link outside a bind mounted directory is printed through
another mount when there is one). A path that no route reaches is
reported as
inode 257 subvol sv1 path dir/file could not be accessed: not mounted
and a reference whose subvolume no route reaches at all as before.
In routes 1 and 3 the opened directory is checked to be the root of that
subvolume (inode 256, INO_LOOKUP root id) on the same filesystem (fsid
from FS_INFO) before INO_PATHS is called on it. That check also takes
care of the bind mount case that the mountinfo lookup was introduced
for: a bind mount of a directory inside a subvolume does not have the
subvolume's path as its mounted path, so it is not taken as the mount of
the subvolume; it is used by route 4 for the paths below it only.
find_mount_fsroot() and find_option() lose their only user; replace
them with iterate_btrfs_mounts(), which reports every btrfs mount and
leaves the matching to the caller.
Link: https://github.com/kdave/btrfs-progs/issues/400 Fixes: 6b8fed9e798d ("btrfs-progs: inspect: use find_mount_fsroot in logical-resolve") Fixes: 6055e736045d ("Btrfs-progs: added ioctls and commands to resolve inodes and logical addrs") Assisted-by: Claude:claude-opus-5-5
Zygo Blaxell [Fri, 30 Oct 2026 03:28:01 +0000 (23:28 -0400)]
btrfs-progs: utils: parse any number of optional fields in mountinfo
parse_mntinfo_line() skips exactly one token between the mount options
(field 5) and the "-" separator, but the optional fields in between are a
list of zero or more entries. A mount with private propagation (e.g. in a
container or after "unshare -m --propagation private") has none, and the
parser then reads the device name as the filesystem type; a slave mount
that is also shared ("shared:N master:M") has two, and the parser reads
the "-" as the filesystem type. Either way the line is not recognized as
a btrfs mount and find_mount_fsroot() never finds it, so for instance
"btrfs inspect-internal logical-resolve" reports every subvolume as "not
mounted".
Skip optional fields up to the lone hyphen instead. libmount, which
find_mount_fsroot() used before, did this correctly.
Zygo Blaxell [Fri, 30 Oct 2026 03:18:07 +0000 (23:18 -0400)]
btrfs-progs: scrub: only change the device limits when --limit is given
Since the --limit option was added, scrub start reads every device's
scrub_speed_max from sysfs and writes the --limit value to it before
the scrub, then writes the old value back afterwards. It does this
whether or not --limit was given. Without --limit the value written is
0, so a plain "btrfs scrub start" runs unthrottled on every device and
ignores limits set with "btrfs scrub limit" or through sysfs.
Until 01c8277d ("btrfs-progs: scrub: only restore the old scrub limit
after scrub is finished", Issue #1165) the old value was written back
right before joining each device's thread, so devid 1 got its limit
back almost at once and was throttled, but every other device ran at
full speed until the devices before it had finished. With a 32 MiB/s
limit on both devices of a two-device filesystem, v6.14 scrubbed devid 1
at 33 MiB/s and devid 2 at 400 MiB/s. 01c8277d moved the restore after
all the joins, so now no device is throttled during a scrub without
--limit.
Read, write and restore the limits only when --limit is given. Without
it, the scrub runs with whatever limits the devices already have.
Link: https://github.com/kdave/btrfs-progs/issues/1165 Fixes: 7c1c88e41668 ("btrfs-progs: scrub: add the new --limit option to set the throughput limit at runtime") Assisted-by: Claude:claude-opus-5-5
Qu Wenruo [Tue, 27 Oct 2026 09:34:50 +0000 (20:04 +1030)]
btrfs-progs: remove set but unused variables
If using clang to compile btrfs-progs, two warnings will be reported:
cmds/rescue-fix-data-checksum.c:73:12: warning: variable 'global_repair_mode' set but not used [-Wunused-but-set-global]
73 | static int global_repair_mode;
| ^
1 warning generated.
cmds/inspect-tree-stats.c:43:12: warning: variable 'verbose' set but not used [-Wunused-but-set-global]
43 | static int verbose = 0;
| ^
1 warning generated.
Both are correct reports, so just remove them.
Reviewed-by: Daniel Vacek <neelx@suse.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
Qu Wenruo [Sun, 25 Oct 2026 08:41:45 +0000 (19:11 +1030)]
btrfs-progs: image: reject incorrect super block size
For an uncompressed image, if the super block size is smaller than
BTRFS_SUPER_INFO_SIZE, we know it's definitely corrupted, reject it or
later code can read beyond the allocated buffer.
This covers the following cases:
- build_chunk_tree()
* Validate the compression level
* Check the uncompressed super block size
* Handle short decompression
- add_cluster()
* Validate the compression level
- fill_mdres_info()
if build_chunk_tree() is skipped, we read super block here.
* Check the uncompressed super block size
* Call btrfs_check_super() to validate the super block
* Handle short decompression
Assisted-by: LLM (exposing the bug) Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
Qu Wenruo [Sun, 25 Oct 2026 08:41:40 +0000 (19:11 +1030)]
btrfs-progs: image: avoid NULL-ptr-deref in build_chunk_tree()
Although very unlikely, if there is a header that has no item in it,
build_chunk_tree() left @item as NULL, then later during the error
message we still call "get_unaligned_le64(&item->bytenr)", which is
going to trigger a NULL-pointer dereference.
Fix it by removing the get_unaligned_le64() call.
Assisted-by: LLM (exposing the bug) Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
Qu Wenruo [Sun, 25 Oct 2026 08:41:38 +0000 (19:11 +1030)]
btrfs-progs: image: do not cleanup the thread that failed to be created
Inside metadump_init(), we initialize every thread slot, but when one
failed, we need to cleanup the previously created one.
However the existing code tries to clean up the slot that failed the
pthread_create() call.
This means in metadump_destroy() we call pthread_join() on the slot where
it's still all zero.
Fix the error handling by only releasing the allocated slots.
Assisted-by: LLM (exposing the bug) Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
Qu Wenruo [Sun, 25 Oct 2026 08:41:37 +0000 (19:11 +1030)]
btrfs-progs: image: cleanup pending ordered works after an error
The function flush_pending() can queue at most ITEMS_PER_CLUSTER
async_works before submitting them to write_buffers().
But if we hit an critical error, e.g. read_tree_block() hit a bad tree
block, and there is already pending async_works, we rely on the last
flush_pending() call inside create_metadump() to do the cleanup.
However that final flush_pending() will hit the same corrupted tree
block and fail halfway, without really dispatch the existing async
works.
Fix the problem by:
- Avoid calling the final flush_pending() if we had already hit an error
- Manually cleanup any pending async works inside metadump_destroy()
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
Qu Wenruo [Sun, 25 Oct 2026 08:41:36 +0000 (19:11 +1030)]
btrfs-progs: image: only remove a chunk map from physical tree if non-empty
During read_chunk_tree_block(), if a chunk is already in the physical
tree, then the fs_chunk is only added to overlapping_chunks list, but
not yet inserted into the physical_tree, since inserting it would
conflict with the existing chunk
Then remap_overlapping_chunks() remap the chunk into a new location, and
re-insert the updated fs_chunk into physical tree.
However if some error happened before remap_overlapping_chunks() is
called, one fs_chunk can be linked to overlapping_chunks list, but its
rbtree entry is not properly initialized (all zero), then
mdrestore_destroy() still calls rb_erase() on the uninitialized entry.
Fix the problem by always clearing the allocated fs_chunk->p.
Then at mdrestore_destroy() only call rb_erase() if the rb tree pointer
is not empty.
Assisted-by: LLM (exposing the bug) Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
Qu Wenruo [Sun, 25 Oct 2026 08:41:35 +0000 (19:11 +1030)]
btrfs-progs: image: destroy chunk tree after all pthreads finished
In the function mdrestore_destroy(), the chunk tree is destroyed before
all the pthreads finished.
This can lead to use-after-free problems for the chunk tree.
Fix it by waiting for all pthreads before destroy the chunk tree.
Since we're here, also fix the mdrestore_destroy() call inside
mdrestore_init() where if an error happened, we wait for a thread that
failed to be created.
Assisted-by: LLM (exposing the bug) Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
Qu Wenruo [Thu, 15 Oct 2026 00:25:02 +0000 (10:55 +1030)]
btrfs-progs: receive: fix and enhance inlined lzo decompression
[BUG]
There is a bug report of heap memory overflow during receive if
--force-decompress is utilized:
=================================================================
==557235==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x5020000000c0 at pc 0x5c38d895e8b9 bp 0x7ffdaefad510 sp 0x7ffdaefad500
WRITE of size 1 at 0x5020000000c0 thread T0
#0 0x5c38d895e8b8 in lzo1x_decompress_safe /tmp/tmp.aIYhV2VAge/lzo-2.10/minilzo/minilzo.c:6313
#1 0x5c38d88623cb in decompress_lzo cmds/receive.c:1165
#2 0x5c38d886274f in decompress_and_write cmds/receive.c:1222
#3 0x5c38d8863459 in process_encoded_write cmds/receive.c:1320
#4 0x5c38d8785daf in read_and_process_cmd common/send-stream.c:505
#5 0x5c38d878a16f in btrfs_read_and_process_send_stream common/send-stream.c:654
#6 0x5c38d88650fc in do_receive cmds/receive.c:1589
#7 0x5c38d88664ab in cmd_receive cmds/receive.c:1827
#8 0x5c38d869729b in cmd_execute cmds/commands.h:126
#9 0x5c38d8698a6b in main /home/kevinv/btrfs-progs/btrfs.c:474
#10 0x72779f02a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#11 0x72779f02a28a in __libc_start_main_impl ../csu/libc-start.c:360
#12 0x5c38d8697184 in _start (/home/kevinv/btrfs-progs/btrfs+0x89184) (BuildId: a2ab208cbd7ebc744934a9ed7a8963a20b35fcf4)
0x5020000000c0 is located 0 bytes after 16-byte region [0x5020000000b0,0x5020000000c0)
allocated by thread T0 here:
#0 0x72779f4fd340 in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:77
#1 0x5c38d8862634 in decompress_and_write cmds/receive.c:1191
#2 0x5c38d8863459 in process_encoded_write cmds/receive.c:1320
#3 0x5c38d8785daf in read_and_process_cmd common/send-stream.c:505
#4 0x5c38d878a16f in btrfs_read_and_process_send_stream common/send-stream.c:654
#5 0x5c38d88650fc in do_receive cmds/receive.c:1589
#6 0x5c38d88664ab in cmd_receive cmds/receive.c:1827
#7 0x5c38d869729b in cmd_execute cmds/commands.h:126
#8 0x5c38d8698a6b in main /home/kevinv/btrfs-progs/btrfs.c:474
#9 0x72779f02a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#10 0x72779f02a28a in __libc_start_main_impl ../csu/libc-start.c:360
#11 0x5c38d8697184 in _start (/home/kevinv/btrfs-progs/btrfs+0x89184) (BuildId: a2ab208cbd7ebc744934a9ed7a8963a20b35fcf4)
[CAUSE]
During lzo decompression, btrfs-progs always assume that the output
buffer is block aligned, even for inlined extents.
However since kernel commit 3eaf5f082c4c ("btrfs: extract inlined
creation into a dedicated delalloc helper"), btrfs will create inlined
compressed extents with tailing zero included.
For zlib and zstd it's fine as they provide multi-shot APIs, if the
output is exhausted, zlib/zstd doesn't error out, allowing us to
gracefully end the decompression.
But LZO has no multi-shot API, thus we have to ensure the output is
large enough to contain one fs block.
For inlined case, the output buffer contains only i_size bytes, which
can be smaller than a block.
This makes inline lzo extents decompression to write the full block into
the output buffer, overflow the heap.
[FIX]
Follow the kernel by introducing a dedicated inline decompression path,
and do extra validation for inlined decompression.
There is also a similar bug in the regular path, that crafted stream can
intentionally create a segment that doesn't take a full block.
In that case we still advance output buffer by blocksize, which can
cause the last block to overflow the buffer.
Also fix that by always limiting the output buffer size comparing to
@unencoded_len.
Issue: #1184 Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
Qu Wenruo [Mon, 12 Oct 2026 09:10:06 +0000 (19:40 +1030)]
btrfs-progs: slightly loosen the balance reject condition
Since commit dde71e2be9bd ("btrfs-progs: reject writes if the fs has
running replace/balance"), btrfs-progs will reject any writable
operation to the fs if there is a balance item in the root tree.
However there is a report that such rejection is too strict, as the user
had a corruption in the extent tree only, for which no reloc tree should
exist, and the corruption can be repaired.
Furthermore, the reloc tree in that image has refs 0, meaning it has
already been merged, thus it's safe to modify the fs.
Instead of the overly strict balance item checks, do a kernel-like reloc
tree search, and only reject the write if there is a valid reloc tree
whose refs are not 0.
If there is no such valid reloc tree, still allow writable operations.
Issue: #1182 Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
Device replace counts blocks that cannot be read from any copy and goes
on without them. That suits rescuing data from a failing device, but
not replacing a healthy one, where an unreadable block should stop the
replace before the source device is removed.
Add "btrfs replace start --abort-on-uncorrectable", which sets
BTRFS_IOCTL_DEV_REPLACE_ABORT_ON_UNCORRECTABLE in the high bits of
cont_reading_from_srcdev_mode. Kernels that do not support it reject
the start with EINVAL (or EOPNOTSUPP for a flag they do not know), and
say so when the replace runs in the foreground; an aborted replace fails
with EIO. dump-tree prints the flag in the dev_replace item's mode.
The kernel side is the patch "btrfs: dev-replace: optionally abort on
uncorrectable read errors" (kernel replace-fixes series), which defines
the flag and keeps it in the dev_replace item.
Zygo Blaxell [Tue, 13 Oct 2026 07:18:57 +0000 (03:18 -0400)]
btrfs-progs: do not write a NULL remap root during mkfs
create_metadata_block_groups() commits before setup_remap_tree_root()
has created the remap tree, and write_ctree_super() dereferenced
fs_info->remap_root->node unconditionally: mkfs -O remap-tree segfaulted.
Leave the superblock remap root fields alone until the node exists.
Zygo Blaxell [Fri, 16 Oct 2026 08:03:07 +0000 (04:03 -0400)]
btrfs-progs: print-tree: print the stop time of a dev-replace item
print_dev_replace_item() passed btrfs_dev_replace_time_started() to both
the "start time" and the "stop time" lines, so dump-tree showed the
start time as the stop time of a finished or canceled replace.
Fixes: 6ad89f67a9ac ("btrfs-progs: print-tree: add support for dev-replace item") Assisted-by: Claude:claude-opus-5-5
Qu Wenruo [Mon, 5 Oct 2026 08:26:17 +0000 (18:56 +1030)]
btrfs-progs: fsck-tests: add a new test case for qgroup tree check
The workload is straight forward:
- Create a btrfs with qgroup full mode
- Create 512 subvolumes
This should cause at least 2 leaves, even using 64K nodesize.
- Create qgroup 1/1 and assign 0/5 to it
- Run btrfs-check on the fs
Unpatched progs will load the last leaf first, add the qgroup relation
item without loading the qgroups in the previous leaf.
Qu Wenruo [Mon, 5 Oct 2026 08:13:53 +0000 (18:43 +1030)]
btrfs-progs: check: refactor load_quota_info()
Modernize that function by:
- Use btrfs_key when possible
The structure btrfs_disk_key is only for on-disk usage, every
in-memory operation is based on btrfs_key.
This includes the function alloc_count(), which now also accepts
btrfs_key as a parameter.
- Enhance the error handling
The path is not released when we hit an error.
- Use an dedicated helper to scan one leaf
Instead of nested loops, which is bug-prone for continue/break calls.
- Remove the unnecessary double loop
There is a local variable @search_relations, which has caused a long
existing bug before.
The idea is to skip all the QGROUP_LIMIT items, but we can skip them
very quickly, and it's way better for one's mental health just to do a
full tree iteration.
Qu Wenruo [Mon, 5 Oct 2026 07:52:08 +0000 (18:22 +1030)]
btrfs-progs: check: fix the inverted condition to load qgroups
[BUG]
When a qgroup tree is higher than level 0 and has a qgroup relation
between the first subvolume and a higher level qgroup, the btrfs check
can failed to load qgroup tree.
A minimal reproducer looks like the following:
mkfs.btrfs -f -K -n 4k $dev > /dev/null
mount $dev $mnt
btrfs quota enable "$mnt"
btrfs quota rescan -w $mnt
for i in $(seq 1 32); do
btrfs subvolume create "$mnt/s$i" >/dev/null
done
id=$(btrfs subvolume list "$mnt" | awk 'NR==1 {print $2}')
btrfs qgroup create 1/1 "$mnt"
btrfs qgroup assign "0/$id" 1/1 "$mnt"
umount $mnt
btrfs check -Q $dev
The failure looks like:
Opening filesystem to check...
Checking filesystem on /dev/test/scratch1
UUID: fbe3bc62-168a-4fec-8ef5-ba5f92d8c22c
Print quota groups report for /dev/test/scratch1
UUID: fbe3bc62-168a-4fec-8ef5-ba5f92d8c22c
ERROR: failed to add qgroup relation, member=256 parent=281474976710657: No such file or directory
ERROR: loading qgroups from disk: -2
extent buffer leak: start 30789632 len 4096
extent buffer leak: start 30793728 len 4096
[CAUSE]
With extra debug output, the following one line shows the problem pretty
well:
That's for the first loop of load_quota_info(), which should start from
the first leaf and load all qgroups into memory.
However the search key is not (0 0 0), it's the inverted condition
initializing the search key cause btrfs-check to grab the leaf with
qgroup relation items, not the first leaf.
Thus btrfs-check failed to load the qgroups in earlier leaves, causing
the above failure.
[FIX]
The code is not that well written, but as a hot fix just revert the
condition to fix the bug.
btrfs-progs: scrub: only restore the old scrub limit after scrub is finished
[BUG]
There is a bug report that the --limit option of `btrfs scrub start`
doesn't properly apply for all devices.
[CAUSE]
It turns out that the timing we restore the scrub limit is too early:
scrub_start()
|- read_scrub_device_limit()
|- write_scrub_device_limit()
| The old limit and new limit are written before we start scrub.
|
|- pthread_create()
| Now we start threads to do the real scrub ioctl for each device
|
|- write_scrub_device_limit()
| Now we already restore the old throughput for the first device.
|
|- pthread_join()
We only wait for the scrub to finish after we have restored the old
limit, which normally is no limit.
[FIX]
Move the old limit restore code after we have waited for all scrub
threads.
Issue: #1165 Fixes: 7c1c88e41668 ("btrfs-progs: scrub: add the new --limit option to set the throughput limit at runtime") Signed-off-by: Qu Wenruo <wqu@suse.com>
Andrus Suvalau [Wed, 30 Sep 2026 10:58:20 +0000 (12:58 +0200)]
btrfs-progs: tests: fix typo in 044-rootdir-hardlink-size
Fix the error that occurs during mkfs test execution:
[TEST/mkfs] 044-rootdir-hardlink-size
./test.sh: line 13: check_glolbal_prereq: command not found
Signed-off-by: Andrus Suvalau <andrus.suvalau@n-able.com>
Add tests/reproducibility-check.sh, a standalone root-required test that
checks mkfs.btrfs produces byte-identical images from identical inputs.
For a matrix of environment variations it runs mkfs twice and cmp's the
raw images:
- baseline, wall-clock time, TZ, locale, umask and working directory,
for both empty and --rootdir images;
- source uid, source filesystem (the same tree on loop-mounted ext4 vs
xfs), and compression (lzo/zlib/zstd), for --rootdir only.
Each run uses the documented recipe: a fixed -U, SOURCE_DATE_EPOCH and
DETERMINISTIC_SEED=1, and no --device-uuid, so the matrix also confirms
mkfs derives the device UUID from the fs UUID on its own. The source
tree is chown'd to 0:0 first (the one piece left to the caller) and mkfs
normalizes timestamps itself. The ext4 mount's lost+found is stripped so
the ext4 and xfs trees match, and two locales are probed at startup so
the locale case compares two real locales or is skipped.
Root is required for the ownership and loop-mounted source-filesystem
variations. With the preceding patches, all variations pass.
Leo Martins [Wed, 3 Jun 2026 03:58:01 +0000 (20:58 -0700)]
btrfs-progs: docs: document SOURCE_DATE_EPOCH and DETERMINISTIC_SEED
Add an ENVIRONMENT section to mkfs.btrfs(8) for the two environment
variables that make image creation reproducible: SOURCE_DATE_EPOCH
(timestamps) and DETERMINISTIC_SEED (UUID derivation, requires -U). It
covers the empty-is-unset and invalid-is-an-error handling, the minimal
recipe, and that normalizing ownership (chown -R 0:0) of a -r tree is
left to the caller.
Leo Martins [Thu, 28 May 2026 21:44:48 +0000 (14:44 -0700)]
btrfs-progs: mkfs: add a sorted directory walker for reproducible -r
mkfs.btrfs -r walks the source tree with nftw(), which returns entries in
the source filesystem's readdir() order. That order comes from the
filesystem, not the tree: ext4 uses a name hash with a per-fs seed, xfs a
b+tree key order, tmpfs and btrfs insertion order. Discovery order then
decides inode numbers, dir_index values, extent bytenrs, and which
hardlink owns the data, so the same tree staged on two different
filesystems produces two different images.
Add path_sorted_walk() in common/path-utils, which sorts each directory's
entries by name (byte-wise strcmp, not the locale-dependent strcoll)
before recursing. It keeps nftw()'s callback signature and
preorder/FTW_PHYS semantics. ftw_add_inode() still needs a NULL-stat
guard: the walker reports a failed lstat() as FTW_NS with a NULL stat
where glibc nftw() passes a buffer, so the callback skips it instead of
dereferencing. The walker also matches
the two nftw() edges the callbacks depend on: an unreadable directory is
reported once as FTW_DNR, and errno is checked after the readdir() loop
since readdir() returns NULL for both end-of-stream and error. The child
path is heap-allocated so a deep tree doesn't overflow the stack.
A sorted traversal means reading a whole directory up front plus the qsort
and allocations that go with it. A regular mkfs doesn't need that, so keep
nftw() as the default and only sort when a reproducible image is actually
requested, i.e. when both SOURCE_DATE_EPOCH and DETERMINISTIC_SEED are set
(reproducible_is_enabled()). Setting just one can't produce a reproducible
image anyway, since the other source of nondeterminism (host time, or
random UUIDs) still leaks in. btrfs_mkfs_size_dir() stays on nftw()
regardless; it only sums sizes and counts inodes, so walk order doesn't
matter there.
The same reproducible mode sorts each inode's xattrs by name in
add_xattr_item(), the xattr counterpart of sorting directory entries. It
only matters when two names collide under btrfs_name_hash() and share a
dir_item stored in insertion order, but sorting removes the dependence.
Leo Martins [Thu, 28 May 2026 21:35:35 +0000 (14:35 -0700)]
btrfs-progs: mkfs: honor SOURCE_DATE_EPOCH for -r source-file timestamps
With `mkfs.btrfs -r`, stat_to_inode_item() copies each source inode's
atime, ctime and mtime from the host, so the image is not reproducible
even when the source tree is otherwise normalized. When
SOURCE_DATE_EPOCH is set, pin ctime and atime to it and clamp mtime to
it. When it is unset or empty, use the host timestamps as before.
The timestamps need different handling:
- ctime cannot be set through the filesystem API; the kernel updates
it after any inode change. mkfs writes the raw inode, so pin it to
the epoch there.
- atime is not stable source metadata. On a relatime filesystem, the
first mkfs can record an old atime and advance it while reading the
source, then a second mkfs records a different value. Pin it to the
epoch so reads cannot change the image.
- mtime remains meaningful source metadata. Preserve values older
than the epoch and clamp newer values, so nothing in the image is
newer than the source date.
Add a regression using a relatime source. The first build advances the
source atime. The test builds again, compares the raw images, and mounts
the result to verify that atime and ctime equal the epoch while
the older mtime is preserved.
Factor the "is SOURCE_DATE_EPOCH set" test into
reproducible_has_source_date() so it reads the same everywhere it is
needed.
uid/gid remain the caller's responsibility (chown -R 0:0): one line of
shell, where a --owner flag would be permanent CLI surface.
Leo Martins [Thu, 28 May 2026 20:01:33 +0000 (13:01 -0700)]
btrfs-progs: mkfs: stop leaking host CPU count and page size
Two host attributes leak into the on-disk image, so the same arguments
produce different output on different build machines.
1. nr_global_roots defaulted to sysconf(_SC_NPROCESSORS_ONLN). With
extent-tree-v2 this is written to the super block
(btrfs_set_super_nr_global_roots) and sets how many global roots
are created, so the host CPU count ends up in the image. Default
to 1; --num-global-roots still overrides it. extent-tree-v2 is
experimental, but the leak is real today.
2. BTRFS_FEATURE_INCOMPAT_BIG_METADATA was set from
sysconf(_SC_PAGE_SIZE): with the default 16K nodesize it was on for
4K-page hosts and off for 16K-page hosts (aarch64). The flag is not
a default feature and not -O-toggleable, so this is its only
setter; the kernel sets it on any modern filesystem anyway, and
only kernels older than Linux 3.4 (2012) lack it. Set it
unconditionally, which drops the page-size dependency and also
covers the 4K-nodesize case the old gate skipped.
Neither change is gated by DETERMINISTIC_SEED; both fix cross-host
consistency regardless of reproducibility intent.
Leo Martins [Thu, 28 May 2026 23:33:11 +0000 (16:33 -0700)]
btrfs-progs: mkfs: derive UUIDs deterministically from the fs UUID
mkfs generates several internal UUIDs with uuid_generate(): every
device's dev_item.uuid, the chunk-tree UUID, the FS_TREE subvolume UUID,
and the per-subvolume UUIDs from the uuid-tree rebuild. All are random,
so even with -U and SOURCE_DATE_EPOCH pinned, two runs with identical
inputs differ. uuid_generate() reads the system CSPRNG and cannot be
seeded, so it cannot be made deterministic in place.
When DETERMINISTIC_SEED=1 is set and a fixed UUID is given with -U (the
xfsprogs convention), derive these UUIDs from the fs UUID. A plain copy
of the seed will not do: an image holds several UUIDs that must stay
distinct on disk, so each is derived from a (role, key) pair in the
fs-UUID namespace with libuuid's name-based (v5) generator:
role is an enum (chunk tree, subvolume, device) and key distinguishes
instances within a role (subvolume objectid, devid; ignored for the
chunk-tree singleton). The name is serialized little-endian so the
result is the same on any host architecture.
uuid_generate_sha1() has been in libuuid since util-linux 2.31 (2017),
so configure.ac now requires uuid >= 2.31, like the version floors
already set for libzstd, libgcrypt and others.
Two helpers join reproducible_now() in common/reproducible.{c,h}:
reproducible_is_deterministic() checks DETERMINISTIC_SEED == "1", and
reproducible_uuid_generate() derives the UUID in deterministic mode and
otherwise falls back to uuid_generate(). mkfs errors out if
DETERMINISTIC_SEED=1 is set without -U, since there is no seed to derive
from.
Converted call sites:
- mkfs/common.c btrfs_create_tree_root(): FS_TREE subvolume UUID.
- mkfs/common.c make_btrfs(): chunk_tree_uuid and device 1's
dev_item.uuid (used when --device-uuid is not given).
- common/device-scan.c btrfs_add_to_fsid(): per-device UUID for
devices 2..N.
- common/root-tree-utils.c rescan_subvol_uuid(): per-subvolume UUID
during the uuid-tree rebuild.
Only super.fsid is not derived; it is the seed, from -U (or random when
-U is absent). A user-supplied --device-uuid still overrides device 1
as before.
Leo Martins [Thu, 28 May 2026 23:32:20 +0000 (16:32 -0700)]
btrfs-progs: mkfs: honor SOURCE_DATE_EPOCH for root-item timestamps
mkfs stamps root-item timestamps with time(NULL), so every run embeds
the current wall clock and no two runs are bit-identical. Honor
SOURCE_DATE_EPOCH when it is set in the environment, the standard
reproducible-builds convention.
Add reproducible_now() in common/reproducible.{c,h}, which parses the
value with parse_u64(). An empty value means unset, per the spec. An
invalid value is rejected with an error and a non-zero exit instead of
falling back, so a typo does not silently produce a non-reproducible
image.
The helper goes in common/ because it is also called from
common/root-tree-utils.c, which is shared with other tools; later
patches add more reproducibility helpers next to it.
Convert the time(NULL) call sites:
- mkfs/common.c btrfs_create_tree_root(): FS_TREE root_item ctime and
otime.
- common/root-tree-utils.c btrfs_make_root_dir(): atime/ctime/mtime/
otime of the top inode of every root it creates (root_tree_dir,
fs_tree, each -u subvolume, the data-reloc tree).
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.