xfstests: support post-udev device mapper nodes
[xfstests-dev.git] / common.punch
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 # common functions for excersizing hole punches with extent size hints etc.
20
21 # source dmap_scratch_mount etc.
22 . ./common.dmapi
23
24 _spawn_test_file() {
25         echo "# spawning test file with $*"
26         local blksize=$1
27         local file_size=`expr $2 \* $blksize`
28         local extent_size_hint=`expr $3 \* $blksize`
29         local test_file=$4
30         local reserve_space=$5
31
32         if [ $extent_size_hint -ne 0 ]; then
33                 echo "+ setting extent size hint to $extent_size_hint"
34                 $XFS_IO_PROG -f \
35                 -c "extsize $extent_size_hint" \
36                 $test_file
37         fi
38         # print extent size hint for $test_file
39         $XFS_IO_PROG -f \
40         -c "extsize" \
41         $test_file
42
43         if [ "$reserve_space" == "noresv" ]; then
44                 echo "+ not using resvsp at file creation"
45                 $XFS_IO_PROG -f \
46                 -c "truncate $file_size" \
47                 $test_file
48         else
49                 $XFS_IO_PROG -f \
50                 -c "truncate $file_size" \
51                 -c "resvsp 0 $file_size" \
52                 $test_file
53         fi
54 }
55
56 _do_punch() {
57         echo "# punching with $*"
58         # punch or bite the ear off $test_file to create a hole
59         local blksize=$1
60         local punch_offset=`expr $2 \* $blksize`
61         local punch_size=`expr $3 \* $blksize`
62         local punch_type=$4             # u for unresvsp, d for dm_punch
63         local test_file=$5
64
65         if [ "$punch_type" == "u" ]; then
66                 echo "+ hole punch using unresvsp"
67                 $XFS_IO_PROG -f \
68                 -c "unresvsp $punch_offset $punch_size" \
69                 $test_file
70         fi
71         if [ "$punch_type" == "d" ]; then
72                 echo "+ hole punch using dmapi punch_hole"
73                 ${DMAPI_QASUITE1_DIR}cmd/punch_hole -o $punch_offset -l $punch_size \
74                         ${SCRATCH_MNT}/$test_file
75         fi
76 }
77
78 _do_write() {
79         echo "# writing with $*"
80         local blksize=$1
81         local write_offset=`expr $2 \* $blksize`
82         local write_size=`expr $3 \* $blksize`
83         local test_file=$4
84
85         $XFS_IO_PROG -f \
86         -c "pwrite $write_offset $write_size" \
87         $test_file >/dev/null
88 }
89
90 _do_bmap() {
91         echo "# showing file state $*"
92         local test_file=$1
93
94         $XFS_IO_PROG -f \
95         -c "bmap -vvp" \
96         $test_file
97 }
98
99 _test_punch() {
100         echo "# testing $* ..."
101         local blksize=$1
102         # all points and sizes below are in terms of filesystem blocks
103         local extsize_hint_blks=$2              # extent size hint in FS blocks, 0=do not set
104         local file_size_blks=$3                 # the file size in blocks
105         local punch_points_blks=( $4 )  # array of places to punch holes in the file
106         local punch_sizes_blks=( $5 )   # array of size of each punch in blocks
107         local punch_types=( $6  )               # array of u=unresvsp or d=dm_punch
108         local write_points_blks=( $7 )  # array of places to pwrite in the file
109         local write_sizes_blks=( $8 )   # array of size of each write
110
111         local punch_write_order=( $9 )  # array of punch/write operation order
112                                                                         # e.g. "w p w w p" means: do 1st write...
113                                                                         # then 1st punch, 2nd & 3rd write, 2nd punch
114         local resvsp=${10}                              # if "noresv" then don't resvsp on file create
115         local filename=punch_test_file
116
117         cd /
118         umount $SCRATCH_MNT >/dev/null 2>&1
119
120         _scratch_mkfs_xfs -bsize=$blksize >/dev/null 2>&1 \
121                 || _fail "mkfs failed"
122
123         local this_punch_type=""
124         local dmap_punch_used=0
125         for this_punch_type in "${punch_types[@]}"; do
126                 [ "$this_punch_type" == "d" ] && dmap_punch_used=1
127         done
128         if [ $dmap_punch_used -ne 0 ]; then
129                 # a punch type of dm_punch has been specified, do a dmapi mount
130                 echo "+ mounting with dmapi enabled"
131                 _dmapi_scratch_mount
132         else
133                 # only unresvsp punch type is used, just do a normal mount
134                 _scratch_mount || _fail "mount failed"
135         fi
136
137         cd $SCRATCH_MNT
138
139         # check a size is specified for each punch
140         [ ${#punch_points_blks[*]} -eq ${#punch_sizes_blks[*]} ] \
141                 || _fail "num punch points given does not equal num punch sizes"
142
143         # check a type is specified for each punch
144         [ ${#punch_points_blks[*]} -eq ${#punch_types[*]} ] \
145                 || _fail "num punch points given does not equal num punch types"
146
147         # check a size is specified for each write
148         [ ${#write_points_blks[*]} -eq ${#write_sizes_blks[*]} ] \
149                 || _fail "num write points given does not equal num write sizes"
150
151         # check punch_write_order operations match number of punches + writes
152         local total_pw_operations=`expr ${#punch_points_blks[*]} + ${#write_points_blks[*]}`
153         [ $total_pw_operations -eq ${#punch_write_order[*]} ] \
154                 || _fail "punch_write_order ops doesn't match number of punches + writes"
155
156         # create the file and setup extent size hint
157         _spawn_test_file $blksize $file_size_blks $extsize_hint_blks $filename $resvsp
158
159         # do the writes and punches
160         local operation=""
161         local punch_index=0
162         local write_index=0
163         for operation in "${punch_write_order[@]}"; do
164                 if [ "$operation" == "p" ]; then
165                         _do_punch $blksize ${punch_points_blks[$punch_index]} \
166                                 ${punch_sizes_blks[$punch_index]} ${punch_types[$punch_index]} \
167                                 $filename
168                         punch_index=`expr $punch_index + 1`
169                 fi
170                 if [ "$operation" == "w" ]; then
171                         _do_write $blksize ${write_points_blks[$write_index]} \
172                                 ${write_sizes_blks[$write_index]} $filename
173                         write_index=`expr $write_index + 1`
174                 fi
175                 sync
176                 _do_bmap $filename              # print out the state of the file
177         done
178 }
179
180 _coalesce_extents()
181 {
182         awk -F: '
183         {
184                 range = $2;
185                 type = $3;
186
187                 split(range, bounds, "[\\[ \\.\\]]");
188                 low = bounds[3];
189                 high = bounds[5];
190
191                 if (type != prev_type) {
192                         if (prev_type != "")
193                                 printf("%u]:%s\n", low - 1, prev_type);
194                         printf("%u: [%u..", out_count++, low);
195                         prev_type = type;
196                 }
197         }
198         END {
199                 if (prev_type != "")
200                         printf("%u]:%s\n", high, prev_type);
201         }'
202 }
203
204 _filter_fiemap()
205 {
206         awk --posix '
207                 $3 ~ /hole/ {
208                         print $1, $2, $3;
209                         next;
210                 }
211                 $5 ~ /0x[[:digit:]]*8[[:digit:]]{2}/ {
212                         print $1, $2, "unwritten";
213                         next;
214                 }
215                 $5 ~ /0x[[:digit:]]+/ {
216                         print $1, $2, "data";
217                 }' |
218         _coalesce_extents
219 }
220
221 _filter_bmap()
222 {
223         awk '
224                 $3 ~ /hole/ {
225                         print $1, $2, $3;
226                         next;
227                 }
228                 $7 ~ /10000/ {
229                         print $1, $2, "unwritten";
230                         next;
231                 }
232                 $7 ~ /00000/ {
233                         print $1, $2, "data"
234                 }' |
235         _coalesce_extents
236 }
237
238 die_now()
239 {
240         status=1
241         exit
242 }
243
244 # test the different corner cases for zeroing a range:
245 #
246 #       1. into a hole
247 #       2. into allocated space
248 #       3. into unwritten space
249 #       4. hole -> data
250 #       5. hole -> unwritten
251 #       6. data -> hole
252 #       7. data -> unwritten
253 #       8. unwritten -> hole
254 #       9. unwritten -> data
255 #       10. hole -> data -> hole
256 #       11. data -> hole -> data
257 #       12. unwritten -> data -> unwritten
258 #       13. data -> unwritten -> data
259 _test_generic_punch()
260 {
261         alloc_cmd=$1
262         punch_cmd=$2
263         zero_cmd=$3     #if not testing zero just set to punch
264         map_cmd=$4
265         filter_cmd=$5
266         testfile=$6
267         xfs_io_opt=$7   #needs to be -F if not testing xfs
268
269         echo "  1. into a hole"
270         rm -f $testfile
271         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
272                 -c "$zero_cmd 4k 8k" \
273                 -c "$map_cmd -v" $testfile | $filter_cmd
274         [ $? -ne 0 ] && die_now
275
276         echo "  2. into allocated space"
277         rm -f $testfile
278         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
279                 -c "pwrite 0 20k" -c "fsync" \
280                 -c "$zero_cmd 4k 8k" \
281                 -c "$map_cmd -v" $testfile | $filter_cmd
282         [ $? -ne 0 ] && die_now
283
284         echo "  3. into unwritten space"
285         rm -f $testfile
286         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
287                 -c "$alloc_cmd 0 20k" \
288                 -c "$zero_cmd 4k 8k" \
289                 -c "$map_cmd -v" $testfile | $filter_cmd
290         [ $? -ne 0 ] && die_now
291
292         echo "  4. hole -> data"
293         rm -f $testfile
294         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
295                 -c "pwrite 8k 8k" -c "fsync" \
296                 -c "$zero_cmd 4k 8k" \
297                 -c "$map_cmd -v" $testfile | $filter_cmd
298         [ $? -ne 0 ] && die_now
299
300         echo "  5. hole -> unwritten"
301         rm -f $testfile
302         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
303                 -c "$alloc_cmd 8k 8k" \
304                 -c "$zero_cmd 4k 8k" \
305                 -c "$map_cmd -v" $testfile | $filter_cmd
306         [ $? -ne 0 ] && die_now
307
308         echo "  6. data -> hole"
309         rm -f $testfile
310         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
311                 -c "pwrite 0 8k" -c "fsync" \
312                 -c "$zero_cmd 4k 8k" \
313                 -c "$map_cmd -v" $testfile | $filter_cmd
314         [ $? -ne 0 ] && die_now
315
316         echo "  7. data -> unwritten"
317         rm -f $testfile
318         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
319                 -c "pwrite 0 8k" -c "fsync" \
320                 -c "$alloc_cmd 8k 8k" \
321                 -c "$zero_cmd 4k 8k" \
322                 -c "$map_cmd -v" $testfile | $filter_cmd
323         [ $? -ne 0 ] && die_now
324
325         echo "  8. unwritten -> hole"
326         rm -f $testfile
327         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
328                 -c "$alloc_cmd 0 8k" \
329                 -c "$zero_cmd 4k 8k" \
330                 -c "$map_cmd -v" $testfile | $filter_cmd
331         [ $? -ne 0 ] && die_now
332
333         echo "  9. unwritten -> data"
334         rm -f $testfile
335         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
336                 -c "$alloc_cmd 0 8k" \
337                 -c "pwrite 8k 8k" -c "fsync" \
338                 -c "$zero_cmd 4k 8k" \
339                 -c "$map_cmd -v" $testfile | $filter_cmd
340         [ $? -ne 0 ] && die_now
341
342         echo "  10. hole -> data -> hole"
343         rm -f $testfile
344         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
345                 -c "pwrite 8k 4k" -c "fsync" \
346                 -c "$zero_cmd 4k 12k" \
347                 -c "$map_cmd -v" $testfile | $filter_cmd
348         [ $? -ne 0 ] && die_now
349
350         echo "  11. data -> hole -> data"
351         rm -f $testfile
352         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
353                 -c "$alloc_cmd 0 20k" \
354                 -c "pwrite 0 8k" \
355                 -c "pwrite 12k 8k" -c "fsync" \
356                 -c "$punch_cmd 8k 4k" \
357                 -c "$zero_cmd 4k 12k" \
358                 -c "$map_cmd -v" $testfile | $filter_cmd
359         [ $? -ne 0 ] && die_now
360
361         echo "  12. unwritten -> data -> unwritten"
362         rm -f $testfile
363         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
364                 -c "$alloc_cmd 0 20k" \
365                 -c "pwrite 8k 4k" -c "fsync" \
366                 -c "$zero_cmd 4k 12k" \
367                 -c "$map_cmd -v" $testfile | $filter_cmd
368         [ $? -ne 0 ] && die_now
369
370         echo "  13. data -> unwritten -> data"
371         rm -f $testfile
372         $XFS_IO_PROG $xfs_io_opt -f -c "truncate 20k" \
373                 -c "$alloc_cmd 0 20k" \
374                 -c "pwrite 0k 8k" -c "fsync" \
375                 -c "pwrite 12k 8k" -c "fsync" \
376                 -c "$zero_cmd 4k 12k" \
377                 -c "$map_cmd -v" $testfile | $filter_cmd
378         [ $? -ne 0 ] && die_now
379 }