common: use _scratch_mount helper in _require_relatime()
[xfstests-dev.git] / common / rc
1 ##/bin/bash
2 #-----------------------------------------------------------------------
3 #  Copyright (c) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will 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 to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17 #  USA
18 #
19 #  Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
20 #  Mountain View, CA 94043, USA, or: http://www.sgi.com
21 #-----------------------------------------------------------------------
22
23 BC=$(which bc 2> /dev/null) || BC=
24
25 _require_math()
26 {
27         if [ -z "$BC" ]; then
28                 _notrun "this test requires 'bc' tool for doing math operations"
29         fi
30 }
31
32 _math()
33 {
34         [ $# -le 0 ] && return
35         if [ "$BC" ]; then
36                 result=$(LANG=C echo "scale=0; $@" | "$BC" -q 2> /dev/null)
37         else
38                 _notrun "this test requires 'bc' tool for doing math operations"
39         fi
40         echo "$result"
41 }
42
43 dd()
44 {
45    if [ "$HOSTOS" == "Linux" ]
46    then 
47         command dd --help 2>&1 | grep noxfer >/dev/null
48         
49         if [ "$?" -eq 0 ]
50             then
51                 command dd status=noxfer $@
52             else
53                 command dd $@
54         fi
55    else
56         command dd $@
57    fi
58 }
59
60 _btrfs_get_subvolid()
61 {
62         mnt=$1
63         name=$2
64
65         $BTRFS_UTIL_PROG sub list $mnt | grep $name | awk '{ print $2 }'
66 }
67
68 # Prints the md5 checksum of a given file
69 _md5_checksum()
70 {
71         md5sum $1 | cut -d ' ' -f1
72 }
73
74
75 # ls -l w/ selinux sometimes puts a dot at the end:
76 # -rwxrw-r--. id1 id2 file1
77 # Also filter out lost+found directory on extN file system if present
78
79 _ls_l()
80 {
81         ls -l $* | sed "s/\(^[-rwxdlbcpsStT]*\)\. /\1 /" | grep -v 'lost+found'
82 }
83
84 # we need common/config
85 if [ "$iam" != "check" ]
86 then
87     if ! . ./common/config
88         then
89         echo "$iam: failed to source common/config"
90         exit 1
91     fi
92 fi
93
94 # check for correct setup
95 case "$FSTYP" in
96     xfs)
97          [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
98          [ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
99          [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
100          [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found"
101          ;;
102     udf)
103          [ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
104          ;;
105     btrfs)
106          [ "$MKFS_BTRFS_PROG" = "" ] && _fatal "mkfs.btrfs not found"
107          ;;
108     ext4)
109          [ "$MKFS_EXT4_PROG" = "" ] && _fatal "mkfs.ext4 not found"
110          ;;
111     nfs)
112          ;;
113     cifs)
114          ;;
115 esac
116
117 # make sure we have a standard umask
118 umask 022
119
120 _mount()
121 {
122     $MOUNT_PROG `_mount_ops_filter $*`
123 }
124
125 _scratch_options()
126 {
127     type=$1
128     SCRATCH_OPTIONS=""
129
130     if [ "$FSTYP" != "xfs" ]; then
131         return
132     fi
133
134     case $type in
135     mkfs)
136         [ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
137         rt_opt="-r"
138         log_opt="-l"
139         ;;
140     mount)
141         rt_opt="-o"
142         log_opt="-o"
143         ;;
144     esac
145     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
146         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
147     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
148         SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
149 }
150
151 _test_options()
152 {
153     type=$1
154     TEST_OPTIONS=""
155
156     if [ "$FSTYP" != "xfs" ]; then
157         return
158     fi
159
160     case $type in
161     mkfs)
162         rt_opt="-r"
163         log_opt="-l"
164         ;;
165     mount)
166         rt_opt="-o"
167         log_opt="-o"
168         ;;
169     esac
170     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
171         TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
172     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
173         TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
174 }
175
176 _mount_ops_filter()
177 {
178     params="$*"
179     
180     #get mount point to handle dmapi mtpt option correctly
181     let last_index=$#-1
182     [ $last_index -gt 0 ] && shift $last_index
183     FS_ESCAPED=$1
184     
185     # irix is fussy about how it is fed its mount options
186     # - multiple -o's are not allowed
187     # - no spaces between comma delimitered options
188     # the sed script replaces all -o's (except the first) with a comma
189     # not required for linux, but won't hurt
190     
191     echo $params | sed -e 's/[[:space:]]\+-o[[:space:]]*/UnIqUe/1; s/[[:space:]]\+-o[[:space:]]*/,/g; s/UnIqUe/ -o /1' \
192         | sed -e 's/dmapi/dmi/' \
193         | $PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$FS_ESCAPED\1\2#; print;"
194
195 }
196
197 _scratch_mount_options()
198 {
199     _scratch_options mount
200
201     echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
202 }
203
204 _scratch_mount()
205 {
206     _mount -t $FSTYP `_scratch_mount_options $*`
207 }
208
209 _scratch_unmount()
210 {
211     $UMOUNT_PROG $SCRATCH_DEV
212 }
213
214 _scratch_remount()
215 {
216     _scratch_unmount
217     _scratch_mount
218 }
219
220 _test_mount()
221 {
222     _test_options mount
223     _mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS $* $TEST_DEV $TEST_DIR
224 }
225
226 _test_remount()
227 {
228     $UMOUNT_PROG $TEST_DEV
229     _test_mount
230 }
231
232 _scratch_mkfs_options()
233 {
234     _scratch_options mkfs
235     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
236 }
237
238 _scratch_metadump()
239 {
240         dumpfile=$1
241         options=
242
243         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
244                 options="-l $SCRATCH_LOGDEV"
245
246         xfs_metadump $options $SCRATCH_DEV $dumpfile
247 }
248
249 _setup_large_xfs_fs()
250 {
251         fs_size=$1
252         local tmp_dir=/tmp/
253
254         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
255         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
256         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
257
258         # calculate the size of the file we need to allocate.
259         # Default free space in the FS is 50GB, but you can specify more via
260         # SCRATCH_DEV_EMPTY_SPACE
261         file_size=$(($fs_size - 50*1024*1024*1024))
262         file_size=$(($file_size - $SCRATCH_DEV_EMPTY_SPACE))
263
264         # mount the filesystem, create the file, unmount it
265         _scratch_mount 2>&1 >$tmp_dir/mnt.err
266         local status=$?
267         if [ $status -ne 0 ]; then
268                 echo "mount failed"
269                 cat $tmp_dir/mnt.err >&2
270                 rm -f $tmp_dir/mnt.err
271                 return $status
272         fi
273         rm -f $tmp_dir/mnt.err
274
275         xfs_io -F -f \
276                 -c "truncate $file_size" \
277                 -c "falloc -k 0 $file_size" \
278                 -c "chattr +d" \
279                 $SCRATCH_MNT/.use_space 2>&1 > /dev/null
280         export NUM_SPACE_FILES=1
281         status=$?
282         umount $SCRATCH_MNT
283         if [ $status -ne 0 ]; then
284                 echo "large file prealloc failed"
285                 cat $tmp_dir/mnt.err >&2
286                 return $status
287         fi
288         return 0
289 }
290
291 _scratch_mkfs_xfs_opts()
292 {
293         mkfs_opts=$*
294
295         _scratch_options mkfs
296
297         $MKFS_XFS_PROG $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
298 }
299
300
301 _scratch_mkfs_xfs_supported()
302 {
303         mkfs_opts=$*
304
305         _scratch_options mkfs
306
307         $MKFS_XFS_PROG -N $MKFS_OPTIONS $SCRATCH_OPTIONS $mkfs_opts $SCRATCH_DEV
308 }
309
310 _scratch_mkfs_xfs()
311 {
312         # extra mkfs options can be added by tests
313         local extra_mkfs_options=$*
314
315         local tmp_dir=/tmp/
316
317         # save mkfs output in case conflict means we need to run again.
318         # only the output for the mkfs that applies should be shown
319         _scratch_mkfs_xfs_opts $MKFS_OPTIONS $extra_mkfs_options \
320                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
321         local mkfs_status=$?
322
323
324         # a mkfs failure may be caused by conflicts between
325         # $MKFS_OPTIONS and $extra_mkfs_options
326         if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
327                 (
328                 echo -n "** mkfs failed with extra mkfs options "
329                 echo "added to \"$MKFS_OPTIONS\" by test $seq **"
330                 echo -n "** attempting to mkfs using only test $seq "
331                 echo "options: $extra_mkfs_options **"
332                 ) >> $seqres.full
333
334                 # running mkfs again. overwrite previous mkfs output files
335                 _scratch_mkfs_xfs_opts $extra_mkfs_options \
336                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
337                 local mkfs_status=$?
338         fi
339
340         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
341                 # manually parse the mkfs output to get the fs size in bytes
342                 local fs_size
343                 fs_size=`cat $tmp_dir.mkfsstd | perl -ne '
344                         if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+)/) {
345                                 my $size = $1 * $2;
346                                 print STDOUT "$size\n";
347                         }'`
348                 _setup_large_xfs_fs $fs_size
349                 mkfs_status=$?
350         fi
351
352         # output stored mkfs output
353         cat $tmp_dir.mkfserr >&2
354         cat $tmp_dir.mkfsstd
355         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
356
357         return $mkfs_status
358 }
359
360 # xfs_check script is planned to be deprecated. But, we want to
361 # be able to invoke "xfs_check" behavior in xfstests in order to
362 # maintain the current verification levels.
363 _xfs_check()
364 {
365     OPTS=" "
366     DBOPTS=" "
367     USAGE="Usage: xfs_check [-fsvV] [-l logdev] [-i ino]... [-b bno]... special"
368
369     while getopts "b:fi:l:stvV" c
370     do
371         case $c in
372             s) OPTS=$OPTS"-s ";;
373             t) OPTS=$OPTS"-t ";;
374             v) OPTS=$OPTS"-v ";;
375             i) OPTS=$OPTS"-i "$OPTARG" ";;
376             b) OPTS=$OPTS"-b "$OPTARG" ";;
377             f) DBOPTS=$DBOPTS" -f";;
378             l) DBOPTS=$DBOPTS" -l "$OPTARG" ";;
379             V) $XFS_DB_PROG -p xfs_check -V
380                 return $?
381                 ;;
382         esac
383     done
384     set -- extra $@
385     shift $OPTIND
386     case $# in
387         1)    ${XFS_DB_PROG}${DBOPTS} -F -i -p xfs_check -c "check$OPTS" $1
388                status=$?
389                ;;
390         2)    echo $USAGE 1>&1
391               status=2
392               ;;
393     esac
394     return $status
395 }
396
397 _setup_large_ext4_fs()
398 {
399         fs_size=$1
400         local tmp_dir=/tmp/
401
402         [ "$LARGE_SCRATCH_DEV" != yes ] && return 0
403         [ -z "$SCRATCH_DEV_EMPTY_SPACE" ] && SCRATCH_DEV_EMPTY_SPACE=0
404         [ $SCRATCH_DEV_EMPTY_SPACE -ge $fs_size ] && return 0
405
406         # Default free space in the FS is 50GB, but you can specify more via
407         # SCRATCH_DEV_EMPTY_SPACE
408         space_to_consume=$(($fs_size - 50*1024*1024*1024 - $SCRATCH_DEV_EMPTY_SPACE))
409
410         # mount the filesystem and create 16TB - 4KB files until we consume
411         # all the necessary space.
412         _scratch_mount 2>&1 >$tmp_dir/mnt.err
413         local status=$?
414         if [ $status -ne 0 ]; then
415                 echo "mount failed"
416                 cat $tmp_dir/mnt.err >&2
417                 rm -f $tmp_dir/mnt.err
418                 return $status
419         fi
420         rm -f $tmp_dir/mnt.err
421
422         file_size=$((16*1024*1024*1024*1024 - 4096))
423         nfiles=0
424         while [ $space_to_consume -gt $file_size ]; do
425
426                 xfs_io -F -f \
427                         -c "truncate $file_size" \
428                         -c "falloc -k 0 $file_size" \
429                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
430                 status=$?
431                 if [ $status -ne 0 ]; then
432                         break;
433                 fi
434
435                 space_to_consume=$(( $space_to_consume - $file_size ))
436                 nfiles=$(($nfiles + 1))
437         done
438
439         # consume the remaining space.
440         if [ $space_to_consume -gt 0 ]; then
441                 xfs_io -F -f \
442                         -c "truncate $space_to_consume" \
443                         -c "falloc -k 0 $space_to_consume" \
444                         $SCRATCH_MNT/.use_space.$nfiles 2>&1
445                 status=$?
446         fi
447         export NUM_SPACE_FILES=$nfiles
448
449         umount $SCRATCH_MNT
450         if [ $status -ne 0 ]; then
451                 echo "large file prealloc failed"
452                 cat $tmp_dir/mnt.err >&2
453                 return $status
454         fi
455         return 0
456 }
457
458 _scratch_mkfs_ext4()
459 {
460         # extra mkfs options can be added by tests
461         local extra_mkfs_options=$*
462
463         local tmp_dir=/tmp/
464
465         $MKFS_EXT4_PROG -F $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
466                         2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
467         local mkfs_status=$?
468
469         # a mkfs failure may be caused by conflicts between
470         # $MKFS_OPTIONS and $extra_mkfs_options
471         if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
472                 (
473                 echo -n "** mkfs failed with extra mkfs options "
474                 echo "added to \"$MKFS_OPTIONS\" by test $seq **"
475                 echo -n "** attempting to mkfs using only test $seq "
476                 echo "options: $extra_mkfs_options **"
477                 ) >> $seqres.full
478
479                 # running mkfs again. overwrite previous mkfs output files
480                 $MKFS_EXT4_PROG -F $extra_mkfs_options $SCRATCH_DEV \
481                                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
482                 local mkfs_status=$?
483         fi
484
485         if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
486                 # manually parse the mkfs output to get the fs size in bytes
487                 fs_size=`cat $tmp_dir.mkfsstd | awk ' \
488                         /^Block size/ { split($2, a, "="); bs = a[2] ; } \
489                         / inodes, / { blks = $3 } \
490                         /reserved for the super user/ { resv = $1 } \
491                         END { fssize = bs * blks - resv; print fssize }'`
492
493                 _setup_large_ext4_fs $fs_size
494                 mkfs_status=$?
495         fi
496
497         # output stored mkfs output
498         cat $tmp_dir.mkfserr >&2
499         cat $tmp_dir.mkfsstd
500         rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
501
502         return $mkfs_status
503 }
504
505 _test_mkfs()
506 {
507     case $FSTYP in
508     nfs*)
509         # do nothing for nfs
510         ;;
511     cifs)
512         # do nothing for cifs
513         ;;
514     udf)
515         $MKFS_UDF_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
516         ;;
517     btrfs)
518         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $TEST_DEV > /dev/null
519         ;;
520     *)
521         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $TEST_DEV
522         ;;
523     esac
524 }
525
526 _mkfs_dev()
527 {
528     case $FSTYP in
529     nfs*)
530         # do nothing for nfs
531         ;;
532     udf)
533         $MKFS_UDF_PROG $MKFS_OPTIONS $* 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
534         ;;
535     btrfs)
536         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
537         ;;
538     *)
539         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* \
540                 2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
541         ;;
542     esac
543
544     if [ $? -ne 0 ]; then
545         # output stored mkfs output
546         cat $tmp_dir.mkfserr >&2
547         cat $tmp_dir.mkfsstd
548         status=1
549         exit 1
550     fi
551     rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
552 }
553
554 # remove all files in $SCRATCH_MNT, useful when testing on NFS/CIFS
555 _scratch_cleanup_files()
556 {
557         _scratch_mount
558         rm -rf $SCRATCH_MNT/*
559         _scratch_unmount
560 }
561
562 _scratch_mkfs()
563 {
564     case $FSTYP in
565     xfs)
566         _scratch_mkfs_xfs $*
567         ;;
568     nfs*)
569         # unable to re-create NFS, just remove all files in $SCRATCH_MNT to
570         # avoid EEXIST caused by the leftover files created in previous runs
571         _scratch_cleanup_files
572         ;;
573     cifs)
574         # do nothing for cifs
575         ;;
576     udf)
577         $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
578         ;;
579     btrfs)
580         $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
581         ;;
582     ext4)
583         _scratch_mkfs_ext4 $*
584         ;;
585     tmpfs)
586         # do nothing for tmpfs
587         ;;
588     *)
589         yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV
590         ;;
591     esac
592 }
593
594 _scratch_pool_mkfs()
595 {
596     case $FSTYP in
597     btrfs)
598         # if dup profile is in mkfs options call _scratch_mkfs instead
599         # because dup profile only works with single device
600         if [[ "$*" =~ dup ]]; then
601             _scratch_mkfs $*
602         else
603             $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV_POOL > /dev/null
604         fi
605         ;;
606     *)
607         echo "_scratch_pool_mkfs is not implemented for $FSTYP" 1>&2
608         ;;
609     esac
610 }
611
612 # Create fs of certain size on scratch device
613 # _scratch_mkfs_sized <size in bytes> [optional blocksize]
614 _scratch_mkfs_sized()
615 {
616     fssize=$1
617     blocksize=$2
618
619     case $FSTYP in
620     xfs)
621         def_blksz=`echo $MKFS_OPTIONS|sed -rn 's/.*-b ?size= ?+([0-9]+).*/\1/p'`
622         ;;
623     ext2|ext3|ext4|ext4dev|udf|btrfs)
624         def_blksz=`echo $MKFS_OPTIONS| sed -rn 's/.*-b ?+([0-9]+).*/\1/p'`
625         ;;
626     esac
627
628     [ -n "$def_blksz" ] && blocksize=$def_blksz
629     [ -z "$blocksize" ] && blocksize=4096
630
631
632     re='^[0-9]+$'
633     if ! [[ $fssize =~ $re ]] ; then
634         _notrun "error: _scratch_mkfs_sized: fs size \"$fssize\" not an integer."
635     fi
636     if ! [[ $blocksize =~ $re ]] ; then
637         _notrun "error: _scratch_mkfs_sized: block size \"$blocksize\" not an integer."
638     fi
639
640     blocks=`expr $fssize / $blocksize`
641
642     if [ "$HOSTOS" == "Linux" ]; then
643         devsize=`blockdev --getsize64 $SCRATCH_DEV`
644         [ "$fssize" -gt "$devsize" ] && _notrun "Scratch device too small"
645     fi
646
647     case $FSTYP in
648     xfs)
649         # don't override MKFS_OPTIONS that set a block size.
650         echo $MKFS_OPTIONS |egrep -q "b?size="
651         if [ $? -eq 0 ]; then
652                 _scratch_mkfs_xfs -d size=$fssize
653         else
654                 _scratch_mkfs_xfs -d size=$fssize -b size=$blocksize
655         fi
656         ;;
657     ext2|ext3|ext4|ext4dev)
658         yes | ${MKFS_PROG}.$FSTYP $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
659         ;;
660     udf)
661         $MKFS_UDF_PROG $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV $blocks
662         ;;
663     btrfs)
664         $MKFS_BTRFS_PROG $MKFS_OPTIONS -b $fssize $SCRATCH_DEV
665         ;;
666     *)
667         _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized"
668         ;;
669     esac
670 }
671
672 # Emulate an N-data-disk stripe w/ various stripe units
673 # _scratch_mkfs_geom <sunit bytes> <swidth multiplier> [optional blocksize]
674 _scratch_mkfs_geom()
675 {
676     sunit_bytes=$1
677     swidth_mult=$2
678     blocksize=$3
679     [ -z "$blocksize" ] && blocksize=4096
680
681     let sunit_blocks=$sunit_bytes/$blocksize
682     let swidth_blocks=$sunit_blocks*$swidth_mult
683
684     case $FSTYP in
685     xfs)
686         MKFS_OPTIONS+=" -b size=$blocksize, -d su=$sunit_bytes,sw=$swidth_mult"
687         ;;
688     ext4|ext4dev)
689         MKFS_OPTIONS+=" -b $blocksize -E stride=$sunit_blocks,stripe_width=$swidth_blocks"
690         ;;
691     *)
692         _notrun "can't mkfs $FSTYP with geometry"
693         ;;
694     esac
695     _scratch_mkfs
696 }
697
698 _scratch_resvblks()
699 {
700         case $FSTYP in
701         xfs)
702                 xfs_io -x -c "resblks $1" $SCRATCH_MNT
703                 ;;
704         *)
705                 ;;
706         esac
707 }
708
709 _scratch_xfs_db_options()
710 {
711     SCRATCH_OPTIONS=""
712     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
713         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
714     echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
715 }
716
717 _scratch_xfs_logprint()
718 {
719     SCRATCH_OPTIONS=""
720     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
721         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
722     $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
723 }
724
725 _scratch_xfs_check()
726 {
727     SCRATCH_OPTIONS=""
728     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
729         SCRATCH_OPTIONS="-l $SCRATCH_LOGDEV"
730     [ "$LARGE_SCRATCH_DEV" = yes ] && \
731         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
732     _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
733 }
734
735 _scratch_xfs_repair()
736 {
737     SCRATCH_OPTIONS=""
738     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
739         SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
740     [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
741         SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
742     [ "$LARGE_SCRATCH_DEV" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
743     $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
744 }
745
746 _get_pids_by_name()
747 {
748     if [ $# -ne 1 ]
749     then
750         echo "Usage: _get_pids_by_name process-name" 1>&2
751         exit 1
752     fi
753
754     # Algorithm ... all ps(1) variants have a time of the form MM:SS or
755     # HH:MM:SS before the psargs field, use this as the search anchor.
756     #
757     # Matches with $1 (process-name) occur if the first psarg is $1
758     # or ends in /$1 ... the matching uses sed's regular expressions,
759     # so passing a regex into $1 will work.
760
761     ps $PS_ALL_FLAGS \
762     | sed -n \
763         -e 's/$/ /' \
764         -e 's/[         ][      ]*/ /g' \
765         -e 's/^ //' \
766         -e 's/^[^ ]* //' \
767         -e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
768         -e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
769 }
770
771 # fix malloc libs output
772 #
773 _fix_malloc()
774 {
775     # filter out the Electric Fence notice
776     $PERL_PROG -e '
777         while (<>) {
778             if (defined $o && /^\s+Electric Fence/) {
779                 chomp($o);
780                 print "$o";
781                 undef $o;
782                 next;
783             }
784             print $o if (defined $o);
785
786             $o=$_;
787         }
788         print $o if (defined $o);
789     '
790 }
791
792 # check if run as root
793 #
794 _need_to_be_root()
795 {
796     id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
797     if [ "$id" -ne 0 ]
798     then
799         echo "Arrgh ... you need to be root (not uid=$id) to run this test"
800         exit 1
801     fi
802 }
803
804
805 #
806 # _df_device : get an IRIX style df line for a given device
807 #
808 #       - returns "" if not mounted
809 #       - returns fs type in field two (ala IRIX)
810 #       - joins line together if split by fancy df formatting
811 #       - strips header etc
812 #
813
814 _df_device()
815 {
816     if [ $# -ne 1 ]
817     then
818         echo "Usage: _df_device device" 1>&2
819         exit 1
820     fi
821
822     # Note that we use "==" here so awk doesn't try to interpret an NFS over
823     # IPv6 server as a regular expression.
824     $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
825         ($1==what) && (NF==1) {
826             v=$1
827             getline
828             print v, $0
829             exit
830         }
831         ($1==what) {
832             print
833             exit
834         }
835     '
836 }
837
838 #
839 # _df_dir : get an IRIX style df line for device where a directory resides
840 #
841 #       - returns fs type in field two (ala IRIX)
842 #       - joins line together if split by fancy df formatting
843 #       - strips header etc
844 #
845
846 _df_dir()
847 {
848     if [ $# -ne 1 ]
849     then
850         echo "Usage: _df_dir device" 1>&2
851         exit 1
852     fi
853
854     $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
855         NR == 2 && NF==1 {
856             v=$1
857             getline
858             print v, $0;
859             exit 0
860         }
861         NR == 2 {
862             print;
863             exit 0
864         }
865         {}
866     '
867     # otherwise, nada
868 }
869
870 # return percentage used disk space for mounted device
871
872 _used()
873 {
874     if [ $# -ne 1 ]
875     then
876         echo "Usage: _used device" 1>&2
877         exit 1
878     fi
879
880     _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
881 }
882
883 # return the FS type of a mounted device
884 #
885 _fs_type()
886 {
887     if [ $# -ne 1 ]
888     then
889         echo "Usage: _fs_type device" 1>&2
890         exit 1
891     fi
892
893     #
894     # The Linux kernel shows NFSv4 filesystems in df output as
895     # filesystem type nfs4, although we mounted it as nfs earlier.
896     # Fix the filesystem type up here so that the callers don't
897     # have to bother with this quirk.
898     #
899     _df_device $1 | $AWK_PROG '{ print $2 }' | sed -e 's/nfs4/nfs/'
900 }
901
902 # return the FS mount options of a mounted device
903 #
904 # should write a version which just parses the output of mount for IRIX
905 # compatibility, but since this isn't used at all, at the moment I'll leave
906 # this for now
907 #
908 _fs_options()
909 {
910     if [ $# -ne 1 ]
911     then
912         echo "Usage: _fs_options device" 1>&2
913         exit 1
914     fi
915
916     $AWK_PROG -v dev=$1 '
917         match($1,dev) { print $4 }
918     ' </proc/mounts
919 }
920
921 # returns device number if a file is a block device
922 #
923 _is_block_dev()
924 {
925     if [ $# -ne 1 ]
926     then
927         echo "Usage: _is_block_dev dev" 1>&2
928         exit 1
929     fi
930
931     _dev=$1
932     if [ -L "${_dev}" ]; then
933         _dev=`readlink -f ${_dev}`
934     fi
935
936     if [ -b "${_dev}" ]; then
937         src/lstat64 ${_dev} | $AWK_PROG '/Device type:/ { print $9 }'
938     fi
939 }
940
941 # Do a command, log it to $seqres.full, optionally test return status
942 # and die if command fails. If called with one argument _do executes the
943 # command, logs it, and returns its exit status. With two arguments _do
944 # first prints the message passed in the first argument, and then "done"
945 # or "fail" depending on the return status of the command passed in the
946 # second argument. If the command fails and the variable _do_die_on_error
947 # is set to "always" or the two argument form is used and _do_die_on_error
948 # is set to "message_only" _do will print an error message to
949 # $seqres.out and exit.
950
951 _do()
952 {
953     if [ $# -eq 1 ]; then
954         _cmd=$1
955     elif [ $# -eq 2 ]; then
956         _note=$1
957         _cmd=$2
958         echo -n "$_note... "
959     else
960         echo "Usage: _do [note] cmd" 1>&2
961         status=1; exit
962     fi
963
964     (eval "echo '---' \"$_cmd\"") >>$seqres.full
965     (eval "$_cmd") >$tmp._out 2>&1; ret=$?
966     cat $tmp._out | _fix_malloc >>$seqres.full
967     if [ $# -eq 2 ]; then
968         if [ $ret -eq 0 ]; then
969             echo "done"
970         else
971             echo "fail"
972         fi
973     fi
974     if [ $ret -ne 0  ] \
975         && [ "$_do_die_on_error" = "always" \
976             -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
977     then
978         [ $# -ne 2 ] && echo
979         eval "echo \"$_cmd\" failed \(returned $ret\): see $seqres.full"
980         status=1; exit
981     fi
982
983     return $ret
984 }
985
986 # bail out, setting up .notrun file. Need to kill the filesystem check files
987 # here, otherwise they are set incorrectly for the next test.
988 #
989 _notrun()
990 {
991     echo "$*" > $seqres.notrun
992     echo "$seq not run: $*"
993     rm -f ${RESULT_DIR}/require_test
994     rm -f ${RESULT_DIR}/require_scratch
995     status=0
996     exit
997 }
998
999 # just plain bail out
1000 #
1001 _fail()
1002 {
1003     echo "$*" | tee -a $seqres.full
1004     echo "(see $seqres.full for details)"
1005     status=1
1006     exit 1
1007 }
1008
1009 # tests whether $FSTYP is one of the supported filesystems for a test
1010 #
1011 _supported_fs()
1012 {
1013     for f
1014     do
1015         if [ "$f" = "$FSTYP" -o "$f" = "generic" ]
1016         then
1017             return
1018         fi
1019     done
1020
1021     _notrun "not suitable for this filesystem type: $FSTYP"
1022 }
1023
1024
1025 # tests whether $FSTYP is one of the supported OSes for a test
1026 #
1027 _supported_os()
1028 {
1029     for h
1030     do
1031         if [ "$h" = "$HOSTOS" ]
1032         then
1033             return
1034         fi
1035     done
1036
1037     _notrun "not suitable for this OS: $HOSTOS"
1038 }
1039
1040 # this test needs a scratch partition - check we're ok & unmount it
1041 # No post-test check of the device is required. e.g. the test intentionally
1042 # finishes the test with the filesystem in a corrupt state
1043 _require_scratch_nocheck()
1044 {
1045     case "$FSTYP" in
1046         nfs*)
1047                 echo $SCRATCH_DEV | grep -q ":/" > /dev/null 2>&1
1048                 if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
1049                         _notrun "this test requires a valid \$SCRATCH_DEV"
1050                 fi
1051                 if [ ! -d "$SCRATCH_MNT" ]; then
1052                         _notrun "this test requires a valid \$SCRATCH_MNT"
1053                 fi
1054                 ;;
1055         cifs)
1056                 _notrun "requires a scratch device"
1057                 ;;
1058         tmpfs)
1059                 if [ -z "$SCRATCH_DEV" -o ! -d "$SCRATCH_MNT" ];
1060                 then
1061                     _notrun "this test requires a valid \$SCRATCH_MNT and unique $SCRATCH_DEV"
1062                 fi
1063                 ;;
1064         *)
1065                  if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
1066                  then
1067                      _notrun "this test requires a valid \$SCRATCH_DEV"
1068                  fi
1069                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
1070                  then
1071                      _notrun "this test requires a valid \$SCRATCH_DEV"
1072                  fi
1073                 if [ ! -d "$SCRATCH_MNT" ]
1074                 then
1075                      _notrun "this test requires a valid \$SCRATCH_MNT"
1076                 fi
1077                  ;;
1078     esac
1079
1080     # mounted?
1081     if _mount | grep -q $SCRATCH_DEV
1082     then
1083         # if it's mounted, make sure its on $SCRATCH_MNT
1084         if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
1085         then
1086             echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
1087             exit 1
1088         fi
1089         # and then unmount it
1090         if ! $UMOUNT_PROG $SCRATCH_DEV
1091         then
1092             echo "failed to unmount $SCRATCH_DEV"
1093             exit 1
1094         fi
1095     fi
1096     rm -f ${RESULT_DIR}/require_scratch
1097 }
1098
1099 # we need the scratch device and it should be checked post test.
1100 _require_scratch()
1101 {
1102         _require_scratch_nocheck
1103         touch ${RESULT_DIR}/require_scratch
1104 }
1105
1106
1107 # this test needs a test partition - check we're ok & unmount it
1108 #
1109 _require_test()
1110 {
1111     case "$FSTYP" in
1112         nfs*)
1113                 echo $TEST_DEV | grep -q ":/" > /dev/null 2>&1
1114                 if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
1115                         _notrun "this test requires a valid \$TEST_DIR"
1116                 fi
1117                 if [ ! -d "$TEST_DIR" ]; then
1118                         _notrun "this test requires a valid \$TEST_DIR"
1119                 fi
1120                 ;;
1121         cifs)
1122                 echo $TEST_DEV | grep -q "//" > /dev/null 2>&1
1123                 if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
1124                         _notrun "this test requires a valid \$TEST_DEV"
1125                 fi
1126                 if [ ! -d "$TEST_DIR" ]; then
1127                      _notrun "this test requires a valid \$TEST_DIR"
1128                 fi
1129                 ;;
1130         tmpfs)
1131                 if [ -z "$TEST_DEV" -o ! -d "$TEST_DIR" ];
1132                 then
1133                     _notrun "this test requires a valid \$TEST_DIR and unique $TEST_DEV"
1134                 fi
1135                 ;;
1136         *)
1137                  if [ -z "$TEST_DEV" -o "`_is_block_dev $TEST_DEV`" = "" ]
1138                  then
1139                      _notrun "this test requires a valid \$TEST_DEV"
1140                  fi
1141                  if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
1142                  then
1143                      _notrun "this test requires a valid \$TEST_DEV"
1144                  fi
1145                 if [ ! -d "$TEST_DIR" ]
1146                 then
1147                      _notrun "this test requires a valid \$TEST_DIR"
1148                 fi
1149                  ;;
1150     esac
1151
1152     # mounted?
1153     # Note that we use -F here so grep doesn't try to interpret an NFS over
1154     # IPv6 server as a regular expression.
1155     if _mount | grep -F -q $TEST_DEV
1156     then
1157         # if it's mounted, make sure its on $TEST_DIR
1158         if ! _mount | grep -F $TEST_DEV | grep -q $TEST_DIR
1159         then
1160             echo "\$TEST_DEV is mounted but not on \$TEST_DIR - aborting"
1161             exit 1
1162         fi
1163     else
1164         out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
1165         if [ $? -ne 1 ]; then
1166                 echo $out
1167                 exit 1
1168         fi
1169     fi
1170     touch ${RESULT_DIR}/require_test
1171 }
1172
1173 # this test needs a logdev
1174 #
1175 _require_logdev()
1176 {
1177     [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
1178         _notrun "This test requires a valid \$SCRATCH_LOGDEV"
1179     [ "$USE_EXTERNAL" != yes ] && \
1180         _notrun "This test requires USE_EXTERNAL to be enabled"
1181
1182     # ensure its not mounted
1183     $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
1184 }
1185
1186 # this test requires loopback device support
1187 #
1188 _require_loop()
1189 {
1190     if [ "$HOSTOS" != "Linux" ]
1191     then
1192         _notrun "This test requires linux for loopback device support"
1193     fi
1194
1195     modprobe loop >/dev/null 2>&1
1196     if grep loop /proc/devices >/dev/null 2>&1
1197     then
1198         :
1199     else
1200         _notrun "This test requires loopback device support"
1201     fi
1202 }
1203
1204 # this test requires ext2 filesystem support
1205 #
1206 _require_ext2()
1207 {
1208     if [ "$HOSTOS" != "Linux" ]
1209     then
1210         _notrun "This test requires linux for ext2 filesystem support"
1211     fi
1212
1213     modprobe ext2 >/dev/null 2>&1
1214     if grep ext2 /proc/filesystems >/dev/null 2>&1
1215     then
1216         :
1217     else
1218         _notrun "This test requires ext2 filesystem support"
1219     fi
1220 }
1221
1222 # this test requires that (large) loopback device files are not in use
1223 #
1224 _require_no_large_scratch_dev()
1225 {
1226     [ "$LARGE_SCRATCH_DEV" = yes ] && \
1227         _notrun "Large filesystem testing in progress, skipped this test"
1228 }
1229
1230 # this test requires that a realtime subvolume is in use, and
1231 # that the kernel supports realtime as well.
1232 #
1233 _require_realtime()
1234 {
1235     [ "$USE_EXTERNAL" = yes ] || \
1236         _notrun "External volumes not in use, skipped this test"
1237     [ "$SCRATCH_RTDEV" = "" ] && \
1238         _notrun "Realtime device required, skipped this test"
1239 }
1240
1241 # this test requires that a specified command (executable) exists
1242 # $1 - command, $2 - name for error message
1243 #
1244 _require_command()
1245 {
1246     [ -n "$1" ] && _cmd="$1" || _cmd="$2"
1247     [ -n "$1" -a -x "$1" ] || _notrun "$_cmd utility required, skipped this test"
1248 }
1249
1250 # this test requires the device to be valid block device
1251 # $1 - device
1252 _require_block_device()
1253 {
1254         if [ -z "$1" ]; then
1255                 echo "Usage: _require_block_device <dev>" 1>&2
1256                 exit 1
1257         fi
1258         if [ "`_is_block_dev $SCRATCH_DEV`" == "" ]; then
1259                 _notrun "require $1 to be valid block disk"
1260         fi
1261 }
1262
1263 # this test requires the device mapper flakey target
1264 #
1265 _require_dm_flakey()
1266 {
1267     # require SCRATCH_DEV to be a valid block device
1268     _require_block_device $SCRATCH_DEV
1269     _require_command $DMSETUP_PROG
1270
1271     modprobe dm-flakey >/dev/null 2>&1
1272     $DMSETUP_PROG targets | grep flakey >/dev/null 2>&1
1273     if [ $? -eq 0 ]
1274     then
1275         :
1276     else
1277         _notrun "This test requires dm flakey support"
1278     fi
1279 }
1280
1281 # this test requires the projid32bit feature to be available in mkfs.xfs.
1282 #
1283 _require_projid32bit()
1284 {
1285        _scratch_mkfs_xfs_supported -i projid32bit=1 >/dev/null 2>&1 \
1286            || _notrun "mkfs.xfs doesn't have projid32bit feature"
1287 }
1288
1289 _require_projid16bit()
1290 {
1291         _scratch_mkfs_xfs_supported -i projid32bit=0 >/dev/null 2>&1 \
1292            || _notrun "16 bit project IDs not supported on $SCRATCH_DEV"
1293 }
1294
1295 # this test requires the crc feature to be available in mkfs.xfs
1296 #
1297 _require_xfs_mkfs_crc()
1298 {
1299         _scratch_mkfs_xfs_supported -m crc=1 >/dev/null 2>&1 \
1300            || _notrun "mkfs.xfs doesn't have crc feature"
1301 }
1302
1303 # this test requires the xfs kernel support crc feature
1304 #
1305 _require_xfs_crc()
1306 {
1307         _scratch_mkfs_xfs -m crc=1 >/dev/null 2>&1
1308         _scratch_mount >/dev/null 2>&1 \
1309            || _notrun "Kernel doesn't support crc feature"
1310         umount $SCRATCH_MNT
1311 }
1312
1313 # this test requires the bigalloc feature to be available in mkfs.ext4
1314 #
1315 _require_ext4_mkfs_bigalloc()
1316 {
1317         $MKFS_EXT4_PROG -F -O bigalloc -n $SCRATCH_DEV 200k >/dev/null 2>&1 \
1318            || _notrun "mkfs.ext4 doesn't have bigalloc feature"
1319 }
1320
1321 # this test requires the ext4 kernel support bigalloc feature
1322 #
1323 _require_ext4_bigalloc()
1324 {
1325         $MKFS_EXT4_PROG -F -O bigalloc $SCRATCH_DEV 200k >/dev/null 2>&1
1326         _scratch_mount >/dev/null 2>&1 \
1327            || _notrun "Ext4 kernel doesn't support bigalloc feature"
1328         umount $SCRATCH_MNT
1329 }
1330
1331 # this test requires the finobt feature to be available in mkfs.xfs
1332 #
1333 _require_xfs_mkfs_finobt()
1334 {
1335         _scratch_mkfs_xfs_supported -m crc=1,finobt=1 >/dev/null 2>&1 \
1336            || _notrun "mkfs.xfs doesn't have finobt feature"
1337 }
1338
1339 # this test requires the xfs kernel support finobt feature
1340 #
1341 _require_xfs_finobt()
1342 {
1343         _scratch_mkfs_xfs -m crc=1,finobt=1 >/dev/null 2>&1
1344         _scratch_mount >/dev/null 2>&1 \
1345            || _notrun "Kernel doesn't support finobt feature"
1346         umount $SCRATCH_MNT
1347 }
1348
1349 # this test requires xfs sysfs attribute support
1350 #
1351 _require_xfs_sysfs()
1352 {
1353         attr=$1
1354         sysfsdir=/sys/fs/xfs
1355
1356         if [ ! -e $sysfsdir ]; then
1357                 _notrun "no kernel support for XFS sysfs attributes"
1358         fi
1359
1360         if [ ! -z $1 ] && [ ! -e $sysfsdir/$attr ]; then
1361                 _notrun "sysfs attribute '$attr' is not supported"
1362         fi
1363 }
1364
1365 # this test requires that external log/realtime devices are not in use
1366 #
1367 _require_nonexternal()
1368 {
1369     [ "$USE_EXTERNAL" = yes ] && \
1370         _notrun "External device testing in progress, skipped this test"
1371 }
1372
1373 # this test requires that a (specified) aio-dio executable exists
1374 # $1 - command (optional)
1375 #
1376 _require_aiodio()
1377 {
1378     if [ -z "$1" ]
1379     then
1380         AIO_TEST=src/aio-dio-regress/aiodio_sparse2
1381         [ -x $AIO_TEST ] || _notrun "aio-dio utilities required"
1382     else
1383         AIO_TEST=src/aio-dio-regress/$1
1384         [ -x $AIO_TEST ] || _notrun "$AIO_TEST not built"
1385     fi
1386 }
1387
1388 # run an aio-dio program
1389 # $1 - command
1390 _run_aiodio()
1391 {
1392     if [ -z "$1" ]
1393     then
1394         echo "usage: _run_aiodio command_name" 2>&1
1395         status=1; exit 1
1396     fi
1397
1398     _require_aiodio $1
1399
1400     local testtemp=$TEST_DIR/aio-testfile
1401     rm -f $testtemp
1402     $AIO_TEST $testtemp 2>&1
1403     status=$?
1404     rm -f $testtemp
1405
1406     return $status
1407 }
1408
1409 # indicate whether YP/NIS is active or not
1410 #
1411 _yp_active()
1412 {
1413         local dn
1414         dn=$(domainname 2>/dev/null)
1415         test -n "${dn}" -a "${dn}" != "(none)"
1416         echo $?
1417 }
1418
1419 # cat the password file
1420 #
1421 _cat_passwd()
1422 {
1423         [ $(_yp_active) -eq 0 ] && ypcat passwd
1424         cat /etc/passwd
1425 }
1426
1427 # cat the group file
1428 #
1429 _cat_group()
1430 {
1431         [ $(_yp_active) -eq 0 ] && ypcat group
1432         cat /etc/group
1433 }
1434
1435 # check for the fsgqa user on the machine
1436 #
1437 _require_user()
1438 {
1439     qa_user=fsgqa
1440     _cat_passwd | grep -q $qa_user
1441     [ "$?" == "0" ] || _notrun "$qa_user user not defined."
1442     echo /bin/true | su $qa_user
1443     [ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
1444 }
1445
1446 # check for the fsgqa group on the machine
1447 #
1448 _require_group()
1449 {
1450     qa_group=fsgqa
1451     _cat_group | grep -q $qa_group
1452     [ "$?" == "0" ] || _notrun "$qa_group user not defined."
1453 }
1454
1455 _filter_user_do()
1456 {
1457         perl -ne "
1458 s,.*Permission\sdenied.*,Permission denied,;
1459 s,.*no\saccess\sto\stty.*,,;
1460 s,.*no\sjob\scontrol\sin\sthis\sshell.*,,;
1461 s,^\s*$,,;
1462         print;"
1463 }
1464
1465 _user_do()
1466 {
1467     if [ "$HOSTOS" == "IRIX" ]
1468         then
1469         echo $1 | /bin/bash "su $qa_user 2>&1" | _filter_user_do
1470     else
1471         echo $1 | su $qa_user 2>&1 | _filter_user_do
1472     fi
1473 }
1474
1475 _require_xfs_io_command()
1476 {
1477         if [ $# -ne 1 ]
1478         then
1479                 echo "Usage: _require_xfs_io_command command" 1>&2
1480                 exit 1
1481         fi
1482         command=$1
1483
1484         testfile=$TEST_DIR/$$.xfs_io
1485         case $command in
1486         "falloc" )
1487                 testio=`$XFS_IO_PROG -F -f -c "falloc 0 1m" $testfile 2>&1`
1488                 ;;
1489         "fpunch" | "fcollapse" | "zero" | "fzero" )
1490                 testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1491                         -c "$command 4k 8k" $testfile 2>&1`
1492                 ;;
1493         "fiemap")
1494                 testio=`$XFS_IO_PROG -F -f -c "pwrite 0 20k" -c "fsync" \
1495                         -c "fiemap -v" $testfile 2>&1`
1496                 ;;
1497         "flink" )
1498                 testio=`$XFS_IO_PROG -T -F -c "flink $testfile" \
1499                         $TEST_DIR 2>&1`
1500                 echo $testio | egrep -q "invalid option|Is a directory" && \
1501                         _notrun "xfs_io $command support is missing"
1502                 ;;
1503         *)
1504                 testio=`$XFS_IO_PROG -c "$command help" 2>&1`
1505         esac
1506
1507         rm -f $testfile 2>&1 > /dev/null
1508         echo $testio | grep -q "not found" && \
1509                 _notrun "xfs_io $command support is missing"
1510         echo $testio | grep -q "Operation not supported" && \
1511                 _notrun "xfs_io $command failed (old kernel/wrong fs?)"
1512 }
1513
1514 # Check that a fs has enough free space (in 1024b blocks)
1515 #
1516 _require_fs_space()
1517 {
1518         MNT=$1
1519         BLOCKS=$2       # in units of 1024
1520         let GB=$BLOCKS/1024/1024
1521
1522         FREE_BLOCKS=`df -klP $MNT | grep -v Filesystem | awk '{print $4}'`
1523         [ $FREE_BLOCKS -lt $BLOCKS ] && \
1524                 _notrun "This test requires at least ${GB}GB free on $MNT to run"
1525 }
1526
1527 #
1528 # Check if the filesystem supports sparse files.
1529 #
1530 # Unfortunately there is no better way to do this than a manual black list.
1531 #
1532 _require_sparse_files()
1533 {
1534     case $FSTYP in
1535     hfsplus)
1536         _notrun "Sparse files not supported by this filesystem type: $FSTYP"
1537         ;;
1538     *)
1539         ;;
1540     esac
1541 }
1542
1543 _require_debugfs()
1544 {
1545     #boot_params always present in debugfs
1546     [ -d "$DEBUGFS_MNT/boot_params" ] || _notrun "Debugfs not mounted"
1547 }
1548
1549 _require_fail_make_request()
1550 {
1551     [ -f "$DEBUGFS_MNT/fail_make_request/probability" ] \
1552         || _notrun "$DEBUGFS_MNT/fail_make_request \
1553  not found. Seems that CONFIG_FAIL_MAKE_REQUEST kernel config option not enabled"
1554 }
1555
1556 #
1557 # Check if the file system supports seek_data/hole
1558 #
1559 _require_seek_data_hole()
1560 {
1561     testfile=$TEST_DIR/$$.seek
1562     testseek=`$here/src/seek_sanity_test -t $testfile 2>&1`
1563     rm -f $testfile &>/dev/null
1564     echo $testseek | grep -q "Kernel does not support" && \
1565         _notrun "File system does not support llseek(2) SEEK_DATA/HOLE"
1566 }
1567
1568 # check that a FS on a device is mounted
1569 # if so, return mount point
1570 #
1571 _is_mounted()
1572 {
1573     if [ $# -ne 1 ]
1574     then
1575         echo "Usage: _is_mounted device" 1>&2
1576         exit 1
1577     fi
1578
1579     device=$1
1580
1581     if _mount | grep "$device " | $AWK_PROG -v pattern="type $FSTYP" '
1582         pattern        { print $3 ; exit 0 }
1583         END            { exit 1 }
1584     '
1585     then
1586         echo "_is_mounted: $device is not a mounted $FSTYP FS"
1587         exit 1
1588     fi
1589 }
1590
1591 # remount a FS to a new mode (ro or rw)
1592 #
1593 _remount()
1594 {
1595     if [ $# -ne 2 ]
1596     then
1597         echo "Usage: _remount device ro/rw" 1>&2
1598         exit 1
1599     fi
1600     device=$1
1601     mode=$2
1602
1603     if ! mount -o remount,$mode $device
1604     then
1605         echo "_remount: failed to remount filesystem on $device as $mode"
1606         exit 1
1607     fi
1608 }
1609
1610 # Run the appropriate repair/check on a filesystem
1611 #
1612 # if the filesystem is mounted, it's either remounted ro before being
1613 # checked or it's unmounted and then remounted
1614 #
1615
1616 # If set, we remount ro instead of unmounting for fsck
1617 USE_REMOUNT=0
1618
1619 _umount_or_remount_ro()
1620 {
1621     if [ $# -ne 1 ]
1622     then
1623         echo "Usage: _umount_or_remount_ro <device>" 1>&2
1624         exit 1
1625     fi
1626
1627     device=$1
1628     mountpoint=`_is_mounted $device`
1629
1630     if [ $USE_REMOUNT -eq 0 ]; then
1631         $UMOUNT_PROG $device
1632     else
1633         _remount $device ro
1634     fi
1635     echo "$mountpoint"
1636 }
1637
1638 _mount_or_remount_rw()
1639 {
1640     if [ $# -ne 3 ]
1641     then
1642         echo "Usage: _mount_or_remount_rw <opts> <device> <mountpoint>" 1>&2
1643         exit 1
1644     fi
1645     mount_opts=$1
1646     device=$2
1647     mountpoint=$3
1648
1649     if [ $USE_REMOUNT -eq 0 ]
1650     then
1651         if ! _mount -t $FSTYP $mount_opts $device $mountpoint
1652         then
1653             echo "!!! failed to remount $device on $mountpoint"
1654             return 0 # ok=0
1655         fi
1656     else
1657         _remount $device rw
1658     fi
1659
1660     return 1 # ok=1
1661 }
1662
1663 # Check a generic filesystem in no-op mode; this assumes that the
1664 # underlying fsck program accepts "-n" for a no-op (check-only) run,
1665 # and that it will still return an errno for corruption in this mode.
1666 #
1667 # Filesystems which don't support this will need to define their
1668 # own check routine.
1669 #
1670 _check_generic_filesystem()
1671 {
1672     device=$1
1673
1674     # If type is set, we're mounted
1675     type=`_fs_type $device`
1676     ok=1
1677
1678     if [ "$type" = "$FSTYP" ]
1679     then
1680         # mounted ...
1681         mountpoint=`_umount_or_remount_ro $device`
1682     fi
1683
1684     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
1685     if [ $? -ne 0 ]
1686     then
1687         echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1688
1689         echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$seqres.full
1690         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1691         cat $tmp.fsck                           >>$seqres.full
1692         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1693
1694         ok=0
1695     fi
1696     rm -f $tmp.fsck
1697
1698     if [ $ok -eq 0 ]
1699     then
1700         echo "*** mount output ***"             >>$seqres.full
1701         _mount                                  >>$seqres.full
1702         echo "*** end mount output"             >>$seqres.full
1703     elif [ "$type" = "$FSTYP" ]
1704     then
1705         # was mounted ...
1706         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1707         ok=$?
1708     fi
1709
1710     if [ $ok -eq 0 ]; then
1711         status=1
1712         exit 1
1713     fi
1714
1715     return 0
1716 }
1717
1718 # run xfs_check and friends on a FS.
1719
1720 _check_xfs_filesystem()
1721 {
1722     if [ $# -ne 3 ]
1723     then
1724         echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
1725         exit 1
1726     fi
1727
1728     extra_mount_options=""
1729     device=$1
1730     if [ "$2" != "none" ]; then
1731         extra_log_options="-l$2"
1732         extra_mount_options="-ologdev=$2"
1733     fi
1734
1735     if [ "$3" != "none" ]; then
1736         extra_rt_options="-r$3"
1737         extra_mount_options=$extra_mount_options" -ortdev=$3"
1738     fi
1739     extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
1740
1741     [ "$FSTYP" != xfs ] && return 0
1742
1743     type=`_fs_type $device`
1744     ok=1
1745
1746     if [ "$type" = "xfs" ]
1747     then
1748         # mounted ...
1749         mountpoint=`_umount_or_remount_ro $device`
1750     fi
1751
1752     $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
1753                 | tee $tmp.logprint | grep -q "<CLEAN>"
1754     if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
1755     then
1756         echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seqres.full)"
1757
1758         echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$seqres.full
1759         echo "*** xfs_logprint -t output ***"   >>$seqres.full
1760         cat $tmp.logprint                       >>$seqres.full
1761         echo "*** end xfs_logprint output"      >>$seqres.full
1762
1763         ok=0
1764     fi
1765
1766     # xfs_check runs out of memory on large files, so even providing the test
1767     # option (-t) to avoid indexing the free space trees doesn't make it pass on
1768     # large filesystems. Avoid it.
1769     if [ "$LARGE_SCRATCH_DEV" != yes ]; then
1770             _xfs_check $extra_log_options $device 2>&1 |\
1771                  _fix_malloc >$tmp.fs_check
1772     fi
1773     if [ -s $tmp.fs_check ]
1774     then
1775         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seqres.full)"
1776
1777         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1778         echo "*** xfs_check output ***"         >>$seqres.full
1779         cat $tmp.fs_check                       >>$seqres.full
1780         echo "*** end xfs_check output"         >>$seqres.full
1781
1782         ok=0
1783     fi
1784
1785     $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
1786     if [ $? -ne 0 ]
1787     then
1788         echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seqres.full)"
1789
1790         echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1791         echo "*** xfs_repair -n output ***"     >>$seqres.full
1792         cat $tmp.repair | _fix_malloc           >>$seqres.full
1793         echo "*** end xfs_repair output"        >>$seqres.full
1794
1795         ok=0
1796     fi
1797     rm -f $tmp.fs_check $tmp.logprint $tmp.repair
1798
1799     if [ $ok -eq 0 ]
1800     then
1801         echo "*** mount output ***"             >>$seqres.full
1802         _mount                                  >>$seqres.full
1803         echo "*** end mount output"             >>$seqres.full
1804     elif [ "$type" = "xfs" ]
1805     then
1806         _mount_or_remount_rw "$extra_mount_options" $device $mountpoint
1807     fi
1808
1809     if [ $ok -eq 0 ]; then
1810         status=1
1811         if [ "$iam" != "check" ]; then
1812                 exit 1
1813         fi
1814     fi
1815
1816     return 0
1817 }
1818
1819 # Filter the knowen errors the UDF Verifier reports.
1820 _udf_test_known_error_filter()
1821 {
1822         egrep -v "PVD  60  Error: Interchange Level: 1, Maximum Interchange Level: 0|FSD  28  Error: Interchange Level: 1, Maximum Interchange Level: 1,|PVD  72  Warning: Volume Set Identifier: \"\*IRIX UDF\",|Warning: [0-9]+ unused blocks NOT marked as unallocated."
1823
1824 }
1825
1826 _check_udf_filesystem()
1827 {
1828     [ "$DISABLE_UDF_TEST" == "1" ] && return
1829
1830     if [ $# -ne 1 -a $# -ne 2 ]
1831     then
1832         echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
1833         exit 1
1834     fi
1835
1836     if [ ! -x $here/src/udf_test ]
1837     then
1838         echo "udf_test not installed, please download and build the Philips"
1839         echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
1840         echo "Then copy the udf_test binary to $here/src/."
1841         echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
1842         echo "to 1."
1843         return
1844     fi
1845
1846     device=$1
1847     if [ $# -eq 2 ];
1848     then
1849         LAST_BLOCK=`expr \( $2 - 1 \)`
1850         OPT_ARG="-lastvalidblock $LAST_BLOCK"
1851     fi
1852
1853     rm -f $seqres.checkfs
1854     sleep 1 # Due to a problem with time stamps in udf_test
1855     $here/src/udf_test $OPT_ARG $device | tee $seqres.checkfs | egrep "Error|Warning" | \
1856         _udf_test_known_error_filter | \
1857         egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" && \
1858         echo "Warning UDF Verifier reported errors see $seqres.checkfs."
1859 }
1860
1861 _check_xfs_test_fs()
1862 {
1863     TEST_LOG="none"
1864     TEST_RT="none"
1865     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
1866         TEST_LOG="$TEST_LOGDEV"
1867
1868     [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
1869         TEST_RT="$TEST_RTDEV"
1870
1871     _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT
1872
1873     # check for ipath consistency
1874     if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
1875         # errors go to stderr
1876         xfs_check_ipaths $TEST_DIR >/dev/null
1877         xfs_repair_ipaths -n $TEST_DIR >/dev/null
1878     fi
1879 }
1880
1881 _check_btrfs_filesystem()
1882 {
1883     device=$1
1884
1885     # If type is set, we're mounted
1886     type=`_fs_type $device`
1887     ok=1
1888
1889     if [ "$type" = "$FSTYP" ]
1890     then
1891         # mounted ...
1892         mountpoint=`_umount_or_remount_ro $device`
1893     fi
1894
1895     btrfsck $device >$tmp.fsck 2>&1
1896     if [ $? -ne 0 ]
1897     then
1898         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
1899
1900         echo "_check_btrfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
1901         echo "*** fsck.$FSTYP output ***"       >>$seqres.full
1902         cat $tmp.fsck                           >>$seqres.full
1903         echo "*** end fsck.$FSTYP output"       >>$seqres.full
1904
1905         ok=0
1906     fi
1907     rm -f $tmp.fsck
1908
1909     if [ $ok -eq 0 ]
1910     then
1911         echo "*** mount output ***"             >>$seqres.full
1912         _mount                                  >>$seqres.full
1913         echo "*** end mount output"             >>$seqres.full
1914     elif [ "$type" = "$FSTYP" ]
1915     then
1916         # was mounted ...
1917         _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
1918         ok=$?
1919     fi
1920
1921     if [ $ok -eq 0 ]; then
1922         status=1
1923         exit 1
1924     fi
1925
1926     return 0
1927 }
1928
1929 _check_test_fs()
1930 {
1931     case $FSTYP in
1932     xfs)
1933         _check_xfs_test_fs
1934         ;;
1935     nfs)
1936         # no way to check consistency for nfs
1937         ;;
1938     cifs)
1939         # no way to check consistency for cifs
1940         ;;
1941     udf)
1942         # do nothing for now
1943         ;;
1944     btrfs)
1945         _check_btrfs_filesystem $TEST_DEV
1946         ;;
1947     tmpfs)
1948         # no way to check consistency for tmpfs
1949         ;;
1950     *)
1951         _check_generic_filesystem $TEST_DEV
1952         ;;
1953     esac
1954 }
1955
1956 _check_scratch_fs()
1957 {
1958     device=$SCRATCH_DEV
1959     [ $# -eq 1 ] && device=$1
1960
1961     case $FSTYP in
1962     xfs)
1963         SCRATCH_LOG="none"
1964         SCRATCH_RT="none"
1965         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
1966             SCRATCH_LOG="$SCRATCH_LOGDEV"
1967
1968         [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
1969             SCRATCH_RT="$SCRATCH_RTDEV"
1970
1971         _check_xfs_filesystem $device $SCRATCH_LOG $SCRATCH_RT
1972         ;;
1973     udf)
1974         _check_udf_filesystem $device $udf_fsize
1975         ;;
1976     nfs*)
1977         # Don't know how to check an NFS filesystem, yet.
1978         ;;
1979     cifs)
1980         # Don't know how to check a CIFS filesystem, yet.
1981         ;;
1982     btrfs)
1983         _check_btrfs_filesystem $device
1984         ;;
1985     tmpfs)
1986         # no way to check consistency for tmpfs
1987         ;;
1988     *)
1989         _check_generic_filesystem $device
1990         ;;
1991     esac
1992 }
1993
1994 _full_fstyp_details()
1995 {
1996      [ -z "$FSTYP" ] && FSTYP=xfs
1997      if [ $FSTYP = xfs ]; then
1998         if [ -d /proc/fs/xfs ]; then
1999             if grep -q 'debug 0' /proc/fs/xfs/stat; then
2000                 FSTYP="$FSTYP (non-debug)"
2001             elif grep -q 'debug 1' /proc/fs/xfs/stat; then
2002                 FSTYP="$FSTYP (debug)"
2003             fi
2004         else
2005             if uname -a | grep -qi 'debug'; then
2006                 FSTYP="$FSTYP (debug)"
2007             else
2008                 FSTYP="$FSTYP (non-debug)"
2009             fi
2010         fi
2011      fi
2012      echo $FSTYP
2013 }
2014
2015 _full_platform_details()
2016 {
2017      os=`uname -s`
2018      host=`hostname -s`
2019      kernel=`uname -r`
2020      platform=`uname -m`
2021      echo "$os/$platform $host $kernel"
2022 }
2023
2024 _link_out_file()
2025 {
2026         if [ -z "$1" -o -z "$2" ]; then
2027                 echo Error must pass src and dst.
2028                 exit
2029         fi
2030         rm -f $2
2031         if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
2032                 ln -s $1.irix $2
2033         elif [ "`uname`" == "Linux" ]; then
2034                 ln -s $1.linux $2
2035         else
2036                 echo Error test $seq does not run on the operating system: `uname`
2037                 exit
2038         fi
2039 }
2040
2041 _die()
2042 {
2043         echo $@
2044         exit 1
2045 }
2046
2047 #takes files, randomdata
2048 _nfiles()
2049 {
2050         f=0
2051         while [ $f -lt $1 ]
2052         do
2053                 file=f$f
2054                 echo > $file
2055                 if [ $size -gt 0 ]; then
2056                     if [ "$2" == "false" ]; then
2057                         dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
2058                     else
2059                         dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
2060                     fi
2061                 fi
2062                 let f=$f+1
2063         done
2064 }
2065
2066 # takes dirname, depth, randomdata
2067 _descend()
2068 {
2069         dirname=$1; depth=$2; randomdata=$3
2070         mkdir $dirname  || die "mkdir $dirname failed"
2071         cd $dirname
2072
2073         _nfiles $files $randomdata          # files for this dir and data type
2074
2075         [ $depth -eq 0 ] && return
2076         let deep=$depth-1 # go 1 down
2077
2078         [ $verbose = true ] && echo "descending, depth from leaves = $deep"
2079
2080         d=0
2081         while [ $d -lt $dirs ]
2082         do
2083                 _descend d$d $deep &
2084                 let d=$d+1
2085                 wait
2086         done
2087 }
2088
2089 # Populate a filesystem with inodes for performance experiments
2090 #
2091 # usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size] [-x]
2092 #
2093 _populate_fs()
2094 {
2095     here=`pwd`
2096     dirs=5          # ndirs in each subdir till leaves
2097     size=0          # sizeof files in K
2098     files=100       # num files in _each_ subdir
2099     depth=2         # depth of tree from root to leaves
2100     verbose=false
2101     root=root       # path of initial root of directory tree
2102     randomdata=false # -x data type urandom or zero
2103
2104     OPTIND=1
2105     while getopts "d:f:n:r:s:v:x" c
2106     do
2107         case $c in
2108         d)      depth=$OPTARG;;
2109         n)      dirs=$OPTARG;;
2110         f)      files=$OPTARG;;
2111         s)      size=$OPTARG;;
2112         v)      verbose=true;;
2113         r)      root=$OPTARG;;
2114         x)      randomdata=true;;
2115         esac
2116     done
2117
2118     _descend $root $depth $randomdata
2119     wait
2120
2121     cd $here
2122
2123     [ $verbose = true ] && echo done
2124 }
2125
2126 # query whether the given file has the given inode flag set
2127 #
2128 _test_inode_flag()
2129 {
2130     flag=$1
2131     file=$2
2132
2133     if which $XFS_IO_PROG >/dev/null; then
2134         if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
2135             return 0
2136         fi
2137     fi
2138     return 1
2139 }
2140
2141 # query the given files extsize allocator hint in bytes (if any)
2142 #
2143 _test_inode_extsz()
2144 {
2145     file=$1
2146     blocks=""
2147
2148     if which $XFS_IO_PROG >/dev/null; then
2149         blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
2150                 awk '/^xattr.extsize =/ { print $3 }'`
2151     fi
2152     [ -z "$blocks" ] && blocks="0"
2153     echo $blocks
2154 }
2155
2156 # scratch_dev_pool should contain the disks pool for the btrfs raid
2157 _require_scratch_dev_pool()
2158 {
2159         local i
2160         local ndevs
2161
2162         if [ -z "$SCRATCH_DEV_POOL" ]; then
2163                 _notrun "this test requires a valid \$SCRATCH_DEV_POOL"
2164         fi
2165
2166         if [ -z "$1" ]; then
2167                 ndevs=2
2168         else
2169                 ndevs=$1
2170         fi
2171
2172         # btrfs test case needs ndevs or more scratch_dev_pool; other FS not sure
2173         # so fail it
2174         case $FSTYP in
2175         btrfs)
2176                 if [ "`echo $SCRATCH_DEV_POOL|wc -w`" -lt $ndevs ]; then
2177                         _notrun "btrfs and this test needs $ndevs or more disks in SCRATCH_DEV_POOL"
2178                 fi
2179         ;;
2180         *)
2181                 _notrun "dev_pool is not supported by fstype \"$FSTYP\""
2182         ;;
2183         esac
2184
2185         for i in $SCRATCH_DEV_POOL; do
2186                 if [ "`_is_block_dev $i`" = "" ]; then
2187                         _notrun "this test requires valid block disk $i"
2188                 fi
2189                 if [ "`_is_block_dev $i`" = "`_is_block_dev $TEST_DEV`" ]; then
2190                         _notrun "$i is part of TEST_DEV, this test requires unique disks"
2191                 fi
2192                 if _mount | grep -q $i; then
2193                         if ! $UMOUNT_PROG $i; then
2194                             echo "failed to unmount $i - aborting"
2195                             exit 1
2196                         fi
2197                 fi
2198                 # to help better debug when something fails, we remove
2199                 # traces of previous btrfs FS on the dev.
2200                 dd if=/dev/zero of=$i bs=4096 count=100 > /dev/null 2>&1
2201         done
2202 }
2203
2204 # ensure devices in SCRATCH_DEV_POOL are of the same size
2205 # must be called after _require_scratch_dev_pool
2206 _require_scratch_dev_pool_equal_size()
2207 {
2208         local _size
2209         local _newsize
2210         local _dev
2211
2212         # SCRATCH_DEV has been set to the first device in SCRATCH_DEV_POOL
2213         _size=`_get_device_size $SCRATCH_DEV`
2214         for _dev in $SCRATCH_DEV_POOL; do
2215                 _newsize=`_get_device_size $_dev`
2216                 if [ $_size -ne $_newsize ]; then
2217                         _notrun "This test requires devices in SCRATCH_DEV_POOL have the same size"
2218                 fi
2219         done
2220 }
2221
2222 # We will check if the device is deletable
2223 _require_deletable_scratch_dev_pool()
2224 {
2225         local i
2226         local x
2227         for i in $SCRATCH_DEV_POOL; do
2228                 x=`echo $i | cut -d"/" -f 3`
2229                 if [ ! -f /sys/class/block/${x}/device/delete ]; then
2230                         _notrun "$i is a device which is not deletable"
2231                 fi
2232         done
2233 }
2234
2235 # We check for btrfs and (optionally) features of the btrfs command
2236 _require_btrfs()
2237 {
2238         cmd=$1
2239         _require_command $BTRFS_UTIL_PROG btrfs
2240         if [ -z "$1" ]; then
2241                 return 1;
2242         fi
2243         $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
2244         [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
2245 }
2246
2247 # Check that fio is present, and it is able to execute given jobfile
2248 _require_fio()
2249 {
2250         job=$1
2251
2252         _require_command $FIO_PROG
2253         if [ -z "$1" ]; then
2254                 return 1;
2255         fi
2256
2257         $FIO_PROG --warnings-fatal --showcmd $job >> $seqres.full 2>&1
2258         [ $? -eq 0 ] || _notrun "$FIO_PROG too old, see $seqres.full"
2259 }
2260
2261 # Does freeze work on this fs?
2262 _require_freeze()
2263 {
2264         xfs_freeze -f "$TEST_DIR" >/dev/null 2>&1
2265         result=$? 
2266         xfs_freeze -u "$TEST_DIR" >/dev/null 2>&1
2267         [ $result -eq 0 ] || _notrun "$FSTYP does not support freezing"
2268 }
2269
2270 # arg 1 is dev to remove and is output of the below eg.
2271 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2272 _devmgt_remove()
2273 {
2274         local lun=$1
2275         local disk=$2
2276
2277         echo 1 > /sys/class/scsi_device/${lun}/device/delete || _fail "Remove disk failed"
2278
2279         stat $disk > /dev/null 2>&1
2280         while [ $? -eq 0 ]; do
2281                 sleep 1
2282                 stat $disk > /dev/null 2>&1
2283         done
2284 }
2285
2286 # arg 1 is dev to add and is output of the below eg.
2287 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
2288 _devmgt_add()
2289 {
2290         local h
2291         local tdl
2292         # arg 1 will be in h:t:d:l format now in the h and "t d l" format
2293         h=`echo ${1} | cut -d":" -f 1`
2294         tdl=`echo ${1} | cut -d":" -f 2-|sed 's/:/ /g'`
2295
2296         echo ${tdl} >  /sys/class/scsi_host/host${h}/scan || _fail "Add disk failed"
2297
2298         # ensure the device comes online
2299         dev_back_oneline=0
2300         for i in `seq 1 10`; do
2301                 if [ -d /sys/class/scsi_device/${1}/device/block ]; then
2302                         dev=`ls /sys/class/scsi_device/${1}/device/block`
2303                         for j in `seq 1 10`;
2304                         do
2305                                 stat /dev/$dev > /dev/null 2>&1
2306                                 if [ $? -eq 0 ]; then
2307                                         dev_back_oneline=1
2308                                         break
2309                                 fi
2310                                 sleep 1
2311                         done
2312                         break
2313                 else
2314                         sleep 1
2315                 fi
2316         done
2317         if [ $dev_back_oneline -eq 0 ]; then
2318                 echo "/dev/$dev online failed" >> $seqres.full
2319         else
2320                 echo "/dev/$dev is back online" >> $seqres.full
2321         fi
2322 }
2323
2324 _require_fstrim()
2325 {
2326         if [ -z "$FSTRIM_PROG" ]; then
2327                 _notrun "This test requires fstrim utility."
2328         fi
2329 }
2330
2331 _test_batched_discard()
2332 {
2333         if [ $# -ne 1 ]; then
2334                 echo "Usage: _test_batched_discard mnt_point" 1>&2
2335                 exit 1
2336         fi
2337         _require_fstrim
2338         $FSTRIM_PROG ${1} &>/dev/null
2339 }
2340
2341 _require_dumpe2fs()
2342 {
2343         if [ -z "$DUMPE2FS_PROG" ]; then
2344                 _notrun "This test requires dumpe2fs utility."
2345         fi
2346 }
2347
2348 _require_ugid_map()
2349 {
2350         if [ ! -e /proc/self/uid_map ]; then
2351                 _notrun "This test requires procfs uid_map support."
2352         fi
2353         if [ ! -e /proc/self/gid_map ]; then
2354                 _notrun "This test requires procfs gid_map support."
2355         fi
2356 }
2357
2358 _require_cp_reflink()
2359 {
2360        cp --help | grep -q reflink || \
2361                _notrun "This test requires a cp with --reflink support."
2362 }
2363
2364 _require_fssum()
2365 {
2366         FSSUM_PROG=$here/src/fssum
2367         [ -x $FSSUM_PROG ] || _notrun "fssum not built"
2368 }
2369
2370 _require_btrfs_cloner()
2371 {
2372         CLONER_PROG=$here/src/cloner
2373         [ -x $CLONER_PROG ] || \
2374                 _notrun "cloner binary not present at $CLONER_PROG"
2375 }
2376
2377 # Given 2 files, verify that they have the same mapping but different
2378 # inodes - i.e. an undisturbed reflink
2379 # Silent if so, make noise if not
2380 _verify_reflink()
2381 {
2382        # not a hard link or symlink?
2383        cmp -s  <(stat -c '%i' $1) <(stat -c '%i' $2) \
2384                && echo "$1 and $2 are not reflinks: same inode number"
2385
2386        # same mapping?
2387        diff -u <($XFS_IO_PROG -c "fiemap" $1 | grep -v $1) \
2388                <($XFS_IO_PROG -c "fiemap" $2 | grep -v $2) \
2389                || echo "$1 and $2 are not reflinks: different extents"
2390 }
2391
2392 _require_atime()
2393 {
2394         if [ "$FSTYP" == "nfs" ]; then
2395                 _notrun "atime related mount options have no effect on NFS"
2396         fi
2397 }
2398
2399 _require_relatime()
2400 {
2401         _scratch_mkfs > /dev/null 2>&1
2402         _scratch_mount -o relatime || \
2403                 _notrun "relatime not supported by the current kernel"
2404         _scratch_unmount
2405 }
2406
2407 _require_userns()
2408 {
2409         [ -x src/nsexec ] || _notrun "src/nsexec executable not found"
2410         src/nsexec -U true 2>/dev/null || _notrun "userns not supported by this kernel"
2411 }
2412
2413 _create_loop_device()
2414 {
2415         file=$1
2416         dev=`losetup -f --show $file` || _fail "Cannot assign $file to a loop device"
2417         echo $dev
2418 }
2419
2420 _destroy_loop_device()
2421 {
2422         dev=$1
2423         losetup -d $dev || _fail "Cannot destroy loop device $dev"
2424 }
2425
2426 _scale_fsstress_args()
2427 {
2428     args=""
2429     while [ $# -gt 0 ]; do
2430         case "$1" in
2431             -n) args="$args $1 $(($2 * $TIME_FACTOR))"; shift ;;
2432             -p) args="$args $1 $(($2 * $LOAD_FACTOR))"; shift ;;
2433             *) args="$args $1" ;;
2434         esac
2435         shift
2436     done
2437     echo $args
2438 }
2439
2440 #
2441 # Return the logical block size if running on a block device,
2442 # else substitute the page size.
2443 #
2444 _min_dio_alignment()
2445 {
2446     dev=$1
2447
2448     if [ -b "$dev" ]; then
2449         blockdev --getss $dev
2450     else
2451         $here/src/feature -s
2452     fi
2453 }
2454
2455 run_check()
2456 {
2457         echo "# $@" >> $seqres.full 2>&1
2458         "$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
2459 }
2460
2461 _run_btrfs_util_prog()
2462 {
2463         run_check $BTRFS_UTIL_PROG $*
2464 }
2465
2466 _require_btrfs_send_stream_version()
2467 {
2468         $BTRFS_UTIL_PROG send 2>&1 | \
2469                 grep '^[ \t]*\-\-stream\-version <version>' > /dev/null 2>&1
2470         if [ $? -ne 0 ]; then
2471                 _notrun "Missing btrfs-progs send --stream-version command line option, skipped this test"
2472         fi
2473
2474         # test if btrfs kernel supports send stream version 2
2475         if [ ! -f /sys/fs/btrfs/send/stream_version ]; then
2476                 _notrun "Missing btrfs kernel patch for send stream version 2, skipped this test"
2477         fi
2478 }
2479
2480 _require_btrfs_mkfs_feature()
2481 {
2482         if [ -z $1 ]; then
2483                 echo "Missing feature name argument for _require_btrfs_mkfs_feature"
2484                 exit 1
2485         fi
2486         feat=$1
2487         $MKFS_BTRFS_PROG -O list-all 2>&1 | \
2488                 grep '^[ \t]*'"$feat"'\b' > /dev/null 2>&1
2489         [ $? -eq 0 ] || \
2490                 _notrun "Feature $feat not supported in the available version of mkfs.btrfs"
2491 }
2492
2493 _require_btrfs_fs_feature()
2494 {
2495         if [ -z $1 ]; then
2496                 echo "Missing feature name argument for _require_btrfs_fs_feature"
2497                 exit 1
2498         fi
2499         feat=$1
2500         modprobe btrfs > /dev/null 2>&1
2501         [ -e /sys/fs/btrfs/features/$feat ] || \
2502                 _notrun "Feature $feat not supported by the available btrfs version"
2503 }
2504
2505 _require_test_symlinks()
2506 {
2507         # IRIX UDF does not support symlinks
2508         [ "$HOSTOS" = "IRIX" -a "$FSTYP" = 'udf' ] && \
2509                 _notrun "Require symlinks support"
2510         target=`mktemp -p $TEST_DIR`
2511         link=`mktemp -p $TEST_DIR -u`
2512         ln -s `basename $target` $link
2513         if [ "$?" -ne 0 ]; then
2514                 rm -f $target
2515                 _notrun "Require symlinks support"
2516         fi
2517         rm -f $target $link
2518 }
2519
2520 _require_test_fcntl_advisory_locks()
2521 {
2522         [ "$FSTYP" != "cifs" ] && return 0
2523         cat /proc/mounts | grep $TEST_DEV | grep cifs | grep -q "nobrl" && return 0
2524         cat /proc/mounts | grep $TEST_DEV | grep cifs | grep -qE "nounix|forcemand" && \
2525                 _notrun "Require fcntl advisory locks support"
2526 }
2527
2528 _get_total_inode()
2529 {
2530         if [ -z "$1" ]; then
2531                 echo "Usage: _get_total_inode <mnt>"
2532                 exit 1
2533         fi
2534         local nr_inode;
2535         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $3}'`
2536         echo $nr_inode
2537 }
2538
2539 _get_used_inode()
2540 {
2541         if [ -z "$1" ]; then
2542                 echo "Usage: _get_used_inode <mnt>"
2543                 exit 1
2544         fi
2545         local nr_inode;
2546         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $4}'`
2547         echo $nr_inode
2548 }
2549
2550 _get_free_inode()
2551 {
2552         if [ -z "$1" ]; then
2553                 echo "Usage: _get_free_inode <mnt>"
2554                 exit 1
2555         fi
2556         local nr_inode;
2557         nr_inode=`$DF_PROG -i $1 | tail -1 | awk '{print $5}'`
2558         echo $nr_inode
2559 }
2560
2561 # get btrfs profile configs being tested
2562 #
2563 # A set of pre-set profile configs are exported via _btrfs_profile_configs
2564 # array. Default configs can be overridden by setting BTRFS_PROFILE_CONFIGS
2565 # var in the format "metadata_profile:data_profile", multiple configs can be
2566 # seperated by space, e.g.
2567 # export BTRFS_PROFILE_CONFIGS="raid0:raid0 raid1:raid1 dup:single"
2568 _btrfs_get_profile_configs()
2569 {
2570         if [ "$FSTYP" != "btrfs" ]; then
2571                 return
2572         fi
2573
2574         # no user specified btrfs profile configs, export the default configs
2575         if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
2576                 # default configs
2577                 _btrfs_profile_configs=(
2578                         "-m single -d single"
2579                         "-m dup -d single"
2580                         "-m raid0 -d raid0"
2581                         "-m raid1 -d raid0"
2582                         "-m raid1 -d raid1"
2583                         "-m raid10 -d raid10"
2584                         "-m raid5 -d raid5"
2585                         "-m raid6 -d raid6"
2586                 )
2587
2588                 # remove dup/raid5/raid6 profiles if we're doing device replace
2589                 # dup profile indicates only one device being used (SCRATCH_DEV),
2590                 # but we don't want to replace SCRATCH_DEV, which will be used in
2591                 # _scratch_mount/_check_scratch_fs etc.
2592                 # and raid5/raid6 doesn't support replace yet
2593                 if [ "$1" == "replace" ]; then
2594                         _btrfs_profile_configs=(
2595                                 "-m single -d single"
2596                                 "-m raid0 -d raid0"
2597                                 "-m raid1 -d raid0"
2598                                 "-m raid1 -d raid1"
2599                                 "-m raid10 -d raid10"
2600                                 # add these back when raid5/6 is working with replace
2601                                 #"-m raid5 -d raid5"
2602                                 #"-m raid6 -d raid6"
2603                         )
2604                 fi
2605                 export _btrfs_profile_configs
2606                 return
2607         fi
2608
2609         # parse user specified btrfs profile configs
2610         local i=0
2611         local cfg=""
2612         for cfg in $BTRFS_PROFILE_CONFIGS; do
2613                 # turn "metadata:data" format to "-m metadata -d data"
2614                 # and assign it to _btrfs_profile_configs array
2615                 cfg=`echo "$cfg" | sed -e 's/^/-m /' -e 's/:/ -d /'`
2616                 _btrfs_profile_configs[$i]="$cfg"
2617                 let i=i+1
2618         done
2619
2620         if [ "$1" == "replace" ]; then
2621                 if echo ${_btrfs_profile_configs[*]} | grep -q raid[56]; then
2622                         _notrun "RAID5/6 doesn't support btrfs device replace yet"
2623                 fi
2624                 if echo ${_btrfs_profile_configs[*]} | grep -q dup; then
2625                         _notrun "Do not set dup profile in btrfs device replace test"
2626                 fi
2627         fi
2628         export _btrfs_profile_configs
2629 }
2630
2631 # stress btrfs by running balance operation in a loop
2632 _btrfs_stress_balance()
2633 {
2634         local btrfs_mnt=$1
2635         while true; do
2636                 $BTRFS_UTIL_PROG balance start $btrfs_mnt
2637         done
2638 }
2639
2640 # stress btrfs by creating/mounting/umounting/deleting subvolume in a loop
2641 _btrfs_stress_subvolume()
2642 {
2643         local btrfs_dev=$1
2644         local btrfs_mnt=$2
2645         local subvol_name=$3
2646         local subvol_mnt=$4
2647
2648         mkdir -p $subvol_mnt
2649         while true; do
2650                 $BTRFS_UTIL_PROG subvolume create $btrfs_mnt/$subvol_name
2651                 $MOUNT_PROG -o subvol=$subvol_name $btrfs_dev $subvol_mnt
2652                 $UMOUNT_PROG $subvol_mnt
2653                 $BTRFS_UTIL_PROG subvolume delete $btrfs_mnt/$subvol_name
2654         done
2655 }
2656
2657 # stress btrfs by running scrub in a loop
2658 _btrfs_stress_scrub()
2659 {
2660         local btrfs_mnt=$1
2661         while true; do
2662                 $BTRFS_UTIL_PROG scrub start -B $btrfs_mnt
2663         done
2664 }
2665
2666 # stress btrfs by defragmenting every file/dir in a loop and compress file
2667 # contents while defragmenting if second argument is not "nocompress"
2668 _btrfs_stress_defrag()
2669 {
2670         local btrfs_mnt=$1
2671         local compress=$2
2672
2673         while true; do
2674                 if [ "$compress" == "nocompress" ]; then
2675                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2676                         $BTRFS_UTIL_PROG filesystem defrag {} \;
2677                 else
2678                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2679                         $BTRFS_UTIL_PROG filesystem defrag -clzo {} \;
2680                         find $btrfs_mnt \( -type f -o -type d \) -exec \
2681                         $BTRFS_UTIL_PROG filesystem defrag -czlib {} \;
2682                 fi
2683         done
2684 }
2685
2686 # stress btrfs by remounting it with different compression algorithms in a loop
2687 # run this with fsstress running at background could exercise the compression
2688 # code path and ensure no race when switching compression algorithm with constant
2689 # I/O activity.
2690 _btrfs_stress_remount_compress()
2691 {
2692         local btrfs_mnt=$1
2693         while true; do
2694                 for algo in no zlib lzo; do
2695                         $MOUNT_PROG -o remount,compress=$algo $btrfs_mnt
2696                 done
2697         done
2698 }
2699
2700 # stress btrfs by replacing devices in a loop
2701 # Note that at least 3 devices are needed in SCRATCH_DEV_POOL and the last
2702 # device should be free(not used by btrfs)
2703 _btrfs_stress_replace()
2704 {
2705         local btrfs_mnt=$1
2706
2707         # The device number in SCRATCH_DEV_POOL should be at least 3,
2708         # one is SCRATCH_DEV, one is to be replaced, one is free device
2709         # we won't replace SCRATCH_DEV, see below for reason
2710         if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt 3 ]; then
2711                 echo "_btrfs_stress_replace requires at least 3 devices in SCRATCH_DEV_POOL"
2712                 return
2713         fi
2714
2715         # take the last device as the first free_dev
2716         local free_dev="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $NF}'`"
2717
2718         # free_dev should be really free
2719         if $BTRFS_UTIL_PROG filesystem show $btrfs_mnt | grep -q "$free_dev"; then
2720                 echo "_btrfs_stress_replace: $free_dev is used by btrfs"
2721                 return
2722         fi
2723
2724         # dev_pool is device list being currently used by btrfs (excluding SCRATCH_DEV)
2725         # and can be replaced. We don't replace SCRATCH_DEV because it will be used in
2726         # _scratch_mount and _check_scratch_fs etc.
2727         local dev_pool=`echo $SCRATCH_DEV_POOL | sed -e "s# *$SCRATCH_DEV *##" \
2728                         -e "s# *$free_dev *##"`
2729
2730         # set the first device in dev_pool as the first src_dev to be replaced
2731         local src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
2732
2733         echo "dev_pool=$dev_pool"
2734         echo "free_dev=$free_dev, src_dev=$src_dev"
2735         while true; do
2736                 echo "Replacing $src_dev with $free_dev"
2737                 $BTRFS_UTIL_PROG replace start -fB $src_dev $free_dev $btrfs_mnt
2738                 if [ $? -ne 0 ]; then
2739                         # don't update src_dev and free_dev if replace failed
2740                         continue
2741                 fi
2742                 dev_pool="$dev_pool $free_dev"
2743                 dev_pool=`echo $dev_pool | sed -e "s# *$src_dev *##"`
2744                 free_dev=$src_dev
2745                 src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
2746         done
2747 }
2748
2749 # return device size in kb
2750 _get_device_size()
2751 {
2752         grep `_short_dev $1` /proc/partitions | awk '{print $3}'
2753 }
2754
2755 init_rc()
2756 {
2757         if [ "$iam" == new ]
2758         then
2759                 return
2760         fi
2761         # make some further configuration checks here
2762         if [ "$TEST_DEV" = ""  ]
2763         then
2764                 echo "common/rc: Error: \$TEST_DEV is not set"
2765                 exit 1
2766         fi
2767
2768         # if $TEST_DEV is not mounted, mount it now as XFS
2769         if [ -z "`_fs_type $TEST_DEV`" ]
2770         then
2771                 # $TEST_DEV is not mounted
2772                 if ! _test_mount
2773                 then
2774                         echo "common/rc: retrying test device mount with external set"
2775                         [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
2776                         if ! _test_mount
2777                         then
2778                                 echo "common/rc: could not mount $TEST_DEV on $TEST_DIR"
2779                                 exit 1
2780                         fi
2781                 fi
2782         fi
2783
2784         if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
2785         then
2786                 echo "common/rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
2787                 $DF_PROG $TEST_DEV
2788                 exit 1
2789         fi
2790         # Figure out if we need to add -F ("foreign", deprecated) option to xfs_io
2791         xfs_io -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \
2792         export XFS_IO_PROG="$XFS_IO_PROG -F"
2793 }
2794
2795 # get real device path name by following link
2796 _real_dev()
2797 {
2798         local _dev=$1
2799         if [ -b "$_dev" ] && [ -L "$_dev" ]; then
2800                 _dev=`readlink -f "$_dev"`
2801         fi
2802         echo $_dev
2803 }
2804
2805 # basename of a device
2806 _short_dev()
2807 {
2808         echo `basename $(_real_dev $1)`
2809 }
2810
2811 get_block_size()
2812 {
2813         if [ -z $1 ] || [ ! -d $1 ]; then
2814                 echo "Missing mount point argument for get_block_size"
2815                 exit 1
2816         fi
2817         echo `stat -f -c %S $1`
2818 }
2819
2820 init_rc
2821
2822 ################################################################################
2823 # make sure this script returns success
2824 /bin/true