common: use mount/umount helpers everywhere
[xfstests-dev.git] / tests / ext4 / 002
1 #! /bin/bash
2 # FS QA Test No. ext4/002
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 seqres=$RESULT_DIR/$seq
46 echo "QA output created by $seq"
47
48 here=`pwd`
49 tmp=/tmp/$$
50 status=1        # failure is the default!
51 trap "_cleanup; exit \$status" 0 1 2 3 15
52
53 # Test specific macros.
54 BIT_NOT_SET=0   # inode flag - 0x400000 bit is not set.
55 BIT_SET=1       # inode flag - 0x400000 bit is set.
56
57 # Generic test cleanup function.
58 _cleanup()
59 {
60   cd /
61   rm -f $tmp.*
62 }
63
64 # Ext4 uses the EOFBLOCKS_FL bit when fallocating blocks with KEEP_SIZE
65 # enabled. The only time this bit should be set is when extending the allocated
66 # blocks further than what the i_size represents. In the situations wherein the
67 # i_size covers all allocated blocks, this bit should be cleared.
68
69 # Checks the state of the sample file in the filesystem and returns whether
70 # the inode flag 0x400000 is set or not.
71 _check_ext4_eof_flag()
72 {
73   # Check whether EOFBLOCK_FL is set.
74   # For ext4 filesystems: use debugfs to check if EOFBLOCKS_FL is set.
75   # Other filesystems: do nothing. The default fsck at the end of the test
76   # should catch any potential errors.
77   if [ "${FSTYP}" == "ext4" ]; then
78     bit_set=1
79
80     # Unmount the ${TEST_DEV}
81     _test_unmount
82
83     # Run debugfs to gather file_parameters - specifically iflags.
84     file_params=`debugfs ${TEST_DEV} -R "stat ${1}" 2>&1 | grep -e Flags:`
85     iflags=${file_params#*Flags: }
86
87     # Ensure that the iflags value was parsed correctly.
88     if [ -z ${iflags} ]; then
89       echo "iFlags value was not parsed successfully." >> $seqres.full
90       status=1
91       exit ${status}
92     fi
93
94     # Check if EOFBLOCKS_FL is set.
95     if ((${iflags} & 0x400000)); then
96       echo "EOFBLOCK_FL bit is set." >> $seqres.full
97       bit_set=1
98     else
99       echo "EOFBLOCK_FL bit is not set." >> $seqres.full
100       bit_set=0
101     fi
102
103     # Check current bit state to expected value.
104     if [ ${bit_set} -ne ${2} ]; then
105       echo "Error: Current bit state incorrect." >> $seqres.full
106       status=1
107       exit ${status}
108     fi
109
110     # Mount the ${TEST_DEV}
111     mount ${TEST_DEV} -t ${FSTYP} ${TEST_DIR}
112   fi
113 }
114
115 # Get standard environment, filters and checks.
116 . ./common/rc
117 . ./common/filter
118
119 # Prerequisites for the test run.
120 _supported_fs ext4
121 _supported_os Linux
122 _require_xfs_io_command "falloc"
123 _require_test
124
125 # Real QA test starts here.
126 rm -f $seqres.full
127
128 # Remove any leftover files from last run.
129 rm -f ${TEST_DIR}/test_?
130
131 # Begin test cases.
132 echo "Test 1: Fallocate 40960 bytes and write 4096 bytes (buffered io)." \
133     >> $seqres.full
134 ${XFS_IO_PROG} -f                       \
135     -c 'falloc -k 0 40960'              \
136     -c 'pwrite 0 4096'                  \
137     ${TEST_DIR}/test_1 | _filter_xfs_io_unique
138 _check_ext4_eof_flag test_1 ${BIT_SET}
139
140 echo "Test 2: Fallocate 40960 bytes and write 4096 bytes (direct io)." \
141     >> $seqres.full
142 ${XFS_IO_PROG} -f -d                    \
143     -c 'falloc -k 0 40960'              \
144     -c 'pwrite 0 4096'                  \
145     ${TEST_DIR}/test_2 | _filter_xfs_io_unique
146 _check_ext4_eof_flag test_2 ${BIT_SET}
147
148 echo "Test 3: Fallocate 40960 bytes and write 40960 bytes (buffered io)." \
149     >> $seqres.full
150 ${XFS_IO_PROG} -f                       \
151     -c 'falloc -k 0 40960'              \
152     -c 'pwrite 0 40960'                 \
153     ${TEST_DIR}/test_3 | _filter_xfs_io_unique
154 _check_ext4_eof_flag test_3 ${BIT_NOT_SET}
155
156 echo "Test 4: Fallocate 40960 bytes and write 40960 bytes (direct io)." \
157     >> $seqres.full
158 ${XFS_IO_PROG} -f -d                    \
159     -c 'falloc -k 0 40960'              \
160     -c 'pwrite 0 40960'                 \
161     ${TEST_DIR}/test_4 | _filter_xfs_io_unique
162 _check_ext4_eof_flag test_4 ${BIT_NOT_SET}
163
164 echo "Test 5: Fallocate 128k, seek 256k and write 4k block (buffered io)." \
165     >> $seqres.full
166 ${XFS_IO_PROG} -f                       \
167     -c 'falloc -k 0 128k'               \
168     -c 'pwrite 256k 4k'                 \
169     ${TEST_DIR}/test_5 | _filter_xfs_io_unique
170 _check_ext4_eof_flag test_5 ${BIT_NOT_SET}
171
172 echo "Test 6: Fallocate 128k, seek to 256k and write a 4k block (direct io)." \
173     >> $seqres.full
174 ${XFS_IO_PROG} -f -d                    \
175     -c 'falloc -k 0 128k'               \
176     -c 'pwrite 256k 4k'                 \
177     ${TEST_DIR}/test_6 | _filter_xfs_io_unique
178 _check_ext4_eof_flag test_6 ${BIT_NOT_SET}
179
180 status=0
181 exit ${status}