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