1 #!/bin/bash 2 3 ################################################################################ 4 # # 5 # Copyright (c) 2009 FUJITSU LIMITED # 6 # # 7 # This program is free software; you can redistribute it and#or modify # 8 # it under the terms of the GNU General Public License as published by # 9 # the Free Software Foundation; either version 2 of the License, or # 10 # (at your option) any later version. # 11 # # 12 # This program is distributed in the hope that it will be useful, but # 13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # 14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # 15 # for more details. # 16 # # 17 # You should have received a copy of the GNU General Public License # 18 # along with this program; if not, write to the Free Software # 19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # 20 # # 21 ################################################################################ 22 23 export TCID="ext4-online-defrag" 24 export TST_TOTAL=18 25 26 . ext4_funcs.sh 27 28 # How to age filesystem 29 EMPTY=1 30 SMALL=2 31 LARGE=3 32 33 # Defrag what 34 FILE=1 35 DIR=2 36 FILESYSTEM=3 37 38 tst_check_cmds e4defrag 39 E4DEFRAG=`which e4defrag` 40 41 age_filesystem() 42 { 43 local dirId= 44 local idx= 45 rm -rf mnt_point/* 46 if [ $1 -eq $EMPTY ]; then 47 if [ $2 -eq $FILE -o $2 -eq $FILESYSTEM ]; then 48 touch mnt_point/tmp_file 49 echo "abc" > mnt_point/tmp_file 50 elif [ $2 -eq $DIR ]; then 51 mkdir mnt_point/tmp_dir 52 echo "abc" > mnt_point/tmp_dir/tmp_file 53 fi 54 elif [ $1 -eq $SMALL ]; then 55 56 dd if=/dev/zero of=mnt_point/occupy bs=1M count=40 57 58 # age filesystem from 0.0 to 0.2 -> 0.4 -> 0.6 -> 0.8 -> 1.0 59 for ((idx = 3; idx < 8; idx++)) 60 { 61 ffsb ffsb-config$idx > /dev/null 62 dirId=$((idx - 3)) 63 mv mnt_point/data mnt_point/data$dirId 64 } 65 66 rm mnt_point/occupy 67 68 df 69 else 70 rm -rf mnt_point/* 71 if [ $2 -eq $DIR ]; then 72 mkdir mnt_point/tmp_dir 73 dest=mnt_point/tmp_dir/tmp_file 74 else 75 dest=mnt_point/tmp_file 76 fi 77 78 bsize=`dumpe2fs -h $EXT4_DEV | grep 'Block size'` 79 bsize=`echo $bsize | awk '{ print $NF }'` 80 bcount=`dumpe2fs -h $EXT4_DEV | grep 'Free blocks'` 81 bcount=`echo $bcount | awk '{ print $NF }'` 82 bcount=$(( $bcount / 2 - 100 )) 83 dd if=/dev/zero of=$dest bs=$bsize count=$bcount 84 85 fi 86 } 87 88 my_e4defrag() 89 { 90 if [ $1 -eq $FILE ]; then 91 if [ $2 -eq $SMALL ]; then 92 $E4DEFRAG -v mnt_point/data0/ 93 return $? 94 # EMPTY or LARGE 95 else 96 $E4DEFRAG -v mnt_point/tmp_file 97 return $? 98 fi 99 elif [ $1 -eq $DIR ]; then 100 if [ $2 -eq $SMALL ]; then 101 $E4DEFRAG -v mnt_point/data0/ 102 return $? 103 else 104 $E4DEFRAG -v mnt_point/tmp_dir 105 return $? 106 fi 107 else 108 $E4DEFRAG -v $EXT4_DEV 109 return $? 110 fi 111 } 112 113 # Test online defragmentation feature 114 # $1: defrag type 115 # $2: 1 - empty, 2 - full with small files, 3 - full with large files 116 # $3: block size 117 ext4_test_online_defrag() 118 { 119 echo Test $TST_COUNT start >> ext4_online_defrag_result.txt 120 121 tst_resm TINFO "defrag type: $1, defrag obj: $2, block size: $3" 122 123 mkfs.ext4 -m 0 -b $3 -O ^flex_bg $EXT4_DEV >> ext4_online_defrag_result.txt 2>&1 124 if [ $? -ne 0 ]; then 125 tst_resm TFAIL "failed to create ext4 filesystem" 126 return 127 fi 128 129 tune2fs -O extents $EXT4_DEV >> ext4_online_defrag_result.txt 2>&1 130 131 mount -t ext4 -o nodelalloc $EXT4_DEV mnt_point 132 if [ $? -ne 0 ]; then 133 tst_resm TFAIL"failed to mount ext4 filesystem" 134 return 135 fi 136 137 age_filesystem $2 $1 >> ext4_online_defrag_result.txt 2>&1 138 139 my_e4defrag $1 $2 >> ext4_online_defrag_result.txt 140 if [ $? -ne 0 ]; then 141 tst_resm TFAIL "e4defrag returned failure" 142 umount mnt_point 143 return 144 fi 145 146 umount mnt_point 147 if [ $? -ne 0 ]; then 148 tst_resm TFAIL "failed to umount ext4 filesystem" 149 return 150 fi 151 152 e2fsck -p $EXT4_DEV >> ext4_online_defrag_result.txt 2>&1 153 if [ $? -ne 0 ]; then 154 tst_resm TFAIL "fsck returned failure" 155 return 156 fi 157 158 tst_resm TPASS "ext4 online defrag test pass" 159 } 160 161 # main 162 ext4_setup 163 164 tst_check_cmds ffsb 165 166 DEFRAG=( $FILE $DIR $FILESYSTEM ) 167 AGING=( $EMPTY $SMALL $LARGE ) 168 BLOCK_SIZE=( 1024 4096 ) 169 170 for ((i = 0; i < 2; i++)) 171 { 172 for ((j = 0; j < 3; j++)) 173 { 174 for ((k = 0; k < 3; k++)) 175 { 176 ext4_test_online_defrag ${DEFRAG[$j]} ${AGING[$k]} \ 177 ${BLOCK_SIZE[$i]} 178 } 179 } 180 } 181 182 tst_exit 183