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