src/idmapped-mounts: use renameat instead of renameat2
[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         # Disable the scratch rt device to avoid test failures relating to the
94         # rt bitmap consuming free space in our small data device and throwing
95         # off the filestreams allocator.
96         unset SCRATCH_RTDEV
97
98         local size=`expr $agsize \* 1024 \* 1024 \* $agcount`
99         _scratch_mkfs_xfs -dsize=$size,agcount=$agcount >/dev/null 2>&1 \
100                 || _fail "mkfs failed"
101
102         if [ "$use_iflag" = "0" ]; then
103                 # mount using filestreams mount option
104                 _try_scratch_mount "-o filestreams" \
105                         || _fail "filestreams mount failed"
106         else
107                 # test will set inode flag
108                 _scratch_mount
109         fi
110
111         cd $SCRATCH_MNT
112
113         # start $stream_count streams
114         # each stream writes ($stream_files x $stream_file_size)M
115         echo "# streaming"
116         local stream_pids=""
117         local stream_index=1
118         while [ $stream_index -le $stream_count ]; do
119                 _do_stream stream${stream_index}-dir $stream_files \
120                         $stream_file_size 1048576 $use_iflag $use_directio &
121                 stream_pids="$stream_pids $!"
122                 stream_index=`expr $stream_index + 1`
123         done
124
125         # wait for streams to finish
126         # XXX wait here not needed? -dgc
127         wait $stream_pids
128
129         # sync the buffered streams out in parallel
130         # _get_stream_ags does a xfs_bmap which syncs delayed allocations
131         echo "# sync AGs..."
132         local ag_sync_pids=""
133         stream_index=1
134         while [ $stream_index -le $stream_count ]; do
135                 _get_stream_ags stream${stream_index}-dir > /dev/null 2>&1 &
136                 ag_sync_pids="$ag_sync_pids $!"
137                 stream_index=`expr $stream_index + 1`
138         done
139
140         # wait for syncs to finish
141         wait $ag_sync_pids
142
143         # confirm streams are in seperate AGs
144         echo "# checking stream AGs..."
145         local this_stream_ags=""
146         local ags_seen=""
147         local num_streams_with_matching_ags=0
148         stream_index=1
149         while [ $stream_index -le $stream_count ]; do
150                 this_stream_ags=`_get_stream_ags stream${stream_index}-dir`
151                 echo "stream $stream_index AGs: $this_stream_ags" >> $seqres.full
152                 _check_for_dupes "$ags_seen" "$this_stream_ags"
153                 if [ $? -ne 0 ]; then
154                         # this stream is not in seperate AGs to previous streams
155                         num_streams_with_matching_ags=`expr $num_streams_with_matching_ags + 1`
156                 fi
157                 ags_seen="$ags_seen $this_stream_ags"
158                 stream_index=`expr $stream_index + 1`
159         done
160
161         _cleanup_streams_umount
162         if [ "$expected_result" != "fail" ]; then
163                 if [ $num_streams_with_matching_ags -eq 0 ]; then
164                         # all streams in seperate AGs, as expected
165                         echo "+ passed, streams are in seperate AGs"
166                 else
167                         # streams with matching AGs, should be seperate
168                         _fail "- failed, $num_streams_with_matching_ags streams with matching AGs"
169                 fi
170         else
171                 # expecting streams to have overlapped
172                 if [ $num_streams_with_matching_ags -eq 0 ]; then
173                         # all streams in seperate AGs, should have overlapped
174                         _fail "- streams are in seperate AGs, expected _matching_"
175                 else
176                         # streams with matching AGs, as expected
177                         echo "+ expected failure, matching AGs"
178                 fi
179         fi
180         return 0
181 }
182
183 _cleanup_streams_umount()
184 {
185         cd /
186         rm -rf ${SCRATCH_MNT}/stream*
187         _scratch_unmount 2>/dev/null
188 }