1#! /bin/sh
2#
3#   Copyright (c) International Business Machines  Corp., 2005
4#
5#   This program is free software;  you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation; either version 2 of the License, or
8#   (at your option) any later version.
9#
10#   This program is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY;  without even the implie; warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13#   the GNU General Public License for more details.
14#
15#   You should have received a copy of the GNU General Public License
16#   along with this program;  if not, write to the Free Software
17#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18#
19#
20#
21#  FILE   : fs_di
22#
23#  PURPOSE: FileSystem Data Integrity
24#	   1. Creates a data file of specified or random size and copies
25#         the file to a random directory depth on a specified filesystem
26#	      The two files are compared and checked for differences.
27#	      If the files differ, then the test fails. By default, this
28#	      test creates a 30Mb file and runs for ten loops.
29#      2. Creates a datafile of size half of the partition size. Creates
30#         two fragmented files on the specified partition and copies datafile
31#         to them. Then compares both the fragmented files with datafile. If
32#         files differ, then test fails.
33#
34#  SETUP: None
35#
36#
37#  HISTORY:
38#    28/07/09 Jyoti Vantagodi (jyotiv@linux.vnet.ibm.com)
39#             Added point two of above PURPOSE
40#    04/11/05 Robbie Williamson (robbiew@us.ibm.com)
41#      -Written
42#
43#***********************************************************************
44
45#Uncomment line below for debug output.
46#trace_logic=${trace_logic:-"set -x"}
47
48$trace_logic
49
50#-----------------------------------------------------------------------
51# Initialize local variables
52#-----------------------------------------------------------------------
53TC=${TC:=fs_di}
54TCbin=${TCbin:=`pwd`}
55TCtmp=${TCtmp:=$TMPDIR/$TC$$}
56export PATH=$PATH:$TCbin:../../../bin
57export TCID=$TC
58export TST_TOTAL=1
59export TST_COUNT=1
60
61# If CLEANUP is not set; set it to "ON"
62CLEANUP=${CLEANUP:="ON"}
63usage()
64{
65    cat <<-EOF >&2
66
67    usage: ./${0##*/} -d TMPDIR [-h] [-l # of LOOPS ] [-s SIZE in Mb][-S partition SIZE in Mb]
68
69    -d TMPDIR       Directory where temporary files will be created.
70    -h              Help. Prints all available options.
71    -l # of LOOPS   The number of times to run the test. Default=10.
72    -s SIZE in Mb   The size of the data file to create. Default=30Mb. A "0" means random sizes from 10-500Mb.
73    -S SIZE in Mb   Size of usable partition (in MBs) on which the testing is carried out (needs to be passed
74                    for fragmented file test)
75    -v              Verbose output.
76    example: ./${0##*/} -d /mnt/cifsmount -l 20 -s 100 -S 200
77    example: ./${0##*/} -d /mnt/cifsmount -l 20 -s 100
78
79	EOF
80exit 0
81}
82
83#=============================================================================
84# FUNCTION NAME:        end_testcase
85#
86# FUNCTION DESCRIPTION: Clean up
87#
88# PARAMETERS:   	None.
89#
90# RETURNS:      	None.
91#=============================================================================
92end_testcase()
93{
94$trace_logic
95    if [ "$CLEANUP" = "ON" ]; then
96	rm -rf $TCtmp
97	rm -rf ${TESTFS}
98        rm -f $TCtmp/testfile*
99    fi
100
101    [ $# = 0 ] && { tst_resm TPASS "Test Successful"; exit 0; }
102    tst_resm TFAIL "Test Failed: $@"
103    exit 1
104}
105
106#=============================================================================
107# FUNCTION NAME:        setup_testcase
108#
109# FUNCTION DESCRIPTION: Perform the setup function for the testcase.
110#
111# PARAMETERS:   	None.
112#
113# RETURNS:      	None.
114#=============================================================================
115$trace_logic
116    TMPBASE=0
117    LOOPS=10
118    SIZE=30
119    RANDOM_SIZE=0
120    DISK_SIZE=0
121    while getopts d:hl:s:S:v arg
122    do
123	case $arg in
124
125        d)  # append $$ to TMP, as it is recursively
126            # removed at end of script.
127            export TMPBASE=$OPTARG
128            TMP="${TMPBASE}/fs_di-$$"
129            export TESTFS="$TMP";;
130        h)  usage
131	    exit 0;;
132
133        l)  # Execute user defined number of loops.
134            LOOPS=$OPTARG;;
135
136        s)  # Size of data file to create
137            SIZE=$OPTARG
138	    if [ $SIZE -eq 0 ]; then
139              RANDOM_SIZE=1
140	    fi;;
141
142	v)  # Verbose
143	    trace_logic=${trace_logic:-"set -x"};;
144
145        S)  # Size of usable partition, which is used for creating creating the files
146            DISK_SIZE=$OPTARG;;
147
148       \?) usage
149	   exit 0;;
150       esac
151    done
152    if [ $TMPBASE = "0" ]; then
153      tst_resm TBROK "You must specify the target directory [-d]"
154      exit 1
155    fi
156
157    export TST_COUNT=$LOOPS
158
159    echo ""
160    echo "Test Options:"
161    echo " Tested Filesystem: $TESTFS"
162    echo "             Loops: $LOOPS"
163    if [ $RANDOM_SIZE -eq 0 ];then
164	    echo "    Data File Size: $SIZE"
165    else
166	    echo "    Data File Size: Random"
167    fi
168    sleep 5
169
170    $trace_logic
171    mkdir -p $TCtmp || end_testcase "Could not create $TCtmp"
172    chmod 777 $TCtmp
173    mkdir -p $TESTFS || end_testcase "Could not create $TESTFS"
174    chmod 777 $TESTFS
175
176
177#=============================================================================
178# FUNCTION NAME:        main
179#
180# FUNCTION DESCRIPTION: Perform the test
181#
182# PARAMETERS:   	None.
183#
184# RETURNS:      	None.
185#=============================================================================
186  loopcount=0
187  tst_resm TINFO "Test Started"
188  while [ $loopcount -lt $LOOPS ]
189    do
190	if [ $RANDOM_SIZE -eq 1 ]; then
191  	  SIZE=$RANDOM
192	  let "SIZE %= 500"
193	  while [ $SIZE -lt 10 ]
194	    do
195              SIZE=$RANDOM
196              let "SIZE %= 500"
197 	    done
198      	fi
199  	create_datafile $SIZE $TCtmp/testfile >/dev/null
200	  if [ $? != 0 ]; then
201		end_testcase "Could not create testfile of size ${SIZE}Mb"
202	  fi
203	RANDOM_DEPTH=$RANDOM
204	: $(( RANDOM_DEPTH %= 500 ))
205
206	RANDOM_LENGTH=$RANDOM
207	: $(( RANDOM_LENGTH %= 500 ))
208	RANDOM_LENGTH=$(( $RANDOM_LENGTH / 10 ))
209
210	NameCount=0
211	DepthCount=0
212	FILEPATH=""
213	while [ $DepthCount -lt $RANDOM_DEPTH ]
214	  do
215	      if [ $NameCount -lt $RANDOM_LENGTH ]; then
216		 FILEPATH=${FILEPATH}X
217		 NameCount=$(( $NameCount + 1 ))
218	      else
219		 FILEPATH=${FILEPATH}/
220		 NameCount=0
221	      fi
222	      DepthCount=$(( $DepthCount + 1 ))
223	  done
224	mkdir -p ${TESTFS}/${FILEPATH} || end_testcase "Could not create ${TESTFS}/${FILEPATH}"
225	chmod -R 777 $TESTFS
226
227	cp $TCtmp/testfile ${TESTFS}/${FILEPATH}
228	cmp $TCtmp/testfile ${TESTFS}/${FILEPATH}/testfile
229	retval=$?
230	if [ "$retval" != 0 ]; then
231		end_testcase "Error in loop $loopcount: cmp after write FAILED"
232	fi
233	cp ${TESTFS}/${FILEPATH}/testfile $TCtmp/testfile_copy
234	cmp $TCtmp/testfile $TCtmp/testfile_copy
235	retval=$?
236	if [ "$retval" != 0 ]; then
237		end_testcase "Error in loop $loopcount: cmp after read FAILED"
238	fi
239	rm -rf ${TESTFS}/${FILEPATH}
240	rm -f $TCtmp/testfile*
241	loopcount=$(( $loopcount + 1 ))
242	tst_resm TINFO "Completed Loop $loopcount"
243    done
244    if [ "$DISK_SIZE" != 0 ]; then
245    #Create a datafile of size half of the disk size
246    tst_resm TINFO "Creating fragmented files. Please wait..."
247    DISK_SIZE=$(( $DISK_SIZE / 2 ))
248	if [ "$DISK_SIZE" == 0 ]; then
249		DISK_SIZE=1
250	fi
251    create_datafile $DISK_SIZE $TCtmp/testfile >/dev/null
252    retval=$?
253    if [ "$retval" != 0 ]; then
254        end_testcase "Error in creating data file"
255    fi
256
257    #Invoke frag to create 2 fragmented files and copy data file to both the files
258    frag $TCtmp/testfile $TMPBASE
259    retval=$?
260    if [ "$retval" != 0 ]; then
261        end_testcase "Error in creating frag files"
262    fi
263    tst_resm TINFO "Created fragmented files"
264
265    #Compare both frag files with data file
266    cmp $TCtmp/testfile $TMPBASE/frag1
267    retval=$?
268    if [ "$retval" != 0 ]; then
269        end_testcase "frag1 and datafile are not matching"
270    fi
271    if [ "$retval" != 0 ]; then
272       end_testcase "frag2 and datafile are not matching"
273    fi
274
275    tst_resm TINFO "Completed test with fragmented files"
276	rm -rf $TMPBASE/*
277	rm -f $TCtmp/testfile*
278    fi
279end_testcase
280