2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015, Oracle and/or its affiliates. All Rights Reserved.
7 # Ensuring that copy on write in buffered mode works when free space
8 # is heavily fragmented.
10 # - Reflink the odd blocks of the first file into a third file.
11 # - Reflink the even blocks of the second file into the third file.
12 # - Try to fragment the free space by allocating a huge file and
13 # punching out every other block.
14 # - CoW across the halfway mark.
15 # - Check that the files are now different where we say they're different.
18 _begin_fstest auto clone punch
20 # Override the default cleanup function.
24 # rm -rf $tmp.* $testdir
27 # Import common functions.
31 # real QA test starts here
32 _require_scratch_reflink
34 _require_xfs_io_command "falloc"
35 _require_xfs_io_command "fpunch"
36 test $FSTYP = "btrfs" && _notrun "Can't fragment free space on btrfs."
42 # consume nearly all available space (leave ~1MB)
43 avail=`_get_available_space $SCRATCH_MNT`
44 echo "$avail bytes left"
45 filesize=$((avail - 1048576))
46 $XFS_IO_PROG -fc "truncate $filesize" $file
49 chunksizemb=$((filesize / chunks / 1048576))
50 seq 1 $chunks | while read f; do
51 echo "$((f * chunksizemb)) file size $f / 20"
52 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
56 chunksizemb=$((filesize / chunks / 1048576))
57 seq 80 $chunks | while read f; do
58 echo "$((f * chunksizemb)) file size $f / $chunks"
59 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
62 filesizemb=$((filesize / 1048576))
63 $XFS_IO_PROG -fc "falloc -k 0 ${filesizemb}m" $file
66 avail=`_get_available_space $SCRATCH_MNT`
67 $XFS_IO_PROG -fc "pwrite -S 0x65 0 $avail" ${file}
69 # Punch out whatever we need
70 seq 1 $((nr * 4)) | while read f; do
71 $XFS_IO_PROG -f -c "fpunch $((f * 2 * blksz)) $blksz" $file
75 echo "Format and mount"
76 _scratch_mkfs > $seqres.full 2>&1
77 _scratch_mount >> $seqres.full 2>&1
79 testdir=$SCRATCH_MNT/test-$seq
82 echo "Create the original files"
85 filesize=$((blksz * nr))
86 _pwrite_byte 0x61 0 $filesize $testdir/file1 >> $seqres.full
87 _pwrite_byte 0x62 0 $filesize $testdir/file2 >> $seqres.full
88 seq 0 2 $((nr-1)) | while read f; do
89 _reflink_range $testdir/file1 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
90 _pwrite_byte 0x61 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
92 seq 1 2 $((nr-1)) | while read f; do
93 _reflink_range $testdir/file2 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
94 _pwrite_byte 0x62 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
97 _fragment_freesp $testdir/bigfile >> $seqres.full 2>&1
98 filesize=$((blksz * nr))
102 md5sum $testdir/file1 | _filter_scratch
103 md5sum $testdir/file2 | _filter_scratch
104 md5sum $testdir/file3 | _filter_scratch
105 md5sum $testdir/file3.chk | _filter_scratch
107 echo "CoW with multiple extents?"
108 cowoff=$((filesize / 4))
109 cowsz=$((filesize / 2))
110 $XFS_IO_PROG -f -c "pwrite -S 0x63 -b $cowsz $cowoff $cowsz" $testdir/file3 >> $seqres.full
111 _pwrite_byte 0x63 $cowoff $cowsz $testdir/file3.chk >> $seqres.full
115 md5sum $testdir/file1 | _filter_scratch
116 md5sum $testdir/file2 | _filter_scratch
117 md5sum $testdir/file3 | _filter_scratch
118 md5sum $testdir/file3.chk | _filter_scratch