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 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
23 status=1 # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
29 # rm -rf $tmp.* $testdir
32 # get standard environment, filters and checks
37 # real QA test starts here
39 _require_scratch_reflink
41 _require_xfs_io_command "falloc"
42 _require_xfs_io_command "fpunch"
43 test $FSTYP = "btrfs" && _notrun "Can't fragment free space on btrfs."
51 # consume nearly all available space (leave ~1MB)
52 avail=`_get_available_space $SCRATCH_MNT`
53 echo "$avail bytes left"
54 filesize=$((avail - 1048576))
55 $XFS_IO_PROG -fc "truncate $filesize" $file
58 chunksizemb=$((filesize / chunks / 1048576))
59 seq 1 $chunks | while read f; do
60 echo "$((f * chunksizemb)) file size $f / 20"
61 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
65 chunksizemb=$((filesize / chunks / 1048576))
66 seq 80 $chunks | while read f; do
67 echo "$((f * chunksizemb)) file size $f / $chunks"
68 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
71 filesizemb=$((filesize / 1048576))
72 $XFS_IO_PROG -fc "falloc -k 0 ${filesizemb}m" $file
75 avail=`_get_available_space $SCRATCH_MNT`
76 $XFS_IO_PROG -fc "pwrite -S 0x65 0 $avail" ${file}
78 # Punch out whatever we need
79 seq 1 $((nr * 4)) | while read f; do
80 $XFS_IO_PROG -f -c "fpunch $((f * 2 * blksz)) $blksz" $file
84 echo "Format and mount"
85 _scratch_mkfs > $seqres.full 2>&1
86 _scratch_mount >> $seqres.full 2>&1
88 testdir=$SCRATCH_MNT/test-$seq
91 echo "Create the original files"
94 filesize=$((blksz * nr))
95 _pwrite_byte 0x61 0 $filesize $testdir/file1 >> $seqres.full
96 _pwrite_byte 0x62 0 $filesize $testdir/file2 >> $seqres.full
97 seq 0 2 $((nr-1)) | while read f; do
98 _reflink_range $testdir/file1 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
99 _pwrite_byte 0x61 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
101 seq 1 2 $((nr-1)) | while read f; do
102 _reflink_range $testdir/file2 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
103 _pwrite_byte 0x62 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
106 _fragment_freesp $testdir/bigfile >> $seqres.full 2>&1
107 filesize=$((blksz * nr))
111 md5sum $testdir/file1 | _filter_scratch
112 md5sum $testdir/file2 | _filter_scratch
113 md5sum $testdir/file3 | _filter_scratch
114 md5sum $testdir/file3.chk | _filter_scratch
116 echo "CoW with multiple extents?"
117 cowoff=$((filesize / 4))
118 cowsz=$((filesize / 2))
119 $XFS_IO_PROG -f -c "pwrite -S 0x63 -b $cowsz $cowoff $cowsz" $testdir/file3 >> $seqres.full
120 _pwrite_byte 0x63 $cowoff $cowsz $testdir/file3.chk >> $seqres.full
124 md5sum $testdir/file1 | _filter_scratch
125 md5sum $testdir/file2 | _filter_scratch
126 md5sum $testdir/file3 | _filter_scratch
127 md5sum $testdir/file3.chk | _filter_scratch