xfs: test xfs-specific reflink pieces
[xfstests-dev.git] / tests / xfs / 132
1 #! /bin/bash
2 # FS QA Test No. 132
3 #
4 # Ensure that fallocate on reflinked files actually CoWs the shared blocks.
5 #   - Record fs block usage (0)
6 #   - Create a file and some reflink copies
7 #   - Record fs block usage (1)
8 #   - funshare half of one of the copies
9 #   - Record fs block usage (2)
10 #   - funshare all of the copies
11 #   - Record fs block usage (3)
12 #   - rewrite the original file
13 #   - Record fs block usage (4)
14 #   - Compare fs block usage of 0-4 to ensure that block usage behaves as
15 #     we expect.
16 #   - Compare the status of the inode reflink flag at each step.
17 #
18 # "funshare" refers to fallocate copy-on-writing the shared blocks
19 #
20 # This is the same test as generic/156 except that we also check the inode
21 # reflink flag.
22 #
23 #-----------------------------------------------------------------------
24 # Copyright (c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
25 #
26 # This program is free software; you can redistribute it and/or
27 # modify it under the terms of the GNU General Public License as
28 # published by the Free Software Foundation.
29 #
30 # This program is distributed in the hope that it would be useful,
31 # but WITHOUT ANY WARRANTY; without even the implied warranty of
32 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 # GNU General Public License for more details.
34 #
35 # You should have received a copy of the GNU General Public License
36 # along with this program; if not, write the Free Software Foundation,
37 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
38 #-----------------------------------------------------------------------
39
40 seq=`basename "$0"`
41 seqres="$RESULT_DIR/$seq"
42 echo "QA output created by $seq"
43
44 here=`pwd`
45 tmp=/tmp/$$
46 status=1    # failure is the default!
47 trap "_cleanup; exit \$status" 0 1 2 3 15
48
49 _cleanup()
50 {
51     cd /
52     rm -rf "$tmp".* "$TESTDIR"
53 }
54
55 # get standard environment, filters and checks
56 . ./common/rc
57 . ./common/filter
58 . ./common/attr
59 . ./common/reflink
60
61 # real QA test starts here
62 _supported_os Linux
63 _require_test_reflink
64 _require_test_lsattr
65 _require_cp_reflink
66 _require_xfs_io_command "falloc"
67
68 rm -f "$seqres.full"
69
70 TESTDIR="$TEST_DIR/test-$seq"
71 rm -rf "$TESTDIR"
72 mkdir "$TESTDIR"
73
74 echo "Create the original file blocks"
75 BLKSZ="$(stat -f "$TESTDIR" -c '%S')"
76 BLKS=2000
77 MARGIN=100
78 SZ=$((BLKSZ * BLKS))
79 FREE_BLOCKS0=$(stat -f "$TESTDIR" -c '%f')
80 NR=4
81 _pwrite_byte 0x61 0 $SZ "$TESTDIR/file1" >> "$seqres.full"
82 _test_remount
83
84 echo "Create the reflink copies"
85 for i in `seq 2 $NR`; do
86         _cp_reflink "$TESTDIR/file1" "$TESTDIR/file$i"
87 done
88 _test_remount
89 FREE_BLOCKS1=$(stat -f "$TESTDIR" -c '%f')
90 lsattr -l $TESTDIR/ | _filter_test_dir
91
92 echo "funshare part of a file"
93 "$XFS_IO_PROG" -f -c "falloc 0 $((SZ / 2))" "$TESTDIR/file2"
94 _test_remount
95 lsattr -l $TESTDIR/ | _filter_test_dir
96
97 echo "funshare some of the copies"
98 "$XFS_IO_PROG" -f -c "falloc 0 $SZ" "$TESTDIR/file2"
99 "$XFS_IO_PROG" -f -c "falloc 0 $SZ" "$TESTDIR/file3"
100 _test_remount
101 FREE_BLOCKS2=$(stat -f "$TESTDIR" -c '%f')
102 lsattr -l $TESTDIR/ | _filter_test_dir
103
104 echo "funshare the rest of the files"
105 "$XFS_IO_PROG" -f -c "falloc 0 $SZ" "$TESTDIR/file4"
106 "$XFS_IO_PROG" -f -c "falloc 0 $SZ" "$TESTDIR/file1"
107 _test_remount
108 FREE_BLOCKS3=$(stat -f "$TESTDIR" -c '%f')
109 lsattr -l $TESTDIR/ | _filter_test_dir
110
111 echo "Rewrite the original file"
112 _pwrite_byte 0x65 0 $SZ "$TESTDIR/file1" >> "$seqres.full"
113 _test_remount
114 FREE_BLOCKS4=$(stat -f "$TESTDIR" -c '%f')
115 lsattr -l $TESTDIR/ | _filter_test_dir
116 #echo $FREE_BLOCKS0 $FREE_BLOCKS1 $FREE_BLOCKS2 $FREE_BLOCKS3 $FREE_BLOCKS4
117
118 _within_tolerance "free blocks after reflinking" $FREE_BLOCKS1 $((FREE_BLOCKS0 - BLKS)) $MARGIN -v
119
120 _within_tolerance "free blocks after nocow'ing some copies" $FREE_BLOCKS2 $((FREE_BLOCKS1 - (2 * BLKS))) $MARGIN -v
121
122 _within_tolerance "free blocks after nocow'ing all copies" $FREE_BLOCKS3 $((FREE_BLOCKS2 - BLKS)) $MARGIN -v
123
124 _within_tolerance "free blocks after overwriting original" $FREE_BLOCKS4 $FREE_BLOCKS3 $MARGIN -v
125
126 _within_tolerance "free blocks after all tests" $FREE_BLOCKS4 $((FREE_BLOCKS0 - (4 * BLKS))) $MARGIN -v
127
128 # success, all done
129 status=0
130 exit