fstests.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] btrfs: test defrag --nocomp converts compressed extents to uncompressed
@ 2026-09-20 12:11 Dhruva G
  2026-09-20 21:45 ` Qu Wenruo
  0 siblings, 1 reply; 2+ messages in thread
From: Dhruva G @ 2026-09-20 12:11 UTC (permalink / raw)
  To: linux-btrfs, kernel-team, fstests
  Cc: Boris Burkov, Anand Jain, goledhruva, Qu Wenruo

"btrfs filesystem defragment --nocomp" rewrites already compressed file
data as uncompressed data. The kernel flag was added by commit
009b2056cb25 ("btrfs: defrag: add flag to force no-compression") in
v6.17, and the userspace option shipped in btrfs-progs v6.16. There is
no test for it.

Add a test that writes compressible data in 128K blocks on a filesystem
mounted with compress=zlib, runs defragment with --nocomp, and uses the
FIEMAP encoded flag to verify that no extent remains compressed. Also
verify that the file contents are unchanged after a remount. Keeping
compression enabled throughout the test confirms that the defrag ioctl
takes precedence over the mount option.

Regular defrag deliberately leaves maximum-sized compressed extents
alone, as asserted by btrfs/259. Creating maximum-sized compressed
extents therefore ensures that an implementation which silently treats
--nocomp as regular defrag fails the test.

Check support in two stages: first require btrfs-progs to advertise
--nocomp, then invoke it on a 10-byte file to verify that the kernel
accepts the flag. Kernels with the unknown-flag validation from commit
173431b274a9 return EOPNOTSUPP when NOCOMPRESS is unavailable. Kernels
predating that validation may silently ignore the flag and fail the
semantic checks instead of skipping.

Signed-off-by: Dhruva Gole <goledhruva@gmail.com>
---

Link to v1:
https://lore.kernel.org/linux-btrfs/d17a93e6-1613-4113-a75a-8ea89efb2bdb@gmail.com/

Changes since v1:
- Use FIEMAP's encoded flag instead of dump-tree to identify compressed
  extents.
- Write in 128K blocks so the test reliably creates maximum-sized compressed
  extents, even under memory pressure.
- Fold the initial sync into the xfs_io write command.

---

 tests/btrfs/354     | 75 +++++++++++++++++++++++++++++++++++++++++++++
 tests/btrfs/354.out |  2 ++
 2 files changed, 77 insertions(+)
 create mode 100755 tests/btrfs/354
 create mode 100644 tests/btrfs/354.out

diff --git a/tests/btrfs/354 b/tests/btrfs/354
new file mode 100755
index 00000000..8a19b7c3
--- /dev/null
+++ b/tests/btrfs/354
@@ -0,0 +1,75 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Meta Platforms, Inc.  All Rights Reserved.
+#
+# FS QA Test 354
+#
+# Make sure "btrfs filesystem defragment --nocomp" rewrites compressed extents
+# as uncompressed ones, even though the filesystem is mounted with compression
+# enabled, and without changing the file contents.
+#
+# Note that regular defrag deliberately skips compressed extents that are
+# already at their maximum size (see btrfs/259), so a broken --nocomp that
+# simply does nothing would go unnoticed without this test.
+#
+. ./common/preamble
+_begin_fstest auto quick defrag fiemap compress
+
+. ./common/filter
+. ./common/punch # for _filter_fiemap_flags
+
+# real QA test starts here
+
+_require_scratch
+_require_xfs_io_command "fiemap"
+_require_btrfs_command filesystem defragment --nocomp
+
+_wants_kernel_commit 009b2056cb25 \
+	"btrfs: defrag: add flag to force no-compression"
+
+_scratch_mkfs >> $seqres.full 2>&1 || _fail "mkfs failed"
+_scratch_mount -o compress=zlib
+
+# Probe kernel support separately from the userspace check above. Kernels
+# without the flag reject it with EOPNOTSUPP when unknown-flag validation is
+# available.
+$XFS_IO_PROG -f -c "pwrite 0 10" "$SCRATCH_MNT/probe" >> $seqres.full
+$BTRFS_UTIL_PROG filesystem defragment --nocomp "$SCRATCH_MNT/probe" \
+	>> $seqres.full 2>&1 || _notrun "defrag --nocomp not supported"
+
+# Default xfs_io pattern is highly compressible. Use a 128K block size so the
+# file lands as maximum-size compressed extents even under memory pressure,
+# and sync so that fiemap below observes stable extents.
+$XFS_IO_PROG -f -c "pwrite -b 128K 0 1m" -c "sync" \
+	"$SCRATCH_MNT/foo" >> $seqres.full
+
+# fiemap reports compressed extents with the ENCODED (0x8) flag, printed as
+# "encoded" by _filter_fiemap_flags (see btrfs/280, btrfs/281).
+$XFS_IO_PROG -c "fiemap -v" "$SCRATCH_MNT/foo" | \
+	_filter_fiemap_flags 1 > $tmp.before
+cat $tmp.before >> $seqres.full
+grep -q "encoded" $tmp.before || \
+	echo "file has no compressed extents before --nocomp defrag"
+
+csum_before=$(_md5_checksum "$SCRATCH_MNT/foo")
+
+$BTRFS_UTIL_PROG filesystem defragment --nocomp "$SCRATCH_MNT/foo" \
+	>> $seqres.full 2>&1
+
+# Should no longer be compressed, despite the compress mount option: no
+# extent of the file may still carry the ENCODED flag.
+$XFS_IO_PROG -c "fiemap -v" "$SCRATCH_MNT/foo" | \
+	_filter_fiemap_flags 1 > $tmp.after
+cat $tmp.after >> $seqres.full
+grep -q "encoded" $tmp.after && \
+	echo "file still has compressed extents after --nocomp defrag"
+
+# Contents must survive the rewrite, read back from disk and not page cache
+_scratch_cycle_mount "compress=zlib"
+csum_after=$(_md5_checksum "$SCRATCH_MNT/foo")
+[ "$csum_before" = "$csum_after" ] || echo "file content changed"
+
+echo "Silence is golden"
+
+# success, all done
+_exit 0
diff --git a/tests/btrfs/354.out b/tests/btrfs/354.out
new file mode 100644
index 00000000..8bc7ecf6
--- /dev/null
+++ b/tests/btrfs/354.out
@@ -0,0 +1,2 @@
+QA output created by 354
+Silence is golden
-- 
2.43.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] btrfs: test defrag --nocomp converts compressed extents to uncompressed
  2026-09-20 12:11 [PATCH v2] btrfs: test defrag --nocomp converts compressed extents to uncompressed Dhruva G
@ 2026-09-20 21:45 ` Qu Wenruo
  0 siblings, 0 replies; 2+ messages in thread
From: Qu Wenruo @ 2026-09-20 21:45 UTC (permalink / raw)
  To: Dhruva G, linux-btrfs, kernel-team, fstests; +Cc: Boris Burkov, Anand Jain



在 2026/9/20 21:41, Dhruva G 写道:
> "btrfs filesystem defragment --nocomp" rewrites already compressed file
> data as uncompressed data. The kernel flag was added by commit
> 009b2056cb25 ("btrfs: defrag: add flag to force no-compression") in
> v6.17, and the userspace option shipped in btrfs-progs v6.16. There is
> no test for it.
> 
> Add a test that writes compressible data in 128K blocks on a filesystem
> mounted with compress=zlib, runs defragment with --nocomp, and uses the
> FIEMAP encoded flag to verify that no extent remains compressed. Also
> verify that the file contents are unchanged after a remount. Keeping
> compression enabled throughout the test confirms that the defrag ioctl
> takes precedence over the mount option.
> 
> Regular defrag deliberately leaves maximum-sized compressed extents
> alone, as asserted by btrfs/259. Creating maximum-sized compressed
> extents therefore ensures that an implementation which silently treats
> --nocomp as regular defrag fails the test.
> 
> Check support in two stages: first require btrfs-progs to advertise
> --nocomp, then invoke it on a 10-byte file to verify that the kernel
> accepts the flag. Kernels with the unknown-flag validation from commit
> 173431b274a9 return EOPNOTSUPP when NOCOMPRESS is unavailable. Kernels
> predating that validation may silently ignore the flag and fail the
> semantic checks instead of skipping.
> 
> Signed-off-by: Dhruva Gole <goledhruva@gmail.com>

Looks good to me overall.

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-20 21:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-20 12:11 [PATCH v2] btrfs: test defrag --nocomp converts compressed extents to uncompressed Dhruva G
2026-09-20 21:45 ` Qu Wenruo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).