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 _begin_fstest auto quick dir
13 # Import common functions.
16 # real QA test starts here
20 _require_command "$DEBUGFS_PROG" debugfs
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
27 testdir="${SCRATCH_MNT}/testdir"
29 # get block number filename's dir ent
30 # argument 1: filename
32 echo $($DEBUGFS_PROG $SCRATCH_DEV -R "dirsearch /testdir $1" 2>> $seqres.full | grep -o -m 1 "phys [0-9]\+" | cut -c 6-)
35 # get offset of filename's dirent within the block
36 # argument 1: filename
38 echo $($DEBUGFS_PROG $SCRATCH_DEV -R "dirsearch /testdir $1" 2>> $seqres.full | grep -o -m 1 "offset [0-9]\+" | cut -c 8-)
41 # get record length of dir ent at specified block and offset
45 if (( big_endian )); then
46 echo $(od $SCRATCH_DEV --skip-bytes=$(($1 * $blocksize + $2 + 4)) --read-bytes=2 -d -An --endian=little | \
49 echo $(od $SCRATCH_DEV --skip-bytes=$(($1 * $blocksize + $2 + 4)) --read-bytes=2 -d -An | \
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
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')
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
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)"
73 _scratch_unmount >> $seqres.full 2>&1
77 # TEST 1: dir entry fields wiped upon file removal
84 _scratch_mkfs_sized $((128 * 1024 * 1024)) >> $seqres.full 2>&1
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
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"
97 blocksize="$(_get_block_size $SCRATCH_MNT)"
100 for file_num in {1..10}; do
101 touch $testdir/test"$(printf "%04d" $file_num)"
103 _scratch_unmount >> $seqres.full 2>&1
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)
110 block2=$(get_block $test_file2)
111 offset2=$(get_offset $test_file2)
112 rec_len2=$(get_reclen $block2 $offset2)
114 _scratch_mount >> $seqres.full 2>&1
115 rm $testdir/$test_file1
116 _scratch_unmount >> $seqres.full 2>&1
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)
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."
127 _fail "ERROR (test 1 part 1): metadata not wiped upon removing test file 1"
130 _scratch_mount >> $seqres.full 2>&1
131 rm $testdir/$test_file2
132 _scratch_unmount >> $seqres.full 2>&1
134 check2=$(read_dir_ent $block2 $offset2 $rec_len2)
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"
140 # TEST 2: old dir entry fields wiped when directory converted to htree
143 # get original location
144 block1=$(get_block $test_file3)
145 offset1=$(get_offset $test_file3)
146 rec_len1=$(get_reclen $block1 $offset1)
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"
154 # force conversion to htree
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"
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)
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"
171 # TEST 3: old dir entries wiped when moved to another block during split_node
174 # force splitting of a node
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)
179 # get line number of "Reading directory block 3"
180 block3_line=$(echo "$hdump" | awk '/Reading directory block 3/{ print NR; exit }')
182 [ -z "$block3_line" ] && echo "ERROR (test 3): could not find block number 3 after node split"
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)
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-)
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)
196 if [ -z "$check1" ] && [ -z "$check2" ]; then
197 echo "Test 3 passed."
199 _fail "ERROR (test 3): file name not wiped during node split"