lib/: spdx license conversion
[xfstests-dev.git] / tests / generic / 156
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. 156
6 #
7 # Ensure that fallocate on reflinked files actually CoWs the shared blocks.
8 #   - Record fs block usage (0)
9 #   - Create a file and some reflink copies
10 #   - Record fs block usage (1)
11 #   - funshare half of one of the copies
12 #   - Record fs block usage (2)
13 #   - funshare all of the copies
14 #   - Record fs block usage (3)
15 #   - rewrite the original file
16 #   - Record fs block usage (4)
17 #   - Compare fs block usage of 0-4 to ensure that block usage behaves as
18 #     we expect.
19 #
20 # "funshare" refers to fallocate copy-on-writing the shared blocks
21 #
22 seq=`basename $0`
23 seqres=$RESULT_DIR/$seq
24 echo "QA output created by $seq"
25
26 here=`pwd`
27 tmp=/tmp/$$
28 status=1    # failure is the default!
29 trap "_cleanup; exit \$status" 0 1 2 3 15
30
31 _cleanup()
32 {
33     cd /
34     rm -rf $tmp.* $testdir
35 }
36
37 # get standard environment, filters and checks
38 . ./common/rc
39 . ./common/filter
40 . ./common/attr
41 . ./common/reflink
42
43 # real QA test starts here
44 _supported_os Linux
45 _require_test_reflink
46 _require_cp_reflink
47 _require_xfs_io_command "funshare"
48
49 rm -f $seqres.full
50
51 testdir=$TEST_DIR/test-$seq
52 rm -rf $testdir
53 mkdir $testdir
54
55 echo "Create the original file blocks"
56 blksz="$(_get_block_size $testdir)"
57 blks=2000
58 margin='15%'
59 sz=$((blksz * blks))
60 free_blocks0=$(stat -f $testdir -c '%f')
61 nr=4
62 filesize=$((blksz * nr))
63 _pwrite_byte 0x61 0 $sz $testdir/file1 >> $seqres.full
64 _test_cycle_mount
65
66 echo "Create the reflink copies"
67 for i in `seq 2 $nr`; do
68         _cp_reflink $testdir/file1 $testdir/file$i
69 done
70 _test_cycle_mount
71 free_blocks1=$(stat -f $testdir -c '%f')
72
73 echo "funshare part of a file"
74 $XFS_IO_PROG -f -c "funshare 0 $((sz / 2))" $testdir/file2
75 _test_cycle_mount
76
77 echo "funshare some of the copies"
78 $XFS_IO_PROG -f -c "funshare 0 $sz" $testdir/file2
79 $XFS_IO_PROG -f -c "funshare 0 $sz" $testdir/file3
80 _test_cycle_mount
81 free_blocks2=$(stat -f $testdir -c '%f')
82
83 echo "funshare the rest of the files"
84 $XFS_IO_PROG -f -c "funshare 0 $sz" $testdir/file4
85 $XFS_IO_PROG -f -c "funshare 0 $sz" $testdir/file1
86 _test_cycle_mount
87 free_blocks3=$(stat -f $testdir -c '%f')
88
89 echo "Rewrite the original file"
90 _pwrite_byte 0x65 0 $sz $testdir/file1 >> $seqres.full
91 _test_cycle_mount
92 free_blocks4=$(stat -f $testdir -c '%f')
93 #echo $free_blocks0 $free_blocks1 $free_blocks2 $free_blocks3 $free_blocks4
94
95 _within_tolerance "free blocks after reflinking" $free_blocks1 $((free_blocks0 - blks)) $margin -v
96
97 _within_tolerance "free blocks after nocow'ing some copies" $free_blocks2 $((free_blocks1 - (2 * blks))) $margin -v
98
99 _within_tolerance "free blocks after nocow'ing all copies" $free_blocks3 $((free_blocks2 - blks)) $margin -v
100
101 _within_tolerance "free blocks after overwriting original" $free_blocks4 $free_blocks3 $margin -v
102
103 _within_tolerance "free blocks after all tests" $free_blocks4 $((free_blocks0 - (4 * blks))) $margin -v
104
105 # success, all done
106 status=0
107 exit