generic: test for non-zero used blocks while writing into a file
[xfstests-dev.git] / tests / generic / 152
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 152
6 #
7 # Ensure that punching all copies of a file reflinked N times releases the blocks
8 #   - Record fs block usage (0)
9 #   - Create a file and some reflink copies
10 #   - Record fs block usage (1)
11 #   - Punch some blocks of the copies
12 #   - Record fs block usage (2)
13 #   - Punch all blocks of the copies
14 #   - Compare fs block usage to (2), (1), and (0)
15 #
16 seq=`basename $0`
17 seqres=$RESULT_DIR/$seq
18 echo "QA output created by $seq"
19
20 here=`pwd`
21 tmp=/tmp/$$
22 status=1    # failure is the default!
23 trap "_cleanup; exit \$status" 0 1 2 3 15
24
25 _cleanup()
26 {
27     cd /
28     rm -rf $tmp.* $testdir
29 }
30
31 # get standard environment, filters and checks
32 . ./common/rc
33 . ./common/filter
34 . ./common/reflink
35
36 # real QA test starts here
37 _require_test_reflink
38 _require_cp_reflink
39 _require_xfs_io_command "fpunch"
40
41 rm -f $seqres.full
42
43 testdir=$TEST_DIR/test-$seq
44 rm -rf $testdir
45 mkdir $testdir
46
47 echo "Create the original file blocks"
48 blksz="$(_get_block_size $testdir)"
49 blks=2000
50 margin='15%'
51 sz=$((blksz * blks))
52 free_blocks0=$(stat -f $testdir -c '%f')
53 nr=4
54 filesize=$((blksz * nr))
55 _pwrite_byte 0x61 0 $sz $testdir/file1 >> $seqres.full
56 sync
57
58 echo "Create the reflink copies"
59 for i in `seq 2 $nr`; do
60         _cp_reflink $testdir/file1 $testdir/file$i
61 done
62 _test_cycle_mount
63 free_blocks1=$(stat -f $testdir -c '%f')
64
65 echo "Punch most of the blocks"
66 $XFS_IO_PROG -f -c "fpunch 0 $sz" $testdir/file2
67 $XFS_IO_PROG -f -c "fpunch 0 $((sz / 2))" $testdir/file3
68 $XFS_IO_PROG -f -c "fpunch $((sz / 2)) $((sz / 2))" $testdir/file4
69 _test_cycle_mount
70 free_blocks2=$(stat -f $testdir -c '%f')
71
72 echo "Punch all the files"
73 for i in `seq 2 $nr`; do
74         $XFS_IO_PROG -f -c "fpunch 0 $sz" $testdir/file$i
75 done
76 $XFS_IO_PROG -f -c "fpunch 0 $sz" $testdir/file1
77 _test_cycle_mount
78 free_blocks3=$(stat -f $testdir -c '%f')
79 #echo $free_blocks0 $free_blocks1 $free_blocks2 $free_blocks3
80
81 _within_tolerance "free blocks after reflink" $free_blocks1 $((free_blocks0 - blks)) $margin -v
82
83 _within_tolerance "free blocks after punching some reflink copies" $free_blocks2 $free_blocks1 $margin -v
84
85 _within_tolerance "free blocks after punching all copies" $free_blocks3 $free_blocks0 $margin -v
86
87 # success, all done
88 status=0
89 exit