2 # Routines for reflinking, deduping, and comparing parts of files.
3 #-----------------------------------------------------------------------
4 # Copyright (c) 2015 Oracle. All Rights Reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 # Contact information: Oracle Corporation, 500 Oracle Parkway,
21 # Redwood Shores, CA 94065, USA, or: http://www.oracle.com/
22 #-----------------------------------------------------------------------
24 # Check that cp has a reflink argument
27 cp --help | grep -q reflink || \
28 _notrun "This test requires a cp with --reflink support."
31 # Given 2 files, verify that they have the same mapping but different
32 # inodes - i.e. an undisturbed reflink
33 # Silent if so, make noise if not
36 # not a hard link or symlink?
37 cmp -s <(stat -c '%i' $1) <(stat -c '%i' $2) \
38 && echo "$1 and $2 are not reflinks: same inode number"
41 diff -u <($XFS_IO_PROG -c "fiemap" $1 | grep -v $1) \
42 <($XFS_IO_PROG -c "fiemap" $2 | grep -v $2) \
43 || echo "$1 and $2 are not reflinks: different extents"
46 # New reflink/dedupe helpers
48 # this test requires the test fs support reflink...
49 _require_test_reflink()
52 _require_xfs_io_command "reflink"
54 rm -rf "$TEST_DIR/file1" "$TEST_DIR/file2"
55 "$XFS_IO_PROG" -f -c "pwrite -S 0x61 0 65536" "$TEST_DIR/file1" > /dev/null
56 "$XFS_IO_PROG" -f -c "reflink $TEST_DIR/file1 0 0 65536" "$TEST_DIR/file2" > /dev/null
57 if [ ! -s "$TEST_DIR/file2" ]; then
58 rm -rf "$TEST_DIR/file1" "$TEST_DIR/file2"
59 _notrun "Reflink not supported by test filesystem type: $FSTYP"
61 rm -rf "$TEST_DIR/file1" "$TEST_DIR/file2"
64 # this test requires the scratch fs support reflink...
65 _require_scratch_reflink()
68 _require_xfs_io_command "reflink"
70 _scratch_mkfs > /dev/null
72 "$XFS_IO_PROG" -f -c "pwrite -S 0x61 0 65536" "$SCRATCH_MNT/file1" > /dev/null
73 "$XFS_IO_PROG" -f -c "reflink $SCRATCH_MNT/file1 0 0 65536" "$SCRATCH_MNT/file2" > /dev/null
74 if [ ! -s "$SCRATCH_MNT/file2" ]; then
76 _notrun "Reflink not supported by scratch filesystem type: $FSTYP"
81 # this test requires the test fs support dedupe...
82 _require_test_dedupe()
85 _require_xfs_io_command "dedupe"
87 rm -rf "$TEST_DIR/file1" "$TEST_DIR/file2"
88 "$XFS_IO_PROG" -f -c "pwrite -S 0x61 0 65536" "$TEST_DIR/file1" > /dev/null
89 "$XFS_IO_PROG" -f -c "pwrite -S 0x61 0 65536" "$TEST_DIR/file2" > /dev/null
90 testio="$("$XFS_IO_PROG" -f -c "dedupe $TEST_DIR/file1 0 0 65536" "$TEST_DIR/file2" 2>&1)"
91 echo $testio | grep -q "Operation not supported" && \
92 _notrun "Dedupe not supported by test filesystem type: $FSTYP"
93 echo $testio | grep -q "Inappropriate ioctl for device" && \
94 _notrun "Dedupe not supported by test filesystem type: $FSTYP"
95 echo $testio | grep -q "Invalid argument" && \
96 _notrun "Dedupe not supported by test filesystem type: $FSTYP"
97 rm -rf "$TEST_DIR/file1" "$TEST_DIR/file2"
100 # this test requires the scratch fs support dedupe...
101 _require_scratch_dedupe()
104 _require_xfs_io_command "dedupe"
106 _scratch_mkfs > /dev/null
108 "$XFS_IO_PROG" -f -c "pwrite -S 0x61 0 65536" "$SCRATCH_MNT/file1" > /dev/null
109 "$XFS_IO_PROG" -f -c "pwrite -S 0x61 0 65536" "$SCRATCH_MNT/file2" > /dev/null
110 testio="$("$XFS_IO_PROG" -f -c "dedupe $TEST_DIR/file1 0 0 65536" "$TEST_DIR/file2" 2>&1)"
111 echo $testio | grep -q "Operation not supported" && \
112 _notrun "Dedupe not supported by test filesystem type: $FSTYP"
113 echo $testio | grep -q "Inappropriate ioctl for device" && \
114 _notrun "Dedupe not supported by test filesystem type: $FSTYP"
115 echo $testio | grep -q "Invalid argument" && \
116 _notrun "Dedupe not supported by test filesystem type: $FSTYP"
120 # Prints a range of a file as a hex dump
127 $XFS_IO_PROG $xfs_io_args -f -c "pread -q -v $offset $len" "$file" | cut -d ' ' -f '3-18'
130 # Compare ranges of two files
138 cmp -s <(_read_range "$file1" "$offset1" "$len") \
139 <(_read_range "$file2" "$offset2" "$len")
142 # Prints the md5 checksum of a hexdump of a part of a given file
143 _md5_range_checksum() {
148 md5sum <(_read_range "$file" "$offset" "$len") | cut -d ' ' -f 1
151 # Reflink some file1 into file2 via cp
156 cp --reflink=always -p "$file1" "$file2"
159 # Reflink some file1 into file2
164 "$XFS_IO_PROG" -f -c "reflink $file1" "$file2"
167 # Reflink some part of file1 into another part of file2
176 "$XFS_IO_PROG" $xfs_io_args -f -c "reflink $file1 $offset1 $offset2 $len" "$file2"
179 # Dedupe some part of file1 into another part of file2
188 "$XFS_IO_PROG" $xfs_io_args -f -c "dedupe $file1 $offset1 $offset2 $len" "$file2"
191 # Create a file of interleaved unwritten and reflinked blocks
192 _weave_reflink_unwritten() {
198 _pwrite_byte 0x61 0 $((blksz * nr)) $sfile
199 $XFS_IO_PROG -f -c "falloc 0 $((blksz * nr))" $dfile
200 _pwrite_byte 0x00 0 $((blksz * nr)) $dfile.chk
201 seq 0 2 $((nr - 1)) | while read i; do
202 _reflink_range $sfile $((blksz * i)) $dfile $((blksz * i)) $blksz
203 _pwrite_byte 0x61 $((blksz * i)) $blksz $dfile.chk
207 # Create a file of interleaved holes and reflinked blocks
208 _weave_reflink_holes() {
214 _pwrite_byte 0x61 0 $((blksz * nr)) $sfile
215 $XFS_IO_PROG -f -c "truncate $((blksz * nr))" $dfile
216 _pwrite_byte 0x00 0 $((blksz * nr)) $dfile.chk
217 seq 0 2 $((nr - 1)) | while read i; do
218 _reflink_range $sfile $((blksz * i)) $dfile $((blksz * i)) $blksz
219 _pwrite_byte 0x61 $((blksz * i)) $blksz $dfile.chk
223 # For a file created with _weave_reflink_holes, fill the holes with delalloc
225 _weave_reflink_holes_delalloc() {
230 seq 1 2 $((nr - 1)) | while read i; do
231 _pwrite_byte 0x62 $((blksz * i)) $blksz $dfile
232 _pwrite_byte 0x62 $((blksz * i)) $blksz $dfile.chk
236 # Create a file of interleaved regular blocks and reflinked blocks
237 _weave_reflink_regular() {
243 _pwrite_byte 0x61 0 $((blksz * nr)) $sfile
244 _pwrite_byte 0x62 0 $((blksz * nr)) $dfile
245 _pwrite_byte 0x62 0 $((blksz * nr)) $dfile.chk
246 seq 0 2 $((nr - 1)) | while read i; do
247 _reflink_range $sfile $((blksz * i)) $dfile $((blksz * i)) $blksz
248 _pwrite_byte 0x61 $((blksz * i)) $blksz $dfile.chk
252 # Create a file of interleaved holes, unwritten blocks, regular blocks, and
254 _weave_reflink_rainbow() {
260 _pwrite_byte 0x61 0 $((blksz * nr)) $sfile
261 $XFS_IO_PROG -f -c "truncate $((blksz * nr))" $dfile
262 _pwrite_byte 0x00 0 $((blksz * nr)) $dfile.chk
263 # 0 blocks are reflinked
264 seq 0 5 $((nr - 1)) | while read i; do
265 _reflink_range $sfile $((blksz * i)) $dfile $((blksz * i)) $blksz
266 _pwrite_byte 0x61 $((blksz * i)) $blksz $dfile.chk
268 # 1 blocks are unwritten
269 seq 1 5 $((nr - 1)) | while read i; do
270 $XFS_IO_PROG -f -c "falloc $((blksz * i)) $blksz" $dfile
271 _pwrite_byte 0x00 $((blksz * i)) $blksz $dfile.chk
274 seq 2 5 $((nr - 1)) | while read i; do
275 _pwrite_byte 0x00 $((blksz * i)) $blksz $dfile.chk
277 # 3 blocks are regular
278 seq 3 5 $((nr - 1)) | while read i; do
279 _pwrite_byte 0x71 $((blksz * i)) $blksz $dfile
280 _pwrite_byte 0x71 $((blksz * i)) $blksz $dfile.chk
282 # 4 blocks will be delalloc later
285 # For a file created with _weave_reflink_rainbow, fill the holes with delalloc
287 _weave_reflink_rainbow_delalloc() {
292 # 4 blocks are delalloc (do later)
293 seq 4 5 $((nr - 1)) | while read i; do
294 _pwrite_byte 0x62 $((blksz * i)) $blksz $dfile
295 _pwrite_byte 0x62 $((blksz * i)) $blksz $dfile.chk
299 # Make the source file have interleaved regular blocks and reflinked blocks
300 _sweave_reflink_regular() {
306 _pwrite_byte 0x61 0 $((blksz * nr)) $sfile
307 _pwrite_byte 0x62 0 $((blksz * nr)) $dfile
308 _pwrite_byte 0x61 0 $((blksz * nr)) $sfile.chk
309 seq 1 2 $((nr - 1)) | while read i; do
310 _reflink_range $sfile $((blksz * i)) $dfile $((blksz * i)) $blksz
314 # Make the source file have interleaved unwritten blocks and reflinked blocks
315 _sweave_reflink_unwritten() {
321 $XFS_IO_PROG -f -c "falloc 0 $((blksz * nr))" $sfile
322 _pwrite_byte 0x00 0 $((blksz * nr)) $sfile.chk
323 _pwrite_byte 0x62 0 $((blksz * nr)) $dfile
324 seq 1 2 $((nr - 1)) | while read i; do
325 _pwrite_byte 0x61 $((blksz * i)) $blksz $sfile
326 _pwrite_byte 0x61 $((blksz * i)) $blksz $sfile.chk
328 seq 1 2 $((nr - 1)) | while read i; do
329 _reflink_range $sfile $((blksz * i)) $dfile $((blksz * i)) $blksz
333 # Make the source file have interleaved holes and reflinked blocks
334 _sweave_reflink_holes() {
340 $XFS_IO_PROG -f -c "truncate $((blksz * nr))" $sfile
341 _pwrite_byte 0x00 0 $((blksz * nr)) $sfile.chk
342 _pwrite_byte 0x62 0 $((blksz * nr)) $dfile
343 seq 1 2 $((nr - 1)) | while read i; do
344 _pwrite_byte 0x61 $((blksz * i)) $blksz $sfile
345 _pwrite_byte 0x61 $((blksz * i)) $blksz $sfile.chk
347 seq 1 2 $((nr - 1)) | while read i; do
348 _reflink_range $sfile $((blksz * i)) $dfile $((blksz * i)) $blksz
352 # For a file created with _sweave_reflink_holes, fill the holes with delalloc
354 _sweave_reflink_holes_delalloc() {
359 seq 0 2 $((nr - 1)) | while read i; do
360 _pwrite_byte 0x64 $((blksz * i)) $blksz $sfile
361 _pwrite_byte 0x64 $((blksz * i)) $blksz $sfile.chk