common/attr: set MAX_ATTR values correctly for NFS
[xfstests-dev.git] / common / filestreams
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2007 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # Core of filestreams tests.
6
7 _check_filestreams_support()
8 {
9         [ -f /proc/sys/fs/xfs/filestream_centisecs ]
10 }
11
12 _set_stream_timeout_centisecs()
13 {
14         echo $1 > /proc/sys/fs/xfs/filestream_centisecs
15 }
16
17 _do_stream()
18 {
19         local directory_name=$1
20         local files=$2
21         local file_size=$3
22         local bsize=$4
23         local iflag=$5
24         local dio=$6
25         local blocks_in_file=`expr $file_size / $bsize`
26
27         mkdir $directory_name
28         if [ "$iflag" = "1" ]; then
29                 $XFS_IO_PROG -x -c "chattr +S" $directory_name \
30                         || _fail "chattr of filestream flag"
31         fi
32         cd $directory_name
33
34         local dd_cmd="dd"
35         [ "$dio" = "1" ] && dd_cmd="$dd_cmd oflag=direct"
36         dd_cmd="$dd_cmd if=/dev/zero bs=${bsize} count=${blocks_in_file}"
37
38         local i=1
39         while [ $i -le $files ]; do
40                 $dd_cmd of=frame-${i} 2>&1 | grep -v records | grep -v secs
41                 i=`expr $i + 1`
42         done
43 }
44
45 _filter_agno()
46 {
47         # the ag number is in column 4 of xfs_bmap output
48         perl -ne '
49                 $ag = (split /\s+/)[4] ;
50                 if ($ag =~ /\d+/) {print "$ag "} ;
51         '
52 }
53
54 _get_stream_ags()
55 {
56         local directory_name=$1
57         local stream_ags=`xfs_bmap -vp ${directory_name}/* | _filter_agno`
58         echo $stream_ags
59 }
60
61 _check_for_dupes()
62 {
63         # check for duplicate numbers between two space seperated vars
64         local num_str_one=$1
65         local num_str_two=$2
66
67         local this_num_one
68         local this_num_two
69         for this_num_one in $num_str_one; do
70                 for this_num_two in $num_str_two; do
71                         if [ "$this_num_one" == "$this_num_two" ]; then
72                                 echo "duplicate AG $this_num_one found" \
73                                         >> $seqres.full
74                                 return 1
75                         fi
76                 done
77         done
78         return 0
79 }
80
81 _test_streams() {
82
83         echo "# testing $* ...."
84         local agcount="$1"
85         local agsize="$2" # in MB
86         local stream_count="$3"
87         local stream_files="$4"
88         local stream_file_size=`expr $5 \* 1024 \* 1024`
89         local use_iflag="$6"
90         local use_directio="$7"
91         local expected_result="$8"      # "fail" if failure is expected
92
93         local size=`expr $agsize \* 1024 \* 1024 \* $agcount`
94         _scratch_mkfs_xfs -dsize=$size,agcount=$agcount >/dev/null 2>&1 \
95                 || _fail "mkfs failed"
96
97         if [ "$use_iflag" = "0" ]; then
98                 # mount using filestreams mount option
99                 _try_scratch_mount "-o filestreams" \
100                         || _fail "filestreams mount failed"
101         else
102                 # test will set inode flag
103                 _scratch_mount
104         fi
105
106         cd $SCRATCH_MNT
107
108         # start $stream_count streams
109         # each stream writes ($stream_files x $stream_file_size)M
110         echo "# streaming"
111         local stream_pids=""
112         local stream_index=1
113         while [ $stream_index -le $stream_count ]; do
114                 _do_stream stream${stream_index}-dir $stream_files \
115                         $stream_file_size 1048576 $use_iflag $use_directio &
116                 stream_pids="$stream_pids $!"
117                 stream_index=`expr $stream_index + 1`
118         done
119
120         # wait for streams to finish
121         # XXX wait here not needed? -dgc
122         wait $stream_pids
123
124         # sync the buffered streams out in parallel
125         # _get_stream_ags does a xfs_bmap which syncs delayed allocations
126         echo "# sync AGs..."
127         local ag_sync_pids=""
128         stream_index=1
129         while [ $stream_index -le $stream_count ]; do
130                 _get_stream_ags stream${stream_index}-dir > /dev/null 2>&1 &
131                 ag_sync_pids="$ag_sync_pids $!"
132                 stream_index=`expr $stream_index + 1`
133         done
134
135         # wait for syncs to finish
136         wait $ag_sync_pids
137
138         # confirm streams are in seperate AGs
139         echo "# checking stream AGs..."
140         local this_stream_ags=""
141         local ags_seen=""
142         local num_streams_with_matching_ags=0
143         stream_index=1
144         while [ $stream_index -le $stream_count ]; do
145                 this_stream_ags=`_get_stream_ags stream${stream_index}-dir`
146                 echo "stream $stream_index AGs: $this_stream_ags" >> $seqres.full
147                 _check_for_dupes "$ags_seen" "$this_stream_ags"
148                 if [ $? -ne 0 ]; then
149                         # this stream is not in seperate AGs to previous streams
150                         num_streams_with_matching_ags=`expr $num_streams_with_matching_ags + 1`
151                 fi
152                 ags_seen="$ags_seen $this_stream_ags"
153                 stream_index=`expr $stream_index + 1`
154         done
155
156         _cleanup_streams_umount
157         if [ "$expected_result" != "fail" ]; then
158                 if [ $num_streams_with_matching_ags -eq 0 ]; then
159                         # all streams in seperate AGs, as expected
160                         echo "+ passed, streams are in seperate AGs"
161                 else
162                         # streams with matching AGs, should be seperate
163                         _fail "- failed, $num_streams_with_matching_ags streams with matching AGs"
164                 fi
165         else
166                 # expecting streams to have overlapped
167                 if [ $num_streams_with_matching_ags -eq 0 ]; then
168                         # all streams in seperate AGs, should have overlapped
169                         _fail "- streams are in seperate AGs, expected _matching_"
170                 else
171                         # streams with matching AGs, as expected
172                         echo "+ expected failure, matching AGs"
173                 fi
174         fi
175         return 0
176 }
177
178 _cleanup_streams_umount()
179 {
180         cd /
181         rm -rf ${SCRATCH_MNT}/stream*
182         _scratch_unmount 2>/dev/null
183 }