btrfs/179: call sync qgroup counts
[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 ${SCRATCH_DEV}
66     _scratch_unmount
67
68     # Run debugfs to gather file_parameters - specifically iflags.
69     file_params=`debugfs ${SCRATCH_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 ${SCRATCH_DEV}
96     _scratch_mount
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_scratch
109
110 # Real QA test starts here.
111 rm -f $seqres.full
112
113 _scratch_mkfs >> $seqres.full 2>&1
114 _scratch_mount
115
116 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
117
118 # Begin test cases.
119 echo "Test 1: Fallocate 10 blocks and write 1 block (buffered io)." \
120     >> $seqres.full
121 ${XFS_IO_PROG} -f                               \
122     -c "falloc -k 0 $((10 * $BLOCK_SIZE))"      \
123     -c "pwrite 0 $BLOCK_SIZE"                   \
124     ${SCRATCH_MNT}/test_1 | _filter_xfs_io_blocks_modified
125 _check_ext4_eof_flag test_1 ${BIT_SET}
126
127 echo "Test 2: Fallocate 10 blocks and write 1 block (direct io)." \
128     >> $seqres.full
129 ${XFS_IO_PROG} -f -d                            \
130     -c "falloc -k 0 $((10 * $BLOCK_SIZE))"      \
131     -c "pwrite 0 $BLOCK_SIZE"                   \
132     ${SCRATCH_MNT}/test_2 | _filter_xfs_io_blocks_modified
133 _check_ext4_eof_flag test_2 ${BIT_SET}
134
135 echo "Test 3: Fallocate 10 blocks and write 10 blocks (buffered io)." \
136     >> $seqres.full
137 ${XFS_IO_PROG} -f                               \
138     -c "falloc -k 0 $((10 * $BLOCK_SIZE))"      \
139     -c "pwrite 0 $((10 * $BLOCK_SIZE))"         \
140     ${SCRATCH_MNT}/test_3 | _filter_xfs_io_blocks_modified
141 _check_ext4_eof_flag test_3 ${BIT_NOT_SET}
142
143 echo "Test 4: Fallocate 10 blocks and write 10 blocks (direct io)." \
144     >> $seqres.full
145 ${XFS_IO_PROG} -f -d                            \
146     -c "falloc -k 0 $((10 * $BLOCK_SIZE))"      \
147     -c "pwrite 0 $((10 * $BLOCK_SIZE))"         \
148     ${SCRATCH_MNT}/test_4 | _filter_xfs_io_blocks_modified
149 _check_ext4_eof_flag test_4 ${BIT_NOT_SET}
150
151 echo "Test 5: Fallocate 32 blocks, seek 64 blocks and write 1 block (buffered io)." \
152     >> $seqres.full
153 ${XFS_IO_PROG} -f                                       \
154     -c "falloc -k 0 $((32 * $BLOCK_SIZE))"              \
155     -c "pwrite $((64 * $BLOCK_SIZE)) $BLOCK_SIZE"       \
156     ${SCRATCH_MNT}/test_5 | _filter_xfs_io_blocks_modified
157 _check_ext4_eof_flag test_5 ${BIT_NOT_SET}
158
159 echo "Test 6: Fallocate 32 blocks, seek to 64 blocks and write 1 block (direct io)." \
160     >> $seqres.full
161 ${XFS_IO_PROG} -f -d                                    \
162     -c "falloc -k 0 $((32 * $BLOCK_SIZE))"              \
163     -c "pwrite $((64 * $BLOCK_SIZE)) $BLOCK_SIZE"       \
164     ${SCRATCH_MNT}/test_6 | _filter_xfs_io_blocks_modified
165 _check_ext4_eof_flag test_6 ${BIT_NOT_SET}
166
167 status=0
168 exit ${status}