2ddb48d7bf652fe2c9bf501dee722b3a931afb3a
[xfstests-dev.git] / tests / generic / 153
1 #! /bin/bash
2 # FS QA Test No. 153
3 #
4 # Ensure that collapse-range on all copies of a file reflinked N times releases the blocks
5 #   - Record fs block usage (0)
6 #   - Create a file and some reflink copies
7 #   - Record fs block usage (1)
8 #   - Collapse-range some blocks of the copies
9 #   - Record fs block usage (2)
10 #   - Truncate all blocks of the copies
11 #   - Compare fs block usage to (2), (1), and (0)
12 #
13 #-----------------------------------------------------------------------
14 # Copyright (c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
15 #
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License as
18 # published by the Free Software Foundation.
19 #
20 # This program is distributed in the hope that it would be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write the Free Software Foundation,
27 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28 #-----------------------------------------------------------------------
29
30 seq=`basename $0`
31 seqres=$RESULT_DIR/$seq
32 echo "QA output created by $seq"
33
34 here=`pwd`
35 tmp=/tmp/$$
36 status=1    # failure is the default!
37 trap "_cleanup; exit \$status" 0 1 2 3 15
38
39 _cleanup()
40 {
41     cd /
42     rm -rf $tmp.* $testdir
43 }
44
45 # get standard environment, filters and checks
46 . ./common/rc
47 . ./common/filter
48 . ./common/reflink
49
50 # real QA test starts here
51 _supported_os Linux
52 _require_test_reflink
53 _require_cp_reflink
54 _require_xfs_io_command "fcollapse"
55
56 rm -f $seqres.full
57
58 testdir=$TEST_DIR/test-$seq
59 rm -rf $testdir
60 mkdir $testdir
61
62 echo "Create the original file blocks"
63 blksz="$(get_block_size $testdir)"
64 blks=2000
65 margin='15%'
66 sz=$((blksz * blks))
67 free_blocks0=$(stat -f $testdir -c '%f')
68 nr=4
69 filesize=$((blksz * nr))
70 _pwrite_byte 0x61 0 $sz $testdir/file1 >> $seqres.full
71 _test_cycle_mount
72
73 echo "Create the reflink copies"
74 for i in `seq 2 $nr`; do
75         _cp_reflink $testdir/file1 $testdir/file$i
76 done
77 _test_cycle_mount
78 free_blocks1=$(stat -f $testdir -c '%f')
79
80 echo "Collapse most of the blocks"
81 $XFS_IO_PROG -f -c "fcollapse 0 $(((blks - 1) * blksz))" $testdir/file2
82 $XFS_IO_PROG -f -c "fcollapse 0 $((sz / 2))" $testdir/file3
83 $XFS_IO_PROG -f -c "fcollapse $((sz / 2)) $(( ((blks / 2) - 1) * blksz))" $testdir/file4
84 _test_cycle_mount
85 free_blocks2=$(stat -f $testdir -c '%f')
86
87 echo "Collpase nearly all the files"
88 $XFS_IO_PROG -f -c "fcollapse 0 $(( ((blks / 2) - 1) * blksz))" $testdir/file3
89 $XFS_IO_PROG -f -c "fcollapse 0 $((sz / 2))" $testdir/file4
90 $XFS_IO_PROG -f -c "fcollapse 0 $(( (blks - 1) * blksz))" $testdir/file1
91 _test_cycle_mount
92 free_blocks3=$(stat -f $testdir -c '%f')
93 #echo $free_blocks0 $free_blocks1 $free_blocks2 $free_blocks3
94
95 _within_tolerance "free blocks after reflink" $free_blocks1 $((free_blocks0 - blks)) $margin -v
96
97 _within_tolerance "free blocks after fcollapsing some reflink copies" $free_blocks2 $free_blocks1 $margin -v
98
99 _within_tolerance "free blocks after fcollapsing all copies" $free_blocks3 $free_blocks0 $margin -v
100
101 # success, all done
102 status=0
103 exit