generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / ext4 / 048
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2021 Google, Inc. All Rights Reserved.
4 #
5 # FS QA Test No. 048
6 #
7 # Test wiping of ext4_dir_entry2 data upon file removal, conversion
8 # to htree, and splitting of htree nodes
9 #
10 . ./common/preamble
11 _begin_fstest auto quick dir
12
13 # Import common functions.
14 . ./common/filter
15
16 # real QA test starts here
17 _supported_fs ext4
18
19 _require_scratch
20 _require_command "$DEBUGFS_PROG" debugfs
21
22 big_endian=$(echo -ne '\x11' | od -tx2 | head -1 | cut -f2 -d' ' | cut -c1)
23 if (( big_endian )); then
24         _require_od_endian_flag
25 fi
26
27 testdir="${SCRATCH_MNT}/testdir"
28
29 # get block number filename's dir ent
30 # argument 1: filename
31 get_block() {
32         echo $($DEBUGFS_PROG $SCRATCH_DEV -R "dirsearch /testdir $1" 2>> $seqres.full | grep -o -m 1 "phys [0-9]\+" | cut -c 6-)
33 }
34
35 # get offset of filename's dirent within the block
36 # argument 1: filename
37 get_offset() {
38         echo $($DEBUGFS_PROG $SCRATCH_DEV -R "dirsearch /testdir $1" 2>> $seqres.full | grep -o -m 1 "offset [0-9]\+" | cut -c 8-)
39 }
40
41 # get record length of dir ent at specified block and offset
42 # argument 1: block
43 # argument 2: offset
44 get_reclen() {
45         if (( big_endian )); then
46                 echo $(od $SCRATCH_DEV --skip-bytes=$(($1 * $blocksize + $2 + 4)) --read-bytes=2  -d -An  --endian=little | \
47                         tr -d ' \t\n\r')
48         else
49                 echo $(od $SCRATCH_DEV --skip-bytes=$(($1 * $blocksize + $2 + 4)) --read-bytes=2  -d -An | \
50                         tr -d ' \t\n\r')
51         fi
52 }
53
54 # reads portion of dirent that should be zero'd out (starting at offset of name_len = 6)
55 # and trims 0s and whitespace
56 # argument 1: block num containing dir ent
57 # argument 2: offset of dir ent within block
58 # argument 3: rec len of dir ent
59 read_dir_ent() {
60         echo $(od $SCRATCH_DEV --skip-bytes=$(($1 * $blocksize + $2 + 6)) --read-bytes=$(($3 - 6)) -d -An -v | sed -e 's/[0 \t\n\r]//g')
61 }
62
63 # forces node split on test directory
64 # can be used to convert to htree and to split node on existing htree
65 # looks for jump in directory size as indicator of node split
66 induce_node_split() {
67         _scratch_mount >> $seqres.full 2>&1
68         dir_size="$(stat --printf="%s" $testdir)"
69         while [[ "$(stat --printf="%s" $testdir)" == "$dir_size" ]]; do
70                 file_num=$(($file_num + 1))
71                 touch $testdir/test"$(printf "%04d" $file_num)"
72         done
73         _scratch_unmount >> $seqres.full 2>&1
74 }
75
76 #
77 # TEST 1: dir entry fields wiped upon file removal
78 #
79
80 test_file1="test0001"
81 test_file2="test0002"
82 test_file3="test0003"
83
84 _scratch_mkfs_sized $((128 * 1024 * 1024)) >> $seqres.full 2>&1
85
86 # create scratch dir for testing
87 # create some files with no name a substr of another name so we can grep later
88 _scratch_mount >> $seqres.full 2>&1
89
90 # Use the presence of the journal checkpoint ioctl as a proxy of filename
91 # wipe being supported
92 if test -x $here/src/checkpoint_journal && \
93         ! $here/src/checkpoint_journal $SCRATCH_MNT --dry-run ; then
94     _notrun "filename wipe not supported"
95 fi
96
97 blocksize="$(_get_block_size $SCRATCH_MNT)"
98 mkdir $testdir
99 file_num=1
100 for file_num in {1..10}; do
101         touch $testdir/test"$(printf "%04d" $file_num)"
102 done
103 _scratch_unmount >> $seqres.full 2>&1
104
105 # get block, offset, and rec_len of two test files
106 block1=$(get_block $test_file1)
107 offset1=$(get_offset $test_file1)
108 rec_len1=$(get_reclen $block1 $offset1)
109
110 block2=$(get_block $test_file2)
111 offset2=$(get_offset $test_file2)
112 rec_len2=$(get_reclen $block2 $offset2)
113
114 _scratch_mount >> $seqres.full 2>&1
115 rm $testdir/$test_file1
116 _scratch_unmount >> $seqres.full 2>&1
117
118 # read name_len field to end of dir entry
119 check1=$(read_dir_ent $block1 $offset1 $rec_len1)
120 check2=$(read_dir_ent $block2 $offset2 $rec_len2)
121
122 # if check is empty, bytes read was all 0's, file data wiped
123 # at this point, check1 should be empty, but check 2 should not be
124 if [ -z "$check1" ] && [ ! -z "$check2" ]; then
125         echo "Test 1 part 1 passed."
126 else
127         _fail "ERROR (test 1 part 1): metadata not wiped upon removing test file 1"
128 fi
129
130 _scratch_mount >> $seqres.full 2>&1
131 rm $testdir/$test_file2
132 _scratch_unmount >> $seqres.full 2>&1
133
134 check2=$(read_dir_ent $block2 $offset2 $rec_len2)
135
136 # at this point, both should be wiped
137 [ -z "$check2" ] && echo "Test 1 part 2 passed." || _fail "ERROR (test 1 part 2): metadata not wiped upon removing test file 2"
138
139 #
140 # TEST 2: old dir entry fields wiped when directory converted to htree
141 #
142
143 # get original location
144 block1=$(get_block $test_file3)
145 offset1=$(get_offset $test_file3)
146 rec_len1=$(get_reclen $block1 $offset1)
147
148 # sanity check, ensures not an htree yet
149 check_htree=$($DEBUGFS_PROG $SCRATCH_DEV -R "htree_dump /testdir" 2>&1)
150 if [[ "$check_htree" != *"htree_dump: Not a hash-indexed directory"* ]]; then
151         _fail "ERROR (test 2): already an htree"
152 fi
153
154 # force conversion to htree
155 induce_node_split
156
157 # ensure it is now an htree
158 check_htree=$($DEBUGFS_PROG $SCRATCH_DEV -R "htree_dump /testdir" 2>&1)
159 if [[ "$check_htree" == *"htree_dump: Not a hash-indexed directory"* ]]; then
160         _fail "ERROR (test 2): directory was not converted to an htree after creation of many files"
161 fi
162
163 # check that old data was wiped
164 # (this location is not immediately reused by ext4)
165 check1=$(read_dir_ent $block1 $offset1 $rec_len1)
166
167 # at this point, check1 should be empty meaning data was wiped
168 [ -z "$check1" ] &&  echo "Test 2 passed." || _fail "ERROR (test 2): file metadata not wiped during conversion to htree"
169
170 #
171 # TEST 3: old dir entries wiped when moved to another block during split_node
172 #
173
174 # force splitting of a node
175 induce_node_split
176 # use debugfs to get names of two files from block 3
177 hdump=$($DEBUGFS_PROG $SCRATCH_DEV -R "htree_dump /testdir" 2>> $seqres.full)
178
179 # get line number of "Reading directory block 3"
180 block3_line=$(echo "$hdump" | awk '/Reading directory block 3/{ print NR; exit }')
181
182 [ -z "$block3_line" ] && echo "ERROR (test 3): could not find block number 3 after node split"
183
184 test_file1=$(echo "$hdump" | sed -n "$(($block3_line + 1))"p | cut -d ' ' -f4)
185 test_file2=$(echo "$hdump" | sed -n "$(($block3_line + 2))"p | cut -d ' ' -f4)
186
187 # check these filenames don't exist in block 1 or 2
188 # get block numbers of first two blocks
189 block1=$(echo "$hdump" | grep -o -m 1 "Reading directory block 1, phys [0-9]\+" | cut -c 33-)
190 block2=$(echo "$hdump" | grep -o -m 1 "Reading directory block 2, phys [0-9]\+" | cut -c 33-)
191
192 # search all of both these blocks for these file names
193 check1=$(od $SCRATCH_DEV --skip-bytes=$(($block1 * $blocksize)) --read-bytes=$blocksize -c -An -v | tr -d '\\ \t\n\r\v' | grep -e $test_file1 -e $test_file2)
194 check2=$(od $SCRATCH_DEV --skip-bytes=$(($block2 * $blocksize)) --read-bytes=$blocksize -c -An -v | tr -d '\\ \t\n\r\v' | grep -e $test_file1 -e $test_file2)
195
196 if [ -z "$check1" ] && [ -z "$check2" ]; then
197         echo "Test 3 passed."
198 else
199         _fail "ERROR (test 3): file name not wiped during node split"
200 fi
201
202 status=0
203 exit