2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2021 Google, Inc. All Rights Reserved.
7 # Test wiping of ext4_dir_entry2 data upon file removal, conversion
8 # to htree, and splitting of htree nodes
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
14 status=1 # failure is the default!
16 # get standard environment, filters and checks
20 # remove previous $seqres.full before test
23 # real QA test starts here
27 _require_command "$DEBUGFS_PROG" debugfs
29 testdir="${SCRATCH_MNT}/testdir"
31 # get block number filename's dir ent
32 # argument 1: filename
34 echo $($DEBUGFS_PROG $SCRATCH_DEV -R "dirsearch /testdir $1" 2>> $seqres.full | grep -o -m 1 "phys [0-9]\+" | cut -c 6-)
37 # get offset of filename's dirent within the block
38 # argument 1: filename
40 echo $($DEBUGFS_PROG $SCRATCH_DEV -R "dirsearch /testdir $1" 2>> $seqres.full | grep -o -m 1 "offset [0-9]\+" | cut -c 8-)
43 # get record length of dir ent at specified block and offset
47 echo $(od $SCRATCH_DEV --skip-bytes=$(($1 * $blocksize + $2 + 4)) --read-bytes=2 -d -An --endian=little | tr -d ' \t\n\r')
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
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')
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
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)"
69 _scratch_unmount >> $seqres.full 2>&1
73 # TEST 1: dir entry fields wiped upon file removal
80 _scratch_mkfs_sized $((128 * 1024 * 1024)) >> $seqres.full 2>&1
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)"
88 for file_num in {1..10}; do
89 touch $testdir/test"$(printf "%04d" $file_num)"
91 _scratch_unmount >> $seqres.full 2>&1
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)
98 block2=$(get_block $test_file2)
99 offset2=$(get_offset $test_file2)
100 rec_len2=$(get_reclen $block2 $offset2)
102 _scratch_mount >> $seqres.full 2>&1
103 rm $testdir/$test_file1
104 _scratch_unmount >> $seqres.full 2>&1
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)
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."
115 _fail "ERROR (test 1 part 1): metadata not wiped upon removing test file 1"
118 _scratch_mount >> $seqres.full 2>&1
119 rm $testdir/$test_file2
120 _scratch_unmount >> $seqres.full 2>&1
122 check2=$(read_dir_ent $block2 $offset2 $rec_len2)
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"
128 # TEST 2: old dir entry fields wiped when directory converted to htree
131 # get original location
132 block1=$(get_block $test_file3)
133 offset1=$(get_offset $test_file3)
134 rec_len1=$(get_reclen $block1 $offset1)
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"
142 # force conversion to htree
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"
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)
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"
159 # TEST 3: old dir entries wiped when moved to another block during split_node
162 # force splitting of a node
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)
167 # get line number of "Reading directory block 3"
168 block3_line=$(echo "$hdump" | awk '/Reading directory block 3/{ print NR; exit }')
170 [ -z "$block3_line" ] && echo "ERROR (test 3): could not find block number 3 after node split"
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)
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-)
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)
184 if [ -z "$check1" ] && [ -z "$check2" ]; then
185 echo "Test 3 passed."
187 _fail "ERROR (test 3): file name not wiped during node split"