xfstests: fix TEST_MNT typos (should be TEST_DIR)
[xfstests-dev.git] / common.filestreams
1 ##/bin/bash
2 #
3 # Copyright (c) 2007 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it would be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write the Free Software Foundation,
16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 #
18 #
19 # Core of filestreams tests.
20 #
21
22 _check_filestreams_support()
23 {
24         local irix_timeout_sysvar="xfs_mfstream_timeout"
25         local linux_timeout_procvar="/proc/sys/fs/xfs/filestream_centisecs"
26         local streams_avail=""
27         if [ "$HOSTOS" == "IRIX" ]; then
28                 # check for the filestreams timeout systune variable in irix
29                 streams_avail=`systune $irix_timeout_sysvar 2>&1 |
30                         perl -ne 'if (/'$irix_timeout_sysvar'\s+=\s+\d+/) {print "true"}'`
31         else
32                 # check for the filestreams timeout proc entry in linux
33                 [ -f $linux_timeout_procvar ] && streams_avail="true"
34         fi
35
36         if [ "$streams_avail" == "true" ]; then
37                 return 0
38         else
39                 return 1
40         fi
41 }
42
43 _set_stream_timeout_centisecs()
44 {
45         local new_timeout_csecs=$1
46         local irix_timeout_sysvar="xfs_mfstream_timeout"
47         local linux_timeout_procvar="/proc/sys/fs/xfs/filestream_centisecs"
48         if [ "$HOSTOS" == "IRIX" ]; then
49                 echo y | systune -r $irix_timeout_sysvar $new_timeout_csecs >/dev/null
50         else
51                 echo $new_timeout_csecs > $linux_timeout_procvar
52         fi
53 }
54
55 _do_stream()
56 {
57         local directory_name=$1
58         local files=$2
59         local file_size=$3
60         local bsize=$4
61         local iflag=$5
62         local dio=$6
63         local blocks_in_file=`expr $file_size / $bsize`
64
65         mkdir $directory_name
66         if [ "$iflag" = "1" -a "$HOSTOS" != "IRIX" ]; then
67                 $XFS_IO_PROG -x -c "chattr +S" $directory_name \
68                         || _fail "chattr of filestream flag"
69         fi
70         cd $directory_name
71
72         local dd_cmd=""
73         if [ "$HOSTOS" == "IRIX" ]; then
74                 # for irix use lmdd
75                 dd_cmd="lmdd"
76                 [ "$dio" = "1" ] && dd_cmd="$dd_cmd odirect=1"
77         else
78                 # for linux use dd
79                 dd_cmd="dd"
80                 [ "$dio" = "1" ] && dd_cmd="$dd_cmd oflag=direct"
81         fi
82         dd_cmd="$dd_cmd if=/dev/zero bs=${bsize} count=${blocks_in_file}"
83
84         local i=1
85         while [ $i -le $files ]; do
86                 $dd_cmd of=frame-${i} 2>&1 | grep -v records | grep -v secs
87                 i=`expr $i + 1`
88         done
89 }
90
91 _filter_agno()
92 {
93         # the ag number is in column 4 of xfs_bmap output
94         perl -ne '
95                 $ag = (split /\s+/)[4] ;
96                 if ($ag =~ /\d+/) {print "$ag "} ;
97         '
98 }
99
100 _get_stream_ags()
101 {
102         local directory_name=$1
103         local stream_ags=`xfs_bmap -vp ${directory_name}/* | _filter_agno`
104         echo $stream_ags
105 }
106
107 _check_for_dupes()
108 {
109         # check for duplicate numbers between two space seperated vars
110         local num_str_one=$1
111         local num_str_two=$2
112
113         local this_num_one
114         local this_num_two
115         for this_num_one in $num_str_one; do
116                 for this_num_two in $num_str_two; do
117                         if [ "$this_num_one" == "$this_num_two" ]; then
118                                 echo "duplicate AG $this_num_one found" \
119                                         >> $here/$seq.full
120                                 return 1
121                         fi
122                 done
123         done
124         return 0
125 }
126
127 _test_streams() {
128
129         echo "# testing $* ...."
130         local agcount="$1"
131         local agsize="$2" # in MB
132         local stream_count="$3"
133         local stream_files="$4"
134         local stream_file_size=`expr $5 \* 1024 \* 1024`
135         local use_iflag="$6"
136         local use_directio="$7"
137         local expected_result="$8"      # "fail" if failure is expected
138
139         local size=`expr $agsize \* 1024 \* 1024 \* $agcount`
140         _scratch_mkfs_xfs -dsize=$size,agcount=$agcount >/dev/null 2>&1 \
141                 || _fail "mkfs failed"
142
143         if [ "$use_iflag" = "0" -o "$HOSTOS" == "IRIX" ]; then
144                 # mount using filestreams mount option
145                 _scratch_mount "-o filestreams" \
146                         || _fail "filestreams mount failed"
147         else
148                 # test will set inode flag
149                 _scratch_mount || _fail "mount failed"
150         fi
151
152         cd $SCRATCH_MNT
153
154         # start $stream_count streams
155         # each stream writes ($stream_files x $stream_file_size)M
156         echo "# streaming"
157         local stream_pids=""
158         local stream_index=1
159         while [ $stream_index -le $stream_count ]; do
160                 _do_stream stream${stream_index}-dir $stream_files \
161                         $stream_file_size 1048576 $use_iflag $use_directio &
162                 stream_pids="$stream_pids $!"
163                 stream_index=`expr $stream_index + 1`
164         done
165
166         # wait for streams to finish
167         # XXX wait here not needed? -dgc
168         wait $stream_pids
169
170         # sync the buffered streams out in parallel
171         # _get_stream_ags does a xfs_bmap which syncs delayed allocations
172         echo "# sync AGs..."
173         local ag_sync_pids=""
174         stream_index=1
175         while [ $stream_index -le $stream_count ]; do
176                 _get_stream_ags stream${stream_index}-dir > /dev/null 2>&1 &
177                 ag_sync_pids="$ag_sync_pids $!"
178                 stream_index=`expr $stream_index + 1`
179         done
180
181         # wait for syncs to finish
182         wait $ag_sync_pids
183
184         # confirm streams are in seperate AGs
185         echo "# checking stream AGs..."
186         local this_stream_ags=""
187         local ags_seen=""
188         local num_streams_with_matching_ags=0
189         stream_index=1
190         while [ $stream_index -le $stream_count ]; do
191                 this_stream_ags=`_get_stream_ags stream${stream_index}-dir`
192                 echo "stream $stream_index AGs: $this_stream_ags" >> $here/$seq.full
193                 _check_for_dupes "$ags_seen" "$this_stream_ags"
194                 if [ $? -ne 0 ]; then
195                         # this stream is not in seperate AGs to previous streams
196                         num_streams_with_matching_ags=`expr $num_streams_with_matching_ags + 1`
197                 fi
198                 ags_seen="$ags_seen $this_stream_ags"
199                 stream_index=`expr $stream_index + 1`
200         done
201
202         _cleanup_streams_umount
203         if [ "$expected_result" != "fail" ]; then
204                 if [ $num_streams_with_matching_ags -eq 0 ]; then
205                         # all streams in seperate AGs, as expected
206                         echo "+ passed, streams are in seperate AGs"
207                 else
208                         # streams with matching AGs, should be seperate
209                         _fail "- failed, $num_streams_with_matching_ags streams with matching AGs"
210                 fi
211         else
212                 # expecting streams to have overlapped
213                 if [ $num_streams_with_matching_ags -eq 0 ]; then
214                         # all streams in seperate AGs, should have overlapped
215                         _fail "- streams are in seperate AGs, expected _matching_"
216                 else
217                         # streams with matching AGs, as expected
218                         echo "+ expected failure, matching AGs"
219                 fi
220         fi
221         return 0
222 }
223
224 _cleanup_streams_umount()
225 {
226         cd /
227         rm -rf ${SCRATCH_MNT}/stream*
228         umount $SCRATCH_DEV 2>/dev/null
229 }