Debian Codegraph @

debian/patches/testsuite_punch_granularity_guard.patch

raw
1 From: Samuel Henrique <[email protected]>
2 Date: Mon, 17 Aug 2026 22:00:00 -0700
3 Subject: testsuite: probe the punch granularity each hole assertion relies on
4
5 The last section of preallocate_test.py writes 256 blocks of
6 [4 KiB data][24 KiB zeros][4 KiB data] and requires --inplace --sparse to
7 deallocate at least half of the file. A punch only frees whole allocation
8 units, and the 24 KiB interior run sits 4 KiB into each 32 KiB block: with a
9 4 KiB unit it covers six whole units (24 KiB freed per block), but with a
10 16 KiB unit it covers none at all, so st_blocks does not move.
11
12 That is what happens on loong64, whose kernel uses 16 KiB pages
13 (arch/loongarch/Kconfig defaults to 16KB_3LEVEL on 64-bit and Debian's
14 config does not override it), and the build fails with
15
16 --inplace --sparse left matching interior zero runs allocated: 8388608 of
17 8388608 bytes remain allocated after a 8388608-byte matched-block update
18
19 i.e. exactly "nothing was freed", which is the arithmetic of a punch
20 granularity >= 16 KiB rather than an rsync regression. fs_can_punch_holes()
21 does not catch it because it punches a whole 64 KiB file, which frees blocks
22 at any granularity.
23
24 Generalise that helper into punch_frees(offset, length, size) so each
25 assertion can probe the shape it actually depends on, and gate the interior
26 one on the exact [4 KiB data][24 KiB zeros][4 KiB data] layout. Where the
27 filesystem can free that run the assertion runs exactly as before; where it
28 cannot, the test says so rather than reporting a regression.
29
30 While here, call fallocate64() instead of fallocate(). ctypes declares the
31 offset and length as c_longlong, but where off_t is 32 bits glibc's
32 fallocate() takes 32-bit offsets -- so the callee reads the high half of
33 `offset` as its `length`, the probe fails with EINVAL, and can_punch comes
34 back False. That is why none of this file's hole-punching assertions have
35 ever run on a 32-bit port; on i386:
36
37 fs_can_punch_holes() as written : before=128 ret=-1 errno=22 -> False
38 the same probe via fallocate64 : before=128 ret=0 -> True
39
40 fallocate64() takes off64_t on every architecture and is a plain alias of
41 fallocate() where off_t is already 64 bits. The probe now writes urandom
42 rather than a repeated byte as well, so a filesystem that compresses does not
43 answer "nothing was freed" merely because nothing was allocated.
44
45 Forwarded: https://github.com/RsyncProject/rsync/pull/1062
46 ---
47 --- a/testsuite/preallocate_test.py
48 +++ b/testsuite/preallocate_test.py
49 @@ -38,12 +38,33 @@ if run_rsync('-a', '--preallocate', f'{s
50 check=False, capture_output=True).returncode != 0:
51 test_skipped("--preallocate not supported on this platform")
52
53 -def fs_can_punch_holes():
54 - """True only where the kernel can deallocate blocks via FALLOC_FL_PUNCH_HOLE
55 - -- the mechanism do_punch_hole uses for --sparse. A filesystem may report
56 - seek-based sparseness yet still keep every block on a punch (e.g. where
57 - rsync's punch falls back to writing zeros), so probe the real capability and
58 - assert the hole only where it actually frees blocks."""
59 +def punch_frees(offset, length, size):
60 + """True where punching [offset, offset+length) out of a `size`-byte file
61 + really deallocates blocks -- the mechanism do_punch_hole uses for --sparse.
62 +
63 + Two separate things can leave st_blocks untouched, so each assertion below
64 + probes the exact shape it relies on. A filesystem may report seek-based
65 + sparseness yet still keep every block on a punch (e.g. where rsync's punch
66 + falls back to writing zeros), which a whole-file probe catches. And a punch
67 + only frees storage in whole allocation units, which are not always 4 KiB: a
68 + tmpfs frees whole pages, 16 KiB on loongarch/loong64 (and 64 KiB on a
69 + 64k-page ppc64el or arm64 kernel), and a filesystem may be formatted with a
70 + block size above the page size. An interior run spanning no whole unit is
71 + zeroed rather than deallocated, so st_blocks does not move and an assertion
72 + phrased in st_blocks would report a hole-punching regression that is really
73 + just the filesystem's granularity.
74 +
75 + fallocate64() rather than fallocate(): where off_t is 32 bits the latter
76 + takes 32-bit offsets, so ctypes' 64-bit arguments do not line up with what
77 + it reads (on i386 it takes the high half of `offset` as its `length`) and
78 + every probe fails with EINVAL -- which is why every assertion below has
79 + silently done nothing on all the 32-bit ports. fallocate64() takes off64_t
80 + everywhere and is a plain alias of fallocate() where off_t is already 64
81 + bits wide.
82 +
83 + The probe data has to be incompressible: a filesystem that compresses
84 + (btrfs with compress=) stores a run of one repeated byte in almost no
85 + blocks, leaving a successful punch with nothing to free."""
86 import ctypes
87 import ctypes.util
88 KEEP_SIZE, PUNCH_HOLE = 0x01, 0x02
89 @@ -52,12 +73,12 @@ def fs_can_punch_holes():
90 try:
91 libc = ctypes.CDLL(ctypes.util.find_library('c') or 'libc.so.6',
92 use_errno=True)
93 - libc.fallocate.argtypes = [ctypes.c_int, ctypes.c_int,
94 - ctypes.c_longlong, ctypes.c_longlong]
95 + libc.fallocate64.argtypes = [ctypes.c_int, ctypes.c_int,
96 + ctypes.c_longlong, ctypes.c_longlong]
97 fd = os.open(p, os.O_CREAT | os.O_RDWR | os.O_TRUNC, 0o644)
98 - os.write(fd, b'\xff' * 65536)
99 + os.write(fd, os.urandom(size))
100 before = os.fstat(fd).st_blocks
101 - ret = libc.fallocate(fd, PUNCH_HOLE | KEEP_SIZE, 0, 65536)
102 + ret = libc.fallocate64(fd, PUNCH_HOLE | KEEP_SIZE, offset, length)
103 return ret == 0 and os.fstat(fd).st_blocks < before
104 except (OSError, AttributeError, ValueError):
105 return False
106 @@ -70,7 +91,7 @@ def fs_can_punch_holes():
107 pass
108
109
110 -can_punch = fs_can_punch_holes()
111 +can_punch = punch_frees(0, 65536, 65536)
112
113
114 def seed_plain(size=1_000_000):
115 @@ -139,6 +160,13 @@ with open(src / deep, 'wb') as source, o
116 source.write(block)
117 dest.write(block)
118
119 +# Only assert the interior punch where the filesystem can free a 24 KiB run
120 +# sitting 4 KiB into a 32 KiB block -- the exact shape written just above.
121 +can_punch_interior = can_punch and punch_frees(4096, 24576, 32768)
122 +if can_punch and not can_punch_interior:
123 + print("preallocate: interior-hole assertion skipped: this filesystem's "
124 + "allocation unit cannot free a 24 KiB run inside a 32 KiB block")
125 +
126 matched_size = os.path.getsize(TODIR / deep)
127 matched_before = allocated(TODIR / deep)
128 run_rsync('-a', '--ignore-times', '--inplace', '--sparse', '--no-whole-file',
129 @@ -146,7 +174,7 @@ run_rsync('-a', '--ignore-times', '--inp
130 assert_same(TODIR / deep, src / deep,
131 label='--inplace --sparse matched-block content')
132 matched_after = allocated(TODIR / deep)
133 -if (can_punch and matched_before >= matched_size
134 +if (can_punch_interior and matched_before >= matched_size
135 and matched_after * 2 >= matched_before):
136 test_fail(f"--inplace --sparse left matching interior zero runs allocated: "
137 f"{matched_after} of {matched_before} bytes remain allocated "