6a1b6d752a3327be110e43874c42566c6549bd63
[xfstests-dev.git] / tests / shared / 243
1 #! /bin/bash
2 # FS QA Test No. 243
3 #
4 # Test to ensure that the EOFBLOCK_FL gets set/unset correctly.
5 #
6 # As found by Theodore Ts'o:
7 # If a 128K file is falloc'ed using the KEEP_SIZE flag, and then
8 # write exactly 128K, the EOFBLOCK_FL doesn't get cleared correctly.
9 # This is bad since it forces e2fsck to complain about that inode.
10 # If you have a large number of inodes that are written with fallocate
11 # using KEEP_SIZE, and then fill them up to their expected size,
12 # e2fsck will potentially complain about a _huge_ number of inodes.
13 # This would also cause a huge increase in the time taken by e2fsck
14 # to complete its check.
15 #
16 # Test scenarios covered:
17 # 1. Fallocating X bytes and writing Y (Y<X) (buffered and direct io)
18 # 2. Fallocating X bytes and writing Y (Y=X) (buffered and direct io)
19 # 3. Fallocating X bytes and writing Y (Y>X) (buffered and direct io)
20 #
21 # These test cases exercise the normal and edge case conditions using
22 # falloc (and KEEP_SIZE).
23 #
24 # Ref: http://thread.gmane.org/gmane.comp.file-systems.ext4/20682
25 #
26 #-----------------------------------------------------------------------
27 # Copyright (c) 2010 Google, Inc.  All Rights Reserved.
28 #
29 # This program is free software; you can redistribute it and/or
30 # modify it under the terms of the GNU General Public License as
31 # published by the Free Software Foundation.
32 #
33 # This program is distributed in the hope that it would be useful,
34 # but WITHOUT ANY WARRANTY; without even the implied warranty of
35 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36 # GNU General Public License for more details.
37 #
38 # You should have received a copy of the GNU General Public License
39 # along with this program; if not, write the Free Software Foundation,
40 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
41 #-----------------------------------------------------------------------
42 #
43
44 seq=`basename $0`
45 echo "QA output created by $seq"
46
47 here=`pwd`
48 tmp=/tmp/$$
49 status=1        # failure is the default!
50 trap "_cleanup; exit \$status" 0 1 2 3 15
51
52 # Test specific macros.
53 BIT_NOT_SET=0   # inode flag - 0x400000 bit is not set.
54 BIT_SET=1       # inode flag - 0x400000 bit is set.
55
56 # Generic test cleanup function.
57 _cleanup()
58 {
59   cd /
60   rm -f $tmp.*
61 }
62
63 # Ext4 uses the EOFBLOCKS_FL bit when fallocating blocks with KEEP_SIZE
64 # enabled. The only time this bit should be set is when extending the allocated
65 # blocks further than what the i_size represents. In the situations wherein the
66 # i_size covers all allocated blocks, this bit should be cleared.
67
68 # Checks the state of the sample file in the filesystem and returns whether
69 # the inode flag 0x400000 is set or not.
70 _check_ext4_eof_flag()
71 {
72   # Check whether EOFBLOCK_FL is set.
73   # For ext4 filesystems: use debugfs to check if EOFBLOCKS_FL is set.
74   # Other filesystems: do nothing. The default fsck at the end of the test
75   # should catch any potential errors.
76   if [ "${FSTYP}" == "ext4" ]; then
77     bit_set=1
78
79     # Unmount the ${TEST_DEV}
80     umount ${TEST_DEV}
81
82     # Run debugfs to gather file_parameters - specifically iflags.
83     file_params=`debugfs ${TEST_DEV} -R "stat ${1}" 2>&1 | grep -e Flags:`
84     iflags=${file_params#*Flags: }
85
86     # Ensure that the iflags value was parsed correctly.
87     if [ -z ${iflags} ]; then
88       echo "iFlags value was not parsed successfully." >> $seq.full
89       status=1
90       exit ${status}
91     fi
92
93     # Check if EOFBLOCKS_FL is set.
94     if ((${iflags} & 0x400000)); then
95       echo "EOFBLOCK_FL bit is set." >> $seq.full
96       bit_set=1
97     else
98       echo "EOFBLOCK_FL bit is not set." >> $seq.full
99       bit_set=0
100     fi
101
102     # Check current bit state to expected value.
103     if [ ${bit_set} -ne ${2} ]; then
104       echo "Error: Current bit state incorrect." >> $seq.full
105       status=1
106       exit ${status}
107     fi
108
109     # Mount the ${TEST_DEV}
110     mount ${TEST_DEV} -t ${FSTYP} ${TEST_DIR}
111   fi
112 }
113
114 # Get standard environment, filters and checks.
115 . ./common.rc
116 . ./common.filter
117
118 # Prerequisites for the test run.
119 _supported_fs ext4 xfs btrfs gfs2
120 _supported_os Linux
121 _require_xfs_io_falloc
122
123 # Real QA test starts here.
124 rm -f $seq.full
125
126 # Remove any leftover files from last run.
127 rm -f ${TEST_DIR}/test_?
128
129 # Begin test cases.
130 echo "Test 1: Fallocate 40960 bytes and write 4096 bytes (buffered io)." \
131     >> $seq.full
132 ${XFS_IO_PROG} -F -f                    \
133     -c 'falloc -k 0 40960'              \
134     -c 'pwrite 0 4096'                  \
135     ${TEST_DIR}/test_1 | _filter_xfs_io_unique
136 _check_ext4_eof_flag test_1 ${BIT_SET}
137
138 echo "Test 2: Fallocate 40960 bytes and write 4096 bytes (direct io)." \
139     >> $seq.full
140 ${XFS_IO_PROG} -F -f -d                 \
141     -c 'falloc -k 0 40960'              \
142     -c 'pwrite 0 4096'                  \
143     ${TEST_DIR}/test_2 | _filter_xfs_io_unique
144 _check_ext4_eof_flag test_2 ${BIT_SET}
145
146 echo "Test 3: Fallocate 40960 bytes and write 40960 bytes (buffered io)." \
147     >> $seq.full
148 ${XFS_IO_PROG} -F -f                    \
149     -c 'falloc -k 0 40960'              \
150     -c 'pwrite 0 40960'                 \
151     ${TEST_DIR}/test_3 | _filter_xfs_io_unique
152 _check_ext4_eof_flag test_3 ${BIT_NOT_SET}
153
154 echo "Test 4: Fallocate 40960 bytes and write 40960 bytes (direct io)." \
155     >> $seq.full
156 ${XFS_IO_PROG} -F -f -d                 \
157     -c 'falloc -k 0 40960'              \
158     -c 'pwrite 0 40960'                 \
159     ${TEST_DIR}/test_4 | _filter_xfs_io_unique
160 _check_ext4_eof_flag test_4 ${BIT_NOT_SET}
161
162 echo "Test 5: Fallocate 128k, seek 256k and write 4k block (buffered io)." \
163     >> $seq.full
164 ${XFS_IO_PROG} -F -f                    \
165     -c 'falloc -k 0 128k'               \
166     -c 'pwrite 256k 4k'                 \
167     ${TEST_DIR}/test_5 | _filter_xfs_io_unique
168 _check_ext4_eof_flag test_5 ${BIT_NOT_SET}
169
170 echo "Test 6: Fallocate 128k, seek to 256k and write a 4k block (direct io)." \
171     >> $seq.full
172 ${XFS_IO_PROG} -F -f -d                 \
173     -c 'falloc -k 0 128k'               \
174     -c 'pwrite 256k 4k'                 \
175     ${TEST_DIR}/test_6 | _filter_xfs_io_unique
176 _check_ext4_eof_flag test_6 ${BIT_NOT_SET}
177
178 status=0
179 exit ${status}