From 420b87ed47953436d52528ef49be054222d039b7 Mon Sep 17 00:00:00 2001
From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
Date: Sat, 31 Oct 2026 01:03:59 -0400
-Subject: [PATCH 0/5] btrfs: fix and simplify inode flag vs compression interactions
+Subject: [PATCH 0/11] btrfs: fix and simplify inode flag vs compression interactions
To: linux-btrfs@vger.kernel.org
This series fixes problems in the interaction between btrfs inode flags
-and compression properties.
+and compression properties. Since 2026-11-03 (ck1103) it is built on
+koraynilay's "[PATCH v6 RESEND 0/6] btrfs: add per-inode compression
+levels in xattrs" (Message-ID
+<20261103194734.598182-1-koray.fra@gmail.com>), which replaces our own
+per-inode level and strict-validation patches; what ours did beyond it
+is in patches 10 and 11. The series is not to be sent as is: patches
+2-7 are other people's, here so that ours apply and test on top.
(An earlier revision carried a first patch, "preserve btrfs.compression
when setting inode flags", against the bug that any FS_IOC_SETFLAGS
operation overwrote an explicitly set btrfs.compression property with
the mount default. Sam Ho's f9926f294e4e ("btrfs: preserve the
compression property when other inode flags change") fixes the same
-bug and is upstream, so that patch is dropped; patch 4 below is built
-on it.)
+bug and is upstream; koraynilay's patch 7 extends it to keep the level.)
-Patch 1 fixes four regressions introduced by commit f37c563bab42
+Patch 1 is Filipe Manana's "btrfs: dont propagate nodatacow mount
+option on files with inherited compression" (Reviewed-by: Qu Wenruo), as
+posted. It is already in btrfs/for-next (75d0e4dd64f2a) but not in this
+series' base; "git am --skip" drops it. A file created in a directory
+with chattr +c on a filesystem mounted with -o nodatacow inherited
+COMPRESS from the directory and got NODATACOW from the mount option.
+The inherited COMPRESS now wins: the file is not given NODATACOW or
+NODATASUM, and its data is compressed. It replaces a patch of our own
+that did the opposite. That patch also dropped COMPRESS when the parent
+directory itself already carries both flags (only an old kernel could
+set that); that part is gone with it and has no replacement, so such a
+directory still passes both flags to new files, as upstream does.
+Patch 8 keeps those files' flags editable.
+
+Patches 2-7 are koraynilay's v6 as posted (Link: tags added): the
+":level" suffix of btrfs.compression becomes effective, is validated
+strictly on setxattr, is inherited, and survives FS_IOC_SETFLAGS. A
+property without a level uses the mount option's level only when the
+mount option names the same algorithm, otherwise the algorithm's default
+level (with -o compress=zstd:15, "zlib" now compresses at zlib's default,
+not at zlib:9).
+
+Patch 8 fixes four regressions introduced by commit f37c563bab42
("btrfs: add missing check for nocow and compression inode flags").
The conflict check in that commit was too strict and inconsistent:
supposedly forbidden combinations of flags were still allowed, some flag
could only be changed if split across different ioctls, and inodes created
on older kernels with conflicting flags set would reject changes to almost
any inode attribute, even the ones not related to compression or datacow.
+Its content is unchanged; only its context moved over patch 7.
-Patch 2 is Filipe Manana's "btrfs: dont propagate nodatacow mount
-option on files with inherited compression" (Reviewed-by: Qu Wenruo), as
-posted. It is already in btrfs/for-next (75d0e4dd64f2a) but not in this
-series' base; "git am --skip" drops it. It closes the other way to get
-the combination patch 1 refuses: inode creation. A file created in a
-directory with chattr +c on a filesystem mounted with -o nodatacow
-inherited COMPRESS from the directory and got NODATACOW from the mount
-option. The inherited COMPRESS now wins: the file is not given
-NODATACOW or NODATASUM, and its data is compressed. It replaces a patch
-of our own that did the opposite (dropped COMPRESS and kept NODATACOW).
-That patch also dropped COMPRESS when the parent directory itself
-already carries both flags (only an old kernel could set that); that
-part is gone with it and has no replacement, so such a directory still
-passes both flags to new files, as upstream does. Patch 1 keeps those
-files' flags editable.
-
-Patch 3 removes the artificial conflict between NODATACOW and NOCOMPRESS,
+Patch 9 removes the artificial conflict between NODATACOW and NOCOMPRESS,
introduced by f37c563bab42 and extended by 0e852ab8974c
("btrfs: do not allow compression on nodatacow files"). There is no
technical justification for rejecting this combination: NODATACOW files
cannot be compressed regardless, so the presence of a NOCOMPRESS bit
-makes no difference in behavior. I have kept this change separate from
-patch 1 in case there is a justification that I am not aware of; if one
-exists, patch 3 can be dropped independently.
-
-Patch 4 adds support for per-inode compression levels. Currently it is
-possible to add a ":5" suffix to a btrfs.compression xattr, but the
-suffix is ignored. This change parses the suffix the same way as the
-mount option. With f9926f294e4e, it retains both compression type and
-level when the fsattr is modified.
-
-Patch 5 adds stricter validation of the btrfs.compression xattr. It does
-not affect current xattr values in existing filesystems, but it rejects
-setting any new xattr values that cannot currently be completely parsed.
-This will introduce incompatible changes in use cases like
+makes no difference in behavior. On top of patch 3 the check moves into
+the valid-algorithm branch of prop_compression_validate(), after
+koraynilay's NUL-terminated copy, so "no"/"none" stay allowed on NOCOW
+inodes. It can be dropped independently if there is a reason for the
+restriction that I am not aware of.
+
+Patch 10 rejects btrfs.compression values with embedded NUL bytes.
+Patch 3 validates a NUL-terminated copy of the value, so "zstd:3"
+followed by a NUL and junk passed validation and was stored verbatim.
+
+Patch 11 keeps compressing inodes whose btrfs.compression value was
+stored by an older kernel that matched only the algorithm-name prefix
+("zstd:banana", "zstd:", "zstd:3x", "zstdgarbage", values longer than
+16 bytes). With patches 3-4 alone such inodes stop compressing at all,
+with an "error applying prop" warning every time they are loaded. The
+apply hook now matches the algorithm by prefix again and treats an
+unparseable level as unspecified; setxattr validation stays strict.
+
+Strict validation is a user-visible change: new kernels reject values
+that old kernels accepted and ignored, so for example
`ssh old-kernel-host btrfs send ... | ssh new-kernel-host btrfs receive ...`
-because new kernels will reject garbage that old kernels would have
-accepted. I have put this change in a separate patch so it can be
-dropped cleanly if this behavior change is considered undesirable.
+fails on such a value. Patch 11 makes sure values already on disk keep
+working.
Testing (KASAN kernels; details in TESTING.md next to these patches):
-compress-props-test.sh (one FS_IOC_SETFLAGS call per flag case, the three
-regressions of patch 1, strict property values, and the inheritance case
-above) passes on the misc-next and 6.18 test kernels with the series and
-fails on a misc-next base without it exactly in the cases the series
-changes. Its inheritance cases (the new file is "c", not "C"; chattr +A
-on it works; FIEMAP shows its data encoded, as fstests btrfs/364 checks)
-fail with our old patch 2 and pass with Filipe's.
-fstests btrfs/349 (levels) and btrfs/350 (flag matrix, not upstream yet)
-pass with the series.
+compress-props-test.sh passes on the misc-next and 6.18 test kernels
+with the series: the one-ioctl flag matrix of patches 8-9, strict
+property values (including the embedded NUL), the inheritance cases of
+patch 1, the level rule (compsize of the same data: "zlib" on a
+compress=zstd:15 mount equals zlib:3, "zstd" equals zstd:15), and a
+fixture image whose values were written by Debian's 6.12 kernel (each
+compresses with the named algorithm, an unparseable level acts as
+unspecified). On the same kernels without patches 10-11 exactly the
+NUL case and the legacy-value cases fail; on the previous lane kernels
+(our old level patch) the level-rule case fails (zlib:9). fstests
+btrfs/349 (levels) and btrfs/350 (flag matrix; neither upstream yet)
+pass.
+
+Note for the integration tree: patch 5 changes the prop_handler
+extract() prototype; the stripe_alloc topic's two property handlers
+need the new prototype when both are merged.
Filipe Manana (1):
btrfs: dont propagate nodatacow mount option on files with inherited
compression
+koraynilay (6):
+ btrfs: export btrfs_match_compress_type(), move it to compression.h
+ btrfs: also validate compression levels in
+ btrfs_compress_is_valid_type()
+ btrfs: add per-inode compression levels in xattrs
+ btrfs: support inheritance for per-inode compression levels
+ btrfs: add btrfs_compress_typelevel2str() helper
+ btrfs: preserve btrfs.compression when setting inode flags
+
Zygo Blaxell (4):
btrfs: fix nodatacow vs compression inode flag conflict check
btrfs: allow NODATACOW | NOCOMPRESS
- btrfs: props: add per-inode compression level support
- btrfs: props: validate compression property values strictly
-
- fs/btrfs/btrfs_inode.h | 7 +++
- fs/btrfs/inode.c | 23 +++++++---
- fs/btrfs/ioctl.c | 78 ++++++++++++++++++++--------
- fs/btrfs/props.c | 113 +++++++++++++++++++++++++++++++++++++++--
- fs/btrfs/props.h | 3 ++
- 5 files changed, 192 insertions(+), 32 deletions(-)
-
+ btrfs: props: reject NUL bytes in btrfs.compression values
+ btrfs: props: keep compressing inodes with legacy compression property
+ values
+
+ fs/btrfs/btrfs_inode.h | 1 +
+ fs/btrfs/compression.c | 55 +++++++++++++++++++++++++++++--
+ fs/btrfs/compression.h | 5 ++-
+ fs/btrfs/inode.c | 29 ++++++++++++++---
+ fs/btrfs/ioctl.c | 87 ++++++++++++++++++++++++++++++++++----------------
+ fs/btrfs/props.c | 78 +++++++++++++++++++++++++++++++++-----------
+ fs/btrfs/props.h | 9 ++++++
+ fs/btrfs/super.c | 8 -----
+ 8 files changed, 208 insertions(+), 64 deletions(-)
base-commit: 83cd81353093be02aa7e60874c413ec340e55bce
--
| compress-props-test.sh | 6.18 #313 (zb64 cc5b17dda57d, KASAN inline + lockdep + BTRFS_ASSERT, bzImage md5 7e052fac6664fed02bcaeb36e6c3bc41), bhive and ahive | PASS on both: I1 "-c--", I2 "-c-A", I3 8/8 extents encoded; L1-L3 on the fixture pass; every flags/props/persist case passes | raid56-wh-harness `forensics/rb1103-618/{bhive,ahive}/rb1103-compress-props.log` |
| same (negative control) | 6.18 #311 (rb1101 lane, our old patch 2) | FAIL in I1-I3 only: "C---", 1 extent, 0 encoded (I2's ioctl succeeds but the flags are "C--A"); legacy on the fixture passes | raid56-wh-harness `forensics/rb1103-618/ahive/compress-props-311.log` |
+## ck1103 (2026-11-03): rebuilt on koraynilay v6
+
+Zygo, 2026-11-03: koraynilay's "[PATCH v6 RESEND n/6] btrfs: add per-inode
+compression levels in xattrs" goes first, ours on top. Our "props: add
+per-inode compression level support" and "props: validate compression
+property values strictly" are dropped. Two new patches of ours keep what
+the series lacks: embedded NUL bytes are rejected (patch 10), and values
+stored by old kernels keep compressing, with an unparseable level counting
+as unspecified (patch 11). Level rule (Zygo): the mount level fills in for
+an unspecified or invalid level only when the mount algorithm matches the
+file's; otherwise the file's algorithm's default level applies
+(koraynilay 3/6). On 6.18, our old "preserve btrfs.compression when
+setting inode flags" is replaced by the upstream e8a0095c7df17 backport
+(Sam Ho, cherry-picked, second in the topic), and koraynilay 3/6 drops its
+run_delalloc_inline() hunk (6.18 has no compression on that path; port
+note in the commit).
+
+Candidates (not promoted, see below): misc-next compress-fixes 464a17c7110f,
+testing 9d42d25b721f, zb64 0e027dd57237; 6.18 compress-fixes 14b34f4561c1,
+testing 9093cf55823d, zb64 5b6f508427aa (`refs/cand/ck1103/<lane>/*`).
+
+compress-props-test.sh (harness b11a535b, df6771b7) adds two phases:
+
+- **xalgo**: a `compress=zstd:15` mount, the same 4 MiB of text written to
+ files with properties `zlib`, `zlib:1`, `zlib:3`, `zlib:9`, `zstd`,
+ `zstd:3`, `zstd:15`; compsize -b disk bytes. X1: `zlib` must equal
+ zlib:3 (zlib's default) and differ from zlib:9. X2: `zstd` must equal
+ zstd:15. Equal reference sizes report BLIND.
+- **oldvalues**: `fixtures/compress-legacy-values.img.zst`, written on
+ Debian 6.12.90 (vm-scripts ck1103/mk-legacy-values-fixture.sh), holds
+ empty files with `zstd:banana`, `zstd:`, `zstd:3x`,
+ `zstd:000000000000000000003` (26 bytes), `zstdgarbage`, `zstd:3<NUL>junk`,
+ `lzogarbage`, `zstd:3`, and a directory with `zstd:banana`. Plain mount:
+ each value reads back verbatim, 1 MiB + fsync is all ENCODED and compsize
+ shows only the named algorithm (O*); `zstd:banana` compresses like zstd:3
+ (OL); a file created in the directory gets `zstd` and zstd data (OD).
+ `compress=zstd:15` mount: `zstd:banana` compresses like zstd:15 (OM). No
+ "error applying prop" warning (OW). The algorithm check matters: the old
+ kernel also set the COMPRESS inode flag, so FIEMAP ENCODED alone could be
+ satisfied by the mount's default algorithm.
+
+| test | kernel | result | evidence (raid56-wh-harness forensics/) |
+|---|---|---|---|
+| compress-props, all phases | 6.18 candidate #314 (KASAN inline + lockdep + BTRFS_ASSERT, bzImage md5 95e80307678ce4a8e66dbbfecbddd6e5), bhive + ahive | PASS on both, 66/66 cases: X1 `zlib` 1245184 = zlib:3 (zlib:9 1118208, zlib:1 1347584), X2 `zstd` 1024000 = zstd:15 (zstd:3 1204224); O* all 8/8 ENCODED with the named algorithm; OL, OD, OM, OW ok; P-`zstd:3<NUL>junk` EINVAL | `ck1103-618/{bhive,ahive}/compress-props-gate-*.log` |
+| same | misc-next candidate #64 (KASAN, md5 29bc5281dd95fe0513febbd263ff03c3), dhive + ehive | PASS on both, 66/66, same sizes | `ck1103-mn/{dhive,ehive}/compress-props-gate-*.log` |
+| same, control | candidate zb64 minus patches 10-11 (6.18 #315, misc-next #65; `refs/cand/ck1103/<lane>/control-kor`, only props.c differs) | FAIL exactly in the NUL case (stored verbatim), O* for the 6 invalid values (not compressed at all: 1 extent, 0 encoded), OL, OD (no property inherited), OW (9 "error applying prop" warnings); X1, X2, OM and the valid `zstd:3` / `zstd:3<NUL>junk` legacy values pass | `ck1103-618/ahive/compress-props-kor-315b.log`, `ck1103-mn/ehive/compress-props-kor-65.log` |
+| same, control | previous lane kernels 6.18 #313, misc-next #63 (our old level patch) | FAIL X1 only: `zlib` 1118208 = zlib:9 (the reported bug); every legacy case passes | `ck1103-618/ahive/compress-props-313.log`, `ck1103-mn/ehive/compress-props-63.log` |
+| same, baseline | Debian 6.12.90 (stock) | O*, OL, OD, OM, OW all ok; X1, X2 BLIND (6.12 ignores property levels: every size equal); flag/strict-value cases FAIL as expected | `ck1103-618/ahive/compress-props-stock-6.12.90.log` |
+| fstests btrfs/349, btrfs/350 (loop devices, plain mounts) | #314 (ahive), #64 (ehive) | PASS 2/2 each; also PASS on #313 and #63 (btrfs/349 does not see the cross-algorithm level) | `ck1103-*/{ahive,ehive}/fstests*.log` |
+| fstests btrfs/364 | - | not run: not in the rigs' fstests checkout (b76e57b9) nor in ~/share/xfstests; compress-props I3 checks the same thing | - |
+
+Per-commit buildcheck (fs/btrfs, lane KASAN config): misc-next 12 commits
+from the base, 6.18 14 from v6.18.60; failed=0, new warnings 0, tip link
+rc 0. Regression smoke (rb1028-regress.sh minus qu-p1) on #314 (bhive)
+and #64 (dhive): same as rb1103 (all PASS, retire-wait-abort SKIP 77).
+
+**Integration conflict, not promoted:** patch 5 (koraynilay 4/6) changes
+the prop_handler extract() prototype; the stripe-alloc topic adds two
+property handlers (stripe_alloc, stripe_alloc_allow_rmw) with the old
+prototype, so the testing octopus does not build without a resolution.
+The candidates carry it in the octopus merge (both handlers take the new
+`(inode, char *buf, size_t len)` arguments; testing delta = topic delta +
+exactly those 4 lines). Zygo's 10-29 rule puts interacting changes in
+topic commits, not in the octopus, so promotion waits for his call.
+
## Not covered / open
- (Obsolete since rb1103: the patch that handled a parent directory