3 # Copyright (c) 2007 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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
19 # common functions for excersizing hole punches with extent size hints etc.
21 # source dmap_scratch_mount etc.
25 echo "# spawning test file with $*"
27 local file_size=`expr $2 \* $blksize`
28 local extent_size_hint=`expr $3 \* $blksize`
30 local reserve_space=$5
32 if [ $extent_size_hint -ne 0 ]; then
33 echo "+ setting extent size hint to $extent_size_hint"
35 -c "extsize $extent_size_hint" \
38 # print extent size hint for $test_file
43 if [ "$reserve_space" == "noresv" ]; then
44 echo "+ not using resvsp at file creation"
46 -c "truncate $file_size" \
50 -c "truncate $file_size" \
51 -c "resvsp 0 $file_size" \
57 echo "# punching with $*"
58 # punch or bite the ear off $test_file to create a hole
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
65 if [ "$punch_type" == "u" ]; then
66 echo "+ hole punch using unresvsp"
68 -c "unresvsp $punch_offset $punch_size" \
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
79 echo "# writing with $*"
81 local write_offset=`expr $2 \* $blksize`
82 local write_size=`expr $3 \* $blksize`
86 -c "pwrite $write_offset $write_size" \
91 echo "# showing file state $*"
100 echo "# testing $* ..."
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
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
118 _scratch_unmount >/dev/null 2>&1
120 _scratch_mkfs_xfs -bsize=$blksize >/dev/null 2>&1 \
121 || _fail "mkfs failed"
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
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"
133 # only unresvsp punch type is used, just do a normal mount
134 _scratch_mount || _fail "mount failed"
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"
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"
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"
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"
156 # create the file and setup extent size hint
157 _spawn_test_file $blksize $file_size_blks $extsize_hint_blks $filename $resvsp
159 # do the writes and punches
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]} \
168 punch_index=`expr $punch_index + 1`
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`
176 _do_bmap $filename # print out the state of the file
187 split(range, bounds, "[\\[ \\.\\]]");
191 if (type != prev_type) {
193 printf("%u]:%s\n", low - 1, prev_type);
194 printf("%u: [%u..", out_count++, low);
200 printf("%u]:%s\n", high, prev_type);
211 $5 ~ /0x[[:xdigit:]]*8[[:xdigit:]][[:xdigit:]]/ {
212 print $1, $2, "unwritten";
215 $5 ~ /0x[[:xdigit:]]+/ {
216 print $1, $2, "data";
221 # Filters fiemap output to only print the
222 # file offset column and whether or not
223 # it is an extent or a hole
224 _filter_hole_fiemap()
231 $5 ~ /0x[[:xdigit:]]+/ {
232 print $1, $2, "extent";
237 # 10000 Unwritten preallocated extent
238 # 01000 Doesn't begin on stripe unit
239 # 00100 Doesn't end on stripe unit
240 # 00010 Doesn't begin on stripe width
241 # 00001 Doesn't end on stripe width
249 $7 ~ /1[01][01][01][01]/ {
250 print $1, $2, "unwritten";
253 $7 ~ /0[01][01][01][01]/ {
265 # test the different corner cases for zeroing a range:
268 # 2. into allocated space
269 # 3. into unwritten space
271 # 5. hole -> unwritten
273 # 7. data -> unwritten
274 # 8. unwritten -> hole
275 # 9. unwritten -> data
276 # 10. hole -> data -> hole
277 # 11. data -> hole -> data
278 # 12. unwritten -> data -> unwritten
279 # 13. data -> unwritten -> data
280 # 14. data -> hole @ EOF
281 # 15. data -> hole @ 0
282 # 16. data -> cache cold ->hole
283 # 17. data -> hole in single block file
285 # Test file is removed, created and sync'd between tests.
287 # Use -k flag to keep the file between tests. This will
288 # test the handling of pre-existing holes.
290 # Use the -d flag to not sync the file between tests.
291 # This will test the handling of delayed extents
293 # Use the -u flag to not run unwritten tests.
294 # This will eliminate some unnecessary information.
296 _test_generic_punch()
303 while getopts 'dku' OPTION
317 shift $(($OPTIND - 1))
321 zero_cmd=$3 #if not testing zero just set to punch
328 # If we are testing collapse range, we increare all the offsets of this
329 # test by a factor of 4. We do this because unlike punch, collapse
330 # range also decreases the size of file hence require bigger offsets.
332 if [ "$zero_cmd" == "fcollapse" ]; then
336 _4k="$((multiple * 4))k"
337 _8k="$((multiple * 8))k"
338 _12k="$((multiple * 12))k"
339 _20k="$((multiple * 20))k"
341 # initial test state must be defined, otherwise the first test can fail
342 # due ot stale file state left from previous tests.
345 echo " 1. into a hole"
346 $XFS_IO_PROG -f -c "truncate $_20k" \
347 -c "$zero_cmd $_4k $_8k" \
348 -c "$map_cmd -v" $testfile | $filter_cmd
349 [ $? -ne 0 ] && die_now
350 _md5_checksum $testfile
352 echo " 2. into allocated space"
353 if [ "$remove_testfile" ]; then
356 $XFS_IO_PROG -f -c "truncate $_20k" \
357 -c "pwrite 0 $_20k" $sync_cmd \
358 -c "$zero_cmd $_4k $_8k" \
359 -c "$map_cmd -v" $testfile | $filter_cmd
360 [ $? -ne 0 ] && die_now
361 _md5_checksum $testfile
363 if [ "$unwritten_tests" ]; then
364 echo " 3. into unwritten space"
365 if [ "$remove_testfile" ]; then
368 $XFS_IO_PROG -f -c "truncate $_20k" \
369 -c "$alloc_cmd 0 $_20k" \
370 -c "$zero_cmd $_4k $_8k" \
371 -c "$map_cmd -v" $testfile | $filter_cmd
372 [ $? -ne 0 ] && die_now
373 _md5_checksum $testfile
376 echo " 4. hole -> data"
377 if [ "$remove_testfile" ]; then
380 $XFS_IO_PROG -f -c "truncate $_20k" \
381 -c "pwrite $_8k $_8k" $sync_cmd \
382 -c "$zero_cmd $_4k $_8k" \
383 -c "$map_cmd -v" $testfile | $filter_cmd
384 [ $? -ne 0 ] && die_now
385 _md5_checksum $testfile
387 if [ "$unwritten_tests" ]; then
388 echo " 5. hole -> unwritten"
389 if [ "$remove_testfile" ]; then
392 $XFS_IO_PROG -f -c "truncate $_20k" \
393 -c "$alloc_cmd $_8k $_8k" \
394 -c "$zero_cmd $_4k $_8k" \
395 -c "$map_cmd -v" $testfile | $filter_cmd
396 [ $? -ne 0 ] && die_now
397 _md5_checksum $testfile
400 echo " 6. data -> hole"
401 if [ "$remove_testfile" ]; then
404 $XFS_IO_PROG -f -c "truncate $_20k" \
405 -c "pwrite 0 $_8k" $sync_cmd \
406 -c "$zero_cmd $_4k $_8k" \
407 -c "$map_cmd -v" $testfile | $filter_cmd
408 [ $? -ne 0 ] && die_now
409 _md5_checksum $testfile
411 if [ "$unwritten_tests" ]; then
412 echo " 7. data -> unwritten"
413 if [ "$remove_testfile" ]; then
416 $XFS_IO_PROG -f -c "truncate $_20k" \
417 -c "pwrite 0 $_8k" $sync_cmd \
418 -c "$alloc_cmd $_8k $_8k" \
419 -c "$zero_cmd $_4k $_8k" \
420 -c "$map_cmd -v" $testfile | $filter_cmd
421 [ $? -ne 0 ] && die_now
422 _md5_checksum $testfile
424 echo " 8. unwritten -> hole"
425 if [ "$remove_testfile" ]; then
428 $XFS_IO_PROG -f -c "truncate $_20k" \
429 -c "$alloc_cmd 0 $_8k" \
430 -c "$zero_cmd $_4k $_8k" \
431 -c "$map_cmd -v" $testfile | $filter_cmd
432 [ $? -ne 0 ] && die_now
433 _md5_checksum $testfile
435 echo " 9. unwritten -> data"
436 if [ "$remove_testfile" ]; then
439 $XFS_IO_PROG -f -c "truncate $_20k" \
440 -c "$alloc_cmd 0 $_8k" \
441 -c "pwrite $_8k $_8k" $sync_cmd \
442 -c "$zero_cmd $_4k $_8k" \
443 -c "$map_cmd -v" $testfile | $filter_cmd
444 [ $? -ne 0 ] && die_now
445 _md5_checksum $testfile
448 echo " 10. hole -> data -> hole"
449 if [ "$remove_testfile" ]; then
452 $XFS_IO_PROG -f -c "truncate $_20k" \
453 -c "pwrite $_8k $_4k" $sync_cmd \
454 -c "$zero_cmd $_4k $_12k" \
455 -c "$map_cmd -v" $testfile | $filter_cmd
456 [ $? -ne 0 ] && die_now
457 _md5_checksum $testfile
459 echo " 11. data -> hole -> data"
460 if [ "$remove_testfile" ]; then
463 $XFS_IO_PROG -f -c "truncate $_20k" \
464 -c "$alloc_cmd 0 $_20k" \
466 -c "pwrite $_12k $_8k" $sync_cmd \
467 -c "$punch_cmd $_8k $_4k" \
468 -c "$zero_cmd $_4k $_12k" \
469 -c "$map_cmd -v" $testfile | $filter_cmd
470 [ $? -ne 0 ] && die_now
471 _md5_checksum $testfile
473 if [ "$unwritten_tests" ]; then
474 echo " 12. unwritten -> data -> unwritten"
475 if [ "$remove_testfile" ]; then
478 $XFS_IO_PROG -f -c "truncate $_20k" \
479 -c "$alloc_cmd 0 $_20k" \
480 -c "pwrite $_8k $_4k" $sync_cmd \
481 -c "$zero_cmd $_4k $_12k" \
482 -c "$map_cmd -v" $testfile | $filter_cmd
483 [ $? -ne 0 ] && die_now
484 _md5_checksum $testfile
486 echo " 13. data -> unwritten -> data"
487 if [ "$remove_testfile" ]; then
490 $XFS_IO_PROG -f -c "truncate $_20k" \
491 -c "$alloc_cmd 0 $_20k" \
492 -c "pwrite 0k $_4k" $sync_cmd \
493 -c "pwrite $_12k $_8k" -c "fsync" \
494 -c "$zero_cmd $_4k $_12k" \
495 -c "$map_cmd -v" $testfile | $filter_cmd
496 [ $? -ne 0 ] && die_now
497 _md5_checksum $testfile
500 # Don't need to check EOF case for collapse range.
501 # VFS layer return invalid error in this case,
502 # So it is not a proper case for collapse range test of each local fs.
503 if [ "$zero_cmd" != "fcollapse" ]; then
504 echo " 14. data -> hole @ EOF"
506 $XFS_IO_PROG -f -c "truncate $_20k" \
507 -c "pwrite 0 $_20k" $sync_cmd \
508 -c "$zero_cmd $_12k $_8k" \
509 -c "$map_cmd -v" $testfile | $filter_cmd
510 [ $? -ne 0 ] && die_now
511 _md5_checksum $testfile
514 if [ "$zero_cmd" == "fcollapse" ]; then
515 echo " 14. data -> hole @ 0"
517 echo " 15. data -> hole @ 0"
520 if [ "$remove_testfile" ]; then
523 $XFS_IO_PROG -f -c "truncate $_20k" \
524 -c "pwrite 0 $_20k" $sync_cmd \
525 -c "$zero_cmd 0 $_8k" \
526 -c "$map_cmd -v" $testfile | $filter_cmd
527 [ $? -ne 0 ] && die_now
528 _md5_checksum $testfile
530 # If zero_cmd is fcollpase, don't check unaligned offsets
531 if [ "$zero_cmd" == "fcollapse" ]; then
535 # If zero_cmd is finsert, don't check unaligned offsets
536 if [ "$zero_cmd" == "finsert" ]; then
540 echo " 16. data -> cache cold ->hole"
541 if [ "$remove_testfile" ]; then
545 cp $testfile $testfile.2
547 $XFS_IO_PROG -f -c "truncate $_20k" \
548 -c "pwrite $_8k $_12k" -c "fsync" $testfile.2 \
550 $XFS_IO_PROG -f -c "truncate $_20k" \
551 -c "pwrite 0 $_20k" $sync_cmd \
552 -c "$zero_cmd 0k $_8k" \
554 -c "$map_cmd -v" $testfile | $filter_cmd
555 diff $testfile $testfile.2
556 [ $? -ne 0 ] && die_now
558 _md5_checksum $testfile
560 # different file sizes mean we can't use md5sum to check the hole is
561 # valid. Hence use hexdump to dump the contents and chop off the last
562 # line of output that indicates the file size. We also have to fudge
563 # the extent size as that will change with file size, too - that's what
564 # the sed line noise does - it will always result in an output of [0..7]
565 # so it matches 4k block size...
566 echo " 17. data -> hole in single block file"
567 if [ "$remove_testfile" ]; then
570 block_size=`get_block_size $TEST_DIR`
571 $XFS_IO_PROG -f -c "truncate $block_size" \
572 -c "pwrite 0 $block_size" $sync_cmd \
573 -c "$zero_cmd 128 128" \
574 -c "$map_cmd -v" $testfile | $filter_cmd | \
575 sed -e "s/\.\.[0-9]*\]/..7\]/"
576 [ $? -ne 0 ] && die_now
577 od -x $testfile | head -n -1
580 _test_block_boundaries()
587 while getopts 'dk' OPTION
599 shift $(($OPTIND - 1))
614 # Block size multiplied by 2
617 # Block size divided by 2
621 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
622 -c "pwrite -S 0x42 $bs $bs" \
624 -c "pread -v 0 $bs_t2" \
625 $testfile | $filter_cmd
627 echo "zero 0, $bs_m1"
628 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
629 -c "pwrite -S 0x42 $bs $bs" \
630 -c "$zero_cmd 0 $bs_m1" \
631 -c "pread -v 0 $bs_t2" \
632 $testfile | $filter_cmd
635 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
636 -c "pwrite -S 0x42 $bs $bs" \
637 -c "$zero_cmd 0 $bs" \
638 -c "pread -v 0 $bs_t2" \
639 $testfile | $filter_cmd
641 echo "zero 0, $bs_p1"
642 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
643 -c "pwrite -S 0x42 $bs $bs" \
644 -c "$zero_cmd 0 $bs_p1" \
645 -c "pread -v 0 $bs_t2" \
646 $testfile | $filter_cmd
648 echo "zero $bs_m1, $bs"
649 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
650 -c "pwrite -S 0x42 $bs $bs" \
651 -c "$zero_cmd $bs_m1 $bs" \
652 -c "pread -v 0 $bs_t2" \
653 $testfile | $filter_cmd
655 echo "zero $bs_m1, $bs_p1"
656 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
657 -c "pwrite -S 0x42 $bs $bs" \
658 -c "$zero_cmd $bs_m1 $bs_p1" \
659 -c "pread -v 0 $bs_t2" \
660 $testfile | $filter_cmd
662 echo "zero $bs_m1, $bs_p2"
663 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
664 -c "pwrite -S 0x42 $bs $bs" \
665 -c "$zero_cmd $bs_m1 $bs_p2" \
666 -c "pread -v 0 $bs_t2" \
667 $testfile | $filter_cmd
671 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
672 -c "pwrite -S 0x42 $bs $bs" \
673 -c "$zero_cmd $bs $bs" \
674 -c "pread -v 0 $bs_t2" \
675 $testfile | $filter_cmd
678 echo "zero $bs_d2 , $bs"
679 $XFS_IO_PROG -f -t -c "pwrite -S 0x41 0 $bs" \
680 -c "pwrite -S 0x42 $bs $bs" \
681 -c "$zero_cmd $bs_d2 $bs" \
682 -c "pread -v 0 $bs_t2" \
683 $testfile | $filter_cmd